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);