mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-08 14:31:06 +00:00
fixup! refactor(core): split polling can_read and reading from USB
This commit is contained in:
parent
51556ef6ba
commit
34559a4771
@ -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_buffer_info_t buf = {0};
|
||||||
mp_get_buffer_raise(args[1], &buf, MP_BUFFER_WRITE);
|
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) {
|
||||||
int len = buf.len - offset;
|
offset = mp_obj_get_int(args[2]);
|
||||||
if (n_args >= 3) {
|
|
||||||
int limit = mp_obj_get_int(args[3]);
|
|
||||||
if ((limit - offset) < len) {
|
|
||||||
len = (limit - offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int limit;
|
||||||
|
if (n_args >= 3) {
|
||||||
|
limit = mp_obj_get_int(args[3]);
|
||||||
|
} else {
|
||||||
|
limit = buf.len - offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t r =
|
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);
|
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, 3, 4,
|
||||||
|
@ -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_buffer_info_t buf = {0};
|
||||||
mp_get_buffer_raise(args[1], &buf, MP_BUFFER_WRITE);
|
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) {
|
||||||
int len = buf.len - offset;
|
offset = mp_obj_get_int(args[2]);
|
||||||
if (n_args >= 3) {
|
|
||||||
int limit = mp_obj_get_int(args[3]);
|
|
||||||
if ((limit - offset) < len) {
|
|
||||||
len = (limit - offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int limit;
|
||||||
|
if (n_args >= 3) {
|
||||||
|
limit = mp_obj_get_int(args[3]);
|
||||||
|
} else {
|
||||||
|
limit = buf.len - offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t r =
|
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);
|
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);
|
mod_trezorio_WebUSB_read);
|
||||||
|
|
||||||
STATIC const mp_rom_map_elem_t mod_trezorio_WebUSB_locals_dict_table[] = {
|
STATIC const mp_rom_map_elem_t mod_trezorio_WebUSB_locals_dict_table[] = {
|
||||||
|
@ -25,12 +25,23 @@ class MockHID:
|
|||||||
self.packet = packet
|
self.packet = packet
|
||||||
return gen.send(len(packet))
|
return gen.send(len(packet))
|
||||||
|
|
||||||
def read(self, buffer):
|
def read(self, buffer, offset=0, limit=None):
|
||||||
if self.packet is None:
|
if self.packet is None:
|
||||||
raise Exception("No packet to read")
|
raise Exception("No packet to read")
|
||||||
buffer[:] = self.packet
|
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
|
self.packet = None
|
||||||
return len(buffer)
|
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):
|
def wait_object(self, mode):
|
||||||
return wait(mode | self.num)
|
return wait(mode | self.num)
|
||||||
|
Loading…
Reference in New Issue
Block a user