lwip.c 4.9 KB

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