1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-14 03:30:02 +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 M1nd3r
parent c8ab4f83ad
commit 1a1135b9b1
2 changed files with 14 additions and 40 deletions

View File

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

View File

@ -53,9 +53,9 @@ if TYPE_CHECKING:
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."""
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(
@ -88,7 +88,7 @@ if __debug__:
async def _handle_single_message(
ctx: context.Context, msg: codec_v1.Message, use_workflow: bool
ctx: context.Context, msg: codec_v1.Message
) -> bool:
"""Handle a message that was loaded from USB by the caller.
@ -148,14 +148,7 @@ async def _handle_single_message(
# 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:
# 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
res_msg = await workflow.spawn(context.with_context(ctx, task))
except context.UnexpectedMessage:
# Workflow was trying to read a message from the wire, and
@ -198,22 +191,10 @@ async def _handle_single_message(
return msg.type in AVOID_RESTARTING_FOR
async def handle_session(
iface: WireInterface, session_id: int, is_debug_session: bool = False
) -> 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)
async def handle_session(iface: WireInterface, session_id: int) -> None:
ctx = context.Context(iface, session_id, WIRE_BUFFER)
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
# roll back and un-import any others.
modules = utils.unimport_begin()
@ -236,9 +217,7 @@ async def handle_session(
next_msg = None
try:
do_not_restart = await _handle_single_message(
ctx, msg, use_workflow=not is_debug_session
)
do_not_restart = await _handle_single_message(ctx, msg)
except context.UnexpectedMessage as unexpected:
# The workflow was interrupted by an unexpected message. We need to
# process it as if it was a new message...
@ -252,17 +231,13 @@ async def handle_session(
if __debug__:
log.exception(__name__, exc)
finally:
if not __debug__ or not is_debug_session:
# Unload modules imported by the workflow. Should not raise.
# 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)
# Unload modules imported by the workflow. Should not raise.
utils.unimport_end(modules)
if not do_not_restart:
# Let the session be restarted from `main`.
loop.clear()
return # pylint: disable=lost-exception
if not do_not_restart:
# Let the session be restarted from `main`.
loop.clear()
return # pylint: disable=lost-exception
except Exception as exc:
# Log and try again. The session handler can only exit explicitly via