5. Python API

Here is detailed the python API of the smartleia package.

class smartleia.TriggerPoints(value)[source]

Class utility to reference the trigger points available.

TRIG_GET_ATR_PRE = 1

Point before getting the ATR.

TRIG_GET_ATR_POST = 2

Point just after the ATR has been received.

TRIG_PRE_SEND_APDU_SHORT_T0 = 4

Point just before sending a simple APDU in T=0.

TRIG_PRE_SEND_APDU_FRAGMENTED_T0 = 8

Point just before sending a fragmented APDU in T=0.

TRIG_PRE_SEND_APDU_T1 = 16

Point just before sending an APDU in T=1.

TRIG_PRE_SEND_APDU = 28

Point just before sending an APDU

TRIG_POST_RESP_T0 = 64

Point just before receiving a RESP in T=0.

TRIG_POST_RESP_T1 = 128

Point just before receiving a RESP in T=1.

TRIG_POST_RESP = 192

Point just before receiving a RESP

TRIG_IRQ_PUTC = 256

Point just after sending a byte through the ISO7816 interface.

TRIG_IRQ_GETC = 512

Point juster afted a byte has been received through the ISO7816 interface.

class smartleia.T(value)[source]

ISO7816 protocol selection.

AUTO = -1

The protocol is negotiated.

T0 = 0

The protocol is T=0

T1 = 1

The protocol is T=1

class smartleia.Triggers(value)[source]

An enumeration.

class smartleia.TriggerStrategy(delay=0, single=0, point_list=None)[source]
Variables
  • delay (int) – the delay between event detection and effective trig on GPIO in milliseconds.

  • point_list (list[int]) – the list of events to match.

smartleia.create_APDU_from_bytes(_bytes)[source]

Create an APDU instance from a list of bytes.

Example

>>> create_APDU_from_bytes([0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09])
APDU(cla=0x00, ins=0x01, p1=0x02, p2=0x03, lc=0x04, data=[0x05, 0x06, 0x07, 0x08], le=0x09, send_le=1)
Return type

APDU

class smartleia.APDU(cla=0, ins=0, p1=0, p2=0, lc=None, le=0, send_le=1, data=None)[source]

Object for representing an APDU.

Variables
  • cla (ctypes.c_uint8) – the CLA field of the APDU.

  • ins (ctypes.c_uint8) – the INS field of the APDU.

  • p1 (ctypes.c_uint8) – the P1 field of the APDU.

  • p2 (ctypes.c_uint8) – the P2 field of the APDU.

  • lc (ctypes.c_uint16) – the Lc field of the APDU.

  • le (ctypes.c_uint32) – the Le field of the APDU.

  • send_le (ctypes.c_uint8) – Description of attr1.

  • data (list[int]) – the data field of the APDU.

Create an APDU structure.

Parameters
  • cla (int) – the CLA field of the APDU.

  • ins (int) – the INS field of the APDU.

  • p1 (int) – the P1 field of the APDU.

  • p2 (int) – the P2 field of the APDU.

  • lc (Optional[int]) – the Lc (data length) field of the APDU.

  • le (int) – the Le (expected length) field of the APDU.

  • send_le (int) – TODO.

  • data (Optional[List[int]]) – the list of bytes to send.

class smartleia.RESP(sw1=0, sw2=0, data=None, delta_t=0, delta_t_answer=0)[source]

This class is used to represent an RESP.

Variables
  • sw1 (ctypes.c_uint8) – The value of SW1 field.

  • sw2 (ctypes.c_uint8) – The value of SW2 field.

  • le (ctypes.c_uint32) – The length of the data.

  • data (list[byte]) – The value of the data field.

Create an RESP structure.

Parameters
  • le (int) – the Le field of the RESP.

  • sw1 (int) – the SW1 field of the RESP.

  • sw2 (int) – the SW2 field of the RESP.

  • delta_t (int) – total time for the APDU.

  • delta_t_answer (int) – answer time for the APDU.

  • data (list[int]) – the list of bytes received.

class smartleia.ATR[source]

This class is used to represent an ATR.

Variables
  • ts (ctypes.c_uint8) – Description of attr1.

  • t0 (ctypes.c_uint8) – Description of attr2.

  • ta (ctypes.c_uint8[4]) – Description of attr1.

  • tb (ctypes.c_uint8[4]) – Description of attr2.

  • tc (ctypes.c_uint8[4]) – Description of attr1.

  • td (ctypes.c_uint8[4]) – Description of attr2.

  • h (ctypes.c_uint8[16]) – Description of attr1.

  • t_mask (ctypes.c_uint8[4]) – Description of attr2.

  • h_num (ctypes.c_uint8) – Description of attr1.

  • tck (ctypes.c_uint8) – Description of attr2.

  • tck_present (ctypes.c_uint8) – Description of attr1.

  • D_i_curr (ctypes.c_uint32) – Description of attr2.

  • F_i_curr (ctypes.c_uint32) – Description of attr1.

  • f_max_curr (ctypes.c_uint32) – Description of attr2.

  • T_protocol_curr (ctypes.c_uint8) – Description of attr1.

  • ifsc (ctypes.c_uint8) – Description of attr2.

class smartleia.LEIA(device='', serial_factory=None, auto_open=True)[source]

This class connects to a LEIA board and provides an access to all the device functionnality.

Parameters

device (str) – the serial port to use with LEIA (like /dev/ttyUSB0).

Raises

Exception – if no serial port is provided.

reset()[source]

Reset LEIA.

open()[source]

Open LEIA.

configure_smartcard(protocol_to_use=None, ETU_to_use=None, freq_to_use=None, negotiate_pts=True, negotiate_baudrate=True)[source]

Configure a smartcard connection.

Method to configure a smartcard. By default, the smartcard reader will negociate with the smartcard the mode (T=0 or T=1), the ETU and the frequence to use. It is possible to:

  • force a mode (by setting protocol_to_use to 0 for T=0, and to 1 for T=1)

  • force an ETU (by setting the ETU_to_use parameter)

  • force a frequence (by setting the freq_to_use parameter)

Example

>>> leia.configure_smartcard(T.T0, ETU_to_use=372)
Parameters
  • protocol_to_use (Optional[T]) – The protocol to use (0: T=0, 1: T=1).

  • ETU_to_use (Optional[int]) – The ETU value to force. If None, will be negociated (or default will be used).

  • freq_to_use (Optional[int]) – The ISO7816 clock frequency to use. If None, will be negociated (or default will be used).

  • negotiate_pts (Optional[bool]) – if LEIA can try to negotiate the PTS.

  • negotiate_baudrate (Optional[bool]) – if LEIA can negotiate the baudrate. There is not impact if ETU_to_use and freq_to_use are set.

get_trigger_strategy(SID)[source]

Returns the strategy N°SID.

Parameters

SID (int) – The trigger strategy’s ID to get.

Returns

The trigger strategy N°SID.

Return type

TriggerStrategy

set_trigger_strategy(SID, point_list, delay=0, single=0)[source]

Set and activate a trigger strategy.

Parameters
  • SID (int) – the strategy bank ID to use.

  • point_list (Union[int, List[int]]) – the sequence to match for the trigger.

  • delay (int) – the delay (in milliseconds) between the moment of the detection and the moment where the trigger is actually set high.

get_timers()[source]

Return the timers of the last command.

Returns

The Timer object.

Return type

Timers

get_ATR()[source]

Return the ATR.

Returns

The ATR object.

Return type

ATR

is_card_inserted()[source]

Return True if a smartcard is inserted in LEIA.

Return type

bool

Returns

True if a smartcard is inserted, else False.

dfu()[source]

Reboot LEIA in DFU mode.

Return type

None

flasher()[source]

Reboot LEIA in funcard flasher mode.

Return type

None

smartreader()[source]

Reboot LEIA in funcard smartreader mode.

Return type

None

send_APDU(apdu)[source]

Send an APDU.

Parameters

apdu (APDU) – The APDU object.

Returns

The RESP object.

Return type

RESP