lwip.c 3.9 KB

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