From 6bfa8a853335e5bd75fb1f40b3ad3df95583f316 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Mon, 9 Oct 2023 13:32:49 +0200 Subject: [PATCH] pySim-shell: print device info in case an exception occurs When an exception occurs while initializing or handling the card we print a traceback, but we do not print any info that allows us to identify the device that was involved when the exception occurred. Let's include the device path or number in the error message before we print the traceback. In order to make it easier to print the device information, let's add a __str__() method to all of our devices. This method shall return the device number or path. Related: OS#6210 Change-Id: I200463e692245da40ea6d5b609bfc0ca02d15bdb --- pySim-shell.py | 6 +++--- pySim-trace.py | 3 +++ pySim/transport/__init__.py | 4 ++++ pySim/transport/calypso.py | 6 ++++++ pySim/transport/modem_atcmd.py | 3 +++ pySim/transport/pcsc.py | 4 ++++ pySim/transport/serial.py | 3 +++ 7 files changed, 26 insertions(+), 3 deletions(-) diff --git a/pySim-shell.py b/pySim-shell.py index 79b4d8b7..56655ba0 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -374,7 +374,7 @@ class PysimApp(Cmd2Compat): rc = self.equip(card, rs) except: self.poutput("") - self.poutput("Card initialization failed with an exception:") + self.poutput("Card initialization (%s) failed with an exception:" % str(self.sl)) self.poutput("---------------------8<---------------------") traceback.print_exc() self.poutput("---------------------8<---------------------") @@ -489,7 +489,7 @@ class PysimApp(Cmd2Compat): return except: self.poutput("") - self.poutput("Card handling failed with an exception:") + self.poutput("Card handling (%s) failed with an exception:" % str(self.sl)) self.poutput("---------------------8<---------------------") traceback.print_exc() self.poutput("---------------------8<---------------------") @@ -1031,7 +1031,7 @@ if __name__ == '__main__': rs, card = init_card(sl) app = PysimApp(card, rs, sl, ch, opts.script) except: - print("Card initialization failed with an exception:") + print("Card initialization (%s) failed with an exception:" % str(sl)) print("---------------------8<---------------------") traceback.print_exc() print("---------------------8<---------------------") diff --git a/pySim-trace.py b/pySim-trace.py index 325fb8ce..eb29ed1a 100755 --- a/pySim-trace.py +++ b/pySim-trace.py @@ -48,6 +48,9 @@ class DummySimLink(LinkBase): self._debug = debug self._atr = h2i('3B9F96801F878031E073FE211B674A4C753034054BA9') + def __str__(self): + return "dummy" + def _send_apdu_raw(self, pdu): #print("DummySimLink-apdu: %s" % pdu) return [], '9000' diff --git a/pySim/transport/__init__.py b/pySim/transport/__init__.py index c8079f66..1dd8d18c 100644 --- a/pySim/transport/__init__.py +++ b/pySim/transport/__init__.py @@ -67,6 +67,10 @@ class LinkBase(abc.ABC): self.apdu_tracer = apdu_tracer self.proactive_handler = proactive_handler + @abc.abstractmethod + def __str__(self): + """Implementation specific method for printing an information to identify the device.""" + @abc.abstractmethod def _send_apdu_raw(self, pdu: Hexstr) -> ResTuple: """Implementation specific method for sending the PDU.""" diff --git a/pySim/transport/calypso.py b/pySim/transport/calypso.py index 34fc6465..b827d88c 100644 --- a/pySim/transport/calypso.py +++ b/pySim/transport/calypso.py @@ -90,6 +90,9 @@ class CalypsoSimLink(LinkBase): self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.sock.connect(sock_path) + # Remember socket path + self._sock_path = sock_path + def __del__(self): self.sock.close() @@ -156,3 +159,6 @@ class CalypsoSimLink(LinkBase): sw = rsp[-2:] return b2h(data), b2h(sw) + + def __str__(self): + return "osmocon:%s" % (self._sock_path) diff --git a/pySim/transport/modem_atcmd.py b/pySim/transport/modem_atcmd.py index e99762dd..58d6f9d1 100644 --- a/pySim/transport/modem_atcmd.py +++ b/pySim/transport/modem_atcmd.py @@ -169,3 +169,6 @@ class ModemATCommandLink(LinkBase): sw = rsp_pdu[-4:].decode().lower() log.debug('Command response: %s, %s', data, sw) return data, sw + + def __str__(self): + return "modem:%s" % self._device diff --git a/pySim/transport/pcsc.py b/pySim/transport/pcsc.py index a01917f4..41c4c190 100644 --- a/pySim/transport/pcsc.py +++ b/pySim/transport/pcsc.py @@ -39,6 +39,7 @@ class PcscSimLink(LinkBase): raise ReaderError('No reader found for number %d' % reader_number) self._reader = r[reader_number] self._con = self._reader.createConnection() + self._reader_number = reader_number def __del__(self): try: @@ -91,3 +92,6 @@ class PcscSimLink(LinkBase): # Return value return i2h(data), i2h(sw) + + def __str__(self): + return "PCSC:%u[%s]" % (self._reader_number, self._reader) diff --git a/pySim/transport/serial.py b/pySim/transport/serial.py index 998d1d89..f4b1621e 100644 --- a/pySim/transport/serial.py +++ b/pySim/transport/serial.py @@ -236,3 +236,6 @@ class SerialSimLink(LinkBase): # Return value return b2h(data), b2h(sw) + + def __str__(self): + return "serial:%s" % (self._sl.name)