hddlZddlZddlZddlZddlmZddlZddlm Z ddl m Z ddl m Z ddlmZmZGdde ZdS) N)Optional)Hexstr)ResTuple) LinkBaseTpdu) ReaderError ProtocolErrorceZdZdZdZejddfdejffd ZdZdd Z d Z d Z dZ de fdZdZddeedefdZde defdZdefdZedejfdZxZS)ModemATCommandLinkz3Transport Link for 3GPP TS 27.007 compliant modems.z-modem for Generic SIM Access (3GPP TS 27.007)z /dev/ttyUSB0) modem_dev modem_baudoptsc |j}|j}tjdi|t j||d|_d|_||_d|_ | | dS)N)timeoutF) r r super__init__serialSerial_sl_echo_device_atr _check_echo reset_card)selfrkwargsdevicebaudrate __class__s I/home/jenkins/workspace/simtester-sanitize/pySim/transport/modem_atcmd.pyrzModemATCommandLink.__init__'s?""6"""=1===     c\t|dr|jdSdS)Nr)hasattrrclosers r"__del__zModemATCommandLink.__del__7s6 4    HNN       r#皙?Mb`?cnt|tr|n|}|dz }|jt jd| |j|}|t|ksJn%#t$r}td|z|d}~wwxYwd}d}tj } ||j |jj z}|d} t| dkrU| d } | d krt jd | n{| d ks| d rt jd| nJtj | z |krt jd|ntj||dz }t jdtj | z |||jr ||d}|}|d}t jd||S)N zSending AT command: %szFailed to send AT command: %sr#Ts OKz Command finished with result: %ssERROR +CME ERROR:zCommand failed with result: %sz$Command finished with timeout >= %ssz%Command took %0.6fs (%d cycles a %fs)s zGot response from modem: %s) isinstancebytesencoderreset_input_bufferlogdebugwritelen Exceptionrtimeread in_waitingsplit startswitherrorinfosleeprstrip) rcmdrpatiencebcmdwlenexcrspitst_startlinesress r" send_at_cmdzModemATCommandLink.send_at_cmd;s/ e,,>ss#**,,   ##%%% *C000 N8>>$''D3t99$$$$$ N N N=CDD# M N)++  dh&9:::CIIg&&E5zzQBi%<<I@#FFF(??cnn^&D&D?I>DDDy{{W$//?III Jx 1HC! " 949;;;PRUW_``` : dee*Ciikkii $$ /555 s /B B2B--B2cd|_|d}t|dkr*|ddkr d|_dS|ddkr d|_dStd |jz) aVerify the correct response to 'AT' command and detect if inputs are echoed by the device Although echo of inputs can be enabled/disabled via ATE1/ATE0, respectively, we rather detect the current configuration of the modem without any change. FATrr0NsAT OKTz/Interface '%s' does not respond to 'AT' command)rrNr9rr)rresults r"rzModemATCommandLink._check_echojs} !!$'' v;;??bzU""" bz]**! ORVR^^___r#c|ddgkrtd|ddgkrtdtjd|jdS)NATZr0zFailed to reset the modemz AT+CSIM=?z-The modem does not seem to support SIM accesszModem at '%s' is ready!)rNrr6rArr's r" _reset_cardzModemATCommandLink._reset_cardss   E " "ug - -9:: :   K ( (UG 3 3MNN N ,dl;;;;;r#cdSNrr's r"connectzModemATCommandLink.connect r#returncdS)N3b00rr's r"get_atrzModemATCommandLink.get_atrsvr#cdSrWrr's r" disconnectzModemATCommandLink.disconnectrYr#NFr newcardonlycdSrWr)rrr`s r" wait_for_cardz ModemATCommandLink.wait_for_cardrYr#tpduc8|}dt||fz}tjd|||}|ddrt dt|zt|dks |ddkrtdt|z|d } tj d |}| \}}n%#t$r}td |z|d}~wwxYw|dd  }|d d } tjd || || fS)NzAT+CSIM=%d,"%s"zSending command: %srQr1zAT+CSIM failed with: %sr.r0zAPDU transfer failed: %srs\+CSIM: (\d+),"([0-9A-F]+)"z'Failed to parse response from modem: %szCommand response: %s, %s)upperr9r6r7rNr?rstrrrematchgroupsr:decodelower) rrcrDrIrR _rsp_tpdu_lenrsp_tpdurHdatasws r" send_tpduzModemATCommandLink.send_tpduszz||"SYY$55 '#...s## r7  n - - F 9CHH DEE E s88q==CGu,,83s88CDD D!f XX>DDF(.  %]HH X X XG#MNNTW W X}##%%++-- bcc] ! ! # # ) ) + + ,dB777Rxs,D D# DD#cd|jzS)Nzmodem:%s)rr's r"__str__zModemATCommandLink.__str__sDL((r# arg_parserc|dd}|ddddd|dtd d d dS) NzAT Command Modem ReaderzTalk to a SIM Card inside a mobile phone or cellular modem which is attached to this computer and offers an AT command interface including the AT+CSIM interface for Generic SIM access as specified in 3GPP TS 27.007.z--modem-devicer DEVzrs&  ((((((77777777 \G\G\G\G\G\G\G\G\G\Gr#