mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-22 21:30:56 +00:00
refactor(core): modify wire to preallocate its buffers
This commit is contained in:
parent
e2cf9cda05
commit
b0116d0bdc
@ -65,7 +65,7 @@ _boot_apps()
|
|||||||
# initialize the wire codec
|
# initialize the wire codec
|
||||||
wire.setup(usb.iface_wire)
|
wire.setup(usb.iface_wire)
|
||||||
if __debug__:
|
if __debug__:
|
||||||
wire.setup(usb.iface_debug, use_workflow=False)
|
wire.setup(usb.iface_debug, is_debug_session=True)
|
||||||
|
|
||||||
loop.run()
|
loop.run()
|
||||||
|
|
||||||
|
@ -83,9 +83,9 @@ def register(wire_type: int, handler: Handler) -> None:
|
|||||||
workflow_handlers[wire_type] = handler
|
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."""
|
"""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:
|
def clear() -> None:
|
||||||
@ -150,12 +150,16 @@ DUMMY_CONTEXT = DummyContext()
|
|||||||
|
|
||||||
PROTOBUF_BUFFER_SIZE = 8192
|
PROTOBUF_BUFFER_SIZE = 8192
|
||||||
|
|
||||||
|
WIRE_BUFFER = bytearray(PROTOBUF_BUFFER_SIZE)
|
||||||
|
if __debug__:
|
||||||
|
WIRE_BUFFER_DEBUG = bytearray(PROTOBUF_BUFFER_SIZE)
|
||||||
|
|
||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
def __init__(self, iface: WireInterface, sid: int) -> None:
|
def __init__(self, iface: WireInterface, sid: int, buffer: bytearray) -> None:
|
||||||
self.iface = iface
|
self.iface = iface
|
||||||
self.sid = sid
|
self.sid = sid
|
||||||
self.buffer = bytearray(PROTOBUF_BUFFER_SIZE)
|
self.buffer = buffer
|
||||||
self.buffer_reader = utils.BufferReader(self.buffer)
|
self.buffer_reader = utils.BufferReader(self.buffer)
|
||||||
self.buffer_writer = utils.BufferWriter(self.buffer)
|
self.buffer_writer = utils.BufferWriter(self.buffer)
|
||||||
|
|
||||||
@ -302,9 +306,13 @@ class UnexpectedMessageError(Exception):
|
|||||||
|
|
||||||
|
|
||||||
async def handle_session(
|
async def handle_session(
|
||||||
iface: WireInterface, session_id: int, use_workflow: bool = True
|
iface: WireInterface, session_id: int, is_debug_session: bool = True
|
||||||
) -> None:
|
) -> 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
|
next_msg: codec_v1.Message | None = None
|
||||||
res_msg: protobuf.MessageType | None = None
|
res_msg: protobuf.MessageType | None = None
|
||||||
req_type = None
|
req_type = None
|
||||||
@ -382,7 +390,7 @@ async def handle_session(
|
|||||||
# communication inside, but it should eventually return a
|
# communication inside, but it should eventually return a
|
||||||
# response message, or raise an exception (a rather common
|
# response message, or raise an exception (a rather common
|
||||||
# thing to do). Exceptions are handled in the code below.
|
# 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)
|
res_msg = await workflow.spawn(wf_task)
|
||||||
else:
|
else:
|
||||||
res_msg = await wf_task
|
res_msg = await wf_task
|
||||||
|
Loading…
Reference in New Issue
Block a user