Skip to content

Commit

Permalink
only update ipv4_address when it changes
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Jan 10, 2022
1 parent b70738a commit 2926ca4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion selfdrive/ui/qt/offroad/networking.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWid
list->addItem(editPasswordButton);

// IP address
ipLabel = new LabelControl("IP Address", wifi->ipv4_address);
ipLabel = new LabelControl("IP Address");
list->addItem(ipLabel);

// SSH keys
Expand Down
3 changes: 2 additions & 1 deletion selfdrive/ui/qt/offroad/networkmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ const QString NM_DBUS_INTERFACE_PROPERTIES = "org.freedesktop.DBus.Prop
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_IP4_ADDRESS = "org.freedesktop.NetworkManager.Device.Ip4Address";
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_INTERFACE_IP4_CONFIG = "org.freedesktop.NetworkManager.Ip4Address";

const QString NM_DBUS_SERVICE = "org.freedesktop.NetworkManager";

Expand Down
47 changes: 21 additions & 26 deletions selfdrive/ui/qt/offroad/wifiManager.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "selfdrive/ui/qt/offroad/wifiManager.h"

#include <arpa/inet.h>
#include <algorithm>

#include <QHostAddress>
#include "selfdrive/common/params.h"
#include "selfdrive/ui/qt/util.h"

Expand Down Expand Up @@ -31,9 +33,13 @@ void WifiManager::setup() {
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)));

raw_adapter_state = call<uint>(adapter, NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_DEVICE, "State");
activeAp = call<QDBusObjectPath>(adapter, NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_DEVICE_WIRELESS, "ActiveAccessPoint").path();


QDBusReply<QMap<QString, QVariant>> replay = call(adapter, NM_DBUS_INTERFACE_PROPERTIES, "GetAll", NM_DBUS_INTERFACE_DEVICE);
auto properties = replay.value();
raw_adapter_state = properties["State"].toUInt();
setIP4Address(properties["Ip4Address"].toUInt());

initConnections();
requestScan();
}
Expand Down Expand Up @@ -78,28 +84,13 @@ void WifiManager::activationFinished(QDBusPendingCallWatcher *watcher) {
seenNetworks[ssid] = {ssid, strength, ctype, security};
}

ipv4_address = get_ipv4_address();
emit refreshSignal();
watcher->deleteLater();
}

QString WifiManager::get_ipv4_address() {
for (const auto &p : get_active_connections()) {
QString type = call<QString>(p.path(), NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Type");
if (type == "802-11-wireless") {
auto ip4config = call<QDBusObjectPath>(p.path(), NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_ACTIVE_CONNECTION, "Ip4Config");
const auto &arr = call<QDBusArgument>(ip4config.path(), NM_DBUS_INTERFACE_PROPERTIES, "Get", NM_DBUS_INTERFACE_IP4_CONFIG, "AddressData");
QMap<QString, QVariant> pth2;
arr.beginArray();
while (!arr.atEnd()) {
arr >> pth2;
arr.endArray();
return pth2.value("address").value<QString>();
}
arr.endArray();
}
}
return "";
void WifiManager::setIP4Address(uint address) {
ipv4_address = QHostAddress(htonl(address)).toString();
emit ipAddressChanged(ipv4_address);
}

SecurityType WifiManager::getSecurityType(const QMap<QString,QVariant> &properties) {
Expand Down Expand Up @@ -223,13 +214,17 @@ 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 == NM_DBUS_INTERFACE_DEVICE_WIRELESS && props.contains("LastScan")) {
if (!stop_ || firstScan) {
refreshNetworks();
firstScan = false;
if (interface == NM_DBUS_INTERFACE_DEVICE_WIRELESS) {
if (props.contains("LastScan")) {
if (!stop_ || firstScan) {
refreshNetworks();
firstScan = false;
}
} else if (props.contains("ActiveAccessPoint")) {
activeAp = props.value("ActiveAccessPoint").value<QDBusObjectPath>().path();
}
} else if (interface == NM_DBUS_INTERFACE_DEVICE_WIRELESS && props.contains("ActiveAccessPoint")) {
activeAp = props.value("ActiveAccessPoint").value<QDBusObjectPath>().path();
} else if (props.contains("Ip4Address")) {
setIP4Address(props["Ip4Address"].toUInt());
}
}

Expand Down
3 changes: 2 additions & 1 deletion selfdrive/ui/qt/offroad/wifiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class WifiManager : public QObject {
QTimer timer;
QString getAdapter(const uint = NM_DEVICE_TYPE_WIFI);
uint getAdapterType(const QDBusObjectPath &path);
QString get_ipv4_address();
void setIP4Address(uint address);
QString activeAp;
void deactivateConnectionBySsid(const QString &ssid);
void deactivateConnection(const QDBusObjectPath &path);
Expand Down Expand Up @@ -117,6 +117,7 @@ class WifiManager : public QObject {
signals:
void wrongPassword(const QString &ssid);
void refreshSignal();
void ipAddressChanged(const QString &ip4_address);

private slots:
void stateChange(unsigned int new_state, unsigned int previous_state, unsigned int change_reason);
Expand Down

0 comments on commit 2926ca4

Please sign in to comment.