101 lines
2.3 KiB
C++
101 lines
2.3 KiB
C++
#ifndef INDOOR_WIFI_SCAN_LINUX_H
|
|
#define INDOOR_WIFI_SCAN_LINUX_H
|
|
|
|
#include "WiFiChannels.h"
|
|
#include "WiFiScan.h"
|
|
#include "../WiFiMeasurements.h"
|
|
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
#include <iwlib.h>
|
|
#include <string>
|
|
#include <iostream>
|
|
|
|
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
|