blob: 73bff76cbb7501e33cca6c3a8ead9b786347d6f7 [file] [log] [blame]
Ole Troan9d420872017-10-12 13:06:35 +02001# JSON generation
2import json
3
4
5def walk_enums(s):
6 r = []
7 for e in s:
8 d = []
9 d.append(e.name)
10 for b in e.block:
11 d.append(b)
12 d.append({'enumtype': e.enumtype})
13 r.append(d)
14 return r
15
16
17def walk_services(s):
Marek Gradzkid05b9262018-02-09 13:39:22 +010018 r = {}
Ole Troan9d420872017-10-12 13:06:35 +020019 for e in s:
20 d = {'reply': e.reply}
21 if e.stream:
22 d['stream'] = True
23 if e.events:
24 d['events'] = e.events
Marek Gradzkid05b9262018-02-09 13:39:22 +010025 r[e.caller] = d
Ole Troan9d420872017-10-12 13:06:35 +020026 return r
27
28
29def walk_defs(s):
30 r = []
31 for t in s:
32 d = []
33 d.append(t.name)
34 for b in t.block:
35 f = []
36 if b.type == 'Field':
37 f = [b.fieldtype, b.fieldname]
38 elif b.type == 'Array':
39 if b.lengthfield:
40 f = [b.fieldtype, b.fieldname, b.length, b.lengthfield]
41 else:
42 f = [b.fieldtype, b.fieldname, b.length]
43 else:
44 raise ValueError("Error in processing array type %s" % b)
45 d.append(f)
46 if t.crc:
47 c = {}
48 c['crc'] = "{0:#0{1}x}".format(t.crc, 10)
49 d.append(c)
50
51 r.append(d)
52 return r
53
54
55#
56# Plugin entry point
57#
58def run(filename, s, file_crc):
59 j = {}
60
61 j['types'] = walk_defs(s['typedefs'])
62 j['messages'] = walk_defs(s['defines'])
63 j['enums'] = walk_enums(s['enums'])
64 j['services'] = walk_services(s['services'])
65 j['vl_api_version'] = hex(file_crc)
66 return json.dumps(j, indent=4, separators=(',', ': '))