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

add: netdrv,添加一个调试开关

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

+ 18 - 0
components/network/netdrv/binding/luat_lib_netdrv.c

@@ -299,6 +299,23 @@ static int l_netdrv_ctrl(lua_State *L) {
     return 2;
 }
 
+/*
+设置调试信息输出
+@api netdrv.debug(id, enable)
+@int 网络适配器编号, 例如 socket.LWIP_ETH, 如果传0就是全局调试开关
+@boolean 是否开启调试信息输出
+@return boolean 成功与否
+@usage
+-- 打开netdrv全局调试开关
+netdrv.debug(0, true)
+*/
+static int l_netdrv_debug(lua_State *L) {
+    int id = luaL_checkinteger(L, 1);
+    int enable = lua_toboolean(L, 2);
+    luat_netdrv_debug_set(id, enable);
+    return 0;
+}
+
 
 #include "rotable2.h"
 static const rotable_Reg_t reg_netdrv[] =
@@ -312,6 +329,7 @@ static const rotable_Reg_t reg_netdrv[] =
     { "ready",          ROREG_FUNC(l_netdrv_ready)},
 
     { "ctrl",           ROREG_FUNC(l_netdrv_ctrl)},
+    { "debug",          ROREG_FUNC(l_netdrv_debug)},
 
     //@const CH390 number 南京沁恒CH390系列,支持CH390D/CH390H, SPI通信
     { "CH390",          ROREG_INT(1)},

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

@@ -10,6 +10,7 @@ typedef int (*luat_netdrv_bootup_cb)(struct luat_netdrv* drv, void* userdata);
 typedef int (*luat_netdrv_ready_cb)(struct luat_netdrv* drv, void* userdata);
 typedef int (*luat_netdrv_dhcp_set)(struct luat_netdrv* drv, void* userdata, int enable);
 typedef int (*luat_netdrv_ctrl_cb)(struct luat_netdrv* drv, void* userdata, int cmd, void* param);
+typedef int (*luat_netdrv_debug_cb)(struct luat_netdrv* drv, void* userdata, int enable);
 
 #define MACFMT "%02X%02X%02X%02X%02X%02X"
 #define MAC_ARG(x) ((uint8_t*)(x))[0],((uint8_t*)(x))[1],((uint8_t*)(x))[2],((uint8_t*)(x))[3],((uint8_t*)(x))[4],((uint8_t*)(x))[5]
@@ -64,6 +65,7 @@ typedef struct luat_netdrv {
     void* userdata;
     luat_netdrv_ctrl_cb ctrl;
     uint8_t gw_mac[6];
+    luat_netdrv_debug_cb debug;
 }luat_netdrv_t;
 
 luat_netdrv_t* luat_netdrv_setup(luat_netdrv_conf_t *conf);
@@ -106,4 +108,8 @@ int luat_netdrv_netif_input_proxy(struct netif * netif, uint8_t* buff, uint16_t
 
 void luat_netdrv_print_tm(const char * tag);
 
+void luat_netdrv_debug_set(int id, int enable);
+
+extern uint32_t g_netdrv_debug_enable;
+
 #endif

+ 24 - 1
components/network/netdrv/src/luat_netdrv.c

@@ -21,6 +21,8 @@
 
 static luat_netdrv_t* drvs[NW_ADAPTER_QTY];
 
+uint32_t g_netdrv_debug_enable;
+
 luat_netdrv_t* luat_netdrv_ch390h_setup(luat_netdrv_conf_t *conf);
 luat_netdrv_t* luat_netdrv_uart_setup(luat_netdrv_conf_t *conf);
 luat_netdrv_t* luat_netdrv_whale_setup(luat_netdrv_conf_t *conf);
@@ -202,7 +204,9 @@ void luat_netdrv_netif_input(void* args) {
         return;
     }
     pbuf_take(p, ptr->buff, ptr->len);
-    // luat_airlink_hexdump("收到IP数据,注入到netif", ptr->buff, ptr->len);
+    if (g_netdrv_debug_enable) {
+        luat_netdrv_print_pkg("收到IP数据,注入到netif", ptr->buff, ptr->len);
+    }
     int ret = ptr->netif->input(p, ptr->netif);
     if (ret) {
         LLOGW("netif->input ret %d", ret);
@@ -242,3 +246,22 @@ void luat_netdrv_print_tm(const char * tag) {
     uint64_t t_us = tnow / luat_mcu_us_period();
     LLOGI("tag %s time %lld", tag, t_us);
 }
+
+void luat_netdrv_debug_set(int id, int enable) {
+    if (id == 0) {
+        g_netdrv_debug_enable = enable;
+        LLOGD("debug is %d now", enable);
+    }
+    else if (id >= NW_ADAPTER_INDEX_LWIP_GPRS && id < NW_ADAPTER_INDEX_LWIP_NETIF_QTY) {
+        luat_netdrv_t* drv = luat_netdrv_get(id);
+        if (drv && drv->debug) {
+            drv->debug(drv, drv->userdata, enable);
+        }
+        else {
+            LLOGW("netdrv %d not support debug", id);
+        }
+    }
+    else {
+        LLOGW("netdrv %d not support debug", id);
+    }
+}

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

@@ -47,7 +47,9 @@ static err_t netif_output(struct netif *netif, struct pbuf *p) {
         return ERR_MEM;
     }
     pbuf_copy_partial(p, buff, p->tot_len, 0);
-    // luat_airlink_hexdump("准备上行给硬件协议栈的IP数据", buff, p->tot_len);
+    if (g_netdrv_debug_enable) {
+        luat_airlink_hexdump("上行给硬件", buff, p->tot_len);
+    }
     // TODO 这里应该根据userdata, 也就是whale上下文, 转发可配置的出口
     luat_airlink_queue_send_ippkg(netdrv->id, buff, p->tot_len);
     luat_heap_opt_free(LUAT_HEAP_PSRAM, buff);
@@ -128,12 +130,10 @@ static err_t luat_netif_init(struct netif *netif) {
     }
     #if ENABLE_PSIF
     netif->primary_ipv4_cid = LWIP_PS_INVALID_CID;
+    netif->primary_ipv6_cid = LWIP_PS_INVALID_CID;
     #endif
     #if LWIP_IPV6
     netif->output_ip6 = netif_output_ip6;
-    #if ENABLE_PSIF
-    netif->primary_ipv6_cid = LWIP_PS_INVALID_CID;
-    #endif
     #endif
     return 0;
 }