worked on wifi-scanner for linux
new time-grouping for vap grouper adjusted test-cases minor changes/fixes/improvements
This commit is contained in:
47
sensors/radio/scan/WiFiRAW.h
Normal file
47
sensors/radio/scan/WiFiRAW.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef INDOOR_WIFIRAW_H
|
||||
#define INDOOR_WIFIRAW_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* parse raw binary wifi packets as defined within the standard
|
||||
*/
|
||||
class WiFiRAW {
|
||||
|
||||
public:
|
||||
|
||||
enum Tags {
|
||||
TAG_SSID = 0x00
|
||||
};
|
||||
|
||||
struct TaggedParams {
|
||||
std::string ssid;
|
||||
};
|
||||
|
||||
/** parsed tagged params within wifi packets: [tag][len][len-bytes][tag][len][len-bytes]... */
|
||||
static TaggedParams parseTaggedParams(const uint8_t* data, const size_t len) {
|
||||
|
||||
TaggedParams res;
|
||||
|
||||
int pos = 0;
|
||||
while(pos < len) {
|
||||
|
||||
const int tag = data[pos+0]; // the tag-ID
|
||||
const int len = data[pos+1]; // the lenght of the following data
|
||||
|
||||
switch(tag) {
|
||||
case TAG_SSID: res.ssid = std::string( (const char*) &(data[pos+2]), len); break;
|
||||
}
|
||||
|
||||
// position at the start of the next tag
|
||||
pos += 1 + 1 + len;
|
||||
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // INDOOR_WIFIRAW_H
|
||||
@@ -28,23 +28,28 @@
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include "WiFiRAW.h"
|
||||
#include "WiFiScan.h"
|
||||
|
||||
class WiFiScanLinux : public WiFiScan {
|
||||
|
||||
struct TMP {
|
||||
Timestamp tsStart;
|
||||
WiFiMeasurements res;
|
||||
};
|
||||
|
||||
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.
|
||||
@@ -54,7 +59,6 @@ class WiFiScanLinux : public WiFiScan {
|
||||
return NL_STOP;
|
||||
}
|
||||
|
||||
|
||||
static int finish_handler(struct nl_msg* msg, void* arg) {
|
||||
(void) msg;
|
||||
// Callback for NL_CB_FINISH.
|
||||
@@ -63,7 +67,6 @@ class WiFiScanLinux : public WiFiScan {
|
||||
return NL_SKIP;
|
||||
}
|
||||
|
||||
|
||||
static int ack_handler(struct nl_msg* msg, void* arg) {
|
||||
(void) msg;
|
||||
// Callback for NL_CB_ACK.
|
||||
@@ -72,7 +75,6 @@ class WiFiScanLinux : public WiFiScan {
|
||||
return NL_STOP;
|
||||
}
|
||||
|
||||
|
||||
static int no_seq_check(struct nl_msg* msg, void* arg) {
|
||||
(void) msg;
|
||||
(void) arg;
|
||||
@@ -80,7 +82,6 @@ class WiFiScanLinux : public WiFiScan {
|
||||
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;
|
||||
@@ -113,11 +114,16 @@ class WiFiScanLinux : public WiFiScan {
|
||||
|
||||
|
||||
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, };
|
||||
|
||||
//struct handler_args grp = { .group = group, .id = -ENOENT, };
|
||||
struct handler_args grp;
|
||||
grp.group = group;
|
||||
grp.id = -ENOENT;
|
||||
|
||||
msg = nlmsg_alloc();
|
||||
if (!msg) return -ENOMEM;
|
||||
@@ -157,45 +163,6 @@ class WiFiScanLinux : public WiFiScan {
|
||||
}
|
||||
|
||||
|
||||
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.
|
||||
@@ -220,42 +187,30 @@ class WiFiScanLinux : public WiFiScan {
|
||||
}
|
||||
|
||||
|
||||
static int callback_dump(struct nl_msg* msg, void* arg) {
|
||||
|
||||
(void) arg;
|
||||
static int addResult(struct nl_msg* msg, void* arg) {
|
||||
|
||||
TMP* tmp = (TMP*) 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];
|
||||
// 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_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_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];// = { };
|
||||
// bss_policy[NL80211_BSS_BEACON_IES] = { };
|
||||
|
||||
|
||||
// Parse and error check.
|
||||
@@ -274,124 +229,212 @@ class WiFiScanLinux : public WiFiScan {
|
||||
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");
|
||||
// // 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");
|
||||
|
||||
WiFiRAW::TaggedParams params = WiFiRAW::parseTaggedParams(
|
||||
(const uint8_t*) nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
|
||||
nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS])
|
||||
);
|
||||
|
||||
const Timestamp ts = Timestamp::fromUnixTime() - Timestamp::fromMS(seen_ago_ms);
|
||||
const int freq_MHz = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
|
||||
const uint8_t* macPtr = (const uint8_t*) nla_data(bss[NL80211_BSS_BSSID]);
|
||||
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 AccessPoint ap(mac, params.ssid);
|
||||
const WiFiMeasurement mes(ap, rssi, freq_MHz, ts);
|
||||
|
||||
// by default, linux also lists older scan results
|
||||
// remove them here!
|
||||
if (ts > tmp->tsStart) {
|
||||
//std::cout << seen_ago_ms << std::endl;
|
||||
tmp->res.entries.push_back(mes);
|
||||
std::cout << mes.asString() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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.
|
||||
// int do_scan_trigger(struct nl_sock *socket, int if_index, int driver_id) {
|
||||
|
||||
// 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;
|
||||
}
|
||||
// // 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;
|
||||
// results.done = 0;
|
||||
// results.aborted = 0;
|
||||
|
||||
// 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.
|
||||
// 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.
|
||||
|
||||
// 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");
|
||||
// // Allocate the messages and callback handler.
|
||||
// msg = nlmsg_alloc();
|
||||
// if (!msg) {throw Exception("Failed to allocate netlink message for msg.");}
|
||||
|
||||
// Cleanup.
|
||||
nlmsg_free(msg);
|
||||
nl_cb_put(cb);
|
||||
nl_socket_drop_membership(socket, mcid); // No longer need this.
|
||||
return 0;
|
||||
}
|
||||
// ssids_to_scan = nlmsg_alloc();
|
||||
// if (!ssids_to_scan) {
|
||||
// nlmsg_free(msg);
|
||||
// throw Exception("Failed to allocate netlink message for ssids_to_scan.");
|
||||
// }
|
||||
|
||||
// cb = nl_cb_alloc(NL_CB_DEFAULT);
|
||||
// if (!cb) {
|
||||
// nlmsg_free(msg);
|
||||
// nlmsg_free(ssids_to_scan);
|
||||
// throw Exception("Failed to allocate netlink callbacks.");
|
||||
// }
|
||||
|
||||
// // 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) {
|
||||
struct nl_cb *cb;
|
||||
int mcid;
|
||||
|
||||
if_index = if_nametoindex("wlp0s20u2u1"); // Use this wireless interface for scanning.
|
||||
int err;
|
||||
|
||||
struct trigger_results results;
|
||||
|
||||
// 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.
|
||||
/** configure all needed callback (from netlink to code) once */
|
||||
void setupOnce() {
|
||||
|
||||
mcid = nl_get_multicast_id(socket, "nl80211", "scan");
|
||||
nl_socket_add_membership(socket, mcid); // Without this, callback_trigger() won't be called.
|
||||
|
||||
cb = nl_cb_alloc(NL_CB_DEFAULT);
|
||||
if (!cb) {throw Exception("Failed to allocate netlink callbacks.");}
|
||||
|
||||
// Setup the messages and callback handler.
|
||||
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.
|
||||
|
||||
}
|
||||
|
||||
/** triger WiFiScan and fetch the result */
|
||||
WiFiMeasurements scan() {
|
||||
/** triggers a new scan within the wifi hardware */
|
||||
void triggerNewScan() {
|
||||
|
||||
// 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";
|
||||
std::cout << "triggerNewScan()" << std::endl;
|
||||
|
||||
struct nl_msg *ssids_to_scan;
|
||||
ssids_to_scan = nlmsg_alloc();
|
||||
if (!ssids_to_scan) {throw Exception("Failed to allocate netlink message for ssids_to_scan.");}
|
||||
nla_put(ssids_to_scan, 1, 0, ""); // Scan all SSIDs.
|
||||
|
||||
// construct message
|
||||
struct nl_msg* msg = nlmsg_alloc();
|
||||
if (!msg) {throw Exception("Failed to allocate netlink message for msg.");}
|
||||
|
||||
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_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.
|
||||
|
||||
results.done = 0;
|
||||
results.aborted = 0;
|
||||
|
||||
// trigger scan by sending the constructed message
|
||||
const int 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");
|
||||
nlmsg_free(msg);
|
||||
|
||||
}
|
||||
|
||||
/** blocks until the scan-result is available. true if OK, false otherwise */
|
||||
bool waitForScanResult() {
|
||||
|
||||
// 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 = 0;
|
||||
// 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);
|
||||
// }
|
||||
|
||||
while(true) {
|
||||
const int ret = nl_recvmsgs(socket, cb);
|
||||
printf("-- ret: %d err: %d \n", ret, err);
|
||||
if (results.done) {
|
||||
return true;
|
||||
}
|
||||
if (ret < 0 || err < 0) {
|
||||
nl_recvmsgs(socket, cb); // seems to fix issues when device is busy?!
|
||||
printf("ERROR: nl_recvmsgs() returned %d (%s).\n", ret, nl_geterror(-ret));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void scanResult(TMP* res) {
|
||||
|
||||
// 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.
|
||||
nl_socket_modify_cb(socket, NL_CB_VALID, NL_CB_CUSTOM, addResult, res); // Add the callback and the measurements to fill
|
||||
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.
|
||||
@@ -401,9 +444,94 @@ public:
|
||||
throw "error";
|
||||
}
|
||||
|
||||
// TODO
|
||||
WiFiMeasurements mes;
|
||||
return mes;
|
||||
}
|
||||
|
||||
void scanCleanup() {
|
||||
|
||||
// Cleanup.
|
||||
//nlmsg_free(msg);
|
||||
nl_cb_put(cb);
|
||||
nl_socket_drop_membership(socket, mcid); // No longer need this.
|
||||
//return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public:
|
||||
|
||||
WiFiScanLinux(const std::string& devName) {
|
||||
|
||||
// convert interface-name to interface-index
|
||||
if_index = if_nametoindex(devName.c_str());
|
||||
|
||||
// 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.
|
||||
|
||||
setupOnce();
|
||||
|
||||
}
|
||||
|
||||
~WiFiScanLinux() {
|
||||
|
||||
// cleanup
|
||||
nl_socket_free(socket);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** triger WiFiScan and fetch the result */
|
||||
WiFiMeasurements scan() {
|
||||
|
||||
|
||||
TMP res;
|
||||
|
||||
|
||||
// Issue NL80211_CMD_TRIGGER_SCAN to the kernel and wait for it to finish.
|
||||
// while(true) {
|
||||
|
||||
// // use the current timestamp to suppress older scan results
|
||||
// // which are cached by linux by default
|
||||
// res.tsStart = Timestamp::fromUnixTime();
|
||||
|
||||
// // trigger a scan
|
||||
// //int err = do_scan_trigger(socket, if_index, driver_id);
|
||||
// int err = scanTrigger();
|
||||
//// if (err == -25) {std::this_thread::sleep_for(std::chrono::milliseconds(100)); continue;} // currently busy. try again
|
||||
//// if (err != 0) {throw Exception("do_scan_trigger() failed with code: " + std::to_string(err));}
|
||||
//// break;
|
||||
|
||||
// }
|
||||
|
||||
again:;
|
||||
|
||||
triggerNewScan();
|
||||
|
||||
std::cout << "scan triggered" << std::endl;
|
||||
|
||||
if (waitForScanResult()) {
|
||||
|
||||
std::cout << "scan done" << std::endl;
|
||||
scanResult(&res);
|
||||
|
||||
// return constructed result
|
||||
return res.res;
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
goto again;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user