pylint: transport/modem_atcmd.py

pySim/transport/modem_atcmd.py:70:0: C0325: Unnecessary parens after 'assert' keyword (superfluous-parens)
pySim/transport/modem_atcmd.py:28:0: W0401: Wildcard import pySim.exceptions (wildcard-import)
pySim/transport/modem_atcmd.py:60:22: C0123: Use isinstance() rather than type() for a typecheck. (unidiomatic-typecheck)
pySim/transport/modem_atcmd.py:72:12: W0707: Consider explicitly re-raising using 'except Exception as exc' and 'raise ReaderError('Failed to send AT command: %s' % cmd) from exc' (raise-missing-from)
pySim/transport/modem_atcmd.py:120:12: R1705: Unnecessary "elif" after "return", remove the leading "el" from "elif" (no-else-return)
pySim/transport/modem_atcmd.py:138:8: W1201: Use lazy % formatting in logging functions (logging-not-lazy)
pySim/transport/modem_atcmd.py:170:12: W0707: Consider explicitly re-raising using 'except Exception as exc' and 'raise ReaderError('Failed to parse response from modem: %s' % rsp) from exc' (raise-missing-from)
pySim/transport/modem_atcmd.py:168:13: W0612: Unused variable 'rsp_pdu_len' (unused-variable)
pySim/transport/modem_atcmd.py:21:0: C0411: standard import "import time" should be placed before "import serial" (wrong-import-order)
pySim/transport/modem_atcmd.py:22:0: C0411: standard import "import re" should be placed before "import serial" (wrong-import-order)
pySim/transport/modem_atcmd.py:23:0: C0411: standard import "import argparse" should be placed before "import serial" (wrong-import-order)
pySim/transport/modem_atcmd.py:24:0: C0411: standard import "from typing import Optional" should be placed before "import serial" (wrong-import-order)
pySim/transport/modem_atcmd.py:28:0: W0614: Unused import(s) NoCardError and SwMatchError from wildcard import of pySim.exceptions (unused-wildcard-import)

Change-Id: I2c8994eabd973b65132af1030429b1021d0c20df
This commit is contained in:
Harald Welte
2024-02-04 22:36:12 +01:00
parent c4d80870e8
commit 181becb676

View File

@@ -17,15 +17,15 @@
# #
import logging as log import logging as log
import serial
import time import time
import re import re
import argparse import argparse
from typing import Optional from typing import Optional
import serial
from pySim.utils import Hexstr, ResTuple from pySim.utils import Hexstr, ResTuple
from pySim.transport import LinkBase from pySim.transport import LinkBase
from pySim.exceptions import * from pySim.exceptions import ReaderError, ProtocolError
# HACK: if somebody needs to debug this thing # HACK: if somebody needs to debug this thing
# log.root.setLevel(log.DEBUG) # log.root.setLevel(log.DEBUG)
@@ -57,7 +57,7 @@ class ModemATCommandLink(LinkBase):
def send_at_cmd(self, cmd, timeout=0.2, patience=0.002): def send_at_cmd(self, cmd, timeout=0.2, patience=0.002):
# Convert from string to bytes, if needed # Convert from string to bytes, if needed
bcmd = cmd if type(cmd) is bytes else cmd.encode() bcmd = cmd if isinstance(cmd, bytes) else cmd.encode()
bcmd += b'\r' bcmd += b'\r'
# Clean input buffer from previous/unexpected data # Clean input buffer from previous/unexpected data
@@ -67,9 +67,9 @@ class ModemATCommandLink(LinkBase):
log.debug('Sending AT command: %s', cmd) log.debug('Sending AT command: %s', cmd)
try: try:
wlen = self._sl.write(bcmd) wlen = self._sl.write(bcmd)
assert(wlen == len(bcmd)) assert wlen == len(bcmd)
except: except Exception as exc:
raise ReaderError('Failed to send AT command: %s' % cmd) raise ReaderError('Failed to send AT command: %s' % cmd) from exc
rsp = b'' rsp = b''
its = 1 its = 1
@@ -91,8 +91,7 @@ class ModemATCommandLink(LinkBase):
break break
time.sleep(patience) time.sleep(patience)
its += 1 its += 1
log.debug('Command took %0.6fs (%d cycles a %fs)', log.debug('Command took %0.6fs (%d cycles a %fs)', time.time() - t_start, its, patience)
time.time() - t_start, its, patience)
if self._echo: if self._echo:
# Skip echo chars # Skip echo chars
@@ -120,11 +119,10 @@ class ModemATCommandLink(LinkBase):
if result[-1] == b'OK': if result[-1] == b'OK':
self._echo = False self._echo = False
return return
elif result[-1] == b'AT\r\r\nOK': if result[-1] == b'AT\r\r\nOK':
self._echo = True self._echo = True
return return
raise ReaderError( raise ReaderError('Interface \'%s\' does not respond to \'AT\' command' % self._device)
'Interface \'%s\' does not respond to \'AT\' command' % self._device)
def reset_card(self): def reset_card(self):
# Reset the modem, just to be sure # Reset the modem, just to be sure
@@ -135,7 +133,7 @@ class ModemATCommandLink(LinkBase):
if self.send_at_cmd('AT+CSIM=?') != [b'OK']: if self.send_at_cmd('AT+CSIM=?') != [b'OK']:
raise ReaderError('The modem does not seem to support SIM access') raise ReaderError('The modem does not seem to support SIM access')
log.info('Modem at \'%s\' is ready!' % self._device) log.info('Modem at \'%s\' is ready!', self._device)
def connect(self): def connect(self):
pass # Nothing to do really ... pass # Nothing to do really ...
@@ -165,9 +163,9 @@ class ModemATCommandLink(LinkBase):
# Make sure that the response has format: b'+CSIM: %d,\"%s\"' # Make sure that the response has format: b'+CSIM: %d,\"%s\"'
try: try:
result = re.match(b'\+CSIM: (\d+),\"([0-9A-F]+)\"', rsp) result = re.match(b'\+CSIM: (\d+),\"([0-9A-F]+)\"', rsp)
(rsp_pdu_len, rsp_pdu) = result.groups() (_rsp_pdu_len, rsp_pdu) = result.groups()
except: except Exception as exc:
raise ReaderError('Failed to parse response from modem: %s' % rsp) raise ReaderError('Failed to parse response from modem: %s' % rsp) from exc
# TODO: make sure we have at least SW # TODO: make sure we have at least SW
data = rsp_pdu[:-4].decode().lower() data = rsp_pdu[:-4].decode().lower()