ui/networking: simplify dbus calls with a helper function (#23531)

* simplify d-bus calls

* move template function to source file
old-commit-hash: 43a2674ffc
commatwo_master
Dean Lee 3 years ago committed by GitHub
parent ccf91380f2
commit bbb691238a
  1. 224
      selfdrive/ui/qt/offroad/wifiManager.cc
  2. 9
      selfdrive/ui/qt/offroad/wifiManager.h

@ -1,26 +1,9 @@
#include "selfdrive/ui/qt/offroad/wifiManager.h" #include "selfdrive/ui/qt/offroad/wifiManager.h"
#include <algorithm>
#include <set>
#include <cstdlib>
#include "selfdrive/common/params.h" #include "selfdrive/common/params.h"
#include "selfdrive/common/swaglog.h" #include "selfdrive/common/swaglog.h"
#include "selfdrive/ui/qt/util.h" #include "selfdrive/ui/qt/util.h"
template <typename T>
T get_response(const QDBusMessage &response) {
QVariant first = response.arguments().at(0);
QDBusVariant dbvFirst = first.value<QDBusVariant>();
QVariant vFirst = dbvFirst.variant();
if (vFirst.canConvert<T>()) {
return vFirst.value<T>();
} else {
LOGE("Variant unpacking failure");
return T();
}
}
bool compare_by_strength(const Network &a, const Network &b) { bool compare_by_strength(const Network &a, const Network &b) {
if (a.connected == ConnectedType::CONNECTED) return true; if (a.connected == ConnectedType::CONNECTED) return true;
if (b.connected == ConnectedType::CONNECTED) return false; if (b.connected == ConnectedType::CONNECTED) return false;
@ -29,10 +12,28 @@ bool compare_by_strength(const Network &a, const Network &b) {
return a.strength > b.strength; return a.strength > b.strength;
} }
template <typename T = QDBusMessage, typename... Args>
T call(const QString &path, const QString &interface, const QString &method, Args &&...args) {
QDBusInterface nm = QDBusInterface(NM_DBUS_SERVICE, path, interface, QDBusConnection::systemBus());
nm.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = nm.call(method, args...);
if constexpr (std::is_same_v<T, QDBusMessage>) {
return response;
} else if (response.arguments().count() >= 1) {
QVariant vFirst = response.arguments().at(0).value<QDBusVariant>().variant();
if (vFirst.canConvert<T>()) {
return vFirst.value<T>();
}
QDebug critical = qCritical();
critical << "Variant unpacking failure :" << method << ',';
(critical << ... << args);
}
return T();
}
WifiManager::WifiManager(QObject *parent) : QObject(parent) { WifiManager::WifiManager(QObject *parent) : QObject(parent) {
qDBusRegisterMetaType<Connection>(); qDBusRegisterMetaType<Connection>();
qDBusRegisterMetaType<IpConfig>(); qDBusRegisterMetaType<IpConfig>();
connecting_to_network = "";
// Set tethering ssid as "weedle" + first 4 characters of a dongle id // Set tethering ssid as "weedle" + first 4 characters of a dongle id
tethering_ssid = "weedle"; tethering_ssid = "weedle";
@ -44,26 +45,23 @@ WifiManager::WifiManager(QObject *parent) : QObject(parent) {
if (!adapter.isEmpty()) { if (!adapter.isEmpty()) {
setup(); setup();
} else { } else {
bus.connect(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, "DeviceAdded", this, SLOT(deviceAdded(QDBusObjectPath))); QDBusConnection::systemBus().connect(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, "DeviceAdded", this, SLOT(deviceAdded(QDBusObjectPath)));
} }
timer.callOnTimeout(this, &WifiManager::requestScan); timer.callOnTimeout(this, &WifiManager::requestScan);
} }
void WifiManager::setup() { void WifiManager::setup() {
QDBusInterface nm(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_DEVICE, bus); auto bus = QDBusConnection::systemBus();
bus.connect(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_DEVICE, "StateChanged", this, SLOT(stateChange(unsigned int, unsigned int, unsigned int))); bus.connect(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_DEVICE, "StateChanged", this, SLOT(stateChange(unsigned int, unsigned int, unsigned int)));
bus.connect(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_PROPERTIES, "PropertiesChanged", this, SLOT(propertyChange(QString, QVariantMap, QStringList))); bus.connect(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_PROPERTIES, "PropertiesChanged", this, SLOT(propertyChange(QString, QVariantMap, QStringList)));
bus.connect(NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, "ConnectionRemoved", this, SLOT(connectionRemoved(QDBusObjectPath))); bus.connect(NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, "ConnectionRemoved", this, SLOT(connectionRemoved(QDBusObjectPath)));
bus.connect(NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, "NewConnection", this, SLOT(newConnection(QDBusObjectPath))); bus.connect(NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, "NewConnection", this, SLOT(newConnection(QDBusObjectPath)));
QDBusInterface device_props(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_PROPERTIES, bus); raw_adapter_state = call<uint>(adapter, NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_DEVICE, "State");
device_props.setTimeout(DBUS_TIMEOUT); activeAp = call<QDBusObjectPath>(adapter, NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_DEVICE_WIRELESS, "ActiveAccessPoint").path();
QDBusMessage response = device_props.call("Get", NM_DBUS_INTERFACE_DEVICE, "State");
raw_adapter_state = get_response<uint>(response);
initActiveAp();
initConnections(); initConnections();
requestScan(); requestScan();
} }
@ -83,10 +81,7 @@ void WifiManager::refreshNetworks() {
seenNetworks.clear(); seenNetworks.clear();
ipv4_address = get_ipv4_address(); ipv4_address = get_ipv4_address();
QDBusInterface nm(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_DEVICE_WIRELESS, bus); QDBusReply<QList<QDBusObjectPath>> response = call(adapter, NM_DBUS_INTERFACE_DEVICE_WIRELESS, "GetAllAccessPoints");
nm.setTimeout(DBUS_TIMEOUT);
const QDBusReply<QList<QDBusObjectPath>> &response = nm.call("GetAllAccessPoints");
for (const QDBusObjectPath &path : response.value()) { for (const QDBusObjectPath &path : response.value()) {
const QByteArray &ssid = get_property(path.path(), "Ssid"); const QByteArray &ssid = get_property(path.path(), "Ssid");
unsigned int strength = get_ap_strength(path.path()); unsigned int strength = get_ap_strength(path.path());
@ -116,28 +111,17 @@ QString WifiManager::get_ipv4_address() {
if (raw_adapter_state != NM_DEVICE_STATE_ACTIVATED) { if (raw_adapter_state != NM_DEVICE_STATE_ACTIVATED) {
return ""; return "";
} }
QVector<QDBusObjectPath> conns = get_active_connections(); for (const auto &p : getActiveConnections()) {
for (auto &p : conns) { QString type = call<QString>(p.path(), NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Type");
QDBusInterface nm(NM_DBUS_SERVICE, p.path(), NM_DBUS_INTERFACE_PROPERTIES, bus);
nm.setTimeout(DBUS_TIMEOUT);
QDBusObjectPath pth = get_response<QDBusObjectPath>(nm.call("Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Ip4Config"));
QString ip4config = pth.path();
QString type = get_response<QString>(nm.call("Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Type"));
if (type == "802-11-wireless") { if (type == "802-11-wireless") {
QDBusInterface nm2(NM_DBUS_SERVICE, ip4config, NM_DBUS_INTERFACE_PROPERTIES, bus); auto ip4config = call<QDBusObjectPath>(p.path(), NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Ip4Config");
nm2.setTimeout(DBUS_TIMEOUT); const auto &arr = call<QDBusArgument>(ip4config.path(), NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_IP4_CONFIG, "AddressData");
QVariantMap path;
const QDBusArgument &arr = get_response<QDBusArgument>(nm2.call("Get", NM_DBUS_INTERFACE_IP4_CONFIG, "AddressData"));
QMap<QString, QVariant> pth2;
arr.beginArray(); arr.beginArray();
while (!arr.atEnd()) { while (!arr.atEnd()) {
arr >> pth2; arr >> path;
QString ipv4 = pth2.value("address").value<QString>();
arr.endArray(); arr.endArray();
return ipv4; return path.value("address").value<QString>();
} }
arr.endArray(); arr.endArray();
} }
@ -187,43 +171,29 @@ void WifiManager::connect(const Network &n, const QString &password, const QStri
connection["ipv4"]["dns-priority"] = 600; connection["ipv4"]["dns-priority"] = 600;
connection["ipv6"]["method"] = "ignore"; connection["ipv6"]["method"] = "ignore";
QDBusInterface nm_settings(NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, bus); call(NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, "AddConnection", QVariant::fromValue(connection));
nm_settings.setTimeout(DBUS_TIMEOUT);
nm_settings.call("AddConnection", QVariant::fromValue(connection));
} }
void WifiManager::deactivateConnectionBySsid(const QString &ssid) { void WifiManager::deactivateConnectionBySsid(const QString &ssid) {
for (QDBusObjectPath active_connection_raw : get_active_connections()) { for (QDBusObjectPath active_connection : getActiveConnections()) {
QString active_connection = active_connection_raw.path(); auto pth = call<QDBusObjectPath>(active_connection.path(), NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "SpecificObject");
QDBusInterface nm(NM_DBUS_SERVICE, active_connection, NM_DBUS_INTERFACE_PROPERTIES, bus);
nm.setTimeout(DBUS_TIMEOUT);
QDBusObjectPath pth = get_response<QDBusObjectPath>(nm.call("Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "SpecificObject"));
if (pth.path() != "" && pth.path() != "/") { if (pth.path() != "" && pth.path() != "/") {
QString Ssid = get_property(pth.path(), "Ssid"); QString Ssid = get_property(pth.path(), "Ssid");
if (Ssid == ssid) { if (Ssid == ssid) {
deactivateConnection(active_connection_raw); deactivateConnection(active_connection);
} }
} }
} }
} }
void WifiManager::deactivateConnection(const QDBusObjectPath &path) { void WifiManager::deactivateConnection(const QDBusObjectPath &path) {
QDBusInterface nm2(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, bus); call(NM_DBUS_PATH, NM_DBUS_INTERFACE, "DeactivateConnection", QVariant::fromValue(path));
nm2.setTimeout(DBUS_TIMEOUT);
nm2.call("DeactivateConnection", QVariant::fromValue(path));
} }
QVector<QDBusObjectPath> WifiManager::get_active_connections() { QVector<QDBusObjectPath> WifiManager::getActiveConnections() {
QDBusInterface nm(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE_PROPERTIES, bus);
nm.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = nm.call("Get", NM_DBUS_INTERFACE, "ActiveConnections");
const QDBusArgument &arr = get_response<QDBusArgument>(response);
QVector<QDBusObjectPath> conns; QVector<QDBusObjectPath> conns;
QDBusObjectPath path; QDBusObjectPath path;
const QDBusArgument &arr = call<QDBusArgument>(NM_DBUS_PATH, NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE, "ActiveConnections");
arr.beginArray(); arr.beginArray();
while (!arr.atEnd()) { while (!arr.atEnd()) {
arr >> path; arr >> path;
@ -240,57 +210,28 @@ bool WifiManager::isKnownConnection(const QString &ssid) {
void WifiManager::forgetConnection(const QString &ssid) { void WifiManager::forgetConnection(const QString &ssid) {
const QDBusObjectPath &path = getConnectionPath(ssid); const QDBusObjectPath &path = getConnectionPath(ssid);
if (!path.path().isEmpty()) { if (!path.path().isEmpty()) {
QDBusInterface nm2(NM_DBUS_SERVICE, path.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, bus); call(path.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, "Delete");
nm2.call("Delete");
} }
} }
uint WifiManager::getAdapterType(const QDBusObjectPath &path) { uint WifiManager::getAdapterType(const QDBusObjectPath &path) {
QDBusInterface device_props(NM_DBUS_SERVICE, path.path(), NM_DBUS_INTERFACE_PROPERTIES, bus); return call<uint>(path.path(), NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_DEVICE, "DeviceType");
device_props.setTimeout(DBUS_TIMEOUT);
return get_response<uint>(device_props.call("Get", NM_DBUS_INTERFACE_DEVICE, "DeviceType"));
}
bool WifiManager::isWirelessAdapter(const QDBusObjectPath &path) {
return getAdapterType(path) == NM_DEVICE_TYPE_WIFI;
} }
void WifiManager::requestScan() { void WifiManager::requestScan() {
QDBusInterface nm(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_DEVICE_WIRELESS, bus); call(adapter, NM_DBUS_INTERFACE_DEVICE_WIRELESS, "RequestScan", QVariantMap());
nm.setTimeout(DBUS_TIMEOUT);
nm.call("RequestScan", QVariantMap());
}
uint WifiManager::get_wifi_device_state() {
QDBusInterface device_props(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_PROPERTIES, bus);
device_props.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = device_props.call("Get", NM_DBUS_INTERFACE_DEVICE, "State");
uint resp = get_response<uint>(response);
return resp;
} }
QByteArray WifiManager::get_property(const QString &network_path , const QString &property) { QByteArray WifiManager::get_property(const QString &network_path , const QString &property) {
QDBusInterface device_props(NM_DBUS_SERVICE, network_path, NM_DBUS_INTERFACE_PROPERTIES, bus); return call<QByteArray>(network_path, NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_ACCESS_POINT, property);
device_props.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = device_props.call("Get", NM_DBUS_INTERFACE_ACCESS_POINT, property);
return get_response<QByteArray>(response);
} }
unsigned int WifiManager::get_ap_strength(const QString &network_path) { unsigned int WifiManager::get_ap_strength(const QString &network_path) {
QDBusInterface device_props(NM_DBUS_SERVICE, network_path, NM_DBUS_INTERFACE_PROPERTIES, bus); return call<unsigned int>(network_path, NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_ACCESS_POINT, "Strength");
device_props.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = device_props.call("Get", NM_DBUS_INTERFACE_ACCESS_POINT, "Strength");
return get_response<unsigned int>(response);
} }
QString WifiManager::getAdapter(const uint adapter_type) { QString WifiManager::getAdapter(const uint adapter_type) {
QDBusInterface nm(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, bus); QDBusReply<QList<QDBusObjectPath>> response = call(NM_DBUS_PATH, NM_DBUS_INTERFACE, "GetDevices");
nm.setTimeout(DBUS_TIMEOUT);
const QDBusReply<QList<QDBusObjectPath>> &response = nm.call("GetDevices");
for (const QDBusObjectPath &path : response.value()) { for (const QDBusObjectPath &path : response.value()) {
if (getAdapterType(path) == adapter_type) { if (getAdapterType(path) == adapter_type) {
return path.path(); return path.path();
@ -315,13 +256,12 @@ void WifiManager::propertyChange(const QString &interface, const QVariantMap &pr
if (interface == NM_DBUS_INTERFACE_DEVICE_WIRELESS && props.contains("LastScan")) { if (interface == NM_DBUS_INTERFACE_DEVICE_WIRELESS && props.contains("LastScan")) {
refreshNetworks(); refreshNetworks();
} else if (interface == NM_DBUS_INTERFACE_DEVICE_WIRELESS && props.contains("ActiveAccessPoint")) { } else if (interface == NM_DBUS_INTERFACE_DEVICE_WIRELESS && props.contains("ActiveAccessPoint")) {
const QDBusObjectPath &path = props.value("ActiveAccessPoint").value<QDBusObjectPath>(); activeAp = props.value("ActiveAccessPoint").value<QDBusObjectPath>().path();
activeAp = path.path();
} }
} }
void WifiManager::deviceAdded(const QDBusObjectPath &path) { void WifiManager::deviceAdded(const QDBusObjectPath &path) {
if (isWirelessAdapter(path) && (adapter.isEmpty() || adapter == "/")) { if (getAdapterType(path) == NM_DEVICE_TYPE_WIFI && (adapter.isEmpty() || adapter == "/")) {
adapter = path.path(); adapter = path.path();
setup(); setup();
} }
@ -332,7 +272,7 @@ void WifiManager::connectionRemoved(const QDBusObjectPath &path) {
} }
void WifiManager::newConnection(const QDBusObjectPath &path) { void WifiManager::newConnection(const QDBusObjectPath &path) {
const Connection &settings = getConnectionSettings(path); Connection settings = getConnectionSettings(path);
if (settings.value("connection").value("type") == "802-11-wireless") { if (settings.value("connection").value("type") == "802-11-wireless") {
knownConnections[path] = settings.value("802-11-wireless").value("ssid").toString(); knownConnections[path] = settings.value("802-11-wireless").value("ssid").toString();
if (knownConnections[path] != tethering_ssid) { if (knownConnections[path] != tethering_ssid) {
@ -357,18 +297,13 @@ QDBusObjectPath WifiManager::getConnectionPath(const QString &ssid) {
} }
Connection WifiManager::getConnectionSettings(const QDBusObjectPath &path) { Connection WifiManager::getConnectionSettings(const QDBusObjectPath &path) {
QDBusInterface nm(NM_DBUS_SERVICE, path.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, bus); return QDBusReply<Connection>(call(path.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, "GetSettings")).value();
nm.setTimeout(DBUS_TIMEOUT);
return QDBusReply<Connection>(nm.call("GetSettings")).value();
} }
void WifiManager::initConnections() { void WifiManager::initConnections() {
QDBusInterface nm(NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, bus); const QDBusReply<QList<QDBusObjectPath>> response = call(NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, "ListConnections");
nm.setTimeout(DBUS_TIMEOUT);
const QDBusReply<QList<QDBusObjectPath>> response = nm.call("ListConnections");
for (const QDBusObjectPath &path : response.value()) { for (const QDBusObjectPath &path : response.value()) {
const Connection &settings = getConnectionSettings(path); const Connection settings = getConnectionSettings(path);
if (settings.value("connection").value("type") == "802-11-wireless") { if (settings.value("connection").value("type") == "802-11-wireless") {
knownConnections[path] = settings.value("802-11-wireless").value("ssid").toString(); knownConnections[path] = settings.value("802-11-wireless").value("ssid").toString();
} else if (path.path() != "/") { } else if (path.path() != "/") {
@ -381,40 +316,29 @@ void WifiManager::activateWifiConnection(const QString &ssid) {
const QDBusObjectPath &path = getConnectionPath(ssid); const QDBusObjectPath &path = getConnectionPath(ssid);
if (!path.path().isEmpty()) { if (!path.path().isEmpty()) {
connecting_to_network = ssid; connecting_to_network = ssid;
QDBusInterface nm3(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, bus); call(NM_DBUS_PATH, NM_DBUS_INTERFACE, "ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(adapter)), QVariant::fromValue(QDBusObjectPath("/")));
nm3.setTimeout(DBUS_TIMEOUT);
nm3.call("ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(adapter)), QVariant::fromValue(QDBusObjectPath("/")));
} }
} }
void WifiManager::activateModemConnection(const QDBusObjectPath &path) { void WifiManager::activateModemConnection(const QDBusObjectPath &path) {
QString modem = getAdapter(NM_DEVICE_TYPE_MODEM); QString modem = getAdapter(NM_DEVICE_TYPE_MODEM);
if (!path.path().isEmpty() && !modem.isEmpty()) { if (!path.path().isEmpty() && !modem.isEmpty()) {
QDBusInterface nm3(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, bus); call(NM_DBUS_PATH, NM_DBUS_INTERFACE, "ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(modem)), QVariant::fromValue(QDBusObjectPath("/")));
nm3.setTimeout(DBUS_TIMEOUT);
nm3.call("ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(modem)), QVariant::fromValue(QDBusObjectPath("/")));
} }
} }
// function matches tici/hardware.py // function matches tici/hardware.py
NetworkType WifiManager::currentNetworkType() { NetworkType WifiManager::currentNetworkType() {
QDBusInterface nm(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE_PROPERTIES, bus); auto primary_conn = call<QDBusObjectPath>(NM_DBUS_PATH, NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE, "PrimaryConnection");
nm.setTimeout(DBUS_TIMEOUT); auto primary_type = call<QString>(primary_conn.path(), NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Type");
const QDBusObjectPath &primary_conn = get_response<QDBusObjectPath>(nm.call("Get", NM_DBUS_INTERFACE, "PrimaryConnection"));
QDBusInterface nm2(NM_DBUS_SERVICE, primary_conn.path(), NM_DBUS_INTERFACE_PROPERTIES, bus);
nm.setTimeout(DBUS_TIMEOUT);
const QString &primary_type = get_response<QString>(nm2.call("Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Type"));
if (primary_type == "802-3-ethernet") { if (primary_type == "802-3-ethernet") {
return NetworkType::ETHERNET; return NetworkType::ETHERNET;
} else if (primary_type == "802-11-wireless" && !isTetheringEnabled()) { } else if (primary_type == "802-11-wireless" && !isTetheringEnabled()) {
return NetworkType::WIFI; return NetworkType::WIFI;
} else { } else {
for (const QDBusObjectPath &conn : get_active_connections()) { for (const QDBusObjectPath &conn : getActiveConnections()) {
QDBusInterface nm3(NM_DBUS_SERVICE, conn.path(), NM_DBUS_INTERFACE_PROPERTIES, bus); auto type = call<QString>(conn.path(), NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Type");
nm3.setTimeout(DBUS_TIMEOUT);
const QString &type = get_response<QString>(nm3.call("Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Type"));
if (type == "gsm") { if (type == "gsm") {
return NetworkType::CELL; return NetworkType::CELL;
} }
@ -425,13 +349,9 @@ NetworkType WifiManager::currentNetworkType() {
void WifiManager::updateGsmSettings(bool roaming, QString apn) { void WifiManager::updateGsmSettings(bool roaming, QString apn) {
if (!lteConnectionPath.path().isEmpty()) { if (!lteConnectionPath.path().isEmpty()) {
QDBusInterface nm(NM_DBUS_SERVICE, lteConnectionPath.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, bus);
nm.setTimeout(DBUS_TIMEOUT);
bool changes = false; bool changes = false;
bool auto_config = apn.isEmpty(); bool auto_config = apn.isEmpty();
Connection settings = QDBusReply<Connection>(nm.call("GetSettings")).value(); Connection settings = getConnectionSettings(lteConnectionPath);
if (settings.value("gsm").value("auto-config").toBool() != auto_config) { if (settings.value("gsm").value("auto-config").toBool() != auto_config) {
qWarning() << "Changing gsm.auto-config to" << auto_config; qWarning() << "Changing gsm.auto-config to" << auto_config;
settings["gsm"]["auto-config"] = auto_config; settings["gsm"]["auto-config"] = auto_config;
@ -451,7 +371,7 @@ void WifiManager::updateGsmSettings(bool roaming, QString apn) {
} }
if (changes) { if (changes) {
nm.call("UpdateUnsaved", QVariant::fromValue(settings)); // update is temporary call(lteConnectionPath.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, "UpdateUnsaved", QVariant::fromValue(settings)); // update is temporary
deactivateConnection(lteConnectionPath); deactivateConnection(lteConnectionPath);
activateModemConnection(lteConnectionPath); activateModemConnection(lteConnectionPath);
} }
@ -478,7 +398,7 @@ void WifiManager::addTetheringConnection() {
connection["802-11-wireless-security"]["psk"] = defaultTetheringPassword; connection["802-11-wireless-security"]["psk"] = defaultTetheringPassword;
connection["ipv4"]["method"] = "shared"; connection["ipv4"]["method"] = "shared";
QMap<QString,QVariant> address; QVariantMap address;
address["address"] = "192.168.43.1"; address["address"] = "192.168.43.1";
address["prefix"] = 24u; address["prefix"] = 24u;
connection["ipv4"]["address-data"] = QVariant::fromValue(IpConfig() << address); connection["ipv4"]["address-data"] = QVariant::fromValue(IpConfig() << address);
@ -486,9 +406,7 @@ void WifiManager::addTetheringConnection() {
connection["ipv4"]["route-metric"] = 1100; connection["ipv4"]["route-metric"] = 1100;
connection["ipv6"]["method"] = "ignore"; connection["ipv6"]["method"] = "ignore";
QDBusInterface nm_settings(NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, bus); call(NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, "AddConnection", QVariant::fromValue(connection));
nm_settings.setTimeout(DBUS_TIMEOUT);
nm_settings.call("AddConnection", QVariant::fromValue(connection));
} }
void WifiManager::setTetheringEnabled(bool enabled) { void WifiManager::setTetheringEnabled(bool enabled) {
@ -502,15 +420,6 @@ void WifiManager::setTetheringEnabled(bool enabled) {
} }
} }
void WifiManager::initActiveAp() {
QDBusInterface device_props(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_PROPERTIES, bus);
device_props.setTimeout(DBUS_TIMEOUT);
const QDBusMessage &response = device_props.call("Get", NM_DBUS_INTERFACE_DEVICE_WIRELESS, "ActiveAccessPoint");
activeAp = get_response<QDBusObjectPath>(response).path();
}
bool WifiManager::isTetheringEnabled() { bool WifiManager::isTetheringEnabled() {
if (activeAp != "" && activeAp != "/") { if (activeAp != "" && activeAp != "/") {
return get_property(activeAp, "Ssid") == tethering_ssid; return get_property(activeAp, "Ssid") == tethering_ssid;
@ -524,10 +433,7 @@ QString WifiManager::getTetheringPassword() {
} }
const QDBusObjectPath &path = getConnectionPath(tethering_ssid); const QDBusObjectPath &path = getConnectionPath(tethering_ssid);
if (!path.path().isEmpty()) { if (!path.path().isEmpty()) {
QDBusInterface nm(NM_DBUS_INTERFACE, path.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, bus); QDBusReply<QMap<QString, QVariantMap>> response = call(path.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, "GetSecrets", "802-11-wireless-security");
nm.setTimeout(DBUS_TIMEOUT);
const QDBusReply<QMap<QString, QMap<QString, QVariant>>> response = nm.call("GetSecrets", "802-11-wireless-security");
return response.value().value("802-11-wireless-security").value("psk").toString(); return response.value().value("802-11-wireless-security").value("psk").toString();
} }
return ""; return "";
@ -536,13 +442,9 @@ QString WifiManager::getTetheringPassword() {
void WifiManager::changeTetheringPassword(const QString &newPassword) { void WifiManager::changeTetheringPassword(const QString &newPassword) {
const QDBusObjectPath &path = getConnectionPath(tethering_ssid); const QDBusObjectPath &path = getConnectionPath(tethering_ssid);
if (!path.path().isEmpty()) { if (!path.path().isEmpty()) {
QDBusInterface nm(NM_DBUS_INTERFACE, path.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, bus); Connection settings = getConnectionSettings(path);
nm.setTimeout(DBUS_TIMEOUT);
Connection settings = QDBusReply<Connection>(nm.call("GetSettings")).value();
settings["802-11-wireless-security"]["psk"] = newPassword; settings["802-11-wireless-security"]["psk"] = newPassword;
nm.call("Update", QVariant::fromValue(settings)); call(path.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, "Update", QVariant::fromValue(settings));
if (isTetheringEnabled()) { if (isTetheringEnabled()) {
activateWifiConnection(tethering_ssid); activateWifiConnection(tethering_ssid);
} }

@ -22,8 +22,8 @@ enum class NetworkType {
ETHERNET ETHERNET
}; };
typedef QMap<QString, QMap<QString, QVariant>> Connection; typedef QMap<QString, QVariantMap> Connection;
typedef QVector<QMap<QString, QVariant>> IpConfig; typedef QVector<QVariantMap> IpConfig;
struct Network { struct Network {
QByteArray ssid; QByteArray ssid;
@ -66,7 +66,6 @@ public:
private: private:
QString adapter; // Path to network manager wifi-device QString adapter; // Path to network manager wifi-device
QTimer timer; QTimer timer;
QDBusConnection bus = QDBusConnection::systemBus();
unsigned int raw_adapter_state; // Connection status https://developer.gnome.org/NetworkManager/1.26/nm-dbus-types.html#NMDeviceState unsigned int raw_adapter_state; // Connection status https://developer.gnome.org/NetworkManager/1.26/nm-dbus-types.html#NMDeviceState
QString connecting_to_network; QString connecting_to_network;
QString tethering_ssid; QString tethering_ssid;
@ -78,11 +77,9 @@ private:
QString get_ipv4_address(); QString get_ipv4_address();
void connect(const QByteArray &ssid, const QString &username, const QString &password, SecurityType security_type); void connect(const QByteArray &ssid, const QString &username, const QString &password, SecurityType security_type);
QString activeAp; QString activeAp;
void initActiveAp();
void deactivateConnectionBySsid(const QString &ssid); void deactivateConnectionBySsid(const QString &ssid);
void deactivateConnection(const QDBusObjectPath &path); void deactivateConnection(const QDBusObjectPath &path);
QVector<QDBusObjectPath> get_active_connections(); QVector<QDBusObjectPath> getActiveConnections();
uint get_wifi_device_state();
QByteArray get_property(const QString &network_path, const QString &property); QByteArray get_property(const QString &network_path, const QString &property);
unsigned int get_ap_strength(const QString &network_path); unsigned int get_ap_strength(const QString &network_path);
SecurityType getSecurityType(const QString &path); SecurityType getSecurityType(const QString &path);

Loading…
Cancel
Save