/* * lwip_util.c * * Created on: 2025年7月29日 * Author: wulianwei */ #include "device/console.h" #include "hal/nim_ethernet.h" #include "lwiplib.h" #include "lwipopts.h" #define TCP_POLL_INTERVAL 4 #define MAX_SIZE 4096 static unsigned char mydata[MAX_SIZE]; static err_t tcp_accept_handle(void *arg, struct tcp_pcb *pcb, err_t err); static err_t tcp_recv_handle(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err); static void tcp_close_conn(struct tcp_pcb *pcb); err_t tcp_send_data(struct tcp_pcb *pcb, struct pbuf *p); static udp_pcb *gui_udp_pcb; void lwip_device_init(void) { unsigned char macArray[6] = MAC_ADDRESS; EthernetInit();//以太网外设配置 unsigned int ipAddr = 0; #if STATIC_IP_ADDRESS ipAddr = lwIPInit(0, macArray, STATIC_IP_ADDRESS, 0, 0, IPADDR_USE_STATIC); #else ipAddr = lwIPInit(0, macArray, 0, 0, 0, IPADDR_USE_DHCP); #endif ConsolePrintf("lwIP HostIP:%d.%d.%d.%d\n", (ipAddr >> (0 * 8)) & 0xFF, (ipAddr >> (1 * 8)) & 0xFF, (ipAddr >> (2 * 8)) & 0xFF, (ipAddr >> (3 * 8)) & 0xFF); gui_udp_pcb = udp_new(); } /** * tcp 服务初始化 */ void lwip_tcp_init(int port) { struct tcp_pcb *pcb; pcb = tcp_new(); tcp_bind(pcb, IP_ADDR_ANY, port); pcb = tcp_listen(pcb); /* initialize callback arg and accept callback */ tcp_arg(pcb, pcb); tcp_accept(pcb, tcp_accept_handle); } /** * 链接监听函数. */ static err_t tcp_accept_handle(void *arg, struct tcp_pcb *pcb, err_t err) { LWIP_UNUSED_ARG(err); /* Decrease the listen backlog counter */ tcp_accepted((struct tcp_pcb_listen*)arg); tcp_setprio(pcb, TCP_PRIO_MAX); /* Set up the various callback functions */ tcp_recv(pcb, tcp_recv_handle); tcp_err(pcb, NULL); tcp_poll(pcb, NULL, TCP_POLL_INTERVAL); tcp_sent(pcb, NULL); return ERR_OK; } /** * 接收数据处理函数. */ static err_t tcp_recv_handle(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { err_t err_send; if (p != NULL){ /* Inform TCP that we have taken the data. */ tcp_recved(pcb, p->tot_len); } if ((err != ERR_OK) || (p == NULL)){ /* error or closed by other side */ if (p != NULL) { pbuf_free(p); } tcp_close_conn(pcb); return ERR_OK; } err_send = tcp_send_data(pcb, p); return err_send; } /** * 关闭远程连接. */ static void tcp_close_conn(struct tcp_pcb *pcb) { tcp_recv(pcb, NULL); tcp_close(pcb); /* closing succeeded */ tcp_arg(pcb, NULL); tcp_sent(pcb, NULL); } /** * 发送数据 */ err_t tcp_send_data(struct tcp_pcb *pcb, struct pbuf *p) { err_t err = ERR_OK; char *data; unsigned int cnt = 0, j, i; unsigned int len, tot_len; struct pbuf *temp = p; tot_len = p->tot_len; /** * traverse pbuf chain and store payload * of each pbuf into buffer */ do { data = (char*)p->payload; len = p->len; for(i = 0,j = 0; i < len; i++,j++,cnt++) { mydata[cnt] = data[j]; } p = p->next; }while(p!=NULL); /* free pbuf's */ pbuf_free(temp); /** *send the data in buffer over network with * tcp header attached */ //err = tcp_write(pcb, mydata, tot_len , TCP_WRITE_FLAG_COPY); ConsolePuts((char*)mydata, tot_len); return err; } /** * udp 链接 */ void lwip_udp_connect(struct udp_pcb *pcb,unsigned int ip,int port) { struct ip_addr ip_dst; ip_dst.addr = htonl(ip); udp_connect(pcb,&ip_dst,port); } /** * udp 发送 */ void lwip_udp_send(struct udp_pcb *pcb, unsigned char* data,int len) { struct pbuf* pbuf = pbuf_alloc(PBUF_TRANSPORT, len+sizeof(struct pbuf), PBUF_REF); pbuf->payload = data; udp_send(pcb,pbuf); pbuf_free(pbuf); } /** * udp 发送 */ void lwip_udp_sendto(struct udp_pcb *pcb, unsigned int ip,int port,unsigned char* data,int len) { struct ip_addr ip_dst; ip_dst.addr = htonl(ip); struct pbuf* pbuf = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct pbuf), PBUF_REF); pbuf->payload = data; udp_sendto(pcb,pbuf,&ip_dst,port); pbuf_free(pbuf); } /** * udp 向gui发送数据 */ void lwip_udp_send_gui(unsigned char* data,int len) { struct ip_addr ip_dst; ip_dst.addr = htonl(UI_IP_ADDRESS); struct pbuf* pbuf = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct pbuf), PBUF_REF); pbuf->payload = data; udp_sendto(gui_udp_pcb,pbuf,&ip_dst,UI_UDP_PORT); pbuf_free(pbuf); }