wlan_pioneer/src/wlanSignal.cpp

102 lines
3.2 KiB
C++
Raw Normal View History

2018-12-16 16:35:47 +00:00
2018-11-26 22:39:18 +00:00
#include <cstring>
#include <limits>
2018-12-16 16:35:47 +00:00
#include <string>
#include "ros/ros.h"
#include "wlan_pioneer/WlanSignalMsg.h"
2018-12-13 20:43:05 +00:00
#include "wifi_scan.h"
2018-11-22 18:48:11 +00:00
using namespace std;
2018-11-22 18:48:11 +00:00
using namespace ros;
using namespace wlan_pioneer;
2018-12-16 16:35:47 +00:00
const int MAX_APS = 64; // the maximum amounts of APs (Access Points) we want to store
2018-12-13 20:43:05 +00:00
struct wifi_scan* wifi = nullptr; // this stores all the library information
2018-12-16 16:35:47 +00:00
struct bss_info bss[MAX_APS]; // this is where we are going to keep informatoin about APs
WlanSignalMsg msg; // global message, will be set and published in every loop
2018-12-15 15:44:19 +00:00
2018-12-13 20:43:05 +00:00
void scanInterface( const string& interface, const string& monitoringSsid ) {
2018-12-16 16:35:47 +00:00
msg.ssid = monitoringSsid;
msg.timestamp = Time::now();
msg.level_2G4 = numeric_limits<int8_t>::min();
msg.level_5G = numeric_limits<int8_t>::min();
2018-12-15 15:44:19 +00:00
msg.cellCount_2G4 = 0;
2018-12-16 16:35:47 +00:00
msg.cellCount_5G = 0;
2018-12-13 20:43:05 +00:00
if ( wifi == nullptr ) {
2018-12-16 16:35:47 +00:00
// initialise wlan for scanning
2018-12-13 20:43:05 +00:00
wifi = wifi_scan_init( interface.c_str() );
2018-11-26 22:39:18 +00:00
}
2018-12-13 20:43:05 +00:00
2018-12-15 15:44:19 +00:00
int status = wifi_scan_all( wifi, bss, MAX_APS );
2018-12-13 20:43:05 +00:00
if ( status < 0 ) {
ROS_ERROR( "Unable to get WLAN scan data: %s", std::strerror( errno ) );
2018-12-16 16:35:47 +00:00
wifi_scan_close( wifi ); // close wlan to re-init in next loop
2018-12-13 20:43:05 +00:00
wifi = nullptr;
return;
}
else {
2018-12-16 16:35:47 +00:00
// run trought list of cells
2018-12-13 20:43:05 +00:00
for ( int i = 0; i < status && i < MAX_APS; ++i ) {
if ( bss[i].seen_ms_ago > 100 ) {
continue; // we do not want old results
}
if ( std::strcmp( bss[i].ssid, monitoringSsid.c_str() ) != 0 ) {
2018-12-16 16:35:47 +00:00
continue; // only cells on monitoring SSID should be scanned
2018-12-13 20:43:05 +00:00
}
2018-12-16 16:35:47 +00:00
int8_t level = bss[i].signal_mbm / 100; // mBm to dBm
2018-12-13 20:43:05 +00:00
if ( bss[i].frequency >= 2400 && bss[i].frequency <= 2499 ) {
2018-12-16 16:35:47 +00:00
// maximum of scanned levels of 2.4 GHz band
2018-12-15 15:44:19 +00:00
msg.level_2G4 = std::max( msg.level_2G4, level );
msg.cellCount_2G4++;
2018-12-13 20:43:05 +00:00
}
else if ( bss[i].frequency >= 5000 && bss[i].frequency <= 5999 ) {
2018-12-16 16:35:47 +00:00
// maximum of scanned levels of 5 GHz band
2018-12-15 15:44:19 +00:00
msg.level_5G = std::max( msg.level_5G, level );
msg.cellCount_5G++;
2018-12-13 20:43:05 +00:00
}
}
}
}
2018-12-16 16:35:47 +00:00
int main( int argc, char** argv ) {
init( argc, argv, "wlanSignal" );
2018-12-13 20:43:05 +00:00
NodeHandle node;
2018-12-13 20:43:05 +00:00
string wlanInterface;
string monitoringSsid;
2018-12-16 16:35:47 +00:00
Publisher publisher = node.advertise<WlanSignalMsg>( "wlan_signal", 1000 );
Rate loopRate = Rate( 1 ); // every second
2018-12-13 20:43:05 +00:00
if ( node.param<string>( "wlan_ssid", monitoringSsid, "" ) ) {
ROS_INFO( "Param wlan_ssid: %s", monitoringSsid.c_str() );
}
2018-11-26 22:39:18 +00:00
else {
2018-12-13 20:43:05 +00:00
ROS_ERROR( "Param 'wlan_ssid' not set. " );
}
2018-12-13 20:43:05 +00:00
if ( node.param<string>( "wlan_interface", wlanInterface, "wlan0" ) ) {
ROS_INFO( "Param wlan_interface: %s", wlanInterface.c_str() );
}
2018-11-26 22:39:18 +00:00
else {
2018-12-13 20:43:05 +00:00
ROS_INFO( "Param 'wlan_interface' not set. Using 'wlan0'" );
}
while ( ok() ) {
2018-12-15 15:44:19 +00:00
scanInterface( wlanInterface, monitoringSsid );
2018-12-16 16:35:47 +00:00
ROS_INFO( "Signal strength 2.4G: %i (%d Cells) Signal strength 5G: %i "
"(%d Cells)",
msg.level_2G4, msg.cellCount_2G4, msg.level_5G, msg.cellCount_5G );
publisher.publish( msg );
spinOnce();
loopRate.sleep();
}
2018-12-13 20:43:05 +00:00
wifi_scan_close( wifi );
return 0;
2018-11-22 18:48:11 +00:00
}