mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-28 00:58:09 +00:00
core/usb: Add write_blocking() to trezor.io.HID.
This commit is contained in:
parent
5cbbcd0ce7
commit
84674a7463
@ -128,10 +128,29 @@ 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 write_blocking(self, msg: bytes, timeout_ms: int) -> int:
|
||||
/// """
|
||||
/// Sends message using USB HID (device) or UDP (emulator).
|
||||
/// """
|
||||
STATIC mp_obj_t mod_trezorio_HID_write_blocking(mp_obj_t self, mp_obj_t msg,
|
||||
mp_obj_t timeout_ms) {
|
||||
mp_obj_HID_t *o = MP_OBJ_TO_PTR(self);
|
||||
mp_buffer_info_t buf;
|
||||
mp_get_buffer_raise(msg, &buf, MP_BUFFER_READ);
|
||||
uint32_t timeout = trezor_obj_get_uint(timeout_ms);
|
||||
ssize_t r =
|
||||
usb_hid_write_blocking(o->info.iface_num, buf.buf, buf.len, timeout);
|
||||
return MP_OBJ_NEW_SMALL_INT(r);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_HID_write_blocking_obj,
|
||||
mod_trezorio_HID_write_blocking);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mod_trezorio_HID_locals_dict_table[] = {
|
||||
{MP_ROM_QSTR(MP_QSTR_iface_num),
|
||||
MP_ROM_PTR(&mod_trezorio_HID_iface_num_obj)},
|
||||
{MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mod_trezorio_HID_write_obj)},
|
||||
{MP_ROM_QSTR(MP_QSTR_write_blocking),
|
||||
MP_ROM_PTR(&mod_trezorio_HID_write_blocking_obj)},
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorio_HID_locals_dict,
|
||||
mod_trezorio_HID_locals_dict_table);
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include <string.h>
|
||||
#include <sys/poll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "touch.h"
|
||||
#include "usb.h"
|
||||
@ -227,6 +228,18 @@ int usb_hid_write(uint8_t iface_num, const uint8_t *buf, uint32_t len) {
|
||||
return usb_emulated_write(iface_num, buf, len);
|
||||
}
|
||||
|
||||
int usb_hid_write_blocking(uint8_t iface_num, const uint8_t *buf, uint32_t len,
|
||||
int timeout) {
|
||||
const uint32_t start = clock();
|
||||
while (sectrue != usb_hid_can_write(iface_num)) {
|
||||
if (timeout >= 0 &&
|
||||
(1000 * (clock() - start)) / CLOCKS_PER_SEC >= timeout) {
|
||||
return 0; // Timeout
|
||||
}
|
||||
}
|
||||
return usb_hid_write(iface_num, buf, len);
|
||||
}
|
||||
|
||||
int usb_webusb_write(uint8_t iface_num, const uint8_t *buf, uint32_t len) {
|
||||
if (iface_num >= USBD_MAX_NUM_INTERFACES ||
|
||||
usb_ifaces[iface_num].type != USB_IFACE_TYPE_WEBUSB) {
|
||||
|
@ -171,6 +171,11 @@ class HID:
|
||||
Sends message using USB HID (device) or UDP (emulator).
|
||||
"""
|
||||
|
||||
def write_blocking(self, msg: bytes, timeout_ms: int) -> int:
|
||||
"""
|
||||
Sends message using USB HID (device) or UDP (emulator).
|
||||
"""
|
||||
|
||||
|
||||
# extmod/modtrezorio/modtrezorio-poll.h
|
||||
def poll(ifaces: Iterable[int], list_ref: List, timeout_us: int) -> bool:
|
||||
|
Loading…
Reference in New Issue
Block a user