forked from xuos/xiuos
support socket demo on nuttx
This commit is contained in:
parent
0ac4dc05fc
commit
3bc1cffa33
|
@ -3,5 +3,11 @@ menu "connection app"
|
|||
menuconfig APPLICATION_CONNECTION
|
||||
bool "Using connection apps"
|
||||
default n
|
||||
|
||||
|
||||
if APPLICATION_CONNECTION
|
||||
menuconfig SOCKET_DEMO
|
||||
bool "Config test socket demo"
|
||||
default n
|
||||
endif
|
||||
|
||||
endmenu
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
############################################################################
|
||||
# APP_Framework/Application/connection_app/Make.defs
|
||||
############################################################################
|
||||
ifneq ($(CONFIG_APPLICATION_CONNECTION),)
|
||||
CONFIGURED_APPS += $(APPDIR)/../../../APP_Framework/Applications/connection_app/socket_demo
|
||||
endif
|
|
@ -1,7 +1,20 @@
|
|||
SRC_DIR :=
|
||||
|
||||
ifeq ($(CONFIG_RESOURCES_LWIP),y)
|
||||
SRC_DIR += socket_demo
|
||||
ifeq ($(CONFIG_SOCKET_DEMO),y)
|
||||
|
||||
include $(KERNEL_ROOT)/.config
|
||||
|
||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
|
||||
include $(APPDIR)/Make.defs
|
||||
include $(APPDIR)/Application.mk
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
|
||||
|
||||
ifeq ($(CONFIG_RESOURCES_LWIP),y)
|
||||
SRC_DIR += socket_demo
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
endif
|
||||
|
||||
endif
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
############################################################################
|
||||
# APP_Framework/Application/connection_app/socket_demo/Make.defs
|
||||
############################################################################
|
||||
ifneq ($(CONFIG_SOCKET_DEMO),)
|
||||
CONFIGURED_APPS += $(APPDIR)/../../../APP_Framework/Applications/connection_app/socket_demo/*/Make.defs
|
||||
include $(wildcard $(APPDIR)/../../../APP_Framework/Applications/connection_app/socket_demo/*/Make.defs)
|
||||
endif
|
|
@ -1,3 +1,14 @@
|
|||
SRC_FILES := lwip_tcp_socket_demo.c lwip_udp_socket_demo.c
|
||||
|
||||
|
||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
|
||||
SRC_FILES := lwip_tcp_socket_demo.c lwip_udp_socket_demo.c
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/.config
|
||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
|
||||
include $(APPDIR)/Make.defs
|
||||
CSRCS += lwip_tcp_socket_demo.c lwip_udp_socket_demo.c
|
||||
include $(APPDIR)/Application.mk
|
||||
endif
|
||||
|
||||
|
|
|
@ -19,14 +19,41 @@
|
|||
*/
|
||||
|
||||
#include <transform.h>
|
||||
|
||||
#ifdef ADD_XIZI_FETURES
|
||||
#include "sys_arch.h"
|
||||
#include <lwip/sockets.h>
|
||||
#include "lwip/sys.h"
|
||||
#endif
|
||||
|
||||
#ifdef ADD_NUTTX_FETURES
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include "stdio.h"
|
||||
#endif
|
||||
|
||||
#define TCP_DEMO_BUF_SIZE 65535
|
||||
|
||||
char tcp_socket_ip[] = {192, 168, 250, 252};
|
||||
u16_t tcp_socket_port = LWIP_TARGET_PORT;
|
||||
|
||||
#ifdef ADD_NUTTX_FETURES
|
||||
#define lw_print printf
|
||||
#define lw_notice printf
|
||||
#define lw_error printf
|
||||
|
||||
#define LWIP_DEMO_TIMES 3
|
||||
|
||||
/** Create u32_t value from bytes */
|
||||
#define LWIP_MAKEU32(a,b,c,d) (((uint32_t)((a) & 0xff) << 24) | \
|
||||
((uint32_t)((b) & 0xff) << 16) | \
|
||||
((uint32_t)((c) & 0xff) << 8) | \
|
||||
(uint32_t)((d) & 0xff))
|
||||
|
||||
#define PP_HTONL(x) ((uint32_t)(x))
|
||||
#define LWIP_TARGET_PORT 6000
|
||||
#endif
|
||||
|
||||
uint16_t tcp_socket_port = LWIP_TARGET_PORT;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
@ -63,7 +90,7 @@ static void TCPSocketRecvTask(void *arg)
|
|||
if (bind(fd, (struct sockaddr *)&tcp_addr, sizeof(struct sockaddr)) == -1)
|
||||
{
|
||||
lw_error("Unable to bind\n");
|
||||
closesocket(fd);
|
||||
close(fd);
|
||||
free(recv_buf);
|
||||
continue;
|
||||
}
|
||||
|
@ -75,7 +102,7 @@ static void TCPSocketRecvTask(void *arg)
|
|||
if (listen(fd, 5) != 0 )
|
||||
{
|
||||
lw_error("Unable to listen\n");
|
||||
closesocket(fd);
|
||||
close(fd);
|
||||
free(recv_buf);
|
||||
continue;
|
||||
}
|
||||
|
@ -97,10 +124,11 @@ static void TCPSocketRecvTask(void *arg)
|
|||
}
|
||||
}
|
||||
|
||||
closesocket(fd);
|
||||
close(fd);
|
||||
free(recv_buf);
|
||||
}
|
||||
|
||||
#ifdef ADD_XIZI_FETURES
|
||||
void TCPSocketRecvTest(int argc, char *argv[])
|
||||
{
|
||||
int result = 0;
|
||||
|
@ -120,6 +148,8 @@ void TCPSocketRecvTest(int argc, char *argv[])
|
|||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
TCPSocketRecv, TCPSocketRecvTest, TCP recv echo);
|
||||
#endif
|
||||
|
||||
|
||||
static void TCPSocketSendTask(void *arg)
|
||||
{
|
||||
|
@ -146,7 +176,7 @@ static void TCPSocketSendTask(void *arg)
|
|||
if (connect(fd, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr)))
|
||||
{
|
||||
lw_print("Unable to connect\n");
|
||||
closesocket(fd);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -158,14 +188,15 @@ static void TCPSocketSendTask(void *arg)
|
|||
snprintf(send_msg, sizeof(send_msg), "TCP test package times %d\r\n", cnt);
|
||||
sendto(fd, send_msg, strlen(send_msg), 0, (struct sockaddr*)&tcp_sock, sizeof(struct sockaddr));
|
||||
lw_notice("Send tcp msg: %s ", send_msg);
|
||||
MdelayKTask(1000);
|
||||
PrivTaskDelay(1000);
|
||||
}
|
||||
|
||||
closesocket(fd);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
#ifdef ADD_XIZI_FETURES
|
||||
void TCPSocketSendTest(int argc, char *argv[])
|
||||
{
|
||||
if(argc >= 2)
|
||||
|
@ -183,4 +214,18 @@ void TCPSocketSendTest(int argc, char *argv[])
|
|||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
|
||||
TCPSocketSend, TCPSocketSendTest, TCP send demo);
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef ADD_NUTTX_FETURES
|
||||
void tcp_recv_demo(void)
|
||||
{
|
||||
TCPSocketRecvTask(NULL);
|
||||
}
|
||||
|
||||
void tcp_send_demo(void)
|
||||
{
|
||||
TCPSocketSendTask(NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -18,13 +18,39 @@
|
|||
* @date 2022-03-21
|
||||
*/
|
||||
#include <transform.h>
|
||||
|
||||
#ifdef ADD_XIZI_FETURES
|
||||
#include "sys_arch.h"
|
||||
#include "lwip/sockets.h"
|
||||
#endif
|
||||
|
||||
#ifdef ADD_NUTTX_FETURES
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LWIP_DEMO_TIMES 3
|
||||
#define LWIP_LOCAL_PORT 6000
|
||||
|
||||
#define lw_error printf
|
||||
#define lw_notice printf
|
||||
#define lw_print printf
|
||||
|
||||
/** Create u32_t value from bytes */
|
||||
#define LWIP_MAKEU32(a,b,c,d) (((uint32_t)((a) & 0xff) << 24) | \
|
||||
((uint32_t)((b) & 0xff) << 16) | \
|
||||
((uint32_t)((c) & 0xff) << 8) | \
|
||||
(uint32_t)((d) & 0xff))
|
||||
|
||||
#define PP_HTONL(x) ((uint32_t)(x))
|
||||
|
||||
#endif
|
||||
|
||||
#define UDP_BUF_SIZE 65536
|
||||
|
||||
char udp_socket_ip[] = {192, 168, 250, 252};
|
||||
u16_t udp_socket_port = LWIP_LOCAL_PORT;
|
||||
uint16_t udp_socket_port = LWIP_LOCAL_PORT;
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
|
@ -61,7 +87,7 @@ static void UdpSocketRecvTask(void *arg)
|
|||
if(bind(fd, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr)) == -1)
|
||||
{
|
||||
lw_error("Unable to bind\n");
|
||||
closesocket(fd);
|
||||
close(fd);
|
||||
free(recv_buf);
|
||||
continue;
|
||||
}
|
||||
|
@ -81,11 +107,12 @@ static void UdpSocketRecvTask(void *arg)
|
|||
sendto(fd, recv_buf, recv_len, 0, (struct sockaddr*)&server_addr, addr_len);
|
||||
}
|
||||
|
||||
closesocket(fd);
|
||||
close(fd);
|
||||
free(recv_buf);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ADD_XIZI_FETURES
|
||||
void UdpSocketRecvTest(int argc, char *argv[])
|
||||
{
|
||||
if(argc >= 2)
|
||||
|
@ -103,6 +130,7 @@ void UdpSocketRecvTest(int argc, char *argv[])
|
|||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
UDPSocketRecv, UdpSocketRecvTest, UDP Receive DEMO);
|
||||
#endif
|
||||
|
||||
static void UdpSocketSendTask(void *arg)
|
||||
{
|
||||
|
@ -128,7 +156,7 @@ static void UdpSocketSendTask(void *arg)
|
|||
if(connect(fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr)))
|
||||
{
|
||||
lw_error("Unable to connect\n");
|
||||
closesocket(fd);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -140,13 +168,14 @@ static void UdpSocketSendTask(void *arg)
|
|||
snprintf(send_str, sizeof(send_str), "UDP test package times %d\r\n", cnt);
|
||||
sendto(fd, send_str, strlen(send_str), 0, (struct sockaddr*)&udp_sock, sizeof(struct sockaddr));
|
||||
lw_notice("Send UDP msg: %s ", send_str);
|
||||
MdelayKTask(1000);
|
||||
PrivTaskDelay(1000);
|
||||
}
|
||||
|
||||
closesocket(fd);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef ADD_XIZI_FETURES
|
||||
void UdpSocketSendTest(int argc, char *argv[])
|
||||
{
|
||||
if(argc >= 2)
|
||||
|
@ -164,4 +193,16 @@ void UdpSocketSendTest(int argc, char *argv[])
|
|||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
UDPSocketSend, UdpSocketSendTest, UDP send echo);
|
||||
#endif
|
||||
|
||||
#ifdef ADD_NUTTX_FETURES
|
||||
void udp_recv_demo(void)
|
||||
{
|
||||
UdpSocketRecvTask(NULL);
|
||||
}
|
||||
|
||||
void udp_send_demo(void)
|
||||
{
|
||||
UdpSocketSendTask(NULL);
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue