2018-01-11 23:03:55 +00:00
|
|
|
#!/usr/bin/env python
|
2019-02-04 12:53:05 +00:00
|
|
|
import sys
|
2017-05-29 11:46:32 +00:00
|
|
|
from collections import defaultdict
|
2019-04-18 14:27:27 +00:00
|
|
|
|
|
|
|
from messages_pb2 import (
|
|
|
|
MessageType,
|
|
|
|
wire_bootloader,
|
|
|
|
wire_debug_in,
|
|
|
|
wire_debug_out,
|
|
|
|
wire_in,
|
|
|
|
wire_no_fsm,
|
|
|
|
wire_out,
|
|
|
|
)
|
2017-05-29 11:46:32 +00:00
|
|
|
|
2019-02-04 12:53:05 +00:00
|
|
|
fh = open("messages_map.h", "wt")
|
|
|
|
fl = open("messages_map_limits.h", "wt")
|
|
|
|
|
2017-05-29 11:46:32 +00:00
|
|
|
# len("MessageType_MessageType_") - len("_fields") == 17
|
2019-02-04 12:53:05 +00:00
|
|
|
TEMPLATE = "\t{{ {type} {dir} {msg_id:46} {fields:29} {process_func} }},\n"
|
2017-05-29 11:46:32 +00:00
|
|
|
|
|
|
|
LABELS = {
|
|
|
|
wire_in: "in messages",
|
|
|
|
wire_out: "out messages",
|
|
|
|
wire_debug_in: "debug in messages",
|
|
|
|
wire_debug_out: "debug out messages",
|
|
|
|
}
|
|
|
|
|
2017-12-24 12:33:18 +00:00
|
|
|
|
2019-02-04 12:53:05 +00:00
|
|
|
def handle_message(fh, fl, skipped, message, extension):
|
2017-05-29 11:46:32 +00:00
|
|
|
name = message.name
|
|
|
|
short_name = name.split("MessageType_", 1).pop()
|
2019-04-18 14:27:27 +00:00
|
|
|
assert short_name != name
|
2017-05-29 11:46:32 +00:00
|
|
|
|
2019-02-04 12:53:05 +00:00
|
|
|
for s in skipped:
|
|
|
|
if short_name.startswith(s):
|
|
|
|
return
|
|
|
|
|
2017-05-29 11:46:32 +00:00
|
|
|
interface = "d" if extension in (wire_debug_in, wire_debug_out) else "n"
|
|
|
|
direction = "i" if extension in (wire_in, wire_debug_in) else "o"
|
|
|
|
|
|
|
|
options = message.GetOptions()
|
|
|
|
bootloader = options.Extensions[wire_bootloader]
|
2018-07-10 14:58:00 +00:00
|
|
|
no_fsm = options.Extensions[wire_no_fsm]
|
2017-05-29 11:46:32 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
if getattr(options, "deprecated", None):
|
|
|
|
fh.write("\t// Message %s is deprecated\n" % short_name)
|
2019-02-04 12:53:05 +00:00
|
|
|
return
|
2017-05-29 12:17:12 +00:00
|
|
|
if bootloader:
|
2019-04-18 14:27:27 +00:00
|
|
|
fh.write("\t// Message %s is used in bootloader mode only\n" % short_name)
|
2019-02-04 12:53:05 +00:00
|
|
|
return
|
2018-07-10 14:58:00 +00:00
|
|
|
if no_fsm:
|
2019-04-18 14:27:27 +00:00
|
|
|
fh.write("\t// Message %s is not used in FSM\n" % short_name)
|
2019-02-04 12:53:05 +00:00
|
|
|
return
|
2017-05-29 11:46:32 +00:00
|
|
|
|
2017-12-24 12:33:18 +00:00
|
|
|
if direction == "i":
|
2019-02-04 13:25:13 +00:00
|
|
|
process_func = "(void (*)(const void *))fsm_msg%s" % short_name
|
2017-12-24 12:33:18 +00:00
|
|
|
else:
|
|
|
|
process_func = "0"
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
fh.write(
|
|
|
|
TEMPLATE.format(
|
|
|
|
type="'%c'," % interface,
|
|
|
|
dir="'%c'," % direction,
|
|
|
|
msg_id="MessageType_%s," % name,
|
|
|
|
fields="%s_fields," % short_name,
|
|
|
|
process_func=process_func,
|
|
|
|
)
|
|
|
|
)
|
2017-05-29 11:46:32 +00:00
|
|
|
|
2019-02-04 12:53:05 +00:00
|
|
|
bufsize = None
|
|
|
|
t = interface + direction
|
|
|
|
if t == "ni":
|
|
|
|
bufsize = "MSG_IN_SIZE"
|
|
|
|
elif t == "no":
|
|
|
|
bufsize = "MSG_OUT_SIZE"
|
|
|
|
elif t == "do":
|
|
|
|
bufsize = "MSG_DEBUG_OUT_SIZE"
|
|
|
|
if bufsize:
|
2019-04-18 14:27:27 +00:00
|
|
|
fl.write(
|
|
|
|
'_Static_assert(%s >= sizeof(%s), "msg buffer too small");\n'
|
|
|
|
% (bufsize, short_name)
|
|
|
|
)
|
2017-12-24 12:33:18 +00:00
|
|
|
|
2019-02-04 12:53:05 +00:00
|
|
|
|
|
|
|
skipped = sys.argv[1:]
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
fh.write(
|
|
|
|
"\t// This file is automatically generated by messages_map.py -- DO NOT EDIT!\n"
|
|
|
|
)
|
|
|
|
fl.write(
|
|
|
|
"// This file is automatically generated by messages_map.py -- DO NOT EDIT!\n\n"
|
|
|
|
)
|
2017-05-29 11:46:32 +00:00
|
|
|
|
|
|
|
messages = defaultdict(list)
|
|
|
|
|
|
|
|
for message in MessageType.DESCRIPTOR.values:
|
|
|
|
extensions = message.GetOptions().Extensions
|
|
|
|
|
|
|
|
for extension in (wire_in, wire_out, wire_debug_in, wire_debug_out):
|
|
|
|
if extensions[extension]:
|
|
|
|
messages[extension].append(message)
|
|
|
|
|
|
|
|
for extension in (wire_in, wire_out, wire_debug_in, wire_debug_out):
|
|
|
|
if extension == wire_debug_in:
|
2019-02-04 12:53:05 +00:00
|
|
|
fh.write("\n#if DEBUG_LINK\n")
|
|
|
|
fl.write("\n#if DEBUG_LINK\n")
|
2017-05-29 11:46:32 +00:00
|
|
|
|
2019-02-04 12:53:05 +00:00
|
|
|
fh.write("\n\t// {label}\n\n".format(label=LABELS[extension]))
|
2017-05-29 11:46:32 +00:00
|
|
|
|
|
|
|
for message in messages[extension]:
|
2019-02-04 12:53:05 +00:00
|
|
|
handle_message(fh, fl, skipped, message, extension)
|
2017-05-29 11:46:32 +00:00
|
|
|
|
|
|
|
if extension == wire_debug_out:
|
2019-02-04 12:53:05 +00:00
|
|
|
fh.write("\n#endif\n")
|
|
|
|
fl.write("#endif\n")
|
|
|
|
|
|
|
|
fh.close()
|
|
|
|
fl.close()
|