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
This commit is contained in:
Harald Welte
2023-12-17 12:38:29 +01:00
committed by laforge
parent c108595041
commit 0f177c1d29
6 changed files with 23 additions and 21 deletions

View File

@@ -38,7 +38,7 @@ class ApduPrintTracer(ApduTracer):
pass pass
def connect_to_card(slot_nr:int): 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() tp.connect()
scc = SimCardCommands(tp) scc = SimCardCommands(tp)

View File

@@ -289,17 +289,15 @@ def init_reader(opts, **kwargs) -> LinkBase:
""" """
if opts.pcsc_dev is not None: if opts.pcsc_dev is not None:
from pySim.transport.pcsc import PcscSimLink from pySim.transport.pcsc import PcscSimLink
sl = PcscSimLink(opts.pcsc_dev, **kwargs) sl = PcscSimLink(opts, **kwargs)
elif opts.osmocon_sock is not None: elif opts.osmocon_sock is not None:
from pySim.transport.calypso import CalypsoSimLink 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: elif opts.modem_dev is not None:
from pySim.transport.modem_atcmd import ModemATCommandLink from pySim.transport.modem_atcmd import ModemATCommandLink
sl = ModemATCommandLink( sl = ModemATCommandLink(opts, **kwargs)
device=opts.modem_dev, baudrate=opts.modem_baud, **kwargs)
else: # Serial reader is default else: # Serial reader is default
print("No reader/driver specified; falling back to default (Serial reader)") print("No reader/driver specified; falling back to default (Serial reader)")
from pySim.transport.serial import SerialSimLink from pySim.transport.serial import SerialSimLink
sl = SerialSimLink(device=opts.device, sl = SerialSimLink(opts, **kwargs)
baudrate=opts.baudrate, **kwargs)
return sl return sl

View File

@@ -77,7 +77,8 @@ class L1CTLMessageSIM(L1CTLMessage):
class CalypsoSimLink(LinkBase): class CalypsoSimLink(LinkBase):
"""Transport Link for Calypso based phones.""" """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) super().__init__(**kwargs)
if os.environ.get('PYSIM_INTEGRATION_TEST') == "1": if os.environ.get('PYSIM_INTEGRATION_TEST') == "1":
print("Using Calypso-based (OsmocomBB) reader interface") print("Using Calypso-based (OsmocomBB) reader interface")

View File

@@ -35,7 +35,10 @@ from pySim.exceptions import *
class ModemATCommandLink(LinkBase): class ModemATCommandLink(LinkBase):
"""Transport Link for 3GPP TS 27.007 compliant modems.""" """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) super().__init__(**kwargs)
if os.environ.get('PYSIM_INTEGRATION_TEST') == "1": if os.environ.get('PYSIM_INTEGRATION_TEST') == "1":
print("Using modem for Generic SIM Access (3GPP TS 27.007)") print("Using modem for Generic SIM Access (3GPP TS 27.007)")

View File

@@ -34,18 +34,18 @@ from pySim.utils import h2i, i2h, Hexstr, ResTuple
class PcscSimLink(LinkBase): class PcscSimLink(LinkBase):
""" pySim: PCSC reader transport link.""" """ 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) super().__init__(**kwargs)
if os.environ.get('PYSIM_INTEGRATION_TEST') == "1": if os.environ.get('PYSIM_INTEGRATION_TEST') == "1":
print("Using PC/SC reader interface") print("Using PC/SC reader interface")
else: else:
print("Using PC/SC reader number %u" % reader_number) print("Using PC/SC reader number %u" % opts.pcsc_dev)
r = readers() r = readers()
if reader_number >= len(r): if opts.pcsc_dev >= len(r):
raise ReaderError('No reader found for number %d' % reader_number) raise ReaderError('No reader found for number %d' % opts.pcsc_dev)
self._reader = r[reader_number] self._reader = r[opts.pcsc_dev]
self._con = self._reader.createConnection() self._con = self._reader.createConnection()
self._reader_number = reader_number self._reader_number = opts.pcsc_dev
def __del__(self): def __del__(self):
try: try:

View File

@@ -30,24 +30,24 @@ from pySim.utils import h2b, b2h, Hexstr, ResTuple
class SerialSimLink(LinkBase): class SerialSimLink(LinkBase):
""" pySim: Transport Link for serial (RS232) based readers included with simcard""" """ 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): debug: bool = False, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
if os.environ.get('PYSIM_INTEGRATION_TEST') == "1": if os.environ.get('PYSIM_INTEGRATION_TEST') == "1":
print("Using serial reader interface") print("Using serial reader interface")
else: else:
print("Using serial reader interface at port %s" % device) print("Using serial reader interface at port %s" % opts.device)
if not os.path.exists(device): if not os.path.exists(opts.device):
raise ValueError("device file %s does not exist -- abort" % device) raise ValueError("device file %s does not exist -- abort" % opts.device)
self._sl = serial.Serial( self._sl = serial.Serial(
port=device, port=opts.device,
parity=serial.PARITY_EVEN, parity=serial.PARITY_EVEN,
bytesize=serial.EIGHTBITS, bytesize=serial.EIGHTBITS,
stopbits=serial.STOPBITS_TWO, stopbits=serial.STOPBITS_TWO,
timeout=1, timeout=1,
xonxoff=0, xonxoff=0,
rtscts=0, rtscts=0,
baudrate=baudrate, baudrate=opts.baudrate,
) )
self._rst_pin = rst self._rst_pin = rst
self._debug = debug self._debug = debug