/*! \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 "stm32f2xx.h" #include "lwip/memp.h" #include "main.h" #include "lwip/api.h" #define UDP_TASK_PRIO ( tskIDLE_PRIORITY + 5) #define MAX_BUF_SIZE 50 #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 udp_send_printf(char *p) { if(sockfd>=0) { sendto(sockfd, p, strlen(p), 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr)); } return 1; }