2021-09-15 16:58:32 +00:00
|
|
|
#include "ble_app.h"
|
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
#include <core/check.h>
|
2022-08-22 17:06:17 +00:00
|
|
|
#include <ble/ble.h>
|
|
|
|
#include <interface/patterns/ble_thread/tl/hci_tl.h>
|
|
|
|
#include <interface/patterns/ble_thread/shci/shci.h>
|
2021-09-15 16:58:32 +00:00
|
|
|
#include "gap.h"
|
2024-02-16 07:20:45 +00:00
|
|
|
#include "furi_ble/event_dispatcher.h"
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2022-06-20 14:54:48 +00:00
|
|
|
#include <furi.h>
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "Bt"
|
2021-09-15 16:58:32 +00:00
|
|
|
|
|
|
|
PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_CmdPacket_t ble_app_cmd_buffer;
|
2021-11-04 17:26:41 +00:00
|
|
|
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint32_t ble_app_nvm[BLE_NVM_SRAM_SIZE];
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
_Static_assert(
|
2023-09-19 14:22:21 +00:00
|
|
|
sizeof(SHCI_C2_Ble_Init_Cmd_Packet_t) == 58,
|
2023-10-09 13:03:27 +00:00
|
|
|
"Ble stack config structure size mismatch (check new config options - last updated for v.1.17.3)");
|
2022-01-02 22:36:42 +00:00
|
|
|
|
2021-09-15 16:58:32 +00:00
|
|
|
typedef struct {
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMutex* hci_mtx;
|
|
|
|
FuriSemaphore* hci_sem;
|
2021-09-15 16:58:32 +00:00
|
|
|
} BleApp;
|
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
static BleApp* ble_app = NULL;
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
static void ble_app_hci_event_handler(void* pPayload);
|
2021-09-15 16:58:32 +00:00
|
|
|
static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status);
|
|
|
|
|
2023-06-08 09:42:02 +00:00
|
|
|
static const HCI_TL_HciInitConf_t hci_tl_config = {
|
|
|
|
.p_cmdbuffer = (uint8_t*)&ble_app_cmd_buffer,
|
|
|
|
.StatusNotCallBack = ble_app_hci_status_not_handler,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SHCI_C2_CONFIG_Cmd_Param_t config_param = {
|
|
|
|
.PayloadCmdSize = SHCI_C2_CONFIG_PAYLOAD_CMD_SIZE,
|
|
|
|
.Config1 = SHCI_C2_CONFIG_CONFIG1_BIT0_BLE_NVM_DATA_TO_SRAM,
|
|
|
|
.BleNvmRamAddress = (uint32_t)ble_app_nvm,
|
|
|
|
.EvtMask1 = SHCI_C2_CONFIG_EVTMASK1_BIT1_BLE_NVM_RAM_UPDATE_ENABLE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = {
|
|
|
|
.Header = {{0, 0, 0}}, // Header unused
|
|
|
|
.Param = {
|
|
|
|
.pBleBufferAddress = 0, // pBleBufferAddress not used
|
|
|
|
.BleBufferSize = 0, // BleBufferSize not used
|
|
|
|
.NumAttrRecord = CFG_BLE_NUM_GATT_ATTRIBUTES,
|
|
|
|
.NumAttrServ = CFG_BLE_NUM_GATT_SERVICES,
|
|
|
|
.AttrValueArrSize = CFG_BLE_ATT_VALUE_ARRAY_SIZE,
|
|
|
|
.NumOfLinks = CFG_BLE_NUM_LINK,
|
|
|
|
.ExtendedPacketLengthEnable = CFG_BLE_DATA_LENGTH_EXTENSION,
|
|
|
|
.PrWriteListSize = CFG_BLE_PREPARE_WRITE_LIST_SIZE,
|
|
|
|
.MblockCount = CFG_BLE_MBLOCK_COUNT,
|
|
|
|
.AttMtu = CFG_BLE_MAX_ATT_MTU,
|
|
|
|
.SlaveSca = CFG_BLE_SLAVE_SCA,
|
|
|
|
.MasterSca = CFG_BLE_MASTER_SCA,
|
|
|
|
.LsSource = CFG_BLE_LSE_SOURCE,
|
|
|
|
.MaxConnEventLength = CFG_BLE_MAX_CONN_EVENT_LENGTH,
|
|
|
|
.HsStartupTime = CFG_BLE_HSE_STARTUP_TIME,
|
|
|
|
.ViterbiEnable = CFG_BLE_VITERBI_MODE,
|
|
|
|
.Options = CFG_BLE_OPTIONS,
|
|
|
|
.HwVersion = 0,
|
|
|
|
.max_coc_initiator_nbr = 32,
|
|
|
|
.min_tx_power = 0,
|
|
|
|
.max_tx_power = 0,
|
|
|
|
.rx_model_config = 1,
|
|
|
|
/* New stack (13.3->15.0) */
|
|
|
|
.max_adv_set_nbr = 1, // Only used if SHCI_C2_BLE_INIT_OPTIONS_EXT_ADV is set
|
2023-09-19 14:22:21 +00:00
|
|
|
.max_adv_data_len = 1650, // Only used if SHCI_C2_BLE_INIT_OPTIONS_EXT_ADV is set
|
2023-06-08 09:42:02 +00:00
|
|
|
.tx_path_compens = 0, // RF TX Path Compensation, * 0.1 dB
|
|
|
|
.rx_path_compens = 0, // RF RX Path Compensation, * 0.1 dB
|
2023-09-19 14:22:21 +00:00
|
|
|
.ble_core_version = SHCI_C2_BLE_INIT_BLE_CORE_5_4,
|
|
|
|
/*15.0->17.0*/
|
|
|
|
.Options_extension = SHCI_C2_BLE_INIT_OPTIONS_ENHANCED_ATT_NOTSUPPORTED |
|
|
|
|
SHCI_C2_BLE_INIT_OPTIONS_APPEARANCE_READONLY,
|
2023-06-08 09:42:02 +00:00
|
|
|
}};
|
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
bool ble_app_init(void) {
|
2021-11-04 17:26:41 +00:00
|
|
|
SHCI_CmdStatus_t status;
|
2022-02-18 19:53:46 +00:00
|
|
|
ble_app = malloc(sizeof(BleApp));
|
2021-09-15 16:58:32 +00:00
|
|
|
// Allocate semafore and mutex for ble command buffer access
|
2022-07-20 10:56:33 +00:00
|
|
|
ble_app->hci_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
|
|
|
|
ble_app->hci_sem = furi_semaphore_alloc(1, 0);
|
2021-09-15 16:58:32 +00:00
|
|
|
|
|
|
|
// Initialize Ble Transport Layer
|
|
|
|
hci_init(ble_app_hci_event_handler, (void*)&hci_tl_config);
|
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
do {
|
|
|
|
// Configure NVM store for pairing data
|
|
|
|
if((status = SHCI_C2_Config((SHCI_C2_CONFIG_Cmd_Param_t*)&config_param))) {
|
|
|
|
FURI_LOG_E(TAG, "Failed to configure 2nd core: %d", status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start ble stack on 2nd core
|
|
|
|
if((status = SHCI_C2_BLE_Init((SHCI_C2_Ble_Init_Cmd_Packet_t*)&ble_init_cmd_packet))) {
|
|
|
|
FURI_LOG_E(TAG, "Failed to start ble stack: %d", status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((status = SHCI_C2_SetFlashActivityControl(FLASH_ACTIVITY_CONTROL_SEM7))) {
|
|
|
|
FURI_LOG_E(TAG, "Failed to set flash activity control: %d", status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(false);
|
2021-11-04 17:26:41 +00:00
|
|
|
|
2021-09-15 16:58:32 +00:00
|
|
|
return status == SHCI_Success;
|
|
|
|
}
|
|
|
|
|
2021-11-04 17:26:41 +00:00
|
|
|
void ble_app_get_key_storage_buff(uint8_t** addr, uint16_t* size) {
|
|
|
|
*addr = (uint8_t*)ble_app_nvm;
|
|
|
|
*size = sizeof(ble_app_nvm);
|
|
|
|
}
|
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
void ble_app_deinit(void) {
|
|
|
|
furi_check(ble_app);
|
2021-12-08 11:28:01 +00:00
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
furi_mutex_free(ble_app->hci_mtx);
|
|
|
|
furi_semaphore_free(ble_app->hci_sem);
|
|
|
|
free(ble_app);
|
|
|
|
ble_app = NULL;
|
|
|
|
memset(&ble_app_cmd_buffer, 0, sizeof(ble_app_cmd_buffer));
|
2021-09-15 16:58:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AN5289, 4.9
|
2021-09-15 16:58:32 +00:00
|
|
|
|
|
|
|
void hci_cmd_resp_release(uint32_t flag) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(flag);
|
2023-04-18 11:38:35 +00:00
|
|
|
furi_check(ble_app);
|
|
|
|
furi_check(furi_semaphore_release(ble_app->hci_sem) == FuriStatusOk);
|
2021-09-15 16:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hci_cmd_resp_wait(uint32_t timeout) {
|
2023-04-18 11:38:35 +00:00
|
|
|
furi_check(ble_app);
|
|
|
|
furi_check(furi_semaphore_acquire(ble_app->hci_sem, timeout) == FuriStatusOk);
|
2021-09-15 16:58:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
static void ble_app_hci_event_handler(void* pPayload) {
|
2024-02-16 07:20:45 +00:00
|
|
|
furi_check(ble_app);
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
tHCI_UserEvtRxParam* pParam = (tHCI_UserEvtRxParam*)pPayload;
|
2024-02-16 07:20:45 +00:00
|
|
|
BleEventFlowStatus event_flow_status =
|
|
|
|
ble_event_dispatcher_process_event((void*)&(pParam->pckt->evtserial));
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
if(event_flow_status != BleEventFlowDisable) {
|
2023-04-18 11:38:35 +00:00
|
|
|
pParam->status = HCI_TL_UserEventFlow_Enable;
|
|
|
|
} else {
|
|
|
|
pParam->status = HCI_TL_UserEventFlow_Disable;
|
2021-09-15 16:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status) {
|
2021-09-15 16:58:32 +00:00
|
|
|
if(status == HCI_TL_CmdBusy) {
|
2023-09-25 05:12:12 +00:00
|
|
|
furi_hal_power_insomnia_enter();
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(ble_app->hci_mtx, FuriWaitForever);
|
2021-09-15 16:58:32 +00:00
|
|
|
} else if(status == HCI_TL_CmdAvailable) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(ble_app->hci_mtx);
|
2023-09-25 05:12:12 +00:00
|
|
|
furi_hal_power_insomnia_exit();
|
2021-09-15 16:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
void SVCCTL_ResumeUserEventFlow(void) {
|
2021-09-15 16:58:32 +00:00
|
|
|
hci_resume_flow();
|
|
|
|
}
|