optimize lwip test

This commit is contained in:
wlyu 2022-01-07 17:02:55 +08:00
parent e2e127bb0e
commit a49eeaf548
15 changed files with 224 additions and 1605 deletions

View File

@ -1,6 +0,0 @@
#ifndef __LWIP_DEMO_H__
#define __LWIP_DEMO_H__
void *eth_input_thread(void *param);
#endif /* __LWIP_DEMO_H__ */

View File

@ -1,172 +0,0 @@
/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2019 NXP
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/*******************************************************************************
* Includes
******************************************************************************/
#include "lwip/opt.h"
#if LWIP_IPV4 && LWIP_DHCP
#include "lwip/timeouts.h"
#include "lwip/ip_addr.h"
#include "lwip/init.h"
#include "lwip/dhcp.h"
#include "lwip/prot/dhcp.h"
#include "netif/ethernet.h"
#include "enet_ethernetif.h"
#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_gpio.h"
#include "fsl_iomuxc.h"
#include "sys_arch.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Prints DHCP status of the interface when it has changed from last status.
*
* @param netif network interface structure
*/
int print_dhcp_state(struct netif *netif)
{
static u8_t dhcp_last_state = DHCP_STATE_OFF;
struct dhcp *dhcp = netif_dhcp_data(netif);
if (dhcp == NULL)
{
dhcp_last_state = DHCP_STATE_OFF;
}
else if (dhcp_last_state != dhcp->state)
{
dhcp_last_state = dhcp->state;
lw_pr_info(" DHCP state : ");
switch (dhcp_last_state)
{
case DHCP_STATE_OFF:
lw_pr_info("OFF");
break;
case DHCP_STATE_REQUESTING:
lw_pr_info("REQUESTING");
break;
case DHCP_STATE_INIT:
lw_pr_info("INIT");
break;
case DHCP_STATE_REBOOTING:
lw_pr_info("REBOOTING");
break;
case DHCP_STATE_REBINDING:
lw_pr_info("REBINDING");
break;
case DHCP_STATE_RENEWING:
lw_pr_info("RENEWING");
break;
case DHCP_STATE_SELECTING:
lw_pr_info("SELECTING");
break;
case DHCP_STATE_INFORMING:
lw_pr_info("INFORMING");
break;
case DHCP_STATE_CHECKING:
lw_pr_info("CHECKING");
break;
case DHCP_STATE_BOUND:
lw_pr_info("BOUND");
break;
case DHCP_STATE_BACKING_OFF:
lw_pr_info("BACKING_OFF");
break;
default:
lw_pr_info("%u", dhcp_last_state);
assert(0);
break;
}
lw_pr_info("\r\n");
if (dhcp_last_state == DHCP_STATE_BOUND)
{
lw_pr_info("\r\n IPv4 Address : %s\r\n", ipaddr_ntoa(&netif->ip_addr));
lw_pr_info(" IPv4 Subnet mask : %s\r\n", ipaddr_ntoa(&netif->netmask));
lw_pr_info(" IPv4 Gateway : %s\r\n\r\n", ipaddr_ntoa(&netif->gw));
return 1;
}
}
return 0;
}
/*!
* @brief Main function.
*/
void lwip_dhcp_test(void)
{
static int flag = 0;
char ip_addr[4] = {0, 0, 0, 0};
ETH_BSP_Config();
lwip_config_net(ip_addr, ip_addr, ip_addr);
is_lwip_test = 1;
dhcp_start(&gnetif);
lw_pr_info("\r\n************************************************\r\n");
lw_pr_info(" DHCP example\r\n");
lw_pr_info("************************************************\r\n");
while (1)
{
/* Poll the driver, get any outstanding frames */
ethernetif_input(&gnetif);
/* Handle all system timeouts for all core protocols */
sys_check_timeouts();
/* Print DHCP progress */
if(print_dhcp_state(&gnetif))
{
sscanf(ipaddr_ntoa(&gnetif.ip_addr), "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1],
&lwip_ipaddr[2], &lwip_ipaddr[3]);
sscanf(ipaddr_ntoa(&gnetif.netmask), "%d.%d.%d.%d", &lwip_netmask[0], &lwip_netmask[1],
&lwip_netmask[2], &lwip_netmask[3]);
sscanf(ipaddr_ntoa(&gnetif.gw), "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1],
&lwip_gwaddr[2], &lwip_gwaddr[3]);
break;
}
}
is_lwip_test = 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
getip, lwip_dhcp_test, DHCP_Test);
#endif

View File

@ -1,124 +0,0 @@
/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2019 NXP
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/*
* Copyright (c) 2021 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 lwip_ping_demo.c
* @brief Demo for ping function
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021.12.15
*/
/*******************************************************************************
* Includes
******************************************************************************/
#include "lwip/opt.h"
#if LWIP_IPV4 && LWIP_RAW
#include "ping.h"
#include "lwip/timeouts.h"
#include "lwip/init.h"
#include "netif/ethernet.h"
#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include <transform.h>
#include <sys_arch.h>
#include "connect_ethernet.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
char test_ip_addr[] = {192, 168, 250, 253};
char test_net_mask[] = {255, 255, 255, 0};
char test_gw_addr[] = {192, 168, 250, 252};
ip4_addr_t ping_addr;
/*******************************************************************************
* Code
******************************************************************************/
static void *lwip_ping_test(void *param)
{
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);
}
void lwip_ping_thread(int argc, char *argv[])
{
int result = 0;
if(argc >= 4)
{
lw_print("lw: [%s] ip %s mask %s gw %s\n", __func__, argv[1], argv[2], argv[3]);
sscanf(argv[1], "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1], &lwip_ipaddr[2], &lwip_ipaddr[3]);
sscanf(argv[2], "%d.%d.%d.%d", &lwip_netmask[0], &lwip_netmask[1], &lwip_netmask[2], &lwip_netmask[3]);
sscanf(argv[3], "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1], &lwip_gwaddr[2], &lwip_gwaddr[3]);
}
else if(argc == 2)
{
lw_print("lw: [%s] gw %s\n", __func__, argv[1]);
sscanf(argv[1], "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1], &lwip_gwaddr[2], &lwip_gwaddr[3]);
}
lw_print("lw: [%s] argc %d\n", __func__, argc);
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, ping [IP] 3 times);
int lwip_dns_test(int argc, char **argv)
{
if (argc <= 1)
{
lw_pr_info("Please input: ping <host address>\n");
return 0;
}
get_url_ip(argv[1]);
return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(2),
dns, lwip_dns_test, dns [url]);
#endif

View File

@ -1,544 +0,0 @@
/**
* @file
* Ping sender module
*
*/
/*
* 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.
*
*/
/**
* This is an example of a "ping" sender (with raw API and socket API).
* It can be used as a start point to maintain opened a network connection, or
* like a network "watchdog" for your device.
*
*/
#include <xs_ktask.h>
#include "lwip/opt.h"
#if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
#include "ping.h"
#include "lwip/mem.h"
#include "lwip/raw.h"
#include "lwip/icmp.h"
#include "lwip/netif.h"
#include "lwip/sys.h"
#include "lwip/timeouts.h"
#include "lwip/inet_chksum.h"
#include "lwip/prot/ip4.h"
#include "lwip/netdb.h"
#include "lwip/prot/ip.h"
#if PING_USE_SOCKETS
#include "lwip/sockets.h"
#include "lwip/inet.h"
#include <string.h>
#endif /* PING_USE_SOCKETS */
/**
* PING_DEBUG: Enable debugging for PING.
*/
#ifndef PING_DEBUG
#define PING_DEBUG LWIP_DBG_ON
#endif
/** ping receive timeout - in milliseconds */
#ifndef PING_RCV_TIMEO
#define PING_RCV_TIMEO 2000
#endif
/** ping delay - in milliseconds */
#ifndef PING_DELAY
#define PING_DELAY 1000
#endif
/** ping identifier - must fit on a u16_t */
#ifndef PING_ID
#define PING_ID 0xAFAF
#endif
/** ping additional data size to include in the packet */
#ifndef PING_DATA_SIZE
#define PING_DATA_SIZE 32
#endif
/** ping result action - no default action */
#ifndef PING_RESULT
#define PING_RESULT(ping_ok)
#endif
/* ping variables */
static const ip_addr_t* ping_target;
static u16_t ping_seq_num;
#ifdef LWIP_DEBUG
static u32_t ping_time;
#endif /* LWIP_DEBUG */
#if !PING_USE_SOCKETS
static struct raw_pcb *ping_pcb;
#endif /* PING_USE_SOCKETS */
#define PING_THREAD_STACKSIZE 4096
#define PING_THREAD_PRIO 15
/** Prepare a echo ICMP request */
static void
ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
{
size_t i;
size_t data_len = len - sizeof(struct icmp_echo_hdr);
ICMPH_TYPE_SET(iecho, ICMP_ECHO);
ICMPH_CODE_SET(iecho, 0);
iecho->chksum = 0;
iecho->id = PING_ID;
iecho->seqno = lwip_htons(++ping_seq_num);
/* fill the additional data buffer with some data */
for(i = 0; i < data_len; i++) {
((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
}
iecho->chksum = inet_chksum(iecho, len);
}
#if PING_USE_SOCKETS
/* Ping using the socket ip */
static err_t
ping_send(int s, const ip_addr_t *addr)
{
int err;
struct icmp_echo_hdr *iecho;
struct sockaddr_storage to;
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
#if LWIP_IPV6
if(IP_IS_V6(addr) && !ip6_addr_isipv4mappedipv6(ip_2_ip6(addr))) {
/* todo: support ICMP6 echo */
return ERR_VAL;
}
#endif /* LWIP_IPV6 */
iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size);
if (!iecho) {
return ERR_MEM;
}
ping_prepare_echo(iecho, (u16_t)ping_size);
#if LWIP_IPV4
if(IP_IS_V4(addr)) {
struct sockaddr_in *to4 = (struct sockaddr_in*)&to;
to4->sin_len = sizeof(to4);
to4->sin_family = AF_INET;
inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr));
}
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
if(IP_IS_V6(addr)) {
struct sockaddr_in6 *to6 = (struct sockaddr_in6*)&to;
to6->sin6_len = sizeof(to6);
to6->sin6_family = AF_INET6;
inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr));
}
#endif /* LWIP_IPV6 */
err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
mem_free(iecho);
return (err ? ERR_OK : ERR_VAL);
}
static void
ping_recv(int s)
{
char buf[64];
int len;
struct sockaddr_storage from;
int fromlen = sizeof(from);
while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) {
ip_addr_t fromaddr;
memset(&fromaddr, 0, sizeof(fromaddr));
#if LWIP_IPV4
if(from.ss_family == AF_INET) {
struct sockaddr_in *from4 = (struct sockaddr_in*)&from;
inet_addr_to_ip4addr(ip_2_ip4(&fromaddr), &from4->sin_addr);
IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V4);
}
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
if(from.ss_family == AF_INET6) {
struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from;
inet6_addr_to_ip6addr(ip_2_ip6(&fromaddr), &from6->sin6_addr);
IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V6);
}
#endif /* LWIP_IPV6 */
LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
ip_addr_debug_print_val(PING_DEBUG, fromaddr);
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time)));
/* todo: support ICMP6 echo */
#if LWIP_IPV4
if (IP_IS_V4_VAL(fromaddr)) {
struct ip_hdr *iphdr;
struct icmp_echo_hdr *iecho;
iphdr = (struct ip_hdr *)buf;
iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
/* do some ping result processing */
PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
return;
} else {
LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n"));
}
}
#endif /* LWIP_IPV4 */
}
fromlen = sizeof(from);
}
if (len == 0) {
LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", (sys_now()-ping_time)));
}
/* do some ping result processing */
PING_RESULT(0);
}
static void
ping_thread(void *arg)
{
int s;
int ret;
int cnt = TEST_LWIP_TIMES;
#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
int timeout = PING_RCV_TIMEO;
#else
struct timeval timeout;
timeout.tv_sec = PING_RCV_TIMEO/1000;
timeout.tv_usec = (PING_RCV_TIMEO%1000)*1000;
#endif
LWIP_UNUSED_ARG(arg);
lw_print("lw: [%s] start...\n", __func__);
#if LWIP_IPV6
if(IP_IS_V4(ping_target) || ip6_addr_isipv4mappedipv6(ip_2_ip6(ping_target))) {
s = lwip_socket(AF_INET6, SOCK_RAW, IP_PROTO_ICMP);
} else {
s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6);
}
#else
s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP);
#endif
if (s < 0) {
lw_print("lw: [%s] ping failed %d!\n", __func__, s);
return;
}
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_UNUSED_ARG(ret);
while (cnt --) {
if (ping_send(s, ping_target) == ERR_OK) {
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
ip_addr_debug_print(PING_DEBUG, ping_target);
LWIP_DEBUGF( PING_DEBUG, ("\n"));
#ifdef LWIP_DEBUG
ping_time = sys_now();
#endif /* LWIP_DEBUG */
ping_recv(s);
} else {
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
ip_addr_debug_print(PING_DEBUG, ping_target);
LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
}
sys_msleep(PING_DELAY);
}
lwip_close(s);
}
#else /* PING_USE_SOCKETS */
/* Ping using the raw ip */
static u8_t
ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr)
{
struct icmp_echo_hdr *iecho;
LWIP_UNUSED_ARG(arg);
LWIP_UNUSED_ARG(pcb);
LWIP_UNUSED_ARG(addr);
LWIP_ASSERT("p != NULL", p != NULL);
if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
pbuf_remove_header(p, PBUF_IP_HLEN) == 0) {
iecho = (struct icmp_echo_hdr *)p->payload;
if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
ip_addr_debug_print(PING_DEBUG, addr);
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time)));
/* do some ping result processing */
PING_RESULT(1);
pbuf_free(p);
return 1; /* eat the packet */
}
/* not eaten, restore original packet */
/* Changed to the "_force" version because of LPC zerocopy pbufs */
pbuf_add_header_force(p, PBUF_IP_HLEN);
}
return 0; /* don't eat the packet */
}
static void
ping_send(struct raw_pcb *raw, const ip_addr_t *addr)
{
struct pbuf *p;
struct icmp_echo_hdr *iecho;
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
ip_addr_debug_print(PING_DEBUG, addr);
LWIP_DEBUGF( PING_DEBUG, ("\n"));
LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
if (!p) {
return;
}
if ((p->len == p->tot_len) && (p->next == NULL)) {
iecho = (struct icmp_echo_hdr *)p->payload;
ping_prepare_echo(iecho, (u16_t)ping_size);
raw_sendto(raw, p, addr);
#ifdef LWIP_DEBUG
ping_time = sys_now();
#endif /* LWIP_DEBUG */
}
pbuf_free(p);
}
static void
ping_timeout(void *arg)
{
static int cnt = TEST_LWIP_TIMES;
struct raw_pcb *pcb = (struct raw_pcb*)arg;
LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
ping_send(pcb, ping_target);
if(cnt -- > 0)
{
sys_timeout(PING_DELAY, ping_timeout, pcb);
}
else
{
cnt = TEST_LWIP_TIMES;
}
}
static void
ping_raw_init(void)
{
ping_pcb = raw_new(IP_PROTO_ICMP);
LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
raw_recv(ping_pcb, ping_recv, NULL);
raw_bind(ping_pcb, IP_ADDR_ANY);
sys_timeout(PING_DELAY, ping_timeout, ping_pcb);
}
void
ping_send_now(void)
{
LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
ping_send(ping_pcb, ping_target);
}
#endif /* PING_USE_SOCKETS */
void
ping_init(const ip_addr_t* ping_addr)
{
sys_thread_t th;
ping_target = ping_addr;
#if PING_USE_SOCKETS
th = sys_thread_new("ping_thread", ping_thread, NULL, PING_THREAD_STACKSIZE, PING_THREAD_PRIO);
lw_print("lw: [%s] new thread %d addr %#x\n", __func__, th, (*ping_addr).addr);
#else /* PING_USE_SOCKETS */
ping_raw_init();
#endif /* PING_USE_SOCKETS */
}
int lwip_ping_send(int s, ip_addr_t *addr, int size)
{
int err;
struct icmp_echo_hdr *iecho;
struct sockaddr_in to;
int ping_size = sizeof(struct icmp_echo_hdr) + size;
LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
iecho = malloc(ping_size);
if (iecho == NULL)
{
return ERR_MEM;
}
ping_prepare_echo(iecho, (u16_t) ping_size);
to.sin_len = sizeof(to);
to.sin_family = AF_INET;
#if LWIP_IPV4 && LWIP_IPV6
to.sin_addr.s_addr = addr->u_addr.ip4.addr;
#elif LWIP_IPV4
to.sin_addr.s_addr = addr->addr;
#elif LWIP_IPV6
#error Not supported IPv6.
#endif
err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*) &to, sizeof(to));
free(iecho);
return (err == ping_size ? ERR_OK : ERR_VAL);
}
int lwip_ping_recv(int s, int *ttl)
{
char buf[64];
int fromlen = sizeof(struct sockaddr_in), len;
struct sockaddr_in from;
struct ip_hdr *iphdr;
struct icmp_echo_hdr *iecho;
while ((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*) &from, (socklen_t*) &fromlen)) > 0)
{
if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr)))
{
iphdr = (struct ip_hdr *) buf;
iecho = (struct icmp_echo_hdr *) (buf + (IPH_HL(iphdr) * 4));
if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
{
*ttl = iphdr->_ttl;
return len;
}
}
}
return len;
}
int get_url_ip(char* url)
{
#if LWIP_VERSION_MAJOR >= 2U
struct timeval timeout = { PING_RCV_TIMEO / TICK_PER_SECOND, PING_RCV_TIMEO % TICK_PER_SECOND };
#else
int timeout = PING_RCV_TIMEO * 1000UL / TICK_PER_SECOND;
#endif
int cnt = TEST_LWIP_TIMES;
int s, ttl, recv_len;
ip_addr_t target_addr;
struct addrinfo hint, *res = NULL;
struct sockaddr_in *h = NULL;
struct in_addr ina;
memset(&hint, 0, sizeof(hint));
/* convert URL to IP */
if (lwip_getaddrinfo(url, NULL, &hint, &res) != 0)
{
lw_pr_info("ping: unknown host %s\n", url);
return -1;
}
memcpy(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
memcpy(&ina, &h->sin_addr, sizeof(ina));
lwip_freeaddrinfo(res);
if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
{
lw_pr_info("ping: unknown host %s\n", url);
return -2;
}
/* new a socket */
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
{
lw_pr_info("ping: create socket failed\n");
return -3;
}
lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
while (cnt --)
{
if (ping_send(s, &target_addr) == ERR_OK)
{
#ifdef LWIP_DEBUG
ping_time = sys_now();
#endif /* LWIP_DEBUG */
if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0)
{
lw_pr_info("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", recv_len, inet_ntoa(ina), cnt,
ttl, sys_now() - ping_time);
}
else
{
lw_pr_info("From %s icmp_seq=%d timeout\n", inet_ntoa(ina), cnt);
}
}
sys_msleep(PING_DELAY);
}
lwip_close(s);
return 0;
}
#endif /* LWIP_RAW */

View File

@ -1,21 +0,0 @@
#ifndef LWIP_PING_H
#define LWIP_PING_H
#include "lwip/ip_addr.h"
/**
* 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
#endif
void ping_init(const ip_addr_t* ping_addr);
#if !PING_USE_SOCKETS
void ping_send_now(void);
#endif /* !PING_USE_SOCKETS */
int get_url_ip(char* url);
#endif /* LWIP_PING_H */

View File

@ -1,302 +0,0 @@
/*
* Copyright (c) 2001-2004 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 and a contribution to the lwIP TCP/IP stack.
*
* Credits go to Adam Dunkels (and the current maintainers) of this software.
*
* Christiaan Simons rewrote this file to get a more stable echo example.
*/
/**
* @file
* TCP echo server example using raw API.
*
* Echos all bytes sent by connecting client,
* and passively closes when client is done.
*
*/
#include "lwip/opt.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "tcp_socket.h"
#if LWIP_TCP && LWIP_CALLBACK_API
static struct tcp_pcb *tcpecho_raw_pcb;
enum tcpecho_raw_states
{
ES_NONE = 0,
ES_ACCEPTED,
ES_RECEIVED,
ES_CLOSING
};
struct tcpecho_raw_state
{
u8_t state;
u8_t retries;
struct tcp_pcb *pcb;
/* pbuf (chain) to recycle */
struct pbuf *p;
};
static void
tcpecho_raw_free(struct tcpecho_raw_state *es)
{
if (es != NULL) {
if (es->p) {
/* free the buffer chain if present */
pbuf_free(es->p);
}
mem_free(es);
}
}
static void
tcpecho_raw_close(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es)
{
tcp_arg(tpcb, NULL);
tcp_sent(tpcb, NULL);
tcp_recv(tpcb, NULL);
tcp_err(tpcb, NULL);
tcp_poll(tpcb, NULL, 0);
tcpecho_raw_free(es);
tcp_close(tpcb);
}
static void
tcpecho_raw_send(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es)
{
struct pbuf *ptr;
err_t wr_err = ERR_OK;
while ((wr_err == ERR_OK) &&
(es->p != NULL) &&
(es->p->len <= tcp_sndbuf(tpcb))) {
ptr = es->p;
/* enqueue data for transmission */
wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1);
if (wr_err == ERR_OK) {
u16_t plen;
plen = ptr->len;
/* continue with next pbuf in chain (if any) */
es->p = ptr->next;
if(es->p != NULL) {
/* new reference! */
pbuf_ref(es->p);
}
/* chop first pbuf from chain */
pbuf_free(ptr);
/* we can read more data now */
tcp_recved(tpcb, plen);
} else if(wr_err == ERR_MEM) {
/* we are low on memory, try later / harder, defer to poll */
es->p = ptr;
} else {
/* other problem ?? */
}
}
}
static void
tcpecho_raw_error(void *arg, err_t err)
{
struct tcpecho_raw_state *es;
LWIP_UNUSED_ARG(err);
es = (struct tcpecho_raw_state *)arg;
tcpecho_raw_free(es);
}
static err_t
tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
{
err_t ret_err;
struct tcpecho_raw_state *es;
es = (struct tcpecho_raw_state *)arg;
if (es != NULL) {
if (es->p != NULL) {
/* there is a remaining pbuf (chain) */
tcpecho_raw_send(tpcb, es);
} else {
/* no remaining pbuf (chain) */
if(es->state == ES_CLOSING) {
tcpecho_raw_close(tpcb, es);
}
}
ret_err = ERR_OK;
} else {
/* nothing to be done */
tcp_abort(tpcb);
ret_err = ERR_ABRT;
}
return ret_err;
}
static err_t
tcpecho_raw_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
{
struct tcpecho_raw_state *es;
LWIP_UNUSED_ARG(len);
es = (struct tcpecho_raw_state *)arg;
es->retries = 0;
if(es->p != NULL) {
/* still got pbufs to send */
tcp_sent(tpcb, tcpecho_raw_sent);
tcpecho_raw_send(tpcb, es);
} else {
/* no more pbufs to send */
if(es->state == ES_CLOSING) {
tcpecho_raw_close(tpcb, es);
}
}
return ERR_OK;
}
static err_t
tcpecho_raw_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
struct tcpecho_raw_state *es;
err_t ret_err;
LWIP_ASSERT("arg != NULL",arg != NULL);
es = (struct tcpecho_raw_state *)arg;
if (p == NULL) {
/* remote host closed connection */
es->state = ES_CLOSING;
if(es->p == NULL) {
/* we're done sending, close it */
tcpecho_raw_close(tpcb, es);
} else {
/* we're not done yet */
tcpecho_raw_send(tpcb, es);
}
ret_err = ERR_OK;
} else if(err != ERR_OK) {
/* cleanup, for unknown reason */
if (p != NULL) {
pbuf_free(p);
}
ret_err = err;
}
else if(es->state == ES_ACCEPTED) {
/* first data chunk in p->payload */
es->state = ES_RECEIVED;
/* store reference to incoming pbuf (chain) */
es->p = p;
tcpecho_raw_send(tpcb, es);
ret_err = ERR_OK;
} else if (es->state == ES_RECEIVED) {
/* read some more data */
if(es->p == NULL) {
es->p = p;
tcpecho_raw_send(tpcb, es);
} else {
struct pbuf *ptr;
/* chain pbufs to the end of what we recv'ed previously */
ptr = es->p;
pbuf_cat(ptr,p);
}
ret_err = ERR_OK;
} else {
/* unkown es->state, trash data */
tcp_recved(tpcb, p->tot_len);
pbuf_free(p);
ret_err = ERR_OK;
}
return ret_err;
}
static err_t
tcpecho_raw_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
{
err_t ret_err;
struct tcpecho_raw_state *es;
LWIP_UNUSED_ARG(arg);
if ((err != ERR_OK) || (newpcb == NULL)) {
return ERR_VAL;
}
/* Unless this pcb should have NORMAL priority, set its priority now.
When running out of pcbs, low priority pcbs can be aborted to create
new pcbs of higher priority. */
tcp_setprio(newpcb, TCP_PRIO_MIN);
es = (struct tcpecho_raw_state *)mem_malloc(sizeof(struct tcpecho_raw_state));
if (es != NULL) {
es->state = ES_ACCEPTED;
es->pcb = newpcb;
es->retries = 0;
es->p = NULL;
/* pass newly allocated es to our callbacks */
tcp_arg(newpcb, es);
tcp_recv(newpcb, tcpecho_raw_recv);
tcp_err(newpcb, tcpecho_raw_error);
tcp_poll(newpcb, tcpecho_raw_poll, 0);
tcp_sent(newpcb, tcpecho_raw_sent);
ret_err = ERR_OK;
} else {
ret_err = ERR_MEM;
}
return ret_err;
}
void
tcpecho_raw_init(void)
{
tcpecho_raw_pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
if (tcpecho_raw_pcb != NULL) {
err_t err;
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);
} else {
/* abort? output diagnostic? */
}
} else {
/* abort? output diagnostic? */
}
}
#endif /* LWIP_TCP && LWIP_CALLBACK_API */

View File

@ -1,37 +0,0 @@
/*
* Copyright (c) 2001-2004 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.
*
*/
#ifndef __LWIP_TCP_SOCKET_H_
#define __LWIP_TCP_SOCKET_H_
#define LWIP_TEST_TCP_PORT 4840
void tcpecho_raw_init(void);
#endif /* __LWIP_TCP_SOCKET_H_ */

View File

@ -37,7 +37,6 @@
******************************************************************************/
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
@ -127,10 +126,12 @@ static void tcp_send_demo(void *arg)
{
int cnt = TEST_LWIP_TIMES;
lw_print("tcp_send_demo start.\n");
int fd = -1;
char send_msg[128];
int sock_tcp_send_once = -1;
sock_tcp_send_once = socket(AF_INET, SOCK_STREAM, 0);
if (sock_tcp_send_once < 0)
memset(send_msg, 0, sizeof(send_msg));
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
lw_print("Socket error\n");
goto __exit;
@ -142,7 +143,7 @@ static void tcp_send_demo(void *arg)
tcp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(tcp_socket_ip[0],tcp_socket_ip[1],tcp_socket_ip[2],tcp_socket_ip[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)))
if (connect(fd, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr)))
{
lw_print("Unable to connect\n");
goto __exit;
@ -154,20 +155,15 @@ static void tcp_send_demo(void *arg)
while (cnt --)
{
lw_print("Lwip client is running.\n");
sendto(sock_tcp_send_once,tcp_socket_str,
strlen(tcp_socket_str),0,
(struct sockaddr*)&tcp_sock,
sizeof(struct sockaddr));
lw_print("Send tcp msg: %s ", tcp_socket_str);
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_print("Send tcp msg: %s ", send_msg);
MdelayKTask(1000);
}
__exit:
if (sock_tcp_send_once >= 0)
closesocket(sock_tcp_send_once);
if (fd >= 0)
closesocket(fd);
return;
}

View File

@ -11,7 +11,7 @@
*/
/**
* @file lwip_udp_demo.c
* @file lwip_udp_socket_demo.c
* @brief One UDP demo based on LwIP
* @version 1.0
* @author AIIT XUOS Lab
@ -50,8 +50,6 @@ char udp_socket_ip[] = {192, 168, 250, 252};
#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");
@ -138,10 +136,13 @@ SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) |
static void udp_send_demo(void *arg)
{
int cnt = TEST_LWIP_TIMES;
char send_str[128];
lw_print("udp_send_demo start.\n");
int socket_fd = -1;
memset(send_str, 0, sizeof(send_str));
socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_fd < 0)
{
@ -166,13 +167,9 @@ static void udp_send_demo(void *arg)
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);
snprintf(send_str, sizeof(send_str), "UDP test package times %d\r\n", cnt);
sendto(socket_fd, send_str, strlen(send_str), 0, (struct sockaddr*)&udp_sock, sizeof(struct sockaddr));
lw_pr_info("Send UDP msg: %s ", send_str);
MdelayKTask(1000);
}

View File

@ -1,3 +1,3 @@
SRC_FILES := ping.c lwip_ping_demo.c udp_echo.c lwip_udp_demo.c lwip_tcp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c
SRC_FILES := ping.c lwip_ping_demo.c lwip_udp_demo.c lwip_tcp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c
include $(KERNEL_ROOT)/compiler.mk

View File

@ -38,7 +38,9 @@
******************************************************************************/
char tcp_target[] = {192, 168, 250, 252};
char* tcp_send_msg = "\n\nThis one is TCP pkg. Congratulations on you.\n\n";
#define MSG_SIZE 128
// this is for test in shell, in fact, shell restrict the length of input string, which is less then 128
char tcp_send_msg[MSG_SIZE] = {0};
/*******************************************************************************
* Code
@ -46,78 +48,69 @@ char* tcp_send_msg = "\n\nThis one is TCP pkg. Congratulations on you.\n\n";
static void lwip_tcp_send_thread(void *arg)
{
int cnt = TEST_LWIP_TIMES;
lw_print("lwip_tcp_send_thread start.\n");
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;
}
int fd = -1;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
lw_print("Socket error\n");
return;
}
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));
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;
}
if (connect(fd, (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);
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));
sendto(fd, 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);
}
__exit:
if (sock_tcp_send_once >= 0)
closesocket(sock_tcp_send_once);
if (fd >= 0)
closesocket(fd);
return;
return;
}
void
lwip_tcp_send_init(void)
void lwip_tcp_send_run(int argc, char *argv[])
{
sys_thread_new("tcp send", lwip_tcp_send_thread, NULL, 4096, 25);
}
void lwip_tcp_client_run(int argc, char *argv[])
{
if(argc == 2)
memset(tcp_send_msg, 0, MSG_SIZE);
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]);
strncpy(tcp_send_msg, argv[1], strlen(argv[1]));
}
else
{
strncpy(tcp_send_msg, "hello world", strlen("hello world"));
}
strcat(tcp_send_msg, "\r\n");
if(argc >= 3)
{
sscanf(argv[2], "%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();
sys_thread_new("tcp send", lwip_tcp_send_thread, NULL, 4096, 25);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
TCPSend, lwip_tcp_client_run, TCP Client);
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
TCPSend, lwip_tcp_send_run, TCP Send message);
void lwip_tcp_server_run(void)
void lwip_tcp_recv_run(void)
{
ETH_BSP_Config();
lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
@ -125,5 +118,5 @@ void lwip_tcp_server_run(void)
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
TCPRecv, lwip_tcp_server_run, TCP Server);
TCPRecv, lwip_tcp_recv_run, TCP Recv message);

View File

@ -21,8 +21,10 @@
#include <xiuos.h>
#include "board.h"
#include "sys_arch.h"
#include "udp_echo.h"
#include "lwip/udp.h"
#include <lwip/sockets.h>
#include "lwip/sys.h"
/*******************************************************************************
* Definitions
@ -30,6 +32,7 @@
#define UDP_TASK_STACK_SIZE 4096
#define UDP_TASK_PRIO 15
#define PBUF_SIZE 27
/*******************************************************************************
* Prototypes
@ -41,77 +44,142 @@
static struct udp_pcb *udpecho_raw_pcb;
char udp_target[] = {192, 168, 250, 252};
char hello_str[] = {"hello world\r\n"};
char udp_send_msg[] = "\n\nThis one is UDP pkg. Congratulations on you.\n\n";
/*******************************************************************************
* Code
******************************************************************************/
static void *lwip_udp_test(void* param)
static void lwip_udp_send(void *arg)
{
ETH_BSP_Config();
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
UdpEchoInit();
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");
return;
}
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);
sendto(socket_fd, 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);
__exit:
if (socket_fd >= 0)
{
closesocket(socket_fd);
}
return;
}
void lwip_udp_thread(int argc, char *argv[])
void *lwip_udp_send_run(int argc, char *argv[])
{
int result = 0;
pthread_t th_id;
pthread_attr_t attr;
if(argc == 2)
memset(udp_send_msg, 0, sizeof(udp_send_msg));
if(argc == 1)
{
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]);
lw_print("lw: [%s] gw %d.%d.%d.%d\n", __func__, udp_target[0], udp_target[1], udp_target[2], udp_target[3]);
strncpy(udp_send_msg, hello_str, strlen(hello_str));
}
attr.schedparam.sched_priority = UDP_TASK_PRIO;
attr.stacksize = UDP_TASK_STACK_SIZE;
result = pthread_create(&th_id, &attr, lwip_udp_test, NULL);
if (0 == result) {
lw_print("lwip_udp_test successfully!\n");
} else {
lw_print("lwip_udp_test failed! error code is %d\n", result);
else
{
strncpy(udp_send_msg, argv[1], strlen(argv[1]));
strncat(udp_send_msg, "\r\n", 2);
if(argc == 3)
{
sscanf(argv[2], "%d.%d.%d.%d", &udp_target[0], &udp_target[1], &udp_target[2], &udp_target[3]);
}
}
lw_print("lw: [%s] gw %d.%d.%d.%d\n", __func__, udp_target[0], udp_target[1], udp_target[2], udp_target[3]);
ETH_BSP_Config();
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
sys_thread_new("udp socket send", lwip_udp_send, NULL, 4096, 25);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
UDPSend, lwip_udp_thread, UDP send echo);
UDPSend, lwip_udp_send_run, UDP send echo);
static void
udpecho_raw_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p,
static void udpecho_raw_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port)
{
LWIP_UNUSED_ARG(arg);
if (p != NULL) {
int udp_len;
err_t err;
struct pbuf* udp_buf;
LWIP_UNUSED_ARG(arg);
if (p == NULL)
{
return;
}
udp_len = p->tot_len;
lw_pr_info("Receive data :%dB\r\n", udp_len);
if(udp_len <= 80)
{
lw_pr_info("%.*s\r\n", udp_len, (char *)(p->payload));
}
udp_buf = pbuf_alloc(PBUF_TRANSPORT, PBUF_SIZE, PBUF_RAM);
memset(udp_buf->payload, 0, PBUF_SIZE);
err = pbuf_take(udp_buf, "Client receive success!\r\n", 27);
/* send received packet back to sender */
udp_sendto(upcb, p, addr, port);
udp_sendto(upcb, udp_buf, addr, port);
/* free the pbuf */
pbuf_free(p);
}
pbuf_free(udp_buf);
}
void
udpecho_raw_init(void)
void udpecho_raw_init(void)
{
udpecho_raw_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
if (udpecho_raw_pcb != NULL) {
err_t err;
lw_print("udpecho_raw_init\r\n");
udpecho_raw_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
if (udpecho_raw_pcb == NULL)
{
return;
}
err = udp_bind(udpecho_raw_pcb, IP_ANY_TYPE, LOCAL_PORT_SERVER);
if (err == ERR_OK) {
udp_recv(udpecho_raw_pcb, udpecho_raw_recv, NULL);
} else {
/* abort? output diagnostic? */
if (err == ERR_OK)
{
udp_recv(udpecho_raw_pcb, udpecho_raw_recv, NULL);
}
} else {
/* abort? output diagnostic? */
}
}
void lwip_udp_server(void)
{
lw_print("lwip_udp_server\r\n");
ETH_BSP_Config();
lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
udpecho_raw_init();

View File

@ -45,6 +45,7 @@
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "tcpecho_raw.h"
#include <string.h>
#if LWIP_TCP && LWIP_CALLBACK_API
@ -94,42 +95,6 @@ tcpecho_raw_close(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es)
tcp_close(tpcb);
}
static void
tcpecho_raw_send(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es)
{
struct pbuf *ptr;
err_t wr_err = ERR_OK;
while ((wr_err == ERR_OK) &&
(es->p != NULL) &&
(es->p->len <= tcp_sndbuf(tpcb))) {
ptr = es->p;
/* enqueue data for transmission */
wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1);
if (wr_err == ERR_OK) {
u16_t plen;
plen = ptr->len;
/* continue with next pbuf in chain (if any) */
es->p = ptr->next;
if(es->p != NULL) {
/* new reference! */
pbuf_ref(es->p);
}
/* chop first pbuf from chain */
pbuf_free(ptr);
/* we can read more data now */
tcp_recved(tpcb, plen);
} else if(wr_err == ERR_MEM) {
/* we are low on memory, try later / harder, defer to poll */
es->p = ptr;
} else {
/* other problem ?? */
}
}
}
static void
tcpecho_raw_error(void *arg, err_t err)
{
@ -142,6 +107,51 @@ tcpecho_raw_error(void *arg, err_t err)
tcpecho_raw_free(es);
}
char* recved_msg;
static void record_msg(struct tcp_pcb *tpcb, struct tcpecho_raw_state* es){
if(es==NULL||es->p==NULL||es->p->len==0){
recved_msg = NULL;
}
int new_size = es->p->len+1;
char* temp = recved_msg;
if(temp!=NULL){
new_size += strlen(temp);
lw_print("temp: %s\r\n", recved_msg);
}
recved_msg = malloc(new_size);
memset(recved_msg, 0, new_size);
if(temp!=NULL){
memcpy(recved_msg,temp,strlen(temp));
}
memcpy(recved_msg+new_size-es->p->len-1, es->p->payload, es->p->len);
free(temp);
if(((char*)es->p->payload)[es->p->len-1]=='\n'){
if(strlen(recved_msg)<80){
lw_pr_info("Received: %s\n", recved_msg);
}else{
lw_pr_info("Received a string of length %d\n", strlen(recved_msg));
}
struct pbuf* reply_pbuf = pbuf_alloc(PBUF_TRANSPORT, 20, PBUF_RAM); // only reply received message length
sprintf(reply_pbuf->payload,"%d\n\0",strlen(recved_msg));
reply_pbuf->len = strlen(reply_pbuf->payload);
reply_pbuf->tot_len = strlen(reply_pbuf->payload);
reply_pbuf->next = NULL;
tcp_write(tpcb, reply_pbuf->payload, reply_pbuf->len, 1);
tcp_recved(tpcb, reply_pbuf->len);
pbuf_free(reply_pbuf);
free(recved_msg);
recved_msg = NULL;
}
es->p = es->p->next;
if(es->p != NULL) {
/* new reference! */
pbuf_ref(es->p);
}
}
static err_t
tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
{
@ -152,7 +162,7 @@ tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
if (es != NULL) {
if (es->p != NULL) {
/* there is a remaining pbuf (chain) */
tcpecho_raw_send(tpcb, es);
record_msg(tpcb, es);
} else {
/* no remaining pbuf (chain) */
if(es->state == ES_CLOSING) {
@ -177,16 +187,10 @@ tcpecho_raw_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
es = (struct tcpecho_raw_state *)arg;
es->retries = 0;
if(es->p != NULL) {
/* still got pbufs to send */
tcp_sent(tpcb, tcpecho_raw_sent);
tcpecho_raw_send(tpcb, es);
} else {
/* no more pbufs to send */
if(es->state == ES_CLOSING) {
tcpecho_raw_close(tpcb, es);
}
es->p = NULL;
// the sent reply from server never have successors
if(es->state == ES_CLOSING) {
tcpecho_raw_close(tpcb, es);
}
return ERR_OK;
}
@ -207,7 +211,7 @@ tcpecho_raw_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
tcpecho_raw_close(tpcb, es);
} else {
/* we're not done yet */
tcpecho_raw_send(tpcb, es);
record_msg(tpcb, es);
}
ret_err = ERR_OK;
} else if(err != ERR_OK) {
@ -222,13 +226,13 @@ tcpecho_raw_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
es->state = ES_RECEIVED;
/* store reference to incoming pbuf (chain) */
es->p = p;
tcpecho_raw_send(tpcb, es);
record_msg(tpcb, es);
ret_err = ERR_OK;
} else if (es->state == ES_RECEIVED) {
/* read some more data */
if(es->p == NULL) {
es->p = p;
tcpecho_raw_send(tpcb, es);
record_msg(tpcb, es);
} else {
struct pbuf *ptr;
@ -251,6 +255,7 @@ tcpecho_raw_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
{
err_t ret_err;
struct tcpecho_raw_state *es;
recved_msg = NULL;
LWIP_UNUSED_ARG(arg);
if ((err != ERR_OK) || (newpcb == NULL)) {

View File

@ -1,192 +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_echo.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_send_msg = "\n\nThis one is UDP pkg. Congratulations on you.\n\n";
static void UdpEchoThreadServer(void *arg)
{
lw_print("UdpEchoThreadServer 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("Recevce 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 UdpEchoThreadClient(void *arg)
{
int cnt = TEST_LWIP_TIMES;
lw_print("UdpEchoThreadClient 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);
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);
__exit:
if (sock_udp_send_once >= 0)
{
closesocket(sock_udp_send_once);
}
return;
}
void
UdpEchoInit(void)
{
#ifdef SET_AS_SERVER
sys_thread_new("UdpEchoThreadServer", UdpEchoThreadServer, NULL, LWIP_UDP_TASK_STACK, LWIP_UDP_TASK_PRIO);
#else
sys_thread_new("UdpEchoThreadClient", UdpEchoThreadClient, NULL, LWIP_UDP_TASK_STACK, LWIP_UDP_TASK_PRIO);
#endif
}
#endif /* LWIP_NETCONN */

View File

@ -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_TCPECHO_H
#define LWIP_TCPECHO_H
extern char udp_target[];
void UdpEchoInit(void);
#endif /* LWIP_TCPECHO_H */