msg: rename to io

This commit is contained in:
Jan Pochyla 2017-08-14 11:08:47 +02:00
parent 4c987454eb
commit d2781d030d
11 changed files with 74 additions and 110 deletions

View File

@ -93,14 +93,12 @@ void check_and_jump(void)
int usb_init_all(void) { int usb_init_all(void) {
static const usb_dev_info_t dev_info = { static const usb_dev_info_t dev_info = {
.vendor_id = 0x1209, .vendor_id = 0x1209,
.product_id = 0x53C0, .product_id = 0x53C0,
.release_num = 0x0002, .release_num = 0x0002,
.manufacturer_str = (const uint8_t *)"SatoshiLabs", .manufacturer = (const uint8_t *)"SatoshiLabs",
.product_str = (const uint8_t *)"TREZOR Bootloader", .product = (const uint8_t *)"TREZOR Bootloader",
.serial_number_str = (const uint8_t *)"", .serial_number = (const uint8_t *)"",
.configuration_str = (const uint8_t *)"",
.interface_str = (const uint8_t *)"",
}; };
static uint8_t hid_rx_buffer[USB_PACKET_SIZE]; static uint8_t hid_rx_buffer[USB_PACKET_SIZE];
static const uint8_t hid_report_desc[] = { static const uint8_t hid_report_desc[] = {

View File

@ -175,7 +175,7 @@ STATIC mp_obj_t mod_trezorio_HID_make_new(const mp_obj_type_t *type, size_t n_ar
/// Returns the configured number of this interface. /// Returns the configured number of this interface.
/// ''' /// '''
STATIC mp_obj_t mod_trezorio_HID_iface_num(mp_obj_t self) { STATIC mp_obj_t mod_trezorio_HID_iface_num(mp_obj_t self) {
mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); mp_obj_HID_t *o = MP_OBJ_TO_PTR(self);
return MP_OBJ_NEW_SMALL_INT(o->info.iface_num); 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 MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_HID_iface_num_obj, mod_trezorio_HID_iface_num);
@ -273,7 +273,7 @@ STATIC mp_obj_t mod_trezorio_VCP_make_new(const mp_obj_type_t *type, size_t n_ar
/// Returns the configured number of this interface. /// Returns the configured number of this interface.
/// ''' /// '''
STATIC mp_obj_t mod_trezorio_VCP_iface_num(mp_obj_t self) { STATIC mp_obj_t mod_trezorio_VCP_iface_num(mp_obj_t self) {
mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); mp_obj_VCP_t *o = MP_OBJ_TO_PTR(self);
return MP_OBJ_NEW_SMALL_INT(o->info.iface_num); 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 MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_VCP_iface_num_obj, mod_trezorio_VCP_iface_num);
@ -391,11 +391,11 @@ STATIC mp_obj_t mod_trezorio_USB_add(mp_obj_t self, mp_obj_t iface) {
if (o->state != USB_CLOSED) { if (o->state != USB_CLOSED) {
mp_raise_msg(&mp_type_RuntimeError, "already initialized"); mp_raise_msg(&mp_type_RuntimeError, "already initialized");
} }
mp_obj_list_append(o->ifaces, iface); mp_obj_list_append(MP_OBJ_FROM_PTR(&o->ifaces), iface);
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_USB_add_obj, mod_trezorio_USB_add); STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_USB_add_obj, mod_trezorio_USB_add);
/// def start(self) -> None: /// def start(self) -> None:
/// ''' /// '''
@ -410,7 +410,7 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) {
size_t iface_cnt; size_t iface_cnt;
mp_obj_t *iface_objs; mp_obj_t *iface_objs;
mp_obj_get_array(o->ifaces, &iface_cnt, &iface_objs); mp_obj_get_array(MP_OBJ_FROM_PTR(&o->ifaces), &iface_cnt, &iface_objs);
// Initialize the USB stack // Initialize the USB stack
if (usb_init(&o->info) != 0) { if (usb_init(&o->info) != 0) {
@ -443,7 +443,7 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) {
} }
// Start the USB stack // Start the USB stack
if (USB_open() != 0) { if (usb_start() != 0) {
usb_deinit(); usb_deinit();
mp_raise_msg(&mp_type_RuntimeError, "failed to start USB"); mp_raise_msg(&mp_type_RuntimeError, "failed to start USB");
} }
@ -455,21 +455,21 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) {
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_USB_open_obj, mod_trezorio_USB_open); STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_USB_open_obj, mod_trezorio_USB_open);
/// def stop(self) -> None: /// def stop(self) -> None:
/// ''' /// '''
/// Cleans up the USB stack. /// Cleans up the USB stack.
/// ''' /// '''
STATIC mp_obj_t mod_trezorio_USB_close_usb(mp_obj_t self) { STATIC mp_obj_t mod_trezorio_USB_close(mp_obj_t self) {
mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); mp_obj_USB_t *o = MP_OBJ_TO_PTR(self);
if (o->state != USB_OPENED) { if (o->state != USB_OPENED) {
mp_raise_msg(&mp_type_RuntimeError, "not initialized"); mp_raise_msg(&mp_type_RuntimeError, "not initialized");
} }
USB_close(); usb_stop();
usb_deinit(); usb_deinit();
mp_obj_list_set_len(o->ifaces, 0); mp_obj_list_set_len(MP_OBJ_FROM_PTR(&o->ifaces), 0);
mp_seq_clear(o->ifaces.items, 0, o->ifaces.alloc, sizeof(*o->ifaces.items)); mp_seq_clear(o->ifaces.items, 0, o->ifaces.alloc, sizeof(*o->ifaces.items));
o->info.vendor_id = 0; o->info.vendor_id = 0;
o->info.product_id = 0; o->info.product_id = 0;
@ -481,7 +481,7 @@ STATIC mp_obj_t mod_trezorio_USB_close_usb(mp_obj_t self) {
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_USB_close_usb_obj, mod_trezorio_USB_close_usb); STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_USB_close_obj, mod_trezorio_USB_close);
/// def write(self, iface: int, msg: bytes) -> int: /// def write(self, iface: int, msg: bytes) -> int:
/// ''' /// '''
@ -503,7 +503,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_USB_write_obj, mod_trezorio_USB_wr
STATIC mp_obj_t mod_trezorio_USB___del__(mp_obj_t self) { STATIC mp_obj_t mod_trezorio_USB___del__(mp_obj_t self) {
mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); mp_obj_USB_t *o = MP_OBJ_TO_PTR(self);
if (o->state != USB_CLOSED) { if (o->state != USB_CLOSED) {
usb_close(); usb_stop();
usb_deinit(); usb_deinit();
o->state = USB_CLOSED; o->state = USB_CLOSED;
} }

View File

@ -68,14 +68,14 @@ int usb_init(const usb_dev_info_t *dev_info) {
usb_dev_desc.bNumConfigurations = 1; usb_dev_desc.bNumConfigurations = 1;
// String table // String table
if ((0 != check_desc_str(dev_info->manufacturer_str)) || if ((0 != check_desc_str(dev_info->manufacturer)) ||
(0 != check_desc_str(dev_info->product_str)) || (0 != check_desc_str(dev_info->product)) ||
(0 != check_desc_str(dev_info->serial_number_str))) { (0 != check_desc_str(dev_info->serial_number))) {
return 1; // Invalid descriptor string return 1; // Invalid descriptor string
} }
usb_str_table.manufacturer_str = dev_info->manufacturer_str; usb_str_table.manufacturer = dev_info->manufacturer;
usb_str_table.product_str = dev_info->product_str; usb_str_table.product = dev_info->product;
usb_str_table.serial_str = dev_info->serial_number_str; usb_str_table.serial_number = dev_info->serial_number;
// Configuration descriptor // Configuration descriptor
usb_config_desc->bLength = sizeof(usb_config_descriptor_t); usb_config_desc->bLength = sizeof(usb_config_descriptor_t);
@ -185,17 +185,17 @@ static uint8_t *usb_get_langid_str_descriptor(USBD_SpeedTypeDef speed, uint16_t
} }
static uint8_t *usb_get_manufacturer_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) { static uint8_t *usb_get_manufacturer_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
USBD_GetString(UNCONST(usb_str_table.manufacturer_str), usb_str_buf, length); USBD_GetString(UNCONST(usb_str_table.manufacturer), usb_str_buf, length);
return usb_str_buf; return usb_str_buf;
} }
static uint8_t *usb_get_product_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) { static uint8_t *usb_get_product_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
USBD_GetString(UNCONST(usb_str_table.product_str), usb_str_buf, length); USBD_GetString(UNCONST(usb_str_table.product), usb_str_buf, length);
return usb_str_buf; return usb_str_buf;
} }
static uint8_t *usb_get_serial_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) { static uint8_t *usb_get_serial_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
USBD_GetString(UNCONST(usb_str_table.serial_str), usb_str_buf, length); USBD_GetString(UNCONST(usb_str_table.serial_number), usb_str_buf, length);
return usb_str_buf; return usb_str_buf;
} }

View File

@ -85,18 +85,18 @@ typedef enum {
} usb_language_id_t; } usb_language_id_t;
typedef struct { typedef struct {
const uint8_t *manufacturer_str; const uint8_t *manufacturer;
const uint8_t *product_str; const uint8_t *product;
const uint8_t *serial_str; const uint8_t *serial_number;
} usb_dev_string_table_t; } usb_dev_string_table_t;
typedef struct { typedef struct {
uint16_t vendor_id; uint16_t vendor_id;
uint16_t product_id; uint16_t product_id;
uint16_t release_num; uint16_t release_num;
const uint8_t *manufacturer_str; const uint8_t *manufacturer;
const uint8_t *product_str; const uint8_t *product;
const uint8_t *serial_number_str; const uint8_t *serial_number;
} usb_dev_info_t; } usb_dev_info_t;
typedef enum { typedef enum {

View File

@ -6,7 +6,7 @@ import utime
from trezor import log from trezor import log
from trezor import loop from trezor import loop
from trezor import msg from trezor import io
from trezor import ui from trezor import ui
from trezor import utils from trezor import utils
from trezor import workflow from trezor import workflow
@ -295,7 +295,7 @@ def send_cmd(cmd: Cmd, iface: int) -> None:
frm.bcnt = datalen frm.bcnt = datalen
offset += utils.memcpy(frm.data, 0, cmd.data, offset, datalen) offset += utils.memcpy(frm.data, 0, cmd.data, offset, datalen)
msg.send(iface, buf) io.send(iface, buf)
# log.debug(__name__, 'send init %s', buf) # log.debug(__name__, 'send init %s', buf)
if offset < datalen: if offset < datalen:
@ -305,7 +305,7 @@ def send_cmd(cmd: Cmd, iface: int) -> None:
frm.seq = seq frm.seq = seq
offset += utils.memcpy(frm.data, 0, cmd.data, offset, datalen) offset += utils.memcpy(frm.data, 0, cmd.data, offset, datalen)
utime.sleep_ms(1) # FIXME: do async send utime.sleep_ms(1) # FIXME: do async send
msg.send(iface, buf) io.send(iface, buf)
# log.debug(__name__, 'send cont %s', buf) # log.debug(__name__, 'send cont %s', buf)
seq += 1 seq += 1

View File

@ -1,16 +1,11 @@
from micropython import const from micropython import const
import trezor.main from trezor import io
from trezor import config
from trezor import msg
from trezor import ui
from trezor import wire from trezor import wire
from trezor import loop from trezor import main
from trezor.wire import codec_v2
config.init() # Load applications
from apps.common import storage
# Load all applications
if __debug__: if __debug__:
from apps import debug from apps import debug
from apps import homescreen from apps import homescreen
@ -19,7 +14,7 @@ from apps import wallet
from apps import ethereum from apps import ethereum
from apps import fido_u2f from apps import fido_u2f
# Initialize all applications # Boot applications
if __debug__: if __debug__:
debug.boot() debug.boot()
homescreen.boot() homescreen.boot()
@ -28,21 +23,9 @@ wallet.boot()
ethereum.boot() ethereum.boot()
fido_u2f.boot() fido_u2f.boot()
# HACK: keep storage loaded at all times # Intialize the USB stack
from apps.common import storage usb_wire = io.HID(
iface_num=0x00,
# Change backlight to white for better visibility
ui.display.backlight(ui.BACKLIGHT_NORMAL)
# Register USB ifaces
_IFACE_WIRE = const(0x00)
_IFACE_VCP = const(0x01)
_IFACE_VCP_DATA = const(0x02)
_IFACE_U2F = const(0x03)
hid_wire = msg.HID(
iface_num=_IFACE_WIRE,
ep_in=0x81, ep_in=0x81,
ep_out=0x01, ep_out=0x01,
report_desc=bytes([ report_desc=bytes([
@ -64,17 +47,15 @@ hid_wire = msg.HID(
0xc0, # END_COLLECTION 0xc0, # END_COLLECTION
]), ]),
) )
usb_vcp = io.VCP(
vcp = msg.VCP( iface_num=0x01,
iface_num=_IFACE_VCP, data_iface_num=0x02,
data_iface_num=_IFACE_VCP_DATA,
ep_in=0x82, ep_in=0x82,
ep_out=0x02, ep_out=0x02,
ep_cmd=0x83, ep_cmd=0x83,
) )
usb_u2f = io.HID(
hid_u2f = msg.HID( iface_num=0x03,
iface_num=_IFACE_U2F,
ep_in=0x84, ep_in=0x84,
ep_out=0x03, ep_out=0x03,
report_desc=bytes([ report_desc=bytes([
@ -96,21 +77,24 @@ hid_u2f = msg.HID(
0xc0, # END_COLLECTION 0xc0, # END_COLLECTION
]), ]),
) )
usb = io.USB(
msg.init_usb(msg.USB(
vendor_id=0x1209, vendor_id=0x1209,
product_id=0x53C1, product_id=0x53C1,
release_num=0x0002, release_num=0x0002,
manufacturer_str="SatoshiLabs", manufacturer="SatoshiLabs",
product_str="TREZOR", product="TREZOR",
serial_number_str="000000000000000000000000" serial_number="000000000000000000000000",
), (hid_wire, vcp, hid_u2f)) )
usb.add(usb_wire)
usb.add(usb_vcp)
usb.add(usb_u2f)
usb.open()
# Initialize the wire codec pipeline # Initialize the wire codec pipeline
wire.setup(_IFACE_WIRE) wire.setup(usb_wire.iface_num())
# Load default homescreen # Load default homescreen
from apps.homescreen.homescreen import layout_homescreen from apps.homescreen.homescreen import layout_homescreen
# Run main even loop and specify which screen is default # Run main even loop and specify which screen is default
trezor.main.run(default_workflow=layout_homescreen) main.run(default_workflow=layout_homescreen)

View File

@ -11,18 +11,16 @@ and `Wait`.
import utime import utime
import utimeq import utimeq
from micropython import const from micropython import const
from trezor import msg
from trezor import log from trezor import log
from trezor import io
import trezormsg TOUCH = io.TOUCH
TOUCH_START = io.TOUCH_START
TOUCH_MOVE = io.TOUCH_MOVE
TOUCH_END = io.TOUCH_END
TOUCH = trezormsg.TOUCH READ = io.POLL_READ
TOUCH_START = trezormsg.TOUCH_START WRITE = io.POLL_WRITE
TOUCH_MOVE = trezormsg.TOUCH_MOVE
TOUCH_END = trezormsg.TOUCH_END
READ = trezormsg.POLL_READ
WRITE = trezormsg.POLL_WRITE
after_step_hook = None # function, called after each task step after_step_hook = None # function, called after each task step

View File

@ -1,15 +0,0 @@
from trezormsg import Msg, USB, HID, VCP
_msg = Msg()
def init_usb(usb, ifaces):
return _msg.init_usb(usb, ifaces)
def select(timeout_us):
return _msg.select(timeout_us)
def send(iface, msg):
return _msg.send(iface, msg)

View File

@ -121,7 +121,6 @@ async def handle_unexp_msg(ctx, reader):
await ctx.write( await ctx.write(
Failure(code=UnexpectedMessage, message='Unexpected message')) Failure(code=UnexpectedMessage, message='Unexpected message'))
def make_failure_msg(exc): def make_failure_msg(exc):
from trezor.messages.Failure import Failure from trezor.messages.Failure import Failure
from trezor.messages.FailureType import FirmwareError from trezor.messages.FailureType import FirmwareError

View File

@ -127,7 +127,7 @@ class Writer:
if self.ofs == _REP_LEN: if self.ofs == _REP_LEN:
# we are at the end of the report, flush it # we are at the end of the report, flush it
await write await write
io.send(self.iface, self.data) io.write(self.iface, self.data)
self.ofs = _REP_CONT_DATA self.ofs = _REP_CONT_DATA
return nwritten return nwritten

View File

@ -200,12 +200,12 @@ class SesssionSupervisor:
self.open(newsid) self.open(newsid)
yield yield
await write await write
self.sendopen(newsid) self.writeopen(newsid)
elif repmarker == _REP_MARKER_CLOSE: elif repmarker == _REP_MARKER_CLOSE:
self.close(repsid) self.close(repsid)
yield yield
await write await write
self.sendclose(repsid) self.writeclose(repsid)
def open(self, sid): def open(self, sid):
if sid not in self.handling_tasks: if sid not in self.handling_tasks:
@ -223,10 +223,10 @@ class SesssionSupervisor:
if sid not in self.handling_tasks: if sid not in self.handling_tasks:
return sid return sid
def sendopen(self, sid): def writeopen(self, sid):
ustruct.pack_into(_REP, self.session_report, 0, _REP_MARKER_OPEN, sid) ustruct.pack_into(_REP, self.session_report, 0, _REP_MARKER_OPEN, sid)
io.send(self.iface, self.session_report) io.write(self.iface, self.session_report)
def sendclose(self, sid): def writeclose(self, sid):
ustruct.pack_into(_REP, self.session_report, 0, _REP_MARKER_CLOSE, sid) ustruct.pack_into(_REP, self.session_report, 0, _REP_MARKER_CLOSE, sid)
io.send(self.iface, self.session_report) io.write(self.iface, self.session_report)