Skip to content

Commit e4a3d7e

Browse files
committed
micropython protobuf support message print
Signed-off-by: Adam BZH <adam@onekey.so>
1 parent 9ebbe60 commit e4a3d7e

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

core/src/trezor/protobuf.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,40 @@ def dump_message_buffer(msg: MessageType) -> bytearray:
4141
buffer = bytearray(encoded_length(msg))
4242
encode(buffer, msg)
4343
return buffer
44+
45+
46+
def print_message(msg: MessageType, indent:int = 0, drop_none: bool = False):
47+
if indent == 0:
48+
print(
49+
f"========================= BEGIN --- Msg: {msg.MESSAGE_NAME} Type: {msg.MESSAGE_WIRE_TYPE} ========================="
50+
)
51+
52+
try:
53+
for key, value in msg.__dict__.items():
54+
55+
if value:
56+
if type(value) == type(msg): # can't figure out what exact type due to shitty ffi designs from upstream
57+
print(f"{' '* (indent)}{str(key)}:{value.MESSAGE_NAME}:{type(value)} = ")
58+
print_message(value, indent + 1)
59+
else:
60+
print(f"{' '* (indent)}{str(key)}:{type(value)} = ", end="")
61+
if type(value) != bytes:
62+
print(str(value)) # have to use otherwise may corrupt the output
63+
else:
64+
print(''.join('{:02x}'.format(x) for x in value))
65+
elif not drop_none:
66+
print(f"{' '* (indent)}{str(key)}:{type(value)} = {str(value)}")
67+
else:
68+
continue
69+
70+
except Exception as ex:
71+
import sys
72+
73+
print(f"Error while handling Msg: {msg.MESSAGE_NAME} Type: {msg.MESSAGE_WIRE_TYPE}")
74+
sys.print_exception(ex)
75+
pass
76+
77+
if indent == 0:
78+
print(
79+
f"========================= END --- Msg: {msg.MESSAGE_NAME} Type: {msg.MESSAGE_WIRE_TYPE} ========================="
80+
)

core/src/trezor/wire/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,18 @@ async def write(self, msg: protobuf.MessageType) -> None:
341341
if __debug__:
342342
log.debug(
343343
__name__,
344-
"%s:%x write: %s",
344+
"%s:%x write: %s %s",
345345
self.iface.iface_num(),
346346
self.sid,
347-
msg.MESSAGE_NAME,
347+
msg.MESSAGE_NAME, msg.MESSAGE_WIRE_TYPE
348348
)
349349

350350
# cannot write message without wire type
351351
assert msg.MESSAGE_WIRE_TYPE is not None
352352

353+
if __debug__:
354+
protobuf.print_message(msg, drop_none=True)
355+
353356
msg_size = protobuf.encoded_length(msg)
354357

355358
if msg_size <= len(self.buffer):

0 commit comments

Comments
 (0)