Răsfoiți Sursa

change: netdrv,大改动,重构IP_READY/IP_LOSE事件,重构dhcp上下文存放逻辑,统一ip事件发布机制

Wendal Chen 6 luni în urmă
părinte
comite
9d03cc7d68

+ 23 - 15
components/airlink/src/exec/luat_airlink_cmd_exec_info.c

@@ -16,6 +16,7 @@
 #include "lwip/pbuf.h"
 #include "lwip/ip_addr.h"
 #include "luat_netdrv_whale.h"
+#include "luat_netdrv_event.h"
 
 #define LUAT_LOG_TAG "airlink"
 #include "luat_log.h"
@@ -61,18 +62,21 @@ __AIRLINK_CODE_IN_RAM__ int luat_airlink_cmd_exec_dev_info(luat_airlink_cmd_t* c
                 drv->netif->hwaddr_len = 6;
 
                 // STA网络状态对吗?
+                // LLOGD("sta station %d  netif link is %d", dev->wifi.sta_state, netif_is_link_up(drv->netif));
                 if (dev->wifi.sta_state == 0) {
-                    if (netif_is_up(drv->netif)) {
+                    if (netif_is_link_up(drv->netif)) {
                         // 网卡掉线了哦
                         LLOGD("wifi sta掉线了");
-                        luat_netdrv_whale_ipevent(drv, 0);
+                        // luat_netdrv_whale_ipevent(drv, 0);
+                        luat_netdrv_set_link_updown(drv, 0);
                     }
                 }
                 else {
-                    if (netif_is_up(drv->netif) == 0) {
+                    if (netif_is_link_up(drv->netif) == 0) {
                         // 网卡上线了哦
                         LLOGD("wifi sta上线了");
-                        luat_netdrv_whale_ipevent(drv, 1);
+                        // luat_netdrv_whale_ipevent(drv, 1);
+                        luat_netdrv_set_link_updown(drv, 1);
                     }
                 }
                 break;
@@ -96,18 +100,20 @@ __AIRLINK_CODE_IN_RAM__ int luat_airlink_cmd_exec_dev_info(luat_airlink_cmd_t* c
                 }
                 // AP网络状态对吗?
                 if (dev->wifi.ap_state == 0) {
-                    if (netif_is_up(drv->netif)) {
+                    if (netif_is_link_up(drv->netif)) {
                         // 网卡掉线了哦
                         LLOGD("wifi ap已关闭");
-                        luat_netdrv_whale_ipevent(drv, 0);
+                        // luat_netdrv_whale_ipevent(drv, 0);
+                        luat_netdrv_set_link_updown(drv, 0);
                     }
                 }
                 else {
-                    if (netif_is_up(drv->netif) == 0) {
+                    if (netif_is_link_up(drv->netif) == 0) {
                         // 网卡上线了哦
                         ipaddr_ntoa_r(&drv->netif->ip_addr, buff, 32);
-                        LLOGD("wifi ap已开启 %s", buff);
-                        luat_netdrv_whale_ipevent(drv, 1);
+                        LLOGD("wifi ap已开启 %s %p", buff, drv->netif);
+                        // luat_netdrv_whale_ipevent(drv, 1);
+                        luat_netdrv_set_link_updown(drv, 1);
                     }
                 }
                 break;
@@ -126,18 +132,20 @@ __AIRLINK_CODE_IN_RAM__ int luat_airlink_cmd_exec_dev_info(luat_airlink_cmd_t* c
         // 1是已注册, 5是漫游且已注册
         if (dev->cat1.cat_state != 1 && dev->cat1.cat_state != 5) {
             // 掉线了
-            if (netif_is_up(drv->netif)) {
+            if (netif_is_link_up(drv->netif)) {
                 // 网卡掉线了哦
-                LLOGD("4G网卡掉线了");
-                luat_netdrv_whale_ipevent(drv, 0);
+                LLOGD("4G代理网卡掉线了");
+                // luat_netdrv_whale_ipevent(drv, 0);
+                luat_netdrv_set_link_updown(drv, 0);
             }
         }
         else {
             // 上线了
-            if (netif_is_up(drv->netif) == 0) {
+            if (netif_is_link_up(drv->netif) == 0) {
                 // 网卡上线了哦
-                LLOGD("4G网卡上线了");
-                luat_netdrv_whale_ipevent(drv, 1);
+                LLOGD("4G代理网卡上线了");
+                // luat_netdrv_whale_ipevent(drv, 1);
+                luat_netdrv_set_link_updown(drv, 1);
             }
         }
     }

+ 1 - 1
components/ethernet/common/dhcp_client.c

@@ -267,7 +267,7 @@ int ip4_dhcp_run(dhcp_client_info_t *dhcp, Buffer_Struct *in, Buffer_Struct *out
 	*remote_ip = 0xffffffff;
 	int result = 0;
 	uint64_t tnow = luat_mcu_tick64_ms();
-	LLOGD("dhcp state %d %lld %lld %lld", dhcp->state, tnow, dhcp->lease_p1_time, dhcp->lease_p2_time);
+	LLOGD("dhcp state %d tnow %lld p1 %lld p2 %lld", dhcp->state, tnow, dhcp->lease_p1_time, dhcp->lease_p2_time);
 	if (in)
 	{
 		result = analyze_ip4_dhcp(dhcp, in);

+ 4 - 0
components/network/netdrv/include/luat_netdrv.h

@@ -2,6 +2,7 @@
 #define LUAT_NETDRV_H
 
 #include "lwip/pbuf.h"
+#include "luat_ulwip.h"
 
 struct luat_netdrv;
 
@@ -68,6 +69,7 @@ typedef struct luat_netdrv_statics
 typedef struct luat_netdrv {
     int32_t id;
     struct netif* netif;
+    ulwip_ctx_t* ulwip;
     luat_netdrv_dataout_cb dataout;
     luat_netdrv_bootup_cb boot;
     luat_netdrv_ready_cb ready;
@@ -125,6 +127,8 @@ void luat_netdrv_netif_set_down(struct netif* netif);
 
 void luat_netdrv_netif_set_link_down(struct netif* netif);
 
+int luat_netdrv_dhcp_opt(luat_netdrv_t* drv, void* userdata, int enable);
+
 extern uint32_t g_netdrv_debug_enable;
 
 #ifndef __NETDRV_CODE_IN_RAM__

+ 2 - 4
components/network/netdrv/include/luat_netdrv_ch390h.h

@@ -21,10 +21,8 @@ typedef struct ch390h
     uint8_t intpin;
     uint8_t adapter_id;
     uint8_t status;
-    uint8_t dhcp;
-    uint8_t hwaddr[6];
-    struct netif* netif;
-    ulwip_ctx_t ulwip;
+    uint8_t init_done;
+    uint8_t init_step;
     luat_netdrv_t* netdrv;
     uint8_t rxbuff[1600];
     uint8_t txbuff[1600];

+ 6 - 1
components/network/netdrv/include/luat_netdrv_event.h

@@ -3,6 +3,7 @@
 
 #include "lwip/pbuf.h"
 #include "lwip/ip_addr.h"
+#include "luat_ulwip.h"
 
 // 事件, 用户可订阅
 enum {
@@ -16,7 +17,7 @@ enum {
 typedef struct netdrv_tcp_evt {
     uint8_t id; // 网络适配器ID
     uint8_t flags; // 事件标志, 标识
-    uint8_t proto; // 协议类型, 1=TCP, 2=UDP, 3=HTTP, 4=MQTT, 5=WEBSOCKET
+    uint8_t proto; // 协议类型, 1=TCP, 2=UDP, 3=HTTP, 4=MQTT, 5=WEBSOCKET, 6=FTP
     uint8_t re; // 保留字段, 目前未使用
     ip_addr_t local_ip; // 本地IP地址
     ip_addr_t remote_ip; // 远程IP地址
@@ -41,4 +42,8 @@ void luat_netdrv_register_socket_event_cb(uint8_t id, uint32_t flags, luat_netdr
 
 void luat_netdrv_fire_socket_event_netctrl(uint32_t event_id, network_ctrl_t* ctrl, uint8_t proto);
 
+void luat_netdrv_send_ip_event(luat_netdrv_t* drv, uint8_t ready);
+
+void luat_netdrv_set_link_updown(luat_netdrv_t* drv, uint8_t updown);
+
 #endif

+ 1 - 3
components/network/netdrv/include/luat_netdrv_whale.h

@@ -8,12 +8,10 @@
 
 typedef struct luat_netdrv_whale {
     uint8_t id;
-    void* userdata;
     uint8_t flags;
     uint16_t mtu;
+    void* userdata;
     uint8_t mac[6];
-    uint8_t dhcp;
-    ulwip_ctx_t ulwip;
 }luat_netdrv_whale_t;
 
 

+ 12 - 27
components/network/netdrv/src/ch390h_task.c

@@ -18,6 +18,7 @@
 #include "luat_wdt.h"
 
 #include "luat_rtos.h"
+#include "luat_netdrv_event.h"
 
 #define LUAT_LOG_TAG "netdrv.ch390x"
 #include "luat_log.h"
@@ -63,7 +64,7 @@ static int ch390h_irq_cb(void *data, void *args) {
 }
 
 static int ch390h_bootup(ch390h_t* ch) {
-    if (ch->ulwip.netif != NULL) {
+    if (ch->init_done) {
         return 0;
     }
     // 初始化SPI设备, 由外部代码初始化, 因为不同bsp的速度不一样, 就不走固定值了
@@ -93,9 +94,7 @@ static int ch390h_bootup(ch390h_t* ch) {
         // LLOGI("enable pull mode, use pool mode");
     }
 
-    // 初始化dhcp相关资源
-    ch->ulwip.netif = ch->netif;
-    ch->ulwip.adapter_index = ch->adapter_id;
+    ch->init_done = 1;
     return 0;
 }
 
@@ -180,7 +179,7 @@ err_t ch390_netif_output(struct netif *netif, struct pbuf *p) {
         if (ch == NULL) {
             continue;
         }
-        if (ch->netif != netif) {
+        if (ch->netdrv->netif != netif) {
             continue;
         }
         ch390h_dataout_pbuf(ch, p);
@@ -265,11 +264,9 @@ static int task_loop_one(ch390h_t* ch, luat_ch390h_cstring_t* cs) {
         
         LLOGD("初始化MAC %02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
         // TODO 判断mac是否合法
-        memcpy(ch->hwaddr, buff, 6);
-        memcpy(ch->netif->hwaddr, buff, 6);
+        memcpy(ch->netdrv->netif->hwaddr, buff, 6);
         ch->status = 2;
         ch->netdrv->dataout = ch390h_dataout;
-        netif_set_up(ch->netif);
         luat_ch390h_basic_config(ch);
         luat_ch390h_set_phy(ch, 1);
         luat_ch390h_set_rx(ch, 1);
@@ -306,28 +303,16 @@ static int task_loop_one(ch390h_t* ch, luat_ch390h_cstring_t* cs) {
         // LLOGD("PHY状态 %02X", buff[0]);
         luat_ch390h_set_phy(ch, 1);
         luat_ch390h_set_rx(ch, 1);
-        if (netif_is_link_up(ch->netif)) {
-            LLOGI("link is down %d %d", ch->spiid, ch->cspin);
-            luat_netdrv_netif_set_link_down(ch->netif);
-            ulwip_netif_ip_event(&ch->ulwip);
-            if (ch->dhcp) {
-                // 停止dhcp定时器
-                ulwip_dhcp_client_stop(&ch->ulwip);
-            }
+        if (netif_is_link_up(ch->netdrv->netif)) {
+            LLOGI("link is down %d %d %p", ch->spiid, ch->cspin, ch->netdrv->netif);
+            luat_netdrv_set_link_updown(ch->netdrv, 0);
         }
         return 0; // 网络断了, 没那么快恢复的, 等吧
     }
 
-    if (!netif_is_link_up(ch->netif)) {
+    if (!netif_is_link_up(ch->netdrv->netif)) {
         LLOGI("link is up %d %d %s", ch->spiid, ch->cspin, (NSR & (1<<7)) ? "10M" : "100M");
-        netif_set_link_up(ch->netif);
-        luat_rtos_task_sleep(20); // 等待50ms
-        ulwip_netif_ip_event(&ch->ulwip);
-        if (ch->dhcp) {
-            luat_rtos_task_sleep(30); // 等待30ms再启动dhcp
-            // 启动dhcp定时器
-            ulwip_dhcp_client_start(&ch->ulwip);
-        }
+        luat_netdrv_set_link_updown(ch->netdrv, 1);
     }
 
     if (cs) {
@@ -366,7 +351,7 @@ static int task_loop_one(ch390h_t* ch, luat_ch390h_cstring_t* cs) {
             }
             else {
                 // 如果返回值是0, 那就是继续处理, 输入到netif
-                ret = luat_netdrv_netif_input_proxy(ch->netif, ch->rxbuff, len - 4);
+                ret = luat_netdrv_netif_input_proxy(ch->netdrv->netif, ch->rxbuff, len - 4);
                 if (ret) {
                     LLOGE("luat_netdrv_netif_input_proxy 返回错误!!! ret %d", ret);
                     return 1;
@@ -396,7 +381,7 @@ static int task_loop(ch390h_t *ch, luat_ch390h_cstring_t* cs) {
     int ret = 0;
     for (size_t i = 0; i < MAX_CH390H_NUM; i++)
     {
-        if (ch390h_drvs[i] != NULL && ch390h_drvs[i]->netif != NULL) {
+        if (ch390h_drvs[i] != NULL && ch390h_drvs[i]->init_step) {
             ret += task_loop_one(ch390h_drvs[i], ch == ch390h_drvs[i] ? cs : NULL);
         }
     }

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

@@ -318,3 +318,26 @@ void luat_netdrv_netif_set_link_down(struct netif* netif) {
     nd6_cleanup_netif(netif);
     #endif /* LWIP_IPV6 */
 }
+
+// DHCP操作
+
+int luat_netdrv_dhcp_opt(luat_netdrv_t* drv, void* userdata, int enable) {
+    if (drv->ulwip == NULL) {
+        return -1;
+    }
+    if (drv->ulwip->dhcp_enable == enable) {
+        return 0;
+    }
+    // cfg->dhcp = (uint8_t)enable;
+    drv->ulwip->dhcp_enable = enable;
+    if (drv->ulwip->netif == NULL) {
+        return 0;
+    }
+    if (enable) {
+        ulwip_dhcp_client_start(drv->ulwip);
+    }
+    else {
+        ulwip_dhcp_client_stop(drv->ulwip);
+    }
+    return 0;
+}

+ 37 - 37
components/network/netdrv/src/luat_netdrv_ch390h.c

@@ -28,19 +28,6 @@ extern err_t luat_netdrv_etharp_output(struct netif *netif, struct pbuf *q, cons
 
 extern err_t ch390_netif_output(struct netif *netif, struct pbuf *p);
 
-static int ch390h_dhcp(luat_netdrv_t* drv, void* userdata, int enable) {
-    ch390h_t* ch = (ch390h_t*)userdata;
-    ch->dhcp = (uint8_t)enable;
-    ch->ulwip.dhcp_enable = enable;
-    if (enable && ch->ulwip.netif) {
-        ulwip_dhcp_client_start(&ch->ulwip);
-    }
-    else {
-        ulwip_dhcp_client_stop(&ch->ulwip);
-    }
-    return 0;
-}
-
 static int ch390h_ctrl(luat_netdrv_t* drv, void* userdata, int cmd, void* buff, size_t len) {
     ch390h_t* ch = (ch390h_t*)userdata;
     if (ch == NULL) {
@@ -76,75 +63,88 @@ static err_t ch390_netif_init(struct netif *netif) {
     #endif
     netif->mtu        = 1460;
     netif->flags      = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
-    memcpy(netif->hwaddr, ch->hwaddr, ETH_HWADDR_LEN);
     netif->hwaddr_len = ETH_HWADDR_LEN;
-    net_lwip2_set_netif(ch->adapter_id, ch->netif);
+    netif_set_up(ch->netdrv->netif);
+    net_lwip2_set_netif(ch->adapter_id, ch->netdrv->netif);
     net_lwip2_register_adapter(ch->adapter_id);
     ch->status = 0;
-    LLOGD("netif init ok %d", ch->adapter_id);
+    LLOGD("adapter %d netif init ok", ch->adapter_id);
     return 0;
 }
 
 static void ch390_lwip_init(void* args) {
     ch390h_t* ch = (ch390h_t*)args;
-    netif_add(ch->netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4, ch, ch390_netif_init, luat_netdrv_netif_input_main);
+    netif_add(ch->netdrv->netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4, ch, ch390_netif_init, luat_netdrv_netif_input_main);
 }
 
 luat_netdrv_t* luat_netdrv_ch390h_setup(luat_netdrv_conf_t *cfg) {
-    
-    LLOGD("注册CH390H设备 SPI id %d cs %d irq %d", cfg->spiid, cfg->cspin, cfg->irqpin);
+
+    LLOGD("注册CH390H设备(%d) SPI id %d cs %d irq %d", cfg->id, cfg->spiid, cfg->cspin, cfg->irqpin);
     ch390h_t* ch = luat_heap_malloc(sizeof(ch390h_t));
-    if (ch == NULL) {
+    struct netif* netif = luat_heap_malloc(sizeof(struct netif));
+    luat_netdrv_t* drv = luat_heap_malloc(sizeof(luat_netdrv_t));
+    ulwip_ctx_t* ulwip = luat_heap_malloc(sizeof(ulwip_ctx_t));
+    if (ch == NULL || netif == NULL || drv == NULL || ulwip == NULL) {
         LLOGD("分配CH390H内存失败!!!");
-        return NULL;
+        goto clean;
     }
+    
     memset(ch, 0, sizeof(ch390h_t));
+    memset(netif, 0, sizeof(struct netif));
+    memset(drv, 0, sizeof(luat_netdrv_t));
+    memset(ulwip, 0, sizeof(ulwip_ctx_t));
+
     ch->adapter_id = cfg->id;
     ch->cspin = cfg->cspin;
     ch->spiid = cfg->spiid;
     ch->intpin = cfg->irqpin;
+    // ch->dhcp = 1;
+    ulwip->dhcp_enable = 1;
+    ulwip->adapter_index = cfg->id;
+    ulwip->netif = netif;
+
+    drv->ulwip = ulwip;
+
     for (size_t i = 0; i < MAX_CH390H_NUM; i++)
     {
         if (ch390h_drvs[i] != NULL) {
             if (ch390h_drvs[i]->adapter_id == ch->adapter_id) {
                 LLOGE("已经注册过相同的adapter_id %d", ch->adapter_id);
-                luat_heap_free(ch);
-                return NULL;
+                goto clean;
             }
             if (ch390h_drvs[i]->spiid == ch->spiid  && ch390h_drvs[i]->cspin == ch->cspin) {
                 LLOGE("已经注册过相同的spi+cs %d %d",ch->spiid, ch->cspin);
-                luat_heap_free(ch);
-                return NULL;
+                goto clean;
             }
             if (ch390h_drvs[i]->intpin != 255 && ch390h_drvs[i]->intpin == ch->intpin) {
                 LLOGE("已经注册过相同的int脚 %d", ch->intpin);
-                luat_heap_free(ch);
-                return NULL;
+                goto clean;
             }
             continue;
         }
         ch390h_drvs[i] = ch;
-        if (ch390h_drvs[i]->netif == NULL) {
-            ch390h_drvs[i]->netif = luat_heap_malloc(sizeof(struct netif));
-            memset(ch390h_drvs[i]->netif, 0, sizeof(struct netif));
-        }
-        luat_netdrv_t* drv = luat_heap_malloc(sizeof(luat_netdrv_t));
-        memset(drv, 0, sizeof(luat_netdrv_t));
-        drv->netif = ch390h_drvs[i]->netif;
+        // ch390h_drvs[i]->netif = netif;
+        
+        drv->netif = netif;
         drv->userdata = ch390h_drvs[i];
         drv->id = ch->adapter_id;
         drv->dataout = NULL;
         drv->boot = NULL;
-        drv->dhcp = ch390h_dhcp;
+        drv->dhcp = luat_netdrv_dhcp_opt;
         drv->ctrl = ch390h_ctrl;
         ch->netdrv = drv;
         tcpip_callback_with_block((tcpip_callback_fn)ch390_lwip_init, ch, 1);
+        ch->init_step = 1; // 已经完成基础初始化
         extern void luat_ch390h_task_start(void);
         luat_ch390h_task_start();
-        LLOGD("ch390注册完成");
+        LLOGD("注册完成 adapter %d spi %d cs %d irq %d", ch->adapter_id, ch->spiid, ch->cspin, ch->intpin);
         return drv;
     }
     LLOGE("已经没有CH390H空位了!!!");
-    luat_heap_free(ch);
+clean:
+    if (ch) luat_heap_free(ch);
+    if (netif) luat_heap_free(netif);
+    if (drv) luat_heap_free(drv);
+    if (ulwip) luat_heap_free(ulwip);
     return NULL;
 }

+ 132 - 1
components/network/netdrv/src/luat_netdrv_event.c

@@ -3,6 +3,7 @@
 #include "luat_network_adapter.h"
 #include "luat_mem.h"
 #include "luat_mcu.h"
+#include "luat_ulwip.h"
 
 #include "lwip/ip_addr.h"
 #include "lwip/netif.h"
@@ -76,4 +77,134 @@ __NETDRV_CODE_IN_RAM__ void luat_netdrv_fire_socket_event_netctrl(uint32_t event
     evt.remote_port = ctrl->remote_port;
     evt.userdata = ctrl->user_data;
     s_tcpevt_regs[adapter_id].cb(&evt, s_tcpevt_regs[adapter_id].userdata);
-}
+}
+
+// IP 事件, 分成 IP_READY 和 IP_LOSE
+#ifdef __LUATOS__
+static int netif_ip_event_cb(lua_State *L, void* ptr) {
+    rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
+    char buff[32] = {0};
+    luat_netdrv_t* netdrv = luat_netdrv_get(msg->arg1);
+    if (netdrv == NULL) {
+        return 0;
+    }
+    lua_getglobal(L, "sys_pub");
+    if (lua_isfunction(L, -1)) {
+        if (msg->arg2 == 0) {
+            LLOGD("IP_LOSE %d", netdrv->id);
+            lua_pushstring(L, "IP_LOSE");
+            lua_pushinteger(L, netdrv->id);
+            lua_call(L, 2, 0);
+        }
+        else {
+            ipaddr_ntoa_r(&netdrv->netif->ip_addr, buff,  32);
+            LLOGD("IP_READY %d %s", netdrv->id, buff);
+            lua_pushstring(L, "IP_READY");
+            lua_pushstring(L, buff);
+            lua_pushinteger(L, netdrv->id);
+            lua_call(L, 3, 0);
+        }
+    }
+    return 0;
+}
+
+void luat_netdrv_send_ip_event(luat_netdrv_t* drv, uint8_t ready) {
+    rtos_msg_t msg = {0};
+    if (drv == NULL || drv->netif == NULL) {
+        return;
+    }
+    msg.arg1 = drv->id;
+    msg.arg2 = ready;
+    msg.ptr = NULL;
+    msg.handler = netif_ip_event_cb;
+    luat_msgbus_put(&msg, 0);
+}
+
+#else
+// TODO 改成weak实现?
+void luat_netdrv_send_ip_event(luat_netdrv_t* drv, uint8_t updown) {
+    // nop
+}
+#endif
+
+typedef struct tmpptr {
+    luat_netdrv_t* drv;
+    uint8_t updown;
+}tmpptr_t;
+
+static void delay_dhcp_start(void* args) {
+    ulwip_ctx_t *ctx = (ulwip_ctx_t *)args;
+    if (ctx && ctx->dhcp_enable) {
+        ulwip_dhcp_client_start(ctx);
+    }
+}
+
+static void link_updown(void* args) {
+    tmpptr_t* ptr = (tmpptr_t*)args;
+    luat_netdrv_t* drv = ptr->drv;
+    uint8_t updown = ptr->updown;
+    void* userdata = NULL;
+    if (drv == NULL || drv->netif == NULL) {
+        return;
+    }
+    struct netif *netif = drv->netif;
+    ulwip_ctx_t *ulwip = drv->ulwip;
+    // LLOGI("netif %d link prev %d set %s %p", drv->id, netif_is_link_up(netif), updown ? "UP" : "DOWN", netif);
+    if (updown && netif_is_link_up(netif) == 0) {
+        LLOGD("网卡(%d)设置为UP", drv->id);
+        netif_set_link_up(netif);
+        net_lwip2_set_link_state(drv->id, 1);
+        if (ulwip) {
+            if (ulwip->netif == NULL) {
+                ulwip->netif = netif;
+            }
+            if (ulwip->dhcp_enable) {
+                ulwip_dhcp_client_stop(ulwip);
+                // 延时50ms, 避免netif_set_up和dhcp冲突
+                sys_timeout(50, delay_dhcp_start, ulwip);
+            }
+            else if (!ip_addr_isany(&netif->ip_addr)) {
+                // 静态IP, 那就发布IP_READY事件
+                luat_netdrv_send_ip_event(drv, 1);
+            }
+        }
+        else {
+            if (!ip_addr_isany(&netif->ip_addr)) {
+                // 静态IP, 那就发布IP_READY事件
+                luat_netdrv_send_ip_event(drv, 1);
+            }
+        }
+        return;
+    }
+    if (updown == 0 && netif_is_link_up(netif)) {
+        LLOGD("网卡(%d)设置为DOWN", drv->id);
+        luat_netdrv_netif_set_link_down(netif);
+        if (ulwip && ulwip->dhcp_enable) {
+            ulwip_dhcp_client_stop(ulwip);
+        }
+        net_lwip2_set_link_state(drv->id, 0);
+        luat_netdrv_send_ip_event(drv, 0);
+    }
+
+    #if 0
+    network_adapter_info* info = network_adapter_fetch(drv->id, &userdata);
+    if (info == NULL || info->check_ready == NULL) {
+        // LLOGI("网络适配器(%d)不存在, 或者没有check_ready函数", adapter_index);
+        return;
+    }
+    int ready = info->check_ready(userdata);
+    net_lwip2_set_link_state(drv->id, ready);
+    luat_netdrv_send_ip_event(drv, ready);
+    #endif
+}
+
+void luat_netdrv_set_link_updown(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(link_updown, &ptr, 1);
+}

+ 22 - 119
components/network/netdrv/src/luat_netdrv_whale.c

@@ -14,6 +14,7 @@
 #include "luat_airlink.h"
 #include "luat_mem.h"
 #include "luat_netdrv_whale.h"
+#include "luat_netdrv_event.h"
 #include "luat_ulwip.h"
 
 #define LUAT_LOG_TAG "netdrv.whale"
@@ -32,7 +33,6 @@ extern luat_airlink_dev_info_t g_airlink_self_dev_info;
 static err_t luat_netif_init(struct netif *netif);
 static err_t netif_output(struct netif *netif, struct pbuf *p);
 static int netif_ip_event_cb(lua_State *L, void* ptr);
-static int whale_dhcp(luat_netdrv_t* drv, void* userdata, int enable);
 
 void luat_netdrv_whale_dataout(luat_netdrv_t* drv, void* userdata, uint8_t* buff, uint16_t len) {
     // TODO 发送到spi slave task
@@ -87,8 +87,8 @@ void luat_netdrv_whale_boot(luat_netdrv_t* drv, void* userdata) {
         memset(netdrv->netif, 0, sizeof(struct netif));
     }
     luat_netdrv_whale_t* cfg = (luat_netdrv_whale_t*)userdata;
-    cfg->ulwip.netif = netdrv->netif;
-    cfg->ulwip.adapter_index = cfg->id;
+    drv->ulwip->netif = netdrv->netif;
+    drv->ulwip->adapter_index = cfg->id;
 
     netif_add(netdrv->netif, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY, netdrv, luat_netif_init, luat_netdrv_netif_input_main);
 
@@ -97,20 +97,15 @@ void luat_netdrv_whale_boot(luat_netdrv_t* drv, void* userdata) {
         // 默认是down的就行
     }
     else if (netdrv->id == NW_ADAPTER_INDEX_LWIP_GP_GW) {
-        // 这里要分情况, 如果本身带4G模块, 那么就up, 否则就是down
-        // 通过devinfo等途径, 通知对端netif的开启与关闭
-        #if defined(LUAT_USE_MOBILE) && !defined(LUAT_USE_DRV_MOBILE)
-        netif_set_up(netdrv->netif);
-        #endif
     }
     else {
-        netif_set_up(netdrv->netif);
+        // 其他的设备, 直接设置成up和link up
+        netif_set_link_up(netdrv->netif);
     }
     if (netdrv->id == NW_ADAPTER_INDEX_LWIP_WIFI_STA) {
-        cfg->dhcp = 1;
-        cfg->ulwip.dhcp_enable = 1;
+        drv->ulwip->dhcp_enable = 1;
     }
-    netif_set_link_up(netdrv->netif);
+    netif_set_up(netdrv->netif);
     net_lwip2_set_netif(netdrv->id, netdrv->netif);
     net_lwip2_register_adapter(netdrv->id);
     // LLOGD("luat_netdrv_whale_boot 执行完成");
@@ -157,109 +152,34 @@ static err_t luat_netif_init(struct netif *netif) {
     return 0;
 }
 
-static int netif_ip_event_cb(lua_State *L, void* ptr) {
-    rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
-    char buff[32] = {0};
-    luat_netdrv_t* netdrv = luat_netdrv_get(msg->arg1);
-    if (netdrv == NULL) {
-        return 0;
-    }
-    lua_getglobal(L, "sys_pub");
-    if (lua_isfunction(L, -1)) {
-        if (msg->arg2 == 0) {
-            LLOGD("IP_LOSE %d", netdrv->id);
-            lua_pushstring(L, "IP_LOSE");
-            lua_pushinteger(L, netdrv->id);
-            lua_call(L, 2, 0);
-        }
-        else {
-            ipaddr_ntoa_r(&netdrv->netif->ip_addr, buff,  32);
-            LLOGD("IP_READY %d %s", netdrv->id, buff);
-            lua_pushstring(L, "IP_READY");
-            lua_pushstring(L, buff);
-            lua_pushinteger(L, netdrv->id);
-            lua_call(L, 3, 0);
-        }
-    }
-    return 0;
-}
-
-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;
-    luat_netdrv_whale_t* cfg = (luat_netdrv_whale_t*)drv->userdata;
-    if (updown) {
-        netif_set_up(drv->netif);
-        if (cfg->ulwip.netif == NULL) {
-            cfg->ulwip.netif = drv->netif;
-        }
-        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);
-        }
-    }
-    else {
-        luat_netdrv_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);
-        }
-    }
-	network_adapter_info* info = network_adapter_fetch(drv->id, &userdata);
-    if (info == NULL || info->check_ready == NULL) {
-        // LLOGI("网络适配器(%d)不存在, 或者没有check_ready函数", adapter_index);
-        return;
-    }
-    int ready = info->check_ready(userdata);
-    net_lwip2_set_link_state(drv->id, ready);
-    msg.arg1 = drv->id;
-    msg.arg2 = ready;
-    msg.ptr = NULL;
-    msg.handler = netif_ip_event_cb;
-    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设备");
     luat_netdrv_t* netdrv = luat_heap_malloc(sizeof(luat_netdrv_t));
-    if (netdrv == NULL) {
+    ulwip_ctx_t* ulwip = luat_heap_malloc(sizeof(ulwip_ctx_t));
+    luat_netdrv_whale_t* cfg = luat_heap_malloc(sizeof(luat_netdrv_whale_t));
+    if (netdrv == NULL || ulwip == NULL || cfg == NULL) {
+        if (netdrv)
+            luat_heap_free(netdrv);
+        if (ulwip)
+            luat_heap_free(ulwip);
+        if (cfg)
+            luat_heap_free(cfg);
         return NULL;
     }
+    memset(ulwip, 0, sizeof(ulwip_ctx_t));
+    memset(netdrv, 0, sizeof(luat_netdrv_t));
     // 把配置信息拷贝一份
-    luat_netdrv_whale_t* cfg = luat_heap_malloc(sizeof(luat_netdrv_whale_t));
     memcpy(cfg, tmp, sizeof(luat_netdrv_whale_t));
 
     // 初始化netdrv
-    memset(netdrv, 0, sizeof(luat_netdrv_t));
     netdrv->id = cfg->id;
     netdrv->netif = NULL;
     netdrv->dataout = luat_netdrv_whale_dataout;
     netdrv->boot = luat_netdrv_whale_boot;
     netdrv->userdata = cfg;
-    netdrv->dhcp = whale_dhcp;
+    netdrv->dhcp = luat_netdrv_dhcp_opt;
+    netdrv->ulwip = ulwip;
+    ulwip->adapter_index = netdrv->id;
     return netdrv;
 }
 
@@ -291,21 +211,4 @@ luat_netdrv_t*  luat_netdrv_whale_setup(luat_netdrv_conf_t* conf) {
     return drv;
 }
 
-static int whale_dhcp(luat_netdrv_t* drv, void* userdata, int enable) {
-    luat_netdrv_whale_t* cfg = (luat_netdrv_whale_t*)userdata;
-    if (cfg->dhcp == enable) {
-        return 0;
-    }
-    cfg->dhcp = (uint8_t)enable;
-    cfg->ulwip.dhcp_enable = enable;
-    if (cfg->ulwip.netif == NULL) {
-        return 0;
-    }
-    if (enable) {
-        ulwip_dhcp_client_start(&cfg->ulwip);
-    }
-    else {
-        ulwip_dhcp_client_stop(&cfg->ulwip);
-    }
-    return 0;
-}
+

+ 18 - 1
components/network/ulwip/src/ulwip_dhcp_client.c

@@ -3,6 +3,11 @@
 #include "luat_ulwip.h"
 #include "luat_crypto.h"
 
+#ifdef LUAT_USE_NETDRV
+#include "luat_netdrv.h"
+#include "luat_netdrv_event.h"
+#endif
+
 #define LUAT_LOG_TAG "ulwip"
 #include "luat_log.h"
 
@@ -76,7 +81,18 @@ on_check:
             luat_heap_free(rxbuff);
             rxbuff = NULL;
         }
+        #ifndef LUAT_USE_NETDRV
         ulwip_netif_ip_event(ctx);
+        #else
+        luat_netdrv_t* drv = luat_netdrv_get(adapter_index);
+        if (drv == NULL || drv->netif == NULL) {
+            LLOGE("adapter %d netdrv not found", adapter_index);
+        }
+        else {
+            net_lwip2_set_link_state(adapter_index, 1);
+            luat_netdrv_send_ip_event(drv, 1);
+        }
+        #endif
         luat_rtos_timer_stop(ctx->dhcp_timer);
         luat_rtos_timer_start(ctx->dhcp_timer, 60000, 1, dhcp_client_timer_cb, ctx);
         if (ctx->event_cb) {
@@ -229,7 +245,7 @@ static void reset_dhcp_client(ulwip_ctx_t *ctx) {
 }
 
 void ulwip_dhcp_client_start(ulwip_ctx_t *ctx) {
-    // LLOGD("dhcp start netif %p", ctx->netif);
+    LLOGD("adapter %d dhcp start netif %p", ctx->adapter_index, ctx->netif);
     if (ctx->netif == NULL) {
         LLOGE("ctx->netif is NULL!!!!");
         return;
@@ -245,6 +261,7 @@ void ulwip_dhcp_client_start(ulwip_ctx_t *ctx) {
     if (!ctx->dhcp_client) {
         ctx->dhcp_client = luat_heap_malloc(sizeof(dhcp_client_info_t));
         reset_dhcp_client(ctx);
+        net_lwip2_set_dhcp_client(ctx->adapter_index, ctx->dhcp_client);
         luat_rtos_timer_create(&ctx->dhcp_timer);
         s_ctxs[ctx->adapter_index] = ctx; // 保存到全局数组中
     }