فهرست منبع

update: 优化ulwip库的dhcp逻辑,加快dhcp过程

Wendal Chen 1 سال پیش
والد
کامیت
ee930cd579
2فایلهای تغییر یافته به همراه29 افزوده شده و 6 حذف شده
  1. 13 5
      components/network/ulwip/binding/luat_lib_ulwip.c
  2. 16 1
      components/network/ulwip/src/ulwip_dhcp_client.c

+ 13 - 5
components/network/ulwip/binding/luat_lib_ulwip.c

@@ -394,12 +394,20 @@ static int l_ulwip_link(lua_State *L) {
     }
     if (lua_isboolean(L, 2))
     {
-        if (lua_toboolean(L, 2))
+        int new_status = lua_toboolean(L, 2);
+        int old_status = netif_is_link_up(nets[idx].netif);
+        if (new_status != old_status)
         {
-            netif_set_link_up(nets[idx].netif);
-        }
-        else {
-            netif_set_link_down(nets[idx].netif);
+            if (new_status) {
+                netif_set_link_up(nets[idx].netif);
+                if (!nets[idx].ip_static) {
+                    ulwip_dhcp_client_start(&nets[idx]);
+                }
+            }
+            else {
+                netif_set_link_down(nets[idx].netif);
+                ulwip_dhcp_client_stop(&nets[idx]);
+            }
         }
         ulwip_netif_ip_event(&nets[idx]);
     }

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

@@ -15,6 +15,8 @@
 //           DHCP 客户端逻辑
 // -------------------------------------
 
+static void dhcp_client_timer_cb(void *arg);
+
 static int ulwip_dhcp_client_run(ulwip_ctx_t* ctx, char* rxbuff, size_t len) {
     PV_Union uIP;
     // 检查dhcp的状态
@@ -34,6 +36,7 @@ static int ulwip_dhcp_client_run(ulwip_ctx_t* ctx, char* rxbuff, size_t len) {
 
     // 看看是不是获取成功了
     if (DHCP_STATE_CHECK == dhcp->state) {
+on_check:
         uIP.u32 = dhcp->ip;
 		LLOGD("动态IP:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
 		uIP.u32 = dhcp->submask;
@@ -49,19 +52,26 @@ static int ulwip_dhcp_client_run(ulwip_ctx_t* ctx, char* rxbuff, size_t len) {
         dhcp->state = DHCP_STATE_WAIT_LEASE_P1;
         if (rxbuff) {
             luat_heap_free(rxbuff);
+            rxbuff = NULL;
         }
         ulwip_netif_ip_event(ctx);
+        luat_rtos_timer_stop(ctx->dhcp_timer);
+        luat_rtos_timer_start(ctx->dhcp_timer, 60000, 1, dhcp_client_timer_cb, ctx);
         return 0;
     }
     result = ip4_dhcp_run(dhcp, rxbuff == NULL ? NULL : &rx_msg_buf, &tx_msg_buf, &remote_ip);
     if (rxbuff) {
         luat_heap_free(rxbuff);
+        rxbuff = NULL;
     }
     if (result) {
         LLOGE("ip4_dhcp_run error %d", result);
         return 0;
     }
     if (!tx_msg_buf.Pos) {
+        if (DHCP_STATE_CHECK == dhcp->state) {
+            goto on_check;
+        }
         return 0; // 没有数据需要发送
     }
     // 通过UDP发出来
@@ -76,13 +86,16 @@ static int ulwip_dhcp_client_run(ulwip_ctx_t* ctx, char* rxbuff, size_t len) {
         data += q->len;
     }
     data = p->payload;
-    // LLOGI("dhcp payload len %d %02X%02X%02X%02X", p->tot_len, data[0], data[1], data[2], data[3]);
+    LLOGI("dhcp payload len %d %02X%02X%02X%02X", p->tot_len, data[0], data[1], data[2], data[3]);
     udp_sendto_if(ctx->dhcp_pcb, p, IP_ADDR_BROADCAST, 67, netif);
     pbuf_free(p);
     return 0;
 }
 
 static int ulwip_dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) {
+    if (addr == NULL || port != 67 || pcb == NULL) {
+        return 0;
+    }
     LLOGD("收到DHCP数据包(len=%d)", p->tot_len);
     ulwip_ctx_t *ctx = (ulwip_ctx_t *)arg;
     char* ptr = luat_heap_malloc(p->tot_len);
@@ -147,5 +160,7 @@ void ulwip_dhcp_client_start(ulwip_ctx_t *ctx) {
 void ulwip_dhcp_client_stop(ulwip_ctx_t *ctx) {
     if (luat_rtos_timer_is_active(ctx->dhcp_timer)) {
         luat_rtos_timer_stop(ctx->dhcp_timer);
+        ctx->dhcp_client->state = DHCP_STATE_DISCOVER;
+        ctx->dhcp_client->discover_cnt = 0;
     }
 }