modtrezormsg: use interfaces index internally, usage_page externally

This commit is contained in:
Pavol Rusnak 2016-10-11 13:13:14 +02:00
parent a423d4602f
commit 882efbf21b
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 37 additions and 23 deletions

View File

@ -9,7 +9,7 @@ extern HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_
void msg_init(void)
{
I2C_InitTypeDef *init = &(I2CHandle1.Init);
init->OwnAddress1 = 0xfe; // master
init->OwnAddress1 = 0xFE; // master
init->ClockSpeed = 400000;
init->DutyCycle = I2C_DUTYCYCLE_16_9;
init->AddressingMode = I2C_ADDRESSINGMODE_7BIT;
@ -20,15 +20,15 @@ void msg_init(void)
i2c_init(&I2CHandle1);
}
ssize_t msg_recv(uint16_t *usage_page, uint8_t *buf, size_t len)
ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
{
*usage_page = 0xFF00; // TODO: return proper usage page
*iface = 0; // TODO: return proper interface
return USBD_HID_Rx(buf, len, 1);
}
ssize_t msg_send(uint16_t usage_page, const uint8_t *buf, size_t len)
ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
{
(void)usage_page; // TODO: ignore usage page for now
(void)iface; // TODO: ignore interface for now
if (len > 0) {
USBD_HID_SendReport(&hUSBDDevice, (uint8_t *)buf, len);
}

View File

@ -36,12 +36,12 @@ void msg_init(void)
assert(b != -1);
}
ssize_t msg_recv(uint16_t *usage_page, uint8_t *buf, size_t len)
ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
{
struct sockaddr_in si;
socklen_t sl = sizeof(si);
memset(buf, 0, len);
*usage_page = 0xFF00; // TODO: return proper usage page
*iface = 0; // TODO: return proper interface
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(uint16_t *usage_page, uint8_t *buf, size_t len)
return r;
}
ssize_t msg_send(uint16_t usage_page, const uint8_t *buf, size_t len)
ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
{
(void)usage_page; // TODO: ignore usage page for now
(void)iface; // TODO: ignore interface for now
ssize_t r = len;
if (slen > 0) {
r = sendto(s, buf, len, MSG_DONTWAIT, (const struct sockaddr *)&si_other, slen);

View File

@ -89,11 +89,20 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorMsg_Msg_get_interfaces_obj, mod_Trezo
/// Sends message using USB HID (device) or UDP (emulator).
/// '''
STATIC mp_obj_t mod_TrezorMsg_Msg_send(mp_obj_t self, mp_obj_t usage_page, mp_obj_t message) {
mp_obj_Msg_t *o = MP_OBJ_TO_PTR(self);
if (o->interface_count == 0) {
mp_raise_TypeError("No interfaces registered");
}
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(up, msg.buf, msg.len);
return MP_OBJ_NEW_SMALL_INT(r);
for (uint8_t i = 0; i < o->interface_count; i++) {
if (o->usage_pages[i] == up) {
mp_buffer_info_t msg;
mp_get_buffer_raise(message, &msg, MP_BUFFER_READ);
ssize_t r = msg_send(i, msg.buf, msg.len);
return MP_OBJ_NEW_SMALL_INT(r);
}
}
mp_raise_TypeError("Interface not found");
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorMsg_Msg_send_obj, mod_TrezorMsg_Msg_send);
@ -106,6 +115,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorMsg_Msg_send_obj, mod_TrezorMsg_Msg_s
/// Function returns None if timeout specified in microseconds is reached.
/// '''
STATIC mp_obj_t mod_TrezorMsg_Msg_select(mp_obj_t self, mp_obj_t timeout_us) {
mp_obj_Msg_t *o = MP_OBJ_TO_PTR(self);
int timeout = mp_obj_get_int(timeout_us);
if (timeout < 0) {
timeout = 0;
@ -120,15 +130,19 @@ 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);
}
uint16_t iface_usage_page;
uint8_t 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_usage_page);
tuple->items[1] = mp_obj_new_str_of_type(&mp_type_bytes, recvbuf, l);
return MP_OBJ_FROM_PTR(tuple);
}
// check for interfaces only when some have been registered
if (o->interface_count > 0) {
uint8_t iface;
uint8_t recvbuf[64];
ssize_t l = msg_recv(&iface, recvbuf, 64);
if (l > 0 && iface < o->interface_count) {
uint16_t iface_usage_page = o->usage_pages[iface];
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
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);
}
}
if (timeout <= 0) {
break;
}

View File

@ -2,7 +2,7 @@ import ubinascii
from micropython import const
from trezor import msg, loop, log
_DEFAULT_IFACE = const(0xFF00)
_DEFAULT_IFACE = const(0xFF00) # TODO: use proper interface
def read_report_stream(target, iface=_DEFAULT_IFACE):