dexter has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/pysim/+/34692?usp=email )


Change subject: pySim-shell: print device info in case an exception occurs
......................................................................

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
---
M pySim-shell.py
M pySim/transport/__init__.py
M pySim/transport/calypso.py
M pySim/transport/modem_atcmd.py
M pySim/transport/pcsc.py
M pySim/transport/serial.py
6 files changed, 44 insertions(+), 3 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/92/34692/1

diff --git a/pySim-shell.py b/pySim-shell.py
index 79b4d8b..56655ba 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -374,7 +374,7 @@
             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 @@
                 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 @@
         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/transport/__init__.py b/pySim/transport/__init__.py
index 80a4208..9b28a8f 100644
--- a/pySim/transport/__init__.py
+++ b/pySim/transport/__init__.py
@@ -69,6 +69,10 @@
         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."""

@@ -300,6 +304,7 @@
             print("Using PC/SC reader interface")
             from pySim.transport.pcsc import PcscSimLink
             sl = PcscSimLink(opts.pcsc_dev, **kwargs)
+            print(sl)
         elif opts.osmocon_sock is not None:
             print("Using Calypso-based (OsmocomBB) reader interface")
             from pySim.transport.calypso import CalypsoSimLink
diff --git a/pySim/transport/calypso.py b/pySim/transport/calypso.py
index 34fc646..9cfc5c6 100644
--- a/pySim/transport/calypso.py
+++ b/pySim/transport/calypso.py
@@ -90,6 +90,9 @@
         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 @@
         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 e99762d..58d6f9d 100644
--- a/pySim/transport/modem_atcmd.py
+++ b/pySim/transport/modem_atcmd.py
@@ -169,3 +169,6 @@
         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 a01917f..41c4c19 100644
--- a/pySim/transport/pcsc.py
+++ b/pySim/transport/pcsc.py
@@ -39,6 +39,7 @@
             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 @@

         # 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 998d1d8..f4b1621 100644
--- a/pySim/transport/serial.py
+++ b/pySim/transport/serial.py
@@ -236,3 +236,6 @@

         # Return value
         return b2h(data), b2h(sw)
+
+    def __str__(self):
+        return "serial:%s" % (self._sl.name)

--
To view, visit https://gerrit.osmocom.org/c/pysim/+/34692?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I200463e692245da40ea6d5b609bfc0ca02d15bdb
Gerrit-Change-Number: 34692
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pma...@sysmocom.de>
Gerrit-MessageType: newchange

Reply via email to