1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-22 22:48:20 +00:00

WIP - get_rotation, set_rotation rust bindings

This commit is contained in:
grdddj 2022-03-14 12:09:38 +01:00
parent afd8b2ff38
commit bbb36a22b2
5 changed files with 58 additions and 17 deletions

View File

@ -25,6 +25,8 @@ static void _librust_qstrs(void) {
MP_QSTR_is_initialized;
MP_QSTR_get_version;
MP_QSTR_set_version;
MP_QSTR_get_rotation;
MP_QSTR_set_rotation;
MP_QSTR_set_timer_fn;
MP_QSTR_touch_event;

View File

@ -1,9 +1,12 @@
use crate::{
error::Error,
micropython::{buffer::Buffer, map::Map, module::Module, obj::Obj, qstr::Qstr},
trezorhal::secbool,
util,
};
use cstr_core::cstr;
use core::convert::{TryFrom, TryInto};
const FLAG_PUBLIC: u8 = 0x80;
@ -73,9 +76,9 @@ extern "C" fn storagedevice_get_version() -> Obj {
unsafe { util::try_or_raise(block) }
}
extern "C" fn storagedevice_set_version(version: Obj) -> Obj {
extern "C" fn storagedevice_set_version(value: Obj) -> Obj {
let block = || {
let value = Buffer::try_from(version)?;
let value = Buffer::try_from(value)?;
let key = _get_appkey(_VERSION, false);
let result = storagedevice_storage_set(key, value);
@ -94,6 +97,33 @@ extern "C" fn storagedevice_is_initialized() -> Obj {
unsafe { util::try_or_raise(block) }
}
extern "C" fn storagedevice_get_rotation() -> Obj {
let block = || {
let key = _get_appkey(_ROTATION, true);
let (buf, len) = storagedevice_storage_get(key);
// TODO: how to convert unknown size buff into int?
let result = (buf[0] as u16) << 8 + (buf[1] as u16);
Ok(result.into())
};
unsafe { util::try_or_raise(block) }
}
extern "C" fn storagedevice_set_rotation(value: Obj) -> Obj {
let block = || {
// TODO: how to raise a micropython exception?
if ![0, 90, 180, 270].contains(&u16::try_from(value)?) {
// return Error::ValueError(cstr!("Not valid rotation"));
}
let value = Buffer::try_from(value)?;
let key = _get_appkey(_ROTATION, true);
let result = storagedevice_storage_set(key, value);
Ok(result.into())
};
unsafe { util::try_or_raise(block) }
}
// TODO: find out how to return the real result
// pub fn storagedevice_storage_get(key: u16) -> &'static [u8] {
// pub fn storagedevice_storage_get(key: u16) -> [u8] {
@ -157,12 +187,20 @@ pub static mp_module_trezorstoragedevice: Module = obj_module! {
Qstr::MP_QSTR_is_initialized => obj_fn_0!(storagedevice_is_initialized).as_obj(),
/// def get_version() -> bytes:
/// """Get from storage."""
/// """Get version."""
Qstr::MP_QSTR_get_version => obj_fn_0!(storagedevice_get_version).as_obj(),
/// def set_version(version: bytes) -> bool:
/// """Save to storage."""
/// def set_version(value: bytes) -> bool:
/// """Set version."""
Qstr::MP_QSTR_set_version => obj_fn_1!(storagedevice_set_version).as_obj(),
/// def get_rotation() -> int:
/// """Get rotation."""
Qstr::MP_QSTR_get_rotation => obj_fn_0!(storagedevice_get_rotation).as_obj(),
/// def set_rotation(value: int) -> bool:
/// """Set rotation."""
Qstr::MP_QSTR_set_rotation => obj_fn_1!(storagedevice_set_rotation).as_obj(),
};
#[cfg(test)]

View File

@ -94,7 +94,7 @@ def get_features() -> Features:
f.passphrase_always_on_device = storage.device.get_passphrase_always_on_device()
f.safety_checks = safety_checks.read_setting()
f.auto_lock_delay_ms = storage.device.get_autolock_delay_ms()
f.display_rotation = storage.device.get_rotation()
f.display_rotation = trezorstoragedevice.get_rotation()
f.experimental_features = storage.device.get_experimental_features()
return f
@ -279,7 +279,7 @@ def reload_settings_from_storage() -> None:
storage.device.get_autolock_delay_ms(), lock_device_if_unlocked
)
wire.experimental_enabled = storage.device.get_experimental_features()
ui.display.orientation(storage.device.get_rotation())
ui.display.orientation(trezorstoragedevice.get_rotation())
def boot() -> None:

View File

@ -1,5 +1,6 @@
import storage
import storage.device
from storage import trezorstoragedevice
from trezor import config, log, loop, ui, utils, wire
from trezor.pin import show_pin_timeout
@ -9,7 +10,7 @@ from apps.homescreen.lockscreen import Lockscreen
async def bootscreen() -> None:
lockscreen = Lockscreen(bootscreen=True)
ui.display.orientation(storage.device.get_rotation())
ui.display.orientation(trezorstoragedevice.get_rotation())
while True:
try:
if can_lock_device():

View File

@ -92,17 +92,17 @@ def get_device_id() -> str:
return dev_id.decode()
def get_rotation() -> int:
rotation = common.get(_NAMESPACE, _ROTATION, public=True)
if not rotation:
return 0
return int.from_bytes(rotation, "big")
# def get_rotation() -> int:
# rotation = common.get(_NAMESPACE, _ROTATION, public=True)
# if not rotation:
# return 0
# return int.from_bytes(rotation, "big")
def set_rotation(value: int) -> None:
if value not in (0, 90, 180, 270):
raise ValueError # unsupported display rotation
common.set(_NAMESPACE, _ROTATION, value.to_bytes(2, "big"), True) # public
# def set_rotation(value: int) -> None:
# if value not in (0, 90, 180, 270):
# raise ValueError # unsupported display rotation
# common.set(_NAMESPACE, _ROTATION, value.to_bytes(2, "big"), True) # public
def get_label() -> str | None: