diff --git a/docs/api.md b/docs/api.md index 86014c03..078b667d 100644 --- a/docs/api.md +++ b/docs/api.md @@ -15,6 +15,23 @@ bytes65 = bytes # bytes variable of exactly 65 bytes Syntax used below is a valid Python function declaration with type hints defined in [PEP 0484](https://www.python.org/dev/peps/pep-0484/). + +##trezor.msg + +``` python +def trezor.msg.receive(callback) -> None + +def trezor.msg.send(message) -> None +``` + +##trezor.protobuf + +``` python +def trezor.protobuf.encode(message) -> bytes + +def trezor.protobuf.decode(data: bytes) -> object +``` + ##trezor.ui ###trezor.ui.display diff --git a/extmod/modTrezorMsg/modTrezorMsg.c b/extmod/modTrezorMsg/modTrezorMsg.c new file mode 100644 index 00000000..b6e27932 --- /dev/null +++ b/extmod/modTrezorMsg/modTrezorMsg.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under Microsoft Reference Source License (Ms-RSL) + * see LICENSE.md file for details + */ + +#include +#include +#include + +#include "py/nlr.h" +#include "py/runtime.h" +#include "py/binary.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); + } +} +*/ + +// class Msg(object): +typedef struct _mp_obj_Msg_t { + mp_obj_base_t base; +} 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_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; +} +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) { + // TODO + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorMsg_Msg_send_obj, mod_TrezorMsg_Msg_send); + +// 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_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 const mp_obj_type_t mod_TrezorMsg_Msg_type = { + { &mp_type_type }, + .name = MP_QSTR_Msg, + .make_new = mod_TrezorMsg_Msg_make_new, + .locals_dict = (void*)&mod_TrezorMsg_Msg_locals_dict, +}; + +// module stuff + +STATIC const mp_rom_map_elem_t mp_module_TrezorMsg_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorMsg) }, + { MP_ROM_QSTR(MP_QSTR_Msg), MP_ROM_PTR(&mod_TrezorMsg_Msg_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_TrezorMsg_globals, mp_module_TrezorMsg_globals_table); + +const mp_obj_module_t mp_module_TrezorMsg = { + .base = { &mp_type_module }, + .name = MP_QSTR_TrezorMsg, + .globals = (mp_obj_dict_t*)&mp_module_TrezorMsg_globals, +}; + +#endif // MICROPY_PY_TREZORMSG diff --git a/extmod/modTrezorProtobuf/modTrezorProtobuf.c b/extmod/modTrezorProtobuf/modTrezorProtobuf.c new file mode 100644 index 00000000..356cac10 --- /dev/null +++ b/extmod/modTrezorProtobuf/modTrezorProtobuf.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under Microsoft Reference Source License (Ms-RSL) + * see LICENSE.md file for details + */ + +#include +#include +#include + +#include "py/nlr.h" +#include "py/runtime.h" +#include "py/binary.h" + +#if MICROPY_PY_TREZORPROTOBUF + +// class Protobuf(object): +typedef struct _mp_obj_Protobuf_t { + mp_obj_base_t base; +} mp_obj_Protobuf_t; + +// def Protobuf.__init__(self): +STATIC mp_obj_t mod_TrezorProtobuf_Protobuf_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_obj_Protobuf_t *o = m_new_obj(mp_obj_Protobuf_t); + o->base.type = type; + return MP_OBJ_FROM_PTR(o); +} + +// def Protobuf.encode(self, message) -> bytes +STATIC mp_obj_t mod_TrezorProtobuf_Protobuf_encode(mp_obj_t self, mp_obj_t callback) { + // TODO + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorProtobuf_Protobuf_encode_obj, mod_TrezorProtobuf_Protobuf_encode); + +// def Protobuf.decode(self, data: bytes) -> object +STATIC mp_obj_t mod_TrezorProtobuf_Protobuf_decode(mp_obj_t self, mp_obj_t data) { + // TODO + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorProtobuf_Protobuf_decode_obj, mod_TrezorProtobuf_Protobuf_decode); + +// Protobuf stuff + +STATIC const mp_rom_map_elem_t mod_TrezorProtobuf_Protobuf_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_encode), MP_ROM_PTR(&mod_TrezorProtobuf_Protobuf_encode_obj) }, + { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&mod_TrezorProtobuf_Protobuf_decode_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(mod_TrezorProtobuf_Protobuf_locals_dict, mod_TrezorProtobuf_Protobuf_locals_dict_table); + +STATIC const mp_obj_type_t mod_TrezorProtobuf_Protobuf_type = { + { &mp_type_type }, + .name = MP_QSTR_Protobuf, + .make_new = mod_TrezorProtobuf_Protobuf_make_new, + .locals_dict = (void*)&mod_TrezorProtobuf_Protobuf_locals_dict, +}; + +// module stuff + +STATIC const mp_rom_map_elem_t mp_module_TrezorProtobuf_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorProtobuf) }, + { MP_ROM_QSTR(MP_QSTR_Protobuf), MP_ROM_PTR(&mod_TrezorProtobuf_Protobuf_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_TrezorProtobuf_globals, mp_module_TrezorProtobuf_globals_table); + +const mp_obj_module_t mp_module_TrezorProtobuf = { + .base = { &mp_type_module }, + .name = MP_QSTR_TrezorProtobuf, + .globals = (mp_obj_dict_t*)&mp_module_TrezorProtobuf_globals, +}; + +#endif // MICROPY_PY_TREZORPROTOBUF diff --git a/src/trezor/__init__.py b/src/trezor/__init__.py index e69de29b..deed03d6 100644 --- a/src/trezor/__init__.py +++ b/src/trezor/__init__.py @@ -0,0 +1,5 @@ +from TrezorMsg import Msg +from TrezorProtobuf import Protobuf + +msg = Msg() +protobuf = Protobuf() diff --git a/vendor/micropython b/vendor/micropython index a396fdf9..b0b7d728 160000 --- a/vendor/micropython +++ b/vendor/micropython @@ -1 +1 @@ -Subproject commit a396fdf99c3c485ec526bc89b7cb11102f776139 +Subproject commit b0b7d728c89ee842e1a0c4b11453e212c606863e