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

fixup! refactor(core): split polling can_read and reading from USB

This commit is contained in:
tychovrahe 2024-12-04 13:20:25 +01:00
parent 51556ef6ba
commit 34559a4771
3 changed files with 34 additions and 19 deletions

View File

@ -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,

View File

@ -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[] = {

View File

@ -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)