From d898841e87d548cd057a7546b1c28be36eab0f1a Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Fri, 8 Oct 2021 08:47:23 +0200 Subject: [PATCH] Change APN from settings (#22440) * Change APN from settings * subtext * one function * bring connection down and up * fix adapter type * trim input * fix scrolling in scanning state Co-authored-by: Adeeb Shihadeh --- selfdrive/common/params.cc | 1 + selfdrive/ui/qt/offroad/networking.cc | 24 +++++++-- selfdrive/ui/qt/offroad/networkmanager.h | 1 + selfdrive/ui/qt/offroad/wifiManager.cc | 68 ++++++++++++++++++------ selfdrive/ui/qt/offroad/wifiManager.h | 11 ++-- 5 files changed, 83 insertions(+), 22 deletions(-) diff --git a/selfdrive/common/params.cc b/selfdrive/common/params.cc index 99b0299b62..22926c5934 100644 --- a/selfdrive/common/params.cc +++ b/selfdrive/common/params.cc @@ -149,6 +149,7 @@ std::unordered_map keys = { {"GithubSshKeys", PERSISTENT}, {"GithubUsername", PERSISTENT}, {"GitRemote", PERSISTENT}, + {"GsmApn", PERSISTENT}, {"GsmRoaming", PERSISTENT}, {"HardwareSerial", PERSISTENT}, {"HasAcceptedTerms", PERSISTENT}, diff --git a/selfdrive/ui/qt/offroad/networking.cc b/selfdrive/ui/qt/offroad/networking.cc index 4f27a836f6..05ca31225f 100644 --- a/selfdrive/ui/qt/offroad/networking.cc +++ b/selfdrive/ui/qt/offroad/networking.cc @@ -150,15 +150,33 @@ AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWid // Roaming toggle const bool roamingEnabled = params.getBool("GsmRoaming"); - wifi->setRoamingEnabled(roamingEnabled); ToggleControl *roamingToggle = new ToggleControl("Enable Roaming", "", "", roamingEnabled); QObject::connect(roamingToggle, &SshToggle::toggleFlipped, [=](bool state) { params.putBool("GsmRoaming", state); - wifi->setRoamingEnabled(state); + wifi->updateGsmSettings(state, QString::fromStdString(params.get("GsmApn"))); }); list->addItem(roamingToggle); - main_layout->addWidget(list); + // APN settings + ButtonControl *editApnButton = new ButtonControl("APN settings", "EDIT"); + connect(editApnButton, &ButtonControl::clicked, [=]() { + const bool roamingEnabled = params.getBool("GsmRoaming"); + const QString cur_apn = QString::fromStdString(params.get("GsmApn")); + QString apn = InputDialog::getText("Enter APN", this, "leave blank for automatic configuration", false, -1, cur_apn).trimmed(); + + if (apn.isEmpty()) { + params.remove("GsmApn"); + } else { + params.put("GsmApn", apn.toStdString()); + } + wifi->updateGsmSettings(roamingEnabled, apn); + }); + list->addItem(editApnButton); + + // Set initial config + wifi->updateGsmSettings(roamingEnabled, QString::fromStdString(params.get("GsmApn"))); + + main_layout->addWidget(new ScrollView(list, this)); main_layout->addStretch(1); } diff --git a/selfdrive/ui/qt/offroad/networkmanager.h b/selfdrive/ui/qt/offroad/networkmanager.h index d90371b60b..52d85c16af 100644 --- a/selfdrive/ui/qt/offroad/networkmanager.h +++ b/selfdrive/ui/qt/offroad/networkmanager.h @@ -33,5 +33,6 @@ const QString NM_DBUS_SERVICE = "org.freedesktop.NetworkM 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_TYPE_MODEM = 8; const int NM_DEVICE_STATE_REASON_SUPPLICANT_DISCONNECT = 8; const int DBUS_TIMEOUT = 100; diff --git a/selfdrive/ui/qt/offroad/wifiManager.cc b/selfdrive/ui/qt/offroad/wifiManager.cc index c6eb27c6ca..957d65e268 100644 --- a/selfdrive/ui/qt/offroad/wifiManager.cc +++ b/selfdrive/ui/qt/offroad/wifiManager.cc @@ -202,7 +202,7 @@ void WifiManager::connect(const QByteArray &ssid, const QString &username, const nm_settings.call("AddConnection", QVariant::fromValue(connection)); } -void WifiManager::deactivateConnection(const QString &ssid) { +void WifiManager::deactivateConnectionBySsid(const QString &ssid) { for (QDBusObjectPath active_connection_raw : get_active_connections()) { QString active_connection = active_connection_raw.path(); QDBusInterface nm(NM_DBUS_SERVICE, active_connection, NM_DBUS_INTERFACE_PROPERTIES, bus); @@ -212,14 +212,18 @@ void WifiManager::deactivateConnection(const QString &ssid) { if (pth.path() != "" && pth.path() != "/") { QString Ssid = get_property(pth.path(), "Ssid"); if (Ssid == ssid) { - QDBusInterface nm2(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, bus); - nm2.setTimeout(DBUS_TIMEOUT); - nm2.call("DeactivateConnection", QVariant::fromValue(active_connection_raw)); + deactivateConnection(active_connection_raw); } } } } +void WifiManager::deactivateConnection(const QDBusObjectPath &path) { + QDBusInterface nm2(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, bus); + nm2.setTimeout(DBUS_TIMEOUT); + nm2.call("DeactivateConnection", QVariant::fromValue(path)); +} + QVector WifiManager::get_active_connections() { QDBusInterface nm(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE_PROPERTIES, bus); nm.setTimeout(DBUS_TIMEOUT); @@ -250,11 +254,14 @@ void WifiManager::forgetConnection(const QString &ssid) { } } -bool WifiManager::isWirelessAdapter(const QDBusObjectPath &path) { +uint WifiManager::getAdapterType(const QDBusObjectPath &path) { QDBusInterface device_props(NM_DBUS_SERVICE, path.path(), NM_DBUS_INTERFACE_PROPERTIES, bus); device_props.setTimeout(DBUS_TIMEOUT); - const uint deviceType = get_response(device_props.call("Get", NM_DBUS_INTERFACE_DEVICE, "DeviceType")); - return deviceType == NM_DEVICE_TYPE_WIFI; + return get_response(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() { @@ -288,13 +295,13 @@ unsigned int WifiManager::get_ap_strength(const QString &network_path) { return get_response(response); } -QString WifiManager::getAdapter() { +QString WifiManager::getAdapter(const uint adapter_type) { QDBusInterface nm(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, bus); nm.setTimeout(DBUS_TIMEOUT); const QDBusReply> &response = nm.call("GetDevices"); for (const QDBusObjectPath &path : response.value()) { - if (isWirelessAdapter(path)) { + if (getAdapterType(path) == adapter_type) { return path.path(); } } @@ -352,7 +359,7 @@ void WifiManager::newConnection(const QDBusObjectPath &path) { void WifiManager::disconnect() { if (activeAp != "" && activeAp != "/") { - deactivateConnection(get_property(activeAp, "Ssid")); + deactivateConnectionBySsid(get_property(activeAp, "Ssid")); } } @@ -381,7 +388,7 @@ void WifiManager::initConnections() { if (settings.value("connection").value("type") == "802-11-wireless") { knownConnections[path] = settings.value("802-11-wireless").value("ssid").toString(); } else if (path.path() != "/") { - lteConnectionPath = path.path(); + lteConnectionPath = path; } } } @@ -396,6 +403,15 @@ void WifiManager::activateWifiConnection(const QString &ssid) { } } +void WifiManager::activateModemConnection(const QDBusObjectPath &path) { + QString modem = getAdapter(NM_DEVICE_TYPE_MODEM); + if (!path.path().isEmpty() && !modem.isEmpty()) { + 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(modem)), QVariant::fromValue(QDBusObjectPath("/"))); + } +} + // function matches tici/hardware.py NetworkType WifiManager::currentNetworkType() { QDBusInterface nm(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE_PROPERTIES, bus); @@ -423,15 +439,37 @@ NetworkType WifiManager::currentNetworkType() { return NetworkType::NONE; } -void WifiManager::setRoamingEnabled(bool roaming) { - if (!lteConnectionPath.isEmpty()) { - QDBusInterface nm(NM_DBUS_SERVICE, lteConnectionPath, NM_DBUS_INTERFACE_SETTINGS_CONNECTION, bus); +void WifiManager::updateGsmSettings(bool roaming, QString apn) { + 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 auto_config = apn.isEmpty(); Connection settings = QDBusReply(nm.call("GetSettings")).value(); + + if (settings.value("gsm").value("auto-config").toBool() != auto_config) { + qWarning() << "Changing gsm.auto-config to" << auto_config; + settings["gsm"]["auto-config"] = auto_config; + changes = true; + } + + if (settings.value("gsm").value("apn").toString() != apn) { + qWarning() << "Changing gsm.apn to" << apn; + settings["gsm"]["apn"] = apn; + changes = true; + } + if (settings.value("gsm").value("home-only").toBool() == roaming) { + qWarning() << "Changing gsm.home-only to" << !roaming; settings["gsm"]["home-only"] = !roaming; + changes = true; + } + + if (changes) { nm.call("UpdateUnsaved", QVariant::fromValue(settings)); // update is temporary + deactivateConnection(lteConnectionPath); + activateModemConnection(lteConnectionPath); } } } @@ -476,7 +514,7 @@ void WifiManager::setTetheringEnabled(bool enabled) { } activateWifiConnection(tethering_ssid); } else { - deactivateConnection(tethering_ssid); + deactivateConnectionBySsid(tethering_ssid); } } diff --git a/selfdrive/ui/qt/offroad/wifiManager.h b/selfdrive/ui/qt/offroad/wifiManager.h index 2fb539260d..04defd32dd 100644 --- a/selfdrive/ui/qt/offroad/wifiManager.h +++ b/selfdrive/ui/qt/offroad/wifiManager.h @@ -42,15 +42,16 @@ public: void requestScan(); QMap seenNetworks; QMap knownConnections; - QString lteConnectionPath; + QDBusObjectPath lteConnectionPath; QString ipv4_address; void refreshNetworks(); void forgetConnection(const QString &ssid); bool isKnownConnection(const QString &ssid); void activateWifiConnection(const QString &ssid); + void activateModemConnection(const QDBusObjectPath &path); NetworkType currentNetworkType(); - void setRoamingEnabled(bool roaming); + void updateGsmSettings(bool roaming, QString apn); void connect(const Network &ssid); void connect(const Network &ssid, const QString &password); @@ -73,13 +74,15 @@ private: const QString defaultTetheringPassword = "swagswagcomma"; bool firstScan = true; - QString getAdapter(); + QString getAdapter(const uint = NM_DEVICE_TYPE_WIFI); + uint getAdapterType(const QDBusObjectPath &path); bool isWirelessAdapter(const QDBusObjectPath &path); QString get_ipv4_address(); void connect(const QByteArray &ssid, const QString &username, const QString &password, SecurityType security_type); QString activeAp; void initActiveAp(); - void deactivateConnection(const QString &ssid); + void deactivateConnectionBySsid(const QString &ssid); + void deactivateConnection(const QDBusObjectPath &path); QVector get_active_connections(); uint get_wifi_device_state(); QByteArray get_property(const QString &network_path, const QString &property);