1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-05 13:01:12 +00:00

refactor(core): remove debuglink support from main handle_session

It is being moved into a custom handler for the debug app.
This commit is contained in:
matejcik 2023-07-25 10:47:55 +02:00 committed by matejcik
parent 0334b42c8e
commit 7617bc8c25
2 changed files with 14 additions and 42 deletions

View File

@ -22,9 +22,8 @@ workflow.start_default()
# initialize the wire codec # initialize the wire codec
wire.setup(usb.iface_wire) wire.setup(usb.iface_wire)
if __debug__:
wire.setup(usb.iface_debug, is_debug_session=True)
# start the event loop
loop.run() loop.run()
if __debug__: if __debug__:

View File

@ -53,9 +53,9 @@ if TYPE_CHECKING:
EXPERIMENTAL_ENABLED = False EXPERIMENTAL_ENABLED = False
def setup(iface: WireInterface, is_debug_session: bool = False) -> None: def setup(iface: WireInterface) -> 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, is_debug_session)) loop.schedule(handle_session(iface, codec_v1.SESSION_ID))
def wrap_protobuf_load( def wrap_protobuf_load(
@ -87,9 +87,7 @@ if __debug__:
WIRE_BUFFER_DEBUG = bytearray(PROTOBUF_BUFFER_SIZE_DEBUG) WIRE_BUFFER_DEBUG = bytearray(PROTOBUF_BUFFER_SIZE_DEBUG)
async def _handle_single_message( async def _handle_single_message(ctx: context.Context, msg: codec_v1.Message) -> bool:
ctx: context.Context, msg: codec_v1.Message, use_workflow: bool
) -> bool:
"""Handle a message that was loaded from USB by the caller. """Handle a message that was loaded from USB by the caller.
Find the appropriate handler, run it and write its result on the wire. In case Find the appropriate handler, run it and write its result on the wire. In case
@ -148,14 +146,7 @@ async def _handle_single_message(
# 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: res_msg = await workflow.spawn(context.with_context(ctx, task))
# Spawn a workflow around the task. This ensures that concurrent
# workflows are shut down.
res_msg = await workflow.spawn(context.with_context(ctx, task))
else:
# For debug messages, ignore workflow processing and just await
# results of the handler.
res_msg = await task
except context.UnexpectedMessage: except context.UnexpectedMessage:
# Workflow was trying to read a message from the wire, and # Workflow was trying to read a message from the wire, and
@ -198,22 +189,10 @@ async def _handle_single_message(
return msg.type in AVOID_RESTARTING_FOR return msg.type in AVOID_RESTARTING_FOR
async def handle_session( async def handle_session(iface: WireInterface, session_id: int) -> None:
iface: WireInterface, session_id: int, is_debug_session: bool = False ctx = context.Context(iface, session_id, WIRE_BUFFER)
) -> None:
if __debug__ and is_debug_session:
ctx_buffer = WIRE_BUFFER_DEBUG
else:
ctx_buffer = WIRE_BUFFER
ctx = context.Context(iface, session_id, ctx_buffer)
next_msg: codec_v1.Message | None = None next_msg: codec_v1.Message | None = None
if __debug__ and is_debug_session:
import apps.debug
apps.debug.DEBUG_CONTEXT = ctx
# Take a mark of modules that are imported at this point, so we can # Take a mark of modules that are imported at this point, so we can
# roll back and un-import any others. # roll back and un-import any others.
modules = utils.unimport_begin() modules = utils.unimport_begin()
@ -236,9 +215,7 @@ async def handle_session(
next_msg = None next_msg = None
try: try:
do_not_restart = await _handle_single_message( do_not_restart = await _handle_single_message(ctx, msg)
ctx, msg, use_workflow=not is_debug_session
)
except context.UnexpectedMessage as unexpected: except context.UnexpectedMessage as unexpected:
# The workflow was interrupted by an unexpected message. We need to # The workflow was interrupted by an unexpected message. We need to
# process it as if it was a new message... # process it as if it was a new message...
@ -252,17 +229,13 @@ async def handle_session(
if __debug__: if __debug__:
log.exception(__name__, exc) log.exception(__name__, exc)
finally: finally:
if not __debug__ or not is_debug_session: # Unload modules imported by the workflow. Should not raise.
# Unload modules imported by the workflow. Should not raise. utils.unimport_end(modules)
# This is not done for the debug session because the snapshot taken
# in a debug session would clear modules which are in use by the
# workflow running on wire.
utils.unimport_end(modules)
if not do_not_restart: if not do_not_restart:
# Let the session be restarted from `main`. # Let the session be restarted from `main`.
loop.clear() loop.clear()
return # pylint: disable=lost-exception return # pylint: disable=lost-exception
except Exception as exc: except Exception as exc:
# Log and try again. The session handler can only exit explicitly via # Log and try again. The session handler can only exit explicitly via