trezor.msg is now iface aware

pull/25/head
Pavol Rusnak 8 years ago
parent c56719ea8c
commit bf02fe0ddf
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -4,12 +4,12 @@ void msg_init(void)
{ {
} }
const uint8_t *msg_recv(void) ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
{ {
return 0; return 0;
} }
int msg_send(uint8_t *buf, size_t len) ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
{ {
return -1; return -1;
} }

@ -36,26 +36,25 @@ void msg_init(void)
assert(b != -1); assert(b != -1);
} }
#define RECV_BUFLEN 64 ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
const uint8_t *msg_recv(void)
{ {
static uint8_t buf[RECV_BUFLEN];
struct sockaddr_in si; struct sockaddr_in si;
socklen_t sl = sizeof(si); socklen_t sl = sizeof(si);
memset(buf, 0, sizeof(buf)); memset(buf, 0, len);
int len = recvfrom(s, buf, RECV_BUFLEN, MSG_DONTWAIT, (struct sockaddr *)&si, &sl); iface = 0; // UDP uses always interface 0
if (len < 0) { size_t r = recvfrom(s, buf, len, MSG_DONTWAIT, (struct sockaddr *)&si, &sl);
return 0; if (r < 0) {
return r;
} }
si_other = si; si_other = si;
slen = sl; slen = sl;
return buf; return r;
} }
int msg_send(uint8_t *buf, size_t len) ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
{ {
int r = -1; (void)iface; // ignore interface for UDP
ssize_t r = -1;
if (slen) { if (slen) {
r = sendto(s, buf, len, MSG_DONTWAIT, (const struct sockaddr *)&si_other, slen); r = sendto(s, buf, len, MSG_DONTWAIT, (const struct sockaddr *)&si_other, slen);
} }

@ -36,17 +36,18 @@ STATIC mp_obj_t mod_TrezorMsg_Msg_make_new(const mp_obj_type_t *type, size_t n_a
return MP_OBJ_FROM_PTR(o); return MP_OBJ_FROM_PTR(o);
} }
/// def trezor.msg.send(message) -> int /// def trezor.msg.send(iface: int, message: bytes) -> int
/// ///
/// Sends message using USB HID (device) or UDP (emulator). /// Sends message using USB HID (device) or UDP (emulator).
/// ///
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 iface, mp_obj_t message) {
uint8_t iface_num = mp_obj_get_int(iface);
mp_buffer_info_t msg; mp_buffer_info_t msg;
mp_get_buffer_raise(message, &msg, MP_BUFFER_READ); mp_get_buffer_raise(message, &msg, MP_BUFFER_READ);
int r = msg_send(msg.buf, msg.len); ssize_t r = msg_send(iface_num, msg.buf, msg.len);
return MP_OBJ_NEW_SMALL_INT(r); 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_3(mod_TrezorMsg_Msg_send_obj, mod_TrezorMsg_Msg_send);
#define TICK_RESOLUTION 1000 #define TICK_RESOLUTION 1000
@ -69,14 +70,17 @@ STATIC mp_obj_t mod_TrezorMsg_Msg_select(mp_obj_t self, mp_obj_t timeout_us) {
tuple->items[2] = MP_OBJ_NEW_SMALL_INT((e & 0xFF)); tuple->items[2] = MP_OBJ_NEW_SMALL_INT((e & 0xFF));
return MP_OBJ_FROM_PTR(tuple); return MP_OBJ_FROM_PTR(tuple);
} }
const uint8_t *m = msg_recv(); uint8_t iface;
if (m) { uint8_t recvbuf[64];
ssize_t l = msg_recv(&iface, recvbuf, 64);
if (l > 0) {
vstr_t vstr; vstr_t vstr;
vstr_init_len(&vstr, 64); vstr_init_len(&vstr, l);
memcpy(vstr.buf, m, 64); memcpy(vstr.buf, recvbuf, l);
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL)); mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(8); tuple->items[0] = MP_OBJ_NEW_SMALL_INT(8);
tuple->items[1] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); tuple->items[1] = MP_OBJ_NEW_SMALL_INT(iface);
tuple->items[2] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
return MP_OBJ_FROM_PTR(tuple); return MP_OBJ_FROM_PTR(tuple);
} }
if (timeout <= 0) { if (timeout <= 0) {

@ -10,8 +10,8 @@ def select(timeout_us):
return _msg.select(timeout_us) return _msg.select(timeout_us)
def send(msg): def send(iface, msg):
return _msg.send(msg) return _msg.send(iface, msg)
REPORT_LEN = const(64) REPORT_LEN = const(64)
@ -20,7 +20,7 @@ HEADER_MAGIC = const(35) # '#'
def read(): def read():
_, rep = yield loop.Select(loop.HID_READ) _, iface, rep = yield loop.Select(loop.HID_READ)
assert rep[0] == REPORT_NUM assert rep[0] == REPORT_NUM
return rep return rep
@ -69,7 +69,7 @@ def write_wire_msg(mtype, mbuf):
while i < len(data): while i < len(data):
data[i] = 0 data[i] = 0
i += 1 i += 1
send(rep) send(0, rep)
mbuf = mbuf[n:] mbuf = mbuf[n:]
data = rep[1:] data = rep[1:]

Loading…
Cancel
Save