1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-06 08:08: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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef TREZORHAL_BLE_H #pragma once
#define TREZORHAL_BLE_H
// This module provides interface to BLE (Bluetooth Low Energy) functionality. // This module provides interface to BLE (Bluetooth Low Energy) functionality.
// It allows the device to advertise itself, connect to other devices, and // It allows the device to advertise itself, connect to other devices, and
@ -30,6 +29,7 @@
#define BLE_TX_PACKET_SIZE 64 #define BLE_TX_PACKET_SIZE 64
#define BLE_ADV_NAME_LEN 20 #define BLE_ADV_NAME_LEN 20
#define BLE_PAIRING_CODE_LEN 6
typedef enum { typedef enum {
BLE_SWITCH_OFF = 0, // Turn off BLE advertising, disconnect 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 // When not using static address, the address is random and may not correspond
// to what is actually used for advertising // to what is actually used for advertising
bool ble_get_mac(uint8_t *mac, size_t max_len); 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) { static bool ble_send_state_request(ble_driver_t *drv) {
(void)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) >= return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL) >=
0; 0;
} }
@ -147,10 +147,15 @@ static bool ble_send_pairing_reject(ble_driver_t *drv) {
return result; return result;
} }
static bool ble_send_pairing_accept(ble_driver_t *drv) { static bool ble_send_pairing_accept(ble_driver_t *drv, uint8_t *code) {
uint8_t cmd = INTERNAL_CMD_ALLOW_PAIRING; cmd_allow_pairing_t data = {
bool result = .cmd_id = INTERNAL_CMD_ALLOW_PAIRING,
nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); };
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) { if (result) {
drv->pairing_requested = false; 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) { static bool ble_send_mac_request(ble_driver_t *drv) {
(void)drv; UNUSED(drv);
uint8_t cmd = INTERNAL_CMD_MAC_REQUEST; uint8_t cmd = INTERNAL_CMD_GET_MAC;
return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); 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; return;
} }
ble_event_t event = {.type = BLE_PAIRING_REQUEST, .data_len = 6}; ble_event_t event = {.type = BLE_PAIRING_REQUEST,
memcpy(event.data, &data[1], 6); .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), if (!tsqueue_enqueue(&drv->event_queue, (uint8_t *)&event, sizeof(event),
NULL)) { NULL)) {
ble_send_pairing_reject(drv); ble_send_pairing_reject(drv);
@ -589,7 +595,7 @@ bool ble_issue_command(ble_command_t *command) {
result = ble_send_erase_bonds(drv); result = ble_send_erase_bonds(drv);
break; break;
case BLE_ALLOW_PAIRING: case BLE_ALLOW_PAIRING:
result = ble_send_pairing_accept(drv); result = ble_send_pairing_accept(drv, command->data.raw);
break; break;
case BLE_REJECT_PAIRING: case BLE_REJECT_PAIRING:
result = ble_send_pairing_reject(drv); result = ble_send_pairing_reject(drv);

View File

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