فهرست منبع

add: airlink,ble,air8000完成ble主机模式

Wendal Chen 8 ماه پیش
والد
کامیت
e2e243b0ff

+ 41 - 23
components/airlink/src/exec/luat_airlink_bt_exec_task.c

@@ -257,6 +257,19 @@ static int drv_ble_read_value(luat_drv_ble_msg_t *msg) {
     return ret;
 }
 
+static int drv_ble_notify_enable(luat_drv_ble_msg_t *msg) {
+    // 从数据中解析出参数, 重新组装
+    luat_ble_rw_req_t write = {0};
+    uint16_t sizeof_write = 0;
+    uint8_t enable = 0;
+    memcpy(&sizeof_write, msg->data, 2);
+    memcpy(&write, msg->data + 2, sizeof(luat_ble_rw_req_t));
+    memcpy(&enable, msg->data + 2 + sizeof_write, 1);
+    int ret = luat_ble_notify_enable(&write.service, &write.characteristic, enable);
+    LLOGD("ble notify enable %d ret %d", enable, ret);
+    return ret;
+}
+
 static void drv_bt_task(void *param) {
     luat_drv_ble_msg_t *msg = NULL;
     LLOGD("bt task start ...");
@@ -274,81 +287,81 @@ static void drv_bt_task(void *param) {
             case LUAT_DRV_BT_CMD_BT_INIT:
                 LLOGD("执行luat_bluetooth_init");
                 ret = luat_bluetooth_init(NULL);
-                LLOGD("bt init %d", ret);
+                LLOGD("bt init ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BT_DEINIT:
                 ret = luat_bluetooth_deinit(NULL);
-                LLOGD("bt deinit %d", ret);
+                LLOGD("bt deinit ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_INIT:
                 ret = luat_ble_init(NULL, drv_ble_cb); // 不能通过airlink传递函数指针, 这里要用本地的
-                LLOGD("ble init %d", ret);
+                LLOGD("ble init ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_DEINIT:
                 ret = luat_ble_deinit(NULL);
-                LLOGD("ble deinit %d", ret);
+                LLOGD("ble deinit ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_GATT_CREATE:
                 ret = drv_gatt_create(msg);
-                LLOGD("ble gatt create %d", ret);
+                LLOGD("ble gatt create ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_SET_NAME:
                 memcpy(buff, msg->data + 1, msg->data[0]);
                 buff[msg->data[0]] = 0;
                 ret = luat_ble_set_name(NULL, buff, msg->data[0]);
-                LLOGD("ble set name %d", ret);
+                LLOGD("ble set name ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_ADV_CREATE:
                 ret = drv_adv_create(msg);
-                LLOGD("ble adv start %d", ret);
+                LLOGD("ble adv start ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_ADV_START:
                 ret = luat_ble_start_advertising(NULL);
-                LLOGD("ble adv start %d", ret);
+                LLOGD("ble adv start ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_ADV_STOP:
                 ret = luat_ble_stop_advertising(NULL);
-                LLOGD("ble adv stop %d", ret);
+                LLOGD("ble adv stop ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_ADV_DELETE:
                 ret = luat_ble_delete_advertising(NULL);
-                LLOGD("ble adv delete %d", ret);
+                LLOGD("ble adv delete ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_ADV_SET_DATA:
                 ret = drv_adv_set_data(msg);
-                LLOGD("ble adv set data %d", ret);
+                LLOGD("ble adv set data ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_ADV_SET_SCAN_RSP_DATA:
                 ret = drv_adv_set_scan_rsp_data(msg);
-                LLOGD("ble adv set resp data %d", ret);
+                LLOGD("ble adv set resp data ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_SCAN_CREATE:
                 ret = drv_adv_create_scanning(msg);
-                LLOGD("ble adv create scanning %d", ret);
+                LLOGD("ble adv create scanning ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_SCAN_START:
                 ret = luat_ble_start_scanning(NULL);
-                LLOGD("ble adv start scanning %d", ret);
+                LLOGD("ble adv start scanning ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_SCAN_STOP:
                 ret = luat_ble_stop_scanning(NULL);
-                LLOGD("ble adv stop scanning %d", ret);
+                LLOGD("ble adv stop scanning ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_SCAN_DELETE:
                 ret = luat_ble_delete_scanning(NULL);
-                LLOGD("ble adv delete scaning %d", ret);
+                LLOGD("ble adv delete scaning ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_WRITE_NOTIFY:
                 ret = drv_ble_write_notify(msg);
-                LLOGD("ble write notify %d", ret);
+                LLOGD("ble write notify ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_WRITE_INDICATION:
                 ret = drv_ble_write_indication(msg);
-                LLOGD("ble write indication %d", ret);
+                LLOGD("ble write indication ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_WRITE_VALUE:
                 ret = drv_ble_write_value(msg);
-                LLOGD("ble write value %d", ret);
+                LLOGD("ble write value ret %d", ret);
                 break;
                 
             // case LUAT_DRV_BT_CMD_BLE_SEND_READ_RESP:
@@ -357,18 +370,23 @@ static void drv_bt_task(void *param) {
             //     break;
             case LUAT_DRV_BT_CMD_BLE_CONNECT:
                 ret = drv_ble_connect(msg);
-                LLOGD("ble connect %d", ret);
+                LLOGD("ble connect ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_DISCONNECT:
                 ret = luat_ble_disconnect(NULL);
-                LLOGD("ble disconnect %d", ret);
+                LLOGD("ble disconnect ret %d", ret);
                 break;
             case LUAT_DRV_BT_CMD_BLE_READ_VALUE:
                 ret = drv_ble_read_value(msg);
-                LLOGD("ble read value %d", ret);
+                LLOGD("ble read value ret %d", ret);
+                break;
+            case LUAT_DRV_BT_CMD_BLE_NOTIFY_ENABLE:
+                // 通知使能
+                ret = drv_ble_notify_enable(msg);
+                LLOGD("ble notify enable ret %d", ret);
                 break;
             default:
-                LLOGD("unknow bt cmd %d", msg->cmd_id);
+                LLOGD("unknow bt cmd id %d", msg->cmd_id);
                 break;
             }
             luat_heap_free(msg);

+ 45 - 4
components/bluetooth/drv/luat_drv_ble_port.c

@@ -535,7 +535,7 @@ int luat_ble_delete_scanning(void* args) {
 int luat_ble_connect(void* args, luat_ble_connect_req_t *conn) {
     LLOGD("执行luat_ble_connect");
     if (get_ble_version() < 11) {
-        LLOGE("luat_ble_connect not support, ble version is %d", get_ble_version());
+        LLOGE("ble:connect not support, ble version is %d", get_ble_version());
         return -1;
     }
     uint64_t seq = luat_airlink_get_next_cmd_id();
@@ -563,7 +563,7 @@ int luat_ble_connect(void* args, luat_ble_connect_req_t *conn) {
 int luat_ble_disconnect(void* args) {
     LLOGD("执行luat_ble_disconnect");
     if (get_ble_version() < 11) {
-        LLOGE("luat_ble_connect not support, ble version is %d", get_ble_version());
+        LLOGE("ble:disconnect not support, ble version is %d", get_ble_version());
         return -1;
     }
     uint64_t seq = luat_airlink_get_next_cmd_id();
@@ -585,6 +585,10 @@ int luat_ble_disconnect(void* args) {
 
 int luat_ble_read_value(luat_ble_uuid_t* uuid_service, luat_ble_uuid_t* uuid_characteristic, luat_ble_uuid_t* uuid_descriptor, uint8_t **data, uint16_t* len) {
     LLOGD("执行luat_ble_read_value");
+    if (get_ble_version() < 11) {
+        LLOGE("ble:read_value not support, ble version is %d", get_ble_version());
+        return -1;
+    }
     uint16_t tmp = 0;
     uint64_t seq = luat_airlink_get_next_cmd_id();
     airlink_queue_item_t item = {
@@ -629,8 +633,45 @@ int luat_ble_read_value(luat_ble_uuid_t* uuid_service, luat_ble_uuid_t* uuid_cha
 }
 
 int luat_ble_notify_enable(luat_ble_uuid_t* uuid_service, luat_ble_uuid_t* uuid_characteristic, uint8_t enable) {
-    LLOGE("not support yet -> luat_ble_notify_enable");
-    return -1;
+    LLOGD("执行luat_ble_notify_enable %d", enable);
+    if (get_ble_version() < 11) {
+        LLOGE("ble:notify_enable not support, ble version is %d", get_ble_version());
+        return -1;
+    }
+    uint16_t tmp = 0;
+    uint64_t seq = luat_airlink_get_next_cmd_id();
+    airlink_queue_item_t item = {
+        .len = sizeof(luat_airlink_cmd_t) 
+               + sizeof(luat_drv_ble_msg_t) + sizeof(luat_ble_rw_req_t) 
+               + sizeof(uint16_t)
+               + 16
+    };
+
+    luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x500, item.len - sizeof(luat_airlink_cmd_t));
+    if (cmd == NULL) {
+        return -101;
+    }
+    
+    luat_drv_ble_msg_t msg = { .id = seq};
+    msg.cmd_id = LUAT_DRV_BT_CMD_BLE_NOTIFY_ENABLE;
+    memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
+    luat_ble_rw_req_t req = {
+        .len = 1
+    };
+    if (uuid_service) {
+        memcpy(&req.service, uuid_service, sizeof(luat_ble_uuid_t));
+    }
+    if (uuid_characteristic) {
+        memcpy(&req.characteristic, uuid_characteristic, sizeof(luat_ble_uuid_t));
+    }
+    tmp = sizeof(luat_ble_rw_req_t);
+    memcpy(cmd->data + sizeof(luat_drv_ble_msg_t), &tmp, 2);
+    memcpy(cmd->data + 2 + sizeof(luat_drv_ble_msg_t), &req, sizeof(luat_ble_rw_req_t));
+    memcpy(cmd->data + 2 + sizeof(luat_drv_ble_msg_t) + sizeof(luat_ble_rw_req_t), &enable, 1);
+
+    item.cmd = cmd;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+    return 0;
 }
 
 

+ 3 - 0
components/bluetooth/include/luat_drv_ble.h

@@ -38,6 +38,9 @@ enum {
     LUAT_DRV_BT_CMD_BLE_CONNECT, // 读取值
     LUAT_DRV_BT_CMD_BLE_DISCONNECT, // 断开连接
     LUAT_DRV_BT_CMD_BLE_READ_VALUE, // 读取值, 异步的
+    LUAT_DRV_BT_CMD_BLE_NOTIFY_ENABLE, // 通知使能
+
+
     LUAT_DRV_BT_CMD_BLE_EVENT_CB = 128, // 事件回调
 
     LUAT_DRV_BT_CMD_MAX

+ 5 - 0
luat/demo/airlink/air8000_ble/master/main.lua

@@ -50,6 +50,11 @@ local function ble_callback(ble_device, ble_event, ble_param)
         -- 读取GATT完成, 打印出来
         log.info("ble", "gatt item", ble_param)
     elseif ble_event == ble.EVENT_GATT_DONE then
+        log.info("ble", "gatt done", ble_param.service_num)
+        local wt = {uuid_service = string.fromHex("FA00"), uuid_characteristic = string.fromHex("EA01")}
+        ble_device:notify_enable(wt, true) -- 开启通知
+
+        -- 主动写入数据, 但不带通知, 带通知是 write_notify
         local wt = {uuid_service = string.fromHex("FA00"), uuid_characteristic = string.fromHex("EA02")}
         ble_device:write_value(wt,string.fromHex("1234"))