forked from reald/anytone-flash-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writecontacts.py
executable file
·130 lines (79 loc) · 3.34 KB
/
writecontacts.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
#!/usr/bin/env python3
import sys
import at_serial
import at_devices
comport = '/dev/ttyACM0'
if len(sys.argv) == 3:
importfilename = sys.argv[1]
comport = sys.argv[2]
elif len(sys.argv) == 2:
importfilename = sys.argv[1]
else:
print("Usage: " + sys.argv[0] + ' inputfilename.csv [comport]')
exit()
# connect to device
at_serial.open_device(comport)
# set pc mode
at_serial.start_pcmode()
# is this device supported?
[devicename, version] = at_serial.get_device_info()
if at_devices.is_device_supported(devicename, version):
# known device found
print('OK: Radio ' + str(devicename[:-1], "utf-8") + ' / ' + str(version[:-1], "utf-8") + ' connected.')
else:
print('ERR: Device not supported (' + str(devicename) + ' ' + str(version) + ')' )
exit()
# do something. what is this read command for?
[resp, resplen] = at_serial.read_memory(0x02fa0020, 16)
# read contact list from file
[offsetData, contactListData, numContacts] = at_devices.csv2contactlist(importfilename)
contactlistdatalen = len(contactListData)
# calculate next free memory address
for contactsection in at_devices.memSectContacts:
#print('address:' + hex(contactsection['address']) )
#print('rest ' + str(contactlistdatalen) )
if ( contactlistdatalen >= contactsection['size'] ):
# this not the section with free memory
contactlistdatalen -= contactsection['size']
else:
nextfreememoryaddr = contactsection['address'] + contactlistdatalen
break
# write contact offsets (cannot be read back)
print("Writing contact offsets...")
i = 0
while ( len(offsetData) > 0 \
and i < len(at_devices.memSectContactsOffsetWrite) ):
# write block
numbytestowrite = min( at_devices.memSectContactsOffsetWrite[i]['size'], len(offsetData) )
print("i " + str(i) + " numbytestofwrite " + str(numbytestowrite) + " len offsetData " + str(len(offsetData)) + ' ' + hex(at_devices.memSectContactsOffsetWrite[i]['address']) )
at_serial.write_memory( at_devices.memSectContactsOffsetWrite[i]['address'], offsetData[:numbytestowrite] )
offsetData = offsetData[numbytestowrite:]
i += 1
# write index
print("Writing index...")
numandindex = bytearray()
numandindex.append( numContacts & 0xff )
numandindex.append( (numContacts >> 8) & 0xff )
numandindex.append( (numContacts >> 16) & 0xff )
numandindex.append( (numContacts >> 24) & 0xff )
numandindex.append( nextfreememoryaddr & 0xff )
numandindex.append( (nextfreememoryaddr >> 8) & 0xff )
numandindex.append( (nextfreememoryaddr >> 16) & 0xff )
numandindex.append( (nextfreememoryaddr >> 24) & 0xff )
at_serial.write_memory( at_devices.memSectContactsIndexAddr, numandindex)
# write contact data
print("Writing contact data...")
i = 0
while ( len(contactListData) > 0 \
and i < len(at_devices.memSectContacts) ):
# write block
numbytestowrite = min( at_devices.memSectContacts[i]['size'], len(contactListData) )
print("i " + str(i) + " numbytestofwrite " + str(numbytestowrite) + " len offsetData " + str(len(contactListData)) + ' ' + hex(at_devices.memSectContacts[i]['address']) )
at_serial.write_memory( at_devices.memSectContacts[i]['address'], contactListData[:numbytestowrite] )
contactListData = contactListData[numbytestowrite:]
i += 1
# fixme: write padding
# end pc mode
at_serial.end_pcmode()
# close device
at_serial.close_device()