From 8f594f95ec5a536faf504f6f3fbb90252625cc01 Mon Sep 17 00:00:00 2001 From: Sergey Ninua Date: Thu, 12 Jul 2018 16:18:52 +0300 Subject: [PATCH 1/2] Fix supportedCommands property for Siemens modems --- gsmmodem/modem.py | 4 ++-- gsmmodem/util.py | 14 ++++++++++++++ test/test_modem.py | 2 ++ test/test_util.py | 8 +++++++- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/gsmmodem/modem.py b/gsmmodem/modem.py index 8fe65f6..4dd6b06 100644 --- a/gsmmodem/modem.py +++ b/gsmmodem/modem.py @@ -9,7 +9,7 @@ from .serial_comms import SerialComms from .exceptions import CommandError, InvalidStateException, CmeError, CmsError, InterruptedException, TimeoutException, PinRequiredError, IncorrectPinError, SmscNumberUnknownError from .pdu import encodeSmsSubmitPdu, decodeSmsPdu, encodeGsm7, encodeTextMode -from .util import SimpleOffsetTzInfo, lineStartingWith, allLinesMatchingPattern, parseTextModeTimeStr +from .util import SimpleOffsetTzInfo, lineStartingWith, allLinesMatchingPattern, parseTextModeTimeStr, removeAtPrefix #from . import compat # For Python 2.6 compatibility from gsmmodem.util import lineMatching @@ -554,7 +554,7 @@ def supportedCommands(self): commands = commands[6:] # remove the +CLAC: prefix before splitting return commands.split(',') elif len(response) > 2: # Multi-line response - return [cmd.strip() for cmd in response[:-1]] + return [removeAtPrefix(cmd.strip()) for cmd in response[:-1]] else: self.log.debug('Unhandled +CLAC response: {0}'.format(response)) return None diff --git a/gsmmodem/util.py b/gsmmodem/util.py index 1008e5c..6bf7cf3 100644 --- a/gsmmodem/util.py +++ b/gsmmodem/util.py @@ -108,3 +108,17 @@ def allLinesMatchingPattern(pattern, lines): if m: result.append(m) return result + + +def removeAtPrefix(string): + """ Remove AT prefix from a specified string. + + :param string: An original string + :type string: str + + :return: A string with AT prefix removed + :rtype: str + """ + if string.startswith('AT'): + return string[2:] + return string diff --git a/test/test_modem.py b/test/test_modem.py index 2a20b5b..9d9adb5 100644 --- a/test/test_modem.py +++ b/test/test_modem.py @@ -207,6 +207,8 @@ def writeCallbackFunc(data): (['FGH,RTY,UIO\r\n', 'OK\r\n'], ['FGH', 'RTY', 'UIO']), # nasty, but possible # ZTE-like response: do not start with +CLAC, and use multiple lines (['A\r\n', 'BCD\r\n', 'EFGH\r\n', 'OK\r\n'], ['A', 'BCD', 'EFGH']), + # Siemens response: like ZTE but each command has AT prefix + (['AT&F\r\n', 'AT&V\r\n', 'AT&W\r\n', 'AT+CACM\r\n', 'OK\r\n'], ['&F', '&V', '&W', '+CACM']), # Some Huawei modems have a ZTE-like response, but add an addition \r character at the end of each listed command (['Q\r\r\n', 'QWERTY\r\r\n', '^DTMF\r\r\n', 'OK\r\n'], ['Q', 'QWERTY', '^DTMF'])) for responseSequence, expected in tests: diff --git a/test/test_util.py b/test/test_util.py index 7e2b0f8..184e2c5 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -9,7 +9,7 @@ from . import compat # For Python 2.6 compatibility -from gsmmodem.util import allLinesMatchingPattern, lineMatching, lineStartingWith, lineMatchingPattern, SimpleOffsetTzInfo +from gsmmodem.util import allLinesMatchingPattern, lineMatching, lineStartingWith, lineMatchingPattern, SimpleOffsetTzInfo, removeAtPrefix class TestUtil(unittest.TestCase): """ Tests misc utilities from gsmmodem.util """ @@ -74,6 +74,12 @@ def test_SimpleOffsetTzInfo(self): self.assertEqual(tz.dst(None), timedelta(0)) self.assertIsInstance(tz.__repr__(), str) + def test_removeAtPrefix(self): + tests = (('AT+CLAC', '+CLAC'), ('ATZ', 'Z'), ('+CLAC', '+CLAC'), ('Z', 'Z')) + for src, dst in tests: + res = removeAtPrefix(src) + self.assertEqual(res, dst) + if __name__ == "__main__": logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG) From 4abcc93e9d43e84062be0132b59633a5dfb8b5bd Mon Sep 17 00:00:00 2001 From: Sergey Ninua Date: Fri, 30 Nov 2018 15:55:57 +0300 Subject: [PATCH 2/2] Fix brand mistake. It's actually Teleofis, not Siemens. Add test docstring. --- test/test_modem.py | 2 +- test/test_util.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_modem.py b/test/test_modem.py index 9d9adb5..420b827 100644 --- a/test/test_modem.py +++ b/test/test_modem.py @@ -207,7 +207,7 @@ def writeCallbackFunc(data): (['FGH,RTY,UIO\r\n', 'OK\r\n'], ['FGH', 'RTY', 'UIO']), # nasty, but possible # ZTE-like response: do not start with +CLAC, and use multiple lines (['A\r\n', 'BCD\r\n', 'EFGH\r\n', 'OK\r\n'], ['A', 'BCD', 'EFGH']), - # Siemens response: like ZTE but each command has AT prefix + # Teleofis response: like ZTE but each command has AT prefix (['AT&F\r\n', 'AT&V\r\n', 'AT&W\r\n', 'AT+CACM\r\n', 'OK\r\n'], ['&F', '&V', '&W', '+CACM']), # Some Huawei modems have a ZTE-like response, but add an addition \r character at the end of each listed command (['Q\r\r\n', 'QWERTY\r\r\n', '^DTMF\r\r\n', 'OK\r\n'], ['Q', 'QWERTY', '^DTMF'])) diff --git a/test/test_util.py b/test/test_util.py index 184e2c5..71ecd65 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -75,6 +75,7 @@ def test_SimpleOffsetTzInfo(self): self.assertIsInstance(tz.__repr__(), str) def test_removeAtPrefix(self): + """ Tests function: removeAtPrefix""" tests = (('AT+CLAC', '+CLAC'), ('ATZ', 'Z'), ('+CLAC', '+CLAC'), ('Z', 'Z')) for src, dst in tests: res = removeAtPrefix(src)