transport: Move printing of reader number/name to generic code

Let's avoid copy+pasting print statements everywhere.  The instances
do already have a __str__ method for the purpose of printing their name in a
generic way.

Change-Id: I663a9ea69bf7e7aaa6502896b6a71ef692f8d844
This commit is contained in:
Harald Welte
2023-11-03 11:49:54 +01:00
parent ad002797e2
commit baec4e9c81
5 changed files with 11 additions and 19 deletions

View File

@@ -3,6 +3,7 @@
""" pySim: PCSC reader transport link base """ pySim: PCSC reader transport link base
""" """
import os
import abc import abc
import argparse import argparse
from typing import Optional, Tuple from typing import Optional, Tuple
@@ -300,4 +301,10 @@ def init_reader(opts, **kwargs) -> LinkBase:
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(opts, **kwargs) sl = SerialSimLink(opts, **kwargs)
if os.environ.get('PYSIM_INTEGRATION_TEST') == "1":
print("Using %s reader interface" % (sl.name))
else:
print("Using reader %s" % sl)
return sl return sl

View File

@@ -76,14 +76,11 @@ class L1CTLMessageSIM(L1CTLMessage):
class CalypsoSimLink(LinkBase): class CalypsoSimLink(LinkBase):
"""Transport Link for Calypso based phones.""" """Transport Link for Calypso based phones."""
name = 'Calypso-based (OsmocomBB) reader'
def __init__(self, opts: argparse.Namespace = argparse.Namespace(osmocon_sock="/tmp/osmocom_l2"), **kwargs): def __init__(self, opts: argparse.Namespace = argparse.Namespace(osmocon_sock="/tmp/osmocom_l2"), **kwargs):
sock_path = opts.osmocon_sock sock_path = opts.osmocon_sock
super().__init__(**kwargs) super().__init__(**kwargs)
if os.environ.get('PYSIM_INTEGRATION_TEST') == "1":
print("Using Calypso-based (OsmocomBB) reader interface")
else:
print("Using Calypso-based (OsmocomBB) reader at socket %s" % sock_path)
# Make sure that a given socket path exists # Make sure that a given socket path exists
if not os.path.exists(sock_path): if not os.path.exists(sock_path):
raise ReaderError( raise ReaderError(

View File

@@ -21,7 +21,6 @@ import serial
import time import time
import re import re
import argparse import argparse
import os
from typing import Optional from typing import Optional
from pySim.utils import Hexstr, ResTuple from pySim.utils import Hexstr, ResTuple
@@ -34,16 +33,13 @@ 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."""
name = "modem for Generic SIM Access (3GPP TS 27.007)"
def __init__(self, opts: argparse.Namespace = argparse.Namespace(modem_dev='/dev/ttyUSB0', def __init__(self, opts: argparse.Namespace = argparse.Namespace(modem_dev='/dev/ttyUSB0',
modem_baud=115200), **kwargs): modem_baud=115200), **kwargs):
device = opts.modem_dev device = opts.modem_dev
baudrate = opts.modem_baud baudrate = opts.modem_baud
super().__init__(**kwargs) super().__init__(**kwargs)
if os.environ.get('PYSIM_INTEGRATION_TEST') == "1":
print("Using modem for Generic SIM Access (3GPP TS 27.007)")
else:
print("Using modem for Generic SIM Access (3GPP TS 27.007) at port %s" % device)
self._sl = serial.Serial(device, baudrate, timeout=5) self._sl = serial.Serial(device, baudrate, timeout=5)
self._echo = False # this will be auto-detected by _check_echo() self._echo = False # this will be auto-detected by _check_echo()
self._device = device self._device = device

View File

@@ -18,7 +18,6 @@
# #
import argparse import argparse
import os
import re import re
from typing import Optional, Union from typing import Optional, Union
@@ -34,6 +33,7 @@ from pySim.utils import h2i, i2h, Hexstr, ResTuple
class PcscSimLink(LinkBase): class PcscSimLink(LinkBase):
""" pySim: PCSC reader transport link.""" """ pySim: PCSC reader transport link."""
name = 'PC/SC'
def __init__(self, opts: argparse.Namespace = argparse.Namespace(pcsc_dev=0), **kwargs): def __init__(self, opts: argparse.Namespace = argparse.Namespace(pcsc_dev=0), **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
@@ -57,11 +57,6 @@ class PcscSimLink(LinkBase):
self._con = self._reader.createConnection() self._con = self._reader.createConnection()
if os.environ.get('PYSIM_INTEGRATION_TEST') == "1":
print("Using PC/SC reader interface")
else:
print("Using PC/SC reader %s" % self)
def __del__(self): def __del__(self):
try: try:
# FIXME: this causes multiple warnings in Python 3.5.3 # FIXME: this causes multiple warnings in Python 3.5.3

View File

@@ -29,14 +29,11 @@ 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"""
name = 'Serial'
def __init__(self, opts = argparse.Namespace(device='/dev/ttyUSB0', baudrate=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":
print("Using serial reader interface")
else:
print("Using serial reader interface at port %s" % opts.device)
if not os.path.exists(opts.device): if not os.path.exists(opts.device):
raise ValueError("device file %s does not exist -- abort" % opts.device) raise ValueError("device file %s does not exist -- abort" % opts.device)
self._sl = serial.Serial( self._sl = serial.Serial(