From b1529299e4f37774c72da31bd70b943e86e985ba Mon Sep 17 00:00:00 2001 From: Erin Harrington Date: Thu, 17 Nov 2022 16:24:10 -0600 Subject: [PATCH] Implemented basic UI interaction for Client Mode, AP mode (both already configured and not already configured), and Wifi Off mode --- .vscode/c_cpp_properties.json | 17 ++++++++ .vscode/settings.json | 7 ++++ include/botui/Network.h | 41 ++++++++++---------- src/Network.cpp | 48 +++++++++++++---------- src/NetworkManager.cpp | 73 ++++++++++++++++------------------- src/NetworkSettingsWidget.cpp | 54 +++++++++++++------------- 6 files changed, 133 insertions(+), 107 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 00000000..4f31303e --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include/x86_64-linux-gnu/qt6/QtDBus" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu17", + "cppStandard": "gnu++17", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..d9a640a1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "C_Cpp.errorSquiggles": "Disabled", + "files.associations": { + "chrono": "cpp", + "variant": "cpp" + } +} \ No newline at end of file diff --git a/include/botui/Network.h b/include/botui/Network.h index 5a5cae18..600a3580 100644 --- a/include/botui/Network.h +++ b/include/botui/Network.h @@ -7,44 +7,46 @@ class Network { public: - enum Security { + enum Security + { None = 0, Wep, DynamicWep, Wpa, WpaEnterprise }; - - enum Mode { + + enum Mode + { Unknown = 0, AdHoc, Infrastructure, AP }; - + Network(); - void setAPPath(const QString& path); - const QString& apPath() const; - - void setSsid(const QString& ssid); - const QString& ssid() const; - - void setSecurity(const Security& security); - const Security& security() const; + void setAPPath(const QString &path); + const QString &apPath() const; + + void setSsid(const QString &ssid); + const QString &ssid() const; + + void setSecurity(const Security &security); + const Security &security() const; QString securityString() const; - - void setPassword(const QString& password); - const QString& password() const; - + + void setPassword(const QString &password); + const QString &password() const; + void setMode(const Mode &mode); const Mode &mode() const; - + void setStrength(const double &strength); const double &strength() const; - + bool isComplete() const; - + private: QString m_ssid; Security m_security; @@ -60,5 +62,4 @@ bool operator==(const Network &lhs, const Network &rhs); QDebug operator<<(QDebug dbg, const Network &network); - #endif diff --git a/src/Network.cpp b/src/Network.cpp index a348e97e..ba66074c 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -4,57 +4,62 @@ Network::Network() { - } -void Network::setAPPath(const QString& path) +void Network::setAPPath(const QString &path) { m_apPath = path; } -const QString& Network::apPath() const +const QString &Network::apPath() const { return m_apPath; } -void Network::setSsid(const QString& ssid) +void Network::setSsid(const QString &ssid) { m_ssid = ssid; } -const QString& Network::ssid() const +const QString &Network::ssid() const { return m_ssid; } -void Network::setSecurity(const Network::Security& security) +void Network::setSecurity(const Network::Security &security) { m_security = security; } -const Network::Security& Network::security() const +const Network::Security &Network::security() const { return m_security; } QString Network::securityString() const { - switch(m_security) { - case Network::None: return "None"; - case Network::Wep: return "WEP"; - case Network::DynamicWep: return "Dynamic WEP"; - case Network::Wpa: return "WPA"; - case Network::WpaEnterprise: return "WPA Enterprise"; + switch (m_security) + { + case Network::None: + return "None"; + case Network::Wep: + return "WEP"; + case Network::DynamicWep: + return "Dynamic WEP"; + case Network::Wpa: + return "WPA"; + case Network::WpaEnterprise: + return "WPA Enterprise"; } return "Unknown"; } -void Network::setPassword(const QString& password) +void Network::setPassword(const QString &password) { m_password = password; } -const QString& Network::password() const +const QString &Network::password() const { return m_password; } @@ -81,7 +86,8 @@ const double &Network::strength() const bool Network::isComplete() const { - if(m_security == Network::None) return true; + if (m_security == Network::None) + return true; return !m_password.isEmpty(); } @@ -93,10 +99,12 @@ bool operator==(const Network &lhs, const Network &rhs) QDebug operator<<(QDebug dbg, const Network &network) { dbg.nospace() << network.ssid() << "(" - << (network.mode() == Network::AP ? "ap" : "infrastructure") - << ")" << " with "; - - switch(network.security()) { + << (network.mode() == Network::AP ? "ap" : "infrastructure") + << ")" + << " with "; + + switch (network.security()) + { case Network::None: dbg.nospace() << "no security"; return dbg.space(); diff --git a/src/NetworkManager.cpp b/src/NetworkManager.cpp index 9c68ddb5..110841d4 100644 --- a/src/NetworkManager.cpp +++ b/src/NetworkManager.cpp @@ -177,29 +177,30 @@ void NetworkManager::addNetwork(const Network &network) { APExist = true; apPathConn = connectionPath; - APuuid = detail["connection"]["uuid"].toString(); + //APuuid = detail["connection"]["uuid"].toString(); qDebug() << "AP Path Connection already exists"; break; } } - qDebug() << "APExist = " << APExist; - qDebug() << "UUID = " << APuuid; + apCon = createAPConfig(); + if (APExist == false) { // if AP Config in Settings doesn't exist already - qDebug() << "AP Connection Config: " << apCon; - QDBusObjectPath apPathConn = settings.AddConnection(apCon); + if (m_device->activeConnection().path() != "") + { + QDBusObjectPath curCon = m_device->activeConnection(); + m_nm->DeactivateConnection(curCon); + } - qDebug() << "AP Path" << apPathConn.path(); - m_nm->ActivateConnection(apPathConn, devicePath, QDBusObjectPath("/")); + apPathConn = settings.AddConnection(apCon); // auto activates + + QDBusObjectPath activeAccess = m_wifi->activeAccessPoint(); // active AP connection } else { // if AP Config in Settings DOES exist already - // apPathConn = settings.GetConnectionByUuid(APuuid); - qDebug() << "AP Path" << apPathConn.path(); - // apPathConn = settings.AddConnection(apCon); - m_nm->ActivateConnection(settings.GetConnectionByUuid(APuuid), devicePath, QDBusObjectPath("/")); + m_nm->ActivateConnection(apPathConn, devicePath, QDBusObjectPath("/")); } // return; @@ -255,7 +256,7 @@ NetworkList NetworkManager::networks() const QDBusConnection::systemBus()); Connection details = conn.GetSettings().value(); - qDebug() << details; + // This connection is not a wifi one. Skip. if (!details.contains("802-11-wireless")) continue; @@ -306,7 +307,20 @@ bool NetworkManager::enableAP() int ret = system("sudo /usr/bin/python /usr/bin/wifi_configurator.py &"); return (ret == 0); #endif + Connection defaultAPConfig = createAPConfig(); + Network APN = networkFromConnection(defaultAPConfig); + addNetwork(APN); + return true; +} +bool NetworkManager::disableAP() +{ +#ifdef WALLABY + int ret = system("sudo ifconfig wlan0 down"); + return (ret == 0); +#endif + QDBusObjectPath curCon = m_device->activeConnection(); + m_nm->DeactivateConnection(curCon); return true; } @@ -320,8 +334,8 @@ Connection NetworkManager::createAPConfig() const apConfig["connection"]["uuid"] = QUuid::createUuid().toString().remove('{').remove('}'); // File name is just the SSID for now apConfig["connection"]["id"] = "APName"; - apConfig["connection"]["autoconnect"] = false; - + // apConfig["connection"]["autoconnect"] = false; + apConfig["connection"]["interface-name"] = "wlo1"; // SSID QByteArray tempCont; QStringList qSList = {"ccmp"}; @@ -345,20 +359,10 @@ Connection NetworkManager::createAPConfig() const // // WEP uses this key // apConfig[NM_802_11_SECURITY_KEY]["password"] = "kipr4609"; // // WPA uses this one - // apConfig[NM_802_11_SECURITY_KEY]["psk"] = "kipr4609"; - - // // Finally, tell our configuration about the security + apConfig[NM_802_11_SECURITY_KEY]["psk"] = "kipr4609"; return apConfig; } -bool NetworkManager::disableAP() -{ -#ifdef WALLABY - int ret = system("sudo ifconfig wlan0 down"); - return (ret == 0); -#endif - return true; -} bool NetworkManager::isOn() const { @@ -390,13 +394,12 @@ NetworkList NetworkManager::accessPoints() const { if (!m_wifi) return NetworkList(); - QList aps = m_wifi->GetAccessPoints(); + QList aps = m_wifi->GetAllAccessPoints(); NetworkList networks; QList netList; QList netSSID; foreach (const QDBusObjectPath &ap, aps) { - // qDebug() << ap.path(); Network newNet = createAccessPoint(ap); netList << newNet; } @@ -448,10 +451,6 @@ NetworkManager::NetworkManager() NM_OBJECT "/Settings", QDBusConnection::systemBus()); - Connection defaultAPConfig = createAPConfig(); - Network APN = networkFromConnection(defaultAPConfig); - addNetwork(APN); - // enableAP(); QDBusPendingReply> reply = m_nm->GetDevices(); if (reply.isError()) @@ -508,16 +507,12 @@ NetworkManager::NetworkManager() requestScan(); - foreach (const Network &nw, accessPoints()) - { - qDebug() << nw; - } + // foreach (const Network &nw, accessPoints()) + // { + // qDebug() << nw; + // } } -// void NetworkManager::connectToWifi() -// { - -// } void NetworkManager::nmAccessPointAdded(const QDBusObjectPath &accessPoint) { Network network = createAccessPoint(accessPoint); diff --git a/src/NetworkSettingsWidget.cpp b/src/NetworkSettingsWidget.cpp index 630cac44..7d2207a0 100644 --- a/src/NetworkSettingsWidget.cpp +++ b/src/NetworkSettingsWidget.cpp @@ -20,8 +20,7 @@ #include NetworkSettingsWidget::NetworkSettingsWidget(Device *device, QWidget *parent) - : StandardWidget(device, parent) - , ui(new Ui::NetworkSettingsWidget) + : StandardWidget(device, parent), ui(new Ui::NetworkSettingsWidget) { ui->setupUi(this); performStandardSetup(tr("Network Settings")); @@ -29,20 +28,13 @@ NetworkSettingsWidget::NetworkSettingsWidget(Device *device, QWidget *parent) enableCoolOffTimer = new QTimer(this); enableCoolOffTimer->setSingleShot(true); QObject::connect(enableCoolOffTimer, SIGNAL(timeout()), SLOT(enableAPControls())); - + // ui->turnOn->setVisible(false); // ui->turnOff->setVisible(false); ui->connect->setEnabled(false); QObject::connect(ui->connect, SIGNAL(clicked()), SLOT(connect())); QObject::connect(ui->manage, SIGNAL(clicked()), SLOT(manage())); - //QObject::connect(ui->turnOn, SIGNAL(clicked()), SLOT(disableAPControlsTemporarily())); - //QObject::connect(ui->turnOff, SIGNAL(clicked()), SLOT(disableAPControlsTemporarily())); - //NetworkManager::ref().connect(ui->turnOn, SIGNAL(clicked()), SLOT(enableAP())); //SLOT(turnOn())); - //NetworkManager::ref().connect(ui->turnOff, SIGNAL(clicked()), SLOT(disableAP())); //SLOT(turnOff())); - - // QObject::connect(ui->turnOn, SIGNAL(clicked()), SLOT(enableAP())); - // QObject::connect(ui->turnOff, SIGNAL(clicked()), SLOT(disableAP())); QObject::connect(ui->tournamentMode, SIGNAL(clicked()), SLOT(TournamentMode())); NetworkManager::ref().connect(ui->connectionMode, SIGNAL(currentIndexChanged(int)), this, SLOT(indexChanged(int))); @@ -51,11 +43,11 @@ NetworkSettingsWidget::NetworkSettingsWidget(Device *device, QWidget *parent) ui->manage->setVisible(true); ui->security->setVisible(false); ui->securityLabel->setVisible(false); - + QObject::connect(&NetworkManager::ref(), - SIGNAL(stateChanged(const NetworkManager::State &, const NetworkManager::State &)), - SLOT(stateChanged(const NetworkManager::State &, const NetworkManager::State &))); - + SIGNAL(stateChanged(const NetworkManager::State &, const NetworkManager::State &)), + SLOT(stateChanged(const NetworkManager::State &, const NetworkManager::State &))); + QTimer *updateTimer = new QTimer(this); QObject::connect(updateTimer, SIGNAL(timeout()), SLOT(updateInformation())); updateTimer->start(10000); @@ -73,15 +65,23 @@ void NetworkSettingsWidget::TournamentMode() void NetworkSettingsWidget::indexChanged(int index) { - - - if(index == 1)//AP mode + + if (index == 1) // AP mode { ui->connect->setEnabled(false); NetworkManager::ref().enableAP(); - } - else if(index == 2){NetworkManager::ref().turnOn();ui->connect->setEnabled(true);} //Wifi on (client mode) - else if(index == 3){NetworkManager::ref().turnOff();ui->connect->setEnabled(false);} //Wifi off + } + else if (index == 2) // Wifi on (client mode) + { + NetworkManager::ref().disableAP(); + NetworkManager::ref().turnOn(); + ui->connect->setEnabled(true); + } + else if (index == 3) // Wifi off + { + NetworkManager::ref().turnOff(); + ui->connect->setEnabled(false); + } } NetworkSettingsWidget::~NetworkSettingsWidget() @@ -90,12 +90,12 @@ NetworkSettingsWidget::~NetworkSettingsWidget() delete enableCoolOffTimer; } -void NetworkSettingsWidget::connect() //Connects to network +void NetworkSettingsWidget::connect() // Connects to network { RootController::ref().presentWidget(new ConnectWidget(device())); } -void NetworkSettingsWidget::manage() //Forget or add network to history +void NetworkSettingsWidget::manage() // Forget or add network to history { RootController::ref().presentWidget(new ManageNetworksWidget(device())); } @@ -141,20 +141,18 @@ void NetworkSettingsWidget::updateInformation() const bool on = NetworkManager::ref().isOn(); // ui->state->setText(on ? tr("ON") : tr("OFF")); - - - const QString id = device()->id(); const QString serial = device()->serial(); - if(!id.isEmpty()) { + if (!id.isEmpty()) + { const QString password = SystemUtils::sha256(id).left(6) + "00"; - const QString ssid = serial + "-wombat"; + const QString ssid = serial + "-wombat"; ui->ssid->setText(ssid); ui->password->setText(password); } Network active = NetworkManager::ref().active(); - //ui->ssid->setText(active.ssid()); + // ui->ssid->setText(active.ssid()); ui->security->setText(active.securityString()); const QString ip = NetworkManager::ref().ipAddress(); ui->ip->setText(ip.isEmpty() ? tr("No IP") : ip);