lwip.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * lwip_util.c
  3. *
  4. * Created on: 2025年7月29日
  5. * Author: wulianwei
  6. */
  7. #include "device/console.h"
  8. #include "hal/nim_ethernet.h"
  9. #include "lwiplib.h"
  10. #include "lwipopts.h"
  11. #define TCP_POLL_INTERVAL 4
  12. #define MAX_SIZE 4096
  13. static unsigned char mydata[MAX_SIZE];
  14. static err_t tcp_accept_handle(void *arg, struct tcp_pcb *pcb, err_t err);
  15. static err_t tcp_recv_handle(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
  16. static void tcp_close_conn(struct tcp_pcb *pcb);
  17. err_t tcp_send_data(struct tcp_pcb *pcb, struct pbuf *p);
  18. static udp_pcb *gui_udp_pcb;
  19. void lwip_device_init(void)
  20. {
  21. unsigned char macArray[6] = MAC_ADDRESS;
  22. EthernetInit();//以太网外设配置
  23. unsigned int ipAddr = 0;
  24. #if STATIC_IP_ADDRESS
  25. ipAddr = lwIPInit(0, macArray, STATIC_IP_ADDRESS, 0, 0, IPADDR_USE_STATIC);
  26. #else
  27. ipAddr = lwIPInit(0, macArray, 0, 0, 0, IPADDR_USE_DHCP);
  28. #endif
  29. ConsolePrintf("lwIP HostIP:%d.%d.%d.%d\n", (ipAddr >> (0 * 8)) & 0xFF,
  30. (ipAddr >> (1 * 8)) & 0xFF, (ipAddr >> (2 * 8)) & 0xFF,
  31. (ipAddr >> (3 * 8)) & 0xFF);
  32. gui_udp_pcb = udp_new();
  33. }
  34. /**
  35. * tcp 服务初始化
  36. */
  37. void lwip_tcp_init(int port)
  38. {
  39. struct tcp_pcb *pcb;
  40. pcb = tcp_new();
  41. tcp_bind(pcb, IP_ADDR_ANY, port);
  42. pcb = tcp_listen(pcb);
  43. /* initialize callback arg and accept callback */
  44. tcp_arg(pcb, pcb);
  45. tcp_accept(pcb, tcp_accept_handle);
  46. }
  47. /**
  48. * 链接监听函数.
  49. */
  50. static err_t tcp_accept_handle(void *arg, struct tcp_pcb *pcb, err_t err)
  51. {
  52. LWIP_UNUSED_ARG(err);
  53. /* Decrease the listen backlog counter */
  54. tcp_accepted((struct tcp_pcb_listen*)arg);
  55. tcp_setprio(pcb, TCP_PRIO_MAX);
  56. /* Set up the various callback functions */
  57. tcp_recv(pcb, tcp_recv_handle);
  58. tcp_err(pcb, NULL);
  59. tcp_poll(pcb, NULL, TCP_POLL_INTERVAL);
  60. tcp_sent(pcb, NULL);
  61. return ERR_OK;
  62. }
  63. /**
  64. * 接收数据处理函数.
  65. */
  66. static err_t tcp_recv_handle(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  67. {
  68. err_t err_send;
  69. if (p != NULL){
  70. /* Inform TCP that we have taken the data. */
  71. tcp_recved(pcb, p->tot_len);
  72. }
  73. if ((err != ERR_OK) || (p == NULL)){
  74. /* error or closed by other side */
  75. if (p != NULL) {
  76. pbuf_free(p);
  77. }
  78. tcp_close_conn(pcb);
  79. return ERR_OK;
  80. }
  81. err_send = tcp_send_data(pcb, p);
  82. return err_send;
  83. }
  84. /**
  85. * 关闭远程连接.
  86. */
  87. static void tcp_close_conn(struct tcp_pcb *pcb)
  88. {
  89. tcp_recv(pcb, NULL);
  90. tcp_close(pcb);
  91. /* closing succeeded */
  92. tcp_arg(pcb, NULL);
  93. tcp_sent(pcb, NULL);
  94. }
  95. /**
  96. * 发送数据
  97. */
  98. err_t tcp_send_data(struct tcp_pcb *pcb, struct pbuf *p)
  99. {
  100. err_t err = ERR_OK;
  101. char *data;
  102. unsigned int cnt = 0, j, i;
  103. unsigned int len, tot_len;
  104. struct pbuf *temp = p;
  105. tot_len = p->tot_len;
  106. /**
  107. * traverse pbuf chain and store payload
  108. * of each pbuf into buffer
  109. */
  110. do {
  111. data = (char*)p->payload;
  112. len = p->len;
  113. for(i = 0,j = 0; i < len; i++,j++,cnt++)
  114. {
  115. mydata[cnt] = data[j];
  116. }
  117. p = p->next;
  118. }while(p!=NULL);
  119. /* free pbuf's */
  120. pbuf_free(temp);
  121. /**
  122. *send the data in buffer over network with
  123. * tcp header attached
  124. */
  125. //err = tcp_write(pcb, mydata, tot_len , TCP_WRITE_FLAG_COPY);
  126. ConsolePuts((char*)mydata, tot_len);
  127. return err;
  128. }
  129. /**
  130. * udp 链接
  131. */
  132. void lwip_udp_connect(struct udp_pcb *pcb,unsigned int ip,int port)
  133. {
  134. struct ip_addr ip_dst;
  135. ip_dst.addr = htonl(ip);
  136. udp_connect(pcb,&ip_dst,port);
  137. }
  138. /**
  139. * udp 发送
  140. */
  141. void lwip_udp_send(struct udp_pcb *pcb, unsigned char* data,int len)
  142. {
  143. struct pbuf* pbuf = pbuf_alloc(PBUF_TRANSPORT, len+sizeof(struct pbuf), PBUF_REF);
  144. pbuf->payload = data;
  145. udp_send(pcb,pbuf);
  146. pbuf_free(pbuf);
  147. }
  148. /**
  149. * udp 发送
  150. */
  151. void lwip_udp_sendto(struct udp_pcb *pcb, unsigned int ip,int port,unsigned char* data,int len)
  152. {
  153. struct ip_addr ip_dst;
  154. ip_dst.addr = htonl(ip);
  155. struct pbuf* pbuf = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct pbuf), PBUF_REF);
  156. pbuf->payload = data;
  157. udp_sendto(pcb,pbuf,&ip_dst,port);
  158. pbuf_free(pbuf);
  159. }
  160. /**
  161. * udp 向gui发送数据
  162. */
  163. void lwip_udp_send_gui(unsigned char* data,int len)
  164. {
  165. struct ip_addr ip_dst;
  166. ip_dst.addr = htonl(UI_IP_ADDRESS);
  167. struct pbuf* pbuf = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct pbuf), PBUF_REF);
  168. pbuf->payload = data;
  169. udp_sendto(gui_udp_pcb,pbuf,&ip_dst,UI_UDP_PORT);
  170. pbuf_free(pbuf);
  171. }