Compare commits

..

5 Commits

Author SHA1 Message Date
Miroslav Hutár
a86a7d3b02 add FTM support 2025-05-17 13:40:40 +02:00
Miroslav Hutár
7860485dbe set frequency only when enable inject/measure 2024-04-30 23:54:32 +02:00
Miroslav Hutár
0f94a0f1cb plot with cairo, remove gnuplot 2024-04-30 22:38:28 +02:00
Miroslav Hutár
2086412fbf async update plot
fix loosing csi packet caused by sync updating plot
2024-04-22 13:37:08 +02:00
Miroslav Hutár
9bb4fd74fa lower compiler optimization flag
higher optimization made segmentation fault on some systems
2024-04-22 10:54:11 +02:00
28 changed files with 4386 additions and 3255 deletions

2
.env
View File

@ -1 +1 @@
FEITCSI_VERSION=1.1.0
FEITCSI_VERSION=2.0.0

View File

@ -87,7 +87,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkNotebook">
<object class="GtkNotebook" id="leftBar">
<property name="visible">True</property>
<property name="can-focus">True</property>
<child>
@ -819,9 +819,16 @@
<property name="can-focus">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkSocket" id="chartSocket">
<object class="GtkBox" id="plotBox">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="resize">False</property>

View File

@ -24,7 +24,7 @@ INCLUDE_LIB_DIRS =
INCLUDE_LIB = $(foreach includedir,$(INCLUDE_LIB_DIRS),-L$(includedir))
# Set compiler, preprocesor and linker flags
CXXFLAGS += -g -O3 -Wall -std=c++17 $(CDEFS) $(INCLUDE)
CXXFLAGS += -g -O1 -Wall -std=c++17 $(CDEFS) $(INCLUDE)
CPPFLAGS += `pkg-config --cflags gtkmm-3.0 libnl-3.0 libnl-genl-3.0 libpcap`
LDFLAGS += $(INCLUDE_LIB)
LDLIBS += `pkg-config --libs gtkmm-3.0 libnl-3.0 libnl-genl-3.0 libpcap`

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2024 Miroslav Hutar.
* Copyright (C) 2024-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -25,8 +25,11 @@
#include <argp.h>
#include "main.h"
#define ETH_ALEN 6
struct Args
{
bool strict;
bool verbose;
uint16_t frequency;
bool gui = false;
@ -48,6 +51,16 @@ struct Args
bool measure;
std::string mode;
std::string ltf;
uint32_t modeDelay;
bool ftm = false;
bool ftmResponder = false;
bool ftmAsap = false;
uint8_t ftmBurstExp;
uint8_t ftmPerBurst;
uint16_t ftmBurstPeriod;
uint8_t ftmBurstDuration;
uint8_t mac[ETH_ALEN];
uint8_t ftmTargetMac[ETH_ALEN];
std::string inputFile;
std::map<enum processor, bool> processors;
};
@ -85,13 +98,22 @@ private:
{"coding", 'c', "CODING", 0, "Coding scheme [LDPC|BCC]"},
{"tx-power", 't', "TXPOWER", 0, "TX power of antenna in dBm [1-22]"},
{"antenna", 'a', "ANTENNA", 0, "Transmitting antenna 1, 2 or 12 for both"},
{"mode", 'i', "MODE", 0, "Mode of program[measure|inject|measureinject]"},
{"mode", 'i', "MODE", 0, "Mode of program[measure|inject|measureinject|ftm|ftmres|injectftmres|measureftm]"},
{"inject-delay", 'd', "INJECTDELAY", 0, "Delay between frame injections is us"},
{"inject-repeat", 'j', "INJECTREPEAT", 0, "How many times inject frame"},
{"verbose", 'v', 0, OPTION_ARG_OPTIONAL, "Produce verbose output"},
{"plot", 'p', 0, OPTION_ARG_OPTIONAL, "Plot CSI data"},
{"gui", 'x', 0, OPTION_ARG_OPTIONAL, "Run application in GUI"},
{"udp-socket", 'u', 0, OPTION_ARG_OPTIONAL, "Run application and listen to UDP"},
{"ftm-asap", 'b', 0, OPTION_ARG_OPTIONAL, "FTM asap mode"},
{"ftm-burst-exp", 'q', "FTMBURSTEXP", 0, "FTM burst exponent"},
{"ftm-per-burst", 'e', "FTMPERBURST", 0, "FTM samples per burst"},
{"ftm-burst-period", 'h', "FTMBURSTPERIOD", 0, "FTM burst period"},
{"ftm-burst-duration", 'k', "FTMBURTSDURATION", 0, "FTM burst duration"},
{"ftm-mac", 'n', "FTMMAC", 0, "FTM target MAC address xx:xx:xx:xx:xx:xx"},
{"mode-delay", 'y', "SWAPTIME", 0, "Delay in ms between inject and ftm responder or measure and ftm initiator when modes are injectftmres|measureftm"},
{"strict", 'z', 0, OPTION_ARG_OPTIONAL, "Strict mode: filter out values that do not contain a specific MCS"},
{"mac", '#', "MAC", 0, "Default NICs MAC will be change to providing MAC xx:xx:xx:xx:xx:xx"},
{0}};
};

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -27,12 +27,14 @@
#define CSI_HEADER_LENGTH 272
struct RawHeaderData
struct __attribute__((__packed__)) RawHeaderData
{
uint32_t csiDataSize;
uint32_t space4;
uint32_t ftmClock;
uint8_t space12[34];
//uint8_t space12[34];
uint64_t timestamp;
uint8_t space20[26];
uint8_t numRx;
uint8_t numTx;
uint8_t space48[4];

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -23,6 +23,8 @@
#include "WiFIController.h"
#include "PacketInjector.h"
#include "gui/MainWindow.h"
#include "gui/Plot.h"
#include "Csi.h"
#include "UdpSocket.h"
#include <thread>
@ -32,8 +34,16 @@ class MainController
public:
inline static UdpSocket *udpSocket = nullptr;
Plot *plotAmplitude;
Plot *plotPhase;
static MainController *getInstance();
static gint updatePlots();
void initPlots();
static void deleteInstance();
void runNoGui(bool detach = false);
@ -59,10 +69,20 @@ private:
inline static MainWindow *mainWindow = nullptr;
Glib::RefPtr<Gtk::Application> app;
inline static Csi *csiToPlot = nullptr;
guint updatePlotsSourceId = 0;
pthread_t measureCsiThread = 0;
pthread_t injectPacketThread = 0;
pthread_t ftmThread = 0;
pthread_t ftmResponderThread = 0;
PacketInjector packetInjector;
WiFIController wifiController;
@ -74,7 +94,18 @@ private:
static void intHandler(int dummy);
static void *injectPackets(void *arg);
static void *ftm(void *arg);
static void *ftmResponder(void *arg);
bool measuring = false;
bool injecting = false;
bool ftmEnabled = false;
bool ftmResponderEnabled = false;
};
#endif

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -36,7 +36,6 @@ public:
void injectVHT();
void injectHE();
private:
void send(uint32_t rateNFlags);
pcap_t *ppcap = nullptr;

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -64,28 +64,36 @@ class WiFIController : public Netlink
public:
void killNetworkProcesses();
void getInterfaces();
void getInterfaceInfo(const char* ifname);
void setTxPower();
void addMonitorDevice(const char *name, const nl80211_iftype type);
void addDevice(const char *name, const nl80211_iftype type);
int setInterfaceUpDown(const char *ifName, bool up);
void setFreq(uint16_t freq, const char *bw);
void removeInterface(const char *name, int ifIndex = 0);
void createMonitorInteface();
void createApInteface();
void getPhys();
int setApMode();
static ChanMode getChanMode(const char *width);
static uint16_t chanModeToWidth(ChanMode &chanMode);
std::vector<InterfaceInfo> interfaces;
std::vector<int64_t> phys;
InterfaceInfo currentInterfaceInfo = {};
private:
std::string macN2a(const unsigned char *arg);
int freqToCHannel(int freq);
int phyLookup(char *name);
std::string currentDeviceName;
static int getCf1(const ChanMode *chanmode, unsigned long freq);
static int processGetInterfacesHandler(nl_msg *msg, void *arg);
static int processGetInterfaceInfoHandler(nl_msg *msg, void *arg);
static int processGetPhysHandler(nl_msg *msg, void *arg);
static int processSetFreq(nl80211_state *state, nl_msg *msg, void *arg);
static int setTxPowerHandler(nl80211_state *state, nl_msg *msg, void *arg);
static int processAddMonitorDeviceHandler(nl80211_state *state, nl_msg *msg, void *arg);
static int processaddDeviceHandler(nl80211_state *state, nl_msg *msg, void *arg);
static int setApModeHandler(nl80211_state *state, nl_msg *msg, void *arg);
};
#endif

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -20,7 +20,9 @@
#define WIFI_CSI_CONTROLLER_H
#include "Netlink.h"
#include "GnuPlot.h"
#include "Csi.h"
#include <mutex>
#include <queue>
#define IWL_MVM_VENDOR_ATTR_CSI_HDR 0x4d
#define IWL_MVM_VENDOR_ATTR_CSI_DATA 0x4e
@ -32,15 +34,17 @@ class WiFiCsiController : public Netlink
public:
void init();
int listenToCsi();
void enableCsi(bool enable = true);
static void enableCsi(bool enable = true);
inline static std::mutex csiQueueMutex;
inline static std::queue<Csi*> csiQueue;
int64_t stopTime = 0;
~WiFiCsiController();
private:
static int listenToCsiHandler(nl80211_state *state, nl_msg *msg, void *arg);
static int processListenToCsiHandler(nl_msg *msg, void *arg);
static void printDetail(Csi &c);
GnuPlot gnuPlot;
static void printDetail(Csi *c);
};
#endif

View File

@ -0,0 +1,63 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WIFI_FTM_CONTROLLER_H
#define WIFI_FTM_CONTROLLER_H
#include "Netlink.h"
struct __attribute__((__packed__)) Ftm
{
uint32_t burstIndex;
uint32_t numFtmrAttempts;
uint32_t numFtmrSuccesses;
uint32_t numBurstsExp;
uint8_t burstDuration;
uint8_t ftmsPerBurst;
uint8_t rssiAvg;
uint32_t rssiSpread;
uint64_t rttAvg;
uint64_t rttVariance;
uint64_t rttSpread;
uint64_t distAvg;
uint64_t distVariance;
uint64_t distSpread;
uint64_t timestamp;
}; // size 79 bytes
#define FTM_SIZE sizeof(Ftm)
class WiFiFtmController : public Netlink
{
public:
void init();
int startInitiator();
int startResponder();
int setApMode();
uint64_t lastRttIsSuccess = false;
private:
static int ftmHandler(nl80211_state *state, nl_msg *msg, void *arg);
static int ftmResponderHandler(nl80211_state *state, nl_msg *msg, void *arg);
static int setApModeHandler(nl80211_state *state, nl_msg *msg, void *arg);
static int processFtmHandler(nl_msg *msg, void *arg);
};
#endif

View File

@ -21,7 +21,6 @@
#include <gtkmm.h>
#include "CsiProcessor.h"
#include "GnuPlot.h"
class CsiProcessingWindow
{
@ -55,7 +54,7 @@ private:
void processingSaveGtkButtonClicked();
CsiProcessor csiProcessor;
GnuPlot gnuPlot;
uint32_t currentIndex = 0;
};

View File

@ -20,7 +20,7 @@
#define MAIN_WINDOW_H
#include <gtkmm.h>
#include "gui/GnuPlot.h"
#include "gui/Plot.h"
#include "gui/CsiProcessingWindow.h"
class MainWindow : public Gtk::ApplicationWindow
@ -31,6 +31,7 @@ public:
MainWindow(BaseObjectType *obj, Glib::RefPtr<Gtk::Builder> const &builder);
void fatalError(std::string msg);
Glib::RefPtr<Gtk::Builder> builder;
private:
int chartWidth = 0;
@ -44,7 +45,6 @@ private:
std::map<std::string, std::string> errors;
Glib::RefPtr<Gtk::Adjustment> consoleScrollbarAdjustment;
Glib::RefPtr<Gtk::Builder> builder;
Glib::RefPtr<Gtk::MessageDialog> errorDialog;
Glib::RefPtr<Gtk::Button> measureButton;
@ -67,8 +67,7 @@ private:
Glib::RefPtr<Gtk::TextView> console;
Glib::RefPtr<Gtk::Label> errorMessages;
Glib::RefPtr<Gtk::Socket> chartSocket;
GnuPlot gnuPlot;
Plot plot;
void closeButton();
@ -103,9 +102,9 @@ private:
void injectRepeatChange();
void updateErrorMessages();
void measureButtonClicked();
void injectButtonClicked();
void updateSensitivity();

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023 Miroslav Hutar.
* Copyright (C) 2024 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -16,27 +16,41 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GNU_PLOT_H
#define GNU_PLOT_H
#include <gtkmm.h>
#include <gtkmm/socket.h>
#include <Csi.h>
#ifndef PLOT_H
#define PLOT_H
#include "Csi.h"
#include <future>
#include <sigc++/sigc++.h>
#include <string>
#include <gtkmm.h>
#include <mutex>
class GnuPlot
class Plot : public Gtk::DrawingArea
{
public:
void init();
void setWindow(uint64_t id);
void setBlank();
void reload();
void updateChart(Csi &csi);
void init(Glib::RefPtr<Gtk::Box> box);
void updateData(Csi *csi, std::vector<double> *data);
double yTicksMax = 200;
double yTicksMin = 0;
std::string yLabel = "";
std::string xLabel = "Subcarrier index";
std::string title = "";
private:
inline static std::string lastCmd;
inline static FILE *gnuPlotPipe;
const double colors[4][3] = {
{ 1.0, 0.0, 0.0 },
{ 0.0, 0.0, 1.0 },
{ 1.0, 0.7, 0.0 },
{ 0.0, 1.0, 0.0 },
};
Csi *csi;
std::vector<double> *data;
std::mutex updateDataMutex;
double yTicks;
bool on_draw(const Cairo::RefPtr<Cairo::Context> &cr);
};
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -19,7 +19,8 @@
#ifndef MAIN_H
#define MAIN_H
#define MONITOR_INTERFACE_NAME "mon0"
#define MONITOR_INTERFACE_NAME "FeitCSImon"
#define AP_INTERFACE_NAME "FeitCSIap"
enum processor
{

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2024 Miroslav Hutar.
* Copyright (C) 2024-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -27,6 +27,7 @@ const char *argp_program_bug_address = "https://github.com/KuskoSoft/FeitCSI/iss
void Arguments::init()
{
Arguments::arguments = {
.strict = false,
.verbose = false,
.frequency = 2412,
.gui = false,
@ -42,11 +43,20 @@ void Arguments::init()
.injectDelay = 100000,
.injectRepeat = 0,
.coding = "LDPC",
.format = "NOHT",
.format = "HT",
.inject = false,
.measure = true,
.mode = "measure",
.ltf = "1xLTF+0.8",
.modeDelay = 3000,
.ftm = false,
.ftmResponder = false,
.ftmAsap = false,
.ftmBurstExp = 0,
.ftmPerBurst = 0,
.ftmBurstPeriod = 0,
.ftmBurstDuration = 0,
.mac = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}
};
}
@ -68,6 +78,9 @@ error_t Arguments::parse_opt(int key, char *arg, struct argp_state *state)
case 'v':
args->verbose = true;
break;
case 'z':
args->strict = true;
break;
case 'x':
args->gui = true;
break;
@ -94,9 +107,30 @@ error_t Arguments::parse_opt(int key, char *arg, struct argp_state *state)
args->measure = true;
args->inject = true;
}
else if (args->mode == "measureftm")
{
args->measure = true;
args->ftm = true;
}
else if (args->mode == "ftm")
{
args->measure = false;
args->ftm = true;
}
else if (args->mode == "ftmres")
{
args->measure = false;
args->ftmResponder = true;
}
else if (args->mode == "injectftmres")
{
args->measure = false;
args->ftmResponder = true;
args->inject = true;
}
else
{
argp_failure(state, 1, 0, "Bad mode. Possible values [measure|inject|measureinject]");
argp_failure(state, 1, 0, "Bad mode. Possible values [measure|inject|measureinject|ftm]");
exit(ARGP_ERR_UNKNOWN);
}
break;
@ -151,6 +185,17 @@ error_t Arguments::parse_opt(int key, char *arg, struct argp_state *state)
}
break;
}
case 'y':
{
int modeDelay = std::atoi(arg);
if (modeDelay <= 0)
{
argp_failure(state, 1, 0, "Mode delay is not correct number");
exit(ARGP_ERR_UNKNOWN);
}
args->modeDelay = (uint32_t)modeDelay;
break;
}
case 'g':
{
int gi = std::atoi(arg);
@ -257,6 +302,74 @@ error_t Arguments::parse_opt(int key, char *arg, struct argp_state *state)
case 'o':
args->outputFile = arg;
break;
case 'b':
args->ftmAsap = true;
break;
case 'q':
{
int f = std::atoi(arg);
if (f <= 0)
{
argp_failure(state, 1, 0, "FTM burst exponent is not correct");
exit(ARGP_ERR_UNKNOWN);
}
args->ftmBurstExp = (uint8_t)f;
break;
}
case 'e':
{
int f = std::atoi(arg);
if (f <= 0)
{
argp_failure(state, 1, 0, "FTM per burst is not correct");
exit(ARGP_ERR_UNKNOWN);
}
args->ftmPerBurst = (uint8_t)f;
break;
}
case 'h':
{
int f = std::atoi(arg);
if (f <= 0)
{
argp_failure(state, 1, 0, "FTM burst period is not correct");
exit(ARGP_ERR_UNKNOWN);
}
args->ftmBurstPeriod = (uint16_t)f;
break;
}
case 'k':
{
int f = std::atoi(arg);
if (f <= 0)
{
argp_failure(state, 1, 0, "FTM burst duration is not correct");
exit(ARGP_ERR_UNKNOWN);
}
args->ftmBurstDuration = (uint8_t)f;
break;
}
case '#':
{
int res = sscanf(arg, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", &args->mac[0], &args->mac[1], &args->mac[2], &args->mac[3], &args->mac[4], &args->mac[5]);
if (res != ETH_ALEN)
{
argp_failure(state, 1, 0, "Bad mac address");
exit(ARGP_ERR_UNKNOWN);
}
break;
}
case 'n':
{
int res = sscanf(arg, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&args->ftmTargetMac[0], &args->ftmTargetMac[1], &args->ftmTargetMac[2], &args->ftmTargetMac[3], &args->ftmTargetMac[4], &args->ftmTargetMac[5]);
if (res != ETH_ALEN)
{
argp_failure(state, 1, 0, "FTM target mac address is not correct");
exit(ARGP_ERR_UNKNOWN);
}
break;
}
case ARGP_KEY_ARG:
case ARGP_KEY_END:
if (args->frequency == 0 ||

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -58,6 +58,7 @@ void Csi::loadFromMemory(uint8_t *pHeader, uint8_t *pRawCsiData)
memcpy(&this->rawHeaderData, pHeader, CSI_HEADER_LENGTH);
this->rawCsiData = new uint8_t[this->rawHeaderData.csiDataSize];
memcpy(this->rawCsiData, pRawCsiData, this->rawHeaderData.csiDataSize);
//this->rawHeaderData.timestamp = (uint64_t)std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
this->processRawCsi();
}

View File

@ -19,7 +19,6 @@
#include "CsiProcessor.h"
#include "main.h"
#include "Logger.h"
#include "GnuPlot.h"
#include "interpolation.h"
#include "Arguments.h"

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -21,6 +21,7 @@
#include "Logger.h"
#include "gui/MainWindow.h"
#include "layout.h"
#include "WiFiFtmController.h"
#include <iostream>
#include <chrono>
#include <thread>
@ -39,23 +40,137 @@ MainController *MainController::getInstance()
return INSTANCE;
}
gint MainController::updatePlots()
{
WiFiCsiController::csiQueueMutex.lock();
if (!WiFiCsiController::csiQueue.empty())
{
if (csiToPlot)
{
delete csiToPlot;
}
csiToPlot = WiFiCsiController::csiQueue.front();
WiFiCsiController::csiQueue.pop();
}
//clear old values
while (!WiFiCsiController::csiQueue.empty()) {
delete WiFiCsiController::csiQueue.front();
WiFiCsiController::csiQueue.pop();
}
WiFiCsiController::csiQueueMutex.unlock();
if (!csiToPlot)
{
return (TRUE);
}
MainController *mainController = MainController::getInstance();
mainController->plotAmplitude->updateData(csiToPlot, &csiToPlot->magnitude);
mainController->plotPhase->updateData(csiToPlot, &csiToPlot->phase);
// force refresh, not wait on next frame redraw
/* GtkAllocation reg;
gtk_widget_get_allocation((GtkWidget *)mainController->plotAmplitude->get_parent()->gobj(), &reg);
cairo_region_t *creg = cairo_region_create_rectangle(&reg);
GdkWindow *pUnderlyingWindow = gtk_widget_get_window((GtkWidget *)mainController->plotAmplitude->get_parent()->gobj());
cairo_t *cr = gdk_cairo_create(pUnderlyingWindow);
gdk_cairo_region(cr, creg);
cairo_clip(cr);
gtk_widget_draw((GtkWidget *)mainController->plotAmplitude->get_parent()->gobj(), cr);
cairo_destroy(cr);
cairo_region_destroy(creg); */
return (TRUE);
}
void MainController::initPlots()
{
Glib::RefPtr<Gtk::Box> plotBox = Glib::RefPtr<Gtk::Box>::cast_dynamic(this->mainWindow->builder->get_object("plotBox"));
MainController *mainController = MainController::getInstance();
mainController->plotAmplitude->yLabel = "Amplitude";
mainController->plotAmplitude->title = "Amplitude";
mainController->plotAmplitude->init(plotBox);
mainController->plotPhase->yLabel = "Phase (rad)";
mainController->plotPhase->title = "Phase";
mainController->plotPhase->yTicksMin = -4;
mainController->plotPhase->yTicksMax = 4;
mainController->plotPhase->init(plotBox);
this->updatePlotsSourceId = gdk_threads_add_idle((GSourceFunc)MainController::updatePlots, nullptr);
}
void MainController::deleteInstance()
{
MainController *mainController = MainController::INSTANCE;
if (mainController->measuring)
{
MainController::INSTANCE->measureCsi(true);
}
if (mainController->injecting)
{
MainController::INSTANCE->injectPackets(true);
}
if (Arguments::arguments.plot)
{
MainController::INSTANCE->app->quit();
}
delete MainController::INSTANCE;
}
void MainController::runNoGui(bool detach)
{
this->initInterface();
if (Arguments::arguments.measure)
if (Arguments::arguments.plot)
{
gtk_init(NULL, NULL);
Glib::init();
this->plotAmplitude = new Plot();
this->plotPhase = new Plot();
this->app = Gtk::Application::create("com.kuskosoft.feitcsi");
std::string layout((char *)MainWindow_glade);
//auto builder = Gtk::Builder::create_from_file("MainWindow.glade");
auto builder = Gtk::Builder::create_from_string(layout);
builder->get_widget_derived("MainWindow", this->mainWindow);
this->initPlots();
if (Arguments::arguments.measure && !Arguments::arguments.ftm)
{
this->measureCsi();
}
if (Arguments::arguments.inject && !Arguments::arguments.ftmResponder)
{
this->injectPackets();
}
this->app->run(*this->mainWindow);
return;
}
if (Arguments::arguments.measure && !Arguments::arguments.ftm)
{
this->measuring = true;
pthread_create(&this->measureCsiThread, NULL, &MainController::measureCsi, NULL);
}
if (Arguments::arguments.inject)
if (Arguments::arguments.inject && !Arguments::arguments.ftmResponder)
{
this->injecting = true;
pthread_create(&this->injectPacketThread, NULL, &MainController::injectPackets, NULL);
}
if (Arguments::arguments.measure)
if (Arguments::arguments.ftm)
{
this->ftmEnabled = true;
pthread_create(&this->ftmThread, NULL, &MainController::ftm, NULL);
}
if (Arguments::arguments.ftmResponder)
{
this->ftmResponderEnabled = true;
pthread_create(&this->ftmResponderThread, NULL, &MainController::ftmResponder, NULL);
}
if (Arguments::arguments.measure && !Arguments::arguments.ftm)
{
if (detach) {
pthread_detach(this->measureCsiThread);
@ -63,7 +178,7 @@ void MainController::runNoGui(bool detach)
pthread_join(this->measureCsiThread, NULL);
}
}
if (Arguments::arguments.inject)
if (Arguments::arguments.inject && !Arguments::arguments.ftmResponder)
{
if (detach) {
pthread_detach(this->injectPacketThread);
@ -71,21 +186,41 @@ void MainController::runNoGui(bool detach)
pthread_join(this->injectPacketThread, NULL);
}
}
if (!detach) {
this->deleteInstance();
if (Arguments::arguments.ftm)
{
if (detach)
{
pthread_detach(this->ftmThread);
}
else
{
pthread_join(this->ftmThread, NULL);
}
}
if (Arguments::arguments.ftmResponder)
{
if (detach)
{
pthread_detach(this->ftmResponderThread);
}
else
{
pthread_join(this->ftmResponderThread, NULL);
}
}
}
void MainController::measureCsi(bool stop)
{
this->wifiController.setFreq(Arguments::arguments.frequency, Arguments::arguments.bandwidth.c_str());
if (stop)
{
this->measuring = false;
pthread_cancel(this->measureCsiThread);
}
else
{
this->wifiController.setFreq(Arguments::arguments.frequency, Arguments::arguments.bandwidth.c_str());
this->measuring = true;
pthread_create(&this->measureCsiThread, NULL, &MainController::measureCsi, NULL);
pthread_detach(this->measureCsiThread);
}
@ -93,14 +228,15 @@ void MainController::measureCsi(bool stop)
void MainController::injectPackets(bool stop)
{
this->wifiController.setTxPower();
this->wifiController.setFreq(Arguments::arguments.frequency, Arguments::arguments.bandwidth.c_str());
if (stop)
{
this->injecting = false;
pthread_cancel(this->injectPacketThread);
}
else
{
this->wifiController.setFreq(Arguments::arguments.frequency, Arguments::arguments.bandwidth.c_str());
this->injecting = true;
pthread_create(&this->injectPacketThread, NULL, &MainController::injectPackets, NULL);
pthread_detach(this->injectPacketThread);
}
@ -108,17 +244,23 @@ void MainController::injectPackets(bool stop)
void MainController::runGui()
{
this->initInterface();
Arguments::arguments.plot = true;
Arguments::arguments.verbose = true;
Arguments::arguments.measure = false;
Arguments::arguments.inject = false;
gtk_init(NULL, NULL);
Glib::init();
auto app = Gtk::Application::create("com.kuskosoft.feitcsi");
this->plotAmplitude = new Plot();
this->plotPhase = new Plot();
this->app = Gtk::Application::create("com.kuskosoft.feitcsi");
std::string layout((char*)MainWindow_glade);
//auto builder = Gtk::Builder::create_from_file("MainWindow.glade");
auto builder = Gtk::Builder::create_from_string(layout);
builder->get_widget_derived("MainWindow", this->mainWindow);
app->run(*this->mainWindow);
this->initPlots();
//g_signal_connect(app, "delete-event", G_CALLBACK(MainController::deleteInstance), NULL);
this->app->run(*this->mainWindow);
}
void MainController::runUdpSocket()
@ -145,15 +287,10 @@ void MainController::initInterface()
this->bkpInterfaces.push_back(interface);
this->wifiController.removeInterface(NULL, interface.ifIndex);
}
this->wifiController.addMonitorDevice(MONITOR_INTERFACE_NAME, NL80211_IFTYPE_MONITOR);
this->wifiController.setInterfaceUpDown(MONITOR_INTERFACE_NAME, true);
this->wifiController.setFreq(Arguments::arguments.frequency, Arguments::arguments.bandwidth.c_str());
if (!MainController::mainWindow)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500)); //wait on init then set power and go next
this->wifiController.setTxPower();
}
this->wifiController.createMonitorInteface();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
this->wifiController.createApInteface();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
catch(const std::exception& e)
{
@ -174,6 +311,8 @@ void *MainController::measureCsi(void *arg)
{
try
{
MainController::getInstance()->wifiController.setInterfaceUpDown(AP_INTERFACE_NAME, false);
MainController::getInstance()->wifiController.setInterfaceUpDown(MONITOR_INTERFACE_NAME, true);
WiFiCsiController wcs;
wcs.init();
wcs.listenToCsi();
@ -193,10 +332,164 @@ void *MainController::measureCsi(void *arg)
return 0;
}
void *MainController::ftm(void *arg)
{
bool firstRun = true;
try
{
MainController::getInstance()->wifiController.setInterfaceUpDown(AP_INTERFACE_NAME, true);
WiFiFtmController wft;
wft.init();
if (Arguments::arguments.measure)
{
uint64_t startFtmTime = 0;
while (1)
{
std::this_thread::sleep_for(std::chrono::microseconds(Arguments::arguments.injectDelay));
try
{
wft.startInitiator();
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
if (wft.lastRttIsSuccess && firstRun)
{
firstRun = false;
startFtmTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
uint64_t now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
if ((startFtmTime + (Arguments::arguments.modeDelay / 2)) > now)
{
continue;
}
if (!wft.lastRttIsSuccess && !firstRun)
{
MainController::getInstance()->measureCsi(false);
std::this_thread::sleep_for(std::chrono::milliseconds(Arguments::arguments.modeDelay));
MainController::getInstance()->measureCsi(true);
WiFiCsiController::enableCsi(false);
firstRun = true;
MainController::getInstance()->wifiController.setInterfaceUpDown(MONITOR_INTERFACE_NAME, false);
MainController::getInstance()->wifiController.setInterfaceUpDown(AP_INTERFACE_NAME, true);
}
}
}
else
{
if (Arguments::arguments.injectRepeat)
{
for (uint32_t i = 0; i < Arguments::arguments.injectRepeat; i++)
{
wft.startInitiator();
std::this_thread::sleep_for(std::chrono::microseconds(Arguments::arguments.injectDelay));
}
}
else
{
while (true)
{
wft.startInitiator();
std::this_thread::sleep_for(std::chrono::microseconds(Arguments::arguments.injectDelay));
}
}
}
}
catch (const std::exception &e)
{
if (MainController::mainWindow)
{
MainController::mainWindow->fatalError(e.what());
}
else
{
Logger::log(error) << e.what() << '\n';
}
}
return 0;
}
void *MainController::ftmResponder(void *arg)
{
WiFiFtmController wft;
wft.init();
try
{
if (Arguments::arguments.inject)
{
while (true)
{
MainController::getInstance()->injectPackets(false);
std::this_thread::sleep_for(std::chrono::milliseconds(Arguments::arguments.modeDelay));
MainController::getInstance()->injectPackets(true);
MainController::getInstance()->wifiController.setInterfaceUpDown(MONITOR_INTERFACE_NAME, false);
MainController::getInstance()->wifiController.setInterfaceUpDown(AP_INTERFACE_NAME, true);
MainController::getInstance()->wifiController.getInterfaceInfo(AP_INTERFACE_NAME);
while (MainController::getInstance()->wifiController.currentInterfaceInfo.freq == 0)
{
try
{
wft.startResponder();
}
catch (const std::exception &e)
{
// Just ignore.. wait on network up
}
MainController::getInstance()->wifiController.getInterfaceInfo(AP_INTERFACE_NAME);
}
std::this_thread::sleep_for(std::chrono::milliseconds(Arguments::arguments.modeDelay));
}
} else
{
while(MainController::getInstance()->wifiController.currentInterfaceInfo.freq == 0) {
try
{
wft.startResponder();
}
catch(const std::exception& e)
{
//Just ignore.. wait on network up
}
MainController::getInstance()->wifiController.getInterfaceInfo(AP_INTERFACE_NAME);
}
if (Arguments::arguments.verbose)
{
Logger::log(info) << "FTM responder was started\n";
}
}
while (1)
{
sleep(1);
}
}
catch (const std::exception &e)
{
if (MainController::mainWindow)
{
MainController::mainWindow->fatalError(e.what());
}
else
{
Logger::log(error) << e.what() << '\n';
}
}
return 0;
}
void *MainController::injectPackets(void *arg)
{
try
{
MainController::getInstance()->wifiController.setInterfaceUpDown(AP_INTERFACE_NAME, false);
MainController::getInstance()->wifiController.setInterfaceUpDown(MONITOR_INTERFACE_NAME, true);
PacketInjector pi;
if (Arguments::arguments.injectRepeat)
{
@ -226,7 +519,7 @@ void *MainController::injectPackets(void *arg)
Logger::log(error) << e.what() << '\n';
}
}
return 0;
}
@ -244,13 +537,14 @@ void MainController::restoreState()
}
mainController->wifiController.removeInterface(MONITOR_INTERFACE_NAME);
mainController->wifiController.removeInterface(AP_INTERFACE_NAME);
for (InterfaceInfo interface : mainController->bkpInterfaces)
{
if (Arguments::arguments.verbose)
{
Logger::log(info) << "Recovering interface " << interface.ifName << "\n";
}
mainController->wifiController.addMonitorDevice(interface.ifName.c_str(), (nl80211_iftype)interface.ifType);
mainController->wifiController.addDevice(interface.ifName.c_str(), (nl80211_iftype)interface.ifType);
}
mainController->bkpInterfaces.clear();
mainController->wifiController.interfaces.clear();
@ -262,15 +556,19 @@ void MainController::restoreState()
}
MainController::~MainController()
{
{
this->restoreState();
if (udpSocket) {
delete udpSocket;
}
if (csiToPlot)
{
delete csiToPlot;
}
}
void MainController::intHandler(int dummy)
{
delete MainController::INSTANCE;
MainController::INSTANCE->deleteInstance();
exit(0);
}

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -27,7 +27,7 @@
void Netlink::init()
{
/* int err = */ this->nlInit(&this->nlstate);
/* int err = */ this->nlInit(&this->nlstate);
// TODO if error
}
@ -127,6 +127,7 @@ int Netlink::nlExecCommand(Cmd &cmd)
nl_socket_set_cb(this->nlstate.nl_sock, s_cb);
err = nl_send_auto_complete(this->nlstate.nl_sock, msg);
if (err < 0)
goto out;
@ -145,7 +146,6 @@ int Netlink::nlExecCommand(Cmd &cmd)
{
errMsg = "command failed: " + std::string(strerror(-err)) + std::to_string(err) + "\n";
}
out:
nl_cb_put(cb);
nl_cb_put(s_cb);

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -175,7 +175,7 @@ void PacketInjector::send(uint32_t rateNFlags)
char szErrbuf[500];
if (ppcap == nullptr) {
ppcap = pcap_open_live("mon0", 800, 1, 20, szErrbuf);
ppcap = pcap_open_live(MONITOR_INTERFACE_NAME, 800, 1, 20, szErrbuf);
}
int r = pcap_inject(ppcap, sendBuffer, totalSize);

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -107,20 +107,34 @@ void WiFIController::getInterfaces()
this->nlExecCommand(cmd);
}
void WiFIController::getInterfaceInfo(const char *ifname)
{
Cmd cmd{
.id = NL80211_CMD_GET_INTERFACE,
.idby = CIB_NETDEV,
.nlFlags = 0,
.device = if_nametoindex(ifname),
.handler = NULL,
.valid_handler = this->processGetInterfaceInfoHandler,
};
this->nlExecCommand(cmd);
}
void WiFIController::setTxPower()
{
Cmd cmd{
.id = NL80211_CMD_SET_WIPHY,
.idby = CIB_NETDEV,
.nlFlags = 0,
.device = if_nametoindex(MONITOR_INTERFACE_NAME),
.device = if_nametoindex(this->currentDeviceName.c_str()),
.handler = this->setTxPowerHandler,
};
this->nlExecCommand(cmd);
}
void WiFIController::addMonitorDevice(const char *name, const nl80211_iftype type)
void WiFIController::addDevice(const char *name, const nl80211_iftype type)
{
if (this->phys.empty())
{
@ -135,7 +149,7 @@ void WiFIController::addMonitorDevice(const char *name, const nl80211_iftype typ
.idby = CIB_PHY,
.nlFlags = 0,
.device = this->phys[0],
.handler = this->processAddMonitorDeviceHandler,
.handler = this->processaddDeviceHandler,
.valid_handler = NULL,
.args = settings,
};
@ -166,6 +180,7 @@ int WiFIController::setInterfaceUpDown(const char *ifName, bool up)
if (up)
{
ifr.ifr_flags |= IFF_UP;
this->currentDeviceName = ifName;
}
else
{
@ -196,7 +211,7 @@ void WiFIController::setFreq(uint16_t freq, const char *bw)
.id = NL80211_CMD_SET_WIPHY,
.idby = CIB_NETDEV,
.nlFlags = 0,
.device = if_nametoindex(MONITOR_INTERFACE_NAME),
.device = if_nametoindex(this->currentDeviceName.c_str()),
.handler = processSetFreq,
.valid_handler = NULL,
.args = settings,
@ -217,6 +232,48 @@ void WiFIController::removeInterface(const char *name, int ifIndex)
this->nlExecCommand(cmd);
}
void WiFIController::createMonitorInteface()
{
this->addDevice(MONITOR_INTERFACE_NAME, NL80211_IFTYPE_MONITOR);
while (this->currentInterfaceInfo.freq != Arguments::arguments.frequency)
{
try
{
this->setInterfaceUpDown(MONITOR_INTERFACE_NAME, true);
this->setFreq(Arguments::arguments.frequency, Arguments::arguments.bandwidth.c_str());
}
catch (const std::exception &e)
{
// Just skip
}
this->getInterfaceInfo(MONITOR_INTERFACE_NAME);
}
this->setTxPower();
}
void WiFIController::createApInteface()
{
this->addDevice(AP_INTERFACE_NAME, NL80211_IFTYPE_MONITOR);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
while (this->currentInterfaceInfo.ifType != NL80211_IFTYPE_AP)
{
try
{
this->setInterfaceUpDown(MONITOR_INTERFACE_NAME, false);
this->setApMode();
}
catch (const std::exception &e)
{
// Just skip
}
this->getInterfaceInfo(AP_INTERFACE_NAME);
}
this->setInterfaceUpDown(AP_INTERFACE_NAME, true);
this->setTxPower();
}
void WiFIController::getPhys()
{
Cmd cmd{
@ -414,10 +471,10 @@ nla_put_failure:
return -ENOBUFS;
}
int WiFIController::setTxPowerHandler(nl80211_state * state, nl_msg * msg, void * arg)
int WiFIController::setTxPowerHandler(nl80211_state *state, nl_msg *msg, void *arg)
{
enum nl80211_tx_power_setting type = NL80211_TX_POWER_FIXED;
int mbm = Arguments::arguments.txPower * 100; //dBm to mbm *100
int mbm = Arguments::arguments.txPower * 100; // dBm to mbm *100
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_TX_POWER_SETTING, type);
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL, mbm);
@ -428,7 +485,7 @@ nla_put_failure:
return -ENOBUFS;
}
int WiFIController::processAddMonitorDeviceHandler(struct nl80211_state *state, struct nl_msg *msg, void *arg)
int WiFIController::processaddDeviceHandler(struct nl80211_state *state, struct nl_msg *msg, void *arg)
{
void **settings = (void **)arg;
const char *name = (const char *)settings[0];
@ -436,6 +493,8 @@ int WiFIController::processAddMonitorDeviceHandler(struct nl80211_state *state,
NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, *type);
NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, Arguments::arguments.mac);
return 0;
nla_put_failure:
return -ENOBUFS;
@ -592,6 +651,104 @@ int WiFIController::processGetInterfacesHandler(struct nl_msg *msg, void *arg)
return NL_OK;
}
int WiFIController::processGetInterfaceInfoHandler(struct nl_msg *msg, void *arg)
{
struct genlmsghdr *gnlh = (genlmsghdr *)nlmsg_data(nlmsg_hdr(msg));
struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0), NULL);
WiFIController *wc = (WiFIController *)arg;
if (tb_msg[NL80211_ATTR_IFNAME])
{
wc->currentInterfaceInfo.ifName = nla_get_string(tb_msg[NL80211_ATTR_IFNAME]);
}
else
{
wc->currentInterfaceInfo.ifName = "Unnamed/non-netdev interface";
return NL_OK;
}
if (tb_msg[NL80211_ATTR_IFINDEX])
{
wc->currentInterfaceInfo.ifIndex = nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]);
}
else
{
wc->currentInterfaceInfo.ifIndex = 0;
}
if (tb_msg[NL80211_ATTR_WDEV])
{
wc->currentInterfaceInfo.wdev = (uint64_t)nla_get_u64(tb_msg[NL80211_ATTR_WDEV]);
}
else
{
wc->currentInterfaceInfo.wdev = 0;
}
if (tb_msg[NL80211_ATTR_MAC])
{
wc->currentInterfaceInfo.mac = wc->macN2a((const unsigned char *)nla_data(tb_msg[NL80211_ATTR_MAC]));
}
else
{
wc->currentInterfaceInfo.mac = "";
}
if (tb_msg[NL80211_ATTR_SSID])
{
wc->currentInterfaceInfo.ssid = (const char *)nla_data(tb_msg[NL80211_ATTR_SSID]);
}
else
{
wc->currentInterfaceInfo.ssid = "";
}
if (tb_msg[NL80211_ATTR_IFTYPE])
{
wc->currentInterfaceInfo.ifType = nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE]);
}
else
{
wc->currentInterfaceInfo.ifType = 0;
}
if (tb_msg[NL80211_ATTR_WIPHY])
{
wc->currentInterfaceInfo.wiphy = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
}
else
{
wc->currentInterfaceInfo.wiphy = 0;
}
if (tb_msg[NL80211_ATTR_WIPHY_FREQ])
{
wc->currentInterfaceInfo.freq = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_FREQ]);
wc->currentInterfaceInfo.channel = wc->freqToCHannel(wc->currentInterfaceInfo.freq);
}
else
{
wc->currentInterfaceInfo.freq = 0;
wc->currentInterfaceInfo.channel = 0;
}
if (tb_msg[NL80211_ATTR_WIPHY_TX_POWER_LEVEL])
{
int32_t txp = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]);
wc->currentInterfaceInfo.txdBm = txp % 100;
wc->currentInterfaceInfo.txdBm += txp / 100;
}
else
{
wc->currentInterfaceInfo.txdBm = 0;
}
return NL_OK;
}
std::string WiFIController::macN2a(const unsigned char *arg)
{
int i, l;
@ -659,4 +816,26 @@ int WiFIController::phyLookup(char *name)
buf[pos] = '\0';
close(fd);
return atoi(buf);
}
int WiFIController::setApMode()
{
Cmd cmd{
.id = NL80211_CMD_SET_INTERFACE,
.idby = CIB_NETDEV,
.nlFlags = 0,
.device = if_nametoindex(AP_INTERFACE_NAME),
.handler = this->setApModeHandler,
.valid_handler = NULL,
};
return this->nlExecCommand(cmd);
}
int WiFIController::setApModeHandler(struct nl80211_state *state, struct nl_msg *msg, void *arg)
{
NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
return 0;
nla_put_failure:
return -ENOBUFS;
}

View File

@ -1,6 +1,6 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
* Copyright (C) 2023-2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -18,7 +18,6 @@
#include "WiFiCsiController.h"
#include "Csi.h"
#include "GnuPlot.h"
#include "MainController.h"
#include "Arguments.h"
@ -44,8 +43,6 @@ void WiFiCsiController::init()
int WiFiCsiController::listenToCsi()
{
gnuPlot.init();
Cmd cmd{
.id = NL80211_CMD_VENDOR,
.idby = CIB_NETDEV,
@ -69,7 +66,6 @@ nla_put_failure:
int WiFiCsiController::processListenToCsiHandler(struct nl_msg *msg, void *arg)
{
WiFiCsiController *instance = (WiFiCsiController *)arg;
struct nlattr *attrs[MAX_CMD + 1];
struct nlmsghdr *nlh = nlmsg_hdr(msg);
@ -93,50 +89,67 @@ int WiFiCsiController::processListenToCsiHandler(struct nl_msg *msg, void *arg)
uint8_t *dataCsi = (uint8_t *)nla_data(attrs[IWL_MVM_VENDOR_ATTR_CSI_DATA]);
memcpy(rawCsi, dataCsi, dataLength);
Csi c;
c.loadFromMemory(header, dataCsi);
Csi *c = new Csi();
c->loadFromMemory(header, dataCsi);
if (
(c.channelWidth == RATE_MCS_CHAN_WIDTH_20 && Arguments::arguments.channelWidth == 20) ||
(c.channelWidth == RATE_MCS_CHAN_WIDTH_40 && Arguments::arguments.channelWidth == 40) ||
(c.channelWidth == RATE_MCS_CHAN_WIDTH_80 && Arguments::arguments.channelWidth == 80) ||
(c.channelWidth == RATE_MCS_CHAN_WIDTH_160 && Arguments::arguments.channelWidth == 160)
(c->channelWidth == RATE_MCS_CHAN_WIDTH_20 && Arguments::arguments.channelWidth == 20) ||
(c->channelWidth == RATE_MCS_CHAN_WIDTH_40 && Arguments::arguments.channelWidth == 40) ||
(c->channelWidth == RATE_MCS_CHAN_WIDTH_80 && Arguments::arguments.channelWidth == 80) ||
(c->channelWidth == RATE_MCS_CHAN_WIDTH_160 && Arguments::arguments.channelWidth == 160)
)
{
if (
(c.format == RATE_MCS_LEGACY_OFDM_MSK && Arguments::arguments.format == "NOHT") ||
(c.format == RATE_MCS_HT_MSK && Arguments::arguments.format == "HT") ||
(c.format == RATE_MCS_VHT_MSK && Arguments::arguments.format == "VHT") ||
(c.format == RATE_MCS_HE_MSK && Arguments::arguments.format == "HESU") ||
(c.format == RATE_MCS_EHT_MSK && Arguments::arguments.format == "EHT")
(c->format == RATE_MCS_LEGACY_OFDM_MSK && Arguments::arguments.format == "NOHT") ||
(c->format == RATE_MCS_HT_MSK && Arguments::arguments.format == "HT") ||
(c->format == RATE_MCS_VHT_MSK && Arguments::arguments.format == "VHT") ||
(c->format == RATE_MCS_HE_MSK && Arguments::arguments.format == "HESU") ||
(c->format == RATE_MCS_EHT_MSK && Arguments::arguments.format == "EHT")
)
{
if (Arguments::arguments.verbose) {
printDetail(c);
}
instance->gnuPlot.updateChart(c);
if ( MainController::getInstance()->udpSocket ) {
c.sendUDP(MainController::getInstance()->udpSocket);
} else {
c.save();
printf("masm csi\n");
if (!Arguments::arguments.strict || (Arguments::arguments.strict && (c->rawHeaderData.rateNflag & RATE_LEGACY_RATE_MSK) == Arguments::arguments.mcs))
{
if (Arguments::arguments.verbose) {
printDetail(c);
}
if ( MainController::getInstance()->udpSocket ) {
c->sendUDP(MainController::getInstance()->udpSocket);
} else {
c->save();
}
if (Arguments::arguments.plot)
{
WiFiCsiController::csiQueueMutex.lock();
WiFiCsiController::csiQueue.push(c);
WiFiCsiController::csiQueueMutex.unlock();
}
}
}
}
}
}
WiFiCsiController *wcc = (WiFiCsiController*) arg;
auto now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
if (wcc->stopTime != 0 && wcc->stopTime < now)
{
return NL_OK;
}
return NL_SKIP;
}
void WiFiCsiController::printDetail(Csi &c)
void WiFiCsiController::printDetail(Csi *c)
{
Logger::log(info) << "Subcarrier count: " << c.rawHeaderData.numSubCarriers << ", ";
Logger::log(info, true) << "RX: " << +c.rawHeaderData.numRx << ", ";
Logger::log(info, true) << "TX: " << +c.rawHeaderData.numTx << ", ";
Logger::log(info) << "Subcarrier count: " << c->rawHeaderData.numSubCarriers << ", ";
Logger::log(info, true) << "RX: " << +c->rawHeaderData.numRx << ", ";
Logger::log(info, true) << "TX: " << +c->rawHeaderData.numTx << ", ";
switch (c.channelWidth)
switch (c->channelWidth)
{
case RATE_MCS_CHAN_WIDTH_20:
Logger::log(info, true) << "Channel width: 20, ";
@ -151,7 +164,7 @@ void WiFiCsiController::printDetail(Csi &c)
Logger::log(info, true) << "Channel width: 160, ";
break;
}
switch (c.format)
switch (c->format)
{
case RATE_MCS_CCK_MSK: // VERY OLD FORMAT
Logger::log(info, true) << "Format: CCK\n";
@ -159,23 +172,18 @@ void WiFiCsiController::printDetail(Csi &c)
case RATE_MCS_LEGACY_OFDM_MSK:
Logger::log(info, true) << "Format: LEGACY_OFDM\n";
break;
break;
case RATE_MCS_HT_MSK:
Logger::log(info, true) << "Format: HT\n";
break;
break;
case RATE_MCS_VHT_MSK:
Logger::log(info, true) << "Format: VHT\n";
break;
break;
case RATE_MCS_HE_MSK:
Logger::log(info, true) << "Format: HE\n";
break;
break;
case RATE_MCS_EHT_MSK:
Logger::log(info, true) << "Format: EHT\n";
break;
break;
}
}

339
src/WiFiFtmController.cpp Normal file
View File

@ -0,0 +1,339 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2025 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WiFiFtmController.h"
#include "main.h"
#include "Arguments.h"
#include <net/if.h>
#include "Logger.h"
#include "MainController.h"
#include <netlink/genl/genl.h>
#include <fstream>
#include <filesystem>
#include <cstring>
uint8_t beaconHeader[] = {0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xea, 0xb9, 0xcc, 0xc2, 0x4a, 0xaf, 0xea, 0xb9, 0xcc, 0xc2, 0x4a, 0xaf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x01, 0x04, 0x00, 0x07, 0x46, 0x65, 0x69, 0x74, 0x43, 0x53, 0x49, 0x01, 0x08, 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x03, 0x01, 0x01};
void WiFiFtmController::init()
{
Netlink::init();
}
int WiFiFtmController::startInitiator()
{
Cmd cmd{
.id = NL80211_CMD_PEER_MEASUREMENT_START,
.idby = CIB_NETDEV,
.nlFlags = 0,
.device = if_nametoindex(AP_INTERFACE_NAME),
.handler = this->ftmHandler,
.valid_handler = this->processFtmHandler,
};
return this->nlExecCommand(cmd);
}
int WiFiFtmController::startResponder()
{
Cmd cmd{
.id = NL80211_CMD_NEW_BEACON,
.idby = CIB_NETDEV,
.nlFlags = 0,
.device = if_nametoindex(AP_INTERFACE_NAME),
.handler = this->ftmResponderHandler,
.valid_handler = NULL,
};
return this->nlExecCommand(cmd);
}
int WiFiFtmController::setApMode()
{
Cmd cmd{
.id = NL80211_CMD_SET_INTERFACE,
.idby = CIB_NETDEV,
.nlFlags = 0,
.device = if_nametoindex(AP_INTERFACE_NAME),
.handler = this->setApModeHandler,
.valid_handler = NULL,
};
return this->nlExecCommand(cmd);
}
int WiFiFtmController::ftmHandler(struct nl80211_state *state, struct nl_msg *msg, void *arg)
{
struct nlattr *pmsr, *peers, *peer, *req, *reqdata, *ftm, *chan;
pmsr = nla_nest_start(msg, NL80211_ATTR_PEER_MEASUREMENTS);
peers = nla_nest_start(msg, NL80211_PMSR_ATTR_PEERS);
peer = nla_nest_start(msg, 1); // TODO multiple peers
NLA_PUT(msg, NL80211_PMSR_PEER_ATTR_ADDR, ETH_ALEN, Arguments::arguments.ftmTargetMac);
req = nla_nest_start(msg, NL80211_PMSR_PEER_ATTR_REQ);
reqdata = nla_nest_start(msg, NL80211_PMSR_REQ_ATTR_DATA);
ftm = nla_nest_start(msg, NL80211_PMSR_TYPE_FTM);
if (Arguments::arguments.ftmBurstExp)
{
NLA_PUT_U8(msg, NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP, Arguments::arguments.ftmBurstExp);
}
if (Arguments::arguments.ftmBurstPeriod)
{
NLA_PUT_U16(msg, NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD, Arguments::arguments.ftmBurstPeriod);
}
if (Arguments::arguments.ftmBurstDuration)
{
NLA_PUT_U8(msg, NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION, Arguments::arguments.ftmBurstDuration);
}
if (Arguments::arguments.ftmPerBurst)
{
NLA_PUT_U8(msg, NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST, Arguments::arguments.ftmPerBurst);
}
if (Arguments::arguments.ftmAsap)
{
NLA_PUT_FLAG(msg, NL80211_PMSR_FTM_REQ_ATTR_ASAP);
}
if (Arguments::arguments.format == "NOHT")
{
NLA_PUT_U32(msg, NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE, NL80211_PREAMBLE_LEGACY);
}
else if (Arguments::arguments.format == "HT")
{
NLA_PUT_U32(msg, NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE, NL80211_PREAMBLE_HT);
}
else if (Arguments::arguments.format == "VHT")
{
NLA_PUT_U32(msg, NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE, NL80211_PREAMBLE_VHT);
}
else if (Arguments::arguments.format == "HESU")
{
NLA_PUT_U32(msg, NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE, NL80211_PREAMBLE_HE);
}
nla_nest_end(msg, ftm);
nla_nest_end(msg, reqdata);
nla_nest_end(msg, req);
chan = nla_nest_start(msg, NL80211_PMSR_PEER_ATTR_CHAN);
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, (uint32_t)Arguments::arguments.frequency);
switch (Arguments::arguments.channelWidth)
{
case 20:
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_20);
break;
case 40:
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_40);
break;
case 80:
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_80);
break;
case 160:
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_160);
break;
case 320:
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_320);
break;
}
nla_nest_end(msg, chan);
nla_nest_end(msg, peer);
nla_nest_end(msg, peers);
nla_nest_end(msg, pmsr);
return 0;
nla_put_failure:
return -ENOBUFS;
}
int WiFiFtmController::ftmResponderHandler(struct nl80211_state *state, struct nl_msg *msg, void *arg)
{
NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, 58, beaconHeader);
NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, 100);
NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, 2);
NLA_PUT(msg, NL80211_ATTR_SSID, 7, "FeitCSI");
NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID, NL80211_HIDDEN_SSID_NOT_IN_USE);
NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, NL80211_AUTHTYPE_OPEN_SYSTEM);
struct nlattr *ftm;
ftm = nla_nest_start(msg, NL80211_ATTR_FTM_RESPONDER);
NLA_PUT_FLAG(msg, NL80211_FTM_RESP_ATTR_ENABLED);
nla_nest_end(msg, ftm);
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, (uint32_t)Arguments::arguments.frequency);
switch (Arguments::arguments.channelWidth)
{
case 20:
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_20);
break;
case 40:
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_40);
break;
case 80:
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_80);
break;
case 160:
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_160);
break;
case 320:
NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_320);
break;
}
NLA_PUT_FLAG(msg, NL80211_ATTR_SOCKET_OWNER);
// nl_msg_dump(msg, stdout);
return 0;
nla_put_failure:
return -ENOBUFS;
}
int WiFiFtmController::setApModeHandler(struct nl80211_state *state, struct nl_msg *msg, void *arg)
{
NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
return 0;
nla_put_failure:
return -ENOBUFS;
}
int WiFiFtmController::processFtmHandler(struct nl_msg *msg, void *arg)
{
struct genlmsghdr *gnlh = (genlmsghdr *)nlmsg_data(nlmsg_hdr(msg));
struct nlattr *tb[NL80211_ATTR_MAX + 1];
nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
if (gnlh->cmd == NL80211_CMD_PEER_MEASUREMENT_COMPLETE)
{
return NL_OK;
}
if (gnlh->cmd == NL80211_CMD_PEER_MEASUREMENT_RESULT)
{
if (!tb[NL80211_ATTR_PEER_MEASUREMENTS])
{
return NL_OK;
}
struct nlattr *pmsr[NL80211_PMSR_ATTR_MAX + 1];
nla_parse_nested(pmsr, NL80211_PMSR_ATTR_MAX, tb[NL80211_ATTR_PEER_MEASUREMENTS], NULL);
if (!pmsr[NL80211_PMSR_ATTR_PEERS])
{
return NL_OK;
}
struct nlattr *peer = (nlattr *)nla_data(pmsr[NL80211_PMSR_ATTR_PEERS]);
struct nlattr *tbpeer[NL80211_PMSR_PEER_ATTR_MAX + 1];
struct nlattr *resp[NL80211_PMSR_RESP_ATTR_MAX + 1];
struct nlattr *data[NL80211_PMSR_TYPE_MAX + 1];
nla_parse_nested(tbpeer, NL80211_PMSR_PEER_ATTR_MAX, peer, NULL);
if (!tbpeer[NL80211_PMSR_PEER_ATTR_ADDR])
{
return NL_OK;
}
uint8_t mac[ETH_ALEN];
memcpy(mac, nla_data(tbpeer[NL80211_PMSR_PEER_ATTR_ADDR]), ETH_ALEN);
if (!tbpeer[NL80211_PMSR_PEER_ATTR_ADDR])
{
return NL_OK;
}
nla_parse_nested(resp, NL80211_PMSR_RESP_ATTR_MAX, tbpeer[NL80211_PMSR_PEER_ATTR_RESP], NULL);
if (!resp[NL80211_PMSR_RESP_ATTR_DATA])
{
return NL_OK;
}
nla_parse_nested(data, NL80211_PMSR_TYPE_MAX, resp[NL80211_PMSR_RESP_ATTR_DATA], NULL);
if (!data[NL80211_PMSR_TYPE_FTM])
{
return NL_OK;
}
struct nlattr *ftm[NL80211_PMSR_FTM_RESP_ATTR_MAX + 1];
nla_parse_nested(ftm, NL80211_PMSR_FTM_RESP_ATTR_MAX, data[NL80211_PMSR_TYPE_FTM], NULL);
WiFiFtmController *wfc = (WiFiFtmController *)arg;
if (ftm[NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON])
{
uint32_t reason = nla_get_u32(ftm[NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON]);
if (Arguments::arguments.verbose)
{
Logger::log(info) << "FTM failed to measure " << reason << "\n";
}
wfc->lastRttIsSuccess = false;
return NL_OK;
}
Ftm ftmData{
.burstIndex = (uint32_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_BURST_INDEX] ? nla_get_u32(ftm[NL80211_PMSR_FTM_RESP_ATTR_BURST_INDEX]) : 0),
.numFtmrAttempts = (uint32_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_ATTEMPTS] ? nla_get_u32(ftm[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_ATTEMPTS]) : 0),
.numFtmrSuccesses = (uint32_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_SUCCESSES] ? nla_get_u32(ftm[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_SUCCESSES]) : 0),
.numBurstsExp = (uint32_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_NUM_BURSTS_EXP] ? nla_get_u32(ftm[NL80211_PMSR_FTM_RESP_ATTR_NUM_BURSTS_EXP]) : 0),
.burstDuration = (uint8_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_BURST_DURATION] ? nla_get_u8(ftm[NL80211_PMSR_FTM_RESP_ATTR_BURST_DURATION]) : 0),
.ftmsPerBurst = (uint8_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_FTMS_PER_BURST] ? nla_get_u8(ftm[NL80211_PMSR_FTM_RESP_ATTR_FTMS_PER_BURST]) : 0),
.rssiAvg = (uint8_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_RSSI_AVG] ? nla_get_u8(ftm[NL80211_PMSR_FTM_RESP_ATTR_RSSI_AVG]) : 0),
.rssiSpread = (uint32_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_RSSI_SPREAD] ? nla_get_u32(ftm[NL80211_PMSR_FTM_RESP_ATTR_RSSI_SPREAD]) : 0),
.rttAvg = (uint64_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_RTT_AVG] ? nla_get_u64(ftm[NL80211_PMSR_FTM_RESP_ATTR_RTT_AVG]) : 0),
.rttVariance = (uint64_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_RTT_VARIANCE] ? nla_get_u64(ftm[NL80211_PMSR_FTM_RESP_ATTR_RTT_VARIANCE]) : 0),
.rttSpread = (uint64_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_RTT_SPREAD] ? nla_get_u64(ftm[NL80211_PMSR_FTM_RESP_ATTR_RTT_SPREAD]) : 0),
.distAvg = (uint64_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_DIST_AVG] ? nla_get_u64(ftm[NL80211_PMSR_FTM_RESP_ATTR_RTT_AVG]) : 0),
.distVariance = (uint64_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_DIST_VARIANCE] ? nla_get_u64(ftm[NL80211_PMSR_FTM_RESP_ATTR_DIST_VARIANCE]) : 0),
.distSpread = (uint64_t)(ftm[NL80211_PMSR_FTM_RESP_ATTR_DIST_SPREAD] ? nla_get_u64(ftm[NL80211_PMSR_FTM_RESP_ATTR_DIST_SPREAD]) : 0),
.timestamp = (uint64_t)std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count(),
};
if (Arguments::arguments.verbose)
{
Logger::log(info) << "FTM average RTT: " << ftmData.rttAvg << "ps\n";
}
if (MainController::getInstance()->udpSocket)
{
MainController::getInstance()->udpSocket->send(reinterpret_cast<char *>(&ftmData), FTM_SIZE);
} else {
std::ofstream outfile;
std::string filename = std::string("FTM_") + Arguments::arguments.outputFile;
outfile.open(filename, std::ios_base::app | std::ios::binary);
if (outfile.fail())
{
throw std::ios_base::failure("Open file failed: " + std::string(std::strerror(errno)));
}
outfile.write(reinterpret_cast<char *>(&ftmData), FTM_SIZE);
outfile.close();
std::filesystem::permissions(filename, std::filesystem::perms::all & ~(std::filesystem::perms::owner_exec | std::filesystem::perms::group_exec | std::filesystem::perms::others_exec), std::filesystem::perm_options::add);
}
wfc->lastRttIsSuccess = true;
return NL_OK;
}
return NL_OK;
}

View File

@ -17,7 +17,6 @@
*/
#include "gui/CsiProcessingWindow.h"
#include "GnuPlot.h"
#include "main.h"
#include "Logger.h"
#include "Arguments.h"
@ -65,10 +64,10 @@ void CsiProcessingWindow::refresh()
this->currentDataCount->set_text(indexLabelText);
if (!this->csiProcessor.csiData.empty())
/* if (!this->csiProcessor.csiData.empty())
{
gnuPlot.updateChart(*this->csiProcessor.csiData[this->currentIndex]);
}
gnuPlot.updateChartAsync(this->csiProcessor.csiData[this->currentIndex]);
} */
}

View File

@ -1,138 +0,0 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2023-2024 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gui/GnuPlot.h"
#include "Arguments.h"
#include "GnuPlot.h"
void GnuPlot::init()
{
if (!Arguments::arguments.plot || gnuPlotPipe)
{
return;
}
this->gnuPlotPipe = popen("gnuplot > NUL 2>&1", "w");
fprintf(this->gnuPlotPipe, "set term x11 noraise\n");
fflush(this->gnuPlotPipe);
}
void GnuPlot::setWindow(uint64_t id)
{
this->gnuPlotPipe = popen("gnuplot > NUL 2>&1", "w");
fprintf(this->gnuPlotPipe, "set term x11 noraise\n");
fprintf(this->gnuPlotPipe, "set terminal x11 window \"%#lx\"\n", id);
fflush(this->gnuPlotPipe);
}
void GnuPlot::setBlank()
{
const char *cmd = R"(
set multiplot layout 2,1
set ylabel "Magnitude"
set xlabel "Subcarrier Index"
set title "Magnitude (Abs) "
set autoscale x
set xrange [0:1]
set yrange [0:1]
plot 1/0
set ylabel "Phase (rad) "
set xlabel "Subcarrier Index"
set title "Phase "
set autoscale
set xrange [0:1]
set yrange [0:1]
plot 1/0
)";
this->lastCmd = cmd;
fprintf(this->gnuPlotPipe, cmd);
fflush(this->gnuPlotPipe);
}
void GnuPlot::updateChart(Csi &csi)
{
if (!Arguments::arguments.plot)
{
return;
}
std::stringstream ss;
// ss << "set multiplot layout 2,1";
ss << R"(
set multiplot layout 2,1
set ylabel "Magnitude"
set xlabel "Subcarrier Index"
set title "Magnitude (Abs) "
set autoscale x
)";
std::stringstream plotCmd;
plotCmd << "set xrange [1:" << csi.numSubCarriers << "]\n";
plotCmd << "plot";
for (uint32_t tx = 0; tx < csi.numTx; tx++)
{
for (uint32_t rx = 0; rx < csi.numRx; rx++)
{
plotCmd << " '-' with lines title \"RX" << (rx + 1) << "TX" << (tx + 1) << "\", ";
}
}
plotCmd << "\n";
ss << plotCmd.str();
std::stringstream dataMagnitude;
std::stringstream dataPhase;
uint32_t index = 0;
for (uint32_t rx = 0; rx < csi.numRx; rx++)
{
for (uint32_t tx = 0; tx < csi.numTx; tx++)
{
for (uint32_t n = 0; n < csi.numSubCarriers; n++)
{
dataMagnitude << (n + 1) << " " << csi.magnitude[index] << "\n";
dataPhase << (n + 1) << " " << csi.phase[index] << "\n";
index++;
}
dataMagnitude << "e\n";
dataPhase << "e\n";
}
}
ss << dataMagnitude.str();
ss << R"(
set ylabel "Phase (rad) "
set xlabel "Subcarrier Index"
set title "Phase "
set autoscale
)";
ss << plotCmd.str();
ss << dataPhase.str();
ss << "unset multiplot\n";
this->lastCmd = ss.str();
fprintf(this->gnuPlotPipe, ss.str().c_str());
fflush(this->gnuPlotPipe);
}
void GnuPlot::reload()
{
fprintf(this->gnuPlotPipe, this->lastCmd.c_str());
fflush(this->gnuPlotPipe);
}

View File

@ -29,6 +29,16 @@ MainWindow::MainWindow(BaseObjectType *obj, Glib::RefPtr<Gtk::Builder> const &bu
set_title("FeitCSI");
set_default_size(1000, 800);
this->signal_delete_event().connect(sigc::mem_fun(*this, &MainWindow::onWindowDelete));
if (!Arguments::arguments.gui && Arguments::arguments.plot)
{
Glib::RefPtr<Gtk::ScrolledWindow>::cast_dynamic(this->builder->get_object("consoleScrollbar"))->hide();
Glib::RefPtr<Gtk::Notebook>::cast_dynamic(this->builder->get_object("leftBar"))->hide();
return;
}
this->measureButton = Glib::RefPtr<Gtk::Button>::cast_dynamic(this->builder->get_object("measureButton"));
this->measureButton->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::measureButtonClicked));
@ -80,8 +90,6 @@ MainWindow::MainWindow(BaseObjectType *obj, Glib::RefPtr<Gtk::Builder> const &bu
this->errorMessages = Glib::RefPtr<Gtk::Label>::cast_dynamic(this->builder->get_object("errorMessages"));
this->chartSocket = Glib::RefPtr<Gtk::Socket>::cast_dynamic(this->builder->get_object("chartSocket"));
this->console = Glib::RefPtr<Gtk::TextView>::cast_dynamic(this->builder->get_object("console"));
Glib::RefPtr<Gtk::ScrolledWindow> consoleScrollbar = Glib::RefPtr<Gtk::ScrolledWindow>::cast_dynamic(this->builder->get_object("consoleScrollbar"));
this->consoleScrollbarAdjustment = consoleScrollbar->get_vadjustment();
@ -92,12 +100,9 @@ MainWindow::MainWindow(BaseObjectType *obj, Glib::RefPtr<Gtk::Builder> const &bu
Logger::guiOutput = this->console->get_buffer();
this->gnuPlot.setWindow(this->chartSocket->get_id());
this->signal_check_resize().connect_notify(sigc::mem_fun(*this, &MainWindow::onResize));
this->signal_show().connect(sigc::mem_fun(*this, &MainWindow::onWindowInit));
this->signal_delete_event().connect(sigc::mem_fun(*this, &MainWindow::onWindowDelete));
this->csiProcessingWindow.init(this->builder);
}
@ -118,16 +123,14 @@ bool MainWindow::onWindowDelete(GdkEventAny *event)
{
MainController *mainController = MainController::getInstance();
mainController->deleteInstance();
return 0;
return true;
}
void MainWindow::onWindowInit()
{
Glib::RefPtr<Gtk::Paned> socketConsolePaned = Glib::RefPtr<Gtk::Paned>::cast_dynamic(this->builder->get_object("socketConsolePaned"));
socketConsolePaned->set_position(socketConsolePaned->get_allocated_height() * 0.8);
MainController *mainController = MainController::getInstance();
mainController->initInterface();
this->gnuPlot.setBlank();
}
void MainWindow::onResize()
@ -137,21 +140,6 @@ void MainWindow::onResize()
this->consoleScrollbarAdjustment->set_value(this->consoleScrollbarAdjustment->get_upper());
return;
}
int pHeight = this->chartSocket->get_allocated_height();
int pWidth = this->chartSocket->get_allocated_width();
if (pHeight < 10 && pWidth < 10)
{
return;
}
if (pWidth != this->chartWidth || pHeight != this->chartHeight)
{
this->gnuPlot.reload();
this->chartWidth = pWidth;
this->chartHeight = pHeight;
}
}
void MainWindow::frequencyChange()

175
src/gui/Plot.cpp Normal file
View File

@ -0,0 +1,175 @@
/*
* FeitCSI is the tool for extracting CSI information from supported intel NICs.
* Copyright (C) 2024 Miroslav Hutar.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Plot.h"
#include "Arguments.h"
#include "Logger.h"
#include "WiFiCsiController.h"
#include <chrono>
#include <thread>
#include <iostream>
void Plot::init()
{
signal_draw().connect(sigc::mem_fun(*this, &Plot::on_draw));
}
void Plot::init(Glib::RefPtr<Gtk::Box> box)
{
this->yTicks = this->yTicksMin >= 0 ? this->yTicksMax : this->yTicksMax + (this->yTicksMin * -1);
signal_draw().connect(sigc::mem_fun(*this, &Plot::on_draw));
box->pack_start(*this);
show();
}
void Plot::updateData(Csi *csi, std::vector<double> *data)
{
this->csi = csi;
this->data = data;
queue_draw();
}
bool Plot::on_draw(const Cairo::RefPtr<Cairo::Context> &cr)
{
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
const double offset = 50;
const double xTicks = csi ? csi->numSubCarriers : 56;
bool redraw = false;
// Set background color
cr->set_source_rgb(1.0, 1.0, 1.0); // White
cr->paint();
// Draw x and y axis
cr->set_source_rgb(0.0, 0.0, 0.0); // Black
cr->set_line_width(1.0);
cr->move_to(offset, offset);
cr->line_to(offset, height - offset);
cr->line_to(width - offset, height - offset);
cr->stroke();
// Calculate the scaling factor for data points to fit in the drawing area
double xScale = (width - 2 * offset) / (xTicks - 1);
double yScale = (height - 2 * offset) / yTicks;
// Draw tick text
const int perXTick = xTicks / 8;
for (int i = 0; i < xTicks; i = i + perXTick)
{
double x = i * xScale + offset;
cr->set_source_rgb(0, 0, 0);
cr->move_to(x, height - 40);
cr->show_text(std::to_string((i + 1)));
// Draw grid lines
cr->set_source_rgb(0.8, 0.7, 0.7); // Light gray
cr->move_to(x, offset);
cr->line_to(x, height - offset);
cr->stroke();
}
const int perYTick = yTicks > 8 ? yTicks / 8 : 1;
for (int i = 0; i <= yTicks; i = i + perYTick)
{
double y = height - offset - i * (height - 100) / yTicks;
cr->set_source_rgb(0, 0, 0);
cr->move_to(30, y);
cr->show_text(std::to_string((int)(this->yTicksMin < 0 ? i + this->yTicksMin : i)));
// Draw grid lines
cr->set_source_rgb(0.8, 0.8, 0.8); // Light gray
cr->move_to(offset, y);
cr->line_to(width - offset, y);
cr->stroke();
}
if (csi)
{
// Draw the line chart
int colorNumber = 0;
uint32_t index = 0;
for (uint32_t rx = 0; rx < csi->numRx; rx++)
{
for (uint32_t tx = 0; tx < csi->numTx; tx++)
{
cr->set_source_rgb(colors[colorNumber][0], colors[colorNumber][1], colors[colorNumber][2]); // Black color
cr->set_line_width(2.0);
cr->move_to(offset, (height - offset) - (this->yTicksMin < 0 ? ((this->yTicksMin * -1) + (*this->data)[0]) : (*this->data)[0]) * yScale);
for (uint32_t n = 0; n < csi->numSubCarriers; n++)
{
double x = n * xScale + offset;
double y = (height - offset) - (this->yTicksMin < 0 ? ((this->yTicksMin * -1) + (*this->data)[index]) : (*this->data)[index]) * yScale;
if ((*this->data)[index] > this->yTicks)
{
this->yTicks = (*this->data)[index];
redraw = true;
}
cr->line_to(x, y);
index++;
}
cr->stroke();
colorNumber++;
}
}
if (redraw)
{
return this->on_draw(cr);
}
// Draw legend
colorNumber = 0;
cr->move_to(offset * 2, offset - 10);
for (uint32_t rx = 0; rx < csi->numRx; rx++)
{
for (uint32_t tx = 0; tx < csi->numTx; tx++)
{
cr->set_source_rgb(colors[colorNumber][0], colors[colorNumber][1], colors[colorNumber][2]); // Black color
cr->show_text(std::string("RX" + std::to_string(rx + 1)) + std::string("TX" + std::to_string(tx + 1)));
cr->show_text(" ");
colorNumber++;
}
}
}
// Draw x label
cr->set_source_rgb(0.0, 0.0, 0.0); // Black color
cr->move_to(width / 2, (offset / 2));
cr->show_text(this->title);
// Draw x label
cr->set_source_rgb(0.0, 0.0, 0.0); // Black color
cr->move_to(width / 2, height - (offset / 2));
cr->show_text(this->xLabel);
// Draw y label
cr->move_to((offset / 2), height / 2);
cr->rotate(-M_PI / 2); // Rotate text for y label
cr->show_text(this->yLabel);
// Draw x label
cr->set_source_rgb(0.0, 0.0, 0.0); // Black color
cr->set_font_size(30);
cr->move_to(width / 2, (offset / 2));
cr->show_text(this->title);
return true;
}