Files
StarPilot/selfdrive/ui/qt/network/wifi_manager.h
T
Shane Smiskol f38a98fc09 Metered Wi-Fi toggle (#35293)
* draft

* here too

* fixes

* fix

* ugh more fix, wifiManager is craze

* more

* be optimistic and let refresh happen naturally, the immediate refresh causes some paths/active connect to temporarily be unavailable

selfdrive/ui/qt/network/wifi_manager.cc: DBus call error: "Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the re
ply timeout expired, or the network connection was broken."
selfdrive/ui/qt/network/wifi_manager.cc: DBus call error: "Object path cannot be empty"

* nm is slow -- it takes 2s to commit to disk, and dbus errors are raised if you try again while previous is running (this is an ubuntu 24.04 bug)

* nice experience

* rm

* minor

* clean up

* self-explanatory

* rename

* rm this too

* revert

* draft

* Revert "draft"

This reverts commit 15283d977880fc60b8f9732772256e8337d6ac8e.

* Reapply "draft"

This reverts commit 8629921b0086ca71b88746d77ec7b8d3af3cd289.

* rm colors

* trinary, bit more code

* choose default when disabled

* only if enabling, wait for disable as normal

* remove original binary toggle

* clean up

* collapse

* clean up wifimanager

* update comment

* lite is a word
2025-05-21 20:45:47 -07:00

112 lines
3.5 KiB
C++

#pragma once
#include <optional>
#include <QtDBus>
#include <QTimer>
#include "selfdrive/ui/qt/network/networkmanager.h"
enum class SecurityType {
OPEN,
WPA,
UNSUPPORTED
};
enum class ConnectedType {
DISCONNECTED,
CONNECTING,
CONNECTED
};
enum class NetworkType {
NONE,
WIFI,
CELL,
ETHERNET
};
enum class MeteredType {
UNKNOWN,
YES,
NO
};
typedef QMap<QString, QVariantMap> Connection;
typedef QVector<QVariantMap> IpConfig;
struct Network {
QByteArray ssid;
unsigned int strength;
ConnectedType connected;
SecurityType security_type;
};
bool compare_by_strength(const Network &a, const Network &b);
inline int strengthLevel(unsigned int strength) { return std::clamp((int)round(strength / 33.), 0, 3); }
class WifiManager : public QObject {
Q_OBJECT
public:
QMap<QString, Network> seenNetworks;
QMap<QDBusObjectPath, QString> knownConnections;
QString ipv4_address;
bool tethering_on = false;
bool ipv4_forward = false;
explicit WifiManager(QObject* parent);
void start();
void stop();
void requestScan();
void forgetConnection(const QString &ssid);
bool isKnownConnection(const QString &ssid);
std::optional<QDBusPendingCall> activateWifiConnection(const QString &ssid);
NetworkType currentNetworkType();
MeteredType currentNetworkMetered();
std::optional<QDBusPendingCall> setCurrentNetworkMetered(MeteredType metered);
void updateGsmSettings(bool roaming, QString apn, bool metered);
void connect(const Network &ssid, const bool is_hidden = false, const QString &password = {}, const QString &username = {});
// Tethering functions
void setTetheringEnabled(bool enabled);
bool isTetheringEnabled();
void changeTetheringPassword(const QString &newPassword);
QString getTetheringPassword();
private:
QString adapter; // Path to network manager wifi-device
QTimer timer;
unsigned int raw_adapter_state = NM_DEVICE_STATE_UNKNOWN; // Connection status https://developer.gnome.org/NetworkManager/1.26/nm-dbus-types.html#NMDeviceState
QString connecting_to_network;
QString tethering_ssid;
const QString defaultTetheringPassword = "swagswagcomma";
QString activeAp;
QDBusObjectPath lteConnectionPath;
QString getAdapter(const uint = NM_DEVICE_TYPE_WIFI);
uint getAdapterType(const QDBusObjectPath &path);
QString getIp4Address();
void deactivateConnectionBySsid(const QString &ssid);
void deactivateConnection(const QDBusObjectPath &path);
QVector<QDBusObjectPath> getActiveConnections();
QByteArray get_property(const QString &network_path, const QString &property);
SecurityType getSecurityType(const QVariantMap &properties);
QDBusObjectPath getConnectionPath(const QString &ssid);
Connection getConnectionSettings(const QDBusObjectPath &path);
void initConnections();
void setup();
void refreshNetworks();
void activateModemConnection(const QDBusObjectPath &path);
void addTetheringConnection();
void setCurrentConnecting(const QString &ssid);
signals:
void wrongPassword(const QString &ssid);
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);
void deviceAdded(const QDBusObjectPath &path);
void connectionRemoved(const QDBusObjectPath &path);
void newConnection(const QDBusObjectPath &path);
void refreshFinished(QDBusPendingCallWatcher *call);
void tetheringActivated(QDBusPendingCallWatcher *call);
};