فهرست منبع

add: airlink,ble,支持read_value,但只支持异步的方式

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

+ 21 - 10
components/airlink/src/exec/luat_airlink_bt_exec_task.c

@@ -237,15 +237,23 @@ static int drv_ble_connect(luat_drv_ble_msg_t *msg) {
     return luat_ble_connect(NULL, &conn);
 }
 
-// static int drv_ble_send_read_resp(luat_drv_ble_msg_t *msg) {
-//     // 从数据中解析出参数, 重新组装
-//     luat_ble_rw_req_t write = {0};
-//     uint16_t sizeof_write = 0;
-//     memcpy(&sizeof_write, msg->data, 2);
-//     memcpy(&write, msg->data + 2, sizeof(luat_ble_rw_req_t));
-//     LLOGD("ble send read resp len %d", write.len);
-//     return luat_ble_read_response_value(NULL, write.handle, msg->data + 2 + sizeof_write, write.len);
-// }
+static int drv_ble_read_value(luat_drv_ble_msg_t *msg) {
+    // 从数据中解析出参数, 重新组装
+    luat_ble_rw_req_t write = {0};
+    uint16_t sizeof_write = 0;
+    memcpy(&sizeof_write, msg->data, 2);
+    memcpy(&write, msg->data + 2, sizeof(luat_ble_rw_req_t));
+    LLOGD("ble read len %d", write.len);
+    uint8_t* value = NULL;
+    int ret = 0;
+    if (write.descriptor.uuid_type) {
+        ret = luat_ble_read_value(&write.service, &write.characteristic, &write.descriptor, &value, write.len);
+    }
+    else {
+        ret = luat_ble_read_value(&write.service, &write.characteristic, NULL, &value, write.len);
+    }
+    return ret;
+}
 
 static void drv_bt_task(void *param) {
     luat_drv_ble_msg_t *msg = NULL;
@@ -353,7 +361,10 @@ static void drv_bt_task(void *param) {
                 ret = luat_ble_disconnect(NULL);
                 LLOGD("ble disconnect %d", ret);
                 break;
-
+            case LUAT_DRV_BT_CMD_BLE_READ_VALUE:
+                ret = drv_ble_read_value(msg);
+                LLOGD("ble read value %d", ret);
+                break;
             default:
                 LLOGD("unknow bt cmd %d", msg->cmd_id);
                 break;

+ 1 - 1
components/airlink/src/exec/luat_airlink_cmd_exec_bluetooth.c

@@ -69,7 +69,7 @@ int luat_airlink_cmd_exec_bt_resp_cb(luat_airlink_cmd_t *cmd, void *userdata) {
         luat_ble_event_t event = (luat_ble_event_t)tmp;
         luat_ble_param_t* param = luat_heap_malloc(sizeof(luat_ble_param_t));
         memcpy(param, msg->data + 4, sizeof(luat_ble_param_t));
-        // LLOGD("收到bt event %d %d", event, cmd->len - sizeof(luat_drv_ble_msg_t));
+        LLOGD("收到bt event %d %d", event, cmd->len - sizeof(luat_drv_ble_msg_t));
         // param->write_req.value = NULL;
         // param->adv_req.data = NULL;
         // param->read_req.value = NULL;

+ 42 - 2
components/bluetooth/drv/luat_drv_ble_port.c

@@ -584,8 +584,48 @@ 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) {
-    LLOGE("not support yet -> luat_ble_read_value");
-    return -1;
+    LLOGD("执行luat_ble_read_value");
+    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
+    };
+    // 暂时全是0
+    *data = NULL;
+    *len = 0;
+
+    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_READ_VALUE;
+    memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
+    luat_ble_rw_req_t req = {
+        .len = len
+    };
+    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));
+    }
+    if (uuid_descriptor) {
+        memcpy(&req.descriptor, uuid_descriptor, 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), data, len);
+
+    item.cmd = cmd;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+    LLOGI("luat_ble_read_value 执行完成");
+    return 0;
 }
 
 int luat_ble_notify_enable(luat_ble_uuid_t* uuid_service, luat_ble_uuid_t* uuid_characteristic, uint8_t enable) {

+ 1 - 1
components/bluetooth/include/luat_drv_ble.h

@@ -37,7 +37,7 @@ enum {
     LUAT_DRV_BT_CMD_BLE_READ_REQ, // 请求读取值
     LUAT_DRV_BT_CMD_BLE_CONNECT, // 读取值
     LUAT_DRV_BT_CMD_BLE_DISCONNECT, // 断开连接
-
+    LUAT_DRV_BT_CMD_BLE_READ_VALUE, // 读取值, 异步的
     LUAT_DRV_BT_CMD_BLE_EVENT_CB = 128, // 事件回调
 
     LUAT_DRV_BT_CMD_MAX

+ 6 - 3
luat/demo/airlink/air8000_ble/master/main.lua

@@ -24,18 +24,21 @@ local function ble_callback(ble_device, ble_event, ble_param)
     if ble_event == ble.EVENT_CONN then
         log.info("ble", "connect 成功")
     elseif ble_event == ble.EVENT_DISCONN then
-        log.info("ble", "disconnect 完成")
+        log.info("ble", "disconnect", ble_param.reason)
+        sys.timerStart(function() ble_device:scan_start() end, 1000)
     elseif ble_event == ble.EVENT_WRITE then
         log.info("ble", "write", ble_param.handle,ble_param.uuid_service:toHex(),ble_param.uuid_characteristic:toHex())
         log.info("ble", "data", ble_param.data:toHex())
-        -- ble_device:write_notify(ble_param,string.fromHex("123456"))
     elseif ble_event == ble.EVENT_READ_VALUE then
-        log.info("ble", "read", ble_param.handle,ble_param.uuid_service:toHex(),ble_param.uuid_characteristic:toHex(),ble_param.data:toHex(),ble_param.data)
+        log.info("ble", "read", ble_param.handle,ble_param.uuid_service:toHex(),ble_param.uuid_characteristic:toHex(),ble_param.data:toHex())
     elseif ble_event == ble.EVENT_SCAN_REPORT then
         print("ble scan report",ble_param.addr_type,ble_param.rssi,ble_param.adv_addr:toHex(),ble_param.data:toHex())
         scan_count = scan_count + 1
         if scan_count > 100 then
+            log.info("ble", "扫描次数超过100次, 停止扫描, 15秒后重新开始")
+            scan_count = 0
             ble_device:scan_stop()
+            sys.timerStart(function() ble_device:scan_start() end, 15000)
         end
         -- 注意, 这里是连接到另外一个设备, 设备名称带LuatOS字样
         if ble_param.addr_type == 0 and ble_param.data:find("LuatOS") then