فهرست منبع

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

# Conflicts:
#	components/lcd/luat_lcd.h
#	components/lcd/luat_lib_lcd.c
alienwalker 1 سال پیش
والد
کامیت
ef9e5d3d81

+ 1 - 1
.github/workflows/air780.yml

@@ -47,7 +47,7 @@ jobs:
         mkdir ../LuatOS/luatosfw
         mv out/luatos/* ../LuatOS/luatosfw
     - name: Upload artifact
-      uses: actions/upload-artifact@v1
+      uses: actions/upload-artifact@v4
       with:
         name: air780
         path: luatosfw

+ 2 - 49
components/iconv/iconv.c

@@ -1,20 +1,3 @@
-/**************************************************************************
- *              Copyright (C), AirM2M Tech. Co., Ltd.
- *
- * Name:    iconv.c
- * Author:  liweiqiang
- * Version: V0.1
- * Date:    2013/7/15
- *
- * Description:
- *          字符编码转换
- **************************************************************************/
- /*
-@module  iconv
-@summary 字符编码转换
-@version V0.1
-@data    2021年12月13日
-*/
 
 #include <string.h>
 #include "iconv.h"
@@ -43,16 +26,6 @@ static const builtin_iconv_map iconv_map[] =
 /*-\NEW\liweiqiang\2013.7.19\增加utf8<->ucs2,ucs2be编码转换*/
 };
 
-/*
-打开相应字符编码转换函数
-@function iconv.open(tocode, fromcode) 
-@string tocode$目标编码格式$gb2312/ucs2/ucs2be/utf8
-@string fromcode$源编码格式$gb2312/ucs2/ucs2be/utf8
-@return table$cd$编码转换函数的转换句柄$ 
-@usage
---unicode大端编码 转化为 utf8编码
-local cd = iconv.open("utf8", "ucs2be")
-*/
 iconv_t iconv_open (const char * to_code, const char * from_code)
 {
     size_t i;
@@ -69,18 +42,7 @@ iconv_t iconv_open (const char * to_code, const char * from_code)
     return (iconv_t)-1;
 }
 
-/*
-字符编码转换
-@function cd:iconv(inbuf) 
-@string inbuf$输入字符串$例如:ucs2s 
-@return number$result$返回编码转换后的结果$0成功,-1失败
-@usage
---unicode大端编码 转化为 utf8编码
-function ucs2beToUtf8(ucs2s)
-    local cd = iconv.open("utf8", "ucs2be")
-    return cd:iconv(ucs2s)
-end
-*/
+
 size_t iconv_convert (iconv_t __cd, char ** __inbuf, size_t * __inbytesleft, char ** __outbuf, size_t * __outbytesleft)
 {
     builtin_iconv_map *_map_cd = (builtin_iconv_map *)__cd;
@@ -95,16 +57,7 @@ size_t iconv_convert (iconv_t __cd, char ** __inbuf, size_t * __inbytesleft, cha
     return _map_cd->fct(__inbuf, __inbytesleft, __outbuf, __outbytesleft);
 }
 
-/*
-关闭字符编码转换
-@function iconv.close(cd) 
-@string cd$iconv.open返回的句柄$ 
-@return  
-@usage
---关闭字符编码转换
-local cd = iconv.open("utf8", "ucs2be")
-iconv.close(cd)
-*/
+
 int iconv_close (iconv_t cd)
 {
     return 0;

+ 14 - 4
components/iconv/luat_lib_iconv.c

@@ -99,7 +99,7 @@ static iconv_t get_iconv_t(lua_State *L, int i) {
 @return userdata 编码转换函数的转换句柄,若不存在会返回nil
 @usage
 --unicode大端编码 转化为 utf8编码
-local iconv = iconv.open("utf8", "ucs2be")
+local ic = iconv.open("utf8", "ucs2be")
 */
 static int Liconv_open(lua_State *L) {
     const char *tocode = luaL_checkstring(L, 1);
@@ -114,14 +114,14 @@ static int Liconv_open(lua_State *L) {
 
 /*
 字符编码转换
-@api iconv:iconv(inbuf)
+@api ic:iconv(inbuf)
 @string 释义:待转换字符串
 @return number 释义:返回编码转换后的结果<br>取值:0成功,-1失败
 @usage
 --unicode大端编码 转化为 utf8编码
 function ucs2beToUtf8(ucs2s)
-    local iconv = iconv.open("utf8", "ucs2be")
-    return iconv:iconv(ucs2s)
+    local ic = iconv.open("utf8", "ucs2be")
+    return ic:iconv(ucs2s)
 end
 */
 static int Liconv(lua_State *L) {
@@ -193,6 +193,16 @@ static int Liconvlist(lua_State *L) {
 
 #endif
 
+/*
+关闭字符编码转换
+@api iconv.close(cd) 
+@userdata iconv.open返回的句柄
+@return bool 成功返回true,否则返回false
+@usage
+--关闭字符编码转换
+local cd = iconv.open("utf8", "ucs2be")
+iconv.close(cd)
+*/
 static int Liconv_close(lua_State *L) {
     iconv_t cd = get_iconv_t(L, 1);
     if (iconv_close(cd) == 0)

+ 0 - 8
components/iotauth/luat_iotauth.c

@@ -1,12 +1,4 @@
 
-/*
-@module  iotauth
-@summary IoT鉴权库, 用于生成各种云平台的参数
-@version core V0007
-@date    2022.08.06
-@demo iotauth
-@tag LUAT_USE_IOTAUTH
-*/
 #include "luat_base.h"
 #include "luat_crypto.h"
 #include "luat_mem.h"

+ 1 - 0
components/lcd/luat_lcd.h

@@ -127,6 +127,7 @@ extern luat_lcd_opts_t lcd_opts_st7735v;
 extern luat_lcd_opts_t lcd_opts_st7789;
 extern luat_lcd_opts_t lcd_opts_st7796;
 extern luat_lcd_opts_t lcd_opts_nv3037;
+extern luat_lcd_opts_t lcd_opts_h050iwv;
 extern luat_lcd_opts_t lcd_opts_jd9261t_inited;
 
 static inline luat_color_t color_swap(luat_color_t color) {

+ 14 - 0
components/lcd/luat_lcd_h050iwv.c

@@ -0,0 +1,14 @@
+#include "luat_base.h"
+#include "luat_lcd.h"
+#include "luat_gpio.h"
+#include "luat_spi.h"
+#include "luat_mem.h"
+#include "luat_rtos.h"
+
+#define LUAT_LOG_TAG "h050iwv"
+#include "luat_log.h"
+
+luat_lcd_opts_t lcd_opts_h050iwv = {
+    .name = "h050iwv",
+};
+

+ 35 - 24
components/lcd/luat_lib_lcd.c

@@ -35,10 +35,18 @@ typedef struct lcd_reg {
   const luat_lcd_opts_t *lcd_opts;
 }lcd_reg_t;
 
-luat_lcd_opts_t lcd_opts_h050iwv = {
-    .name = "h050iwv",
+luat_lcd_opts_t lcd_opts_nv3052c = {
+    .name = "nv3052c",
+};
+luat_lcd_opts_t lcd_opts_hx8282 = {
+    .name = "hx8282",
+};
+luat_lcd_opts_t lcd_opts_st7701s = {
+    .name = "st7701s",
+};
+luat_lcd_opts_t lcd_opts_st7701sn = {
+    .name = "st7701sn",
 };
-
 static const lcd_reg_t lcd_regs[] = {
   {"custom",  &lcd_opts_custom},   //0 固定为第零个
   {"st7735",  &lcd_opts_st7735},
@@ -55,6 +63,10 @@ static const lcd_reg_t lcd_regs[] = {
   {"nv3037",  &lcd_opts_nv3037},
   {"h050iwv", &lcd_opts_h050iwv},
   {"jd9261t_inited", &lcd_opts_jd9261t_inited},
+  {"nv3052c", &lcd_opts_nv3052c},
+  {"hx8282",  &lcd_opts_hx8282},
+  {"st7701s",  &lcd_opts_st7701s},
+  {"st7701sn",  &lcd_opts_st7701sn},
   {"", NULL} // 最后一个必须是空字符串
 };
 
@@ -1489,28 +1501,27 @@ static int l_lcd_showimage(lua_State *L){
 lcd.flush()
 */
 static int l_lcd_flush(lua_State* L) {
-  luat_lcd_conf_t * conf = NULL;
-  if (lua_gettop(L) == 1) {
-    conf = lua_touserdata(L, 1);
-  }
-  else {
-    conf = default_conf;
-  }
-  if (conf == NULL) {
-    //LLOGW("lcd not init");
-    return 0;
-  }
-  if (conf->buff == NULL) {
-    //LLOGW("lcd without buff, not support flush");
-    return 0;
-  }
-  if (conf->auto_flush) {
-    //LLOGI("lcd auto flush is enable, no need for flush");
+    luat_lcd_conf_t * conf = NULL;
+    if (lua_gettop(L) == 1) {
+        conf = lua_touserdata(L, 1);
+    }else {
+        conf = default_conf;
+    }
+    if (conf == NULL) {
+        //LLOGW("lcd not init");
+        return 0;
+    }
+    if (conf->buff == NULL) {
+        //LLOGW("lcd without buff, not support flush");
+        return 0;
+    }
+    if (conf->auto_flush) {
+        //LLOGI("lcd auto flush is enable, no need for flush");
+        return 0;
+    }
+    luat_lcd_flush(conf);
+    lua_pushboolean(L, 1);
     return 0;
-  }
-  luat_lcd_flush(conf);
-  lua_pushboolean(L, 1);
-  return 0;
 }
 
 /*

+ 0 - 8
components/network/libftp/luat_lib_ftp.c

@@ -1,11 +1,3 @@
-/*
-@module  ftp
-@summary ftp 客户端
-@version 1.0
-@date    2022.09.05
-@demo    ftp
-@tag LUAT_USE_FTP
-*/
 
 #include "luat_base.h"
 

+ 1 - 1
components/network/libhttp/luat_http.h

@@ -37,7 +37,7 @@ typedef struct luat_zbuff {
 
 #define HTTP_CALLBACK 		(1)
 #define HTTP_RE_REQUEST_MAX (3)
-#define HTTP_TIMEOUT 		(10*60*1000) // 10分钟
+#define HTTP_TIMEOUT 		(5*60*1000) // 10分钟
 
 /**
  * @defgroup luatos_HTTP  HTTP(S)相关接口

+ 16 - 2
components/network/libhttp/luat_lib_http.c

@@ -134,7 +134,7 @@ static int l_http_request(lua_State *L) {
 	}
 	memset(http_ctrl, 0, sizeof(luat_http_ctrl_t));
 
-	http_ctrl->timeout = HTTP_TIMEOUT;
+	http_ctrl->timeout = 0;
 	int use_ipv6 = 0;
 	int is_debug = 0;
 
@@ -149,7 +149,7 @@ static int l_http_request(lua_State *L) {
 
 		lua_pushstring(L, "timeout");
 		if (LUA_TNUMBER == lua_gettable(L, 5)) {
-			http_ctrl->timeout = luaL_optinteger(L, -1, HTTP_TIMEOUT);
+			http_ctrl->timeout = luaL_optinteger(L, -1, 0);
 		}
 		lua_pop(L, 1);
 
@@ -231,6 +231,20 @@ static int l_http_request(lua_State *L) {
 		LLOGE("netc create fail");
 		goto error;
 	}
+	#ifdef LUAT_USE_FOTA
+	if (http_ctrl->timeout < 1000 && http_ctrl->isfota) {
+		http_ctrl->timeout = HTTP_TIMEOUT;
+	}
+	#endif
+	if (http_ctrl->timeout < 1000) {
+		if (http_ctrl->is_download) {
+			http_ctrl->timeout = HTTP_TIMEOUT;
+		}
+		else {
+			http_ctrl->timeout = 60*1000;
+		}
+	}
+	LLOGD("http action timeout %dms", http_ctrl->timeout);
 
     luat_http_client_init(http_ctrl, use_ipv6);
 	http_ctrl->netc->is_debug = (uint8_t)is_debug;

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

@@ -6,7 +6,13 @@
 #include "lwip/pbuf.h"
 #include "luat_ulwip.h"
 
-#define CH390H_MAX_TX_NUM (16)
+#define CH390H_MAX_TX_NUM (128)
+
+typedef struct luat_ch390h_cstring
+{
+    uint16_t len;
+    uint8_t buff[4];
+}luat_ch390h_cstring_t;
 
 typedef struct ch390h
 {
@@ -22,7 +28,7 @@ typedef struct ch390h
     luat_netdrv_t* netdrv;
     uint8_t rxbuff[1600];
     uint8_t txbuff[1600];
-    struct pbuf* txqueue[CH390H_MAX_TX_NUM];
+    luat_ch390h_cstring_t* txqueue[CH390H_MAX_TX_NUM];
 }ch390h_t;
 
 

+ 95 - 80
components/network/netdrv/src/ch390h_task.c

@@ -13,14 +13,13 @@
 #include "lwip/sys.h"
 #include "lwip/tcpip.h"
 #include "lwip/pbuf.h"
+#include "luat_mem.h"
 
 #include "luat_rtos.h"
 
 #define LUAT_LOG_TAG "ch390x"
 #include "luat_log.h"
 
-
-
 typedef struct pkg_msg
 {
     struct netif * netif;
@@ -34,7 +33,7 @@ extern ch390h_t* ch390h_drvs[MAX_CH390H_NUM];
 
 static luat_rtos_task_handle ch390h_task_handle;
 
-static int is_waiting;
+// static uint32_t remain_tx_size;
 
 static int ch390h_bootup(ch390h_t* ch) {
     // 初始化SPI设备, 由外部代码初始化, 因为不同bsp的速度不一样, 就不走固定值了
@@ -57,25 +56,31 @@ static int ch390h_bootup(ch390h_t* ch) {
 
 static void ch390h_dataout(void* userdata, uint8_t* buff, uint16_t len) {
     ch390h_t* ch = (ch390h_t*)userdata;
-    struct pbuf *p = NULL;
-    p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
-    if (p == NULL) {
-        LLOGE("内存不足, 无法传递pbuf");
+    luat_ch390h_cstring_t* cs = luat_heap_opt_malloc(LUAT_HEAP_PSRAM, sizeof(luat_ch390h_cstring_t) + len - 4);
+    if (cs == NULL) {
+        LLOGE("内存不足, 无法传递buff到驱动task");
         return;
     }
-    pbuf_take(p, buff, len);
-    for (size_t j = 0; j < CH390H_MAX_TX_NUM; j++)
-    {
-        if (ch->txqueue[j] == NULL) {
-            ch->txqueue[j] = p;
-            // LLOGD("找到空位了 %d", j);
-            if (is_waiting) {
-                luat_rtos_event_send(ch390h_task_handle, 0, 0, 0, 0, 0);
-            }
-            return;
-        }
+    cs->len = len;
+    memcpy(cs->buff, buff, len);
+    // remain_tx_size += len;
+    // LLOGD("数据传递到驱动task %p %p %d remain %ld", ch, cs, len, remain_tx_size);
+    luat_rtos_event_send(ch390h_task_handle, 1, (uint32_t)ch, (uint32_t)cs, 0, 0);
+    return;
+}
+
+static void ch390h_dataout_pbuf(ch390h_t* ch, struct pbuf* p) {
+    luat_ch390h_cstring_t* cs = luat_heap_opt_malloc(LUAT_HEAP_PSRAM, sizeof(luat_ch390h_cstring_t) + p->tot_len - 4);
+    if (cs == NULL) {
+        LLOGE("内存不足, 无法传递buff到驱动task");
+        return;
     }
-    pbuf_free(p);
+    cs->len = p->tot_len;
+    pbuf_copy_partial(p, cs->buff, p->tot_len, 0);
+    // LLOGD("数据传递到驱动task %p %p %d", ch, cs, cs->len);
+    luat_rtos_event_send(ch390h_task_handle, 1, (uint32_t)ch, (uint32_t)cs, 0, 0);
+    // remain_tx_size += p->tot_len;
+    // LLOGD("数据传递到驱动task %p %p %d remain %ld", ch, cs, p->tot_len, remain_tx_size);
     return;
 }
 
@@ -83,7 +88,6 @@ static void ch390h_dataout(void* userdata, uint8_t* buff, uint16_t len) {
 static err_t netif_output(struct netif *netif, struct pbuf *p) {
     // LLOGD("lwip待发送数据 %p %d", p, p->tot_len);
     ch390h_t* ch = NULL;
-    struct pbuf *p2 = NULL;
 
     for (size_t i = 0; i < MAX_CH390H_NUM; i++)
     {
@@ -94,24 +98,8 @@ static err_t netif_output(struct netif *netif, struct pbuf *p) {
         if (ch->netif != netif) {
             continue;
         }
-        for (size_t j = 0; j < CH390H_MAX_TX_NUM; j++)
-        {
-            if (ch->txqueue[j] == NULL) {
-                p2 = pbuf_alloc(PBUF_TRANSPORT, p->tot_len, PBUF_RAM);
-                if (p2 == NULL) {
-                    LLOGE("内存不足, 无法传递pbuf");
-                    return 0;
-                }
-                pbuf_copy(p2, p);
-                // TODO 改成消息传送
-                ch->txqueue[j] = p2;
-                // LLOGD("找到空位了 %d", j);
-                if (is_waiting) {
-                    luat_rtos_event_send(ch390h_task_handle, 0, 0, 0, 0, 0);
-                }
-                return 0;
-            }
-        }
+        ch390h_dataout_pbuf(ch, p);
+        break;
     }
     return 0;
 }
@@ -169,7 +157,7 @@ static int check_vid_pid(ch390h_t* ch) {
 }
 
 
-static int task_loop_one(ch390h_t* ch) {
+static int task_loop_one(ch390h_t* ch, luat_ch390h_cstring_t* cs) {
     uint8_t buff[16] = {0};
     int ret = 0;
     uint16_t len = 0;
@@ -237,6 +225,11 @@ static int task_loop_one(ch390h_t* ch) {
         }
     }
 
+    if (cs) {
+        // LLOGD("数据写入 %p %d", cs->buff, cs->len);
+        luat_ch390h_write_pkg(ch, cs->buff, cs->len);
+    }
+
     // 有没有数据待读取
     if (NSR & 0x01) {
         ret = luat_ch390h_read_pkg(ch, ch->rxbuff, &len);
@@ -258,24 +251,27 @@ static int task_loop_one(ch390h_t* ch) {
             // 先经过netdrv过滤器
             // LLOGD("ETH数据包 " MACFMT " " MACFMT " %02X%02X", MAC_ARG(ch->rxbuff), MAC_ARG(ch->rxbuff + 6), ((uint16_t)ch->rxbuff[6]) + (((uint16_t)ch->rxbuff[7])));
             ret = luat_netdrv_napt_pkg_input(ch->adapter_id, ch->rxbuff, len - 4);
-            if (ret == 0) {
-
+            // LLOGD("napt ret %d", ret);
+            if (ret != 0) {
+                // 不需要输入到LWIP了
+                // LLOGD("napt说不需要注入lwip了");
             }
-            
-            // 如果返回值是0, 那就是继续处理, 输入到netif
-            pkg_msg_t* ptr = luat_heap_malloc(sizeof(pkg_msg_t) + len - 4);
-            if (ptr == NULL) {
-                LLOGE("收到rx数据,但内存已满, 无法处理只能抛弃 %d", len - 4);
-                return 1; // 需要处理下一个包
-            }
-            memcpy(ptr->buff, ch->rxbuff, len - 4);
-            ptr->netif = ch->netif;
-            ptr->len = len - 4;
-            ret = tcpip_callback(netdrv_netif_input, ptr);
-            if (ret) {
-                luat_heap_free(ptr);
-                LLOGE("tcpip_callback 返回错误!!! ret %d", ret);
-                return 1;
+            else {
+                // 如果返回值是0, 那就是继续处理, 输入到netif
+                pkg_msg_t* ptr = luat_heap_malloc(sizeof(pkg_msg_t) + len - 4);
+                if (ptr == NULL) {
+                    LLOGE("收到rx数据,但内存已满, 无法处理只能抛弃 %d", len - 4);
+                    return 1; // 需要处理下一个包
+                }
+                memcpy(ptr->buff, ch->rxbuff, len - 4);
+                ptr->netif = ch->netif;
+                ptr->len = len - 4;
+                ret = tcpip_callback(netdrv_netif_input, ptr);
+                if (ret) {
+                    luat_heap_free(ptr);
+                    LLOGE("tcpip_callback 返回错误!!! ret %d", ret);
+                    return 1;
+                }
             }
         }
         // 很好, RX数据处理完成了
@@ -285,38 +281,36 @@ static int task_loop_one(ch390h_t* ch) {
     }
 
     // 那有没有需要发送的数据呢?
-    struct pbuf* p = NULL;
-    int has_tx = 0;
-    for (size_t i = 0; i < CH390H_MAX_TX_NUM; i++)
-    {
-        p = ch->txqueue[i];
-        if (p == NULL) {
-            continue;
-        }
-        // LLOGD("txqueue收到数据包 %p", p);
-        len = p->tot_len;
-        pbuf_copy_partial(p, ch->txbuff, len, 0);
-        pbuf_free(p);
-        ch->txqueue[i] = NULL;
-        luat_ch390h_write_pkg(ch, ch->txbuff, len);
-        has_tx = 1;
-    }
+    // luat_ch390h_cstring_t* cs = NULL;
+    // int has_tx = 0;
+    // for (size_t i = 0; i < CH390H_MAX_TX_NUM; i++)
+    // {
+    //     cs = ch->txqueue[i];
+    //     if (cs == NULL) {
+    //         continue;
+    //     }
+    //     memcpy(ch->txbuff, cs->buff, cs->len);
+    //     ch->txqueue[i] = NULL;
+    //     luat_ch390h_write_pkg(ch, ch->txbuff, cs->len);
+    //     luat_heap_opt_free(LUAT_HEAP_PSRAM, cs);
+    //     has_tx = 1;
+    // }
     
     // 这一轮处理完成了
     // 如果rx有数据, 那就不要等待, 立即开始下一轮
-    if (NSR & 0x01 || has_tx) {
+    if (NSR & 0x01 || cs) {
         return 1;
     }
 
     return 0;
 }
 
-static int task_loop() {
+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) {
-            ret += task_loop_one(ch390h_drvs[i]);
+            ret += task_loop_one(ch390h_drvs[i], ch == ch390h_drvs[i] ? cs : NULL);
         }
     }
     return ret;
@@ -324,14 +318,35 @@ static int task_loop() {
 
 static void ch390_task_main(void* args) {
     (void)args;
-    luat_event_t evt;
+    luat_event_t evt = {0};
+    int ret = 0;
+    ch390h_t *ch = NULL;
+    luat_ch390h_cstring_t* cs = NULL;
     while (1) {
         // LLOGD("开始新的循环");
         // luat_rtos_task_sleep(10);
-        if (task_loop() == 0) {
-            is_waiting = 1;
-            luat_rtos_event_recv(ch390h_task_handle, 0, &evt, NULL, 10);
-            is_waiting = 0;
+        if (ret == 0) {
+            // is_waiting = 1;
+            ret = luat_rtos_event_recv(ch390h_task_handle, 0, &evt, NULL, 5);
+            // is_waiting = 0;
+            if (ret == 0) {
+                // 收到消息了
+                ch = (ch390h_t *)evt.param1;
+                cs = (luat_ch390h_cstring_t*)evt.param2;
+                // LLOGD("收到消息 %p %p", ch, cs);
+                ret = task_loop(ch, cs);
+                if (cs) {
+                    // remain_tx_size -= cs->len;
+                    luat_heap_opt_free(LUAT_HEAP_PSRAM, cs);
+                    cs = NULL;
+                }
+            }
+            else {
+                ret = task_loop(NULL, NULL);
+            }
+        }
+        else {
+            ret = task_loop(NULL, NULL);
         }
     }
 }

+ 6 - 6
components/network/netdrv/src/luat_netdrv_napt_icmp.c

@@ -14,10 +14,10 @@
 #include "luat_log.h"
 
 #define ICMP_MAP_SIZE (64)
-#define IP_MAP_SIZE (1024)
+#define ICMP_MAP_TIMEOUT (5000)
 
 /* napt icmp id range: 3000-65535 */
-#define NAPT_ICMP_ID_RANGE_START     0xBB8
+#define NAPT_ICMP_ID_RANGE_START     0x1BB8
 #define NAPT_ICMP_ID_RANGE_END       0xFFFF
 
 extern int luat_netdrv_gw_adapter_id;
@@ -129,16 +129,16 @@ int luat_napt_icmp_handle(napt_ctx_t* ctx) {
     else {
         // 内网, 尝试对外网的请求吗?
         if (ip_hdr->dest.addr == ip_addr_get_ip4_u32(&ctx->net->netif->ip_addr)) {
-            return 1; // 对网关的ICMP/PING请求, 交给LWIP处理
+            return 0; // 对网关的ICMP/PING请求, 交给LWIP处理
         }
         // 寻找一个空位
         uint64_t tnow = luat_mcu_tick64_ms();
         for (size_t i = 0; i < ICMP_MAP_SIZE; i++)
         {
-            if (icmps[i].is_vaild && (tnow - icmps[i].tm_ms) < 5000) {
+            if (icmps[i].is_vaild && (tnow - icmps[i].tm_ms) < ICMP_MAP_TIMEOUT) {
                 continue;
             }
-            if (icmps[i].is_vaild && (tnow - icmps[i].tm_ms) > 5000) {
+            if (icmps[i].is_vaild && (tnow - icmps[i].tm_ms) > ICMP_MAP_TIMEOUT) {
                 icmps[i].is_vaild = 0;
             }
             // 有空位, 马上处理
@@ -181,7 +181,7 @@ int luat_napt_icmp_handle(napt_ctx_t* ctx) {
             else {
                 LLOGD("ICMP改写完成, 但GW不支持dataout回调?!!");
             }
-            return 0;
+            return 1;
         }
         LLOGD("没有ICMP空位了");
     }

+ 33 - 19
components/network/netdrv/src/luat_netdrv_napt_tcp.c

@@ -10,6 +10,7 @@
 #include "lwip/tcp.h"
 #include "lwip/prot/tcp.h"
 #include "luat_mcu.h"
+#include "lwip/ip_addr.h"
 
 #define LUAT_LOG_TAG "netdrv.napt.tcp"
 #include "luat_log.h"
@@ -59,7 +60,7 @@ again:
 static void print_item(const char* tag, luat_netdrv_napt_tcpudp_t* it) {
     char buff[16] = {0};
     char buff2[16] = {0};
-    struct ip_addr ip;
+    ip_addr_t ip;
     
     ip_addr_set_ip4_u32(&ip, it->inet_ip);
     ipaddr_ntoa_r(&ip, buff, 16);
@@ -87,30 +88,32 @@ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
     uint64_t tnow = luat_mcu_tick64_ms();
     if (ctx->is_wnet) {
         // 这是从外网到内网的TCP包
-        LLOGD("wnet.search dst port %d", ntohs(tcp_hdr->dest));
+        // LLOGD("wnet.search dst port %d", ntohs(tcp_hdr->dest));
         for (size_t i = 0; i < TCP_MAP_SIZE; i++)
         {
             it = &tcps[i];
             if (it->is_vaild == 0) {
                 continue;
             }
-            if (it->is_vaild && (tnow - it->tm_ms) > TCP_MAP_TIMEOUT) {
+            tnow = luat_mcu_tick64_ms();
+            if (it->is_vaild && tnow > it->tm_ms &&  (tnow - it->tm_ms) > TCP_MAP_TIMEOUT) {
+                LLOGD("映射关系超时了!!设置为无效 %lld %lld %lld", tnow, it->tm_ms, tnow - it->tm_ms);
                 it->is_vaild = 0;
                 continue;
             }
-            print_item("wnet.search item", it);
+            // print_item("wnet.search item", it);
             // 校验远程IP与预期IP是否相同
             if (ip_hdr->src.addr != it->wnet_ip) {
-                LLOGD("IP地址不匹配,下一条");
+                // LLOGD("IP地址不匹配,下一条");
                 continue;
             }
             // 下行的目标端口, 与本地端口, 是否一直
             if (tcp_hdr->dest != tcps[i].wnet_local_port) {
-                LLOGD("port不匹配,下一条");
+                // LLOGD("port不匹配,下一条");
                 continue;
             }
             // 找到映射关系了!!!
-            LLOGD("TCP port %u -> %d", ntohs(tcp_hdr->dest), ntohs(tcps[i].inet_port));
+            // LLOGD("TCP port %u -> %d", ntohs(tcp_hdr->dest), ntohs(tcps[i].inet_port));
             tcps[i].tm_ms = tnow;
             // 修改目标端口
             tcp_hdr->dest = tcps[i].inet_port;
@@ -121,14 +124,14 @@ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
             ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
 
             // 重新计算icmp的checksum
-            if (tcp_hdr->chksum) {
+            // if (tcp_hdr->chksum) {
                 tcp_hdr->chksum = 0;
                 tcp_hdr->chksum = alg_tcpudphdr_chksum(ip_hdr->src.addr,
                                                ip_hdr->dest.addr,
                                                IP_PROTO_TCP,
                                                (u16 *)tcp_hdr,
                                                ntohs(ip_hdr->_len) - iphdr_len);
-            }
+            // }
 
             // 如果是ETH包, 那还需要修改源MAC和目标MAC
             if (ctx->eth) {
@@ -167,42 +170,53 @@ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
     else {
         // 内网, 尝试对外网的请求吗?
         if (ip_hdr->dest.addr == ip_addr_get_ip4_u32(&ctx->net->netif->ip_addr)) {
-            return 1; // 对网关的TCP请求, 交给LWIP处理
+            return 0; // 对网关的TCP请求, 交给LWIP处理
         }
         // 第一轮循环, 是否有已知映射
-        LLOGD("inet.search src port %d -> %d", ntohs(tcp_hdr->src), ntohs(tcp_hdr->dest));
+        // LLOGD("inet.search src port %d -> %d", ntohs(tcp_hdr->src), ntohs(tcp_hdr->dest));
         for (size_t i = 0; i < TCP_MAP_SIZE; i++)
         {
             it = &tcps[i];
             if (it->is_vaild == 0) {
                 continue;
             }
-            if ((tnow - it->tm_ms) > TCP_MAP_TIMEOUT) {
+            tnow = luat_mcu_tick64_ms();
+            if (tnow > it->tm_ms && (tnow - it->tm_ms) > TCP_MAP_TIMEOUT) {
+                LLOGD("映射关系超时了!!设置为无效 %lld %lld %lld", tnow, it->tm_ms, tnow - it->tm_ms);
                 it->is_vaild = 0;
                 it->tm_ms = 0;
                 continue;
             }
             
-            print_item("inet.search", it);
+            // print_item("inet.search", it);
             // 几个要素都要相同 源IP/源端口/目标IP/目标端口, 如果是MAC包, 源MAC也要相同
             if (it->inet_ip != ip_hdr->src.addr || it->inet_port != tcp_hdr->src) {
-                LLOGD("源ip/port不匹配, 继续下一条");
+                // LLOGD("源ip/port不匹配, 继续下一条");
                 continue;
             }
             if (it->wnet_ip != ip_hdr->dest.addr || it->wnet_port != tcp_hdr->dest) {
-                LLOGD("目标ip/port不匹配, 继续下一条");
+                // LLOGD("目标ip/port不匹配, 继续下一条");
                 continue;
             }
             if (ctx->eth && memcmp(ctx->eth->src.addr, it->inet_mac, 6)) {
-                LLOGD("源MAC不匹配, 继续下一条");
+                // LLOGD("源MAC不匹配, 继续下一条");
                 continue;
             }
-            // 都相同, 那就是同一个映射了, 可以
+            // 都相同, 那就是同一个映射了, 可以
             it->tm_ms = tnow;
             it_map = it;
+            break;
         }
         // 寻找一个空位
         if (it_map == NULL) {
+            if ((TCPH_FLAGS(tcp_hdr) & (TCP_SYN|TCP_ACK)) == TCP_SYN && PP_NTOHS(tcp_hdr->src) >= 1024) {
+                // 允许新增映射
+            }
+            else {
+                LLOGI("非SYN包/源端口小于1024,且没有已知映射,不允许新增映射 %02X %d", TCPH_FLAGS(tcp_hdr), PP_NTOHS(tcp_hdr->src));
+                // TODO 应该返回RST?
+                return 0;
+            }
             for (size_t i = 0; i < TCP_MAP_SIZE; i++) {
                 it = &tcps[i];
                 if (it->is_vaild) {
@@ -236,14 +250,14 @@ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
         ip_hdr->_chksum = 0;
         ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
         // 4. 计算IP包的checksum
-        if (tcp_hdr->chksum) {
+        // if (tcp_hdr->chksum) {
             tcp_hdr->chksum = 0;
             tcp_hdr->chksum = alg_tcpudphdr_chksum(ip_hdr->src.addr,
                                                    ip_hdr->dest.addr,
                                                    IP_PROTO_TCP,
                                                    (u16 *)tcp_hdr,
                                                    ntohs(ip_hdr->_len) - iphdr_len);
-        }
+        // }
 
         // 发送出去
         if (gw && gw->dataout && gw->netif) {

+ 6 - 6
components/network/netdrv/src/luat_netdrv_napt_udp.c

@@ -140,7 +140,7 @@ int luat_napt_udp_handle(napt_ctx_t* ctx) {
     else {
         // 内网, 尝试对外网的请求吗?
         if (ip_hdr->dest.addr == ip_addr_get_ip4_u32(&ctx->net->netif->ip_addr)) {
-            return 1; // 对网关的UDP请求, 交给LWIP处理
+            return 0; // 对网关的UDP请求, 交给LWIP处理
         }
         // 寻找一个空位
         // 第一轮循环, 是否有已知映射
@@ -159,23 +159,23 @@ int luat_napt_udp_handle(napt_ctx_t* ctx) {
             }
             // 几个要素都要相同 源IP/源端口/目标IP/目标端口, 如果是MAC包, 源MAC也要相同
             if (it->inet_ip != ip_hdr->src.addr) {
-                LLOGD("源IP不匹配, 继续下一条");
+                // LLOGD("源IP不匹配, 继续下一条");
                 continue;
             }
             if (it->inet_port != udp_hdr->src) {
-                LLOGD("源port不匹配, 继续下一条");
+                // LLOGD("源port不匹配, 继续下一条");
                 continue;
             }
             if (it->wnet_ip != ip_hdr->dest.addr) {
-                LLOGD("目标IP不匹配, 继续下一条");
+                // LLOGD("目标IP不匹配, 继续下一条");
                 continue;
             }
             if (it->wnet_port != udp_hdr->dest) {
-                LLOGD("目标port不匹配, 继续下一条");
+                // LLOGD("目标port不匹配, 继续下一条");
                 continue;
             }
             if (ctx->eth && memcmp(ctx->eth->src.addr, it->inet_mac, 6)) {
-                LLOGD("源MAC不匹配, 继续下一条");
+                // LLOGD("源MAC不匹配, 继续下一条");
                 continue;
             }
             // 都相同, 那就是同一个映射了, 可以服用

+ 12 - 12
tools/make_doc_file.py

@@ -101,18 +101,18 @@ def make(path,modules,index_text):
         mdoc.write("# "+module["module"]+" - "+module["summary"]+"\n\n")
 
         #支持的芯片
-        mdoc.write(get_tags(module["tag"]))
-        mdoc.write("\n\n")
-
-        if len(module["url"]) > 0:
-            mdoc.write("```{note}\n本页文档由[这个文件]("+module["url"]+")自动生成。如有错误,请提交issue或帮忙修改后pr,谢谢!\n```\n\n")
-
-        if len(module["demo"]) > 0:
-            mdoc.write("```{tip}\n本库有专属demo,[点此链接查看"+module["module"]+"的demo例子]("+module["demo"]+")\n```\n")
-        if len(module["video"]) > 0:
-            mdoc.write("```{tip}\n本库还有视频教程,[点此链接查看]("+module["video"]+")\n```\n\n")
-        else:
-            mdoc.write("\n")
+        # mdoc.write(get_tags(module["tag"]))
+        # mdoc.write("\n\n")
+
+        # if len(module["url"]) > 0:
+        #     mdoc.write("```{note}\n本页文档由[这个文件]("+module["url"]+")自动生成。如有错误,请提交issue或帮忙修改后pr,谢谢!\n```\n\n")
+
+        # if len(module["demo"]) > 0:
+        #     mdoc.write("```{tip}\n本库有专属demo,[点此链接查看"+module["module"]+"的demo例子]("+module["demo"]+")\n```\n")
+        # if len(module["video"]) > 0:
+        #     mdoc.write("```{tip}\n本库还有视频教程,[点此链接查看]("+module["video"]+")\n```\n\n")
+        # else:
+        #     mdoc.write("\n")
 
         if len(module["usage"]) > 0:
             mdoc.write("**示例**\n\n")