Просмотр исходного кода

fix: netdrv,whale的luat_netdrv_whale_ipevent应该在lwip线程内执行

Wendal Chen 9 месяцев назад
Родитель
Сommit
8a4eb3b6a7

+ 4 - 3
components/airlink/src/luat_airlink.c

@@ -107,13 +107,14 @@ int luat_airlink_queue_send(int tp, airlink_queue_item_t *item)
             ret = luat_rtos_queue_send(airlink_ippkg_queue, item, 0, 0);
         } 
     }
+    // LLOGD("luat_airlink_queue_send %d %d", tp, ret);
     if (ret == 0) {
         if (g_airlink_newdata_notify_cb) {
             g_airlink_newdata_notify_cb();
         }
         return 0;
     }
-    return -2;
+    return ret;
 }
 
 int luat_airlink_queue_get_cnt(int tp)
@@ -289,8 +290,8 @@ void luat_airlink_print_mac_pkg(uint8_t* buff, uint16_t len) {
 }
 
 void luat_airlink_hexdump(const char* tag, uint8_t* buff, uint16_t len) {
-    if (len > 500) {
-        len = 500;
+    if (len > 256) {
+        len = 256;
     }
     uint8_t* tmp = luat_heap_opt_zalloc(AIRLINK_MEM_TYPE, len * 2 + 1);
     if (tmp == NULL) {

+ 6 - 5
components/airlink/src/task/luat_airlink_spi_master_task.c

@@ -53,8 +53,6 @@ luat_airlink_irq_ctx_t g_airlink_irq_ctx;
 __USER_FUNC_IN_RAM__ static int slave_irq_cb(void *data, void *args)
 {
     uint32_t len = 0;
-    // if (is_waiting_queue) {
-    //     is_waiting_queue = 0;
     luat_rtos_queue_get_cnt(evt_queue, &len);
     // luat_rtos_event_send(spi_task_handle, 2, 2, 3, 4, 100);
     static uint32_t irq_seq = 0;
@@ -66,7 +64,6 @@ __USER_FUNC_IN_RAM__ static int slave_irq_cb(void *data, void *args)
         luat_rtos_queue_send(evt_queue, &evt, sizeof(evt), 0);
         luat_rtos_queue_send(evt_queue, &evt, sizeof(evt), 0);
     }
-    // }
     return 0;
 }
 
@@ -230,6 +227,7 @@ __USER_FUNC_IN_RAM__ void airlink_wait_and_prepare_data(uint8_t *txbuff)
     luat_event_t event = {0};
     airlink_queue_item_t item = {0};
     uint32_t timeout = 5;
+    int ret = 0;
     if (g_airlink_pause) {
         while (g_airlink_pause) {
             LLOGD("airlink spi 交互暂停中,允许主控休眠, 监测周期1000ms");
@@ -240,7 +238,7 @@ __USER_FUNC_IN_RAM__ void airlink_wait_and_prepare_data(uint8_t *txbuff)
     // LLOGD("link irq %d cmd %d ip %d", s_link.flags.irq_ready, s_link.flags.queue_cmd, s_link.flags.queue_ip);
     if (s_link.flags.irq_ready) {
         if (g_airlink_spi_conf.irq_timeout == 0) {
-            g_airlink_spi_conf.irq_timeout = 5000;
+            g_airlink_spi_conf.irq_timeout = 500;
         }
         timeout = g_airlink_spi_conf.irq_timeout;
     }
@@ -252,11 +250,14 @@ __USER_FUNC_IN_RAM__ void airlink_wait_and_prepare_data(uint8_t *txbuff)
     else
     {
         // is_waiting_queue = 1;
-        luat_rtos_queue_recv(evt_queue, &event, sizeof(luat_event_t), timeout);
+        ret = luat_rtos_queue_recv(evt_queue, &event, sizeof(luat_event_t), timeout);
         // is_waiting_queue = 0;
         if (2 == event.id) {
             // LLOGD("从机通知IRQ中断");
         }
+        else if (ret) {
+            // LLOGD("irq timeout %d", timeout);
+        }
     }
 
     record_statistic(event);

+ 0 - 10
components/network/netdrv/src/luat_netdrv.c

@@ -1,10 +1,6 @@
 #include "luat_base.h"
 #include "luat_netdrv.h"
 #include "luat_network_adapter.h"
-#ifdef __LUATOS__
-// #include "luat_netdrv_ch390h.h"
-// #include "luat_netdrv_uart.h"
-#endif
 #include "luat_mem.h"
 #include "luat_mcu.h"
 
@@ -40,12 +36,6 @@ luat_netdrv_t* luat_netdrv_setup(luat_netdrv_conf_t *conf) {
             return drvs[conf->id];
         }
         #endif
-        #ifdef LUAT_USE_NETDRV_UART
-        if (conf->impl == 16) { // UART
-            drvs[conf->id] = luat_netdrv_uart_setup(conf);
-            return drvs[conf->id];
-        }
-        #endif
         if (conf->impl == 64) { // WHALE
             drvs[conf->id] = luat_netdrv_whale_setup(conf);
             return drvs[conf->id];

+ 0 - 109
components/network/netdrv/src/luat_netdrv_uart.c

@@ -1,109 +0,0 @@
-#include "luat_base.h"
-#include "luat_netdrv.h"
-#include "luat_network_adapter.h"
-#include "luat_netdrv_uart.h"
-#include "luat_malloc.h"
-#include "luat_spi.h"
-#include "luat_gpio.h"
-#include "net_lwip2.h"
-#include "luat_ulwip.h"
-#include "lwip/tcp.h"
-#include "lwip/sys.h"
-#include "lwip/tcpip.h"
-#include "luat_ulwip.h"
-#include "luat_uart.h"
-#include "luat_crypto.h"
-
-#define LUAT_LOG_TAG "netdrv.uart"
-#include "luat_log.h"
-
-#define MAX_UART_CTX_ID (2)
-
-static luat_netdrv_uart_ctx_t* ctxs[MAX_UART_CTX_ID];
-
-static void uart_ip_output(void* userdata, uint8_t* buff, uint16_t len) {
-    luat_netdrv_uart_ctx_t* uc = (luat_netdrv_uart_ctx_t*)userdata;
-    
-    memcpy(uc->txbuff, "LU\x00\x00", 4);
-    uc->txbuff[2] = len & 0xFF;
-    uc->txbuff[3] = (len >> 8) & 0xFF;
-    
-    // 计算CRC32
-    uint32_t crc = luat_crc32(uc->txbuff + 4, len, 0xFFFFFFFF, 0);
-    memcpy(uc->txbuff + 4 + len, &crc, 4);
-    // 写到UART去
-    luat_uart_write(uc->uart_id, uc->txbuff, len + 8);
-}
-
-static void uart_data_input(int uart_id, uint32_t data_len) {
-
-}
-
-static err_t netif_output(struct netif *netif, struct pbuf *p) {
-    luat_netdrv_uart_ctx_t* uc = NULL;
-    for (size_t i = 0; i < MAX_UART_CTX_ID; i++)
-    {
-        if (ctxs[i] == NULL || ctxs[i]->netdrv.netif != netif) {
-            continue;
-        }
-        uc = (luat_netdrv_uart_ctx_t*)netif->state;
-        break;
-    }
-    if (uc == NULL) {
-        return 0;
-    }
-    pbuf_copy_partial(p, uc->txbuff + 4, p->tot_len, 0);
-    uart_ip_output(uc, uc->txbuff, p->tot_len);
-    return 0;
-}
-
-static err_t uart_netif_init(struct netif *netif) {
-    luat_netdrv_uart_ctx_t* uc = (luat_netdrv_uart_ctx_t*)netif->state;
-    netif->linkoutput = netif_output;
-    netif->output     = ulwip_etharp_output;
-    #if LWIP_IPV6
-    netif->output_ip6 = ethip6_output;
-    #endif
-    netif->mtu        = 1420;
-    netif->flags      = NETIF_FLAG_BROADCAST;
-    // memcpy(netif->hwaddr, uc->hwaddr, ETH_HWADDR_LEN);
-    // netif->hwaddr_len = ETH_HWADDR_LEN;
-    net_lwip2_set_netif(uc->netdrv.id, uc->netdrv.netif);
-    net_lwip2_register_adapter(uc->netdrv.id);
-    netif_set_up(uc->netdrv.netif);
-    return 0;
-}
-
-luat_netdrv_t* luat_netdrv_uart_setup(luat_netdrv_conf_t *conf) {
-    luat_netdrv_uart_ctx_t* ctx = NULL;
-    for (size_t i = 0; i < MAX_UART_CTX_ID; i++)
-    {
-        if (ctxs[i] == NULL) {
-            ctxs[i] = luat_heap_malloc(sizeof(luat_netdrv_uart_ctx_t));
-            ctx = ctxs[i];
-            memset(ctx, 0, sizeof(luat_netdrv_uart_ctx_t));
-            break;
-        }
-    }
-    if (ctx == NULL) {
-        return NULL;
-    }
-    ctx->netdrv.userdata = ctx;
-    ctx->uart_id = conf->spiid;
-    ctx->netdrv.netif = luat_heap_malloc(sizeof(struct netif));
-    ctx->netdrv.netif->state = ctx;
-    ctx->netdrv.id = conf->id;
-    ctx->netdrv.dataout = uart_ip_output;
-    // ctx->netdrv.dhcp = uart_dhcp;
-    ctx->ulwip.adapter_index = conf->id;
-    ctx->ulwip.netif = ctx->netdrv.netif;
-    ctx->ulwip.mtu = 1420;
-
-    netif_add(ctx->netdrv.netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4, ctx, uart_netif_init, netif_input);
-    netif_set_up(ctx->netdrv.netif);
-    netif_set_link_up(ctx->netdrv.netif);
-
-    luat_uart_ctrl(ctx->uart_id, LUAT_UART_SET_RECV_CALLBACK, uart_data_input);
-
-    return &ctx->netdrv;
-}

+ 24 - 4
components/network/netdrv/src/luat_netdrv_whale.c

@@ -9,6 +9,7 @@
 #include "lwip/netif.h"
 #include "lwip/ip.h"
 #include "lwip/pbuf.h"
+#include "lwip/tcpip.h"
 #include "net_lwip2.h"
 #include "luat_airlink.h"
 #include "luat_mem.h"
@@ -164,12 +165,16 @@ static int netif_ip_event_cb(lua_State *L, void* ptr) {
     return 0;
 }
 
-void luat_netdrv_whale_ipevent(luat_netdrv_t* drv, uint8_t updown) {
+typedef struct tmpptr {
+    luat_netdrv_t* drv;
+    uint8_t updown;
+}tmpptr_t;
+
+static void _luat_netdrv_whale_ipevent(tmpptr_t* ptr) {
+    luat_netdrv_t* drv = ptr->drv;
+    uint8_t updown = ptr->updown;
     rtos_msg_t msg = {0};
     void* userdata = NULL;
-    if (drv == NULL || drv->netif == NULL) {
-        return;
-    }
     luat_netdrv_whale_t* cfg = (luat_netdrv_whale_t*)drv->userdata;
     if (updown) {
         netif_set_up(drv->netif);
@@ -178,6 +183,8 @@ void luat_netdrv_whale_ipevent(luat_netdrv_t* drv, uint8_t updown) {
         }
         if (cfg->dhcp) {
             // LLOGD("dhcp启动 %p", cfg->ulwip.netif);
+            ip_addr_set_ip4_u32(&cfg->ulwip.netif->ip_addr, 0);
+            ip_addr_set_ip4_u32(&cfg->ulwip.netif->gw, 0);
             ulwip_dhcp_client_start(&cfg->ulwip);
         }
     }
@@ -185,6 +192,8 @@ void luat_netdrv_whale_ipevent(luat_netdrv_t* drv, uint8_t updown) {
         netif_set_down(drv->netif);
         if (cfg->dhcp) {
             // LLOGD("dhcp停止");
+            ip_addr_set_ip4_u32(&cfg->ulwip.netif->ip_addr, 0);
+            ip_addr_set_ip4_u32(&cfg->ulwip.netif->gw, 0);
             ulwip_dhcp_client_stop(&cfg->ulwip);
         }
     }
@@ -202,6 +211,17 @@ void luat_netdrv_whale_ipevent(luat_netdrv_t* drv, uint8_t updown) {
     luat_msgbus_put(&msg, 0);
 }
 
+void luat_netdrv_whale_ipevent(luat_netdrv_t* drv, uint8_t updown) {
+    if (drv == NULL || drv->netif == NULL) {
+        return;
+    }
+    tmpptr_t ptr = {
+        .drv = drv,
+        .updown = updown,
+    };
+    tcpip_callback_with_block(_luat_netdrv_whale_ipevent, &ptr, 1);
+}
+
 
 luat_netdrv_t* luat_netdrv_whale_create(luat_netdrv_whale_t* tmp) {
     // LLOGD("创建Whale设备");

+ 1 - 1
luat/demo/airlink/air8000_wifi/main.lua

@@ -24,7 +24,7 @@ end, gpio.PULLDOWN)
 
 if airlink.irqmode then
     log.info("尝试开启IRQ模式", "GPIO20 <--> GPIO140")
-    airlink.irqmode(true, 20, 140)
+    -- airlink.irqmode(true, 20, 140)
 end
 
 function test_ap()