Donggyu1998

Untitled

Jun 17th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include <QtCore/QCoreApplication>
  2. #include <QtCore/QDebug>
  3. #include <QtCore/QStringList>
  4. #include <QtDBus/QtDBus>
  5. #include <QDebug>
  6. #include <QThread>
  7. int main(int argc, char **argv)
  8. {
  9. QCoreApplication app(argc, argv);
  10. // get the interface to nm
  11. QDBusInterface nm("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager",
  12. "org.freedesktop.NetworkManager", QDBusConnection::systemBus());
  13. if(!nm.isValid())
  14. {
  15. qFatal("Failed to connect to the system bus");
  16. }
  17. // get all devices
  18. QDBusMessage msg = nm.call("GetDevices");
  19. qDebug() << "GetDevices reply: " << msg << endl;
  20. QDBusArgument arg = msg.arguments().at(0).value<QDBusArgument>();
  21. if(arg.currentType() != QDBusArgument::ArrayType)
  22. {
  23. qFatal("Something went wrong with getting the device list");
  24. }
  25. QList<QDBusObjectPath> pathsLst = qdbus_cast<QList<QDBusObjectPath> >(arg);
  26. foreach(QDBusObjectPath p, pathsLst)
  27. {
  28. qDebug() << "DEV PATH: " << p.path();
  29. // creating an interface used to gather this devices properties
  30. QDBusInterface device("org.freedesktop.NetworkManager", p.path(),
  31. "org.freedesktop.NetworkManager.Device", QDBusConnection::systemBus());
  32. // 2 is WiFi dev, see https://people.freedesktop.org/~lkundrak/nm-docs/nm-dbus-types.html#NMDeviceType
  33. if (device.property("DeviceType").toInt() != 2)
  34. {
  35. continue;
  36. }
  37. // we got a wifi device, let's get an according dbus interface
  38. QDBusInterface wifi_device("org.freedesktop.NetworkManager", p.path(),
  39. "org.freedesktop.NetworkManager.Device.Wireless", QDBusConnection::systemBus());
  40. // we need to call scan on the inteface prior to request the list of interfaces
  41. QMap<QString, QVariant> argList;
  42. QDBusMessage msg = wifi_device.call("RequestScan", argList);
  43. QThread::sleep(2); // not the best solution, but here we just wait for the scan
  44. // doing the actual call
  45. msg = wifi_device.call("GetAllAccessPoints");
  46. qDebug()<< "Answer for GetAllAccessPoints: " << msg << endl << endl;
  47. // dig out the paths of the Access Point objects:
  48. QDBusArgument ap_list_arg = msg.arguments().at(0).value<QDBusArgument>();
  49. QList<QDBusObjectPath> ap_path_list = qdbus_cast<QList<QDBusObjectPath> >(ap_list_arg);
  50. // and iterate through the list
  51. foreach(QDBusObjectPath p ,ap_path_list)
  52. {
  53. // for each Access Point we create an interface
  54. QDBusInterface ap_interface("org.freedesktop.NetworkManager", p.path(),
  55. "org.freedesktop.NetworkManager.AccessPoint", QDBusConnection::systemBus());
  56. // and getting the name of the SSID
  57. qDebug() << "SSID: " << ap_interface.property("Ssid").toString();
  58. }
  59. }
  60. return 0;
  61. }
Add Comment
Please, Sign In to add comment