Skip to content

Commit

Permalink
fixup! refactor(core): split polling can_read and reading from USB
Browse files Browse the repository at this point in the history
  • Loading branch information
TychoVrahe committed Jan 13, 2025
1 parent edde952 commit 2eeeec5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 6 additions & 2 deletions core/embed/upymod/modtrezorio/modtrezorio-hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ STATIC mp_obj_t mod_trezorio_HID_write(mp_obj_t self, mp_obj_t msg) {
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_HID_write_obj,
mod_trezorio_HID_write);

/// def read(self, buf: bytes, offset: int = 0) -> int
/// def read(self, buf: bytearray, offset: int = 0) -> int:
/// """
/// Reads message using USB HID (device) or UDP (emulator).
/// """
Expand All @@ -159,6 +159,10 @@ STATIC mp_obj_t mod_trezorio_HID_read(size_t n_args, const mp_obj_t *args) {
mp_raise_ValueError("Negative offset not allowed");
}

if (offset > buf.len) {
mp_raise_ValueError("Offset out of bounds");
}

uint32_t buffer_space = buf.len - offset;

if (buffer_space < USB_PACKET_LEN) {
Expand All @@ -174,7 +178,7 @@ STATIC mp_obj_t mod_trezorio_HID_read(size_t n_args, const mp_obj_t *args) {

return MP_OBJ_NEW_SMALL_INT(r);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_HID_read_obj, 3, 4,
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_HID_read_obj, 2, 3,
mod_trezorio_HID_read);

/// def write_blocking(self, msg: bytes, timeout_ms: int) -> int:
Expand Down
6 changes: 5 additions & 1 deletion core/embed/upymod/modtrezorio/modtrezorio-webusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ STATIC mp_obj_t mod_trezorio_WebUSB_write(mp_obj_t self, mp_obj_t msg) {
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_WebUSB_write_obj,
mod_trezorio_WebUSB_write);

/// def read(self, buf: bytes, offset: int = 0) -> int
/// def read(self, buf: bytearray, offset: int = 0) -> int:
/// """
/// Reads message using USB WebUSB (device) or UDP (emulator).
/// """
Expand All @@ -145,6 +145,10 @@ STATIC mp_obj_t mod_trezorio_WebUSB_read(size_t n_args, const mp_obj_t *args) {
mp_raise_ValueError("Negative offset not allowed");
}

if (offset > buf.len) {
mp_raise_ValueError("Offset out of bounds");
}

uint32_t buffer_space = buf.len - offset;

if (buffer_space < USB_PACKET_LEN) {
Expand Down
4 changes: 2 additions & 2 deletions core/mocks/generated/trezorio/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class HID:
Sends message using USB HID (device) or UDP (emulator).
"""

def read(self, buf: bytes, offset: int = 0) -> int
def read(self, buf: bytearray, offset: int = 0) -> int:
"""
Reads message using USB HID (device) or UDP (emulator).
"""
Expand Down Expand Up @@ -156,7 +156,7 @@ class WebUSB:
Sends message using USB WebUSB (device) or UDP (emulator).
"""

def read(self, buf: bytes, offset: int = 0) -> int
def read(self, buf: bytearray, offset: int = 0) -> int:
"""
Reads message using USB WebUSB (device) or UDP (emulator).
"""
Expand Down

0 comments on commit 2eeeec5

Please sign in to comment.