From e0f2898a3c9098db955181d0970b975b6f3e8454 Mon Sep 17 00:00:00 2001 From: SnickerSun Date: Sat, 27 Apr 2024 17:21:46 +0800 Subject: [PATCH] =?utf8?q?=E6=B7=BB=E5=8A=A0ble=20heartrate=20=E6=9C=8D?= =?utf8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../ble_service/bleservice/bleService.c | 22 ++- .../config/target_config/brandy/config.py | 1 + .../sdk/drivers/peripheral/ppg/CMakeLists.txt | 3 +- .../ppg/ppgBleService/CMakeLists.txt | 50 ++++++ .../ppg/ppgBleService/ppgBleService.c | 147 ++++++++++++++++++ .../ppg/ppgBleService/ppgBleService.h | 106 +++++++++++++ 6 files changed, 325 insertions(+), 4 deletions(-) create mode 100644 code/sdk/drivers/peripheral/ppg/ppgBleService/CMakeLists.txt create mode 100644 code/sdk/drivers/peripheral/ppg/ppgBleService/ppgBleService.c create mode 100644 code/sdk/drivers/peripheral/ppg/ppgBleService/ppgBleService.h diff --git a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c index 7c72278ae..b27155032 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c @@ -34,6 +34,7 @@ extern "C" { #include "bleEvent.h" #include "hidService.h" #include "factorytestService.h" +#include "ppgBleService.h" // extern void bts_data_to_uuid_len2(uint16_t uuid_data, bt_uuid_t *out_uuid); // extern void bts_init_128bit_uuid(const bt_uuid_t *uuid); @@ -739,6 +740,9 @@ static void svr_ble_bt_gatts_service_add_cb(uint8_t server_id, bt_uuid_t *uuid, bt_uuid_t factory_service_uuid = {0}; bts_data_to_uuid_len2(BLE_UUID_ATE_FACTORYTEST_SERVICE, &factory_service_uuid); + bt_uuid_t ppg_service_uuid = {0}; + bts_data_to_uuid_len2(BLE_UUID_PPG_BLE_SERVICE, &ppg_service_uuid); + errcode_t ret = 0; if (ble_gatts_cmp_uuid(uuid, &ble_service_uuid) == true) {//通过传入的uuid进行比较 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "svr_ble_bt_gatts_service_add_cb ble_service_uuid\r\n"); @@ -760,6 +764,13 @@ static void svr_ble_bt_gatts_service_add_cb(uint8_t server_id, bt_uuid_t *uuid, ret = gatts_start_service(server_id, srv_handle); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "factorytest service svr_ble_bt_gatts_service_add_cb ret:%d\r\n",ret); } + + if (ble_gatts_cmp_uuid(uuid, &ppg_service_uuid) == true){ + ble_ppgtest_add_characters_and_descriptors(server_id,srv_handle); + ret = gatts_start_service(server_id, srv_handle); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ppg ble service svr_ble_bt_gatts_service_add_cb ret:%d\r\n",ret); + } + if (ble_gatts_cmp_uuid(uuid, &alipay_service_uuid) == true){ alipay_add_chart_decr_recv_cmd1(server_id,srv_handle); ret = gatts_start_service(server_id, srv_handle); @@ -894,8 +905,13 @@ static void svr_ble_bt_gatts_characteristic_add_cb(uint8_t server_id, bt_uuid_t } - - + bt_uuid_t ppgtest_report_uuid = {0}; + bts_data_to_uuid_len2(BLE_UUID_PPG_NOTIFY, &ppgtest_report_uuid); + if (ble_gatts_cmp_uuid(uuid, &ppgtest_report_uuid) == true) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "result->ble_ppgtest_store_report_handle %d\r\n",result->value_handle); + ble_ppgtest_store_report_handle(result->value_handle); + } + return; } @@ -1403,7 +1419,7 @@ void svr_bluetooth_init(void) #endif svr_alipay_bt_init_gatts(); ble_factory_server_init(); - + ble_ppgtest_server_init(); svr_ble_adv_start_param(); ble_recv_register_msg_callback(ble_recv_msg_callback);//注册驱动回调 return; diff --git a/code/sdk/build/config/target_config/brandy/config.py b/code/sdk/build/config/target_config/brandy/config.py index 38b333d39..ac6a2c94e 100755 --- a/code/sdk/build/config/target_config/brandy/config.py +++ b/code/sdk/build/config/target_config/brandy/config.py @@ -155,6 +155,7 @@ target = { 'bt_manager_at_service', "audio_proc", "audio_dump", "audio_probe", "power_manager", "gpu_proc", 'bts_header', "ppg_sensor", + "ppgBleService", "mag_sensor", "vibrator", "vibrator_service", diff --git a/code/sdk/drivers/peripheral/ppg/CMakeLists.txt b/code/sdk/drivers/peripheral/ppg/CMakeLists.txt index 115aa97a3..958ca1f20 100644 --- a/code/sdk/drivers/peripheral/ppg/CMakeLists.txt +++ b/code/sdk/drivers/peripheral/ppg/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory_if_exist(service) -add_subdirectory_if_exist(hx3695) \ No newline at end of file +add_subdirectory_if_exist(hx3695) +add_subdirectory_if_exist(ppgBleService) \ No newline at end of file diff --git a/code/sdk/drivers/peripheral/ppg/ppgBleService/CMakeLists.txt b/code/sdk/drivers/peripheral/ppg/ppgBleService/CMakeLists.txt new file mode 100644 index 000000000..61cbf56ea --- /dev/null +++ b/code/sdk/drivers/peripheral/ppg/ppgBleService/CMakeLists.txt @@ -0,0 +1,50 @@ +#=============================================================================== +# @brief cmake file +# Copyright (c) @CompanyNameMagicTag 2022-2022. All rights reserved. +#=============================================================================== + +set(COMPONENT_NAME "ppgBleService") + +set(PUBLIC_HEADER +${CMAKE_CURRENT_SOURCE_DIR} +) +set(PRIVATE_HEADER + # ${ROOT_DIR}/protocol/bt/host/bt/bts/brandy_service/service/src/base + # ${ROOT_DIR}/protocol/bt/host/bt/bts/bg_common/include/sdk_common + # ${ROOT_DIR}/protocol/bt/host/bt/bts/bg_common/include/gdk_common + # ${ROOT_DIR}/protocol/bt/host/bt/bts/bg_common/include/ahi_common + # ${ROOT_DIR}/protocol/bt/host/bt/bts/bg_common/include/log + # ${ROOT_DIR}/protocol/bt/host/bt/bts/bg_common/include/ahi_a + # ${ROOT_DIR}/protocol/bt/host/bt/bts/bg_common/include/host + + # ${CMAKE_CURRENT_SOURCE_DIR}/../bg_common/include/log + +) + +set(SOURCES +${CMAKE_CURRENT_SOURCE_DIR}/ppgBleService.c +) + +set(COMPONENT_PUBLIC_CCFLAGS +) + +set(COMPONENT_CCFLAGS + +) + +set(WHOLE_LINK + true +) + +set(MAIN_COMPONENT + false +) + +set(PUBLIC_DEFINES + +) + +set(PRIVATE_DEFINES +) + +build_component() diff --git a/code/sdk/drivers/peripheral/ppg/ppgBleService/ppgBleService.c b/code/sdk/drivers/peripheral/ppg/ppgBleService/ppgBleService.c new file mode 100644 index 000000000..105d8b8c6 --- /dev/null +++ b/code/sdk/drivers/peripheral/ppg/ppgBleService/ppgBleService.c @@ -0,0 +1,147 @@ +/* + * Copyright (c) @CompanyNameMagicTag 2022. All rights reserved. + * Description: BT HID Service Server Module. + */ + +#include "ppgBleService.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef SUPPORT_BLE_SERVICE +#include "bleService.h" +#endif +#include "bts_gatt_stru.h" +#include "bts_a2dp_source.h" +#include "bts_gatt_server.h" +#include "securec.h" +#include "wearable_log.h" + +// #include "bts_def.h" +// #include "sdk_general.h" +// #include "bts_gatt_stru.h" +// #include "bts_gatt_server.h" +// #include "ble_gatts_callbacks.h" +// #include "bts_uuid.h" + +#define BLE_HID_SERVER_ID 1 + +/* HID gatt notification flag, do not need confirm */ +#define GATT_NOTIFICATION_FLAG 0 +/* HID gatt indication flag, need confirm */ +#define GATT_INDICATION_FLAG 1 +#define INVALID_SERVER_ID 0 +/* Client Characteristic Configuration UUID */ +#define BLE_UUID_CLIENT_CHARACTERISTIC_CONF 0x2902 +/*user Characteristic Configuration UUID*/ +#define BLE_UUID_USER_CHARACTERISTIC_CONF 0x2901 +/* gatt server ID */ +static uint8_t g_server_id = INVALID_SERVER_ID; +// RX定为Notify属性,TX为Write属性 +enum { + /* HID input report characteristic properties */ + PPGTEST_INPUT_REPORT_PROPERTIES = GATT_CHARACTER_PROPERTY_BIT_NOTIFY|GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP|GATT_CHARACTER_PROPERTY_BIT_WRITE|GATT_CHARACTER_PROPERTY_BIT_READ, + /* HID output report characteristic properties */ + PPGTEST_OUTPUT_REPORT_PROPERTIES = GATT_CHARACTER_PROPERTY_BIT_WRITE|GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP, +}; + +#define uint16_to_byte(n) ((uint8_t)(n)), ((uint8_t)((n) >> 8)) + +/* hid input report att handle */ +uint32_t g_ppgtest_notify_report_att_hdl = 0; +uint32_t ppgtest_ate_write_handl = 0; + +uint32_t g_ppgtest_ota_report_hdl = 0; +uint32_t ppgtest_ota_write_handl = 0; + +/* 创建服务 */ +static void ble_ppgtest_add_service(void) +{ + bt_uuid_t ppgtest_service_uuid = {0}; + bts_data_to_uuid_len2(BLE_UUID_PPG_BLE_SERVICE, &ppgtest_service_uuid); + //只有一个服务,永远是1 + gatts_add_service(BLE_HID_SERVER_ID, &ppgtest_service_uuid, true); +} +static void ble_ppgtest_add_descriptor_report_reference(uint8_t server_id, uint16_t srvc_handle, bool is_notify_report) +{ + /* HID input report reference value for test [report id 1, input] */ + static uint8_t report_reference_val_input[] = {0, 0}; + gatts_add_desc_info_t descriptor; + if (is_notify_report) + { + bts_data_to_uuid_len2(BLE_UUID_CLIENT_CHARACTERISTIC_CONF, &descriptor.desc_uuid); + descriptor.permissions = GATT_ATTRIBUTE_PERMISSION_WRITE ; + }else{ + bts_data_to_uuid_len2(BLE_UUID_USER_CHARACTERISTIC_CONF, &descriptor.desc_uuid); + descriptor.permissions = GATT_ATTRIBUTE_PERMISSION_WRITE | GATT_ATTRIBUTE_PERMISSION_READ ; + + } + descriptor.value = report_reference_val_input; + descriptor.value_len = (uint16_t)sizeof(report_reference_val_input); + + gatts_add_descriptor(server_id, srvc_handle, &descriptor); +} + +/* 添加特征:HID information */ +static void ble_ppgtest_add_character_and_information(uint8_t server_id, uint16_t srvc_handle) +{ + /* HID information value for test */ + gatts_add_chara_info_t character={0}; + + memset_s(&character,sizeof(gatts_add_chara_info_t),0,sizeof(gatts_add_chara_info_t)); + bts_data_to_uuid_len2(BLE_UUID_PPG_NOTIFY, &character.chara_uuid); + character.permissions = GATT_ATTRIBUTE_PERMISSION_READ | GATT_ATTRIBUTE_PERMISSION_WRITE; + character.properties = PPGTEST_INPUT_REPORT_PROPERTIES; + character.value_len = 0;//(uint16_t)sizeof(hid_information_val); + character.value = NULL;//hid_information_val; + gatts_add_characteristic(server_id, srvc_handle, &character); + ble_ppgtest_add_descriptor_report_reference(server_id,srvc_handle,true); + ble_ppgtest_add_descriptor_report_reference(server_id,srvc_handle,false); +} + +/* device向host发送数据:input report */ +errcode_t ble_ppgtest_server_send_input_report(const uint8_t *data, uint8_t len) +{ + // printf("ble_hid_server_send_input_report:%d %d %d",data[0],data[1],len); + gatts_ntf_ind_t param = {0}; + param.attr_handle = (uint16_t)g_ppgtest_notify_report_att_hdl; + param.value_len = len; + param.value = bt_os_new(len); + if (param.value == NULL) { + printf( "[ppgtest][ERROR]send input report new fail\r\n"); + return ERRCODE_BT_MALLOC_FAIL; + } + if (memcpy_s(param.value, param.value_len, data, len) != EOK) { + printf( "[ppgtest][ERROR]send input report memcpy fail\r\n"); + bt_os_free(param.value); + return ERRCODE_BT_FAIL; + } + errcode_t ret = 0; + ret = gatts_notify_indicate(g_ble_server_id, g_ble_conn_id, ¶m); + printf("ble_ppgtest_server_send_input_report: 0x%x g_ble_conn_id:%d g_ppgtest_notify_report_att_hdl:%d\r\n", + ret,g_ble_conn_id,g_ppgtest_notify_report_att_hdl); + bt_os_free(param.value); + return ERRCODE_BT_SUCCESS; +} + + +void ble_ppgtest_store_report_handle(uint16_t srvc_handle) +{ + printf("g_ppgtest_notify_report_att_hdl: 0x%X\r\n",srvc_handle); + g_ppgtest_notify_report_att_hdl = srvc_handle; +} + +void ble_ppgtest_add_characters_and_descriptors(uint8_t server_id, uint16_t srvc_handle) +{ + ble_ppgtest_add_character_and_information(server_id, srvc_handle); +} + +void ble_ppgtest_server_init(void) +{ + ble_ppgtest_add_service(); +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/code/sdk/drivers/peripheral/ppg/ppgBleService/ppgBleService.h b/code/sdk/drivers/peripheral/ppg/ppgBleService/ppgBleService.h new file mode 100644 index 000000000..d988d6820 --- /dev/null +++ b/code/sdk/drivers/peripheral/ppg/ppgBleService/ppgBleService.h @@ -0,0 +1,106 @@ +/* + * Copyright (c) @CompanyNameMagicTag 2022. All rights reserved. + * Description: BT HID Service Server module. + */ + +/** + * @defgroup bluetooth_bts_hid_server HID SERVER API + * @ingroup bluetooth + * @{ + */ +#ifndef PPG_BLE_SERVER_H +#define PPG_BLE_SERVER_H + +#include "errcode.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* UUID */ +#define BLE_UUID_PPG_BLE_SERVICE 0x180D +/* notify */ +#define BLE_UUID_PPG_NOTIFY 0x2A37 + + +/** + * @if Eng + * @brief Use this funtion to init HID service. + * @par Description:add HID service, character and character descriptor, then start service. + * @attention NULL + * @param NULL + * @retval NULL + * @par Dependency: + * @li ble_hid_server.h + * @else + * @brief 初始化HID服务。 + * @par 说明:添加HID服务,特征,特征描述符,然后启动服务。 + * @attention NULL + * @param NULL + * @retval NULL + * @par 依赖: + * @li ble_hid_server.h + * @endif + */ +void ble_ppgtest_server_init(void); + +/** + * @if Eng + * @brief Notify remote device the press button value. + * @par Description:Notify remote device the press button value.. + * @attention NULL + * @param [in] value Press button value. + * @param [in] len Length of press button value。 + * @retval ERRCODE_BT_SUCCESS Excute successfully + * @retval ERRCODE_BT_FAIL Execute fail + * @par Dependency: + * @li ble_hid_server.h + * @else + * @brief 通知对端按键变化。 + * @par 说明:通知对端按键变化。 + * @attention NULL + * @param [in] data 按键值。 + * @param [in] len 按键值数据长度。 + * @retval ERRCODE_BT_SUCCESS 执行成功 + * @retval ERRCODE_BT_FAIL 执行失败 + * @par 依赖: + * @li ble_hid_server.h + * @endif + */ +errcode_t ble_ppgtest_server_send_input_report(const uint8_t *data, uint8_t len); + +/** + * @if Eng + * @brief Hid add characters and descriptors. + * @par Description:Hid add characters and descriptors. + * @attention NULL + * @param [in] server_id server id. + * @param [in] srvc_handle service handle. + * @retval NULL + * @par Dependency: + * @li ble_hid_server.h + * @else + * @brief HID 服务添加特征和描述符。 + * @par 说明:HID 服务添加特征和描述符。 + * @attention NULL + * @param [in] server_id 服务id。 + * @param [in] srvc_handle 服务handle。 + * @retval NULL + * @par 依赖: + * @li ble_hid_server.h + * @endif + */ +void ble_ppgtest_add_characters_and_descriptors(uint8_t server_id, uint16_t srvc_handle); + +void ble_ppgtest_store_report_handle(uint16_t srvc_handle); + + +/** + * @} + */ +#ifdef __cplusplus +} +#endif + + +#endif//BLE_FACTORY_TEST_SERVER_H -- 2.17.1