diff --git a/core/embed/upymod/modtrezorio/modtrezorio-hid.h b/core/embed/upymod/modtrezorio/modtrezorio-hid.h index 9b6e7c0a37..ee92df81c1 100644 --- a/core/embed/upymod/modtrezorio/modtrezorio-hid.h +++ b/core/embed/upymod/modtrezorio/modtrezorio-hid.h @@ -150,18 +150,20 @@ STATIC mp_obj_t mod_trezorio_HID_read(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t buf = {0}; mp_get_buffer_raise(args[1], &buf, MP_BUFFER_WRITE); - int offset = mp_obj_get_int(args[2]); + int offset = 0; + if (n_args >= 2) { + offset = mp_obj_get_int(args[2]); + } - int len = buf.len - offset; + int limit; if (n_args >= 3) { - int limit = mp_obj_get_int(args[3]); - if ((limit - offset) < len) { - len = (limit - offset); - } + limit = mp_obj_get_int(args[3]); + } else { + limit = buf.len - offset; } ssize_t r = - usb_hid_read(o->info.iface_num, &((uint8_t *)buf.buf)[offset], len); + usb_hid_read(o->info.iface_num, &((uint8_t *)buf.buf)[offset], limit); return MP_OBJ_NEW_SMALL_INT(r); } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_HID_read_obj, 3, 4, diff --git a/core/embed/upymod/modtrezorio/modtrezorio-webusb.h b/core/embed/upymod/modtrezorio/modtrezorio-webusb.h index bb3c639f14..20b69c99f8 100644 --- a/core/embed/upymod/modtrezorio/modtrezorio-webusb.h +++ b/core/embed/upymod/modtrezorio/modtrezorio-webusb.h @@ -136,21 +136,23 @@ STATIC mp_obj_t mod_trezorio_WebUSB_read(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t buf = {0}; mp_get_buffer_raise(args[1], &buf, MP_BUFFER_WRITE); - int offset = mp_obj_get_int(args[2]); + int offset = 0; + if (n_args >= 2) { + offset = mp_obj_get_int(args[2]); + } - int len = buf.len - offset; + int limit; if (n_args >= 3) { - int limit = mp_obj_get_int(args[3]); - if ((limit - offset) < len) { - len = (limit - offset); - } + limit = mp_obj_get_int(args[3]); + } else { + limit = buf.len - offset; } ssize_t r = - usb_webusb_read(o->info.iface_num, &((uint8_t *)buf.buf)[offset], len); + usb_webusb_read(o->info.iface_num, &((uint8_t *)buf.buf)[offset], limit); return MP_OBJ_NEW_SMALL_INT(r); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_WebUSB_read_obj, 3, 4, +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_WebUSB_read_obj, 2, 4, mod_trezorio_WebUSB_read); STATIC const mp_rom_map_elem_t mod_trezorio_WebUSB_locals_dict_table[] = { diff --git a/core/tests/test_trezor.wire.codec.codec_v1.py b/core/tests/test_trezor.wire.codec.codec_v1.py index 5a73467e25..b4551077aa 100644 --- a/core/tests/test_trezor.wire.codec.codec_v1.py +++ b/core/tests/test_trezor.wire.codec.codec_v1.py @@ -25,12 +25,23 @@ class MockHID: self.packet = packet return gen.send(len(packet)) - def read(self, buffer): + def read(self, buffer, offset=0, limit=None): if self.packet is None: raise Exception("No packet to read") - buffer[:] = self.packet - self.packet = None - return len(buffer) + if limit is None: + limit = len(buffer) - offset + + if len(self.packet) > limit: + end = offset + limit + buffer[offset:end] = self.packet[:limit] + self.packet = None + return limit + else: + end = offset + len(self.packet) + buffer[offset:end] = self.packet + read = len(self.packet) + self.packet = None + return read def wait_object(self, mode): return wait(mode | self.num)