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
This commit is contained in:
Philipp Maier 2023-10-09 13:32:49 +02:00
parent 8e03f2f2ed
commit 6bfa8a8533
7 changed files with 26 additions and 3 deletions

View File

@ -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<---------------------")

View File

@ -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'

View File

@ -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."""

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -236,3 +236,6 @@ class SerialSimLink(LinkBase):
# Return value
return b2h(data), b2h(sw)
def __str__(self):
return "serial:%s" % (self._sl.name)