udp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*!
  2. \file udp_echo.c
  3. \brief UDP demo program
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "lwip/opt.h"
  33. #include "lwip/tcp.h"
  34. #include "lwip/sys.h"
  35. #include "udp.h"
  36. #include <string.h>
  37. #include <stdio.h>
  38. #include "gd32f30x.h"
  39. #include "lwip/memp.h"
  40. #include "main.h"
  41. #include "lwip/api.h"
  42. #define UDP_TASK_PRIO ( tskIDLE_PRIORITY + 5)
  43. #define MAX_BUF_SIZE 50
  44. #if ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1))
  45. struct recev_packet
  46. {
  47. int length;
  48. char bytes[MAX_BUF_SIZE];
  49. };
  50. static err_t udp_echo_recv(struct netconn *conn, struct netbuf *buf, struct ip_addr *addr, u16_t port);
  51. /*!
  52. \brief called when a data is received
  53. \param[in] conn: the UDP netconn over which to send data
  54. \param[in] buf: the received data
  55. \param[in] addr: the IP address to send packet to
  56. \param[in] port: the port number to send packet to
  57. \param[out] none
  58. \retval err_t: error value
  59. */
  60. static err_t udp_echo_recv(struct netconn *conn, struct netbuf *buf, struct ip_addr *addr, u16_t port)
  61. {
  62. struct pbuf *q;
  63. struct recev_packet recev_packet;
  64. uint32_t data_len = 0;
  65. struct netbuf *udpbuf;
  66. /* we perform here any necessary processing on the buf */
  67. if(NULL != buf){
  68. for(q = buf->p; q != NULL; q = q->next){
  69. /* if the received data size is larger than the size we want to get */
  70. if(q->len > (MAX_BUF_SIZE - data_len)){
  71. /* only copy MAX_BUF_SIZE bytes data */
  72. memcpy((recev_packet.bytes + data_len), q->payload, (MAX_BUF_SIZE - data_len));
  73. data_len = MAX_BUF_SIZE;
  74. }else{
  75. /* copy q->len bytes data */
  76. memcpy((recev_packet.bytes + data_len), q->payload, q->len);
  77. data_len += q->len;
  78. }
  79. if(data_len >= MAX_BUF_SIZE){
  80. break;
  81. }
  82. }
  83. recev_packet.length = data_len;
  84. udpbuf = netbuf_new();
  85. netbuf_ref(udpbuf, recev_packet.bytes, recev_packet.length);
  86. netconn_sendto(conn, udpbuf, addr, port);
  87. netbuf_delete(udpbuf);
  88. }
  89. return ERR_OK;
  90. }
  91. /*!
  92. \brief udp echo task
  93. \param[in] arg: user supplied argument
  94. \param[out] none
  95. \retval none
  96. */
  97. static void udp_task(void *arg)
  98. {
  99. struct netconn *conn;
  100. struct ip_addr ipaddr;
  101. struct netbuf *buf;
  102. err_t recv_err;
  103. IP4_ADDR(&ipaddr, IP_S_ADDR0, IP_S_ADDR1, IP_S_ADDR2, IP_S_ADDR3);
  104. /* creat UDP connection */
  105. conn = netconn_new(NETCONN_UDP);
  106. netconn_bind(conn, IP_ADDR_ANY, 1025);
  107. while(1){
  108. recv_err = netconn_recv(conn, &buf);
  109. while (ERR_OK == recv_err){
  110. udp_echo_recv(conn, buf, &ipaddr, 1025);
  111. netbuf_delete(buf);
  112. recv_err = netconn_recv(conn, &buf);
  113. }
  114. /* close connection and discard connection identifier */
  115. netconn_close(conn);
  116. netconn_delete(conn);
  117. }
  118. }
  119. #endif /* ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1)) */
  120. #include "lwip/sockets.h"
  121. /*!
  122. \brief udp echo task
  123. \param[in] arg: user supplied argument
  124. \param[out] none
  125. \retval none
  126. */
  127. volatile int8_t sockfd=-1;
  128. #define UDP_LOCAL_PORT 12345
  129. #define UDP_REMOTE_PORT 54321
  130. #define SERCER_IP_ADDRESS "192.168.2.22" //接收服务器ip
  131. struct sockaddr_in remote_addr;
  132. int udp_log_start()
  133. {
  134. int ret;
  135. struct sockaddr_in local_addr;
  136. // 创建UDP套接字
  137. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  138. if (sockfd < -1) {
  139. perror("Error creating socket");
  140. goto exit;
  141. }
  142. local_addr.sin_family=AF_INET;
  143. local_addr.sin_port=htons(UDP_LOCAL_PORT);
  144. local_addr.sin_addr.s_addr=INADDR_ANY;
  145. ret = bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr));
  146. if(ret < 0)
  147. {
  148. lwip_close(sockfd);
  149. sockfd = -1;
  150. goto exit;
  151. }
  152. //设置远程服务器地址 TODO:服务器地址配置后自动获取
  153. remote_addr.sin_family=AF_INET;
  154. remote_addr.sin_port=htons(UDP_REMOTE_PORT);
  155. inet_aton(SERCER_IP_ADDRESS,&(remote_addr.sin_addr));
  156. return 1;
  157. exit:
  158. return -1;
  159. }
  160. void udp_log_close()
  161. {
  162. lwip_close(sockfd);
  163. sockfd=-1;
  164. }
  165. int fputc(int ch, FILE *f)
  166. {
  167. if(sockfd>=0)
  168. {
  169. sendto(sockfd, &ch, 1, 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr));
  170. return ch;
  171. }
  172. }
  173. int udp_send_printf(char *p)
  174. {
  175. if(sockfd>=0)
  176. {
  177. sendto(sockfd, p, strlen(p), 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr));
  178. }
  179. }