1. fixed ping exception

2. added tcp send test example
3. added udp [IP] to test udp
This commit is contained in:
wlyu 2022-01-05 20:05:07 +08:00
parent d1625c7d84
commit d1d4632292
12 changed files with 175 additions and 66 deletions

View File

@ -73,7 +73,7 @@ static void *lwip_config_test(void *param)
lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
}
void lwip_config_thread(int argc, char *argv[])
void lwip_setip_thread(int argc, char *argv[])
{
int result = 0;
pthread_t th_id;
@ -104,6 +104,25 @@ void lwip_config_thread(int argc, char *argv[])
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
eth, lwip_config_thread, eth [IP] [Netmask] [Gateway]);
SetIp, lwip_setip_thread, SetIp [IP] [Netmask] [Gateway]);
void lwip_getip_thread(int argc, char *argv[])
{
lw_pr_info("\r\n************************************************\r\n");
lw_pr_info(" Network Configuration\r\n");
lw_pr_info("************************************************\r\n");
lw_pr_info(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_ipaddr)[0], ((u8_t *)&lwip_ipaddr)[1],
((u8_t *)&lwip_ipaddr)[2], ((u8_t *)&lwip_ipaddr)[3]);
lw_pr_info(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_netmask)[0], ((u8_t *)&lwip_netmask)[1],
((u8_t *)&lwip_netmask)[2], ((u8_t *)&lwip_netmask)[3]);
lw_pr_info(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_gwaddr)[0], ((u8_t *)&lwip_gwaddr)[1],
((u8_t *)&lwip_gwaddr)[2], ((u8_t *)&lwip_gwaddr)[3]);
lw_pr_info("************************************************\r\n");
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
GetIp, lwip_getip_thread, GetIp [IP] [Netmask] [Gateway]);
#endif

View File

@ -82,10 +82,6 @@ static void *lwip_ping_test(void *param)
void lwip_ping_thread(int argc, char *argv[])
{
int result = 0;
pthread_t ping_demo_id = 0;
pthread_attr_t attr;
attr.schedparam.sched_priority = 15;
attr.stacksize = 4096;
if(argc >= 4)
{
@ -102,15 +98,13 @@ void lwip_ping_thread(int argc, char *argv[])
lw_print("lw: [%s] argc %d\n", __func__, argc);
result = PrivTaskCreate(&ping_demo_id, &attr, lwip_ping_test, NULL);
if (0 == result) {
lw_print("lwip_setip_test %d successfully!\n", ping_demo_id);
} else {
lw_print("lwip_setip_test failed! error code is %d\n", result);
}
IP4_ADDR(&ping_addr, lwip_gwaddr[0], lwip_gwaddr[1], lwip_gwaddr[2], lwip_gwaddr[3]);
ETH_BSP_Config();
lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
ping_init(&ping_addr);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
ping, lwip_ping_thread, lwip_ping_thread);
ping, lwip_ping_thread, ping [IP] 5 times);
#endif

View File

@ -21,6 +21,8 @@
#include <xiuos.h>
#include "board.h"
#include "sys_arch.h"
#include <lwip/sockets.h>
#include "lwip/sys.h"
#include "tcpecho_raw.h"
/*******************************************************************************
@ -35,11 +37,87 @@
* Variables
******************************************************************************/
char tcp_target[] = {192, 168, 250, 252};
char* tcp_send_msg = "\n\nThis one is TCP pkg. Congratulations on you.\n\n";
/*******************************************************************************
* Code
******************************************************************************/
void lwip_tcp_run(void)
static void lwip_tcp_send_thread(void *arg)
{
int cnt = 5;
lw_print("lwip_tcp_send_thread start.\n");
int sock_tcp_send_once = -1;
sock_tcp_send_once = socket(AF_INET, SOCK_STREAM, 0);
if (sock_tcp_send_once < 0)
{
lw_print("Socket error\n");
goto __exit;
}
struct sockaddr_in tcp_sock;
tcp_sock.sin_family = AF_INET;
tcp_sock.sin_port = htons(TARGET_PORT_CLIENT);
tcp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(tcp_target[0],tcp_target[1],tcp_target[2],tcp_target[3]));
memset(&(tcp_sock.sin_zero), 0, sizeof(tcp_sock.sin_zero));
if (connect(sock_tcp_send_once, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr)))
{
lw_print("Unable to connect\n");
goto __exit;
}
lw_print("tcp connect success, start to send.\n");
lw_print("\n\nTarget Port:%d\n\n", tcp_sock.sin_port);
while (cnt --)
{
lw_print("Lwip client is running.\n");
sendto(sock_tcp_send_once,tcp_send_msg,
strlen(tcp_send_msg),0,
(struct sockaddr*)&tcp_sock,
sizeof(struct sockaddr));
lw_print("Send tcp msg: %s ", tcp_send_msg);
MdelayKTask(1000);
}
return;
__exit:
if (sock_tcp_send_once >= 0) closesocket(sock_tcp_send_once);
return;
}
void
lwip_tcp_send_init(void)
{
sys_thread_new("tcp send", lwip_tcp_send_thread, NULL, 4096, 25);
}
void lwip_tcp_client_run(int argc, char *argv[])
{
if(argc == 2)
{
lw_print("lw: [%s] gw %s\n", __func__, argv[1]);
sscanf(argv[1], "%d.%d.%d.%d", &tcp_target[0], &tcp_target[1], &tcp_target[2], &tcp_target[3]);
}
ETH_BSP_Config();
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
lwip_tcp_send_init();
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
LwTcpClient, lwip_tcp_client_run, TCP Client);
void lwip_tcp_server_run(void)
{
ETH_BSP_Config();
lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
@ -47,5 +125,5 @@ void lwip_tcp_run(void)
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
LwTcpTest, lwip_tcp_run, TCP raw echo);
LwTcpServer, lwip_tcp_server_run, TCP server);

View File

@ -40,6 +40,7 @@
******************************************************************************/
static struct udp_pcb *udpecho_raw_pcb;
char udp_target[] = {192, 168, 250, 252};
/*******************************************************************************
* Code
@ -52,12 +53,18 @@ static void *lwip_udp_test(void* param)
UdpEchoInit();
}
void lwip_udp_thread(void)
void lwip_udp_thread(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_target[0], &udp_target[1], &udp_target[2], &udp_target[3]);
}
attr.schedparam.sched_priority = UDP_TASK_PRIO;
attr.stacksize = UDP_TASK_STACK_SIZE;
@ -69,7 +76,7 @@ void lwip_udp_thread(void)
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
LwUdpEcho, lwip_udp_thread, UDP send echo);
static void

View File

@ -69,12 +69,12 @@
/** ping receive timeout - in milliseconds */
#ifndef PING_RCV_TIMEO
#define PING_RCV_TIMEO 1000
#define PING_RCV_TIMEO 2000
#endif
/** ping delay - in milliseconds */
#ifndef PING_DELAY
#define PING_DELAY 3000
#define PING_DELAY 1000
#endif
/** ping identifier - must fit on a u16_t */
@ -275,7 +275,7 @@ ping_thread(void *arg)
lw_print("lw: [%s] ping start!\n", __func__);
ret = lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
LWIP_ASSERT("setting receive timeout failed", ret == 0);
LWIP_ASSERT("setting receive timeout failed", ret != 0);
LWIP_UNUSED_ARG(ret);
while (cnt --) {
@ -363,13 +363,21 @@ ping_send(struct raw_pcb *raw, const ip_addr_t *addr)
static void
ping_timeout(void *arg)
{
static int cnt = 3;
struct raw_pcb *pcb = (struct raw_pcb*)arg;
LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
ping_send(pcb, ping_target);
sys_timeout(PING_DELAY, ping_timeout, pcb);
if(cnt -- > 0)
{
sys_timeout(PING_DELAY, ping_timeout, pcb);
}
else
{
cnt = 3;
}
}
static void

View File

@ -7,7 +7,7 @@
* PING_USE_SOCKETS: Set to 1 to use sockets, otherwise the raw api is used
*/
#ifndef PING_USE_SOCKETS
#define PING_USE_SOCKETS LWIP_SOCKET
#define PING_USE_SOCKETS 0 //LWIP_SOCKET
#endif
void ping_init(const ip_addr_t* ping_addr);

View File

@ -287,8 +287,7 @@ tcpecho_raw_init(void)
tcpecho_raw_pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
if (tcpecho_raw_pcb != NULL) {
err_t err;
//tst by wly
err = tcp_bind(tcpecho_raw_pcb, IP_ANY_TYPE, 4840);
err = tcp_bind(tcpecho_raw_pcb, IP_ANY_TYPE, LWIP_TEST_TCP_PORT);
if (err == ERR_OK) {
tcpecho_raw_pcb = tcp_listen(tcpecho_raw_pcb);
tcp_accept(tcpecho_raw_pcb, tcpecho_raw_accept);

View File

@ -1,8 +1,8 @@
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* 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,
@ -11,25 +11,27 @@
* 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.
* 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
* 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.
*
*
*/
#ifndef LWIP_TCPECHO_RAW_H
#define LWIP_TCPECHO_RAW_H
#define LWIP_TEST_TCP_PORT 4840
void tcpecho_raw_init(void);
#endif /* LWIP_TCPECHO_RAW_H */

View File

@ -68,7 +68,7 @@
#define RECV_DATA (1024)
char* send_msg = "\n\nThis one is UDP pkg. Congratulations on you.\n\n";
char* udp_send_msg = "\n\nThis one is UDP pkg. Congratulations on you.\n\n";
static void UdpEchoThreadServer(void *arg)
{
@ -150,7 +150,7 @@ static void UdpEchoThreadClient(void *arg)
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(IP_ADDR0_SERVER,IP_ADDR1_SERVER,IP_ADDR2_SERVER,IP_ADDR3_SERVER));
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)))
@ -166,12 +166,12 @@ static void UdpEchoThreadClient(void *arg)
{
KPrintf("Lwip client is running.\n");
sendto(sock_udp_send_once,send_msg,
strlen(send_msg),0,
sendto(sock_udp_send_once,udp_send_msg,
strlen(udp_send_msg),0,
(struct sockaddr*)&udp_sock,
sizeof(struct sockaddr));
KPrintf("Send UDP msg: %s ", send_msg);
lw_pr_info("Send UDP msg: %s ", udp_send_msg);
MdelayKTask(1000);
}

View File

@ -1,8 +1,8 @@
/*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* 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,
@ -11,21 +11,21 @@
* 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.
* 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
* 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>
*
*/
@ -34,6 +34,8 @@
#ifndef LWIP_TCPECHO_H
#define LWIP_TCPECHO_H
extern char udp_target[];
void UdpEchoInit(void);

View File

@ -107,7 +107,7 @@ a lot of data that needs to be copied, this should be set high. */
#define TCP_SND_QUEUELEN (8* TCP_SND_BUF/TCP_MSS)
/* TCP receive window. */
#define TCP_WND 16000//(12*TCP_MSS)
#define TCP_WND (12*TCP_MSS)
/* ---------- ICMP options ---------- */
@ -254,7 +254,7 @@ typedef unsigned int nfds_t;
#define lw_print //KPrintf
#define lw_trace() //KPrintf("lw: [%s][%d] passed!\n", __func__, __LINE__)
#define lw_error() //KPrintf("lw: [%s][%d] failed!\n", __func__, __LINE__)
#define lw_pr_info KPrintf
#endif /* __LWIPOPTS_H__ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View File

@ -516,16 +516,16 @@ void lwip_config_net(char *ip, char *mask, char *gw)
netif_set_default(&gnetif);
netif_set_up(&gnetif);
lw_print("\r\n************************************************\r\n");
lw_print(" Network Configuration\r\n");
lw_print("************************************************\r\n");
lw_print(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&net_ipaddr)[0], ((u8_t *)&net_ipaddr)[1],
lw_pr_info("\r\n************************************************\r\n");
lw_pr_info(" Network Configuration\r\n");
lw_pr_info("************************************************\r\n");
lw_pr_info(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&net_ipaddr)[0], ((u8_t *)&net_ipaddr)[1],
((u8_t *)&net_ipaddr)[2], ((u8_t *)&net_ipaddr)[3]);
lw_print(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&net_netmask)[0], ((u8_t *)&net_netmask)[1],
lw_pr_info(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&net_netmask)[0], ((u8_t *)&net_netmask)[1],
((u8_t *)&net_netmask)[2], ((u8_t *)&net_netmask)[3]);
lw_print(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&net_gw)[0], ((u8_t *)&net_gw)[1],
lw_pr_info(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&net_gw)[0], ((u8_t *)&net_gw)[1],
((u8_t *)&net_gw)[2], ((u8_t *)&net_gw)[3]);
lw_print("************************************************\r\n");
lw_pr_info("************************************************\r\n");
lwip_config_input(&gnetif);
}