1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-10 15:30:55 +00:00

fixup! feat(core:): introduce BLE driver

This commit is contained in:
tychovrahe 2025-01-08 10:07:11 +01:00
parent 87610cc35c
commit bece4d87e1
3 changed files with 38 additions and 35 deletions

View File

@ -69,45 +69,48 @@ typedef struct {
static ble_driver_t g_ble_driver = {0}; static ble_driver_t g_ble_driver = {0};
static void ble_send_state_request(void) { static bool ble_send_state_request(void) {
uint8_t cmd = INTERNAL_CMD_PING; uint8_t cmd = INTERNAL_CMD_PING;
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;
} }
static void ble_send_advertising_on(bool whitelist) { static bool ble_send_advertising_on(bool whitelist) {
uint8_t data[2]; uint8_t data[2];
data[0] = INTERNAL_CMD_ADVERTISING_ON; data[0] = INTERNAL_CMD_ADVERTISING_ON;
data[1] = whitelist ? 1 : 0; data[1] = whitelist ? 1 : 0;
nrf_send_msg(NRF_SERVICE_BLE_MANAGER, data, sizeof(data), NULL, NULL); return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, data, sizeof(data), NULL,
NULL) >= 0;
} }
static void ble_send_advertising_off(void) { static bool ble_send_advertising_off(void) {
uint8_t cmd = INTERNAL_CMD_ADVERTISING_OFF; uint8_t cmd = INTERNAL_CMD_ADVERTISING_OFF;
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;
} }
static bool ble_send_erase_bonds(void) { static bool ble_send_erase_bonds(void) {
uint8_t cmd = INTERNAL_CMD_ERASE_BONDS; uint8_t cmd = INTERNAL_CMD_ERASE_BONDS;
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;
return true;
} }
static bool ble_send_disconnect(void) { static bool ble_send_disconnect(void) {
uint8_t cmd = INTERNAL_CMD_DISCONNECT; uint8_t cmd = INTERNAL_CMD_DISCONNECT;
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;
return true;
} }
static void ble_send_pairing_reject(void) { static bool ble_send_pairing_reject(void) {
uint8_t cmd = INTERNAL_CMD_REJECT_PAIRING; uint8_t cmd = INTERNAL_CMD_REJECT_PAIRING;
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;
} }
static void ble_send_pairing_accept(void) { static bool ble_send_pairing_accept(void) {
uint8_t cmd = INTERNAL_CMD_ALLOW_PAIRING; uint8_t cmd = INTERNAL_CMD_ALLOW_PAIRING;
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;
} }
static void ble_process_rx_msg_status(const uint8_t *data, uint32_t len) { static void ble_process_rx_msg_status(const uint8_t *data, uint32_t len) {
@ -187,7 +190,10 @@ static void ble_process_rx_msg_pairing_request(const uint8_t *data,
ble_event_t event = {.type = BLE_PAIRING_REQUEST, .data_len = 6}; ble_event_t event = {.type = BLE_PAIRING_REQUEST, .data_len = 6};
memcpy(event.data, &data[1], 6); memcpy(event.data, &data[1], 6);
tsqueue_enqueue(&drv->event_queue, (uint8_t *)&event, sizeof(event), NULL); if (!tsqueue_enqueue(&drv->event_queue, (uint8_t *)&event, sizeof(event),
NULL)) {
ble_send_pairing_reject();
}
} }
static void ble_process_rx_msg_pairing_cancelled(const uint8_t *data, static void ble_process_rx_msg_pairing_cancelled(const uint8_t *data,
@ -430,6 +436,8 @@ bool ble_issue_command(ble_command_t command) {
return false; return false;
} }
bool result = false;
switch (command) { switch (command) {
case BLE_SWITCH_OFF: case BLE_SWITCH_OFF:
drv->mode_requested = BLE_MODE_OFF; drv->mode_requested = BLE_MODE_OFF;
@ -441,23 +449,23 @@ bool ble_issue_command(ble_command_t command) {
drv->mode_requested = BLE_MODE_PAIRING; drv->mode_requested = BLE_MODE_PAIRING;
break; break;
case BLE_DISCONNECT: case BLE_DISCONNECT:
ble_send_disconnect(); result = ble_send_disconnect();
break; break;
case BLE_ERASE_BONDS: case BLE_ERASE_BONDS:
ble_send_erase_bonds(); result = ble_send_erase_bonds();
break; break;
case BLE_ALLOW_PAIRING: case BLE_ALLOW_PAIRING:
ble_send_pairing_accept(); result = ble_send_pairing_accept();
break; break;
case BLE_REJECT_PAIRING: case BLE_REJECT_PAIRING:
ble_send_pairing_reject(); result = ble_send_pairing_reject();
break; break;
default: default:
// unknown command // unknown command
return false; return false;
} }
return true; return result;
} }
bool ble_get_event(ble_event_t *event) { bool ble_get_event(ble_event_t *event) {

View File

@ -32,16 +32,16 @@ typedef struct {
typedef struct { typedef struct {
tsqueue_entry_t *entries; // Array of queue entries tsqueue_entry_t *entries; // Array of queue entries
int rix; // Read index uint16_t rix; // Read index
int wix; // Write index uint16_t wix; // Write index
int qlen; // Queue length uint16_t qlen; // Queue length
uint16_t size; // Size of each buffer uint16_t size; // Size of each buffer
int32_t next_id; // ID of the next item int32_t next_id; // ID of the next item
} tsqueue_t; } tsqueue_t;
// Initialize the queue // Initialize the queue
void tsqueue_init(tsqueue_t *queue, tsqueue_entry_t *entries, void tsqueue_init(tsqueue_t *queue, tsqueue_entry_t *entries,
uint8_t *buffer_mem, uint16_t size, int qlen); uint8_t *buffer_mem, uint16_t size, uint16_t qlen);
void tsqueue_reset(tsqueue_t *queue); void tsqueue_reset(tsqueue_t *queue);

View File

@ -24,23 +24,18 @@
// Initialize the queue // Initialize the queue
void tsqueue_init(tsqueue_t *queue, tsqueue_entry_t *entries, void tsqueue_init(tsqueue_t *queue, tsqueue_entry_t *entries,
uint8_t *buffer_mem, uint16_t size, int qlen) { uint8_t *buffer_mem, uint16_t size, uint16_t qlen) {
irq_key_t key = irq_lock(); irq_key_t key = irq_lock();
queue->entries = entries; queue->entries = entries;
queue->rix = 0;
queue->wix = 0;
queue->qlen = qlen; queue->qlen = qlen;
queue->size = size; queue->size = size;
queue->next_id = 1;
for (int i = 0; i < qlen; i++) { for (int i = 0; i < qlen; i++) {
if (buffer_mem != NULL) {
queue->entries[i].buffer = buffer_mem + i * size; queue->entries[i].buffer = buffer_mem + i * size;
memset(queue->entries[i].buffer, 0, size);
}
queue->entries[i].len = 0;
} }
tsqueue_reset(queue);
irq_unlock(key); irq_unlock(key);
} }