diff --git a/include/wireless/wapi.h b/include/wireless/wapi.h index d97bb58fc..69ea3e38f 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 @@ -96,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. @@ -293,19 +291,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 * @@ -336,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 * @@ -572,19 +541,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/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/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 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..28dd7b600 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: @@ -46,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 @@ -72,91 +99,11 @@ 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); } /**************************************************************************** - * 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..09f6538ef 100644 --- a/wireless/wapi/src/wapi.c +++ b/wireless/wapi/src/wapi.c @@ -1,8 +1,14 @@ /**************************************************************************** * 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 + * + * Largely and original work, but highly influenced by sampled code provided + * with 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: @@ -35,210 +41,652 @@ #include #include #include -#include +#include #include #include #include #include "wireless/wapi.h" +#include "util.h" +/**************************************************************************** + * Private Types + ****************************************************************************/ -/* 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. - */ +/* Describes one command */ -static void conf(int sock, FAR const char *ifname) +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_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') + { + WAPI_ERROR("ERROR: Garbage after numeric argument\n"); + exit(EXIT_FAILURE); + } + + if (value > INT_MAX || value < INT_MIN) + { + WAPI_ERROR("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') + { + WAPI_ERROR("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 + * 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 wapi_show_cmd(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 + WAPI_ERROR("ERROR: wapi_get_ip() failed: %d\n", 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 + WAPI_ERROR("ERROR: wapi_get_netmask() failed: %d\n", 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) + { + WAPI_ERROR("ERROR: wapi_get_freq() failed: %d\n", 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); + WAPI_ERROR("ERROR: wapi_freq2chan() failed: %d\n", 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); + WAPI_ERROR("ERROR: wapi_chan2freq() failed: %d\n", 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 + WAPI_ERROR("ERROR: wapi_get_essid() failed: %d\n", 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 + WAPI_ERROR("ERROR: wapi_get_mode() failed: %d\n", 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", + WAPI_ERROR("ERROR: wapi_get_ap() failed: %d\n", ret); + } + else + { + 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]); - -#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 + WAPI_ERROR("ERROR: wapi_get_bitrate() failed: %d\n", 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 + WAPI_ERROR("ERROR: wapi_get_txpower() failed: %d\n", 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_ip_cmd + * + * Description: + * Set the IP address. + * + * Returned Value: + * None + * + ****************************************************************************/ -static void scan(int sock, FAR const char *ifname) +static void wapi_ip_cmd(int sock, FAR const char *ifname, + FAR const char *addrstr) +{ + struct in_addr addr; + int ret; + + /* Format the request */ + + addr.s_addr = inet_addr(addrstr); + + /* Set the IP address */ + + ret = wapi_set_ip(sock, ifname, &addr); + if (ret < 0) + { + WAPI_ERROR("ERROR: wapi_set_ip() failed: %d\n", ret); + } +} + +/**************************************************************************** + * Name: wapi_mask_cmd + * + * Description: + * Set the network mask + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_mask_cmd(int sock, FAR const char *ifname, + FAR const char *maskstr) +{ + struct in_addr addr; + int ret; + + /* Format the request */ + + addr.s_addr = inet_addr(maskstr); + + /* Set the network mask */ + + ret = wapi_set_netmask(sock, ifname, &addr); + if (ret < 0) + { + WAPI_ERROR("ERROR: wapi_set_netmask() failed: %d\n", ret); + } +} + +/**************************************************************************** + * Name: wapi_freq_cmd + * + * Description: + * Set the frequency + * + * Returned Value: + * None + * + ****************************************************************************/ + +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; + + /* Convert input strings to values */ + + 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) + { + WAPI_ERROR("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) + { + WAPI_ERROR("ERROR: \nwapi_set_freq() failed: %d\n", ret); + } +} + +/**************************************************************************** + * Name: wapi_essid_cmd + * + * Description: + * Set the ESSID + * + * Returned Value: + * None + * + ****************************************************************************/ + +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; + + /* 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) + { + WAPI_ERROR("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) + { + WAPI_ERROR("ERROR: wapi_set_essid() failed: %d\n", ret); + } +} + +/**************************************************************************** + * Name: wapi_mode_cmd + * + * Description: + * Set the operating mode + * + * Returned Value: + * None + * + ****************************************************************************/ + +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) + { + WAPI_ERROR("ERROR: Invalid operating mode: %s\n", modestr); + exit(EXIT_FAILURE); + } + + /* Set operating mode */ + + ret = wapi_set_mode(sock, ifname, mode); + if (ret < 0) + { + WAPI_ERROR("ERROR: \nwapi_set_mode() failed: %d\n", ret); + } +} + +/**************************************************************************** + * Name: wapi_ap_cmd + * + * Description: + * Set the AP + * + * Returned Value: + * None + * + ****************************************************************************/ + +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); + if (ret < 0) + { + WAPI_ERROR("ERROR: \nwapi_set_ap() failed: %d\n", ret); + } +} + +/**************************************************************************** + * Name: wapi_bitrate_cmd + * + * Description: + * Set the bit rate + * + * Returned Value: + * None + * + ****************************************************************************/ + +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) + { + WAPI_ERROR("ERROR: Invalid bitrate flag: %s\n", flagstr); + exit(EXIT_FAILURE); + } + + /* Set bitrate */ + + ret = wapi_set_bitrate(sock, ifname, bitrate, bitrate_flag); + if (ret < 0) + { + WAPI_ERROR("ERROR: \nwapi_set_bitrate() failed: %d\n", ret); + } +} + +/**************************************************************************** + * Name: wapi_txpower_cmd + * + * Description: + * Set the TX power + * + * Returned Value: + * None + * + ****************************************************************************/ + +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 < IW_TXPOW_NFLAGS; i++) + { + if (strcmp(flagstr, g_wapi_txpower_flags[i]) == 0) + { + txpower_flag = (wapi_txpower_flag_t)i; + found = true; + break; + } + } + + if (!found) + { + WAPI_ERROR("ERROR: Invalid TX power flag: %s\n", flagstr); + exit(EXIT_FAILURE); + } + + /* Set txpower */ + + ret = wapi_set_txpower(sock, ifname, txpower, txpower_flag); + if (ret < 0) + { + WAPI_ERROR("ERROR: \nwapi_set_txpower() failed: %d\n", ret); + } +} + +/**************************************************************************** + * Name: wapi_scan_cmd + * + * Description: + * Scans available APs in the range using given ifname interface. + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void wapi_scan_cmd(int sock, FAR const char *ifname) { int sleepdur = 1; int sleeptries = 5; @@ -249,7 +697,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); + WAPI_ERROR("ERROR: wapi_scan_init() failed: %d\n", ret); /* Wait for completion */ @@ -257,7 +705,8 @@ 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); + WAPI_ERROR("ERROR: wapi_scan_stat() failed: %d, sleeptries: %d\n", + ret, sleeptries); } while (--sleeptries > 0 && ret > 0); @@ -270,7 +719,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) + { + WAPI_ERROR("ERROR: wapi_scan_coll() failed: %d\n", ret); + } /* Print found aps */ @@ -296,6 +748,66 @@ 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) +{ + int i; + + fprintf(stderr, "Usage: %s show \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\n", g_wapi_freq_flags[i]); + } + + fprintf(stderr, "\nESSID Flags:\n"); + for (i = 0; i < 2; 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\n", g_wapi_modes[i]); + } + + fprintf(stderr, "\nBitrate Flags:\n"); + for (i = 0; i < 2; i++) + { + fprintf(stderr, " %s\n", g_wapi_bitrate_flags[i]); + } + + fprintf(stderr, "\nTX power Flags:\n"); + for (i = 0; i < IW_TXPOW_NFLAGS; i++) + { + fprintf(stderr, " %s\n", g_wapi_txpower_flags[i]); + } + + exit(exitcode); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -306,104 +818,92 @@ int main(int argc, FAR char *argv[]) int wapi_main(int argc, char *argv[]) #endif { - 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 != 2) + if (argc < 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); + WAPI_ERROR("ERROR: Missing command\n"); + wapi_showusage(argv[0], EXIT_FAILURE); + } + + cmdname = argv[1]; + + /* 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) + { + WAPI_ERROR("ERROR: Unsupported command: %s\n", cmdname); + wapi_showusage(argv[0], EXIT_FAILURE); + } + + if (wapicmd->noptions + 2 < argc) + { + WAPI_ERROR("ERROR: Garbage at end of command ignored\n"); + } + else if (wapicmd->noptions + 2 > argc) + { + WAPI_ERROR("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) + { + WAPI_ERROR("ERROR: wapi_make_socket() failed: %d\n", sock); return EXIT_FAILURE; } - ifname = argv[1]; + /* Dispatch the command handling */ - /* Get ifnames */ - - bzero(&list, sizeof(wapi_list_t)); - ret = wapi_get_ifnames(&list); - printf("wapi_get_ifnames(): ret: %d", ret); - if (ret >= 0) + switch (wapicmd->noptions) { - FAR wapi_string_t *str; + default: + case 0: + WAPI_ERROR("ERROR: Internal craziness\n"); + wapi_showusage(argv[0], EXIT_FAILURE); - /* Print ifnames */ + case 1: + ((cmd1_t)wapicmd->handler)(sock, argv[2]); + break; - printf(", ifnames:"); - for (str = list.head.string; str; str = str->next) - { - printf(" %s", str->data); - } + case 2: + ((cmd2_t)wapicmd->handler)(sock, argv[2], argv[3]); + break; - /* Free ifnames */ - - str = list.head.string; - while (str) - { - FAR wapi_string_t *tmp; - - tmp = str->next; - free(str->data); - free(str); - str = tmp; - } + case 3: + ((cmd3_t)wapicmd->handler)(sock, argv[2], argv[3], argv[4]); + break; } - 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(); - printf("wapi_make_socket(): sock: %d\n", sock); - - /* List conf */ - - printf("\nconf\n"); - printf("------------\n"); - conf(sock, ifname); - - /* Scan aps */ - - printf("\nscan\n"); - printf("----\n"); - scan(sock, ifname); - - /* Close comm. sock. */ + /* Close communication socket */ close(sock); return EXIT_SUCCESS; diff --git a/wireless/wapi/src/wireless.c b/wireless/wapi/src/wireless.c index 8ee1bdc83..36bac9600 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: @@ -32,30 +37,25 @@ * Included Files ****************************************************************************/ +#include #include -#include #include +#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 */ @@ -92,7 +92,8 @@ FAR const char *g_wapi_modes[] = "WAPI_MODE_MASTER", "WAPI_MODE_REPEAT", "WAPI_MODE_SECOND", - "WAPI_MODE_MONITOR" + "WAPI_MODE_MONITOR", + "WAPI_MODE_MESH" }; /* Bit Rate */ @@ -174,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; } } @@ -194,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; } /**************************************************************************** @@ -319,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 * @@ -373,7 +332,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; @@ -396,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; } @@ -416,8 +375,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; @@ -799,19 +758,22 @@ 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"); + WAPI_ERROR("ERROR: Bitrate is disabled\n"); return -1; } /* 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; } @@ -912,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; } @@ -937,7 +899,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; @@ -1063,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) @@ -1087,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); @@ -1124,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) @@ -1139,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);