From a653d9b572d0013062b2d8611818c172f142c6a2 Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 25 Nov 2024 15:20:31 -0600 Subject: [PATCH] Fixed bug --- src/NetworkManager.cpp | 132 +++++++---------------------------------- 1 file changed, 21 insertions(+), 111 deletions(-) diff --git a/src/NetworkManager.cpp b/src/NetworkManager.cpp index ec2f0ce5..cd694f37 100644 --- a/src/NetworkManager.cpp +++ b/src/NetworkManager.cpp @@ -47,26 +47,23 @@ QDBusObjectPath AP_PATH; Connection DEFAULT_AP; QString RASPBERRYPI_TYPE; #ifdef WOMBAT -#define WIFI_DEVICE "wlan0" // always wlan0 for raspberry pi -#else -#define WIFI_DEVICE "wlo1" // wlo1 for dev machine -#endif + #define WIFI_DEVICE "wlan0" // always wlan0 for raspberry pi + #else + #define WIFI_DEVICE "wlo1" // wlo1 for dev machine + #endif #define AP_NAME m_dev->serial() + "-wombat" #define AP_SSID (AP_NAME).toUtf8() #define AP_PASSWORD SystemUtils::sha256(m_dev->id()).left(6) + "00" -#define NETWORK_MANAGER_GROUP "NetworkManager" -#define ON_KEY "on" - -#define NM_802_11_WIRELESS_KEY ("802-11-wireless") -#define NM_802_11_SECURITY_KEY ("802-11-wireless-security") - NetworkManager::~NetworkManager() { } +#define NM_802_11_WIRELESS_KEY ("802-11-wireless") +#define NM_802_11_SECURITY_KEY ("802-11-wireless-security") + OrgFreedesktopNetworkManagerInterface *NetworkManager::networkManagerInterface() { static OrgFreedesktopNetworkManagerInterface nmInterface( @@ -77,7 +74,6 @@ OrgFreedesktopNetworkManagerInterface *NetworkManager::networkManagerInterface() return &nmInterface; } -// Adds a new connection to the NetworkManager void NetworkManager::addNetwork(const Network &network) { // Yes, yes... this is a hard coded mess. @@ -153,7 +149,6 @@ void NetworkManager::addNetwork(const Network &network) emit networkAdded(network); } -// Forgets the given network void NetworkManager::forgetNetwork(const Network &network) { Q_FOREACH (const QDBusObjectPath &connectionPath, getAllConnectionPaths()) @@ -178,13 +173,11 @@ void NetworkManager::forgetNetwork(const Network &network) emit networkForgotten(network); } -// Get the name of the AP connection QString NetworkManager::getAPName() { return AP_NAME; } -// Returns list of all networks NetworkList NetworkManager::networks() const { NetworkList networks; @@ -197,7 +190,6 @@ NetworkList NetworkManager::networks() const return networks; } -// Scans for available networks void NetworkManager::requestScan() { if (!m_wifi) @@ -210,8 +202,9 @@ void NetworkManager::requestScan() qWarning() << "NetworkManager::requestScan" << reply.error().message(); } +#define NETWORK_MANAGER_GROUP "NetworkManager" +#define ON_KEY "on" -// Turns on the network manager bool NetworkManager::turnOn() { m_nm->Enable(true); @@ -222,7 +215,6 @@ bool NetworkManager::turnOn() return true; // TODO: This is a bad assumption } -// Turns off the network manager void NetworkManager::turnOff() { m_nm->Enable(false); @@ -232,7 +224,6 @@ void NetworkManager::turnOff() settings.endGroup(); } -// Returns the AP connection configuration QString NetworkManager::getAPConnectionConfig() { QString command = QString("cat /etc/NetworkManager/system-connections/%1.nmconnection").arg(AP_NAME); @@ -245,7 +236,6 @@ QString NetworkManager::getAPConnectionConfig() return QString(output).trimmed(); } -// Changes the WiFi band and channel void NetworkManager::changeWifiBands(QString band, int channel) { @@ -272,8 +262,6 @@ void NetworkManager::changeWifiBands(QString band, int channel) QList activeConnections = nm.activeConnections(); QDBusObjectPath correctConnectionPath; - - // Loop through active connection paths to find the correct one foreach (const QDBusObjectPath &activeConnectionPath, activeConnections) { OrgFreedesktopNetworkManagerConnectionActiveInterface activeConn(NM_SERVICE, activeConnectionPath.path(), QDBusConnection::systemBus()); @@ -287,7 +275,6 @@ void NetworkManager::changeWifiBands(QString band, int channel) qDebug() << "Correct connection ssid " << connectionSettings[NM_802_11_WIRELESS_KEY]["ssid"].toString(); QPair correctConnectionPair = getConnection(connectionSettings[NM_802_11_WIRELESS_KEY]["ssid"].toString()); - // Activate found connection QDBusPendingReply reply = m_nm->ActivateConnection(correctConnectionPair.second, devicePath, QDBusObjectPath("/")); reply.waitForFinished(); getReply(reply); @@ -318,7 +305,6 @@ void NetworkManager::changeWifiBands(QString band, int channel) emit stateChangedBandBouncer(oldBand, newBand, oldChannel, newChannel); }); } -// Enables AP connection bool NetworkManager::enableAP() { @@ -337,22 +323,27 @@ bool NetworkManager::enableAP() qDebug() << "AP Path: " << apPath.path(); qDebug() << "AP Path Connection already exists"; qDebug() << "AP Strength: " << active().strength(); + qDebug() << "State: " << m_device->state(); OrgFreedesktopNetworkManagerSettingsConnectionInterface connection(NM_SERVICE, apPath.path(), QDBusConnection::systemBus()); Connection settings = connection.GetSettings().value(); QMap wirelessSettings = settings.value("802-11-wireless"); + qDebug() << "Settings before anything: " << settings; bool containsBand = wirelessSettings.contains("band"); bool containsChannel = wirelessSettings.contains("channel"); - // Check if the current settings contain "band" and "channel" + qDebug() << "AP settings: " << settings; + qDebug() << "Contains band? : " << containsBand; + qDebug() << "Contains channel? : " << containsChannel; + + // Check if the settings contain "band" and "channel" if (!containsBand || !containsChannel) { qDebug() << "Missing 'band' or 'channel' in AP settings. Recreating AP configuration."; bool activeConnectionOn = NetworkManager::ref().isActiveConnectionOn(); - // If there is a current connection active, deactivate it if (activeConnectionOn == true) { qDebug() << "Current active connection: " << m_device->activeConnection().path(); @@ -370,7 +361,6 @@ bool NetworkManager::enableAP() } } - // Assign the default band and channel based on the Raspberry Pi type if (RASPBERRYPI_TYPE == "3B+") { settings[NM_802_11_WIRELESS_KEY]["band"] = "a"; @@ -384,7 +374,6 @@ bool NetworkManager::enableAP() qDebug() << "Modified AP settings: band and channel added."; - // Update the connection settings QDBusPendingReply reply = connection.Update(settings); reply.waitForFinished(); @@ -400,7 +389,6 @@ bool NetworkManager::enableAP() qDebug() << "Connection after update: " << connection.GetSettings().value(); } - // Activate updated connection QDBusPendingReply activateReply = m_nm->ActivateConnection(apPath, devicePath, QDBusObjectPath("/")); activateReply.waitForFinished(); @@ -413,7 +401,7 @@ bool NetworkManager::enableAP() { qDebug() << "Connection activated successfully."; } - + qDebug() << "Device is now active. Proceeding to reapply settings."; QDBusPendingReply reapplyReply = m_device->Reapply(settings, 0, 0); @@ -438,16 +426,14 @@ bool NetworkManager::enableAP() } else if (NetworkManager::ref().isActiveConnectionOn() == false) { - + turnOn(); uint stateReply; while (true) { stateReply = m_device->state(); - if (stateReply == 30)NetworkManager::~NetworkManager() -{ -} + if (stateReply == 30) { qDebug() << "Reached Disconnected state"; break; @@ -470,7 +456,6 @@ bool NetworkManager::enableAP() return true; } -// Returns AP settings object path QDBusObjectPath NetworkManager::getAPSettingsObjectPath() const { QDBusObjectPath settingsPath = getConnection(AP_NAME).second; @@ -478,7 +463,6 @@ QDBusObjectPath NetworkManager::getAPSettingsObjectPath() const return settingsPath; } -// Turns off the AP connection void NetworkManager::deactivateAP() { qDebug() << "inside deactivateAP()"; @@ -489,7 +473,6 @@ void NetworkManager::deactivateAP() m_nm->DeactivateConnection(activePath); } -// Deletes the AP connection bool NetworkManager::disableAP() { foreach (const QDBusObjectPath &settingPath, getAllConnectionPaths()) @@ -510,8 +493,7 @@ bool NetworkManager::disableAP() return true; } -// Creates a default AP_SSID configuration for settings -Connection NetworkManager::createAPConfig() const +Connection NetworkManager::createAPConfig() const // Creates a default AP_SSID configuration for settings { qDebug() << "Creating AP Config..."; @@ -535,7 +517,6 @@ Connection NetworkManager::createAPConfig() const DEFAULT_AP["ipv4"]["method"] = "shared"; DEFAULT_AP["ipv6"]["method"] = "auto"; - // Assign the default band and channel based on the Raspberry Pi type if (RASPBERRYPI_TYPE == "3B+") { DEFAULT_AP[NM_802_11_WIRELESS_KEY]["band"] = "a"; @@ -554,7 +535,6 @@ Connection NetworkManager::createAPConfig() const QDBusObjectPath defaultPath = settings.AddConnection(DEFAULT_AP); qDebug() << "settings.AddConnection(DEFAULT_AP): " << defaultPath.path(); - // Hardcode WiFi IP address QString str = tr("nmcli connection modify %1 ipv4.address 192.168.125.1/24").arg(AP_NAME); QByteArray ba = str.toLocal8Bit(); const char *csys = ba.data(); @@ -563,13 +543,11 @@ Connection NetworkManager::createAPConfig() const return DEFAULT_AP; } -// Returns if the network manager is on bool NetworkManager::isOn() const { return m_nm->networkingEnabled(); } -// Returns if the network manager is persistent bool NetworkManager::isPersistentOn() const { QSettings settings; @@ -579,13 +557,11 @@ bool NetworkManager::isPersistentOn() const return ret; } -// Returns the current state of the network manager NetworkManager::State NetworkManager::state() const { return (NetworkManager::State)m_device->state(); } - Network NetworkManager::active() const { if (!m_wifi) @@ -593,7 +569,6 @@ Network NetworkManager::active() const return createAccessPoint(m_wifi->activeAccessPoint()); } -// Returns active connection password QString NetworkManager::activeConnectionPassword() const { if (!isActiveConnectionOn()) @@ -603,7 +578,6 @@ QString NetworkManager::activeConnectionPassword() const return getPassword(active().ssid()); } -// Returns if the active connection is on bool NetworkManager::isActiveConnectionOn() const { bool activeConnOn; @@ -618,7 +592,6 @@ bool NetworkManager::isActiveConnectionOn() const return activeConnOn; } -// Returns the current active connection name QString NetworkManager::currentActiveConnectionName() const { QDBusObjectPath activePath = m_device->activeConnection(); // Device's current active connection @@ -631,7 +604,6 @@ QString NetworkManager::currentActiveConnectionName() const return activeConnObj.id(); } -// Returns NetworkList of all access points NetworkList NetworkManager::accessPoints() const { if (!m_wifi) @@ -662,7 +634,6 @@ NetworkList NetworkManager::accessPoints() const return networks; } -// Returns network IP address QString NetworkManager::ipAddress() const { int fd = socket(AF_INET, SOCK_DGRAM, 0); @@ -678,7 +649,6 @@ QString NetworkManager::ipAddress() const return ret; } -// Returns if event mode is on or off bool NetworkManager::eventModeState() { QProcess eventModeProcess; @@ -707,7 +677,6 @@ bool NetworkManager::eventModeState() } } -// Returns the network's IPv4 address QString NetworkManager::ip4Address() const { QString ipAddr; @@ -725,31 +694,15 @@ QString NetworkManager::ip4Address() const return ipAddr; } -// Initilization function void NetworkManager::init(const Device *device) { - qDebug() << "Init"; m_dev = device; bool checkEventState = eventModeState(); - int checkNetworkMode = getBootUpNetworkMode(); - if (!checkEventState && (checkNetworkMode != 1 && checkNetworkMode != 2)) // Event Mode enabled + if (!checkEventState) // Event Mode enabled { - enableAP(); } - else if (checkNetworkMode == 1) // Client Mode - { - - if(currentActiveConnectionName() == AP_NAME) - { - disableAP(); - } - } - else if (checkNetworkMode == 2) //Wifi off - { - turnOff(); - } } NetworkManager::NetworkManager() @@ -821,43 +774,9 @@ NetworkManager::NetworkManager() turnOn(); requestScan(); - qDebug() << "Active strength: " << active().strength(); - -} - -// Returns the network mode on boot up -int NetworkManager::getBootUpNetworkMode() -{ - QProcess networkModeProcess; - QString command = "grep '^MODE' /home/kipr/wombat-os/configFiles/wifiConnectionMode.txt | awk '{print $2}'"; - - networkModeProcess.start("bash", QStringList() << "-c" << command); - networkModeProcess.waitForFinished(); - - QString output = networkModeProcess.readAllStandardOutput().trimmed(); - qDebug() << "getBootUpNetworkMode: " << output.toInt(); - if (!output.isEmpty()) - { - switch (output.toInt()) - { - case 0: // AP Mode - return 0; - case 1: // Client Mode - return 1; - case 2: // Wifi Off - return 2; - default: - qDebug() << "Unknown MODE."; - } - } - else - { - qDebug() << "Failed to read MODE."; - } } -// Returns the Raspberry Pi type inside Wombat void NetworkManager::getRaspberryPiType() { QStringList arguments; @@ -885,7 +804,6 @@ void NetworkManager::getRaspberryPiType() qDebug() << "RASPBERRYPI_TYPE: " << RASPBERRYPI_TYPE; } -// NetworkManager AP added signal void NetworkManager::nmAccessPointAdded(const QDBusObjectPath &accessPoint) { Network network = createAccessPoint(accessPoint); @@ -894,7 +812,6 @@ void NetworkManager::nmAccessPointAdded(const QDBusObjectPath &accessPoint) emit accessPointAdded(network); } -// NetworkManager AP removed signal void NetworkManager::nmAccessPointRemoved(const QDBusObjectPath &accessPoint) { Network network = createAccessPoint(accessPoint); @@ -903,7 +820,6 @@ void NetworkManager::nmAccessPointRemoved(const QDBusObjectPath &accessPoint) m_accessPoints.removeAll(network); } -// Signal for when the network state changes void NetworkManager::stateChangedBouncer(uint newState, uint oldState) { NetworkManager::State networkStateNew = (NetworkManager::State)newState; @@ -911,7 +827,6 @@ void NetworkManager::stateChangedBouncer(uint newState, uint oldState) emit stateChanged(networkStateNew, networkStateOld); } -// Returns Network config from connection Network NetworkManager::networkFromConnection(const Connection &connection) const { // TODO: It would be nice to make this static somewhere @@ -940,7 +855,6 @@ Network NetworkManager::networkFromConnection(const Connection &connection) cons return network; } -// Creates a network from an access point Network NetworkManager::createAccessPoint(const QDBusObjectPath &accessPoint) const { OrgFreedesktopNetworkManagerAccessPointInterface accessPointObject( @@ -1017,7 +931,6 @@ Network NetworkManager::createAccessPoint(const QDBusObjectPath &accessPoint) co return newNetwork; } -// Returns a list of all connections QList NetworkManager::getAllConnectionPaths() const { // initialize settings provider to get all dbus path connections @@ -1049,7 +962,6 @@ NetworkManager::getAllConnections() const return ret; } -// Returns the connection and path for a given ssid QPair NetworkManager::getConnection(QString ssid) const { @@ -1070,7 +982,6 @@ NetworkManager::getConnection(QString ssid) const return qMakePair(Connection(), QDBusObjectPath()); } -// Returns the password for a given ssid QString NetworkManager::getPassword(QString ssid) const { // get connection and path @@ -1099,7 +1010,6 @@ QString NetworkManager::getPassword(QString ssid) const } } -// Returns the reply from a given query void NetworkManager::getReply(QDBusPendingReply<> &reply, const QString where, const bool throwError) const { reply.waitForFinished();