-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathat_devices.py
332 lines (244 loc) · 9.68 KB
/
at_devices.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
import csv
# devices supported by this software
list_known_devices = []
list_known_devices.append({'devicename': b'ID878UV\x00', 'version': b'V100\x00'}) # Anytone D878UV
memSectContactsOffsetWrite = []
memSectContactsIndexAddr = 0
memSectContactsIndexSize = 0
memSectContacts = []
def is_device_supported(devicename, version):
# is this device supported?
for device in list_known_devices:
if devicename == device['devicename'] and version == device['version']:
# known device found
if devicename == b'ID878UV\x00':
import at_d878uv as atm
else:
print('ERR: Device found but no data file defined.')
exit()
global memSectContactsOffsetWrite
global memSectContactsIndexAddr
global memSectContactsIndexSize
global memSectContacts
memSectContactsOffsetWrite = atm.memSectContactsOffsetWrite
memSectContactsIndexAddr = atm.memSectContactsIndexAddr
memSectContactsIndexSize = atm.memSectContactsIndexSize
memSectContacts = atm.memSectContacts
return True
return False
# read contact list from device and save to csv file
def contactlist2csv(rawdata, numallcontacts, filename):
f = open(filename, "w")
f.write ('"No.","Radio ID","Callsign","Name","City","State","Country","Remarks","Call Type","Call Alert"' + "\r\n")
i = 0
numdatasets = 0
while ( numdatasets < numallcontacts ): # fixme. add checks and avoid out of bound access
numdatasets += 1
# CT: Call Type: 00 -> Private Call, 01 -> Group Call, 02 -> "All" (only 1x allowed), 1 byte
if rawdata[i] == 0:
calltype = 'Private Call'
elif rawdata[i] == 1:
calltype = 'Group Call'
elif rawdata[i] == 2:
calltype = 'All'
else:
print("ERR: Unknown call type " + hex(rawdata[i]) + ' at offset ' + str(i) )
exit()
i += 1
# ID: TG/DMRID als BCD, 4 bytes
radioid = ''
radioid += str( (rawdata[i] & 0xf0) >> 4 )
radioid += str( (rawdata[i] & 0x0f) )
radioid += str( (rawdata[i+1] & 0xf0) >> 4 )
radioid += str( (rawdata[i+1] & 0x0f) )
radioid += str( (rawdata[i+2] & 0xf0) >> 4 )
radioid += str( (rawdata[i+2] & 0x0f) )
radioid += str( (rawdata[i+3] & 0xf0) >> 4 )
radioid += str( (rawdata[i+3] & 0x0f) )
radioid = radioid.lstrip('0')
i += 4
# CA: Call Alert. 00 -> none, 01 -> Ring, 02 -> Online Alert
if rawdata[i] == 0:
callalert = 'None'
elif rawdata[i] == 1:
callalert = 'Ring'
elif rawdata[i] == 2:
callalert = 'Online Alert'
else:
print("ERR: Unknown call alert " + hex(rawdata[i]) + ' at offset ' + str(i) )
exit()
i += 1
# NA: Name. Ascii, 0 terminated, max 16 bytes
name = ''
while ( rawdata[i] != 0x00 ):
name += chr(rawdata[i])
i += 1
i += 1
# CI: City. Ascii, 0 terminated, max 15 bytes
city = ''
while ( rawdata[i] != 0x00 ):
city += chr(rawdata[i])
i += 1
i += 1
# CS: Callsign, Ascii, 0 terminated , max 8 bytes
callsign = ''
while ( rawdata[i] != 0x00 ):
callsign += chr(rawdata[i])
i += 1
i += 1
# SP: State/Prov, Ascii, 0 terminated, max. 16 bytes
state = ''
while ( rawdata[i] != 0x00 ):
state += chr(rawdata[i])
i += 1
i += 1
# CO: Country, Ascii, 0 terminated, max. 16 bytes
country = ''
while ( rawdata[i] != 0x00 ):
country += chr(rawdata[i])
i += 1
i += 1
# RE: Remarks, Ascii, 0 terminated, max. 16 bytes
remarks = ''
while ( rawdata[i] != 0x00 ):
remarks += chr(rawdata[i])
i += 1
i += 1
#1,1023001,VE3THW,Wayne,Toronto,Ontario,Canada,,Private Call,None
f.write( '"' + str(numdatasets) + '","' + radioid + '","' + callsign + '","' + name + '","' + city + '","' + state + '","' + country + '","' + remarks + '","' + calltype + '","' + callalert + '"' + "\r\n" )
# all done. close file
f.close()
# read csv and write contact list to device
def csv2contactlist(importfilename):
offsetData = bytearray()
contactListData = bytearray()
numContacts = 0
# read contact list file
with open(importfilename, newline = '', encoding='latin1') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',') #, quotechar='"') # fixme: CPS uses quotechar, https://github.com/ContactLists/ContactLists doesn´t
# read header
headers = next(csvreader, None)
# 'No.', 'Radio ID', 'Callsign', 'Name', 'City', 'State', 'Country', 'Remarks', 'Call Type', 'Call Alert'
numcols = 0
# mandatory columns
columnidx = {
'radioid' : -1,
'callsign' : -1,
'name' : -1,
'city' : -1,
'state' : -1,
'country' : -1,
'remarks' : -1,
'calltype' : -1,
'callalert' : -1
}
for h in headers:
if h == 'No.':
pass # number is not needed
elif h == 'Radio ID':
columnidx['radioid'] = numcols
elif h == 'Callsign':
columnidx['callsign'] = numcols
elif h == 'Name':
columnidx['name'] = numcols
elif h == 'City':
columnidx['city'] = numcols
elif h == 'State':
columnidx['state'] = numcols
elif h == 'Country':
columnidx['country'] = numcols
elif h == 'Remarks':
columnidx['remarks'] = numcols
elif h == 'Call Type':
columnidx['calltype'] = numcols
elif h == 'Call Alert':
columnidx['callalert'] = numcols
else:
print('WAR: Unknown column: ' + h)
numcols += 1
for x in columnidx:
if columnidx[x] == -1:
print('ERR: Mandatory column for ' + x + ' not present in .csv file!')
exit()
### read all datasets
numcalltypeall = 0
actualcontactofs = 0
numContacts = 0
for line in csvreader:
## build contact list data
# byte 0: Call Type
contactentry = bytearray()
if line[columnidx['calltype']] == 'All':
contactentry.append(2)
numcalltypeall += 1
if numcalltypeall > 1:
print('ERR: Only one entry with call type "All" allowed')
exit()
elif line[columnidx['calltype']] == 'Group Call':
contactentry.append(1)
else:
contactentry.append(0)
# byte 1..4: radio id
radioidstr = line[columnidx['radioid']]
if radioidstr != '':
radioid = 0
for i in range(len(radioidstr)):
radioid <<= 4
radioid |= ord(radioidstr[i]) - ord('0')
contactentry.append( (radioid >> 24) & 0xff )
contactentry.append( (radioid >> 16) & 0xff )
contactentry.append( (radioid >> 8) & 0xff )
contactentry.append( radioid & 0xff )
else:
print('ERR: Radio ID does not be empty!')
exit()
# byte 5: call alert
if line[columnidx['callalert']] == 'Online Alert':
contactentry.append(2)
elif line[columnidx['callalert']] == 'Ring':
contactentry.append(1)
else:
contactentry.append(0)
# byte 6+ ... name
for i in range( min(16, len(line[ columnidx['name'] ])) ):
contactentry.append( ord(line[ columnidx['name'] ][i]) )
contactentry.append(0)
# ... city ...
for i in range( min(15, len(line[ columnidx['city'] ])) ):
contactentry.append( ord(line[ columnidx['city'] ][i]) )
contactentry.append(0)
# ... callsign ...
for i in range( min(8, len(line[ columnidx['callsign'] ])) ):
contactentry.append( ord(line[ columnidx['callsign'] ][i]) )
contactentry.append(0)
# ... state ...
for i in range( min(16, len(line[ columnidx['state'] ])) ):
contactentry.append( ord(line[ columnidx['state'] ][i]) )
contactentry.append(0)
# ... country ...
for i in range( min(16, len(line[ columnidx['country'] ])) ):
contactentry.append( ord(line[ columnidx['country'] ][i]) )
contactentry.append(0)
# ... remarks ...
for i in range( min(16, len(line[ columnidx['remarks'] ])) ):
contactentry.append( ord(line[ columnidx['remarks'] ][i]) )
contactentry.append(0)
## build offset table
offsetentry = bytearray()
calltype = line[columnidx['calltype']]
idandtype = radioid << 1
idandtype |= (calltype == 'Group Call') # fixme: what about "all"?
offsetentry.append( idandtype & 0xff )
offsetentry.append( (idandtype >> 8) & 0xff )
offsetentry.append( (idandtype >> 16) & 0xff )
offsetentry.append( (idandtype >> 24) & 0xff )
offsetentry.append( actualcontactofs & 0xff )
offsetentry.append( (actualcontactofs >> 8) & 0xff )
offsetentry.append( (actualcontactofs >> 16) & 0xff )
offsetentry.append( (actualcontactofs >> 24) & 0xff )
# everything collected for this dataset
contactListData += contactentry
offsetData += offsetentry
actualcontactofs += len(contactentry)
numContacts += 1
return [offsetData, contactListData, numContacts]