diff --git a/ble-client-seq.puml b/ble-client-seq.puml new file mode 100644 index 0000000..810d17e --- /dev/null +++ b/ble-client-seq.puml @@ -0,0 +1,85 @@ +@startuml +== Button pressed == +participant unknown as uk +participant main +participant interrupt as it +participant app_entry +participant app_ble +participant p2p_client_app +participant stm32_seq +it -> app_entry : HAL_GPIO_EXTI_Callback() +app_entry -> app_ble : APP_BLE_Key_Button1_Action() +app_ble -> p2p_client_app : P2PC_APP_SW1_Button_Action() +p2p_client_app -> stm32_seq : UTIL_SEQ_SetTask(CFG_TASK_SW1_BUTTON_PUSHED_ID) + +...waiting for task gets executed.... + +stm32_seq -> p2p_client_app : Button_Trigger_Received() +p2p_client_app -> p2p_client_app : toggleButtonStatus() +p2p_client_app -> p2p_client_app : Write_Char(P2P_WRITE_CHAR_UUID, 0, buttonStatus) +activate p2p_client_app +p2p_client_app -> "?b" : aci_gatt_write_without_resp() +deactivate p2p_client_app + +== Event received == +"?a" -> p2p_client_app : Event_Handler() +activate p2p_client_app +opt event == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE +opt event->core_event == ACI_GATT_PROC_COMPLETE_VSEVT_CODE +opt aP2PClientContext[index].connHandle != pr->Connection_Handle +p2p_client_app -> stm32_seq : UTIL_SEQ_SetTask(CFG_TASK_SEARCH_SERVICE_ID) +end +end +end +deactivate p2p_client_app + +...waiting for task gets executed.... + +stm32_seq -> p2p_client_app : Update_Service() + +...... +uk -> app_entry : UTIL_SEQ_Idle() +uk -> app_entry : shci_notify_asynch_evt() +activate app_entry + app_entry -> stm32_seq : UTIL_SEQ_SetTask(CFG_TASK_SYSTEM_HCI_ASYNCH_EVT_ID) +deactivate app_entry +uk -> app_entry : shci_cmd_resp_release() +activate app_entry + app_entry -> stm32_seq : UTIL_SEQ_SetEvt(CFG_IDLEEVT_SYSTEM_HCI_CMD_EVT_RSP_ID) +deactivate app_entry +uk -> app_entry : shci_cmd_resp_wait() +activate app_entry + app_entry -> stm32_seq : UTIL_SEQ_WaitEvt(CFG_IDLEEVT_SYSTEM_HCI_CMD_EVT_RSP_ID) +deactivate app_entry + +== INIT == +main -> stm32_seq : UTIL_SEQ_Run(UTIL_SEQ_DEFAULT) +activate stm32_seq +stm32_seq -> shci_tl : shci_user_evt_proc() + activate shci_tl + shci_tl -> app_entry : APPE_SysUserEvtRx() + activate app_entry + app_entry -> app_debug : APPD_EnableCPU2() + app_entry -> app_ble : APP_BLE_Init() + activate app_ble + app_ble -> app_ble : Ble_Tl_Init() + app_ble -> stm32_seq : UTIL_SEQ_RegTask(CFG_TASK_HCI_ASYNCH_EVT_ID, UTIL_SEQ_RFU, hci_user_evt_proc) + app_ble -> shci : SHCI_C2_BLE_Init() + app_ble -> app_ble : Ble_Hci_Gap_Gatt_Init() + app_ble -> svc_ctl : SVCCTL_Init() + app_ble -> stm32_seq : UTIL_SEQ_RegTask(CFG_TASK_START_SCAN_ID, UTIL_SEQ_RFU, Scan_Request) + app_ble -> stm32_seq : UTIL_SEQ_RegTask(CFG_TASK_CONN_DEV_1_ID, UTIL_SEQ_RFU, Connect_Request) + app_ble -> uk : aci_hal_set_radio_activity_mask() + app_ble -> app_p2p_client : P2PC_APP_Init() + activate app_p2p_client + app_p2p_client -> svc_ctl : SVCCTL_RegisterCltHandler(Event_Handler) + deactivate app_p2p_client + app_ble -> hw_timeserver : HW_TS_Create(CFG_TIM_PROC_ID_ISR, &(BleApplicationContext.SwitchOffGPIO_timer_Id), hw_ts_SingleShot, Switch_OFF_GPIO) + deactivate app_ble + deactivate app_entry + deactivate shci_tl +stm32_seq -> stm32_seq : UTIL_SEQ_PreIdle() +stm32_seq -> stm32_seq : UTIL_SEQ_Idle() +stm32_seq -> stm32_seq : UTIL_SEQ_PostIdle() +deactivate stm32_seq +@enduml \ No newline at end of file diff --git a/nucleo-wb55-dongle-ble/Core/Src/main.c b/nucleo-wb55-dongle-ble/Core/Src/main.c index e9d753c..0ff8440 100644 --- a/nucleo-wb55-dongle-ble/Core/Src/main.c +++ b/nucleo-wb55-dongle-ble/Core/Src/main.c @@ -114,14 +114,15 @@ int main(void) /* Init code for STM32_WPAN */ APPE_Init(); + HAL_Delay(1000); /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { - HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET); - HAL_Delay(20); - HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); - HAL_Delay(20); + // HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET); + // HAL_Delay(20); + // HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); + // HAL_Delay(20); UTIL_SEQ_Run(UTIL_SEQ_DEFAULT); pop_and_execute_commands(); diff --git a/nucleo-wb55-dongle-ble/STM32_WPAN/App/app_ble.c b/nucleo-wb55-dongle-ble/STM32_WPAN/App/app_ble.c index 43dec43..83618b1 100644 --- a/nucleo-wb55-dongle-ble/STM32_WPAN/App/app_ble.c +++ b/nucleo-wb55-dongle-ble/STM32_WPAN/App/app_ble.c @@ -360,7 +360,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt ) int k = 0; uint8_t adtype, adlength; - log_debug("SVCCTL_App_Notification", "enter, event=%n", 1, event_pckt->evt); + log_debug("SVCCTL_App_Notification", "enter, event=%d", 1, event_pckt->evt); switch (event_pckt->evt) { /* USER CODE BEGIN evt */ @@ -391,6 +391,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt ) { /* USER CODE BEGIN GAP_GENERAL_DISCOVERY_PROC */ + log_debug("SVCCTL_App_Notification", "-- GAP GENERAL DISCOVERY PROCEDURE_COMPLETED", 0); /* USER CODE END GAP_GENERAL_DISCOVERY_PROC */ APP_DBG_MSG("-- GAP GENERAL DISCOVERY PROCEDURE_COMPLETED\n"); /*if a device found, connect to it, device 1 being chosen first if both found*/ @@ -427,7 +428,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt ) 0x01); if(result != BLE_STATUS_SUCCESS) { /* USER CODE BEGIN BLE_STATUS_SUCCESS */ - + log_debug("SVCCTL_App_Notification", "BLE_STATUS_SUCCESS: false", 0); /* USER CODE END BLE_STATUS_SUCCESS */ } aci_hal_set_radio_activity_mask(0x0020); @@ -439,6 +440,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt ) case 0x0004: { /* USER CODE BEGIN RADIO_ACTIVITY_EVENT */ + log_debug("SVCCTL_App_Notification", "RADIO_ACTIVITY_EVENT", 0); HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET); HAL_Delay(5); HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); @@ -647,7 +649,21 @@ APP_BLE_ConnStatus_t APP_BLE_Get_Client_Connection_Status( uint16_t Connection_H return APP_BLE_IDLE; } /* USER CODE BEGIN FD */ - +void APP_BLE_Key_Button1_Action(void) +{ +#if OOB_DEMO == 0 + P2PC_APP_SW1_Button_Action(); +#else + if(P2P_Client_APP_Get_State () != APP_BLE_CONNECTED_CLIENT) + { + UTIL_SEQ_SetTask(1 << CFG_TASK_START_SCAN_ID, CFG_SCH_PRIO_0); + } + else + { + P2PC_APP_SW1_Button_Action(); + } +#endif +} /* USER CODE END FD */ /************************************************************* * @@ -808,7 +824,7 @@ static void Scan_Request( void ) { /* USER CODE BEGIN BLE_SCAN_SUCCESS */ - log_debug("Scan_Request", "START GENERAL DISCOVERY (SCAN)", 0); + log_debug("Scan_Request", "START GENERAL DISCOVERY (SCAN)", 0); /* USER CODE END BLE_SCAN_SUCCESS */ APP_DBG_MSG(" \r\n\r** START GENERAL DISCOVERY (SCAN) ** \r\n\r"); } diff --git a/nucleo-wb55-dongle-ble/STM32_WPAN/App/app_ble.h b/nucleo-wb55-dongle-ble/STM32_WPAN/App/app_ble.h index 11282d0..3338041 100644 --- a/nucleo-wb55-dongle-ble/STM32_WPAN/App/app_ble.h +++ b/nucleo-wb55-dongle-ble/STM32_WPAN/App/app_ble.h @@ -80,7 +80,7 @@ extern "C" { APP_BLE_ConnStatus_t APP_BLE_Get_Client_Connection_Status( uint16_t Connection_Handle ); /* USER CODE BEGIN EF */ - + void APP_BLE_Key_Button1_Action(void); /* USER CODE END EF */ #ifdef __cplusplus diff --git a/nucleo-wb55-dongle-ble/STM32_WPAN/App/p2p_client_app.c b/nucleo-wb55-dongle-ble/STM32_WPAN/App/p2p_client_app.c index 43acef8..c4fba4a 100644 --- a/nucleo-wb55-dongle-ble/STM32_WPAN/App/p2p_client_app.c +++ b/nucleo-wb55-dongle-ble/STM32_WPAN/App/p2p_client_app.c @@ -106,7 +106,28 @@ typedef struct }P2P_ClientContext_t; /* USER CODE BEGIN PTD */ +typedef struct{ + uint8_t Device_Led_Selection; + uint8_t Led1; +}P2P_LedCharValue_t; +typedef struct{ + uint8_t Device_Button_Selection; + uint8_t Button1; +}P2P_ButtonCharValue_t; + +typedef struct +{ + + uint8_t Notification_Status; /* used to chek if P2P Server is enabled to Notify */ + + P2P_LedCharValue_t LedControl; + P2P_ButtonCharValue_t ButtonStatus; + + uint16_t ConnectionHandle; + + +} P2P_Client_App_Context_t; /* USER CODE END PTD */ /* Private defines ------------------------------------------------------------*/ @@ -133,14 +154,16 @@ PLACE_IN_SECTION("BLE_APP_CONTEXT") static P2P_ClientContext_t aP2PClientContext * END of Section BLE_APP_CONTEXT */ /* USER CODE BEGIN PV */ - +PLACE_IN_SECTION("BLE_APP_CONTEXT") static P2P_Client_App_Context_t P2P_Client_App_Context; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ static void Gatt_Notification(P2P_Client_App_Notification_evt_t *pNotification); static SVCCTL_EvtAckStatus_t Event_Handler(void *Event); /* USER CODE BEGIN PFP */ - +static tBleStatus Write_Char(uint16_t UUID, uint8_t Service_Instance, uint8_t *pPayload); +static void Button_Trigger_Received( void ); +static void Update_Service( void ); /* USER CODE END PFP */ /* Functions Definition ------------------------------------------------------*/ @@ -153,7 +176,20 @@ void P2PC_APP_Init(void) { uint8_t index =0; /* USER CODE BEGIN P2PC_APP_Init_1 */ + log_debug("P2PC_APP_Init", "enter", 0); + UTIL_SEQ_RegTask( 1<< CFG_TASK_SEARCH_SERVICE_ID, UTIL_SEQ_RFU, Update_Service ); + UTIL_SEQ_RegTask( 1<< CFG_TASK_SW1_BUTTON_PUSHED_ID, UTIL_SEQ_RFU, Button_Trigger_Received ); + /** + * Initialize LedButton Service + */ + P2P_Client_App_Context.Notification_Status=0; + P2P_Client_App_Context.ConnectionHandle = 0x00; + + P2P_Client_App_Context.LedControl.Device_Led_Selection=0x00;/* device Led */ + P2P_Client_App_Context.LedControl.Led1=0x00; /* led OFF */ + P2P_Client_App_Context.ButtonStatus.Device_Button_Selection=0x01;/* Device1 */ + P2P_Client_App_Context.ButtonStatus.Button1=0x00; /* USER CODE END P2PC_APP_Init_1 */ for(index = 0; index < BLE_CFG_CLT_MAX_NBR_CB; index++) { @@ -188,13 +224,26 @@ void P2PC_APP_Notification(P2PC_APP_ConnHandle_Not_evt_t *pNotification) case PEER_CONN_HANDLE_EVT : /* USER CODE BEGIN PEER_CONN_HANDLE_EVT */ - + P2P_Client_App_Context.ConnectionHandle = pNotification->ConnectionHandle; /* USER CODE END PEER_CONN_HANDLE_EVT */ break; case PEER_DISCON_HANDLE_EVT : /* USER CODE BEGIN PEER_DISCON_HANDLE_EVT */ - + { + uint8_t index = 0; + P2P_Client_App_Context.ConnectionHandle = 0x00; + while((index < BLE_CFG_CLT_MAX_NBR_CB) && + (aP2PClientContext[index].state != APP_BLE_IDLE)) + { + aP2PClientContext[index].state = APP_BLE_IDLE; + } + // BSP_LED_Off(LED_BLUE); + +#if OOB_DEMO == 0 + UTIL_SEQ_SetTask(1<DataTransfered.pPayload[0]; + switch(P2P_Client_App_Context.LedControl.Device_Led_Selection) { + + case 0x01 : { + + P2P_Client_App_Context.LedControl.Led1=pNotification->DataTransfered.pPayload[1]; + + if(P2P_Client_App_Context.LedControl.Led1==0x00){ + // BSP_LED_Off(LED_BLUE); + log_debug("Gatt_Notification", " -- P2P APPLICATION CLIENT : NOTIFICATION RECEIVED - LED OFF ", 0); + } else { + log_debug("Gatt_Notification", " -- P2P APPLICATION CLIENT : NOTIFICATION RECEIVED - LED ON", 0); + // BSP_LED_On(LED_BLUE); + } + + break; + } + default : break; + } + } /* USER CODE END P2P_NOTIFICATION_INFO_RECEIVED_EVT */ break; @@ -546,7 +619,124 @@ uint8_t P2P_Client_APP_Get_State( void ) { return aP2PClientContext[0].state; } /* USER CODE BEGIN LF */ +/** + * @brief Feature Characteristic update + * @param pFeatureValue: The address of the new value to be written + * @retval None + */ +tBleStatus Write_Char(uint16_t UUID, uint8_t Service_Instance, uint8_t *pPayload) +{ + + tBleStatus ret = BLE_STATUS_INVALID_PARAMS; + uint8_t index; + + index = 0; + while((index < BLE_CFG_CLT_MAX_NBR_CB) && + (aP2PClientContext[index].state != APP_BLE_IDLE)) + { + + switch(UUID) + { + case P2P_WRITE_CHAR_UUID: /* SERVER RX -- so CLIENT TX */ + ret =aci_gatt_write_without_resp(aP2PClientContext[index].connHandle, + aP2PClientContext[index].P2PWriteToServerCharHdle, + 2, /* charValueLen */ + (uint8_t *) pPayload); + + break; + + default: + break; + } + index++; + } + + return ret; +}/* end Write_Char() */ + +void Button_Trigger_Received(void) +{ + log_debug("Button_Trigger_Received", "-- P2P APPLICATION CLIENT : BUTTON PUSHED - WRITE TO SERVER ", 0); + if(P2P_Client_App_Context.ButtonStatus.Button1==0x00){ + P2P_Client_App_Context.ButtonStatus.Button1=0x01; + }else { + P2P_Client_App_Context.ButtonStatus.Button1=0x00; + } + + Write_Char( P2P_WRITE_CHAR_UUID, 0, (uint8_t *)&P2P_Client_App_Context.ButtonStatus); + + return; +} + +void Update_Service() +{ + log_debug("Update_Service", "enter", 0); + uint16_t enable = 0x0001; + uint16_t disable = 0x0000; + + uint8_t index; + + index = 0; + while((index < BLE_CFG_CLT_MAX_NBR_CB) && + (aP2PClientContext[index].state != APP_BLE_IDLE)) + { + + switch(aP2PClientContext[index].state) + { + + case APP_BLE_DISCOVER_SERVICES: + log_debug("Update_Service", "P2P_DISCOVER_SERVICES", 0); + break; + case APP_BLE_DISCOVER_CHARACS: + log_debug("Update_Service", "* GATT : Discover P2P Characteristics", 0); + aci_gatt_disc_all_char_of_service(aP2PClientContext[index].connHandle, + aP2PClientContext[index].P2PServiceHandle, + aP2PClientContext[index].P2PServiceEndHandle); + + break; + case APP_BLE_DISCOVER_WRITE_DESC: /* Not Used - No decriptor */ + log_debug("Update_Service", "* GATT : Discover Descriptor of TX - Write Characteritic", 0); + aci_gatt_disc_all_char_desc(aP2PClientContext[index].connHandle, + aP2PClientContext[index].P2PWriteToServerCharHdle, + aP2PClientContext[index].P2PWriteToServerCharHdle+2); + + break; + case APP_BLE_DISCOVER_NOTIFICATION_CHAR_DESC: + log_debug("Update_Service", "* GATT : Discover Descriptor of Rx - Notification Characteritic", 0); + aci_gatt_disc_all_char_desc(aP2PClientContext[index].connHandle, + aP2PClientContext[index].P2PNotificationCharHdle, + aP2PClientContext[index].P2PNotificationCharHdle+2); + + break; + case APP_BLE_ENABLE_NOTIFICATION_DESC: + log_debug("Update_Service", "* GATT : Enable Server Notification", 0); + aci_gatt_write_char_desc(aP2PClientContext[index].connHandle, + aP2PClientContext[index].P2PNotificationDescHandle, + 2, + (uint8_t *)&enable); + + aP2PClientContext[index].state = APP_BLE_CONNECTED_CLIENT; + // BSP_LED_Off(LED_RED); + + break; + case APP_BLE_DISABLE_NOTIFICATION_DESC : + log_debug("Update_Service", "* GATT : Disable Server Notification", 0); + aci_gatt_write_char_desc(aP2PClientContext[index].connHandle, + aP2PClientContext[index].P2PNotificationDescHandle, + 2, + (uint8_t *)&disable); + + aP2PClientContext[index].state = APP_BLE_CONNECTED_CLIENT; + + break; + default: + break; + } + index++; + } + return; +} /* USER CODE END LF */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/nucleo-wb55-dongle-ble/STM32_WPAN/App/p2p_client_app.h b/nucleo-wb55-dongle-ble/STM32_WPAN/App/p2p_client_app.h index 0814a40..4664ffb 100644 --- a/nucleo-wb55-dongle-ble/STM32_WPAN/App/p2p_client_app.h +++ b/nucleo-wb55-dongle-ble/STM32_WPAN/App/p2p_client_app.h @@ -70,7 +70,7 @@ void P2PC_APP_Init( void ); void P2PC_APP_Notification( P2PC_APP_ConnHandle_Not_evt_t *pNotification ); uint8_t P2P_Client_APP_Get_State( void ); /* USER CODE BEGIN EFP */ - +void P2PC_APP_SW1_Button_Action(void); /* USER CODE END EFP */ #ifdef __cplusplus