app_conf.h: 添加task id
app_entry.c:初始化BLE传输层和BSP(例如LED和按钮)
app_ble.c:初始化GAP并管理连接(例如广播和扫描)
p2p_server_app.c:初始化GATT并管理应用。
app_conf.h: 添加task id, 枚举变量直接赋值
cpp
typedef enum
{
CFG_TASK_ADV_CANCEL_ID,
#if (L2CAP_REQUEST_NEW_CONN_PARAM != 0 )
CFG_TASK_CONN_UPDATE_REG_ID,
#endif
CFG_TASK_HCI_ASYNCH_EVT_ID,
/* USER CODE BEGIN CFG_Task_Id_With_HCI_Cmd_t */
CFG_TASK_SW1_BUTTON_PUSHED_ID,
/* USER CODE END CFG_Task_Id_With_HCI_Cmd_t */
CFG_LAST_TASK_ID_WITH_HCICMD, /**< Shall be LAST in the list */
} CFG_Task_Id_With_HCI_Cmd_t;
app_entry.c 初始化BLE传输层和BSP(例如LED和KEY)
cpp
void MX_APPE_Init(void)
{
System_Init(); /**< System initialization */
SystemPower_Config(); /**< Configure the system Power Mode */
HW_TS_Init(hw_ts_InitMode_Full, &hrtc); /**< Initialize the TimerServer */
/* USER CODE BEGIN APPE_Init_1 */
APPD_Init();
/*** The Standby mode should not be entered before the initialization is over
* The default state of the Low Power Manager is to allow the Standby Mode so an request is needed here
*/
UTIL_LPM_SetOffMode(1 << CFG_LPM_APP, UTIL_LPM_DISABLE);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
/* USER CODE END APPE_Init_1 */
按键PD1配置为外部中断,添加中断处理函数
cpp
/* USER CODE BEGIN FD_WRAP_FUNCTIONS */
void HAL_GPIO_EXTI_Callback( uint16_t GPIO_Pin )
{
switch (GPIO_Pin)
{
case GPIO_PIN_1:
UTIL_SEQ_SetTask( 1<<CFG_TASK_SW1_BUTTON_PUSHED_ID, CFG_SCH_PRIO_0);
break;
// case BUTTON_SW2_PIN:
// APP_BLE_Key_Button2_Action();
// break;
// case BUTTON_SW3_PIN:
// APP_BLE_Key_Button3_Action();
// break;
default:
break;
}
return;
}
/* USER CODE END FD_WRAP_FUNCTIONS */
app_ble.c 配对事件
cpp
switch (p_blecore_evt->ecode)
{
/* USER CODE BEGIN ecode */
aci_gap_pairing_complete_event_rp0 *pairing_complete;
case ACI_GAP_LIMITED_DISCOVERABLE_VSEVT_CODE:
APP_DBG_MSG(">>== ACI_GAP_LIMITED_DISCOVERABLE_VSEVT_CODE \n");
break; /* ACI_GAP_LIMITED_DISCOVERABLE_VSEVT_CODE */
case ACI_GAP_PASS_KEY_REQ_VSEVT_CODE:
APP_DBG_MSG(">>== ACI_GAP_PASS_KEY_REQ_VSEVT_CODE \n");
ret = aci_gap_pass_key_resp(BleApplicationContext.BleApplicationContext_legacy.connectionHandle,123456);
if (ret != BLE_STATUS_SUCCESS)
{
APP_DBG_MSG("==>> aci_gap_pass_key_resp : Fail, reason: 0x%x\n", ret);
}
else
{
APP_DBG_MSG("==>> aci_gap_pass_key_resp : Success \n");
}
break; /* ACI_GAP_PASS_KEY_REQ_VSEVT_CODE */
case ACI_GAP_AUTHORIZATION_REQ_VSEVT_CODE:
APP_DBG_MSG(">>== ACI_GAP_AUTHORIZATION_REQ_VSEVT_CODE\n");
break; /* ACI_GAP_AUTHORIZATION_REQ_VSEVT_CODE */
case ACI_GAP_PERIPHERAL_SECURITY_INITIATED_VSEVT_CODE:
APP_DBG_MSG("==>> ACI_GAP_PERIPHERAL_SECURITY_INITIATED_VSEVT_CODE \n");
break; /* ACI_GAP_PERIPHERAL_SECURITY_INITIATED_VSEVT_CODE */
case ACI_GAP_BOND_LOST_VSEVT_CODE:
APP_DBG_MSG("==>> ACI_GAP_BOND_LOST_VSEVT_CODE \n");
ret = aci_gap_allow_rebond(BleApplicationContext.BleApplicationContext_legacy.connectionHandle);
if (ret != BLE_STATUS_SUCCESS)
{
APP_DBG_MSG("==>> aci_gap_allow_rebond : Fail, reason: 0x%x\n", ret);
}
else
{
APP_DBG_MSG("==>> aci_gap_allow_rebond : Success \n");
}
break; /* ACI_GAP_BOND_LOST_VSEVT_CODE */
case ACI_GAP_ADDR_NOT_RESOLVED_VSEVT_CODE:
APP_DBG_MSG(">>== ACI_GAP_ADDR_NOT_RESOLVED_VSEVT_CODE \n");
break; /* ACI_GAP_ADDR_NOT_RESOLVED_VSEVT_CODE */
case (ACI_GAP_KEYPRESS_NOTIFICATION_VSEVT_CODE):
APP_DBG_MSG(">>== ACI_GAP_KEYPRESS_NOTIFICATION_VSEVT_CODE\n");
break; /* ACI_GAP_KEYPRESS_NOTIFICATION_VSEVT_CODE */
case (ACI_GAP_NUMERIC_COMPARISON_VALUE_VSEVT_CODE):
APP_DBG_MSG(">>== ACI_GAP_NUMERIC_COMPARISON_VALUE_VSEVT_CODE\n");
APP_DBG_MSG(" - numeric_value = %ld\n",
((aci_gap_numeric_comparison_value_event_rp0 *)(p_blecore_evt->data))->Numeric_Value);
APP_DBG_MSG(" - Hex_value = %lx\n",
((aci_gap_numeric_comparison_value_event_rp0 *)(p_blecore_evt->data))->Numeric_Value);
ret = aci_gap_numeric_comparison_value_confirm_yesno(BleApplicationContext.BleApplicationContext_legacy.connectionHandle, YES); /* CONFIRM_YES = 1 */
if (ret != BLE_STATUS_SUCCESS)
{
APP_DBG_MSG("==>> aci_gap_numeric_comparison_value_confirm_yesno-->YES : Fail, reason: 0x%x\n", ret);
}
else
{
APP_DBG_MSG("==>> aci_gap_numeric_comparison_value_confirm_yesno-->YES : Success \n");
}
break;
case (ACI_GAP_PAIRING_COMPLETE_VSEVT_CODE):
{
pairing_complete = (aci_gap_pairing_complete_event_rp0*)p_blecore_evt->data;
APP_DBG_MSG(">>== ACI_GAP_PAIRING_COMPLETE_VSEVT_CODE\n");
if (pairing_complete->Status == 0)
{
APP_DBG_MSG(" - Pairing Success\n");
}
else
{
APP_DBG_MSG(" - Pairing KO \n - Status: 0x%x\n - Reason: 0x%x\n",pairing_complete->Status, pairing_complete->Reason);
}
APP_DBG_MSG("\n");
}
break;
/* USER CODE END ecode */
/**
* SPECIFIC to P2P Server APP
*/
case ACI_L2CAP_CONNECTION_UPDATE_RESP_VSEVT_CODE:
#if (L2CAP_REQUEST_NEW_CONN_PARAM != 0)
mutex = 1;
#endif /* L2CAP_REQUEST_NEW_CONN_PARAM != 0 */
/* USER CODE BEGIN EVT_BLUE_L2CAP_CONNECTION_UPDATE_RESP */
/* USER CODE END EVT_BLUE_L2CAP_CONNECTION_UPDATE_RESP */
break;
case ACI_GAP_PROC_COMPLETE_VSEVT_CODE:
APP_DBG_MSG(">>== ACI_GAP_PROC_COMPLETE_VSEVT_CODE \r");
/* USER CODE BEGIN EVT_BLUE_GAP_PROCEDURE_COMPLETE */
/* USER CODE END EVT_BLUE_GAP_PROCEDURE_COMPLETE */
break; /* ACI_GAP_PROC_COMPLETE_VSEVT_CODE */
#if (RADIO_ACTIVITY_EVENT != 0)
case ACI_HAL_END_OF_RADIO_ACTIVITY_VSEVT_CODE:
/* USER CODE BEGIN RADIO_ACTIVITY_EVENT*/
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);//BSP_LED_On(LED_GREEN);
HW_TS_Start(BleApplicationContext.SwitchOffGPIO_timer_Id, (uint32_t)LED_ON_TIMEOUT);
/* USER CODE END RADIO_ACTIVITY_EVENT*/
break; /* ACI_HAL_END_OF_RADIO_ACTIVITY_VSEVT_CODE */
#endif /* RADIO_ACTIVITY_EVENT != 0 */
p2p_servier_app.c
cpp
/* 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 ButtonStatus;
}P2P_ButtonCharValue_t;
typedef struct
{
uint8_t Notification_Status; /* used to check if P2P Server is enabled to Notify */
P2P_LedCharValue_t LedControl;
P2P_ButtonCharValue_t ButtonControl;
uint16_t ConnectionHandle;
} P2P_Server_App_Context_t;
/* USER CODE END PTD */
实例化
cpp
/* USER CODE BEGIN PV */
/**
* START of Section BLE_APP_CONTEXT
*/
static P2P_Server_App_Context_t P2P_Server_App_Context;
/**
* END of Section BLE_APP_CONTEXT
*/
/* USER CODE END PV */
变量初始化与按键动作函数声明
cpp
/* USER CODE BEGIN PFP */
static void P2PS_Send_Notification(void);
static void P2PS_APP_LED_BUTTON_context_Init(void);
/* USER CODE END PFP */
通知函数
cpp
void P2PS_STM_App_Notification(P2PS_STM_App_Notification_evt_t *pNotification)
{
/* USER CODE BEGIN P2PS_STM_App_Notification_1 */
/* USER CODE END P2PS_STM_App_Notification_1 */
switch(pNotification->P2P_Evt_Opcode)
{
/* USER CODE BEGIN P2PS_STM_App_Notification_P2P_Evt_Opcode */
#if(BLE_CFG_OTA_REBOOT_CHAR != 0)
case P2PS_STM_BOOT_REQUEST_EVT:
APP_DBG_MSG("-- P2P APPLICATION SERVER : BOOT REQUESTED\n");
APP_DBG_MSG(" \n\r");
*(uint32_t*)SRAM1_BASE = *(uint32_t*)pNotification->DataTransfered.pPayload;
NVIC_SystemReset();
break;
#endif
/* USER CODE END P2PS_STM_App_Notification_P2P_Evt_Opcode */
case P2PS_STM__NOTIFY_ENABLED_EVT:
/* USER CODE BEGIN P2PS_STM__NOTIFY_ENABLED_EVT */
P2P_Server_App_Context.Notification_Status = 1;
APP_DBG_MSG("-- P2P APPLICATION SERVER : NOTIFICATION ENABLED\n");
APP_DBG_MSG(" \n\r");
/* USER CODE END P2PS_STM__NOTIFY_ENABLED_EVT */
break;
case P2PS_STM_NOTIFY_DISABLED_EVT:
/* USER CODE BEGIN P2PS_STM_NOTIFY_DISABLED_EVT */
P2P_Server_App_Context.Notification_Status = 0;
APP_DBG_MSG("-- P2P APPLICATION SERVER : NOTIFICATION DISABLED\n");
APP_DBG_MSG(" \n\r");
/* USER CODE END P2PS_STM_NOTIFY_DISABLED_EVT */
break;
case P2PS_STM_WRITE_EVT:
/* USER CODE BEGIN P2PS_STM_WRITE_EVT */
if(pNotification->DataTransfered.pPayload[0] == 0x00){ /* ALL Deviceselected - may be necessary as LB Routeur informs all connection */
if(pNotification->DataTransfered.pPayload[1] == 0x01)
{
BSP_LED_On(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER : LED1 ON\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x01; /* LED1 ON */
}
if(pNotification->DataTransfered.pPayload[1] == 0x00)
{
BSP_LED_Off(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER : LED1 OFF\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x00; /* LED1 OFF */
}
}
#if(P2P_SERVER1 != 0)
if(pNotification->DataTransfered.pPayload[0] == 0x01){ /* end device 1 selected - may be necessary as LB Routeur informs all connection */
if(pNotification->DataTransfered.pPayload[1] == 0x01)
{
BSP_LED_On(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 1 : LED1 ON\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x01; /* LED1 ON */
}
if(pNotification->DataTransfered.pPayload[1] == 0x00)
{
BSP_LED_Off(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 1 : LED1 OFF\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x00; /* LED1 OFF */
}
}
#endif
#if(P2P_SERVER2 != 0)
if(pNotification->DataTransfered.pPayload[0] == 0x02){ /* end device 2 selected */
if(pNotification->DataTransfered.pPayload[1] == 0x01)
{
BSP_LED_On(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 2 : LED1 ON\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x01; /* LED1 ON */
}
if(pNotification->DataTransfered.pPayload[1] == 0x00)
{
BSP_LED_Off(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 2 : LED1 OFF\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x00; /* LED1 OFF */
}
}
#endif
#if(P2P_SERVER3 != 0)
if(pNotification->DataTransfered.pPayload[0] == 0x03){ /* end device 3 selected - may be necessary as LB Routeur informs all connection */
if(pNotification->DataTransfered.pPayload[1] == 0x01)
{
BSP_LED_On(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 3 : LED1 ON\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x01; /* LED1 ON */
}
if(pNotification->DataTransfered.pPayload[1] == 0x00)
{
BSP_LED_Off(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 3 : LED1 OFF\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x00; /* LED1 OFF */
}
}
#endif
#if(P2P_SERVER4 != 0)
if(pNotification->DataTransfered.pPayload[0] == 0x04){ /* end device 4 selected */
if(pNotification->DataTransfered.pPayload[1] == 0x01)
{
BSP_LED_On(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 2 : LED1 ON\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x01; /* LED1 ON */
}
if(pNotification->DataTransfered.pPayload[1] == 0x00)
{
BSP_LED_Off(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 2 : LED1 OFF\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x00; /* LED1 OFF */
}
}
#endif
#if(P2P_SERVER5 != 0)
if(pNotification->DataTransfered.pPayload[0] == 0x05){ /* end device 5 selected - may be necessary as LB Routeur informs all connection */
if(pNotification->DataTransfered.pPayload[1] == 0x01)
{
BSP_LED_On(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 5 : LED1 ON\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x01; /* LED1 ON */
}
if(pNotification->DataTransfered.pPayload[1] == 0x00)
{
BSP_LED_Off(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 5 : LED1 OFF\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x00; /* LED1 OFF */
}
}
#endif
#if(P2P_SERVER6 != 0)
if(pNotification->DataTransfered.pPayload[0] == 0x06){ /* end device 6 selected */
if(pNotification->DataTransfered.pPayload[1] == 0x01)
{
BSP_LED_On(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 6 : LED1 ON\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x01; /* LED1 ON */
}
if(pNotification->DataTransfered.pPayload[1] == 0x00)
{
BSP_LED_Off(LED_BLUE);
APP_DBG_MSG("-- P2P APPLICATION SERVER 6 : LED1 OFF\n");
APP_DBG_MSG(" \n\r");
P2P_Server_App_Context.LedControl.Led1=0x00; /* LED1 OFF */
}
}
#endif
/* USER CODE END P2PS_STM_WRITE_EVT */
break;
default:
/* USER CODE BEGIN P2PS_STM_App_Notification_default */
/* USER CODE END P2PS_STM_App_Notification_default */
break;
}
/* USER CODE BEGIN P2PS_STM_App_Notification_2 */
/* USER CODE END P2PS_STM_App_Notification_2 */
return;
}
cpp
void P2PS_APP_Notification(P2PS_APP_ConnHandle_Not_evt_t *pNotification)
{
/* USER CODE BEGIN P2PS_APP_Notification_1 */
/* USER CODE END P2PS_APP_Notification_1 */
switch(pNotification->P2P_Evt_Opcode)
{
/* USER CODE BEGIN P2PS_APP_Notification_P2P_Evt_Opcode */
/* USER CODE END P2PS_APP_Notification_P2P_Evt_Opcode */
case PEER_CONN_HANDLE_EVT :
/* USER CODE BEGIN PEER_CONN_HANDLE_EVT */
/* USER CODE END PEER_CONN_HANDLE_EVT */
break;
case PEER_DISCON_HANDLE_EVT :
/* USER CODE BEGIN PEER_DISCON_HANDLE_EVT */
P2PS_APP_LED_BUTTON_context_Init();
/* USER CODE END PEER_DISCON_HANDLE_EVT */
break;
default:
/* USER CODE BEGIN P2PS_APP_Notification_default */
/* USER CODE END P2PS_APP_Notification_default */
break;
}
/* USER CODE BEGIN P2PS_APP_Notification_2 */
/* USER CODE END P2PS_APP_Notification_2 */
return;
}
调用初始化函数
cpp
void P2PS_APP_Init(void)
{
/* USER CODE BEGIN P2PS_APP_Init */
UTIL_SEQ_RegTask( 1<< CFG_TASK_SW1_BUTTON_PUSHED_ID, UTIL_SEQ_RFU, P2PS_Send_Notification );
/**
* Initialize LedButton Service
*/
P2P_Server_App_Context.Notification_Status=0;
P2PS_APP_LED_BUTTON_context_Init();
/* USER CODE END P2PS_APP_Init */
return;
}
/* USER CODE BEGIN FD */
void P2PS_APP_LED_BUTTON_context_Init(void){
BSP_LED_Off(LED_BLUE);
APP_DBG_MSG("LED BLUE OFF\n");
#if(P2P_SERVER1 != 0)
P2P_Server_App_Context.LedControl.Device_Led_Selection=0x01; /* Device1 */
P2P_Server_App_Context.LedControl.Led1=0x00; /* led OFF */
P2P_Server_App_Context.ButtonControl.Device_Button_Selection=0x01;/* Device1 */
P2P_Server_App_Context.ButtonControl.ButtonStatus=0x00;
#endif
#if(P2P_SERVER2 != 0)
P2P_Server_App_Context.LedControl.Device_Led_Selection=0x02; /* Device2 */
P2P_Server_App_Context.LedControl.Led1=0x00; /* led OFF */
P2P_Server_App_Context.ButtonControl.Device_Button_Selection=0x02;/* Device2 */
P2P_Server_App_Context.ButtonControl.ButtonStatus=0x00;
#endif
#if(P2P_SERVER3 != 0)
P2P_Server_App_Context.LedControl.Device_Led_Selection=0x03; /* Device3 */
P2P_Server_App_Context.LedControl.Led1=0x00; /* led OFF */
P2P_Server_App_Context.ButtonControl.Device_Button_Selection=0x03; /* Device3 */
P2P_Server_App_Context.ButtonControl.ButtonStatus=0x00;
#endif
#if(P2P_SERVER4 != 0)
P2P_Server_App_Context.LedControl.Device_Led_Selection=0x04; /* Device4 */
P2P_Server_App_Context.LedControl.Led1=0x00; /* led OFF */
P2P_Server_App_Context.ButtonControl.Device_Button_Selection=0x04; /* Device4 */
P2P_Server_App_Context.ButtonControl.ButtonStatus=0x00;
#endif
#if(P2P_SERVER5 != 0)
P2P_Server_App_Context.LedControl.Device_Led_Selection=0x05; /* Device5 */
P2P_Server_App_Context.LedControl.Led1=0x00; /* led OFF */
P2P_Server_App_Context.ButtonControl.Device_Button_Selection=0x05; /* Device5 */
P2P_Server_App_Context.ButtonControl.ButtonStatus=0x00;
#endif
#if(P2P_SERVER6 != 0)
P2P_Server_App_Context.LedControl.Device_Led_Selection=0x06; /* device6 */
P2P_Server_App_Context.LedControl.Led1=0x00; /* led OFF */
P2P_Server_App_Context.ButtonControl.Device_Button_Selection=0x06; /* Device6 */
P2P_Server_App_Context.ButtonControl.ButtonStatus=0x00;
#endif
}
void P2PS_APP_SW1_Button_Action(void)
{
UTIL_SEQ_SetTask( 1<<CFG_TASK_SW1_BUTTON_PUSHED_ID, CFG_SCH_PRIO_0);
return;
}
/* USER CODE END FD */
发送通知
cpp
/*************************************************************
*
* LOCAL FUNCTIONS
*
*************************************************************/
/* USER CODE BEGIN FD_LOCAL_FUNCTIONS*/
void P2PS_Send_Notification(void)
{
if(P2P_Server_App_Context.ButtonControl.ButtonStatus == 0x00){
P2P_Server_App_Context.ButtonControl.ButtonStatus=0x01;
} else {
P2P_Server_App_Context.ButtonControl.ButtonStatus=0x00;
}
if(P2P_Server_App_Context.Notification_Status){
APP_DBG_MSG("-- P2P APPLICATION SERVER : INFORM CLIENT BUTTON 1 PUSHED \n ");
APP_DBG_MSG(" \n\r");
P2PS_STM_App_Update_Char(P2P_NOTIFY_CHAR_UUID, (uint8_t *)&P2P_Server_App_Context.ButtonControl);
} else {
APP_DBG_MSG("-- P2P APPLICATION SERVER : CAN'T INFORM CLIENT - NOTIFICATION DISABLED\n ");
}
return;
}
/* USER CODE END FD_LOCAL_FUNCTIONS*/