From 4c987454ebf1c9f577b64c4e42690b220d134d90 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Mon, 14 Aug 2017 11:05:19 +0200 Subject: [PATCH] modtrezormsg: merge to modtrezorio --- Makefile.firmware | 7 - SConscript.firmware | 5 - SConscript.unix | 9 +- .../modtrezorio-msg.h} | 449 +++++++++--------- embed/extmod/modtrezorio/modtrezorio.c | 15 + .../unix-msg-mock.h} | 1 + embed/extmod/modtrezorio/unix-sdcard-mock.h | 7 + .../extmod/modtrezormsg/modtrezormsg-stm32.h | 9 - embed/firmware/mpconfigport.h | 3 - embed/trezorhal/touch.c | 7 + embed/trezorhal/touch.h | 7 + embed/trezorhal/usb.c | 10 +- embed/trezorhal/usb.h | 4 - embed/unix/mpconfigport.h | 7 - 14 files changed, 255 insertions(+), 285 deletions(-) rename embed/extmod/{modtrezormsg/modtrezormsg.c => modtrezorio/modtrezorio-msg.h} (58%) rename embed/extmod/{modtrezormsg/modtrezormsg-unix.h => modtrezorio/unix-msg-mock.h} (99%) delete mode 100644 embed/extmod/modtrezormsg/modtrezormsg-stm32.h diff --git a/Makefile.firmware b/Makefile.firmware index 701adf4e22..093a01d22c 100644 --- a/Makefile.firmware +++ b/Makefile.firmware @@ -84,13 +84,6 @@ OBJ_MOD += $(addprefix $(BUILD_FW)/,\ ) endif -# OBJ embed/extmod/modtrezormsg -ifeq ($(MICROPY_PY_TREZORMSG),1) -OBJ_MOD += $(addprefix $(BUILD_FW)/,\ - extmod/modtrezormsg/modtrezormsg.o \ - ) -endif - # OBJ embed/extmod/modtrezorui ifeq ($(MICROPY_PY_TREZORUI),1) CFLAGS_MOD += -DQR_MAX_VERSION=0 diff --git a/SConscript.firmware b/SConscript.firmware index 539873ffe5..74c3584b71 100644 --- a/SConscript.firmware +++ b/SConscript.firmware @@ -57,11 +57,6 @@ SOURCE_MOD += [ 'embed/extmod/modtrezorio/modtrezorio.c', ] -# modtrezormsg -SOURCE_MOD += [ - 'embed/extmod/modtrezormsg/modtrezormsg.c', -] - # modtrezorui CPPDEFINES_MOD += [('QR_MAX_VERSION', '0')] SOURCE_MOD += [ diff --git a/SConscript.unix b/SConscript.unix index 7582aa3d8a..acc581adbd 100644 --- a/SConscript.unix +++ b/SConscript.unix @@ -58,12 +58,6 @@ SOURCE_MOD += [ 'embed/extmod/modtrezorio/modtrezorio.c', ] -# modtrezormsg -SOURCE_MOD += [ - 'embed/unix/touch.c', - 'embed/extmod/modtrezormsg/modtrezormsg.c', -] - # modtrezorui CPPDEFINES_MOD += [('QR_MAX_VERSION', '0')] SOURCE_MOD += [ @@ -220,6 +214,7 @@ SOURCE_UNIX = [ 'vendor/micropython/unix/moduselect.c', 'vendor/micropython/unix/alloc.c', 'embed/unix/common.c', + 'embed/unix/touch.c', ] SOURCE_EMIT_NATIVE = ['vendor/micropython/py/emitnative.c'] @@ -270,7 +265,7 @@ env.Replace( MPY_TOOL='$PYTHON vendor/micropython/tools/mpy-tool.py', MPY_CROSS='vendor/micropython/mpy-cross/mpy-cross', ) -if ARGUMENTS.get('TREZOR_X86', 0): +if int(ARGUMENTS.get('TREZOR_X86', '0')): env.Append( CCFLAGS='-m32', LINKFLAGS='-m32', ) diff --git a/embed/extmod/modtrezormsg/modtrezormsg.c b/embed/extmod/modtrezorio/modtrezorio-msg.h similarity index 58% rename from embed/extmod/modtrezormsg/modtrezormsg.c rename to embed/extmod/modtrezorio/modtrezorio-msg.h index 7e2eb69bab..4c017763b7 100644 --- a/embed/extmod/modtrezormsg/modtrezormsg.c +++ b/embed/extmod/modtrezorio/modtrezorio-msg.h @@ -1,30 +1,88 @@ /* - * Copyright (c) Pavol Rusnak, Jan Pochyla, SatoshiLabs + * Copyright (c) Pavol Rusnak, SatoshiLabs * * Licensed under TREZOR License * see LICENSE file for details */ -#include -#include -#include - -#include "py/runtime.h" -#include "py/mphal.h" -#include "py/objstr.h" -#include "py/smallint.h" - -#if MICROPY_PY_TREZORMSG - #if defined TREZOR_STM32 -#include "modtrezormsg-stm32.h" +#include "usb.h" +#include "touch.h" #include "pendsv.h" #elif defined TREZOR_UNIX -#include "modtrezormsg-unix.h" +#include "unix-msg-mock.h" #else #error Unsupported TREZOR port. Only STM32 and UNIX ports are supported. #endif +#define TOUCH_IFACE (255) +#define POLL_READ (0x0000) +#define POLL_WRITE (0x0100) + +/// def poll(ifaces: Iterable[int], list_ref: List, timeout_us: int) -> bool: +/// ''' +/// ''' +STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, mp_obj_t timeout_us) { + mp_obj_list_t *ret = MP_OBJ_TO_PTR(list_ref); + if (!MP_OBJ_IS_TYPE(list_ref, &mp_type_list) || ret->len < 2) { + mp_raise_TypeError("invalid list_ref"); + } + + const mp_uint_t timeout = mp_obj_get_int(timeout_us); + const mp_uint_t deadline = mp_hal_ticks_us() + timeout; + mp_obj_iter_buf_t iterbuf; + + for (;;) { + mp_obj_t iter = mp_getiter(ifaces, &iterbuf); + mp_obj_t item; + while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { + const mp_uint_t i = mp_obj_int_get_truncated(item); + const mp_uint_t iface = i & 0x00FF; + const mp_uint_t mode = i & 0xFF00; + + if (iface == TOUCH_IFACE) { + uint32_t evt = touch_read(); + if (evt) { + mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); + tuple->items[0] = MP_OBJ_NEW_SMALL_INT((evt & 0xFF0000) >> 16); // event type + tuple->items[1] = MP_OBJ_NEW_SMALL_INT((evt & 0xFF00) >> 8); // x position + tuple->items[2] = MP_OBJ_NEW_SMALL_INT((evt & 0xFF)); // y position + ret->items[0] = MP_OBJ_NEW_SMALL_INT(i); + ret->items[1] = MP_OBJ_FROM_PTR(tuple); + return mp_const_true; + } + } else + if (mode == POLL_READ) { + if (usb_hid_can_read(iface)) { + uint8_t buf[64]; + int l = usb_hid_read(iface, buf, sizeof(buf)); + if (l > 0) { + ret->items[0] = MP_OBJ_NEW_SMALL_INT(i); + ret->items[1] = mp_obj_new_str_of_type(&mp_type_bytes, buf, l); + return mp_const_true; + } + } + } else + if (mode == POLL_WRITE) { + if (usb_hid_can_write(iface)) { + ret->items[0] = MP_OBJ_NEW_SMALL_INT(i); + ret->items[1] = mp_const_none; + return mp_const_true; + } + } + } + + if (mp_hal_ticks_us() >= deadline) { + break; + } else { + MICROPY_EVENT_POLL_HOOK + } + } + + return mp_const_false; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_poll_obj, mod_trezorio_poll); + /// class HID: /// ''' /// USB HID interface configuration. @@ -45,7 +103,7 @@ typedef struct _mp_obj_HID_t { /// max_packet_len: int = 64) -> None: /// ''' /// ''' -STATIC mp_obj_t mod_trezormsg_HID_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t mod_trezorio_HID_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { STATIC const mp_arg_t allowed_args[] = { { MP_QSTR_iface_num, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, @@ -112,14 +170,26 @@ STATIC mp_obj_t mod_trezormsg_HID_make_new(const mp_obj_type_t *type, size_t n_a return MP_OBJ_FROM_PTR(o); } -STATIC const mp_rom_map_elem_t mod_trezormsg_HID_locals_dict_table[] = {}; -STATIC MP_DEFINE_CONST_DICT(mod_trezormsg_HID_locals_dict, mod_trezormsg_HID_locals_dict_table); +/// def iface_num(self) -> int: +/// ''' +/// Returns the configured number of this interface. +/// ''' +STATIC mp_obj_t mod_trezorio_HID_iface_num(mp_obj_t self) { + mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); + return MP_OBJ_NEW_SMALL_INT(o->info.iface_num); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_HID_iface_num_obj, mod_trezorio_HID_iface_num); -STATIC const mp_obj_type_t mod_trezormsg_HID_type = { +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) }, +}; +STATIC MP_DEFINE_CONST_DICT(mod_trezorio_HID_locals_dict, mod_trezorio_HID_locals_dict_table); + +STATIC const mp_obj_type_t mod_trezorio_HID_type = { { &mp_type_type }, .name = MP_QSTR_HID, - .make_new = mod_trezormsg_HID_make_new, - .locals_dict = (void*)&mod_trezormsg_HID_locals_dict, + .make_new = mod_trezorio_HID_make_new, + .locals_dict = (void*)&mod_trezorio_HID_locals_dict, }; /// class VCP: @@ -139,7 +209,7 @@ typedef struct _mp_obj_VCP_t { /// ep_cmd: int) -> None: /// ''' /// ''' -STATIC mp_obj_t mod_trezormsg_VCP_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t mod_trezorio_VCP_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { STATIC const mp_arg_t allowed_args[] = { { MP_QSTR_iface_num, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, @@ -198,14 +268,31 @@ STATIC mp_obj_t mod_trezormsg_VCP_make_new(const mp_obj_type_t *type, size_t n_a return MP_OBJ_FROM_PTR(o); } -STATIC const mp_rom_map_elem_t mod_trezormsg_VCP_locals_dict_table[] = {}; -STATIC MP_DEFINE_CONST_DICT(mod_trezormsg_VCP_locals_dict, mod_trezormsg_VCP_locals_dict_table); +/// def iface_num(self) -> int: +/// ''' +/// Returns the configured number of this interface. +/// ''' +STATIC mp_obj_t mod_trezorio_VCP_iface_num(mp_obj_t self) { + mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); + return MP_OBJ_NEW_SMALL_INT(o->info.iface_num); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_VCP_iface_num_obj, mod_trezorio_VCP_iface_num); -STATIC const mp_obj_type_t mod_trezormsg_VCP_type = { +STATIC const mp_rom_map_elem_t mod_trezorio_VCP_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_iface_num), MP_ROM_PTR(&mod_trezorio_HID_iface_num_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(mod_trezorio_VCP_locals_dict, mod_trezorio_VCP_locals_dict_table); + +STATIC const mp_obj_type_t mod_trezorio_VCP_type = { { &mp_type_type }, .name = MP_QSTR_VCP, - .make_new = mod_trezormsg_VCP_make_new, - .locals_dict = (void*)&mod_trezormsg_VCP_locals_dict, + .make_new = mod_trezorio_VCP_make_new, + .locals_dict = (void*)&mod_trezorio_VCP_locals_dict, +}; + +enum { + USB_CLOSED = 0, + USB_OPENED = 1, }; /// class USB: @@ -214,7 +301,9 @@ STATIC const mp_obj_type_t mod_trezormsg_VCP_type = { /// ''' typedef struct _mp_obj_USB_t { mp_obj_base_t base; + mp_obj_list_t ifaces; usb_dev_info_t info; + mp_int_t state; } mp_obj_USB_t; static const char *get_0str(mp_obj_t o, size_t min_len, size_t max_len) { @@ -235,24 +324,20 @@ static const char *get_0str(mp_obj_t o, size_t min_len, size_t max_len) { /// vendor_id: int, /// product_id: int, /// release_num: int, -/// manufacturer_str: str, -/// product_str: str, -/// serial_number_str: str, -/// configuration_str: str = '', -/// interface_str: str = '') -> None: +/// manufacturer: str, +/// product: str, +/// serial_number: str) -> None: /// ''' /// ''' -STATIC mp_obj_t mod_trezormsg_USB_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t mod_trezorio_USB_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { STATIC const mp_arg_t allowed_args[] = { - { MP_QSTR_vendor_id, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_product_id, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_release_num, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_manufacturer_str, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_product_str, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_serial_number_str, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_configuration_str, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} }, - { MP_QSTR_interface_str, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} }, + { MP_QSTR_vendor_id, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_product_id, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_release_num, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_manufacturer, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_product, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_serial_number, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, }; mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals); @@ -260,11 +345,9 @@ STATIC mp_obj_t mod_trezormsg_USB_make_new(const mp_obj_type_t *type, size_t n_a const mp_int_t vendor_id = vals[0].u_int; const mp_int_t product_id = vals[1].u_int; const mp_int_t release_num = vals[2].u_int; - const char *manufacturer_str = get_0str(vals[3].u_obj, 0, 32); - const char *product_str = get_0str(vals[4].u_obj, 0, 32); - const char *serial_number_str = get_0str(vals[5].u_obj, 0, 32); - const char *configuration_str = get_0str(vals[6].u_obj, 0, 32); - const char *interface_str = get_0str(vals[7].u_obj, 0, 32); + const char *manufacturer = get_0str(vals[3].u_obj, 0, 32); + const char *product = get_0str(vals[4].u_obj, 0, 32); + const char *serial_number = get_0str(vals[5].u_obj, 0, 32); if (vendor_id < 0 || vendor_id > 65535) { mp_raise_ValueError("vendor_id is invalid"); @@ -272,92 +355,66 @@ STATIC mp_obj_t mod_trezormsg_USB_make_new(const mp_obj_type_t *type, size_t n_a if (product_id < 0 || product_id > 65535) { mp_raise_ValueError("product_id is invalid"); } - if (manufacturer_str == NULL) { - mp_raise_ValueError("manufacturer_str is invalid"); + if (manufacturer == NULL) { + mp_raise_ValueError("manufacturer is invalid"); } - if (product_str == NULL) { - mp_raise_ValueError("product_str is invalid"); + if (product == NULL) { + mp_raise_ValueError("product is invalid"); } - if (serial_number_str == NULL) { - mp_raise_ValueError("serial_number_str is invalid"); - } - if (configuration_str == NULL) { - mp_raise_ValueError("configuration_str is invalid"); - } - if (interface_str == NULL) { - mp_raise_ValueError("interface_str is invalid"); + if (serial_number == NULL) { + mp_raise_ValueError("serial_number is invalid"); } mp_obj_USB_t *o = m_new_obj(mp_obj_USB_t); o->base.type = type; - o->info.vendor_id = (uint16_t)(vendor_id); - o->info.product_id = (uint16_t)(product_id); - o->info.release_num = (uint16_t)(release_num); - o->info.manufacturer_str = (const uint8_t *)(manufacturer_str); - o->info.product_str = (const uint8_t *)(product_str); - o->info.serial_number_str = (const uint8_t *)(serial_number_str); - o->info.configuration_str = (const uint8_t *)(configuration_str); - o->info.interface_str = (const uint8_t *)(interface_str); + o->state = USB_CLOSED; + + o->info.vendor_id = (uint16_t)(vendor_id); + o->info.product_id = (uint16_t)(product_id); + o->info.release_num = (uint16_t)(release_num); + o->info.manufacturer = (const uint8_t *)(manufacturer); + o->info.product = (const uint8_t *)(product); + o->info.serial_number = (const uint8_t *)(serial_number); + mp_obj_list_init(&o->ifaces, 0); return MP_OBJ_FROM_PTR(o); } -STATIC const mp_rom_map_elem_t mod_trezormsg_USB_locals_dict_table[] = {}; -STATIC MP_DEFINE_CONST_DICT(mod_trezormsg_USB_locals_dict, mod_trezormsg_USB_locals_dict_table); +/// def add(self, iface: Union[HID, VCP]) -> None: +/// ''' +/// Registers passed interface into the USB stack. +/// ''' +STATIC mp_obj_t mod_trezorio_USB_add(mp_obj_t self, mp_obj_t iface) { + mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); -STATIC const mp_obj_type_t mod_trezormsg_USB_type = { - { &mp_type_type }, - .name = MP_QSTR_USB, - .make_new = mod_trezormsg_USB_make_new, - .locals_dict = (void*)&mod_trezormsg_USB_locals_dict, -}; + if (o->state != USB_CLOSED) { + mp_raise_msg(&mp_type_RuntimeError, "already initialized"); + } + mp_obj_list_append(o->ifaces, iface); -/// class Msg: -/// ''' -/// Interface with USB and touch events. -/// ''' -typedef struct _mp_obj_Msg_t { - mp_obj_base_t base; - mp_obj_t usb_info; - mp_obj_t usb_ifaces; -} mp_obj_Msg_t; - -/// def __init__(self) -> None: -/// ''' -/// ''' -STATIC mp_obj_t mod_trezormsg_Msg_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 0, 0, false); - mp_obj_Msg_t *o = m_new_obj(mp_obj_Msg_t); - o->base.type = type; - o->usb_info = mp_const_none; - o->usb_ifaces = mp_const_none; - return MP_OBJ_FROM_PTR(o); + return mp_const_none; } +STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_USB_add_obj, mod_trezorio_USB_add); -/// def init_usb(self, usb_info: USB, usb_ifaces: List[Union[HID, VCP]]) -> None: +/// def start(self) -> None: /// ''' -/// Registers passed interfaces and initializes the USB stack. +/// Initializes the USB stack. /// ''' -STATIC mp_obj_t mod_trezormsg_Msg_init_usb(mp_obj_t self, mp_obj_t usb_info, mp_obj_t usb_ifaces) { +STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) { + mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); - mp_obj_Msg_t *o = MP_OBJ_TO_PTR(self); - if (o->usb_info != mp_const_none || o->usb_ifaces != mp_const_none) { + if (o->state != USB_CLOSED) { mp_raise_msg(&mp_type_RuntimeError, "already initialized"); } size_t iface_cnt; mp_obj_t *iface_objs; - mp_obj_get_array(usb_ifaces, &iface_cnt, &iface_objs); + mp_obj_get_array(o->ifaces, &iface_cnt, &iface_objs); // Initialize the USB stack - if (MP_OBJ_IS_TYPE(usb_info, &mod_trezormsg_USB_type)) { - mp_obj_USB_t *usb = MP_OBJ_TO_PTR(usb_info); - if (usb_init(&usb->info) != 0) { - mp_raise_msg(&mp_type_RuntimeError, "failed to initialize USB"); - } - } else { - mp_raise_TypeError("expected USB type"); + if (usb_init(&o->info) != 0) { + mp_raise_msg(&mp_type_RuntimeError, "failed to initialize USB"); } int vcp_iface_num = -1; @@ -366,13 +423,13 @@ STATIC mp_obj_t mod_trezormsg_Msg_init_usb(mp_obj_t self, mp_obj_t usb_info, mp_ for (size_t i = 0; i < iface_cnt; i++) { mp_obj_t iface = iface_objs[i]; - if (MP_OBJ_IS_TYPE(iface, &mod_trezormsg_HID_type)) { + if (MP_OBJ_IS_TYPE(iface, &mod_trezorio_HID_type)) { mp_obj_HID_t *hid = MP_OBJ_TO_PTR(iface); if (usb_hid_add(&hid->info) != 0) { usb_deinit(); mp_raise_msg(&mp_type_RuntimeError, "failed to add HID interface"); } - } else if (MP_OBJ_IS_TYPE(iface, &mod_trezormsg_VCP_type)) { + } else if (MP_OBJ_IS_TYPE(iface, &mod_trezorio_VCP_type)) { mp_obj_VCP_t *vcp = MP_OBJ_TO_PTR(iface); if (usb_vcp_add(&vcp->info) != 0) { usb_deinit(); @@ -386,166 +443,86 @@ STATIC mp_obj_t mod_trezormsg_Msg_init_usb(mp_obj_t self, mp_obj_t usb_info, mp_ } // Start the USB stack - if (usb_start() != 0) { + if (USB_open() != 0) { usb_deinit(); mp_raise_msg(&mp_type_RuntimeError, "failed to start USB"); } + o->state = USB_OPENED; // If we found any VCP interfaces, use the last one for stdio, // otherwise disable the stdio support mp_hal_set_vcp_iface(vcp_iface_num); - o->usb_info = usb_info; - o->usb_ifaces = usb_ifaces; - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezormsg_Msg_init_usb_obj, mod_trezormsg_Msg_init_usb); +STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_USB_open_obj, mod_trezorio_USB_open); -/// def deinit_usb(self) -> None: +/// def stop(self) -> None: /// ''' /// Cleans up the USB stack. /// ''' -STATIC mp_obj_t mod_trezormsg_Msg_deinit_usb(mp_obj_t self) { +STATIC mp_obj_t mod_trezorio_USB_close_usb(mp_obj_t self) { + mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); - usb_stop(); + if (o->state != USB_OPENED) { + mp_raise_msg(&mp_type_RuntimeError, "not initialized"); + } + USB_close(); usb_deinit(); - - mp_obj_Msg_t *o = MP_OBJ_TO_PTR(self); - o->usb_info = mp_const_none; - o->usb_ifaces = mp_const_none; + mp_obj_list_set_len(o->ifaces, 0); + mp_seq_clear(o->ifaces.items, 0, o->ifaces.alloc, sizeof(*o->ifaces.items)); + o->info.vendor_id = 0; + o->info.product_id = 0; + o->info.release_num = 0; + o->info.manufacturer = NULL; + o->info.product = NULL; + o->info.serial_number = NULL; + o->state = USB_CLOSED; return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezormsg_Msg_deinit_usb_obj, mod_trezormsg_Msg_deinit_usb); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_USB_close_usb_obj, mod_trezorio_USB_close_usb); -/// def send(self, iface: int, message: bytes) -> int: +/// def write(self, iface: int, msg: 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) { - // mp_obj_Msg_t *o = MP_OBJ_TO_PTR(self); +STATIC mp_obj_t mod_trezorio_USB_write(mp_obj_t self, mp_obj_t iface, mp_obj_t msg) { + mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); + if (o->state != USB_OPENED) { + mp_raise_msg(&mp_type_RuntimeError, "not initialized"); + } + mp_buffer_info_t buf; + mp_get_buffer_raise(msg, &buf, MP_BUFFER_READ); uint8_t i = mp_obj_get_int(iface); - mp_buffer_info_t msg; - mp_get_buffer_raise(message, &msg, MP_BUFFER_READ); - ssize_t r = usb_hid_write(i, msg.buf, msg.len); + ssize_t r = usb_hid_write(i, buf.buf, buf.len); return MP_OBJ_NEW_SMALL_INT(r); } -STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezormsg_Msg_send_obj, mod_trezormsg_Msg_send); +STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_USB_write_obj, mod_trezorio_USB_write); -STATIC mp_obj_t mod_trezormsg_Msg___del__(mp_obj_t self) { - mp_obj_Msg_t *o = MP_OBJ_TO_PTR(self); - if (o->usb_info != mp_const_none || o->usb_ifaces != mp_const_none) { - usb_stop(); +STATIC mp_obj_t mod_trezorio_USB___del__(mp_obj_t self) { + mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); + if (o->state != USB_CLOSED) { + usb_close(); usb_deinit(); + o->state = USB_CLOSED; } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezormsg_Msg___del___obj, mod_trezormsg_Msg___del__); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_USB___del___obj, mod_trezorio_USB___del__); -STATIC const mp_rom_map_elem_t mod_trezormsg_Msg_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_trezormsg_Msg___del___obj) }, - { MP_ROM_QSTR(MP_QSTR_init_usb), MP_ROM_PTR(&mod_trezormsg_Msg_init_usb_obj) }, - { MP_ROM_QSTR(MP_QSTR_deinit_usb), MP_ROM_PTR(&mod_trezormsg_Msg_deinit_usb_obj) }, - { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&mod_trezormsg_Msg_send_obj) }, +STATIC const mp_rom_map_elem_t mod_trezorio_USB_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&mod_trezorio_USB_add_obj) }, + { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mod_trezorio_USB_open_obj) }, + { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mod_trezorio_USB_close_obj) }, + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mod_trezorio_USB_write_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_trezorio_USB___del___obj) }, }; -STATIC MP_DEFINE_CONST_DICT(mod_trezormsg_Msg_locals_dict, mod_trezormsg_Msg_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(mod_trezorio_USB_locals_dict, mod_trezorio_USB_locals_dict_table); -STATIC const mp_obj_type_t mod_trezormsg_Msg_type = { +STATIC const mp_obj_type_t mod_trezorio_USB_type = { { &mp_type_type }, - .name = MP_QSTR_Msg, - .make_new = mod_trezormsg_Msg_make_new, - .locals_dict = (void*)&mod_trezormsg_Msg_locals_dict, -}; - -#define TOUCH_IFACE (255) -#define POLL_READ (0x0000) -#define POLL_WRITE (0x0100) - -/// def poll(ifaces: Iterable[int], list_ref: List, timeout_us: int) -> bool: -/// ''' -/// ''' -STATIC mp_obj_t mod_trezormsg_poll(mp_obj_t ifaces, mp_obj_t list_ref, mp_obj_t timeout_us) { - mp_obj_list_t *ret = MP_OBJ_TO_PTR(list_ref); - if (!MP_OBJ_IS_TYPE(list_ref, &mp_type_list) || ret->len < 2) { - mp_raise_TypeError("invalid list_ref"); - } - - const mp_uint_t timeout = mp_obj_get_int(timeout_us); - const mp_uint_t deadline = mp_hal_ticks_us() + timeout; - mp_obj_iter_buf_t iterbuf; - - for (;;) { - mp_obj_t iter = mp_getiter(ifaces, &iterbuf); - mp_obj_t item; - while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { - const mp_uint_t i = mp_obj_int_get_truncated(item); - const mp_uint_t iface = i & 0x00FF; - const mp_uint_t mode = i & 0xFF00; - - if (iface == TOUCH_IFACE) { - uint32_t evt = touch_read(); - if (evt) { - mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); - tuple->items[0] = MP_OBJ_NEW_SMALL_INT((evt & 0xFF0000) >> 16); // event type - tuple->items[1] = MP_OBJ_NEW_SMALL_INT((evt & 0xFF00) >> 8); // x position - tuple->items[2] = MP_OBJ_NEW_SMALL_INT((evt & 0xFF)); // y position - ret->items[0] = MP_OBJ_NEW_SMALL_INT(i); - ret->items[1] = MP_OBJ_FROM_PTR(tuple); - return mp_const_true; - } - } else - if (mode == POLL_READ) { - if (usb_hid_can_read(iface)) { - uint8_t buf[64]; - int l = usb_hid_read(iface, buf, sizeof(buf)); - if (l > 0) { - ret->items[0] = MP_OBJ_NEW_SMALL_INT(i); - ret->items[1] = mp_obj_new_str_of_type(&mp_type_bytes, buf, l); - return mp_const_true; - } - } - } else - if (mode == POLL_WRITE) { - if (usb_hid_can_write(iface)) { - ret->items[0] = MP_OBJ_NEW_SMALL_INT(i); - ret->items[1] = mp_const_none; - return mp_const_true; - } - } - } - - if (mp_hal_ticks_us() >= deadline) { - break; - } else { - MICROPY_EVENT_POLL_HOOK - } - } - - return mp_const_false; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezormsg_poll_obj, mod_trezormsg_poll); - -STATIC const mp_rom_map_elem_t mp_module_trezormsg_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezormsg) }, - { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&mod_trezormsg_USB_type) }, - { MP_ROM_QSTR(MP_QSTR_HID), MP_ROM_PTR(&mod_trezormsg_HID_type) }, - { MP_ROM_QSTR(MP_QSTR_VCP), MP_ROM_PTR(&mod_trezormsg_VCP_type) }, - { MP_ROM_QSTR(MP_QSTR_Msg), MP_ROM_PTR(&mod_trezormsg_Msg_type) }, - - { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mod_trezormsg_poll_obj) }, - { MP_ROM_QSTR(MP_QSTR_TOUCH), MP_OBJ_NEW_SMALL_INT(TOUCH_IFACE) }, - { MP_ROM_QSTR(MP_QSTR_TOUCH_START), MP_OBJ_NEW_SMALL_INT((TOUCH_START & 0xFF0000) >> 16) }, - { MP_ROM_QSTR(MP_QSTR_TOUCH_MOVE), MP_OBJ_NEW_SMALL_INT((TOUCH_MOVE & 0xFF0000) >> 16) }, - { MP_ROM_QSTR(MP_QSTR_TOUCH_END), MP_OBJ_NEW_SMALL_INT((TOUCH_END & 0xFF0000) >> 16) }, - { MP_ROM_QSTR(MP_QSTR_POLL_READ), MP_OBJ_NEW_SMALL_INT(POLL_READ) }, - { MP_ROM_QSTR(MP_QSTR_POLL_WRITE), MP_OBJ_NEW_SMALL_INT(POLL_WRITE) }, -}; -STATIC MP_DEFINE_CONST_DICT(mp_module_trezormsg_globals, mp_module_trezormsg_globals_table); - -const mp_obj_module_t mp_module_trezormsg = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&mp_module_trezormsg_globals, -}; - -#endif // MICROPY_PY_TREZORMSG + .name = MP_QSTR_USB, + .make_new = mod_trezorio_USB_make_new, + .locals_dict = (void*)&mod_trezorio_USB_locals_dict, +}; \ No newline at end of file diff --git a/embed/extmod/modtrezorio/modtrezorio.c b/embed/extmod/modtrezorio/modtrezorio.c index a2a5c2e99a..71a85fab88 100644 --- a/embed/extmod/modtrezorio/modtrezorio.c +++ b/embed/extmod/modtrezorio/modtrezorio.c @@ -6,14 +6,29 @@ */ #include "py/runtime.h" +#include "py/mphal.h" +#include "py/objstr.h" #if MICROPY_PY_TREZORIO #include "modtrezorio-sdcard.h" +#include "modtrezorio-msg.h" STATIC const mp_rom_map_elem_t mp_module_trezorio_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorio) }, + { MP_ROM_QSTR(MP_QSTR_SDCard), MP_ROM_PTR(&mod_trezorio_SDCard_type) }, + + { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&mod_trezorio_USB_type) }, + { MP_ROM_QSTR(MP_QSTR_HID), MP_ROM_PTR(&mod_trezorio_HID_type) }, + { MP_ROM_QSTR(MP_QSTR_VCP), MP_ROM_PTR(&mod_trezorio_VCP_type) }, + { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mod_trezorio_poll_obj) }, + { MP_ROM_QSTR(MP_QSTR_TOUCH), MP_OBJ_NEW_SMALL_INT(TOUCH_IFACE) }, + { MP_ROM_QSTR(MP_QSTR_TOUCH_START), MP_OBJ_NEW_SMALL_INT((TOUCH_START & 0xFF0000) >> 16) }, + { MP_ROM_QSTR(MP_QSTR_TOUCH_MOVE), MP_OBJ_NEW_SMALL_INT((TOUCH_MOVE & 0xFF0000) >> 16) }, + { MP_ROM_QSTR(MP_QSTR_TOUCH_END), MP_OBJ_NEW_SMALL_INT((TOUCH_END & 0xFF0000) >> 16) }, + { MP_ROM_QSTR(MP_QSTR_POLL_READ), MP_OBJ_NEW_SMALL_INT(POLL_READ) }, + { MP_ROM_QSTR(MP_QSTR_POLL_WRITE), MP_OBJ_NEW_SMALL_INT(POLL_WRITE) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_trezorio_globals, mp_module_trezorio_globals_table); diff --git a/embed/extmod/modtrezormsg/modtrezormsg-unix.h b/embed/extmod/modtrezorio/unix-msg-mock.h similarity index 99% rename from embed/extmod/modtrezormsg/modtrezormsg-unix.h rename to embed/extmod/modtrezorio/unix-msg-mock.h index f00499ae28..0ccb5c3c58 100644 --- a/embed/extmod/modtrezormsg/modtrezormsg-unix.h +++ b/embed/extmod/modtrezorio/unix-msg-mock.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "../../trezorhal/usb.h" #include "../../trezorhal/touch.h" diff --git a/embed/extmod/modtrezorio/unix-sdcard-mock.h b/embed/extmod/modtrezorio/unix-sdcard-mock.h index 21e3040737..debbbdddc9 100644 --- a/embed/extmod/modtrezorio/unix-sdcard-mock.h +++ b/embed/extmod/modtrezorio/unix-sdcard-mock.h @@ -1,3 +1,10 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under TREZOR License + * see LICENSE file for details + */ + #include "../../trezorhal/sdcard.h" #define SD_ERROR 41U diff --git a/embed/extmod/modtrezormsg/modtrezormsg-stm32.h b/embed/extmod/modtrezormsg/modtrezormsg-stm32.h deleted file mode 100644 index 4925a03fc6..0000000000 --- a/embed/extmod/modtrezormsg/modtrezormsg-stm32.h +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Copyright (c) Pavol Rusnak, SatoshiLabs - * - * Licensed under TREZOR License - * see LICENSE file for details - */ - -#include "usb.h" -#include "touch.h" diff --git a/embed/firmware/mpconfigport.h b/embed/firmware/mpconfigport.h index dea590ef45..2ac5c4dd0c 100644 --- a/embed/firmware/mpconfigport.h +++ b/embed/firmware/mpconfigport.h @@ -117,7 +117,6 @@ #define MICROPY_PY_TREZORCONFIG (1) #define MICROPY_PY_TREZORCRYPTO (1) #define MICROPY_PY_TREZORIO (1) -#define MICROPY_PY_TREZORMSG (1) #define MICROPY_PY_TREZORUI (1) #define MICROPY_PY_TREZORUTILS (1) @@ -130,7 +129,6 @@ extern const struct _mp_obj_module_t mp_module_utime; extern const struct _mp_obj_module_t mp_module_trezorconfig; extern const struct _mp_obj_module_t mp_module_trezorcrypto; extern const struct _mp_obj_module_t mp_module_trezorio; -extern const struct _mp_obj_module_t mp_module_trezormsg; extern const struct _mp_obj_module_t mp_module_trezorui; extern const struct _mp_obj_module_t mp_module_trezorutils; @@ -139,7 +137,6 @@ extern const struct _mp_obj_module_t mp_module_trezorutils; { MP_OBJ_NEW_QSTR(MP_QSTR_trezorconfig), (mp_obj_t)&mp_module_trezorconfig }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_trezorcrypto), (mp_obj_t)&mp_module_trezorcrypto }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_trezorio), (mp_obj_t)&mp_module_trezorio }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_trezormsg), (mp_obj_t)&mp_module_trezormsg }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_trezorui), (mp_obj_t)&mp_module_trezorui }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_trezorutils), (mp_obj_t)&mp_module_trezorutils }, diff --git a/embed/trezorhal/touch.c b/embed/trezorhal/touch.c index e40a2ef5cc..6b8fb90fee 100644 --- a/embed/trezorhal/touch.c +++ b/embed/trezorhal/touch.c @@ -1,3 +1,10 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under TREZOR License + * see LICENSE file for details + */ + #include STM32_HAL_H #include diff --git a/embed/trezorhal/touch.h b/embed/trezorhal/touch.h index 406efd7812..2184031bec 100644 --- a/embed/trezorhal/touch.h +++ b/embed/trezorhal/touch.h @@ -1,3 +1,10 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under TREZOR License + * see LICENSE file for details + */ + #ifndef __TREZORHAL_TOUCH_H__ #define __TREZORHAL_TOUCH_H__ diff --git a/embed/trezorhal/usb.c b/embed/trezorhal/usb.c index 6eed473ffd..60795f100e 100644 --- a/embed/trezorhal/usb.c +++ b/embed/trezorhal/usb.c @@ -70,16 +70,12 @@ int usb_init(const usb_dev_info_t *dev_info) { // String table if ((0 != check_desc_str(dev_info->manufacturer_str)) || (0 != check_desc_str(dev_info->product_str)) || - (0 != check_desc_str(dev_info->serial_number_str)) || - (0 != check_desc_str(dev_info->configuration_str)) || - (0 != check_desc_str(dev_info->interface_str))) { + (0 != check_desc_str(dev_info->serial_number_str))) { return 1; // Invalid descriptor string } usb_str_table.manufacturer_str = dev_info->manufacturer_str; usb_str_table.product_str = dev_info->product_str; usb_str_table.serial_str = dev_info->serial_number_str; - usb_str_table.config_str = dev_info->configuration_str; - usb_str_table.interface_str = dev_info->interface_str; // Configuration descriptor usb_config_desc->bLength = sizeof(usb_config_descriptor_t); @@ -204,12 +200,12 @@ static uint8_t *usb_get_serial_str_descriptor(USBD_SpeedTypeDef speed, uint16_t } static uint8_t *usb_get_config_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) { - USBD_GetString(UNCONST(usb_str_table.config_str), usb_str_buf, length); + *length = 0; return usb_str_buf; } static uint8_t *usb_get_interface_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) { - USBD_GetString(UNCONST(usb_str_table.interface_str), usb_str_buf, length); + *length = 0; return usb_str_buf; } diff --git a/embed/trezorhal/usb.h b/embed/trezorhal/usb.h index 0cf5867115..a4b3e944a4 100644 --- a/embed/trezorhal/usb.h +++ b/embed/trezorhal/usb.h @@ -88,8 +88,6 @@ typedef struct { const uint8_t *manufacturer_str; const uint8_t *product_str; const uint8_t *serial_str; - const uint8_t *config_str; - const uint8_t *interface_str; } usb_dev_string_table_t; typedef struct { @@ -99,8 +97,6 @@ typedef struct { const uint8_t *manufacturer_str; const uint8_t *product_str; const uint8_t *serial_number_str; - const uint8_t *configuration_str; - const uint8_t *interface_str; } usb_dev_info_t; typedef enum { diff --git a/embed/unix/mpconfigport.h b/embed/unix/mpconfigport.h index f0c4dd134b..9272860db6 100644 --- a/embed/unix/mpconfigport.h +++ b/embed/unix/mpconfigport.h @@ -182,7 +182,6 @@ extern const struct _mp_obj_module_t mp_module_jni; extern const struct _mp_obj_module_t mp_module_trezorconfig; extern const struct _mp_obj_module_t mp_module_trezorcrypto; extern const struct _mp_obj_module_t mp_module_trezorio; -extern const struct _mp_obj_module_t mp_module_trezormsg; extern const struct _mp_obj_module_t mp_module_trezorui; extern const struct _mp_obj_module_t mp_module_trezorutils; @@ -237,11 +236,6 @@ extern const struct _mp_obj_module_t mp_module_trezorutils; #else #define MICROPY_PY_TREZORIO_DEF #endif -#if MICROPY_PY_TREZORMSG -#define MICROPY_PY_TREZORMSG_DEF { MP_ROM_QSTR(MP_QSTR_trezormsg), MP_ROM_PTR(&mp_module_trezormsg) }, -#else -#define MICROPY_PY_TREZORMSG_DEF -#endif #if MICROPY_PY_TREZORUI #define MICROPY_PY_TREZORUI_DEF { MP_ROM_QSTR(MP_QSTR_trezorui), MP_ROM_PTR(&mp_module_trezorui) }, #else @@ -265,7 +259,6 @@ extern const struct _mp_obj_module_t mp_module_trezorutils; MICROPY_PY_TREZORCONFIG_DEF \ MICROPY_PY_TREZORCRYPTO_DEF \ MICROPY_PY_TREZORIO_DEF \ - MICROPY_PY_TREZORMSG_DEF \ MICROPY_PY_TREZORUI_DEF \ MICROPY_PY_TREZORUTILS_DEF \