From bf9a64ecad0f228f4a743feec6a0a40d6f01d807 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Fri, 20 Nov 2020 22:24:07 -0800 Subject: [PATCH] Qt wifi cleanup (#2594) * cleanup wifi * spacing * cleanup * little more * wifi manager * typo old-commit-hash: 45eccc842a65944d6fb2267b93cf03dc36694e27 --- selfdrive/ui/qt/offroad/wifi.cc | 87 +++++++++++-------------- selfdrive/ui/qt/offroad/wifi.hpp | 18 ++--- selfdrive/ui/qt/offroad/wifiManager.cc | 48 ++++++++------ selfdrive/ui/qt/offroad/wifiManager.hpp | 65 +++++++++--------- 4 files changed, 107 insertions(+), 111 deletions(-) diff --git a/selfdrive/ui/qt/offroad/wifi.cc b/selfdrive/ui/qt/offroad/wifi.cc index 54b63a2d14..4eb6b174cf 100644 --- a/selfdrive/ui/qt/offroad/wifi.cc +++ b/selfdrive/ui/qt/offroad/wifi.cc @@ -1,31 +1,19 @@ #include -#include -#include #include #include #include #include -#include #include -#include -#include -#include #include "wifi.hpp" -#include "wifiManager.hpp" -#include "input_field.hpp" -CustomConnectButton::CustomConnectButton(QString text, int iid){ - setText(text); - id=iid; -} -void clearLayout(QLayout* layout){ - while (QLayoutItem* item = layout->takeAt(0)){ +void clearLayout(QLayout* layout) { + while (QLayoutItem* item = layout->takeAt(0)) { if (QWidget* widget = item->widget()){ widget->deleteLater(); } - if (QLayout* childLayout = item->layout()){ + if (QLayout* childLayout = item->layout()) { clearLayout(childLayout); } delete item; @@ -53,10 +41,10 @@ WifiUI::WifiUI(QWidget *parent) : QWidget(parent) { top_layout->addWidget(swidget); setLayout(top_layout); a->setStyleSheet(R"( - QLineEdit { - background-color: #114265; - } - )"); + QLineEdit { + background-color: #114265; + } + )"); // TODO: implement (not) connecting with wrong password @@ -69,8 +57,8 @@ WifiUI::WifiUI(QWidget *parent) : QWidget(parent) { wifi->request_scan(); } -void WifiUI::refresh(){ - if (!this->isVisible()){ +void WifiUI::refresh() { + if (!this->isVisible()) { return; } @@ -78,13 +66,19 @@ void WifiUI::refresh(){ wifi->refreshNetworks(); clearLayout(vlayout); - int i=0; - QButtonGroup* connectButtons=new QButtonGroup(this); + connectButtons = new QButtonGroup(this); QObject::connect(connectButtons, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(handleButton(QAbstractButton*))); + + int i = 0; for (Network &network : wifi->seen_networks){ QHBoxLayout *hlayout = new QHBoxLayout; + + // SSID + hlayout->addSpacing(50); hlayout->addWidget(new QLabel(QString::fromUtf8(network.ssid))); + + // strength indicator unsigned int strength_scale = std::round(network.strength / 25.0) * 25; QPixmap pix("../assets/offroad/indicator_wifi_" + QString::number(strength_scale) + ".png"); QLabel *icon = new QLabel(); @@ -93,21 +87,22 @@ void WifiUI::refresh(){ hlayout->addWidget(icon); hlayout->addSpacing(20); - CustomConnectButton* m_button = new CustomConnectButton(network.connected ? "Connected" : "Connect",i); - m_button->setFixedWidth(300); - m_button->setDisabled(network.connected || network.security_type == SecurityType::UNSUPPORTED); - connectButtons->addButton(m_button,i); - - hlayout->addWidget(m_button); + // connect button + QPushButton* btn = new QPushButton(network.connected ? "Connected" : "Connect"); + btn->setFixedWidth(300); + btn->setDisabled(network.connected || network.security_type == SecurityType::UNSUPPORTED); + hlayout->addWidget(btn); hlayout->addSpacing(20); + connectButtons->addButton(btn, i++); + QWidget * w = new QWidget; w->setLayout(hlayout); vlayout->addWidget(w); w->setStyleSheet(R"( - QLabel { - font-size: 40px + QLabel { + font-size: 40px; } QPushButton:enabled { background-color: #114265; @@ -119,41 +114,37 @@ void WifiUI::refresh(){ background-color: #114265; } )"); - i+=1; } } -void WifiUI::handleButton(QAbstractButton* button){ - CustomConnectButton* m_button = static_cast(button); - int id = m_button->id; - qDebug()<seen_networks[id]; - a->label->setText("Password for "+n.ssid); - if(n.security_type==SecurityType::OPEN){ - wifi->connect(n); - } else if (n.security_type==SecurityType::WPA){ +void WifiUI::handleButton(QAbstractButton* button) { + QPushButton* btn = static_cast(button); + qDebug() << connectButtons->id(btn); + Network n = wifi->seen_networks[connectButtons->id(btn)]; + + a->label->setText("Enter password for \"" + n.ssid + "\""); + if(n.security_type == SecurityType::OPEN){ + wifi->connect(n); + } else if (n.security_type == SecurityType::WPA){ QString password = getStringFromUser(); - if(password != ""){ + + if(password.size()){ wifi->connect(n, password); } } else { - qDebug() << "Cannot determine a network's security type"; + qDebug() << "Cannot determine network's security type"; } - } QString WifiUI::getStringFromUser(){ swidget->setCurrentIndex(1); - loop.exec(); - swidget->setCurrentIndex(0); - return text; } -void WifiUI::receiveText(QString t){ +void WifiUI::receiveText(QString t) { loop.quit(); text = t; } diff --git a/selfdrive/ui/qt/offroad/wifi.hpp b/selfdrive/ui/qt/offroad/wifi.hpp index b88c9183e1..b04bb5bf8e 100644 --- a/selfdrive/ui/qt/offroad/wifi.hpp +++ b/selfdrive/ui/qt/offroad/wifi.hpp @@ -1,22 +1,14 @@ #pragma once -#include "wifiManager.hpp" -#include "input_field.hpp" + #include -#include -#include #include #include -#include #include #include +#include "wifiManager.hpp" +#include "input_field.hpp" -class CustomConnectButton : public QPushButton{ - -public: - explicit CustomConnectButton(QString text, int iid); - int id; -}; class WifiUI : public QWidget { Q_OBJECT @@ -32,6 +24,8 @@ private: QEventLoop loop; QTimer * timer; QString text; + QButtonGroup *connectButtons; + QString getStringFromUser(); public: @@ -41,6 +35,4 @@ private slots: void handleButton(QAbstractButton* m_button); void refresh(); void receiveText(QString text); -signals: - void gotText(); }; diff --git a/selfdrive/ui/qt/offroad/wifiManager.cc b/selfdrive/ui/qt/offroad/wifiManager.cc index a602b75e12..987b09c8e4 100644 --- a/selfdrive/ui/qt/offroad/wifiManager.cc +++ b/selfdrive/ui/qt/offroad/wifiManager.cc @@ -2,8 +2,7 @@ #include #include "wifiManager.hpp" -#include "wifi.hpp" -typedef QMap > Connection; + QString nm_path = "/org/freedesktop/NetworkManager"; QString nm_settings_path = "/org/freedesktop/NetworkManager/Settings"; @@ -91,11 +90,12 @@ SecurityType WifiManager::getSecurityType(QString path){ int wpaflag = get_property(path, "WpaFlags").toInt(); int rsnflag = get_property(path, "RsnFlags").toInt(); int wpa_props = wpaflag | rsnflag; + if(sflag == 0){ return SecurityType::OPEN; - }else if((sflag & 0x1) && (wpa_props & (0x333) && !(wpa_props & 0x200)) ){ + } else if((sflag & 0x1) && (wpa_props & (0x333) && !(wpa_props & 0x200))) { return SecurityType::WPA; - }else{ + } else { // qDebug() << "Cannot determine security type for " << get_property(path, "Ssid") << " with flags"; // qDebug() << "flag " << sflag; // qDebug() << "WpaFlag " << wpaflag; @@ -103,22 +103,25 @@ SecurityType WifiManager::getSecurityType(QString path){ return SecurityType::UNSUPPORTED; } } + void WifiManager::connect(Network n){ - return connect(n,"",""); + return connect(n, "", ""); } + void WifiManager::connect(Network n, QString password){ return connect(n, "", password); } void WifiManager::connect(Network n, QString username, QString password){ QString active_ap = get_active_ap(); - if(active_ap!=""){ - clear_connections(get_property(active_ap,"Ssid")); + if (active_ap!="") { + clear_connections(get_property(active_ap, "Ssid")); } clear_connections(n.ssid); qDebug() << "Connecting to"<< n.ssid << "with username, password =" << username << "," <> path; - qDebug()<> path; + qDebug()< > map; dbusArg >> map; - for( QString outer_key : map.keys() ){ - QMap innerMap = map.value(outer_key); - for( QString inner_key : innerMap.keys() ){ - if(inner_key=="ssid"){ - QString value = innerMap.value(inner_key).value(); - if(value == ssid){ - // qDebug()<<"Deleting "< innerMap = map.value(outer_key); + for(QString inner_key : innerMap.keys()) { + if(inner_key == "ssid"){ + QString value = innerMap.value(inner_key).value(); + if(value == ssid){ + // qDebug()<<"Deleting "<(response); return resp; } + QString WifiManager::get_active_ap(){ QDBusInterface device_props(nm_service, adapter, props_iface, bus); QDBusMessage response = device_props.call("Get", wireless_device_iface, "ActiveAccessPoint"); QDBusObjectPath r = get_response(response); return r.path(); } + QByteArray WifiManager::get_property(QString network_path ,QString property){ QDBusInterface device_props(nm_service, network_path, props_iface, bus); QDBusMessage response = device_props.call("Get", ap_iface, property); @@ -244,7 +250,7 @@ QString WifiManager::get_adapter(){ QDBusMessage response = device_props.call("Get", device_iface, "DeviceType"); uint device_type = get_response(response); - if (device_type == 2){ // Wireless + if (device_type == 2) { // Wireless adapter_path = path.path(); break; } diff --git a/selfdrive/ui/qt/offroad/wifiManager.hpp b/selfdrive/ui/qt/offroad/wifiManager.hpp index 463e3520f8..e107f6ade3 100644 --- a/selfdrive/ui/qt/offroad/wifiManager.hpp +++ b/selfdrive/ui/qt/offroad/wifiManager.hpp @@ -1,43 +1,50 @@ #pragma once + #include #include -enum class SecurityType{OPEN, WPA, UNSUPPORTED}; + +enum class SecurityType { + OPEN, + WPA, + UNSUPPORTED +}; + +typedef QMap> Connection; struct Network { QString path; QByteArray ssid; unsigned int strength; bool connected; - SecurityType security_type; }; class WifiManager{ - private: - QVector seen_ssids; - QString adapter;//Path to network manager wifi-device - QDBusConnection bus = QDBusConnection::systemBus(); - - QString get_adapter(); - QList get_networks(); - void connect(QByteArray ssid, QString username, QString password, SecurityType security_type); - QString get_active_ap(); - void clear_connections(QString ssid); - void print_active_connections(); - uint get_wifi_device_state(); - QByteArray get_ap_ssid(QString network_path); - QByteArray get_property(QString network_path, QString property); - unsigned int get_ap_strength(QString network_path); - SecurityType getSecurityType(QString ssid); - - public: - bool has_adapter; - void request_scan(); - QVector seen_networks; - - explicit WifiManager(); - void refreshNetworks(); - void connect(Network ssid); - void connect(Network ssid, QString password); - void connect(Network ssid, QString username, QString password); +public: + explicit WifiManager(); + + bool has_adapter; + void request_scan(); + QVector seen_networks; + + void refreshNetworks(); + void connect(Network ssid); + void connect(Network ssid, QString password); + void connect(Network ssid, QString username, QString password); + +private: + QVector seen_ssids; + QString adapter;//Path to network manager wifi-device + QDBusConnection bus = QDBusConnection::systemBus(); + + QString get_adapter(); + QList get_networks(); + void connect(QByteArray ssid, QString username, QString password, SecurityType security_type); + QString get_active_ap(); + void clear_connections(QString ssid); + void print_active_connections(); + uint get_wifi_device_state(); + QByteArray get_property(QString network_path, QString property); + unsigned int get_ap_strength(QString network_path); + SecurityType getSecurityType(QString ssid); };