From 2fe9b6a3e9f997b12bf0042e7620a5a86242d06d Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 7 Sep 2024 13:09:48 +0200 Subject: [PATCH] pySim.transport: Also trace card reset events in ApduTracer Change-Id: Ia46b65124520eb2b8015dfa3f0a135b497668b92 --- pySim-trace.py | 2 +- pySim/transport/__init__.py | 13 +++++++++++++ pySim/transport/calypso.py | 2 +- pySim/transport/modem_atcmd.py | 2 +- pySim/transport/pcsc.py | 2 +- pySim/transport/serial.py | 6 +++--- 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pySim-trace.py b/pySim-trace.py index 3652b3a7..46d4c408 100755 --- a/pySim-trace.py +++ b/pySim-trace.py @@ -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): diff --git a/pySim/transport/__init__.py b/pySim/transport/__init__.py index 856e86e4..94825282 100644 --- a/pySim/transport/__init__.py +++ b/pySim/transport/__init__.py @@ -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 diff --git a/pySim/transport/calypso.py b/pySim/transport/calypso.py index d8e5b89f..94629398 100644 --- a/pySim/transport/calypso.py +++ b/pySim/transport/calypso.py @@ -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()) diff --git a/pySim/transport/modem_atcmd.py b/pySim/transport/modem_atcmd.py index 882cb651..8970f7d8 100644 --- a/pySim/transport/modem_atcmd.py +++ b/pySim/transport/modem_atcmd.py @@ -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') diff --git a/pySim/transport/pcsc.py b/pySim/transport/pcsc.py index 7843f7f9..207cf6c6 100644 --- a/pySim/transport/pcsc.py +++ b/pySim/transport/pcsc.py @@ -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 diff --git a/pySim/transport/serial.py b/pySim/transport/serial.py index e1552261..04b4ab7d 100644 --- a/pySim/transport/serial.py +++ b/pySim/transport/serial.py @@ -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,