Networking: Use common header (#21456)

old-commit-hash: 4cdd26596e
commatwo_master
sshane 4 years ago committed by GitHub
parent d28d90818d
commit c110154869
  1. 37
      selfdrive/ui/qt/offroad/networkmanager.h
  2. 160
      selfdrive/ui/qt/offroad/wifiManager.cc
  3. 2
      selfdrive/ui/qt/offroad/wifiManager.h

@ -0,0 +1,37 @@
/**
* We are using a NetworkManager DBUS API : https://developer.gnome.org/NetworkManager/1.26/spec.html
* */
// https://developer.gnome.org/NetworkManager/1.26/nm-dbus-types.html#NM80211ApFlags
const int NM_802_11_AP_FLAGS_NONE = 0x00000000;
const int NM_802_11_AP_FLAGS_PRIVACY = 0x00000001;
const int NM_802_11_AP_FLAGS_WPS = 0x00000002;
// https://developer.gnome.org/NetworkManager/1.26/nm-dbus-types.html#NM80211ApSecurityFlags
const int NM_802_11_AP_SEC_PAIR_WEP40 = 0x00000001;
const int NM_802_11_AP_SEC_PAIR_WEP104 = 0x00000002;
const int NM_802_11_AP_SEC_GROUP_WEP40 = 0x00000010;
const int NM_802_11_AP_SEC_GROUP_WEP104 = 0x00000020;
const int NM_802_11_AP_SEC_KEY_MGMT_PSK = 0x00000100;
const int NM_802_11_AP_SEC_KEY_MGMT_802_1X = 0x00000200;
const QString NM_DBUS_PATH = "/org/freedesktop/NetworkManager";
const QString NM_DBUS_PATH_SETTINGS = "/org/freedesktop/NetworkManager/Settings";
const QString NM_DBUS_INTERFACE = "org.freedesktop.NetworkManager";
const QString NM_DBUS_INTERFACE_PROPERTIES = "org.freedesktop.DBus.Properties";
const QString NM_DBUS_INTERFACE_SETTINGS = "org.freedesktop.NetworkManager.Settings";
const QString NM_DBUS_INTERFACE_SETTINGS_CONNECTION = "org.freedesktop.NetworkManager.Settings.Connection";
const QString NM_DBUS_INTERFACE_DEVICE = "org.freedesktop.NetworkManager.Device";
const QString NM_DBUS_INTERFACE_DEVICE_WIRELESS = "org.freedesktop.NetworkManager.Device.Wireless";
const QString NM_DBUS_INTERFACE_ACCESS_POINT = "org.freedesktop.NetworkManager.AccessPoint";
const QString NM_DBUS_INTERFACE_ACTIVE_CONNECTION = "org.freedesktop.NetworkManager.Connection.Active";
const QString NM_DBUS_INTERFACE_IP4_CONFIG = "org.freedesktop.NetworkManager.IP4Config";
const QString NM_DBUS_SERVICE = "org.freedesktop.NetworkManager";
const int NM_DEVICE_STATE_ACTIVATED = 100;
const int NM_DEVICE_STATE_NEED_AUTH = 60;
const int NM_DEVICE_TYPE_WIFI = 2;
const int NM_DEVICE_STATE_REASON_SUPPLICANT_DISCONNECT = 8;
const int DBUS_TIMEOUT = 100;

@ -7,44 +7,6 @@
#include "selfdrive/common/params.h"
#include "selfdrive/common/swaglog.h"
/**
* We are using a NetworkManager DBUS API : https://developer.gnome.org/NetworkManager/1.26/spec.html
* */
// https://developer.gnome.org/NetworkManager/1.26/nm-dbus-types.html#NM80211ApFlags
const int NM_802_11_AP_FLAGS_NONE = 0x00000000;
const int NM_802_11_AP_FLAGS_PRIVACY = 0x00000001;
const int NM_802_11_AP_FLAGS_WPS = 0x00000002;
// https://developer.gnome.org/NetworkManager/1.26/nm-dbus-types.html#NM80211ApSecurityFlags
const int NM_802_11_AP_SEC_PAIR_WEP40 = 0x00000001;
const int NM_802_11_AP_SEC_PAIR_WEP104 = 0x00000002;
const int NM_802_11_AP_SEC_GROUP_WEP40 = 0x00000010;
const int NM_802_11_AP_SEC_GROUP_WEP104 = 0x00000020;
const int NM_802_11_AP_SEC_KEY_MGMT_PSK = 0x00000100;
const int NM_802_11_AP_SEC_KEY_MGMT_802_1X = 0x00000200;
const QString nm_path = "/org/freedesktop/NetworkManager";
const QString nm_settings_path = "/org/freedesktop/NetworkManager/Settings";
const QString nm_iface = "org.freedesktop.NetworkManager";
const QString props_iface = "org.freedesktop.DBus.Properties";
const QString nm_settings_iface = "org.freedesktop.NetworkManager.Settings";
const QString nm_settings_conn_iface = "org.freedesktop.NetworkManager.Settings.Connection";
const QString device_iface = "org.freedesktop.NetworkManager.Device";
const QString wireless_device_iface = "org.freedesktop.NetworkManager.Device.Wireless";
const QString ap_iface = "org.freedesktop.NetworkManager.AccessPoint";
const QString connection_iface = "org.freedesktop.NetworkManager.Connection.Active";
const QString ipv4config_iface = "org.freedesktop.NetworkManager.IP4Config";
const QString nm_service = "org.freedesktop.NetworkManager";
const int state_connected = 100;
const int state_need_auth = 60;
const int device_type_wifi = 2;
const int reason_wrong_password = 8;
const int dbus_timeout = 100;
template <typename T>
T get_response(QDBusMessage response) {
QVariant first = response.arguments().at(0);
@ -82,7 +44,7 @@ WifiManager::WifiManager(QWidget* parent) : QWidget(parent) {
if (!adapter.isEmpty()) {
setup();
} else {
bus.connect(nm_service, nm_path, nm_iface, "DeviceAdded", this, SLOT(deviceAdded(QDBusObjectPath)));
bus.connect(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, "DeviceAdded", this, SLOT(deviceAdded(QDBusObjectPath)));
}
QTimer* timer = new QTimer(this);
@ -95,16 +57,16 @@ WifiManager::WifiManager(QWidget* parent) : QWidget(parent) {
}
void WifiManager::setup() {
QDBusInterface nm(nm_service, adapter, device_iface, bus);
bus.connect(nm_service, adapter, device_iface, "StateChanged", this, SLOT(stateChange(unsigned int, unsigned int, unsigned int)));
bus.connect(nm_service, adapter, props_iface, "PropertiesChanged", this, SLOT(propertyChange(QString, QVariantMap, QStringList)));
QDBusInterface nm(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_DEVICE, bus);
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_service, nm_settings_path, nm_settings_iface, "ConnectionRemoved", this, SLOT(connectionRemoved(QDBusObjectPath)));
bus.connect(nm_service, nm_settings_path, nm_settings_iface, "NewConnection", this, SLOT(newConnection(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)));
QDBusInterface device_props(nm_service, adapter, props_iface, bus);
device_props.setTimeout(dbus_timeout);
QDBusMessage response = device_props.call("Get", device_iface, "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");
raw_adapter_state = get_response<uint>(response);
requestScan();
}
@ -123,25 +85,25 @@ void WifiManager::refreshNetworks() {
}
QString WifiManager::get_ipv4_address() {
if (raw_adapter_state != state_connected) {
if (raw_adapter_state != NM_DEVICE_STATE_ACTIVATED) {
return "";
}
QVector<QDBusObjectPath> conns = get_active_connections();
for (auto &p : conns) {
QString active_connection = p.path();
QDBusInterface nm(nm_service, active_connection, props_iface, bus);
nm.setTimeout(dbus_timeout);
QDBusInterface nm(NM_DBUS_SERVICE, active_connection, NM_DBUS_INTERFACE_PROPERTIES, bus);
nm.setTimeout(DBUS_TIMEOUT);
QDBusObjectPath pth = get_response<QDBusObjectPath>(nm.call("Get", connection_iface, "Ip4Config"));
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", connection_iface, "Type"));
QString type = get_response<QString>(nm.call("Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Type"));
if (type == "802-11-wireless") {
QDBusInterface nm2(nm_service, ip4config, props_iface, bus);
nm2.setTimeout(dbus_timeout);
QDBusInterface nm2(NM_DBUS_SERVICE, ip4config, NM_DBUS_INTERFACE_PROPERTIES, bus);
nm2.setTimeout(DBUS_TIMEOUT);
const QDBusArgument &arr = get_response<QDBusArgument>(nm2.call("Get", ipv4config_iface, "AddressData"));
const QDBusArgument &arr = get_response<QDBusArgument>(nm2.call("Get", NM_DBUS_INTERFACE_IP4_CONFIG, "AddressData"));
QMap<QString, QVariant> pth2;
arr.beginArray();
while (!arr.atEnd()) {
@ -158,8 +120,8 @@ QString WifiManager::get_ipv4_address() {
QList<Network> WifiManager::get_networks() {
QList<Network> r;
QDBusInterface nm(nm_service, adapter, wireless_device_iface, bus);
nm.setTimeout(dbus_timeout);
QDBusInterface nm(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_DEVICE_WIRELESS, bus);
nm.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = nm.call("GetAllAccessPoints");
QVariant first = response.arguments().at(0);
@ -249,8 +211,8 @@ void WifiManager::connect(const QByteArray &ssid, const QString &username, const
connection["ipv4"]["method"] = "auto";
connection["ipv6"]["method"] = "ignore";
QDBusInterface nm_settings(nm_service, nm_settings_path, nm_settings_iface, bus);
nm_settings.setTimeout(dbus_timeout);
QDBusInterface nm_settings(NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, bus);
nm_settings.setTimeout(DBUS_TIMEOUT);
nm_settings.call("AddConnection", QVariant::fromValue(connection));
}
@ -258,15 +220,15 @@ void WifiManager::connect(const QByteArray &ssid, const QString &username, const
void WifiManager::deactivateConnection(const QString &ssid) {
for (QDBusObjectPath active_connection_raw : get_active_connections()) {
QString active_connection = active_connection_raw.path();
QDBusInterface nm(nm_service, active_connection, props_iface, bus);
nm.setTimeout(dbus_timeout);
QDBusInterface nm(NM_DBUS_SERVICE, active_connection, NM_DBUS_INTERFACE_PROPERTIES, bus);
nm.setTimeout(DBUS_TIMEOUT);
QDBusObjectPath pth = get_response<QDBusObjectPath>(nm.call("Get", connection_iface, "SpecificObject"));
QDBusObjectPath pth = get_response<QDBusObjectPath>(nm.call("Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "SpecificObject"));
if (pth.path() != "" && pth.path() != "/") {
QString Ssid = get_property(pth.path(), "Ssid");
if (Ssid == ssid) {
QDBusInterface nm2(nm_service, nm_path, nm_iface, bus);
nm2.setTimeout(dbus_timeout);
QDBusInterface nm2(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, bus);
nm2.setTimeout(DBUS_TIMEOUT);
nm2.call("DeactivateConnection", QVariant::fromValue(active_connection_raw));
}
}
@ -274,10 +236,10 @@ void WifiManager::deactivateConnection(const QString &ssid) {
}
QVector<QDBusObjectPath> WifiManager::get_active_connections() {
QDBusInterface nm(nm_service, nm_path, props_iface, bus);
nm.setTimeout(dbus_timeout);
QDBusInterface nm(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE_PROPERTIES, bus);
nm.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = nm.call("Get", nm_iface, "ActiveConnections");
QDBusMessage response = nm.call("Get", NM_DBUS_INTERFACE, "ActiveConnections");
const QDBusArgument &arr = get_response<QDBusArgument>(response);
QVector<QDBusObjectPath> conns;
@ -298,61 +260,61 @@ bool WifiManager::isKnownConnection(const QString &ssid) {
void WifiManager::forgetConnection(const QString &ssid) {
const QDBusObjectPath &path = getConnectionPath(ssid);
if (!path.path().isEmpty()) {
QDBusInterface nm2(nm_service, path.path(), nm_settings_conn_iface, bus);
QDBusInterface nm2(NM_DBUS_SERVICE, path.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, bus);
nm2.call("Delete");
}
}
bool WifiManager::isWirelessAdapter(const QDBusObjectPath &path) {
QDBusInterface device_props(nm_service, path.path(), props_iface, bus);
device_props.setTimeout(dbus_timeout);
const uint deviceType = get_response<uint>(device_props.call("Get", device_iface, "DeviceType"));
return deviceType == device_type_wifi;
QDBusInterface device_props(NM_DBUS_SERVICE, path.path(), NM_DBUS_INTERFACE_PROPERTIES, bus);
device_props.setTimeout(DBUS_TIMEOUT);
const uint deviceType = get_response<uint>(device_props.call("Get", NM_DBUS_INTERFACE_DEVICE, "DeviceType"));
return deviceType == NM_DEVICE_TYPE_WIFI;
}
void WifiManager::requestScan() {
QDBusInterface nm(nm_service, adapter, wireless_device_iface, bus);
nm.setTimeout(dbus_timeout);
QDBusInterface nm(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_DEVICE_WIRELESS, bus);
nm.setTimeout(DBUS_TIMEOUT);
nm.call("RequestScan", QVariantMap());
}
uint WifiManager::get_wifi_device_state() {
QDBusInterface device_props(nm_service, adapter, props_iface, bus);
device_props.setTimeout(dbus_timeout);
QDBusInterface device_props(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_PROPERTIES, bus);
device_props.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = device_props.call("Get", device_iface, "State");
QDBusMessage response = device_props.call("Get", NM_DBUS_INTERFACE_DEVICE, "State");
uint resp = get_response<uint>(response);
return resp;
}
QString WifiManager::get_active_ap() {
QDBusInterface device_props(nm_service, adapter, props_iface, bus);
device_props.setTimeout(dbus_timeout);
QDBusInterface device_props(NM_DBUS_SERVICE, adapter, NM_DBUS_INTERFACE_PROPERTIES, bus);
device_props.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = device_props.call("Get", wireless_device_iface, "ActiveAccessPoint");
QDBusMessage response = device_props.call("Get", NM_DBUS_INTERFACE_DEVICE_WIRELESS, "ActiveAccessPoint");
QDBusObjectPath r = get_response<QDBusObjectPath>(response);
return r.path();
}
QByteArray WifiManager::get_property(const QString &network_path , const QString &property) {
QDBusInterface device_props(nm_service, network_path, props_iface, bus);
device_props.setTimeout(dbus_timeout);
QDBusInterface device_props(NM_DBUS_SERVICE, network_path, NM_DBUS_INTERFACE_PROPERTIES, bus);
device_props.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = device_props.call("Get", ap_iface, property);
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) {
QDBusInterface device_props(nm_service, network_path, props_iface, bus);
device_props.setTimeout(dbus_timeout);
QDBusInterface device_props(NM_DBUS_SERVICE, network_path, NM_DBUS_INTERFACE_PROPERTIES, bus);
device_props.setTimeout(DBUS_TIMEOUT);
QDBusMessage response = device_props.call("Get", ap_iface, "Strength");
QDBusMessage response = device_props.call("Get", NM_DBUS_INTERFACE_ACCESS_POINT, "Strength");
return get_response<unsigned int>(response);
}
QString WifiManager::getAdapter() {
QDBusInterface nm(nm_service, nm_path, nm_iface, bus);
nm.setTimeout(dbus_timeout);
QDBusInterface nm(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, bus);
nm.setTimeout(DBUS_TIMEOUT);
const QDBusReply<QList<QDBusObjectPath>> &response = nm.call("GetDevices");
for (const QDBusObjectPath &path : response.value()) {
@ -365,10 +327,10 @@ QString WifiManager::getAdapter() {
void WifiManager::stateChange(unsigned int new_state, unsigned int previous_state, unsigned int change_reason) {
raw_adapter_state = new_state;
if (new_state == state_need_auth && change_reason == reason_wrong_password) {
if (new_state == NM_DEVICE_STATE_NEED_AUTH && change_reason == NM_DEVICE_STATE_REASON_SUPPLICANT_DISCONNECT) {
knownConnections.remove(getConnectionPath(connecting_to_network));
emit wrongPassword(connecting_to_network);
} else if (new_state == state_connected) {
} else if (new_state == NM_DEVICE_STATE_ACTIVATED) {
connecting_to_network = "";
if (this->isVisible()) {
refreshNetworks();
@ -379,7 +341,7 @@ void WifiManager::stateChange(unsigned int new_state, unsigned int previous_stat
// https://developer.gnome.org/NetworkManager/stable/gdbus-org.freedesktop.NetworkManager.Device.Wireless.html
void WifiManager::propertyChange(const QString &interface, const QVariantMap &props, const QStringList &invalidated_props) {
if (interface == wireless_device_iface && props.contains("LastScan")) {
if (interface == NM_DBUS_INTERFACE_DEVICE_WIRELESS && props.contains("LastScan")) {
if (knownConnections.isEmpty()) {
knownConnections = listConnections();
}
@ -423,16 +385,16 @@ QDBusObjectPath WifiManager::getConnectionPath(const QString &ssid) {
}
QString WifiManager::getConnectionSsid(const QDBusObjectPath &path) {
QDBusInterface nm(nm_service, path.path(), nm_settings_conn_iface, bus);
nm.setTimeout(dbus_timeout);
QDBusInterface nm(NM_DBUS_SERVICE, path.path(), NM_DBUS_INTERFACE_SETTINGS_CONNECTION, bus);
nm.setTimeout(DBUS_TIMEOUT);
const QDBusReply<Connection> result = nm.call("GetSettings");
return result.value().value("802-11-wireless").value("ssid").toString();
}
QMap<QDBusObjectPath, QString> WifiManager::listConnections() {
QMap<QDBusObjectPath, QString> connections;
QDBusInterface nm(nm_service, nm_settings_path, nm_settings_iface, bus);
nm.setTimeout(dbus_timeout);
QDBusInterface nm(NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, bus);
nm.setTimeout(DBUS_TIMEOUT);
const QDBusReply<QList<QDBusObjectPath>> response = nm.call("ListConnections");
for (const QDBusObjectPath &path : response.value()) {
@ -445,8 +407,8 @@ void WifiManager::activateWifiConnection(const QString &ssid) {
const QDBusObjectPath &path = getConnectionPath(ssid);
if (!path.path().isEmpty()) {
connecting_to_network = ssid;
QDBusInterface nm3(nm_service, nm_path, nm_iface, bus);
nm3.setTimeout(dbus_timeout);
QDBusInterface nm3(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, bus);
nm3.setTimeout(DBUS_TIMEOUT);
nm3.call("ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(adapter)), QVariant::fromValue(QDBusObjectPath("/")));
}
}
@ -479,8 +441,8 @@ void WifiManager::addTetheringConnection() {
connection["ipv4"]["route-metric"] = 1100;
connection["ipv6"]["method"] = "ignore";
QDBusInterface nm_settings(nm_service, nm_settings_path, nm_settings_iface, bus);
nm_settings.setTimeout(dbus_timeout);
QDBusInterface nm_settings(NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, bus);
nm_settings.setTimeout(DBUS_TIMEOUT);
nm_settings.call("AddConnection", QVariant::fromValue(connection));
}

@ -3,6 +3,8 @@
#include <QtDBus>
#include <QWidget>
#include "selfdrive/ui/qt/offroad/networkmanager.h"
enum class SecurityType {
OPEN,
WPA,

Loading…
Cancel
Save