1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-04-15 23:05:45 +00:00

feat(core): send pairing code to NRF when accepting pairing

[no changelog]
This commit is contained in:
tychovrahe 2025-04-02 15:40:24 +02:00 committed by TychoVrahe
parent d597b58274
commit 0355ea6dd3
3 changed files with 27 additions and 16 deletions

View File

@ -17,8 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TREZORHAL_BLE_H
#define TREZORHAL_BLE_H
#pragma once
// This module provides interface to BLE (Bluetooth Low Energy) functionality.
// It allows the device to advertise itself, connect to other devices, and
@ -30,6 +29,7 @@
#define BLE_TX_PACKET_SIZE 64
#define BLE_ADV_NAME_LEN 20
#define BLE_PAIRING_CODE_LEN 6
typedef enum {
BLE_SWITCH_OFF = 0, // Turn off BLE advertising, disconnect
@ -150,5 +150,3 @@ uint32_t ble_read(uint8_t *data, uint16_t max_len);
// When not using static address, the address is random and may not correspond
// to what is actually used for advertising
bool ble_get_mac(uint8_t *mac, size_t max_len);
#endif

View File

@ -82,7 +82,7 @@ static const syshandle_vmt_t ble_iface_handle_vmt;
static bool ble_send_state_request(ble_driver_t *drv) {
(void)drv;
uint8_t cmd = INTERNAL_CMD_PING;
uint8_t cmd = INTERNAL_CMD_SEND_STATE;
return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL) >=
0;
}
@ -147,10 +147,15 @@ static bool ble_send_pairing_reject(ble_driver_t *drv) {
return result;
}
static bool ble_send_pairing_accept(ble_driver_t *drv) {
uint8_t cmd = INTERNAL_CMD_ALLOW_PAIRING;
bool result =
nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL);
static bool ble_send_pairing_accept(ble_driver_t *drv, uint8_t *code) {
cmd_allow_pairing_t data = {
.cmd_id = INTERNAL_CMD_ALLOW_PAIRING,
};
memcpy(data.code, code, sizeof(data.code));
bool result = nrf_send_msg(NRF_SERVICE_BLE_MANAGER, (uint8_t *)&data,
sizeof(data), NULL, NULL);
if (result) {
drv->pairing_requested = false;
@ -160,8 +165,8 @@ static bool ble_send_pairing_accept(ble_driver_t *drv) {
}
static bool ble_send_mac_request(ble_driver_t *drv) {
(void)drv;
uint8_t cmd = INTERNAL_CMD_MAC_REQUEST;
UNUSED(drv);
uint8_t cmd = INTERNAL_CMD_GET_MAC;
return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL);
}
@ -235,8 +240,9 @@ static void ble_process_rx_msg_pairing_request(const uint8_t *data,
return;
}
ble_event_t event = {.type = BLE_PAIRING_REQUEST, .data_len = 6};
memcpy(event.data, &data[1], 6);
ble_event_t event = {.type = BLE_PAIRING_REQUEST,
.data_len = BLE_PAIRING_CODE_LEN};
memcpy(event.data, &data[1], BLE_PAIRING_CODE_LEN);
if (!tsqueue_enqueue(&drv->event_queue, (uint8_t *)&event, sizeof(event),
NULL)) {
ble_send_pairing_reject(drv);
@ -589,7 +595,7 @@ bool ble_issue_command(ble_command_t *command) {
result = ble_send_erase_bonds(drv);
break;
case BLE_ALLOW_PAIRING:
result = ble_send_pairing_accept(drv);
result = ble_send_pairing_accept(drv, command->data.raw);
break;
case BLE_REJECT_PAIRING:
result = ble_send_pairing_reject(drv);

View File

@ -43,13 +43,15 @@ typedef struct {
typedef enum {
INTERNAL_EVENT_STATUS = 0x01,
INTERNAL_EVENT_SUCCESS = 0x02,
INTERNAL_EVENT_FAILURE = 0x03,
INTERNAL_EVENT_PAIRING_REQUEST = 0x04,
INTERNAL_EVENT_PAIRING_CANCELLED = 0x05,
INTERNAL_EVENT_MAC = 0x06,
} internal_event_t;
typedef enum {
INTERNAL_CMD_PING = 0x00,
INTERNAL_CMD_SEND_STATE = 0x00,
INTERNAL_CMD_ADVERTISING_ON = 0x01,
INTERNAL_CMD_ADVERTISING_OFF = 0x02,
INTERNAL_CMD_ERASE_BONDS = 0x03,
@ -58,7 +60,7 @@ typedef enum {
INTERNAL_CMD_ALLOW_PAIRING = 0x06,
INTERNAL_CMD_REJECT_PAIRING = 0x07,
INTERNAL_CMD_UNPAIR = 0x08,
INTERNAL_CMD_MAC_REQUEST = 0x09,
INTERNAL_CMD_GET_MAC = 0x09,
} internal_cmd_t;
typedef struct {
@ -69,3 +71,8 @@ typedef struct {
uint32_t device_code;
uint8_t name[BLE_ADV_NAME_LEN];
} cmd_advertising_on_t;
typedef struct {
uint8_t cmd_id;
uint8_t code[BLE_PAIRING_CODE_LEN];
} cmd_allow_pairing_t;