blob: c2a2b7d0682cd17f3c6de4f196c5a137a27dde5b [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
Ole Troan33a58172019-09-04 09:12:29 +020029def walk_defs(s, is_message=False):
Ole Troan9d420872017-10-12 13:06:35 +020030 r = []
31 for t in s:
32 d = []
33 d.append(t.name)
34 for b in t.block:
Ole Troan33a58172019-09-04 09:12:29 +020035 if b.type == 'Option':
36 continue
Ole Troan9d420872017-10-12 13:06:35 +020037 if b.type == 'Field':
Ole Troan9ac11382019-04-23 17:11:01 +020038 if b.limit:
39 d.append([b.fieldtype, b.fieldname, b.limit])
40 else:
41 d.append([b.fieldtype, b.fieldname])
Ole Troan9d420872017-10-12 13:06:35 +020042 elif b.type == 'Array':
43 if b.lengthfield:
Ole Troan33a58172019-09-04 09:12:29 +020044 d.append([b.fieldtype, b.fieldname,
45 b.length, b.lengthfield])
Ole Troan9d420872017-10-12 13:06:35 +020046 else:
Ole Troan413f4a52018-11-28 11:36:05 +010047 d.append([b.fieldtype, b.fieldname, b.length])
Ole Troan2c2feab2018-04-24 00:02:37 -040048 elif b.type == 'Union':
Ole Troan413f4a52018-11-28 11:36:05 +010049 pass
Ole Troan9d420872017-10-12 13:06:35 +020050 else:
51 raise ValueError("Error in processing array type %s" % b)
Ole Troan413f4a52018-11-28 11:36:05 +010052
Ole Troan8dbfb432019-04-24 14:31:18 +020053 if is_message and t.crc:
Ole Troan9d420872017-10-12 13:06:35 +020054 c = {}
55 c['crc'] = "{0:#0{1}x}".format(t.crc, 10)
56 d.append(c)
57
58 r.append(d)
59 return r
60
Ole Troan9d420872017-10-12 13:06:35 +020061#
62# Plugin entry point
63#
Ole Troan2a1ca782019-09-19 01:08:30 +020064def run(args, filename, s):
Ole Troan9d420872017-10-12 13:06:35 +020065 j = {}
66
Ole Troan33a58172019-09-04 09:12:29 +020067 j['types'] = (walk_defs([o for o in s['types']
68 if o.__class__.__name__ == 'Typedef']))
Ole Troan8dbfb432019-04-24 14:31:18 +020069 j['messages'] = walk_defs(s['Define'], True)
Ole Troan33a58172019-09-04 09:12:29 +020070 j['unions'] = (walk_defs([o for o in s['types']
71 if o.__class__.__name__ == 'Union']))
72 j['enums'] = (walk_enums([o for o in s['types']
73 if o.__class__.__name__ == 'Enum']))
Ole Troan2c2feab2018-04-24 00:02:37 -040074 j['services'] = walk_services(s['Service'])
Ole Troanca80dcc2019-02-26 18:03:16 +010075 j['options'] = s['Option']
Ole Troan75761b92019-09-11 17:49:08 +020076 j['aliases'] = {o.name:o.alias for o in s['types'] if o.__class__.__name__ == 'Using'}
Ole Troan8dbfb432019-04-24 14:31:18 +020077 j['vl_api_version'] = hex(s['file_crc'])
Ole Troan9d420872017-10-12 13:06:35 +020078 return json.dumps(j, indent=4, separators=(',', ': '))