/*! \file udp_echo.c \brief UDP demo program \version 2017-02-10, V1.0.0, firmware for GD32F30x \version 2018-10-10, V1.1.0, firmware for GD32F30x \version 2018-12-25, V2.0.0, firmware for GD32F30x */ /* Copyright (c) 2018, GigaDevice Semiconductor Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "lwip/opt.h" #include "lwip/tcp.h" #include "lwip/sys.h" #include "udp.h" #include #include #include "gd32f30x.h" #include "lwip/memp.h" #include "main.h" #include "lwip/api.h" #define UDP_TASK_PRIO ( tskIDLE_PRIORITY + 5) #define MAX_BUF_SIZE 50 #if ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1)) struct recev_packet { int length; char bytes[MAX_BUF_SIZE]; }; static err_t udp_echo_recv(struct netconn *conn, struct netbuf *buf, struct ip_addr *addr, u16_t port); /*! \brief called when a data is received \param[in] conn: the UDP netconn over which to send data \param[in] buf: the received data \param[in] addr: the IP address to send packet to \param[in] port: the port number to send packet to \param[out] none \retval err_t: error value */ static err_t udp_echo_recv(struct netconn *conn, struct netbuf *buf, struct ip_addr *addr, u16_t port) { struct pbuf *q; struct recev_packet recev_packet; uint32_t data_len = 0; struct netbuf *udpbuf; /* we perform here any necessary processing on the buf */ if(NULL != buf){ for(q = buf->p; q != NULL; q = q->next){ /* if the received data size is larger than the size we want to get */ if(q->len > (MAX_BUF_SIZE - data_len)){ /* only copy MAX_BUF_SIZE bytes data */ memcpy((recev_packet.bytes + data_len), q->payload, (MAX_BUF_SIZE - data_len)); data_len = MAX_BUF_SIZE; }else{ /* copy q->len bytes data */ memcpy((recev_packet.bytes + data_len), q->payload, q->len); data_len += q->len; } if(data_len >= MAX_BUF_SIZE){ break; } } recev_packet.length = data_len; udpbuf = netbuf_new(); netbuf_ref(udpbuf, recev_packet.bytes, recev_packet.length); netconn_sendto(conn, udpbuf, addr, port); netbuf_delete(udpbuf); } return ERR_OK; } /*! \brief udp echo task \param[in] arg: user supplied argument \param[out] none \retval none */ static void udp_task(void *arg) { struct netconn *conn; struct ip_addr ipaddr; struct netbuf *buf; err_t recv_err; IP4_ADDR(&ipaddr, IP_S_ADDR0, IP_S_ADDR1, IP_S_ADDR2, IP_S_ADDR3); /* creat UDP connection */ conn = netconn_new(NETCONN_UDP); netconn_bind(conn, IP_ADDR_ANY, 1025); while(1){ recv_err = netconn_recv(conn, &buf); while (ERR_OK == recv_err){ udp_echo_recv(conn, buf, &ipaddr, 1025); netbuf_delete(buf); recv_err = netconn_recv(conn, &buf); } /* close connection and discard connection identifier */ netconn_close(conn); netconn_delete(conn); } } #endif /* ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1)) */ #include "lwip/sockets.h" /*! \brief udp echo task \param[in] arg: user supplied argument \param[out] none \retval none */ volatile int8_t sockfd=-1; #define UDP_LOCAL_PORT 12345 #define UDP_REMOTE_PORT 54321 #define SERCER_IP_ADDRESS "192.168.2.22" //接收服务器ip struct sockaddr_in remote_addr; int udp_log_start() { int ret; struct sockaddr_in local_addr; // 创建UDP套接字 sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < -1) { perror("Error creating socket"); goto exit; } local_addr.sin_family=AF_INET; local_addr.sin_port=htons(UDP_LOCAL_PORT); local_addr.sin_addr.s_addr=INADDR_ANY; ret = bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr)); if(ret < 0) { lwip_close(sockfd); sockfd = -1; goto exit; } //设置远程服务器地址 TODO:服务器地址配置后自动获取 remote_addr.sin_family=AF_INET; remote_addr.sin_port=htons(UDP_REMOTE_PORT); inet_aton(SERCER_IP_ADDRESS,&(remote_addr.sin_addr)); return 1; exit: return -1; } void udp_log_close() { lwip_close(sockfd); sockfd=-1; } int fputc(int ch, FILE *f) { if(sockfd>=0) { sendto(sockfd, &ch, 1, 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr)); return ch; } } int udp_send_printf(char *p) { if(sockfd>=0) { sendto(sockfd, p, strlen(p), 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr)); } }