From baec4e9c81d42a42799062ac7595bc74f0b2b5f5 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 3 Nov 2023 11:49:54 +0100 Subject: [PATCH] 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 --- pySim/transport/__init__.py | 7 +++++++ pySim/transport/calypso.py | 5 +---- pySim/transport/modem_atcmd.py | 6 +----- pySim/transport/pcsc.py | 7 +------ pySim/transport/serial.py | 5 +---- 5 files changed, 11 insertions(+), 19 deletions(-) diff --git a/pySim/transport/__init__.py b/pySim/transport/__init__.py index d075878c..59d45546 100644 --- a/pySim/transport/__init__.py +++ b/pySim/transport/__init__.py @@ -3,6 +3,7 @@ """ pySim: PCSC reader transport link base """ +import os import abc import argparse 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)") from pySim.transport.serial import SerialSimLink 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 diff --git a/pySim/transport/calypso.py b/pySim/transport/calypso.py index 01bbe232..dd30f800 100644 --- a/pySim/transport/calypso.py +++ b/pySim/transport/calypso.py @@ -76,14 +76,11 @@ class L1CTLMessageSIM(L1CTLMessage): class CalypsoSimLink(LinkBase): """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): sock_path = opts.osmocon_sock 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 if not os.path.exists(sock_path): raise ReaderError( diff --git a/pySim/transport/modem_atcmd.py b/pySim/transport/modem_atcmd.py index 9a4f0a56..88e6253f 100644 --- a/pySim/transport/modem_atcmd.py +++ b/pySim/transport/modem_atcmd.py @@ -21,7 +21,6 @@ import serial import time import re import argparse -import os from typing import Optional from pySim.utils import Hexstr, ResTuple @@ -34,16 +33,13 @@ from pySim.exceptions import * class ModemATCommandLink(LinkBase): """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', 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)") - 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._echo = False # this will be auto-detected by _check_echo() self._device = device diff --git a/pySim/transport/pcsc.py b/pySim/transport/pcsc.py index b1c424f6..8a99e9f5 100644 --- a/pySim/transport/pcsc.py +++ b/pySim/transport/pcsc.py @@ -18,7 +18,6 @@ # import argparse -import os import re from typing import Optional, Union @@ -34,6 +33,7 @@ from pySim.utils import h2i, i2h, Hexstr, ResTuple class PcscSimLink(LinkBase): """ pySim: PCSC reader transport link.""" + name = 'PC/SC' def __init__(self, opts: argparse.Namespace = argparse.Namespace(pcsc_dev=0), **kwargs): super().__init__(**kwargs) @@ -57,11 +57,6 @@ class PcscSimLink(LinkBase): 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): try: # FIXME: this causes multiple warnings in Python 3.5.3 diff --git a/pySim/transport/serial.py b/pySim/transport/serial.py index e6803bef..fbf907f5 100644 --- a/pySim/transport/serial.py +++ b/pySim/transport/serial.py @@ -29,14 +29,11 @@ from pySim.utils import h2b, b2h, Hexstr, ResTuple class SerialSimLink(LinkBase): """ 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', 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" % opts.device) if not os.path.exists(opts.device): raise ValueError("device file %s does not exist -- abort" % opts.device) self._sl = serial.Serial(