diff --git a/emu.sh b/emu.sh index 3bf3c93c97..fe2d4361fc 100755 --- a/emu.sh +++ b/emu.sh @@ -1,10 +1,8 @@ #!/bin/bash cd `dirname $0`/src -rm -f ../pipe.* if [ "$1" == -d ]; then shift gdb --args ../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py else ../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py fi -rm -f ../pipe.* diff --git a/extmod/modtrezormsg/modtrezormsg-stmhal.h b/extmod/modtrezormsg/modtrezormsg-stmhal.h new file mode 100644 index 0000000000..b0acdbd58c --- /dev/null +++ b/extmod/modtrezormsg/modtrezormsg-stmhal.h @@ -0,0 +1,18 @@ +void msg_init(void) +{ +} + +const uint8_t *msg_recv(void) +{ + return 0; +} + +int msg_send(uint8_t *buf, size_t len) +{ + return -1; +} + +uint32_t msg_poll_ui_event(void) +{ + return 0; +} diff --git a/extmod/modtrezormsg/modtrezormsg-unix.h b/extmod/modtrezormsg/modtrezormsg-unix.h new file mode 100644 index 0000000000..7080e150e7 --- /dev/null +++ b/extmod/modtrezormsg/modtrezormsg-unix.h @@ -0,0 +1,54 @@ +#include +#include +#include + +#define TREZOR_PORT 21324 + +static int s; +static struct sockaddr_in si_me, si_other; +static socklen_t slen = 0; + +void msg_init(void) +{ + s = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP); + assert(s != -1); + + si_me.sin_family = AF_INET; + si_me.sin_port = htons(TREZOR_PORT); + si_me.sin_addr.s_addr = htonl(INADDR_ANY); + + int b; + b = bind(s, (struct sockaddr*)&si_me, sizeof(si_me)); + assert(b != -1); +} + +#define RECV_BUFLEN 64 + +const uint8_t *msg_recv(void) +{ + static uint8_t buf[RECV_BUFLEN]; + struct sockaddr_in si; + socklen_t sl; + memset(buf, 0, sizeof(buf)); + int len = recvfrom(s, buf, RECV_BUFLEN, MSG_DONTWAIT, (struct sockaddr *)&si, &sl); + if (len < 0) { + return 0; + } + si_other = si; + slen = sl; + return buf; +} + +int msg_send(uint8_t *buf, size_t len) +{ + int r = -1; + if (slen) { + r = sendto(s, buf, len, MSG_DONTWAIT, (const struct sockaddr *)&si_other, slen); + } + return r; +} + +// from modtrezorui: +uint32_t trezorui_poll_sdl_event(void); + +#define msg_poll_ui_event trezorui_poll_sdl_event diff --git a/extmod/modtrezormsg/modtrezormsg.c b/extmod/modtrezormsg/modtrezormsg.c index 7f17f17171..a22a8c2e15 100644 --- a/extmod/modtrezormsg/modtrezormsg.c +++ b/extmod/modtrezormsg/modtrezormsg.c @@ -12,20 +12,17 @@ #include "py/nlr.h" #include "py/runtime.h" #include "py/binary.h" +#include "py/mphal.h" #if MICROPY_PY_TREZORMSG -// io callbacks - -mp_obj_t msg_receive_callback = mp_const_none; - -/* -static void msg_receive(mp_obj_t message) { - if (touch_start_callback != mp_const_none) { - mp_call_function_1(msg_receive_callback, message); - } -} -*/ +#if defined STM32_HAL_H +#include "modtrezormsg-stmhal.h" +#elif defined UNIX +#include "modtrezormsg-unix.h" +#else +#error Unsupported port. Only STMHAL and UNIX ports are supported. +#endif // class Msg(object): typedef struct _mp_obj_Msg_t { @@ -35,29 +32,50 @@ typedef struct _mp_obj_Msg_t { // def Msg.__init__(self) 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); + msg_init(); mp_obj_Msg_t *o = m_new_obj(mp_obj_Msg_t); o->base.type = type; return MP_OBJ_FROM_PTR(o); } -// def Msg.receive(self, callback) -> None -STATIC mp_obj_t mod_TrezorMsg_Msg_receive(mp_obj_t self, mp_obj_t callback) { - msg_receive_callback = callback; - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorMsg_Msg_receive_obj, mod_TrezorMsg_Msg_receive); - -// def Msg.send(self, message) -> None +// def Msg.send(self, message) -> int STATIC mp_obj_t mod_TrezorMsg_Msg_send(mp_obj_t self, mp_obj_t message) { - // TODO - return mp_const_none; + mp_buffer_info_t buf; + mp_get_buffer_raise(message, &buf, MP_BUFFER_READ); + int r = msg_send(buf.buf, buf.len); + return MP_OBJ_NEW_SMALL_INT(r); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorMsg_Msg_send_obj, mod_TrezorMsg_Msg_send); +// def Msg.select(self, timeout_ms: int) -> None/tuple/bytes +STATIC mp_obj_t mod_TrezorMsg_Msg_select(mp_obj_t self, mp_obj_t timeout_ms) { + int to = mp_obj_get_int(timeout_ms); + while (--to >= 0) { + uint32_t e = msg_poll_ui_event(); + if (e) { + mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); + tuple->items[0] = MP_OBJ_NEW_SMALL_INT((e & 0xFF0000) >> 16); + tuple->items[1] = MP_OBJ_NEW_SMALL_INT((e & 0xFF00) >> 8); + tuple->items[2] = MP_OBJ_NEW_SMALL_INT((e & 0xFF)); + return MP_OBJ_FROM_PTR(tuple); + } + const uint8_t *m = msg_recv(); + if (m) { + vstr_t vstr; + vstr_init_len(&vstr, 64); + memcpy(vstr.buf, m, 64); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + } + mp_hal_delay_ms(1); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorMsg_Msg_select_obj, mod_TrezorMsg_Msg_select); + // Msg stuff STATIC const mp_rom_map_elem_t mod_TrezorMsg_Msg_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_receive), MP_ROM_PTR(&mod_TrezorMsg_Msg_receive_obj) }, + { MP_ROM_QSTR(MP_QSTR_select), MP_ROM_PTR(&mod_TrezorMsg_Msg_select_obj) }, { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&mod_TrezorMsg_Msg_send_obj) }, }; STATIC MP_DEFINE_CONST_DICT(mod_TrezorMsg_Msg_locals_dict, mod_TrezorMsg_Msg_locals_dict_table); diff --git a/extmod/modtrezorui/modtrezorui-unix.h b/extmod/modtrezorui/modtrezorui-unix.h index b0d2b0ae37..73e6fcaeff 100644 --- a/extmod/modtrezorui/modtrezorui-unix.h +++ b/extmod/modtrezorui/modtrezorui-unix.h @@ -34,11 +34,11 @@ static void DATAfunc(uint8_t x) { } } -uint32_t trezorui_poll_sdl_event(uint32_t timeout_us) +uint32_t trezorui_poll_sdl_event(void) { SDL_Event event; int x, y; - if (SDL_WaitEventTimeout(&event, timeout_us / 1000) > 0) { + if (SDL_PollEvent(&event) > 0) { switch (event.type) { case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEMOTION: diff --git a/extmod/modtrezorutils/modtrezorutils.c b/extmod/modtrezorutils/modtrezorutils.c index b3025a6fe6..938240a7d5 100644 --- a/extmod/modtrezorutils/modtrezorutils.c +++ b/extmod/modtrezorutils/modtrezorutils.c @@ -42,29 +42,10 @@ STATIC mp_obj_t mod_TrezorUtils_Utils_memaccess(mp_obj_t self, mp_obj_t address, } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorUtils_Utils_memaccess_obj, mod_TrezorUtils_Utils_memaccess); -// from modtrezorui -uint32_t trezorui_poll_sdl_event(uint32_t timeout_us); - -// def Utils.select(self, timeout_us: int) -> None/tuple -STATIC mp_obj_t mod_TrezorUtils_Utils_select(mp_obj_t self, mp_obj_t timeout_us) { - uint32_t to = mp_obj_get_int(timeout_us); - uint32_t e = trezorui_poll_sdl_event(to); - if (e) { - mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); - tuple->items[0] = MP_OBJ_NEW_SMALL_INT((e & 0xFF0000) >> 16); - tuple->items[1] = MP_OBJ_NEW_SMALL_INT((e & 0xFF00) >> 8); - tuple->items[2] = MP_OBJ_NEW_SMALL_INT((e & 0xFF)); - return MP_OBJ_FROM_PTR(tuple); - } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorUtils_Utils_select_obj, mod_TrezorUtils_Utils_select); - // Utils stuff STATIC const mp_rom_map_elem_t mod_TrezorUtils_Utils_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_memaccess), MP_ROM_PTR(&mod_TrezorUtils_Utils_memaccess_obj) }, - { MP_ROM_QSTR(MP_QSTR_select), MP_ROM_PTR(&mod_TrezorUtils_Utils_select_obj) }, }; STATIC MP_DEFINE_CONST_DICT(mod_TrezorUtils_Utils_locals_dict, mod_TrezorUtils_Utils_locals_dict_table); diff --git a/src/playground/__init__.py b/src/playground/__init__.py index 2451340700..b901a920a2 100644 --- a/src/playground/__init__.py +++ b/src/playground/__init__.py @@ -1,11 +1,6 @@ # import time import sys sys.path.append('lib') - -if sys.platform == 'linux': - # Packages used only on linux platform (named pipes, ...) - sys.path.append('lib_linux') - import utime import math import gc diff --git a/src/trezor/msg.py b/src/trezor/msg.py index 7513c7a305..e57b6172f4 100644 --- a/src/trezor/msg.py +++ b/src/trezor/msg.py @@ -1,28 +1,9 @@ -import sys +from TrezorMsg import Msg -if sys.platform == 'linux': - import transport_pipe as pipe +_msg = Msg() - def send(msg): - return pipe.write(msg) - - def read(): - return pipe.read() +def select(timeout_ms): + return _msg.select(timeout_ms) - def set_notify(_on_read): - return pipe.set_notify(_on_read) - - pipe.init('../pipe') - -else: - from TrezorMsg import Msg - - def send(msg): - return Msg.send(msg) - - def read(): - raise NotImplementedError - return Msg.receive() - - def set_notify(_on_read): - raise NotImplementedError +def send(msg): + return _msg.send(msg) diff --git a/src/trezor/ui.py b/src/trezor/ui.py index 4b648323ef..88e3faf748 100644 --- a/src/trezor/ui.py +++ b/src/trezor/ui.py @@ -1,12 +1,11 @@ import math import utime -from TrezorUi import Display, Touch +from TrezorUi import Display from . import loop display = Display() -touch = Touch() def rgbcolor(r: int, g: int, b: int) -> int: return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3) diff --git a/src/trezor/utils.py b/src/trezor/utils.py index bfc2da7a4a..1775ae17a6 100644 --- a/src/trezor/utils.py +++ b/src/trezor/utils.py @@ -8,9 +8,6 @@ type_gen = type((lambda: (yield))()) def memaccess(address, length): return _utils.memaccess(address, length) -def select(timeout_us): - return _utils.select(timeout_us) - def unimport_func(func): def inner(*args, **kwargs): mods = set(sys.modules)