添加ble heartrate 服务 100/target/1 99/1 99/head
authorSnickerSun <snickersun@163.com>
Sat, 27 Apr 2024 09:21:46 +0000 (17:21 +0800)
committerSnickerSun <snickersun@163.com>
Sat, 27 Apr 2024 09:21:46 +0000 (17:21 +0800)
code/sdk/application/wearable/service/ble_service/bleservice/bleService.c
code/sdk/build/config/target_config/brandy/config.py
code/sdk/drivers/peripheral/ppg/CMakeLists.txt
code/sdk/drivers/peripheral/ppg/ppgBleService/CMakeLists.txt [new file with mode: 0644]
code/sdk/drivers/peripheral/ppg/ppgBleService/ppgBleService.c [new file with mode: 0644]
code/sdk/drivers/peripheral/ppg/ppgBleService/ppgBleService.h [new file with mode: 0644]

index 7c72278..b271550 100644 (file)
@@ -34,6 +34,7 @@ extern "C" {
 #include "bleEvent.h"\r
 #include "hidService.h"\r
 #include "factorytestService.h"\r
+#include "ppgBleService.h"\r
 \r
 // extern void bts_data_to_uuid_len2(uint16_t uuid_data, bt_uuid_t *out_uuid);\r
 // extern void bts_init_128bit_uuid(const bt_uuid_t *uuid);\r
@@ -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};\r
     bts_data_to_uuid_len2(BLE_UUID_ATE_FACTORYTEST_SERVICE, &factory_service_uuid);\r
 \r
+    bt_uuid_t ppg_service_uuid = {0};\r
+    bts_data_to_uuid_len2(BLE_UUID_PPG_BLE_SERVICE, &ppg_service_uuid);\r
+\r
     errcode_t ret = 0;\r
     if (ble_gatts_cmp_uuid(uuid, &ble_service_uuid) == true) {//通过传入的uuid进行比较\r
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "svr_ble_bt_gatts_service_add_cb ble_service_uuid\r\n");\r
@@ -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);\r
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "factorytest service svr_ble_bt_gatts_service_add_cb ret:%d\r\n",ret);\r
     }\r
+\r
+    if (ble_gatts_cmp_uuid(uuid, &ppg_service_uuid) == true){\r
+        ble_ppgtest_add_characters_and_descriptors(server_id,srv_handle);\r
+        ret = gatts_start_service(server_id, srv_handle);\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ppg ble service svr_ble_bt_gatts_service_add_cb ret:%d\r\n",ret);\r
+    }\r
+\r
     if (ble_gatts_cmp_uuid(uuid, &alipay_service_uuid) == true){\r
         alipay_add_chart_decr_recv_cmd1(server_id,srv_handle);\r
         ret = gatts_start_service(server_id, srv_handle);\r
@@ -894,8 +905,13 @@ static void svr_ble_bt_gatts_characteristic_add_cb(uint8_t server_id, bt_uuid_t
     }\r
 \r
 \r
-\r
-\r
+    bt_uuid_t ppgtest_report_uuid = {0};\r
+    bts_data_to_uuid_len2(BLE_UUID_PPG_NOTIFY, &ppgtest_report_uuid);\r
+    if (ble_gatts_cmp_uuid(uuid, &ppgtest_report_uuid) == true) {\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "result->ble_ppgtest_store_report_handle  %d\r\n",result->value_handle);\r
+        ble_ppgtest_store_report_handle(result->value_handle);\r
+    }\r
+    \r
     return;\r
 }\r
 \r
@@ -1403,7 +1419,7 @@ void svr_bluetooth_init(void)
     #endif\r
     svr_alipay_bt_init_gatts();\r
     ble_factory_server_init();\r
-\r
+    ble_ppgtest_server_init();\r
     svr_ble_adv_start_param();\r
     ble_recv_register_msg_callback(ble_recv_msg_callback);//注册驱动回调\r
     return;\r
index 38b333d..ac6a2c9 100755 (executable)
@@ -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",
index 115aa97..958ca1f 100644 (file)
@@ -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 (file)
index 0000000..61cbf56
--- /dev/null
@@ -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 (file)
index 0000000..105d8b8
--- /dev/null
@@ -0,0 +1,147 @@
+/*\r
+ * Copyright (c) @CompanyNameMagicTag 2022. All rights reserved.\r
+ * Description: BT HID Service Server Module.\r
+ */\r
+\r
+#include "ppgBleService.h"\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+#ifdef SUPPORT_BLE_SERVICE\r
+#include "bleService.h"\r
+#endif\r
+#include "bts_gatt_stru.h"\r
+#include "bts_a2dp_source.h"\r
+#include "bts_gatt_server.h"\r
+#include "securec.h"\r
+#include "wearable_log.h"\r
+\r
+// #include "bts_def.h"\r
+// #include "sdk_general.h"\r
+// #include "bts_gatt_stru.h"\r
+// #include "bts_gatt_server.h"\r
+// #include "ble_gatts_callbacks.h"\r
+// #include "bts_uuid.h"\r
+\r
+#define BLE_HID_SERVER_ID 1\r
+\r
+/* HID gatt notification flag, do not need confirm */\r
+#define GATT_NOTIFICATION_FLAG 0\r
+/* HID gatt indication flag, need confirm */\r
+#define GATT_INDICATION_FLAG 1\r
+#define INVALID_SERVER_ID 0\r
+/* Client Characteristic Configuration UUID */\r
+#define BLE_UUID_CLIENT_CHARACTERISTIC_CONF 0x2902\r
+/*user Characteristic Configuration UUID*/\r
+#define BLE_UUID_USER_CHARACTERISTIC_CONF 0x2901\r
+/* gatt server ID */\r
+static uint8_t g_server_id = INVALID_SERVER_ID;\r
+// RX定为Notify属性,TX为Write属性\r
+enum {\r
+    /* HID input report characteristic properties */\r
+    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,\r
+    /* HID output report characteristic properties */\r
+    PPGTEST_OUTPUT_REPORT_PROPERTIES =  GATT_CHARACTER_PROPERTY_BIT_WRITE|GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP,\r
+};\r
+    \r
+#define uint16_to_byte(n) ((uint8_t)(n)), ((uint8_t)((n) >> 8))\r
+\r
+/* hid input report att handle */\r
+uint32_t g_ppgtest_notify_report_att_hdl = 0;\r
+uint32_t ppgtest_ate_write_handl = 0;\r
+\r
+uint32_t g_ppgtest_ota_report_hdl = 0;\r
+uint32_t ppgtest_ota_write_handl = 0;\r
+\r
+/* 创建服务 */\r
+static void ble_ppgtest_add_service(void)\r
+{\r
+    bt_uuid_t ppgtest_service_uuid = {0};\r
+    bts_data_to_uuid_len2(BLE_UUID_PPG_BLE_SERVICE, &ppgtest_service_uuid);\r
+    //只有一个服务,永远是1\r
+    gatts_add_service(BLE_HID_SERVER_ID, &ppgtest_service_uuid, true);\r
+}\r
+static void ble_ppgtest_add_descriptor_report_reference(uint8_t server_id, uint16_t srvc_handle, bool is_notify_report)\r
+{\r
+    /* HID input report reference value for test  [report id 1, input] */\r
+    static uint8_t report_reference_val_input[] = {0, 0};\r
+    gatts_add_desc_info_t descriptor;\r
+    if (is_notify_report)\r
+    {\r
+        bts_data_to_uuid_len2(BLE_UUID_CLIENT_CHARACTERISTIC_CONF, &descriptor.desc_uuid);\r
+        descriptor.permissions = GATT_ATTRIBUTE_PERMISSION_WRITE ;\r
+    }else{\r
+        bts_data_to_uuid_len2(BLE_UUID_USER_CHARACTERISTIC_CONF, &descriptor.desc_uuid);\r
+        descriptor.permissions = GATT_ATTRIBUTE_PERMISSION_WRITE | GATT_ATTRIBUTE_PERMISSION_READ ;\r
+        \r
+    }\r
+    descriptor.value = report_reference_val_input;\r
+    descriptor.value_len = (uint16_t)sizeof(report_reference_val_input);\r
+\r
+    gatts_add_descriptor(server_id, srvc_handle, &descriptor);\r
+}\r
+\r
+/* 添加特征:HID information */\r
+static void ble_ppgtest_add_character_and_information(uint8_t server_id, uint16_t srvc_handle)\r
+{\r
+    /* HID information value for test */\r
+    gatts_add_chara_info_t character={0};\r
+\r
+    memset_s(&character,sizeof(gatts_add_chara_info_t),0,sizeof(gatts_add_chara_info_t));\r
+    bts_data_to_uuid_len2(BLE_UUID_PPG_NOTIFY, &character.chara_uuid);\r
+    character.permissions = GATT_ATTRIBUTE_PERMISSION_READ | GATT_ATTRIBUTE_PERMISSION_WRITE;\r
+    character.properties = PPGTEST_INPUT_REPORT_PROPERTIES;\r
+    character.value_len = 0;//(uint16_t)sizeof(hid_information_val);\r
+    character.value = NULL;//hid_information_val;\r
+    gatts_add_characteristic(server_id, srvc_handle, &character);\r
+    ble_ppgtest_add_descriptor_report_reference(server_id,srvc_handle,true);\r
+    ble_ppgtest_add_descriptor_report_reference(server_id,srvc_handle,false);\r
+}\r
+\r
+/* device向host发送数据:input report */\r
+errcode_t ble_ppgtest_server_send_input_report(const uint8_t *data, uint8_t len)\r
+{\r
+    // printf("ble_hid_server_send_input_report:%d %d %d",data[0],data[1],len);\r
+    gatts_ntf_ind_t param = {0};\r
+    param.attr_handle = (uint16_t)g_ppgtest_notify_report_att_hdl;\r
+    param.value_len = len;\r
+    param.value = bt_os_new(len);\r
+    if (param.value == NULL) {\r
+        printf(  "[ppgtest][ERROR]send input report new fail\r\n");\r
+        return ERRCODE_BT_MALLOC_FAIL;\r
+    }\r
+    if (memcpy_s(param.value, param.value_len, data, len) != EOK) {\r
+        printf( "[ppgtest][ERROR]send input report memcpy fail\r\n");\r
+        bt_os_free(param.value);\r
+        return ERRCODE_BT_FAIL;\r
+    }\r
+    errcode_t ret = 0;\r
+    ret = gatts_notify_indicate(g_ble_server_id, g_ble_conn_id, &param);\r
+    printf("ble_ppgtest_server_send_input_report: 0x%x g_ble_conn_id:%d g_ppgtest_notify_report_att_hdl:%d\r\n",\r
+            ret,g_ble_conn_id,g_ppgtest_notify_report_att_hdl);\r
+    bt_os_free(param.value);\r
+    return ERRCODE_BT_SUCCESS;\r
+}\r
+\r
+\r
+void ble_ppgtest_store_report_handle(uint16_t srvc_handle)\r
+{\r
+    printf("g_ppgtest_notify_report_att_hdl: 0x%X\r\n",srvc_handle);\r
+    g_ppgtest_notify_report_att_hdl = srvc_handle;\r
+}\r
+\r
+void ble_ppgtest_add_characters_and_descriptors(uint8_t server_id, uint16_t srvc_handle)\r
+{\r
+    ble_ppgtest_add_character_and_information(server_id, srvc_handle);\r
+}\r
+\r
+void ble_ppgtest_server_init(void)\r
+{\r
+    ble_ppgtest_add_service();\r
+}\r
+\r
+#ifdef __cplusplus\r
+}\r
+#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 (file)
index 0000000..d988d68
--- /dev/null
@@ -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