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