mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-13 03:08:35 -07:00
Merge branch 'ofw_dev' into blerefactr
This commit is contained in:
@@ -7,6 +7,6 @@
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <core/common_defines.h>
|
||||
#include <tl.h>
|
||||
#include <interface/patterns/ble_thread/tl/tl.h>
|
||||
|
||||
#include "app_conf.h"
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
* Maximum number of simultaneous connections that the device will support.
|
||||
* Valid values are from 1 to 8
|
||||
*/
|
||||
#define CFG_BLE_NUM_LINK 1
|
||||
#define CFG_BLE_NUM_LINK 2
|
||||
|
||||
/**
|
||||
* Maximum number of Services that can be stored in the GATT database.
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
#include "ble_app.h"
|
||||
|
||||
#include <core/check.h>
|
||||
#include <ble/ble.h>
|
||||
#include <interface/patterns/ble_thread/tl/hci_tl.h>
|
||||
#include <interface/patterns/ble_thread/shci/shci.h>
|
||||
#include "gap.h"
|
||||
#include "furi_ble/event_dispatcher.h"
|
||||
|
||||
#include <furi_hal.h>
|
||||
#include <furi.h>
|
||||
|
||||
#define TAG "Bt"
|
||||
|
||||
#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)
|
||||
|
||||
PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_CmdPacket_t ble_app_cmd_buffer;
|
||||
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint32_t ble_app_nvm[BLE_NVM_SRAM_SIZE];
|
||||
|
||||
@@ -24,12 +22,10 @@ _Static_assert(
|
||||
typedef struct {
|
||||
FuriMutex* hci_mtx;
|
||||
FuriSemaphore* hci_sem;
|
||||
FuriThread* thread;
|
||||
} BleApp;
|
||||
|
||||
static BleApp* ble_app = NULL;
|
||||
|
||||
static int32_t ble_app_hci_thread(void* context);
|
||||
static void ble_app_hci_event_handler(void* pPayload);
|
||||
static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status);
|
||||
|
||||
@@ -81,30 +77,35 @@ static const SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = {
|
||||
SHCI_C2_BLE_INIT_OPTIONS_APPEARANCE_READONLY,
|
||||
}};
|
||||
|
||||
bool ble_app_init() {
|
||||
bool ble_app_init(void) {
|
||||
SHCI_CmdStatus_t status;
|
||||
ble_app = malloc(sizeof(BleApp));
|
||||
// Allocate semafore and mutex for ble command buffer access
|
||||
ble_app->hci_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
|
||||
ble_app->hci_sem = furi_semaphore_alloc(1, 0);
|
||||
// HCI transport layer thread to handle user asynch events
|
||||
ble_app->thread = furi_thread_alloc_ex("BleHciDriver", 1024, ble_app_hci_thread, ble_app);
|
||||
furi_thread_start(ble_app->thread);
|
||||
|
||||
// Initialize Ble Transport Layer
|
||||
hci_init(ble_app_hci_event_handler, (void*)&hci_tl_config);
|
||||
|
||||
// Configure NVM store for pairing data
|
||||
status = SHCI_C2_Config((SHCI_C2_CONFIG_Cmd_Param_t*)&config_param);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to configure 2nd core: %d", status);
|
||||
}
|
||||
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);
|
||||
|
||||
// Start ble stack on 2nd core
|
||||
status = SHCI_C2_BLE_Init((SHCI_C2_Ble_Init_Cmd_Packet_t*)&ble_init_cmd_packet);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to start ble stack: %d", status);
|
||||
}
|
||||
return status == SHCI_Success;
|
||||
}
|
||||
|
||||
@@ -113,48 +114,19 @@ void ble_app_get_key_storage_buff(uint8_t** addr, uint16_t* size) {
|
||||
*size = sizeof(ble_app_nvm);
|
||||
}
|
||||
|
||||
void ble_app_thread_stop() {
|
||||
if(ble_app) {
|
||||
FuriThreadId thread_id = furi_thread_get_id(ble_app->thread);
|
||||
furi_assert(thread_id);
|
||||
furi_thread_flags_set(thread_id, BLE_APP_FLAG_KILL_THREAD);
|
||||
furi_thread_join(ble_app->thread);
|
||||
furi_thread_free(ble_app->thread);
|
||||
// Free resources
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t ble_app_hci_thread(void* arg) {
|
||||
UNUSED(arg);
|
||||
uint32_t flags = 0;
|
||||
|
||||
while(1) {
|
||||
flags = furi_thread_flags_wait(BLE_APP_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
|
||||
if(flags & BLE_APP_FLAG_KILL_THREAD) {
|
||||
break;
|
||||
}
|
||||
if(flags & BLE_APP_FLAG_HCI_EVENT) {
|
||||
hci_user_evt_proc();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Called by WPAN lib
|
||||
void hci_notify_asynch_evt(void* pdata) {
|
||||
UNUSED(pdata);
|
||||
void ble_app_deinit(void) {
|
||||
furi_check(ble_app);
|
||||
FuriThreadId thread_id = furi_thread_get_id(ble_app->thread);
|
||||
furi_assert(thread_id);
|
||||
furi_thread_flags_set(thread_id, BLE_APP_FLAG_HCI_EVENT);
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// AN5289, 4.9
|
||||
|
||||
void hci_cmd_resp_release(uint32_t flag) {
|
||||
UNUSED(flag);
|
||||
furi_check(ble_app);
|
||||
@@ -166,13 +138,16 @@ void hci_cmd_resp_wait(uint32_t timeout) {
|
||||
furi_check(furi_semaphore_acquire(ble_app->hci_sem, timeout) == FuriStatusOk);
|
||||
}
|
||||
|
||||
static void ble_app_hci_event_handler(void* pPayload) {
|
||||
SVCCTL_UserEvtFlowStatus_t svctl_return_status;
|
||||
tHCI_UserEvtRxParam* pParam = (tHCI_UserEvtRxParam*)pPayload;
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static void ble_app_hci_event_handler(void* pPayload) {
|
||||
furi_check(ble_app);
|
||||
svctl_return_status = SVCCTL_UserEvtRx((void*)&(pParam->pckt->evtserial));
|
||||
if(svctl_return_status != SVCCTL_UserEvtFlowDisable) {
|
||||
|
||||
tHCI_UserEvtRxParam* pParam = (tHCI_UserEvtRxParam*)pPayload;
|
||||
BleEventFlowStatus event_flow_status =
|
||||
ble_event_dispatcher_process_event((void*)&(pParam->pckt->evtserial));
|
||||
|
||||
if(event_flow_status != BleEventFlowDisable) {
|
||||
pParam->status = HCI_TL_UserEventFlow_Enable;
|
||||
} else {
|
||||
pParam->status = HCI_TL_UserEventFlow_Disable;
|
||||
|
||||
@@ -3,13 +3,19 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
* BLE stack init and cleanup
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool ble_app_init();
|
||||
bool ble_app_init(void);
|
||||
|
||||
void ble_app_get_key_storage_buff(uint8_t** addr, uint16_t* size);
|
||||
void ble_app_thread_stop();
|
||||
|
||||
void ble_app_deinit(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -3,12 +3,9 @@
|
||||
#include "app_conf.h"
|
||||
|
||||
/**
|
||||
* There is one handler per service enabled
|
||||
* Note: There is no handler for the Device Information Service
|
||||
*
|
||||
* This shall take into account all registered handlers
|
||||
* (from either the provided services or the custom services)
|
||||
* We're not using WPAN's event dispatchers
|
||||
* so both client & service max callback count is set to 0.
|
||||
*/
|
||||
#define BLE_CFG_SVC_MAX_NBR_CB 7
|
||||
#define BLE_CFG_SVC_MAX_NBR_CB 0
|
||||
|
||||
#define BLE_CFG_CLT_MAX_NBR_CB 0
|
||||
|
||||
96
targets/f7/ble_glue/ble_event_thread.c
Normal file
96
targets/f7/ble_glue/ble_event_thread.c
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "app_common.h"
|
||||
|
||||
#include <core/check.h>
|
||||
#include <core/thread.h>
|
||||
#include <core/log.h>
|
||||
|
||||
#include <interface/patterns/ble_thread/tl/shci_tl.h>
|
||||
#include <interface/patterns/ble_thread/tl/hci_tl.h>
|
||||
|
||||
#define TAG "BleEvt"
|
||||
|
||||
#define BLE_EVENT_THREAD_FLAG_SHCI_EVENT (1UL << 0)
|
||||
#define BLE_EVENT_THREAD_FLAG_HCI_EVENT (1UL << 1)
|
||||
#define BLE_EVENT_THREAD_FLAG_KILL_THREAD (1UL << 2)
|
||||
|
||||
#define BLE_EVENT_THREAD_FLAG_ALL \
|
||||
(BLE_EVENT_THREAD_FLAG_SHCI_EVENT | BLE_EVENT_THREAD_FLAG_HCI_EVENT | \
|
||||
BLE_EVENT_THREAD_FLAG_KILL_THREAD)
|
||||
|
||||
static FuriThread* event_thread = NULL;
|
||||
|
||||
static int32_t ble_event_thread(void* context) {
|
||||
UNUSED(context);
|
||||
uint32_t flags = 0;
|
||||
|
||||
while((flags & BLE_EVENT_THREAD_FLAG_KILL_THREAD) == 0) {
|
||||
flags =
|
||||
furi_thread_flags_wait(BLE_EVENT_THREAD_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
|
||||
if(flags & BLE_EVENT_THREAD_FLAG_SHCI_EVENT) {
|
||||
#ifdef FURI_BLE_EXTRA_LOG
|
||||
FURI_LOG_W(TAG, "shci_user_evt_proc");
|
||||
#endif
|
||||
shci_user_evt_proc();
|
||||
}
|
||||
if(flags & BLE_EVENT_THREAD_FLAG_HCI_EVENT) {
|
||||
#ifdef FURI_BLE_EXTRA_LOG
|
||||
FURI_LOG_W(TAG, "hci_user_evt_proc");
|
||||
#endif
|
||||
hci_user_evt_proc();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void shci_notify_asynch_evt(void* pdata) {
|
||||
UNUSED(pdata);
|
||||
if(!event_thread) {
|
||||
#ifdef FURI_BLE_EXTRA_LOG
|
||||
FURI_LOG_E(TAG, "shci: event_thread is NULL");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
FuriThreadId thread_id = furi_thread_get_id(event_thread);
|
||||
furi_assert(thread_id);
|
||||
furi_thread_flags_set(thread_id, BLE_EVENT_THREAD_FLAG_SHCI_EVENT);
|
||||
}
|
||||
|
||||
void hci_notify_asynch_evt(void* pdata) {
|
||||
UNUSED(pdata);
|
||||
if(!event_thread) {
|
||||
#ifdef FURI_BLE_EXTRA_LOG
|
||||
FURI_LOG_E(TAG, "hci: event_thread is NULL");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
FuriThreadId thread_id = furi_thread_get_id(event_thread);
|
||||
furi_assert(thread_id);
|
||||
furi_thread_flags_set(thread_id, BLE_EVENT_THREAD_FLAG_HCI_EVENT);
|
||||
}
|
||||
|
||||
void ble_event_thread_stop(void) {
|
||||
if(!event_thread) {
|
||||
#ifdef FURI_BLE_EXTRA_LOG
|
||||
FURI_LOG_E(TAG, "thread_stop: event_thread is NULL");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
FuriThreadId thread_id = furi_thread_get_id(event_thread);
|
||||
furi_assert(thread_id);
|
||||
furi_thread_flags_set(thread_id, BLE_EVENT_THREAD_FLAG_KILL_THREAD);
|
||||
furi_thread_join(event_thread);
|
||||
furi_thread_free(event_thread);
|
||||
event_thread = NULL;
|
||||
}
|
||||
|
||||
void ble_event_thread_start(void) {
|
||||
furi_check(event_thread == NULL);
|
||||
|
||||
event_thread = furi_thread_alloc_ex("BleEventWorker", 1024, ble_event_thread, NULL);
|
||||
furi_thread_set_priority(event_thread, FuriThreadPriorityHigh);
|
||||
furi_thread_start(event_thread);
|
||||
}
|
||||
15
targets/f7/ble_glue/ble_event_thread.h
Normal file
15
targets/f7/ble_glue/ble_event_thread.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Controls for thread handling SHCI & HCI event queues. Used internally. */
|
||||
|
||||
void ble_event_thread_start(void);
|
||||
|
||||
void ble_event_thread_stop(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,6 +1,11 @@
|
||||
#include "ble_glue.h"
|
||||
#include "app_common.h"
|
||||
#include "ble_app.h"
|
||||
#include "ble_event_thread.h"
|
||||
|
||||
#include <furi_hal_cortex.h>
|
||||
#include <core/mutex.h>
|
||||
#include <core/timer.h>
|
||||
#include <ble/ble.h>
|
||||
#include <hci_tl.h>
|
||||
|
||||
@@ -13,26 +18,26 @@
|
||||
|
||||
#define TAG "Core2"
|
||||
|
||||
#define BLE_GLUE_FLAG_SHCI_EVENT (1UL << 0)
|
||||
#define BLE_GLUE_FLAG_KILL_THREAD (1UL << 1)
|
||||
#define BLE_GLUE_FLAG_ALL (BLE_GLUE_FLAG_SHCI_EVENT | BLE_GLUE_FLAG_KILL_THREAD)
|
||||
#define BLE_GLUE_HARDFAULT_CHECK_PERIOD_MS (5000)
|
||||
|
||||
#define BLE_GLUE_HARDFAULT_INFO_MAGIC (0x1170FD0F)
|
||||
|
||||
#define POOL_SIZE \
|
||||
(CFG_TLBLE_EVT_QUEUE_LENGTH * 4U * \
|
||||
DIVC((sizeof(TL_PacketHeader_t) + TL_BLE_EVENT_FRAME_SIZE), 4U))
|
||||
|
||||
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t ble_glue_event_pool[POOL_SIZE];
|
||||
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t ble_glue_system_cmd_buff;
|
||||
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t ble_event_pool[POOL_SIZE];
|
||||
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t ble_glue_cmd_buff;
|
||||
PLACE_IN_SECTION("MB_MEM2")
|
||||
ALIGN(4)
|
||||
static uint8_t ble_glue_system_spare_event_buff[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255U];
|
||||
static uint8_t ble_glue_spare_event_buff[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255U];
|
||||
PLACE_IN_SECTION("MB_MEM2")
|
||||
ALIGN(4)
|
||||
static uint8_t ble_glue_ble_spare_event_buff[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255];
|
||||
static uint8_t ble_spare_event_buff[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255];
|
||||
|
||||
typedef struct {
|
||||
FuriMutex* shci_mtx;
|
||||
FuriThread* thread;
|
||||
FuriTimer* hardfault_check_timer;
|
||||
BleGlueStatus status;
|
||||
BleGlueKeyStorageChangedCallback callback;
|
||||
BleGlueC2Info c2_info;
|
||||
@@ -41,9 +46,10 @@ typedef struct {
|
||||
|
||||
static BleGlue* ble_glue = NULL;
|
||||
|
||||
static int32_t ble_glue_shci_thread(void* argument);
|
||||
static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status);
|
||||
static void ble_glue_sys_user_event_callback(void* pPayload);
|
||||
// static int32_t ble_glue_shci_thread(void* argument);
|
||||
static void ble_sys_status_not_callback(SHCI_TL_CmdStatus_t status);
|
||||
static void ble_sys_user_event_callback(void* pPayload);
|
||||
static void ble_glue_clear_shared_memory();
|
||||
|
||||
void ble_glue_set_key_storage_changed_callback(
|
||||
BleGlueKeyStorageChangedCallback callback,
|
||||
@@ -54,43 +60,21 @@ void ble_glue_set_key_storage_changed_callback(
|
||||
ble_glue->context = context;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/* TL hook to catch hardfaults */
|
||||
|
||||
int32_t ble_glue_TL_SYS_SendCmd(uint8_t* buffer, uint16_t size) {
|
||||
if(furi_hal_bt_get_hardfault_info()) {
|
||||
static void furi_hal_bt_hardfault_check(void* context) {
|
||||
UNUSED(context);
|
||||
if(ble_glue_get_hardfault_info()) {
|
||||
furi_crash("ST(R) Copro(R) HardFault");
|
||||
}
|
||||
|
||||
return TL_SYS_SendCmd(buffer, size);
|
||||
}
|
||||
|
||||
void shci_register_io_bus(tSHciIO* fops) {
|
||||
/* Register IO bus services */
|
||||
fops->Init = TL_SYS_Init;
|
||||
fops->Send = ble_glue_TL_SYS_SendCmd;
|
||||
}
|
||||
|
||||
static int32_t ble_glue_TL_BLE_SendCmd(uint8_t* buffer, uint16_t size) {
|
||||
if(furi_hal_bt_get_hardfault_info()) {
|
||||
furi_crash("ST(R) Copro(R) HardFault");
|
||||
}
|
||||
|
||||
return TL_BLE_SendCmd(buffer, size);
|
||||
}
|
||||
|
||||
void hci_register_io_bus(tHciIO* fops) {
|
||||
/* Register IO bus services */
|
||||
fops->Init = TL_BLE_Init;
|
||||
fops->Send = ble_glue_TL_BLE_SendCmd;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void ble_glue_init() {
|
||||
void ble_glue_init(void) {
|
||||
ble_glue = malloc(sizeof(BleGlue));
|
||||
ble_glue->status = BleGlueStatusStartup;
|
||||
ble_glue->hardfault_check_timer =
|
||||
furi_timer_alloc(furi_hal_bt_hardfault_check, FuriTimerTypePeriodic, NULL);
|
||||
furi_timer_start(ble_glue->hardfault_check_timer, BLE_GLUE_HARDFAULT_CHECK_PERIOD_MS);
|
||||
|
||||
#ifdef BLE_GLUE_DEBUG
|
||||
APPD_Init();
|
||||
@@ -105,18 +89,17 @@ void ble_glue_init() {
|
||||
ble_glue->shci_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
|
||||
|
||||
// FreeRTOS system task creation
|
||||
ble_glue->thread = furi_thread_alloc_ex("BleShciDriver", 1024, ble_glue_shci_thread, ble_glue);
|
||||
furi_thread_start(ble_glue->thread);
|
||||
ble_event_thread_start();
|
||||
|
||||
// System channel initialization
|
||||
SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&ble_glue_system_cmd_buff;
|
||||
SHci_Tl_Init_Conf.StatusNotCallBack = ble_glue_sys_status_not_callback;
|
||||
shci_init(ble_glue_sys_user_event_callback, (void*)&SHci_Tl_Init_Conf);
|
||||
SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&ble_glue_cmd_buff;
|
||||
SHci_Tl_Init_Conf.StatusNotCallBack = ble_sys_status_not_callback;
|
||||
shci_init(ble_sys_user_event_callback, (void*)&SHci_Tl_Init_Conf);
|
||||
|
||||
/**< Memory Manager channel initialization */
|
||||
tl_mm_config.p_BleSpareEvtBuffer = ble_glue_ble_spare_event_buff;
|
||||
tl_mm_config.p_SystemSpareEvtBuffer = ble_glue_system_spare_event_buff;
|
||||
tl_mm_config.p_AsynchEvtPool = ble_glue_event_pool;
|
||||
tl_mm_config.p_BleSpareEvtBuffer = ble_spare_event_buff;
|
||||
tl_mm_config.p_SystemSpareEvtBuffer = ble_glue_spare_event_buff;
|
||||
tl_mm_config.p_AsynchEvtPool = ble_event_pool;
|
||||
tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
|
||||
TL_MM_Init(&tl_mm_config);
|
||||
TL_Enable();
|
||||
@@ -124,15 +107,15 @@ void ble_glue_init() {
|
||||
/*
|
||||
* From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
|
||||
* received on the system channel before starting the Stack
|
||||
* This system event is received with ble_glue_sys_user_event_callback()
|
||||
* This system event is received with ble_sys_user_event_callback()
|
||||
*/
|
||||
}
|
||||
|
||||
const BleGlueC2Info* ble_glue_get_c2_info() {
|
||||
const BleGlueC2Info* ble_glue_get_c2_info(void) {
|
||||
return &ble_glue->c2_info;
|
||||
}
|
||||
|
||||
BleGlueStatus ble_glue_get_c2_status() {
|
||||
BleGlueStatus ble_glue_get_c2_status(void) {
|
||||
return ble_glue->status;
|
||||
}
|
||||
|
||||
@@ -159,7 +142,7 @@ static const char* ble_glue_get_reltype_str(const uint8_t reltype) {
|
||||
}
|
||||
}
|
||||
|
||||
static void ble_glue_update_c2_fw_info() {
|
||||
static void ble_glue_update_c2_fw_info(void) {
|
||||
WirelessFwInfo_t wireless_info;
|
||||
SHCI_GetWirelessFwInfo(&wireless_info);
|
||||
BleGlueC2Info* local_info = &ble_glue->c2_info;
|
||||
@@ -178,7 +161,7 @@ static void ble_glue_update_c2_fw_info() {
|
||||
local_info->StackType = wireless_info.StackType;
|
||||
snprintf(
|
||||
local_info->StackTypeString,
|
||||
BLE_GLUE_MAX_VERSION_STRING_LEN,
|
||||
BLE_MAX_VERSION_STRING_LEN,
|
||||
"%d.%d.%d:%s",
|
||||
local_info->VersionMajor,
|
||||
local_info->VersionMinor,
|
||||
@@ -193,7 +176,7 @@ static void ble_glue_update_c2_fw_info() {
|
||||
local_info->FusMemorySizeFlash = wireless_info.FusMemorySizeFlash;
|
||||
}
|
||||
|
||||
static void ble_glue_dump_stack_info() {
|
||||
static void ble_glue_dump_stack_info(void) {
|
||||
const BleGlueC2Info* c2_info = &ble_glue->c2_info;
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
@@ -216,59 +199,63 @@ static void ble_glue_dump_stack_info() {
|
||||
c2_info->MemorySizeFlash);
|
||||
}
|
||||
|
||||
bool ble_glue_wait_for_c2_start(int32_t timeout) {
|
||||
bool ble_glue_wait_for_c2_start(int32_t timeout_ms) {
|
||||
bool started = false;
|
||||
|
||||
FuriHalCortexTimer timer = furi_hal_cortex_timer_get(timeout_ms * 1000);
|
||||
do {
|
||||
furi_delay_tick(1);
|
||||
started = ble_glue->status == BleGlueStatusC2Started;
|
||||
if(!started) {
|
||||
timeout--;
|
||||
furi_delay_tick(1);
|
||||
}
|
||||
} while(!started && (timeout > 0));
|
||||
} while(!started && !furi_hal_cortex_timer_is_expired(timer));
|
||||
|
||||
if(started) {
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"C2 boot completed, mode: %s",
|
||||
ble_glue->c2_info.mode == BleGlueC2ModeFUS ? "FUS" : "Stack");
|
||||
ble_glue_update_c2_fw_info();
|
||||
ble_glue_dump_stack_info();
|
||||
} else {
|
||||
if(!started) {
|
||||
FURI_LOG_E(TAG, "C2 startup failed");
|
||||
ble_glue->status = BleGlueStatusBroken;
|
||||
return false;
|
||||
}
|
||||
|
||||
return started;
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"C2 boot completed, mode: %s",
|
||||
ble_glue->c2_info.mode == BleGlueC2ModeFUS ? "FUS" : "Stack");
|
||||
ble_glue_update_c2_fw_info();
|
||||
ble_glue_dump_stack_info();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ble_glue_start() {
|
||||
bool ble_glue_start(void) {
|
||||
furi_assert(ble_glue);
|
||||
|
||||
if(ble_glue->status != BleGlueStatusC2Started) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ret = false;
|
||||
if(ble_app_init()) {
|
||||
FURI_LOG_I(TAG, "Radio stack started");
|
||||
ble_glue->status = BleGlueStatusRadioStackRunning;
|
||||
ret = true;
|
||||
if(SHCI_C2_SetFlashActivityControl(FLASH_ACTIVITY_CONTROL_SEM7) == SHCI_Success) {
|
||||
FURI_LOG_I(TAG, "Flash activity control switched to SEM7");
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed to switch flash activity control to SEM7");
|
||||
}
|
||||
} else {
|
||||
if(!ble_app_init()) {
|
||||
FURI_LOG_E(TAG, "Radio stack startup failed");
|
||||
ble_glue->status = BleGlueStatusRadioStackMissing;
|
||||
ble_app_thread_stop();
|
||||
ble_app_deinit();
|
||||
return false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
FURI_LOG_I(TAG, "Radio stack started");
|
||||
ble_glue->status = BleGlueStatusRadioStackRunning;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ble_glue_is_alive() {
|
||||
void ble_glue_stop(void) {
|
||||
furi_assert(ble_glue);
|
||||
|
||||
ble_event_thread_stop();
|
||||
// Free resources
|
||||
furi_mutex_free(ble_glue->shci_mtx);
|
||||
furi_timer_free(ble_glue->hardfault_check_timer);
|
||||
|
||||
ble_glue_clear_shared_memory();
|
||||
free(ble_glue);
|
||||
ble_glue = NULL;
|
||||
}
|
||||
|
||||
bool ble_glue_is_alive(void) {
|
||||
if(!ble_glue) {
|
||||
return false;
|
||||
}
|
||||
@@ -276,7 +263,7 @@ bool ble_glue_is_alive() {
|
||||
return ble_glue->status >= BleGlueStatusC2Started;
|
||||
}
|
||||
|
||||
bool ble_glue_is_radio_stack_ready() {
|
||||
bool ble_glue_is_radio_stack_ready(void) {
|
||||
if(!ble_glue) {
|
||||
return false;
|
||||
}
|
||||
@@ -319,7 +306,7 @@ BleGlueCommandResult ble_glue_force_c2_mode(BleGlueC2Mode desired_mode) {
|
||||
return BleGlueCommandResultError;
|
||||
}
|
||||
|
||||
static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
|
||||
static void ble_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
|
||||
switch(status) {
|
||||
case SHCI_TL_CmdBusy:
|
||||
furi_mutex_acquire(ble_glue->shci_mtx, FuriWaitForever);
|
||||
@@ -341,7 +328,7 @@ static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
|
||||
* ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
|
||||
* When the status is not filled, the buffer is released by default
|
||||
*/
|
||||
static void ble_glue_sys_user_event_callback(void* pPayload) {
|
||||
static void ble_sys_user_event_callback(void* pPayload) {
|
||||
UNUSED(pPayload);
|
||||
|
||||
#ifdef BLE_GLUE_DEBUG
|
||||
@@ -375,60 +362,18 @@ static void ble_glue_sys_user_event_callback(void* pPayload) {
|
||||
}
|
||||
}
|
||||
|
||||
static void ble_glue_clear_shared_memory() {
|
||||
memset(ble_glue_event_pool, 0, sizeof(ble_glue_event_pool));
|
||||
memset(&ble_glue_system_cmd_buff, 0, sizeof(ble_glue_system_cmd_buff));
|
||||
memset(ble_glue_system_spare_event_buff, 0, sizeof(ble_glue_system_spare_event_buff));
|
||||
memset(ble_glue_ble_spare_event_buff, 0, sizeof(ble_glue_ble_spare_event_buff));
|
||||
static void ble_glue_clear_shared_memory(void) {
|
||||
memset(ble_event_pool, 0, sizeof(ble_event_pool));
|
||||
memset(&ble_glue_cmd_buff, 0, sizeof(ble_glue_cmd_buff));
|
||||
memset(ble_glue_spare_event_buff, 0, sizeof(ble_glue_spare_event_buff));
|
||||
memset(ble_spare_event_buff, 0, sizeof(ble_spare_event_buff));
|
||||
}
|
||||
|
||||
void ble_glue_thread_stop() {
|
||||
if(ble_glue) {
|
||||
FuriThreadId thread_id = furi_thread_get_id(ble_glue->thread);
|
||||
furi_assert(thread_id);
|
||||
furi_thread_flags_set(thread_id, BLE_GLUE_FLAG_KILL_THREAD);
|
||||
furi_thread_join(ble_glue->thread);
|
||||
furi_thread_free(ble_glue->thread);
|
||||
// Free resources
|
||||
furi_mutex_free(ble_glue->shci_mtx);
|
||||
ble_glue_clear_shared_memory();
|
||||
free(ble_glue);
|
||||
ble_glue = NULL;
|
||||
}
|
||||
bool ble_glue_reinit_c2(void) {
|
||||
return (SHCI_C2_Reinit() == SHCI_Success);
|
||||
}
|
||||
|
||||
// Wrap functions
|
||||
static int32_t ble_glue_shci_thread(void* context) {
|
||||
UNUSED(context);
|
||||
uint32_t flags = 0;
|
||||
|
||||
while(true) {
|
||||
flags = furi_thread_flags_wait(BLE_GLUE_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
|
||||
if(flags & BLE_GLUE_FLAG_SHCI_EVENT) {
|
||||
shci_user_evt_proc();
|
||||
}
|
||||
if(flags & BLE_GLUE_FLAG_KILL_THREAD) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void shci_notify_asynch_evt(void* pdata) {
|
||||
UNUSED(pdata);
|
||||
if(ble_glue) {
|
||||
FuriThreadId thread_id = furi_thread_get_id(ble_glue->thread);
|
||||
furi_assert(thread_id);
|
||||
furi_thread_flags_set(thread_id, BLE_GLUE_FLAG_SHCI_EVENT);
|
||||
}
|
||||
}
|
||||
|
||||
bool ble_glue_reinit_c2() {
|
||||
return SHCI_C2_Reinit() == SHCI_Success;
|
||||
}
|
||||
|
||||
BleGlueCommandResult ble_glue_fus_stack_delete() {
|
||||
BleGlueCommandResult ble_glue_fus_stack_delete(void) {
|
||||
FURI_LOG_I(TAG, "Erasing stack");
|
||||
SHCI_CmdStatus_t erase_stat = SHCI_C2_FUS_FwDelete();
|
||||
FURI_LOG_I(TAG, "Cmd res = %x", erase_stat);
|
||||
@@ -450,8 +395,9 @@ BleGlueCommandResult ble_glue_fus_stack_install(uint32_t src_addr, uint32_t dst_
|
||||
return BleGlueCommandResultError;
|
||||
}
|
||||
|
||||
BleGlueCommandResult ble_glue_fus_get_status() {
|
||||
BleGlueCommandResult ble_glue_fus_get_status(void) {
|
||||
furi_check(ble_glue->c2_info.mode == BleGlueC2ModeFUS);
|
||||
|
||||
SHCI_FUS_GetState_ErrorCode_t error_code = 0;
|
||||
uint8_t fus_state = SHCI_C2_FUS_GetState(&error_code);
|
||||
FURI_LOG_I(TAG, "FUS state: %x, error: %x", fus_state, error_code);
|
||||
@@ -465,7 +411,7 @@ BleGlueCommandResult ble_glue_fus_get_status() {
|
||||
return BleGlueCommandResultOK;
|
||||
}
|
||||
|
||||
BleGlueCommandResult ble_glue_fus_wait_operation() {
|
||||
BleGlueCommandResult ble_glue_fus_wait_operation(void) {
|
||||
furi_check(ble_glue->c2_info.mode == BleGlueC2ModeFUS);
|
||||
|
||||
while(true) {
|
||||
@@ -479,3 +425,12 @@ BleGlueCommandResult ble_glue_fus_wait_operation() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const BleGlueHardfaultInfo* ble_glue_get_hardfault_info(void) {
|
||||
/* AN5289, 4.8.2 */
|
||||
const BleGlueHardfaultInfo* info = (BleGlueHardfaultInfo*)(SRAM2A_BASE);
|
||||
if(info->magic != BLE_GLUE_HARDFAULT_INFO_MAGIC) {
|
||||
return NULL;
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
@@ -7,13 +7,17 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Low-level interface to Core2 - startup, shutdown, mode switching, FUS commands.
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
BleGlueC2ModeUnknown = 0,
|
||||
BleGlueC2ModeFUS,
|
||||
BleGlueC2ModeStack,
|
||||
} BleGlueC2Mode;
|
||||
|
||||
#define BLE_GLUE_MAX_VERSION_STRING_LEN 20
|
||||
#define BLE_MAX_VERSION_STRING_LEN (20)
|
||||
typedef struct {
|
||||
BleGlueC2Mode mode;
|
||||
/**
|
||||
@@ -29,7 +33,7 @@ typedef struct {
|
||||
uint8_t MemorySizeSram1; /*< Multiple of 1K */
|
||||
uint8_t MemorySizeFlash; /*< Multiple of 4K */
|
||||
uint8_t StackType;
|
||||
char StackTypeString[BLE_GLUE_MAX_VERSION_STRING_LEN];
|
||||
char StackTypeString[BLE_MAX_VERSION_STRING_LEN];
|
||||
/**
|
||||
* Fus Info
|
||||
*/
|
||||
@@ -55,35 +59,37 @@ typedef void (
|
||||
*BleGlueKeyStorageChangedCallback)(uint8_t* change_addr_start, uint16_t size, void* context);
|
||||
|
||||
/** Initialize start core2 and initialize transport */
|
||||
void ble_glue_init();
|
||||
void ble_glue_init(void);
|
||||
|
||||
/** Start Core2 Radio stack
|
||||
*
|
||||
* @return true on success
|
||||
*/
|
||||
bool ble_glue_start();
|
||||
bool ble_glue_start(void);
|
||||
|
||||
void ble_glue_stop(void);
|
||||
|
||||
/** Is core2 alive and at least FUS is running
|
||||
*
|
||||
* @return true if core2 is alive
|
||||
*/
|
||||
bool ble_glue_is_alive();
|
||||
bool ble_glue_is_alive(void);
|
||||
|
||||
/** Waits for C2 to reports its mode to callback
|
||||
*
|
||||
* @return true if it reported before reaching timeout
|
||||
*/
|
||||
bool ble_glue_wait_for_c2_start(int32_t timeout);
|
||||
bool ble_glue_wait_for_c2_start(int32_t timeout_ms);
|
||||
|
||||
BleGlueStatus ble_glue_get_c2_status();
|
||||
BleGlueStatus ble_glue_get_c2_status(void);
|
||||
|
||||
const BleGlueC2Info* ble_glue_get_c2_info();
|
||||
const BleGlueC2Info* ble_glue_get_c2_info(void);
|
||||
|
||||
/** Is core2 radio stack present and ready
|
||||
*
|
||||
* @return true if present and ready
|
||||
*/
|
||||
bool ble_glue_is_radio_stack_ready();
|
||||
bool ble_glue_is_radio_stack_ready(void);
|
||||
|
||||
/** Set callback for NVM in RAM changes
|
||||
*
|
||||
@@ -94,9 +100,6 @@ void ble_glue_set_key_storage_changed_callback(
|
||||
BleGlueKeyStorageChangedCallback callback,
|
||||
void* context);
|
||||
|
||||
/** Stop SHCI thread */
|
||||
void ble_glue_thread_stop();
|
||||
|
||||
bool ble_glue_reinit_c2();
|
||||
|
||||
typedef enum {
|
||||
@@ -113,13 +116,26 @@ typedef enum {
|
||||
*/
|
||||
BleGlueCommandResult ble_glue_force_c2_mode(BleGlueC2Mode mode);
|
||||
|
||||
BleGlueCommandResult ble_glue_fus_stack_delete();
|
||||
BleGlueCommandResult ble_glue_fus_stack_delete(void);
|
||||
|
||||
BleGlueCommandResult ble_glue_fus_stack_install(uint32_t src_addr, uint32_t dst_addr);
|
||||
|
||||
BleGlueCommandResult ble_glue_fus_get_status();
|
||||
BleGlueCommandResult ble_glue_fus_get_status(void);
|
||||
|
||||
BleGlueCommandResult ble_glue_fus_wait_operation();
|
||||
BleGlueCommandResult ble_glue_fus_wait_operation(void);
|
||||
|
||||
typedef struct {
|
||||
uint32_t magic;
|
||||
uint32_t source_pc;
|
||||
uint32_t source_lr;
|
||||
uint32_t source_sp;
|
||||
} BleGlueHardfaultInfo;
|
||||
|
||||
/** Get hardfault info
|
||||
*
|
||||
* @return hardfault info. NULL if no hardfault
|
||||
*/
|
||||
const BleGlueHardfaultInfo* ble_glue_get_hardfault_info(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
40
targets/f7/ble_glue/ble_tl_hooks.c
Normal file
40
targets/f7/ble_glue/ble_tl_hooks.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "ble_glue.h"
|
||||
|
||||
#include <interface/patterns/ble_thread/tl/shci_tl.h>
|
||||
#include <ble/ble.h>
|
||||
#include <hci_tl.h>
|
||||
#include <furi.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
* TL hooks to catch hardfaults
|
||||
*/
|
||||
|
||||
int32_t ble_glue_TL_SYS_SendCmd(uint8_t* buffer, uint16_t size) {
|
||||
if(ble_glue_get_hardfault_info()) {
|
||||
furi_crash("ST(R) Copro(R) HardFault");
|
||||
}
|
||||
|
||||
return TL_SYS_SendCmd(buffer, size);
|
||||
}
|
||||
|
||||
void shci_register_io_bus(tSHciIO* fops) {
|
||||
/* Register IO bus services */
|
||||
fops->Init = TL_SYS_Init;
|
||||
fops->Send = ble_glue_TL_SYS_SendCmd;
|
||||
}
|
||||
|
||||
static int32_t ble_glue_TL_BLE_SendCmd(uint8_t* buffer, uint16_t size) {
|
||||
if(ble_glue_get_hardfault_info()) {
|
||||
furi_crash("ST(R) Copro(R) HardFault");
|
||||
}
|
||||
|
||||
return TL_BLE_SendCmd(buffer, size);
|
||||
}
|
||||
|
||||
void hci_register_io_bus(tHciIO* fops) {
|
||||
/* Register IO bus services */
|
||||
fops->Init = TL_BLE_Init;
|
||||
fops->Send = ble_glue_TL_BLE_SendCmd;
|
||||
}
|
||||
161
targets/f7/ble_glue/extra_beacon.c
Normal file
161
targets/f7/ble_glue/extra_beacon.c
Normal file
@@ -0,0 +1,161 @@
|
||||
#include "extra_beacon.h"
|
||||
#include "gap.h"
|
||||
|
||||
#include <ble/ble.h>
|
||||
#include <furi.h>
|
||||
|
||||
#define TAG "BleExtraBeacon"
|
||||
|
||||
#define GAP_MS_TO_SCAN_INTERVAL(x) ((uint16_t)((x) / 0.625))
|
||||
|
||||
// Also used as an indicator of whether the beacon had ever been configured
|
||||
#define GAP_MIN_ADV_INTERVAL_MS (20)
|
||||
|
||||
typedef struct {
|
||||
GapExtraBeaconConfig last_config;
|
||||
GapExtraBeaconState extra_beacon_state;
|
||||
uint8_t extra_beacon_data[EXTRA_BEACON_MAX_DATA_SIZE];
|
||||
uint8_t extra_beacon_data_len;
|
||||
FuriMutex* state_mutex;
|
||||
} ExtraBeacon;
|
||||
|
||||
static ExtraBeacon extra_beacon = {0};
|
||||
|
||||
void gap_extra_beacon_init() {
|
||||
if(extra_beacon.state_mutex) {
|
||||
// Already initialized - restore state if needed
|
||||
FURI_LOG_I(TAG, "Restoring state");
|
||||
gap_extra_beacon_set_data(
|
||||
extra_beacon.extra_beacon_data, extra_beacon.extra_beacon_data_len);
|
||||
if(extra_beacon.extra_beacon_state == GapExtraBeaconStateStarted) {
|
||||
extra_beacon.extra_beacon_state = GapExtraBeaconStateStopped;
|
||||
gap_extra_beacon_set_config(&extra_beacon.last_config);
|
||||
}
|
||||
|
||||
} else {
|
||||
// First time init
|
||||
FURI_LOG_I(TAG, "Init");
|
||||
extra_beacon.extra_beacon_state = GapExtraBeaconStateStopped;
|
||||
extra_beacon.extra_beacon_data_len = 0;
|
||||
memset(extra_beacon.extra_beacon_data, 0, EXTRA_BEACON_MAX_DATA_SIZE);
|
||||
extra_beacon.state_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
||||
}
|
||||
}
|
||||
|
||||
bool gap_extra_beacon_set_config(const GapExtraBeaconConfig* config) {
|
||||
furi_check(extra_beacon.state_mutex);
|
||||
furi_check(config);
|
||||
|
||||
furi_check(config->min_adv_interval_ms <= config->max_adv_interval_ms);
|
||||
furi_check(config->min_adv_interval_ms >= GAP_MIN_ADV_INTERVAL_MS);
|
||||
|
||||
if(extra_beacon.extra_beacon_state != GapExtraBeaconStateStopped) {
|
||||
return false;
|
||||
}
|
||||
|
||||
furi_mutex_acquire(extra_beacon.state_mutex, FuriWaitForever);
|
||||
if(config != &extra_beacon.last_config) {
|
||||
memcpy(&extra_beacon.last_config, config, sizeof(GapExtraBeaconConfig));
|
||||
}
|
||||
furi_mutex_release(extra_beacon.state_mutex);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool gap_extra_beacon_start() {
|
||||
furi_check(extra_beacon.state_mutex);
|
||||
furi_check(extra_beacon.last_config.min_adv_interval_ms >= GAP_MIN_ADV_INTERVAL_MS);
|
||||
|
||||
if(extra_beacon.extra_beacon_state != GapExtraBeaconStateStopped) {
|
||||
return false;
|
||||
}
|
||||
|
||||
FURI_LOG_I(TAG, "Starting");
|
||||
furi_mutex_acquire(extra_beacon.state_mutex, FuriWaitForever);
|
||||
const GapExtraBeaconConfig* config = &extra_beacon.last_config;
|
||||
tBleStatus status = aci_gap_additional_beacon_start(
|
||||
GAP_MS_TO_SCAN_INTERVAL(config->min_adv_interval_ms),
|
||||
GAP_MS_TO_SCAN_INTERVAL(config->max_adv_interval_ms),
|
||||
(uint8_t)config->adv_channel_map,
|
||||
config->address_type,
|
||||
config->address,
|
||||
(uint8_t)config->adv_power_level);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to start: 0x%x", status);
|
||||
return false;
|
||||
}
|
||||
extra_beacon.extra_beacon_state = GapExtraBeaconStateStarted;
|
||||
gap_emit_ble_beacon_status_event(true);
|
||||
furi_mutex_release(extra_beacon.state_mutex);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool gap_extra_beacon_stop() {
|
||||
furi_check(extra_beacon.state_mutex);
|
||||
|
||||
if(extra_beacon.extra_beacon_state != GapExtraBeaconStateStarted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
FURI_LOG_I(TAG, "Stopping");
|
||||
furi_mutex_acquire(extra_beacon.state_mutex, FuriWaitForever);
|
||||
tBleStatus status = aci_gap_additional_beacon_stop();
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to stop: 0x%x", status);
|
||||
return false;
|
||||
}
|
||||
extra_beacon.extra_beacon_state = GapExtraBeaconStateStopped;
|
||||
gap_emit_ble_beacon_status_event(false);
|
||||
furi_mutex_release(extra_beacon.state_mutex);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool gap_extra_beacon_set_data(const uint8_t* data, uint8_t length) {
|
||||
furi_check(extra_beacon.state_mutex);
|
||||
furi_check(data);
|
||||
furi_check(length <= EXTRA_BEACON_MAX_DATA_SIZE);
|
||||
|
||||
furi_mutex_acquire(extra_beacon.state_mutex, FuriWaitForever);
|
||||
if(data != extra_beacon.extra_beacon_data) {
|
||||
memcpy(extra_beacon.extra_beacon_data, data, length);
|
||||
}
|
||||
extra_beacon.extra_beacon_data_len = length;
|
||||
|
||||
tBleStatus status = aci_gap_additional_beacon_set_data(length, data);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed updating adv data: %d", status);
|
||||
return false;
|
||||
}
|
||||
furi_mutex_release(extra_beacon.state_mutex);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t gap_extra_beacon_get_data(uint8_t* data) {
|
||||
furi_check(extra_beacon.state_mutex);
|
||||
furi_check(data);
|
||||
|
||||
furi_mutex_acquire(extra_beacon.state_mutex, FuriWaitForever);
|
||||
memcpy(data, extra_beacon.extra_beacon_data, extra_beacon.extra_beacon_data_len);
|
||||
furi_mutex_release(extra_beacon.state_mutex);
|
||||
|
||||
return extra_beacon.extra_beacon_data_len;
|
||||
}
|
||||
|
||||
GapExtraBeaconState gap_extra_beacon_get_state() {
|
||||
furi_check(extra_beacon.state_mutex);
|
||||
|
||||
return extra_beacon.extra_beacon_state;
|
||||
}
|
||||
|
||||
const GapExtraBeaconConfig* gap_extra_beacon_get_config() {
|
||||
furi_check(extra_beacon.state_mutex);
|
||||
|
||||
if(extra_beacon.last_config.min_adv_interval_ms < GAP_MIN_ADV_INTERVAL_MS) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &extra_beacon.last_config;
|
||||
}
|
||||
98
targets/f7/ble_glue/extra_beacon.h
Normal file
98
targets/f7/ble_glue/extra_beacon.h
Normal file
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Additinal non-connetable beacon API.
|
||||
* Not to be used directly, but through furi_hal_bt_extra_beacon_* APIs.
|
||||
*/
|
||||
|
||||
#define EXTRA_BEACON_MAX_DATA_SIZE (31)
|
||||
#define EXTRA_BEACON_MAC_ADDR_SIZE (6)
|
||||
|
||||
typedef enum {
|
||||
GapAdvChannelMap37 = 0b001,
|
||||
GapAdvChannelMap38 = 0b010,
|
||||
GapAdvChannelMap39 = 0b100,
|
||||
GapAdvChannelMapAll = 0b111,
|
||||
} GapAdvChannelMap;
|
||||
|
||||
typedef enum {
|
||||
GapAdvPowerLevel_Neg40dBm = 0x00,
|
||||
GapAdvPowerLevel_Neg20_85dBm = 0x01,
|
||||
GapAdvPowerLevel_Neg19_75dBm = 0x02,
|
||||
GapAdvPowerLevel_Neg18_85dBm = 0x03,
|
||||
GapAdvPowerLevel_Neg17_6dBm = 0x04,
|
||||
GapAdvPowerLevel_Neg16_5dBm = 0x05,
|
||||
GapAdvPowerLevel_Neg15_25dBm = 0x06,
|
||||
GapAdvPowerLevel_Neg14_1dBm = 0x07,
|
||||
GapAdvPowerLevel_Neg13_15dBm = 0x08,
|
||||
GapAdvPowerLevel_Neg12_05dBm = 0x09,
|
||||
GapAdvPowerLevel_Neg10_9dBm = 0x0A,
|
||||
GapAdvPowerLevel_Neg9_9dBm = 0x0B,
|
||||
GapAdvPowerLevel_Neg8_85dBm = 0x0C,
|
||||
GapAdvPowerLevel_Neg7_8dBm = 0x0D,
|
||||
GapAdvPowerLevel_Neg6_9dBm = 0x0E,
|
||||
GapAdvPowerLevel_Neg5_9dBm = 0x0F,
|
||||
GapAdvPowerLevel_Neg4_95dBm = 0x10,
|
||||
GapAdvPowerLevel_Neg4dBm = 0x11,
|
||||
GapAdvPowerLevel_Neg3_15dBm = 0x12,
|
||||
GapAdvPowerLevel_Neg2_45dBm = 0x13,
|
||||
GapAdvPowerLevel_Neg1_8dBm = 0x14,
|
||||
GapAdvPowerLevel_Neg1_3dBm = 0x15,
|
||||
GapAdvPowerLevel_Neg0_85dBm = 0x16,
|
||||
GapAdvPowerLevel_Neg0_5dBm = 0x17,
|
||||
GapAdvPowerLevel_Neg0_15dBm = 0x18,
|
||||
GapAdvPowerLevel_0dBm = 0x19,
|
||||
GapAdvPowerLevel_1dBm = 0x1A,
|
||||
GapAdvPowerLevel_2dBm = 0x1B,
|
||||
GapAdvPowerLevel_3dBm = 0x1C,
|
||||
GapAdvPowerLevel_4dBm = 0x1D,
|
||||
GapAdvPowerLevel_5dBm = 0x1E,
|
||||
GapAdvPowerLevel_6dBm = 0x1F,
|
||||
} GapAdvPowerLevelInd;
|
||||
|
||||
typedef enum {
|
||||
GapAddressTypePublic = 0,
|
||||
GapAddressTypeRandom = 1,
|
||||
} GapAddressType;
|
||||
|
||||
typedef struct {
|
||||
uint16_t min_adv_interval_ms, max_adv_interval_ms;
|
||||
GapAdvChannelMap adv_channel_map;
|
||||
GapAdvPowerLevelInd adv_power_level;
|
||||
GapAddressType address_type;
|
||||
uint8_t address[EXTRA_BEACON_MAC_ADDR_SIZE];
|
||||
} GapExtraBeaconConfig;
|
||||
|
||||
typedef enum {
|
||||
GapExtraBeaconStateUndefined = 0,
|
||||
GapExtraBeaconStateStopped,
|
||||
GapExtraBeaconStateStarted,
|
||||
} GapExtraBeaconState;
|
||||
|
||||
void gap_extra_beacon_init();
|
||||
|
||||
GapExtraBeaconState gap_extra_beacon_get_state();
|
||||
|
||||
bool gap_extra_beacon_start();
|
||||
|
||||
bool gap_extra_beacon_stop();
|
||||
|
||||
bool gap_extra_beacon_set_config(const GapExtraBeaconConfig* config);
|
||||
|
||||
const GapExtraBeaconConfig* gap_extra_beacon_get_config();
|
||||
|
||||
bool gap_extra_beacon_set_data(const uint8_t* data, uint8_t length);
|
||||
|
||||
// Fill "data" with last configured extra beacon data and return its length
|
||||
uint8_t gap_extra_beacon_get_data(uint8_t* data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
97
targets/f7/ble_glue/furi_ble/event_dispatcher.c
Normal file
97
targets/f7/ble_glue/furi_ble/event_dispatcher.c
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "event_dispatcher.h"
|
||||
#include <core/check.h>
|
||||
#include <furi.h>
|
||||
#include <ble/ble.h>
|
||||
|
||||
#include <m-list.h>
|
||||
|
||||
struct GapEventHandler {
|
||||
void* context;
|
||||
BleSvcEventHandlerCb callback;
|
||||
};
|
||||
|
||||
LIST_DEF(GapSvcEventHandlerList, GapSvcEventHandler, M_POD_OPLIST);
|
||||
|
||||
static GapSvcEventHandlerList_t handlers;
|
||||
static bool initialized = false;
|
||||
|
||||
BleEventFlowStatus ble_event_dispatcher_process_event(void* payload) {
|
||||
furi_check(initialized);
|
||||
|
||||
GapSvcEventHandlerList_it_t it;
|
||||
BleEventAckStatus ack_status = BleEventNotAck;
|
||||
|
||||
for(GapSvcEventHandlerList_it(it, handlers); !GapSvcEventHandlerList_end_p(it);
|
||||
GapSvcEventHandlerList_next(it)) {
|
||||
const GapSvcEventHandler* item = GapSvcEventHandlerList_cref(it);
|
||||
ack_status = item->callback(payload, item->context);
|
||||
if(ack_status == BleEventNotAck) {
|
||||
/* Keep going */
|
||||
continue;
|
||||
} else if((ack_status == BleEventAckFlowEnable) || (ack_status == BleEventAckFlowDisable)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Handlers for client-mode events are also to be implemented here. But not today. */
|
||||
|
||||
/* Now, decide on a flow control action based on results of all handlers */
|
||||
switch(ack_status) {
|
||||
case BleEventNotAck:
|
||||
/* The event has NOT been managed yet. Pass to app for processing */
|
||||
return ble_event_app_notification(payload);
|
||||
case BleEventAckFlowEnable:
|
||||
return BleEventFlowEnable;
|
||||
case BleEventAckFlowDisable:
|
||||
return BleEventFlowDisable;
|
||||
default:
|
||||
return BleEventFlowEnable;
|
||||
}
|
||||
}
|
||||
|
||||
void ble_event_dispatcher_init(void) {
|
||||
furi_assert(!initialized);
|
||||
|
||||
GapSvcEventHandlerList_init(handlers);
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
void ble_event_dispatcher_reset(void) {
|
||||
furi_assert(initialized);
|
||||
furi_check(GapSvcEventHandlerList_size(handlers) == 0);
|
||||
|
||||
GapSvcEventHandlerList_clear(handlers);
|
||||
}
|
||||
|
||||
GapSvcEventHandler*
|
||||
ble_event_dispatcher_register_svc_handler(BleSvcEventHandlerCb handler, void* context) {
|
||||
furi_check(handler);
|
||||
furi_check(context);
|
||||
furi_check(initialized);
|
||||
|
||||
GapSvcEventHandler* item = GapSvcEventHandlerList_push_raw(handlers);
|
||||
item->context = context;
|
||||
item->callback = handler;
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
void ble_event_dispatcher_unregister_svc_handler(GapSvcEventHandler* handler) {
|
||||
furi_check(handler);
|
||||
|
||||
bool found = false;
|
||||
GapSvcEventHandlerList_it_t it;
|
||||
|
||||
for(GapSvcEventHandlerList_it(it, handlers); !GapSvcEventHandlerList_end_p(it);
|
||||
GapSvcEventHandlerList_next(it)) {
|
||||
const GapSvcEventHandler* item = GapSvcEventHandlerList_cref(it);
|
||||
|
||||
if(item == handler) {
|
||||
GapSvcEventHandlerList_remove(handlers, it);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
furi_check(found);
|
||||
}
|
||||
50
targets/f7/ble_glue/furi_ble/event_dispatcher.h
Normal file
50
targets/f7/ble_glue/furi_ble/event_dispatcher.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <core/common_defines.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
BleEventNotAck,
|
||||
BleEventAckFlowEnable,
|
||||
BleEventAckFlowDisable,
|
||||
} BleEventAckStatus;
|
||||
|
||||
typedef enum {
|
||||
BleEventFlowDisable,
|
||||
BleEventFlowEnable,
|
||||
} BleEventFlowStatus;
|
||||
|
||||
/* Using other types so not to leak all the BLE stack headers
|
||||
(we don't have a wrapper for them yet)
|
||||
* Event data is hci_uart_pckt*
|
||||
* Context is user-defined
|
||||
*/
|
||||
typedef BleEventAckStatus (*BleSvcEventHandlerCb)(void* event, void* context);
|
||||
|
||||
typedef struct GapEventHandler GapSvcEventHandler;
|
||||
|
||||
/* To be called once at BLE system startup */
|
||||
void ble_event_dispatcher_init(void);
|
||||
|
||||
/* To be called at stack reset - ensures that all handlers are unregistered */
|
||||
void ble_event_dispatcher_reset(void);
|
||||
|
||||
BleEventFlowStatus ble_event_dispatcher_process_event(void* payload);
|
||||
|
||||
/* Final handler for event not ack'd by services - to be implemented by app */
|
||||
BleEventFlowStatus ble_event_app_notification(void* pckt);
|
||||
|
||||
/* Add a handler to the list of handlers */
|
||||
FURI_WARN_UNUSED GapSvcEventHandler*
|
||||
ble_event_dispatcher_register_svc_handler(BleSvcEventHandlerCb handler, void* context);
|
||||
|
||||
/* Remove a handler from the list of handlers */
|
||||
void ble_event_dispatcher_unregister_svc_handler(GapSvcEventHandler* handler);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "gatt_char.h"
|
||||
#include "gatt.h"
|
||||
#include <ble/ble.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
@@ -6,19 +7,19 @@
|
||||
|
||||
#define GATT_MIN_READ_KEY_SIZE (10)
|
||||
|
||||
void flipper_gatt_characteristic_init(
|
||||
void ble_gatt_characteristic_init(
|
||||
uint16_t svc_handle,
|
||||
const FlipperGattCharacteristicParams* char_descriptor,
|
||||
FlipperGattCharacteristicInstance* char_instance) {
|
||||
const BleGattCharacteristicParams* char_descriptor,
|
||||
BleGattCharacteristicInstance* char_instance) {
|
||||
furi_assert(char_descriptor);
|
||||
furi_assert(char_instance);
|
||||
|
||||
// Copy the descriptor to the instance, since it may point to stack memory
|
||||
char_instance->characteristic = malloc(sizeof(FlipperGattCharacteristicParams));
|
||||
char_instance->characteristic = malloc(sizeof(BleGattCharacteristicParams));
|
||||
memcpy(
|
||||
(void*)char_instance->characteristic,
|
||||
char_descriptor,
|
||||
sizeof(FlipperGattCharacteristicParams));
|
||||
sizeof(BleGattCharacteristicParams));
|
||||
|
||||
uint16_t char_data_size = 0;
|
||||
if(char_descriptor->data_prop_type == FlipperGattCharacteristicDataFixed) {
|
||||
@@ -46,7 +47,7 @@ void flipper_gatt_characteristic_init(
|
||||
char_instance->descriptor_handle = 0;
|
||||
if((status == 0) && char_descriptor->descriptor_params) {
|
||||
uint8_t const* char_data = NULL;
|
||||
const FlipperGattCharacteristicDescriptorParams* char_data_descriptor =
|
||||
const BleGattCharacteristicDescriptorParams* char_data_descriptor =
|
||||
char_descriptor->descriptor_params;
|
||||
bool release_data = char_data_descriptor->data_callback.fn(
|
||||
char_data_descriptor->data_callback.context, &char_data, &char_data_size);
|
||||
@@ -74,9 +75,9 @@ void flipper_gatt_characteristic_init(
|
||||
}
|
||||
}
|
||||
|
||||
void flipper_gatt_characteristic_delete(
|
||||
void ble_gatt_characteristic_delete(
|
||||
uint16_t svc_handle,
|
||||
FlipperGattCharacteristicInstance* char_instance) {
|
||||
BleGattCharacteristicInstance* char_instance) {
|
||||
tBleStatus status = aci_gatt_del_char(svc_handle, char_instance->handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(
|
||||
@@ -85,12 +86,12 @@ void flipper_gatt_characteristic_delete(
|
||||
free((void*)char_instance->characteristic);
|
||||
}
|
||||
|
||||
bool flipper_gatt_characteristic_update(
|
||||
bool ble_gatt_characteristic_update(
|
||||
uint16_t svc_handle,
|
||||
FlipperGattCharacteristicInstance* char_instance,
|
||||
BleGattCharacteristicInstance* char_instance,
|
||||
const void* source) {
|
||||
furi_assert(char_instance);
|
||||
const FlipperGattCharacteristicParams* char_descriptor = char_instance->characteristic;
|
||||
const BleGattCharacteristicParams* char_descriptor = char_instance->characteristic;
|
||||
FURI_LOG_D(TAG, "Updating %s char", char_descriptor->name);
|
||||
|
||||
const uint8_t* char_data = NULL;
|
||||
@@ -119,4 +120,28 @@ bool flipper_gatt_characteristic_update(
|
||||
free((void*)char_data);
|
||||
}
|
||||
return result != BLE_STATUS_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
bool ble_gatt_service_add(
|
||||
uint8_t Service_UUID_Type,
|
||||
const Service_UUID_t* Service_UUID,
|
||||
uint8_t Service_Type,
|
||||
uint8_t Max_Attribute_Records,
|
||||
uint16_t* Service_Handle) {
|
||||
tBleStatus result = aci_gatt_add_service(
|
||||
Service_UUID_Type, Service_UUID, Service_Type, Max_Attribute_Records, Service_Handle);
|
||||
if(result) {
|
||||
FURI_LOG_E(TAG, "Failed to add service: %x", result);
|
||||
}
|
||||
|
||||
return result == BLE_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
bool ble_gatt_service_delete(uint16_t svc_handle) {
|
||||
tBleStatus result = aci_gatt_del_service(svc_handle);
|
||||
if(result) {
|
||||
FURI_LOG_E(TAG, "Failed to delete service: %x", result);
|
||||
}
|
||||
|
||||
return result == BLE_STATUS_SUCCESS;
|
||||
}
|
||||
110
targets/f7/ble_glue/furi_ble/gatt.h
Normal file
110
targets/f7/ble_glue/furi_ble/gatt.h
Normal file
@@ -0,0 +1,110 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <ble/core/auto/ble_types.h>
|
||||
|
||||
/* Callback signature for getting characteristic data
|
||||
* Is called when characteristic is created to get max data length. Data ptr is NULL in this case
|
||||
* The result is passed to aci_gatt_add_char as "Char_Value_Length"
|
||||
* For updates, called with a context - see flipper_gatt_characteristic_update
|
||||
* Returns true if *data ownership is transferred to the caller and will be freed */
|
||||
typedef bool (
|
||||
*cbBleGattCharacteristicData)(const void* context, const uint8_t** data, uint16_t* data_len);
|
||||
|
||||
/* Used to specify the type of data for a characteristic - constant or callback-based */
|
||||
typedef enum {
|
||||
FlipperGattCharacteristicDataFixed,
|
||||
FlipperGattCharacteristicDataCallback,
|
||||
} BleGattCharacteristicDataType;
|
||||
|
||||
typedef struct {
|
||||
Char_Desc_Uuid_t uuid;
|
||||
struct {
|
||||
cbBleGattCharacteristicData fn;
|
||||
const void* context;
|
||||
} data_callback;
|
||||
uint8_t uuid_type;
|
||||
uint8_t max_length;
|
||||
uint8_t security_permissions;
|
||||
uint8_t access_permissions;
|
||||
uint8_t gatt_evt_mask;
|
||||
uint8_t is_variable;
|
||||
} BleGattCharacteristicDescriptorParams;
|
||||
|
||||
/* Describes a single characteristic, providing data or callbacks to get data */
|
||||
typedef struct {
|
||||
const char* name;
|
||||
BleGattCharacteristicDescriptorParams* descriptor_params;
|
||||
union {
|
||||
struct {
|
||||
const uint8_t* ptr;
|
||||
uint16_t length;
|
||||
} fixed;
|
||||
struct {
|
||||
cbBleGattCharacteristicData fn;
|
||||
const void* context;
|
||||
} callback;
|
||||
} data;
|
||||
Char_UUID_t uuid;
|
||||
// Some packed bitfields to save space
|
||||
BleGattCharacteristicDataType data_prop_type : 2;
|
||||
uint8_t is_variable : 2;
|
||||
uint8_t uuid_type : 2;
|
||||
uint8_t char_properties;
|
||||
uint8_t security_permissions;
|
||||
uint8_t gatt_evt_mask;
|
||||
} BleGattCharacteristicParams;
|
||||
|
||||
_Static_assert(
|
||||
sizeof(BleGattCharacteristicParams) == 36,
|
||||
"BleGattCharacteristicParams size must be 36 bytes");
|
||||
|
||||
typedef struct {
|
||||
const BleGattCharacteristicParams* characteristic;
|
||||
uint16_t handle;
|
||||
uint16_t descriptor_handle;
|
||||
} BleGattCharacteristicInstance;
|
||||
|
||||
/* Initialize a characteristic instance; copies the characteristic descriptor
|
||||
* into the instance */
|
||||
void ble_gatt_characteristic_init(
|
||||
uint16_t svc_handle,
|
||||
const BleGattCharacteristicParams* char_descriptor,
|
||||
BleGattCharacteristicInstance* char_instance);
|
||||
|
||||
/* Delete a characteristic instance; frees the copied characteristic
|
||||
* descriptor from the instance */
|
||||
void ble_gatt_characteristic_delete(
|
||||
uint16_t svc_handle,
|
||||
BleGattCharacteristicInstance* char_instance);
|
||||
|
||||
/* Update a characteristic instance; if source==NULL, uses the data from
|
||||
* the characteristic:
|
||||
* - For fixed data, fixed.ptr is used as the source if source==NULL
|
||||
* - For callback-based data, collback.context is passed as the context
|
||||
* if source==NULL
|
||||
*/
|
||||
bool ble_gatt_characteristic_update(
|
||||
uint16_t svc_handle,
|
||||
BleGattCharacteristicInstance* char_instance,
|
||||
const void* source);
|
||||
|
||||
bool ble_gatt_service_add(
|
||||
uint8_t Service_UUID_Type,
|
||||
const Service_UUID_t* Service_UUID,
|
||||
uint8_t Service_Type,
|
||||
uint8_t Max_Attribute_Records,
|
||||
uint16_t* Service_Handle);
|
||||
|
||||
bool ble_gatt_service_delete(uint16_t svc_handle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
39
targets/f7/ble_glue/furi_ble/profile_interface.h
Normal file
39
targets/f7/ble_glue/furi_ble/profile_interface.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <gap.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct FuriHalBleProfileTemplate FuriHalBleProfileTemplate;
|
||||
|
||||
/* Actual profiles must inherit (include this structure) as their first field */
|
||||
typedef struct {
|
||||
/* Pointer to the config for this profile. Must be used to check if the
|
||||
* instance belongs to the profile */
|
||||
const FuriHalBleProfileTemplate* config;
|
||||
} FuriHalBleProfileBase;
|
||||
|
||||
typedef void* FuriHalBleProfileParams;
|
||||
|
||||
typedef FuriHalBleProfileBase* (*FuriHalBleProfileStart)(FuriHalBleProfileParams profile_params);
|
||||
typedef void (*FuriHalBleProfileStop)(FuriHalBleProfileBase* profile);
|
||||
typedef void (*FuriHalBleProfileGetGapConfig)(
|
||||
GapConfig* target_config,
|
||||
FuriHalBleProfileParams profile_params);
|
||||
|
||||
struct FuriHalBleProfileTemplate {
|
||||
/* Returns an instance of the profile */
|
||||
FuriHalBleProfileStart start;
|
||||
/* Destroys the instance of the profile. Must check if instance belongs to the profile */
|
||||
FuriHalBleProfileStop stop;
|
||||
/* Called before starting the profile to get the GAP configuration */
|
||||
FuriHalBleProfileGetGapConfig get_gap_config;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,12 +1,15 @@
|
||||
#include "gap.h"
|
||||
|
||||
#include "app_common.h"
|
||||
#include <core/mutex.h>
|
||||
#include "furi_ble/event_dispatcher.h"
|
||||
#include <ble/ble.h>
|
||||
|
||||
#include <furi_hal.h>
|
||||
#include <furi.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define TAG "BtGap"
|
||||
#define TAG "BleGap"
|
||||
|
||||
#define FAST_ADV_TIMEOUT 30000
|
||||
#define INITIAL_ADV_TIMEOUT 60000
|
||||
@@ -98,7 +101,7 @@ static void gap_verify_connection_parameters(Gap* gap) {
|
||||
}
|
||||
}
|
||||
|
||||
SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
|
||||
BleEventFlowStatus ble_event_app_notification(void* pckt) {
|
||||
hci_event_pckt* event_pckt;
|
||||
evt_le_meta_event* meta_evt;
|
||||
evt_blecore_aci* blue_evt;
|
||||
@@ -293,7 +296,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
|
||||
if(gap) {
|
||||
furi_mutex_release(gap->state_mutex);
|
||||
}
|
||||
return SVCCTL_UserEvtFlowEnable;
|
||||
return BleEventFlowEnable;
|
||||
}
|
||||
|
||||
static void set_advertisment_service_uid(uint8_t* uid, uint8_t uid_len) {
|
||||
@@ -409,6 +412,8 @@ static void gap_advertise_start(GapState new_state) {
|
||||
uint16_t min_interval;
|
||||
uint16_t max_interval;
|
||||
|
||||
FURI_LOG_I(TAG, "Start: %d", new_state);
|
||||
|
||||
if(new_state == GapStateAdvFast) {
|
||||
min_interval = 0x80; // 80 ms
|
||||
max_interval = 0xa0; // 100 ms
|
||||
@@ -451,7 +456,8 @@ static void gap_advertise_start(GapState new_state) {
|
||||
furi_timer_start(gap->advertise_timer, INITIAL_ADV_TIMEOUT);
|
||||
}
|
||||
|
||||
static void gap_advertise_stop() {
|
||||
static void gap_advertise_stop(void) {
|
||||
FURI_LOG_I(TAG, "Stop");
|
||||
tBleStatus ret;
|
||||
if(gap->state > GapStateIdle) {
|
||||
if(gap->state == GapStateConnected) {
|
||||
@@ -477,7 +483,7 @@ static void gap_advertise_stop() {
|
||||
gap->on_event_cb(event, gap->context);
|
||||
}
|
||||
|
||||
void gap_start_advertising() {
|
||||
void gap_start_advertising(void) {
|
||||
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
|
||||
if(gap->state == GapStateIdle) {
|
||||
gap->state = GapStateStartingAdv;
|
||||
@@ -489,7 +495,7 @@ void gap_start_advertising() {
|
||||
furi_mutex_release(gap->state_mutex);
|
||||
}
|
||||
|
||||
void gap_stop_advertising() {
|
||||
void gap_stop_advertising(void) {
|
||||
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
|
||||
if(gap->state > GapStateIdle) {
|
||||
FURI_LOG_I(TAG, "Stop advertising");
|
||||
@@ -518,8 +524,7 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
|
||||
// Initialization of GATT & GAP layer
|
||||
gap->service.adv_name = config->adv_name;
|
||||
gap_init_svc(gap);
|
||||
// Initialization of the BLE Services
|
||||
SVCCTL_Init();
|
||||
ble_event_dispatcher_init();
|
||||
// Initialization of the GAP state
|
||||
gap->state_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
||||
gap->state = GapStateIdle;
|
||||
@@ -545,21 +550,11 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
|
||||
// Set callback
|
||||
gap->on_event_cb = on_event_cb;
|
||||
gap->context = context;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get RSSI
|
||||
uint32_t gap_get_remote_conn_rssi(int8_t* rssi) {
|
||||
if(gap && gap->state == GapStateConnected) {
|
||||
fetch_rssi();
|
||||
*rssi = gap->conn_rssi;
|
||||
|
||||
if(gap->time_rssi_sample) return furi_get_tick() - gap->time_rssi_sample;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
GapState gap_get_state() {
|
||||
GapState gap_get_state(void) {
|
||||
GapState state;
|
||||
if(gap) {
|
||||
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
|
||||
@@ -571,7 +566,7 @@ GapState gap_get_state() {
|
||||
return state;
|
||||
}
|
||||
|
||||
void gap_thread_stop() {
|
||||
void gap_thread_stop(void) {
|
||||
if(gap) {
|
||||
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
|
||||
gap->enable_adv = false;
|
||||
@@ -584,6 +579,8 @@ void gap_thread_stop() {
|
||||
furi_mutex_free(gap->state_mutex);
|
||||
furi_message_queue_free(gap->command_queue);
|
||||
furi_timer_free(gap->advertise_timer);
|
||||
|
||||
ble_event_dispatcher_reset();
|
||||
free(gap);
|
||||
gap = NULL;
|
||||
}
|
||||
@@ -614,3 +611,9 @@ static int32_t gap_app(void* context) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void gap_emit_ble_beacon_status_event(bool active) {
|
||||
GapEvent event = {.type = active ? GapEventTypeBeaconStart : GapEventTypeBeaconStop};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
FURI_LOG_I(TAG, "Beacon status event: %d", active);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
|
||||
#define GAP_MAC_ADDR_SIZE (6)
|
||||
|
||||
/*
|
||||
* GAP helpers - background thread that handles BLE GAP events and advertising.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -19,6 +23,8 @@ typedef enum {
|
||||
GapEventTypePinCodeShow,
|
||||
GapEventTypePinCodeVerify,
|
||||
GapEventTypeUpdateMTU,
|
||||
GapEventTypeBeaconStart,
|
||||
GapEventTypeBeaconStop,
|
||||
} GapEventType;
|
||||
|
||||
typedef union {
|
||||
@@ -73,13 +79,15 @@ typedef struct {
|
||||
|
||||
bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context);
|
||||
|
||||
void gap_start_advertising();
|
||||
void gap_start_advertising(void);
|
||||
|
||||
void gap_stop_advertising();
|
||||
void gap_stop_advertising(void);
|
||||
|
||||
GapState gap_get_state();
|
||||
GapState gap_get_state(void);
|
||||
|
||||
void gap_thread_stop();
|
||||
void gap_thread_stop(void);
|
||||
|
||||
void gap_emit_ble_beacon_status_event(bool active);
|
||||
|
||||
uint32_t gap_get_remote_conn_rssi(int8_t* rssi);
|
||||
|
||||
|
||||
114
targets/f7/ble_glue/profiles/serial_profile.c
Normal file
114
targets/f7/ble_glue/profiles/serial_profile.c
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "serial_profile.h"
|
||||
|
||||
#include <gap.h>
|
||||
#include <furi_ble/profile_interface.h>
|
||||
#include <services/dev_info_service.h>
|
||||
#include <services/battery_service.h>
|
||||
#include <services/serial_service.h>
|
||||
#include <furi.h>
|
||||
|
||||
typedef struct {
|
||||
FuriHalBleProfileBase base;
|
||||
|
||||
BleServiceDevInfo* dev_info_svc;
|
||||
BleServiceBattery* battery_svc;
|
||||
BleServiceSerial* serial_svc;
|
||||
} BleProfileSerial;
|
||||
_Static_assert(offsetof(BleProfileSerial, base) == 0, "Wrong layout");
|
||||
|
||||
static FuriHalBleProfileBase* ble_profile_serial_start(FuriHalBleProfileParams profile_params) {
|
||||
UNUSED(profile_params);
|
||||
|
||||
BleProfileSerial* profile = malloc(sizeof(BleProfileSerial));
|
||||
|
||||
profile->base.config = ble_profile_serial;
|
||||
|
||||
profile->dev_info_svc = ble_svc_dev_info_start();
|
||||
profile->battery_svc = ble_svc_battery_start(true);
|
||||
profile->serial_svc = ble_svc_serial_start();
|
||||
|
||||
return &profile->base;
|
||||
}
|
||||
|
||||
static void ble_profile_serial_stop(FuriHalBleProfileBase* profile) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_serial);
|
||||
|
||||
BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
|
||||
ble_svc_battery_stop(serial_profile->battery_svc);
|
||||
ble_svc_dev_info_stop(serial_profile->dev_info_svc);
|
||||
ble_svc_serial_stop(serial_profile->serial_svc);
|
||||
}
|
||||
|
||||
static GapConfig serial_template_config = {
|
||||
.adv_service_uuid = 0x3080,
|
||||
.appearance_char = 0x8600,
|
||||
.bonding_mode = true,
|
||||
.pairing_method = GapPairingPinCodeShow,
|
||||
.conn_param = {
|
||||
.conn_int_min = 0x18, // 30 ms
|
||||
.conn_int_max = 0x24, // 45 ms
|
||||
.slave_latency = 0,
|
||||
.supervisor_timeout = 0,
|
||||
}};
|
||||
|
||||
static void
|
||||
ble_profile_serial_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
|
||||
UNUSED(profile_params);
|
||||
|
||||
furi_check(config);
|
||||
memcpy(config, &serial_template_config, sizeof(GapConfig));
|
||||
// Set mac address
|
||||
memcpy(config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
|
||||
// Set advertise name
|
||||
strlcpy(
|
||||
config->adv_name,
|
||||
furi_hal_version_get_ble_local_device_name_ptr(),
|
||||
FURI_HAL_VERSION_DEVICE_NAME_LENGTH);
|
||||
config->adv_service_uuid |= furi_hal_version_get_hw_color();
|
||||
}
|
||||
|
||||
static const FuriHalBleProfileTemplate profile_callbacks = {
|
||||
.start = ble_profile_serial_start,
|
||||
.stop = ble_profile_serial_stop,
|
||||
.get_gap_config = ble_profile_serial_get_config,
|
||||
};
|
||||
|
||||
const FuriHalBleProfileTemplate* ble_profile_serial = &profile_callbacks;
|
||||
|
||||
void ble_profile_serial_set_event_callback(
|
||||
FuriHalBleProfileBase* profile,
|
||||
uint16_t buff_size,
|
||||
FuriHalBtSerialCallback callback,
|
||||
void* context) {
|
||||
furi_check(profile && (profile->config == ble_profile_serial));
|
||||
|
||||
BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
|
||||
ble_svc_serial_set_callbacks(serial_profile->serial_svc, buff_size, callback, context);
|
||||
}
|
||||
|
||||
void ble_profile_serial_notify_buffer_is_empty(FuriHalBleProfileBase* profile) {
|
||||
furi_check(profile && (profile->config == ble_profile_serial));
|
||||
|
||||
BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
|
||||
ble_svc_serial_notify_buffer_is_empty(serial_profile->serial_svc);
|
||||
}
|
||||
|
||||
void ble_profile_serial_set_rpc_active(FuriHalBleProfileBase* profile, bool active) {
|
||||
furi_check(profile && (profile->config == ble_profile_serial));
|
||||
|
||||
BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
|
||||
ble_svc_serial_set_rpc_active(serial_profile->serial_svc, active);
|
||||
}
|
||||
|
||||
bool ble_profile_serial_tx(FuriHalBleProfileBase* profile, uint8_t* data, uint16_t size) {
|
||||
furi_check(profile && (profile->config == ble_profile_serial));
|
||||
|
||||
BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
|
||||
|
||||
if(size > BLE_PROFILE_SERIAL_PACKET_SIZE_MAX) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ble_svc_serial_update_tx(serial_profile->serial_svc, data, size);
|
||||
}
|
||||
61
targets/f7/ble_glue/profiles/serial_profile.h
Normal file
61
targets/f7/ble_glue/profiles/serial_profile.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi_ble/profile_interface.h>
|
||||
|
||||
#include <services/serial_service.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define BLE_PROFILE_SERIAL_PACKET_SIZE_MAX BLE_SVC_SERIAL_DATA_LEN_MAX
|
||||
|
||||
typedef enum {
|
||||
FuriHalBtSerialRpcStatusNotActive,
|
||||
FuriHalBtSerialRpcStatusActive,
|
||||
} FuriHalBtSerialRpcStatus;
|
||||
|
||||
/** Serial service callback type */
|
||||
typedef SerialServiceEventCallback FuriHalBtSerialCallback;
|
||||
|
||||
/** Serial profile descriptor */
|
||||
extern const FuriHalBleProfileTemplate* ble_profile_serial;
|
||||
|
||||
/** Send data through BLE
|
||||
*
|
||||
* @param profile Profile instance
|
||||
* @param data data buffer
|
||||
* @param size data buffer size
|
||||
*
|
||||
* @return true on success
|
||||
*/
|
||||
bool ble_profile_serial_tx(FuriHalBleProfileBase* profile, uint8_t* data, uint16_t size);
|
||||
|
||||
/** Set BLE RPC status
|
||||
*
|
||||
* @param profile Profile instance
|
||||
* @param active true if RPC is active
|
||||
*/
|
||||
void ble_profile_serial_set_rpc_active(FuriHalBleProfileBase* profile, bool active);
|
||||
|
||||
/** Notify that application buffer is empty
|
||||
* @param profile Profile instance
|
||||
*/
|
||||
void ble_profile_serial_notify_buffer_is_empty(FuriHalBleProfileBase* profile);
|
||||
|
||||
/** Set Serial service events callback
|
||||
*
|
||||
* @param profile Profile instance
|
||||
* @param buffer_size Applicaition buffer size
|
||||
* @param calback FuriHalBtSerialCallback instance
|
||||
* @param context pointer to context
|
||||
*/
|
||||
void ble_profile_serial_set_event_callback(
|
||||
FuriHalBleProfileBase* profile,
|
||||
uint16_t buff_size,
|
||||
FuriHalBtSerialCallback callback,
|
||||
void* context);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,28 +1,30 @@
|
||||
#include "battery_service.h"
|
||||
#include "app_common.h"
|
||||
#include "gatt_char.h"
|
||||
#include <core/check.h>
|
||||
#include <furi_ble/gatt.h>
|
||||
|
||||
#include <ble/ble.h>
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal_power.h>
|
||||
|
||||
#include <m-list.h>
|
||||
|
||||
#define TAG "BtBatterySvc"
|
||||
|
||||
enum {
|
||||
// Common states
|
||||
/* Common states */
|
||||
BatterySvcPowerStateUnknown = 0b00,
|
||||
BatterySvcPowerStateUnsupported = 0b01,
|
||||
// Level states
|
||||
/* Level states */
|
||||
BatterySvcPowerStateGoodLevel = 0b10,
|
||||
BatterySvcPowerStateCriticallyLowLevel = 0b11,
|
||||
// Charging states
|
||||
/* Charging states */
|
||||
BatterySvcPowerStateNotCharging = 0b10,
|
||||
BatterySvcPowerStateCharging = 0b11,
|
||||
// Discharging states
|
||||
/* Discharging states */
|
||||
BatterySvcPowerStateNotDischarging = 0b10,
|
||||
BatterySvcPowerStateDischarging = 0b11,
|
||||
// Battery states
|
||||
/* Battery states */
|
||||
BatterySvcPowerStateBatteryNotPresent = 0b10,
|
||||
BatterySvcPowerStateBatteryPresent = 0b11,
|
||||
};
|
||||
@@ -46,96 +48,110 @@ typedef enum {
|
||||
BatterySvcGattCharacteristicCount,
|
||||
} BatterySvcGattCharacteristicId;
|
||||
|
||||
static const FlipperGattCharacteristicParams battery_svc_chars[BatterySvcGattCharacteristicCount] =
|
||||
{[BatterySvcGattCharacteristicBatteryLevel] =
|
||||
{.name = "Battery Level",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = 1,
|
||||
.uuid.Char_UUID_16 = BATTERY_LEVEL_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_NOTIFY,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT},
|
||||
[BatterySvcGattCharacteristicPowerState] = {
|
||||
.name = "Power State",
|
||||
static const BleGattCharacteristicParams battery_svc_chars[BatterySvcGattCharacteristicCount] = {
|
||||
[BatterySvcGattCharacteristicBatteryLevel] =
|
||||
{.name = "Battery Level",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = 1,
|
||||
.uuid.Char_UUID_16 = BATTERY_POWER_STATE,
|
||||
.uuid.Char_UUID_16 = BATTERY_LEVEL_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_NOTIFY,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT}};
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT},
|
||||
[BatterySvcGattCharacteristicPowerState] = {
|
||||
.name = "Power State",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = 1,
|
||||
.uuid.Char_UUID_16 = BATTERY_POWER_STATE,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_NOTIFY,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT}};
|
||||
|
||||
typedef struct {
|
||||
struct BleServiceBattery {
|
||||
uint16_t svc_handle;
|
||||
FlipperGattCharacteristicInstance chars[BatterySvcGattCharacteristicCount];
|
||||
} BatterySvc;
|
||||
BleGattCharacteristicInstance chars[BatterySvcGattCharacteristicCount];
|
||||
bool auto_update;
|
||||
};
|
||||
|
||||
static BatterySvc* battery_svc = NULL;
|
||||
LIST_DEF(BatterySvcInstanceList, BleServiceBattery*, M_POD_OPLIST);
|
||||
|
||||
void battery_svc_start() {
|
||||
battery_svc = malloc(sizeof(BatterySvc));
|
||||
tBleStatus status;
|
||||
/* We need to keep track of all battery service instances so that we can update
|
||||
* them when the battery state changes. */
|
||||
static BatterySvcInstanceList_t instances;
|
||||
static bool instances_initialized = false;
|
||||
|
||||
// Add Battery service
|
||||
status = aci_gatt_add_service(
|
||||
UUID_TYPE_16, (Service_UUID_t*)&service_uuid, PRIMARY_SERVICE, 8, &battery_svc->svc_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to add Battery service: %d", status);
|
||||
BleServiceBattery* ble_svc_battery_start(bool auto_update) {
|
||||
BleServiceBattery* battery_svc = malloc(sizeof(BleServiceBattery));
|
||||
|
||||
if(!ble_gatt_service_add(
|
||||
UUID_TYPE_16,
|
||||
(Service_UUID_t*)&service_uuid,
|
||||
PRIMARY_SERVICE,
|
||||
8,
|
||||
&battery_svc->svc_handle)) {
|
||||
free(battery_svc);
|
||||
return NULL;
|
||||
}
|
||||
for(size_t i = 0; i < BatterySvcGattCharacteristicCount; i++) {
|
||||
flipper_gatt_characteristic_init(
|
||||
ble_gatt_characteristic_init(
|
||||
battery_svc->svc_handle, &battery_svc_chars[i], &battery_svc->chars[i]);
|
||||
}
|
||||
|
||||
battery_svc_update_power_state();
|
||||
}
|
||||
|
||||
void battery_svc_stop() {
|
||||
tBleStatus status;
|
||||
if(battery_svc) {
|
||||
for(size_t i = 0; i < BatterySvcGattCharacteristicCount; i++) {
|
||||
flipper_gatt_characteristic_delete(battery_svc->svc_handle, &battery_svc->chars[i]);
|
||||
battery_svc->auto_update = auto_update;
|
||||
if(auto_update) {
|
||||
if(!instances_initialized) {
|
||||
BatterySvcInstanceList_init(instances);
|
||||
instances_initialized = true;
|
||||
}
|
||||
// Delete Battery service
|
||||
status = aci_gatt_del_service(battery_svc->svc_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to delete Battery service: %d", status);
|
||||
|
||||
BatterySvcInstanceList_push_back(instances, battery_svc);
|
||||
}
|
||||
|
||||
return battery_svc;
|
||||
}
|
||||
|
||||
void ble_svc_battery_stop(BleServiceBattery* battery_svc) {
|
||||
furi_assert(battery_svc);
|
||||
if(battery_svc->auto_update) {
|
||||
BatterySvcInstanceList_it_t it;
|
||||
for(BatterySvcInstanceList_it(it, instances); !BatterySvcInstanceList_end_p(it);
|
||||
BatterySvcInstanceList_next(it)) {
|
||||
if(*BatterySvcInstanceList_ref(it) == battery_svc) {
|
||||
BatterySvcInstanceList_remove(instances, it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(battery_svc);
|
||||
battery_svc = NULL;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < BatterySvcGattCharacteristicCount; i++) {
|
||||
ble_gatt_characteristic_delete(battery_svc->svc_handle, &battery_svc->chars[i]);
|
||||
}
|
||||
/* Delete Battery service */
|
||||
ble_gatt_service_delete(battery_svc->svc_handle);
|
||||
free(battery_svc);
|
||||
}
|
||||
|
||||
bool battery_svc_is_started() {
|
||||
return battery_svc != NULL;
|
||||
}
|
||||
|
||||
bool battery_svc_update_level(uint8_t battery_charge) {
|
||||
// Check if service was started
|
||||
if(battery_svc == NULL) {
|
||||
return false;
|
||||
}
|
||||
// Update battery level characteristic
|
||||
return flipper_gatt_characteristic_update(
|
||||
bool ble_svc_battery_update_level(BleServiceBattery* battery_svc, uint8_t battery_charge) {
|
||||
furi_check(battery_svc);
|
||||
/* Update battery level characteristic */
|
||||
return ble_gatt_characteristic_update(
|
||||
battery_svc->svc_handle,
|
||||
&battery_svc->chars[BatterySvcGattCharacteristicBatteryLevel],
|
||||
&battery_charge);
|
||||
}
|
||||
|
||||
bool battery_svc_update_power_state() {
|
||||
// Check if service was started
|
||||
if(battery_svc == NULL) {
|
||||
return false;
|
||||
}
|
||||
// Update power state characteristic
|
||||
bool ble_svc_battery_update_power_state(BleServiceBattery* battery_svc, bool charging) {
|
||||
furi_check(battery_svc);
|
||||
|
||||
/* Update power state characteristic */
|
||||
BattrySvcPowerState power_state = {
|
||||
.level = BatterySvcPowerStateUnsupported,
|
||||
.present = BatterySvcPowerStateBatteryPresent,
|
||||
};
|
||||
if(furi_hal_power_is_charging()) {
|
||||
if(charging) {
|
||||
power_state.charging = BatterySvcPowerStateCharging;
|
||||
power_state.discharging = BatterySvcPowerStateNotDischarging;
|
||||
} else {
|
||||
@@ -143,8 +159,29 @@ bool battery_svc_update_power_state() {
|
||||
power_state.discharging = BatterySvcPowerStateDischarging;
|
||||
}
|
||||
|
||||
return flipper_gatt_characteristic_update(
|
||||
return ble_gatt_characteristic_update(
|
||||
battery_svc->svc_handle,
|
||||
&battery_svc->chars[BatterySvcGattCharacteristicPowerState],
|
||||
&power_state);
|
||||
}
|
||||
|
||||
void ble_svc_battery_state_update(uint8_t* battery_level, bool* charging) {
|
||||
if(!instances_initialized) {
|
||||
#ifdef FURI_BLE_EXTRA_LOG
|
||||
FURI_LOG_W(TAG, "Battery service not initialized");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
BatterySvcInstanceList_it_t it;
|
||||
for(BatterySvcInstanceList_it(it, instances); !BatterySvcInstanceList_end_p(it);
|
||||
BatterySvcInstanceList_next(it)) {
|
||||
BleServiceBattery* battery_svc = *BatterySvcInstanceList_ref(it);
|
||||
if(battery_level) {
|
||||
ble_svc_battery_update_level(battery_svc, *battery_level);
|
||||
}
|
||||
if(charging) {
|
||||
ble_svc_battery_update_power_state(battery_svc, *charging);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,15 +7,27 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void battery_svc_start();
|
||||
/*
|
||||
* Battery service. Can be used in most profiles.
|
||||
* If auto_update is true, the service will automatically update the battery
|
||||
* level and charging state from power state updates.
|
||||
*/
|
||||
|
||||
void battery_svc_stop();
|
||||
typedef struct BleServiceBattery BleServiceBattery;
|
||||
|
||||
bool battery_svc_is_started();
|
||||
BleServiceBattery* ble_svc_battery_start(bool auto_update);
|
||||
|
||||
bool battery_svc_update_level(uint8_t battery_level);
|
||||
void ble_svc_battery_stop(BleServiceBattery* service);
|
||||
|
||||
bool battery_svc_update_power_state();
|
||||
bool ble_svc_battery_update_level(BleServiceBattery* service, uint8_t battery_level);
|
||||
|
||||
bool ble_svc_battery_update_power_state(BleServiceBattery* service, bool charging);
|
||||
|
||||
/* Global function, callable without a service instance
|
||||
* Will update all service instances created with auto_update==true
|
||||
* Both parameters are optional, pass NULL if no value is available
|
||||
*/
|
||||
void ble_svc_battery_state_update(uint8_t* battery_level, bool* charging);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "dev_info_service.h"
|
||||
#include "app_common.h"
|
||||
#include "gatt_char.h"
|
||||
#include <furi_ble/gatt.h>
|
||||
#include <ble/ble.h>
|
||||
|
||||
#include <furi.h>
|
||||
@@ -20,45 +20,30 @@ typedef enum {
|
||||
DevInfoSvcGattCharacteristicCount,
|
||||
} DevInfoSvcGattCharacteristicId;
|
||||
|
||||
#define DEVICE_INFO_HARDWARE_REV_SIZE 4
|
||||
typedef struct {
|
||||
uint16_t service_handle;
|
||||
FlipperGattCharacteristicInstance characteristics[DevInfoSvcGattCharacteristicCount];
|
||||
FuriString* version_string;
|
||||
char hardware_revision[DEVICE_INFO_HARDWARE_REV_SIZE];
|
||||
} DevInfoSvc;
|
||||
#define DEVICE_INFO_HARDWARE_REV_SIZE (4)
|
||||
#define DEVICE_INFO_SOFTWARE_REV_SIZE (40)
|
||||
|
||||
static DevInfoSvc* dev_info_svc = NULL;
|
||||
struct BleServiceDevInfo {
|
||||
uint16_t service_handle;
|
||||
BleGattCharacteristicInstance characteristics[DevInfoSvcGattCharacteristicCount];
|
||||
};
|
||||
|
||||
static const char dev_info_man_name[] = "Flipper Devices Inc.";
|
||||
static const char dev_info_serial_num[] = "1.0";
|
||||
static const char dev_info_rpc_version[] = TOSTRING(PROTOBUF_MAJOR_VERSION.PROTOBUF_MINOR_VERSION);
|
||||
static char hardware_revision[DEVICE_INFO_HARDWARE_REV_SIZE] = {0};
|
||||
static char software_revision[DEVICE_INFO_SOFTWARE_REV_SIZE] = {0};
|
||||
|
||||
static bool dev_info_char_firmware_rev_callback(
|
||||
const void* context,
|
||||
const uint8_t** data,
|
||||
uint16_t* data_len) {
|
||||
const DevInfoSvc* dev_info_svc = *(DevInfoSvc**)context;
|
||||
*data_len = strlen(dev_info_svc->hardware_revision);
|
||||
static bool
|
||||
dev_info_char_data_callback(const void* context, const uint8_t** data, uint16_t* data_len) {
|
||||
*data_len = (uint16_t)strlen(context); //-V1029
|
||||
if(data) {
|
||||
*data = (const uint8_t*)&dev_info_svc->hardware_revision;
|
||||
*data = (const uint8_t*)context;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool dev_info_char_software_rev_callback(
|
||||
const void* context,
|
||||
const uint8_t** data,
|
||||
uint16_t* data_len) {
|
||||
const DevInfoSvc* dev_info_svc = *(DevInfoSvc**)context;
|
||||
*data_len = furi_string_size(dev_info_svc->version_string);
|
||||
if(data) {
|
||||
*data = (const uint8_t*)furi_string_get_cstr(dev_info_svc->version_string);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static const FlipperGattCharacteristicParams dev_info_svc_chars[DevInfoSvcGattCharacteristicCount] =
|
||||
static const BleGattCharacteristicParams ble_svc_dev_info_chars[DevInfoSvcGattCharacteristicCount] =
|
||||
{[DevInfoSvcGattCharacteristicMfgName] =
|
||||
{.name = "Manufacturer Name",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
@@ -84,8 +69,8 @@ static const FlipperGattCharacteristicParams dev_info_svc_chars[DevInfoSvcGattCh
|
||||
[DevInfoSvcGattCharacteristicFirmwareRev] =
|
||||
{.name = "Firmware Revision",
|
||||
.data_prop_type = FlipperGattCharacteristicDataCallback,
|
||||
.data.callback.context = &dev_info_svc,
|
||||
.data.callback.fn = dev_info_char_firmware_rev_callback,
|
||||
.data.callback.context = hardware_revision,
|
||||
.data.callback.fn = dev_info_char_data_callback,
|
||||
.uuid.Char_UUID_16 = FIRMWARE_REVISION_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ,
|
||||
@@ -95,8 +80,8 @@ static const FlipperGattCharacteristicParams dev_info_svc_chars[DevInfoSvcGattCh
|
||||
[DevInfoSvcGattCharacteristicSoftwareRev] =
|
||||
{.name = "Software Revision",
|
||||
.data_prop_type = FlipperGattCharacteristicDataCallback,
|
||||
.data.callback.context = &dev_info_svc,
|
||||
.data.callback.fn = dev_info_char_software_rev_callback,
|
||||
.data.callback.context = software_revision,
|
||||
.data.callback.fn = dev_info_char_data_callback,
|
||||
.uuid.Char_UUID_16 = SOFTWARE_REVISION_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ,
|
||||
@@ -115,64 +100,52 @@ static const FlipperGattCharacteristicParams dev_info_svc_chars[DevInfoSvcGattCh
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT}};
|
||||
|
||||
void dev_info_svc_start() {
|
||||
dev_info_svc = malloc(sizeof(DevInfoSvc));
|
||||
dev_info_svc->version_string = furi_string_alloc_printf(
|
||||
BleServiceDevInfo* ble_svc_dev_info_start(void) {
|
||||
BleServiceDevInfo* dev_info_svc = malloc(sizeof(BleServiceDevInfo));
|
||||
snprintf(
|
||||
software_revision,
|
||||
sizeof(software_revision),
|
||||
"%s %s %s %s",
|
||||
version_get_githash(NULL),
|
||||
version_get_version(NULL),
|
||||
version_get_gitbranchnum(NULL),
|
||||
version_get_builddate(NULL));
|
||||
snprintf(
|
||||
dev_info_svc->hardware_revision,
|
||||
sizeof(dev_info_svc->hardware_revision),
|
||||
"%d",
|
||||
version_get_target(NULL));
|
||||
tBleStatus status;
|
||||
snprintf(hardware_revision, sizeof(hardware_revision), "%d", version_get_target(NULL));
|
||||
|
||||
// Add Device Information Service
|
||||
uint16_t uuid = DEVICE_INFORMATION_SERVICE_UUID;
|
||||
status = aci_gatt_add_service(
|
||||
UUID_TYPE_16,
|
||||
(Service_UUID_t*)&uuid,
|
||||
PRIMARY_SERVICE,
|
||||
1 + 2 * DevInfoSvcGattCharacteristicCount,
|
||||
&dev_info_svc->service_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to add Device Information Service: %d", status);
|
||||
if(!ble_gatt_service_add(
|
||||
UUID_TYPE_16,
|
||||
(Service_UUID_t*)&uuid,
|
||||
PRIMARY_SERVICE,
|
||||
1 + 2 * DevInfoSvcGattCharacteristicCount,
|
||||
&dev_info_svc->service_handle)) {
|
||||
free(dev_info_svc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < DevInfoSvcGattCharacteristicCount; i++) {
|
||||
flipper_gatt_characteristic_init(
|
||||
ble_gatt_characteristic_init(
|
||||
dev_info_svc->service_handle,
|
||||
&dev_info_svc_chars[i],
|
||||
&ble_svc_dev_info_chars[i],
|
||||
&dev_info_svc->characteristics[i]);
|
||||
flipper_gatt_characteristic_update(
|
||||
ble_gatt_characteristic_update(
|
||||
dev_info_svc->service_handle, &dev_info_svc->characteristics[i], NULL);
|
||||
}
|
||||
|
||||
return dev_info_svc;
|
||||
}
|
||||
|
||||
void dev_info_svc_stop() {
|
||||
tBleStatus status;
|
||||
if(dev_info_svc) {
|
||||
// Delete service characteristics
|
||||
for(size_t i = 0; i < DevInfoSvcGattCharacteristicCount; i++) {
|
||||
flipper_gatt_characteristic_delete(
|
||||
dev_info_svc->service_handle, &dev_info_svc->characteristics[i]);
|
||||
}
|
||||
|
||||
// Delete service
|
||||
status = aci_gatt_del_service(dev_info_svc->service_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to delete device info service: %d", status);
|
||||
}
|
||||
|
||||
furi_string_free(dev_info_svc->version_string);
|
||||
free(dev_info_svc);
|
||||
dev_info_svc = NULL;
|
||||
void ble_svc_dev_info_stop(BleServiceDevInfo* dev_info_svc) {
|
||||
furi_assert(dev_info_svc);
|
||||
/* Delete service characteristics */
|
||||
for(size_t i = 0; i < DevInfoSvcGattCharacteristicCount; i++) {
|
||||
ble_gatt_characteristic_delete(
|
||||
dev_info_svc->service_handle, &dev_info_svc->characteristics[i]);
|
||||
}
|
||||
}
|
||||
|
||||
bool dev_info_svc_is_started() {
|
||||
return dev_info_svc != NULL;
|
||||
/* Delete service */
|
||||
ble_gatt_service_delete(dev_info_svc->service_handle);
|
||||
|
||||
free(dev_info_svc);
|
||||
}
|
||||
|
||||
@@ -7,11 +7,16 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void dev_info_svc_start();
|
||||
/*
|
||||
* Device information service.
|
||||
* Holds Flipper name, version and other information.
|
||||
*/
|
||||
|
||||
void dev_info_svc_stop();
|
||||
typedef struct BleServiceDevInfo BleServiceDevInfo;
|
||||
|
||||
bool dev_info_svc_is_started();
|
||||
BleServiceDevInfo* ble_svc_dev_info_start(void);
|
||||
|
||||
void ble_svc_dev_info_stop(BleServiceDevInfo* service);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <ble/ble.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Callback signature for getting characteristic data
|
||||
// Is called when characteristic is created to get max data length. Data ptr is NULL in this case
|
||||
// The result is passed to aci_gatt_add_char as "Char_Value_Length"
|
||||
// For updates, called with a context - see flipper_gatt_characteristic_update
|
||||
// Returns true if *data ownership is transferred to the caller and will be freed
|
||||
typedef bool (*cbFlipperGattCharacteristicData)(
|
||||
const void* context,
|
||||
const uint8_t** data,
|
||||
uint16_t* data_len);
|
||||
|
||||
typedef enum {
|
||||
FlipperGattCharacteristicDataFixed,
|
||||
FlipperGattCharacteristicDataCallback,
|
||||
} FlipperGattCharacteristicDataType;
|
||||
|
||||
typedef struct {
|
||||
Char_Desc_Uuid_t uuid;
|
||||
struct {
|
||||
cbFlipperGattCharacteristicData fn;
|
||||
const void* context;
|
||||
} data_callback;
|
||||
uint8_t uuid_type;
|
||||
uint8_t max_length;
|
||||
uint8_t security_permissions;
|
||||
uint8_t access_permissions;
|
||||
uint8_t gatt_evt_mask;
|
||||
uint8_t is_variable;
|
||||
} FlipperGattCharacteristicDescriptorParams;
|
||||
|
||||
typedef struct {
|
||||
const char* name;
|
||||
FlipperGattCharacteristicDescriptorParams* descriptor_params;
|
||||
union {
|
||||
struct {
|
||||
const uint8_t* ptr;
|
||||
uint16_t length;
|
||||
} fixed;
|
||||
struct {
|
||||
cbFlipperGattCharacteristicData fn;
|
||||
const void* context;
|
||||
} callback;
|
||||
} data;
|
||||
Char_UUID_t uuid;
|
||||
// Some packed bitfields to save space
|
||||
FlipperGattCharacteristicDataType data_prop_type : 2;
|
||||
uint8_t is_variable : 2;
|
||||
uint8_t uuid_type : 2;
|
||||
uint8_t char_properties;
|
||||
uint8_t security_permissions;
|
||||
uint8_t gatt_evt_mask;
|
||||
} FlipperGattCharacteristicParams;
|
||||
|
||||
_Static_assert(
|
||||
sizeof(FlipperGattCharacteristicParams) == 36,
|
||||
"FlipperGattCharacteristicParams size must be 36 bytes");
|
||||
|
||||
typedef struct {
|
||||
const FlipperGattCharacteristicParams* characteristic;
|
||||
uint16_t handle;
|
||||
uint16_t descriptor_handle;
|
||||
} FlipperGattCharacteristicInstance;
|
||||
|
||||
// Initialize a characteristic instance; copies the characteristic descriptor into the instance
|
||||
void flipper_gatt_characteristic_init(
|
||||
uint16_t svc_handle,
|
||||
const FlipperGattCharacteristicParams* char_descriptor,
|
||||
FlipperGattCharacteristicInstance* char_instance);
|
||||
|
||||
// Delete a characteristic instance; frees the copied characteristic descriptor from the instance
|
||||
void flipper_gatt_characteristic_delete(
|
||||
uint16_t svc_handle,
|
||||
FlipperGattCharacteristicInstance* char_instance);
|
||||
|
||||
// Update a characteristic instance; if source==NULL, uses the data from the characteristic
|
||||
// - For fixed data, fixed.ptr is used as the source if source==NULL
|
||||
// - For callback-based data, collback.context is passed as the context if source==NULL
|
||||
bool flipper_gatt_characteristic_update(
|
||||
uint16_t svc_handle,
|
||||
FlipperGattCharacteristicInstance* char_instance,
|
||||
const void* source);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,366 +0,0 @@
|
||||
#include "hid_service.h"
|
||||
#include "app_common.h"
|
||||
#include <ble/ble.h>
|
||||
#include "gatt_char.h"
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
#define TAG "BtHid"
|
||||
|
||||
typedef enum {
|
||||
HidSvcGattCharacteristicProtocolMode = 0,
|
||||
HidSvcGattCharacteristicReportMap,
|
||||
HidSvcGattCharacteristicInfo,
|
||||
HidSvcGattCharacteristicCtrlPoint,
|
||||
HidSvcGattCharacteristicLed,
|
||||
HidSvcGattCharacteristicCount,
|
||||
} HidSvcGattCharacteristicId;
|
||||
|
||||
typedef struct {
|
||||
uint8_t report_idx;
|
||||
uint8_t report_type;
|
||||
} HidSvcReportId;
|
||||
|
||||
static_assert(sizeof(HidSvcReportId) == sizeof(uint16_t), "HidSvcReportId must be 2 bytes");
|
||||
|
||||
static const Service_UUID_t hid_svc_uuid = {
|
||||
.Service_UUID_16 = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
|
||||
};
|
||||
|
||||
static bool
|
||||
hid_svc_char_desc_data_callback(const void* context, const uint8_t** data, uint16_t* data_len) {
|
||||
const HidSvcReportId* report_id = context;
|
||||
*data_len = sizeof(HidSvcReportId);
|
||||
if(data) {
|
||||
*data = (const uint8_t*)report_id;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
const void* data_ptr;
|
||||
uint16_t data_len;
|
||||
} HidSvcDataWrapper;
|
||||
|
||||
static bool
|
||||
hid_svc_report_data_callback(const void* context, const uint8_t** data, uint16_t* data_len) {
|
||||
const HidSvcDataWrapper* report_data = context;
|
||||
if(data) {
|
||||
*data = report_data->data_ptr;
|
||||
*data_len = report_data->data_len;
|
||||
} else {
|
||||
*data_len = HID_SVC_REPORT_MAP_MAX_LEN;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// LED Descriptor params for BadBT
|
||||
|
||||
static uint8_t led_desc_context_buf[2] = {HID_SVC_REPORT_COUNT + 1, 2};
|
||||
|
||||
static FlipperGattCharacteristicDescriptorParams hid_svc_char_descr_led = {
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.uuid.Char_UUID_16 = REPORT_REFERENCE_DESCRIPTOR_UUID,
|
||||
.max_length = HID_SVC_REPORT_REF_LEN,
|
||||
.data_callback.fn = hid_svc_char_desc_data_callback,
|
||||
.data_callback.context = led_desc_context_buf,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.access_permissions = ATTR_ACCESS_READ_WRITE,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT,
|
||||
};
|
||||
|
||||
static const FlipperGattCharacteristicParams hid_svc_chars[HidSvcGattCharacteristicCount] = {
|
||||
[HidSvcGattCharacteristicProtocolMode] =
|
||||
{.name = "Protocol Mode",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = 1,
|
||||
.uuid.Char_UUID_16 = PROTOCOL_MODE_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_WRITE_WITHOUT_RESP,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT},
|
||||
[HidSvcGattCharacteristicReportMap] =
|
||||
{.name = "Report Map",
|
||||
.data_prop_type = FlipperGattCharacteristicDataCallback,
|
||||
.data.callback.fn = hid_svc_report_data_callback,
|
||||
.data.callback.context = NULL,
|
||||
.uuid.Char_UUID_16 = REPORT_MAP_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_VARIABLE},
|
||||
[HidSvcGattCharacteristicInfo] =
|
||||
{.name = "HID Information",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = HID_SVC_INFO_LEN,
|
||||
.data.fixed.ptr = NULL,
|
||||
.uuid.Char_UUID_16 = HID_INFORMATION_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT},
|
||||
[HidSvcGattCharacteristicCtrlPoint] =
|
||||
{.name = "HID Control Point",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = HID_SVC_CONTROL_POINT_LEN,
|
||||
.uuid.Char_UUID_16 = HID_CONTROL_POINT_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_WRITE_WITHOUT_RESP,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT},
|
||||
[HidSvcGattCharacteristicLed] =
|
||||
{
|
||||
.name =
|
||||
"HID LED State", // LED Characteristic and descriptor for BadBT to get numlock state for altchars
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = 1,
|
||||
.uuid.Char_UUID_16 = REPORT_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE |
|
||||
GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT,
|
||||
.descriptor_params = &hid_svc_char_descr_led,
|
||||
},
|
||||
};
|
||||
|
||||
static const FlipperGattCharacteristicDescriptorParams hid_svc_char_descr_template = {
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.uuid.Char_UUID_16 = REPORT_REFERENCE_DESCRIPTOR_UUID,
|
||||
.max_length = HID_SVC_REPORT_REF_LEN,
|
||||
.data_callback.fn = hid_svc_char_desc_data_callback,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.access_permissions = ATTR_ACCESS_READ_WRITE,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT,
|
||||
};
|
||||
|
||||
static const FlipperGattCharacteristicParams hid_svc_report_template = {
|
||||
.name = "Report",
|
||||
.data_prop_type = FlipperGattCharacteristicDataCallback,
|
||||
.data.callback.fn = hid_svc_report_data_callback,
|
||||
.data.callback.context = NULL,
|
||||
.uuid.Char_UUID_16 = REPORT_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_NOTIFY,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_VARIABLE,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
uint16_t svc_handle;
|
||||
FlipperGattCharacteristicInstance chars[HidSvcGattCharacteristicCount];
|
||||
FlipperGattCharacteristicInstance input_report_chars[HID_SVC_INPUT_REPORT_COUNT];
|
||||
FlipperGattCharacteristicInstance output_report_chars[HID_SVC_OUTPUT_REPORT_COUNT];
|
||||
FlipperGattCharacteristicInstance feature_report_chars[HID_SVC_FEATURE_REPORT_COUNT];
|
||||
// led state
|
||||
HidLedStateEventCallback led_state_event_callback;
|
||||
void* led_state_ctx;
|
||||
} HIDSvc;
|
||||
|
||||
static HIDSvc* hid_svc = NULL;
|
||||
|
||||
static SVCCTL_EvtAckStatus_t hid_svc_event_handler(void* event) {
|
||||
SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
|
||||
hci_event_pckt* event_pckt = (hci_event_pckt*)(((hci_uart_pckt*)event)->data);
|
||||
evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
|
||||
// aci_gatt_attribute_modified_event_rp0* attribute_modified;
|
||||
if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
|
||||
if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
|
||||
// Process modification events
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
} else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
|
||||
// Process notification confirmation
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
} else if(blecore_evt->ecode == ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE) {
|
||||
// LED Characteristic and descriptor for BadBT to get numlock state for altchars
|
||||
//
|
||||
// Process write request
|
||||
aci_gatt_write_permit_req_event_rp0* req =
|
||||
(aci_gatt_write_permit_req_event_rp0*)blecore_evt->data;
|
||||
|
||||
furi_check(hid_svc->led_state_event_callback && hid_svc->led_state_ctx);
|
||||
|
||||
// this check is likely to be incorrect, it will actually work in our case
|
||||
// but we need to investigate gatt api to see what is the rules
|
||||
// that specify attibute handle value from char handle (or the reverse)
|
||||
if(req->Attribute_Handle == (hid_svc->chars[HidSvcGattCharacteristicLed].handle + 1)) {
|
||||
hid_svc->led_state_event_callback(req->Data[0], hid_svc->led_state_ctx);
|
||||
aci_gatt_write_resp(
|
||||
req->Connection_Handle,
|
||||
req->Attribute_Handle,
|
||||
0x00, /* write_status = 0 (no error))*/
|
||||
0x00, /* err_code */
|
||||
req->Data_Length,
|
||||
req->Data);
|
||||
aci_gatt_write_char_value(
|
||||
req->Connection_Handle,
|
||||
hid_svc->chars[HidSvcGattCharacteristicLed].handle,
|
||||
req->Data_Length,
|
||||
req->Data);
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void hid_svc_start() {
|
||||
tBleStatus status;
|
||||
hid_svc = malloc(sizeof(HIDSvc));
|
||||
|
||||
// Register event handler
|
||||
SVCCTL_RegisterSvcHandler(hid_svc_event_handler);
|
||||
/**
|
||||
* Add Human Interface Device Service
|
||||
*/
|
||||
status = aci_gatt_add_service(
|
||||
UUID_TYPE_16,
|
||||
&hid_svc_uuid,
|
||||
PRIMARY_SERVICE,
|
||||
2 + /* protocol mode */
|
||||
(4 * HID_SVC_INPUT_REPORT_COUNT) + (3 * HID_SVC_OUTPUT_REPORT_COUNT) +
|
||||
(3 * HID_SVC_FEATURE_REPORT_COUNT) + 1 + 2 + 2 + 2 +
|
||||
4, /* Service + Report Map + HID Information + HID Control Point + LED state */
|
||||
&hid_svc->svc_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to add HID service: %d", status);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < HidSvcGattCharacteristicCount; i++) {
|
||||
flipper_gatt_characteristic_init(
|
||||
hid_svc->svc_handle, &hid_svc_chars[i], &hid_svc->chars[i]);
|
||||
}
|
||||
uint8_t protocol_mode = 1;
|
||||
flipper_gatt_characteristic_update(
|
||||
hid_svc->svc_handle,
|
||||
&hid_svc->chars[HidSvcGattCharacteristicProtocolMode],
|
||||
&protocol_mode);
|
||||
|
||||
// reports
|
||||
FlipperGattCharacteristicDescriptorParams hid_svc_char_descr;
|
||||
FlipperGattCharacteristicParams report_char;
|
||||
HidSvcReportId report_id;
|
||||
|
||||
memcpy(&hid_svc_char_descr, &hid_svc_char_descr_template, sizeof(hid_svc_char_descr));
|
||||
memcpy(&report_char, &hid_svc_report_template, sizeof(report_char));
|
||||
|
||||
hid_svc_char_descr.data_callback.context = &report_id;
|
||||
report_char.descriptor_params = &hid_svc_char_descr;
|
||||
|
||||
typedef struct {
|
||||
uint8_t report_type;
|
||||
uint8_t report_count;
|
||||
FlipperGattCharacteristicInstance* chars;
|
||||
} HidSvcReportCharProps;
|
||||
|
||||
HidSvcReportCharProps hid_report_chars[] = {
|
||||
{0x01, HID_SVC_INPUT_REPORT_COUNT, hid_svc->input_report_chars},
|
||||
{0x02, HID_SVC_OUTPUT_REPORT_COUNT, hid_svc->output_report_chars},
|
||||
{0x03, HID_SVC_FEATURE_REPORT_COUNT, hid_svc->feature_report_chars},
|
||||
};
|
||||
|
||||
for(size_t report_type_idx = 0; report_type_idx < COUNT_OF(hid_report_chars);
|
||||
report_type_idx++) {
|
||||
report_id.report_type = hid_report_chars[report_type_idx].report_type;
|
||||
for(size_t report_idx = 0; report_idx < hid_report_chars[report_type_idx].report_count;
|
||||
report_idx++) {
|
||||
report_id.report_idx = report_idx + 1;
|
||||
flipper_gatt_characteristic_init(
|
||||
hid_svc->svc_handle,
|
||||
&report_char,
|
||||
&hid_report_chars[report_type_idx].chars[report_idx]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool hid_svc_update_report_map(const uint8_t* data, uint16_t len) {
|
||||
furi_assert(data);
|
||||
furi_assert(hid_svc);
|
||||
|
||||
HidSvcDataWrapper report_data = {
|
||||
.data_ptr = data,
|
||||
.data_len = len,
|
||||
};
|
||||
return flipper_gatt_characteristic_update(
|
||||
hid_svc->svc_handle, &hid_svc->chars[HidSvcGattCharacteristicReportMap], &report_data);
|
||||
}
|
||||
|
||||
bool hid_svc_update_input_report(uint8_t input_report_num, uint8_t* data, uint16_t len) {
|
||||
furi_assert(data);
|
||||
furi_assert(hid_svc);
|
||||
furi_assert(input_report_num < HID_SVC_INPUT_REPORT_COUNT);
|
||||
|
||||
HidSvcDataWrapper report_data = {
|
||||
.data_ptr = data,
|
||||
.data_len = len,
|
||||
};
|
||||
return flipper_gatt_characteristic_update(
|
||||
hid_svc->svc_handle, &hid_svc->input_report_chars[input_report_num], &report_data);
|
||||
}
|
||||
|
||||
bool hid_svc_update_info(uint8_t* data) {
|
||||
furi_assert(data);
|
||||
furi_assert(hid_svc);
|
||||
|
||||
return flipper_gatt_characteristic_update(
|
||||
hid_svc->svc_handle, &hid_svc->chars[HidSvcGattCharacteristicInfo], &data);
|
||||
}
|
||||
|
||||
void hid_svc_register_led_state_callback(HidLedStateEventCallback callback, void* context) {
|
||||
furi_assert(hid_svc);
|
||||
furi_assert(callback);
|
||||
furi_assert(context);
|
||||
|
||||
hid_svc->led_state_event_callback = callback;
|
||||
hid_svc->led_state_ctx = context;
|
||||
}
|
||||
|
||||
bool hid_svc_is_started() {
|
||||
return hid_svc != NULL;
|
||||
}
|
||||
|
||||
void hid_svc_stop() {
|
||||
tBleStatus status;
|
||||
if(hid_svc) {
|
||||
// Delete characteristics
|
||||
for(size_t i = 0; i < HidSvcGattCharacteristicCount; i++) {
|
||||
flipper_gatt_characteristic_delete(hid_svc->svc_handle, &hid_svc->chars[i]);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
uint8_t report_count;
|
||||
FlipperGattCharacteristicInstance* chars;
|
||||
} HidSvcReportCharProps;
|
||||
|
||||
HidSvcReportCharProps hid_report_chars[] = {
|
||||
{HID_SVC_INPUT_REPORT_COUNT, hid_svc->input_report_chars},
|
||||
{HID_SVC_OUTPUT_REPORT_COUNT, hid_svc->output_report_chars},
|
||||
{HID_SVC_FEATURE_REPORT_COUNT, hid_svc->feature_report_chars},
|
||||
};
|
||||
|
||||
for(size_t report_type_idx = 0; report_type_idx < COUNT_OF(hid_report_chars);
|
||||
report_type_idx++) {
|
||||
for(size_t report_idx = 0; report_idx < hid_report_chars[report_type_idx].report_count;
|
||||
report_idx++) {
|
||||
flipper_gatt_characteristic_delete(
|
||||
hid_svc->svc_handle, &hid_report_chars[report_type_idx].chars[report_idx]);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete service
|
||||
status = aci_gatt_del_service(hid_svc->svc_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to delete HID service: %d", status);
|
||||
}
|
||||
free(hid_svc);
|
||||
hid_svc = NULL;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define HID_SVC_REPORT_MAP_MAX_LEN (255)
|
||||
#define HID_SVC_REPORT_MAX_LEN (255)
|
||||
#define HID_SVC_REPORT_REF_LEN (2)
|
||||
#define HID_SVC_INFO_LEN (4)
|
||||
#define HID_SVC_CONTROL_POINT_LEN (1)
|
||||
|
||||
#define HID_SVC_INPUT_REPORT_COUNT (3)
|
||||
#define HID_SVC_OUTPUT_REPORT_COUNT (0)
|
||||
#define HID_SVC_FEATURE_REPORT_COUNT (0)
|
||||
#define HID_SVC_REPORT_COUNT \
|
||||
(HID_SVC_INPUT_REPORT_COUNT + HID_SVC_OUTPUT_REPORT_COUNT + HID_SVC_FEATURE_REPORT_COUNT)
|
||||
|
||||
typedef uint16_t (*HidLedStateEventCallback)(uint8_t state, void* ctx);
|
||||
|
||||
void hid_svc_start();
|
||||
|
||||
void hid_svc_stop();
|
||||
|
||||
bool hid_svc_is_started();
|
||||
|
||||
bool hid_svc_update_report_map(const uint8_t* data, uint16_t len);
|
||||
|
||||
bool hid_svc_update_input_report(uint8_t input_report_num, uint8_t* data, uint16_t len);
|
||||
|
||||
// Expects data to be of length HID_SVC_INFO_LEN (4 bytes)
|
||||
bool hid_svc_update_info(uint8_t* data);
|
||||
|
||||
void hid_svc_register_led_state_callback(HidLedStateEventCallback callback, void* context);
|
||||
@@ -1,11 +1,13 @@
|
||||
#include "serial_service.h"
|
||||
#include "app_common.h"
|
||||
#include <ble/ble.h>
|
||||
#include "gatt_char.h"
|
||||
#include <furi_ble/event_dispatcher.h>
|
||||
#include <furi_ble/gatt.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
#include "serial_service_uuid.inc"
|
||||
#include <stdint.h>
|
||||
|
||||
#define TAG "BtSerialSvc"
|
||||
|
||||
@@ -17,12 +19,12 @@ typedef enum {
|
||||
SerialSvcGattCharacteristicCount,
|
||||
} SerialSvcGattCharacteristicId;
|
||||
|
||||
static const FlipperGattCharacteristicParams serial_svc_chars[SerialSvcGattCharacteristicCount] = {
|
||||
static const BleGattCharacteristicParams ble_svc_serial_chars[SerialSvcGattCharacteristicCount] = {
|
||||
[SerialSvcGattCharacteristicRx] =
|
||||
{.name = "RX",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = SERIAL_SVC_DATA_LEN_MAX,
|
||||
.uuid.Char_UUID_128 = SERIAL_SVC_RX_CHAR_UUID,
|
||||
.data.fixed.length = BLE_SVC_SERIAL_DATA_LEN_MAX,
|
||||
.uuid.Char_UUID_128 = BLE_SVC_SERIAL_RX_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_128,
|
||||
.char_properties = CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE | CHAR_PROP_READ,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
|
||||
@@ -31,8 +33,8 @@ static const FlipperGattCharacteristicParams serial_svc_chars[SerialSvcGattChara
|
||||
[SerialSvcGattCharacteristicTx] =
|
||||
{.name = "TX",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = SERIAL_SVC_DATA_LEN_MAX,
|
||||
.uuid.Char_UUID_128 = SERIAL_SVC_TX_CHAR_UUID,
|
||||
.data.fixed.length = BLE_SVC_SERIAL_DATA_LEN_MAX,
|
||||
.uuid.Char_UUID_128 = BLE_SVC_SERIAL_TX_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_128,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_INDICATE,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
|
||||
@@ -42,7 +44,7 @@ static const FlipperGattCharacteristicParams serial_svc_chars[SerialSvcGattChara
|
||||
{.name = "Flow control",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = sizeof(uint32_t),
|
||||
.uuid.Char_UUID_128 = SERIAL_SVC_FLOW_CONTROL_UUID,
|
||||
.uuid.Char_UUID_128 = BLE_SVC_SERIAL_FLOW_CONTROL_UUID,
|
||||
.uuid_type = UUID_TYPE_128,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_NOTIFY,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
|
||||
@@ -51,28 +53,28 @@ static const FlipperGattCharacteristicParams serial_svc_chars[SerialSvcGattChara
|
||||
[SerialSvcGattCharacteristicStatus] = {
|
||||
.name = "RPC status",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = sizeof(SerialServiceRpcStatus),
|
||||
.uuid.Char_UUID_128 = SERIAL_SVC_RPC_STATUS_UUID,
|
||||
.data.fixed.length = sizeof(uint32_t),
|
||||
.uuid.Char_UUID_128 = BLE_SVC_SERIAL_RPC_STATUS_UUID,
|
||||
.uuid_type = UUID_TYPE_128,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_WRITE | CHAR_PROP_NOTIFY,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
|
||||
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT}};
|
||||
|
||||
typedef struct {
|
||||
struct BleServiceSerial {
|
||||
uint16_t svc_handle;
|
||||
FlipperGattCharacteristicInstance chars[SerialSvcGattCharacteristicCount];
|
||||
BleGattCharacteristicInstance chars[SerialSvcGattCharacteristicCount];
|
||||
FuriMutex* buff_size_mtx;
|
||||
uint32_t buff_size;
|
||||
uint16_t bytes_ready_to_receive;
|
||||
SerialServiceEventCallback callback;
|
||||
void* context;
|
||||
} SerialSvc;
|
||||
GapSvcEventHandler* event_handler;
|
||||
};
|
||||
|
||||
static SerialSvc* serial_svc = NULL;
|
||||
|
||||
static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
|
||||
SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
|
||||
static BleEventAckStatus ble_svc_serial_event_handler(void* event, void* context) {
|
||||
BleServiceSerial* serial_svc = (BleServiceSerial*)context;
|
||||
BleEventAckStatus ret = BleEventNotAck;
|
||||
hci_event_pckt* event_pckt = (hci_event_pckt*)(((hci_uart_pckt*)event)->data);
|
||||
evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
|
||||
aci_gatt_attribute_modified_event_rp0* attribute_modified;
|
||||
@@ -82,7 +84,7 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
|
||||
if(attribute_modified->Attr_Handle ==
|
||||
serial_svc->chars[SerialSvcGattCharacteristicRx].handle + 2) {
|
||||
// Descriptor handle
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
ret = BleEventAckFlowEnable;
|
||||
FURI_LOG_D(TAG, "RX descriptor event");
|
||||
} else if(
|
||||
attribute_modified->Attr_Handle ==
|
||||
@@ -111,13 +113,12 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
|
||||
FURI_LOG_D(TAG, "Available buff size: %ld", buff_free_size);
|
||||
furi_check(furi_mutex_release(serial_svc->buff_size_mtx) == FuriStatusOk);
|
||||
}
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
ret = BleEventAckFlowEnable;
|
||||
} else if(
|
||||
attribute_modified->Attr_Handle ==
|
||||
serial_svc->chars[SerialSvcGattCharacteristicStatus].handle + 1) {
|
||||
SerialServiceRpcStatus* rpc_status =
|
||||
(SerialServiceRpcStatus*)attribute_modified->Attr_Data;
|
||||
if(*rpc_status == SerialServiceRpcStatusNotActive) {
|
||||
bool* rpc_status = (bool*)attribute_modified->Attr_Data;
|
||||
if(!*rpc_status) {
|
||||
if(serial_svc->callback) {
|
||||
SerialServiceEvent event = {
|
||||
.event = SerialServiceEventTypesBleResetRequest,
|
||||
@@ -134,43 +135,47 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
|
||||
};
|
||||
serial_svc->callback(event, serial_svc->context);
|
||||
}
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
ret = BleEventAckFlowEnable;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void serial_svc_update_rpc_char(SerialServiceRpcStatus status) {
|
||||
flipper_gatt_characteristic_update(
|
||||
typedef enum {
|
||||
SerialServiceRpcStatusNotActive = 0UL,
|
||||
SerialServiceRpcStatusActive = 1UL,
|
||||
} SerialServiceRpcStatus;
|
||||
|
||||
static void
|
||||
ble_svc_serial_update_rpc_char(BleServiceSerial* serial_svc, SerialServiceRpcStatus status) {
|
||||
ble_gatt_characteristic_update(
|
||||
serial_svc->svc_handle, &serial_svc->chars[SerialSvcGattCharacteristicStatus], &status);
|
||||
}
|
||||
|
||||
void serial_svc_start() {
|
||||
UNUSED(serial_svc_chars);
|
||||
tBleStatus status;
|
||||
serial_svc = malloc(sizeof(SerialSvc));
|
||||
// Register event handler
|
||||
SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
|
||||
BleServiceSerial* ble_svc_serial_start(void) {
|
||||
BleServiceSerial* serial_svc = malloc(sizeof(BleServiceSerial));
|
||||
|
||||
// Add service
|
||||
status = aci_gatt_add_service(
|
||||
UUID_TYPE_128, &service_uuid, PRIMARY_SERVICE, 12, &serial_svc->svc_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to add Serial service: %d", status);
|
||||
serial_svc->event_handler =
|
||||
ble_event_dispatcher_register_svc_handler(ble_svc_serial_event_handler, serial_svc);
|
||||
|
||||
if(!ble_gatt_service_add(
|
||||
UUID_TYPE_128, &service_uuid, PRIMARY_SERVICE, 12, &serial_svc->svc_handle)) {
|
||||
free(serial_svc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Add characteristics
|
||||
for(uint8_t i = 0; i < SerialSvcGattCharacteristicCount; i++) {
|
||||
flipper_gatt_characteristic_init(
|
||||
serial_svc->svc_handle, &serial_svc_chars[i], &serial_svc->chars[i]);
|
||||
ble_gatt_characteristic_init(
|
||||
serial_svc->svc_handle, &ble_svc_serial_chars[i], &serial_svc->chars[i]);
|
||||
}
|
||||
|
||||
serial_svc_update_rpc_char(SerialServiceRpcStatusNotActive);
|
||||
// Allocate buffer size mutex
|
||||
ble_svc_serial_update_rpc_char(serial_svc, SerialServiceRpcStatusNotActive);
|
||||
serial_svc->buff_size_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
|
||||
|
||||
return serial_svc;
|
||||
}
|
||||
|
||||
void serial_svc_set_callbacks(
|
||||
void ble_svc_serial_set_callbacks(
|
||||
BleServiceSerial* serial_svc,
|
||||
uint16_t buff_size,
|
||||
SerialServiceEventCallback callback,
|
||||
void* context) {
|
||||
@@ -181,13 +186,13 @@ void serial_svc_set_callbacks(
|
||||
serial_svc->bytes_ready_to_receive = buff_size;
|
||||
|
||||
uint32_t buff_size_reversed = REVERSE_BYTES_U32(serial_svc->buff_size);
|
||||
flipper_gatt_characteristic_update(
|
||||
ble_gatt_characteristic_update(
|
||||
serial_svc->svc_handle,
|
||||
&serial_svc->chars[SerialSvcGattCharacteristicFlowCtrl],
|
||||
&buff_size_reversed);
|
||||
}
|
||||
|
||||
void serial_svc_notify_buffer_is_empty() {
|
||||
void ble_svc_serial_notify_buffer_is_empty(BleServiceSerial* serial_svc) {
|
||||
furi_assert(serial_svc);
|
||||
furi_assert(serial_svc->buff_size_mtx);
|
||||
|
||||
@@ -197,7 +202,7 @@ void serial_svc_notify_buffer_is_empty() {
|
||||
serial_svc->bytes_ready_to_receive = serial_svc->buff_size;
|
||||
|
||||
uint32_t buff_size_reversed = REVERSE_BYTES_U32(serial_svc->buff_size);
|
||||
flipper_gatt_characteristic_update(
|
||||
ble_gatt_characteristic_update(
|
||||
serial_svc->svc_handle,
|
||||
&serial_svc->chars[SerialSvcGattCharacteristicFlowCtrl],
|
||||
&buff_size_reversed);
|
||||
@@ -205,35 +210,26 @@ void serial_svc_notify_buffer_is_empty() {
|
||||
furi_check(furi_mutex_release(serial_svc->buff_size_mtx) == FuriStatusOk);
|
||||
}
|
||||
|
||||
void serial_svc_stop() {
|
||||
tBleStatus status;
|
||||
if(serial_svc) {
|
||||
for(uint8_t i = 0; i < SerialSvcGattCharacteristicCount; i++) {
|
||||
flipper_gatt_characteristic_delete(serial_svc->svc_handle, &serial_svc->chars[i]);
|
||||
}
|
||||
// Delete service
|
||||
status = aci_gatt_del_service(serial_svc->svc_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to delete Serial service: %d", status);
|
||||
}
|
||||
// Delete buffer size mutex
|
||||
furi_mutex_free(serial_svc->buff_size_mtx);
|
||||
free(serial_svc);
|
||||
serial_svc = NULL;
|
||||
void ble_svc_serial_stop(BleServiceSerial* serial_svc) {
|
||||
furi_check(serial_svc);
|
||||
|
||||
ble_event_dispatcher_unregister_svc_handler(serial_svc->event_handler);
|
||||
|
||||
for(uint8_t i = 0; i < SerialSvcGattCharacteristicCount; i++) {
|
||||
ble_gatt_characteristic_delete(serial_svc->svc_handle, &serial_svc->chars[i]);
|
||||
}
|
||||
ble_gatt_service_delete(serial_svc->svc_handle);
|
||||
furi_mutex_free(serial_svc->buff_size_mtx);
|
||||
free(serial_svc);
|
||||
}
|
||||
|
||||
bool serial_svc_is_started() {
|
||||
return serial_svc != NULL;
|
||||
}
|
||||
|
||||
bool serial_svc_update_tx(uint8_t* data, uint16_t data_len) {
|
||||
if(data_len > SERIAL_SVC_DATA_LEN_MAX) {
|
||||
bool ble_svc_serial_update_tx(BleServiceSerial* serial_svc, uint8_t* data, uint16_t data_len) {
|
||||
if(data_len > BLE_SVC_SERIAL_DATA_LEN_MAX) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for(uint16_t remained = data_len; remained > 0;) {
|
||||
uint8_t value_len = MIN(SERIAL_SVC_CHAR_VALUE_LEN_MAX, remained);
|
||||
uint8_t value_len = MIN(BLE_SVC_SERIAL_CHAR_VALUE_LEN_MAX, remained);
|
||||
uint16_t value_offset = data_len - remained;
|
||||
remained -= value_len;
|
||||
|
||||
@@ -256,7 +252,8 @@ bool serial_svc_update_tx(uint8_t* data, uint16_t data_len) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void serial_svc_set_rpc_status(SerialServiceRpcStatus status) {
|
||||
void ble_svc_serial_set_rpc_active(BleServiceSerial* serial_svc, bool active) {
|
||||
furi_assert(serial_svc);
|
||||
serial_svc_update_rpc_char(status);
|
||||
ble_svc_serial_update_rpc_char(
|
||||
serial_svc, active ? SerialServiceRpcStatusActive : SerialServiceRpcStatusNotActive);
|
||||
}
|
||||
|
||||
@@ -3,17 +3,16 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define SERIAL_SVC_DATA_LEN_MAX (486)
|
||||
#define SERIAL_SVC_CHAR_VALUE_LEN_MAX (243)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
SerialServiceRpcStatusNotActive = 0UL,
|
||||
SerialServiceRpcStatusActive = 1UL,
|
||||
} SerialServiceRpcStatus;
|
||||
/*
|
||||
* Serial service. Implements RPC over BLE, with flow control.
|
||||
*/
|
||||
|
||||
#define BLE_SVC_SERIAL_DATA_LEN_MAX (486)
|
||||
#define BLE_SVC_SERIAL_CHAR_VALUE_LEN_MAX (243)
|
||||
|
||||
typedef enum {
|
||||
SerialServiceEventTypeDataReceived,
|
||||
@@ -33,22 +32,23 @@ typedef struct {
|
||||
|
||||
typedef uint16_t (*SerialServiceEventCallback)(SerialServiceEvent event, void* context);
|
||||
|
||||
void serial_svc_start();
|
||||
typedef struct BleServiceSerial BleServiceSerial;
|
||||
|
||||
void serial_svc_set_callbacks(
|
||||
BleServiceSerial* ble_svc_serial_start(void);
|
||||
|
||||
void ble_svc_serial_stop(BleServiceSerial* service);
|
||||
|
||||
void ble_svc_serial_set_callbacks(
|
||||
BleServiceSerial* service,
|
||||
uint16_t buff_size,
|
||||
SerialServiceEventCallback callback,
|
||||
void* context);
|
||||
|
||||
void serial_svc_set_rpc_status(SerialServiceRpcStatus status);
|
||||
void ble_svc_serial_set_rpc_active(BleServiceSerial* service, bool active);
|
||||
|
||||
void serial_svc_notify_buffer_is_empty();
|
||||
void ble_svc_serial_notify_buffer_is_empty(BleServiceSerial* service);
|
||||
|
||||
void serial_svc_stop();
|
||||
|
||||
bool serial_svc_is_started();
|
||||
|
||||
bool serial_svc_update_tx(uint8_t* data, uint16_t data_len);
|
||||
bool ble_svc_serial_update_tx(BleServiceSerial* service, uint8_t* data, uint16_t data_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
static const Service_UUID_t service_uuid = { .Service_UUID_128 = \
|
||||
{ 0x00, 0x00, 0xfe, 0x60, 0xcc, 0x7a, 0x48, 0x2a, 0x98, 0x4a, 0x7f, 0x2e, 0xd5, 0xb3, 0xe5, 0x8f }};
|
||||
|
||||
#define SERIAL_SVC_TX_CHAR_UUID \
|
||||
#define BLE_SVC_SERIAL_TX_CHAR_UUID \
|
||||
{ 0x00, 0x00, 0xfe, 0x61, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19 }
|
||||
#define SERIAL_SVC_RX_CHAR_UUID \
|
||||
#define BLE_SVC_SERIAL_RX_CHAR_UUID \
|
||||
{ 0x00, 0x00, 0xfe, 0x62, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19 }
|
||||
#define SERIAL_SVC_FLOW_CONTROL_UUID \
|
||||
#define BLE_SVC_SERIAL_FLOW_CONTROL_UUID \
|
||||
{ 0x00, 0x00, 0xfe, 0x63, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19 }
|
||||
#define SERIAL_SVC_RPC_STATUS_UUID \
|
||||
#define BLE_SVC_SERIAL_RPC_STATUS_UUID \
|
||||
{ 0x00, 0x00, 0xfe, 0x64, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19 }
|
||||
|
||||
Reference in New Issue
Block a user