Procházet zdrojové kódy

add: airlink,bt,peripheral模式能被连接,但还无法收发到数据

Wendal Chen před 9 měsíci
rodič
revize
4a40db56db

+ 50 - 3
components/airlink/src/exec/luat_airlink_bt_exec_task.c

@@ -44,8 +44,32 @@ static int drv_gatt_create(luat_drv_ble_msg_t *msg) {
     {
         LLOGD("gatt chara %d maxsize %d", i, gatt.characteristics[i].max_size);
     }
-    luat_ble_create_gatt(NULL, &gatt);
-    return 0;
+    return luat_ble_create_gatt(NULL, &gatt);
+}
+
+static int drv_adv_create(luat_drv_ble_msg_t *msg) {
+    // 从数据中解析出参数, 重新组装
+    luat_ble_adv_cfg_t adv = {0};
+    uint16_t sizeof_adv = 0;
+    memcpy(&sizeof_adv, msg->data + 6, 2);
+    memcpy(&adv, msg->data + 8, sizeof(luat_ble_adv_cfg_t));
+    return luat_ble_create_advertising(NULL, &adv);
+}
+
+static int drv_adv_set_data(luat_drv_ble_msg_t *msg) {
+    // 从数据中解析出参数, 重新组装
+    uint16_t datalen = 0;
+    memcpy(&datalen, msg->data + 6, 2);
+    LLOGD("adv set data len %d", datalen);
+    return luat_ble_set_adv_data(NULL, msg->data + 8, datalen);
+}
+
+static int drv_adv_set_scan_rsp_data(luat_drv_ble_msg_t *msg) {
+    // 从数据中解析出参数, 重新组装
+    uint16_t datalen = 0;
+    memcpy(&datalen, msg->data + 6, 2);
+    LLOGD("adv set scan rsp data len %d", datalen);
+    return luat_ble_set_scan_rsp_data(NULL, msg->data + 8, datalen);
 }
 
 static void drv_bt_task(void *param) {
@@ -86,10 +110,33 @@ static void drv_bt_task(void *param) {
             case LUAT_DRV_BT_CMD_BLE_SET_NAME:
                 memcpy(buff, msg->data + 7, msg->data[6]);
                 buff[msg->data[6]] = 0;
-                LLOGD("ble set name len = %d %s", msg->data[6], buff);
                 ret = luat_ble_set_name(NULL, buff, msg->data[6]);
                 LLOGD("ble set name %d", ret);
                 break;
+            case LUAT_DRV_BT_CMD_BLE_ADV_CREATE:
+                ret = drv_adv_create(msg);
+                LLOGD("ble adv start %d", ret);
+                break;
+            case LUAT_DRV_BT_CMD_BLE_ADV_START:
+                ret = luat_ble_start_advertising(NULL);
+                LLOGD("ble adv start %d", ret);
+                break;
+            case LUAT_DRV_BT_CMD_BLE_ADV_STOP:
+                ret = luat_ble_stop_advertising(NULL);
+                LLOGD("ble adv stop %d", ret);
+                break;
+            case LUAT_DRV_BT_CMD_BLE_ADV_DELETE:
+                ret = luat_ble_delete_advertising(NULL);
+                LLOGD("ble adv delete %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);
+                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);
+                break;
             default:
                 LLOGD("unknow bt cmd %d", msg->cmd_id);
                 break;

+ 176 - 14
components/bluetooth/drv/luat_drv_ble_port.c

@@ -46,8 +46,29 @@ int luat_ble_init(void* args, luat_ble_cb_t luat_ble_cb) {
 }
 
 int luat_ble_deinit(void* args) {
-    LLOGE("not support yet");
-    return -1;
+    LLOGD("执行luat_ble_deinit");
+    uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
+    airlink_queue_item_t item = {
+        .len = 8 + sizeof(luat_airlink_cmd_t) + 8
+    };
+    luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x500, 8 + 8) ;
+    if (cmd == NULL) {
+        return -101;
+    }
+    memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
+    uint8_t data[8] = {0};
+     // 前2个字节是蓝牙cmd id
+    uint16_t id = LUAT_DRV_BT_CMD_BLE_DEINIT;
+    memcpy(data, &id, 2);
+    // 然后2个字节的主机协议版本号, 当前全是0
+    // 剩余4个字节做预留
+
+    // 全部拷贝过去
+    memcpy(cmd->data + 8, data, 8);
+
+    item.cmd = cmd;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+    return 0;
 }
 
 
@@ -89,38 +110,179 @@ int luat_ble_set_max_mtu(void* args, uint16_t max_mtu) {
 
 // advertise
 int luat_ble_create_advertising(void* args, luat_ble_adv_cfg_t* adv_cfg) {
-    LLOGE("not support yet");
-    return -1;
+    LLOGD("执行luat_ble_create_advertising");
+    uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
+    airlink_queue_item_t item = {
+        .len = 8 + sizeof(luat_airlink_cmd_t) + 8 + sizeof(uint16_t) + sizeof(luat_ble_adv_cfg_t)
+    };
+    luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x500, item.len - sizeof(luat_airlink_cmd_t));
+    if (cmd == NULL) {
+        return -101;
+    }
+    memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
+    uint8_t data[8] = {0};
+     // 前2个字节是蓝牙cmd id
+    uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_CREATE;
+    memcpy(data, &id, 2);
+    // 然后2个字节的主机协议版本号, 当前全是0
+    // 剩余4个字节做预留
+
+    // 全部拷贝过去
+    memcpy(cmd->data + 8, data, 8);
+    // 然后是结构体大小
+    uint16_t sizeof_adv = sizeof(luat_ble_adv_cfg_t);
+    memcpy(cmd->data + 8 + 8, &sizeof_adv, 2);
+    // 然后是数据
+    memcpy(cmd->data + 8 + 8 + 2, adv_cfg, sizeof_adv);
+
+    item.cmd = cmd;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+    return 0;
 }
 
 
 int luat_ble_set_adv_data(void* args, uint8_t* adv_buff, uint8_t adv_len) {
-    LLOGE("not support yet");
-    return -1;
+    LLOGD("执行luat_ble_set_adv_data %p %d", adv_buff, adv_len);
+    uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
+    airlink_queue_item_t item = {
+        .len = 8 + sizeof(luat_airlink_cmd_t) + 8 + sizeof(uint16_t) + adv_len
+    };
+    luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x500, item.len - sizeof(luat_airlink_cmd_t));
+    if (cmd == NULL) {
+        return -101;
+    }
+    memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
+    uint8_t data[8] = {0};
+     // 前2个字节是蓝牙cmd id
+    uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_SET_DATA;
+    memcpy(data, &id, 2);
+    // 然后2个字节的主机协议版本号, 当前全是0
+    // 剩余4个字节做预留
+
+    // 全部拷贝过去
+    memcpy(cmd->data + 8, data, 8);
+    // 然后是结构体大小
+    uint16_t datalen = adv_len;
+    memcpy(cmd->data + 8 + 8, &datalen, 2);
+    // 然后是名字
+    memcpy(cmd->data + 8 + 8 + 2, adv_buff, datalen);
+
+    item.cmd = cmd;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+    return 0;
 }
 
 
 int luat_ble_set_scan_rsp_data(void* args, uint8_t* rsp_data, uint8_t rsp_len) {
-    LLOGE("not support yet");
-    return -1;
+    LLOGD("执行luat_ble_set_scan_rsp_data %p %d", rsp_data, rsp_len);
+    uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
+    airlink_queue_item_t item = {
+        .len = 8 + sizeof(luat_airlink_cmd_t) + 8 + sizeof(uint16_t) + rsp_len
+    };
+    luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x500, item.len - sizeof(luat_airlink_cmd_t));
+    if (cmd == NULL) {
+        return -101;
+    }
+    memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
+    uint8_t data[8] = {0};
+     // 前2个字节是蓝牙cmd id
+    uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_SET_SCAN_RSP_DATA;
+    memcpy(data, &id, 2);
+    // 然后2个字节的主机协议版本号, 当前全是0
+    // 剩余4个字节做预留
+
+    // 全部拷贝过去
+    memcpy(cmd->data + 8, data, 8);
+    // 然后是结构体大小
+    uint16_t datalen = rsp_len;
+    memcpy(cmd->data + 8 + 8, &datalen, 2);
+    // 然后是名字
+    memcpy(cmd->data + 8 + 8 + 2, rsp_data, datalen);
+
+    item.cmd = cmd;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+    return 0;
 }
 
 
 int luat_ble_start_advertising(void* args) {
-    LLOGE("not support yet");
-    return -1;
+    LLOGD("执行luat_ble_start_advertising");
+    uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
+    airlink_queue_item_t item = {
+        .len = 8 + sizeof(luat_airlink_cmd_t) + 8
+    };
+    luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x500, item.len - sizeof(luat_airlink_cmd_t));
+    if (cmd == NULL) {
+        return -101;
+    }
+    memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
+    uint8_t data[8] = {0};
+     // 前2个字节是蓝牙cmd id
+    uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_START;
+    memcpy(data, &id, 2);
+    // 然后2个字节的主机协议版本号, 当前全是0
+    // 剩余4个字节做预留
+
+    // 全部拷贝过去
+    memcpy(cmd->data + 8, data, 8);
+
+    item.cmd = cmd;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+    return 0;
 }
 
 
 int luat_ble_stop_advertising(void* args) {
-    LLOGE("not support yet");
-    return -1;
+    LLOGD("执行luat_ble_start_advertising");
+    uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
+    airlink_queue_item_t item = {
+        .len = 8 + sizeof(luat_airlink_cmd_t) + 8
+    };
+    luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x500, item.len - sizeof(luat_airlink_cmd_t));
+    if (cmd == NULL) {
+        return -101;
+    }
+    memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
+    uint8_t data[8] = {0};
+     // 前2个字节是蓝牙cmd id
+    uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_STOP;
+    memcpy(data, &id, 2);
+    // 然后2个字节的主机协议版本号, 当前全是0
+    // 剩余4个字节做预留
+
+    // 全部拷贝过去
+    memcpy(cmd->data + 8, data, 8);
+
+    item.cmd = cmd;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+    return 0;
 }
 
 
 int luat_ble_delete_advertising(void* args) {
-    LLOGE("not support yet");
-    return -1;
+        LLOGD("执行luat_ble_start_advertising");
+    uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
+    airlink_queue_item_t item = {
+        .len = 8 + sizeof(luat_airlink_cmd_t) + 8
+    };
+    luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x500, item.len - sizeof(luat_airlink_cmd_t));
+    if (cmd == NULL) {
+        return -101;
+    }
+    memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
+    uint8_t data[8] = {0};
+     // 前2个字节是蓝牙cmd id
+    uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_START;
+    memcpy(data, &id, 2);
+    // 然后2个字节的主机协议版本号, 当前全是0
+    // 剩余4个字节做预留
+
+    // 全部拷贝过去
+    memcpy(cmd->data + 8, data, 8);
+
+    item.cmd = cmd;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+    return 0;
 }
 
 

+ 8 - 2
components/bluetooth/include/luat_drv_ble.h

@@ -16,8 +16,14 @@ enum {
     LUAT_DRV_BT_CMD_BT_DEINIT,
     LUAT_DRV_BT_CMD_BLE_INIT,
     LUAT_DRV_BT_CMD_BLE_DEINIT,
-    LUAT_DRV_BT_CMD_BLE_GATT_CREATE,
-    LUAT_DRV_BT_CMD_BLE_SET_NAME,
+    LUAT_DRV_BT_CMD_BLE_GATT_CREATE, // 创建GATT
+    LUAT_DRV_BT_CMD_BLE_SET_NAME, // 设置设备名
+    LUAT_DRV_BT_CMD_BLE_ADV_CREATE, // 创建广播
+    LUAT_DRV_BT_CMD_BLE_ADV_START, // 开始广播
+    LUAT_DRV_BT_CMD_BLE_ADV_STOP, // 停止广播
+    LUAT_DRV_BT_CMD_BLE_ADV_DELETE, // 删除广播
+    LUAT_DRV_BT_CMD_BLE_ADV_SET_DATA, // 设置广播数据
+    LUAT_DRV_BT_CMD_BLE_ADV_SET_SCAN_RSP_DATA, // 设置广播响应数据
 
     LUAT_DRV_BT_CMD_MAX
 };

+ 5 - 0
luat/demo/airlink/air8000_ble_peripheral/main.lua

@@ -64,14 +64,17 @@ end
 
 
 sys.taskInit(function()
+    sys.wait(500)
     log.info("开始初始化蓝牙核心")
     bluetooth_device = bluetooth.init()
+    sys.wait(100)
     log.info("初始化BLE功能")
     ble_device = bluetooth_device:ble(ble_callback)
     if ble_device == nil then
         log.error("当前固件不支持完整的BLE")
         return
     end
+    sys.wait(100)
 
     log.info('开始创建GATT')
     characteristic1,characteristic2,characteristic3,characteristic4 = ble_device:gatt_create(att_db)
@@ -80,6 +83,7 @@ sys.taskInit(function()
         log.error("创建GATT失败")
     end
 
+    sys.wait(100)
     log.info("开始设置广播内容")
     ble_device:adv_create({
         addr_mode = ble.PUBLIC,
@@ -94,6 +98,7 @@ sys.taskInit(function()
         }
     })
 
+    sys.wait(100)
     log.info("开始广播")
     ble_device:adv_start()
     -- ble_device:adv_stop()