1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

refactor(core): modify wire to preallocate its buffers

This commit is contained in:
matejcik 2021-02-25 15:37:15 +01:00 committed by matejcik
parent e2cf9cda05
commit b0116d0bdc
2 changed files with 16 additions and 8 deletions

View File

@ -65,7 +65,7 @@ _boot_apps()
# initialize the wire codec
wire.setup(usb.iface_wire)
if __debug__:
wire.setup(usb.iface_debug, use_workflow=False)
wire.setup(usb.iface_debug, is_debug_session=True)
loop.run()

View File

@ -83,9 +83,9 @@ def register(wire_type: int, handler: Handler) -> None:
workflow_handlers[wire_type] = handler
def setup(iface: WireInterface, use_workflow: bool = True) -> None:
def setup(iface: WireInterface, is_debug_session: bool = False) -> None:
"""Initialize the wire stack on passed USB interface."""
loop.schedule(handle_session(iface, codec_v1.SESSION_ID, use_workflow))
loop.schedule(handle_session(iface, codec_v1.SESSION_ID, is_debug_session))
def clear() -> None:
@ -150,12 +150,16 @@ DUMMY_CONTEXT = DummyContext()
PROTOBUF_BUFFER_SIZE = 8192
WIRE_BUFFER = bytearray(PROTOBUF_BUFFER_SIZE)
if __debug__:
WIRE_BUFFER_DEBUG = bytearray(PROTOBUF_BUFFER_SIZE)
class Context:
def __init__(self, iface: WireInterface, sid: int) -> None:
def __init__(self, iface: WireInterface, sid: int, buffer: bytearray) -> None:
self.iface = iface
self.sid = sid
self.buffer = bytearray(PROTOBUF_BUFFER_SIZE)
self.buffer = buffer
self.buffer_reader = utils.BufferReader(self.buffer)
self.buffer_writer = utils.BufferWriter(self.buffer)
@ -302,9 +306,13 @@ class UnexpectedMessageError(Exception):
async def handle_session(
iface: WireInterface, session_id: int, use_workflow: bool = True
iface: WireInterface, session_id: int, is_debug_session: bool = True
) -> None:
ctx = Context(iface, session_id)
if __debug__ and is_debug_session:
ctx_buffer = WIRE_BUFFER_DEBUG
else:
ctx_buffer = WIRE_BUFFER
ctx = Context(iface, session_id, ctx_buffer)
next_msg: codec_v1.Message | None = None
res_msg: protobuf.MessageType | None = None
req_type = None
@ -382,7 +390,7 @@ async def handle_session(
# communication inside, but it should eventually return a
# response message, or raise an exception (a rather common
# thing to do). Exceptions are handled in the code below.
if use_workflow:
if not is_debug_session:
res_msg = await workflow.spawn(wf_task)
else:
res_msg = await wf_task