1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-25 01:18:54 +00:00

refactor(core): ble: expose connection state in micropython

[no changelog]
This commit is contained in:
Martin Milata 2025-04-09 01:20:13 +02:00
parent a9b5c3d74d
commit d0b814fd97
3 changed files with 27 additions and 11 deletions

View File

@ -180,17 +180,26 @@ STATIC mp_obj_t mod_trezorio_BLE_peer_count(void) {
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_BLE_peer_count_obj,
mod_trezorio_BLE_peer_count);
/// def is_connected() -> bool:
/// def connection_state() -> int:
/// """
/// TODO: this should really return struct or enum
/// Returns current connection state as flags:
///
/// 0x01 state known
/// 0x02 connectable
/// 0x04 connected
/// 0x08 pairing
/// 0x10 pairing request
/// """
STATIC mp_obj_t mod_trezorio_BLE_is_connected(void) {
STATIC mp_obj_t mod_trezorio_BLE_connection_state(void) {
ble_state_t state;
ble_get_state(&state);
return MP_OBJ_NEW_SMALL_INT(state.connected);
mp_int_t flags = (state.state_known << 0) | (state.connectable << 1) |
(state.connected << 2) | (state.pairing << 3) |
(state.pairing_requested << 4);
return MP_OBJ_NEW_SMALL_INT(flags);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_BLE_is_connected_obj,
mod_trezorio_BLE_is_connected);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_BLE_connection_state_obj,
mod_trezorio_BLE_connection_state);
const size_t CODE_LEN = 6;
static bool encode_pairing_code(mp_obj_t obj, uint8_t *outbuf) {
@ -365,8 +374,8 @@ STATIC const mp_rom_map_elem_t mod_trezorio_BLE_globals_table[] = {
MP_ROM_PTR(&mod_trezorio_BLE_disconnect_obj)},
{MP_ROM_QSTR(MP_QSTR_peer_count),
MP_ROM_PTR(&mod_trezorio_BLE_peer_count_obj)},
{MP_ROM_QSTR(MP_QSTR_is_connected),
MP_ROM_PTR(&mod_trezorio_BLE_is_connected_obj)},
{MP_ROM_QSTR(MP_QSTR_connection_state),
MP_ROM_PTR(&mod_trezorio_BLE_connection_state_obj)},
{MP_ROM_QSTR(MP_QSTR_allow_pairing),
MP_ROM_PTR(&mod_trezorio_BLE_allow_pairing_obj)},
{MP_ROM_QSTR(MP_QSTR_reject_pairing),

View File

@ -52,9 +52,15 @@ def peer_count() -> int:
# upymod/modtrezorio/modtrezorio-ble.h
def is_connected() -> bool:
def connection_state() -> int:
"""
TODO: this should really return struct or enum
Returns current connection state as flags:
0x01 state known
0x02 connectable
0x04 connected
0x08 pairing
0x10 pairing request
"""

View File

@ -10,10 +10,11 @@ async def handle_device_menu() -> None:
# MOCK DATA
failed_backup = True
battery_percentage = 22
paired_devices = ["Trezor Suite"] if ble.is_connected() else []
paired_devices = ["Trezor Suite"] if (ble.connection_state() & 0x04) else []
# ###
firmware_version = ".".join(map(str, utils.VERSION))
device_name = storage.device.get_label() or "Trezor"
print(bin(ble.connection_state()))
menu_result = await raise_if_not_confirmed(
trezorui_api.show_device_menu(