From 30a0b40b5d70cc13fc66dbfe58b304a67771e5a6 Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Wed, 9 Feb 2022 20:27:58 +0100 Subject: [PATCH] ui: configure tethering based on prime type (#23733) * Store prime type in param * set tethering metric based on prime type * update existing connections * returning zero is fine * always set * add callback on activate * call systemctl * match server enum * add delay * assume field is there * snake case old-commit-hash: 4e4cb07297485eac378058c6b4e60901e10cbd15 --- selfdrive/common/params.cc | 2 +- selfdrive/ui/qt/offroad/wifiManager.cc | 30 +++++++++++++++++++++++--- selfdrive/ui/qt/offroad/wifiManager.h | 3 ++- selfdrive/ui/qt/onroad.cc | 2 +- selfdrive/ui/qt/widgets/prime.cc | 16 +++++++------- selfdrive/ui/qt/widgets/prime.h | 6 ++++++ selfdrive/ui/ui.cc | 2 +- selfdrive/ui/ui.h | 2 +- 8 files changed, 47 insertions(+), 16 deletions(-) diff --git a/selfdrive/common/params.cc b/selfdrive/common/params.cc index e53d03f87b..3f64f790c1 100644 --- a/selfdrive/common/params.cc +++ b/selfdrive/common/params.cc @@ -115,7 +115,6 @@ std::unordered_map keys = { {"GsmRoaming", PERSISTENT}, {"HardwareSerial", PERSISTENT}, {"HasAcceptedTerms", PERSISTENT}, - {"HasPrime", PERSISTENT}, {"IMEI", PERSISTENT}, {"InstallDate", PERSISTENT}, {"IsDriverViewEnabled", CLEAR_ON_MANAGER_START}, @@ -145,6 +144,7 @@ std::unordered_map keys = { {"PandaSignatures", CLEAR_ON_MANAGER_START}, {"Passive", PERSISTENT}, {"PrimeRedirected", PERSISTENT}, + {"PrimeType", PERSISTENT}, {"RecordFront", PERSISTENT}, {"RecordFrontLock", PERSISTENT}, // for the internal fleet {"ReleaseNotes", PERSISTENT}, diff --git a/selfdrive/ui/qt/offroad/wifiManager.cc b/selfdrive/ui/qt/offroad/wifiManager.cc index 32ff0918ef..1628f35d99 100644 --- a/selfdrive/ui/qt/offroad/wifiManager.cc +++ b/selfdrive/ui/qt/offroad/wifiManager.cc @@ -1,5 +1,8 @@ #include "selfdrive/ui/qt/offroad/wifiManager.h" +#include "selfdrive/ui/ui.h" +#include "selfdrive/ui/qt/widgets/prime.h" + #include "selfdrive/common/params.h" #include "selfdrive/common/swaglog.h" #include "selfdrive/ui/qt/util.h" @@ -306,12 +309,13 @@ void WifiManager::initConnections() { } } -void WifiManager::activateWifiConnection(const QString &ssid) { +std::optional WifiManager::activateWifiConnection(const QString &ssid) { const QDBusObjectPath &path = getConnectionPath(ssid); if (!path.path().isEmpty()) { connecting_to_network = ssid; - asyncCall(NM_DBUS_PATH, NM_DBUS_INTERFACE, "ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(adapter)), QVariant::fromValue(QDBusObjectPath("/"))); + return asyncCall(NM_DBUS_PATH, NM_DBUS_INTERFACE, "ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(adapter)), QVariant::fromValue(QDBusObjectPath("/"))); } + return std::nullopt; } void WifiManager::activateModemConnection(const QDBusObjectPath &path) { @@ -403,12 +407,32 @@ void WifiManager::addTetheringConnection() { call(NM_DBUS_PATH_SETTINGS, NM_DBUS_INTERFACE_SETTINGS, "AddConnection", QVariant::fromValue(connection)); } +void WifiManager::tetheringActivated(QDBusPendingCallWatcher *call) { + int prime_type = uiState()->prime_type; + int ipv4_forward = (prime_type == PrimeType::NONE || prime_type == PrimeType::LITE); + + if (!ipv4_forward) { + QTimer::singleShot(5000, this, [=] { + qWarning() << "net.ipv4.ip_forward = 0"; + std::system("sudo sysctl net.ipv4.ip_forward=0"); + }); + } + call->deleteLater(); +} + void WifiManager::setTetheringEnabled(bool enabled) { if (enabled) { if (!isKnownConnection(tethering_ssid)) { addTetheringConnection(); } - activateWifiConnection(tethering_ssid); + + auto pending_call = activateWifiConnection(tethering_ssid); + + if (pending_call) { + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(*pending_call); + QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, &WifiManager::tetheringActivated); + } + } else { deactivateConnectionBySsid(tethering_ssid); } diff --git a/selfdrive/ui/qt/offroad/wifiManager.h b/selfdrive/ui/qt/offroad/wifiManager.h index 49c107f555..659c237721 100644 --- a/selfdrive/ui/qt/offroad/wifiManager.h +++ b/selfdrive/ui/qt/offroad/wifiManager.h @@ -47,7 +47,7 @@ public: void requestScan(); void forgetConnection(const QString &ssid); bool isKnownConnection(const QString &ssid); - void activateWifiConnection(const QString &ssid); + std::optional activateWifiConnection(const QString &ssid); NetworkType currentNetworkType(); void updateGsmSettings(bool roaming, QString apn); void connect(const Network &ssid, const QString &password = {}, const QString &username = {}); @@ -97,4 +97,5 @@ private slots: void connectionRemoved(const QDBusObjectPath &path); void newConnection(const QDBusObjectPath &path); void refreshFinished(QDBusPendingCallWatcher *call); + void tetheringActivated(QDBusPendingCallWatcher *call); }; diff --git a/selfdrive/ui/qt/onroad.cc b/selfdrive/ui/qt/onroad.cc index 50e2e4de4b..f7e2dc9bf8 100644 --- a/selfdrive/ui/qt/onroad.cc +++ b/selfdrive/ui/qt/onroad.cc @@ -78,7 +78,7 @@ void OnroadWindow::mousePressEvent(QMouseEvent* e) { void OnroadWindow::offroadTransition(bool offroad) { #ifdef ENABLE_MAPS if (!offroad) { - if (map == nullptr && (uiState()->has_prime || !MAPBOX_TOKEN.isEmpty())) { + if (map == nullptr && (uiState()->prime_type || !MAPBOX_TOKEN.isEmpty())) { MapWindow * m = new MapWindow(get_mapbox_settings()); m->setFixedWidth(topWidget(this)->width() / 2); m->offroadTransition(offroad); diff --git a/selfdrive/ui/qt/widgets/prime.cc b/selfdrive/ui/qt/widgets/prime.cc index 2e9a9e6af9..27d472535b 100644 --- a/selfdrive/ui/qt/widgets/prime.cc +++ b/selfdrive/ui/qt/widgets/prime.cc @@ -307,19 +307,19 @@ void SetupWidget::replyFinished(const QString &response, bool success) { } QJsonObject json = doc.object(); + int prime_type = json["prime_type"].toInt(); + + if (uiState()->prime_type != prime_type) { + uiState()->prime_type = prime_type; + Params().put("PrimeType", std::to_string(prime_type)); + } + if (!json["is_paired"].toBool()) { mainLayout->setCurrentIndex(0); } else { popup->reject(); - bool prime = json["prime"].toBool(); - - if (uiState()->has_prime != prime) { - uiState()->has_prime = prime; - Params().putBool("HasPrime", prime); - } - - if (prime) { + if (prime_type) { mainLayout->setCurrentWidget(primeUser); } else { mainLayout->setCurrentWidget(primeAd); diff --git a/selfdrive/ui/qt/widgets/prime.h b/selfdrive/ui/qt/widgets/prime.h index f7470fe441..566238fc33 100644 --- a/selfdrive/ui/qt/widgets/prime.h +++ b/selfdrive/ui/qt/widgets/prime.h @@ -7,6 +7,12 @@ #include "selfdrive/ui/qt/widgets/input.h" +enum PrimeType { + NONE = 0, + MAGENTA, + LITE, +}; + // pairing QR code class PairingQRWidget : public QWidget { Q_OBJECT diff --git a/selfdrive/ui/ui.cc b/selfdrive/ui/ui.cc index d3f33294a1..c6fde5c48f 100644 --- a/selfdrive/ui/ui.cc +++ b/selfdrive/ui/ui.cc @@ -231,7 +231,7 @@ UIState::UIState(QObject *parent) : QObject(parent) { Params params; wide_camera = Hardware::TICI() ? params.getBool("EnableWideCamera") : false; - has_prime = params.getBool("HasPrime"); + prime_type = std::atoi(params.get("PrimeType").c_str()); // update timer timer = new QTimer(this); diff --git a/selfdrive/ui/ui.h b/selfdrive/ui/ui.h index dc7f7c697b..cd7ea446ec 100644 --- a/selfdrive/ui/ui.h +++ b/selfdrive/ui/ui.h @@ -127,7 +127,7 @@ public: UIScene scene = {}; bool awake; - bool has_prime = false; + int prime_type = 0; QTransform car_space_transform; bool wide_camera;