diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-11-04 18:54:04 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-11-04 18:54:04 +0000 |
commit | 08a0eae6371ad7d6af7c0d941381fa1e11370a86 (patch) | |
tree | 7a3a8ae8e2b29e3832a17a7def5b0531383172bb /apps/nshlib | |
parent | 813efa266bb67269669e532b5ce6f00ee2097948 (diff) | |
download | px4-nuttx-08a0eae6371ad7d6af7c0d941381fa1e11370a86.tar.gz px4-nuttx-08a0eae6371ad7d6af7c0d941381fa1e11370a86.tar.bz2 px4-nuttx-08a0eae6371ad7d6af7c0d941381fa1e11370a86.zip |
Add interfaces flags, extend ifconfig, add ifup and ifdown commands (Darcy Gong
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5308 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'apps/nshlib')
-rw-r--r-- | apps/nshlib/README.txt | 43 | ||||
-rw-r--r-- | apps/nshlib/nsh.h | 34 | ||||
-rw-r--r-- | apps/nshlib/nsh_netcmds.c | 222 | ||||
-rw-r--r-- | apps/nshlib/nsh_parse.c | 32 |
4 files changed, 290 insertions, 41 deletions
diff --git a/apps/nshlib/README.txt b/apps/nshlib/README.txt index 9f371678e..227f01c0a 100644 --- a/apps/nshlib/README.txt +++ b/apps/nshlib/README.txt @@ -385,7 +385,7 @@ o help [-v] [<cmd>] <cmd> Show full command usage only for this command -o ifconfig +o ifconfig [nic_name [ip]] [dr|gw|gateway <dr-address>] [netmask <net-mask>] [dns <dns-address>] [hw <hw-mac>] Show the current configuration of the network, for example: @@ -396,6 +396,22 @@ o ifconfig if uIP statistics are enabled (CONFIG_NET_STATISTICS), then this command will also show the detailed state of uIP. +o ifdown <nic-name> + + Take down the interface identified by the name <nic-name>. + + Example: + + ifdown eth0 + +o ifup <nic-name> + + Bring up down the interface identified by the name <nic-name>. + + Example: + + ifup eth0 + o kill -<signal> <pid> Send the <signal> to the task identified by <pid>. @@ -850,6 +866,8 @@ Command Dependencies on Configuration Settings get CONFIG_NET && CONFIG_NET_UDP && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NET_BUFSIZE >= 558 (see note 1) help -- ifconfig CONFIG_NET + ifdown CONFIG_NET + ifup CONFIG_NET kill !CONFIG_DISABLE_SIGNALS losetup !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 ls CONFIG_NFILE_DESCRIPTORS > 0 @@ -899,17 +917,18 @@ also allow it to squeeze into very small memory footprints. CONFIG_NSH_DISABLE_CD, CONFIG_NSH_DISABLE_CP, CONFIG_NSH_DISABLE_DD, CONFIG_NSH_DISABLE_DF, CONFIG_NSH_DISABLE_ECHO, CONFIG_NSH_DISABLE_EXEC, CONFIG_NSH_DISABLE_EXIT, CONFIG_NSH_DISABLE_FREE, CONFIG_NSH_DISABLE_GET, - CONFIG_NSH_DISABLE_HELP, CONFIG_NSH_DISABLE_IFCONFIG, CONFIG_NSH_DISABLE_KILL, - CONFIG_NSH_DISABLE_LOSETUP, CONFIG_NSH_DISABLE_LS, CONFIG_NSH_DISABLE_MD5 - CONFIG_NSH_DISABLE_MB, CONFIG_NSH_DISABLE_MKDIR, CONFIG_NSH_DISABLE_MKFATFS, - CONFIG_NSH_DISABLE_MKFIFO, CONFIG_NSH_DISABLE_MKRD, CONFIG_NSH_DISABLE_MH, - CONFIG_NSH_DISABLE_MOUNT, CONFIG_NSH_DISABLE_MW, CONFIG_NSH_DISABLE_MV, - CONFIG_NSH_DISABLE_NFSMOUNT, CONFIG_NSH_DISABLE_PS, CONFIG_NSH_DISABLE_PING, - CONFIG_NSH_DISABLE_PUT, CONFIG_NSH_DISABLE_PWD, CONFIG_NSH_DISABLE_RM, - CONFIG_NSH_DISABLE_RMDIR, CONFIG_NSH_DISABLE_SET, CONFIG_NSH_DISABLE_SH, - CONFIG_NSH_DISABLE_SLEEP, CONFIG_NSH_DISABLE_TEST, CONFIG_NSH_DISABLE_UMOUNT, - CONFIG_NSH_DISABLE_UNSET, CONFIG_NSH_DISABLE_URLDECODE, CONFIG_NSH_DISABLE_URLENCODE, - CONFIG_NSH_DISABLE_USLEEP, CONFIG_NSH_DISABLE_WGET, CONFIG_NSH_DISABLE_XD + CONFIG_NSH_DISABLE_HELP, CONFIG_NSH_DISABLE_IFCONFIG, CONFIG_NSH_DISABLE_IFUPDOWN, + CONFIG_NSH_DISABLE_KILL, CONFIG_NSH_DISABLE_LOSETUP, CONFIG_NSH_DISABLE_LS, + CONFIG_NSH_DISABLE_MD5 CONFIG_NSH_DISABLE_MB, CONFIG_NSH_DISABLE_MKDIR, + CONFIG_NSH_DISABLE_MKFATFS, CONFIG_NSH_DISABLE_MKFIFO, CONFIG_NSH_DISABLE_MKRD, + CONFIG_NSH_DISABLE_MH, CONFIG_NSH_DISABLE_MOUNT, CONFIG_NSH_DISABLE_MW, + CONFIG_NSH_DISABLE_MV, CONFIG_NSH_DISABLE_NFSMOUNT, CONFIG_NSH_DISABLE_PS, + CONFIG_NSH_DISABLE_PING, CONFIG_NSH_DISABLE_PUT, CONFIG_NSH_DISABLE_PWD, + CONFIG_NSH_DISABLE_RM, CONFIG_NSH_DISABLE_RMDIR, CONFIG_NSH_DISABLE_SET, + CONFIG_NSH_DISABLE_SH, CONFIG_NSH_DISABLE_SLEEP, CONFIG_NSH_DISABLE_TEST, + CONFIG_NSH_DISABLE_UMOUNT, CONFIG_NSH_DISABLE_UNSET, CONFIG_NSH_DISABLE_URLDECODE, + CONFIG_NSH_DISABLE_URLENCODE, CONFIG_NSH_DISABLE_USLEEP, CONFIG_NSH_DISABLE_WGET, + CONFIG_NSH_DISABLE_XD Verbose help output can be suppressed by defining CONFIG_NSH_HELP_TERSE. In that case, the help command is still available but will be slightly smaller. diff --git a/apps/nshlib/nsh.h b/apps/nshlib/nsh.h index 9f36b1d1f..ac75cf2e1 100644 --- a/apps/nshlib/nsh.h +++ b/apps/nshlib/nsh.h @@ -267,9 +267,35 @@ # undef CONFIG_NSH_ROMFSSECTSIZE #endif -/* This is the maximum number of arguments that will be accepted for a command */ +/* This is the maximum number of arguments that will be accepted for a + * command. Here we attempt to select the smallest number possible depending + * upon the of commands that are available. Most commands use six or fewer + * arguments, but there are a few that require more. + * + * This value is also configurable with CONFIG_NSH_MAXARGUMENTS. This + * configurability is necessary since there may also be external, "built-in" + * commands that require more commands than NSH is aware of. + */ + +#ifndef CONFIG_NSH_MAXARGUMENTS +# define CONFIG_NSH_MAXARGUMENTS 6 +#endif + +#if CONFIG_NSH_MAXARGUMENTS < 11 +# if defined(CONFIG_NET) && !defined(CONFIG_NSH_DISABLE_IFCONFIG) +# undef CONFIG_NSH_MAXARGUMENTS +# define CONFIG_NSH_MAXARGUMENTS 11 +# endif +#endif -#define NSH_MAX_ARGUMENTS 6 +#if CONFIG_NSH_MAXARGUMENTS < 7 +# if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0 +# if !defined(CONFIG_NSH_DISABLE_GET) || !defined(CONFIG_NSH_DISABLE_PUT) +# undef CONFIG_NSH_MAXARGUMENTS +# define CONFIG_NSH_MAXARGUMENTS 7 +# endif +# endif +#endif /* strerror() produces much nicer output but is, however, quite large and * will only be used if CONFIG_NSH_STRERROR is defined. Note that the strerror @@ -602,6 +628,10 @@ void nsh_usbtrace(void); # ifndef CONFIG_NSH_DISABLE_IFCONFIG int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif +# ifndef CONFIG_NSH_DISABLE_IFUPDOWN + int cmd_ifup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_ifdown(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); +# endif #if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0 # ifndef CONFIG_NSH_DISABLE_GET int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); diff --git a/apps/nshlib/nsh_netcmds.c b/apps/nshlib/nsh_netcmds.c index f3457a809..d28fe873d 100644 --- a/apps/nshlib/nsh_netcmds.c +++ b/apps/nshlib/nsh_netcmds.c @@ -277,14 +277,34 @@ int ifconfig_callback(FAR struct uip_driver_s *dev, void *arg) { struct nsh_vtbl_s *vtbl = (struct nsh_vtbl_s*)arg; struct in_addr addr; + bool is_running = false; + int ret; + + ret = uip_getifstatus(dev->d_ifname,&is_running); + if (ret != OK) + { + nsh_output(vtbl, "\tGet %s interface flags error: %d\n", + dev->d_ifname, ret); + } + + nsh_output(vtbl, "%s\tHWaddr %s at %s\n", + dev->d_ifname, ether_ntoa(&dev->d_mac), (is_running)?"UP":"DOWN"); - nsh_output(vtbl, "%s\tHWaddr %s\n", dev->d_ifname, ether_ntoa(&dev->d_mac)); addr.s_addr = dev->d_ipaddr; nsh_output(vtbl, "\tIPaddr:%s ", inet_ntoa(addr)); + addr.s_addr = dev->d_draddr; nsh_output(vtbl, "DRaddr:%s ", inet_ntoa(addr)); + addr.s_addr = dev->d_netmask; - nsh_output(vtbl, "Mask:%s\n\n", inet_ntoa(addr)); + nsh_output(vtbl, "Mask:%s\n", inet_ntoa(addr)); + +#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS) + resolv_getserver(&addr); + nsh_output(vtbl, "\tDNSaddr:%s\n", inet_ntoa(addr)); +#endif + + nsh_output(vtbl, "\n"); return OK; } @@ -484,6 +504,54 @@ int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) #endif /**************************************************************************** + * Name: cmd_ifup + ****************************************************************************/ + +#ifndef CONFIG_NSH_DISABLE_IFUPDOWN +int cmd_ifup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) +{ + FAR char *intf = NULL; + int ret; + + if (argc != 2) + { + nsh_output(vtbl, "Please select nic_name:\n"); + netdev_foreach(ifconfig_callback, vtbl); + return OK; + } + + intf = argv[1]; + ret = uip_ifup(intf); + nsh_output(vtbl, "ifup %s...%s\n", intf, (ret == OK) ? "OK" : "Failed"); + return ret; +} +#endif + +/**************************************************************************** + * Name: cmd_ifdown + ****************************************************************************/ + +#ifndef CONFIG_NSH_DISABLE_IFUPDOWN +int cmd_ifdown(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) +{ + FAR char *intf = NULL; + int ret; + + if (argc != 2) + { + nsh_output(vtbl, "Please select nic_name:\n"); + netdev_foreach(ifconfig_callback, vtbl); + return OK; + } + + intf = argv[1]; + ret = uip_ifdown(intf); + nsh_output(vtbl, "ifdown %s...%s\n", intf, (ret == OK) ? "OK" : "Failed"); + return ret; +} +#endif + +/**************************************************************************** * Name: cmd_ifconfig ****************************************************************************/ @@ -491,7 +559,17 @@ int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { struct in_addr addr; - in_addr_t ip; + in_addr_t gip; + int i; + FAR char *intf = NULL; + FAR char *hostip = NULL; + FAR char *gwip = NULL; + FAR char *mask = NULL; + FAR char *tmp = NULL; + FAR char *hw = NULL; + FAR char *dns = NULL; + bool badarg=false; + uint8_t mac[6]; /* With one or no arguments, ifconfig simply shows the status of ethernet * device: @@ -513,24 +591,142 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) * ifconfig nic_name ip_address */ + if (argc > 2) + { + for(i = 0; i < argc; i++) + { + if (i == 1) + { + intf = argv[i]; + } + else if (i == 2) + { + hostip = argv[i]; + } + else + { + tmp = argv[i]; + if (!strcmp(tmp, "dr") || !strcmp(tmp, "gw") || !strcmp(tmp, "gateway")) + { + if (argc-1 >= i+1) + { + gwip = argv[i+1]; + i++; + } + else + { + badarg = true; + } + } + else if(!strcmp(tmp, "netmask")) + { + if (argc-1 >= i+1) + { + mask = argv[i+1]; + i++; + } + else + { + badarg = true; + } + } + else if(!strcmp(tmp, "hw")) + { + if (argc-1>=i+1) + { + hw = argv[i+1]; + i++; + badarg = !uiplib_hwmacconv(hw, mac); + } + else + { + badarg = true; + } + } + else if(!strcmp(tmp, "dns")) + { + if (argc-1 >= i+1) + { + dns = argv[i+1]; + i++; + } + else + { + badarg = true; + } + } + } + } + } + + if (badarg) + { + nsh_output(vtbl, g_fmtargrequired, argv[0]); + return ERROR; + } + + /* Set Hardware ethernet MAC addr */ + + if (hw) + { + ndbg("HW MAC: %s\n", hw); + uip_setmacaddr(intf, mac); + } + /* Set host ip address */ - ip = addr.s_addr = inet_addr(argv[2]); - uip_sethostaddr(argv[1], &addr); + ndbg("Host IP: %s\n", hostip); + gip = addr.s_addr = inet_addr(hostip); + uip_sethostaddr(intf, &addr); /* Set gateway */ - ip = NTOHL(ip); - ip &= ~0x000000ff; - ip |= 0x00000001; + if (gwip) + { + ndbg("Gateway: %s\n", gwip); + gip = addr.s_addr = inet_addr(gwip); + } + else + { + ndbg("Gateway: default\n"); + gip = NTOHL(gip); + gip &= ~0x000000ff; + gip |= 0x00000001; + gip = HTONL(gip); + addr.s_addr = gip; + } + + uip_setdraddr(intf, &addr); + + /* Set network mask */ + + if (mask) + { + ndbg("Netmask: %s\n",mask); + addr.s_addr = inet_addr(mask); + } + else + { + ndbg("Netmask: Default\n"); + addr.s_addr = inet_addr("255.255.255.0"); + } - addr.s_addr = HTONL(ip); - uip_setdraddr(argv[1], &addr); + uip_setnetmask(intf, &addr); - /* Set netmask */ +#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS) + if (dns) + { + ndbg("DNS: %s\n", dns); + addr.s_addr = inet_addr(dns); + } + else + { + ndbg("DNS: Default\n"); + addr.s_addr = gip; + } - addr.s_addr = inet_addr("255.255.255.0"); - uip_setnetmask(argv[1], &addr); + resolv_conf(&addr); +#endif return OK; } diff --git a/apps/nshlib/nsh_parse.c b/apps/nshlib/nsh_parse.c index f9642809f..bf2b8a4a4 100644 --- a/apps/nshlib/nsh_parse.c +++ b/apps/nshlib/nsh_parse.c @@ -73,19 +73,19 @@ /* Argument list size * * argv[0]: The command name. - * argv[1]: The beginning of argument (up to NSH_MAX_ARGUMENTS) + * argv[1]: The beginning of argument (up to CONFIG_NSH_MAXARGUMENTS) * argv[argc-3]: Possibly '>' or '>>' * argv[argc-2]: Possibly <file> * argv[argc-1]: Possibly '&' (if pthreads are enabled) * argv[argc]: NULL terminating pointer * - * Maximum size is NSH_MAX_ARGUMENTS+5 + * Maximum size is CONFIG_NSH_MAXARGUMENTS+5 */ #ifndef CONFIG_NSH_DISABLEBG -# define MAX_ARGV_ENTRIES (NSH_MAX_ARGUMENTS+5) +# define MAX_ARGV_ENTRIES (CONFIG_NSH_MAXARGUMENTS+5) #else -# define MAX_ARGV_ENTRIES (NSH_MAX_ARGUMENTS+4) +# define MAX_ARGV_ENTRIES (CONFIG_NSH_MAXARGUMENTS+4) #endif /* Help command summary layout */ @@ -146,7 +146,7 @@ static const char g_failure[] = "1"; static const struct cmdmap_s g_cmdmap[] = { #if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_TEST) - { "[", cmd_lbracket, 4, NSH_MAX_ARGUMENTS, "<expression> ]" }, + { "[", cmd_lbracket, 4, CONFIG_NSH_MAXARGUMENTS, "<expression> ]" }, #endif #ifndef CONFIG_NSH_DISABLE_HELP @@ -164,7 +164,7 @@ static const struct cmdmap_s g_cmdmap[] = #if CONFIG_NFILE_DESCRIPTORS > 0 # ifndef CONFIG_NSH_DISABLE_CAT - { "cat", cmd_cat, 2, NSH_MAX_ARGUMENTS, "<path> [<path> [<path> ...]]" }, + { "cat", cmd_cat, 2, CONFIG_NSH_MAXARGUMENTS, "<path> [<path> [<path> ...]]" }, # endif #ifndef CONFIG_DISABLE_ENVIRON # ifndef CONFIG_NSH_DISABLE_CD @@ -196,9 +196,9 @@ static const struct cmdmap_s g_cmdmap[] = #ifndef CONFIG_NSH_DISABLE_ECHO # ifndef CONFIG_DISABLE_ENVIRON - { "echo", cmd_echo, 0, NSH_MAX_ARGUMENTS, "[<string|$name> [<string|$name>...]]" }, + { "echo", cmd_echo, 0, CONFIG_NSH_MAXARGUMENTS, "[<string|$name> [<string|$name>...]]" }, # else - { "echo", cmd_echo, 0, NSH_MAX_ARGUMENTS, "[<string> [<string>...]]" }, + { "echo", cmd_echo, 0, CONFIG_NSH_MAXARGUMENTS, "[<string> [<string>...]]" }, # endif #endif @@ -229,7 +229,11 @@ static const struct cmdmap_s g_cmdmap[] = #ifdef CONFIG_NET # ifndef CONFIG_NSH_DISABLE_IFCONFIG - { "ifconfig", cmd_ifconfig, 1, 3, "[nic_name [ip]]" }, + { "ifconfig", cmd_ifconfig, 1, 11, "[nic_name [ip]] [dr|gw|gateway <dr-address>] [netmask <net-mask>] [dns <dns-address>] [hw <hw-mac>]" }, +# endif +# ifndef CONFIG_NSH_DISABLE_IFUPDOWN + { "ifdown", cmd_ifdown, 2, 2, "<nic_name>" }, + { "ifup", cmd_ifup, 2, 2, "<nic_name>" }, # endif #endif @@ -363,7 +367,7 @@ static const struct cmdmap_s g_cmdmap[] = #endif #if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_TEST) - { "test", cmd_test, 3, NSH_MAX_ARGUMENTS, "<expression>" }, + { "test", cmd_test, 3, CONFIG_NSH_MAXARGUMENTS, "<expression>" }, #endif #if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_READABLE) @@ -736,7 +740,7 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl, int argc, char *argv[]) * * argv[0]: The command name. This is argv[0] when the arguments * are, finally, received by the command vtblr - * argv[1]: The beginning of argument (up to NSH_MAX_ARGUMENTS) + * argv[1]: The beginning of argument (up to CONFIG_NSH_MAXARGUMENTS) * argv[argc]: NULL terminating pointer */ @@ -1343,13 +1347,13 @@ int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline) * of argv is: * * argv[0]: The command name. - * argv[1]: The beginning of argument (up to NSH_MAX_ARGUMENTS) + * argv[1]: The beginning of argument (up to CONFIG_NSH_MAXARGUMENTS) * argv[argc-3]: Possibly '>' or '>>' * argv[argc-2]: Possibly <file> * argv[argc-1]: Possibly '&' * argv[argc]: NULL terminating pointer * - * Maximum size is NSH_MAX_ARGUMENTS+5 + * Maximum size is CONFIG_NSH_MAXARGUMENTS+5 */ argv[0] = cmd; @@ -1423,7 +1427,7 @@ int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline) /* Check if the maximum number of arguments was exceeded */ - if (argc > NSH_MAX_ARGUMENTS) + if (argc > CONFIG_NSH_MAXARGUMENTS) { nsh_output(vtbl, g_fmttoomanyargs, cmd); } |