Qt wifi cleanup (#2594)

* cleanup wifi

* spacing

* cleanup

* little more

* wifi manager

* typo
old-commit-hash: 45eccc842a
commatwo_master
Adeeb Shihadeh 5 years ago committed by GitHub
parent a2ee407c2b
commit bf9a64ecad
  1. 87
      selfdrive/ui/qt/offroad/wifi.cc
  2. 18
      selfdrive/ui/qt/offroad/wifi.hpp
  3. 48
      selfdrive/ui/qt/offroad/wifiManager.cc
  4. 65
      selfdrive/ui/qt/offroad/wifiManager.hpp

@ -1,31 +1,19 @@
#include <QDebug> #include <QDebug>
#include <QListWidget>
#include <QVBoxLayout>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QPixmap> #include <QPixmap>
#include <QPushButton> #include <QPushButton>
#include <QInputDialog>
#include <QLineEdit> #include <QLineEdit>
#include <QCoreApplication>
#include <QButtonGroup>
#include <QStackedWidget>
#include "wifi.hpp" #include "wifi.hpp"
#include "wifiManager.hpp"
#include "input_field.hpp"
CustomConnectButton::CustomConnectButton(QString text, int iid){
setText(text);
id=iid;
}
void clearLayout(QLayout* layout){ void clearLayout(QLayout* layout) {
while (QLayoutItem* item = layout->takeAt(0)){ while (QLayoutItem* item = layout->takeAt(0)) {
if (QWidget* widget = item->widget()){ if (QWidget* widget = item->widget()){
widget->deleteLater(); widget->deleteLater();
} }
if (QLayout* childLayout = item->layout()){ if (QLayout* childLayout = item->layout()) {
clearLayout(childLayout); clearLayout(childLayout);
} }
delete item; delete item;
@ -53,10 +41,10 @@ WifiUI::WifiUI(QWidget *parent) : QWidget(parent) {
top_layout->addWidget(swidget); top_layout->addWidget(swidget);
setLayout(top_layout); setLayout(top_layout);
a->setStyleSheet(R"( a->setStyleSheet(R"(
QLineEdit { QLineEdit {
background-color: #114265; background-color: #114265;
} }
)"); )");
// TODO: implement (not) connecting with wrong password // TODO: implement (not) connecting with wrong password
@ -69,8 +57,8 @@ WifiUI::WifiUI(QWidget *parent) : QWidget(parent) {
wifi->request_scan(); wifi->request_scan();
} }
void WifiUI::refresh(){ void WifiUI::refresh() {
if (!this->isVisible()){ if (!this->isVisible()) {
return; return;
} }
@ -78,13 +66,19 @@ void WifiUI::refresh(){
wifi->refreshNetworks(); wifi->refreshNetworks();
clearLayout(vlayout); clearLayout(vlayout);
int i=0;
QButtonGroup* connectButtons=new QButtonGroup(this); connectButtons = new QButtonGroup(this);
QObject::connect(connectButtons, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(handleButton(QAbstractButton*))); QObject::connect(connectButtons, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(handleButton(QAbstractButton*)));
int i = 0;
for (Network &network : wifi->seen_networks){ for (Network &network : wifi->seen_networks){
QHBoxLayout *hlayout = new QHBoxLayout; QHBoxLayout *hlayout = new QHBoxLayout;
// SSID
hlayout->addSpacing(50);
hlayout->addWidget(new QLabel(QString::fromUtf8(network.ssid))); hlayout->addWidget(new QLabel(QString::fromUtf8(network.ssid)));
// strength indicator
unsigned int strength_scale = std::round(network.strength / 25.0) * 25; unsigned int strength_scale = std::round(network.strength / 25.0) * 25;
QPixmap pix("../assets/offroad/indicator_wifi_" + QString::number(strength_scale) + ".png"); QPixmap pix("../assets/offroad/indicator_wifi_" + QString::number(strength_scale) + ".png");
QLabel *icon = new QLabel(); QLabel *icon = new QLabel();
@ -93,21 +87,22 @@ void WifiUI::refresh(){
hlayout->addWidget(icon); hlayout->addWidget(icon);
hlayout->addSpacing(20); hlayout->addSpacing(20);
CustomConnectButton* m_button = new CustomConnectButton(network.connected ? "Connected" : "Connect",i); // connect button
m_button->setFixedWidth(300); QPushButton* btn = new QPushButton(network.connected ? "Connected" : "Connect");
m_button->setDisabled(network.connected || network.security_type == SecurityType::UNSUPPORTED); btn->setFixedWidth(300);
connectButtons->addButton(m_button,i); btn->setDisabled(network.connected || network.security_type == SecurityType::UNSUPPORTED);
hlayout->addWidget(btn);
hlayout->addWidget(m_button);
hlayout->addSpacing(20); hlayout->addSpacing(20);
connectButtons->addButton(btn, i++);
QWidget * w = new QWidget; QWidget * w = new QWidget;
w->setLayout(hlayout); w->setLayout(hlayout);
vlayout->addWidget(w); vlayout->addWidget(w);
w->setStyleSheet(R"( w->setStyleSheet(R"(
QLabel { QLabel {
font-size: 40px font-size: 40px;
} }
QPushButton:enabled { QPushButton:enabled {
background-color: #114265; background-color: #114265;
@ -119,41 +114,37 @@ void WifiUI::refresh(){
background-color: #114265; background-color: #114265;
} }
)"); )");
i+=1;
} }
} }
void WifiUI::handleButton(QAbstractButton* button){ void WifiUI::handleButton(QAbstractButton* button) {
CustomConnectButton* m_button = static_cast<CustomConnectButton*>(button); QPushButton* btn = static_cast<QPushButton*>(button);
int id = m_button->id; qDebug() << connectButtons->id(btn);
qDebug()<<id; Network n = wifi->seen_networks[connectButtons->id(btn)];
Network n = wifi->seen_networks[id];
a->label->setText("Password for "+n.ssid); a->label->setText("Enter password for \"" + n.ssid + "\"");
if(n.security_type==SecurityType::OPEN){
wifi->connect(n);
} else if (n.security_type==SecurityType::WPA){
if(n.security_type == SecurityType::OPEN){
wifi->connect(n);
} else if (n.security_type == SecurityType::WPA){
QString password = getStringFromUser(); QString password = getStringFromUser();
if(password != ""){
if(password.size()){
wifi->connect(n, password); wifi->connect(n, password);
} }
} else { } else {
qDebug() << "Cannot determine a network's security type"; qDebug() << "Cannot determine network's security type";
} }
} }
QString WifiUI::getStringFromUser(){ QString WifiUI::getStringFromUser(){
swidget->setCurrentIndex(1); swidget->setCurrentIndex(1);
loop.exec(); loop.exec();
swidget->setCurrentIndex(0); swidget->setCurrentIndex(0);
return text; return text;
} }
void WifiUI::receiveText(QString t){ void WifiUI::receiveText(QString t) {
loop.quit(); loop.quit();
text = t; text = t;
} }

@ -1,22 +1,14 @@
#pragma once #pragma once
#include "wifiManager.hpp"
#include "input_field.hpp"
#include <QWidget> #include <QWidget>
#include <QtDBus>
#include <QPushButton>
#include <QButtonGroup> #include <QButtonGroup>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QStackedLayout>
#include <QStackedWidget> #include <QStackedWidget>
#include <QTimer> #include <QTimer>
#include "wifiManager.hpp"
#include "input_field.hpp"
class CustomConnectButton : public QPushButton{
public:
explicit CustomConnectButton(QString text, int iid);
int id;
};
class WifiUI : public QWidget { class WifiUI : public QWidget {
Q_OBJECT Q_OBJECT
@ -32,6 +24,8 @@ private:
QEventLoop loop; QEventLoop loop;
QTimer * timer; QTimer * timer;
QString text; QString text;
QButtonGroup *connectButtons;
QString getStringFromUser(); QString getStringFromUser();
public: public:
@ -41,6 +35,4 @@ private slots:
void handleButton(QAbstractButton* m_button); void handleButton(QAbstractButton* m_button);
void refresh(); void refresh();
void receiveText(QString text); void receiveText(QString text);
signals:
void gotText();
}; };

@ -2,8 +2,7 @@
#include <set> #include <set>
#include "wifiManager.hpp" #include "wifiManager.hpp"
#include "wifi.hpp"
typedef QMap<QString, QMap<QString, QVariant> > Connection;
QString nm_path = "/org/freedesktop/NetworkManager"; QString nm_path = "/org/freedesktop/NetworkManager";
QString nm_settings_path = "/org/freedesktop/NetworkManager/Settings"; 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 wpaflag = get_property(path, "WpaFlags").toInt();
int rsnflag = get_property(path, "RsnFlags").toInt(); int rsnflag = get_property(path, "RsnFlags").toInt();
int wpa_props = wpaflag | rsnflag; int wpa_props = wpaflag | rsnflag;
if(sflag == 0){ if(sflag == 0){
return SecurityType::OPEN; 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; return SecurityType::WPA;
}else{ } else {
// qDebug() << "Cannot determine security type for " << get_property(path, "Ssid") << " with flags"; // qDebug() << "Cannot determine security type for " << get_property(path, "Ssid") << " with flags";
// qDebug() << "flag " << sflag; // qDebug() << "flag " << sflag;
// qDebug() << "WpaFlag " << wpaflag; // qDebug() << "WpaFlag " << wpaflag;
@ -103,22 +103,25 @@ SecurityType WifiManager::getSecurityType(QString path){
return SecurityType::UNSUPPORTED; return SecurityType::UNSUPPORTED;
} }
} }
void WifiManager::connect(Network n){ void WifiManager::connect(Network n){
return connect(n,"",""); return connect(n, "", "");
} }
void WifiManager::connect(Network n, QString password){ void WifiManager::connect(Network n, QString password){
return connect(n, "", password); return connect(n, "", password);
} }
void WifiManager::connect(Network n, QString username, QString password){ void WifiManager::connect(Network n, QString username, QString password){
QString active_ap = get_active_ap(); QString active_ap = get_active_ap();
if(active_ap!=""){ if (active_ap!="") {
clear_connections(get_property(active_ap,"Ssid")); clear_connections(get_property(active_ap, "Ssid"));
} }
clear_connections(n.ssid); clear_connections(n.ssid);
qDebug() << "Connecting to"<< n.ssid << "with username, password =" << username << "," <<password; qDebug() << "Connecting to"<< n.ssid << "with username, password =" << username << "," <<password;
connect(n.ssid, username, password, n.security_type); connect(n.ssid, username, password, n.security_type);
} }
void WifiManager::connect(QByteArray ssid, QString username, QString password, SecurityType security_type){ void WifiManager::connect(QByteArray ssid, QString username, QString password, SecurityType security_type){
Connection connection; Connection connection;
connection["connection"]["type"] = "802-11-wireless"; connection["connection"]["type"] = "802-11-wireless";
@ -145,7 +148,6 @@ void WifiManager::connect(QByteArray ssid, QString username, QString password, S
} else { } else {
qDebug() << result.value().path(); qDebug() << result.value().path();
} }
} }
void WifiManager::print_active_connections(){ void WifiManager::print_active_connections(){
@ -159,11 +161,12 @@ void WifiManager::print_active_connections(){
QDBusObjectPath path; QDBusObjectPath path;
step4.beginArray(); step4.beginArray();
while (!step4.atEnd()){ while (!step4.atEnd()){
step4 >> path; step4 >> path;
qDebug()<<path.path(); qDebug()<<path.path();
} }
step4.endArray(); step4.endArray();
} }
void WifiManager::clear_connections(QString ssid){ void WifiManager::clear_connections(QString ssid){
QDBusInterface nm(nm_service, nm_settings_path, nm_settings_iface, bus); QDBusInterface nm(nm_service, nm_settings_path, nm_settings_iface, bus);
QDBusMessage response = nm.call("ListConnections"); QDBusMessage response = nm.call("ListConnections");
@ -180,20 +183,21 @@ void WifiManager::clear_connections(QString ssid){
QMap<QString,QMap<QString,QVariant> > map; QMap<QString,QMap<QString,QVariant> > map;
dbusArg >> map; dbusArg >> map;
for( QString outer_key : map.keys() ){ for(QString outer_key : map.keys()) {
QMap<QString,QVariant> innerMap = map.value(outer_key); QMap<QString,QVariant> innerMap = map.value(outer_key);
for( QString inner_key : innerMap.keys() ){ for(QString inner_key : innerMap.keys()) {
if(inner_key=="ssid"){ if(inner_key == "ssid"){
QString value = innerMap.value(inner_key).value<QString>(); QString value = innerMap.value(inner_key).value<QString>();
if(value == ssid){ if(value == ssid){
// qDebug()<<"Deleting "<<value; // qDebug()<<"Deleting "<<value;
nm2.call("Delete"); nm2.call("Delete");
} }
}
} }
}
} }
} }
} }
void WifiManager::request_scan(){ void WifiManager::request_scan(){
if (!has_adapter) return; if (!has_adapter) return;
@ -207,12 +211,14 @@ uint WifiManager::get_wifi_device_state(){
uint resp = get_response<uint>(response); uint resp = get_response<uint>(response);
return resp; return resp;
} }
QString WifiManager::get_active_ap(){ QString WifiManager::get_active_ap(){
QDBusInterface device_props(nm_service, adapter, props_iface, bus); QDBusInterface device_props(nm_service, adapter, props_iface, bus);
QDBusMessage response = device_props.call("Get", wireless_device_iface, "ActiveAccessPoint"); QDBusMessage response = device_props.call("Get", wireless_device_iface, "ActiveAccessPoint");
QDBusObjectPath r = get_response<QDBusObjectPath>(response); QDBusObjectPath r = get_response<QDBusObjectPath>(response);
return r.path(); return r.path();
} }
QByteArray WifiManager::get_property(QString network_path ,QString property){ QByteArray WifiManager::get_property(QString network_path ,QString property){
QDBusInterface device_props(nm_service, network_path, props_iface, bus); QDBusInterface device_props(nm_service, network_path, props_iface, bus);
QDBusMessage response = device_props.call("Get", ap_iface, property); 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"); QDBusMessage response = device_props.call("Get", device_iface, "DeviceType");
uint device_type = get_response<uint>(response); uint device_type = get_response<uint>(response);
if (device_type == 2){ // Wireless if (device_type == 2) { // Wireless
adapter_path = path.path(); adapter_path = path.path();
break; break;
} }

@ -1,43 +1,50 @@
#pragma once #pragma once
#include <QWidget> #include <QWidget>
#include <QtDBus> #include <QtDBus>
enum class SecurityType{OPEN, WPA, UNSUPPORTED};
enum class SecurityType {
OPEN,
WPA,
UNSUPPORTED
};
typedef QMap<QString, QMap<QString, QVariant>> Connection;
struct Network { struct Network {
QString path; QString path;
QByteArray ssid; QByteArray ssid;
unsigned int strength; unsigned int strength;
bool connected; bool connected;
SecurityType security_type; SecurityType security_type;
}; };
class WifiManager{ class WifiManager{
private: public:
QVector<QByteArray> seen_ssids; explicit WifiManager();
QString adapter;//Path to network manager wifi-device
QDBusConnection bus = QDBusConnection::systemBus(); bool has_adapter;
void request_scan();
QString get_adapter(); QVector<Network> seen_networks;
QList<Network> get_networks();
void connect(QByteArray ssid, QString username, QString password, SecurityType security_type); void refreshNetworks();
QString get_active_ap(); void connect(Network ssid);
void clear_connections(QString ssid); void connect(Network ssid, QString password);
void print_active_connections(); void connect(Network ssid, QString username, QString password);
uint get_wifi_device_state();
QByteArray get_ap_ssid(QString network_path); private:
QByteArray get_property(QString network_path, QString property); QVector<QByteArray> seen_ssids;
unsigned int get_ap_strength(QString network_path); QString adapter;//Path to network manager wifi-device
SecurityType getSecurityType(QString ssid); QDBusConnection bus = QDBusConnection::systemBus();
public: QString get_adapter();
bool has_adapter; QList<Network> get_networks();
void request_scan(); void connect(QByteArray ssid, QString username, QString password, SecurityType security_type);
QVector<Network> seen_networks; QString get_active_ap();
void clear_connections(QString ssid);
explicit WifiManager(); void print_active_connections();
void refreshNetworks(); uint get_wifi_device_state();
void connect(Network ssid); QByteArray get_property(QString network_path, QString property);
void connect(Network ssid, QString password); unsigned int get_ap_strength(QString network_path);
void connect(Network ssid, QString username, QString password); SecurityType getSecurityType(QString ssid);
}; };

Loading…
Cancel
Save