-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathchameleon_com.py
More file actions
415 lines (365 loc) · 14.2 KB
/
chameleon_com.py
File metadata and controls
415 lines (365 loc) · 14.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import queue
import struct
import threading
import time
import serial
import chameleon_status
import socket
class NotOpenException(Exception):
"""
Chameleon err status
"""
class OpenFailException(Exception):
"""
Chameleon open fail(serial port may be error)
"""
class CMDInvalidException(Exception):
"""
CMD invalid(Unsupported)
"""
class Response:
"""
Chameleon Response Data
"""
def __init__(self, cmd, status, data):
self.cmd = cmd
self.status = status
self.data: bytearray = data
class ChameleonTransport:
def __init__(self):
pass
def is_open(self):
return False
def open(self):
raise NotImplementedError('Please implement this')
def close(self):
pass
def write(self, data):
raise NotImplementedError('Please implement this')
def read(self):
raise NotImplementedError('Please implement this')
class ChameleonTCPTransport(ChameleonTransport):
def __init__(self, connection_string: str):
self.connection_string = connection_string
self.socket = None
def is_open(self):
return self.socket is not None
def open(self):
host, _, port = self.connection_string.rpartition(':')
self.socket = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
self.socket.connect((host, int(port)))
def close(self):
self.socket.close()
self.socket = None
def write(self, data):
self.socket.send(data)
def read(self, bufsize=1): # Bufsize matching serial
return self.socket.recv(bufsize)
class ChameleonSerialTransport(ChameleonTransport):
def __init__(self, port):
self.port = port
self.serial_instance = None
def is_open(self):
return self.serial_instance is not None and self.serial_instance.isOpen()
def open(self):
self.serial_instance = serial.Serial(port=self.port, baudrate=115200)
try:
self.serial_instance.dtr = 1 # must make dtr enable
except Exception:
# not all serial support dtr, e.g. virtual serial over BLE
pass
self.serial_instance.timeout = 0 # do not block
def close(self):
self.serial_instance.close()
self.serial_instance = None
def write(self, data):
self.serial_instance.write(data)
def read(self, size=1):
return self.serial_instance.read(size=size)
class ChameleonCom:
"""
Chameleon device base class
Communication and Data frame implemented
"""
data_frame_sof = 0x11
data_max_length = 512
def __init__(self):
"""
Create a chameleon device instance
"""
self.serial_instance: serial.Serial | None = None
self.send_data_queue = queue.Queue()
self.wait_response_map = {}
self.event_closing = threading.Event()
self.transport: ChameleonTransport | None = None
def is_open(self):
"""
Chameleon is connected and init.
:return:
"""
return self.transport is not None and self.transport.is_open()
def open(self, transport):
"""
Open chameleon port to communication
And init some variables
:param port: com port, comXXX or ttyXXX
:return:
"""
if not self.is_open():
error = None
try:
self.transport = transport
self.transport.open()
except Exception as e:
error = e
finally:
if error is not None:
raise OpenFailException(error)
# clear variable
self.send_data_queue.queue.clear()
self.wait_response_map.clear()
# Start a sub thread to process data
self.event_closing.clear()
threading.Thread(target=self.thread_data_receive).start()
threading.Thread(target=self.thread_data_transfer).start()
threading.Thread(target=self.thread_check_timeout).start()
return self
def check_open(self):
"""
:return:
"""
if not self.is_open():
raise NotOpenException("Please call open() function to start device.")
@staticmethod
def lrc_calc(array):
"""
calc lrc and auto cut byte
:param array: value array
:return: u8 result
"""
# add and cut byte and return
ret = 0x00
for b in array:
ret += b
ret &= 0xFF
return (0x100 - ret) & 0xFF
def close(self):
"""
Close chameleon and clear variable
:return:
"""
self.event_closing.set()
try:
self.transport.close()
except Exception:
pass
finally:
self.transport = None
self.wait_response_map.clear()
self.send_data_queue.queue.clear()
def thread_data_receive(self):
"""
SubThread to receive data from chameleon device
:return:
"""
data_buffer = bytearray()
data_position = 0
data_cmd = 0x0000
data_status = 0x0000
data_length = 0x0000
while self.is_open():
# receive
try:
data_bytes = self.transport.read()
except Exception as e:
if not self.event_closing.is_set():
print(f"Serial Error {e}, thread for receiver exit.")
self.close()
break
if len(data_bytes) > 0:
data_byte = data_bytes[0]
data_buffer.append(data_byte)
if data_position < 2: # start of frame
if data_position == 0:
if data_buffer[data_position] != self.data_frame_sof:
print("Data frame no sof byte.")
data_position = 0
data_buffer.clear()
continue
if data_position == 1:
if data_buffer[data_position] != self.lrc_calc(data_buffer[0:1]):
data_position = 0
data_buffer.clear()
print("Data frame sof lrc error.")
continue
elif data_position == 8: # frame head lrc
if data_buffer[data_position] != self.lrc_calc(data_buffer[0:8]):
data_position = 0
data_buffer.clear()
print("Data frame head lrc error.")
continue
# frame head complete, cache info
data_cmd = struct.unpack(">H", data_buffer[2:4])[0]
data_status = struct.unpack(">H", data_buffer[4:6])[0]
data_length = struct.unpack(">H", data_buffer[6:8])[0]
if data_length > self.data_max_length:
data_position = 0
data_buffer.clear()
print("Data frame data length too than of max.")
continue
elif data_position > 8: # // frame data
if data_position == (8 + data_length + 1):
if data_buffer[data_position] == self.lrc_calc(data_buffer[0:-1]):
# ok, lrc for data is correct.
# and we are receive completed
# print(f"Buffer data = {data_buffer.hex()}")
if data_cmd in self.wait_response_map:
# call processor
if 'callback' in self.wait_response_map[data_cmd]:
fn_call = self.wait_response_map[data_cmd]['callback']
else:
fn_call = None
data_response = data_buffer[9: 9 + data_length]
if callable(fn_call):
# delete wait task from map
del self.wait_response_map[data_cmd]
fn_call(data_cmd, data_status, data_response)
else:
self.wait_response_map[data_cmd]['response'] = Response(data_cmd, data_status,
data_response)
else:
print(f"No task wait process: ${data_cmd}")
else:
print("Data frame finally lrc error.")
data_position = 0
data_buffer.clear()
continue
data_position += 1
else:
time.sleep(0.001)
def thread_data_transfer(self):
"""
SubThread to transfer data to chameleon device
:return:
"""
while self.is_open():
# get a task from queue(if exists)
if self.send_data_queue.empty():
time.sleep(0.001)
continue
task = self.send_data_queue.get()
task_cmd = task['cmd']
task_timeout = task['timeout']
task_close = task['close']
# register to wait map
if 'callback' in task and callable(task['callback']):
self.wait_response_map[task_cmd] = {'callback': task['callback']} # The callback for this task
else:
self.wait_response_map[task_cmd] = {'response': None}
# set start time
start_time = time.time()
self.wait_response_map[task_cmd]['start_time'] = start_time
self.wait_response_map[task_cmd]['end_time'] = start_time + task_timeout
self.wait_response_map[task_cmd]['is_timeout'] = False
try:
# send to device
self.transport.write(task['frame'])
except Exception as e:
print(f"Serial Error {e}, thread for transfer exit.")
self.close()
break
# update queue status
self.send_data_queue.task_done()
# disconnect if DFU command has been sent
if task_close:
self.close()
def thread_check_timeout(self):
"""
Check task timeout
:return:
"""
while self.is_open():
for task_cmd in self.wait_response_map.keys():
if time.time() > self.wait_response_map[task_cmd]['end_time']:
if 'callback' in self.wait_response_map[task_cmd]:
# not sync, call function to notify timeout.
self.wait_response_map[task_cmd]['callback'](task_cmd, None, None)
else:
# sync mode, set timeout flag
self.wait_response_map[task_cmd]['is_timeout'] = True
time.sleep(0.001)
def make_data_frame_bytes(self, cmd: int, status: int, data: bytearray = None) -> bytearray:
"""
Make data frame
:return: frame
"""
frame = bytearray()
# sof and sof lrc byte
frame.append(self.data_frame_sof)
frame.append(self.lrc_calc(frame[0:1]))
# head info
frame.extend(struct.pack('>H', cmd))
frame.extend(struct.pack('>H', status))
frame.extend(struct.pack('>H', len(data) if data is not None else 0))
frame.append(self.lrc_calc(frame[2:8]))
# data
if data is not None:
frame.extend(data)
# frame lrc
frame.append(self.lrc_calc(frame))
return frame
def send_cmd_auto(self, cmd: int, status: int, data: bytearray = None, callback=None, timeout: int = 3,
close: bool = False):
"""
Send cmd to device
:param timeout: wait response timeout
:param cmd: cmd
:param status: status(optional)
:param callback: call on response
:param data: bytes data
:param close: close connection after executing
:return:
"""
self.check_open()
# delete old task
if cmd in self.wait_response_map:
del self.wait_response_map[cmd]
# make data frame
data_frame = self.make_data_frame_bytes(cmd, status, data)
task = {'cmd': cmd, 'frame': data_frame, 'timeout': timeout, 'close': close}
if callable(callback):
task['callback'] = callback
self.send_data_queue.put(task)
return self
def send_cmd_sync(self, cmd: int, status: int, data: bytearray or bytes or list or int = None,
timeout: int = 3) -> Response:
"""
Send cmd to device, and block receive data.
:param cmd: cmd
:param status: status(optional)
:param data: bytes data
:param timeout: wait response timeout
:return: response data
"""
if isinstance(data, int):
data = [data] # warp array.
# first to send cmd, no callback mode(sync)
self.send_cmd_auto(cmd, status, data, None, timeout)
# wait cmd start process
while cmd not in self.wait_response_map:
time.sleep(0.01)
# wait response data set
while self.wait_response_map[cmd]['response'] is None:
if 'is_timeout' in self.wait_response_map[cmd] and self.wait_response_map[cmd]['is_timeout']:
raise TimeoutError(f"CMD {cmd} exec timeout")
time.sleep(0.01)
# ok, data received.
data_response = self.wait_response_map[cmd]['response']
del self.wait_response_map[cmd]
if data_response.status == chameleon_status.Device.STATUS_INVALID_CMD:
raise CMDInvalidException(f"Device unsupported cmd: {cmd}")
return data_response
if __name__ == '__main__':
cml = ChameleonCom().open("com19")
resp = cml.send_cmd_sync(0x03E9, 0xBEEF, bytearray([0x01, 0x02]))
print(resp)