pySim.transport: Also trace card reset events in ApduTracer

Change-Id: Ia46b65124520eb2b8015dfa3f0a135b497668b92
This commit is contained in:
Harald Welte
2024-09-07 13:09:48 +02:00
parent 241d65db12
commit 2fe9b6a3e9
6 changed files with 20 additions and 7 deletions

View File

@@ -65,7 +65,7 @@ class DummySimLink(LinkBase):
def disconnect(self):
pass
def reset_card(self):
def _reset_card(self):
return 1
def get_atr(self):

View File

@@ -40,12 +40,18 @@ class ApduTracer:
def trace_response(self, cmd, sw, resp):
pass
def trace_reset(self):
pass
class StdoutApduTracer(ApduTracer):
"""Minimalistic APDU tracer, printing commands to stdout."""
def trace_response(self, cmd, sw, resp):
print("-> %s %s" % (cmd[:10], cmd[10:]))
print("<- %s: %s" % (sw, resp))
def trace_reset(self):
print("-- RESET")
class ProactiveHandler(abc.ABC):
"""Abstract base class representing the interface of some code that handles
the proactive commands, as returned by the card in responses to the FETCH
@@ -117,9 +123,16 @@ class LinkBase(abc.ABC):
"""
@abc.abstractmethod
def _reset_card(self):
"""Resets the card (power down/up)
"""
def reset_card(self):
"""Resets the card (power down/up)
"""
if self.apdu_tracer:
self.apdu_tracer.trace_reset()
return self._reset_card()
def send_apdu_raw(self, pdu: Hexstr) -> ResTuple:
"""Sends an APDU with minimal processing

View File

@@ -109,7 +109,7 @@ class CalypsoSimLink(LinkBase):
rsp = self.sock.recv(exp_len)
return rsp
def reset_card(self):
def _reset_card(self):
# Request FULL reset
req_msg = L1CTLMessageReset()
self.sock.send(req_msg.gen_msg())

View File

@@ -125,7 +125,7 @@ class ModemATCommandLink(LinkBase):
return
raise ReaderError('Interface \'%s\' does not respond to \'AT\' command' % self._device)
def reset_card(self):
def _reset_card(self):
# Reset the modem, just to be sure
if self.send_at_cmd('ATZ') != [b'OK']:
raise ReaderError('Failed to reset the modem')

View File

@@ -97,7 +97,7 @@ class PcscSimLink(LinkBase):
def disconnect(self):
self._con.disconnect()
def reset_card(self):
def _reset_card(self):
self.disconnect()
self.connect()
return 1

View File

@@ -101,15 +101,15 @@ class SerialSimLink(LinkBase):
def disconnect(self):
pass # Nothing to do really ...
def reset_card(self):
rv = self._reset_card()
def _reset_card(self):
rv = self.__reset_card()
if rv == 0:
raise NoCardError()
if rv < 0:
raise ProtocolError()
return rv
def _reset_card(self):
def __reset_card(self):
self._atr = None
rst_meth_map = {
'rts': self._sl.setRTS,