Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | """ pySim: PCSC reader transport link base |
| 4 | """ |
| 5 | |
Harald Welte | baec4e9 | 2023-11-03 11:49:54 +0100 | [diff] [blame] | 6 | import os |
Vadim Yanitskiy | e8c34ca | 2021-05-02 02:29:52 +0200 | [diff] [blame] | 7 | import abc |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 8 | import argparse |
Vadim Yanitskiy | e8c34ca | 2021-05-02 02:29:52 +0200 | [diff] [blame] | 9 | from typing import Optional, Tuple |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 10 | from construct import Construct |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 11 | |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 12 | from pySim.exceptions import * |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame] | 13 | from pySim.construct import filter_dict |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 14 | from pySim.utils import sw_match, b2h, h2b, i2h, Hexstr, SwHexstr, SwMatchstr, ResTuple |
Christian Amsüss | 59f3b11 | 2022-08-12 15:46:52 +0200 | [diff] [blame] | 15 | from pySim.cat import ProactiveCommand, CommandDetails, DeviceIdentities, Result |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 16 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 17 | # |
| 18 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 19 | # Copyright (C) 2021-2023 Harald Welte <laforge@osmocom.org> |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 20 | # |
| 21 | # This program is free software: you can redistribute it and/or modify |
| 22 | # it under the terms of the GNU General Public License as published by |
| 23 | # the Free Software Foundation, either version 2 of the License, or |
| 24 | # (at your option) any later version. |
| 25 | # |
| 26 | # This program is distributed in the hope that it will be useful, |
| 27 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 | # GNU General Public License for more details. |
| 30 | # |
| 31 | # You should have received a copy of the GNU General Public License |
| 32 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 33 | # |
| 34 | |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 35 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 36 | class ApduTracer: |
| 37 | def trace_command(self, cmd): |
| 38 | pass |
| 39 | |
| 40 | def trace_response(self, cmd, sw, resp): |
| 41 | pass |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 42 | |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 43 | class ProactiveHandler(abc.ABC): |
| 44 | """Abstract base class representing the interface of some code that handles |
| 45 | the proactive commands, as returned by the card in responses to the FETCH |
| 46 | command.""" |
Christian Amsüss | 59f3b11 | 2022-08-12 15:46:52 +0200 | [diff] [blame] | 47 | def receive_fetch_raw(self, pcmd: ProactiveCommand, parsed: Hexstr): |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 48 | # try to find a generic handler like handle_SendShortMessage |
| 49 | handle_name = 'handle_%s' % type(parsed).__name__ |
| 50 | if hasattr(self, handle_name): |
| 51 | handler = getattr(self, handle_name) |
| 52 | return handler(pcmd.decoded) |
| 53 | # fall back to common handler |
| 54 | return self.receive_fetch(pcmd) |
| 55 | |
| 56 | def receive_fetch(self, pcmd: ProactiveCommand): |
| 57 | """Default handler for not otherwise handled proactive commands.""" |
| 58 | raise NotImplementedError('No handler method for %s' % pcmd.decoded) |
| 59 | |
| 60 | |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 61 | |
Vadim Yanitskiy | e8c34ca | 2021-05-02 02:29:52 +0200 | [diff] [blame] | 62 | class LinkBase(abc.ABC): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 63 | """Base class for link/transport to card.""" |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 64 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 65 | def __init__(self, sw_interpreter=None, apdu_tracer: Optional[ApduTracer]=None, |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 66 | proactive_handler: Optional[ProactiveHandler]=None): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 67 | self.sw_interpreter = sw_interpreter |
| 68 | self.apdu_tracer = apdu_tracer |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 69 | self.proactive_handler = proactive_handler |
Harald Welte | 4f2c546 | 2021-04-03 11:48:22 +0200 | [diff] [blame] | 70 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 71 | @abc.abstractmethod |
Philipp Maier | 58e89eb | 2023-10-10 11:59:03 +0200 | [diff] [blame] | 72 | def __str__(self) -> str: |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 73 | """Implementation specific method for printing an information to identify the device.""" |
| 74 | |
| 75 | @abc.abstractmethod |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 76 | def _send_apdu_raw(self, pdu: Hexstr) -> ResTuple: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 77 | """Implementation specific method for sending the PDU.""" |
Vadim Yanitskiy | e8c34ca | 2021-05-02 02:29:52 +0200 | [diff] [blame] | 78 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 79 | def set_sw_interpreter(self, interp): |
| 80 | """Set an (optional) status word interpreter.""" |
| 81 | self.sw_interpreter = interp |
Harald Welte | 4f2c546 | 2021-04-03 11:48:22 +0200 | [diff] [blame] | 82 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 83 | @abc.abstractmethod |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 84 | def wait_for_card(self, timeout: Optional[int] = None, newcardonly: bool = False): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 85 | """Wait for a card and connect to it |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 86 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 87 | Args: |
| 88 | timeout : Maximum wait time in seconds (None=no timeout) |
| 89 | newcardonly : Should we wait for a new card, or an already inserted one ? |
| 90 | """ |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 91 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 92 | @abc.abstractmethod |
| 93 | def connect(self): |
| 94 | """Connect to a card immediately |
| 95 | """ |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 96 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 97 | @abc.abstractmethod |
| 98 | def disconnect(self): |
| 99 | """Disconnect from card |
| 100 | """ |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 101 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 102 | @abc.abstractmethod |
| 103 | def reset_card(self): |
| 104 | """Resets the card (power down/up) |
| 105 | """ |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 106 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 107 | def send_apdu_raw(self, pdu: Hexstr) -> ResTuple: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 108 | """Sends an APDU with minimal processing |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 109 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 110 | Args: |
| 111 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 112 | Returns: |
| 113 | tuple(data, sw), where |
| 114 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 115 | sw : string (in hex) of status word (ex. "9000") |
| 116 | """ |
| 117 | if self.apdu_tracer: |
| 118 | self.apdu_tracer.trace_command(pdu) |
| 119 | (data, sw) = self._send_apdu_raw(pdu) |
| 120 | if self.apdu_tracer: |
| 121 | self.apdu_tracer.trace_response(pdu, sw, data) |
| 122 | return (data, sw) |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 123 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 124 | def send_apdu(self, pdu: Hexstr) -> ResTuple: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 125 | """Sends an APDU and auto fetch response data |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 126 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 127 | Args: |
| 128 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 129 | Returns: |
| 130 | tuple(data, sw), where |
| 131 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 132 | sw : string (in hex) of status word (ex. "9000") |
| 133 | """ |
| 134 | data, sw = self.send_apdu_raw(pdu) |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 135 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 136 | # When we have sent the first APDU, the SW may indicate that there are response bytes |
| 137 | # available. There are two SWs commonly used for this 9fxx (sim) and 61xx (usim), where |
| 138 | # xx is the number of response bytes available. |
| 139 | # See also: |
| 140 | if (sw is not None): |
Harald Welte | 4e02436 | 2023-12-04 21:32:46 +0100 | [diff] [blame] | 141 | while ((sw[0:2] == '9f') or (sw[0:2] == '61')): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 142 | # SW1=9F: 3GPP TS 51.011 9.4.1, Responses to commands which are correctly executed |
| 143 | # SW1=61: ISO/IEC 7816-4, Table 5 — General meaning of the interindustry values of SW1-SW2 |
| 144 | pdu_gr = pdu[0:2] + 'c00000' + sw[2:4] |
Harald Welte | 4e02436 | 2023-12-04 21:32:46 +0100 | [diff] [blame] | 145 | d, sw = self.send_apdu_raw(pdu_gr) |
| 146 | data += d |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 147 | if sw[0:2] == '6c': |
| 148 | # SW1=6C: ETSI TS 102 221 Table 7.1: Procedure byte coding |
| 149 | pdu_gr = pdu[0:8] + sw[2:4] |
| 150 | data, sw = self.send_apdu_raw(pdu_gr) |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 151 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 152 | return data, sw |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 153 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 154 | def send_apdu_checksw(self, pdu: Hexstr, sw: SwMatchstr = "9000") -> ResTuple: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 155 | """Sends an APDU and check returned SW |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 156 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 157 | Args: |
| 158 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 159 | sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain |
| 160 | digits using a '?' to add some ambiguity if needed. |
| 161 | Returns: |
| 162 | tuple(data, sw), where |
| 163 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 164 | sw : string (in hex) of status word (ex. "9000") |
| 165 | """ |
| 166 | rv = self.send_apdu(pdu) |
Christian Amsüss | 98552ef | 2022-08-11 19:29:37 +0200 | [diff] [blame] | 167 | last_sw = rv[1] |
Philipp Maier | d4ebb6f | 2018-06-12 17:56:07 +0200 | [diff] [blame] | 168 | |
Christian Amsüss | 98552ef | 2022-08-11 19:29:37 +0200 | [diff] [blame] | 169 | while sw == '9000' and sw_match(last_sw, '91xx'): |
| 170 | # It *was* successful after all -- the extra pieces FETCH handled |
| 171 | # need not concern the caller. |
| 172 | rv = (rv[0], '9000') |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 173 | # proactive sim as per TS 102 221 Setion 7.4.2 |
Christian Amsüss | 59f3b11 | 2022-08-12 15:46:52 +0200 | [diff] [blame] | 174 | # TODO: Check SW manually to avoid recursing on the stack (provided this piece of code stays in this place) |
Christian Amsüss | 98552ef | 2022-08-11 19:29:37 +0200 | [diff] [blame] | 175 | fetch_rv = self.send_apdu_checksw('80120000' + last_sw[2:], sw) |
Christian Amsüss | 59f3b11 | 2022-08-12 15:46:52 +0200 | [diff] [blame] | 176 | # Setting this in case we later decide not to send a terminal |
| 177 | # response immediately unconditionally -- the card may still have |
| 178 | # something pending even though the last command was not processed |
| 179 | # yet. |
Christian Amsüss | 98552ef | 2022-08-11 19:29:37 +0200 | [diff] [blame] | 180 | last_sw = fetch_rv[1] |
Christian Amsüss | 59f3b11 | 2022-08-12 15:46:52 +0200 | [diff] [blame] | 181 | # parse the proactive command |
| 182 | pcmd = ProactiveCommand() |
| 183 | parsed = pcmd.from_tlv(h2b(fetch_rv[0])) |
| 184 | print("FETCH: %s (%s)" % (fetch_rv[0], type(parsed).__name__)) |
| 185 | result = Result() |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 186 | if self.proactive_handler: |
Christian Amsüss | 59f3b11 | 2022-08-12 15:46:52 +0200 | [diff] [blame] | 187 | # Extension point: If this does return a list of TLV objects, |
| 188 | # they could be appended after the Result; if the first is a |
| 189 | # Result, that cuold replace the one built here. |
| 190 | self.proactive_handler.receive_fetch_raw(pcmd, parsed) |
| 191 | result.from_dict({'general_result': 'performed_successfully', 'additional_information': ''}) |
| 192 | else: |
| 193 | result.from_dict({'general_result': 'command_beyond_terminal_capability', 'additional_information': ''}) |
| 194 | |
| 195 | # Send response immediately, thus also flushing out any further |
| 196 | # proactive commands that the card already wants to send |
| 197 | # |
| 198 | # Structure as per TS 102 223 V4.4.0 Section 6.8 |
| 199 | |
| 200 | # The Command Details are echoed from the command that has been processed. |
| 201 | (command_details,) = [c for c in pcmd.decoded.children if isinstance(c, CommandDetails)] |
| 202 | # The Device Identities are fixed. (TS 102 223 V4.0.0 Section 6.8.2) |
| 203 | device_identities = DeviceIdentities() |
| 204 | device_identities.from_dict({'source_dev_id': 'terminal', 'dest_dev_id': 'uicc'}) |
| 205 | |
| 206 | # Testing hint: The value of tail does not influence the behavior |
| 207 | # of an SJA2 that sent ans SMS, so this is implemented only |
| 208 | # following TS 102 223, and not fully tested. |
| 209 | tail = command_details.to_tlv() + device_identities.to_tlv() + result.to_tlv() |
| 210 | # Testing hint: In contrast to the above, this part is positively |
| 211 | # essential to get the SJA2 to provide the later parts of a |
| 212 | # multipart SMS in response to an OTA RFM command. |
| 213 | terminal_response = '80140000' + b2h(len(tail).to_bytes(1, 'big') + tail) |
| 214 | |
| 215 | terminal_response_rv = self.send_apdu(terminal_response) |
| 216 | last_sw = terminal_response_rv[1] |
| 217 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 218 | if not sw_match(rv[1], sw): |
| 219 | raise SwMatchError(rv[1], sw.lower(), self.sw_interpreter) |
| 220 | return rv |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 221 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 222 | def send_apdu_constr(self, cla: Hexstr, ins: Hexstr, p1: Hexstr, p2: Hexstr, cmd_constr: Construct, |
| 223 | cmd_data: Hexstr, resp_constr: Construct) -> Tuple[dict, SwHexstr]: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 224 | """Build and sends an APDU using a 'construct' definition; parses response. |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame] | 225 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 226 | Args: |
| 227 | cla : string (in hex) ISO 7816 class byte |
| 228 | ins : string (in hex) ISO 7816 instruction byte |
| 229 | p1 : string (in hex) ISO 7116 Parameter 1 byte |
| 230 | p2 : string (in hex) ISO 7116 Parameter 2 byte |
| 231 | cmd_cosntr : defining how to generate binary APDU command data |
| 232 | cmd_data : command data passed to cmd_constr |
| 233 | resp_cosntr : defining how to decode binary APDU response data |
| 234 | Returns: |
| 235 | Tuple of (decoded_data, sw) |
| 236 | """ |
| 237 | cmd = cmd_constr.build(cmd_data) if cmd_data else '' |
| 238 | p3 = i2h([len(cmd)]) |
| 239 | pdu = ''.join([cla, ins, p1, p2, p3, b2h(cmd)]) |
| 240 | (data, sw) = self.send_apdu(pdu) |
| 241 | if data: |
| 242 | # filter the resulting dict to avoid '_io' members inside |
| 243 | rsp = filter_dict(resp_constr.parse(h2b(data))) |
| 244 | else: |
| 245 | rsp = None |
| 246 | return (rsp, sw) |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame] | 247 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 248 | def send_apdu_constr_checksw(self, cla: Hexstr, ins: Hexstr, p1: Hexstr, p2: Hexstr, |
| 249 | cmd_constr: Construct, cmd_data: Hexstr, resp_constr: Construct, |
| 250 | sw_exp: SwMatchstr="9000") -> Tuple[dict, SwHexstr]: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 251 | """Build and sends an APDU using a 'construct' definition; parses response. |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame] | 252 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 253 | Args: |
| 254 | cla : string (in hex) ISO 7816 class byte |
| 255 | ins : string (in hex) ISO 7816 instruction byte |
| 256 | p1 : string (in hex) ISO 7116 Parameter 1 byte |
| 257 | p2 : string (in hex) ISO 7116 Parameter 2 byte |
| 258 | cmd_cosntr : defining how to generate binary APDU command data |
| 259 | cmd_data : command data passed to cmd_constr |
| 260 | resp_cosntr : defining how to decode binary APDU response data |
| 261 | exp_sw : string (in hex) of status word (ex. "9000") |
| 262 | Returns: |
| 263 | Tuple of (decoded_data, sw) |
| 264 | """ |
| 265 | (rsp, sw) = self.send_apdu_constr(cla, ins, |
| 266 | p1, p2, cmd_constr, cmd_data, resp_constr) |
| 267 | if not sw_match(sw, sw_exp): |
| 268 | raise SwMatchError(sw, sw_exp.lower(), self.sw_interpreter) |
| 269 | return (rsp, sw) |
| 270 | |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame] | 271 | |
Philipp Maier | 8c82378 | 2023-10-23 10:44:44 +0200 | [diff] [blame] | 272 | def argparse_add_reader_args(arg_parser: argparse.ArgumentParser): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 273 | """Add all reader related arguments to the given argparse.Argumentparser instance.""" |
Philipp Maier | 8c82378 | 2023-10-23 10:44:44 +0200 | [diff] [blame] | 274 | from pySim.transport.serial import SerialSimLink |
| 275 | from pySim.transport.pcsc import PcscSimLink |
| 276 | from pySim.transport.modem_atcmd import ModemATCommandLink |
| 277 | from pySim.transport.calypso import CalypsoSimLink |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 278 | |
Philipp Maier | 8c82378 | 2023-10-23 10:44:44 +0200 | [diff] [blame] | 279 | SerialSimLink.argparse_add_reader_args(arg_parser) |
| 280 | PcscSimLink.argparse_add_reader_args(arg_parser) |
| 281 | ModemATCommandLink.argparse_add_reader_args(arg_parser) |
| 282 | CalypsoSimLink.argparse_add_reader_args(arg_parser) |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 283 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 284 | return arg_parser |
| 285 | |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 286 | |
Philipp Maier | af4e5bb | 2023-10-10 12:29:14 +0200 | [diff] [blame] | 287 | def init_reader(opts, **kwargs) -> LinkBase: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 288 | """ |
| 289 | Init card reader driver |
| 290 | """ |
Harald Welte | ad00279 | 2023-11-03 11:42:18 +0100 | [diff] [blame] | 291 | if opts.pcsc_dev is not None or opts.pcsc_regex is not None: |
Philipp Maier | af4e5bb | 2023-10-10 12:29:14 +0200 | [diff] [blame] | 292 | from pySim.transport.pcsc import PcscSimLink |
Harald Welte | 0f177c1 | 2023-12-17 12:38:29 +0100 | [diff] [blame] | 293 | sl = PcscSimLink(opts, **kwargs) |
Philipp Maier | af4e5bb | 2023-10-10 12:29:14 +0200 | [diff] [blame] | 294 | elif opts.osmocon_sock is not None: |
Philipp Maier | af4e5bb | 2023-10-10 12:29:14 +0200 | [diff] [blame] | 295 | from pySim.transport.calypso import CalypsoSimLink |
Harald Welte | 0f177c1 | 2023-12-17 12:38:29 +0100 | [diff] [blame] | 296 | sl = CalypsoSimLink(opts, **kwargs) |
Philipp Maier | af4e5bb | 2023-10-10 12:29:14 +0200 | [diff] [blame] | 297 | elif opts.modem_dev is not None: |
Philipp Maier | af4e5bb | 2023-10-10 12:29:14 +0200 | [diff] [blame] | 298 | from pySim.transport.modem_atcmd import ModemATCommandLink |
Harald Welte | 0f177c1 | 2023-12-17 12:38:29 +0100 | [diff] [blame] | 299 | sl = ModemATCommandLink(opts, **kwargs) |
Philipp Maier | af4e5bb | 2023-10-10 12:29:14 +0200 | [diff] [blame] | 300 | else: # Serial reader is default |
Harald Welte | 2d44f03 | 2023-11-03 08:45:49 +0100 | [diff] [blame] | 301 | print("No reader/driver specified; falling back to default (Serial reader)") |
Philipp Maier | af4e5bb | 2023-10-10 12:29:14 +0200 | [diff] [blame] | 302 | from pySim.transport.serial import SerialSimLink |
Harald Welte | 0f177c1 | 2023-12-17 12:38:29 +0100 | [diff] [blame] | 303 | sl = SerialSimLink(opts, **kwargs) |
Harald Welte | baec4e9 | 2023-11-03 11:49:54 +0100 | [diff] [blame] | 304 | |
| 305 | if os.environ.get('PYSIM_INTEGRATION_TEST') == "1": |
| 306 | print("Using %s reader interface" % (sl.name)) |
| 307 | else: |
| 308 | print("Using reader %s" % sl) |
| 309 | |
Philipp Maier | af4e5bb | 2023-10-10 12:29:14 +0200 | [diff] [blame] | 310 | return sl |