1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-25 16:08:32 +00:00

feat(core): include pairing code in BLE_ALLOW_PAIRING command

[no changelog]
This commit is contained in:
Martin Milata 2025-03-31 19:59:11 +02:00
parent 88706d12f8
commit 469f093b8a
3 changed files with 37 additions and 13 deletions

View File

@ -35,13 +35,19 @@ pub fn pairing_mode(name: &str) {
} }
} }
pub fn allow_pairing() { pub fn allow_pairing(mut code: u32) {
unsafe { const CODE_LEN: u8 = 6;
let mut cmd = ffi::ble_command_t { let mut cmd = ffi::ble_command_t {
cmd_type: ffi::ble_command_type_t_BLE_ALLOW_PAIRING, cmd_type: ffi::ble_command_type_t_BLE_ALLOW_PAIRING,
data_len: 0, data_len: CODE_LEN,
data: ffi::ble_command_data_t { raw: [0; 32] }, data: ffi::ble_command_data_t { raw: [0; 32] },
}; };
unsafe {
for i in (0..CODE_LEN).rev() {
let digit = b'0' + ((code % 10) as u8);
code /= 10;
cmd.data.raw[i as usize] = digit;
}
ffi::ble_issue_command(&mut cmd as _); ffi::ble_issue_command(&mut cmd as _);
} }
} }

View File

@ -169,15 +169,32 @@ 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 allow_pairing() -> bool: const size_t CODE_LEN = 6;
static bool encode_pairing_code(mp_obj_t obj, uint8_t *outbuf) {
mp_int_t code = mp_obj_get_int(obj);
if (code < 0 || code > 999999) {
return false;
}
for (size_t i = 0; i < CODE_LEN; i++) {
outbuf[CODE_LEN - i - 1] = '0' + (code % 10);
code /= 10;
}
return true;
}
/// def allow_pairing(code: int) -> bool:
/// """ /// """
/// Accept BLE pairing request /// Accept BLE pairing request. Code must match the one received with
/// BLE_PAIRING_REQUEST event.
/// """ /// """
STATIC mp_obj_t mod_trezorio_BLE_allow_pairing() { STATIC mp_obj_t mod_trezorio_BLE_allow_pairing(mp_obj_t code) {
ble_command_t cmd = {.cmd_type = BLE_ALLOW_PAIRING, .data_len = 0}; ble_command_t cmd = {.cmd_type = BLE_ALLOW_PAIRING, .data_len = CODE_LEN};
if (!encode_pairing_code(code, cmd.data.raw)) {
return mp_const_false;
}
return mp_obj_new_bool(ble_issue_command(&cmd)); return mp_obj_new_bool(ble_issue_command(&cmd));
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_BLE_allow_pairing_obj, STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_BLE_allow_pairing_obj,
mod_trezorio_BLE_allow_pairing); mod_trezorio_BLE_allow_pairing);
/// def reject_pairing() -> bool: /// def reject_pairing() -> bool:

View File

@ -45,9 +45,10 @@ def peer_count() -> int:
# upymod/modtrezorio/modtrezorio-ble.h # upymod/modtrezorio/modtrezorio-ble.h
def allow_pairing() -> bool: def allow_pairing(code: int) -> bool:
""" """
Accept BLE pairing request Accept BLE pairing request. Code must match the one received with
BLE_PAIRING_REQUEST event.
""" """