lwip.c 4.4 KB

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