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

Change-Id: I0c957c0b86473413f31e4bd8bc4e633fc1470222
This commit is contained in:
Harald Welte
2021-04-10 11:28:53 +02:00
parent eb05b2f60e
commit 7829d8a357
3 changed files with 39 additions and 3 deletions

View File

@@ -25,11 +25,20 @@ from pySim.utils import sw_match
# 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 @@ class LinkBase(object):
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