From 0f177c1d290202d808c94eb084f45ae6b383da26 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 17 Dec 2023 12:38:29 +0100 Subject: [PATCH] transport: Pass argparse.Namespace directly into transport classes It's odd that the individual transport driver specifies their argparse options but then the core transport part evaluates them individually. This means we cannot add new options within a transport. Let's pass the Namespace instance into the constructor of the specific transport to improve this. Change-Id: Ib977007dd605ec9a9c09a3d143d2c2308991a12c --- contrib/sim-rest-server.py | 2 +- pySim/transport/__init__.py | 10 ++++------ pySim/transport/calypso.py | 3 ++- pySim/transport/modem_atcmd.py | 5 ++++- pySim/transport/pcsc.py | 12 ++++++------ pySim/transport/serial.py | 12 ++++++------ 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/contrib/sim-rest-server.py b/contrib/sim-rest-server.py index fc8afd94..0e44b23a 100755 --- a/contrib/sim-rest-server.py +++ b/contrib/sim-rest-server.py @@ -38,7 +38,7 @@ class ApduPrintTracer(ApduTracer): pass def connect_to_card(slot_nr:int): - tp = PcscSimLink(slot_nr, apdu_tracer=ApduPrintTracer()) + tp = PcscSimLink(argparse.Namespace(pcsc_dev=slot_nr), apdu_tracer=ApduPrintTracer()) tp.connect() scc = SimCardCommands(tp) diff --git a/pySim/transport/__init__.py b/pySim/transport/__init__.py index 2ffb9c35..e0e1e620 100644 --- a/pySim/transport/__init__.py +++ b/pySim/transport/__init__.py @@ -289,17 +289,15 @@ def init_reader(opts, **kwargs) -> LinkBase: """ if opts.pcsc_dev is not None: from pySim.transport.pcsc import PcscSimLink - sl = PcscSimLink(opts.pcsc_dev, **kwargs) + sl = PcscSimLink(opts, **kwargs) elif opts.osmocon_sock is not None: from pySim.transport.calypso import CalypsoSimLink - sl = CalypsoSimLink(sock_path=opts.osmocon_sock, **kwargs) + sl = CalypsoSimLink(opts, **kwargs) elif opts.modem_dev is not None: from pySim.transport.modem_atcmd import ModemATCommandLink - sl = ModemATCommandLink( - device=opts.modem_dev, baudrate=opts.modem_baud, **kwargs) + sl = ModemATCommandLink(opts, **kwargs) else: # Serial reader is default print("No reader/driver specified; falling back to default (Serial reader)") from pySim.transport.serial import SerialSimLink - sl = SerialSimLink(device=opts.device, - baudrate=opts.baudrate, **kwargs) + sl = SerialSimLink(opts, **kwargs) return sl diff --git a/pySim/transport/calypso.py b/pySim/transport/calypso.py index 98330a97..01bbe232 100644 --- a/pySim/transport/calypso.py +++ b/pySim/transport/calypso.py @@ -77,7 +77,8 @@ class L1CTLMessageSIM(L1CTLMessage): class CalypsoSimLink(LinkBase): """Transport Link for Calypso based phones.""" - def __init__(self, sock_path: str = "/tmp/osmocom_l2", **kwargs): + def __init__(self, opts: argparse.Namespace = argparse.Namespace(osmocon_sock="/tmp/osmocom_l2"), **kwargs): + sock_path = opts.osmocon_sock super().__init__(**kwargs) if os.environ.get('PYSIM_INTEGRATION_TEST') == "1": print("Using Calypso-based (OsmocomBB) reader interface") diff --git a/pySim/transport/modem_atcmd.py b/pySim/transport/modem_atcmd.py index 71f76e20..9a4f0a56 100644 --- a/pySim/transport/modem_atcmd.py +++ b/pySim/transport/modem_atcmd.py @@ -35,7 +35,10 @@ from pySim.exceptions import * class ModemATCommandLink(LinkBase): """Transport Link for 3GPP TS 27.007 compliant modems.""" - def __init__(self, device: str = '/dev/ttyUSB0', baudrate: int = 115200, **kwargs): + def __init__(self, opts: argparse.Namespace = argparse.Namespace(modem_dev='/dev/ttyUSB0', + modem_baud=115200), **kwargs): + device = opts.modem_dev + baudrate = opts.modem_baud super().__init__(**kwargs) if os.environ.get('PYSIM_INTEGRATION_TEST') == "1": print("Using modem for Generic SIM Access (3GPP TS 27.007)") diff --git a/pySim/transport/pcsc.py b/pySim/transport/pcsc.py index 762908c6..6afcdac4 100644 --- a/pySim/transport/pcsc.py +++ b/pySim/transport/pcsc.py @@ -34,18 +34,18 @@ from pySim.utils import h2i, i2h, Hexstr, ResTuple class PcscSimLink(LinkBase): """ pySim: PCSC reader transport link.""" - def __init__(self, reader_number: int = 0, **kwargs): + def __init__(self, opts: argparse.Namespace = argparse.Namespace(pcsc_dev=0), **kwargs): super().__init__(**kwargs) if os.environ.get('PYSIM_INTEGRATION_TEST') == "1": print("Using PC/SC reader interface") else: - print("Using PC/SC reader number %u" % reader_number) + print("Using PC/SC reader number %u" % opts.pcsc_dev) r = readers() - if reader_number >= len(r): - raise ReaderError('No reader found for number %d' % reader_number) - self._reader = r[reader_number] + if opts.pcsc_dev >= len(r): + raise ReaderError('No reader found for number %d' % opts.pcsc_dev) + self._reader = r[opts.pcsc_dev] self._con = self._reader.createConnection() - self._reader_number = reader_number + self._reader_number = opts.pcsc_dev def __del__(self): try: diff --git a/pySim/transport/serial.py b/pySim/transport/serial.py index ad800ede..e6803bef 100644 --- a/pySim/transport/serial.py +++ b/pySim/transport/serial.py @@ -30,24 +30,24 @@ from pySim.utils import h2b, b2h, Hexstr, ResTuple class SerialSimLink(LinkBase): """ pySim: Transport Link for serial (RS232) based readers included with simcard""" - def __init__(self, device: str = '/dev/ttyUSB0', baudrate: int = 9600, rst: str = '-rts', + def __init__(self, opts = argparse.Namespace(device='/dev/ttyUSB0', baudrate=9600), rst: str = '-rts', debug: bool = False, **kwargs): super().__init__(**kwargs) if os.environ.get('PYSIM_INTEGRATION_TEST') == "1": print("Using serial reader interface") else: - print("Using serial reader interface at port %s" % device) - if not os.path.exists(device): - raise ValueError("device file %s does not exist -- abort" % device) + print("Using serial reader interface at port %s" % opts.device) + if not os.path.exists(opts.device): + raise ValueError("device file %s does not exist -- abort" % opts.device) self._sl = serial.Serial( - port=device, + port=opts.device, parity=serial.PARITY_EVEN, bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_TWO, timeout=1, xonxoff=0, rtscts=0, - baudrate=baudrate, + baudrate=opts.baudrate, ) self._rst_pin = rst self._debug = debug