Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 3 | # Copyright (C) 2020 Vadim Yanitskiy <axilirator@gmail.com> |
| 4 | # |
| 5 | # This program is free software: you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation, either version 2 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | # |
| 18 | |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 19 | import logging as log |
| 20 | import serial |
| 21 | import time |
| 22 | import re |
Philipp Maier | 8c82378 | 2023-10-23 10:44:44 +0200 | [diff] [blame] | 23 | import argparse |
Philipp Maier | 4af63dc | 2023-10-26 12:17:32 +0200 | [diff] [blame] | 24 | import os |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 25 | from typing import Optional |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 26 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 27 | from pySim.utils import Hexstr, ResTuple |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 28 | from pySim.transport import LinkBase |
| 29 | from pySim.exceptions import * |
| 30 | |
| 31 | # HACK: if somebody needs to debug this thing |
| 32 | # log.root.setLevel(log.DEBUG) |
| 33 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 34 | |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 35 | class ModemATCommandLink(LinkBase): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 36 | """Transport Link for 3GPP TS 27.007 compliant modems.""" |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 37 | |
Harald Welte | 0f177c1 | 2023-12-17 12:38:29 +0100 | [diff] [blame^] | 38 | def __init__(self, opts: argparse.Namespace = argparse.Namespace(modem_dev='/dev/ttyUSB0', |
| 39 | modem_baud=115200), **kwargs): |
| 40 | device = opts.modem_dev |
| 41 | baudrate = opts.modem_baud |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 42 | super().__init__(**kwargs) |
Philipp Maier | 4af63dc | 2023-10-26 12:17:32 +0200 | [diff] [blame] | 43 | if os.environ.get('PYSIM_INTEGRATION_TEST') == "1": |
| 44 | print("Using modem for Generic SIM Access (3GPP TS 27.007)") |
| 45 | else: |
| 46 | print("Using modem for Generic SIM Access (3GPP TS 27.007) at port %s" % device) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 47 | self._sl = serial.Serial(device, baudrate, timeout=5) |
| 48 | self._echo = False # this will be auto-detected by _check_echo() |
| 49 | self._device = device |
| 50 | self._atr = None |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 51 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 52 | # Check the AT interface |
| 53 | self._check_echo() |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 54 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 55 | # Trigger initial reset |
| 56 | self.reset_card() |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 57 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 58 | def __del__(self): |
| 59 | if hasattr(self, '_sl'): |
| 60 | self._sl.close() |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 61 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 62 | def send_at_cmd(self, cmd, timeout=0.2, patience=0.002): |
| 63 | # Convert from string to bytes, if needed |
| 64 | bcmd = cmd if type(cmd) is bytes else cmd.encode() |
| 65 | bcmd += b'\r' |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 66 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 67 | # Clean input buffer from previous/unexpected data |
| 68 | self._sl.reset_input_buffer() |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 69 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 70 | # Send command to the modem |
| 71 | log.debug('Sending AT command: %s', cmd) |
| 72 | try: |
| 73 | wlen = self._sl.write(bcmd) |
| 74 | assert(wlen == len(bcmd)) |
| 75 | except: |
| 76 | raise ReaderError('Failed to send AT command: %s' % cmd) |
Robert Falkenberg | dddcc60 | 2021-05-06 09:55:57 +0200 | [diff] [blame] | 77 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 78 | rsp = b'' |
| 79 | its = 1 |
| 80 | t_start = time.time() |
| 81 | while True: |
| 82 | rsp = rsp + self._sl.read(self._sl.in_waiting) |
| 83 | lines = rsp.split(b'\r\n') |
| 84 | if len(lines) >= 2: |
| 85 | res = lines[-2] |
| 86 | if res == b'OK': |
| 87 | log.debug('Command finished with result: %s', res) |
| 88 | break |
| 89 | if res == b'ERROR' or res.startswith(b'+CME ERROR:'): |
| 90 | log.error('Command failed with result: %s', res) |
| 91 | break |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 92 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 93 | if time.time() - t_start >= timeout: |
| 94 | log.info('Command finished with timeout >= %ss', timeout) |
| 95 | break |
| 96 | time.sleep(patience) |
| 97 | its += 1 |
| 98 | log.debug('Command took %0.6fs (%d cycles a %fs)', |
| 99 | time.time() - t_start, its, patience) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 100 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 101 | if self._echo: |
| 102 | # Skip echo chars |
| 103 | rsp = rsp[wlen:] |
| 104 | rsp = rsp.strip() |
| 105 | rsp = rsp.split(b'\r\n\r\n') |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 106 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 107 | log.debug('Got response from modem: %s', rsp) |
| 108 | return rsp |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 109 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 110 | def _check_echo(self): |
| 111 | """Verify the correct response to 'AT' command |
| 112 | and detect if inputs are echoed by the device |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 113 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 114 | Although echo of inputs can be enabled/disabled via |
| 115 | ATE1/ATE0, respectively, we rather detect the current |
| 116 | configuration of the modem without any change. |
| 117 | """ |
| 118 | # Next command shall not strip the echo from the response |
| 119 | self._echo = False |
| 120 | result = self.send_at_cmd('AT') |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 121 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 122 | # Verify the response |
| 123 | if len(result) > 0: |
| 124 | if result[-1] == b'OK': |
| 125 | self._echo = False |
| 126 | return |
| 127 | elif result[-1] == b'AT\r\r\nOK': |
| 128 | self._echo = True |
| 129 | return |
| 130 | raise ReaderError( |
| 131 | 'Interface \'%s\' does not respond to \'AT\' command' % self._device) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 132 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 133 | def reset_card(self): |
| 134 | # Reset the modem, just to be sure |
| 135 | if self.send_at_cmd('ATZ') != [b'OK']: |
| 136 | raise ReaderError('Failed to reset the modem') |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 137 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 138 | # Make sure that generic SIM access is supported |
| 139 | if self.send_at_cmd('AT+CSIM=?') != [b'OK']: |
| 140 | raise ReaderError('The modem does not seem to support SIM access') |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 141 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 142 | log.info('Modem at \'%s\' is ready!' % self._device) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 143 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 144 | def connect(self): |
| 145 | pass # Nothing to do really ... |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 146 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 147 | def disconnect(self): |
| 148 | pass # Nothing to do really ... |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 149 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 150 | def wait_for_card(self, timeout: Optional[int] = None, newcardonly: bool = False): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 151 | pass # Nothing to do really ... |
Robert Falkenberg | 8e15c18 | 2021-05-01 08:07:27 +0200 | [diff] [blame] | 152 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 153 | def _send_apdu_raw(self, pdu: Hexstr) -> ResTuple: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 154 | # Make sure pdu has upper case hex digits [A-F] |
| 155 | pdu = pdu.upper() |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 156 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 157 | # Prepare the command as described in 8.17 |
| 158 | cmd = 'AT+CSIM=%d,\"%s\"' % (len(pdu), pdu) |
| 159 | log.debug('Sending command: %s', cmd) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 160 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 161 | # Send AT+CSIM command to the modem |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 162 | rsp = self.send_at_cmd(cmd) |
Tobias Engel | d70ac22 | 2023-05-29 21:20:59 +0200 | [diff] [blame] | 163 | if rsp[-1].startswith(b'+CME ERROR:'): |
| 164 | raise ProtocolError('AT+CSIM failed with: %s' % str(rsp)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 165 | if len(rsp) != 2 or rsp[-1] != b'OK': |
| 166 | raise ReaderError('APDU transfer failed: %s' % str(rsp)) |
| 167 | rsp = rsp[0] # Get rid of b'OK' |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 168 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 169 | # Make sure that the response has format: b'+CSIM: %d,\"%s\"' |
| 170 | try: |
| 171 | result = re.match(b'\+CSIM: (\d+),\"([0-9A-F]+)\"', rsp) |
| 172 | (rsp_pdu_len, rsp_pdu) = result.groups() |
| 173 | except: |
| 174 | raise ReaderError('Failed to parse response from modem: %s' % rsp) |
| 175 | |
| 176 | # TODO: make sure we have at least SW |
| 177 | data = rsp_pdu[:-4].decode().lower() |
| 178 | sw = rsp_pdu[-4:].decode().lower() |
| 179 | log.debug('Command response: %s, %s', data, sw) |
| 180 | return data, sw |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 181 | |
Philipp Maier | 58e89eb | 2023-10-10 11:59:03 +0200 | [diff] [blame] | 182 | def __str__(self) -> str: |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 183 | return "modem:%s" % self._device |
Philipp Maier | 8c82378 | 2023-10-23 10:44:44 +0200 | [diff] [blame] | 184 | |
| 185 | @staticmethod |
| 186 | def argparse_add_reader_args(arg_parser: argparse.ArgumentParser): |
| 187 | modem_group = arg_parser.add_argument_group('AT Command Modem Reader') |
| 188 | modem_group.add_argument('--modem-device', dest='modem_dev', metavar='DEV', default=None, |
| 189 | help='Serial port of modem for Generic SIM Access (3GPP TS 27.007)') |
| 190 | modem_group.add_argument('--modem-baud', type=int, metavar='BAUD', default=115200, |
| 191 | help='Baud rate used for modem port') |