mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-22 05:10:56 +00:00
modtrezormsg: use usage_page to identify interfaces
This commit is contained in:
parent
d9546d45b9
commit
3f44dfedb5
@ -20,15 +20,15 @@ void msg_init(void)
|
||||
i2c_init(&I2CHandle1);
|
||||
}
|
||||
|
||||
ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
|
||||
ssize_t msg_recv(uint16_t *usage_page, uint8_t *buf, size_t len)
|
||||
{
|
||||
*iface = 0; // use always interface 0 for now
|
||||
*usage_page = 0xFF00; // TODO: return proper usage page
|
||||
return USBD_HID_Rx(buf, len, 1);
|
||||
}
|
||||
|
||||
ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
|
||||
ssize_t msg_send(uint16_t usage_page, const uint8_t *buf, size_t len)
|
||||
{
|
||||
(void)iface; // ignore interface for now
|
||||
(void)usage_page; // TODO: ignore usage page for now
|
||||
if (len > 0) {
|
||||
USBD_HID_SendReport(&hUSBDDevice, (uint8_t *)buf, len);
|
||||
}
|
||||
|
@ -36,12 +36,12 @@ void msg_init(void)
|
||||
assert(b != -1);
|
||||
}
|
||||
|
||||
ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
|
||||
ssize_t msg_recv(uint16_t *usage_page, uint8_t *buf, size_t len)
|
||||
{
|
||||
struct sockaddr_in si;
|
||||
socklen_t sl = sizeof(si);
|
||||
memset(buf, 0, len);
|
||||
*iface = 0; // UDP uses always interface 0
|
||||
*usage_page = 0xFF00; // TODO: return proper usage page
|
||||
ssize_t r = recvfrom(s, buf, len, MSG_DONTWAIT, (struct sockaddr *)&si, &sl);
|
||||
if (r < 0) {
|
||||
return r;
|
||||
@ -51,9 +51,9 @@ ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
|
||||
return r;
|
||||
}
|
||||
|
||||
ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
|
||||
ssize_t msg_send(uint16_t usage_page, const uint8_t *buf, size_t len)
|
||||
{
|
||||
(void)iface; // ignore interface for UDP
|
||||
(void)usage_page; // TODO: ignore usage page for now
|
||||
ssize_t r = len;
|
||||
if (slen > 0) {
|
||||
r = sendto(s, buf, len, MSG_DONTWAIT, (const struct sockaddr *)&si_other, slen);
|
||||
|
@ -39,50 +39,42 @@ STATIC mp_obj_t mod_TrezorMsg_Msg_make_new(const mp_obj_type_t *type, size_t n_a
|
||||
|
||||
/// def trezor.msg.setup(ifaces: list) -> None:
|
||||
/// '''
|
||||
/// Configures USB interfaces with a list of tuples (interface_number, usage_page)
|
||||
/// Configures USB interfaces with a list of (usage_page, ...)
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_TrezorMsg_Msg_setup(mp_obj_t self, mp_obj_t ifaces) {
|
||||
mp_uint_t iface_cnt;
|
||||
mp_obj_t *iface;
|
||||
mp_obj_t *usage_pages;
|
||||
if (MP_OBJ_IS_TYPE(ifaces, &mp_type_tuple)) {
|
||||
mp_obj_tuple_get(ifaces, &iface_cnt, &iface);
|
||||
mp_obj_tuple_get(ifaces, &iface_cnt, &usage_pages);
|
||||
} else
|
||||
if (MP_OBJ_IS_TYPE(ifaces, &mp_type_list)) {
|
||||
mp_obj_list_get(ifaces, &iface_cnt, &iface);
|
||||
mp_obj_list_get(ifaces, &iface_cnt, &usage_pages);
|
||||
} else {
|
||||
mp_raise_TypeError("List or tuple expected");
|
||||
}
|
||||
for (mp_uint_t i = 0; i < iface_cnt; i++) {
|
||||
if (!MP_OBJ_IS_TYPE(iface[i], &mp_type_tuple)) {
|
||||
mp_raise_TypeError("Tuple expected");
|
||||
}
|
||||
mp_uint_t attr_cnt;
|
||||
mp_obj_t *attr;
|
||||
mp_obj_tuple_get(iface[i], &attr_cnt, &attr);
|
||||
assert(attr_cnt == 2);
|
||||
uint8_t endpoint = mp_obj_get_int(attr[0]);
|
||||
uint16_t usage_page = mp_obj_get_int(attr[1]);
|
||||
printf("iface %d: ep=%d up=%04x\n", (uint16_t)i, endpoint, usage_page);
|
||||
uint16_t usage_page = mp_obj_get_int(usage_pages[i]);
|
||||
printf("iface %d: usage_page=%04x\n", i + 1, usage_page);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorMsg_Msg_setup_obj, mod_TrezorMsg_Msg_setup);
|
||||
|
||||
/// def trezor.msg.send(iface: int, message: bytes) -> int:
|
||||
/// def trezor.msg.send(usage_page: int, message: bytes) -> int:
|
||||
/// '''
|
||||
/// Sends message using USB HID (device) or UDP (emulator).
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_TrezorMsg_Msg_send(mp_obj_t self, mp_obj_t iface, mp_obj_t message) {
|
||||
uint8_t iface_num = mp_obj_get_int(iface);
|
||||
STATIC mp_obj_t mod_TrezorMsg_Msg_send(mp_obj_t self, mp_obj_t usage_page, mp_obj_t message) {
|
||||
uint16_t up = mp_obj_get_int(usage_page);
|
||||
mp_buffer_info_t msg;
|
||||
mp_get_buffer_raise(message, &msg, MP_BUFFER_READ);
|
||||
ssize_t r = msg_send(iface_num, msg.buf, msg.len);
|
||||
ssize_t r = msg_send(up, msg.buf, msg.len);
|
||||
return MP_OBJ_NEW_SMALL_INT(r);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorMsg_Msg_send_obj, mod_TrezorMsg_Msg_send);
|
||||
|
||||
#define TICK_RESOLUTION 1000
|
||||
#define TOUCH_IFACE 256
|
||||
#define TOUCH_IFACE 0
|
||||
|
||||
/// def trezor.msg.select(timeout_us: int) -> tuple:
|
||||
/// '''
|
||||
@ -104,12 +96,12 @@ STATIC mp_obj_t mod_TrezorMsg_Msg_select(mp_obj_t self, mp_obj_t timeout_us) {
|
||||
tuple->items[3] = MP_OBJ_NEW_SMALL_INT((e & 0xFF)); // y position
|
||||
return MP_OBJ_FROM_PTR(tuple);
|
||||
}
|
||||
uint8_t iface;
|
||||
uint16_t iface_usage_page;
|
||||
uint8_t recvbuf[64];
|
||||
ssize_t l = msg_recv(&iface, recvbuf, 64);
|
||||
ssize_t l = msg_recv(&iface_usage_page, recvbuf, 64);
|
||||
if (l > 0) {
|
||||
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
|
||||
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(iface);
|
||||
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(iface_usage_page);
|
||||
tuple->items[1] = mp_obj_new_str_of_type(&mp_type_bytes, recvbuf, l);
|
||||
return MP_OBJ_FROM_PTR(tuple);
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ wallet.boot()
|
||||
ui.display.backlight(ui.BACKLIGHT_NORMAL)
|
||||
|
||||
# Just a demo to show how to register USB ifaces
|
||||
msg.setup([(1, 0xF53C), (2, 0xF1D0)])
|
||||
msg.setup([0xFF00, 0xFF01, 0xF1D0])
|
||||
|
||||
# Initialize the wire codec pipeline
|
||||
wire.setup()
|
||||
|
@ -17,10 +17,10 @@ scheduled_tasks = [] # heap: [(time, counter, task, value)]
|
||||
MAX_SELECT_DELAY = const(1000000)
|
||||
|
||||
# message interfaces:
|
||||
# 0-255 - USB HID
|
||||
# 256 - touch event interface
|
||||
# 0x0000 - touch event interface
|
||||
# 0x0001 - 0xFFFF - USB HID
|
||||
|
||||
TOUCH = const(256) # interface
|
||||
TOUCH = const(0) # interface
|
||||
TOUCH_START = const(1) # event
|
||||
TOUCH_MOVE = const(2) # event
|
||||
TOUCH_END = const(4) # event
|
||||
|
@ -2,7 +2,7 @@ import ubinascii
|
||||
from micropython import const
|
||||
from trezor import msg, loop, log
|
||||
|
||||
_DEFAULT_IFACE = const(0)
|
||||
_DEFAULT_IFACE = const(0xFF00)
|
||||
|
||||
|
||||
def read_report_stream(target, iface=_DEFAULT_IFACE):
|
||||
|
Loading…
Reference in New Issue
Block a user