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 |
| 23 | |
| 24 | from pySim.transport import LinkBase |
| 25 | from pySim.exceptions import * |
| 26 | |
| 27 | # HACK: if somebody needs to debug this thing |
| 28 | # log.root.setLevel(log.DEBUG) |
| 29 | |
| 30 | class ModemATCommandLink(LinkBase): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 31 | """Transport Link for 3GPP TS 27.007 compliant modems.""" |
Harald Welte | eb05b2f | 2021-04-10 11:01:56 +0200 | [diff] [blame] | 32 | def __init__(self, device:str='/dev/ttyUSB0', baudrate:int=115200, **kwargs): |
| 33 | super().__init__(**kwargs) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 34 | self._sl = serial.Serial(device, baudrate, timeout=5) |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 35 | self._echo = False # this will be auto-detected by _check_echo() |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 36 | self._device = device |
| 37 | self._atr = None |
| 38 | |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 39 | # Check the AT interface |
| 40 | self._check_echo() |
| 41 | |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 42 | # Trigger initial reset |
| 43 | self.reset_card() |
| 44 | |
| 45 | def __del__(self): |
Vadim Yanitskiy | 52efc03 | 2021-05-02 23:07:38 +0200 | [diff] [blame] | 46 | if hasattr(self, '_sl'): |
| 47 | self._sl.close() |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 48 | |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 49 | def send_at_cmd(self, cmd, timeout=0.2, patience=0.002): |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 50 | # Convert from string to bytes, if needed |
| 51 | bcmd = cmd if type(cmd) is bytes else cmd.encode() |
| 52 | bcmd += b'\r' |
| 53 | |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 54 | # Clean input buffer from previous/unexpected data |
| 55 | self._sl.reset_input_buffer() |
| 56 | |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 57 | # Send command to the modem |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 58 | log.debug('Sending AT command: %s', cmd) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 59 | try: |
| 60 | wlen = self._sl.write(bcmd) |
| 61 | assert(wlen == len(bcmd)) |
| 62 | except: |
| 63 | raise ReaderError('Failed to send AT command: %s' % cmd) |
| 64 | |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 65 | rsp = b'' |
| 66 | its = 1 |
| 67 | t_start = time.time() |
| 68 | while True: |
| 69 | rsp = rsp + self._sl.read(self._sl.in_waiting) |
| 70 | if rsp.endswith(b'OK\r\n'): |
| 71 | log.debug('Command finished with result: OK') |
| 72 | break |
| 73 | if rsp.endswith(b'ERROR\r\n'): |
Robert Falkenberg | 7cb7c78 | 2021-05-06 09:52:31 +0200 | [diff] [blame^] | 74 | log.error('Command finished with result: ERROR') |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 75 | break |
| 76 | if time.time() - t_start >= timeout: |
Robert Falkenberg | 7cb7c78 | 2021-05-06 09:52:31 +0200 | [diff] [blame^] | 77 | log.info('Command finished with timeout >= %ss', timeout) |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 78 | break |
| 79 | time.sleep(patience) |
| 80 | its += 1 |
| 81 | log.debug('Command took %0.6fs (%d cycles a %fs)', time.time() - t_start, its, patience) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 82 | |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 83 | if self._echo: |
| 84 | # Skip echo chars |
| 85 | rsp = rsp[wlen:] |
| 86 | rsp = rsp.strip() |
| 87 | rsp = rsp.split(b'\r\n\r\n') |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 88 | |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 89 | log.debug('Got response from modem: %s', rsp) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 90 | return rsp |
| 91 | |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 92 | def _check_echo(self): |
| 93 | """Verify the correct response to 'AT' command |
| 94 | and detect if inputs are echoed by the device |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 95 | |
Robert Falkenberg | 18fb82b | 2021-05-02 10:21:23 +0200 | [diff] [blame] | 96 | Although echo of inputs can be enabled/disabled via |
| 97 | ATE1/ATE0, respectively, we rather detect the current |
| 98 | configuration of the modem without any change. |
| 99 | """ |
| 100 | # Next command shall not strip the echo from the response |
| 101 | self._echo = False |
| 102 | result = self.send_at_cmd('AT') |
| 103 | |
| 104 | # Verify the response |
| 105 | if len(result) > 0: |
| 106 | if result[-1] == b'OK': |
| 107 | self._echo = False |
| 108 | return |
| 109 | elif result[-1] == b'AT\r\r\nOK': |
| 110 | self._echo = True |
| 111 | return |
| 112 | raise ReaderError('Interface \'%s\' does not respond to \'AT\' command' % self._device) |
| 113 | |
| 114 | def reset_card(self): |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 115 | # Reset the modem, just to be sure |
| 116 | if self.send_at_cmd('ATZ') != [b'OK']: |
| 117 | raise ReaderError('Failed to reset the modem') |
| 118 | |
| 119 | # Make sure that generic SIM access is supported |
| 120 | if self.send_at_cmd('AT+CSIM=?') != [b'OK']: |
| 121 | raise ReaderError('The modem does not seem to support SIM access') |
| 122 | |
| 123 | log.info('Modem at \'%s\' is ready!' % self._device) |
| 124 | |
| 125 | def connect(self): |
| 126 | pass # Nothing to do really ... |
| 127 | |
| 128 | def disconnect(self): |
| 129 | pass # Nothing to do really ... |
| 130 | |
| 131 | def wait_for_card(self, timeout=None, newcardonly=False): |
| 132 | pass # Nothing to do really ... |
| 133 | |
Harald Welte | c34f940 | 2021-04-10 10:55:24 +0200 | [diff] [blame] | 134 | def _send_apdu_raw(self, pdu): |
Robert Falkenberg | 8e15c18 | 2021-05-01 08:07:27 +0200 | [diff] [blame] | 135 | # Make sure pdu has upper case hex digits [A-F] |
| 136 | pdu = pdu.upper() |
| 137 | |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 138 | # Prepare the command as described in 8.17 |
| 139 | cmd = 'AT+CSIM=%d,\"%s\"' % (len(pdu), pdu) |
Robert Falkenberg | 7cb7c78 | 2021-05-06 09:52:31 +0200 | [diff] [blame^] | 140 | log.debug('Sending command: %s', cmd) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 141 | |
| 142 | # Send AT+CSIM command to the modem |
| 143 | # TODO: also handle +CME ERROR: <err> |
| 144 | rsp = self.send_at_cmd(cmd) |
| 145 | if len(rsp) != 2 or rsp[-1] != b'OK': |
| 146 | raise ReaderError('APDU transfer failed: %s' % str(rsp)) |
| 147 | rsp = rsp[0] # Get rid of b'OK' |
| 148 | |
| 149 | # Make sure that the response has format: b'+CSIM: %d,\"%s\"' |
| 150 | try: |
| 151 | result = re.match(b'\+CSIM: (\d+),\"([0-9A-F]+)\"', rsp) |
| 152 | (rsp_pdu_len, rsp_pdu) = result.groups() |
| 153 | except: |
| 154 | raise ReaderError('Failed to parse response from modem: %s' % rsp) |
| 155 | |
| 156 | # TODO: make sure we have at least SW |
Robert Falkenberg | e5a5ffb | 2021-05-06 09:50:43 +0200 | [diff] [blame] | 157 | data = rsp_pdu[:-4].decode().lower() |
| 158 | sw = rsp_pdu[-4:].decode().lower() |
Robert Falkenberg | 7cb7c78 | 2021-05-06 09:52:31 +0200 | [diff] [blame^] | 159 | log.debug('Command response: %s, %s', data, sw) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 160 | return data, sw |