/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file App/custom_app.c * @author MCD Application Team * @brief Custom Example Application (Server) ****************************************************************************** * @attention * * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "app_common.h" #include "dbg_trace.h" #include "ble.h" #include "custom_app.h" #include "custom_stm.h" #include "stm32_seq.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "b_wb1m_wpan1.h" #include "b_wb1m_wpan1_conf.h" #include "b_wb1m_wpan1_errno.h" #include "env_server_app.h" #include "motenv_server_app.h" #include "motion_server_app.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ typedef struct { /* My_P2P_Server */ uint8_t Switch_c_Notification_Status; uint8_t Long_c_Notification_Status; /* My_Heart_Rate */ // uint8_t Hrs_m_Notification_Status; /* My_BLE_Sensor */ uint8_t Motion_c_Notification_Status; uint8_t Env_c_Notification_Status; /* USER CODE BEGIN CUSTOM_APP_Context_t */ uint8_t SW1_Status; uint8_t TimerMeasurement_Id; uint8_t ResetEnergyExpended; // Custom_STM_HRS_BodySensorLocation_t BodySensorLocationChar; // Custom_STM_HRS_MeasVal_t MeasurementvalueChar; uint8_t AccGyroMag_Update_Timer_Id; uint8_t Env_Update_Timer_Id; uint8_t SW2_Status; /* USER CODE END CUSTOM_APP_Context_t */ uint16_t ConnectionHandle; } Custom_App_Context_t; /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private defines ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ //#define CUSTOM_STM_HRS_VALUE_MIN 0x44 //#define CUSTOM_STM_HRS_VALUE_MAX 0x49 //#define CUSTOM_STM_HRS_ENERGY_MIN 0x0100 //#define CUSTOM_STM_HRS_ENERGY_RESET 0x00 //#define CUSTOM_STM_HRS_ENERGY_NOT_RESET 0x01 #define TOGGLE_ON 1 #define TOGGLE_OFF 0 #define ENVIRONMENT_UPDATE_PERIOD (uint32_t)(0.5*1000*1000/CFG_TS_TICK_VAL) /*500ms*/ #define ACC_GYRO_MAG_UPDATE_PERIOD (uint32_t)(0.05*1000*1000/CFG_TS_TICK_VAL) /*50ms (20Hz)*/ /* USER CODE END PD */ /* Private macros -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ //#define HRS_APP_MEASUREMENT_INTERVAL (1000000/CFG_TS_TICK_VAL) /**< 1s */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /** * START of Section BLE_APP_CONTEXT */ static Custom_App_Context_t Custom_App_Context; /** * END of Section BLE_APP_CONTEXT */ uint8_t UpdateCharData[512]; uint8_t NotifyCharData[512]; uint16_t Connection_Handle; /* USER CODE BEGIN PV */ //uint8_t hr_energy_reset = CUSTOM_STM_HRS_ENERGY_NOT_RESET; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ /* My_P2P_Server */ static void Custom_Switch_c_Update_Char(void); static void Custom_Switch_c_Send_Notification(void); static void Custom_Long_c_Update_Char(void); static void Custom_Long_c_Send_Notification(void); /* My_Heart_Rate */ //static void Custom_Hrs_m_Update_Char(void); //static void Custom_Hrs_m_Send_Notification(void); /* My_BLE_Sensor */ static void Custom_Motion_c_Update_Char(void); static void Custom_Motion_c_Send_Notification(void); static void Custom_Env_c_Update_Char(void); static void Custom_Env_c_Send_Notification(void); /* USER CODE BEGIN PFP */ //static void HrMeas(void); //static uint32_t HRSAPP_Read_RTC_SSR_SS (void); static void MOTENV_AccGyroMagUpdate_Timer_Callback(void); static void MOTENV_EnvUpdate_Timer_Callback(void); static void MOTENV_APP_context_Init(void); /* USER CODE END PFP */ /* Functions Definition ------------------------------------------------------*/ void Custom_STM_App_Notification(Custom_STM_App_Notification_evt_t *pNotification) { /* USER CODE BEGIN CUSTOM_STM_App_Notification_1 */ tBleStatus ret = BLE_STATUS_INVALID_PARAMS; /* USER CODE END CUSTOM_STM_App_Notification_1 */ switch (pNotification->Custom_Evt_Opcode) { /* USER CODE BEGIN CUSTOM_STM_App_Notification_Custom_Evt_Opcode */ /* USER CODE END CUSTOM_STM_App_Notification_Custom_Evt_Opcode */ /* My_P2P_Server */ case CUSTOM_STM_LED_C_READ_EVT: /* USER CODE BEGIN CUSTOM_STM_LED_C_READ_EVT */ APP_DBG_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE My_Led_Char Read\n"); /* USER CODE END CUSTOM_STM_LED_C_READ_EVT */ break; case CUSTOM_STM_LED_C_WRITE_NO_RESP_EVT: /* USER CODE BEGIN CUSTOM_STM_LED_C_WRITE_NO_RESP_EVT */ APP_DBG_MSG("\r\n\r** CUSTOM_STM_LED_C_WRITE_NO_RESP_EVT \n"); APP_DBG_MSG("\r\n\r** Write Data: 0x%02X %02X \n", pNotification->DataTransfered.pPayload[0], pNotification->DataTransfered.pPayload[1]); if(pNotification->DataTransfered.pPayload[1] == 0x01) { BSP_LED_On(LED_BLUE); } if(pNotification->DataTransfered.pPayload[1] == 0x00) { BSP_LED_Off(LED_BLUE); } /* USER CODE END CUSTOM_STM_LED_C_WRITE_NO_RESP_EVT */ break; case CUSTOM_STM_SWITCH_C_NOTIFY_ENABLED_EVT: /* USER CODE BEGIN CUSTOM_STM_SWITCH_C_NOTIFY_ENABLED_EVT */ APP_DBG_MSG("\r\n\r** CUSTOM_STM_BUTTON_C_NOTIFY_ENABLED_EVT \n"); Custom_App_Context.Switch_c_Notification_Status = TOGGLE_ON; /* My_Switch_Char notification status has been enabled */ /* USER CODE END CUSTOM_STM_SWITCH_C_NOTIFY_ENABLED_EVT */ break; case CUSTOM_STM_SWITCH_C_NOTIFY_DISABLED_EVT: /* USER CODE BEGIN CUSTOM_STM_SWITCH_C_NOTIFY_DISABLED_EVT */ APP_DBG_MSG("\r\n\r** CUSTOM_STM_BUTTON_C_NOTIFY_DISABLED_EVT \n"); Custom_App_Context.Switch_c_Notification_Status = TOGGLE_OFF; /* My_Switch_Char notification status has been disabled */ /* USER CODE END CUSTOM_STM_SWITCH_C_NOTIFY_DISABLED_EVT */ break; case CUSTOM_STM_LONG_C_NOTIFY_ENABLED_EVT: /* USER CODE BEGIN CUSTOM_STM_LONG_C_NOTIFY_ENABLED_EVT */ APP_DBG_MSG("\r\n\r** CUSTOM_STM_LONG_C_NOTIFY_ENABLED_EVT \n"); Custom_App_Context.Long_c_Notification_Status = TOGGLE_ON; ret = aci_gatt_exchange_config(Connection_Handle); if (ret != BLE_STATUS_SUCCESS) { APP_DBG_MSG("aci_gatt_exchange_config failure: reason=0x%02X\n", ret); } else { APP_DBG_MSG("==>> aci_gatt_exchange_config : Success\n"); } /* USER CODE END CUSTOM_STM_LONG_C_NOTIFY_ENABLED_EVT */ break; case CUSTOM_STM_LONG_C_NOTIFY_DISABLED_EVT: /* USER CODE BEGIN CUSTOM_STM_LONG_C_NOTIFY_DISABLED_EVT */ APP_DBG_MSG("\r\n\r** CUSTOM_STM_LONG_C_NOTIFY_DISABLED_EVT \n"); Custom_App_Context.Long_c_Notification_Status = TOGGLE_OFF; /* USER CODE END CUSTOM_STM_LONG_C_NOTIFY_DISABLED_EVT */ break; /* My_BLE_Sensor */ case CUSTOM_STM_MOTION_C_NOTIFY_ENABLED_EVT: /* USER CODE BEGIN CUSTOM_STM_MOTION_C_NOTIFY_ENABLED_EVT */ MOTION_Set_Notification_Status(1); APP_DBG_MSG("-- TEMPLATE APPLICATION SERVER : MOTION NOTIFICATION ENABLED\n"); APP_DBG_MSG(" \n\r"); /* Start the timer used to update the AccGyroMag characteristic */ HW_TS_Start(Custom_App_Context.AccGyroMag_Update_Timer_Id, ACC_GYRO_MAG_UPDATE_PERIOD); /* USER CODE END CUSTOM_STM_MOTION_C_NOTIFY_ENABLED_EVT */ break; case CUSTOM_STM_MOTION_C_NOTIFY_DISABLED_EVT: /* USER CODE BEGIN CUSTOM_STM_MOTION_C_NOTIFY_DISABLED_EVT */ MOTION_Set_Notification_Status(0); APP_DBG_MSG("-- TEMPLATE APPLICATION SERVER : MOTION NOTIFICATION DISABLED\n"); APP_DBG_MSG(" \n\r"); /* Stop the timer used to update the Motion characteristic */ HW_TS_Stop(Custom_App_Context.AccGyroMag_Update_Timer_Id); /* USER CODE END CUSTOM_STM_MOTION_C_NOTIFY_DISABLED_EVT */ break; case CUSTOM_STM_ENV_C_READ_EVT: /* USER CODE BEGIN CUSTOM_STM_ENV_C_READ_EVT */ ENV_Update(); APP_DBG_MSG("-- TEMPLATE APPLICATION SERVER : ENV READ\n"); APP_DBG_MSG(" \n\r"); /* USER CODE END CUSTOM_STM_ENV_C_READ_EVT */ break; case CUSTOM_STM_ENV_C_NOTIFY_ENABLED_EVT: /* USER CODE BEGIN CUSTOM_STM_ENV_C_NOTIFY_ENABLED_EVT */ ENV_Set_Notification_Status(1); APP_DBG_MSG("-- TEMPLATE APPLICATION SERVER : ENV NOTIFICATION ENABLED\n"); APP_DBG_MSG(" \n\r"); /* Start the timer used to update the Env characteristic */ HW_TS_Start(Custom_App_Context.Env_Update_Timer_Id, ENVIRONMENT_UPDATE_PERIOD); /* USER CODE END CUSTOM_STM_ENV_C_NOTIFY_ENABLED_EVT */ break; case CUSTOM_STM_ENV_C_NOTIFY_DISABLED_EVT: /* USER CODE BEGIN CUSTOM_STM_ENV_C_NOTIFY_DISABLED_EVT */ ENV_Set_Notification_Status(0); APP_DBG_MSG("-- TEMPLATE APPLICATION SERVER : ENV NOTIFICATION DISABLED\n"); APP_DBG_MSG(" \n\r"); /* Stop the timer used to update the Env characteristic */ HW_TS_Stop(Custom_App_Context.Env_Update_Timer_Id); /* USER CODE END CUSTOM_STM_ENV_C_NOTIFY_DISABLED_EVT */ break; case CUSTOM_STM_NOTIFICATION_COMPLETE_EVT: /* USER CODE BEGIN CUSTOM_STM_NOTIFICATION_COMPLETE_EVT */ /* USER CODE END CUSTOM_STM_NOTIFICATION_COMPLETE_EVT */ break; default: /* USER CODE BEGIN CUSTOM_STM_App_Notification_default */ /* USER CODE END CUSTOM_STM_App_Notification_default */ break; } /* USER CODE BEGIN CUSTOM_STM_App_Notification_2 */ /* USER CODE END CUSTOM_STM_App_Notification_2 */ return; } void Custom_APP_Notification(Custom_App_ConnHandle_Not_evt_t *pNotification) { /* USER CODE BEGIN CUSTOM_APP_Notification_1 */ /* USER CODE END CUSTOM_APP_Notification_1 */ switch (pNotification->Custom_Evt_Opcode) { /* USER CODE BEGIN CUSTOM_APP_Notification_Custom_Evt_Opcode */ /* USER CODE END P2PS_CUSTOM_Notification_Custom_Evt_Opcode */ case CUSTOM_CONN_HANDLE_EVT : /* USER CODE BEGIN CUSTOM_CONN_HANDLE_EVT */ Connection_Handle = pNotification->ConnectionHandle; /* USER CODE END CUSTOM_CONN_HANDLE_EVT */ break; case CUSTOM_DISCON_HANDLE_EVT : /* USER CODE BEGIN CUSTOM_DISCON_HANDLE_EVT */ Connection_Handle = pNotification->ConnectionHandle; /* USER CODE END CUSTOM_DISCON_HANDLE_EVT */ break; default: /* USER CODE BEGIN CUSTOM_APP_Notification_default */ /* USER CODE END CUSTOM_APP_Notification_default */ break; } /* USER CODE BEGIN CUSTOM_APP_Notification_2 */ /* USER CODE END CUSTOM_APP_Notification_2 */ return; } void Custom_APP_Init(void) { /* USER CODE BEGIN CUSTOM_APP_Init */ // Custom_App_Context.ResetEnergyExpended = 0; // Custom_App_Context.BodySensorLocationChar = CUSTOM_STM_HRS_BODY_SENSOR_LOCATION_HAND; // Custom_STM_App_Update_Char(CUSTOM_STM_HRS_SL, (uint8_t *) &Custom_App_Context.BodySensorLocationChar); //initialise My_Sensor_Loc char to wrist // Custom_Switch_c_Update_Char(); // Custom_Hrs_m_Update_Char(); UTIL_SEQ_RegTask(1<< CFG_TASK_SW1_BUTTON_PUSHED_ID, UTIL_SEQ_RFU, Custom_Switch_c_Send_Notification); // UTIL_SEQ_RegTask(1<< CFG_TASK_MEAS_REQ_ID, UTIL_SEQ_RFU, Custom_Hrs_m_Send_Notification); UTIL_SEQ_RegTask(1<< CFG_TASK_SW2_BUTTON_PUSHED_ID, UTIL_SEQ_RFU, Custom_Long_c_Send_Notification); Custom_App_Context.Switch_c_Notification_Status = TOGGLE_OFF; Custom_App_Context.SW1_Status = 0; /** * Set Flags for measurement value */ /* Custom_App_Context.MeasurementvalueChar.Flags = ( CUSTOM_STM_HRS_HRM_VALUE_FORMAT_UINT16 | CUSTOM_STM_HRS_HRM_SENSOR_CONTACTS_PRESENT | CUSTOM_STM_HRS_HRM_SENSOR_CONTACTS_SUPPORTED | CUSTOM_STM_HRS_HRM_ENERGY_EXPENDED_PRESENT | CUSTOM_STM_HRS_HRM_RR_INTERVAL_PRESENT ); #if (BLE_CFG_HRS_ENERGY_EXPENDED_INFO_FLAG != 0) if(Custom_App_Context.MeasurementvalueChar.Flags & CUSTOM_STM_HRS_HRM_ENERGY_EXPENDED_PRESENT) { Custom_App_Context.ResetEnergyExpended = 0; Custom_App_Context.MeasurementvalueChar.EnergyExpended = 10; } #endif #if (BLE_CFG_HRS_ENERGY_RR_INTERVAL_FLAG != 0) if(Custom_App_Context.MeasurementvalueChar.Flags & HRS_HRM_RR_INTERVAL_PRESENT) { uint8_t i; Custom_App_Context.MeasurementvalueChar.NbreOfValidRRIntervalValues = BLE_CFG_HRS_ENERGY_RR_INTERVAL_FLAG; for(i = 0; i < BLE_CFG_HRS_ENERGY_RR_INTERVAL_FLAG; i++) { Custom_App_Context.MeasurementvalueChar.aRRIntervalValues[i] = 1024; } } #endif */ /** * Create timer for Heart Rate Measurement */ // HW_TS_Create(CFG_TIM_PROC_ID_ISR, &(Custom_App_Context.TimerMeasurement_Id), hw_ts_Repeated, HrMeas); UTIL_SEQ_RegTask( 1<