From 2d73f735fe87360aa4ac28083387adf03e390c2a Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 10 Apr 2017 15:34:56 -0600 Subject: [PATCH 1/6] Fix a little kruft left in ported wireless/wapi/Makefile --- wireless/wapi/Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wireless/wapi/Makefile b/wireless/wapi/Makefile index 02297248f..03de538c9 100644 --- a/wireless/wapi/Makefile +++ b/wireless/wapi/Makefile @@ -40,12 +40,12 @@ -include $(TOPDIR)/Make.defs include $(APPDIR)/Make.defs -# LM-75 Temperature Sensor Application +# IEEE802.11 Wapi Application CONFIG_WIRELESS_WAPI_PRIORITY ?= 100 CONFIG_WIRELESS_WAPI_STACKSIZE ?= 2048 -APPNAME = temp +APPNAME = wapi PRIORITY = $(CONFIG_WIRELESS_WAPI_PRIORITY) STACKSIZE = $(CONFIG_WIRELESS_WAPI_STACKSIZE) @@ -84,7 +84,7 @@ else INSTALL_DIR = $(BIN_DIR) endif -CONFIG_WAPI_PROGNAME ?= lm75$(EXEEXT) +CONFIG_WAPI_PROGNAME ?= wapi$(EXEEXT) PROGNAME = $(CONFIG_WAPI_PROGNAME) ROOTDEPPATH = --dep-path . @@ -121,7 +121,7 @@ endif ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile - $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),lm75_main) + $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat else From 322ffe193b45fef0646b0d48a20d2d1bfd245d83 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 10 Apr 2017 16:02:17 -0600 Subject: [PATCH 2/6] wireless/wapi: Remove functionality that depends on the Linux procfs: This includes only 1) listing of available interfaces and 2) listing of all routes. --- include/wireless/wapi.h | 43 +++---------- wireless/wapi/src/network.c | 119 +++-------------------------------- wireless/wapi/src/util.c | 91 +++------------------------ wireless/wapi/src/util.h | 9 ++- wireless/wapi/src/wapi.c | 74 +++------------------- wireless/wapi/src/wireless.c | 9 ++- 6 files changed, 43 insertions(+), 302 deletions(-) diff --git a/include/wireless/wapi.h b/include/wireless/wapi.h index d97bb58fc..9bbc7d6f7 100644 --- a/include/wireless/wapi.h +++ b/include/wireless/wapi.h @@ -1,8 +1,13 @@ /**************************************************************************** * apps/include/wireless/wapi.h * - * Copyright (c) 2010, Volkan YAZICI - * All rights reserved. + * Copyright (C) 2011, 2017Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Adapted for Nuttx from WAPI: + * + * Copyright (c) 2010, Volkan YAZICI + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -48,14 +53,6 @@ #define WAPI_ESSID_MAX_SIZE IW_ESSID_MAX_SIZE -/* Path to /proc/net/wireless. (Requires procfs mounted.) */ - -#define WAPI_PROC_NET_WIRELESS "/proc/net/wireless" - -/* Path to /proc/net/route. (Requires procfs mounted.) */ - -#define WAPI_PROC_NET_ROUTE "/proc/net/route" - /* Buffer size while reading lines from PROC_NET_ files. */ #define WAPI_PROC_LINE_SIZE 1024 @@ -293,19 +290,6 @@ int wapi_get_netmask(int sock, const char *ifname, struct in_addr *addr); int wapi_set_netmask(int sock, const char *ifname, const struct in_addr *addr); -/**************************************************************************** - * Name: wapi_get_routes - * - * Description: - * Parses routing table rows from WAPI_PROC_NET_ROUTE. - * - * Input Parameters: - * list - Pushes collected wapi_route_info_t into this list. - * - ****************************************************************************/ - -int wapi_get_routes(wapi_list_t * list); - /**************************************************************************** * Name: wapi_add_route_gw * @@ -572,19 +556,6 @@ int wapi_set_txpower(int sock, FAR const char *ifname, int power, int wapi_make_socket(void); -/**************************************************************************** - * Name: wapi_get_ifnames - * - * Description: - * Parses WAPI_PROC_NET_WIRELESS. - * - * Returned Value: - * list Pushes collected wapi_string_t into this list. - * - ****************************************************************************/ - -int wapi_get_ifnames(FAR wapi_list_t *list); - /**************************************************************************** * Name: wapi_scan_init * diff --git a/wireless/wapi/src/network.c b/wireless/wapi/src/network.c index 170510ade..a2e2a1b9a 100644 --- a/wireless/wapi/src/network.c +++ b/wireless/wapi/src/network.c @@ -1,8 +1,13 @@ /**************************************************************************** * apps/wireless/wapi/examples/network.c * - * Copyright (c) 2010, Volkan YAZICI - * All rights reserved. + * Copyright (C) 2011, 2017Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Adapted for Nuttx from WAPI: + * + * Copyright (c) 2010, Volkan YAZICI + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -287,116 +292,6 @@ int wapi_set_netmask(int sock, FAR const char *ifname, return wapi_set_addr(sock, ifname, SIOCSIFNETMASK, addr); } -/**************************************************************************** - * Name: wapi_get_routes - * - * Description: - * Parses routing table rows from WAPI_PROC_NET_ROUTE. - * - * Input Parameters: - * list - Pushes collected wapi_route_info_t into this list. - * - ****************************************************************************/ - -int wapi_get_routes(FAR wapi_list_t *list) -{ - FAR FILE *fp; - size_t bufsiz = WAPI_PROC_LINE_SIZE * sizeof(char); - char buf[WAPI_PROC_LINE_SIZE]; - int ret; - - WAPI_VALIDATE_PTR(list); - - /* Open file for reading. */ - - fp = fopen(WAPI_PROC_NET_ROUTE, "r"); - if (!fp) - { - WAPI_STRERROR("fopen(\"%s\", \"r\")", WAPI_PROC_NET_ROUTE); - return -1; - } - - /* Skip header line. */ - - if (!fgets(buf, bufsiz, fp)) - { - WAPI_ERROR("Invalid \"%s\" content!\n", WAPI_PROC_NET_ROUTE); - return -1; - } - - /* Read lines. */ - - ret = 0; - while (fgets(buf, bufsiz, fp)) - { - wapi_route_info_t *ri; - char ifname[WAPI_PROC_LINE_SIZE]; - int refcnt, use, metric, mtu, window, irtt; - unsigned int dest, gw, flags, netmask; - - /* Allocate route row buffer. */ - - ri = malloc(sizeof(wapi_route_info_t)); - if (!ri) - { - WAPI_STRERROR("malloc()"); - ret = -1; - break; - } - - /* Read and tokenize fields. */ - - sscanf(buf, "%s\t" /* ifname */ - "%x\t" /* dest */ - "%x\t" /* gw */ - "%x\t" /* flags */ - "%d\t" /* refcnt */ - "%d\t" /* use */ - "%d\t" /* metric */ - "%x\t" /* mask */ - "%d\t" /* mtu */ - "%d\t" /* window */ - "%d\t", /* irtt */ - ifname, &dest, &gw, &flags, &refcnt, &use, &metric, &netmask, &mtu, - &window, &irtt); - - /* Allocate "ifname". */ - - ri->ifname = malloc((strlen(ifname) + 1) * sizeof(char)); - if (!ri->ifname) - { - WAPI_STRERROR("malloc()"); - free(ri); - ret = -1; - break; - } - - /* Copy fields. */ - - sprintf(ri->ifname, "%s", ifname); - ri->dest.s_addr = dest; - ri->gw.s_addr = gw; - ri->flags = flags; - ri->refcnt = refcnt; - ri->use = use; - ri->metric = metric; - ri->netmask.s_addr = netmask; - ri->mtu = mtu; - ri->window = window; - ri->irtt = irtt; - - /* Push parsed node to the list. */ - - ri->next = list->head.route; - list->head.route = ri; - } - - /* Close file. */ - - fclose(fp); - return ret; -} - /**************************************************************************** * Name: wapi_add_route_gw * diff --git a/wireless/wapi/src/util.c b/wireless/wapi/src/util.c index 8b9379e46..6e379c1e2 100644 --- a/wireless/wapi/src/util.c +++ b/wireless/wapi/src/util.c @@ -1,8 +1,13 @@ /**************************************************************************** * apps/wireless/wapi/src/util.c * - * Copyright (c) 2010, Volkan YAZICI - * All rights reserved. + * Copyright (C) 2011, 2017Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Adapted for Nuttx from WAPI: + * + * Copyright (c) 2010, Volkan YAZICI + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -76,87 +81,7 @@ int wapi_make_socket(void) } /**************************************************************************** - * Name: wapi_get_ifnames - * - * Description: - * Parses WAPI_PROC_NET_WIRELESS. - * - * Returned Value: - * list Pushes collected wapi_string_t into this list. - * - ****************************************************************************/ - -int wapi_get_ifnames(FAR wapi_list_t *list) -{ - FILE *fp; - int ret; - size_t tmpsize = WAPI_PROC_LINE_SIZE * sizeof(char); - char tmp[WAPI_PROC_LINE_SIZE]; - - WAPI_VALIDATE_PTR(list); - - /* Open file for reading. */ - - fp = fopen(WAPI_PROC_NET_WIRELESS, "r"); - if (!fp) - { - WAPI_STRERROR("fopen(\"%s\", \"r\")", WAPI_PROC_NET_WIRELESS); - return -1; - } - - /* Skip first two lines. */ - - if (!fgets(tmp, tmpsize, fp) || !fgets(tmp, tmpsize, fp)) - { - WAPI_ERROR("Invalid \"%s\" content!\n", WAPI_PROC_NET_WIRELESS); - return -1; - } - - /* Iterate over available lines. */ - - ret = 0; - while (fgets(tmp, tmpsize, fp)) - { - char *beg; - char *end; - wapi_string_t *string; - - /* Locate the interface name region. */ - - for (beg = tmp; *beg && isspace(*beg); beg++); - for (end = beg; *end && *end != ':'; end++); - - /* Allocate both wapi_string_t and char vector. */ - - string = malloc(sizeof(wapi_string_t)); - if (string) - { - string->data = malloc(end - beg + sizeof(char)); - } - - if (!string || !string->data) - { - WAPI_STRERROR("malloc()"); - ret = -1; - break; - } - - /* Copy region into the buffer. */ - - snprintf(string->data, (end - beg + sizeof(char)), "%s", beg); - - /* Push string into the list. */ - - string->next = list->head.string; - list->head.string = string; - } - - fclose(fp); - return ret; -} - -/**************************************************************************** - * Name: wapi_get_ifnames + * Name: wapi_ioctl_command_name * * Description: * Return name string for IOCTL command diff --git a/wireless/wapi/src/util.h b/wireless/wapi/src/util.h index 4982e3155..50de0b6eb 100644 --- a/wireless/wapi/src/util.h +++ b/wireless/wapi/src/util.h @@ -1,8 +1,13 @@ /**************************************************************************** * apps/wireless/wapi/src/util.h * - * Copyright (c) 2010, Volkan YAZICI - * All rights reserved. + * Copyright (C) 2011, 2017Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Adapted for Nuttx from WAPI: + * + * Copyright (c) 2010, Volkan YAZICI + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: diff --git a/wireless/wapi/src/wapi.c b/wireless/wapi/src/wapi.c index c7a13ee3f..5b1aa6f70 100644 --- a/wireless/wapi/src/wapi.c +++ b/wireless/wapi/src/wapi.c @@ -1,8 +1,13 @@ /**************************************************************************** * apps/wireless/wapi/src/wapi.c * - * Copyright (c) 2010, Volkan YAZICI - * All rights reserved. + * Copyright (C) 2011, 2017Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Adapted for Nuttx from WAPI: + * + * Copyright (c) 2010, Volkan YAZICI + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -321,71 +326,6 @@ int wapi_main(int argc, char *argv[]) ifname = argv[1]; - /* Get ifnames */ - - bzero(&list, sizeof(wapi_list_t)); - ret = wapi_get_ifnames(&list); - printf("wapi_get_ifnames(): ret: %d", ret); - if (ret >= 0) - { - FAR wapi_string_t *str; - - /* Print ifnames */ - - printf(", ifnames:"); - for (str = list.head.string; str; str = str->next) - { - printf(" %s", str->data); - } - - /* Free ifnames */ - - str = list.head.string; - while (str) - { - FAR wapi_string_t *tmp; - - tmp = str->next; - free(str->data); - free(str); - str = tmp; - } - } - - putchar('\n'); - - /* Get routes */ - - bzero(&list, sizeof(wapi_list_t)); - ret = wapi_get_routes(&list); - printf("wapi_get_routes(): ret: %d\n", ret); - if (ret >= 0) - { - wapi_route_info_t *ri; - - /* Print route */ - - for (ri = list.head.route; ri; ri = ri->next) - { - printf(">> dest: %s, gw: %s, netmask: %s\n", - inet_ntoa(ri->dest), inet_ntoa(ri->gw), - inet_ntoa(ri->netmask)); - } - - /* Free routes */ - - ri = list.head.route; - while (ri) - { - FAR wapi_route_info_t *tmpri; - - tmpri = ri->next; - free(ri->ifname); - free(ri); - ri = tmpri; - } - } - /* Make a comm. sock. */ sock = wapi_make_socket(); diff --git a/wireless/wapi/src/wireless.c b/wireless/wapi/src/wireless.c index 8ee1bdc83..a1bf9a666 100644 --- a/wireless/wapi/src/wireless.c +++ b/wireless/wapi/src/wireless.c @@ -1,8 +1,13 @@ /**************************************************************************** * apps/wireless/wapi/src/wireless.c * - * Copyright (c) 2010, Volkan YAZICI - * All rights reserved. + * Copyright (C) 2011, 2017Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Adapted for Nuttx from WAPI: + * + * Copyright (c) 2010, Volkan YAZICI + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: From 2c53872a42a5fb762d81dcaeef57b5f07b527b4b Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 10 Apr 2017 16:14:49 -0600 Subject: [PATCH 3/6] wireless/wapi: Fix a few more compilation problems. --- wireless/wapi/src/wireless.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/wireless/wapi/src/wireless.c b/wireless/wapi/src/wireless.c index a1bf9a666..acd2eebd4 100644 --- a/wireless/wapi/src/wireless.c +++ b/wireless/wapi/src/wireless.c @@ -38,8 +38,9 @@ ****************************************************************************/ #include -#include #include +#include +#include #include @@ -378,7 +379,7 @@ int wapi_get_we_version(int sock, const char *ifname, FAR int *we_version) ****************************************************************************/ int wapi_get_freq(int sock, FAR const char *ifname, FAR double *freq, - FAR wapi_freq_flag_t *flag); + FAR wapi_freq_flag_t *flag) { struct iwreq wrq; int ret; @@ -421,8 +422,8 @@ int wapi_get_freq(int sock, FAR const char *ifname, FAR double *freq, * ****************************************************************************/ -int wapi_set_freq(int sock, FARconst char *ifname, double freq, - wapi_freq_flag_t flag); +int wapi_set_freq(int sock, FAR const char *ifname, double freq, + wapi_freq_flag_t flag) { struct iwreq wrq; int ret; @@ -804,6 +805,7 @@ int wapi_get_bitrate(int sock, FAR const char *ifname, if ((ret = ioctl(sock, SIOCGIWRATE, (unsigned long)((uintptr_t)&wrq))) >= 0) { /* Check if enabled. */ + if (wrq.u.bitrate.disabled) { WAPI_ERROR("Bitrate is disabled.\n"); @@ -811,12 +813,14 @@ int wapi_get_bitrate(int sock, FAR const char *ifname, } /* Get bitrate. */ + *bitrate = wrq.u.bitrate.value; *flag = wrq.u.bitrate.fixed ? WAPI_BITRATE_FIXED : WAPI_BITRATE_AUTO; } else { - WAPI_IOCTL_STRERROR(SIOCGIWRATE); + WAPI_IOCTL_STRERROR(SIOCGIWRATE); + } return ret; } @@ -942,7 +946,7 @@ int wapi_get_txpower(int sock, FAR const char *ifname, FAR int *power, ****************************************************************************/ int wapi_set_txpower(int sock, FAR const char *ifname, int power, - wapi_txpower_flag_t flag); + wapi_txpower_flag_t flag) { struct iwreq wrq; int ret; From dae395cb8e8d98c1d98e4351aa809b147b78d2b1 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 10 Apr 2017 18:50:50 -0600 Subject: [PATCH 4/6] Beginning real design of wapi application based on Wapi sample code. --- wireless/wapi/src/wapi.c | 576 +++++++++++++++++++++++++++++---------- 1 file changed, 437 insertions(+), 139 deletions(-) diff --git a/wireless/wapi/src/wapi.c b/wireless/wapi/src/wapi.c index 5b1aa6f70..12239aff9 100644 --- a/wireless/wapi/src/wapi.c +++ b/wireless/wapi/src/wapi.c @@ -4,7 +4,8 @@ * Copyright (C) 2011, 2017Gregory Nutt. All rights reserved. * Author: Gregory Nutt * - * Adapted for Nuttx from WAPI: + * Largely and original work, but highly influenced by sampled code provided + * with WAPI: * * Copyright (c) 2010, Volkan YAZICI * All rights reserved. @@ -47,203 +48,395 @@ #include "wireless/wapi.h" +/**************************************************************************** + * Private Functions + ****************************************************************************/ -/* Gets current configuration of the @a ifname using WAPI accessors and prints - * them in a pretty fashion with their corresponding return values. If a getter - * succeeds, we try to set that property with the same value to test the setters - * as well. - */ +/**************************************************************************** + * Name: wapi_show_command + * + * Description: + * Gets current configuration of the ifname using WAPI accessors and prints + * them in a pretty fashion with their corresponding return values. If a + * getter succeeds, we try to set that property with the same value to test + * the setters as well. + * + * Returned Value: + * None + * + ****************************************************************************/ -static void conf(int sock, FAR const char *ifname) +static void wapi_show_command(int sock, FAR const char *ifname) { - int ret; struct in_addr addr; + double freq; wapi_freq_flag_t freq_flag; + char essid[WAPI_ESSID_MAX_SIZE + 1]; wapi_essid_flag_t essid_flag; + wapi_mode_t mode; + struct ether_addr ap; + int bitrate; wapi_bitrate_flag_t bitrate_flag; + int txpower; wapi_txpower_flag_t txpower_flag; + int ret; + + printf("%s Configuration:\n", ifname); + /* Get ip */ bzero(&addr, sizeof(struct in_addr)); ret = wapi_get_ip(sock, ifname, &addr); - printf("wapi_get_ip(): ret: %d", ret); - if (ret >= 0) + if (ret < 0) { - printf(", ip: %s", inet_ntoa(addr)); - -#ifdef CONFIG_WIRELESS_WAPI_ENABLE_SET - /* Set ip (Make sure sin.sin_family is set to AF_INET.) */ - - ret = wapi_set_ip(sock, ifname, &addr); - printf("\nwapi_set_ip(): ret: %d", ret); -#endif + fprintf(stderr, "ERROR: wapi_get_ip() failed: %d", ret); + } + else + { + printf(" IP: %s\n", inet_ntoa(addr)); } - - putchar('\n'); /* Get netmask */ bzero(&addr, sizeof(struct in_addr)); ret = wapi_get_netmask(sock, ifname, &addr); - printf("wapi_get_netmask(): ret: %d", ret); - if (ret >= 0) + if (ret < 0) { - printf(", netmask: %s", inet_ntoa(addr)); - -#ifdef CONFIG_WIRELESS_WAPI_ENABLE_SET - /* set netmask (Make sure sin.sin_family is set to AF_INET.) */ - - ret = wapi_set_netmask(sock, ifname, &addr); - printf("\nwapi_set_netmask(): ret: %d", ret); -#endif + fprintf(stderr, "ERROR: wapi_get_netmask() failed: %d", ret); + } + else + { + printf(" NetMask: %s", inet_ntoa(addr)); } - putchar('\n'); - - /* Get freq */ + /* Get frequency */ ret = wapi_get_freq(sock, ifname, &freq, &freq_flag); - printf("wapi_get_freq(): ret: %d", ret); - if (ret >= 0) + if (ret < 0) + { + fprintf(stderr, "ERROR: wapi_get_freq() failed: %d", ret); + } + else { - int chan; double tmpfreq; + int chan; - printf(", freq: %g, freq_flag: %s", freq, g_wapi_freq_flags[freq_flag]); + printf("Frequency: %g\n", freq; + printf(" Flag: %s\n", g_wapi_freq_flags[freq_flag]); ret = wapi_freq2chan(sock, ifname, freq, &chan); - printf("\nwapi_freq2chan(): ret: %d", ret); - if (ret >= 0) + if (ret < 0) { - printf(", chan: %d", chan); + fprintf(stderr, "ERROR: wapi_freq2chan() failed: %d", ret); + } + else + { + printf(" Channel: %d\n", chan); } ret = wapi_chan2freq(sock, ifname, chan, &tmpfreq); - printf("\nwapi_chan2freq(): ret: %d", ret); - if (ret >= 0) + if (ret < 0) { - printf(", freq: %g", tmpfreq); + fprintf(stderr, "ERROR: wapi_chan2freq() failed: %d", ret); + } + else + { + printf("Frequency: %g\n", tmpfreq); } - -#ifdef CONFIG_WIRELESS_WAPI_ENABLE_SET - /* Set freq */ - - ret = wapi_set_freq(sock, ifname, freq, freq_flag); - printf("\nwapi_set_freq(): ret: %d", ret); -#endif } - putchar('\n'); - - /* Get essid */ + /* Get the ESSID */ ret = wapi_get_essid(sock, ifname, essid, &essid_flag); - printf("wapi_get_essid(): ret: %d", ret); - if (ret >= 0) + if (ret < 0) { - printf(", essid: %s, essid_flag: %s", - essid, g_wapi_essid_flags[essid_flag]); - -#ifdef CONFIG_WIRELESS_WAPI_ENABLE_SET - /* Set essid */ - - ret = wapi_set_essid(sock, ifname, essid, essid_flag); - printf("\nwapi_set_essid(): ret: %d", ret); -#endif + fprintf(stderr, "ERROR: wapi_get_essid() failed: %d", ret); + } + else + { + printf(" ESSID: %s\n", essid); + printf(" Flag: %s\n", g_wapi_essid_flags[essid_flag]); } - - putchar('\n'); /* Get operating mode */ ret = wapi_get_mode(sock, ifname, &mode); - printf("wapi_get_mode(): ret: %d", ret); - if (ret >= 0) + if (ret < 0) { - printf(", mode: %s", g_wapi_modes[mode]); - -#ifdef CONFIG_WIRELESS_WAPI_ENABLE_SET - /* Set operating mode */ - - ret = wapi_set_mode(sock, ifname, mode); - printf("\nwapi_set_mode(): ret: %d", ret); -#endif + fprintf(stderr, "ERROR: wapi_get_mode() failed: %d", ret); + } + else + { + printf(" Mode: %s", g_wapi_modes[mode]); } - putchar('\n'); - - /* Get ap */ + /* Get AP */ ret = wapi_get_ap(sock, ifname, &ap); - printf("wapi_get_ap(): ret: %d", ret); - if (ret >= 0) + if (ret < 0) { - printf(", ap: %02X:%02X:%02X:%02X:%02X:%02X", + fprintf(stderr, "ERROR: wapi_get_ap() failed: %d", ret); + } + else + { + printf(" AP: %02x:%02x:%02x:%02x:%02x:%02x", ap.ether_addr_octet[0], ap.ether_addr_octet[1], ap.ether_addr_octet[2], ap.ether_addr_octet[3], ap.ether_addr_octet[4], ap.ether_addr_octet[5]); - -#ifdef CONFIG_WIRELESS_WAPI_ENABLE_SET - /* Set ap */ - - ret = wapi_set_ap(sock, ifname, &ap); - printf("\nwapi_set_ap(): ret: %d", ret); -#endif } - putchar('\n'); - /* Get bitrate */ ret = wapi_get_bitrate(sock, ifname, &bitrate, &bitrate_flag); - printf("wapi_get_bitrate(): ret: %d", ret); - if (ret >= 0) + if (ret < 0) { - printf(", bitrate: %d, bitrate_flag: %s", bitrate, - g_wapi_bitrate_flags[bitrate_flag]); - -#ifdef CONFIG_WIRELESS_WAPI_ENABLE_SET - /* Set bitrate */ - - ret = wapi_set_bitrate(sock, ifname, bitrate, bitrate_flag); - printf("\nwapi_set_bitrate(): ret: %d", ret); -#endif + fprintf(stderr, "ERROR: wapi_get_bitrate() failed: %d", ret); + } + else + { + printf(" BitRate: %d\n", bitrate); + printf(" Flag: %s\n", g_wapi_bitrate_flags[bitrate_flag]); } - - putchar('\n'); /* Get txpower */ ret = wapi_get_txpower(sock, ifname, &txpower, &txpower_flag); - printf("wapi_get_txpower(): ret: %d", ret); - if (ret >= 0) + if (ret < 0) { - printf(", txpower: %d, txpower_flag: %s", - txpower, g_wapi_txpower_flags[txpower_flag]); - -#ifdef CONFIG_WIRELESS_WAPI_ENABLE_SET - /* Set txpower */ - - ret = wapi_set_txpower(sock, ifname, txpower, txpower_flag); - printf("\nwapi_set_txpower(): ret: %d", ret); -#endif + fprintf(stderr, "ERROR: wapi_get_txpower() failed: %d", ret); + } + else + { + printf(" TxPower: %d\n", txpower); + printf(" Flag: %s\n", g_wapi_txpower_flags[txpower_flag]); } - - putchar('\n'); } -/* Scans available APs in the range using given @a ifname interface. (Requires - * root privileges to start a scan.) - */ +/**************************************************************************** + * Name: wapi_setip_cmd + * + * Description: + * Set the IP address. + * + * Returned Value: + * None + * + ****************************************************************************/ -static void scan(int sock, FAR const char *ifname) +static void wapi_setip_cmd(int sock, FAR const char *addrstr) +{ + struct in_addr addr; + int ret; + + /* Format the request */ +#warning Missing logic + + /* Set the IP address */ + + ret = wapi_set_ip(sock, ifname, &addr); + if (ret < 0) + { + fprintf(stderr, "ERROR: wapi_set_ip() failed: %d", ret); + } +} + +/**************************************************************************** + * Name: wapi_setmask_cmd + * + * Description: + * Set the network mask + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_setmask_cmd(int sock, FAR const char *maskstr) +{ + struct in_addr addr; + int ret; + + /* Format the request */ +#warning Missing logic + + /* Set the network mask */ + + ret = wapi_set_netmask(sock, ifname, &addr); + if (ret < 0) + { + fprintf(stderr, "ERROR: wapi_set_netmask() failed: %d", ret); + } +} + +/**************************************************************************** + * Name: wapi_setfreq_cmd + * + * Description: + * Set the frequency + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_setfreq_cmd(int sock, double frequency, + wapi_freq_flag_t freq_flag) +{ + int ret; + + /* Set the network mask */ + + ret = wapi_set_freq(sock, ifname, freq, freq_flag); + if (ret < 0) + { + fprintf(stderr, "ERROR: \nwapi_set_freq() failed: %d", ret); + } +} + +/**************************************************************************** + * Name: wapi_setessid_cmd + * + * Description: + * Set the frequency + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_setessid_cmd(int sock, FAR char *essid, + wapi_essid_flag_t essid_flag) +{ + int ret; + + /* Set essid */ + + ret = wapi_set_essid(sock, ifname, essid, essid_flag); + if (ret < 0) + { + fprintf(stderr, "ERROR: wapi_set_essid() failed: %d", ret); + } +} + +/**************************************************************************** + * Name: wapi_setmode_cmd + * + * Description: + * Set the operating mode + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_setmode_cmd(int sock, FAR char *ifname, wapi_mode_t mode) +{ + int ret; + + /* Set operating mode */ + + ret = wapi_set_mode(sock, ifname, mode); + if (ret < 0) + { + fprintf(stderr, "ERROR: \nwapi_set_mode() failed: %d", ret); + } +} + +/**************************************************************************** + * Name: wapi_setap_cmd + * + * Description: + * Set the AP + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_setap_cmd(int sock, FAR char *ifname, + FAR struct ether_addr *ap) +{ + int ret; + + /* Set ap */ + + ret = wapi_set_ap(sock, ifname, ap); + if (ret < 0) + { + fprintf(stderr, "ERROR: \nwapi_set_ap() failed: %d", ret); + } +} + +/**************************************************************************** + * Name: wapi_setbitrate_cmd + * + * Description: + * Set the bit rate + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_setbitrate_cmd(int sock, int bitrate, + wapi_bitrate_flag_t bitrate_flag) +{ + int ret; + + /* Set bitrate */ + + ret = wapi_set_bitrate(sock, ifname, bitrate, bitrate_flag); + if (ret < 0) + { + fprintf(stderr, "ERROR: \nwapi_set_bitrate() failed: %d", ret); + } +} + +/**************************************************************************** + * Name: wapi_txpower_cmd + * + * Description: + * Set the TX power + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_txpower_cmd(int sock, int txpower, + wapi_txpower_flag_t txpower_flag) +{ + + int ret; + + /* Set txpower */ + + ret = wapi_set_txpower(sock, ifname, txpower, txpower_flag); + if (ret < 0) + { + fprintf(stderr, "ERROR: \nwapi_set_txpower() failed: %d", ret); + } +} + +/**************************************************************************** + * Name: wapi_scan + * + * Description: + * Scans available APs in the range using given ifname interface. + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_scan(int sock, FAR const char *ifname) { int sleepdur = 1; int sleeptries = 5; @@ -254,7 +447,7 @@ static void scan(int sock, FAR const char *ifname) /* Start scan */ ret = wapi_scan_init(sock, ifname); - printf("wapi_scan_init(): ret: %d\n", ret); + fprintf(stderr, "ERROR: wapi_scan_init() failed: %d\n", ret); /* Wait for completion */ @@ -262,7 +455,7 @@ static void scan(int sock, FAR const char *ifname) { sleep(sleepdur); ret = wapi_scan_stat(sock, ifname); - printf("wapi_scan_stat(): ret: %d, sleeptries: %d\n", ret, sleeptries); + fprintf(stderr, "ERROR: wapi_scan_stat() failed: %d, sleeptries: %d\n", ret, sleeptries); } while (--sleeptries > 0 && ret > 0); @@ -275,7 +468,10 @@ static void scan(int sock, FAR const char *ifname) bzero(&list, sizeof(wapi_list_t)); ret = wapi_scan_coll(sock, ifname, &list); - printf("wapi_scan_coll(): ret: %d\n", ret); + if (ret < 0) + { + fprintf(stderr, "ERROR: wapi_scan_coll() failed: %d\n", ret); + } /* Print found aps */ @@ -301,6 +497,34 @@ static void scan(int sock, FAR const char *ifname) } } +/**************************************************************************** + * Name: wapi_showusage + * + * Description: + * Show program usage. + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_showusage(FAR const char *progname, int exitcode) +{ + fprintf(stderr, "Usage: %s show \n", progname); + fprintf(stderr, " %s setip OPTIONS\n", progname); + fprintf(stderr, " %s setmask OPTIONS\n", progname); + fprintf(stderr, " %s setfreq OPTIONS\n", progname); + fprintf(stderr, " %s setessid OPTIONS\n", progname); + fprintf(stderr, " %s setmode OPTIONS\n", progname); + fprintf(stderr, " %s setap OPTIONS\n", progname); + fprintf(stderr, " %s setbitrate OPTIONS\n", progname); + fprintf(stderr, " %s txpower OPTIONS\n", progname); + fprintf(stderr, " %s scan \n", progname); + fprintf(stderr, " %s help\n", progname); + + exit(exitcode); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -311,6 +535,7 @@ int main(int argc, FAR char *argv[]) int wapi_main(int argc, char *argv[]) #endif { + FAR const char *cmd; FAR const char *ifname; wapi_list_t list; int ret; @@ -318,32 +543,105 @@ int wapi_main(int argc, char *argv[]) /* Check command line args */ - if (argc != 2) + if (argc == 1 && strcmp(argv[1], "help") == 0) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return EXIT_FAILURE; + wapi_showusage(argv[0], EXIT_SUCCESS); + } + else if (argc < 3) + { + fprintf(stderr, "ERROR: Too few command line arguments\n"); + wapi_showusage(argv[0], EXIT_FAILURE); } - ifname = argv[1]; + cmd = argv[1]; + ifname = argv[2]; - /* Make a comm. sock. */ + /* Create a communication sock. */ sock = wapi_make_socket(); - printf("wapi_make_socket(): sock: %d\n", sock); + if (sock < 0) + { + fprintf(stderr, "ERROR: wapi_make_socket() failed: %d\n", sock); + } - /* List conf */ + /* Execute command */ - printf("\nconf\n"); - printf("------------\n"); - conf(sock, ifname); + if (strcmp(cmd, "show") + { + wapi_show_command(sock, ifname); + } + else if (strcmp(cmd, "setip") + { + /* Parse command specific options */ +#warning Missing logic * - /* Scan aps */ + /* Execute the command */ + //wapi_setip_cmd(sock, addrstr); + } + else if (strcmp(cmd, "setmask") + { + /* Parse command specific options */ +#warning Missing logic * - printf("\nscan\n"); - printf("----\n"); - scan(sock, ifname); + /* Execute the command */ + //wapi_setmask_cmd(sock, maskstr); + } + else if (strcmp(cmd, "setfreq") + { + /* Parse command specific options */ +#warning Missing logic * - /* Close comm. sock. */ + /* Execute the command */ + //wapi_setfreq_cmd(sock, frequency, freq_flag); + } + else if (strcmp(cmd, "setessid") + { + /* Parse command specific options */ +#warning Missing logic * + + /* Execute the command */ + //wapi_setessid_cmd(sock, essid, essid_flag); + } + else if (strcmp(cmd, "setmode") + { + /* Parse command specific options */ +#warning Missing logic * + + /* Execute the command */ + //wapi_setmode_cmd(sock, ifname, mode); + } + else if (strcmp(cmd, "setbitrate") + { + /* Parse command specific options */ +#warning Missing logic * + + /* Execute the command */ + //wapi_setap_cmd(sock, ifname, ap); + } + else if (strcmp(cmd, "txpower") + { + /* Parse command specific options */ +#warning Missing logic * + + /* Execute the command */ + //wapi_setbitrate_cmd(sock, bitrate, bitrate_flag); + } + else if (strcmp(cmd, "scan") + { + wapi_scan(sock, ifname); + } + else if (strcmp(cmd, "help") + { + fprintf(stderr, "WARNING: Garbage after help common ignored.\n"); + wapi_showusage(argv[0], EXIT_SUCCESS); + } + else + { + fprintf(stderr, "WARNING: Unrecognized command: %s\n", cmd); + wapi_showusage(argv[0], EXIT_FAILURE); + } + + /* Close communication socket */ close(sock); return EXIT_SUCCESS; From 1a470c6480caa83365a0d623524052af4f67ac6c Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 11 Apr 2017 10:22:47 -0600 Subject: [PATCH 5/6] wireless/wapi: Re-write of wapi tool UI. --- include/wireless/wapi.h | 15 +- wireless/wapi/Kconfig | 4 - wireless/wapi/src/wapi.c | 513 ++++++++++++++++++++++++++--------- wireless/wapi/src/wireless.c | 2 + 4 files changed, 397 insertions(+), 137 deletions(-) diff --git a/include/wireless/wapi.h b/include/wireless/wapi.h index 9bbc7d6f7..8713d1acb 100644 --- a/include/wireless/wapi.h +++ b/include/wireless/wapi.h @@ -93,13 +93,14 @@ typedef enum typedef enum { - WAPI_MODE_AUTO = IW_MODE_AUTO, /* Driver decides. */ - WAPI_MODE_ADHOC = IW_MODE_ADHOC, /* Single cell network. */ - WAPI_MODE_MANAGED = IW_MODE_INFRA, /* Multi cell network, roaming, ... */ - WAPI_MODE_MASTER = IW_MODE_MASTER, /* Synchronisation master or access point. */ - WAPI_MODE_REPEAT = IW_MODE_REPEAT, /* Wireless repeater, forwarder. */ - WAPI_MODE_SECOND = IW_MODE_SECOND, /* Secondary master/repeater, backup. */ - WAPI_MODE_MONITOR = IW_MODE_MONITOR /* Passive monitor, listen only. */ + WAPI_MODE_AUTO = IW_MODE_AUTO, /* Driver decides. */ + WAPI_MODE_ADHOC = IW_MODE_ADHOC, /* Single cell network. */ + WAPI_MODE_MANAGED = IW_MODE_INFRA, /* Multi cell network, roaming, ... */ + WAPI_MODE_MASTER = IW_MODE_MASTER, /* Synchronisation master or access point. */ + WAPI_MODE_REPEAT = IW_MODE_REPEAT, /* Wireless repeater, forwarder. */ + WAPI_MODE_SECOND = IW_MODE_SECOND, /* Secondary master/repeater, backup. */ + WAPI_MODE_MONITOR = IW_MODE_MONITOR, /* Passive monitor, listen only. */ + WAPI_MODE_MESH = IW_MODE_MESH /* Mesh (IEEE 802.11s) network */ } wapi_mode_t; /* Bitrate flags. diff --git a/wireless/wapi/Kconfig b/wireless/wapi/Kconfig index 4e8b7edcc..249a1593d 100644 --- a/wireless/wapi/Kconfig +++ b/wireless/wapi/Kconfig @@ -21,10 +21,6 @@ config WIRELESS_WAPI_CMDTOOL selected than a simple command line tool that can be ran from NSH will also be generated. -config WIRELESS_WAPI_ENABLE_SET - bool "Enable Setting Options" - default n - config WIRELESS_WAPI_PROGNAME string "Program Name" default "wapi" diff --git a/wireless/wapi/src/wapi.c b/wireless/wapi/src/wapi.c index 12239aff9..03fa6ba71 100644 --- a/wireless/wapi/src/wapi.c +++ b/wireless/wapi/src/wapi.c @@ -41,19 +41,141 @@ #include #include #include -#include +#include #include #include #include #include "wireless/wapi.h" +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* Describes one command */ + +struct wapi_command_s +{ + FAR const char *name; + uint8_t noptions; + CODE void *handler; +}; + +/* Generic form of a commnd handler */ + +typedef void (*cmd1_t)(int sock, FAR const char *arg1); +typedef void (*cmd2_t)(int sock, FAR const char *arg1, + FAR const char *arg2); +typedef void (*cmd3_t)(int sock, FAR const char *arg1, + FAR const char *arg2, FAR const char *arg3); + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int wapi_str2int(FAR const char *str); +static double wapi_str2double(FAR const char *str); + +static void wapi_show_cmd(int sock, FAR const char *ifname); +static void wapi_ip_cmd(int sock, FAR const char *ifname, + FAR const char *addrstr); +static void wapi_mask_cmd(int sock, FAR const char *ifname, + FAR const char *maskstr); +static void wapi_freq_cmd(int sock, FAR const char *ifname, + FAR const char *freqstr, FAR const char *flagstr); +static void wapi_essid_cmd(int sock, FAR const char *ifname, + FAR const char *essid, FAR const char *flagstr); +static void wapi_mode_cmd(int sock, FAR const char *ifname, + FAR const char *modestr); +static void wapi_ap_cmd(int sock, FAR const char *ifname, + FAR const char *macstr); +static void wapi_bitrate_cmd(int sock, FAR const char *ifname, + FAR const char *ratestr, FAR const char *flagstr); +static void wapi_txpower_cmd(int sock, FAR const char *ifname, + FAR const char *pwrstr, FAR const char *flagstr); +static void wapi_scan_cmd(int sock, FAR const char *ifname); + +static void wapi_showusage(FAR const char *progname, int exitcode); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct wapi_command_s g_wapi_commands[] = +{ + {"help", 0, (CODE void *)NULL}, + {"show", 1, (CODE void *)wapi_show_cmd}, + {"scan", 1, (CODE void *)wapi_scan_cmd}, + {"ip", 2, (CODE void *)wapi_ip_cmd}, + {"mask", 2, (CODE void *)wapi_mask_cmd}, + {"freq", 3, (CODE void *)wapi_freq_cmd}, + {"essid", 3, (CODE void *)wapi_essid_cmd}, + {"mode", 2, (CODE void *)wapi_mode_cmd}, + {"ap", 2, (CODE void *)wapi_ap_cmd}, + {"bitrate", 3, (CODE void *)wapi_bitrate_cmd}, + {"txpower", 2, (CODE void *)wapi_txpower_cmd}, +}; + +#define NCOMMANDS (sizeof(g_wapi_commands) / sizeof(struct wapi_command_s)) + /**************************************************************************** * Private Functions ****************************************************************************/ /**************************************************************************** - * Name: wapi_show_command + * Name: wapi_str2int + * + * Description: + * Convert a string to an integer value + * + ****************************************************************************/ + +static int wapi_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: wapi_str2double + * + * Description: + * Convert a string to a double value + * + ****************************************************************************/ + +static double wapi_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: wapi_show_cmd * * Description: * Gets current configuration of the ifname using WAPI accessors and prints @@ -66,7 +188,7 @@ * ****************************************************************************/ -static void wapi_show_command(int sock, FAR const char *ifname) +static void wapi_show_cmd(int sock, FAR const char *ifname) { struct in_addr addr; @@ -128,7 +250,7 @@ static void wapi_show_command(int sock, FAR const char *ifname) double tmpfreq; int chan; - printf("Frequency: %g\n", freq; + printf("Frequency: %g\n", freq); printf(" Flag: %s\n", g_wapi_freq_flags[freq_flag]); ret = wapi_freq2chan(sock, ifname, freq, &chan); @@ -220,7 +342,7 @@ static void wapi_show_command(int sock, FAR const char *ifname) } /**************************************************************************** - * Name: wapi_setip_cmd + * Name: wapi_ip_cmd * * Description: * Set the IP address. @@ -230,13 +352,15 @@ static void wapi_show_command(int sock, FAR const char *ifname) * ****************************************************************************/ -static void wapi_setip_cmd(int sock, FAR const char *addrstr) +static void wapi_ip_cmd(int sock, FAR const char *ifname, + FAR const char *addrstr) { struct in_addr addr; int ret; /* Format the request */ -#warning Missing logic + + addr.s_addr = inet_addr(addrstr); /* Set the IP address */ @@ -248,7 +372,7 @@ static void wapi_setip_cmd(int sock, FAR const char *addrstr) } /**************************************************************************** - * Name: wapi_setmask_cmd + * Name: wapi_mask_cmd * * Description: * Set the network mask @@ -258,13 +382,15 @@ static void wapi_setip_cmd(int sock, FAR const char *addrstr) * ****************************************************************************/ -static void wapi_setmask_cmd(int sock, FAR const char *maskstr) +static void wapi_mask_cmd(int sock, FAR const char *ifname, + FAR const char *maskstr) { struct in_addr addr; int ret; /* Format the request */ -#warning Missing logic + + addr.s_addr = inet_addr(maskstr); /* Set the network mask */ @@ -276,7 +402,7 @@ static void wapi_setmask_cmd(int sock, FAR const char *maskstr) } /**************************************************************************** - * Name: wapi_setfreq_cmd + * Name: wapi_freq_cmd * * Description: * Set the frequency @@ -286,14 +412,38 @@ static void wapi_setmask_cmd(int sock, FAR const char *maskstr) * ****************************************************************************/ -static void wapi_setfreq_cmd(int sock, double frequency, - wapi_freq_flag_t freq_flag) +static void wapi_freq_cmd(int sock, FAR const char *ifname, + FAR const char *freqstr, FAR const char *flagstr) { + double frequency; + wapi_freq_flag_t freq_flag; + bool found = false; int ret; + int i; - /* Set the network mask */ + /* Convert input strings to values */ - ret = wapi_set_freq(sock, ifname, freq, freq_flag); + frequency = wapi_str2double(freqstr); + + for (i = 0; i < IW_FREQ_NFLAGS; i++) + { + if (strcmp(flagstr, g_wapi_freq_flags[i]) == 0) + { + freq_flag = (wapi_freq_flag_t)i; + found = true; + break; + } + } + + if (!found) + { + fprintf(stderr, "ERROR: Invalid frequency flag: %s\n", flagstr); + exit(EXIT_FAILURE); + } + + /* Set the frequency */ + + ret = wapi_set_freq(sock, ifname, frequency, freq_flag); if (ret < 0) { fprintf(stderr, "ERROR: \nwapi_set_freq() failed: %d", ret); @@ -301,22 +451,43 @@ static void wapi_setfreq_cmd(int sock, double frequency, } /**************************************************************************** - * Name: wapi_setessid_cmd + * Name: wapi_essid_cmd * * Description: - * Set the frequency + * Set the ESSID * * Returned Value: * None * ****************************************************************************/ -static void wapi_setessid_cmd(int sock, FAR char *essid, - wapi_essid_flag_t essid_flag) +static void wapi_essid_cmd(int sock, FAR const char *ifname, + FAR const char *essid, FAR const char *flagstr) { + wapi_essid_flag_t essid_flag; + bool found = false; int ret; + int i; - /* Set essid */ + /* Convert input strings to values */ + + for (i = 0; i < 2; i++) + { + if (strcmp(flagstr, g_wapi_essid_flags[i]) == 0) + { + essid_flag = (wapi_essid_flag_t)i; + found = true; + break; + } + } + + if (!found) + { + fprintf(stderr, "ERROR: Invalid ESSID flag: %s\n", flagstr); + exit(EXIT_FAILURE); + } + + /* Set the ESSID */ ret = wapi_set_essid(sock, ifname, essid, essid_flag); if (ret < 0) @@ -326,7 +497,7 @@ static void wapi_setessid_cmd(int sock, FAR char *essid, } /**************************************************************************** - * Name: wapi_setmode_cmd + * Name: wapi_mode_cmd * * Description: * Set the operating mode @@ -336,9 +507,31 @@ static void wapi_setessid_cmd(int sock, FAR char *essid, * ****************************************************************************/ -static void wapi_setmode_cmd(int sock, FAR char *ifname, wapi_mode_t mode) +static void wapi_mode_cmd(int sock, FAR const char *ifname, + FAR const char *modestr) { + wapi_mode_t mode; + bool found = false; int ret; + int i; + + /* Convert input strings to values */ + + for (i = 0; i < IW_MODE_NFLAGS; i++) + { + if (strcmp(modestr, g_wapi_modes[i]) == 0) + { + mode = (wapi_mode_t)i; + found = true; + break; + } + } + + if (!found) + { + fprintf(stderr, "ERROR: Invalid operating mode: %s\n", modestr); + exit(EXIT_FAILURE); + } /* Set operating mode */ @@ -350,7 +543,7 @@ static void wapi_setmode_cmd(int sock, FAR char *ifname, wapi_mode_t mode) } /**************************************************************************** - * Name: wapi_setap_cmd + * Name: wapi_ap_cmd * * Description: * Set the AP @@ -360,14 +553,22 @@ static void wapi_setmode_cmd(int sock, FAR char *ifname, wapi_mode_t mode) * ****************************************************************************/ -static void wapi_setap_cmd(int sock, FAR char *ifname, - FAR struct ether_addr *ap) +static void wapi_ap_cmd(int sock, FAR const char *ifname, + FAR const char *macstr) { + struct ether_addr ap; int ret; + /* Convert input strings to values */ + + sscanf(macstr, "%02x:%02x:%02x:%02x:%02x:%02x", + &ap.ether_addr_octet[0], &ap.ether_addr_octet[1], + &ap.ether_addr_octet[2], &ap.ether_addr_octet[3], + &ap.ether_addr_octet[4], &ap.ether_addr_octet[5]); + /* Set ap */ - ret = wapi_set_ap(sock, ifname, ap); + ret = wapi_set_ap(sock, ifname, &ap); if (ret < 0) { fprintf(stderr, "ERROR: \nwapi_set_ap() failed: %d", ret); @@ -375,7 +576,7 @@ static void wapi_setap_cmd(int sock, FAR char *ifname, } /**************************************************************************** - * Name: wapi_setbitrate_cmd + * Name: wapi_bitrate_cmd * * Description: * Set the bit rate @@ -385,10 +586,35 @@ static void wapi_setap_cmd(int sock, FAR char *ifname, * ****************************************************************************/ -static void wapi_setbitrate_cmd(int sock, int bitrate, - wapi_bitrate_flag_t bitrate_flag) +static void wapi_bitrate_cmd(int sock, FAR const char *ifname, + FAR const char *ratestr, FAR const char *flagstr) + { + wapi_bitrate_flag_t bitrate_flag; + bool found = false; + int bitrate; int ret; + int i; + + /* Convert input strings to values */ + + bitrate = wapi_str2int(ratestr); + + for (i = 0; i < 2; i++) + { + if (strcmp(flagstr, g_wapi_bitrate_flags[i]) == 0) + { + bitrate_flag = (wapi_bitrate_flag_t)i; + found = true; + break; + } + } + + if (!found) + { + fprintf(stderr, "ERROR: Invalid bitrate flag: %s\n", flagstr); + exit(EXIT_FAILURE); + } /* Set bitrate */ @@ -410,11 +636,34 @@ static void wapi_setbitrate_cmd(int sock, int bitrate, * ****************************************************************************/ -static void wapi_txpower_cmd(int sock, int txpower, - wapi_txpower_flag_t txpower_flag) +static void wapi_txpower_cmd(int sock, FAR const char *ifname, + FAR const char *pwrstr, FAR const char *flagstr) { - + wapi_txpower_flag_t txpower_flag; + bool found = false; + int txpower; int ret; + int i; + + /* Convert input strings to values */ + + txpower = wapi_str2int(pwrstr); + + for (i = 0; i < 3; i++) + { + if (strcmp(flagstr, g_wapi_txpower_flags[i]) == 0) + { + txpower_flag = (wapi_txpower_flag_t)i; + found = true; + break; + } + } + + if (!found) + { + fprintf(stderr, "ERROR: Invalid TX power flag: %s\n", flagstr); + exit(EXIT_FAILURE); + } /* Set txpower */ @@ -426,7 +675,7 @@ static void wapi_txpower_cmd(int sock, int txpower, } /**************************************************************************** - * Name: wapi_scan + * Name: wapi_scan_cmd * * Description: * Scans available APs in the range using given ifname interface. @@ -436,7 +685,7 @@ static void wapi_txpower_cmd(int sock, int txpower, * ****************************************************************************/ -static void wapi_scan(int sock, FAR const char *ifname) +static void wapi_scan_cmd(int sock, FAR const char *ifname) { int sleepdur = 1; int sleeptries = 5; @@ -510,18 +759,52 @@ static void wapi_scan(int sock, FAR const char *ifname) static void wapi_showusage(FAR const char *progname, int exitcode) { + int i; + fprintf(stderr, "Usage: %s show \n", progname); - fprintf(stderr, " %s setip OPTIONS\n", progname); - fprintf(stderr, " %s setmask OPTIONS\n", progname); - fprintf(stderr, " %s setfreq OPTIONS\n", progname); - fprintf(stderr, " %s setessid OPTIONS\n", progname); - fprintf(stderr, " %s setmode OPTIONS\n", progname); - fprintf(stderr, " %s setap OPTIONS\n", progname); - fprintf(stderr, " %s setbitrate OPTIONS\n", progname); - fprintf(stderr, " %s txpower OPTIONS\n", progname); fprintf(stderr, " %s scan \n", progname); + fprintf(stderr, " %s ip \n", progname); + fprintf(stderr, " %s mask \n", progname); + fprintf(stderr, " %s freq \n", progname); + fprintf(stderr, " %s essid \n", progname); + fprintf(stderr, " %s mode \n", progname); + fprintf(stderr, " %s ap \n", progname); + fprintf(stderr, " %s bitrate \n", progname); + fprintf(stderr, " %s txpower \n", progname); fprintf(stderr, " %s help\n", progname); + fprintf(stderr, "\nFrequency Flags:\n"); + for (i = 0; i < IW_FREQ_NFLAGS; i++) + { + fprintf(stderr, " %s", g_wapi_freq_flags[i]); + } + + fprintf(stderr, "\nESSID Flags:\n"); + for (i = 0; i < 2; i++) + { + fprintf(stderr, " %s", g_wapi_essid_flags[i]); + } + + fprintf(stderr, "\nOperating Modes:\n"); + for (i = 0; i < IW_MODE_NFLAGS; i++) + { + fprintf(stderr, " %s", g_wapi_modes[i]); + } + + fprintf(stderr, "\nBitrate Flags:\n"); + for (i = 0; i < 6; i++) + { + fprintf(stderr, " %s", g_wapi_bitrate_flags[i]); + } + + fprintf(stderr, "\nTX power Flags:\n"); + for (i = 0; i < 3; i++) + { + fprintf(stderr, " %s", g_wapi_txpower_flags[i]); + } + + putchar('\n'); + exit(exitcode); } @@ -535,28 +818,61 @@ int main(int argc, FAR char *argv[]) int wapi_main(int argc, char *argv[]) #endif { - FAR const char *cmd; - FAR const char *ifname; - wapi_list_t list; - int ret; + FAR const char *cmdname; + FAR const struct wapi_command_s *wapicmd; int sock; + int i; - /* Check command line args */ + /* Get the command */ - if (argc == 1 && strcmp(argv[1], "help") == 0) + if (argc < 2) { - wapi_showusage(argv[0], EXIT_SUCCESS); - } - else if (argc < 3) - { - fprintf(stderr, "ERROR: Too few command line arguments\n"); + fprintf(stderr, "ERROR: Missing command\n"); wapi_showusage(argv[0], EXIT_FAILURE); } - cmd = argv[1]; - ifname = argv[2]; + cmdname = argv[1]; - /* Create a communication sock. */ + /* Find the command in the g_wapi_command[] list */ + + wapicmd = NULL; + for (i = 0; i < NCOMMANDS; i++) + { + FAR const struct wapi_command_s *cmd = &g_wapi_commands[i]; + if (strcmp(cmdname, cmd->name) == 0) + { + wapicmd = cmd; + break; + } + } + + if (wapicmd == NULL) + { + fprintf(stderr, "ERROR: Unsupported command: %s\n", cmdname); + wapi_showusage(argv[0], EXIT_FAILURE); + } + + if (wapicmd->noptions + 2 < argc) + { + fprintf(stderr, "ERROR: Garbage at end of command ignored\n"); + } + else if (wapicmd->noptions + 2 > argc) + { + fprintf(stderr, "ERROR: Missing required command options: %s\n", + cmdname); + wapi_showusage(argv[0], EXIT_FAILURE); + } + + /* Special case the help command which has no arguments, no handler, + * and does not need a socket. + */ + + if (wapicmd->handler == NULL) + { + wapi_showusage(argv[0], EXIT_SUCCESS); + } + + /* Create a communication socket */ sock = wapi_make_socket(); if (sock < 0) @@ -564,81 +880,26 @@ int wapi_main(int argc, char *argv[]) fprintf(stderr, "ERROR: wapi_make_socket() failed: %d\n", sock); } - /* Execute command */ + /* Dispatch the command handling */ - if (strcmp(cmd, "show") + switch (wapicmd->noptions) { - wapi_show_command(sock, ifname); - } - else if (strcmp(cmd, "setip") - { - /* Parse command specific options */ -#warning Missing logic * + default: + case 0: + fprintf(stderr, "ERROR: Internal craziness\n"); + wapi_showusage(argv[0], EXIT_FAILURE); - /* Execute the command */ - //wapi_setip_cmd(sock, addrstr); - } - else if (strcmp(cmd, "setmask") - { - /* Parse command specific options */ -#warning Missing logic * + case 1: + ((cmd1_t)wapicmd->handler)(sock, argv[2]); + break; - /* Execute the command */ - //wapi_setmask_cmd(sock, maskstr); - } - else if (strcmp(cmd, "setfreq") - { - /* Parse command specific options */ -#warning Missing logic * + case 2: + ((cmd2_t)wapicmd->handler)(sock, argv[2], argv[3]); + break; - /* Execute the command */ - //wapi_setfreq_cmd(sock, frequency, freq_flag); - } - else if (strcmp(cmd, "setessid") - { - /* Parse command specific options */ -#warning Missing logic * - - /* Execute the command */ - //wapi_setessid_cmd(sock, essid, essid_flag); - } - else if (strcmp(cmd, "setmode") - { - /* Parse command specific options */ -#warning Missing logic * - - /* Execute the command */ - //wapi_setmode_cmd(sock, ifname, mode); - } - else if (strcmp(cmd, "setbitrate") - { - /* Parse command specific options */ -#warning Missing logic * - - /* Execute the command */ - //wapi_setap_cmd(sock, ifname, ap); - } - else if (strcmp(cmd, "txpower") - { - /* Parse command specific options */ -#warning Missing logic * - - /* Execute the command */ - //wapi_setbitrate_cmd(sock, bitrate, bitrate_flag); - } - else if (strcmp(cmd, "scan") - { - wapi_scan(sock, ifname); - } - else if (strcmp(cmd, "help") - { - fprintf(stderr, "WARNING: Garbage after help common ignored.\n"); - wapi_showusage(argv[0], EXIT_SUCCESS); - } - else - { - fprintf(stderr, "WARNING: Unrecognized command: %s\n", cmd); - wapi_showusage(argv[0], EXIT_FAILURE); + case 3: + ((cmd3_t)wapicmd->handler)(sock, argv[2], argv[3], argv[4]); + break; } /* Close communication socket */ diff --git a/wireless/wapi/src/wireless.c b/wireless/wapi/src/wireless.c index acd2eebd4..12f9451a1 100644 --- a/wireless/wapi/src/wireless.c +++ b/wireless/wapi/src/wireless.c @@ -37,6 +37,7 @@ * Included Files ****************************************************************************/ +#include #include #include #include @@ -99,6 +100,7 @@ FAR const char *g_wapi_modes[] = "WAPI_MODE_REPEAT", "WAPI_MODE_SECOND", "WAPI_MODE_MONITOR" + "WAPI_MODE_MESH" }; /* Bit Rate */ From 568e932e809f987c3160281df5fcde038add36d5 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 11 Apr 2017 12:42:15 -0600 Subject: [PATCH 6/6] apps/wireless/wapi: Some changes from initial debug. --- include/wireless/wapi.h | 16 ----- wireless/wapi/src/util.c | 28 ++++++++- wireless/wapi/src/wapi.c | 95 ++++++++++++++-------------- wireless/wapi/src/wireless.c | 116 ++++++++++------------------------- 4 files changed, 104 insertions(+), 151 deletions(-) diff --git a/include/wireless/wapi.h b/include/wireless/wapi.h index 8713d1acb..69ea3e38f 100644 --- a/include/wireless/wapi.h +++ b/include/wireless/wapi.h @@ -321,22 +321,6 @@ int wapi_del_route_gw(int sock, wapi_route_target_t targettype, FAR const struct in_addr *gw); #endif -/**************************************************************************** - * Name: wapi_get_we_version - * - * Description: - * Gets kernel WE (Wireless Extensions) version. - * - * Input Parameters: - * we_version Set to we_version_compiled of range information. - * - * Returned Value: - * Zero on success. - * - ****************************************************************************/ - -int wapi_get_we_version(int sock, const char *ifname, FAR int *we_version); - /**************************************************************************** * Name: wapi_get_freq * diff --git a/wireless/wapi/src/util.c b/wireless/wapi/src/util.c index 6e379c1e2..28dd7b600 100644 --- a/wireless/wapi/src/util.c +++ b/wireless/wapi/src/util.c @@ -51,8 +51,30 @@ * Pre-processor Definitions ****************************************************************************/ -#define WAPI_IOCTL_COMMAND_NAMEBUFSIZ 128 /* Is fairly enough to print an - * integer. */ +/* 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_WAPI SOCK_DGRAM +#else +# define SOCK_WAPI SOCK_STREAM +#endif + +/* Size of the command buffer */ + +#define WAPI_IOCTL_COMMAND_NAMEBUFSIZ 24 /**************************************************************************** * Public Functions @@ -77,7 +99,7 @@ static char g_ioctl_command_namebuf[WAPI_IOCTL_COMMAND_NAMEBUFSIZ]; int wapi_make_socket(void) { - return socket(AF_INET, SOCK_DGRAM, 0); + return socket(PF_INETX, SOCK_WAPI, 0); } /**************************************************************************** diff --git a/wireless/wapi/src/wapi.c b/wireless/wapi/src/wapi.c index 03fa6ba71..09f6538ef 100644 --- a/wireless/wapi/src/wapi.c +++ b/wireless/wapi/src/wapi.c @@ -47,6 +47,7 @@ #include #include "wireless/wapi.h" +#include "util.h" /**************************************************************************** * Private Types @@ -138,13 +139,13 @@ static int wapi_str2int(FAR const char *str) value = strtol(str, &endptr, 0); if (*endptr != '\0') { - fprintf(stderr, "ERROR: Garbage after numeric argument\n"); + WAPI_ERROR("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"); + WAPI_ERROR("ERROR: Integer value out of range\n"); exit(EXIT_FAILURE); } @@ -167,7 +168,7 @@ static double wapi_str2double(FAR const char *str) value = strtod(str, &endptr); if (*endptr != '\0') { - fprintf(stderr, "ERROR: Garbage after numeric argument\n"); + WAPI_ERROR("ERROR: Garbage after numeric argument\n"); exit(EXIT_FAILURE); } @@ -218,7 +219,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname) ret = wapi_get_ip(sock, ifname, &addr); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_get_ip() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_get_ip() failed: %d\n", ret); } else { @@ -231,7 +232,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname) ret = wapi_get_netmask(sock, ifname, &addr); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_get_netmask() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_get_netmask() failed: %d\n", ret); } else { @@ -243,7 +244,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname) ret = wapi_get_freq(sock, ifname, &freq, &freq_flag); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_get_freq() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_get_freq() failed: %d\n", ret); } else { @@ -256,7 +257,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname) ret = wapi_freq2chan(sock, ifname, freq, &chan); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_freq2chan() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_freq2chan() failed: %d\n", ret); } else { @@ -266,7 +267,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname) ret = wapi_chan2freq(sock, ifname, chan, &tmpfreq); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_chan2freq() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_chan2freq() failed: %d\n", ret); } else { @@ -279,7 +280,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname) ret = wapi_get_essid(sock, ifname, essid, &essid_flag); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_get_essid() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_get_essid() failed: %d\n", ret); } else { @@ -292,7 +293,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname) ret = wapi_get_mode(sock, ifname, &mode); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_get_mode() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_get_mode() failed: %d\n", ret); } else { @@ -304,11 +305,11 @@ static void wapi_show_cmd(int sock, FAR const char *ifname) ret = wapi_get_ap(sock, ifname, &ap); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_get_ap() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_get_ap() failed: %d\n", ret); } else { - printf(" AP: %02x:%02x:%02x:%02x:%02x:%02x", + printf(" AP: %02x:%02x:%02x:%02x:%02x:%02x\n", ap.ether_addr_octet[0], ap.ether_addr_octet[1], ap.ether_addr_octet[2], ap.ether_addr_octet[3], ap.ether_addr_octet[4], ap.ether_addr_octet[5]); @@ -319,7 +320,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname) ret = wapi_get_bitrate(sock, ifname, &bitrate, &bitrate_flag); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_get_bitrate() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_get_bitrate() failed: %d\n", ret); } else { @@ -332,7 +333,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname) ret = wapi_get_txpower(sock, ifname, &txpower, &txpower_flag); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_get_txpower() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_get_txpower() failed: %d\n", ret); } else { @@ -367,7 +368,7 @@ static void wapi_ip_cmd(int sock, FAR const char *ifname, ret = wapi_set_ip(sock, ifname, &addr); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_set_ip() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_set_ip() failed: %d\n", ret); } } @@ -397,7 +398,7 @@ static void wapi_mask_cmd(int sock, FAR const char *ifname, ret = wapi_set_netmask(sock, ifname, &addr); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_set_netmask() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_set_netmask() failed: %d\n", ret); } } @@ -437,7 +438,7 @@ static void wapi_freq_cmd(int sock, FAR const char *ifname, if (!found) { - fprintf(stderr, "ERROR: Invalid frequency flag: %s\n", flagstr); + WAPI_ERROR("ERROR: Invalid frequency flag: %s\n", flagstr); exit(EXIT_FAILURE); } @@ -446,7 +447,7 @@ static void wapi_freq_cmd(int sock, FAR const char *ifname, ret = wapi_set_freq(sock, ifname, frequency, freq_flag); if (ret < 0) { - fprintf(stderr, "ERROR: \nwapi_set_freq() failed: %d", ret); + WAPI_ERROR("ERROR: \nwapi_set_freq() failed: %d\n", ret); } } @@ -483,7 +484,7 @@ static void wapi_essid_cmd(int sock, FAR const char *ifname, if (!found) { - fprintf(stderr, "ERROR: Invalid ESSID flag: %s\n", flagstr); + WAPI_ERROR("ERROR: Invalid ESSID flag: %s\n", flagstr); exit(EXIT_FAILURE); } @@ -492,7 +493,7 @@ static void wapi_essid_cmd(int sock, FAR const char *ifname, ret = wapi_set_essid(sock, ifname, essid, essid_flag); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_set_essid() failed: %d", ret); + WAPI_ERROR("ERROR: wapi_set_essid() failed: %d\n", ret); } } @@ -529,7 +530,7 @@ static void wapi_mode_cmd(int sock, FAR const char *ifname, if (!found) { - fprintf(stderr, "ERROR: Invalid operating mode: %s\n", modestr); + WAPI_ERROR("ERROR: Invalid operating mode: %s\n", modestr); exit(EXIT_FAILURE); } @@ -538,7 +539,7 @@ static void wapi_mode_cmd(int sock, FAR const char *ifname, ret = wapi_set_mode(sock, ifname, mode); if (ret < 0) { - fprintf(stderr, "ERROR: \nwapi_set_mode() failed: %d", ret); + WAPI_ERROR("ERROR: \nwapi_set_mode() failed: %d\n", ret); } } @@ -571,7 +572,7 @@ static void wapi_ap_cmd(int sock, FAR const char *ifname, ret = wapi_set_ap(sock, ifname, &ap); if (ret < 0) { - fprintf(stderr, "ERROR: \nwapi_set_ap() failed: %d", ret); + WAPI_ERROR("ERROR: \nwapi_set_ap() failed: %d\n", ret); } } @@ -612,7 +613,7 @@ static void wapi_bitrate_cmd(int sock, FAR const char *ifname, if (!found) { - fprintf(stderr, "ERROR: Invalid bitrate flag: %s\n", flagstr); + WAPI_ERROR("ERROR: Invalid bitrate flag: %s\n", flagstr); exit(EXIT_FAILURE); } @@ -621,7 +622,7 @@ static void wapi_bitrate_cmd(int sock, FAR const char *ifname, ret = wapi_set_bitrate(sock, ifname, bitrate, bitrate_flag); if (ret < 0) { - fprintf(stderr, "ERROR: \nwapi_set_bitrate() failed: %d", ret); + WAPI_ERROR("ERROR: \nwapi_set_bitrate() failed: %d\n", ret); } } @@ -649,7 +650,7 @@ static void wapi_txpower_cmd(int sock, FAR const char *ifname, txpower = wapi_str2int(pwrstr); - for (i = 0; i < 3; i++) + for (i = 0; i < IW_TXPOW_NFLAGS; i++) { if (strcmp(flagstr, g_wapi_txpower_flags[i]) == 0) { @@ -661,7 +662,7 @@ static void wapi_txpower_cmd(int sock, FAR const char *ifname, if (!found) { - fprintf(stderr, "ERROR: Invalid TX power flag: %s\n", flagstr); + WAPI_ERROR("ERROR: Invalid TX power flag: %s\n", flagstr); exit(EXIT_FAILURE); } @@ -670,7 +671,7 @@ static void wapi_txpower_cmd(int sock, FAR const char *ifname, ret = wapi_set_txpower(sock, ifname, txpower, txpower_flag); if (ret < 0) { - fprintf(stderr, "ERROR: \nwapi_set_txpower() failed: %d", ret); + WAPI_ERROR("ERROR: \nwapi_set_txpower() failed: %d\n", ret); } } @@ -696,7 +697,7 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname) /* Start scan */ ret = wapi_scan_init(sock, ifname); - fprintf(stderr, "ERROR: wapi_scan_init() failed: %d\n", ret); + WAPI_ERROR("ERROR: wapi_scan_init() failed: %d\n", ret); /* Wait for completion */ @@ -704,7 +705,8 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname) { sleep(sleepdur); ret = wapi_scan_stat(sock, ifname); - fprintf(stderr, "ERROR: wapi_scan_stat() failed: %d, sleeptries: %d\n", ret, sleeptries); + WAPI_ERROR("ERROR: wapi_scan_stat() failed: %d, sleeptries: %d\n", + ret, sleeptries); } while (--sleeptries > 0 && ret > 0); @@ -719,7 +721,7 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname) ret = wapi_scan_coll(sock, ifname, &list); if (ret < 0) { - fprintf(stderr, "ERROR: wapi_scan_coll() failed: %d\n", ret); + WAPI_ERROR("ERROR: wapi_scan_coll() failed: %d\n", ret); } /* Print found aps */ @@ -776,35 +778,33 @@ static void wapi_showusage(FAR const char *progname, int exitcode) fprintf(stderr, "\nFrequency Flags:\n"); for (i = 0; i < IW_FREQ_NFLAGS; i++) { - fprintf(stderr, " %s", g_wapi_freq_flags[i]); + fprintf(stderr, " %s\n", g_wapi_freq_flags[i]); } fprintf(stderr, "\nESSID Flags:\n"); for (i = 0; i < 2; i++) { - fprintf(stderr, " %s", g_wapi_essid_flags[i]); + fprintf(stderr, " %s\n", g_wapi_essid_flags[i]); } fprintf(stderr, "\nOperating Modes:\n"); for (i = 0; i < IW_MODE_NFLAGS; i++) { - fprintf(stderr, " %s", g_wapi_modes[i]); + fprintf(stderr, " %s\n", g_wapi_modes[i]); } fprintf(stderr, "\nBitrate Flags:\n"); - for (i = 0; i < 6; i++) + for (i = 0; i < 2; i++) { - fprintf(stderr, " %s", g_wapi_bitrate_flags[i]); + fprintf(stderr, " %s\n", g_wapi_bitrate_flags[i]); } fprintf(stderr, "\nTX power Flags:\n"); - for (i = 0; i < 3; i++) + for (i = 0; i < IW_TXPOW_NFLAGS; i++) { - fprintf(stderr, " %s", g_wapi_txpower_flags[i]); + fprintf(stderr, " %s\n", g_wapi_txpower_flags[i]); } - putchar('\n'); - exit(exitcode); } @@ -827,7 +827,7 @@ int wapi_main(int argc, char *argv[]) if (argc < 2) { - fprintf(stderr, "ERROR: Missing command\n"); + WAPI_ERROR("ERROR: Missing command\n"); wapi_showusage(argv[0], EXIT_FAILURE); } @@ -848,18 +848,18 @@ int wapi_main(int argc, char *argv[]) if (wapicmd == NULL) { - fprintf(stderr, "ERROR: Unsupported command: %s\n", cmdname); + WAPI_ERROR("ERROR: Unsupported command: %s\n", cmdname); wapi_showusage(argv[0], EXIT_FAILURE); } if (wapicmd->noptions + 2 < argc) { - fprintf(stderr, "ERROR: Garbage at end of command ignored\n"); + WAPI_ERROR("ERROR: Garbage at end of command ignored\n"); } else if (wapicmd->noptions + 2 > argc) { - fprintf(stderr, "ERROR: Missing required command options: %s\n", - cmdname); + WAPI_ERROR("ERROR: Missing required command options: %s\n", + cmdname); wapi_showusage(argv[0], EXIT_FAILURE); } @@ -877,7 +877,8 @@ int wapi_main(int argc, char *argv[]) sock = wapi_make_socket(); if (sock < 0) { - fprintf(stderr, "ERROR: wapi_make_socket() failed: %d\n", sock); + WAPI_ERROR("ERROR: wapi_make_socket() failed: %d\n", sock); + return EXIT_FAILURE; } /* Dispatch the command handling */ @@ -886,7 +887,7 @@ int wapi_main(int argc, char *argv[]) { default: case 0: - fprintf(stderr, "ERROR: Internal craziness\n"); + WAPI_ERROR("ERROR: Internal craziness\n"); wapi_showusage(argv[0], EXIT_FAILURE); case 1: diff --git a/wireless/wapi/src/wireless.c b/wireless/wapi/src/wireless.c index 12f9451a1..36bac9600 100644 --- a/wireless/wapi/src/wireless.c +++ b/wireless/wapi/src/wireless.c @@ -43,26 +43,19 @@ #include #include +#include #include #include "wireless/wapi.h" #include "util.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#ifdef LIBNL1 -# define nl_sock nl_handle -#endif - /**************************************************************************** * Private Types ****************************************************************************/ /* Events & Streams */ -struct iw_event_stream_s +struct wapi_event_stream_s { FAR char *end; /* End of the stream */ FAR char *current; /* Current event in stream of events */ @@ -99,7 +92,7 @@ FAR const char *g_wapi_modes[] = "WAPI_MODE_MASTER", "WAPI_MODE_REPEAT", "WAPI_MODE_SECOND", - "WAPI_MODE_MONITOR" + "WAPI_MODE_MONITOR", "WAPI_MODE_MESH" }; @@ -182,7 +175,7 @@ static int wapi_parse_mode(int iw_mode, FAR wapi_mode_t *wapi_mode) return 0; default: - WAPI_ERROR("Unknown mode: %d.\n", iw_mode); + WAPI_ERROR("ERROR: Unknown mode: %d\n", iw_mode); return -1; } } @@ -202,32 +195,35 @@ static int wapi_make_ether(FAR struct ether_addr *addr, int byte) } /**************************************************************************** - * Name: iw_event_stream_init + * Name: wapi_event_stream_init * * Description: + * Initialize a stream to access the events. * ****************************************************************************/ -static void iw_event_stream_init(FAR struct iw_event_stream_s *stream, - FAR char *data, size_t len) +static void wapi_event_stream_init(FAR struct wapi_event_stream_s *stream, + FAR char *data, size_t len) { - memset(stream, 0, sizeof(struct iw_event_stream_s)); + memset(stream, 0, sizeof(struct wapi_event_stream_s)); stream->current = data; stream->end = &data[len]; } /**************************************************************************** - * Name: iw_event_stream_pop + * Name: wapi_event_stream_extract * * Description: + * Extract the next event from the stream. * ****************************************************************************/ -static int iw_event_stream_pop(FAR struct iw_event_stream_s *stream, - FAR struct iw_event *iwe, int we_version) +static int wapi_event_stream_extract(FAR struct wapi_event_stream_s *stream, + FAR struct iw_event *iwe) { - return iw_extract_event_stream((struct stream_descr *)stream, iwe, - we_version); +#warning Missing logic +// return iw_extract_event_stream((struct stream_descr *)stream, iwe, 0); +return -ENOSYS; } /**************************************************************************** @@ -327,51 +323,6 @@ static int wapi_scan_event(FAR struct iw_event *event, FAR wapi_list_t *list) * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: wapi_get_we_version - * - * Description: - * Gets kernel WE (Wireless Extensions) version. - * - * Input Parameters: - * we_version Set to we_version_compiled of range information. - * - * Returned Value: - * Zero on success. - * - ****************************************************************************/ - -int wapi_get_we_version(int sock, const char *ifname, FAR int *we_version) -{ - struct iwreq wrq; - char buf[sizeof(struct iw_range) * 2]; - int ret; - - WAPI_VALIDATE_PTR(we_version); - - /* Prepare request. */ - - bzero(buf, sizeof(buf)); - wrq.u.data.pointer = buf; - wrq.u.data.length = sizeof(buf); - wrq.u.data.flags = 0; - - /* Get WE version. */ - - strncpy(wrq.ifr_name, ifname, IFNAMSIZ); - if ((ret = ioctl(sock, SIOCGIWRANGE, (unsigned long)((uintptr_t)&wrq))) >= 0) - { - struct iw_range *range = (struct iw_range *)buf; - *we_version = (int)range->we_version_compiled; - } - else - { - WAPI_IOCTL_STRERROR(SIOCGIWRANGE); - } - - return ret; -} - /**************************************************************************** * Name: wapi_get_freq * @@ -404,7 +355,7 @@ int wapi_get_freq(int sock, FAR const char *ifname, FAR double *freq, } else { - WAPI_ERROR("Unknown flag: %d.\n", wrq.u.freq.flags); + WAPI_ERROR("ERROR: Unknown flag: %d\n", wrq.u.freq.flags); return -1; } @@ -810,7 +761,7 @@ int wapi_get_bitrate(int sock, FAR const char *ifname, if (wrq.u.bitrate.disabled) { - WAPI_ERROR("Bitrate is disabled.\n"); + WAPI_ERROR("ERROR: Bitrate is disabled\n"); return -1; } @@ -923,7 +874,7 @@ int wapi_get_txpower(int sock, FAR const char *ifname, FAR int *power, } else { - WAPI_ERROR("Unknown flag: %d.\n", wrq.u.txpower.flags); + WAPI_ERROR("ERROR: Unknown flag: %d\n", wrq.u.txpower.flags); return -1; } @@ -1074,18 +1025,10 @@ int wapi_scan_coll(int sock, FAR const char *ifname, FAR wapi_list_t *aps) FAR char *buf; int buflen; struct iwreq wrq; - int we_version; int ret; WAPI_VALIDATE_PTR(aps); - /* Get WE version. (Required for event extraction via libiw.) */ - - if ((ret = wapi_get_we_version(sock, ifname, &we_version)) < 0) - { - return ret; - } - buflen = IW_SCAN_MAX_DATA; buf = malloc(buflen * sizeof(char)); if (!buf) @@ -1098,13 +1041,14 @@ alloc: /* Collect results. */ wrq.u.data.pointer = buf; - wrq.u.data.length = buflen; - wrq.u.data.flags = 0; + wrq.u.data.length = buflen; + wrq.u.data.flags = 0; strncpy(wrq.ifr_name, ifname, IFNAMSIZ); - if ((ret = ioctl(sock, SIOCGIWSCAN, (unsigned long)((uintptr_t)&wrq))) < 0 && - errno == E2BIG) + + ret = ioctl(sock, SIOCGIWSCAN, (unsigned long)((uintptr_t)&wrq)); + if (ret < 0 && errno == E2BIG) { - char *tmp; + FAR char *tmp; buflen *= 2; tmp = realloc(buf, buflen); @@ -1135,12 +1079,14 @@ alloc: if (wrq.u.data.length) { struct iw_event iwe; - struct iw_event_stream_s stream; + struct wapi_event_stream_s stream; - iw_event_stream_init(&stream, buf, wrq.u.data.length); + wapi_event_stream_init(&stream, buf, wrq.u.data.length); do { - if ((ret = iw_event_stream_pop(&stream, &iwe, we_version)) >= 0) + /* Get the next event from the stream */ + + if ((ret = wapi_event_stream_extract(&stream, &iwe)) >= 0) { int eventret = wapi_scan_event(&iwe, aps); if (eventret < 0) @@ -1150,7 +1096,7 @@ alloc: } else { - WAPI_ERROR("iw_event_stream_pop() failed!\n"); + WAPI_ERROR("ERROR: wapi_event_stream_extract() failed!\n"); } } while (ret > 0);