Ver código fonte

Merge branch 'master' of https://gitee.com/openLuat/LuatOS

alienwalker 11 meses atrás
pai
commit
ee5c4273ad

+ 1 - 1
components/airlink/src/task/luat_airlink_task.c

@@ -51,13 +51,13 @@ __USER_FUNC_IN_RAM__ static int luat_airlink_task(void *param) {
             // if (ptr->cmd != 0x10) {
             //     LLOGD("收到指令/回复 cmd %d len %d", ptr->cmd, ptr->len);
             // }
+            g_airlink_last_cmd_timestamp = luat_mcu_tick64_ms();
             cmd_reg = airlink_cmds;
             while (1) {
                 if (cmd_reg->id == 0) {
                     break;
                 }
                 if (cmd_reg->id == ptr->cmd) {
-                    g_airlink_last_cmd_timestamp = luat_mcu_tick64_ms();
                     // LLOGI("找到CMD执行程序 %p", cmd_reg->exec);
                     cmd_reg->exec(ptr, NULL);
                     break;

+ 2 - 0
components/network/httpsrv/inc/luat_httpsrv.h

@@ -9,6 +9,8 @@ typedef struct luat_httpsrv_ctx
     int lua_ref_id;
     int server_fd;
     void* userdata;
+    uint8_t adapter_id;
+    struct netif* netif;
 }luat_httpsrv_ctx_t;
 
 

+ 7 - 14
components/network/httpsrv/src/luat_httpsrv_lwip.c

@@ -18,16 +18,6 @@
 
 #include "http_parser.h"
 
-// #define HTTPSRV_MAX (4)
-// static luat_httpsrv_ctx_t ctxs[HTTPSRV_MAX];
-
-// #define CLIENT_BUFF_SIZE (4096)
-
-// #define HTTP_RESP_400 "HTTP/1.0 400 Bad Request\r\n"
-// #define HTTP_RESP_200 "HTTP/1.0 200 OK\r\n"
-// #define HTTP_RESP_302 "HTTP/1.0 302 Found\r\n"
-// #define HTTP_RESP_404 "HTTP/1.0 404 Not Found\r\n"
-
 
 typedef struct client_socket_ctx
 {
@@ -107,6 +97,7 @@ static int client_write(client_socket_ctx_t* client, const char* buff, size_t le
 #endif
     if (ret == 0) {
         client->send_size += len;
+        // LLOGD("send more %d/%d", client->sent_size, client->send_size);
     }
     else {
         LLOGE("client_write err %d", ret);
@@ -269,7 +260,7 @@ static err_t client_recv_cb(void *arg, struct tcp_pcb *tpcb,
         tcp_abort(tpcb);
         return ERR_ABRT;
     }
-    LLOGD("tpcb %p p %p len %d err %d", tpcb, p, p->len, err);
+    // LLOGD("tpcb %p p %p len %d err %d", tpcb, p, p->len, err);
     client_socket_ctx_t* ctx = (client_socket_ctx_t*)arg;
     if (ctx->buff == NULL) {
         ctx->buff = luat_heap_malloc(4096);
@@ -331,7 +322,7 @@ static err_t client_sent_cb(void *arg, struct tcp_pcb *tpcb, u16_t len) {
         if (ctx->sbuff_offset) {
             ret = client_write(ctx, (const char*)ctx->sbuff, ctx->sbuff_offset);
             if (ret == 0) {
-                ctx->send_size += ctx->sbuff_offset;
+                // ctx->send_size += ctx->sbuff_offset;
                 ctx->sbuff_offset = 0;
             }
         }
@@ -350,12 +341,13 @@ static err_t client_sent_cb(void *arg, struct tcp_pcb *tpcb, u16_t len) {
                 ctx->sbuff_offset = ret;
                 ret = client_write(ctx, (const char*)ctx->sbuff, ctx->sbuff_offset);
                 if (ret == 0) {
-                    ctx->send_size += ctx->sbuff_offset;
+                    // ctx->send_size += ctx->sbuff_offset;
                     ctx->sbuff_offset = 0;
                 }
             }
         }
     }
+    // LLOGD("done? %d sent %d/%d", ctx->write_done, ctx->sent_size, ctx->send_size);
     if (ctx->write_done && ctx->send_size == ctx->sent_size) {
         tcp_err(ctx->pcb, NULL);
         tcp_sent(ctx->pcb, NULL);
@@ -383,6 +375,7 @@ static err_t srv_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
     (void)arg;
     if (err) {
         LLOGD("accpet err %d", err);
+        tcp_abort(newpcb);
         return ERR_OK;
     }
     client_socket_ctx_t* ctx = luat_heap_malloc(sizeof(client_socket_ctx_t));
@@ -422,7 +415,7 @@ int luat_httpsrv_start(luat_httpsrv_ctx_t* ctx) {
         return -1;
     }
     tcp->flags |= SOF_REUSEADDR;
-    ret = tcp_bind(tcp, NULL, ctx->port);
+    ret = tcp_bind(tcp, &ctx->netif->ip_addr, ctx->port);
     if (ret) {
         LLOGD("httpsrv bind port %d ret %d", ctx->port);
         return -2;

+ 21 - 6
components/network/httpsrv/src/luat_lib_httpsrv.c

@@ -9,15 +9,20 @@
 
 #include "luat_base.h"
 #include "luat_httpsrv.h"
+#include "luat_network_adapter.h"
+#include "luat_netdrv.h"
+#include "lwip/netif.h"
+#include "lwip/ip_addr.h"
 
 #define LUAT_LOG_TAG "httpsrv"
 #include "luat_log.h"
 
 /*
 启动并监听一个http端口
-@api httpsrv.start(port, func)
+@api httpsrv.start(port, func, adapter)
 @int 端口号
 @function 回调函数
+@int 网络适配器编号, 默认是平台自带的网络协议栈
 @return bool 成功返回true, 否则返回false
 @usage
 
@@ -46,20 +51,30 @@ end)
 -- 当前默认查找 /luadb/xxx 下的文件,暂不可配置
 */
 static int l_httpsrv_start(lua_State *L) {
+    luat_ip_addr_t local_ip, net_mask, gate_way, ipv6;
+    char buff[64] = {0};
     int port = luaL_checkinteger(L, 1);
     if (!lua_isfunction(L, 2)) {
         LLOGW("httpsrv need callback function!!!");
         return 0;
     }
-    lua_pushvalue(L, 2);
-    int lua_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
     luat_httpsrv_ctx_t ctx = {
-        .port = port,
-        .lua_ref_id = lua_ref_id
+        .port = port
     };
+    uint8_t adapter_index = luaL_optinteger(L, 3, network_get_last_register_adapter());
+    ctx.adapter_id = adapter_index;
+    luat_netdrv_t* drv = luat_netdrv_get(adapter_index);
+    if (drv == NULL || drv->netif == NULL) {
+        LLOGW("该网络还没准备好 %d", adapter_index);
+        return 0;
+    }
+    ctx.netif = drv->netif;
+    lua_pushvalue(L, 2);
+    ctx.lua_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
     int ret = luat_httpsrv_start(&ctx);
     if (ret == 0) {
-        LLOGI("http listen at 0.0.0.0:%d", ctx.port);
+        ipaddr_ntoa_r(&drv->netif->ip_addr, buff,  32);
+        LLOGI("http listen at %s:%d", buff, ctx.port);
     }
     lua_pushboolean(L, ret == 0 ? 1 : 0);
     return 1;

+ 1 - 1
components/wlan/luat_lib_wlan.c

@@ -289,7 +289,7 @@ log.info("wlan mac", wlan.getMac())
 static int l_wlan_get_mac(lua_State* L){
     uint8_t tmp[6] = {0};
     char tmpbuff[16] = {0};
-    #ifdef LUAT_USE_NETDRV
+    #if defined(LUAT_USE_NETDRV) && !defined(CONFIG_SOC_BK7258)
     int id = luaL_optinteger(L, 1, 0);
     if (id == 0) {
         id = NW_ADAPTER_INDEX_LWIP_WIFI_STA;

+ 2 - 1
demo/wlan/ap_get_start/main.lua

@@ -28,6 +28,7 @@ local LEDA= gpio.setup(12, 0, gpio.PULLUP)
 local scan_result = {}
 
 sys.taskInit(function()
+    log.info("AP", "luatos_" .. wlan.getMac(), "12341234")
     sys.wait(1000)
     wlan.init()
     sys.wait(300)
@@ -70,7 +71,7 @@ sys.taskInit(function()
         end
         -- 其他情况就是找不到了
         return 404, {}, "Not Found" .. uri
-    end)
+    end, socket.LWIP_AP)
     log.info("web", "pls open url http://192.168.4.1/")
 end)
 

+ 1 - 1
demo/wlan/httpsrv/main.lua

@@ -45,7 +45,7 @@ sys.taskInit(function()
             return 200, {}, "ok"
         end
         return 404, {}, "Not Found" .. uri
-    end)
+    end, socket.LWIP_STA)
     log.info("web", "pls open url http://" .. _G.wlan_ip .. "/")
 end)