1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-27 01:48:17 +00:00
trezor-firmware/firmware/protob/messages_map.py

75 lines
2.3 KiB
Python
Raw Normal View History

2018-01-11 23:03:55 +00:00
#!/usr/bin/env python
2017-05-29 11:46:32 +00:00
from collections import defaultdict
from messages_pb2 import MessageType
from messages_pb2 import wire_in, wire_out
from messages_pb2 import wire_debug_in, wire_debug_out
from messages_pb2 import wire_bootloader, wire_no_fsm
2017-05-29 11:46:32 +00:00
# len("MessageType_MessageType_") - len("_fields") == 17
TEMPLATE = "\t{{ {type} {dir} {msg_id:46} {fields:29} {process_func} }},"
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
2017-05-29 11:46:32 +00:00
def handle_message(message, extension):
name = message.name
short_name = name.split("MessageType_", 1).pop()
assert(short_name != name)
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]
no_fsm = options.Extensions[wire_no_fsm]
2017-05-29 11:46:32 +00:00
2017-09-29 19:01:59 +00:00
if getattr(options, 'deprecated', None):
2017-05-29 12:17:12 +00:00
return '\t// Message %s is deprecated' % short_name
if bootloader:
return '\t// Message %s is used in bootloader mode only' % short_name
if no_fsm:
return '\t// Message %s is not used in FSM' % short_name
2017-05-29 11:46:32 +00:00
2017-12-24 12:33:18 +00:00
if direction == "i":
process_func = "(void (*)(void *)) fsm_msg%s" % short_name
else:
process_func = "0"
2017-05-29 12:17:12 +00:00
return TEMPLATE.format(
2017-05-29 11:46:32 +00:00
type="'%c'," % interface,
dir="'%c'," % direction,
msg_id="MessageType_%s," % name,
fields="%s_fields," % short_name,
2017-12-24 12:33:18 +00:00
process_func=process_func,
2017-05-29 11:46:32 +00:00
)
2017-12-24 12:33:18 +00:00
print("\t// This file is automatically generated"
"by messages_map.py -- DO NOT EDIT!")
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:
2017-05-29 12:17:12 +00:00
print("\n#if DEBUG_LINK")
2017-05-29 11:46:32 +00:00
2017-05-29 12:17:12 +00:00
print("\n\t// {label}\n".format(label=LABELS[extension]))
2017-05-29 11:46:32 +00:00
for message in messages[extension]:
print(handle_message(message, extension))
if extension == wire_debug_out:
2017-05-29 12:17:12 +00:00
print("\n#endif")