mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-06-25 09:22:33 +00:00
refactor(core): ble: expose connection state in micropython
[no changelog]
This commit is contained in:
parent
a9b5c3d74d
commit
d0b814fd97
@ -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,
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_BLE_peer_count_obj,
|
||||||
mod_trezorio_BLE_peer_count);
|
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_state_t state;
|
||||||
ble_get_state(&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,
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_BLE_connection_state_obj,
|
||||||
mod_trezorio_BLE_is_connected);
|
mod_trezorio_BLE_connection_state);
|
||||||
|
|
||||||
const size_t CODE_LEN = 6;
|
const size_t CODE_LEN = 6;
|
||||||
static bool encode_pairing_code(mp_obj_t obj, uint8_t *outbuf) {
|
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_PTR(&mod_trezorio_BLE_disconnect_obj)},
|
||||||
{MP_ROM_QSTR(MP_QSTR_peer_count),
|
{MP_ROM_QSTR(MP_QSTR_peer_count),
|
||||||
MP_ROM_PTR(&mod_trezorio_BLE_peer_count_obj)},
|
MP_ROM_PTR(&mod_trezorio_BLE_peer_count_obj)},
|
||||||
{MP_ROM_QSTR(MP_QSTR_is_connected),
|
{MP_ROM_QSTR(MP_QSTR_connection_state),
|
||||||
MP_ROM_PTR(&mod_trezorio_BLE_is_connected_obj)},
|
MP_ROM_PTR(&mod_trezorio_BLE_connection_state_obj)},
|
||||||
{MP_ROM_QSTR(MP_QSTR_allow_pairing),
|
{MP_ROM_QSTR(MP_QSTR_allow_pairing),
|
||||||
MP_ROM_PTR(&mod_trezorio_BLE_allow_pairing_obj)},
|
MP_ROM_PTR(&mod_trezorio_BLE_allow_pairing_obj)},
|
||||||
{MP_ROM_QSTR(MP_QSTR_reject_pairing),
|
{MP_ROM_QSTR(MP_QSTR_reject_pairing),
|
||||||
|
@ -52,9 +52,15 @@ def peer_count() -> int:
|
|||||||
|
|
||||||
|
|
||||||
# upymod/modtrezorio/modtrezorio-ble.h
|
# 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
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,10 +10,11 @@ async def handle_device_menu() -> None:
|
|||||||
# MOCK DATA
|
# MOCK DATA
|
||||||
failed_backup = True
|
failed_backup = True
|
||||||
battery_percentage = 22
|
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))
|
firmware_version = ".".join(map(str, utils.VERSION))
|
||||||
device_name = storage.device.get_label() or "Trezor"
|
device_name = storage.device.get_label() or "Trezor"
|
||||||
|
print(bin(ble.connection_state()))
|
||||||
|
|
||||||
menu_result = await raise_if_not_confirmed(
|
menu_result = await raise_if_not_confirmed(
|
||||||
trezorui_api.show_device_menu(
|
trezorui_api.show_device_menu(
|
||||||
|
Loading…
Reference in New Issue
Block a user