Skip to content

Commit 86540ac

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

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

core/src/apps/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import storage.cache
44
import storage.device
5-
from trezor import config, loop, ui, utils, wire, workflow
5+
from trezor import config, loop, protobuf, ui, utils, wire, workflow
66
from trezor.enums import MessageType
77
from trezor.messages import Success, UnlockPath
88

99
from . import workflow_handlers
1010

1111
if TYPE_CHECKING:
12-
from trezor import protobuf
1312
from trezor.messages import (
1413
Features,
1514
Initialize,

core/src/trezor/protobuf.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,47 @@ 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+
if value:
55+
if type(value) is type(
56+
msg
57+
): # can't figure out what exact type due to shitty ffi designs from upstream
58+
print(
59+
f"{' ' * (indent)}{str(key)}:{value.MESSAGE_NAME}:{type(value)} = "
60+
)
61+
print_message(value, indent + 1)
62+
else:
63+
print(f"{' ' * (indent)}{str(key)}:{type(value)} = ", end="")
64+
if type(value) is not bytes:
65+
print(
66+
str(value)
67+
) # have to use otherwise may corrupt the output
68+
else:
69+
print("".join(f"{x:02x}" for x in value))
70+
elif not drop_none:
71+
print(f"{' ' * (indent)}{str(key)}:{type(value)} = {str(value)}")
72+
else:
73+
continue
74+
75+
except Exception as ex:
76+
from traceback import print_exception
77+
78+
print(
79+
f"Error while handling Msg: {msg.MESSAGE_NAME} Type: {msg.MESSAGE_WIRE_TYPE}"
80+
)
81+
print_exception(ex)
82+
pass
83+
84+
if indent == 0:
85+
print(
86+
f"========================= END --- Msg: {msg.MESSAGE_NAME} Type: {msg.MESSAGE_WIRE_TYPE} ========================="
87+
)

core/src/trezor/wire/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,19 @@ 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,
347347
msg.MESSAGE_NAME,
348+
msg.MESSAGE_WIRE_TYPE,
348349
)
349350

350351
# cannot write message without wire type
351352
assert msg.MESSAGE_WIRE_TYPE is not None
352353

354+
if __debug__:
355+
protobuf.print_message(msg, drop_none=True)
356+
353357
msg_size = protobuf.encoded_length(msg)
354358

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

0 commit comments

Comments
 (0)