2021-09-13 11:25:37 +00:00
|
|
|
#include "gap.h"
|
|
|
|
|
2022-08-22 17:06:17 +00:00
|
|
|
#include <ble/ble.h>
|
2021-09-13 11:25:37 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2021-12-08 11:28:01 +00:00
|
|
|
#include <furi.h>
|
2021-09-13 11:25:37 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "BtGap"
|
2021-09-13 11:25:37 +00:00
|
|
|
|
|
|
|
#define FAST_ADV_TIMEOUT 30000
|
|
|
|
#define INITIAL_ADV_TIMEOUT 60000
|
|
|
|
|
2022-03-31 14:57:23 +00:00
|
|
|
#define GAP_INTERVAL_TO_MS(x) (uint16_t)((x)*1.25)
|
|
|
|
|
2021-09-13 11:25:37 +00:00
|
|
|
typedef struct {
|
2021-10-12 16:41:42 +00:00
|
|
|
uint16_t gap_svc_handle;
|
|
|
|
uint16_t dev_name_char_handle;
|
|
|
|
uint16_t appearance_char_handle;
|
|
|
|
uint16_t connection_handle;
|
|
|
|
uint8_t adv_svc_uuid_len;
|
|
|
|
uint8_t adv_svc_uuid[20];
|
2021-12-08 11:28:01 +00:00
|
|
|
char* adv_name;
|
2021-09-13 11:25:37 +00:00
|
|
|
} GapSvc;
|
|
|
|
|
|
|
|
typedef struct {
|
2021-12-08 11:28:01 +00:00
|
|
|
GapSvc service;
|
|
|
|
GapConfig* config;
|
2022-03-31 14:57:23 +00:00
|
|
|
GapConnectionParams connection_params;
|
2021-10-12 16:41:42 +00:00
|
|
|
GapState state;
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMutex* state_mutex;
|
2022-01-02 22:36:42 +00:00
|
|
|
GapEventCallback on_event_cb;
|
2021-10-12 16:41:42 +00:00
|
|
|
void* context;
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriTimer* advertise_timer;
|
2021-12-08 11:28:01 +00:00
|
|
|
FuriThread* thread;
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMessageQueue* command_queue;
|
2021-10-12 16:41:42 +00:00
|
|
|
bool enable_adv;
|
2021-09-13 11:25:37 +00:00
|
|
|
} Gap;
|
|
|
|
|
2021-09-15 16:58:32 +00:00
|
|
|
typedef enum {
|
|
|
|
GapCommandAdvFast,
|
|
|
|
GapCommandAdvLowPower,
|
|
|
|
GapCommandAdvStop,
|
2021-12-08 11:28:01 +00:00
|
|
|
GapCommandKillThread,
|
2021-09-15 16:58:32 +00:00
|
|
|
} GapCommand;
|
|
|
|
|
2021-09-13 11:25:37 +00:00
|
|
|
// Identity root key
|
2022-01-05 16:10:18 +00:00
|
|
|
static const uint8_t gap_irk[16] =
|
|
|
|
{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
|
2021-09-13 11:25:37 +00:00
|
|
|
// Encryption root key
|
2022-01-05 16:10:18 +00:00
|
|
|
static const uint8_t gap_erk[16] =
|
|
|
|
{0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21};
|
2021-09-13 11:25:37 +00:00
|
|
|
|
|
|
|
static Gap* gap = NULL;
|
|
|
|
|
2021-09-15 16:58:32 +00:00
|
|
|
static void gap_advertise_start(GapState new_state);
|
2021-12-08 11:28:01 +00:00
|
|
|
static int32_t gap_app(void* context);
|
2021-09-13 11:25:37 +00:00
|
|
|
|
2022-03-31 14:57:23 +00:00
|
|
|
static void gap_verify_connection_parameters(Gap* gap) {
|
|
|
|
furi_assert(gap);
|
|
|
|
|
|
|
|
FURI_LOG_I(
|
|
|
|
TAG,
|
|
|
|
"Connection parameters: Connection Interval: %d (%d ms), Slave Latency: %d, Supervision Timeout: %d",
|
|
|
|
gap->connection_params.conn_interval,
|
|
|
|
GAP_INTERVAL_TO_MS(gap->connection_params.conn_interval),
|
|
|
|
gap->connection_params.slave_latency,
|
|
|
|
gap->connection_params.supervisor_timeout);
|
|
|
|
|
|
|
|
// Send connection parameters request update if necessary
|
|
|
|
GapConnectionParamsRequest* params = &gap->config->conn_param;
|
|
|
|
if(params->conn_int_min > gap->connection_params.conn_interval ||
|
|
|
|
params->conn_int_max < gap->connection_params.conn_interval) {
|
|
|
|
FURI_LOG_W(TAG, "Unsupported connection interval. Request connection parameters update");
|
|
|
|
if(aci_l2cap_connection_parameter_update_req(
|
|
|
|
gap->service.connection_handle,
|
|
|
|
params->conn_int_min,
|
|
|
|
params->conn_int_max,
|
|
|
|
gap->connection_params.slave_latency,
|
|
|
|
gap->connection_params.supervisor_timeout)) {
|
|
|
|
FURI_LOG_E(TAG, "Failed to request connection parameters update");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
|
2021-12-08 11:28:01 +00:00
|
|
|
hci_event_pckt* event_pckt;
|
|
|
|
evt_le_meta_event* meta_evt;
|
|
|
|
evt_blue_aci* blue_evt;
|
|
|
|
hci_le_phy_update_complete_event_rp0* evt_le_phy_update_complete;
|
2021-09-15 16:58:32 +00:00
|
|
|
uint8_t tx_phy;
|
|
|
|
uint8_t rx_phy;
|
|
|
|
tBleStatus ret = BLE_STATUS_INVALID_PARAMS;
|
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
event_pckt = (hci_event_pckt*)((hci_uart_pckt*)pckt)->data;
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
if(gap) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
|
2022-01-02 22:36:42 +00:00
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
switch(event_pckt->evt) {
|
|
|
|
case EVT_DISCONN_COMPLETE: {
|
|
|
|
hci_disconnection_complete_event_rp0* disconnection_complete_event =
|
|
|
|
(hci_disconnection_complete_event_rp0*)event_pckt->data;
|
|
|
|
if(disconnection_complete_event->Connection_Handle == gap->service.connection_handle) {
|
|
|
|
gap->service.connection_handle = 0;
|
|
|
|
gap->state = GapStateIdle;
|
|
|
|
FURI_LOG_I(
|
|
|
|
TAG, "Disconnect from client. Reason: %02X", disconnection_complete_event->Reason);
|
|
|
|
}
|
|
|
|
if(gap->enable_adv) {
|
|
|
|
// Restart advertising
|
|
|
|
gap_advertise_start(GapStateAdvFast);
|
|
|
|
}
|
|
|
|
GapEvent event = {.type = GapEventTypeDisconnected};
|
|
|
|
gap->on_event_cb(event, gap->context);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case EVT_LE_META_EVENT:
|
|
|
|
meta_evt = (evt_le_meta_event*)event_pckt->data;
|
|
|
|
switch(meta_evt->subevent) {
|
2022-01-21 17:32:03 +00:00
|
|
|
case EVT_LE_CONN_UPDATE_COMPLETE: {
|
|
|
|
hci_le_connection_update_complete_event_rp0* event =
|
|
|
|
(hci_le_connection_update_complete_event_rp0*)meta_evt->data;
|
2022-03-31 14:57:23 +00:00
|
|
|
gap->connection_params.conn_interval = event->Conn_Interval;
|
|
|
|
gap->connection_params.slave_latency = event->Conn_Latency;
|
|
|
|
gap->connection_params.supervisor_timeout = event->Supervision_Timeout;
|
|
|
|
FURI_LOG_I(TAG, "Connection parameters event complete");
|
|
|
|
gap_verify_connection_parameters(gap);
|
2022-01-05 16:10:18 +00:00
|
|
|
break;
|
2022-01-21 17:32:03 +00:00
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
|
|
|
|
case EVT_LE_PHY_UPDATE_COMPLETE:
|
|
|
|
evt_le_phy_update_complete = (hci_le_phy_update_complete_event_rp0*)meta_evt->data;
|
|
|
|
if(evt_le_phy_update_complete->Status) {
|
|
|
|
FURI_LOG_E(
|
|
|
|
TAG, "Update PHY failed, status %d", evt_le_phy_update_complete->Status);
|
|
|
|
} else {
|
|
|
|
FURI_LOG_I(TAG, "Update PHY succeed");
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
ret = hci_le_read_phy(gap->service.connection_handle, &tx_phy, &rx_phy);
|
|
|
|
if(ret) {
|
|
|
|
FURI_LOG_E(TAG, "Read PHY failed, status: %d", ret);
|
|
|
|
} else {
|
|
|
|
FURI_LOG_I(TAG, "PHY Params TX = %d, RX = %d ", tx_phy, rx_phy);
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
break;
|
2021-09-15 16:58:32 +00:00
|
|
|
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
case EVT_LE_CONN_COMPLETE: {
|
2022-03-31 14:57:23 +00:00
|
|
|
hci_le_connection_complete_event_rp0* event =
|
2022-01-05 16:10:18 +00:00
|
|
|
(hci_le_connection_complete_event_rp0*)meta_evt->data;
|
2022-03-31 14:57:23 +00:00
|
|
|
gap->connection_params.conn_interval = event->Conn_Interval;
|
|
|
|
gap->connection_params.slave_latency = event->Conn_Latency;
|
|
|
|
gap->connection_params.supervisor_timeout = event->Supervision_Timeout;
|
2022-01-05 16:10:18 +00:00
|
|
|
|
|
|
|
// Stop advertising as connection completed
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_timer_stop(gap->advertise_timer);
|
2022-01-05 16:10:18 +00:00
|
|
|
|
|
|
|
// Update connection status and handle
|
|
|
|
gap->state = GapStateConnected;
|
2022-03-31 14:57:23 +00:00
|
|
|
gap->service.connection_handle = event->Connection_Handle;
|
2022-01-05 16:10:18 +00:00
|
|
|
|
2022-03-31 14:57:23 +00:00
|
|
|
gap_verify_connection_parameters(gap);
|
2022-01-05 16:10:18 +00:00
|
|
|
// Start pairing by sending security request
|
2022-03-31 14:57:23 +00:00
|
|
|
aci_gap_slave_security_req(event->Connection_Handle);
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
} break;
|
2022-01-05 16:10:18 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-09-15 16:58:32 +00:00
|
|
|
break;
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
case EVT_VENDOR:
|
|
|
|
blue_evt = (evt_blue_aci*)event_pckt->data;
|
|
|
|
switch(blue_evt->ecode) {
|
|
|
|
aci_gap_pairing_complete_event_rp0* pairing_complete;
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
case EVT_BLUE_GAP_LIMITED_DISCOVERABLE:
|
|
|
|
FURI_LOG_I(TAG, "Limited discoverable event");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVT_BLUE_GAP_PASS_KEY_REQUEST: {
|
|
|
|
// Generate random PIN code
|
2022-11-05 15:07:24 +00:00
|
|
|
uint32_t pin = rand() % 999999; //-V1064
|
2022-01-05 16:10:18 +00:00
|
|
|
aci_gap_pass_key_resp(gap->service.connection_handle, pin);
|
2022-02-24 12:38:06 +00:00
|
|
|
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock)) {
|
|
|
|
FURI_LOG_I(TAG, "Pass key request event. Pin: ******");
|
|
|
|
} else {
|
2022-10-07 13:35:15 +00:00
|
|
|
FURI_LOG_I(TAG, "Pass key request event. Pin: %06ld", pin);
|
2022-02-24 12:38:06 +00:00
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
GapEvent event = {.type = GapEventTypePinCodeShow, .data.pin_code = pin};
|
|
|
|
gap->on_event_cb(event, gap->context);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case EVT_BLUE_ATT_EXCHANGE_MTU_RESP: {
|
|
|
|
aci_att_exchange_mtu_resp_event_rp0* pr = (void*)blue_evt->data;
|
|
|
|
FURI_LOG_I(TAG, "Rx MTU size: %d", pr->Server_RX_MTU);
|
|
|
|
// Set maximum packet size given header size is 3 bytes
|
|
|
|
GapEvent event = {
|
|
|
|
.type = GapEventTypeUpdateMTU, .data.max_packet_size = pr->Server_RX_MTU - 3};
|
|
|
|
gap->on_event_cb(event, gap->context);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case EVT_BLUE_GAP_AUTHORIZATION_REQUEST:
|
2022-01-21 17:32:03 +00:00
|
|
|
FURI_LOG_D(TAG, "Authorization request event");
|
2022-01-05 16:10:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EVT_BLUE_GAP_SLAVE_SECURITY_INITIATED:
|
2022-01-21 17:32:03 +00:00
|
|
|
FURI_LOG_D(TAG, "Slave security initiated");
|
2022-01-05 16:10:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EVT_BLUE_GAP_BOND_LOST:
|
2022-01-21 17:32:03 +00:00
|
|
|
FURI_LOG_D(TAG, "Bond lost event. Start rebonding");
|
2022-01-05 16:10:18 +00:00
|
|
|
aci_gap_allow_rebond(gap->service.connection_handle);
|
|
|
|
break;
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
case EVT_BLUE_GAP_DEVICE_FOUND:
|
2022-01-21 17:32:03 +00:00
|
|
|
FURI_LOG_D(TAG, "Device found event");
|
2022-01-05 16:10:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EVT_BLUE_GAP_ADDR_NOT_RESOLVED:
|
2022-01-21 17:32:03 +00:00
|
|
|
FURI_LOG_D(TAG, "Address not resolved event");
|
2022-01-05 16:10:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EVT_BLUE_GAP_KEYPRESS_NOTIFICATION:
|
2022-01-21 17:32:03 +00:00
|
|
|
FURI_LOG_D(TAG, "Key press notification event");
|
2022-01-05 16:10:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EVT_BLUE_GAP_NUMERIC_COMPARISON_VALUE: {
|
|
|
|
uint32_t pin =
|
|
|
|
((aci_gap_numeric_comparison_value_event_rp0*)(blue_evt->data))->Numeric_Value;
|
2022-12-26 12:13:30 +00:00
|
|
|
FURI_LOG_I(TAG, "Verify numeric comparison: %06lu", pin);
|
2022-01-05 16:10:18 +00:00
|
|
|
GapEvent event = {.type = GapEventTypePinCodeVerify, .data.pin_code = pin};
|
|
|
|
bool result = gap->on_event_cb(event, gap->context);
|
|
|
|
aci_gap_numeric_comparison_value_confirm_yesno(gap->service.connection_handle, result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVT_BLUE_GAP_PAIRING_CMPLT:
|
|
|
|
pairing_complete = (aci_gap_pairing_complete_event_rp0*)blue_evt->data;
|
|
|
|
if(pairing_complete->Status) {
|
|
|
|
FURI_LOG_E(
|
|
|
|
TAG,
|
|
|
|
"Pairing failed with status: %d. Terminating connection",
|
|
|
|
pairing_complete->Status);
|
|
|
|
aci_gap_terminate(gap->service.connection_handle, 5);
|
|
|
|
} else {
|
|
|
|
FURI_LOG_I(TAG, "Pairing complete");
|
|
|
|
GapEvent event = {.type = GapEventTypeConnected};
|
2022-12-26 12:13:30 +00:00
|
|
|
gap->on_event_cb(event, gap->context); //-V595
|
2021-09-15 16:58:32 +00:00
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EVT_BLUE_GAP_PROCEDURE_COMPLETE:
|
2022-01-21 17:32:03 +00:00
|
|
|
FURI_LOG_D(TAG, "Procedure complete event");
|
2022-01-05 16:10:18 +00:00
|
|
|
break;
|
2022-01-21 17:32:03 +00:00
|
|
|
|
|
|
|
case EVT_BLUE_L2CAP_CONNECTION_UPDATE_RESP: {
|
|
|
|
uint16_t result =
|
|
|
|
((aci_l2cap_connection_update_resp_event_rp0*)(blue_evt->data))->Result;
|
|
|
|
if(result == 0) {
|
|
|
|
FURI_LOG_D(TAG, "Connection parameters accepted");
|
|
|
|
} else if(result == 1) {
|
|
|
|
FURI_LOG_D(TAG, "Connection parameters denied");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2021-09-15 16:58:32 +00:00
|
|
|
}
|
2022-01-02 22:36:42 +00:00
|
|
|
if(gap) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(gap->state_mutex);
|
2022-01-02 22:36:42 +00:00
|
|
|
}
|
2021-09-15 16:58:32 +00:00
|
|
|
return SVCCTL_UserEvtFlowEnable;
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_advertisment_service_uid(uint8_t* uid, uint8_t uid_len) {
|
|
|
|
if(uid_len == 2) {
|
2021-12-08 11:28:01 +00:00
|
|
|
gap->service.adv_svc_uuid[0] = AD_TYPE_16_BIT_SERV_UUID;
|
2022-01-05 16:10:18 +00:00
|
|
|
} else if(uid_len == 4) {
|
2021-12-08 11:28:01 +00:00
|
|
|
gap->service.adv_svc_uuid[0] = AD_TYPE_32_BIT_SERV_UUID;
|
2021-09-13 11:25:37 +00:00
|
|
|
} else if(uid_len == 16) {
|
2021-12-08 11:28:01 +00:00
|
|
|
gap->service.adv_svc_uuid[0] = AD_TYPE_128_BIT_SERV_UUID_CMPLT_LIST;
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
memcpy(&gap->service.adv_svc_uuid[gap->service.adv_svc_uuid_len], uid, uid_len);
|
|
|
|
gap->service.adv_svc_uuid_len += uid_len;
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gap_init_svc(Gap* gap) {
|
|
|
|
tBleStatus status;
|
|
|
|
uint32_t srd_bd_addr[2];
|
|
|
|
|
2021-10-12 16:41:42 +00:00
|
|
|
// HCI Reset to synchronise BLE Stack
|
2021-09-13 11:25:37 +00:00
|
|
|
hci_reset();
|
|
|
|
// Configure mac address
|
2022-01-05 16:10:18 +00:00
|
|
|
aci_hal_write_config_data(
|
|
|
|
CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, gap->config->mac_address);
|
2021-09-13 11:25:37 +00:00
|
|
|
|
|
|
|
/* Static random Address
|
|
|
|
* The two upper bits shall be set to 1
|
|
|
|
* The lowest 32bits is read from the UDN to differentiate between devices
|
|
|
|
* The RNG may be used to provide a random number on each power on
|
|
|
|
*/
|
|
|
|
srd_bd_addr[1] = 0x0000ED6E;
|
|
|
|
srd_bd_addr[0] = LL_FLASH_GetUDN();
|
2022-01-05 16:10:18 +00:00
|
|
|
aci_hal_write_config_data(
|
|
|
|
CONFIG_DATA_RANDOM_ADDRESS_OFFSET, CONFIG_DATA_RANDOM_ADDRESS_LEN, (uint8_t*)srd_bd_addr);
|
2021-09-13 11:25:37 +00:00
|
|
|
// Set Identity root key used to derive LTK and CSRK
|
2022-01-05 16:10:18 +00:00
|
|
|
aci_hal_write_config_data(CONFIG_DATA_IR_OFFSET, CONFIG_DATA_IR_LEN, (uint8_t*)gap_irk);
|
2021-09-13 11:25:37 +00:00
|
|
|
// Set Encryption root key used to derive LTK and CSRK
|
2022-01-05 16:10:18 +00:00
|
|
|
aci_hal_write_config_data(CONFIG_DATA_ER_OFFSET, CONFIG_DATA_ER_LEN, (uint8_t*)gap_erk);
|
2021-09-13 11:25:37 +00:00
|
|
|
// Set TX Power to 0 dBm
|
|
|
|
aci_hal_set_tx_power_level(1, 0x19);
|
|
|
|
// Initialize GATT interface
|
|
|
|
aci_gatt_init();
|
|
|
|
// Initialize GAP interface
|
2021-12-08 11:28:01 +00:00
|
|
|
// Skip fist symbol AD_TYPE_COMPLETE_LOCAL_NAME
|
2022-01-05 16:10:18 +00:00
|
|
|
char* name = gap->service.adv_name + 1;
|
|
|
|
aci_gap_init(
|
|
|
|
GAP_PERIPHERAL_ROLE,
|
|
|
|
0,
|
|
|
|
strlen(name),
|
|
|
|
&gap->service.gap_svc_handle,
|
|
|
|
&gap->service.dev_name_char_handle,
|
|
|
|
&gap->service.appearance_char_handle);
|
2021-09-13 11:25:37 +00:00
|
|
|
|
|
|
|
// Set GAP characteristics
|
2022-01-05 16:10:18 +00:00
|
|
|
status = aci_gatt_update_char_value(
|
|
|
|
gap->service.gap_svc_handle,
|
|
|
|
gap->service.dev_name_char_handle,
|
|
|
|
0,
|
|
|
|
strlen(name),
|
|
|
|
(uint8_t*)name);
|
|
|
|
if(status) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Failed updating name characteristic: %d", status);
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
uint8_t gap_appearence_char_uuid[2] = {
|
|
|
|
gap->config->appearance_char & 0xff, gap->config->appearance_char >> 8};
|
|
|
|
status = aci_gatt_update_char_value(
|
|
|
|
gap->service.gap_svc_handle,
|
|
|
|
gap->service.appearance_char_handle,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
gap_appearence_char_uuid);
|
2021-09-13 11:25:37 +00:00
|
|
|
if(status) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Failed updating appearence characteristic: %d", status);
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
|
|
|
// Set default PHY
|
|
|
|
hci_le_set_default_phy(ALL_PHYS_PREFERENCE, TX_2M_PREFERRED, RX_2M_PREFERRED);
|
|
|
|
// Set I/O capability
|
2021-12-08 11:28:01 +00:00
|
|
|
bool keypress_supported = false;
|
|
|
|
if(gap->config->pairing_method == GapPairingPinCodeShow) {
|
|
|
|
aci_gap_set_io_capability(IO_CAP_DISPLAY_ONLY);
|
2022-01-05 16:10:18 +00:00
|
|
|
} else if(gap->config->pairing_method == GapPairingPinCodeVerifyYesNo) {
|
2021-12-08 11:28:01 +00:00
|
|
|
aci_gap_set_io_capability(IO_CAP_DISPLAY_YES_NO);
|
|
|
|
keypress_supported = true;
|
|
|
|
}
|
2021-09-13 11:25:37 +00:00
|
|
|
// Setup authentication
|
2021-12-08 11:28:01 +00:00
|
|
|
aci_gap_set_authentication_requirement(
|
|
|
|
gap->config->bonding_mode,
|
|
|
|
CFG_MITM_PROTECTION,
|
|
|
|
CFG_SC_SUPPORT,
|
|
|
|
keypress_supported,
|
|
|
|
CFG_ENCRYPTION_KEY_SIZE_MIN,
|
|
|
|
CFG_ENCRYPTION_KEY_SIZE_MAX,
|
|
|
|
CFG_USED_FIXED_PIN,
|
|
|
|
0,
|
|
|
|
PUBLIC_ADDR);
|
2021-09-13 11:25:37 +00:00
|
|
|
// Configure whitelist
|
|
|
|
aci_gap_configure_whitelist();
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
static void gap_advertise_start(GapState new_state) {
|
2021-09-13 11:25:37 +00:00
|
|
|
tBleStatus status;
|
|
|
|
uint16_t min_interval;
|
|
|
|
uint16_t max_interval;
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(new_state == GapStateAdvFast) {
|
2021-09-13 11:25:37 +00:00
|
|
|
min_interval = 0x80; // 80 ms
|
|
|
|
max_interval = 0xa0; // 100 ms
|
|
|
|
} else {
|
|
|
|
min_interval = 0x0640; // 1 s
|
|
|
|
max_interval = 0x0fa0; // 2.5 s
|
|
|
|
}
|
|
|
|
// Stop advertising timer
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_timer_stop(gap->advertise_timer);
|
2021-09-13 11:25:37 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if((new_state == GapStateAdvLowPower) &&
|
|
|
|
((gap->state == GapStateAdvFast) || (gap->state == GapStateAdvLowPower))) {
|
2021-09-13 11:25:37 +00:00
|
|
|
// Stop advertising
|
|
|
|
status = aci_gap_set_non_discoverable();
|
2022-01-05 16:10:18 +00:00
|
|
|
if(status) {
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
FURI_LOG_E(TAG, "set_non_discoverable failed %d", status);
|
|
|
|
} else {
|
|
|
|
FURI_LOG_D(TAG, "set_non_discoverable success");
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Configure advertising
|
2022-01-05 16:10:18 +00:00
|
|
|
status = aci_gap_set_discoverable(
|
|
|
|
ADV_IND,
|
|
|
|
min_interval,
|
|
|
|
max_interval,
|
|
|
|
PUBLIC_ADDR,
|
|
|
|
0,
|
|
|
|
strlen(gap->service.adv_name),
|
|
|
|
(uint8_t*)gap->service.adv_name,
|
|
|
|
gap->service.adv_svc_uuid_len,
|
|
|
|
gap->service.adv_svc_uuid,
|
|
|
|
0,
|
|
|
|
0);
|
2021-09-13 11:25:37 +00:00
|
|
|
if(status) {
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
FURI_LOG_E(TAG, "set_discoverable failed %d", status);
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
2021-09-15 16:58:32 +00:00
|
|
|
gap->state = new_state;
|
2022-01-02 22:36:42 +00:00
|
|
|
GapEvent event = {.type = GapEventTypeStartAdvertising};
|
2021-10-12 16:41:42 +00:00
|
|
|
gap->on_event_cb(event, gap->context);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_timer_start(gap->advertise_timer, INITIAL_ADV_TIMEOUT);
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 16:58:32 +00:00
|
|
|
static void gap_advertise_stop() {
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
tBleStatus ret;
|
2021-09-15 16:58:32 +00:00
|
|
|
if(gap->state > GapStateIdle) {
|
2021-12-08 11:28:01 +00:00
|
|
|
if(gap->state == GapStateConnected) {
|
|
|
|
// Terminate connection
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
ret = aci_gap_terminate(gap->service.connection_handle, 0x13);
|
|
|
|
if(ret != BLE_STATUS_SUCCESS) {
|
|
|
|
FURI_LOG_E(TAG, "terminate failed %d", ret);
|
|
|
|
} else {
|
|
|
|
FURI_LOG_D(TAG, "terminate success");
|
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
2021-09-15 16:58:32 +00:00
|
|
|
// Stop advertising
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_timer_stop(gap->advertise_timer);
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
ret = aci_gap_set_non_discoverable();
|
|
|
|
if(ret != BLE_STATUS_SUCCESS) {
|
|
|
|
FURI_LOG_E(TAG, "set_non_discoverable failed %d", ret);
|
|
|
|
} else {
|
|
|
|
FURI_LOG_D(TAG, "set_non_discoverable success");
|
|
|
|
}
|
2021-09-15 16:58:32 +00:00
|
|
|
gap->state = GapStateIdle;
|
|
|
|
}
|
2022-01-02 22:36:42 +00:00
|
|
|
GapEvent event = {.type = GapEventTypeStopAdvertising};
|
2021-10-12 16:41:42 +00:00
|
|
|
gap->on_event_cb(event, gap->context);
|
2021-09-15 16:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gap_start_advertising() {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
|
2021-12-08 11:28:01 +00:00
|
|
|
if(gap->state == GapStateIdle) {
|
|
|
|
gap->state = GapStateStartingAdv;
|
|
|
|
FURI_LOG_I(TAG, "Start advertising");
|
|
|
|
gap->enable_adv = true;
|
|
|
|
GapCommand command = GapCommandAdvFast;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(gap->state_mutex);
|
2021-09-15 16:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gap_stop_advertising() {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
|
2021-12-08 11:28:01 +00:00
|
|
|
if(gap->state > GapStateIdle) {
|
|
|
|
FURI_LOG_I(TAG, "Stop advertising");
|
|
|
|
gap->enable_adv = false;
|
|
|
|
GapCommand command = GapCommandAdvStop;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(gap->state_mutex);
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gap_advetise_timer_callback(void* context) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(context);
|
2021-09-15 16:58:32 +00:00
|
|
|
GapCommand command = GapCommandAdvLowPower;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
|
2022-01-05 16:10:18 +00:00
|
|
|
if(!ble_glue_is_radio_stack_ready()) {
|
2021-09-13 11:25:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
gap = malloc(sizeof(Gap));
|
2021-12-08 11:28:01 +00:00
|
|
|
gap->config = config;
|
2021-09-13 11:25:37 +00:00
|
|
|
// Create advertising timer
|
2022-07-20 10:56:33 +00:00
|
|
|
gap->advertise_timer = furi_timer_alloc(gap_advetise_timer_callback, FuriTimerTypeOnce, NULL);
|
2021-09-15 16:58:32 +00:00
|
|
|
// Initialization of GATT & GAP layer
|
2021-12-08 11:28:01 +00:00
|
|
|
gap->service.adv_name = config->adv_name;
|
2021-09-13 11:25:37 +00:00
|
|
|
gap_init_svc(gap);
|
|
|
|
// Initialization of the BLE Services
|
|
|
|
SVCCTL_Init();
|
2021-09-15 16:58:32 +00:00
|
|
|
// Initialization of the GAP state
|
2022-07-20 10:56:33 +00:00
|
|
|
gap->state_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
2021-09-13 11:25:37 +00:00
|
|
|
gap->state = GapStateIdle;
|
2021-12-08 11:28:01 +00:00
|
|
|
gap->service.connection_handle = 0xFFFF;
|
2021-09-15 16:58:32 +00:00
|
|
|
gap->enable_adv = true;
|
2021-09-13 11:25:37 +00:00
|
|
|
|
|
|
|
// Thread configuration
|
2022-11-23 12:49:17 +00:00
|
|
|
gap->thread = furi_thread_alloc_ex("BleGapDriver", 1024, gap_app, gap);
|
2021-12-08 11:28:01 +00:00
|
|
|
furi_thread_start(gap->thread);
|
2021-09-13 11:25:37 +00:00
|
|
|
|
2021-09-15 16:58:32 +00:00
|
|
|
// Command queue allocation
|
2022-07-20 10:56:33 +00:00
|
|
|
gap->command_queue = furi_message_queue_alloc(8, sizeof(GapCommand));
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2021-09-13 11:25:37 +00:00
|
|
|
uint8_t adv_service_uid[2];
|
2021-12-08 11:28:01 +00:00
|
|
|
gap->service.adv_svc_uuid_len = 1;
|
|
|
|
adv_service_uid[0] = gap->config->adv_service_uuid & 0xff;
|
|
|
|
adv_service_uid[1] = gap->config->adv_service_uuid >> 8;
|
2021-09-13 11:25:37 +00:00
|
|
|
set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid));
|
2021-10-12 16:41:42 +00:00
|
|
|
|
|
|
|
// Set callback
|
|
|
|
gap->on_event_cb = on_event_cb;
|
|
|
|
gap->context = context;
|
2021-09-13 11:25:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
GapState gap_get_state() {
|
|
|
|
GapState state;
|
2022-01-02 22:36:42 +00:00
|
|
|
if(gap) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
|
2022-01-02 22:36:42 +00:00
|
|
|
state = gap->state;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(gap->state_mutex);
|
2022-01-02 22:36:42 +00:00
|
|
|
} else {
|
|
|
|
state = GapStateUninitialized;
|
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gap_thread_stop() {
|
|
|
|
if(gap) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
|
2021-12-08 11:28:01 +00:00
|
|
|
gap->enable_adv = false;
|
|
|
|
GapCommand command = GapCommandKillThread;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_message_queue_put(gap->command_queue, &command, FuriWaitForever);
|
|
|
|
furi_mutex_release(gap->state_mutex);
|
2021-12-08 11:28:01 +00:00
|
|
|
furi_thread_join(gap->thread);
|
|
|
|
furi_thread_free(gap->thread);
|
|
|
|
// Free resources
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_free(gap->state_mutex);
|
|
|
|
furi_message_queue_free(gap->command_queue);
|
|
|
|
furi_timer_stop(gap->advertise_timer);
|
|
|
|
while(xTimerIsTimerActive(gap->advertise_timer) == pdTRUE) furi_delay_tick(1);
|
|
|
|
furi_timer_free(gap->advertise_timer);
|
2021-12-08 11:28:01 +00:00
|
|
|
free(gap);
|
|
|
|
gap = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
static int32_t gap_app(void* context) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(context);
|
2021-09-15 16:58:32 +00:00
|
|
|
GapCommand command;
|
2021-09-13 11:25:37 +00:00
|
|
|
while(1) {
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriStatus status = furi_message_queue_get(gap->command_queue, &command, FuriWaitForever);
|
|
|
|
if(status != FuriStatusOk) {
|
2021-12-15 17:39:06 +00:00
|
|
|
FURI_LOG_E(TAG, "Message queue get error: %d", status);
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
|
2021-12-08 11:28:01 +00:00
|
|
|
if(command == GapCommandKillThread) {
|
|
|
|
break;
|
|
|
|
}
|
2021-09-15 16:58:32 +00:00
|
|
|
if(command == GapCommandAdvFast) {
|
|
|
|
gap_advertise_start(GapStateAdvFast);
|
|
|
|
} else if(command == GapCommandAdvLowPower) {
|
|
|
|
gap_advertise_start(GapStateAdvLowPower);
|
|
|
|
} else if(command == GapCommandAdvStop) {
|
|
|
|
gap_advertise_stop();
|
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(gap->state_mutex);
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
|
|
|
|
return 0;
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|