diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2008-09-02 23:04:10 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2008-09-02 23:04:10 +0000 |
commit | 9f60d09c6faaac732a62a188228a88321aad7368 (patch) | |
tree | 4404ee0e25d63abaad5e23b9aa8dce9ed6a9c8e9 /nuttx/examples/nsh | |
parent | 8a2d54981a1c1ee6563f3444cffc1d2d4784156e (diff) | |
download | px4-nuttx-9f60d09c6faaac732a62a188228a88321aad7368.tar.gz px4-nuttx-9f60d09c6faaac732a62a188228a88321aad7368.tar.bz2 px4-nuttx-9f60d09c6faaac732a62a188228a88321aad7368.zip |
Add NSH ping command
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@870 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/examples/nsh')
-rw-r--r-- | nuttx/examples/nsh/Makefile | 2 | ||||
-rw-r--r-- | nuttx/examples/nsh/nsh.h | 6 | ||||
-rw-r--r-- | nuttx/examples/nsh/nsh_main.c | 6 | ||||
-rw-r--r-- | nuttx/examples/nsh/nsh_netcmds.c | 150 |
4 files changed, 158 insertions, 6 deletions
diff --git a/nuttx/examples/nsh/Makefile b/nuttx/examples/nsh/Makefile index 5ff7e3c6a..8e308d43f 100644 --- a/nuttx/examples/nsh/Makefile +++ b/nuttx/examples/nsh/Makefile @@ -40,10 +40,8 @@ ASRCS = CSRCS = nsh_main.c nsh_fscmds.c nsh_proccmds.c nsh_envcmds.c nsh_dbgcmds.c ifeq ($(CONFIG_NET),y) -ifneq ($(CONFIG_NSOCKET_DESCRIPTORS),0) CSRCS += nsh_netcmds.c endif -endif ifeq ($(CONFIG_EXAMPLES_NSH_CONSOLE),y) CSRCS += nsh_serial.c diff --git a/nuttx/examples/nsh/nsh.h b/nuttx/examples/nsh/nsh.h index 9203d7c0f..861be30c2 100644 --- a/nuttx/examples/nsh/nsh.h +++ b/nuttx/examples/nsh/nsh.h @@ -296,8 +296,12 @@ extern int cmd_lbracket(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif /* !CONFIG_DISABLE_MOUNTPOINT */ #endif /* CONFIG_NFILE_DESCRIPTORS */ -#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 +#if defined(CONFIG_NET) extern int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); +#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \ + !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS) + extern int cmd_ping(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); +#endif #endif #ifndef CONFIG_DISABLE_ENVIRON diff --git a/nuttx/examples/nsh/nsh_main.c b/nuttx/examples/nsh/nsh_main.c index a70d2176d..ba7ad389f 100644 --- a/nuttx/examples/nsh/nsh_main.c +++ b/nuttx/examples/nsh/nsh_main.c @@ -146,7 +146,7 @@ static const struct cmdmap_s g_cmdmap[] = { "exec", cmd_exec, 2, 3, "<hex-address>" }, { "exit", cmd_exit, 1, 1, NULL }, { "help", cmd_help, 1, 1, NULL }, -#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 +#ifdef CONFIG_NET { "ifconfig", cmd_ifconfig, 1, 1, NULL }, #endif #if CONFIG_NFILE_DESCRIPTORS > 0 @@ -169,6 +169,10 @@ static const struct cmdmap_s g_cmdmap[] = #endif { "mw", cmd_mw, 2, 3, "<hex-address>[=<hex-value>][ <hex-byte-count>]" }, { "ps", cmd_ps, 1, 1, NULL }, +#if defined(CONFIG_NET) && defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \ + !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS) + { "ping", cmd_ping, 2, 6, "[-c <count>] [-i <interval>] <ip-address>" }, +#endif #if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON) { "pwd", cmd_pwd, 1, 1, NULL }, #endif diff --git a/nuttx/examples/nsh/nsh_netcmds.c b/nuttx/examples/nsh/nsh_netcmds.c index df4197c87..c5eebe425 100644 --- a/nuttx/examples/nsh/nsh_netcmds.c +++ b/nuttx/examples/nsh/nsh_netcmds.c @@ -38,14 +38,16 @@ ****************************************************************************/ #include <nuttx/config.h> -#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 +#ifdef CONFIG_NET #include <sys/types.h> #include <stdio.h> #include <stdlib.h> +#include <unistd.h> #include <sched.h> #include <nuttx/net.h> +#include <nuttx/clock.h> #include <net/ethernet.h> #include <net/uip/uip.h> #include <net/uip/uip-arch.h> @@ -55,12 +57,19 @@ #include <net/uip/uip.h> #endif +#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \ + !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS) +#include <net/uip/uip-lib.h> +#endif + #include "nsh.h" /**************************************************************************** * Definitions ****************************************************************************/ +#define DEFAULT_PING_DATALEN 56 + /**************************************************************************** * Private Types ****************************************************************************/ @@ -73,6 +82,11 @@ * Private Data ****************************************************************************/ +#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \ + !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS) +static uint16 g_pingid = 0; +#endif + /**************************************************************************** * Public Data ****************************************************************************/ @@ -82,6 +96,21 @@ ****************************************************************************/ /**************************************************************************** + * Name: ping_newid + ****************************************************************************/ + +#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \ + !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS) +static inline uint16 ping_newid(void) +{ + irqstate_t save = irqsave(); + uint16 ret = ++g_pingid; + irqrestore(save); + return ret; +} +#endif + +/**************************************************************************** * Name: uip_statistics ****************************************************************************/ @@ -228,4 +257,121 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) return OK; } -#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ +/**************************************************************************** + * Name: cmd_ping + ****************************************************************************/ + +#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \ + !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS) +int cmd_ping(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) +{ + FAR const char *fmt = g_fmtarginvalid; + const char *staddr; + uip_ipaddr_t ipaddr; + uint32 start; + uint32 next; + uint32 dsec; + uint16 id; + int sec = 1; + int count = 10; + int option; + int seqno; + int replies = 0; + int elapsed; + int i; + + /* Get the ping options */ + + while ((option = getopt(argc, argv, ":c:i:")) != ERROR) + { + switch (option) + { + case 'c': + count = atoi(optarg); + if (count < 1 || count > 10000) + { + fmt = g_fmtargrange; + goto errout; + } + break; + + case 'i': + sec = atoi(optarg); + if (sec < 1 || sec >= 4294) + { + fmt = g_fmtargrange; + goto errout; + } + break; + + case ':': + fmt = g_fmtargrequired; + + case '?': + default: + goto errout; + } + } + + /* There should be exactly on parameter left on the command-line */ + + if (optind == argc-1) + { + staddr = argv[optind]; + if (!uiplib_ipaddrconv(staddr, (FAR unsigned char*)&ipaddr)) + { + goto errout; + } + } + else if (optind >= argc) + { + fmt = g_fmttoomanyargs; + goto errout; + } + else + { + fmt = g_fmtargrequired; + goto errout; + } + + /* Convert the ping interval to microseconds and deciseconds*/ + + dsec = 10 * sec; + + /* Get the ID to use */ + + id = ping_newid(); + + /* Loop for the specified count */ + + nsh_output(vtbl, "PING %s %dbytes of data\n", staddr, DEFAULT_PING_DATALEN); + start = g_system_timer; + for (i = 0; i < count; i++) + { + next = g_system_timer; + seqno = uip_ping(ipaddr, id, i, DEFAULT_PING_DATALEN, dsec); + elapsed = TICK2MSEC(g_system_timer - next); + if (seqno >= 0) + { + nsh_output(vtbl, "%d bytes from %s: icmp_seq=%d time=%d ms\n", + DEFAULT_PING_DATALEN, staddr, seqno, elapsed); + replies++; + } + elapsed = TICK2DSEC(g_system_timer - next); + if (elapsed < dsec) + { + usleep(100000*dsec); + } + } + elapsed = TICK2MSEC(g_system_timer - start); + + nsh_output(vtbl, "%d packets transmitted, %d received, %d%% packet loss, time %dms\n", + count, replies, (100*replies + count/2)/count, elapsed); + return OK; + +errout: + nsh_output(vtbl, fmt, argv[0]); + return ERROR; +} +#endif /* CONFIG_NET_ICMP && CONFIG_NET_ICMP_PING */ +#endif /* CONFIG_NET */ |