diff --git a/wireless/iwpan/src/iwpan.c b/wireless/iwpan/src/iwpan.c index bce2a84ec..b5b599dab 100644 --- a/wireless/iwpan/src/iwpan.c +++ b/wireless/iwpan/src/iwpan.c @@ -34,6 +34,7 @@ * Included Files ****************************************************************************/ +#include #include #include #include @@ -42,6 +43,31 @@ #include "wireless/ieee802154.h" #include "wireless/iwpan.h" +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The address family that we used to create the socket really does not + * matter. It should, however, be valid in the current configuration. + */ + +#if defined(CONFIG_NET_IPv4) +# define PF_INETX PF_INET +#elif defined(CONFIG_NET_IPv6) +# define PF_INETX PF_INET6 +#endif + +/* SOCK_DGRAM is the preferred socket type to use when we just want a + * socket for performing driver ioctls. However, we can't use SOCK_DRAM + * if UDP is disabled. + */ + +#ifdef CONFIG_NET_UDP +# define SOCK_IWPAN SOCK_DGRAM +#else +# define SOCK_IWPAN SOCK_STREAM +#endif + /**************************************************************************** * Private Types ****************************************************************************/ @@ -67,36 +93,247 @@ typedef void (*cmd3_t)(int sock, FAR const char *arg1, * Private Function Prototypes ****************************************************************************/ +static void iwpan_show_cmd(int sock, FAR const char *ifname); +static void iwpan_cca_cmd(int sock, FAR const char *ifname, + FAR const char *ccastr); +static void iwpan_chan_cmd(int sock, FAR const char *ifname, + FAR const char *chanstr); +static void iwpan_devmode_cmd(int sock, FAR const char *ifname, + FAR const char *modstr); +static void iwpan_eaddr_cmd(int sock, FAR const char *ifname, + FAR const char *addrstr); +static void iwpan_panid_cmd(int sock, FAR const char *ifname, + FAR const char *panstr); +static void iwpan_promisc_cmd(int sock, FAR const char *ifname, + FAR const char *boolstr); +static void iwpan_saddr_cmd(int sock, FAR const char *ifname, + FAR const char *addrstr); +static void iwpan_txpwr_cmd(int sock, FAR const char *ifname, + FAR const char *pwrstr); + static void iwpan_showusage(FAR const char *progname, int exitcode); /**************************************************************************** * Private Data ****************************************************************************/ +static const struct iwpan_command_s g_iwpan_commands[] = +{ + {"help", 0, (CODE void *)NULL}, + {"show", 1, (CODE void *)iwpan_show_cmd}, + {"cca", 2, (CODE void *)iwpan_cca_cmd}, + {"chan", 2, (CODE void *)iwpan_chan_cmd}, + {"devmode", 2, (CODE void *)iwpan_devmode_cmd}, + {"eaddr", 2, (CODE void *)iwpan_eaddr_cmd}, + {"panid", 2, (CODE void *)iwpan_panid_cmd}, + {"promisc", 2, (CODE void *)iwpan_promisc_cmd}, + {"saddr", 2, (CODE void *)iwpan_saddr_cmd}, + {"txpwr", 2, (CODE void *)iwpan_txpwr_cmd}, +}; + +#define NCOMMANDS (sizeof(g_iwpan_commands) / sizeof(struct iwpan_command_s)) + /**************************************************************************** * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: iwpan_str2int + * + * Description: + * Convert a string to an integer value + * + ****************************************************************************/ + +static int iwpan_str2int(FAR const char *str) +{ + FAR char *endptr; + long value; + + value = strtol(str, &endptr, 0); + if (*endptr != '\0') + { + fprintf(stderr, "ERROR: Garbage after numeric argument\n"); + exit(EXIT_FAILURE); + } + + if (value > INT_MAX || value < INT_MIN) + { + fprintf(stderr, "ERROR: Integer value out of range\n"); + exit(EXIT_FAILURE); + } + + return (int)value; +} + +/**************************************************************************** + * Name: iwpan_str2double + * + * Description: + * Convert a string to a double value + * + ****************************************************************************/ + +static double iwpan_str2double(FAR const char *str) +{ + FAR char *endptr; + double value; + + value = strtod(str, &endptr); + if (*endptr != '\0') + { + fprintf(stderr, "ERROR: Garbage after numeric argument\n"); + exit(EXIT_FAILURE); + } + + return value; +} + +/**************************************************************************** + * Name: iwpan_show_cmd + * + * Description: + * Show all radio device settings + * + ****************************************************************************/ + +static void iwpan_show_cmd(int sock, FAR const char *ifname) +{ + +} + +/**************************************************************************** + * Name: iwpan_cca_cmd + * + * Description: + * Set radio device Clear Channel Assessment (CCA) thresholds. + * + ****************************************************************************/ + +static void iwpan_cca_cmd(int sock, FAR const char *ifname, + FAR const char *ccastr) +{ + +} + +/**************************************************************************** + * Name: iwpan_chan_cmd + * + * Description: + * Set radio device channel + * + ****************************************************************************/ + +static void iwpan_chan_cmd(int sock, FAR const char *ifname, + FAR const char *chanstr) +{ + +} + +/**************************************************************************** + * Name: iwpan_devmode_cmd + * + * Description: + * Set radio device mode + * + ****************************************************************************/ + +static void iwpan_devmode_cmd(int sock, FAR const char *ifname, + FAR const char *modstr) +{ + +} + +/**************************************************************************** + * Name: iwpan_eaddr_cmd + * + * Description: + * Set radio device extended address + * + * + ****************************************************************************/ + +static void iwpan_eaddr_cmd(int sock, FAR const char *ifname, + FAR const char *addrstr) +{ + +} + +/**************************************************************************** + * Name: iwpan_panid_cmd + * + * Description: + * Set radio device PAN ID. + * + * + ****************************************************************************/ + +static void iwpan_panid_cmd(int sock, FAR const char *ifname, + FAR const char *panstr) +{ + +} + +/**************************************************************************** + * Name: iwpan_promisc_cmd + * + * Description: + * Set radio device PAN ID. + * + * + ****************************************************************************/ + +static void iwpan_promisc_cmd(int sock, FAR const char *ifname, + FAR const char *boolstr) +{ + +} + +/**************************************************************************** + * Name: iwpan_saddr_cmd + * + * Description: + * Set radio device short address + * + ****************************************************************************/ + +static void iwpan_saddr_cmd(int sock, FAR const char *ifname, + FAR const char *addrstr) +{ + +} + +/**************************************************************************** + * Name: iwpan_txpwr_cmd + * + * Description: + * Set radio device TX power + * + ****************************************************************************/ + +static void iwpan_txpwr_cmd(int sock, FAR const char *ifname, + FAR const char *pwrstr) +{ + +} + /**************************************************************************** * Name: iwpan_showusage * * Description: * Show program usage. * - * Returned Value: - * None - * ****************************************************************************/ static void iwpan_showusage(FAR const char *progname, int exitcode) { fprintf(stderr, "Usage:\n", progname); - fprintf(stderr, "\t%s [OPTIONS]\n"); + fprintf(stderr, "\t%s [OPTIONS]\n", progname); fprintf(stderr, "\nWhere supported commands an [OPTIONS] appear below\n"); fprintf(stderr, "\nRadio Query:\n"); fprintf(stderr, "\t%s show \n", progname); fprintf(stderr, "\nRadio Settings:\n"); - fprintf(stderr, "\t%s cca \n", progname); + fprintf(stderr, "\t%s cca \n", progname); fprintf(stderr, "\t%s chan \n", progname); fprintf(stderr, "\t%s devmode \n", progname); fprintf(stderr, "\t%s eaddr \n", progname); @@ -128,5 +365,93 @@ int main(int argc, FAR char *argv[]) int iwpan_main(int argc, char *argv[]) #endif { + FAR const char *cmdname; + FAR const struct iwpan_command_s *iwpancmd; + int sock; + int i; + + /* Get the command */ + + if (argc < 2) + { + fprintf(stderr, "ERROR: Missing command\n"); + iwpan_showusage(argv[0], EXIT_FAILURE); + } + + cmdname = argv[1]; + + /* Find the command in the g_iwpan_command[] list */ + + iwpancmd = NULL; + for (i = 0; i < NCOMMANDS; i++) + { + FAR const struct iwpan_command_s *cmd = &g_iwpan_commands[i]; + if (strcmp(cmdname, cmd->name) == 0) + { + iwpancmd = cmd; + break; + } + } + + if (iwpancmd == NULL) + { + fprintf(stderr, "ERROR: Unsupported command: %s\n", cmdname); + iwpan_showusage(argv[0], EXIT_FAILURE); + } + + if (iwpancmd->noptions + 2 < argc) + { + fprintf(stderr, "ERROR: Garbage at end of command ignored\n"); + } + else if (iwpancmd->noptions + 2 > argc) + { + fprintf(stderr, "ERROR: Missing required command options: %s\n", + cmdname); + iwpan_showusage(argv[0], EXIT_FAILURE); + } + + /* Special case the help command which has no arguments, no handler, + * and does not need a socket. + */ + + if (iwpancmd->handler == NULL) + { + iwpan_showusage(argv[0], EXIT_SUCCESS); + } + + /* Create a communication socket */ + + sock = socket(PF_INETX, SOCK_IWPAN, 0); + if (sock < 0) + { + fprintf(stderr, "ERROR: iwpan_make_socket() failed: %d\n", sock); + return EXIT_FAILURE; + } + + /* Dispatch the command handling */ + + switch (iwpancmd->noptions) + { + default: + case 0: + fprintf(stderr, "ERROR: Internal craziness\n"); + iwpan_showusage(argv[0], EXIT_FAILURE); + + case 1: + ((cmd1_t)iwpancmd->handler)(sock, argv[2]); + break; + + case 2: + ((cmd2_t)iwpancmd->handler)(sock, argv[2], argv[3]); + break; + + case 3: + ((cmd3_t)iwpancmd->handler)(sock, argv[2], argv[3], argv[4]); + break; + } + + /* Close communication socket */ + + close(sock); return EXIT_SUCCESS; } diff --git a/wireless/wapi/src/network.c b/wireless/wapi/src/network.c index a2e2a1b9a..9bebf445d 100644 --- a/wireless/wapi/src/network.c +++ b/wireless/wapi/src/network.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/wireless/wapi/examples/network.c * - * Copyright (C) 2011, 2017Gregory Nutt. All rights reserved. + * Copyright (C) 2011, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Adapted for Nuttx from WAPI: