pySim.transport: Add support for generic stdout apdu tracer

Any program using argparse_add_reader_args() will get a new
long-opt '--apdu-trace' which enables a raw APDU trace to the console.

Change-Id: I4bc3d2e023ba360f07f024d7b661a93322f87530
This commit is contained in:
Harald Welte
2024-09-07 09:22:26 +02:00
parent bf0689a48e
commit 241d65db12

View File

@@ -40,6 +40,12 @@ class ApduTracer:
def trace_response(self, cmd, sw, resp): def trace_response(self, cmd, sw, resp):
pass pass
class StdoutApduTracer(ApduTracer):
"""Minimalistic APDU tracer, printing commands to stdout."""
def trace_response(self, cmd, sw, resp):
print("-> %s %s" % (cmd[:10], cmd[10:]))
print("<- %s: %s" % (sw, resp))
class ProactiveHandler(abc.ABC): class ProactiveHandler(abc.ABC):
"""Abstract base class representing the interface of some code that handles """Abstract base class representing the interface of some code that handles
the proactive commands, as returned by the card in responses to the FETCH the proactive commands, as returned by the card in responses to the FETCH
@@ -239,6 +245,8 @@ def argparse_add_reader_args(arg_parser: argparse.ArgumentParser):
PcscSimLink.argparse_add_reader_args(arg_parser) PcscSimLink.argparse_add_reader_args(arg_parser)
ModemATCommandLink.argparse_add_reader_args(arg_parser) ModemATCommandLink.argparse_add_reader_args(arg_parser)
CalypsoSimLink.argparse_add_reader_args(arg_parser) CalypsoSimLink.argparse_add_reader_args(arg_parser)
arg_parser.add_argument('--apdu-trace', action='store_true',
help='Trace the command/response APDUs exchanged with the card')
return arg_parser return arg_parser
@@ -247,6 +255,9 @@ def init_reader(opts, **kwargs) -> LinkBase:
""" """
Init card reader driver Init card reader driver
""" """
if opts.apdu_trace and not 'apdu_tracer' in kwargs:
kwargs['apdu_tracer'] = StdoutApduTracer()
if opts.pcsc_dev is not None or opts.pcsc_regex is not None: if opts.pcsc_dev is not None or opts.pcsc_regex is not None:
from pySim.transport.pcsc import PcscSimLink from pySim.transport.pcsc import PcscSimLink
sl = PcscSimLink(opts, **kwargs) sl = PcscSimLink(opts, **kwargs)