-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfgc.py
More file actions
452 lines (387 loc) · 19.8 KB
/
cfgc.py
File metadata and controls
452 lines (387 loc) · 19.8 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import sys
import os
import getopt
import struct
import json
import base64
import socket
import re
import math
from pyclibrary import CParser
from pprint import pprint
from datetime import datetime, timezone
usage = """usage:
cfgc.py [-H <header>] -i <infile> | --in=<infile> [-o <outfile> | --out=<outfile>] [-c | --compile] -e | --encode
cfgc.py [-H <header>] -i <infile> | --in=<infile> [-o <outfile> | --out=<outfile>] -d | --decode
cfgc.py -h | --help
Options:
-h | --help Show this information.
-H <header> | --header=<header> Interface header file path.
-i <infile> | --in=<infile> Input file.
-o <outfile> | --out=<outfile> Output file.
-c | --compile Compile AT commands to JSON representation.
-e | --encode Encode JSON representation to binary format.
-d | --decode Decode binary format to JSON representation.
"""
# Default interface header: WINCS02 nc_driver header relative to this script's location.
DEFAULT_HEADER = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'..', '..', 'WINCS02', 'driver', 'src', 'include',
'microchip_wincs02_intf.h')
if __name__ == "__main__":
try:
opts, remaining = getopt.getopt(sys.argv[1:], 'hH:i:o:edc', ['help', 'header=', 'in=','out=','encode','decode','compile'])
except getopt.GetoptError:
sys.exit(1)
cfg_in = None
cfg_out = None
cfg_in_fh = None
cfg_out_fh = None
encode = False
cfg_in_mode = ''
decode = False
cfg_out_mode = ''
cfg_out_encoding = None
compile = False
src_in = None
src_out = None
src_in_fh = None
src_in_json = None
src_out_fh = None
src_out_mode = None
header_path = DEFAULT_HEADER
print('RNWF Configuration Command Compiler/Encoder/Decoder (c) 2025 Microchip Technology Inc')
print()
for opt, arg in opts:
if opt in ('-h', '--help'):
print(usage)
elif opt in ('-H', '--header'):
header_path = os.path.normpath(arg)
elif opt in ('-i', '--in'):
cfg_in = os.path.normpath(arg)
src_in = cfg_in
elif opt in ('-o', '--out'):
cfg_out = os.path.normpath(arg)
src_out = cfg_out
elif opt in ('-e', '--encode'):
encode = True
cfg_in_mode = 'r'
cfg_out_mode = 'wb'
cfg_out_encoding = None
elif opt in ('-d', '--decode'):
decode = True
cfg_in_mode = 'rb'
cfg_out_mode = 'w'
cfg_out_encoding = 'utf8'
elif opt in ('-c', '--compile'):
compile = True
src_in_mode = 'r'
src_out_mode = 'w'
if not encode and not decode and not compile:
exit(0)
if compile:
cfg_in = None
else:
src_in = None
header_path = os.path.normpath(header_path)
print('parsing interface header %s' %(header_path))
parser = CParser(header_path)
print('building tables...')
all_macros = parser.defs['macros']
all_modules = {parser.eval(val): key for key, val in all_macros.items() if key.startswith('WINC_MOD_ID_')}
all_commands = {parser.eval(val): key for key, val in all_macros.items() if key.startswith('WINC_CMD_ID_')}
all_values = parser.defs['values']
all_types = {val: key for key, val in all_values.items() if key.startswith('WINC_TYPE')}
if compile and src_in:
print('opening source file %s' %(src_in))
src_in_fh = open(src_in, src_in_mode)
elif cfg_in:
print('opening configuration file %s' %(cfg_in))
cfg_in_fh = open(cfg_in, cfg_in_mode)
if cfg_out_mode:
if cfg_out:
print('opening output file %s' %(cfg_out))
cfg_out_fh = open(cfg_out, cfg_out_mode, encoding=cfg_out_encoding)
else:
print('outputting to terminal')
cfg_out_fh = sys.stdout
elif src_out_mode:
if src_out:
src_out_fh = open(src_out, src_out_mode)
print('opening output file %s' %(src_out))
else:
src_out_fh = sys.stdout
print('outputting to terminal')
F_CFG_ARC_HDR = 'bbHI'
F_CFG_ARC_ELEM_HDR = 'HHbb'
F_CFG_TLV_ELEM = '>bbH'
if decode and cfg_in_fh:
print('decoding %s' %(cfg_in))
cfg_arc_file_header = cfg_in_fh.read(struct.calcsize(F_CFG_ARC_HDR))
if cfg_arc_file_header:
cfg_arc_hdr = dict(zip(['sig', 'hdr_len', 'pad', 'time'], struct.unpack(F_CFG_ARC_HDR, cfg_arc_file_header)))
cfg_cmds = []
while True:
elem_file_hdr = cfg_in_fh.read(struct.calcsize(F_CFG_ARC_ELEM_HDR))
if not elem_file_hdr:
break
elem_list_hdr = dict(zip(['id', 'data_length', 'num_elements', 'pad'], struct.unpack(F_CFG_ARC_ELEM_HDR, elem_file_hdr)))
elem_list_data = cfg_in_fh.read(elem_list_hdr['data_length'])
if not elem_list_data:
break
elems = []
for i in range(elem_list_hdr['num_elements']):
elem_hdr = dict(zip(['type', 'flags', 'length'], struct.unpack(F_CFG_TLV_ELEM, elem_list_data[0:struct.calcsize(F_CFG_TLV_ELEM)])))
elem_list_data = elem_list_data[struct.calcsize(F_CFG_TLV_ELEM):]
elem_data = elem_list_data[0:elem_hdr['length']]
elem_list_data = elem_list_data[elem_hdr['length']:]
elem = {}
elem['type'] = all_types[elem_hdr['type']]
elem['length'] = len(elem_data)
if elem['type'] == 'WINC_TYPE_INTEGER':
elem['value'] = int.from_bytes(elem_data, byteorder='big', signed=True)
elif elem['type'] == 'WINC_TYPE_INTEGER_UNSIGNED':
elem['value'] = int.from_bytes(elem_data, byteorder='big', signed=False)
elif elem['type'] == 'WINC_TYPE_INTEGER_FRAC':
frac = dict(zip(['i', 'f'], struct.unpack('>HH', elem_data)))
elem['value'] = float('%d.%d' %(frac['i'],frac['f']))
elif elem['type'] == 'WINC_TYPE_STRING':
test_str = ''
for s in elem_data.decode('utf-8'):
if s.isprintable():
test_str += s
elif s in '\t\\\"\r\n\a\b\v\f\x1b':
test_str += s
else:
test_str = None
break
if test_str:
elem['value'] = test_str
elem['encoding'] = 'ascii'
else:
elem['value'] = elem_data.hex()
elem['encoding'] = 'bytes'
elif elem['type'] == 'WINC_TYPE_BYTE_ARRAY':
elem['value'] = elem_data.hex()
elem['encoding'] = 'bytes'
elif elem['type'] == 'WINC_TYPE_BOOL':
elem['value'] = ord(elem_data) != 0
elif elem['type'] == 'WINC_TYPE_IPV4ADDR':
elem['value'] = socket.inet_ntop(socket.AF_INET, elem_data[0:4])
if elem['length'] == 5:
elem['scope'] = elem_data[4]
elif elem['length'] == 8:
elem['scope'] = socket.inet_ntop(socket.AF_INET, elem_data[4:8])
elif elem['type'] == 'WINC_TYPE_IPV6ADDR':
elem['value'] = socket.inet_ntop(socket.AF_INET6, elem_data[0:16])
if elem['length'] == 17:
elem['scope'] = elem_data[16]
elif elem['type'] == 'WINC_TYPE_MACADDR':
elem['length'] = 6
elem['value'] = elem_data[:6].hex(':')
elif elem['type'] == 'WINC_TYPE_UTC_TIME':
elem['value'] = int.from_bytes(elem_data, byteorder='big', signed=False)
else:
elem['value'] = elem_data
elems.append(elem)
cfg_cmds.append({'cmd':all_commands[elem_list_hdr['id']], 'elems':elems})
if cfg_out_fh:
print('outputting decoded configuration')
print(json.dumps(cfg_cmds, indent=4), file=cfg_out_fh)
elif compile and src_in:
print('compiling %s' %(src_in))
cfg_cmds = []
for line in src_in_fh:
line = line.rstrip()
if line.endswith('*/'):
m = re.search('^AT\+(\w+)=(.+)\s+/\*\s*(.*)\s*\*/$', line)
else:
m = re.search('^AT\+(\w+)=(.+)$', line)
if m:
command = m.group(1)
parameters = m.group(2).rstrip()
comment = None
if len(m.groups()) == 3:
comment = m.group(3).rstrip()
if 'WINC_CMD_ID_'+command in all_commands.values():
elems = []
param_list = re.split(r',\s*(?=(?:[^"]*"[^"]*")*[^"]*$)', parameters)
for param in param_list:
elem = {}
if param.startswith('"'):
param = param.strip('"')
try:
if '/' in param:
(ip, scope) = param.split('/')
else:
ip = param
scope = None
ipv4 = socket.inet_pton(socket.AF_INET, ip)
if scope:
try:
socket.inet_pton(socket.AF_INET, scope)
elem['length'] = 8
except:
scope = int(scope)
elem['length'] = 5
else:
elem['length'] = 4
except:
try:
if '/' in param:
(ip, scope) = param.split('/')
else:
ip = param
scope = None
ipv6 = socket.inet_pton(socket.AF_INET6, ip)
if scope:
scope = int(scope)
except:
if re.search('^(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})$', param):
elem['type'] = 'WINC_TYPE_MACADDR'
elem['value'] = param
elem['length'] = 6
elif re.search('^((?:(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?))(Z|[\+-]\d{2}:\d{2})?)$', param):
elem['type'] = 'WINC_TYPE_UTC_TIME'
elem['value'] = int((datetime.strptime(param, '%Y-%m-%dT%H:%M:%S.%fZ') - datetime(1970, 1, 1)).total_seconds())
elem['length'] = 4
else:
elem['type'] = 'WINC_TYPE_STRING'
elem['value'] = ''
escape = False
for s in param:
if escape == True:
escape_char = {'t':0x09, '\\':0x5c, '"':0x22, 'r':0x0d, 'n':0x0a, 'a':0x07, 'b':0x08, 'v':0x0b, 'f':0x0c, 'e':0x1b}
elem['value'] += chr(escape_char[s])
escape = False
elif s == '\\':
escape = True
else:
elem['value'] += s
elem['length'] = len(elem['value'])
elem['encoding'] = 'ascii'
else:
elem['type'] = 'WINC_TYPE_IPV6ADDR'
elem['value'] = ip
if scope:
elem['length'] = 17
elem['scope'] = scope
else:
elem['length'] = 16
else:
elem['type'] = 'WINC_TYPE_IPV4ADDR'
elem['value'] = ip
if scope:
elem['scope'] = scope
elif param.startswith('['):
elem['type'] = 'WINC_TYPE_BYTE_ARRAY'
elem['value'] = param.strip('[]')
elem['length'] = int(len(elem['value'])/2)
elem['encoding'] = 'bytes'
elif param.startswith('0x'):
elem['type'] = 'WINC_TYPE_INTEGER_UNSIGNED'
elem['value'] = int(param, 16)
elem['length'] = int((math.ceil(math.log(elem['value'], 2))+7)/8)
elif param.startswith('0o'):
elem['type'] = 'WINC_TYPE_INTEGER_UNSIGNED'
elem['value'] = int(param, 8)
elem['length'] = int((math.ceil(math.log(elem['value'], 2))+7)/8)
elif param.startswith('0b'):
elem['type'] = 'WINC_TYPE_INTEGER_UNSIGNED'
elem['value'] = int(param, 2)
elem['length'] = int((math.ceil(math.log(elem['value'], 2))+7)/8)
elif param == 'TRUE' or param == 'FALSE':
elem['type'] = 'WINC_TYPE_BOOL'
elem['value'] = (param == 'TRUE')
elem['length'] = 1
elif param.lstrip('-').isdigit():
elem['value'] = int(param)
if elem['value'] < 0:
elem['type'] = 'WINC_TYPE_INTEGER'
elem['length'] = 4
else:
elem['type'] = 'WINC_TYPE_INTEGER_UNSIGNED'
elem['length'] = int((math.ceil(math.log(elem['value'], 2))+7)/8)
else:
elem['type'] = 'WINC_TYPE_INTEGER_FRAC'
elem['value'] = float(param)
elem['length'] = 4
elems.append(elem)
cfg_cmds.append({'at':'AT'+command+'='+parameters, 'comment':comment, 'cmd':'WINC_CMD_ID_'+command, 'elems':elems})
else:
print('error, command \'%s\' not recognised' %(m.group(1)))
def del_nulls(value):
if isinstance(value, dict):
return {k: del_nulls(v) for k, v in value.items() if v is not None}
elif isinstance(value, list):
return [del_nulls(item) for item in value if item is not None]
else:
return value
cfg_cmds = del_nulls(cfg_cmds)
src_in_json = json.dumps(cfg_cmds, indent=4)
if not encode:
print(src_in_json, file=src_out_fh)
if encode:
cfg_in_json = None
if cfg_in_fh:
print('encoding %s' %(cfg_in))
cfg_in_json = json.load(cfg_in_fh)
elif src_in_json:
print('encoding JSON')
cfg_in_json = json.loads(src_in_json)
cfg_out_fh.write(struct.pack(F_CFG_ARC_HDR, 0, struct.calcsize(F_CFG_ARC_HDR), 0, int(datetime.now(timezone.utc).timestamp())))
for cfg_cmd in cfg_in_json:
id = parser.eval(all_macros[cfg_cmd['cmd']])
elem_data_length = 0
elem_data = b''
for elem in cfg_cmd['elems']:
elem_data += struct.pack(F_CFG_TLV_ELEM, all_values[elem['type']], 0, elem['length'])
if elem['type'] == 'WINC_TYPE_INTEGER':
elem_data += elem['value'].to_bytes(elem['length'], byteorder='big', signed=True)
elif elem['type'] == 'WINC_TYPE_INTEGER_UNSIGNED':
elem_data += elem['value'].to_bytes(elem['length'], byteorder='big')
elif elem['type'] == 'WINC_TYPE_INTEGER_FRAC':
(i, f) = str(elem['value']).split('.')
elem_data += int(i).to_bytes(2, byteorder='big', signed=True)
elem_data += int(f).to_bytes(2, byteorder='big')
elif elem['type'] == 'WINC_TYPE_STRING':
if elem['encoding'] == 'bytes':
elem_data += bytes.fromhex(elem['value'].replace(':', ''))
elif elem['encoding'] == 'ascii':
elem_data += elem['value'].encode('utf-8')
else:
pass
elif elem['type'] == 'WINC_TYPE_BYTE_ARRAY':
elem_data += bytes.fromhex(elem['value'])
elif elem['type'] == 'WINC_TYPE_BOOL':
elem_data += bytes((elem['value'],))
elif elem['type'] == 'WINC_TYPE_IPV4ADDR':
elem_data += socket.inet_pton(socket.AF_INET, elem['value']);
if 'scope' in elem:
if isinstance(elem['scope'], int):
elem_data += elem['scope'].to_bytes(1, byteorder='big')
elif isinstance(elem['scope'], str):
elem_data += socket.inet_pton(socket.AF_INET, elem['scope']);
elif elem['type'] == 'WINC_TYPE_IPV6ADDR':
elem_data += socket.inet_pton(socket.AF_INET6, elem['value']);
if 'scope' in elem:
elem_data += elem['scope'].to_bytes(1, byteorder='big')
elif elem['type'] == 'WINC_TYPE_BOOL':
if elem['value']:
elem_data += 1
else:
elem_data += 0
elif elem['type'] == 'WINC_TYPE_MACADDR':
elem_data += bytes.fromhex(elem['value'].replace(':', ''))
elif elem['type'] == 'WINC_TYPE_UTC_TIME':
elem_data += elem['value'].to_bytes(elem['length'], byteorder='big')
else:
pass
if elem_data:
elem_data_length += struct.calcsize(F_CFG_TLV_ELEM) + elem['length']
cfg_out_fh.write(struct.pack(F_CFG_ARC_ELEM_HDR, id, elem_data_length, len(cfg_cmd['elems']), 0))
cfg_out_fh.write(elem_data)
print('encoding complete, data written to %s' %(cfg_out))
if cfg_in_fh:
cfg_in_fh.close()
if cfg_out_fh:
cfg_out_fh.close()