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
|
#!/usr/bin/python
import os
import sys
import time
import struct
import serial
from binascii import crc32
def _write(dst, data):
for i in range(len(data)):
dst.write(data[i])
time.sleep(0.1)
if len(data) == 4:
print("Wrote 0x{:02x}{:02x}{:02x}{:02x}".format(ord(data[0]), ord(data[1]), ord(data[2]), ord(data[3])))
else:
print("Wrote {!r}".format(data))
def _read(dst):
res = ''
while True:
x = dst.read(1)
if not x:
break
res += x
print ("Read {!r}".format(res))
return res
def _execute(dst, cmd):
_write(dst, '\r')
prompt = _read(dst)
if prompt.endswith('Username: '):
_write(dst, 'ct\r')
prompt = _read(dst)
if prompt.endswith('Password: '):
_write(dst, 'ct\r')
prompt = _read(dst)
if not prompt.endswith('> '):
sys.stderr.write('Device does not seem to be ready for a file transfer (got {!r})\n'.format(prompt))
return False
_write(dst, cmd + '\r')
response = _read(dst)
return response
def send_file(filename, device='/dev/ttyUSB0', initiate=True):
s = os.stat(filename)
size = s.st_size
src = open(filename, 'rb')
dst = serial.Serial(device, 921600, timeout=0.1)
if initiate:
response = _execute(dst, 'filetransfer')
if 'OK' not in response:
sys.stderr.write('Device did not accept the filetransfer command (got {!r})\n'.format(response))
return False
# 1. Write size of file (4 bytes)
_write(dst, struct.pack('<I', size))
_read(dst)
# 2. Write file contents while calculating CRC-32
crc = 0
counter = 0
while True:
data = src.read(4096)
if not data:
break
dst.write(data)
print("Wrote {!s} bytes".format(len(data)))
# read ACK (a counter of number of 4k chunks received)
while True:
ack = dst.read(4)
if len(ack) == 4:
break
print('ERROR: Did not receive an ACK, got {!r}'.format(ack))
dst.write('\r')
new_counter = struct.unpack('<I', ack)[0]
if new_counter != counter + 1:
print('ERROR: Did not receive the expected counter as ACK (got {!r}/{!r}, not {!r})'.format(new_counter, ack, counter))
flush = dst.read(100)
print('FLUSH data: {!r}'.format(flush))
return False
counter += 1
crc = crc32(data, crc) & 0xffffffff
_read(dst)
# 3. Write CRC-32 (4 bytes)
_write(dst, struct.pack('<I', crc))
_read(dst)
src.close()
dst.close()
return True
if len(sys.argv) != 2:
sys.stderr.write('Syntax: {!s} filename\n'.format(sys.argv[0]))
sys.exit(1)
if send_file(sys.argv[1]):
sys.exit(0)
sys.exit(1)
|