/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file App/custom_stm.c * @author MCD Application Team * @brief Custom Example Service. ****************************************************************************** * @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 "common_blesvc.h" #include "custom_stm.h" /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ typedef struct{ uint16_t CustomTestsHdle; /**< Test_Serv handle */ uint16_t CustomTestcHdle; /**< Test_Char handle */ /* USER CODE BEGIN Context */ /* Place holder for Characteristic Descriptors Handle*/ /* USER CODE END Context */ }CustomContext_t; /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private defines -----------------------------------------------------------*/ #define UUID_128_SUPPORTED 1 #if (UUID_128_SUPPORTED == 1) #define BM_UUID_LENGTH UUID_TYPE_128 #else #define BM_UUID_LENGTH UUID_TYPE_16 #endif #define BM_REQ_CHAR_SIZE (3) /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macros ------------------------------------------------------------*/ #define CHARACTERISTIC_DESCRIPTOR_ATTRIBUTE_OFFSET 2 #define CHARACTERISTIC_VALUE_ATTRIBUTE_OFFSET 1 /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ uint8_t SizeTestc = 1; /** * START of Section BLE_DRIVER_CONTEXT */ static CustomContext_t CustomContext; /** * END of Section BLE_DRIVER_CONTEXT */ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ static SVCCTL_EvtAckStatus_t Custom_STM_Event_Handler(void *pckt); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Functions Definition ------------------------------------------------------*/ /* USER CODE BEGIN PFD */ /* USER CODE END PFD */ /* Private functions ----------------------------------------------------------*/ #define COPY_UUID_128(uuid_struct, uuid_15, uuid_14, uuid_13, uuid_12, uuid_11, uuid_10, uuid_9, uuid_8, uuid_7, uuid_6, uuid_5, uuid_4, uuid_3, uuid_2, uuid_1, uuid_0) \ do {\ uuid_struct[0] = uuid_0; uuid_struct[1] = uuid_1; uuid_struct[2] = uuid_2; uuid_struct[3] = uuid_3; \ uuid_struct[4] = uuid_4; uuid_struct[5] = uuid_5; uuid_struct[6] = uuid_6; uuid_struct[7] = uuid_7; \ uuid_struct[8] = uuid_8; uuid_struct[9] = uuid_9; uuid_struct[10] = uuid_10; uuid_struct[11] = uuid_11; \ uuid_struct[12] = uuid_12; uuid_struct[13] = uuid_13; uuid_struct[14] = uuid_14; uuid_struct[15] = uuid_15; \ }while(0) #define COPY_TEST_SERV_UUID(uuid_struct) COPY_UUID_128(uuid_struct,0x00,0x00,0x00,0x00,0xcc,0x7a,0x48,0x2a,0x98,0x4a,0x7f,0x2e,0xd5,0xb3,0xe5,0x8f) #define COPY_TEST_CHAR_UUID(uuid_struct) COPY_UUID_128(uuid_struct,0x00,0x00,0x00,0x00,0x8e,0x22,0x45,0x41,0x9d,0x4c,0x21,0xed,0xae,0x82,0xed,0x19) /* USER CODE BEGIN PF */ /* USER CODE END PF */ /** * @brief Event handler * @param Event: Address of the buffer holding the Event * @retval Ack: Return whether the Event has been managed or not */ static SVCCTL_EvtAckStatus_t Custom_STM_Event_Handler(void *Event) { SVCCTL_EvtAckStatus_t return_value; hci_event_pckt *event_pckt; evt_blecore_aci *blecore_evt; aci_gatt_notification_complete_event_rp0 *notification_complete; Custom_STM_App_Notification_evt_t Notification; /* USER CODE BEGIN Custom_STM_Event_Handler_1 */ /* USER CODE END Custom_STM_Event_Handler_1 */ return_value = SVCCTL_EvtNotAck; event_pckt = (hci_event_pckt *)(((hci_uart_pckt*)Event)->data); switch (event_pckt->evt) { case HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE: blecore_evt = (evt_blecore_aci*)event_pckt->data; switch (blecore_evt->ecode) { case ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE: /* USER CODE BEGIN EVT_BLUE_GATT_ATTRIBUTE_MODIFIED_BEGIN */ /* USER CODE END EVT_BLUE_GATT_ATTRIBUTE_MODIFIED_BEGIN */ /* USER CODE BEGIN EVT_BLUE_GATT_ATTRIBUTE_MODIFIED_END */ /* USER CODE END EVT_BLUE_GATT_ATTRIBUTE_MODIFIED_END */ break; case ACI_GATT_READ_PERMIT_REQ_VSEVT_CODE : /* USER CODE BEGIN EVT_BLUE_GATT_READ_PERMIT_REQ_BEGIN */ /* USER CODE END EVT_BLUE_GATT_READ_PERMIT_REQ_BEGIN */ /* USER CODE BEGIN EVT_BLUE_GATT_READ_PERMIT_REQ_END */ /* USER CODE END EVT_BLUE_GATT_READ_PERMIT_REQ_END */ break; case ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE: /* USER CODE BEGIN EVT_BLUE_GATT_WRITE_PERMIT_REQ_BEGIN */ /* USER CODE END EVT_BLUE_GATT_WRITE_PERMIT_REQ_BEGIN */ /* USER CODE BEGIN EVT_BLUE_GATT_WRITE_PERMIT_REQ_END */ /* USER CODE END EVT_BLUE_GATT_WRITE_PERMIT_REQ_END */ break; case ACI_GATT_NOTIFICATION_COMPLETE_VSEVT_CODE: { /* USER CODE BEGIN EVT_BLUE_GATT_NOTIFICATION_COMPLETE_BEGIN */ /* USER CODE END EVT_BLUE_GATT_NOTIFICATION_COMPLETE_BEGIN */ notification_complete = (aci_gatt_notification_complete_event_rp0*)blecore_evt->data; Notification.Custom_Evt_Opcode = CUSTOM_STM_NOTIFICATION_COMPLETE_EVT; Notification.AttrHandle = notification_complete->Attr_Handle; Custom_STM_App_Notification(&Notification); /* USER CODE BEGIN EVT_BLUE_GATT_NOTIFICATION_COMPLETE_END */ /* USER CODE END EVT_BLUE_GATT_NOTIFICATION_COMPLETE_END */ break; } /* USER CODE BEGIN BLECORE_EVT */ /* USER CODE END BLECORE_EVT */ default: /* USER CODE BEGIN EVT_DEFAULT */ /* USER CODE END EVT_DEFAULT */ break; } /* USER CODE BEGIN EVT_VENDOR*/ /* USER CODE END EVT_VENDOR*/ break; /* HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE */ /* USER CODE BEGIN EVENT_PCKT_CASES*/ /* USER CODE END EVENT_PCKT_CASES*/ default: /* USER CODE BEGIN EVENT_PCKT*/ /* USER CODE END EVENT_PCKT*/ break; } /* USER CODE BEGIN Custom_STM_Event_Handler_2 */ /* USER CODE END Custom_STM_Event_Handler_2 */ return(return_value); }/* end Custom_STM_Event_Handler */ /* Public functions ----------------------------------------------------------*/ /** * @brief Service initialization * @param None * @retval None */ void SVCCTL_InitCustomSvc(void) { Char_UUID_t uuid; tBleStatus ret = BLE_STATUS_INVALID_PARAMS; uint8_t max_attr_record; /* USER CODE BEGIN SVCCTL_InitCustomSvc_1 */ // Test Text if gets deleted /* USER CODE END SVCCTL_InitCustomSvc_1 */ /** * Register the event handler to the BLE controller */ SVCCTL_RegisterSvcHandler(Custom_STM_Event_Handler); /** * Test_Serv * * Max_Attribute_Records = 1 + 2*1 + 1*no_of_char_with_notify_or_indicate_property + 1*no_of_char_with_broadcast_property * service_max_attribute_record = 1 for Test_Serv + * 2 for Test_Char + * = 3 * * This value doesn't take into account number of descriptors manually added * In case of descriptors added, please update the max_attr_record value accordingly in the next SVCCTL_InitService User Section */ max_attr_record = 3; /* USER CODE BEGIN SVCCTL_InitService */ /* max_attr_record to be updated if descriptors have been added */ // Test Text if gets deleted /* USER CODE END SVCCTL_InitService */ COPY_TEST_SERV_UUID(uuid.Char_UUID_128); ret = aci_gatt_add_service(UUID_TYPE_128, (Service_UUID_t *) &uuid, PRIMARY_SERVICE, max_attr_record, &(CustomContext.CustomTestsHdle)); if (ret != BLE_STATUS_SUCCESS) { APP_DBG_MSG(" Fail : aci_gatt_add_service command: TESTS, error code: 0x%x \n\r", ret); } else { APP_DBG_MSG(" Success: aci_gatt_add_service command: TESTS \n\r"); } /** * Test_Char */ COPY_TEST_CHAR_UUID(uuid.Char_UUID_128); ret = aci_gatt_add_char(CustomContext.CustomTestsHdle, UUID_TYPE_128, &uuid, SizeTestc, CHAR_PROP_NONE, ATTR_PERMISSION_NONE, GATT_NOTIFY_ATTRIBUTE_WRITE | GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP | GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP, 0x10, CHAR_VALUE_LEN_CONSTANT, &(CustomContext.CustomTestcHdle)); if (ret != BLE_STATUS_SUCCESS) { APP_DBG_MSG(" Fail : aci_gatt_add_char command : TESTC, error code: 0x%x \n\r", ret); } else { APP_DBG_MSG(" Success: aci_gatt_add_char command : TESTC \n\r"); } /* USER CODE BEGIN SVCCTL_Init_Service1_Char1/ */ /* Place holder for Characteristic Descriptors */ // Test Text if gets deleted /* USER CODE END SVCCTL_Init_Service1_Char1 */ /* USER CODE BEGIN SVCCTL_InitCustomSvc_2 */ // Test Text if gets deleted /* USER CODE END SVCCTL_InitCustomSvc_2 */ return; } /** * @brief Characteristic update * @param CharOpcode: Characteristic identifier * @param Service_Instance: Instance of the service to which the characteristic belongs * */ tBleStatus Custom_STM_App_Update_Char(Custom_STM_Char_Opcode_t CharOpcode, uint8_t *pPayload) { tBleStatus ret = BLE_STATUS_INVALID_PARAMS; /* USER CODE BEGIN Custom_STM_App_Update_Char_1 */ /* USER CODE END Custom_STM_App_Update_Char_1 */ switch (CharOpcode) { case CUSTOM_STM_TESTC: ret = aci_gatt_update_char_value(CustomContext.CustomTestsHdle, CustomContext.CustomTestcHdle, 0, /* charValOffset */ SizeTestc, /* charValueLen */ (uint8_t *) pPayload); if (ret != BLE_STATUS_SUCCESS) { APP_DBG_MSG(" Fail : aci_gatt_update_char_value TESTC command, result : 0x%x \n\r", ret); } else { APP_DBG_MSG(" Success: aci_gatt_update_char_value TESTC command\n\r"); } /* USER CODE BEGIN CUSTOM_STM_App_Update_Service_1_Char_1*/ /* USER CODE END CUSTOM_STM_App_Update_Service_1_Char_1*/ break; default: break; } /* USER CODE BEGIN Custom_STM_App_Update_Char_2 */ /* USER CODE END Custom_STM_App_Update_Char_2 */ return ret; }