Browse Source

fixed the exception of close socket and added dhcp and dns demo

pull/123/head
wlyu 7 months ago
parent
commit
acd53026b0
  1. 3
      APP_Framework/Applications/connection_app/lwip_demo/lwip_config_demo.c
  2. 62
      APP_Framework/Applications/connection_app/lwip_demo/lwip_dhcp_demo.c
  3. 14
      APP_Framework/Applications/connection_app/lwip_demo/lwip_ping_demo.c
  4. 5
      APP_Framework/Applications/connection_app/lwip_demo/lwip_tcp_demo.c
  5. 128
      APP_Framework/Applications/connection_app/lwip_demo/ping.c
  6. 4
      APP_Framework/Applications/connection_app/lwip_demo/ping.h
  7. 6
      APP_Framework/Applications/connection_app/lwip_demo/udp_echo.c
  8. 2
      Ubiquitous/XiUOS/resources/ethernet/LwIP/api/api_lib.c
  9. 6
      Ubiquitous/XiUOS/resources/ethernet/LwIP/api/tcpip.c
  10. 34
      Ubiquitous/XiUOS/resources/ethernet/LwIP/arch/sys_arch.c
  11. 2
      Ubiquitous/XiUOS/resources/ethernet/LwIP/arch/sys_arch.h

3
APP_Framework/Applications/connection_app/lwip_demo/lwip_config_demo.c

@ -119,10 +119,9 @@ void lwip_getip_thread(int argc, char *argv[])
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]);
showip, lwip_getip_thread, GetIp [IP] [Netmask] [Gateway]);
#endif

62
APP_Framework/Applications/connection_app/lwip_demo/lwip_dhcp_demo.c

@ -34,36 +34,6 @@
/*******************************************************************************
* Definitions
******************************************************************************/
/* IP address configuration. */
#define configIP_ADDR0 192
#define configIP_ADDR1 168
#define configIP_ADDR2 0
#define configIP_ADDR3 102
/* Netmask configuration. */
#define configNET_MASK0 255
#define configNET_MASK1 255
#define configNET_MASK2 255
#define configNET_MASK3 0
/* Gateway address configuration. */
#define configGW_ADDR0 192
#define configGW_ADDR1 168
#define configGW_ADDR2 0
#define configGW_ADDR3 100
/* MAC address configuration. */
#define configMAC_ADDR \
{ \
0x02, 0x12, 0x13, 0x10, 0x15, 0x11 \
}
/* Address of PHY interface. */
#define EXAMPLE_PHY_ADDRESS BOARD_ENET0_PHY_ADDRESS
/* System clock name. */
#define EXAMPLE_CLOCK_NAME kCLOCK_CoreSysClk
/*******************************************************************************
* Prototypes
@ -155,31 +125,10 @@ int print_dhcp_state(struct netif *netif)
void lwip_dhcp_test(void)
{
static int flag = 0;
struct netif fsl_netif0;
ip4_addr_t fsl_netif0_ipaddr, fsl_netif0_netmask, fsl_netif0_gw;
ethernetif_config_t fsl_enet_config0 = {
.phyAddress = EXAMPLE_PHY_ADDRESS,
.clockName = EXAMPLE_CLOCK_NAME,
.macAddress = configMAC_ADDR,
};
char ip_addr[4] = {0, 0, 0, 0};
ETH_BSP_Config();
// IP4_ADDR(&fsl_netif0_ipaddr, 0U, 0U, 0U, 0U);
// IP4_ADDR(&fsl_netif0_netmask, 0U, 0U, 0U, 0U);
// IP4_ADDR(&fsl_netif0_gw, 0U, 0U, 0U, 0U);
// if(flag == 0)
// {
// lwip_init();
// netif_add(&fsl_netif0, &fsl_netif0_ipaddr, &fsl_netif0_netmask, &fsl_netif0_gw, &fsl_enet_config0,
// ethernetif0_init, ethernet_input);
// netif_set_default(&fsl_netif0);
// flag = 1;
// }
// netif_set_up(&fsl_netif0);
lwip_config_net(ip_addr, ip_addr, ip_addr);
is_lwip_test = 1;
@ -200,6 +149,15 @@ void lwip_dhcp_test(void)
/* 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;
}
}
@ -209,6 +167,6 @@ void lwip_dhcp_test(void)
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
DHCPTest, lwip_dhcp_test, DHCP_Test);
getip, lwip_dhcp_test, DHCP_Test);
#endif

14
APP_Framework/Applications/connection_app/lwip_demo/lwip_ping_demo.c

@ -107,4 +107,18 @@ void lwip_ping_thread(int argc, char *argv[])
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

5
APP_Framework/Applications/connection_app/lwip_demo/lwip_tcp_demo.c

@ -86,10 +86,9 @@ static void lwip_tcp_send_thread(void *arg)
MdelayKTask(1000);
}
return;
__exit:
if (sock_tcp_send_once >= 0) closesocket(sock_tcp_send_once);
if (sock_tcp_send_once >= 0)
closesocket(sock_tcp_send_once);
return;
}

128
APP_Framework/Applications/connection_app/lwip_demo/ping.c

@ -38,6 +38,7 @@
*
*/
#include <xs_ktask.h>
#include "lwip/opt.h"
#if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
@ -52,6 +53,8 @@
#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"
@ -245,7 +248,7 @@ ping_thread(void *arg)
{
int s;
int ret;
int cnt = 5;
int cnt = TEST_LWIP_TIMES;
#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
int timeout = PING_RCV_TIMEO;
@ -295,6 +298,7 @@ ping_thread(void *arg)
}
sys_msleep(PING_DELAY);
}
lwip_close(s);
}
#else /* PING_USE_SOCKETS */
@ -415,4 +419,126 @@ ping_init(const ip_addr_t* ping_addr)
#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 */

4
APP_Framework/Applications/connection_app/lwip_demo/ping.h

@ -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 0 //LWIP_SOCKET
#define PING_USE_SOCKETS LWIP_SOCKET
#endif
void ping_init(const ip_addr_t* ping_addr);
@ -16,4 +16,6 @@ void ping_init(const ip_addr_t* ping_addr);
void ping_send_now(void);
#endif /* !PING_USE_SOCKETS */
int get_url_ip(char* url);
#endif /* LWIP_PING_H */

6
APP_Framework/Applications/connection_app/lwip_demo/udp_echo.c

@ -176,10 +176,12 @@ static void UdpEchoThreadClient(void *arg)
MdelayKTask(1000);
}
return;
__exit:
if (sock_udp_send_once >= 0) closesocket(sock_udp_send_once);
if (sock_udp_send_once >= 0)
{
closesocket(sock_udp_send_once);
}
return;
}

2
Ubiquitous/XiUOS/resources/ethernet/LwIP/api/api_lib.c

@ -246,7 +246,7 @@ netconn_delete(struct netconn *conn)
} else
#endif /* LWIP_NETCONN_FULLDUPLEX */
{
err = netconn_prepare_delete(conn);
// err = netconn_prepare_delete(conn);
}
if (err == ERR_OK) {
netconn_free(conn);

6
Ubiquitous/XiUOS/resources/ethernet/LwIP/api/tcpip.c

@ -601,8 +601,14 @@ tcpip_callbackmsg_trycallback_fromisr(struct tcpip_callback_msg *msg)
void
tcpip_init(tcpip_init_done_fn initfunc, void *arg)
{
static int tcpip_flag = 0;
lwip_init();
if(tcpip_flag)
return;
tcpip_flag = 1;
tcpip_init_done = initfunc;
tcpip_init_done_arg = arg;
if (sys_mbox_new(&tcpip_mbox, TCPIP_MBOX_SIZE) != ERR_OK) {

34
Ubiquitous/XiUOS/resources/ethernet/LwIP/arch/sys_arch.c

@ -93,6 +93,8 @@ int is_lwip_test = 0; //for lwip input thread
x_ticks_t lwip_sys_now;
static int lwip_init_flag = 0;
struct sys_timeouts {
struct sys_timeo *next;
};
@ -318,7 +320,7 @@ void sys_mbox_free(sys_mbox_t *mbox)
int sys_mbox_valid(sys_mbox_t *mbox)
{
if (*mbox < SYS_MBOX_NULL)
if (*mbox <= SYS_MBOX_NULL)
return 0;
else
return 1;
@ -471,10 +473,6 @@ void lwip_config_input(struct netif *net)
{
pthread_t th_id = 0;
//neglect create input thread for test
if(is_lwip_test)
return;
th_id = sys_thread_new("eth_input", lwip_input_thread, net, 4096, 15);
if (th_id >= 0) {
@ -484,8 +482,6 @@ void lwip_config_input(struct netif *net)
}
}
static int lwip_init_flag = 0;
void lwip_config_net(char *ip, char *mask, char *gw)
{
#if defined(FSL_FEATURE_SOC_LPC_ENET_COUNT) && (FSL_FEATURE_SOC_LPC_ENET_COUNT > 0)
@ -521,17 +517,19 @@ void lwip_config_net(char *ip, char *mask, char *gw)
netif_set_default(&gnetif);
netif_set_up(&gnetif);
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_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_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_pr_info("************************************************\r\n");
if(is_lwip_test)
{
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_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_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_pr_info("************************************************\r\n");
}
lwip_config_input(&gnetif);
}

2
Ubiquitous/XiUOS/resources/ethernet/LwIP/arch/sys_arch.h

@ -92,7 +92,7 @@
#define GW_ADDR3 5
/* USER CODE END 0 */
#define SYS_MBOX_NULL 0
#define SYS_MBOX_NULL -1
#define SYS_SEM_NULL 0
#define SYS_MRTEX_NULL SYS_SEM_NULL

Loading…
Cancel
Save