添加cywee蓝牙服务 689/1
authorpenglei <1624952103@qq.com>
Tue, 13 Aug 2024 06:46:20 +0000 (14:46 +0800)
committerpenglei <1624952103@qq.com>
Tue, 13 Aug 2024 06:46:20 +0000 (14:46 +0800)
code/sdk/application/wearable/service/ble_service/bleservice/bleService.c
code/sdk/drivers/peripheral/cywee/Inc/cywee_ble_Service.h [new file with mode: 0644]
code/sdk/drivers/peripheral/cywee/service/cywee_ble_Service.c [new file with mode: 0644]
code/sdk/drivers/peripheral/cywee/src/board_yh601s.c

index 455a9ee..5373804 100644 (file)
@@ -39,6 +39,9 @@ extern "C" {
 #include "custom_runtime.h"\r
 #include "custom_utils.h"\r
 #include "bletest.h"\r
+#ifdef SUPPORT_AT_CYWEE\r
+#include "cywee_ble_Service.h"\r
+#endif\r
 #ifdef SUPPORT_AT_OHOSFWK\r
 #include "BluetoothWrapper.h"\r
 #endif\r
@@ -792,6 +795,9 @@ static void svr_ble_bt_gatts_service_add_cb(uint8_t server_id, bt_uuid_t *uuid,
         || uuid_tmp == BLE_UUID_ATE_FACTORYTEST_SERVICE \r
         || uuid_tmp == BLE_UUID_PPG_BLE_SERVICE \r
         || uuid_tmp == ALIPAYY_SERVICE_ID\r
+#ifdef SUPPORT_AT_CYWEE\r
+        || uuid_tmp == BLE_UUID_CYWEE_REPORT_SERVICE\r
+#endif\r
          ) \r
     {\r
          WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " 0x%x\r\n",uuid_tmp);\r
@@ -822,6 +828,15 @@ static void svr_ble_bt_gatts_service_add_cb(uint8_t server_id, bt_uuid_t *uuid,
     bt_uuid_t ppg_service_uuid = {0};\r
     bts_data_to_uuid_len2(BLE_UUID_PPG_BLE_SERVICE, &ppg_service_uuid);\r
 \r
+\r
+#ifdef SUPPORT_AT_CYWEE\r
+\r
+    bt_uuid_t cywee_ble_service_uuid = {0};\r
+    bts_data_to_uuid_len2(BLE_UUID_CYWEE_REPORT_SERVICE, &cywee_ble_service_uuid);\r
+\r
+#endif\r
+\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
@@ -852,6 +867,14 @@ static void svr_ble_bt_gatts_service_add_cb(uint8_t server_id, bt_uuid_t *uuid,
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ppg ble service svr_ble_bt_gatts_service_add_cb ret:%d\r\n",ret);\r
     }\r
 \r
+#ifdef SUPPORT_AT_CYWEE\r
+    if (ble_gatts_cmp_uuid(uuid, &cywee_ble_service_uuid) == true){\r
+        ble_cywee_add_characters_and_descriptors(server_id,srv_handle);\r
+        ret = gatts_start_service(server_id, srv_handle);\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "cywee ble service svr_ble_bt_gatts_service_add_cb ret:%d\r\n",ret);\r
+    }   \r
+\r
+#endif\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
@@ -1029,6 +1052,19 @@ static void svr_ble_bt_gatts_characteristic_add_cb(uint8_t server_id, bt_uuid_t
         ble_ppgtest_store_report_handle(result->value_handle);\r
     }\r
     \r
+#ifdef SUPPORT_AT_CYWEE\r
+    bt_uuid_t cywee_report_uuid = {0};\r
+    bts_data_to_uuid_len2(BLE_UUID_CYWEE_RX, &cywee_report_uuid);\r
+\r
+    if (ble_gatts_cmp_uuid(uuid, &cywee_report_uuid) == true) {\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "result->cywee_report_uuid  %d\r\n",result->value_handle);\r
+        // if(g_hid_server_handle == 0){\r
+            // g_hid_server_handle = result->value_handle;\r
+            // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "g_hid_server_handle  %d\r\n",g_hid_server_handle);\r
+        ble_cywee_store_report_handle(result->value_handle);\r
+        // }\r
+    }\r
+#endif\r
     return;\r
 }\r
 \r
@@ -1359,6 +1395,9 @@ void svr_ble_bluetooth_init(void)
     \r
     ble_ppgtest_server_init(g_ble_server_id);\r
 \r
+#ifdef SUPPORT_AT_CYWEE\r
+    ble_cywee_server_init(g_ble_server_id);\r
+#endif\r
     gattc_reg_id();\r
 \r
    \r
@@ -1540,6 +1579,13 @@ static void blesrv_service_check_task(void const *arg)
         {\r
             factory_recv_ota_data(p_msgbuf,msg_len,server_handle);\r
         }\r
+\r
+#ifdef SUPPORT_AT_CYWEE\r
+        else if (server_handle == get_cywee_ate_write_handl())\r
+        {\r
+            cywee_recv_cmd_data(p_msgbuf,msg_len,server_handle);\r
+        }\r
+#endif\r
         // else if(server_handle == alipay_handle)\r
         // {\r
         //     printf("alipay wirte!!!!!");\r
diff --git a/code/sdk/drivers/peripheral/cywee/Inc/cywee_ble_Service.h b/code/sdk/drivers/peripheral/cywee/Inc/cywee_ble_Service.h
new file mode 100644 (file)
index 0000000..28cd440
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) @CompanyNameMagicTag 2022. All rights reserved.
+ * Description: BT HID Service Server module.
+ */
+
+/**
+ * @defgroup bluetooth_bts_hid_server HID SERVER API
+ * @ingroup  bluetooth
+ * @{
+ */
+#ifndef BLE_CYWEE_SERVER_H
+#define BLE_CYWEE_SERVER_H
+
+#include "errcode.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+// CYWEE 日志上报 服务UUID:0000FEE1-0000-1000-8000-00805f9b34fb
+// Service下有characteristic: 0000FEE3-0000-1000-8000-00805f9b34fb,
+// 并通过此characteristic 发送数据,APP 保存收到的数据到文件中
+
+/*  UUID */
+#define BLE_UUID_CYWEE_REPORT_SERVICE 0xFEE1
+/* notify */
+#define BLE_UUID_CYWEE_RX 0xFEE3
+
+
+/**
+ * @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_cywee_server_init(uint8_t server_id);
+
+/**
+ * @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_cywee_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_cywee_add_characters_and_descriptors(uint8_t server_id, uint16_t srvc_handle);
+
+void ble_cywee_store_report_handle(uint16_t srvc_handle);
+
+void ble_cywee_store_recv_handle(uint16_t srvc_handle);
+//协议接收client下发的数据接口
+void cywee_recv_cmd_data(uint8_t *buf, uint16_t event_size,uint16_t handle);
+uint32_t get_cywee_ate_write_handl(void);
+
+
+/**
+ * @}
+ */
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif//BLE_cywee_TEST_SERVER_H
diff --git a/code/sdk/drivers/peripheral/cywee/service/cywee_ble_Service.c b/code/sdk/drivers/peripheral/cywee/service/cywee_ble_Service.c
new file mode 100644 (file)
index 0000000..9cf9f62
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) @CompanyNameMagicTag 2022. All rights reserved.
+ * Description: BT HID Service Server Module.
+ */
+#include "cywee_ble_Service.h"
+#ifdef SUPPORT_BLE_SERVICE
+#include "bleService.h"
+#endif
+#include "bts_gatt_stru.h"
+#include "bts_gatt_server.h"
+#include "securec.h"
+#include "wearable_log.h"
+#ifdef SUPPORT_AT_OHOSFWK
+#include "BluetoothWrapper.h"
+#endif
+
+/* 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 */
+    FACTORYTEST_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 */
+    FACTORYTEST_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_cywee_notify_report_att_hdl = 0;
+uint32_t cywee_ate_write_handl = 0;
+
+/* 创建服务 */
+static void ble_cywee_add_service(uint8_t server_id)
+{
+    bt_uuid_t cywee_service_uuid = {0};
+    bts_data_to_uuid_len2(BLE_UUID_CYWEE_REPORT_SERVICE, &cywee_service_uuid);
+    //只有一个服务,永远是1
+   errcode_t ret = 0;
+#ifndef SUPPORT_AT_OHOSFWK
+    ret = gatts_add_service(server_id, &cywee_service_uuid, true);
+    printf("ble_cywee_add_service gatts_add_service ret: 0x%x serverId = 0x%x uuid.uuid_len:%d\n", ret, server_id,cywee_service_uuid.uuid_len);
+#else
+    ret = wrapper_gatts_add_service(server_id, &cywee_service_uuid, true);
+    printf("ble_cywee_add_service wrapper_gatts_add_service ret: 0x%x serverId = 0x%x uuid.uuid_len:%d\n", ret, server_id,cywee_service_uuid.uuid_len);
+#endif
+
+}
+static void ble_cywee_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_cywee_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_CYWEE_RX, &character.chara_uuid);
+    character.permissions = GATT_ATTRIBUTE_PERMISSION_READ | GATT_ATTRIBUTE_PERMISSION_WRITE;
+    character.properties = FACTORYTEST_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_cywee_add_descriptor_report_reference(server_id,srvc_handle,true);
+    ble_cywee_add_descriptor_report_reference(server_id,srvc_handle,false);    
+    
+}
+
+/* device向host发送数据:input report */
+errcode_t ble_cywee_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);
+    if (g_cywee_notify_report_att_hdl==0) 
+        return ERRCODE_BT_FAIL;
+
+    gatts_ntf_ind_t param = {0};
+    param.attr_handle = (uint16_t)g_cywee_notify_report_att_hdl;
+    param.value_len = len;
+    param.value = bt_os_new(len);
+    if (param.value == NULL) {
+        printf(  "[cywee][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( "[cywee][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, &param);
+    printf("ble_cywee_server_send_input_report: 0x%x g_ble_conn_id:%d g_cywee_notify_report_att_hdl:%d\r\n",ret,g_ble_conn_id,g_cywee_notify_report_att_hdl);
+    bt_os_free(param.value);
+    return ERRCODE_BT_SUCCESS;
+}
+
+
+void ble_cywee_store_report_handle(uint16_t srvc_handle)
+{
+    printf("g_cywee_notify_report_att_hdl: 0x%X\r\n",srvc_handle);
+    if(g_cywee_notify_report_att_hdl == 0){
+        g_cywee_notify_report_att_hdl = srvc_handle;
+    }
+}
+void ble_cywee_store_recv_handle(uint16_t srvc_handle)
+{
+    printf("cywee_ate_write_handl : 0x%X\r\n",srvc_handle);
+    if(cywee_ate_write_handl == 0)
+    {
+        cywee_ate_write_handl = srvc_handle;
+    }
+    
+}
+
+uint32_t get_cywee_ate_write_handl(void)
+{
+    return cywee_ate_write_handl;
+}
+
+
+void ble_cywee_add_characters_and_descriptors(uint8_t server_id, uint16_t srvc_handle)
+{
+    ble_cywee_add_character_and_information(server_id, srvc_handle);
+}
+
+void ble_cywee_server_init(uint8_t server_id)
+{
+    ble_cywee_add_service(server_id);
+}
+
+
+//协议接收client下发的数据接口
+void cywee_recv_cmd_data(uint8_t *buf, uint16_t event_size,uint16_t handle)
+{
+    if(buf != NULL)
+        printf("cywee_recv_cmd_data len %d , handle 0x%X ,*buf 0x%02X\r\n",event_size,handle,*buf);
+    /****此处为接收指令,处理后反馈结果的参考**/
+ 
+    /*****参考结束****/
+}
+
+
index 0f00dc1..5bde834 100644 (file)
@@ -29,6 +29,7 @@
 #include "config.h"
 #include "OsApi.h"
 #include "los_printf.h"
+#include "cywee_ble_Service.h"
 /*********************/
 /*** Macro Defines ***/
 /*********************/
@@ -238,6 +239,7 @@ void CWM_OS_free(void *ptr)
 int CWM_OS_dbgOutput(const char * format)
 {
     PRINT("%s", format); 
+    ble_cywee_server_send_input_report((uint8_t *)format,strlen(format));
     return 0;
 }