From: 孙春阳 Date: Sat, 23 Aug 2025 09:26:44 +0000 (+0800) Subject: slip通过lwip初始化 X-Git-Tag: leiniao_debug_1.0.0_20250922_020716~164^2~2 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=89b4d6637b636030275e40257c389e540358a500;p=mirrors%2FFD230.git slip通过lwip初始化 --- diff --git a/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/arch/sio.c b/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/arch/sio.c index f0c6dd9fb..2108834da 100644 --- a/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/arch/sio.c +++ b/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/arch/sio.c @@ -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 diff --git a/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/include/lwip/lwipopts_default.h b/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/include/lwip/lwipopts_default.h index a75bcaeb5..8d88c4e99 100644 --- a/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/include/lwip/lwipopts_default.h +++ b/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/include/lwip/lwipopts_default.h @@ -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. */ diff --git a/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/dns.c b/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/dns.c index b63a09999..8e3a11212 100644 --- a/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/dns.c +++ b/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/dns.c @@ -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", diff --git a/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/init.c b/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/init.c index 9cd79642e..9bd7cd5a8 100644 --- a/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/init.c +++ b/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/init.c @@ -64,7 +64,11 @@ #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 */ diff --git a/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/include/lwip/sio.h b/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/include/lwip/sio.h index 7643e1956..536c95629 100644 --- a/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/include/lwip/sio.h +++ b/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/include/lwip/sio.h @@ -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 diff --git a/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/netif/slipif.c b/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/netif/slipif.c index a8f659d9c..cebdd59b7 100644 --- a/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/netif/slipif.c +++ b/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/netif/slipif.c @@ -83,6 +83,10 @@ #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)); diff --git a/code/sdk/zhihui/4g/cat1Service/Service/api_shell.c b/code/sdk/zhihui/4g/cat1Service/Service/api_shell.c index bc9416e64..b53e877ad 100644 --- a/code/sdk/zhihui/4g/cat1Service/Service/api_shell.c +++ b/code/sdk/zhihui/4g/cat1Service/Service/api_shell.c @@ -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); diff --git a/code/sdk/zhihui/4g/cat1Service/Service/cat1_server.c b/code/sdk/zhihui/4g/cat1Service/Service/cat1_server.c index a14cfa9f7..980c6345c 100644 --- a/code/sdk/zhihui/4g/cat1Service/Service/cat1_server.c +++ b/code/sdk/zhihui/4g/cat1Service/Service/cat1_server.c @@ -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)