#include #include #include #include #include #include #include using namespace std; const char *WPAS_DBUS_SERVICE = "fi.w1.wpa_supplicant1"; bool startsWith( const std::string &str, const std::string &prefix ) { return str.substr(0,prefix.size())==prefix; } // Throws exceptions on errors. void checkResult( const QDBusMessage &result, const std::string &func, int expectedNumArgs ) { if (result.type() != QDBusMessage::ReplyMessage) { if ( startsWith( result.errorMessage().toStdString(), "The name fi.w1.wpa_supplicant1 was not provided by any .service files" ) ) { throw "Error: "+result.errorMessage().toStdString(); } throw "Error: wpa_supplicant DBus error in "+func+": " + result.errorMessage().toStdString(); } if ( result.arguments().size() != 1 ) { stringstream ss; ss << "Expected just one wifi interface, found "< "/fi/w1/wpa_supplicant1/Interfaces/3") std::string getNetDevObjectPath( const std::string &netDevInterfaceName, const QDBusConnection &systemBus ) { cout<<__func__ << "("<(); return opath.path().toStdString(); } // Returns a list of object-paths std::vector listNetDevInterfaces( QDBusConnection &systemBus ) { cout<<__func__ << "()" << endl; QDBusInterface interface( WPAS_DBUS_SERVICE, "/fi/w1/wpa_supplicant1", "org.freedesktop.DBus.Properties", systemBus ); QDBusMessage result = interface.call( "Get", "fi.w1.wpa_supplicant1", "Interfaces" ); checkResult( result, __func__, 1 ); const QVariant &var = result.arguments()[0].value().variant(); const QDBusArgument &dbArg = var.value(); std::vector ret; dbArg.beginArray(); while ( !dbArg.atEnd() ) { QString item; dbArg >> item; ret.push_back( item.toStdString() ); } dbArg.endArray(); return ret; } void removeNetDevInterface( const std::string &objPath, QDBusConnection &systemBus ) { QDBusInterface interface( WPAS_DBUS_SERVICE, "/fi/w1/wpa_supplicant1", "fi.w1.wpa_supplicant1", systemBus ); QDBusObjectPath path( objPath.c_str() ); QDBusMessage result = interface.call( "RemoveInterface", QVariant::fromValue(path) ); checkResult( result, __func__, 0 ); } void clearNetDevInterfaces(QDBusConnection &systemBus) { cout<<__func__ << "()" << endl; std::vector objPaths = listNetDevInterfaces(systemBus); for ( auto &objPath : objPaths ) { removeNetDevInterface( objPath, systemBus ); } } // Returns the objectPath of the newly-created interface std::string createNetDevInterface( const std::string &netDev, QDBusConnection &systemBus ) { cout<<__func__ << "()" << endl; QDBusInterface interface( WPAS_DBUS_SERVICE, "/fi/w1/wpa_supplicant1", "fi.w1.wpa_supplicant1", systemBus ); QMap args; args["Ifname"] = netDev.c_str(); args["Driver"] = "nl80211"; QDBusMessage result = interface.call( "CreateInterface", args ); //qDebug() << __func__ << ": result: " << result; checkResult( result, __func__, 1 ); QDBusObjectPath opath = result.arguments()[0].value(); return opath.path().toStdString(); } int signalPoll( QDBusInterface &netDevInterfaceInterface ) { QDBusMessage result = netDevInterfaceInterface.call( "SignalPoll" ); //qDebug() << __func__ << ": 'SignalPoll' result: "<().variant(); const QDBusArgument &dbArg = var.value(); // Iterate over it int rssi; int numEntries = 0; dbArg.beginMap(); while ( !dbArg.atEnd() ) { QString key; QVariant value; dbArg.beginMapEntry(); dbArg >> key; dbArg >> value; dbArg.endMapEntry(); numEntries++; if ( key == "rssi" ) { rssi = value.toInt(); return rssi; } } throw "rssi not found"; } const char *USAGE_ARGS = "[-i interface]"; int main( int argc, char **argv ) { std::string interface; for ( int i=1; i < argc; i++ ) { if ( i netDevInterfaceInterface_; // the DBus interface corresponding to the network interface. std::auto_ptr netDevInterfaceProperties_; // the DBus interface corresponding to the 'Properties' of the network interface. netDevInterfaceInterface_.reset( new QDBusInterface( WPAS_DBUS_SERVICE, netDevInterfaceObjectPath_.c_str(), "fi.w1.wpa_supplicant1.Interface", systemBus_ ) ); netDevInterfaceProperties_.reset( new QDBusInterface( WPAS_DBUS_SERVICE, netDevInterfaceObjectPath_.c_str(), "org.freedesktop.DBus.Properties", systemBus_ ) ); QTime timer; timer.start(); int i = 0; while ( true ) { try { int a = timer.elapsed(); int rssi = signalPoll( *netDevInterfaceInterface_ ); int b = timer.elapsed(); cout << i++ << " " << b << " " << (b-a) << " signalInfo rssi=" << rssi << endl; } catch ( std::exception &e ) { cout << e.what() << endl; } catch ( char const *e ) { cout << e << endl; } catch ( const std::string &e ) { cout << e << endl; } //usleep(0.001 * 1e6); } }