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