a sUgf^*@sdZddlZddlZddlZddlmZmZddlmZddl Tddl m Z m Z m Z mZmZmZmZmZddlmZmZmZmZGdd d ZGd d d ejZGd d d ejZejdddZedddZdS)z( pySim: PCSC reader transport link base N)OptionalTuple) Construct)*)sw_matchb2hh2bi2hHexstrSwHexstr SwMatchstrResTuple)ProactiveCommandCommandDetailsDeviceIdentitiesResultc@seZdZddZddZdS) ApduTracercCsdSN)selfcmdrr/home/jenkins/workspace/gerrit-pysim-build/JOB_TYPE/test/a1/default/a3/default/a4/default/label/simtester/pySim/transport/__init__.py trace_command$szApduTracer.trace_commandcCsdSrr)rrswZresprrrtrace_response'szApduTracer.trace_responseN)__name__ __module__ __qualname__rrrrrrr#src@s.eZdZdZeedddZedddZdS) ProactiveHandlerzAbstract base class representing the interface of some code that handles the proactive commands, as returned by the card in responses to the FETCH command.)pcmdparsedcCs6dt|j}t||r,t||}||jS||S)Nz handle_%s)typerhasattrgetattrdecoded receive_fetch)rrr Z handle_namehandlerrrrreceive_fetch_raw.s    z"ProactiveHandler.receive_fetch_raw)rcCstd|jdS)z=Default handler for not otherwise handled proactive commands.zNo handler method for %sN)NotImplementedErrorr$)rrrrrr%7szProactiveHandler.receive_fetchN)rrr__doc__rr r'r%rrrrr*s rc@seZdZdZd eeeedddZej e dddZ ej e e d d d Zd d Zej d!eeedddZej ddZej ddZej ddZe e d ddZe e d ddZd"e ee dddZdS)#LinkBasez&Base class for link/transport to card.N) apdu_tracerproactive_handlercCs||_||_||_dSr)sw_interpreterr+r,)rr-r+r,rrr__init__@szLinkBase.__init__returncCsdS)zRImplementation specific method for printing an information to identify the device.Nrrrrr__str__FszLinkBase.__str__)pdur0cCsdS)z3Implementation specific method for sending the PDU.Nr)rr3rrr_send_apdu_rawJszLinkBase._send_apdu_rawcCs ||_dS)z*Set an (optional) status word interpreter.N)r-)rinterprrrset_sw_interpreterNszLinkBase.set_sw_interpreterF)timeout newcardonlycCsdS)zWait for a card and connect to it Args: timeout : Maximum wait time in seconds (None=no timeout) newcardonly : Should we wait for a new card, or an already inserted one ? Nr)rr7r8rrr wait_for_cardRszLinkBase.wait_for_cardcCsdS)z&Connect to a card immediately Nrr1rrrconnect[szLinkBase.connectcCsdS)zDisconnect from card Nrr1rrr disconnect`szLinkBase.disconnectcCsdS)z(Resets the card (power down/up) Nrr1rrr reset_cardeszLinkBase.reset_cardcCs>|jr|j|||\}}|jr6|j|||||fS)aWSends an APDU with minimal processing Args: pdu : string of hexadecimal characters (ex. "A0A40000023F00") Returns: tuple(data, sw), where data : string (in hex) of returned data (ex. "074F4EFFFF") sw : string (in hex) of status word (ex. "9000") )r+rr4r)rr3datarrrr send_apdu_rawjs  zLinkBase.send_apdu_rawcCs||\}}|dur|dddks6|dddkrj|ddd|dd}||\}}||7}q|dddkr|dd |dd}||\}}||fS) a\Sends an APDU and auto fetch response data Args: pdu : string of hexadecimal characters (ex. "A0A40000023F00") Returns: tuple(data, sw), where data : string (in hex) of returned data (ex. "074F4EFFFF") sw : string (in hex) of status word (ex. "9000") NrZ9fZ61Zc00000Z6c)r>)rr3r=rZpdu_grdrrr send_apdu{s   zLinkBase.send_apdu9000)r3rr0cCs^||}|d}|dkr4t|dr4|ddf}|d|dd|}|d}t}|t|d}td|dt|jft }|j r|j ||| d d d n| d d d d d|j jD\} t} | ddd| | |} dtt| dd| } || } | d}qt|d|sZt|d||j|S)aSends an APDU and check returned SW Args: pdu : string of hexadecimal characters (ex. "A0A40000023F00") sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain digits using a '?' to add some ambiguity if needed. Returns: tuple(data, sw), where data : string (in hex) of returned data (ex. "074F4EFFFF") sw : string (in hex) of status word (ex. "9000") rDZ91xxrZ80120000r?NzFETCH: %s (%s)Zperformed_successfully)Zgeneral_resultZadditional_informationZ"command_beyond_terminal_capabilitycSsg|]}t|tr|qSr) isinstancer).0crrr z.LinkBase.send_apdu_checksw..ZterminalZuicc)Z source_dev_idZ dest_dev_idZ80140000big)rCrsend_apdu_checkswrfrom_tlvrprintr!rrr,r' from_dictr$childrenrto_tlvrlento_bytes SwMatchErrorlowerr-)rr3rrvZlast_swZfetch_rvrr resultZcommand_detailsZdevice_identitiestailZterminal_responseZterminal_response_rvrrrrMs0    zLinkBase.send_apdu_checksw)NNN)NF)rD)rrrr)rrrr.abcabstractmethodstrr2r r r4r6intboolr9r:r;r<r>rCr rMrrrrr*=s,    r*) arg_parsercCs\ddlm}ddlm}ddlm}ddlm}|||||||||S)zOAdd all reader related arguments to the given argparse.Argumentparser instance.r SerialSimLink PcscSimLinkModemATCommandLinkCalypsoSimLink) pySim.transport.serialrapySim.transport.pcscrcpySim.transport.modem_atcmdrepySim.transport.calypsorgargparse_add_reader_args)r_rarcrergrrrrls        rlr/cKs|jdus|jdur2ddlm}||fi|}nt|jdurZddlm}||fi|}nL|jdurddlm }||fi|}n$t dddl m }||fi|}t jdd krt d |jn t d ||S) z! Init card reader driver NrrbrfrdzCNo reader/driver specified; falling back to default (Serial reader)r`ZPYSIM_INTEGRATION_TEST1zUsing %s reader interfacezUsing reader %s)Zpcsc_devZ pcsc_regexrircZ osmocon_sockrkrgZ modem_devrjrerOrhraosenvirongetname)optskwargsrcslrgrerarrr init_readers        ru)r)rnrZargparsetypingrr constructrpySim.exceptions pySim.utilsrrrr r r r r Z pySim.catrrrrrABCrr*ArgumentParserrlrurrrrs (!