Browse Source

added socket send and recv for udp and tcp

pull/125/head
wlyu 7 months ago
parent
commit
e2e127bb0e
  1. 2
      APP_Framework/Applications/connection_app/socket_demo/Makefile
  2. 92
      APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c
  3. 156
      APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c
  4. 202
      APP_Framework/Applications/connection_app/socket_demo/udp_socket.c
  5. 42
      APP_Framework/Applications/connection_app/socket_demo/udp_socket.h
  6. 17
      Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/udp_echo.c

2
APP_Framework/Applications/connection_app/socket_demo/Makefile

@ -1,3 +1,3 @@
SRC_FILES := udp_socket.c lwip_tcp_socket_demo.c lwip_udp_socket_demo.c
SRC_FILES := lwip_tcp_socket_demo.c lwip_udp_socket_demo.c
include $(KERNEL_ROOT)/compiler.mk

92
APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c

@ -39,14 +39,94 @@
char tcp_socket_ip[] = {192, 168, 250, 252};
char* tcp_socket_str = "\n\nThis one is TCP pkg. Congratulations on you.\n\n";
#define TCP_BUF_SIZE 1024
/*******************************************************************************
* Code
******************************************************************************/
static void lwip_tcp_socket_thread(void *arg)
static void tcp_recv_demo(void *arg)
{
lw_print("tcp_recv_demo start.\n");
int socket_fd = -1;
char *recv_buf;
struct sockaddr_in tcp_addr, server_addr;
int recv_len;
socklen_t addr_len;
while(1)
{
recv_buf = (char *)malloc(TCP_BUF_SIZE);
if (recv_buf == NULL)
{
lw_print("No memory\n");
goto __exit;
}
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0)
{
lw_print("Socket error\n");
goto __exit;
}
tcp_addr.sin_family = AF_INET;
tcp_addr.sin_addr.s_addr = INADDR_ANY;
tcp_addr.sin_port = htons(LOCAL_PORT_SERVER);
memset(&(tcp_addr.sin_zero), 0, sizeof(tcp_addr.sin_zero));
if (bind(socket_fd, (struct sockaddr *)&tcp_addr, sizeof(struct sockaddr)) == -1)
{
lw_print("Unable to bind\n");
goto __exit;
}
lw_print("tcp bind sucess, start to receive.\n");
lw_print("\n\nLocal Port:%d\n\n", LOCAL_PORT_SERVER);
while(1)
{
memset(recv_buf, 0, TCP_BUF_SIZE);
recv_len = recvfrom(socket_fd, recv_buf, TCP_BUF_SIZE, 0, (struct sockaddr *)&server_addr, &addr_len);
lw_print("Receive from : %s\n", inet_ntoa(server_addr.sin_addr));
lw_print("Receive data : %s\n\n", recv_buf);
sendto(socket_fd, recv_buf, recv_len, 0, (struct sockaddr*)&server_addr, addr_len);
}
__exit:
if (socket_fd >= 0)
closesocket(socket_fd);
if (recv_buf)
free(recv_buf);
}
}
void tcp_socket_recv_run(int argc, char *argv[])
{
int result = 0;
pthread_t th_id;
pthread_attr_t attr;
if(argc == 2)
{
lw_print("lw: [%s] gw %s\n", __func__, argv[1]);
sscanf(argv[1], "%d.%d.%d.%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3]);
}
ETH_BSP_Config();
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
sys_thread_new("tcp_recv_demo", tcp_recv_demo, NULL, 4096, 25);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
TcpSocketRecv, tcp_socket_recv_run, TCP recv echo);
static void tcp_send_demo(void *arg)
{
int cnt = TEST_LWIP_TIMES;
lw_print("lwip_tcp_socket_thread start.\n");
lw_print("tcp_send_demo start.\n");
int sock_tcp_send_once = -1;
sock_tcp_send_once = socket(AF_INET, SOCK_STREAM, 0);
@ -85,7 +165,7 @@ static void lwip_tcp_socket_thread(void *arg)
MdelayKTask(1000);
}
__exit:
__exit:
if (sock_tcp_send_once >= 0)
closesocket(sock_tcp_send_once);
@ -93,7 +173,7 @@ static void lwip_tcp_socket_thread(void *arg)
}
void lwip_tcp_socket_run(int argc, char *argv[])
void tcp_socket_send_run(int argc, char *argv[])
{
if(argc == 2)
{
@ -103,9 +183,9 @@ void lwip_tcp_socket_run(int argc, char *argv[])
ETH_BSP_Config();
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
sys_thread_new("tcp socket", lwip_tcp_socket_thread, NULL, 4096, 25);
sys_thread_new("tcp socket", tcp_send_demo, NULL, 4096, 25);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
TCPSocket, lwip_tcp_socket_run, TCP Client);
TCPSocketSend, tcp_socket_send_run, TCP send demo);

156
APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c

@ -21,8 +21,8 @@
#include <xiuos.h>
#include "board.h"
#include "sys_arch.h"
#include "udp_socket.h"
#include "lwip/udp.h"
#include "lwip/opt.h"
/*******************************************************************************
* Definitions
@ -35,7 +35,7 @@
/*******************************************************************************
* Variables
******************************************************************************/
extern char udp_target[];
static struct udp_pcb *udpecho_raw_pcb;
char udp_socket_ip[] = {192, 168, 250, 252};
@ -43,14 +43,156 @@ char udp_socket_ip[] = {192, 168, 250, 252};
* Code
******************************************************************************/
static void lwip_udp_socket_thread(void* param)
#include <lwip/sockets.h>
#include "lwip/sys.h"
#define LWIP_UDP_TASK_STACK 4096
#define LWIP_UDP_TASK_PRIO 25
#define UDP_BUF_SIZE 1024
char* udp_socket_str = "\n\nThis one is UDP pkg. Congratulations on you.\n\n";
static void udp_recv_demo(void *arg)
{
lw_print("udp_recv_demo start.\n");
int socket_fd = -1;
char *recv_buf;
struct sockaddr_in udp_addr, server_addr;
int recv_len;
socklen_t addr_len;
while(1)
{
recv_buf = (char *)malloc(UDP_BUF_SIZE);
if (recv_buf == NULL)
{
lw_print("No memory\n");
goto __exit;
}
socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_fd < 0)
{
lw_print("Socket error\n");
goto __exit;
}
udp_addr.sin_family = AF_INET;
udp_addr.sin_addr.s_addr = INADDR_ANY;
udp_addr.sin_port = htons(LOCAL_PORT_SERVER);
memset(&(udp_addr.sin_zero), 0, sizeof(udp_addr.sin_zero));
if (bind(socket_fd, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr)) == -1)
{
lw_print("Unable to bind\n");
goto __exit;
}
lw_print("UDP bind sucess, start to receive.\n");
lw_print("\n\nLocal Port:%d\n\n", LOCAL_PORT_SERVER);
while(1)
{
memset(recv_buf, 0, UDP_BUF_SIZE);
recv_len = recvfrom(socket_fd, recv_buf, UDP_BUF_SIZE, 0, (struct sockaddr *)&server_addr, &addr_len);
lw_print("Receive from : %s\n", inet_ntoa(server_addr.sin_addr));
lw_print("Receive data : %s\n\n", recv_buf);
sendto(socket_fd, recv_buf, recv_len, 0, (struct sockaddr*)&server_addr, addr_len);
}
__exit:
if (socket_fd >= 0)
closesocket(socket_fd);
if (recv_buf)
free(recv_buf);
}
}
static void udp_recv_demo_thread(void* param)
{
ETH_BSP_Config();
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
sys_thread_new("udp_recv_demo", udp_recv_demo, NULL, LWIP_UDP_TASK_STACK, LWIP_UDP_TASK_PRIO);
}
void udp_socket_recv_run(int argc, char *argv[])
{
int result = 0;
pthread_t th_id;
pthread_attr_t attr;
if(argc == 2)
{
lw_print("lw: [%s] gw %s\n", __func__, argv[1]);
sscanf(argv[1], "%d.%d.%d.%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3]);
}
sys_thread_new("udp socket send", udp_recv_demo_thread, NULL, 4096, 15);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
UDPSocketRecv, udp_socket_recv_run, UDP recv echo);
static void udp_send_demo(void *arg)
{
int cnt = TEST_LWIP_TIMES;
lw_print("udp_send_demo start.\n");
int socket_fd = -1;
socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_fd < 0)
{
lw_print("Socket error\n");
goto __exit;
}
struct sockaddr_in udp_sock;
udp_sock.sin_family = AF_INET;
udp_sock.sin_port = htons(TARGET_PORT_CLIENT);
udp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(udp_target[0],udp_target[1],udp_target[2],udp_target[3]));
memset(&(udp_sock.sin_zero), 0, sizeof(udp_sock.sin_zero));
if (connect(socket_fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr)))
{
lw_print("Unable to connect\n");
goto __exit;
}
lw_print("UDP connect success, start to send.\n");
lw_print("\n\nTarget Port:%d\n\n", udp_sock.sin_port);
while (cnt --)
{
sendto(socket_fd, udp_socket_str,
strlen(udp_socket_str), 0,
(struct sockaddr*)&udp_sock,
sizeof(struct sockaddr));
lw_pr_info("Send UDP msg: %s ", udp_socket_str);
MdelayKTask(1000);
}
__exit:
if (socket_fd >= 0)
{
closesocket(socket_fd);
}
return;
}
static void udp_send_demo_thread(void* param)
{
ETH_BSP_Config();
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
UdpSocketInit();
sys_thread_new("udp_send_demo", udp_send_demo, NULL, LWIP_UDP_TASK_STACK, LWIP_UDP_TASK_PRIO);
}
void lwip_udp_socket_run(int argc, char *argv[])
void udp_socket_send_run(int argc, char *argv[])
{
int result = 0;
pthread_t th_id;
@ -62,9 +204,9 @@ void lwip_udp_socket_run(int argc, char *argv[])
sscanf(argv[1], "%d.%d.%d.%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3]);
}
sys_thread_new("udp socket", lwip_udp_socket_thread, NULL, 4096, 15);
sys_thread_new("udp socket send", udp_send_demo_thread, NULL, 4096, 15);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
UDPSocket, lwip_udp_socket_run, UDP send echo);
UDPSocketSend, udp_socket_send_run, UDP send echo);

202
APP_Framework/Applications/connection_app/socket_demo/udp_socket.c

@ -1,202 +0,0 @@
/*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* 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. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
/*
* Copyright (c) 2020 AIIT XUOS Lab
* XiUOS is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* @file tcpecho.c
* @brief Add UDP client function
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021-05-29
*/
#include "udp_socket.h"
#include <transform.h>
#include "lwip/opt.h"
#include "sys_arch.h"
#if LWIP_SOCKET
#include <lwip/sockets.h>
#include "lwip/sys.h"
#ifdef BOARD_CORTEX_M7_EVB
#define LWIP_UDP_TASK_STACK 4096
#else
#define LWIP_UDP_TASK_STACK 2048
#endif
#define LWIP_UDP_TASK_PRIO 25
#define RECV_DATA (1024)
char* udp_socket_str = "\n\nThis one is UDP pkg. Congratulations on you.\n\n";
static void UdpSocketThreadServer(void *arg)
{
lw_print("UdpSocketThreadServer start.\n");
int sock = -1;
char *recv_data;
struct sockaddr_in udp_addr,seraddr;
int recv_data_len;
socklen_t addrlen;
while(1)
{
recv_data = (char *)malloc(RECV_DATA);
if (recv_data == NULL)
{
lw_print("No memory\n");
goto __exit;
}
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
{
lw_print("Socket error\n");
goto __exit;
}
udp_addr.sin_family = AF_INET;
udp_addr.sin_addr.s_addr = INADDR_ANY;
udp_addr.sin_port = htons(LOCAL_PORT_SERVER);
memset(&(udp_addr.sin_zero), 0, sizeof(udp_addr.sin_zero));
if (bind(sock, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr)) == -1)
{
lw_print("Unable to bind\n");
goto __exit;
}
lw_print("UDP bind sucess, start to receive.\n");
lw_print("\n\nLocal Port:%d\n\n", LOCAL_PORT_SERVER);
while(1)
{
memset(recv_data, 0, RECV_DATA);
recv_data_len=recvfrom(sock,recv_data,
RECV_DATA,0,
(struct sockaddr*)&seraddr,
&addrlen);
lw_print("Receive from : %s\n",inet_ntoa(seraddr.sin_addr));
lw_print("Receive data : %s\n\n",recv_data);
sendto(sock,recv_data,
recv_data_len,0,
(struct sockaddr*)&seraddr,
addrlen);
}
__exit:
if (sock >= 0)
closesocket(sock);
if (recv_data)
free(recv_data);
}
}
static void UdpSocketThreadClient(void *arg)
{
int cnt = TEST_LWIP_TIMES;
lw_print("UdpSocketThreadClient start.\n");
int sock_udp_send_once = -1;
sock_udp_send_once = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_udp_send_once < 0)
{
lw_print("Socket error\n");
goto __exit;
}
struct sockaddr_in udp_sock;
udp_sock.sin_family = AF_INET;
udp_sock.sin_port = htons(TARGET_PORT_CLIENT);
udp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(udp_target[0],udp_target[1],udp_target[2],udp_target[3]));
memset(&(udp_sock.sin_zero), 0, sizeof(udp_sock.sin_zero));
if (connect(sock_udp_send_once, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr)))
{
lw_print("Unable to connect\n");
goto __exit;
}
lw_print("UDP connect success, start to send.\n");
lw_print("\n\nTarget Port:%d\n\n", udp_sock.sin_port);
while (cnt --)
{
lw_print("UDP Client is running.\n");
sendto(sock_udp_send_once, udp_socket_str,
strlen(udp_socket_str), 0,
(struct sockaddr*)&udp_sock,
sizeof(struct sockaddr));
lw_pr_info("Send UDP msg: %s ", udp_socket_str);
MdelayKTask(1000);
}
__exit:
if (sock_udp_send_once >= 0)
{
closesocket(sock_udp_send_once);
}
return;
}
void
UdpSocketInit(void)
{
#ifdef SET_AS_SERVER
sys_thread_new("UdpSocketThreadServer", UdpSocketThreadServer, NULL, LWIP_UDP_TASK_STACK, LWIP_UDP_TASK_PRIO);
#else
sys_thread_new("UdpSocketThreadClient", UdpSocketThreadClient, NULL, LWIP_UDP_TASK_STACK, LWIP_UDP_TASK_PRIO);
#endif
}
#endif /* LWIP_NETCONN */

42
APP_Framework/Applications/connection_app/socket_demo/udp_socket.h

@ -1,42 +0,0 @@
/*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* 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. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
#ifndef LWIP_UDP_SOCKET_H
#define LWIP_UDP_SOCKET_H
extern char udp_target[];
void UdpSocketInit(void);
#endif /* LWIP_TCPECHO_H */

17
Ubiquitous/XiUOS/resources/ethernet/cmd_lwip/udp_echo.c

@ -163,19 +163,12 @@ static void UdpEchoThreadClient(void *arg)
lw_print("UDP connect success, start to send.\n");
lw_print("\n\nTarget Port:%d\n\n", udp_sock.sin_port);
while (cnt --)
{
lw_print("UDP Client is running.\n");
sendto(sock_udp_send_once, udp_send_msg,
strlen(udp_send_msg), 0,
(struct sockaddr*)&udp_sock,
sizeof(struct sockaddr));
sendto(sock_udp_send_once, udp_send_msg,
strlen(udp_send_msg), 0,
(struct sockaddr*)&udp_sock,
sizeof(struct sockaddr));
lw_pr_info("Send UDP msg: %s ", udp_send_msg);
MdelayKTask(1000);
}
lw_pr_info("Send UDP msg: %s ", udp_send_msg);
__exit:
if (sock_udp_send_once >= 0)

Loading…
Cancel
Save