2021-09-15 19:58:32 +03:00
|
|
|
#include "ble_app.h"
|
|
|
|
|
2022-08-22 20:06:17 +03: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 19:58:32 +03:00
|
|
|
#include "gap.h"
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
#include <furi_hal.h>
|
2022-06-20 17:54:48 +03:00
|
|
|
#include <furi.h>
|
2021-09-15 19:58:32 +03:00
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#define TAG "Bt"
|
2021-09-15 19:58:32 +03:00
|
|
|
|
2021-12-08 14:28:01 +03:00
|
|
|
#define BLE_APP_FLAG_HCI_EVENT (1UL << 0)
|
|
|
|
#define BLE_APP_FLAG_KILL_THREAD (1UL << 1)
|
|
|
|
#define BLE_APP_FLAG_ALL (BLE_APP_FLAG_HCI_EVENT | BLE_APP_FLAG_KILL_THREAD)
|
|
|
|
|
2021-09-15 19:58:32 +03:00
|
|
|
PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_CmdPacket_t ble_app_cmd_buffer;
|
2021-11-04 20:26:41 +03:00
|
|
|
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint32_t ble_app_nvm[BLE_NVM_SRAM_SIZE];
|
2021-09-15 19:58:32 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
_Static_assert(
|
|
|
|
sizeof(SHCI_C2_Ble_Init_Cmd_Packet_t) == 49,
|
|
|
|
"Ble stack config structure size mismatch");
|
2022-01-03 01:36:42 +03:00
|
|
|
|
2021-09-15 19:58:32 +03:00
|
|
|
typedef struct {
|
2022-07-20 13:56:33 +03:00
|
|
|
FuriMutex* hci_mtx;
|
|
|
|
FuriSemaphore* hci_sem;
|
2021-12-08 14:28:01 +03:00
|
|
|
FuriThread* thread;
|
2021-09-15 19:58:32 +03:00
|
|
|
} BleApp;
|
|
|
|
|
2021-12-08 14:28:01 +03:00
|
|
|
static BleApp* ble_app = NULL;
|
2021-09-15 19:58:32 +03:00
|
|
|
|
2021-12-08 14:28:01 +03:00
|
|
|
static int32_t ble_app_hci_thread(void* context);
|
|
|
|
static void ble_app_hci_event_handler(void* pPayload);
|
2021-09-15 19:58:32 +03:00
|
|
|
static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status);
|
|
|
|
|
|
|
|
bool ble_app_init() {
|
2021-11-04 20:26:41 +03:00
|
|
|
SHCI_CmdStatus_t status;
|
2022-02-18 22:53:46 +03:00
|
|
|
ble_app = malloc(sizeof(BleApp));
|
2021-09-15 19:58:32 +03:00
|
|
|
// Allocate semafore and mutex for ble command buffer access
|
2022-07-20 13:56:33 +03:00
|
|
|
ble_app->hci_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
|
|
|
|
ble_app->hci_sem = furi_semaphore_alloc(1, 0);
|
2021-09-15 19:58:32 +03:00
|
|
|
// HCI transport layer thread to handle user asynch events
|
2021-12-08 14:28:01 +03:00
|
|
|
ble_app->thread = furi_thread_alloc();
|
2022-02-16 20:52:34 +03:00
|
|
|
furi_thread_set_name(ble_app->thread, "BleHciDriver");
|
2021-12-08 14:28:01 +03:00
|
|
|
furi_thread_set_stack_size(ble_app->thread, 1024);
|
|
|
|
furi_thread_set_context(ble_app->thread, ble_app);
|
|
|
|
furi_thread_set_callback(ble_app->thread, ble_app_hci_thread);
|
|
|
|
furi_thread_start(ble_app->thread);
|
2021-09-15 19:58:32 +03:00
|
|
|
|
|
|
|
// Initialize Ble Transport Layer
|
|
|
|
HCI_TL_HciInitConf_t hci_tl_config = {
|
|
|
|
.p_cmdbuffer = (uint8_t*)&ble_app_cmd_buffer,
|
|
|
|
.StatusNotCallBack = ble_app_hci_status_not_handler,
|
|
|
|
};
|
|
|
|
hci_init(ble_app_hci_event_handler, (void*)&hci_tl_config);
|
|
|
|
|
2021-11-04 20:26:41 +03:00
|
|
|
// Configure NVM store for pairing data
|
|
|
|
SHCI_C2_CONFIG_Cmd_Param_t config_param = {
|
|
|
|
.PayloadCmdSize = SHCI_C2_CONFIG_PAYLOAD_CMD_SIZE,
|
2022-01-05 19:10:18 +03:00
|
|
|
.Config1 = SHCI_C2_CONFIG_CONFIG1_BIT0_BLE_NVM_DATA_TO_SRAM,
|
2021-11-04 20:26:41 +03:00
|
|
|
.BleNvmRamAddress = (uint32_t)ble_app_nvm,
|
|
|
|
.EvtMask1 = SHCI_C2_CONFIG_EVTMASK1_BIT1_BLE_NVM_RAM_UPDATE_ENABLE,
|
|
|
|
};
|
|
|
|
status = SHCI_C2_Config(&config_param);
|
|
|
|
if(status) {
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_E(TAG, "Failed to configure 2nd core: %d", status);
|
2021-11-04 20:26:41 +03:00
|
|
|
}
|
|
|
|
|
2021-09-15 19:58:32 +03:00
|
|
|
// Start ble stack on 2nd core
|
|
|
|
SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = {
|
2022-01-05 19:10:18 +03:00
|
|
|
.Header = {{0, 0, 0}}, // Header unused
|
2021-09-15 19:58:32 +03:00
|
|
|
.Param = {
|
2021-12-23 19:24:09 +03:00
|
|
|
.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,
|
2022-01-05 19:10:18 +03:00
|
|
|
}};
|
2021-11-04 20:26:41 +03:00
|
|
|
status = SHCI_C2_BLE_Init(&ble_init_cmd_packet);
|
2021-09-15 19:58:32 +03:00
|
|
|
if(status) {
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_E(TAG, "Failed to start ble stack: %d", status);
|
2021-09-15 19:58:32 +03:00
|
|
|
}
|
|
|
|
return status == SHCI_Success;
|
|
|
|
}
|
|
|
|
|
2021-11-04 20:26:41 +03: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);
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:28:01 +03:00
|
|
|
void ble_app_thread_stop() {
|
|
|
|
if(ble_app) {
|
2022-06-20 17:54:48 +03:00
|
|
|
FuriThreadId thread_id = furi_thread_get_id(ble_app->thread);
|
2022-03-31 17:57:23 +03:00
|
|
|
furi_assert(thread_id);
|
2022-06-20 17:54:48 +03:00
|
|
|
furi_thread_flags_set(thread_id, BLE_APP_FLAG_KILL_THREAD);
|
2021-12-08 14:28:01 +03:00
|
|
|
furi_thread_join(ble_app->thread);
|
|
|
|
furi_thread_free(ble_app->thread);
|
|
|
|
// Free resources
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_mutex_free(ble_app->hci_mtx);
|
|
|
|
furi_semaphore_free(ble_app->hci_sem);
|
2021-12-08 14:28:01 +03:00
|
|
|
free(ble_app);
|
|
|
|
ble_app = NULL;
|
|
|
|
memset(&ble_app_cmd_buffer, 0, sizeof(ble_app_cmd_buffer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
static int32_t ble_app_hci_thread(void* arg) {
|
2022-05-06 16:37:10 +03:00
|
|
|
UNUSED(arg);
|
2021-12-08 14:28:01 +03:00
|
|
|
uint32_t flags = 0;
|
2022-03-31 17:57:23 +03:00
|
|
|
|
2021-09-15 19:58:32 +03:00
|
|
|
while(1) {
|
2022-07-20 13:56:33 +03:00
|
|
|
flags = furi_thread_flags_wait(BLE_APP_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
|
2021-12-08 14:28:01 +03:00
|
|
|
if(flags & BLE_APP_FLAG_KILL_THREAD) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(flags & BLE_APP_FLAG_HCI_EVENT) {
|
|
|
|
hci_user_evt_proc();
|
|
|
|
}
|
2021-09-15 19:58:32 +03:00
|
|
|
}
|
2021-12-08 14:28:01 +03:00
|
|
|
|
|
|
|
return 0;
|
2021-09-15 19:58:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called by WPAN lib
|
|
|
|
void hci_notify_asynch_evt(void* pdata) {
|
2022-05-06 16:37:10 +03:00
|
|
|
UNUSED(pdata);
|
2021-12-08 14:28:01 +03:00
|
|
|
if(ble_app) {
|
2022-06-20 17:54:48 +03:00
|
|
|
FuriThreadId thread_id = furi_thread_get_id(ble_app->thread);
|
2022-03-31 17:57:23 +03:00
|
|
|
furi_assert(thread_id);
|
2022-06-20 17:54:48 +03:00
|
|
|
furi_thread_flags_set(thread_id, BLE_APP_FLAG_HCI_EVENT);
|
2021-12-08 14:28:01 +03:00
|
|
|
}
|
2021-09-15 19:58:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void hci_cmd_resp_release(uint32_t flag) {
|
2022-05-06 16:37:10 +03:00
|
|
|
UNUSED(flag);
|
2021-12-08 14:28:01 +03:00
|
|
|
if(ble_app) {
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_semaphore_release(ble_app->hci_sem);
|
2021-12-08 14:28:01 +03:00
|
|
|
}
|
2021-09-15 19:58:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void hci_cmd_resp_wait(uint32_t timeout) {
|
2022-05-06 16:37:10 +03:00
|
|
|
UNUSED(timeout);
|
2021-12-08 14:28:01 +03:00
|
|
|
if(ble_app) {
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_semaphore_acquire(ble_app->hci_sem, FuriWaitForever);
|
2021-12-08 14:28:01 +03:00
|
|
|
}
|
2021-09-15 19:58:32 +03:00
|
|
|
}
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
static void ble_app_hci_event_handler(void* pPayload) {
|
2021-09-15 19:58:32 +03:00
|
|
|
SVCCTL_UserEvtFlowStatus_t svctl_return_status;
|
2022-01-05 19:10:18 +03:00
|
|
|
tHCI_UserEvtRxParam* pParam = (tHCI_UserEvtRxParam*)pPayload;
|
2021-09-15 19:58:32 +03:00
|
|
|
|
2021-12-08 14:28:01 +03:00
|
|
|
if(ble_app) {
|
2022-01-05 19:10:18 +03:00
|
|
|
svctl_return_status = SVCCTL_UserEvtRx((void*)&(pParam->pckt->evtserial));
|
|
|
|
if(svctl_return_status != SVCCTL_UserEvtFlowDisable) {
|
2021-12-08 14:28:01 +03:00
|
|
|
pParam->status = HCI_TL_UserEventFlow_Enable;
|
|
|
|
} else {
|
|
|
|
pParam->status = HCI_TL_UserEventFlow_Disable;
|
|
|
|
}
|
2021-09-15 19:58:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status) {
|
2021-09-15 19:58:32 +03:00
|
|
|
if(status == HCI_TL_CmdBusy) {
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_mutex_acquire(ble_app->hci_mtx, FuriWaitForever);
|
2021-09-15 19:58:32 +03:00
|
|
|
} else if(status == HCI_TL_CmdAvailable) {
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_mutex_release(ble_app->hci_mtx);
|
2021-09-15 19:58:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
void SVCCTL_ResumeUserEventFlow(void) {
|
2021-09-15 19:58:32 +03:00
|
|
|
hci_resume_flow();
|
|
|
|
}
|