diff --git a/CMakeLists.txt b/CMakeLists.txt index f214537..908fa41 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,7 @@ else() # system specific compiler flags ADD_DEFINITIONS( - -std=gnu++11 + -std=gnu++11 -Wall -Werror=return-type @@ -95,11 +95,16 @@ ADD_EXECUTABLE( ${SOURCES} ) +SET(EXTRA_LIBS ${EXTRA_LIBS} nl-genl-3 nl-3) +INCLUDE_DIRECTORIES(/usr/include/libnl3/) +SET(EXTRA_LIBS ${EXTRA_LIBS} iw) + # needed external libraries TARGET_LINK_LIBRARIES( ${PROJECT_NAME} gtest pthread + ${EXTRA_LIBS} ) SET(CMAKE_C_COMPILER ${CMAKE_CXX_COMPILER}) diff --git a/main.cpp b/main.cpp index 8657c03..492f544 100755 --- a/main.cpp +++ b/main.cpp @@ -10,8 +10,17 @@ class Test : public GridPoint { #include "tests/Tests.h" +#include "sensors/radio/scan/WiFiScanLinux.h" +void wifi() { + const std::string dev = "wlp0s20u2u1"; + WiFiScanLinux scanner(dev); + scanner.scan(); +} + int main(int argc, char** argv) { + wifi(); return 0; + #ifdef WITH_TESTS ::testing::InitGoogleTest(&argc, argv); @@ -25,8 +34,8 @@ int main(int argc, char** argv) { //::testing::GTEST_FLAG(filter) = "*Grid.*"; //::testing::GTEST_FLAG(filter) = "*Dijkstra.*"; - //::testing::GTEST_FLAG(filter) = "*LogDistanceCeilingModelBeacon*"; - //::testing::GTEST_FLAG(filter) = "*WiFiOptimizer*"; + //::testing::GTEST_FLAG(filter) = "*LogDistanceCeilingModelBeacon*"; + //::testing::GTEST_FLAG(filter) = "*WiFiOptimizer*"; //::testing::GTEST_FLAG(filter) = "*Offline.readWrite*"; @@ -41,7 +50,7 @@ int main(int argc, char** argv) { //::testing::GTEST_FLAG(filter) = "*BVH*"; - //::testing::GTEST_FLAG(filter) = "*Barometer*"; + //::testing::GTEST_FLAG(filter) = "*Barometer*"; //::testing::GTEST_FLAG(filter) = "*GridWalk2RelPressure*"; //::testing::GTEST_FLAG(filter) = "Heading*"; diff --git a/sensors/MACAddress.h b/sensors/MACAddress.h index 8c31237..7443aed 100644 --- a/sensors/MACAddress.h +++ b/sensors/MACAddress.h @@ -47,27 +47,27 @@ public: MACAddress(const std::string& str) { // sanity check - if (str.size() == 17 ){ - mac = 0; // all zeros - fields.h5 = hexWordToInt(str[ 0], str[ 1]); - fields.h4 = hexWordToInt(str[ 3], str[ 4]); - fields.h3 = hexWordToInt(str[ 6], str[ 7]); - fields.h2 = hexWordToInt(str[ 9], str[10]); - fields.h1 = hexWordToInt(str[12], str[13]); - fields.h0 = hexWordToInt(str[15], str[16]); - } - else if (str.size() == 12){ - mac = 0; // all zeros - fields.h5 = hexWordToInt(str[ 0], str[ 1]); - fields.h4 = hexWordToInt(str[ 2], str[ 3]); - fields.h3 = hexWordToInt(str[ 4], str[ 5]); + if (str.size() == 17 ){ + mac = 0; // all zeros + fields.h5 = hexWordToInt(str[ 0], str[ 1]); + fields.h4 = hexWordToInt(str[ 3], str[ 4]); + fields.h3 = hexWordToInt(str[ 6], str[ 7]); + fields.h2 = hexWordToInt(str[ 9], str[10]); + fields.h1 = hexWordToInt(str[12], str[13]); + fields.h0 = hexWordToInt(str[15], str[16]); + } + else if (str.size() == 12){ + mac = 0; // all zeros + fields.h5 = hexWordToInt(str[ 0], str[ 1]); + fields.h4 = hexWordToInt(str[ 2], str[ 3]); + fields.h3 = hexWordToInt(str[ 4], str[ 5]); fields.h2 = hexWordToInt(str[ 6], str[ 7]); fields.h1 = hexWordToInt(str[ 8], str[ 9]); - fields.h0 = hexWordToInt(str[10], str[11]); - } - else{ - throw Exception("invalid hex string length. must be 17 or 12 (without :)"); - } + fields.h0 = hexWordToInt(str[10], str[11]); + } + else{ + throw Exception("invalid hex string length. must be 17 or 12 (without :)"); + } } diff --git a/sensors/radio/scan/WiFiChannels.h b/sensors/radio/scan/WiFiChannels.h new file mode 100644 index 0000000..6585397 --- /dev/null +++ b/sensors/radio/scan/WiFiChannels.h @@ -0,0 +1,30 @@ +#ifndef INDOOR_WIFICHANNELS_H +#define INDOOR_WIFICHANNELS_H + +class WiFiChannels { + +public: + + static int freqToChannel(const int freq) { + switch(freq) { + case 2412: return 1; + case 2417: return 2; + case 2422: return 3; + case 2427: return 4; + case 2432: return 5; + case 2437: return 6; + case 2442: return 7; + case 2447: return 8; + case 2452: return 9; + case 2457: return 10; + case 2462: return 11; + case 2467: return 12; + case 2472: return 13; + case 2484: return 14; + } + throw "error"; + } + +}; + +#endif // INDOOR_WIFICHANNELS_H diff --git a/sensors/radio/scan/WiFiScan.h b/sensors/radio/scan/WiFiScan.h new file mode 100644 index 0000000..5f39105 --- /dev/null +++ b/sensors/radio/scan/WiFiScan.h @@ -0,0 +1,15 @@ +#ifndef INDOOR_WIFI_SCAN_H +#define INDOOR_WIFI_SCAN_H + +#include "../WiFiMeasurements.h" + +class WiFiScan { + +public: + + /** scan for nearby access points */ + virtual WiFiMeasurements scan() = 0; + +}; + +#endif //INDOOR_WIFI_SCAN_H diff --git a/sensors/radio/scan/WiFiScanLinux.h b/sensors/radio/scan/WiFiScanLinux.h new file mode 100644 index 0000000..baab78d --- /dev/null +++ b/sensors/radio/scan/WiFiScanLinux.h @@ -0,0 +1,419 @@ +#ifndef INDOOR_WIFISCANNER_LINUX_H +#define INDOOR_WIFISCANNER_LINUX_H + +/* + * scan_access_points.c: Prints all detected access points with wlan0 using NL80211 (netlink). + * + * Only works on network interfaces whose drivers are compatible with Netlink. Test this by running `iw list`. + * + * Since only privileged users may submit NL80211_CMD_TRIGGER_SCAN, you'll have to run the compiled program as root. + * + * Build with: gcc $(pkg-config --cflags --libs libnl-genl-3.0) scan_access_points.c + * + * Raspbian prerequisites: + * sudo apt-get install libnl-genl-3-dev + * + * Resources: + * http://git.kernel.org/cgit/linux/kernel/git/jberg/iw.git/tree/scan.c + * http://stackoverflow.com/questions/21601521/how-to-use-the-libnl-library-to-trigger-nl80211-commands + * http://stackoverflow.com/questions/23760780/how-to-send-single-channel-scan-request-to-libnl-and-receive-single- + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "WiFiScan.h" + +class WiFiScanLinux : public WiFiScan { + + struct trigger_results { + int done; + int aborted; + }; + + + struct handler_args { // For family_handler() and nl_get_multicast_id(). + const char *group; + int id; + }; + + + static int error_handler(struct sockaddr_nl* nla, struct nlmsgerr* err, void* arg) { + (void) nla; + // Callback for errors. + printf("error_handler() called.\n"); + int* ret = (int*) arg; + *ret = err->error; + return NL_STOP; + } + + + static int finish_handler(struct nl_msg* msg, void* arg) { + (void) msg; + // Callback for NL_CB_FINISH. + int* ret = (int*) arg; + *ret = 0; + return NL_SKIP; + } + + + static int ack_handler(struct nl_msg* msg, void* arg) { + (void) msg; + // Callback for NL_CB_ACK. + int* ret = (int*)arg; + *ret = 0; + return NL_STOP; + } + + + static int no_seq_check(struct nl_msg* msg, void* arg) { + (void) msg; + (void) arg; + // Callback for NL_CB_SEQ_CHECK. + return NL_OK; + } + + + static int family_handler(struct nl_msg* msg, void* arg) { + // Callback for NL_CB_VALID within nl_get_multicast_id(). From http://sourcecodebrowser.com/iw/0.9.14/genl_8c.html. + struct handler_args* grp = (struct handler_args*) arg; + struct nlattr *tb[CTRL_ATTR_MAX + 1]; + struct genlmsghdr* gnlh = (struct genlmsghdr*) nlmsg_data(nlmsg_hdr(msg)); + struct nlattr *mcgrp; + int rem_mcgrp; + + nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); + + if (!tb[CTRL_ATTR_MCAST_GROUPS]) return NL_SKIP; + + nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mcgrp) { // This is a loop. + struct nlattr* tb_mcgrp[CTRL_ATTR_MCAST_GRP_MAX + 1]; + + nla_parse(tb_mcgrp, CTRL_ATTR_MCAST_GRP_MAX, (struct nlattr*) nla_data(mcgrp), nla_len(mcgrp), NULL); + + if (!tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME] || !tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]) continue; + if (strncmp((const char*) nla_data(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]), grp->group, + nla_len(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]))) { + continue; + } + + grp->id = nla_get_u32(tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]); + break; + } + + return NL_SKIP; + } + + + int nl_get_multicast_id(struct nl_sock *sock, const char *family, const char *group) { + // From http://sourcecodebrowser.com/iw/0.9.14/genl_8c.html. + struct nl_msg *msg; + struct nl_cb *cb; + int ret, ctrlid; + struct handler_args grp = { .group = group, .id = -ENOENT, }; + + msg = nlmsg_alloc(); + if (!msg) return -ENOMEM; + + cb = nl_cb_alloc(NL_CB_DEFAULT); + if (!cb) { + ret = -ENOMEM; + goto out_fail_cb; + } + + ctrlid = genl_ctrl_resolve(sock, "nlctrl"); + + genlmsg_put(msg, 0, 0, ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0); + + ret = -ENOBUFS; + NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family); + + ret = nl_send_auto_complete(sock, msg); + if (ret < 0) goto out; + + ret = 1; + + nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &ret); + nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &ret); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, family_handler, &grp); + + while (ret > 0) nl_recvmsgs(sock, cb); + + if (ret == 0) ret = grp.id; + + nla_put_failure: + out: + nl_cb_put(cb); + out_fail_cb: + nlmsg_free(msg); + return ret; + } + + + static void mac_addr_n2a(char *mac_addr, unsigned char *arg) { + // From http://git.kernel.org/cgit/linux/kernel/git/jberg/iw.git/tree/util.c. + int i, l; + + l = 0; + for (i = 0; i < 6; i++) { + if (i == 0) { + sprintf(mac_addr+l, "%02x", arg[i]); + l += 2; + } else { + sprintf(mac_addr+l, ":%02x", arg[i]); + l += 3; + } + } + } + + + static void print_ssid(unsigned char *ie, int ielen) { + uint8_t len; + uint8_t *data; + int i; + + while (ielen >= 2 && ielen >= ie[1]) { + if (ie[0] == 0 && ie[1] >= 0 && ie[1] <= 32) { + len = ie[1]; + data = ie + 2; + for (i = 0; i < len; i++) { + if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\') printf("%c", data[i]); + else if (data[i] == ' ' && (i != 0 && i != len -1)) printf(" "); + else printf("\\x%.2x", data[i]); + } + break; + } + ielen -= ie[1] + 2; + ie += ie[1] + 2; + } + } + + + static int callback_trigger(struct nl_msg *msg, void *arg) { + + // Called by the kernel when the scan is done or has been aborted. + struct genlmsghdr* gnlh = (struct genlmsghdr*) nlmsg_data(nlmsg_hdr(msg)); + struct trigger_results* results = (struct trigger_results*) arg; + + //printf("Got something.\n"); + //printf("%d\n", arg); + //nl_msg_dump(msg, stdout); + + if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED) { + printf("Got NL80211_CMD_SCAN_ABORTED.\n"); + results->done = 1; + results->aborted = 1; + } else if (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS) { + printf("Got NL80211_CMD_NEW_SCAN_RESULTS.\n"); + results->done = 1; + results->aborted = 0; + } // else probably an uninteresting multicast message. + + return NL_SKIP; + } + + + static int callback_dump(struct nl_msg* msg, void* arg) { + + (void) arg; + + // Called by the kernel with a dump of the successful scan's data. Called for each SSID. + struct genlmsghdr* gnlh = (struct genlmsghdr*) nlmsg_data(nlmsg_hdr(msg)); + char mac_addr[20]; + struct nlattr *tb[NL80211_ATTR_MAX + 1]; + struct nlattr *bss[NL80211_BSS_MAX + 1]; + + // static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { + // [NL80211_BSS_TSF] = { .type = NLA_U64 }, + // [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, + // [NL80211_BSS_BSSID] = { }, + // [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, + // [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, + // [NL80211_BSS_INFORMATION_ELEMENTS] = { }, + // [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, + // [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, + // [NL80211_BSS_STATUS] = { .type = NLA_U32 }, + // [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, + // [NL80211_BSS_BEACON_IES] = { }, + // }; + static struct nla_policy bss_policy[NL80211_BSS_MAX + 1]; + memset(&bss_policy, 0, sizeof(bss_policy)); + bss_policy[NL80211_BSS_TSF].type = NLA_U64; + bss_policy[NL80211_BSS_FREQUENCY].type = NLA_U32; + bss_policy[NL80211_BSS_BSSID];// = { }; + bss_policy[NL80211_BSS_BEACON_INTERVAL].type = NLA_U16; + bss_policy[NL80211_BSS_CAPABILITY].type = NLA_U16; + bss_policy[NL80211_BSS_INFORMATION_ELEMENTS];// = { }; + bss_policy[NL80211_BSS_SIGNAL_MBM].type = NLA_U32; + bss_policy[NL80211_BSS_SIGNAL_UNSPEC].type = NLA_U8; + bss_policy[NL80211_BSS_STATUS].type = NLA_U32; + bss_policy[NL80211_BSS_SEEN_MS_AGO].type = NLA_U32; + bss_policy[NL80211_BSS_BEACON_IES];// = { }; + + + // Parse and error check. + nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); + if (!tb[NL80211_ATTR_BSS]) { + printf("bss info missing!\n"); + return NL_SKIP; + } + if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], bss_policy)) { + printf("failed to parse nested attributes!\n"); + return NL_SKIP; + } + if (!bss[NL80211_BSS_BSSID]) return NL_SKIP; + if (!bss[NL80211_BSS_INFORMATION_ELEMENTS]) return NL_SKIP; + + const uint64_t seen_ago_ms = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]); + const int rssi = (nla_get_s32(bss[NL80211_BSS_SIGNAL_MBM])) / 100.0f; + + // Start printing. + mac_addr_n2a(mac_addr, (unsigned char*) nla_data(bss[NL80211_BSS_BSSID])); + printf("%s, ", mac_addr); + printf("%d MHz, ", nla_get_u32(bss[NL80211_BSS_FREQUENCY])); + print_ssid((unsigned char*) nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS])); + printf(" %d ms", seen_ago_ms); + printf(" %d dBm", rssi); + printf("\n"); + + return NL_SKIP; + } + + + int do_scan_trigger(struct nl_sock *socket, int if_index, int driver_id) { + // Starts the scan and waits for it to finish. Does not return until the scan is done or has been aborted. + struct trigger_results results = { .done = 0, .aborted = 0 }; + struct nl_msg *msg; + struct nl_cb *cb; + struct nl_msg *ssids_to_scan; + int err; + int ret; + int mcid = nl_get_multicast_id(socket, "nl80211", "scan"); + nl_socket_add_membership(socket, mcid); // Without this, callback_trigger() won't be called. + + // Allocate the messages and callback handler. + msg = nlmsg_alloc(); + if (!msg) { + printf("ERROR: Failed to allocate netlink message for msg.\n"); + return -ENOMEM; + } + ssids_to_scan = nlmsg_alloc(); + if (!ssids_to_scan) { + printf("ERROR: Failed to allocate netlink message for ssids_to_scan.\n"); + nlmsg_free(msg); + return -ENOMEM; + } + cb = nl_cb_alloc(NL_CB_DEFAULT); + if (!cb) { + printf("ERROR: Failed to allocate netlink callbacks.\n"); + nlmsg_free(msg); + nlmsg_free(ssids_to_scan); + return -ENOMEM; + } + + // Setup the messages and callback handler. + genlmsg_put(msg, 0, 0, driver_id, 0, 0, NL80211_CMD_TRIGGER_SCAN, 0); // Setup which command to run. + nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index); // Add message attribute, which interface to use. + nla_put(ssids_to_scan, 1, 0, ""); // Scan all SSIDs. + nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids_to_scan); // Add message attribute, which SSIDs to scan for. + nlmsg_free(ssids_to_scan); // Copied to `msg` above, no longer need this. + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, callback_trigger, &results); // Add the callback. + nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); + nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); + nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); + nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); // No sequence checking for multicast messages. + + // Send NL80211_CMD_TRIGGER_SCAN to start the scan. The kernel may reply with NL80211_CMD_NEW_SCAN_RESULTS on + // success or NL80211_CMD_SCAN_ABORTED if another scan was started by another process. + err = 1; + ret = nl_send_auto(socket, msg); // Send the message. + printf("NL80211_CMD_TRIGGER_SCAN sent %d bytes to the kernel.\n", ret); + printf("Waiting for scan to complete...\n"); + while (err > 0) ret = nl_recvmsgs(socket, cb); // First wait for ack_handler(). This helps with basic errors. + if (err < 0) { + printf("WARNING: err has a value of %d.\n", err); + } + if (ret < 0) { + printf("ERROR: nl_recvmsgs() returned %d (%s).\n", ret, nl_geterror(-ret)); + return ret; + } + while (!results.done) nl_recvmsgs(socket, cb); // Now wait until the scan is done or aborted. + if (results.aborted) { + printf("ERROR: Kernel aborted scan.\n"); + return 1; + } + printf("Scan is done.\n"); + + // Cleanup. + nlmsg_free(msg); + nl_cb_put(cb); + nl_socket_drop_membership(socket, mcid); // No longer need this. + return 0; + } + + + int if_index; + struct nl_sock* socket; + int driver_id; + +public: + + WiFiScanLinux(const std::string& devName) { + + if_index = if_nametoindex("wlp0s20u2u1"); // Use this wireless interface for scanning. + + + // Open socket to kernel. + socket = nl_socket_alloc(); // Allocate new netlink socket in memory. + genl_connect(socket); // Create file descriptor and bind socket. + driver_id = genl_ctrl_resolve(socket, "nl80211"); // Find the nl80211 driver ID. + + } + + /** triger WiFiScan and fetch the result */ + WiFiMeasurements scan() { + + // Issue NL80211_CMD_TRIGGER_SCAN to the kernel and wait for it to finish. + int err = do_scan_trigger(socket, if_index, driver_id); + if (err != 0) { + printf("do_scan_trigger() failed with %d.\n", err); + throw "error"; + } + + // Now get info for all SSIDs detected. + struct nl_msg *msg = nlmsg_alloc(); // Allocate a message. + genlmsg_put(msg, 0, 0, driver_id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0); // Setup which command to run. + nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index); // Add message attribute, which interface to use. + nl_socket_modify_cb(socket, NL_CB_VALID, NL_CB_CUSTOM, callback_dump, NULL); // Add the callback. + int ret = nl_send_auto(socket, msg); // Send the message. + printf("NL80211_CMD_GET_SCAN sent %d bytes to the kernel.\n", ret); + ret = nl_recvmsgs_default(socket); // Retrieve the kernel's answer. callback_dump() prints SSIDs to stdout. + nlmsg_free(msg); + if (ret < 0) { + printf("ERROR: nl_recvmsgs_default() returned %d (%s).\n", ret, nl_geterror(-ret)); + throw "error"; + } + + // TODO + WiFiMeasurements mes; + return mes; + + } + + + +}; + + +// gcc -I /usr/include/libnl3 main.c -lnl-genl-3 -lnl-3 + + + +#endif // INDOOR_WIFISCANNER_LINUX_H diff --git a/sensors/radio/scan/WiFiScanLinux_IW.h b/sensors/radio/scan/WiFiScanLinux_IW.h new file mode 100644 index 0000000..138e4f8 --- /dev/null +++ b/sensors/radio/scan/WiFiScanLinux_IW.h @@ -0,0 +1,100 @@ +#ifndef INDOOR_WIFI_SCAN_LINUX_H +#define INDOOR_WIFI_SCAN_LINUX_H + +#include "WiFiChannels.h" +#include "WiFiScan.h" +#include "../WiFiMeasurements.h" + +#include +#include +#include +#include +#include + +class WiFiScanLinux : public WiFiScan { + +private: + + wireless_scan_head head; + wireless_scan *result; + iwrange range; + int sock; + + std::string dev; + +public: + + WiFiScanLinux(const std::string& dev) : dev(dev) { + + /* Open socket to kernel */ + sock = iw_sockets_open(); + std::cout << sock << std::endl; + + } + + WiFiMeasurements scan() override { + + WiFiMeasurements res; + char* dev = (char*) this->dev.c_str(); + + /* Get some metadata to use for scanning */ + if (iw_get_range_info(sock, dev, &range) < 0) { + printf("Error during iw_get_range_info. Aborting.\n"); + exit(2); + } + if (range.we_version_compiled < 14) { + printf("scanning not supported"); + exit(2); + } + +// // params +// struct iwreq request; +// request.u.param.flags = IW_SCAN_DEFAULT; +// request.u.param.value = 0; +// if (iw_set_ext(sock, dev, SIOCSIWSCAN, &request) == -1) { +// printf("iw_set_ext(SIOCSIWSCAN)"); +// exit(EXIT_FAILURE); +// } + + /* Perform the scan */ + if (iw_scan(sock, dev, range.we_version_compiled, &head) < 0) { + printf("Error during iw_scan. Aborting.\n"); + exit(2); + } + + /* Traverse the results */ + result = head.result; + while (NULL != result) { + + // access-point's MAC + const uint8_t* macPtr = (const uint8_t*) result->ap_addr.sa_data; + const uint64_t macLng = ((uint64_t)macPtr[5]<<40)|((uint64_t)macPtr[4]<<32)|((uint64_t)macPtr[3]<<24)|((uint64_t)macPtr[2]<<16)|((uint64_t)macPtr[1]<<8)|((uint64_t)macPtr[0]<<0); + const MACAddress mac(macLng); + + const int8_t rssi = result->stats.qual.level; + const std::string ssid = result->b.essid; + + result->b. + + const int freq = (result->b.freq)/10e5; + const int channel = WiFiChannels::freqToChannel(freq); + //std::cout << ssid << "\t" << "freq: " << freq << "\t" << "rssi: " << (int) (rssi) << " dBm" << std::endl; + //printf("%s - %d\n", result->b.essid, + result = result->next; + + std::cout << mac.asString() << "\t" << ssid << "\t" << channel << "\t" << (int)rssi << " dBm" << std::endl; + + AccessPoint ap(mac, ssid); + + WiFiMeasurement mes(ap, rssi); + res.entries.push_back(mes); + + } + + return res; + + } + +}; + +#endif //INDOOR_WIFI_SCAN_LINUX_H