-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
executable file
·365 lines (285 loc) · 12.9 KB
/
upload.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
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
#!/bin/python3
"""
A mode for working with Makeblock's HaloCode devices.
Copyright (c) 2022 Lukas Bai
Copyright (c) 2015-2017 Nicholas H.Tollervey and others (see the AUTHORS file).
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 <http://www.gnu.org/licenses/>.
"""
import time
import signal
import sys
import serial # https://pyserial.readthedocs.io/en/latest/pyserial_api.html
import serial.tools.list_ports
debug = False
use_dsrdtr = False
class halocode_config():
def __init__(self):
self.COMMAND_MAX_TIME_OUT = 10
self.FILE_BLOCK_SIZE = 240
self.frame_header_str = "F3"
self.frame_end_str = "F4"
self.protocol_id_str = "01"
self.dev_id_str = "00"
self.srv_id_str = "5E"
self.file_header_cmd_id_str = "01"
self.file_block_cmd_id_str = "02"
self.file_delete_str = "03"
self.file_state_cmd_id_str = "F0"
self.file_type_str = "00"
class FileTransferFSM(halocode_config):
def __init__(self):
super().__init__()
self.FRAME_HEAD = int(self.frame_header_str, 16)
self.FRAME_END = int(self.frame_end_str, 16)
self.DEV_ID = int(self.dev_id_str, 16)
self.SRV_ID = int(self.srv_id_str, 16)
self.CMD_STATE_ID = int(self.file_state_cmd_id_str, 16)
self.FTP_FSM_HEAD_S = 0
self.FTP_FSM_HEAD_CHECK_S = 1
self.FTP_FSM_LEN1_S = 2
self.FTP_FSM_LEN2_S = 3
self.FTP_FSM_DATA_S = 4
self.FTP_FSM_CHECK_S = 5
self.FTP_FSM_END_S = 6
self.__state = self.FTP_FSM_HEAD_S
self.__buf = []
self.__data_len = 0
self.__checksum = 0x00
self.__headchecksum = 0x00
self.__recv_head_checksum = 0x00
self.frame_received_process = None
def get_state(self):
return self.__state
def set_state(self, s):
self.__state = s
def push_chars(self, data):
for c in data:
ret = self.push_char(c)
if ret:
if self.frame_received_process:
self.frame_received_process(ret)
self.clear_buf()
def push_char(self, c):
if self.FTP_FSM_HEAD_S == self.__state:
if self.FRAME_HEAD == c:
self.__state = self.FTP_FSM_HEAD_CHECK_S
self.__buf.clear()
self.__checksum = 0
self.__headchecksum = c
elif self.FTP_FSM_HEAD_CHECK_S == self.__state:
self.__recv_head_checksum = c
self.__state = self.FTP_FSM_LEN1_S
elif self.FTP_FSM_LEN1_S == self.__state:
self.__headchecksum += c
self.__data_len = c
self.__state = self.FTP_FSM_LEN2_S
elif self.FTP_FSM_LEN2_S == self.__state:
self.__headchecksum += c
if self.__headchecksum == self.__recv_head_checksum:
self.__data_len += c * 256
self.__state = self.FTP_FSM_DATA_S
else:
self.__state = self.FTP_FSM_HEAD_S
elif self.FTP_FSM_DATA_S == self.__state:
self.__checksum += c
self.__buf.append(c)
if len(self.__buf) == self.__data_len:
self.__state = self.FTP_FSM_CHECK_S
elif self.FTP_FSM_CHECK_S == self.__state:
if (self.__checksum & 0xFF) == c:
self.__state = self.FTP_FSM_END_S
else:
self.__state = self.FTP_FSM_HEAD_S
elif self.FTP_FSM_END_S == self.__state:
if self.FRAME_END == c:
self.__state = self.FTP_FSM_HEAD_S
return self.__buf
else:
self.__state = self.FTP_FSM_HEAD_S
def clear_buf(self):
self.__buf.clear()
def get_buf(self):
return self.__buf
class file_content_parse(halocode_config):
def __init__(self, content = ""):
super().__init__()
self.update(content)
def update(self, content):
self.content = content
self.content_len = len(content)
self.write_offset = 0
def __bytes_to_hex_str(self, bytes_data):
return " ".join("{:02x}".format(c) for c in bytes_data)
def __calc_add_checksum(self, data):
ret = 0
for c in data:
ret = ret + c
return ret & 0xFF
def __calc_32bit_xor(self, data):
bytes_len = len(data)
data_bytes = bytes(data)
checksum = bytearray.fromhex("00 00 00 00")
for i in range(int(bytes_len / 4)):
checksum[0] = checksum[0] ^ data_bytes[i * 4 + 0]
checksum[1] = checksum[1] ^ data_bytes[i * 4 + 1]
checksum[2] = checksum[2] ^ data_bytes[i * 4 + 2]
checksum[3] = checksum[3] ^ data_bytes[i * 4 + 3]
if (bytes_len % 4):
for i in range(bytes_len % 4):
checksum[0 + i] = checksum[0 + i] ^ data_bytes[4 * int(bytes_len / 4) + i]
return checksum
def create_head_frame(self, target_file_path):
# file header
# 1(file_type) + 4(file_size) + 4(file_check_sum) = 0x09
cmd_len_str = self.__bytes_to_hex_str((0x09 + len(target_file_path)).to_bytes(2, byteorder = 'little'))
input_file_size_str = self.__bytes_to_hex_str(self.content_len.to_bytes(4, byteorder = 'little'))
input_file_checksum_str = self.__bytes_to_hex_str(self.__calc_32bit_xor(self.content))
input_file_name_str = self.__bytes_to_hex_str(bytes(target_file_path, encoding = 'utf-8'))
frame_data_str = self.protocol_id_str + " " + self.dev_id_str + " " + self.srv_id_str + " " + \
self.file_header_cmd_id_str + " " + cmd_len_str + " " + self.file_type_str + " " + \
input_file_size_str + " " + input_file_checksum_str + " " + input_file_name_str
frame_data_len = len(bytes.fromhex(frame_data_str))
frame_data_len_str = self.__bytes_to_hex_str((frame_data_len).to_bytes(2, byteorder='little'))
frame_head_checkusum_str = self.__bytes_to_hex_str(self.__calc_add_checksum(bytes.fromhex(self.frame_header_str + frame_data_len_str)).to_bytes(1, byteorder = 'little'))
frame_checksum_str = self.__bytes_to_hex_str(self.__calc_add_checksum(bytes.fromhex(frame_data_str)).to_bytes(1, byteorder = 'little'))
send_head_str = self.frame_header_str + " " + frame_head_checkusum_str + " " + frame_data_len_str + " " + \
frame_data_str + " " + frame_checksum_str + " " + self.frame_end_str
return bytes.fromhex(send_head_str)
def get_next_block(self):
if self.write_offset >= self.content_len:
return
if (self.write_offset + self.FILE_BLOCK_SIZE) < self.content_len:
send_file_size = self.FILE_BLOCK_SIZE
else:
send_file_size = self.content_len - self.write_offset
file_offset_str = self.__bytes_to_hex_str(self.write_offset.to_bytes(4, byteorder = 'little'))
cmd_len_str = self.__bytes_to_hex_str((0x04 + send_file_size).to_bytes(2, byteorder = 'little'))
file_block_str = self.__bytes_to_hex_str(bytes(self.content[self.write_offset: self.write_offset + send_file_size]))
frame_data_str = self.protocol_id_str + " " + self.dev_id_str + " " + self.srv_id_str + " " + self.file_block_cmd_id_str + \
" " + cmd_len_str + " " + file_offset_str + " " + file_block_str
frame_data_len = len(bytes.fromhex(frame_data_str))
frame_data_len_str = self.__bytes_to_hex_str((frame_data_len).to_bytes(2, byteorder = 'little'))
frame_head_checkusum_str = self.__bytes_to_hex_str(self.__calc_add_checksum(bytes.fromhex(self.frame_header_str + frame_data_len_str)).to_bytes(1, byteorder = 'little'))
frame_checksum_str = self.__bytes_to_hex_str(self.__calc_add_checksum(bytes.fromhex(frame_data_str)).to_bytes(1, byteorder = 'little'))
send_block_str = self.frame_header_str + " " + frame_head_checkusum_str + " " + frame_data_len_str + \
" " + frame_data_str + " " + frame_checksum_str + " " + self.frame_end_str
send_block_bytes = bytearray.fromhex(send_block_str)
self.write_offset += send_file_size
return send_block_bytes
def get_current_percentage(self):
return int(100 * self.write_offset / self.content_len)
class halocode_communication():
def __init__(self):
self.serial_fd = None
self.input_file_content = None
self.target_file_path = None
self.ftp_process = FileTransferFSM()
self.ftp_process.frame_received_process = self.__frame_process
self.file_content = file_content_parse()
self.process_status = 0
def update_paras(self, serial, content, target_file_path):
self.serial_fd = serial
self.input_file_content = content
self.target_file_path = target_file_path
self.file_content.update(content)
self.process_status = 0
def send_file_content(self, ser = None, input_file_data = None, target_file_path = None):
if ser == None:
ser = self.serial_fd
if input_file_data == None:
input_file_data = self.input_file_content
if target_file_path == None:
target_file_path = self.target_file_path
if self.process_status == 0:
frame = self.file_content.create_head_frame(self.target_file_path)
if frame:
if debug: print(f'write frame: ${frame}')
ser.write(frame)
self.process_status = 1
else:
frame = self.file_content.get_next_block()
if frame:
if debug: print(f'write frame: ${frame}')
ser.write(frame)
self.show_status_message("transmitting: %s%s" %('%', self.file_content.get_current_percentage(), ))
def __frame_process(self, frame):
if (0x01 == frame[0] and 0x00 == frame[6]):
if self.file_content.get_current_percentage() < 100:
self.send_file_content()
else:
global uploading
uploading = False
self.show_status_message("Complete file transfer!")
self.process_status = 0
def show_status_message(self, message):
print(message)
def find_port(vid=6790, pid=29987):
"""
Find the port of Makeblock's devices
Return: Makeblock Device or None if not found
"""
for port in serial.tools.list_ports.comports():
if port.vid == vid and port.pid == pid:
return port.device
return None
def file_read(path):
file = open(path, 'r')
data = file.read()
file.close()
return data
def init_upload(path, ser):
target_path = '/flash/main.py'
script = file_read(path).encode('utf-8')
print(f'uploading {path} to {target_path}')
comm = halocode_communication()
comm.update_paras(ser, script, target_path)
comm.send_file_content()
return comm
def upload_and_log(path = ''):
"""
Upload Code to Makeblock Devices
"""
global uploading
uploading = path != ''
port = find_port()
if port == None:
print('Could not find an attached Makeblock Device')
print('Please attach your Makeblock device (Codey Rocky or HaloCode) with USB Cable')
else:
print(f'Connecting to {port}')
ser = serial.Serial(port, 115200, dsrdtr=use_dsrdtr)
if uploading:
comm = init_upload(path, ser)
print('press Ctrl+C to cancel')
global running
while running:
if ser.in_waiting > 0 and ser.out_waiting == 0:
if debug: print('input received')
bytes = ser.read_all()
if uploading: comm.ftp_process.push_chars(bytes)
else:
try:
sys.stdout.write(bytes.decode())
except:
pass
time.sleep(0.25)
running = True
uploading = False
def signal_handler(signal, frame):
global running
running = False
signal.signal(signal.SIGINT, signal_handler)
if len(sys.argv) < 2:
print('No file specified. Connecting to log console')
upload_and_log()
else:
upload_and_log(sys.argv[1])