From 84369b373da9fc93196b64fb0a888578db8a87f7 Mon Sep 17 00:00:00 2001 From: sshane Date: Mon, 21 Jun 2021 20:55:55 -0700 Subject: [PATCH] Scan for networks on UI init (#21310) * refresh on UI init * scan and wait for signal from networkmanager to update * fixes incorrectPassword never triggering on known connections * Update UI on connect state change * pause timer on leaving networking * no need to update adapter state on init * uses AccessPoints signal clean up clean up * Revert "uses AccessPoints signal" This reverts commit 534219857a10dc200f979e1a66705ac13ca68698. * perform non-blocking scan on first start only * suggestions * forgetNetwork already checks this * Prevent double refresh on connect interaction and revert timer change * Should fix nm is inactive error * Revert "Should fix nm is inactive error" This reverts commit 36a7ad098b713ae7e17cf1a8d717455ecdd29e2b. * Fixup original timer code. could be cleaned up, but works * use WifiManager's firstScan * no more force! * fix after rebase fix after rebase * scan for next update (seems clearer this way) * see how responsive device is at scanning, PC is taking up to 15 seconds * clean up and check ifVisible for scanning and UI updating * explicitly define relationship since it's not called in Networking any more old-commit-hash: a3a8fa9eac1897964ab339ec97694f90822aa623 --- selfdrive/ui/qt/offroad/networking.cc | 13 ++++++++----- selfdrive/ui/qt/offroad/networking.h | 8 ++++---- selfdrive/ui/qt/offroad/wifiManager.cc | 24 ++++++++++++++++-------- selfdrive/ui/qt/offroad/wifiManager.h | 15 ++++++++------- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/selfdrive/ui/qt/offroad/networking.cc b/selfdrive/ui/qt/offroad/networking.cc index dcf97d8f6..2d36287a9 100644 --- a/selfdrive/ui/qt/offroad/networking.cc +++ b/selfdrive/ui/qt/offroad/networking.cc @@ -33,7 +33,7 @@ Networking::Networking(QWidget* parent, bool show_advanced) : QWidget(parent), s main_layout->addWidget(warning); QTimer* timer = new QTimer(this); - QObject::connect(timer, &QTimer::timeout, this, &Networking::refresh); + QObject::connect(timer, &QTimer::timeout, this, &Networking::requestScan); timer->start(5000); attemptInitialization(); } @@ -47,6 +47,7 @@ void Networking::attemptInitialization() { } connect(wifi, &WifiManager::wrongPassword, this, &Networking::wrongPassword); + connect(wifi, &WifiManager::refreshSignal, this, &Networking::refreshSlot); QWidget* wifiScreen = new QWidget(this); QVBoxLayout* vlayout = new QVBoxLayout(wifiScreen); @@ -87,9 +88,10 @@ void Networking::attemptInitialization() { )"); main_layout->setCurrentWidget(wifiScreen); ui_setup_complete = true; + wifi->requestScan(); } -void Networking::refresh() { +void Networking::requestScan() { if (!this->isVisible()) { return; } @@ -99,6 +101,10 @@ void Networking::refresh() { return; } } + wifi->requestScan(); +} + +void Networking::refreshSlot() { wifiWidget->refresh(); an->refresh(); } @@ -182,7 +188,6 @@ void AdvancedNetworking::toggleTethering(bool enable) { editPasswordButton->setEnabled(!enable); } - // WifiUI functions WifiUI::WifiUI(QWidget *parent, WifiManager* wifi) : QWidget(parent), wifi(wifi) { @@ -196,8 +201,6 @@ WifiUI::WifiUI(QWidget *parent, WifiManager* wifi) : QWidget(parent), wifi(wifi) } void WifiUI::refresh() { - wifi->request_scan(); - wifi->refreshNetworks(); clearLayout(main_layout); connectButtons = new QButtonGroup(this); // TODO check if this is a leak diff --git a/selfdrive/ui/qt/offroad/networking.h b/selfdrive/ui/qt/offroad/networking.h index ef0153ee9..22b1ad9a9 100644 --- a/selfdrive/ui/qt/offroad/networking.h +++ b/selfdrive/ui/qt/offroad/networking.h @@ -72,15 +72,15 @@ private: bool ui_setup_complete = false; bool show_advanced; - Network selectedNetwork; - WifiUI* wifiWidget; WifiManager* wifi = nullptr; void attemptInitialization(); + void requestScan(); + +public slots: + void refreshSlot(); private slots: void connectToNetwork(const Network &n); - void refresh(); void wrongPassword(const QString &ssid); }; - diff --git a/selfdrive/ui/qt/offroad/wifiManager.cc b/selfdrive/ui/qt/offroad/wifiManager.cc index a29fe1a44..aaf281328 100644 --- a/selfdrive/ui/qt/offroad/wifiManager.cc +++ b/selfdrive/ui/qt/offroad/wifiManager.cc @@ -75,13 +75,13 @@ WifiManager::WifiManager(QWidget* parent) : QWidget(parent) { } QDBusInterface nm(nm_service, adapter, device_iface, bus); - bus.connect(nm_service, adapter, device_iface, "StateChanged", this, SLOT(change(unsigned int, unsigned int, unsigned int))); + 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 device_props(nm_service, adapter, props_iface, bus); device_props.setTimeout(dbus_timeout); QDBusMessage response = device_props.call("Get", device_iface, "State"); raw_adapter_state = get_response(response); - change(raw_adapter_state, 0, 0); // Set tethering ssid as "weedle" + first 4 characters of a dongle id tethering_ssid = "weedle"; @@ -291,7 +291,7 @@ void WifiManager::forgetConnection(const QString &ssid) { } } -void WifiManager::request_scan() { +void WifiManager::requestScan() { QDBusInterface nm(nm_service, adapter, wireless_device_iface, bus); nm.setTimeout(dbus_timeout); nm.call("RequestScan", QVariantMap()); @@ -363,13 +363,22 @@ QString WifiManager::get_adapter() { return adapter_path; } -void WifiManager::change(unsigned int new_state, unsigned int previous_state, unsigned int change_reason) { +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) { emit wrongPassword(connecting_to_network); } else if (new_state == state_connected) { - emit successfulConnection(connecting_to_network); connecting_to_network = ""; + refreshNetworks(); + emit refreshSignal(); + } +} + +// 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")) { + refreshNetworks(); + emit refreshSignal(); } } @@ -421,6 +430,7 @@ QVector> WifiManager::listConnections() { void WifiManager::activateWifiConnection(const QString &ssid) { QDBusObjectPath path = pathFromSsid(ssid); if (!path.path().isEmpty()) { + connecting_to_network = ssid; QString devicePath = get_adapter(); QDBusInterface nm3(nm_service, nm_path, nm_iface, bus); nm3.setTimeout(dbus_timeout); @@ -479,8 +489,6 @@ bool WifiManager::tetheringEnabled() { void WifiManager::changeTetheringPassword(const QString &newPassword) { tetheringPassword = newPassword; - if (isKnownNetwork(tethering_ssid.toUtf8())) { - forgetConnection(tethering_ssid.toUtf8()); - } + forgetConnection(tethering_ssid.toUtf8()); addTetheringConnection(); } diff --git a/selfdrive/ui/qt/offroad/wifiManager.h b/selfdrive/ui/qt/offroad/wifiManager.h index 7aac7025d..571211449 100644 --- a/selfdrive/ui/qt/offroad/wifiManager.h +++ b/selfdrive/ui/qt/offroad/wifiManager.h @@ -30,7 +30,7 @@ class WifiManager : public QWidget { public: explicit WifiManager(QWidget* parent); - void request_scan(); + void requestScan(); QVector seen_networks; QString ipv4_address; @@ -54,9 +54,9 @@ public: private: QVector seen_ssids; - QString adapter;//Path to network manager wifi-device + QString adapter; // Path to network manager wifi-device 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 tethering_ssid; QString tetheringPassword = "swagswagcommma"; @@ -75,10 +75,11 @@ private: QDBusObjectPath pathFromSsid(const QString &ssid); QVector> listConnections(); -private slots: - void change(unsigned int new_state, unsigned int previous_state, unsigned int change_reason); signals: void wrongPassword(const QString &ssid); - void successfulConnection(const QString &ssid); - void refresh(); + void refreshSignal(); + +private slots: + void stateChange(unsigned int new_state, unsigned int previous_state, unsigned int change_reason); + void propertyChange(const QString &interface, const QVariantMap &props, const QStringList &invalidated_props); };