-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat_protocol.py
126 lines (106 loc) · 3.49 KB
/
at_protocol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
#
# at_protocol.py
# AT command protocol with pyserial
# Copyright (C) 2019 Dr. PO <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
import threading
import serial
import serial.threaded
class ATProtocol(serial.threaded.LineReader):
COLON = ':'
# V.250 – Result codes, https://www.itu.int/rec/T-REC-V.250-200307-I/en
RESULT_CODE = {
'OK': 0,
'CONNECT': 1,
'RING': 2,
'NO CARRIER': 3,
'ERROR': 4,
'NO DIALTONE': 6,
'BUSY': 7,
'NO ANSWER': 8,
}
TERMINATOR = b'\r\n'
def __init__(self):
super().__init__()
self.urc_handler = {}
self.lines = []
self.final = threading.Condition()
self.waiting = False
@classmethod
def result_code(cls, line):
'''result code of line in ITU-T V.25ter, return -1 if line is not a result code'''
return cls.RESULT_CODE[line] if line in cls.RESULT_CODE else -1
@classmethod
def is_final(cls, line):
'''return True if line is the final response'''
if cls.COLON in line:
prefix, extra = line.split(cls.COLON, 1)
if prefix in ('+CME ERROR', '+CMS ERROR'):
logging.warning('got error: %s', line)
return True
return cls.result_code(line) >= 0
def handle_line(self, line):
if not line: return
with self.final:
if not self.waiting:
self.handle_urc(line)
return
self.lines.append(line)
if self.is_final(line):
self.final.notify()
def handle_urc(self, line):
if cls.COLON in line:
prefix = line.split(cls.COLON, 1)[0]
if prefix in self.urc_handler:
self.urc_handler[prefix](line)
return
logging.warning('unhandled line: %s', line)
def register(self, prefix, handler):
'''register a URC handler for PREFIX, HANDLER has signature handler(line)'''
self.urc_handler[prefix] = handler
def unregister(self, prefix):
'''unregister URC handler for PREFIX'''
self.urc_handler.pop(prefix, None)
def _send(self, cmd, timeout=None):
with self.final:
self.write_line(cmd)
self.waiting = True
self.final.wait(timeout)
self.waiting = False
resp = self.lines
self.lines = []
return resp
def command(self, cmd, timeout=None):
'''send at command CMD and return the result code'''
resp = self._send(cmd, timeout=timeout)
return self.result_code(resp[-1]) if len(resp) > 0 else -1
def multiline(self, cmd, prefix='', timeout=None):
'''send CMD and get lines with prefix PREFIX'''
resp = self._send(cmd, timeout=timeout)
if not resp:
return -1
result = self.result_code(resp[-1])
if 0 != result:
return result
start = 1 if resp[0].startswith(cmd) else 0
return list(line for line in resp[start:-1] if line.startswith(prefix))
def singleline(self, cmd, prefix='', timeout=None):
'''send get first line with prefix PREFIX, prefix is removed'''
result = self.multiline(cmd, prefix=prefix, timeout=timeout)
if not isinstance(result, list):
return result
return result[0][len(prefix):].strip() if len(result) > 0 else None