From bf919e9192e510f691d480d0cee7e3c1e7186822 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 14 Oct 2016 18:40:30 +0200 Subject: [PATCH] modtrezorconfig: prepare for norcow usage --- extmod/modtrezorconfig/modtrezorconfig.c | 32 ++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/extmod/modtrezorconfig/modtrezorconfig.c b/extmod/modtrezorconfig/modtrezorconfig.c index 125b1f802f..85aad476d1 100644 --- a/extmod/modtrezorconfig/modtrezorconfig.c +++ b/extmod/modtrezorconfig/modtrezorconfig.c @@ -16,6 +16,14 @@ #if MICROPY_PY_TREZORCONFIG + +// temporary function stubs from norcow +void norcow_init(void) { } +bool norcow_get(uint16_t key, const void **val, uint32_t *len) { *val = "Works!"; *len = 6; return true; } +bool norcow_set(uint16_t key, const void *val, uint32_t len) { return true; } +// end + + typedef struct _mp_obj_Config_t { mp_obj_base_t base; } mp_obj_Config_t; @@ -24,6 +32,7 @@ STATIC mp_obj_t mod_TrezorConfig_Config_make_new(const mp_obj_type_t *type, size mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_Config_t *o = m_new_obj(mp_obj_Config_t); o->base.type = type; + norcow_init(); return MP_OBJ_FROM_PTR(o); } @@ -32,16 +41,35 @@ STATIC mp_obj_t mod_TrezorConfig_Config_make_new(const mp_obj_type_t *type, size /// Gets a value of given key for given app (or None if not set). /// ''' STATIC mp_obj_t mod_TrezorConfig_Config_get(mp_obj_t self, mp_obj_t app, mp_obj_t key) { - return mp_const_none; + uint8_t a = mp_obj_get_int(app); + uint8_t k = mp_obj_get_int(key); + uint16_t appkey = a << 8 | k; + const void *val; + uint32_t len; + bool r = norcow_get(appkey, &val, &len); + if (!r) return mp_const_none; + vstr_t vstr; + vstr_init_len(&vstr, len); + memcpy(vstr.buf, val, len); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorConfig_Config_get_obj, mod_TrezorConfig_Config_get); -/// def trezor.config.set(app: int, key: int) -> bool: +/// def trezor.config.set(app: int, key: int, value: bytes) -> None: /// ''' /// Sets a value of given key for given app. /// Returns True on success. /// ''' STATIC mp_obj_t mod_TrezorConfig_Config_set(size_t n_args, const mp_obj_t *args) { + uint8_t a = mp_obj_get_int(args[1]); + uint8_t k = mp_obj_get_int(args[2]); + uint16_t appkey = a << 8 | k; + mp_buffer_info_t value; + mp_get_buffer_raise(args[3], &value, MP_BUFFER_READ); + bool r = norcow_set(appkey, value.buf, value.len); + if (!r) { + mp_raise_ValueError("Could not save value"); + } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorConfig_Config_set_obj, 4, 4, mod_TrezorConfig_Config_set);