a sUgf@stddlZddlZddlZddlZddlmZddlZddlm Z m Z ddl m Z ddl mZmZGddde ZdS)N)Optional)HexstrResTuple)LinkBase) ReaderError ProtocolErrorcseZdZdZdZejdddfejdfdd Zd d Zd%d dZ ddZ ddZ ddZ ddZ d&eeedddZeedddZedd d!Zeejd"d#d$ZZS)'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_baud)optsc sV|j}|j}tjfi|tj||dd|_d|_||_d|_ | | dS)N)timeoutF) r r super__init__serialSerial_sl_echo_device_atr _check_echo reset_card)selfr kwargsdevicebaudrate __class__/home/jenkins/workspace/gerrit-pysim-build/JOB_TYPE/test/a1/default/a3/default/a4/default/label/simtester/pySim/transport/modem_atcmd.pyr&szModemATCommandLink.__init__cCst|dr|jdS)Nr)hasattrrcloserrrr __del__6s zModemATCommandLink.__del__皙?Mb`?c Cst|tr|n|}|d7}|jtd|z |j|}|t|ksRJWn2t y}zt d||WYd}~n d}~00d}d}t } ||j |jj }|d} t| dkr| d} | d krtd | q>| d ks| d rtd | q>t | |kr*td|q>t ||d7}qtdt | |||jrj||d}|}|d}td||S)N zSending AT command: %szFailed to send AT command: %ss 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) rcmdrZpatienceZbcmdZwlenexcrspZitsZt_startlinesresrrr send_at_cmd:sD   $         zModemATCommandLink.send_at_cmdcCsZd|_|d}t|dkrH|ddkr2d|_dS|ddkrHd|_dStd |jdS) 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. FATrr,NsAT OKTz/Interface '%s' does not respond to 'AT' command)rrEr5rr)rresultrrr ris     zModemATCommandLink._check_echocCsB|ddgkrtd|ddgkr0tdtd|jdS)NZATZr,zFailed to reset the modemz AT+CSIM=?z-The modem does not seem to support SIM accesszModem at '%s' is ready!)rErr2r=rr#rrr rs zModemATCommandLink.reset_cardcCsdSNrr#rrr connectszModemATCommandLink.connectcCsdSrIrr#rrr disconnectszModemATCommandLink.disconnectNF)r newcardonlycCsdSrIr)rrrLrrr wait_for_cardsz ModemATCommandLink.wait_for_card)pdureturnc Cs |}dt||f}td|||}|ddrLtdt|t|dksd|ddkrttdt||d }zt d |}| \}}Wn2t y}ztd ||WYd}~n d}~00|dd  }|d d } td || || fS)NzAT+CSIM=%d,"%s"zSending command: %srGr-zAT+CSIM failed with: %sr*r,zAPDU transfer failed: %srs\+CSIM: (\d+),"([0-9A-F]+)"z'Failed to parse response from modem: %szCommand response: %s, %s)upperr5r2r3rEr;rstrrrematchgroupsr6decodelower) rrNr@rBrHZ _rsp_pdu_lenZrsp_pdurAdataswrrr _send_apdu_raws$   $z!ModemATCommandLink._send_apdu_raw)rOcCs d|jS)Nzmodem:%s)rr#rrr __str__szModemATCommandLink.__str__) arg_parsercCs8|dd}|jdddddd|jdtd d 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 DEVzs