slip通过lwip初始化
author孙春阳 <snickersun@163.com>
Sat, 23 Aug 2025 09:26:44 +0000 (17:26 +0800)
committer孙春阳 <snickersun@163.com>
Sat, 23 Aug 2025 09:26:44 +0000 (17:26 +0800)
code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/arch/sio.c
code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/include/lwip/lwipopts_default.h
code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/dns.c
code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/init.c
code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/include/lwip/sio.h
code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/netif/slipif.c
code/sdk/zhihui/4g/cat1Service/Service/api_shell.c
code/sdk/zhihui/4g/cat1Service/Service/cat1_server.c

index f0c6dd9..2108834 100644 (file)
@@ -73,6 +73,11 @@ static struct {
 /* SLIP UART state */
 static bool g_slip_uart_initialized = false;
 
+/* SIO write buffer configuration */
+#define SIO_WRITE_BUFFER_SIZE   1024
+static uint8_t g_sio_write_buffer[SIO_WRITE_BUFFER_SIZE];
+static uint32_t g_sio_write_buffer_pos = 0;
+
 extern void uart_slip_read_int_handler(const void *buffer, uint16_t length, bool error);
 
 /**
@@ -126,11 +131,13 @@ sio_fd_t sio_open(u8_t devnum)
 
 /**
  * Sends a single character to the serial device.
+ * Uses internal buffer to optimize UART write operations.
  *
  * @param c character to send
  * @param fd serial device handle
  *
- * @note This function will block until the character can be sent.
+ * @note This function buffers data and only writes to UART when buffer is full.
+ *       Use sio_send_flush() to force write buffered data.
  */
 void sio_send(u8_t c, sio_fd_t fd)
 {
@@ -140,7 +147,15 @@ void sio_send(u8_t c, sio_fd_t fd)
         return;
     }
     
-    uapi_uart_write(UART_BUS_0, &c, 1, 1000); /* 1 second timeout */
+    /* Add character to buffer */
+    g_sio_write_buffer[g_sio_write_buffer_pos] = c;
+    g_sio_write_buffer_pos++;
+    
+    /* Write to UART when buffer is full */
+    if (g_sio_write_buffer_pos >= SIO_WRITE_BUFFER_SIZE) {
+        uapi_uart_write(UART_BUS_0, g_sio_write_buffer, g_sio_write_buffer_pos, 1000);
+        g_sio_write_buffer_pos = 0; /* Reset buffer position */
+    }
 }
 
 /**
@@ -245,4 +260,21 @@ void sio_read_abort(sio_fd_t fd)
     
     /* Flush RX data to abort any pending read */
     uapi_uart_flush_rx_data(UART_BUS_0);
+}
+
+/**
+ * Flush the SIO write buffer to UART.
+ * Forces all buffered data to be written immediately.
+ */
+void sio_send_flush(void)
+{
+    if (!g_slip_uart_initialized) {
+        return;
+    }
+    
+    /* Write any remaining data in buffer to UART */
+    if (g_sio_write_buffer_pos > 0) {
+        uapi_uart_write(UART_BUS_0, g_sio_write_buffer, g_sio_write_buffer_pos, 1000);
+        g_sio_write_buffer_pos = 0; /* Reset buffer position */
+    }
 } 
\ No newline at end of file
index a75bcae..8d88c4e 100644 (file)
@@ -57,6 +57,12 @@ extern "C" {
 #endif
 #define LWIP_DEBUG              0
 #define LWIP_DBG_TYPES_ON       LWIP_DBG_OFF
+
+#ifndef LWIP_SLIP
+#define LWIP_SLIP 1
+#endif
+
+#if LWIP_SLIP
 /* SLIP UART configuration */
 // UART_BUS_0 = 0  UART_BUS_1 = 1
 #define SLIP_IFNUM              0  
@@ -71,6 +77,7 @@ extern "C" {
 #define SLIP_UART_STOP_BIT      UART_STOP_BIT_1
 #define SLIP_UART_FLOW_CTRL     UART_FLOW_CTRL_NONE
 #define SLIP_DEBUG              LWIP_DBG_ON
+#endif
 
 #define LWIP_HTTPC_HAVE_FILE_IO 1
 /*
@@ -419,10 +426,6 @@ extern "C" {
 #define LWIP_WND_SCALE    0
 #endif
 
-#ifndef LWIP_SNTP
-#define LWIP_SNTP               0
-#endif
-
 #ifndef LWIP_SOCK_FILTER
 #define LWIP_SOCK_FILTER        0
 #endif
@@ -1750,6 +1753,27 @@ TCP sender buffer space (pbufs). This must be at least = 2 *
 #define LWIP_SNTP                      1
 #endif
 
+#if LWIP_SNTP
+
+#define LWIP_DHCP_MAX_NTP_SERVERS       1 
+#define SNTP_SERVER_DNS                 1
+
+#if SNTP_SERVER_DNS
+#define SNTP_SERVER_ADDRESS "pool.ntp.org" //"pool.ntp.org"
+#endif
+
+// 三个服务器
+
+// #define LWIP_DHCP_MAX_NTP_SERVERS       3 
+// #define SNTP_SERVER_DNS                 1
+
+// #if SNTP_SERVER_DNS
+// #define SNTP_SERVER_ADDRESS "cn.pool.ntp.org" //"pool.ntp.org"
+// #define SNTP_SERVER_ADDRESS2 "ntp1.aliyun.com" //"pool.ntp.org"
+// #define SNTP_SERVER_ADDRESS3 "pool.ntp.org" //"pool.ntp.org"
+// #endif
+
+#endif
 /**
  * SO_REUSE==1: Enable SO_REUSEADDR option.
  */
index b63a099..8e3a112 100644 (file)
@@ -307,10 +307,16 @@ const ip_addr_t dns_mquery_v4group = DNS_MQUERY_IPV4_GROUP_INIT;
 const ip_addr_t dns_mquery_v6group = DNS_MQUERY_IPV6_GROUP_INIT;
 #endif /* LWIP_IPV6 */
 
-#ifndef DNS_SERVER_ADDRESS
-#define DNS_SERVER_ADDRESS(ipaddr)        (ip4_addr_set_u32(ip_2_ip4(ipaddr), ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */
+// #ifndef DNS_SERVER_ADDRESS
+// #define DNS_SERVER_ADDRESS(ipaddr)        (ip4_addr_set_u32(ip_2_ip4(ipaddr), ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */
+// #endif
+#if LWIP_IPV6 
+#define DNS_SERVER_ADDRESS(ipaddr)      ip_addr_set_ip4_u32(ipaddr,ipaddr_addr("8.8.8.8"))    
+#define DNS_SERVER_ADDRESS_SECONDARY(ipaddr)      ip_addr_set_ip4_u32(ipaddr,ipaddr_addr("114.114.114.114"))      
+#else
+#define DNS_SERVER_ADDRESS(ipaddr)      ip4_addr_set_u32(ipaddr,ipaddr_addr("8.8.8.8"))    
+#define DNS_SERVER_ADDRESS_SECONDARY(ipaddr)      ip4_addr_set_u32(ipaddr,ipaddr_addr("114.114.114.114"))    
 #endif
-
 /**
  * Initialize the resolver: set up the UDP pcb and configure the default server
  * (if DNS_SERVER_ADDRESS is set).
@@ -318,11 +324,16 @@ const ip_addr_t dns_mquery_v6group = DNS_MQUERY_IPV6_GROUP_INIT;
 void
 dns_init(void)
 {
-#ifdef DNS_SERVER_ADDRESS
+
+  #ifdef DNS_SERVER_ADDRESS
   /* initialize default DNS server address */
   ip_addr_t dnsserver = {0};
   DNS_SERVER_ADDRESS(&dnsserver);
   dns_setserver(0, &dnsserver);
+#ifdef DNS_SERVER_ADDRESS_SECONDARY
+  DNS_SERVER_ADDRESS_SECONDARY(&dnsserver);
+  dns_setserver(1, &dnsserver);
+#endif
 #endif /* DNS_SERVER_ADDRESS */
 
   LWIP_ASSERT("sanity check SIZEOF_DNS_QUERY",
index 9cd7964..9bd7cd5 100644 (file)
 #ifndef LWIP_SKIP_PACKING_CHECK
 
 #ifdef PACK_STRUCT_USE_INCLUDES
-#  include "arch/bpstruct.h"
+#include "arch/bpstruct.h"
+#endif
+
+#if LWIP_SLIP
+extern void slip_test_init(void);
 #endif
 PACK_STRUCT_BEGIN
 struct packed_struct_test {
@@ -375,6 +379,10 @@ lwip_init(void)
   ppp_init();
 #endif
 
+#if LWIP_SLIP
+  slip_test_init();
+#endif /* LWIP_SLIP */
+
 #if LWIP_TIMERS
   sys_timeouts_init();
 #endif /* LWIP_TIMERS */
index 7643e19..536c956 100644 (file)
@@ -135,6 +135,14 @@ u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len);
 void sio_read_abort(sio_fd_t fd);
 #endif
 
+#ifndef sio_send_flush
+/**
+ * Flush the SIO write buffer to UART.
+ * Forces all buffered data to be written immediately.
+ */
+void sio_send_flush(void);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
index a8f659d..cebdd59 100644 (file)
 #define SLIP_MAX_SIZE 1500
 #endif
 
+#ifdef SUPPORT_CAT1_SERVICE
+extern void cat1_api_wakeup_cat1(uint8_t wakeup);
+#endif
+
 /** Define this to the interface speed for SNMP
  * (sio_fd is the sio_fd_t returned by sio_open).
  * The default value of zero means 'unknown'.
@@ -141,6 +145,12 @@ slipif_output(struct netif *netif, struct pbuf *p)
   LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output: priv=%p, sd=%p\n", priv, priv->sd));
 
   /* Send pbuf out on the serial I/O device. */
+
+#ifdef SUPPORT_CAT1_SERVICE
+  //控制cat1_api_wakeup_cat1(true);
+  cat1_api_wakeup_cat1(true);
+#endif
+
   LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output: sending SLIP_END (0xC0) to start packet\n"));
   sio_send(SLIP_END, priv->sd);
 
@@ -178,6 +188,15 @@ slipif_output(struct netif *netif, struct pbuf *p)
   LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output: sending SLIP_END (0xC0) to end packet\n"));
   sio_send(SLIP_END, priv->sd);
   
+  /* Flush SIO write buffer to ensure all data is sent before CAT1 sleep */
+  sio_send_flush();
+  
+
+  //控制 cat1_api_wakeup_cat1(false);
+#ifdef SUPPORT_CAT1_SERVICE
+  cat1_api_wakeup_cat1(false);
+#endif
+
   LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output: transmission complete, total_bytes=%d, escaped_bytes=%d\n", 
                            total_bytes, escaped_bytes));
   
index bc9416e..b53e877 100644 (file)
@@ -335,17 +335,26 @@ LWIP_STATIC unsigned int get_hostip(const char *hname)
     const size_t hstbuflen = 1024;
     char *tmphstbuf = NULL;
 
+    if (hname == NULL || strlen(hname) == 0) {
+        PRINTK("get_hostip: invalid hostname\n");
+        return 0;
+    }
+
     tmphstbuf = (char *)zalloc(sizeof(char) * hstbuflen);
-    if (tmphstbuf == NULL)
+    if (tmphstbuf == NULL) {
+        PRINTK("get_hostip: failed to allocate DNS buffer\n");
         return 0;
+    }
 
     struct hostent *pent = gethostnameinfo(hname, tmphstbuf, hstbuflen);
     if (pent == NULL || pent->h_addr == NULL) {
+        PRINTK("get_hostip: DNS resolution failed for %s\n", hname);
         free(tmphstbuf);
         return 0;
     }
-    ret = memcpy_s(&ip, sizeof(ip), pent->h_addr, sizeof(pent->h_addr));
+    ret = memcpy_s(&ip, sizeof(ip), pent->h_addr, sizeof(ip));
     if (ret != EOK) {
+        PRINTK("get_hostip: failed to copy resolved IP address\n");
         free(tmphstbuf);
         return 0;
     }
@@ -624,6 +633,9 @@ u32_t OsShellPing(int argc, const char *argv)
     TSK_INIT_PARAM_S stPingTask;
     u32_t *parg = NULL;
 
+    /* Initialize dst_ipaddr to avoid random values */
+    dst_ipaddr.addr = IPADDR_NONE;
+
     if ((argc < 1) || (argv == NULL)) {
         PRINTK("Ping: require dest ipaddr at least\n");
         goto ping_error;
@@ -633,18 +645,33 @@ u32_t OsShellPing(int argc, const char *argv)
         count = LWIP_SHELL_CMD_PING_RETRY_TIMES;
     }
 
-    /* initialize dst IP address */
-    if (argc <= 0) {
+    printf("%s %d name %s\n",__func__,__LINE__,argv);
+    
+    /* Safe hostname copying with buffer overflow protection */
+    memset(hostname, 0, sizeof(hostname));
+    size_t argv_len = strlen(argv);
+    if (argv_len >= sizeof(hostname)) {
+        PRINTK("Hostname too long (max %d chars)\n", (int)(sizeof(hostname) - 1));
         goto ping_error;
     }
-    // printf("%s %d\n",__func__,__LINE__);
-    printf("%s %d name %s\n",__func__,__LINE__,argv);
-    memset(hostname,0,sizeof(hostname));
-    memcpy_s(hostname,20,argv,strlen(argv));
+    if (memcpy_s(hostname, sizeof(hostname), argv, argv_len) != EOK) {
+        PRINTK("Failed to copy hostname\n");
+        goto ping_error;
+    }
+
+    /* Try direct IP address parsing first */
+    if (inet_aton(argv, (struct in_addr*)&dst_ipaddr.addr) == 0) {
+        /* Direct IP parsing failed, try DNS resolution */
 #if LWIP_DNS
-    dst_ipaddr.addr = get_hostip(argv);
-    printf("%s %d dst_ip 0x%X\n",__func__,__LINE__,dst_ipaddr.addr);
+        dst_ipaddr.addr = get_hostip(argv);
+        printf("%s %d dst_ip 0x%X (from DNS)\n",__func__,__LINE__,dst_ipaddr.addr);
+#else
+        PRINTK("DNS not supported and invalid IP address format: %s\n", argv);
+        goto ping_error;
 #endif /* LWIP_DNS */
+    } else {
+        printf("%s %d dst_ip 0x%X (direct IP)\n",__func__,__LINE__,dst_ipaddr.addr);
+    }
 
     if (dst_ipaddr.addr == IPADDR_NONE || dst_ipaddr.addr == IPADDR_ANY) {
         PRINTK("Invalid dest ipaddr: NONE or ANY\n");
@@ -652,8 +679,9 @@ u32_t OsShellPing(int argc, const char *argv)
         return LOS_NOK;
     }
 
-    parg = (u32_t *)malloc(4 * sizeof(int)); /* 4: number of args */
+    parg = (u32_t *)malloc(4 * sizeof(u32_t)); /* 4: number of args */
     if (parg == NULL) {
+        PRINTK("Failed to allocate memory for ping arguments\n");
         return LOS_NOK;
     }
     parg[0] = dst_ipaddr.addr;
@@ -688,7 +716,7 @@ u32_t OsShellPing(int argc, const char *argv)
      2, ping task create failed;
     */
     if (OsPingFunc(parg) < 0) {
-        PRINTK("222Ping cmd failed due some errors\n");
+        PRINTK("Ping cmd failed due to some errors\n");
     }
 
     free(parg);
index a14cfa9..980c634 100644 (file)
@@ -1635,7 +1635,7 @@ errcode_t cat1_server_init(void)
     cat1_api_uart_init();
 
     // netif_service_init();
-    slip_test_init();
+    // slip_test_init();
     
     errcode_t ret=cat1_create_osThread_custom(&taskcfg);
     if(ret != ERRCODE_SUCC)