Kaynağa Gözat

实现UUID及通信框架

Hansome 3 ay önce
ebeveyn
işleme
300d029d94

+ 3 - 0
src/hlw2025141/app/modules/bluetooth/app/ab_mate/ab_mate_profile.c

@@ -1,6 +1,7 @@
 #include "include.h"
 #include "ab_mate_profile.h"
 #include "com_adv.h"
+#include "com_ble.h"
 
 #if AB_MATE_APP_EN
 
@@ -378,6 +379,8 @@ void ble_app_gatts_service_init(void)
                                        &ab_mate_ctkd_base_cb_info);      //characteristic
 #endif
 
+    ret = com_ble_init();
+
     if(ret != BLE_GATTS_SUCCESS){
         printf("gatt err: %d\n", ret);
         return;

+ 5 - 0
src/hlw2025141/app/projects/earphone/app.cbp

@@ -15,6 +15,7 @@
 					<Add directory="../../modules/bluetooth/app/tuya/include" />
 					<Add directory="../../modules/bluetooth/app/tuya/port" />
 					<Add directory="../../modules/bluetooth/app/tuya/mbedtls" />
+					<Add directory="com" />
 				</Compiler>
 				<Linker>
 					<Add library="../../modules/bluetooth/app/tuya/lib/tuya_ble_sdk_lib.a" />
@@ -717,6 +718,10 @@
 			<Option compilerVar="CC" />
 		</Unit>
 		<Unit filename="com/com_adv.h" />
+		<Unit filename="com/com_ble.c">
+			<Option compilerVar="CC" />
+		</Unit>
+		<Unit filename="com/com_ble.h" />
 		<Unit filename="config.c">
 			<Option compilerVar="CC" />
 		</Unit>

+ 16 - 2
src/hlw2025141/app/projects/earphone/app.depend

@@ -7340,14 +7340,28 @@
 1733475627 source:e:\hlwei\projects\hlw2025141\src\hlw2025141\app\projects\earphone\ram.ld
 	"config.h"
 
-1743682338 source:e:\hlwei\projects\hlw2025141\src\hlw2025141\app\projects\earphone\com\com_adv.c
+1743996563 source:e:\hlwei\projects\hlw2025141\src\hlw2025141\app\projects\earphone\com\com_adv.c
 	"com_adv.h"
 
 1743682357 e:\hlwei\projects\hlw2025141\src\hlw2025141\app\projects\earphone\com\com_adv.h
 	"include.h"
 
-1743682274 source:e:\hlwei\projects\hlw2025141\src\hlw2025141\app\modules\bluetooth\app\ab_mate\ab_mate_profile.c
+1743989986 source:e:\hlwei\projects\hlw2025141\src\hlw2025141\app\modules\bluetooth\app\ab_mate\ab_mate_profile.c
 	"include.h"
 	"ab_mate_profile.h"
 	"com_adv.h"
+	"com_ble.h"
+
+1744006153 e:\hlwei\projects\hlw2025141\src\hlw2025141\app\projects\earphone\com\com_ble.h
+	"include.h"
+
+1744005828 source:e:\hlwei\projects\hlw2025141\src\hlw2025141\app\projects\earphone\com\com_ble.c
+	"com_ble.h"
+	"com_dec.h"
+
+1744005700 source:e:\hlwei\projects\hlw2025141\src\hlw2025141\app\projects\earphone\com\com_dec.c
+	"com_dec.h"
+
+1744011007 e:\hlwei\projects\hlw2025141\src\hlw2025141\app\projects\earphone\com\com_dec.h
+	"include.h"
 

+ 11 - 0
src/hlw2025141/app/projects/earphone/com/checksum.py

@@ -0,0 +1,11 @@
+def calc(datas):
+    cs = 0;
+    for data in datas:
+        cs += data
+    cs &= 0xff
+    datas.append(cs)
+    print([hex(data) for data in datas])
+
+if __name__ == '__main__':
+    datas = [0xaa, 0x01, 0x01, 0x64]
+    calc(datas)

+ 2 - 2
src/hlw2025141/app/projects/earphone/com/com_adv.c

@@ -9,7 +9,7 @@ static uint32_t _fill_adv_type(uint8_t offset, uint8_t* buf)
 
     buf[i++] = 2;
     buf[i++] = 0x01;
-    buf[i++] = 0x06;
+    buf[i++] = 0x02;
 
     printf("adv type: %d\r\n", i);
     return i;
@@ -43,7 +43,7 @@ static uint32_t _fill_adv_uuid_datas(uint8_t offset, uint8_t* buf)
     uint8_t i = 0;
 
     buf[i++] = 7;
-    buf[i++] = 16;
+    buf[i++] = 0x16;
     buf[i++] = 0xaa;
     buf[i++] = 0xfa;
     buf[i++] = 100;     /* 左耳电量 */

+ 80 - 0
src/hlw2025141/app/projects/earphone/com/com_ble.c

@@ -0,0 +1,80 @@
+#include "com_ble.h"
+#include "com_dec.h"
+
+
+static int _on_write(uint16_t con_handle, uint16_t handle, uint32_t flag, uint8_t* datas, uint16_t len);
+
+const uint8_t _service_uuid16[2]={0xa0, 0xfa};
+static const gatts_uuid_base_st _service_uuid_base = {
+    .type = BLE_GATTS_UUID_TYPE_16BIT,
+    .uuid = _service_uuid16,
+};
+static gatts_service_base_st _service_gatts_base;
+
+const uint8_t _tx_uuid16[2]={0xa1, 0xfa};
+static const gatts_uuid_base_st _tx_uuid_base = {
+    .props = ATT_WRITE_WITHOUT_RESPONSE | ATT_READ,
+    .type = BLE_GATTS_UUID_TYPE_16BIT,
+    .uuid = _tx_uuid16,
+};
+
+const uint8_t _rx_uuid16[2]={0xa2, 0xfa};
+static const gatts_uuid_base_st _rx_uuid_base = {
+    .props = ATT_NOTIFY | ATT_READ,
+    .type = BLE_GATTS_UUID_TYPE_16BIT,
+    .uuid = _rx_uuid16,
+};
+static gatts_service_base_st _rx_gatts_base;
+
+static ble_gatt_characteristic_cb_info_t _tx_write_base_cb_info = {
+    .att_callback_func = _on_write,
+};
+
+static ble_gatt_characteristic_cb_info_t _rx_notify_base_cb_info = {
+    .client_config = GATT_CLIENT_CONFIG_NOTIFY,
+};
+
+
+int com_ble_init(void)
+{
+
+    int ret = 0;
+
+    ret = ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
+                                _service_uuid_base.uuid,
+                                _service_uuid_base.type,
+                                &_service_gatts_base.handle);
+
+    ret = ble_gatts_characteristic_add(_tx_uuid_base.uuid,
+                                       _tx_uuid_base.type,
+                                       _tx_uuid_base.props,
+                                       NULL,
+                                       &_tx_write_base_cb_info);
+
+    ret = ble_gatts_characteristic_add(_rx_uuid_base.uuid,
+                                       _rx_uuid_base.type,
+                                       _rx_uuid_base.props,
+                                       &_rx_gatts_base.handle,
+                                       &_rx_notify_base_cb_info);
+
+    return ret;
+
+}
+
+
+static int _on_write(uint16_t con_handle, uint16_t handle, uint32_t flag, uint8_t* datas, uint16_t len)
+{
+
+    com_dec_on_recv(datas, len);
+
+    return 0;
+
+}
+
+
+void com_ble_notify(uint8_t* buf, uint8_t len)
+{
+
+    ble_tx_notify(_rx_gatts_base.handle, buf, len);
+
+}

+ 13 - 0
src/hlw2025141/app/projects/earphone/com/com_ble.h

@@ -0,0 +1,13 @@
+#ifndef __COM_BLE_H__
+#define __COM_BLE_H__
+
+#include "include.h"
+
+
+int com_ble_init(void);
+
+
+void com_ble_notify(uint8_t* buf, uint8_t len);
+
+
+#endif

+ 123 - 0
src/hlw2025141/app/projects/earphone/com/com_dec.c

@@ -0,0 +1,123 @@
+#include "com_dec.h"
+
+
+static uint8_t _tx_buf[COM_DEC_BUF_LEN];
+static uint8_t _tx_len;
+
+
+static void _send(void)
+{
+
+    uint8_t cs = 0;
+    uint8_t i;
+
+    for(i = 0; i < _tx_len; i++){
+        cs += _tx_buf[i];
+    }
+    _tx_buf[i] = cs;
+
+    com_ble_notify(_tx_buf, _tx_len + 1);
+
+}
+
+
+void com_dec_on_recv(uint8_t* datas, uint16_t len)
+{
+
+    uint8_t cmd;
+    uint8_t plen;
+    uint8_t cs;
+    uint8_t i;
+
+    if(len < 4){
+        printf("Parsing: insufficient length\r\n");
+        return;
+    }
+
+    if(datas[0] != COM_DEC_APP_TO_DEV){
+        printf("Parsing: frame error\r\n");
+        return;
+    }
+
+    cs = 0;
+    for(i = 0; i < len - 1; i++){
+        cs += datas[i];
+    }
+    if(cs != datas[len - 1]){
+        printf("Parsing: checksum error\r\n");
+        return;
+    }
+
+    i = 1;
+    cmd = datas[i++];
+    plen = datas[i++];
+
+    switch(cmd){
+
+        case COM_DEC_CMD_DEC_VOL:
+            com_dec_send_dec_vol_ack(0);
+            break;
+
+        case COM_DEC_CMD_INC_VOL:
+            com_dec_send_inc_vol_ack(0);
+            break;
+
+        case COM_DEC_CMD_QUERY_VOL:
+            com_dec_send_query_vol_ack(100);
+            break;
+
+        default:
+            break;
+
+    }
+
+}
+
+
+void com_dec_send_dec_vol_ack(uint8_t ret)
+{
+
+    uint8_t i = 0;
+
+    _tx_buf[i++] = COM_DEC_DEV_TO_APP;
+    _tx_buf[i++] = COM_DEC_CMD_DEC_VOL;
+    _tx_buf[i++] = 1;
+    _tx_buf[i++] = ret;
+
+    _tx_len = i;
+    _send();
+
+}
+
+
+void com_dec_send_inc_vol_ack(uint8_t ret)
+{
+
+    uint8_t i = 0;
+
+    _tx_buf[i++] = COM_DEC_DEV_TO_APP;
+    _tx_buf[i++] = COM_DEC_CMD_INC_VOL;
+    _tx_buf[i++] = 1;
+    _tx_buf[i++] = ret;
+
+    _tx_len = i;
+    _send();
+
+}
+
+
+void com_dec_send_query_vol_ack(uint8_t vol)
+{
+
+    uint8_t i = 0;
+
+    _tx_buf[i++] = COM_DEC_DEV_TO_APP;
+    _tx_buf[i++] = COM_DEC_CMD_QUERY_VOL;
+    _tx_buf[i++] = 1;
+    _tx_buf[i++] = vol;
+
+    _tx_len = i;
+    _send();
+
+}
+

+ 27 - 0
src/hlw2025141/app/projects/earphone/com/com_dec.h

@@ -0,0 +1,27 @@
+#ifndef __COM_DEC_H__
+#define __COM_DEC_H__
+
+#include "include.h"
+
+
+#define COM_DEC_APP_TO_DEV          (0xaa)
+#define COM_DEC_DEV_TO_APP          (0xa5)
+
+
+#define COM_DEC_BUF_LEN             (20)
+
+
+#define COM_DEC_CMD_DEC_VOL         (0x01)
+#define COM_DEC_CMD_INC_VOL         (0x02)
+#define COM_DEC_CMD_QUERY_VOL       (0x03)
+
+
+void com_dec_on_recv(uint8_t* datas, uint16_t len);
+
+
+void com_dec_send_dec_vol_ack(uint8_t ret);
+void com_dec_send_inc_vol_ack(uint8_t ret);
+void com_dec_send_query_vol_ack(uint8_t vol);
+
+
+#endif