Change in pysim[master]: shell: Add 'apdu_trace' settable parameter for hex-dumping APDUs

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Sat Apr 10 22:13:58 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/23698 )

Change subject: shell: Add 'apdu_trace' settable parameter for hex-dumping APDUs
......................................................................

shell: Add 'apdu_trace' settable parameter for hex-dumping APDUs

Change-Id: I0c957c0b86473413f31e4bd8bc4e633fc1470222
---
M docs/shell.rst
M pySim-shell.py
M pySim/transport/__init__.py
3 files changed, 39 insertions(+), 3 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/docs/shell.rst b/docs/shell.rst
index 06e7f42..854944c 100644
--- a/docs/shell.rst
+++ b/docs/shell.rst
@@ -328,6 +328,11 @@
 
 If enabled, full python back-traces will be displayed in case of exceptions
 
+apdu_trace
+~~~~~~~~~~
+
+Boolean variable that determines if a hex-dump of the command + response APDU shall be printed.
+
 numeric_path
 ~~~~~~~~~~~~
 
diff --git a/pySim-shell.py b/pySim-shell.py
index 29138cc..ce1b3bf 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -37,7 +37,7 @@
 
 from pySim.exceptions import *
 from pySim.commands import SimCardCommands
-from pySim.transport import init_reader
+from pySim.transport import init_reader, ApduTracer
 from pySim.cards import card_detect, Card
 from pySim.utils import h2b, swap_nibbles, rpad, h2s
 from pySim.utils import dec_st, sanitize_pin_adm, tabulate_str_list, is_hex
@@ -74,6 +74,9 @@
 		self.update_prompt()
 		self.json_pretty_print = True
 		self.add_settable(cmd2.Settable('json_pretty_print', bool, 'Pretty-Print JSON output'))
+		self.apdu_trace = False
+		self.add_settable(cmd2.Settable('apdu_trace', bool, 'Trace and display APDUs exchanged with card',
+						  onchange_cb=self._onchange_apdu_trace))
 
 	def poutput_json(self, data, force_no_pretty = False):
 		"""line cmd2.putput() but for a json serializable dict."""
@@ -89,6 +92,20 @@
 	def _onchange_conserve_write(self, param_name, old, new):
 		self.rs.conserve_write = new
 
+	def _onchange_apdu_trace(self, param_name, old, new):
+		if new == True:
+			self.card._scc._tp.apdu_tracer = self.Cmd2ApduTracer(self)
+		else:
+			self.card._scc._tp.apdu_tracer = None
+
+	class Cmd2ApduTracer(ApduTracer):
+		def __init__(self, cmd2_app):
+			self.cmd2 = app
+
+		def trace_response(self, cmd, sw, resp):
+			self.cmd2.poutput("-> %s %s" % (cmd[:10], cmd[10:]))
+			self.cmd2.poutput("<- %s: %s" % (sw, resp))
+
 	def update_prompt(self):
 		path_list = self.rs.selected_file.fully_qualified_path(not self.numeric_path)
 		self.prompt = 'pySIM-shell (%s)> ' % ('/'.join(path_list))
diff --git a/pySim/transport/__init__.py b/pySim/transport/__init__.py
index c176f8a..96ad974 100644
--- a/pySim/transport/__init__.py
+++ b/pySim/transport/__init__.py
@@ -25,11 +25,20 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+class ApduTracer:
+	def trace_command(self, cmd):
+		pass
+
+	def trace_response(self, cmd, sw, resp):
+		pass
+
+
 class LinkBase(object):
 	"""Base class for link/transport to card."""
 
-	def __init__(self, sw_interpreter=None):
+	def __init__(self, sw_interpreter=None, apdu_tracer=None):
 		self.sw_interpreter = sw_interpreter
+		self.apdu_tracer = apdu_tracer
 
 	def set_sw_interpreter(self, interp):
 		"""Set an (optional) status word interpreter."""
@@ -69,7 +78,12 @@
 				data : string (in hex) of returned data (ex. "074F4EFFFF")
 				sw   : string (in hex) of status word (ex. "9000")
 		"""
-		return self._send_apdu_raw(pdu)
+		if self.apdu_tracer:
+			self.apdu_tracer.trace_command(pdu)
+		(data, sw) = self._send_apdu_raw(pdu)
+		if self.apdu_tracer:
+			self.apdu_tracer.trace_response(pdu, sw, data)
+		return (data, sw)
 
 	def send_apdu(self, pdu):
 		"""Sends an APDU and auto fetch response data

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23698
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I0c957c0b86473413f31e4bd8bc4e633fc1470222
Gerrit-Change-Number: 23698
Gerrit-PatchSet: 3
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210410/4a61c63e/attachment.htm>


More information about the gerrit-log mailing list