diff --git a/selfdrive/ui/qt/offroad/settings.cc b/selfdrive/ui/qt/offroad/settings.cc index 91785c68dd..c857ebfbfe 100644 --- a/selfdrive/ui/qt/offroad/settings.cc +++ b/selfdrive/ui/qt/offroad/settings.cc @@ -197,7 +197,8 @@ QWidget * developer_panel() { } QWidget * network_panel(QWidget * parent) { - WifiUI *w = new WifiUI(); + Networking *w = new Networking(); + QObject::connect(parent, SIGNAL(sidebarPressed()), w, SLOT(sidebarChange())); QObject::connect(w, SIGNAL(openKeyboard()), parent, SLOT(closeSidebar())); QObject::connect(w, SIGNAL(closeKeyboard()), parent, SLOT(openSidebar())); return w; @@ -261,6 +262,7 @@ SettingsWindow::SettingsWindow(QWidget *parent) : QFrame(parent) { sidebar_layout->addWidget(btn, 0, Qt::AlignRight | Qt::AlignTop); panel_layout->addWidget(panel.second); QObject::connect(btn, SIGNAL(released()), this, SLOT(setActivePanel())); + QObject::connect(btn, &QPushButton::released, [=](){emit sidebarPressed();}); } qobject_cast(nav_btns->buttons()[0])->setChecked(true); sidebar_layout->addStretch(); @@ -301,9 +303,9 @@ SettingsWindow::SettingsWindow(QWidget *parent) : QFrame(parent) { } void SettingsWindow::closeSidebar() { - sidebar_widget->setVisible(false); + sidebar_widget->setFixedWidth(0); } void SettingsWindow::openSidebar() { - sidebar_widget->setVisible(true); + sidebar_widget->setFixedWidth(300); } diff --git a/selfdrive/ui/qt/offroad/settings.hpp b/selfdrive/ui/qt/offroad/settings.hpp index 6a39be404d..8d6ac3f535 100644 --- a/selfdrive/ui/qt/offroad/settings.hpp +++ b/selfdrive/ui/qt/offroad/settings.hpp @@ -36,6 +36,7 @@ public: signals: void closeSettings(); + void sidebarPressed(); private: QPushButton *sidebar_alert_widget; diff --git a/selfdrive/ui/qt/offroad/wifi.cc b/selfdrive/ui/qt/offroad/wifi.cc index 65ec42ebaa..d0211c7e6d 100644 --- a/selfdrive/ui/qt/offroad/wifi.cc +++ b/selfdrive/ui/qt/offroad/wifi.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include "wifi.hpp" #include "widgets/toggle.hpp" @@ -20,9 +21,17 @@ void clearLayout(QLayout* layout) { } } -WifiUI::WifiUI(QWidget *parent, int page_length) : QWidget(parent), networks_per_page(page_length) { +QWidget* layoutToWidget(QLayout* l, QWidget* parent){ + QWidget* q = new QWidget(parent); + q->setLayout(l); + return q; +} + +// Networking functions + +Networking::Networking(QWidget* parent){ try { - wifi = new WifiManager; + wifi = new WifiManager(this); } catch (std::exception &e) { QLabel* warning = new QLabel("Network manager is inactive!"); warning->setStyleSheet(R"(font-size: 65px;)"); @@ -32,57 +41,231 @@ WifiUI::WifiUI(QWidget *parent, int page_length) : QWidget(parent), networks_per setLayout(warning_layout); return; } + connect(wifi, SIGNAL(wrongPassword(QString)), this, SLOT(wrongPassword(QString))); + connect(wifi, SIGNAL(successfulConnection(QString)), this, SLOT(successfulConnection(QString))); + + + s = new QStackedLayout(this); + + inputField = new InputField(this, 8); + connect(inputField, SIGNAL(emitText(QString)), this, SLOT(receiveText(QString))); + connect(inputField, SIGNAL(cancel()), this, SLOT(abortTextInput())); + s->addWidget(inputField); + + QVBoxLayout* vlayout = new QVBoxLayout(this); + QPushButton* advancedSettings = new QPushButton("Advanced"); + advancedSettings->setStyleSheet(R"(margin-right: 30px)"); + advancedSettings->setFixedSize(300, 100); + connect(advancedSettings, &QPushButton::released, [=](){s->setCurrentIndex(2);}); + vlayout->addSpacing(10); + vlayout->addWidget(advancedSettings, 0, Qt::AlignRight); + vlayout->addSpacing(10); + + wifiWidget = new WifiUI(0, 5, wifi); + connect(wifiWidget, SIGNAL(connectToNetwork(Network)), this, SLOT(connectToNetwork(Network))); + vlayout->addWidget(wifiWidget, 1); + + s->addWidget(layoutToWidget(vlayout, this)); + + an = new AdvancedNetworking(this, wifi); + connect(an, &AdvancedNetworking::backPress, [=](){s->setCurrentIndex(1);}); + s->addWidget(an); + + s->setCurrentIndex(1); + + // Update network status + QTimer* timer = new QTimer(this); + QObject::connect(timer, SIGNAL(timeout()), this, SLOT(refresh())); + timer->start(5000); + + setStyleSheet(R"( + QPushButton { + font-size: 50px; + margin: 0px; + padding: 15px; + border-radius: 25px; + color: #dddddd; + background-color: #444444; + } + QPushButton:disabled { + padding: 20px; + color: #777777; + background-color: #222222; + } + )"); +} + +void Networking::refresh(){ + wifiWidget->refresh(); +} + +void Networking::connectToNetwork(Network n) { + if (n.security_type == SecurityType::OPEN) { + wifi->connect(n); + } else if (n.security_type == SecurityType::WPA) { + inputField->setPromptText("Enter password for \"" + n.ssid + "\""); + s->setCurrentIndex(0); + selectedNetwork = n; + } +} + +void Networking::abortTextInput(){ + s->setCurrentIndex(1); +} + +void Networking::receiveText(QString text) { + wifi->disconnect(); + wifi->connect(selectedNetwork, text); + s->setCurrentIndex(1); +} - QObject::connect(wifi, SIGNAL(wrongPassword(QString)), this, SLOT(wrongPassword(QString))); +void Networking::wrongPassword(QString ssid) { + if(s->currentIndex()==0){ + qDebug()<<"Wrong password, but we are already trying a new network"; + return; + } + for (Network n : wifi->seen_networks) { + if (n.ssid == ssid) { + inputField->setPromptText("Wrong password for \"" + n.ssid +"\""); + s->setCurrentIndex(0); + return; + } + } +} - QVBoxLayout * top_layout = new QVBoxLayout; - top_layout->setSpacing(0); - swidget = new QStackedWidget; +void Networking::successfulConnection(QString ssid) { + //Maybe we will want to do something here in the future. +} - // Networks page - wifi_widget = new QWidget; - QVBoxLayout* networkLayout = new QVBoxLayout; - QHBoxLayout *tethering_field = new QHBoxLayout; - tethering_field->addSpacing(50); +void Networking::sidebarChange(){ + s->setCurrentIndex(1); + an->s->setCurrentIndex(1); + refresh(); +} - ipv4 = new QLabel(""); - tethering_field->addWidget(ipv4); - tethering_field->addWidget(new QLabel("Enable Tethering")); +QFrame* hline(QWidget* parent = 0){ + QFrame* line = new QFrame(parent); + line->setFrameShape(QFrame::StyledPanel); + line->setStyleSheet("margin-left: 40px; margin-right: 40px; border-width: 1px; border-bottom-style: solid; border-color: gray;"); + line->setFixedHeight(2); + return line; +} +// AdvancedNetworking functions + +AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWidget(parent), wifi(wifi){ + s = new QStackedLayout(this);// inputField and settings + inputField = new InputField(this, 8); + connect(inputField, SIGNAL(emitText(QString)), this, SLOT(receiveText(QString))); + connect(inputField, SIGNAL(cancel()), this, SLOT(abortTextInput())); + s->addWidget(inputField); + + QVBoxLayout* vlayout = new QVBoxLayout(this); + + //Back button + QHBoxLayout* backLayout = new QHBoxLayout(this); + QPushButton* back = new QPushButton("BACK"); + back->setFixedWidth(500); + connect(back, &QPushButton::released, [=](){emit backPress();}); + backLayout->addWidget(back, 0, Qt::AlignLeft); + vlayout->addWidget(layoutToWidget(backLayout, this), 0, Qt::AlignLeft); + + //Enable tethering layout + QHBoxLayout* tetheringToggleLayout = new QHBoxLayout(this); + tetheringToggleLayout->addWidget(new QLabel("Enable tethering", this)); Toggle* toggle_switch = new Toggle(this); toggle_switch->setFixedSize(150, 100); - tethering_field->addWidget(toggle_switch); + tetheringToggleLayout->addWidget(toggle_switch); + tetheringToggleLayout->addSpacing(40); if (wifi->tetheringEnabled()) { toggle_switch->togglePosition(); } QObject::connect(toggle_switch, SIGNAL(stateChanged(int)), this, SLOT(toggleTethering(int))); + vlayout->addWidget(layoutToWidget(tetheringToggleLayout, this), 0); + vlayout->addWidget(hline(this), 0); + + //Change tethering password + QHBoxLayout *tetheringPassword = new QHBoxLayout(this); + tetheringPassword->addWidget(new QLabel("Edit tethering password", this), 1); + editPasswordButton = new QPushButton("EDIT", this); + editPasswordButton->setFixedWidth(500); + connect(editPasswordButton, &QPushButton::released, [=](){inputField->setPromptText("Enter the new hotspot password"); s->setCurrentIndex(0);}); + tetheringPassword->addWidget(editPasswordButton, 1, Qt::AlignRight); + vlayout->addWidget(layoutToWidget(tetheringPassword, this), 0); + vlayout->addWidget(hline(this), 0); + + //IP adress + QHBoxLayout* IPlayout = new QHBoxLayout(this); + IPlayout->addWidget(new QLabel("IP address: "), 0); + ipLabel = new QLabel(wifi->ipv4_address); + ipLabel->setStyleSheet("color: #aaaaaa"); + IPlayout->addWidget(ipLabel, 0, Qt::AlignRight); + vlayout->addWidget(layoutToWidget(IPlayout, this), 0); + vlayout->addWidget(hline(this), 0); + vlayout->addSpacing(300); + + // //Enable SSH + // QHBoxLayout* enableSSHLayout = new QHBoxLayout(this); + // enableSSHLayout->addWidget(new QLabel("Enable SSH", this)); + // Toggle* toggle_switch_SSH = new Toggle(this); + // toggle_switch_SSH->setFixedSize(150, 100); + // enableSSHLayout->addWidget(toggle_switch_SSH); + // vlayout->addWidget(layoutToWidget(enableSSHLayout, this)); + + // //Authorized SSH keys + // QHBoxLayout* authSSHLayout = new QHBoxLayout(this); + // authSSHLayout->addWidget(new QLabel("Authorized SSH keys", this)); + // QPushButton* editAuthSSHButton = new QPushButton("EDIT", this); + // authSSHLayout->addWidget(editAuthSSHButton); + // vlayout->addWidget(layoutToWidget(authSSHLayout, this)); + + // //Disconnect or delete connections + // QHBoxLayout* dangerZone = new QHBoxLayout(this); + // QPushButton* disconnect = new QPushButton("Disconnect from WiFi", this); + // dangerZone->addWidget(disconnect); + // QPushButton* deleteAll = new QPushButton("DELETE ALL NETWORKS", this); + // dangerZone->addWidget(deleteAll); + // vlayout->addWidget(layoutToWidget(dangerZone, this)); + + //vlayout to widget + QWidget* settingsWidget = layoutToWidget(vlayout, this); + settingsWidget->setStyleSheet("margin-left: 40px; margin-right: 40px;"); + s->addWidget(settingsWidget); + s->setCurrentIndex(1); + setLayout(s); + + // Update network status + QTimer* timer = new QTimer(this); + QObject::connect(timer, SIGNAL(timeout()), this, SLOT(refresh())); + timer->start(5000); +} - QWidget* tetheringWidget = new QWidget; - tetheringWidget->setLayout(tethering_field); - tetheringWidget->setFixedHeight(150); - networkLayout->addWidget(tetheringWidget); +void AdvancedNetworking::refresh(){ + ipLabel->setText(wifi->ipv4_address); +} - vlayout = new QVBoxLayout; - wifi_widget->setLayout(vlayout); - networkLayout->addWidget(wifi_widget); +void AdvancedNetworking::toggleTethering(int enable) { + if (enable) { + wifi->enableTethering(); + } else { + wifi->disableTethering(); + } + editPasswordButton->setEnabled(!enable); +} - QWidget* networkWidget = new QWidget; - networkWidget->setLayout(networkLayout); - swidget->addWidget(networkWidget); +void AdvancedNetworking::receiveText(QString text){ + wifi->changeTetheringPassword(text); + s->setCurrentIndex(1); +} - // Keyboard page - input_field = new InputField(); - QObject::connect(input_field, SIGNAL(emitText(QString)), this, SLOT(receiveText(QString))); - swidget->addWidget(input_field); - swidget->setCurrentIndex(0); +void AdvancedNetworking::abortTextInput(){ + s->setCurrentIndex(1); +} - top_layout->addWidget(swidget); - setLayout(top_layout); +// WifiUI functions - // Update network list - timer = new QTimer(this); - QObject::connect(timer, SIGNAL(timeout()), this, SLOT(refresh())); - timer->start(2000); +WifiUI::WifiUI(QWidget *parent, int page_length, WifiManager* wifi) : QWidget(parent), networks_per_page(page_length), wifi(wifi) { + vlayout = new QVBoxLayout; // Scan on startup QLabel *scanning = new QLabel("Scanning for networks"); @@ -90,22 +273,16 @@ WifiUI::WifiUI(QWidget *parent, int page_length) : QWidget(parent), networks_per vlayout->addWidget(scanning, 0, Qt::AlignCenter); vlayout->setSpacing(25); - wifi->request_scan(); - refresh(); + setLayout(vlayout); page = 0; } void WifiUI::refresh() { - if (!this->isVisible()) { - return; - } - wifi->request_scan(); wifi->refreshNetworks(); - ipv4->setText(wifi->ipv4_address); clearLayout(vlayout); - connectButtons = new QButtonGroup(this); + connectButtons = new QButtonGroup(this); // TODO check if this is a leak QObject::connect(connectButtons, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(handleButton(QAbstractButton*))); int i = 0; @@ -116,7 +293,11 @@ void WifiUI::refresh() { if (page * networks_per_page <= i && i < (page + 1) * networks_per_page) { // SSID hlayout->addSpacing(50); - hlayout->addWidget(new QLabel(QString::fromUtf8(network.ssid))); + QString ssid = QString::fromUtf8(network.ssid); + if(ssid.length() > 20){ + ssid = ssid.left(20)+"…"; + } + hlayout->addWidget(new QLabel(ssid)); // strength indicator unsigned int strength_scale = network.strength / 17; @@ -129,7 +310,7 @@ void WifiUI::refresh() { // connect button QPushButton* btn = new QPushButton(network.connected == ConnectedType::CONNECTED ? "Connected" : (network.connected == ConnectedType::CONNECTING ? "Connecting" : "Connect")); - btn->setFixedWidth(300); + btn->setFixedWidth(350); btn->setFixedHeight(button_height); btn->setDisabled(network.connected == ConnectedType::CONNECTED || network.connected == ConnectedType::CONNECTING || network.security_type == SecurityType::UNSUPPORTED); hlayout->addWidget(btn); @@ -140,20 +321,10 @@ void WifiUI::refresh() { QWidget * w = new QWidget; w->setLayout(hlayout); vlayout->addWidget(w); - w->setStyleSheet(R"( - QLabel { - font-size: 50px; - } - QPushButton { - padding: 0; - font-size: 50px; - border-radius: 10px; - background-color: #114265; - } - QPushButton:disabled { - background-color: #323C43; - } - )"); + // Don't add the last line + if (page * networks_per_page <= i+1 && i+1 < (page + 1) * networks_per_page && i+1 < wifi->seen_networks.size()) { + vlayout->addWidget(hline(this)); + } countWidgets++; } i++; @@ -165,12 +336,13 @@ void WifiUI::refresh() { vlayout->addWidget(w); } - QHBoxLayout *prev_next_buttons = new QHBoxLayout; + QHBoxLayout *prev_next_buttons = new QHBoxLayout;//Adding constructor exposes the qt bug QPushButton* prev = new QPushButton("Previous"); prev->setEnabled(page); - prev->setFixedHeight(button_height); + prev->setFixedSize(400, button_height); + QPushButton* next = new QPushButton("Next"); - next->setFixedHeight(button_height); + next->setFixedSize(400, button_height); // If there are more visible networks then we can show, enable going to next page next->setEnabled(wifi->seen_networks.size() > (page + 1) * networks_per_page); @@ -182,74 +354,13 @@ void WifiUI::refresh() { QWidget *w = new QWidget; w->setLayout(prev_next_buttons); - w->setStyleSheet(R"( - QPushButton { - padding: 0; - background-color: #114265; - } - QPushButton:disabled { - background-color: #323C43; - } - )"); vlayout->addWidget(w); } - - -void WifiUI::toggleTethering(int enable) { - if (enable) { - wifi->enableTethering(); - } else { - wifi->disableTethering(); - } -} - void WifiUI::handleButton(QAbstractButton* button) { QPushButton* btn = static_cast(button); Network n = wifi->seen_networks[connectButtons->id(btn)]; - connectToNetwork(n); -} - -void WifiUI::connectToNetwork(Network n) { - timer->stop(); - if (n.security_type == SecurityType::OPEN) { - wifi->connect(n); - } else if (n.security_type == SecurityType::WPA) { - input_field->setPromptText("Enter password for \"" + n.ssid + "\""); - QString password = getStringFromUser(); - if (password.size()) { - wifi->connect(n, password); - } - } - refresh(); - timer->start(); -} - -QString WifiUI::getStringFromUser() { - emit openKeyboard(); - swidget->setCurrentIndex(1); - loop.exec(); - emit closeKeyboard(); - swidget->setCurrentIndex(0); - return text; -} - -void WifiUI::receiveText(QString t) { - loop.quit(); - text = t; -} - - -void WifiUI::wrongPassword(QString ssid) { - if (loop.isRunning()) { - return; - } - for (Network n : wifi->seen_networks) { - if (n.ssid == ssid) { - input_field->setPromptText("Wrong password for \"" + n.ssid +"\""); - connectToNetwork(n); - } - } + emit connectToNetwork(n); } void WifiUI::prevPage() { diff --git a/selfdrive/ui/qt/offroad/wifi.hpp b/selfdrive/ui/qt/offroad/wifi.hpp index bad8d9356c..bbfa3d9dac 100644 --- a/selfdrive/ui/qt/offroad/wifi.hpp +++ b/selfdrive/ui/qt/offroad/wifi.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "wifiManager.hpp" @@ -15,38 +16,82 @@ class WifiUI : public QWidget { public: int page; - explicit WifiUI(QWidget *parent = 0, int page_length = 5); + explicit WifiUI(QWidget *parent = 0, int page_length = 5, WifiManager* wifi = 0); private: WifiManager *wifi = nullptr; - const int networks_per_page; - - QStackedWidget *swidget; + int networks_per_page; QVBoxLayout *vlayout; - QWidget *wifi_widget; - InputField *input_field; - QEventLoop loop; - QTimer *timer; - QString text; QButtonGroup *connectButtons; bool tetheringEnabled; - QLabel *ipv4; +signals: + void openKeyboard(); + void closeKeyboard(); void connectToNetwork(Network n); - QString getStringFromUser(); -private slots: +public slots: void handleButton(QAbstractButton* m_button); - void toggleTethering(int enable); void refresh(); - void receiveText(QString text); - void wrongPassword(QString ssid); void prevPage(); void nextPage(); +}; + +class AdvancedNetworking : public QWidget { + Q_OBJECT +public: + explicit AdvancedNetworking(QWidget* parent = 0, WifiManager* wifi = 0); + QStackedLayout* s; + +private: + InputField* inputField; + QLabel* ipLabel; + QPushButton* editPasswordButton; + + + WifiManager* wifi = nullptr; + +signals: + void openKeyboard(); + void closeKeyboard(); + void backPress(); + +private slots: + void receiveText(QString text); + void abortTextInput(); + void toggleTethering(int enable); + void refresh(); +}; + +class Networking : public QWidget { + Q_OBJECT + +public: + explicit Networking(QWidget* parent = 0); + +private: + QStackedLayout* s;// keyboard, wifiScreen, advanced + + Network selectedNetwork; + + AdvancedNetworking* an; + WifiUI* wifiWidget; + WifiManager* wifi = nullptr; + InputField* inputField; signals: void openKeyboard(); void closeKeyboard(); + +private slots: + void connectToNetwork(Network n); + void refresh(); + void receiveText(QString text); + void abortTextInput(); + void wrongPassword(QString ssid); + void successfulConnection(QString ssid); + void sidebarChange(); }; + diff --git a/selfdrive/ui/qt/offroad/wifiManager.cc b/selfdrive/ui/qt/offroad/wifiManager.cc index 296e93c422..46454470ac 100644 --- a/selfdrive/ui/qt/offroad/wifiManager.cc +++ b/selfdrive/ui/qt/offroad/wifiManager.cc @@ -57,7 +57,7 @@ bool compare_by_strength(const Network &a, const Network &b) { } -WifiManager::WifiManager() { +WifiManager::WifiManager(QWidget* parent) { qDBusRegisterMetaType(); qDBusRegisterMetaType(); connecting_to_network = ""; @@ -96,6 +96,7 @@ void WifiManager::refreshNetworks() { seen_ssids.push_back(network.ssid); seen_networks.push_back(network); } + } QString WifiManager::get_ipv4_address(){ @@ -190,7 +191,7 @@ void WifiManager::connect(Network n, QString password) { void WifiManager::connect(Network n, QString username, QString password) { connecting_to_network = n.ssid; - disconnect(); + // disconnect(); clear_connections(n.ssid); //Clear all connections that may already exist to the network we are connecting connect(n.ssid, username, password, n.security_type); } @@ -216,6 +217,7 @@ void WifiManager::connect(QByteArray ssid, QString username, QString password, S QDBusInterface nm_settings(nm_service, nm_settings_path, nm_settings_iface, bus); nm_settings.call("AddConnection", QVariant::fromValue(connection)); + activate_wifi_connection(QString(ssid)); } void WifiManager::deactivate_connections(QString ssid) { @@ -226,7 +228,7 @@ void WifiManager::deactivate_connections(QString ssid) { QString Ssid = get_property(pth.path(), "Ssid"); if (Ssid == ssid) { QDBusInterface nm2(nm_service, nm_path, nm_iface, bus); - nm2.call("DeactivateConnection", QVariant::fromValue(active_connection_raw)); + nm2.call("DeactivateConnection", QVariant::fromValue(active_connection_raw));// TODO change to disconnect } } } @@ -248,14 +250,7 @@ QVector WifiManager::get_active_connections() { } void WifiManager::clear_connections(QString ssid) { - QDBusInterface nm(nm_service, nm_settings_path, nm_settings_iface, bus); - QDBusMessage response = nm.call("ListConnections"); - QVariant first = response.arguments().at(0); - const QDBusArgument &args = first.value(); - args.beginArray(); - while (!args.atEnd()) { - QDBusObjectPath path; - args >> path; + for(QDBusObjectPath path : list_connections()){ QDBusInterface nm2(nm_service, path.path(), nm_settings_conn_iface, bus); QDBusMessage response = nm2.call("GetSettings"); @@ -336,11 +331,12 @@ QString WifiManager::get_adapter() { return adapter_path; } -void WifiManager::change(unsigned int new_state,unsigned int previous_state,unsigned int change_reason) { +void WifiManager::change(unsigned int new_state, unsigned int previous_state, unsigned int change_reason) { raw_adapter_state = new_state; if (new_state == state_need_auth && change_reason == reason_wrong_password) { emit wrongPassword(connecting_to_network); } else if (new_state == state_connected) { + emit successfulConnection(connecting_to_network); connecting_to_network = ""; } } @@ -352,15 +348,78 @@ void WifiManager::disconnect() { } } +QVector WifiManager::list_connections(){ + QVector connections; + QDBusInterface nm(nm_service, nm_settings_path, nm_settings_iface, bus); + QDBusMessage response = nm.call("ListConnections"); + QVariant first = response.arguments().at(0); + const QDBusArgument &args = first.value(); + args.beginArray(); + while (!args.atEnd()) { + QDBusObjectPath path; + args >> path; + connections.push_back(path); + } + return connections; +} +bool WifiManager::activate_wifi_connection(QString ssid){ + QString devicePath = get_adapter(); + + for(QDBusObjectPath path : list_connections()){ + QDBusInterface nm2(nm_service, path.path(), nm_settings_conn_iface, bus); + QDBusMessage response = nm2.call("GetSettings"); + const QDBusArgument &dbusArg = response.arguments().at(0).value(); + + QMap> map; + dbusArg >> map; + for (auto &inner : map) { + for (auto &val : inner) { + QString key = inner.key(val); + if (key == "ssid") { + if (val == ssid) { + QDBusInterface nm3(nm_service, nm_path, nm_iface, bus); + nm3.call("ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(devicePath)), QVariant::fromValue(QDBusObjectPath("/"))); + return true; + } + } + } + } + } + return false; +} //Functions for tethering +bool WifiManager::activate_tethering_connection(){ + QString devicePath = get_adapter(); -void WifiManager::enableTethering() { - disconnect(); + for(QDBusObjectPath path : list_connections()){ + QDBusInterface nm2(nm_service, path.path(), nm_settings_conn_iface, bus); + QDBusMessage response = nm2.call("GetSettings"); + const QDBusArgument &dbusArg = response.arguments().at(0).value(); + + QMap> map; + dbusArg >> map; + for (auto &inner : map) { + for (auto &val : inner) { + QString key = inner.key(val); + if (key == "ssid") { + if (val == tethering_ssid.toUtf8()) { + QDBusInterface nm3(nm_service, nm_path, nm_iface, bus); + nm3.call("ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(devicePath)), QVariant::fromValue(QDBusObjectPath("/"))); + return true; + } + } + } + } + } + return false; +} +void WifiManager::addTetheringConnection(){ Connection connection; connection["connection"]["id"] = "Hotspot"; connection["connection"]["uuid"] = QUuid::createUuid().toString().remove('{').remove('}'); connection["connection"]["type"] = "802-11-wireless"; connection["connection"]["interface-name"] = "wlan0"; + connection["connection"]["autoconnect"] = false; connection["802-11-wireless"]["band"] = "bg"; connection["802-11-wireless"]["mode"] = "ap"; @@ -370,7 +429,7 @@ void WifiManager::enableTethering() { connection["802-11-wireless-security"]["key-mgmt"] = "wpa-psk"; connection["802-11-wireless-security"]["pairwise"] = QStringList("ccmp"); connection["802-11-wireless-security"]["proto"] = QStringList("rsn"); - connection["802-11-wireless-security"]["psk"] = "swagswagcomma"; + connection["802-11-wireless-security"]["psk"] = tetheringPassword; connection["ipv4"]["method"] = "shared"; QMap address; @@ -382,14 +441,27 @@ void WifiManager::enableTethering() { QDBusInterface nm_settings(nm_service, nm_settings_path, nm_settings_iface, bus); nm_settings.call("AddConnection", QVariant::fromValue(connection)); +} +void WifiManager::enableTethering() { + if(activate_tethering_connection()){ + return; + } + addTetheringConnection(); + activate_tethering_connection(); } void WifiManager::disableTethering() { - clear_connections(tethering_ssid); + deactivate_connections(tethering_ssid.toUtf8()); } bool WifiManager::tetheringEnabled() { QString active_ap = get_active_ap(); return get_property(active_ap, "Ssid") == tethering_ssid; } + +void WifiManager::changeTetheringPassword(QString newPassword){ + tetheringPassword = newPassword; + clear_connections(tethering_ssid.toUtf8()); + addTetheringConnection(); +} \ No newline at end of file diff --git a/selfdrive/ui/qt/offroad/wifiManager.hpp b/selfdrive/ui/qt/offroad/wifiManager.hpp index 13eb90f3fb..bbbe2e3ed8 100644 --- a/selfdrive/ui/qt/offroad/wifiManager.hpp +++ b/selfdrive/ui/qt/offroad/wifiManager.hpp @@ -28,7 +28,7 @@ struct Network { class WifiManager : public QWidget { Q_OBJECT public: - explicit WifiManager(); + explicit WifiManager(QWidget* parent); void request_scan(); QVector seen_networks; @@ -38,12 +38,18 @@ public: void connect(Network ssid); void connect(Network ssid, QString password); void connect(Network ssid, QString username, QString password); - // Tethering functions + void disconnect(); + // Tethering functions void enableTethering(); void disableTethering(); bool tetheringEnabled(); + bool activate_tethering_connection(); + void addTetheringConnection(); + bool activate_wifi_connection(QString ssid); + void changeTetheringPassword(QString newPassword); + private: QVector seen_ssids; QString adapter;//Path to network manager wifi-device @@ -51,6 +57,7 @@ private: unsigned int raw_adapter_state;//Connection status https://developer.gnome.org/NetworkManager/1.26/nm-dbus-types.html#NMDeviceState QString connecting_to_network; QString tethering_ssid; + QString tetheringPassword = "swagswagcommma"; QString get_adapter(); QString get_ipv4_address(); @@ -64,11 +71,12 @@ private: QByteArray get_property(QString network_path, QString property); unsigned int get_ap_strength(QString network_path); SecurityType getSecurityType(QString ssid); - void disconnect(); + QVector list_connections(); private slots: void change(unsigned int new_state, unsigned int previous_state, unsigned int change_reason); signals: void wrongPassword(QString ssid); + void successfulConnection(QString ssid); void refresh(); }; diff --git a/selfdrive/ui/qt/widgets/input_field.cc b/selfdrive/ui/qt/widgets/input_field.cc index 858c1c75a2..d7f9613a12 100644 --- a/selfdrive/ui/qt/widgets/input_field.cc +++ b/selfdrive/ui/qt/widgets/input_field.cc @@ -2,7 +2,7 @@ #include "input_field.hpp" -InputField::InputField(QWidget *parent): QWidget(parent) { +InputField::InputField(QWidget *parent, int minTextLength): QWidget(parent), minTextLength(minTextLength) { layout = new QGridLayout(); layout->setSpacing(30); @@ -39,8 +39,8 @@ void InputField::setPromptText(QString text) { } void InputField::emitEmpty() { - emitText(""); line->setText(""); + emit cancel(); } void InputField::getText(QString s) { @@ -49,6 +49,10 @@ void InputField::getText(QString s) { } if (!QString::compare(s,"⏎")) { + if(line->text().length()text()); line->setText(""); } diff --git a/selfdrive/ui/qt/widgets/input_field.hpp b/selfdrive/ui/qt/widgets/input_field.hpp index 8dad0c0dda..6f2c69a578 100644 --- a/selfdrive/ui/qt/widgets/input_field.hpp +++ b/selfdrive/ui/qt/widgets/input_field.hpp @@ -12,8 +12,9 @@ class InputField : public QWidget { Q_OBJECT public: - explicit InputField(QWidget* parent = 0); + explicit InputField(QWidget* parent = 0, int minTextLength = 0); void setPromptText(QString text); + int minTextLength; private: QLineEdit *line; @@ -22,9 +23,10 @@ private: QGridLayout *layout; public slots: - void emitEmpty(); void getText(QString s); + void emitEmpty(); signals: + void cancel(); void emitText(QString s); };