| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- /*
- * lwip_util.c
- *
- * Created on: 2025年7月29日
- * Author: wulianwei
- */
- #include "nim_config.h"
- #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 struct udp_pcb *gui_udp_pcb;
- void lwip_device_init(void)
- {
- unsigned char macArray[6] = MAC_ADDRESS;
- EthernetInit(); //以太网外设配置
- unsigned int ipAddr = 0;
- unsigned int retry_count = 0;
- while(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
- if(retry_count++ > 5) {
- ConsolePrintf("lwIPInit failed!.\r\n\r\n");
- }
- delay_ms(2000);
- }
- 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();
- lwip_udp_connect(gui_udp_pcb, UI_IP_ADDRESS, UI_UDP_PORT);
- }
- /**
- * 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 pbuf *pbuf = pbuf_alloc(PBUF_TRANSPORT, len + sizeof(struct pbuf),
- PBUF_REF);
- pbuf->payload = data;
- udp_send(gui_udp_pcb, pbuf);
- pbuf_free(pbuf);
- }
|