[FL-1796] Disable bluetooth (#703)
* bt: add authentication for all characteristics * bt: app_ble cleanup * bt: add start and stop advertising API * bt: rework application with start and stop advertising API * bt: support f7 target * bt: f7 target remove unused files * bt: stop advertising in bt debug application * bt: fix bt status bar update * bt: change bluetooth On Off order
This commit is contained in:
		| @@ -1,140 +0,0 @@ | ||||
| #include "main.h" | ||||
|  | ||||
| #include "app_entry.h" | ||||
| #include "app_common.h" | ||||
| #include "dbg_trace.h" | ||||
| #include "ble.h" | ||||
| #include "tl.h" | ||||
| #include "app_ble.h" | ||||
| #include "shci.h" | ||||
| #include "cmsis_os.h" | ||||
|  | ||||
| #include <furi-hal.h> | ||||
|  | ||||
| PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_CmdPacket_t BleCmdBuffer; | ||||
|  | ||||
| // PLACE_IN_SECTION("TAG_OTA_END") const uint32_t MagicKeywordValue = 0x94448A29 ; | ||||
| // PLACE_IN_SECTION("TAG_OTA_START") const uint32_t MagicKeywordAddress = (uint32_t)&MagicKeywordValue; | ||||
|  | ||||
| osMutexId_t MtxHciId; | ||||
| osSemaphoreId_t SemHciId; | ||||
| osThreadId_t HciUserEvtProcessId; | ||||
|  | ||||
| const osThreadAttr_t HciUserEvtProcess_attr = { | ||||
|     .name = CFG_HCI_USER_EVT_PROCESS_NAME, | ||||
|     .attr_bits = CFG_HCI_USER_EVT_PROCESS_ATTR_BITS, | ||||
|     .cb_mem = CFG_HCI_USER_EVT_PROCESS_CB_MEM, | ||||
|     .cb_size = CFG_HCI_USER_EVT_PROCESS_CB_SIZE, | ||||
|     .stack_mem = CFG_HCI_USER_EVT_PROCESS_STACK_MEM, | ||||
|     .priority = CFG_HCI_USER_EVT_PROCESS_PRIORITY, | ||||
|     .stack_size = CFG_HCI_USER_EVT_PROCESS_STACK_SIZE | ||||
| }; | ||||
|  | ||||
| /* Private function prototypes -----------------------------------------------*/ | ||||
| static void HciUserEvtProcess(void *argument); | ||||
| static void BLE_UserEvtRx( void * pPayload ); | ||||
| static void BLE_StatusNot( HCI_TL_CmdStatus_t status ); | ||||
| static void Ble_Tl_Init( void ); | ||||
|  | ||||
| bool APP_BLE_Init() { | ||||
|   SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = { | ||||
|     {{0,0,0}},                  /**< Header unused */ | ||||
|     {0,                         /** pBleBufferAddress not used */ | ||||
|     0,                          /** BleBufferSize not used */ | ||||
|     CFG_BLE_NUM_GATT_ATTRIBUTES, | ||||
|     CFG_BLE_NUM_GATT_SERVICES, | ||||
|     CFG_BLE_ATT_VALUE_ARRAY_SIZE, | ||||
|     CFG_BLE_NUM_LINK, | ||||
|     CFG_BLE_DATA_LENGTH_EXTENSION, | ||||
|     CFG_BLE_PREPARE_WRITE_LIST_SIZE, | ||||
|     CFG_BLE_MBLOCK_COUNT, | ||||
|     CFG_BLE_MAX_ATT_MTU, | ||||
|     CFG_BLE_SLAVE_SCA, | ||||
|     CFG_BLE_MASTER_SCA, | ||||
|     CFG_BLE_LSE_SOURCE, | ||||
|     CFG_BLE_MAX_CONN_EVENT_LENGTH, | ||||
|     CFG_BLE_HSE_STARTUP_TIME, | ||||
|     CFG_BLE_VITERBI_MODE, | ||||
|     CFG_BLE_LL_ONLY, | ||||
|     0} | ||||
|   }; | ||||
|  | ||||
|   // Initialize Ble Transport Layer | ||||
|   Ble_Tl_Init( ); | ||||
|   // Register the hci transport layer to handle BLE User Asynchronous Events | ||||
|   HciUserEvtProcessId = osThreadNew(HciUserEvtProcess, NULL, &HciUserEvtProcess_attr); | ||||
|   // Starts the BLE Stack on CPU2 | ||||
|   return (SHCI_C2_BLE_Init( &ble_init_cmd_packet ) == SHCI_Success); | ||||
| } | ||||
|  | ||||
| static void Ble_Tl_Init( void ) { | ||||
|   HCI_TL_HciInitConf_t Hci_Tl_Init_Conf; | ||||
|  | ||||
|   MtxHciId = osMutexNew( NULL ); | ||||
|   SemHciId = osSemaphoreNew( 1, 0, NULL ); /*< Create the semaphore and make it busy at initialization */ | ||||
|  | ||||
|   Hci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&BleCmdBuffer; | ||||
|   Hci_Tl_Init_Conf.StatusNotCallBack = BLE_StatusNot; | ||||
|   hci_init(BLE_UserEvtRx, (void*) &Hci_Tl_Init_Conf); | ||||
| } | ||||
|  | ||||
| static void HciUserEvtProcess(void *argument) { | ||||
|   UNUSED(argument); | ||||
|  | ||||
|   for(;;) | ||||
|   { | ||||
|     osThreadFlagsWait( 1, osFlagsWaitAny, osWaitForever); | ||||
|     hci_user_evt_proc( ); | ||||
|   } | ||||
| } | ||||
|  | ||||
| /************************************************************* | ||||
|  * | ||||
|  * WRAP FUNCTIONS | ||||
|  * | ||||
|  *************************************************************/ | ||||
| void hci_notify_asynch_evt(void* pdata) { | ||||
|   UNUSED(pdata); | ||||
|   osThreadFlagsSet( HciUserEvtProcessId, 1 ); | ||||
| } | ||||
|  | ||||
| void hci_cmd_resp_release(uint32_t flag) { | ||||
|   UNUSED(flag); | ||||
|   osSemaphoreRelease( SemHciId ); | ||||
| } | ||||
|  | ||||
| void hci_cmd_resp_wait(uint32_t timeout) { | ||||
|   UNUSED(timeout); | ||||
|   osSemaphoreAcquire( SemHciId, osWaitForever ); | ||||
| } | ||||
|  | ||||
| static void BLE_UserEvtRx( void * pPayload ) { | ||||
|   SVCCTL_UserEvtFlowStatus_t svctl_return_status; | ||||
|   tHCI_UserEvtRxParam *pParam; | ||||
|  | ||||
|   pParam = (tHCI_UserEvtRxParam *)pPayload; | ||||
|  | ||||
|   svctl_return_status = SVCCTL_UserEvtRx((void *)&(pParam->pckt->evtserial)); | ||||
|   if (svctl_return_status != SVCCTL_UserEvtFlowDisable) { | ||||
|     pParam->status = HCI_TL_UserEventFlow_Enable; | ||||
|   } else { | ||||
|     pParam->status = HCI_TL_UserEventFlow_Disable; | ||||
|   } | ||||
| } | ||||
|  | ||||
| static void BLE_StatusNot( HCI_TL_CmdStatus_t status ) { | ||||
|   switch (status) { | ||||
|     case HCI_TL_CmdBusy: | ||||
|       osMutexAcquire( MtxHciId, osWaitForever ); | ||||
|       break; | ||||
|     case HCI_TL_CmdAvailable: | ||||
|       osMutexRelease( MtxHciId ); | ||||
|       break; | ||||
|     default: | ||||
|       break; | ||||
|   } | ||||
| } | ||||
|  | ||||
| void SVCCTL_ResumeUserEventFlow( void ) { | ||||
|   hci_resume_flow(); | ||||
| } | ||||
| @@ -427,9 +427,6 @@ typedef enum | ||||
| #define DBG_TRACE_MSG_QUEUE_SIZE 4096 | ||||
| #define MAX_DBG_TRACE_MSG_SIZE 1024 | ||||
|  | ||||
| #define CFG_LED_SUPPORTED         0 | ||||
| #define CFG_BUTTON_SUPPORTED      0 | ||||
|  | ||||
| /****************************************************************************** | ||||
|  * FreeRTOS | ||||
|  ******************************************************************************/ | ||||
| @@ -441,34 +438,5 @@ typedef enum | ||||
| #define CFG_SHCI_USER_EVT_PROCESS_PRIORITY    osPriorityNone | ||||
| #define CFG_SHCI_USER_EVT_PROCESS_STACK_SIZE  (128 * 7) | ||||
|  | ||||
| #define CFG_HCI_USER_EVT_PROCESS_NAME         "ble_hci_evt" | ||||
| #define CFG_HCI_USER_EVT_PROCESS_ATTR_BITS    (0) | ||||
| #define CFG_HCI_USER_EVT_PROCESS_CB_MEM       (0) | ||||
| #define CFG_HCI_USER_EVT_PROCESS_CB_SIZE      (0) | ||||
| #define CFG_HCI_USER_EVT_PROCESS_STACK_MEM    (0) | ||||
| #define CFG_HCI_USER_EVT_PROCESS_PRIORITY     osPriorityNone | ||||
| #define CFG_HCI_USER_EVT_PROCESS_STACK_SIZE   (128 * 8) | ||||
|  | ||||
| #define CFG_ADV_UPDATE_PROCESS_NAME           "ble_adv_upd" | ||||
| #define CFG_ADV_UPDATE_PROCESS_ATTR_BITS      (0) | ||||
| #define CFG_ADV_UPDATE_PROCESS_CB_MEM         (0) | ||||
| #define CFG_ADV_UPDATE_PROCESS_CB_SIZE        (0) | ||||
| #define CFG_ADV_UPDATE_PROCESS_STACK_MEM      (0) | ||||
| #define CFG_ADV_UPDATE_PROCESS_PRIORITY       osPriorityNone | ||||
| #define CFG_ADV_UPDATE_PROCESS_STACK_SIZE     (128 * 6) | ||||
|  | ||||
| #define CFG_HRS_PROCESS_NAME                  "hrs" | ||||
| #define CFG_HRS_PROCESS_ATTR_BITS             (0) | ||||
| #define CFG_HRS_PROCESS_CB_MEM                (0) | ||||
| #define CFG_HRS_PROCESS_CB_SIZE               (0) | ||||
| #define CFG_HRS_PROCESS_STACK_MEM             (0) | ||||
| #define CFG_HRS_PROCESS_PRIORITY              osPriorityNone | ||||
| #define CFG_HRS_PROCESS_STACK_SIZE            (128 * 8) | ||||
|  | ||||
| typedef enum { | ||||
|     CFG_LPM_APP, | ||||
|     CFG_LPM_APP_BLE, | ||||
| } CFG_LPM_Id_t; | ||||
|  | ||||
| #define CFG_OTP_BASE_ADDRESS    OTP_AREA_BASE | ||||
| #define CFG_OTP_END_ADRESS      OTP_AREA_END_ADDR | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| #include "app_common.h" | ||||
| #include "main.h" | ||||
| #include "app_entry.h" | ||||
| #include "app_ble.h" | ||||
| #include "ble_app.h" | ||||
| #include "ble.h" | ||||
| #include "tl.h" | ||||
| #include "cmsis_os.h" | ||||
| @@ -47,8 +47,6 @@ void APPE_Init() { | ||||
|   ble_glue_status = BleGlueStatusStartup; | ||||
|   SystemPower_Config(); /**< Configure the system Power Mode */ | ||||
|  | ||||
|   HW_TS_Init(hw_ts_InitMode_Full, &hrtc); /**< Initialize the TimerServer */ | ||||
|  | ||||
|   // APPD_Init(); | ||||
|   furi_hal_power_insomnia_enter(); | ||||
|  | ||||
| @@ -137,7 +135,7 @@ static void APPE_SysUserEvtRx( void * pPayload ) { | ||||
|   /* Traces channel initialization */ | ||||
|   // APPD_EnableCPU2( ); | ||||
|    | ||||
|   if (APP_BLE_Init()) { | ||||
|   if(ble_app_init()) { | ||||
|     FURI_LOG_I("Core2", "BLE stack started"); | ||||
|     ble_glue_status = BleGlueStatusStarted; | ||||
|   } else { | ||||
|   | ||||
| @@ -31,7 +31,7 @@ void battery_svc_start() { | ||||
|                                 (Char_UUID_t *) &char_battery_level_uuid, | ||||
|                                 1, | ||||
|                                 CHAR_PROP_READ | CHAR_PROP_NOTIFY, | ||||
|                                 ATTR_PERMISSION_NONE, | ||||
|                                 ATTR_PERMISSION_AUTHEN_READ, | ||||
|                                 GATT_DONT_NOTIFY_EVENTS, | ||||
|                                 10, | ||||
|                                 CHAR_VALUE_LEN_CONSTANT, | ||||
|   | ||||
							
								
								
									
										119
									
								
								firmware/targets/f7/ble-glue/ble_app.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								firmware/targets/f7/ble-glue/ble_app.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,119 @@ | ||||
| #include "ble_app.h" | ||||
|  | ||||
| #include "hci_tl.h" | ||||
| #include "ble.h" | ||||
| #include "shci.h" | ||||
| #include "cmsis_os.h" | ||||
| #include "gap.h" | ||||
|  | ||||
| #include <furi-hal.h> | ||||
|  | ||||
| #define BLE_APP_TAG "ble app" | ||||
|  | ||||
| PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_CmdPacket_t ble_app_cmd_buffer; | ||||
|  | ||||
| typedef struct { | ||||
|     osMutexId_t hci_mtx; | ||||
|     osSemaphoreId_t hci_sem; | ||||
|     osThreadId_t hci_thread_id; | ||||
|     osThreadAttr_t hci_thread_attr; | ||||
| } BleApp; | ||||
|  | ||||
| static BleApp* ble_app; | ||||
|  | ||||
| static void ble_app_hci_thread(void *arg); | ||||
| static void ble_app_hci_event_handler(void * pPayload); | ||||
| static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status); | ||||
|  | ||||
| bool ble_app_init() { | ||||
|     ble_app = furi_alloc(sizeof(BleApp)); | ||||
|  | ||||
|     // Allocate semafore and mutex for ble command buffer access | ||||
|     ble_app->hci_mtx = osMutexNew(NULL); | ||||
|     ble_app->hci_sem = osSemaphoreNew(1, 0, NULL); | ||||
|     // HCI transport layer thread to handle user asynch events | ||||
|     ble_app->hci_thread_attr.name = "ble hci"; | ||||
|     ble_app->hci_thread_attr.stack_size = 1024; | ||||
|     ble_app->hci_thread_id = osThreadNew(ble_app_hci_thread, NULL, &ble_app->hci_thread_attr); | ||||
|  | ||||
|     // Initialize Ble Transport Layer | ||||
|     HCI_TL_HciInitConf_t hci_tl_config = { | ||||
|         .p_cmdbuffer = (uint8_t*)&ble_app_cmd_buffer, | ||||
|         .StatusNotCallBack = ble_app_hci_status_not_handler, | ||||
|     }; | ||||
|     hci_init(ble_app_hci_event_handler, (void*)&hci_tl_config); | ||||
|  | ||||
|     // Start ble stack on 2nd core | ||||
|     SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = { | ||||
|         .Header = {{0,0,0}}, // Header unused | ||||
|         .Param = { | ||||
|             0, // pBleBufferAddress not used | ||||
|             0, // BleBufferSize not used | ||||
|             CFG_BLE_NUM_GATT_ATTRIBUTES, | ||||
|             CFG_BLE_NUM_GATT_SERVICES, | ||||
|             CFG_BLE_ATT_VALUE_ARRAY_SIZE, | ||||
|             CFG_BLE_NUM_LINK, | ||||
|             CFG_BLE_DATA_LENGTH_EXTENSION, | ||||
|             CFG_BLE_PREPARE_WRITE_LIST_SIZE, | ||||
|             CFG_BLE_MBLOCK_COUNT, | ||||
|             CFG_BLE_MAX_ATT_MTU, | ||||
|             CFG_BLE_SLAVE_SCA, | ||||
|             CFG_BLE_MASTER_SCA, | ||||
|             CFG_BLE_LSE_SOURCE, | ||||
|             CFG_BLE_MAX_CONN_EVENT_LENGTH, | ||||
|             CFG_BLE_HSE_STARTUP_TIME, | ||||
|             CFG_BLE_VITERBI_MODE, | ||||
|             CFG_BLE_LL_ONLY, | ||||
|             0, | ||||
|         } | ||||
|     }; | ||||
|     SHCI_CmdStatus_t status = SHCI_C2_BLE_Init(&ble_init_cmd_packet); | ||||
|     if(status) { | ||||
|         FURI_LOG_E(BLE_APP_TAG, "Failed to start ble stack: %d", status); | ||||
|     } | ||||
|     return status == SHCI_Success; | ||||
| } | ||||
|  | ||||
| static void ble_app_hci_thread(void *arg) { | ||||
|     while(1) { | ||||
|         osThreadFlagsWait(1, osFlagsWaitAny, osWaitForever); | ||||
|         hci_user_evt_proc(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| // Called by WPAN lib | ||||
| void hci_notify_asynch_evt(void* pdata) { | ||||
|     osThreadFlagsSet(ble_app->hci_thread_id, 1); | ||||
| } | ||||
|  | ||||
| void hci_cmd_resp_release(uint32_t flag) { | ||||
|     osSemaphoreRelease(ble_app->hci_sem); | ||||
| } | ||||
|  | ||||
| void hci_cmd_resp_wait(uint32_t timeout) { | ||||
|     osSemaphoreAcquire(ble_app->hci_sem, osWaitForever); | ||||
| } | ||||
|  | ||||
| static void ble_app_hci_event_handler( void * pPayload ) { | ||||
|     SVCCTL_UserEvtFlowStatus_t svctl_return_status; | ||||
|     tHCI_UserEvtRxParam *pParam = (tHCI_UserEvtRxParam *)pPayload; | ||||
|  | ||||
|     svctl_return_status = SVCCTL_UserEvtRx((void *)&(pParam->pckt->evtserial)); | ||||
|     if (svctl_return_status != SVCCTL_UserEvtFlowDisable) { | ||||
|         pParam->status = HCI_TL_UserEventFlow_Enable; | ||||
|     } else { | ||||
|         pParam->status = HCI_TL_UserEventFlow_Disable; | ||||
|     } | ||||
| } | ||||
|  | ||||
| static void ble_app_hci_status_not_handler( HCI_TL_CmdStatus_t status ) { | ||||
|     if(status == HCI_TL_CmdBusy) { | ||||
|         osMutexAcquire(ble_app->hci_mtx, osWaitForever ); | ||||
|     } else if(status == HCI_TL_CmdAvailable) { | ||||
|         osMutexRelease(ble_app->hci_mtx); | ||||
|     } | ||||
| } | ||||
|  | ||||
| void SVCCTL_ResumeUserEventFlow( void ) { | ||||
|     hci_resume_flow(); | ||||
| } | ||||
| @@ -5,9 +5,8 @@ extern "C" { | ||||
| #endif | ||||
| 
 | ||||
| #include <stdbool.h> | ||||
| #include "hci_tl.h" | ||||
| 
 | ||||
| bool APP_BLE_Init(); | ||||
| bool ble_app_init(); | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| @@ -39,7 +39,7 @@ void dev_info_svc_start() { | ||||
|                                 (Char_UUID_t*)&uuid, | ||||
|                                 strlen(dev_info_man_name), | ||||
|                                 CHAR_PROP_READ, | ||||
|                                 ATTR_PERMISSION_NONE, | ||||
|                                 ATTR_PERMISSION_AUTHEN_READ, | ||||
|                                 GATT_DONT_NOTIFY_EVENTS, | ||||
|                                 10, | ||||
|                                 CHAR_VALUE_LEN_CONSTANT, | ||||
| @@ -53,7 +53,7 @@ void dev_info_svc_start() { | ||||
|                                 (Char_UUID_t*)&uuid, | ||||
|                                 strlen(dev_info_serial_num), | ||||
|                                 CHAR_PROP_READ, | ||||
|                                 ATTR_PERMISSION_NONE, | ||||
|                                 ATTR_PERMISSION_AUTHEN_READ, | ||||
|                                 GATT_DONT_NOTIFY_EVENTS, | ||||
|                                 10, | ||||
|                                 CHAR_VALUE_LEN_CONSTANT, | ||||
| @@ -67,7 +67,7 @@ void dev_info_svc_start() { | ||||
|                                 (Char_UUID_t*)&uuid, | ||||
|                                 strlen(dev_info_firmware_rev_num), | ||||
|                                 CHAR_PROP_READ, | ||||
|                                 ATTR_PERMISSION_NONE, | ||||
|                                 ATTR_PERMISSION_AUTHEN_READ, | ||||
|                                 GATT_DONT_NOTIFY_EVENTS, | ||||
|                                 10, | ||||
|                                 CHAR_VALUE_LEN_CONSTANT, | ||||
| @@ -81,7 +81,7 @@ void dev_info_svc_start() { | ||||
|                                 (Char_UUID_t*)&uuid, | ||||
|                                 strlen(dev_info_software_rev_num), | ||||
|                                 CHAR_PROP_READ, | ||||
|                                 ATTR_PERMISSION_NONE, | ||||
|                                 ATTR_PERMISSION_AUTHEN_READ, | ||||
|                                 GATT_DONT_NOTIFY_EVENTS, | ||||
|                                 10, | ||||
|                                 CHAR_VALUE_LEN_CONSTANT, | ||||
|   | ||||
| @@ -31,13 +31,22 @@ typedef struct { | ||||
| typedef struct { | ||||
|   GapSvc gap_svc; | ||||
|   GapState state; | ||||
|   osMutexId_t state_mutex; | ||||
|   uint8_t mac_address[BD_ADDR_SIZE_LOCAL]; | ||||
|   Bt* bt; | ||||
|   osTimerId advertise_timer; | ||||
|   osThreadAttr_t thread_attr; | ||||
|   osThreadId_t thread_id; | ||||
|   osMessageQueueId_t command_queue; | ||||
|   bool enable_adv; | ||||
| } Gap; | ||||
|  | ||||
| typedef enum { | ||||
|     GapCommandAdvFast, | ||||
|     GapCommandAdvLowPower, | ||||
|     GapCommandAdvStop, | ||||
| } GapCommand; | ||||
|  | ||||
| // Identity root key | ||||
| static const uint8_t gap_irk[16] = {0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0}; | ||||
| // Encryption root key | ||||
| @@ -49,150 +58,151 @@ static const uint8_t gap_default_mac_addr[] = {0x6c, 0x7a, 0xd8, 0xac, 0x57, 0x7 | ||||
|  | ||||
| static Gap* gap = NULL; | ||||
|  | ||||
| static void gap_advertise(GapState new_state); | ||||
| static void gap_advertise_start(GapState new_state); | ||||
| static void gap_app(void *arg); | ||||
|  | ||||
| SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt ) | ||||
| { | ||||
|   hci_event_pckt *event_pckt; | ||||
|   evt_le_meta_event *meta_evt; | ||||
|   evt_blue_aci *blue_evt; | ||||
|   hci_le_phy_update_complete_event_rp0 *evt_le_phy_update_complete; | ||||
|   uint8_t tx_phy; | ||||
|   uint8_t rx_phy; | ||||
|   tBleStatus ret = BLE_STATUS_INVALID_PARAMS; | ||||
|     hci_event_pckt *event_pckt; | ||||
|     evt_le_meta_event *meta_evt; | ||||
|     evt_blue_aci *blue_evt; | ||||
|     hci_le_phy_update_complete_event_rp0 *evt_le_phy_update_complete; | ||||
|     uint8_t tx_phy; | ||||
|     uint8_t rx_phy; | ||||
|     tBleStatus ret = BLE_STATUS_INVALID_PARAMS; | ||||
|  | ||||
|   event_pckt = (hci_event_pckt*) ((hci_uart_pckt *) pckt)->data; | ||||
|     event_pckt = (hci_event_pckt*) ((hci_uart_pckt *) pckt)->data; | ||||
|  | ||||
|   switch (event_pckt->evt) { | ||||
|     case EVT_DISCONN_COMPLETE: | ||||
|     { | ||||
|         hci_disconnection_complete_event_rp0 *disconnection_complete_event = (hci_disconnection_complete_event_rp0 *) event_pckt->data; | ||||
|         if (disconnection_complete_event->Connection_Handle == gap->gap_svc.connection_handle) { | ||||
|             gap->gap_svc.connection_handle = 0; | ||||
|             gap->state = GapStateIdle; | ||||
|             FURI_LOG_I(GAP_TAG, "Disconnect from client"); | ||||
|     osMutexAcquire(gap->state_mutex, osWaitForever); | ||||
|     switch (event_pckt->evt) { | ||||
|         case EVT_DISCONN_COMPLETE: | ||||
|         { | ||||
|             hci_disconnection_complete_event_rp0 *disconnection_complete_event = (hci_disconnection_complete_event_rp0 *) event_pckt->data; | ||||
|             if (disconnection_complete_event->Connection_Handle == gap->gap_svc.connection_handle) { | ||||
|                 gap->gap_svc.connection_handle = 0; | ||||
|                 gap->state = GapStateIdle; | ||||
|                 FURI_LOG_I(GAP_TAG, "Disconnect from client"); | ||||
|             } | ||||
|             if(gap->enable_adv) { | ||||
|                 // Restart advertising | ||||
|                 gap_start_advertising(); | ||||
|                 furi_hal_power_insomnia_exit(); | ||||
|             } | ||||
|         } | ||||
|         // Restart advertising | ||||
|         gap_advertise(GapStateAdvFast); | ||||
|         furi_hal_power_insomnia_exit(); | ||||
|     } | ||||
|     break; | ||||
|         break; | ||||
|  | ||||
|     case EVT_LE_META_EVENT: | ||||
|         meta_evt = (evt_le_meta_event*) event_pckt->data; | ||||
|         switch (meta_evt->subevent) { | ||||
|             case EVT_LE_CONN_UPDATE_COMPLETE: | ||||
|             FURI_LOG_D(GAP_TAG, "Connection update event"); | ||||
|             break; | ||||
|         case EVT_LE_META_EVENT: | ||||
|             meta_evt = (evt_le_meta_event*) event_pckt->data; | ||||
|             switch (meta_evt->subevent) { | ||||
|                 case EVT_LE_CONN_UPDATE_COMPLETE: | ||||
|                 FURI_LOG_D(GAP_TAG, "Connection update event"); | ||||
|                 break; | ||||
|  | ||||
|             case EVT_LE_PHY_UPDATE_COMPLETE: | ||||
|             evt_le_phy_update_complete = (hci_le_phy_update_complete_event_rp0*)meta_evt->data; | ||||
|             if(evt_le_phy_update_complete->Status) { | ||||
|                 FURI_LOG_E(GAP_TAG, "Update PHY failed, status %d", evt_le_phy_update_complete->Status); | ||||
|             } else { | ||||
|                 FURI_LOG_I(GAP_TAG, "Update PHY succeed"); | ||||
|                 case EVT_LE_PHY_UPDATE_COMPLETE: | ||||
|                 evt_le_phy_update_complete = (hci_le_phy_update_complete_event_rp0*)meta_evt->data; | ||||
|                 if(evt_le_phy_update_complete->Status) { | ||||
|                     FURI_LOG_E(GAP_TAG, "Update PHY failed, status %d", evt_le_phy_update_complete->Status); | ||||
|                 } else { | ||||
|                     FURI_LOG_I(GAP_TAG, "Update PHY succeed"); | ||||
|                 } | ||||
|                 ret = hci_le_read_phy(gap->gap_svc.connection_handle,&tx_phy,&rx_phy); | ||||
|                 if(ret) { | ||||
|                     FURI_LOG_E(GAP_TAG, "Read PHY failed, status: %d", ret); | ||||
|                 } else { | ||||
|                     FURI_LOG_I(GAP_TAG, "PHY Params TX = %d, RX = %d ", tx_phy, rx_phy); | ||||
|                 } | ||||
|                 break; | ||||
|  | ||||
|                 case EVT_LE_CONN_COMPLETE: | ||||
|                 furi_hal_power_insomnia_enter(); | ||||
|                 hci_le_connection_complete_event_rp0* connection_complete_event = (hci_le_connection_complete_event_rp0 *) meta_evt->data; | ||||
|                 FURI_LOG_I(GAP_TAG, "Connection complete for connection handle 0x%x", connection_complete_event->Connection_Handle); | ||||
|  | ||||
|                 // Stop advertising as connection completed | ||||
|                 osTimerStop(gap->advertise_timer); | ||||
|  | ||||
|                 // Update connection status and handle | ||||
|                 gap->state = GapStateConnected; | ||||
|                 gap->gap_svc.connection_handle = connection_complete_event->Connection_Handle; | ||||
|  | ||||
|                 // Start pairing by sending security request | ||||
|                 aci_gap_slave_security_req(connection_complete_event->Connection_Handle); | ||||
|                 break; | ||||
|  | ||||
|                 default: | ||||
|                 break; | ||||
|             } | ||||
|             ret = hci_le_read_phy(gap->gap_svc.connection_handle,&tx_phy,&rx_phy); | ||||
|             if(ret) { | ||||
|                 FURI_LOG_E(GAP_TAG, "Read PHY failed, status: %d", ret); | ||||
|             } else { | ||||
|                 FURI_LOG_I(GAP_TAG, "PHY Params TX= %d, RX= %d ", tx_phy, rx_phy); | ||||
|         break; | ||||
|  | ||||
|         case EVT_VENDOR: | ||||
|             blue_evt = (evt_blue_aci*) event_pckt->data; | ||||
|             switch (blue_evt->ecode) { | ||||
|                 aci_gap_pairing_complete_event_rp0 *pairing_complete; | ||||
|  | ||||
|             case EVT_BLUE_GAP_LIMITED_DISCOVERABLE: | ||||
|                 FURI_LOG_I(GAP_TAG, "Limited discoverable event"); | ||||
|                 break; | ||||
|  | ||||
|             case EVT_BLUE_GAP_PASS_KEY_REQUEST: | ||||
|             { | ||||
|                 // Generate random PIN code | ||||
|                 uint32_t pin = rand() % 999999; | ||||
|                 aci_gap_pass_key_resp(gap->gap_svc.connection_handle, pin); | ||||
|                 FURI_LOG_I(GAP_TAG, "Pass key request event. Pin: %d", pin); | ||||
|                 bt_pin_code_show(gap->bt, pin); | ||||
|             } | ||||
|             break; | ||||
|                 break; | ||||
|  | ||||
|             case EVT_LE_CONN_COMPLETE: | ||||
|             furi_hal_power_insomnia_enter(); | ||||
|             hci_le_connection_complete_event_rp0* connection_complete_event = (hci_le_connection_complete_event_rp0 *) meta_evt->data; | ||||
|             FURI_LOG_I(GAP_TAG, "Connection complete for connection handle 0x%x", connection_complete_event->Connection_Handle); | ||||
|             case EVT_BLUE_GAP_AUTHORIZATION_REQUEST: | ||||
|                 FURI_LOG_I(GAP_TAG, "Authorization request event"); | ||||
|                 break; | ||||
|  | ||||
|             // Stop advertising as connection completed | ||||
|             osTimerStop(gap->advertise_timer); | ||||
|             case EVT_BLUE_GAP_SLAVE_SECURITY_INITIATED: | ||||
|                 FURI_LOG_I(GAP_TAG, "Slave security initiated"); | ||||
|                 break; | ||||
|  | ||||
|             // Update connection status and handle | ||||
|             gap->state = GapStateConnected; | ||||
|             gap->gap_svc.connection_handle = connection_complete_event->Connection_Handle; | ||||
|             case EVT_BLUE_GAP_BOND_LOST: | ||||
|                 FURI_LOG_I(GAP_TAG, "Bond lost event. Start rebonding"); | ||||
|                 aci_gap_allow_rebond(gap->gap_svc.connection_handle); | ||||
|                 break; | ||||
|  | ||||
|             // Start pairing by sending security request | ||||
|             aci_gap_slave_security_req(connection_complete_event->Connection_Handle); | ||||
|             break; | ||||
|             case EVT_BLUE_GAP_DEVICE_FOUND: | ||||
|                 FURI_LOG_I(GAP_TAG, "Device found event"); | ||||
|                 break; | ||||
|  | ||||
|             case EVT_BLUE_GAP_ADDR_NOT_RESOLVED: | ||||
|                 FURI_LOG_I(GAP_TAG, "Address not resolved event"); | ||||
|                 break; | ||||
|  | ||||
|             case EVT_BLUE_GAP_KEYPRESS_NOTIFICATION: | ||||
|                 FURI_LOG_I(GAP_TAG, "Key press notification event"); | ||||
|                 break; | ||||
|  | ||||
|             case EVT_BLUE_GAP_NUMERIC_COMPARISON_VALUE: | ||||
|                 FURI_LOG_I(GAP_TAG, "Hex_value = %lx", | ||||
|                             ((aci_gap_numeric_comparison_value_event_rp0 *)(blue_evt->data))->Numeric_Value); | ||||
|                 aci_gap_numeric_comparison_value_confirm_yesno(gap->gap_svc.connection_handle, 1); | ||||
|                 break; | ||||
|  | ||||
|             case EVT_BLUE_GAP_PAIRING_CMPLT: | ||||
|                 pairing_complete = (aci_gap_pairing_complete_event_rp0*)blue_evt->data; | ||||
|                 if (pairing_complete->Status) { | ||||
|                     FURI_LOG_E(GAP_TAG, "Pairing failed with status: %d. Terminating connection", pairing_complete->Status); | ||||
|                     aci_gap_terminate(gap->gap_svc.connection_handle, 5); | ||||
|                 } else { | ||||
|                     FURI_LOG_I(GAP_TAG, "Pairing complete"); | ||||
|                 } | ||||
|                 break; | ||||
|  | ||||
|             case EVT_BLUE_GAP_PROCEDURE_COMPLETE: | ||||
|                 FURI_LOG_I(GAP_TAG, "Procedure complete event"); | ||||
|                 break; | ||||
|             } | ||||
|             default: | ||||
|             break; | ||||
|         } | ||||
|     break; | ||||
|  | ||||
|     case EVT_VENDOR: | ||||
|         blue_evt = (evt_blue_aci*) event_pckt->data; | ||||
|         switch (blue_evt->ecode) { | ||||
|             aci_gap_pairing_complete_event_rp0 *pairing_complete; | ||||
|  | ||||
|         case EVT_BLUE_GAP_LIMITED_DISCOVERABLE: | ||||
|             FURI_LOG_I(GAP_TAG, "Limited discoverable event"); | ||||
|             break; | ||||
|  | ||||
|         case EVT_BLUE_GAP_PASS_KEY_REQUEST: | ||||
|         { | ||||
|             // Generate random PIN code | ||||
|             uint32_t pin = rand() % 999999; | ||||
|             aci_gap_pass_key_resp(gap->gap_svc.connection_handle, pin); | ||||
|             FURI_LOG_I(GAP_TAG, "Pass key request event. Pin: %d", pin); | ||||
|             bt_pin_code_show(gap->bt, pin); | ||||
|         } | ||||
|             break; | ||||
|  | ||||
|         case EVT_BLUE_GAP_AUTHORIZATION_REQUEST: | ||||
|             FURI_LOG_I(GAP_TAG, "Authorization request event"); | ||||
|             break; | ||||
|  | ||||
|         case EVT_BLUE_GAP_SLAVE_SECURITY_INITIATED: | ||||
|             FURI_LOG_I(GAP_TAG, "Slave security initiated"); | ||||
|             break; | ||||
|  | ||||
|         case EVT_BLUE_GAP_BOND_LOST: | ||||
|             FURI_LOG_I(GAP_TAG, "Bond lost event. Start rebonding"); | ||||
|             aci_gap_allow_rebond(gap->gap_svc.connection_handle); | ||||
|             break; | ||||
|  | ||||
|         case EVT_BLUE_GAP_DEVICE_FOUND: | ||||
|             FURI_LOG_I(GAP_TAG, "Device found event"); | ||||
|             break; | ||||
|  | ||||
|         case EVT_BLUE_GAP_ADDR_NOT_RESOLVED: | ||||
|             FURI_LOG_I(GAP_TAG, "Address not resolved event"); | ||||
|             break; | ||||
|  | ||||
|         case EVT_BLUE_GAP_KEYPRESS_NOTIFICATION: | ||||
|             FURI_LOG_I(GAP_TAG, "Key press notification event"); | ||||
|             break; | ||||
|  | ||||
|         case EVT_BLUE_GAP_NUMERIC_COMPARISON_VALUE: | ||||
|             FURI_LOG_I(GAP_TAG, "Hex_value = %lx", | ||||
|                         ((aci_gap_numeric_comparison_value_event_rp0 *)(blue_evt->data))->Numeric_Value); | ||||
|             aci_gap_numeric_comparison_value_confirm_yesno(gap->gap_svc.connection_handle, 1); | ||||
|             break; | ||||
|  | ||||
|         case (EVT_BLUE_GAP_PAIRING_CMPLT): | ||||
|         { | ||||
|             pairing_complete = (aci_gap_pairing_complete_event_rp0*)blue_evt->data; | ||||
|             if (pairing_complete->Status) { | ||||
|             FURI_LOG_E(GAP_TAG, "Pairing failed with status: %d. Terminating connection", pairing_complete->Status); | ||||
|             aci_gap_terminate(gap->gap_svc.connection_handle, 5); | ||||
|             } else { | ||||
|             FURI_LOG_I(GAP_TAG, "Pairing complete"); | ||||
|             } | ||||
|         } | ||||
|             break; | ||||
|  | ||||
|         case EVT_BLUE_GAP_PROCEDURE_COMPLETE: | ||||
|             FURI_LOG_I(GAP_TAG, "Procedure complete event"); | ||||
|             break; | ||||
|         } | ||||
|         default: | ||||
|             break; | ||||
|   } | ||||
|  | ||||
|   return SVCCTL_UserEvtFlowEnable; | ||||
|                 break; | ||||
|     } | ||||
|     osMutexRelease(gap->state_mutex); | ||||
|     return SVCCTL_UserEvtFlowEnable; | ||||
| } | ||||
|  | ||||
| void SVCCTL_SvcInit() { | ||||
| @@ -213,7 +223,7 @@ static void set_advertisment_service_uid(uint8_t* uid, uint8_t uid_len) { | ||||
|     gap->gap_svc.adv_svc_uuid_len += uid_len; | ||||
| } | ||||
|  | ||||
| GapState gap_get_status() { | ||||
| GapState gap_get_state() { | ||||
|     return gap->state; | ||||
| } | ||||
|  | ||||
| @@ -293,7 +303,7 @@ static void gap_init_svc(Gap* gap) { | ||||
|     aci_gap_configure_whitelist(); | ||||
| } | ||||
|  | ||||
| static void gap_advertise(GapState new_state) | ||||
| static void gap_advertise_start(GapState new_state) | ||||
| { | ||||
|     tBleStatus status; | ||||
|     uint16_t min_interval; | ||||
| @@ -317,7 +327,6 @@ static void gap_advertise(GapState new_state) | ||||
|         } | ||||
|     } | ||||
|     // Configure advertising | ||||
|     gap->state = new_state; | ||||
|     const char* name = furi_hal_version_get_ble_local_device_name_ptr(); | ||||
|     status = aci_gap_set_discoverable(ADV_IND, min_interval, max_interval, PUBLIC_ADDR, 0, | ||||
|                                         strlen(name), (uint8_t*)name, | ||||
| @@ -325,17 +334,40 @@ static void gap_advertise(GapState new_state) | ||||
|     if(status) { | ||||
|         FURI_LOG_E(GAP_TAG, "Set discoverable err: %d", status); | ||||
|     } | ||||
|     gap->state = new_state; | ||||
|     bt_update_statusbar(gap->bt); | ||||
|     osTimerStart(gap->advertise_timer, INITIAL_ADV_TIMEOUT); | ||||
| } | ||||
|  | ||||
| static void gap_advertise_request(Gap* gap) { | ||||
|     osThreadFlagsSet(gap->thread_id, 1); | ||||
| static void gap_advertise_stop() { | ||||
|     if(gap->state == GapStateConnected) { | ||||
|         // Terminate connection | ||||
|         aci_gap_terminate(gap->gap_svc.connection_handle, 0x13); | ||||
|     } | ||||
|     if(gap->state > GapStateIdle) { | ||||
|         // Stop advertising | ||||
|         osTimerStop(gap->advertise_timer); | ||||
|         aci_gap_set_non_discoverable(); | ||||
|         gap->state = GapStateIdle; | ||||
|         bt_update_statusbar(gap->bt); | ||||
|     } | ||||
| } | ||||
|  | ||||
| void gap_start_advertising() { | ||||
|     gap->enable_adv = true; | ||||
|     GapCommand command = GapCommandAdvFast; | ||||
|     furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK); | ||||
| } | ||||
|  | ||||
| void gap_stop_advertising() { | ||||
|     gap->enable_adv = false; | ||||
|     GapCommand command = GapCommandAdvStop; | ||||
|     furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK); | ||||
| } | ||||
|  | ||||
| static void gap_advetise_timer_callback(void* context) { | ||||
|     furi_assert(context); | ||||
|     Gap* gap = context; | ||||
|     gap_advertise_request(gap); | ||||
|     GapCommand command = GapCommandAdvLowPower; | ||||
|     furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK); | ||||
| } | ||||
|  | ||||
| bool gap_init() { | ||||
| @@ -348,20 +380,25 @@ bool gap_init() { | ||||
|     // Open Bt record | ||||
|     gap->bt = furi_record_open("bt"); | ||||
|     // Create advertising timer | ||||
|     gap->advertise_timer = osTimerNew(gap_advetise_timer_callback, osTimerOnce, &gap, NULL); | ||||
|     // Initialization of HCI & GATT & GAP layer | ||||
|     gap->advertise_timer = osTimerNew(gap_advetise_timer_callback, osTimerOnce, NULL, NULL); | ||||
|     // Initialization of GATT & GAP layer | ||||
|     gap_init_svc(gap); | ||||
|     // Initialization of the BLE Services | ||||
|     SVCCTL_Init(); | ||||
|     // Initialization of the BLE App Context | ||||
|     // Initialization of the GAP state | ||||
|     gap->state_mutex = osMutexNew(NULL); | ||||
|     gap->state = GapStateIdle; | ||||
|     gap->gap_svc.connection_handle = 0xFFFF; | ||||
|     gap->enable_adv = true; | ||||
|  | ||||
|     // Thread configuration | ||||
|     gap->thread_attr.name = "BLE advertising"; | ||||
|     gap->thread_attr.stack_size = 512; | ||||
|     gap->thread_attr.stack_size = 1024; | ||||
|     gap->thread_id = osThreadNew(gap_app, NULL, &gap->thread_attr); | ||||
|  | ||||
|     // Command queue allocation | ||||
|     gap->command_queue = osMessageQueueNew(8, sizeof(GapCommand), NULL); | ||||
|  | ||||
|     // Start Device Information service | ||||
|     dev_info_svc_start(); | ||||
|     // Start Battery service | ||||
| @@ -374,14 +411,21 @@ bool gap_init() { | ||||
|     adv_service_uid[1] = 0x30; | ||||
|  | ||||
|     set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid)); | ||||
|     gap_advertise(GapStateAdvFast); | ||||
|     return true; | ||||
| } | ||||
|  | ||||
| static void gap_app(void *arg) { | ||||
|     // TODO Exit from app, stop service, clean memory | ||||
|     GapCommand command; | ||||
|     while(1) { | ||||
|         osThreadFlagsWait(1, osFlagsWaitAny, osWaitForever); | ||||
|         gap_advertise(GapStateAdvLowPower); | ||||
|         furi_check(osMessageQueueGet(gap->command_queue, &command, NULL, osWaitForever) == osOK); | ||||
|         osMutexAcquire(gap->state_mutex, osWaitForever); | ||||
|         if(command == GapCommandAdvFast) { | ||||
|             gap_advertise_start(GapStateAdvFast); | ||||
|         } else if(command == GapCommandAdvLowPower) { | ||||
|             gap_advertise_start(GapStateAdvLowPower); | ||||
|         } else if(command == GapCommandAdvStop) { | ||||
|             gap_advertise_stop(); | ||||
|         } | ||||
|         osMutexRelease(gap->state_mutex); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -15,7 +15,11 @@ typedef enum { | ||||
|  | ||||
| bool gap_init(); | ||||
|  | ||||
| GapState gap_get_status(); | ||||
| void gap_start_advertising(); | ||||
|  | ||||
| void gap_stop_advertising(); | ||||
|  | ||||
| GapState gap_get_state(); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
|   | ||||
| @@ -61,7 +61,7 @@ void serial_svc_start() { | ||||
|     status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_tx_uuid, | ||||
|                                 SERIAL_SVC_DATA_LEN_MAX, | ||||
|                                 CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE | CHAR_PROP_READ, | ||||
|                                 ATTR_PERMISSION_NONE, | ||||
|                                 ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE, | ||||
|                                 GATT_NOTIFY_ATTRIBUTE_WRITE, | ||||
|                                 10, | ||||
|                                 CHAR_VALUE_LEN_VARIABLE, | ||||
| @@ -74,7 +74,7 @@ void serial_svc_start() { | ||||
|     status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_rx_uuid, | ||||
|                                 SERIAL_SVC_DATA_LEN_MAX,                                   | ||||
|                                 CHAR_PROP_READ | CHAR_PROP_INDICATE, | ||||
|                                 ATTR_PERMISSION_NONE, | ||||
|                                 ATTR_PERMISSION_AUTHEN_READ, | ||||
|                                 GATT_DONT_NOTIFY_EVENTS, | ||||
|                                 10, | ||||
|                                 CHAR_VALUE_LEN_VARIABLE, | ||||
| @@ -108,13 +108,15 @@ void serial_svc_stop() { | ||||
|  | ||||
|  | ||||
| bool serial_svc_update_rx(uint8_t* data, uint8_t data_len) { | ||||
|     furi_assert(data_len < SERIAL_SVC_DATA_LEN_MAX); | ||||
|     if(data_len > SERIAL_SVC_DATA_LEN_MAX) { | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|     tBleStatus result = aci_gatt_update_char_value(serial_svc->svc_handle, | ||||
|                                           serial_svc->rx_char_handle, | ||||
|                                           0, | ||||
|                                           data_len, | ||||
|                                           data); | ||||
|                                         serial_svc->rx_char_handle, | ||||
|                                         0, | ||||
|                                         data_len, | ||||
|                                         data); | ||||
|     if(result) { | ||||
|         FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed updating RX characteristic: %d", result); | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user