瀏覽代碼

add: ble,添加解码广播数据的函数,adv_decode

Wendal Chen 8 月之前
父節點
當前提交
3557f14611

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

@@ -29,7 +29,9 @@ static luat_rtos_task_handle g_task_handle;
 
 static void drv_ble_cb(luat_ble_t* luat_ble, luat_ble_event_t event, luat_ble_param_t* param) {
     // 注意, 这里要代理不同的事件, 转发到airlink
-    LLOGD("drv_ble event %d", event);
+    if (event != LUAT_BLE_EVENT_SCAN_REPORT) {
+        LLOGD("drv_ble event %d", event);
+    }
     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) + 1024

+ 52 - 0
components/bluetooth/src/luat_lib_ble.c

@@ -1061,6 +1061,56 @@ static int l_ble_disconnect(lua_State *L){
     return 1;
 }
 
+/*
+解码广播数据
+@api ble.adv_decode(data)
+@string data 广播数据
+@return table 广播数据的解码结果
+@usage
+-- 解码广播数据
+local data = string.fromHex("1EFF060001092002BE0F0AAD8A6D2E251ED6DFBB3D15249929E10BE138DF7B")
+-- 解析广播数据
+local adv_data = ble_device:adv_decode(data)
+if adv_data then
+    for k, v in pairs(adv_data) do
+        log.info("ble", "adv data", v.len, v.tp, v.data:toHex())
+    end
+end
+*/
+static int l_ble_adv_decode(lua_State *L) {
+    size_t len = 0;
+    const char *data = luaL_checklstring(L, 2, &len);
+    if (len == 0) {
+        lua_pushnil(L);
+        return 1;
+    }
+    lua_newtable(L);
+    uint8_t *p = (uint8_t *)data;
+    size_t offset = 0;
+    int index = 1;
+    while (offset < len) {
+        uint8_t length = p[offset++];
+        if (length == 0 || offset + length > len) {
+            LLOGE("Invalid BLE advertisement data");
+            lua_pushnil(L);
+            return 1;
+        }
+        uint8_t type = p[offset++];
+        lua_newtable(L);
+        lua_pushinteger(L, length - 1); // Length does not include the length byte itself
+        lua_setfield(L, -2, "len");
+        lua_pushinteger(L, type);
+        lua_setfield(L, -2, "tp");
+        lua_pushlstring(L, (const char *)&p[offset], length - 1); // Data does not include the length byte
+        lua_setfield(L, -2, "data");
+
+        lua_seti(L, -2, index);
+        offset += length - 1;
+        index ++;
+    }
+    return 1;
+}
+
 static int _ble_struct_newindex(lua_State *L);
 
 void luat_ble_struct_init(lua_State *L){
@@ -1076,6 +1126,7 @@ static const rotable_Reg_t reg_ble[] = {
     {"adv_create", ROREG_FUNC(l_ble_advertising_create)},
     {"adv_start", ROREG_FUNC(l_ble_advertising_start)},
     {"adv_stop", ROREG_FUNC(l_ble_advertising_stop)},
+    {"adv_decode", ROREG_FUNC(l_ble_adv_decode)},
 
     // gatt
     // slaver
@@ -1094,6 +1145,7 @@ static const rotable_Reg_t reg_ble[] = {
     {"connect", ROREG_FUNC(l_ble_connect)},
     {"disconnect", ROREG_FUNC(l_ble_disconnect)},
 
+
     // BLE_EVENT
     {"EVENT_NONE", ROREG_INT(LUAT_BLE_EVENT_NONE)},
     {"EVENT_INIT", ROREG_INT(LUAT_BLE_EVENT_INIT)},

+ 7 - 0
luat/demo/airlink/air8000_ble/scan/main.lua

@@ -28,6 +28,13 @@ local function ble_callback(ble_device, ble_event, ble_param)
         log.info("ble", "scan init")
     elseif ble_event == ble.EVENT_SCAN_REPORT then
         log.info("ble", "scan report", ble_param.rssi, ble_param.adv_addr:toHex(), ble_param.data:toHex())
+        -- 解析广播数据, 日志很多, 按需使用
+        -- local adv_data = ble_device:adv_decode(ble_param.data)
+        -- if adv_data then
+        --     for k, v in pairs(adv_data) do
+        --         log.info("ble", "adv data", v.len, v.tp, v.data:toHex())
+        --     end
+        -- end
     elseif ble_event == ble.EVENT_SCAN_STOP then
         log.info("ble", "scan stop")
     end