Skip to content

Commit

Permalink
refactor rf metrics aggregation into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
Consti10 committed Oct 6, 2023
1 parent 63abdc4 commit 24bc4cc
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 74 deletions.
7 changes: 4 additions & 3 deletions src/WBTxRx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ void WBTxRx::on_new_packet(const uint8_t wlan_idx,const uint8_t *pkt,const int p
auto &this_wifi_card_stats = m_rx_stats_per_card.at(wlan_idx);
PerCardCalculators& this_wifi_card_calc= *m_per_card_calc.at(wlan_idx);
if(m_options.debug_rssi>=2){
m_console->debug("{}",radiotap::rx::all_rf_path_to_string(parsedPacket->allAntennaValues));
m_console->debug("{}",radiotap::rx::all_rf_path_to_string(parsedPacket->rf_paths));
}
this_wifi_card_calc.rf_aggregator.on_valid_openhd_packet(parsedPacket.value());
this_wifi_card_stats.count_p_valid++;
Expand Down Expand Up @@ -562,7 +562,8 @@ bool WBTxRx::process_received_data_packet(int wlan_idx,uint8_t stream_index,bool
m_packet_decrypt_time.reset();
}
}
on_valid_packet(nonce,wlan_idx,stream_index,decrypted->data(),decrypted->size());
on_valid_data_packet(nonce, wlan_idx, stream_index, decrypted->data(),
decrypted->size());
// Calculate sequence number stats per card
auto& seq_nr_for_card=m_per_card_calc.at(wlan_idx)->seq_nr;
seq_nr_for_card.on_new_sequence_number(nonce);
Expand All @@ -589,7 +590,7 @@ bool WBTxRx::process_received_data_packet(int wlan_idx,uint8_t stream_index,bool
return false;
}

void WBTxRx::on_valid_packet(uint64_t nonce,int wlan_index,const uint8_t stream_index,const uint8_t *data, const int data_len) {
void WBTxRx::on_valid_data_packet(uint64_t nonce,int wlan_index,const uint8_t stream_index,const uint8_t *data, const int data_len) {
if(m_output_cb!= nullptr){
m_output_cb(nonce,wlan_index,stream_index,data,data_len);
}
Expand Down
4 changes: 3 additions & 1 deletion src/WBTxRx.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ class WBTxRx {
// returns true if packet could be decrypted successfully
bool process_received_data_packet(int wlan_idx,uint8_t stream_index,bool encrypted,uint64_t nonce,const uint8_t *pkt_payload,int pkt_payload_size);
// called avery time we have successfully decrypted a packet
void on_valid_packet(uint64_t nonce,int wlan_index,uint8_t stream_index,const uint8_t *data,int data_len);
void on_valid_data_packet(uint64_t nonce,int wlan_index,
uint8_t stream_index,const uint8_t *data,
int data_len);
static std::string options_to_string(const std::vector<std::string>& wifi_cards,const Options& options);
// Adjustment of which card is used for injecting packets in case there are multiple RX card(s)
// (Of all cards currently receiving data, find the one with the highest reported dBm)
Expand Down
11 changes: 6 additions & 5 deletions src/radiotap/RadiotapHeaderRx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ struct ParsedAdapter{
};

struct ParsedRxRadiotapPacket {
// Size can be anything from size=1 to size== N where N is the number of Antennas of this adapter
const std::vector<ParsedRfPath> allAntennaValues;
const Ieee80211HeaderRaw *ieee80211Header;
const uint8_t *payload;
const std::size_t payloadSize;
ParsedAdapter adapter;
const ParsedAdapter adapter;
// Size can be anything from size=1 to size== N where N is the number of Antennas of this adapter
const std::vector<ParsedRfPath> rf_paths;
};

// Returns std::nullopt if radiotap was unable to parse the header
Expand All @@ -68,6 +68,7 @@ static std::optional<ParsedRxRadiotapPacket> process_received_radiotap_packet(co
// for rtl8812au fixup
//
ParsedAdapter parsed_adapter{};
std::vector<ParsedRfPath> parsed_rf_paths{};

int8_t currentAntenna = -1;
// not confirmed yet, but one radiotap packet might include stats for multiple antennas
Expand Down Expand Up @@ -163,7 +164,7 @@ static std::optional<ParsedRxRadiotapPacket> process_received_radiotap_packet(co
ss<<(int)antsignal<<",";
}
std::cout<<ss.str();*/
return ParsedRxRadiotapPacket{allAntennaValues, ieee80211Header, payload, payloadSize, parsed_adapter};
return ParsedRxRadiotapPacket{ieee80211Header, payload, payloadSize, parsed_adapter,parsed_rf_paths};
}

static std::string rf_path_to_string(const ParsedRfPath& rf_path){
Expand All @@ -188,7 +189,7 @@ static std::string all_rf_path_to_string(const std::vector<ParsedRfPath>& all_rf

static std::string parsed_radiotap_to_string(const ParsedRxRadiotapPacket& parsed){
std::stringstream ss;
ss<<all_rf_path_to_string(parsed.allAntennaValues)<<"\n";
ss<<all_rf_path_to_string(parsed.rf_paths)<<"\n";
if(parsed.adapter.radiotap_dbm_antsignal.has_value()){
ss<<"Antsignal:"<<(int)parsed.adapter.radiotap_dbm_antsignal.value()<<" ";
}
Expand Down
70 changes: 70 additions & 0 deletions src/radiotap/RadiotapRxRfAggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,73 @@
//

#include "RadiotapRxRfAggregator.h"

void RadiotapRxRfAggregator::on_valid_openhd_packet(
const radiotap::rx::ParsedRxRadiotapPacket& packet) {
if(packet.adapter.radiotap_dbm_antsignal.has_value()){
const auto radiotap_dbm_antsignal=packet.adapter.radiotap_dbm_antsignal.value();
auto opt_minmaxavg= adapter_rssi.add_and_recalculate_if_needed(radiotap_dbm_antsignal);
if(opt_minmaxavg.has_value()){
m_current_rx_stats.adapter.rssi_dbm=opt_minmaxavg->avg;
}
}
if(packet.adapter.radiotap_dbm_antnoise.has_value()){
const auto radiotap_dbm_antnoise=packet.adapter.radiotap_dbm_antnoise.value();
auto opt_minmaxavg= adapter_noise.add_and_recalculate_if_needed(radiotap_dbm_antnoise);
if(opt_minmaxavg.has_value()){
m_current_rx_stats.adapter.noise_dbm=opt_minmaxavg->avg;
}
}
if(packet.adapter.radiotap_lock_quality.has_value()){
const auto radiotap_lock_quality=packet.adapter.radiotap_lock_quality.value();
adapter_signal_quality.add_signal_quality(radiotap_lock_quality);
m_current_rx_stats.adapter.card_signal_quality_perc=adapter_signal_quality.get_current_signal_quality();
}
for(int i=0;i<packet.rf_paths.size();i++){
const auto& path=packet.rf_paths[i];
on_per_rf_path(i,path);
}
//if(m_wifi_cards[wlan_idx].type==wifibroadcast::WIFI_CARD_TYPE_RTL8812AU){
// RTL8812AU BUG - general value cannot be used, use max of antennas instead
// this_wifi_card_stats.card_dbm=std::max(this_wifi_card_stats.antenna1_dbm,this_wifi_card_stats.antenna2_dbm);
//}
}

void RadiotapRxRfAggregator::on_per_rf_path(
int index, const radiotap::rx::ParsedRfPath& data) {
// For simplicity, we only track up to 2 antennas
if(index>1) return ;
auto& per_path=index==0 ? m_antenna1 : m_antenna2;
auto& current=index==0 ? m_current_rx_stats.antenna1 : m_current_rx_stats.antenna2;
{
auto opt_minmaxavg= per_path.rssi_dbm.add_and_recalculate_if_needed(data.radiotap_dbm_antsignal);
if(opt_minmaxavg.has_value()){
current.rssi_dbm=opt_minmaxavg->avg;
}
}
{
auto opt_minmaxavg= per_path.noise_dbm.add_and_recalculate_if_needed(data.radiotap_dbm_antnoise);
if(opt_minmaxavg.has_value()){
current.rssi_dbm=opt_minmaxavg->avg;
}
}
{
per_path.signal_quality.add_signal_quality(data.radiotap_dbm_antsignal);
current.card_signal_quality_perc=per_path.signal_quality.get_current_signal_quality();
}
}

void RadiotapRxRfAggregator::set_debug_invalid_values(bool enable) {
adapter_rssi.set_debug_invalid_rssi(enable,0);
adapter_noise.set_debug_invalid_rssi(enable,0);
m_antenna1.rssi_dbm.set_debug_invalid_rssi(enable,1);
m_antenna2.rssi_dbm.set_debug_invalid_rssi(enable,2);
}

void RadiotapRxRfAggregator::reset() {
m_current_rx_stats={};
adapter_rssi.reset();
adapter_noise.reset();
m_antenna1.rssi_dbm.reset();
m_antenna2.rssi_dbm.reset();
}
74 changes: 9 additions & 65 deletions src/radiotap/RadiotapRxRfAggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,12 @@
#ifndef WIFIBROADCAST_RADIOTAPRXRFAGGREGATOR_H
#define WIFIBROADCAST_RADIOTAPRXRFAGGREGATOR_H

#include "RSSIAccumulator.hpp"
#include "RadiotapHeaderRx.hpp"
#include "RSSIAccumulator.hpp"
#include "SignalQualityAccumulator.hpp"

class RadiotapRxRfAggregator {
public:
void on_valid_openhd_packet(const radiotap::rx::ParsedRxRadiotapPacket& packet){
if(packet.adapter.radiotap_dbm_antsignal.has_value()){
const auto radiotap_dbm_antsignal=packet.adapter.radiotap_dbm_antsignal.value();
auto opt_minmaxavg= adapter_rssi.add_and_recalculate_if_needed(radiotap_dbm_antsignal);
if(opt_minmaxavg.has_value()){
m_current_rx_stats.adapter.rssi_dbm=opt_minmaxavg->avg;
}
}
if(packet.adapter.radiotap_dbm_antnoise.has_value()){
const auto radiotap_dbm_antnoise=packet.adapter.radiotap_dbm_antnoise.value();
auto opt_minmaxavg= adapter_noise.add_and_recalculate_if_needed(radiotap_dbm_antnoise);
if(opt_minmaxavg.has_value()){
m_current_rx_stats.adapter.noise_dbm=opt_minmaxavg->avg;
}
}
if(packet.adapter.radiotap_lock_quality.has_value()){
const auto radiotap_lock_quality=packet.adapter.radiotap_lock_quality.value();
adapter_signal_quality.add_signal_quality(radiotap_lock_quality);
m_current_rx_stats.adapter.card_signal_quality_perc=adapter_signal_quality.get_current_signal_quality();
}
for(int i=0;i<packet.allAntennaValues.size();i++){
const auto& path=packet.allAntennaValues[i];
on_per_rf_path(i,path);
}
//if(m_wifi_cards[wlan_idx].type==wifibroadcast::WIFI_CARD_TYPE_RTL8812AU){
// RTL8812AU BUG - general value cannot be used, use max of antennas instead
// this_wifi_card_stats.card_dbm=std::max(this_wifi_card_stats.antenna1_dbm,this_wifi_card_stats.antenna2_dbm);
//}
}
struct KeyRfIndicators {
// -128 = invalid, [-127..-1] otherwise
int8_t rssi_dbm=-128;
Expand All @@ -54,45 +25,18 @@ class RadiotapRxRfAggregator {
KeyRfIndicators antenna1;
KeyRfIndicators antenna2;
};
void set_debug_invalid_values(bool enable){
adapter_rssi.set_debug_invalid_rssi(enable,0);
adapter_noise.set_debug_invalid_rssi(enable,0);
m_antenna1.rssi_dbm.set_debug_invalid_rssi(enable,1);
m_antenna2.rssi_dbm.set_debug_invalid_rssi(enable,2);
}
void reset(){
m_current_rx_stats={};
adapter_rssi.reset();
adapter_noise.reset();
m_antenna1.rssi_dbm.reset();
m_antenna2.rssi_dbm.reset();
}
// Called every time a valid openhd packet is received
void on_valid_openhd_packet(const radiotap::rx::ParsedRxRadiotapPacket& packet);
// debugging of the 'invalid values reported by driver' issue
void set_debug_invalid_values(bool enable);
// Reset all rf metrics
void reset();
// TODO: Thread safety ?
CardKeyRfIndicators get_current(){
return m_current_rx_stats;
}
private:
void on_per_rf_path(int index,const radiotap::rx::ParsedRfPath& data){
// For simplicity, we only track up to 2 antennas
if(index>1) return ;
auto& per_path=index==0 ? m_antenna1 : m_antenna2;
auto& current=index==0 ? m_current_rx_stats.antenna1 : m_current_rx_stats.antenna2;
{
auto opt_minmaxavg= per_path.rssi_dbm.add_and_recalculate_if_needed(data.radiotap_dbm_antsignal);
if(opt_minmaxavg.has_value()){
current.rssi_dbm=opt_minmaxavg->avg;
}
}
{
auto opt_minmaxavg= per_path.noise_dbm.add_and_recalculate_if_needed(data.radiotap_dbm_antnoise);
if(opt_minmaxavg.has_value()){
current.rssi_dbm=opt_minmaxavg->avg;
}
}
{
per_path.signal_quality.add_signal_quality(data.radiotap_dbm_antsignal);
current.card_signal_quality_perc=per_path.signal_quality.get_current_signal_quality();
}
}
void on_per_rf_path(int index,const radiotap::rx::ParsedRfPath& data);
// Stats per-adapter
RSSIAccumulator adapter_rssi{};
RSSIAccumulator adapter_noise{};
Expand Down

0 comments on commit 24bc4cc

Please sign in to comment.