*gBdZddlZddlZddlmZddlmZmZmZmZm Z m Z ddlm Z m Z m Z mZmZmZddlmZmZmZddlmZmZmZddlmZmZmZdd lmZmZejee fZ!Gd d Z"d ej#d e$fdZ%d ej#fdZ&GddZ'Gddej(Z)Gdde)Z*Gdde)Z+dS)z%Code related to SMS Encoding/DecodingN)bidict)Int8ubByteBytesBitFlag BitsInteger)StructEnumTell BitStructthisPadding)Prefixed GreedyRange GreedyBytes) HexAdapter BcdAdapterTonNpi)Hexstrh2bb2h) pdu_types operationsc eZdZedez dez deejz Zede ee ez de z Z gfdZ defdZdedefd Zed edejdeffd Zdefd Zd S)UserDataHeaderieilengthvalueiesdatac||_dSNr )selfr s 7/home/jenkins/workspace/simtester-sanitize/pySim/sms.py__init__zUserDataHeader.__init__(s returncd|jzS)NzUDH(%r)r$r%s r&__repr__zUserDataHeader.__repr__+s48##r(c8|jD]}|d|krdSdS)NrTFr$)r%ries r&has_iezUserDataHeader.has_ie.s3(  B%yCtt ur(inbct|trt|}|j|}||d|dfS)Nr r! isinstancestrr _constructparse)clsr0ress r& from_byteszUserDataHeader.from_bytes4sO c3   c((Cn""3''s3u:F ++r(cF|j|jddS)Nr()r r!)r5buildr r+s r&to_byteszUserDataHeader.to_bytes;s"$$DHS%A%ABBBr(N)__name__ __module__ __qualname__r rrrrie_crrrr5r'r4r,intboolr/ classmethod BytesOrHextypingTuplebytesr9r<r(r&rr!s) 6%,t{9K9K1K L LDhhv{{4/@/@AAA{*,,J$#$$$$ ,Z,FL9I59P,Q,,,[, C%CCCCCCr(rdcsr)cb|tjtjjtjjkrdS|tjtjjtjjkrdS|jtjjkr"|j dtj j krdSdS)z?Determine if the given SMPP data coding scheme is 8-bit or not.T msgCodingF) r DataCodingDataCodingSchemeDEFAULTDataCodingDefaultOCTET_UNSPECIFIEDOCTET_UNSPECIFIED_COMMONschemeGSM_MESSAGE_CLASS schemeDataDataCodingGsmMsgCoding DATA_8BITrIs r&smpp_dcs_is_8bitrX?s i"9#=#E#,#>#PRRRRt i"9#=#E#,#>#WYYYYt zY/AAAcnU`FaenfFfPGPGPtur(cBt|stddS)z5Assert if given SMPP data coding scheme is not 8-bit.z&We only support 8bit coded SMS for nowN)rX ValueErrorrWs r&ensure_smpp_is_8bitr[Ns. C CABBBCCr(c PeZdZdZedez dez deee j dze j dzzz de z Z e ddd d d d d ddZe ddd dddddZddZdZededejdeffdZed dZdZdefdZdS)! AddressFieldz8Representation of an address field as used in SMS T-PDU.addr_len type_of_addrdigitstellunknown isdn_e164 data_x121 telex_f69 sc_specific6nationalprivateermes)UNKNOWNISDNDATATELEX LAND_MOBILENATIONALPRIVATEERMES internationalnetwork_specific short_code alphanumeric abbreviated)rk INTERNATIONALrpNETWORK_SPECIFICSUBSCRIBER_NUMBER ALPHANUMERIC ABBREVIATEDc0||_||_||_dSr#tonnpir`)r%r`rrs r&r'zAddressField.__init__ns r(c8d|jd|jd|jdS)NzAddressField(TON=z, NPI=z, )r~r+s r&__str__zAddressField.__str__ss#59XXXtxxxUUr(r0r)ct|trt|}|j|}|dd}|dd}||dd|d||||ddfS)zHConstruct an AddressField instance from the binary T-PDU address format.r_type_of_numbernumbering_plan_idr`Nr^rbr2)r7r0r8rrs r&r9zAddressField.from_bytesvs c3   c((Cn""3''.!"23.!"56s3x=!1#j/!12C==s3v;<  KK3 KK""&(%)X k  $$Q'''r(N)rcrc)r)r])r=r>r?__doc__r rrrrrr^r r5rrrr'rrCrDrErFrGr9rrr<rHr(r&r]r]SsBB 6)&v- EE$-2BT]ST_2T,U,U!V!VVt %%J6%     L6(.)&$    L VVV QZ QFL9N,O Q Q Q[ Qsss[s ggg(%((((((r(r]ceZdZdZdZdS)SMS_TPDUzBase class for a SMS T-PDU.c |dd|_|dd|_|dd|_|dd|_|dd|_|dd|_|dd|_dS) Ntp_mtitp_rpFtp_udhitp_pidtp_dcstp_udltp_ud)getrrrrrrr)r%kwargss r&r'zSMS_TPDU.__init__sjj400 ZZ// zz)U33 jj400 jj400 jj400 ZZ.. r(N)r=r>r?rr'rHr(r&rrs)%%/////r(rc eZdZdZedez dez dez dez eddez dedz Zfd Z d Z e d e d dfd Z d efdZe ddZe ddZxZS) SMS_DELIVERzYRepresentation of a SMS-DELIVER T-PDU. This is the Network to MS/UE (downlink) direction.rrtp_sritp_mmsrrac Rd|d<tjd i||dd|_|dd|_|dd|_|dd|_|dd|_dS) Nrrtp_lpFrtp_oatp_sctsrrH)superr'rrrrrrr%r __class__s r&r'zSMS_DELIVER.__init__sx""6"""ZZ// jj511 ZZ.. zz)T22 jj511 r(cd|jj|j|j|j|j|j|j|j|j |j |j |j |j f zS)Nza%s(MTI=%s, MMS=%s, LP=%s, RP=%s, UDHI=%s, SRI=%s, OA=%s, PID=%2x, DCS=%x, SCTS=%s, UDL=%u, UD=%s))rr=rrrrrrrrrrrrr+s r&r,zSMS_DELIVER.__repr__srvzwEwNPTP[]a]hjnjtvzv@BFBNPTP[]a]gimitvzvACGCOQUQ\^b^hvii ir(r0r)ct|trt|}tj|}t |dd\}}||d<d}|||d<|dz }|||d<|dz }|||dz|d<|dz }|||d <|dz }||d|d <|d i|S) zQConstruct a SMS_DELIVER instance from the binary encoded format as used in T-PDU.rNrrrrrrrrH)r3r4rrflags_constructr6r]r9)r7r0roa remainderoffsets r&r9zSMS_DELIVER.from_bytess c3   c((C  ' - -c 2 2$//ABB88 I' '( ! '( !  q1) ! '( ! vww'' sxxQxxr(c4t}|j|j|j|j|j|jd}tj |}| || |j | |j| |j| |j| |j| |j|S)zLEncode a SMS_DELIVER instance to the binary encoded format as used in T-PDU.)rrrrrr) bytearrayrrrrrrrrr;extendrr<appendrrrrrr%outbrflagss r&r<zSMS_DELIVER.to_bytess{{kT[4:ZDLDK  +11!44 E DJ''))*** DK    DK    DL!!! DK    DJ r(c|jtjjkr||St d|jz)zJConstruct a SMS_DELIVER instance from the deliver format used by smpp.pdu.Unsupported SMPP commandId %sidr CommandId submit_smfrom_smpp_submitrZr7smpp_pdus r&rzSMS_DELIVER.from_smppA ;)-7 7 7''11 1r?rr rrr rr'r,rCrDr9rGr<rr __classcell__rs@r&rrs*cci ingdlHUYM ' HTM8KKPQNN;RTTO22222iiiZM[(%$LLL[L[r(rc eZdZdZedez dez dez deeddddd z d ez d edz Zfd Z dZ e de ddfdZ defdZe ddZe ddZdejfdZxZS) SMS_SUBMITzVRepresentation of a SMS-SUBMIT T-PDU. This is the MS/UE -> network (uplink) direction.tp_srrrrtp_vpfrarr)nonerelativeenhancedabsolutetp_rdrc d|d<tjd i||dd|_|dd|_|dd|_|dd|_|d d|_|d d|_dS) NrrrFrrrtp_mrtp_datp_vprH) rr'rrrrrrrrs r&r'zSMS_SUBMIT.__init__sx""6"""ZZ// jj622 jj511 ZZ.. ZZ.. ZZ.. r(cd|jj|j|j|j|j|j|j|j|j |j |j |j |j f zS)Nz_%s(MTI=%s, RD=%s, VPF=%u, RP=%s, UDHI=%s, SRR=%s, DA=%s, PID=%2x, DCS=%x, VP=%s, UDL=%u, UD=%s))rr=rrrrrrrrrrrrr+s r&r,zSMS_SUBMIT.__repr__sptxuCuLNRNY[_[egkgrtxt~@D@LNRNY[_[egkgrtxtAEAKMQMXZ^Zdtee er(r0r)cd}t|trt|}tj|}|dz }|||d<|dz }t |dd\}}||d<d}|||d<|dz }|||d<|dz }|d d krn~|d d kr|||dz|d <|dz }n\|d d kr|||dz|d <|dz }n:|d dkr|||dz|d <|dz }ntd|d z|||d<|dz }||d|d<|di|S)zPConstruct a SMS_SUBMIT instance from the binary encoded format as used in T-PDU.rrrraNrrrrrrrrrrzInvalid VPF: %srrrH) r3r4rrrr6r]r9rZ)r7r0rrdars r&r9zSMS_SUBMIT.from_bytess c3   c((C  & , ,S 1 1! K' ! $//ABB88 I' '( ! '( !  X;&  x[J & &"6&(?3AgJ aKFF x[J & &"6&(?3AgJ aKFF x[J & &"6&(?3AgJ aKFF.8<== ='( ! vww'' sxxQxxr(c~t}|j|j|j|j|j|jd}tj |}| || |j | |j | |j| |j|jdkr| |j| |j| |j|S)zKEncode a SMS_SUBMIT instance to the binary encoded format as used in T-PDU.)rrrrrrr)rrrrrrrrrr;rrrrr<rrrrrrs r&r<zSMS_SUBMIT.to_bytes;s{{kDJ$+ZDLDK  *0033 E DJ DJ''))*** DK    DK   ;& KK # # # DK    DJ r(c|jtjjkr||St d|jz)zAConstruct a SMS_SUBMIT instance from the format used by smpp.pdu.rrrs r&rzSMS_SUBMIT.from_smppNrr(c t|jdt|jd|jd|jd}|jd}|jdjdkrdnd d d t jj|jd jv|jd rdnd d ||jddd t||d }|di|S)zHConstruct a SMS_SUBMIT instance from the submit format used by smpp.pdu.rdestination_addr dest_addr_ton dest_addr_npirreplace_if_present_flagREPLACETFNrregistered_deliveryrrr) rrrrrrrrrrrrrH) r[rr]rrrrrrr)r7rrrrs r&rzSMS_SUBMIT.from_smpp_submitVs HOM:;;;&&x7I'J'/'G'/'GII0 &_-FGLPYYYTT_d 4G8?[fKgKss&o.CDOdd%om4%jj  sxxQxxr(ctjtjjtjj}tjtjj}|jrtj j }ntj j }|j dkrtdtjtjjtjj}|j\}}}t)jdtjjtjjd|||||jtjj||||jS)zKTranslate a SMS_SUBMIT instance to a smpp.pdu.operations.SubmitSM instance.rz1Unsupported DCS: We only support DCS=0xF6 for nowsimcard) service_typerrrrrrrr priority_flagrrrr)rEsmClass EsmClassModerN EsmClassTypeRegisteredDeliveryRegisteredDeliveryReceipt"NO_SMSC_DELIVERY_RECEIPT_REQUESTEDrReplaceIfPresentFlagrDO_NOT_REPLACErrZrLrMrOrPrrrSubmitSMAddrTonr{AddrNpirkr PriorityFlagLEVEL_0r)r%rreg_delrepl_ifdcdaddrrrs r&rzSMS_SUBMIT.to_smppqs&y'='EyG]Geff .y/R/uvv : D4r?rr rr r rr'r,rCrDr9rGr<rrrPDUrrrs@r&rrs]``i y~wt| (kk!nn1q[\gh)i)i)i i ' h{{1~~.EGGO/////eee$J$<$$$[$L%&LLL[L[4=========r(r),rrEabcr constructrrrrrr r r r r rrrrrosmocom.constructrrr osmocom.utilsrrrsmpp.pdurrUnionrGrDrrLrBrXr[r]ABCrrrrHr(r&rsY++& AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB8888888888<<<<<<<<<<****************** \&%- ( CCCCCCCC< ). 4    CY1CCCC G(G(G(G(G(G(G(G(T / / / / /sw / / /TTTTT(TTTpL=L=L=L=L=L=L=L=L=L=r(