lwip.c 4.7 KB

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