1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 14:28:07 +00:00

new message handling using msg.select

This commit is contained in:
Pavol Rusnak 2016-04-29 03:15:18 +02:00
parent 338be18601
commit 7559207a62
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
10 changed files with 121 additions and 80 deletions

2
emu.sh
View File

@ -1,10 +1,8 @@
#!/bin/bash #!/bin/bash
cd `dirname $0`/src cd `dirname $0`/src
rm -f ../pipe.*
if [ "$1" == -d ]; then if [ "$1" == -d ]; then
shift shift
gdb --args ../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py gdb --args ../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py
else else
../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py ../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py
fi fi
rm -f ../pipe.*

View File

@ -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;
}

View File

@ -0,0 +1,54 @@
#include <arpa/inet.h>
#include <sys/socket.h>
#include <assert.h>
#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

View File

@ -12,20 +12,17 @@
#include "py/nlr.h" #include "py/nlr.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "py/binary.h" #include "py/binary.h"
#include "py/mphal.h"
#if MICROPY_PY_TREZORMSG #if MICROPY_PY_TREZORMSG
// io callbacks #if defined STM32_HAL_H
#include "modtrezormsg-stmhal.h"
mp_obj_t msg_receive_callback = mp_const_none; #elif defined UNIX
#include "modtrezormsg-unix.h"
/* #else
static void msg_receive(mp_obj_t message) { #error Unsupported port. Only STMHAL and UNIX ports are supported.
if (touch_start_callback != mp_const_none) { #endif
mp_call_function_1(msg_receive_callback, message);
}
}
*/
// class Msg(object): // class Msg(object):
typedef struct _mp_obj_Msg_t { typedef struct _mp_obj_Msg_t {
@ -35,29 +32,50 @@ typedef struct _mp_obj_Msg_t {
// def Msg.__init__(self) // 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) { 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_arg_check_num(n_args, n_kw, 0, 0, false);
msg_init();
mp_obj_Msg_t *o = m_new_obj(mp_obj_Msg_t); mp_obj_Msg_t *o = m_new_obj(mp_obj_Msg_t);
o->base.type = type; o->base.type = type;
return MP_OBJ_FROM_PTR(o); return MP_OBJ_FROM_PTR(o);
} }
// def Msg.receive(self, callback) -> None // def Msg.send(self, message) -> int
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
STATIC mp_obj_t mod_TrezorMsg_Msg_send(mp_obj_t self, mp_obj_t message) { STATIC mp_obj_t mod_TrezorMsg_Msg_send(mp_obj_t self, mp_obj_t message) {
// TODO mp_buffer_info_t buf;
return mp_const_none; 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); 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 // Msg stuff
STATIC const mp_rom_map_elem_t mod_TrezorMsg_Msg_locals_dict_table[] = { 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) }, { 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); STATIC MP_DEFINE_CONST_DICT(mod_TrezorMsg_Msg_locals_dict, mod_TrezorMsg_Msg_locals_dict_table);

View File

@ -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; SDL_Event event;
int x, y; int x, y;
if (SDL_WaitEventTimeout(&event, timeout_us / 1000) > 0) { if (SDL_PollEvent(&event) > 0) {
switch (event.type) { switch (event.type) {
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:

View File

@ -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); 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 // Utils stuff
STATIC const mp_rom_map_elem_t mod_TrezorUtils_Utils_locals_dict_table[] = { 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_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); STATIC MP_DEFINE_CONST_DICT(mod_TrezorUtils_Utils_locals_dict, mod_TrezorUtils_Utils_locals_dict_table);

View File

@ -1,11 +1,6 @@
# import time # import time
import sys import sys
sys.path.append('lib') sys.path.append('lib')
if sys.platform == 'linux':
# Packages used only on linux platform (named pipes, ...)
sys.path.append('lib_linux')
import utime import utime
import math import math
import gc import gc

View File

@ -1,28 +1,9 @@
import sys from TrezorMsg import Msg
if sys.platform == 'linux': _msg = Msg()
import transport_pipe as pipe
def send(msg): def select(timeout_ms):
return pipe.write(msg) return _msg.select(timeout_ms)
def read():
return pipe.read()
def set_notify(_on_read): def send(msg):
return pipe.set_notify(_on_read) return _msg.send(msg)
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

View File

@ -1,12 +1,11 @@
import math import math
import utime import utime
from TrezorUi import Display, Touch from TrezorUi import Display
from . import loop from . import loop
display = Display() display = Display()
touch = Touch()
def rgbcolor(r: int, g: int, b: int) -> int: def rgbcolor(r: int, g: int, b: int) -> int:
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3) return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)

View File

@ -8,9 +8,6 @@ type_gen = type((lambda: (yield))())
def memaccess(address, length): def memaccess(address, length):
return _utils.memaccess(address, length) return _utils.memaccess(address, length)
def select(timeout_us):
return _utils.select(timeout_us)
def unimport_func(func): def unimport_func(func):
def inner(*args, **kwargs): def inner(*args, **kwargs):
mods = set(sys.modules) mods = set(sys.modules)