/* 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);
/**
/**
* 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)
{
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 */
+ }
}
/**
/* 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
#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
#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
/*
#define LWIP_WND_SCALE 0
#endif
-#ifndef LWIP_SNTP
-#define LWIP_SNTP 0
-#endif
-
#ifndef LWIP_SOCK_FILTER
#define LWIP_SOCK_FILTER 0
#endif
#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.
*/
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).
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",
#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 {
ppp_init();
#endif
+#if LWIP_SLIP
+ slip_test_init();
+#endif /* LWIP_SLIP */
+
#if LWIP_TIMERS
sys_timeouts_init();
#endif /* LWIP_TIMERS */
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
#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'.
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);
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));
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;
}
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;
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");
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;
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);
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)