-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart.py
More file actions
372 lines (318 loc) · 12.2 KB
/
uart.py
File metadata and controls
372 lines (318 loc) · 12.2 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import asyncio
import binascii
import logging
import serial
import serial_asyncio
from bellows.config import (
CONF_DEVICE_BAUDRATE,
CONF_DEVICE_PATH,
CONF_FLOW_CONTROL,
CONF_FLOW_CONTROL_DEFAULT,
)
from bellows.thread import EventLoopThread, ThreadsafeProxy
import bellows.types as t
LOGGER = logging.getLogger(__name__)
RESET_TIMEOUT = 5
class Gateway(asyncio.Protocol):
FLAG = b"\x7E" # Marks end of frame
ESCAPE = b"\x7D"
XON = b"\x11" # Resume transmission
XOFF = b"\x13" # Stop transmission
SUBSTITUTE = b"\x18"
CANCEL = b"\x1A" # Terminates a frame in progress
STUFF = 0x20
RANDOMIZE_START = 0x42
RANDOMIZE_SEQ = 0xB8
RESERVED = FLAG + ESCAPE + XON + XOFF + SUBSTITUTE + CANCEL
class Terminator:
pass
def __init__(self, application, connected_future=None, connection_done_future=None):
self._send_seq = 0
self._rec_seq = 0
self._buffer = b""
self._application = application
self._reset_future = None
self._connected_future = connected_future
self._sendq = asyncio.Queue()
self._pending = (-1, None)
self._connection_done_future = connection_done_future
def connection_made(self, transport):
"""Callback when the uart is connected"""
self._transport = transport
if self._connected_future is not None:
self._connected_future.set_result(True)
asyncio.create_task(self._send_task())
def data_received(self, data):
"""Callback when there is data received from the uart"""
# TODO: Fix this handling for multiple instances of the characters
# If a Cancel Byte or Substitute Byte is received, the bytes received
# so far are discarded. In the case of a Substitute Byte, subsequent
# bytes will also be discarded until the next Flag Byte.
if self.CANCEL in data:
self._buffer = b""
data = data[data.rfind(self.CANCEL) + 1 :]
if self.SUBSTITUTE in data:
self._buffer = b""
data = data[data.find(self.FLAG) + 1 :]
self._buffer += data
while self._buffer:
frame, self._buffer = self._extract_frame(self._buffer)
if frame is None:
break
self.frame_received(frame)
def _extract_frame(self, data):
"""Extract a frame from the data buffer"""
if self.FLAG in data:
place = data.find(self.FLAG)
frame = self._unstuff(data[: place + 1])
rest = data[place + 1 :]
crc = binascii.crc_hqx(frame[:-3], 0xFFFF)
crc = bytes([crc >> 8, crc % 256])
if crc != frame[-3:-1]:
LOGGER.error(
"CRC error in frame %s (%s != %s)",
binascii.hexlify(frame),
binascii.hexlify(frame[-3:-1]),
binascii.hexlify(crc),
)
self.write(self._nak_frame())
# Make sure that we also handle the next frame if it is already received
return self._extract_frame(rest)
return frame, rest
return None, data
def frame_received(self, data):
"""Frame receive handler"""
if (data[0] & 0b10000000) == 0:
self.data_frame_received(data)
elif (data[0] & 0b11100000) == 0b10000000:
self.ack_frame_received(data)
elif (data[0] & 0b11100000) == 0b10100000:
self.nak_frame_received(data)
elif data[0] == 0b11000000:
self.rst_frame_received(data)
elif data[0] == 0b11000001:
self.rstack_frame_received(data)
elif data[0] == 0b11000010:
self.error_frame_received(data)
else:
LOGGER.error("UNKNOWN FRAME RECEIVED: %r", data) # TODO
def data_frame_received(self, data):
"""Data frame receive handler"""
LOGGER.debug("Data frame: %s", binascii.hexlify(data))
seq = (data[0] & 0b01110000) >> 4
self._rec_seq = (seq + 1) % 8
self.write(self._ack_frame())
self._handle_ack(data[0])
self._application.frame_received(self._randomize(data[1:-3]))
def ack_frame_received(self, data):
"""Acknowledgement frame receive handler"""
LOGGER.debug("ACK frame: %s", binascii.hexlify(data))
self._handle_ack(data[0])
def nak_frame_received(self, data):
"""Negative acknowledgement frame receive handler"""
LOGGER.debug("NAK frame: %s", binascii.hexlify(data))
self._handle_nak(data[0])
def rst_frame_received(self, data):
"""Reset frame handler"""
LOGGER.debug("RST frame: %s", binascii.hexlify(data))
def rstack_frame_received(self, data):
"""Reset acknowledgement frame receive handler"""
self._send_seq = 0
self._rec_seq = 0
code, version = self._get_error_code(data)
LOGGER.debug(
"RSTACK Version: %d Reason: %s frame: %s",
version,
code.name,
binascii.hexlify(data),
)
# not a reset we've requested. Signal application reset
if code is not t.NcpResetCode.RESET_SOFTWARE:
self._application.enter_failed_state(code)
return
if self._reset_future is None:
LOGGER.warning("Reset future is None")
return
# Make sure that the reset_future is not done
if not self._reset_future.done():
self._reset_future.set_result(True)
@staticmethod
def _get_error_code(data):
"""Extracts error code from RSTACK or ERROR frames."""
return t.NcpResetCode(data[2]), data[1]
def error_frame_received(self, data):
"""Error frame receive handler."""
error_code, version = self._get_error_code(data)
LOGGER.debug(
"Error code: %s, Version: %d, frame: %s",
error_code.name,
version,
binascii.hexlify(data),
)
self._application.enter_failed_state(error_code)
def write(self, data):
"""Send data to the uart"""
LOGGER.debug("Sending: %s", binascii.hexlify(data))
self._transport.write(data)
def close(self):
self._sendq.put_nowait(self.Terminator)
self._transport.close()
def _reset_cleanup(self, future):
"""Delete reset future."""
self._reset_future = None
def connection_lost(self, exc):
"""Port was closed unexpectedly."""
if self._connection_done_future:
self._connection_done_future.set_result(exc)
if exc is None:
LOGGER.debug("Closed serial connection")
return
LOGGER.error("Lost serial connection: %s", exc)
self._application.connection_lost(exc)
async def reset(self):
"""Send a reset frame and init internal state."""
LOGGER.debug("Resetting ASH")
if self._reset_future is not None:
LOGGER.error(
("received new reset request while an existing " "one is in progress")
)
return await self._reset_future
self._send_seq = 0
self._rec_seq = 0
self._buffer = b""
while not self._sendq.empty():
self._sendq.get_nowait()
if self._pending[1]:
self._pending[1].set_result(True)
self._pending = (-1, None)
self._reset_future = asyncio.get_event_loop().create_future()
self._reset_future.add_done_callback(self._reset_cleanup)
self.write(self._rst_frame())
return await asyncio.wait_for(self._reset_future, timeout=RESET_TIMEOUT)
async def _send_task(self):
"""Send queue handler"""
while True:
item = await self._sendq.get()
if item is self.Terminator:
break
data, seq = item
success = False
rxmit = 0
while not success:
self._pending = (seq, asyncio.get_event_loop().create_future())
self.write(self._data_frame(data, seq, rxmit))
rxmit = 1
success = await self._pending[1]
def _handle_ack(self, control):
"""Handle an acknowledgement frame"""
ack = ((control & 0b00000111) - 1) % 8
if ack == self._pending[0]:
pending, self._pending = self._pending, (-1, None)
pending[1].set_result(True)
def _handle_nak(self, control):
"""Handle negative acknowledgment frame"""
nak = control & 0b00000111
if nak == self._pending[0]:
self._pending[1].set_result(False)
def data(self, data):
"""Send a data frame"""
seq = self._send_seq
self._send_seq = (seq + 1) % 8
self._sendq.put_nowait((data, seq))
def _data_frame(self, data, seq, rxmit):
"""Construct a data frame"""
assert 0 <= seq <= 7
assert 0 <= rxmit <= 1
control = (seq << 4) | (rxmit << 3) | self._rec_seq
return self._frame(bytes([control]), self._randomize(data))
def _ack_frame(self):
"""Construct a acknowledgement frame"""
assert 0 <= self._rec_seq < 8
control = bytes([0b10000000 | (self._rec_seq & 0b00000111)])
return self._frame(control, b"")
def _nak_frame(self):
"""Construct a negative acknowledgement frame"""
assert 0 <= self._rec_seq < 8
control = bytes([0b10100000 | (self._rec_seq & 0b00000111)])
return self._frame(control, b"")
def _rst_frame(self):
"""Construct a reset frame"""
return self.CANCEL + self._frame(b"\xC0", b"")
def _frame(self, control, data):
"""Construct a frame"""
crc = binascii.crc_hqx(control + data, 0xFFFF)
crc = bytes([crc >> 8, crc % 256])
return self._stuff(control + data + crc) + self.FLAG
def _randomize(self, s):
"""XOR s with a pseudo-random sequence for transmission
Used only in data frames
"""
rand = self.RANDOMIZE_START
out = b""
for c in s:
out += bytes([c ^ rand])
if rand % 2:
rand = (rand >> 1) ^ self.RANDOMIZE_SEQ
else:
rand = rand >> 1
return out
def _stuff(self, s):
"""Byte stuff (escape) a string for transmission"""
out = b""
for c in s:
if c in self.RESERVED:
out += self.ESCAPE + bytes([c ^ self.STUFF])
else:
out += bytes([c])
return out
def _unstuff(self, s):
"""Unstuff (unescape) a string after receipt"""
out = b""
escaped = False
for c in s:
if escaped:
out += bytes([c ^ self.STUFF])
escaped = False
elif c in self.ESCAPE:
escaped = True
else:
out += bytes([c])
return out
async def _connect(config, application):
loop = asyncio.get_event_loop()
connection_future = loop.create_future()
connection_done_future = loop.create_future()
protocol = Gateway(application, connection_future, connection_done_future)
if config[CONF_FLOW_CONTROL] == CONF_FLOW_CONTROL_DEFAULT:
xon_xoff, rtscts = True, False
else:
xon_xoff, rtscts = False, True
transport, protocol = await serial_asyncio.create_serial_connection(
loop,
lambda: protocol,
url=config[CONF_DEVICE_PATH],
baudrate=config[CONF_DEVICE_BAUDRATE],
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
xonxoff=xon_xoff,
rtscts=rtscts,
)
await connection_future
thread_safe_protocol = ThreadsafeProxy(protocol, loop)
return thread_safe_protocol, connection_done_future
async def connect(config, application, use_thread=True):
if use_thread:
application = ThreadsafeProxy(application, asyncio.get_event_loop())
thread = EventLoopThread()
await thread.start()
try:
protocol, connection_done = await thread.run_coroutine_threadsafe(
_connect(config, application)
)
except Exception:
thread.force_stop()
raise
connection_done.add_done_callback(lambda _: thread.force_stop())
else:
protocol, _ = await _connect(config, application)
return protocol