فهرست منبع

add: airlink,drv,支持wifiscan,并添加demo

Wendal Chen 1 سال پیش
والد
کامیت
0ff3536e0e

+ 49 - 2
components/airlink/src/driver/luat_airlink_drv_wlan.c

@@ -102,9 +102,33 @@ int luat_airlink_drv_wlan_disconnect(void) {
     return 0;
 }
 
-int luat_airlink_drv_wlan_scan(void);
+int luat_airlink_drv_wlan_scan(void) {
+    uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
+    airlink_queue_item_t item = {
+        .len = sizeof(luat_airlink_cmd_t) + 8
+    };
+    luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x205, item.len) ;
+    if (cmd == NULL) {
+        return -101;
+    }
+    memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
+    item.cmd = cmd;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+    return 0;
+}
 
-int luat_airlink_drv_wlan_scan_get_result(luat_wlan_scan_result_t *results, size_t ap_limit);
+uint8_t drv_scan_result_size = 0;
+uint8_t *drv_scan_result;
+int luat_airlink_drv_wlan_scan_get_result(luat_wlan_scan_result_t *results, size_t ap_limit) {
+    if (drv_scan_result == NULL || drv_scan_result_size == 0) {
+        return 0;
+    }
+    if (ap_limit > drv_scan_result_size) {
+        ap_limit = drv_scan_result_size;
+    }
+    memcpy(results, drv_scan_result, ap_limit * sizeof(luat_wlan_scan_result_t));
+    return ap_limit;
+}
 
 int luat_airlink_drv_wlan_set_station_ip(luat_wlan_station_info_t *info);
 
@@ -134,3 +158,26 @@ int luat_airlink_drv_wlan_get_ap_rssi(void);
 int luat_airlink_drv_wlan_get_ap_gateway(char* buff);
 
 
+int luat_airlink_drv_wlan_scan_result_cb(void) {
+    #define MAX_SCAN_RESULT_SIZE 33
+    #define MAX_SCAN_RESULT_BUFF_SIZE (sizeof(luat_wlan_scan_result_t) * MAX_SCAN_RESULT_SIZE)
+    size_t fulllen = sizeof(luat_airlink_cmd_t) + 1 + MAX_SCAN_RESULT_BUFF_SIZE;
+    uint8_t* ptr = luat_heap_opt_zalloc(AIRLINK_MEM_TYPE, fulllen);
+    if (ptr == NULL) {
+        LLOGD("内存不足, 无法发送扫描结果");
+        return -1;
+    }
+    luat_wlan_scan_result_t *scan_result = ptr + sizeof(luat_airlink_cmd_t) + 1;
+
+    uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
+    airlink_queue_item_t item = {
+        .len = fulllen,
+        .cmd = ptr
+    };
+    item.cmd->cmd = 0x206;
+    item.cmd->len = 1 + MAX_SCAN_RESULT_BUFF_SIZE;
+    item.cmd->data[0] = (uint8_t)luat_wlan_scan_get_result(scan_result, MAX_SCAN_RESULT_SIZE);;
+    luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
+
+    return 0;
+}

+ 38 - 0
components/airlink/src/exec/luat_airlink_cmd_exec_wlan.c

@@ -12,6 +12,8 @@
 #include "lwip/netif.h"
 #include "lwip/pbuf.h"
 #include "luat_wlan.h"
+#include "luat_mem.h"
+#include "luat_msgbus.h"
 
 #define LUAT_LOG_TAG "airlink.wlan"
 #include "luat_log.h"
@@ -53,3 +55,39 @@ int luat_airlink_cmd_exec_wlan_scan(luat_airlink_cmd_t* cmd, void* userdata) {
     LLOGD("luat_wlan_scan ret=%d", ret);
     return 0;
 }
+
+// 注意, 下面的函数, 是主机端使用的, 例如scan的回调结果
+extern uint8_t drv_scan_result_size;
+extern uint8_t *drv_scan_result;
+
+static int scan_result_handler(lua_State* L, void* ptr) {
+    lua_getglobal(L, "sys_pub");
+    lua_pushstring(L, "WLAN_SCAN_DONE");
+    lua_call(L, 1, 0);
+    return 0;
+}
+
+int luat_airlink_cmd_exec_wlan_scan_result_cb(luat_airlink_cmd_t* cmd, void* userdata) {
+    uint8_t tmp = cmd->data[0];
+    LLOGD("收到扫描结果 %d", tmp);
+    if (drv_scan_result) {
+        luat_heap_opt_free(AIRLINK_MEM_TYPE, drv_scan_result);
+        drv_scan_result = NULL;
+        drv_scan_result_size = 0;
+    }
+    if (tmp > 0) {
+        drv_scan_result = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, tmp * sizeof(luat_wlan_scan_result_t));
+        if (drv_scan_result) {
+            memcpy(drv_scan_result, cmd->data + 1, tmp * sizeof(luat_wlan_scan_result_t));
+            drv_scan_result_size = tmp;
+        }
+        else {
+            LLOGW("获取到扫描结果, 但是内存不足, 丢弃 %d", tmp);
+        }
+    }
+    // 发送通知
+    rtos_msg_t msg = {0};
+    msg.handler = scan_result_handler;
+    luat_msgbus_put(&msg, 0);
+    return 0;
+}

+ 3 - 2
components/airlink/src/luat_airlink_cmds.c

@@ -40,7 +40,7 @@ CMD_DEFINE(wlan_sta_disconnect);
 CMD_DEFINE(wlan_ap_start);
 CMD_DEFINE(wlan_ap_stop);
 CMD_DEFINE(wlan_scan);
-CMD_DEFINE(wlan_scan_result);
+CMD_DEFINE(wlan_scan_result_cb);
 
 // GPIO指令, 0x300开始
 CMD_DEFINE(gpio_setup);
@@ -71,7 +71,8 @@ const luat_airlink_cmd_reg_t airlink_cmds[] = {
     CMD_REG(0x203, wlan_ap_start),
     CMD_REG(0x204, wlan_ap_stop),
     CMD_REG(0x205, wlan_scan),
-    // CMD_REG(0x206, wlan_scan_result),
+#else
+    CMD_REG(0x206, wlan_scan_result_cb),
 #endif
 
 #ifdef LUAT_USE_AIRLINK_EXEC_GPIO

+ 2 - 2
components/drv/src/luat_drv_wlan.c

@@ -34,11 +34,11 @@ int luat_drv_wlan_disconnect(void) {
 }
 
 int luat_drv_wlan_scan(void) {
-    return 0;
+    return luat_airlink_drv_wlan_scan();
 }
 
 int luat_drv_wlan_scan_get_result(luat_wlan_scan_result_t *results, size_t ap_limit) {
-    return 0;
+    return luat_airlink_drv_wlan_scan_get_result(results, ap_limit);
 }
 
 int luat_drv_wlan_set_station_ip(luat_wlan_station_info_t *info) {

+ 5 - 0
components/wlan/luat_lib_wlan.c

@@ -190,6 +190,7 @@ end)
 static int l_wlan_scan(lua_State* L){
     (void)L;
     #ifdef LUAT_USE_DRV_WLAN
+    luat_drv_wlan_scan();
     #else
     luat_wlan_scan();
     #endif
@@ -216,7 +217,11 @@ static int l_wlan_scan_result(lua_State* L) {
         return 1;
     }
     memset(results, 0, sizeof(luat_wlan_scan_result_t) * ap_limit);
+    #ifdef LUAT_USE_DRV_WLAN
+    int len = luat_drv_wlan_scan_get_result(results, ap_limit);
+    #else
     int len = luat_wlan_scan_get_result(results, ap_limit);
+    #endif
     for (int i = 0; i < len; i++)
     {
         lua_newtable(L);

+ 19 - 2
demo/airlink/air8000_wifi/main.lua

@@ -29,7 +29,7 @@ function test_ap()
 end
 
 function test_sta()
-    wlan.connect("uiot", "12345678")
+    wlan.connect("luatos1234", "12341234")
     netdrv.dhcp(socket.LWIP_STA, true)
     netdrv.napt(socket.LWIP_STA)
     while 1 do
@@ -44,6 +44,20 @@ function test_sta()
     end
 end
 
+function test_scan()
+    while 1 do
+        wlan.scan()
+        sys.wait(30 * 1000)
+    end
+end
+sys.subscribe("WLAN_SCAN_DONE", function ()
+    local results = wlan.scanResult()
+    log.info("scan", "results", #results)
+    for k,v in pairs(results) do
+        log.info("scan", v["ssid"], v["rssi"], (v["bssid"]:toHex()))
+    end
+end)
+
 sys.taskInit(function()
     -- 稍微缓一下
     sys.wait(10)
@@ -63,7 +77,10 @@ sys.taskInit(function()
     -- test_ap()
 
     -- 连接STA测试
-    test_sta()
+    -- test_sta()
+
+    -- wifi扫描测试
+    test_scan()
 end)