From 3eaac63adb47b65f879543bba383c6a23677f37e Mon Sep 17 00:00:00 2001
From: gururaajar <r.gururaaja@gmail.com>
Date: Fri, 13 Dec 2024 21:26:57 -0500
Subject: [PATCH] Changed the wpa_cli verification completion logic

---
 NetworkManagerGnomeWIFI.cpp | 203 +++++++++++++++---------------------
 1 file changed, 83 insertions(+), 120 deletions(-)

diff --git a/NetworkManagerGnomeWIFI.cpp b/NetworkManagerGnomeWIFI.cpp
index 4bb0f38..6443546 100644
--- a/NetworkManagerGnomeWIFI.cpp
+++ b/NetworkManagerGnomeWIFI.cpp
@@ -973,7 +973,6 @@ namespace WPEFramework
             long wpsPBCDuration = 0;
             int count = 0;
             bool scanResult = false;
-            bool ssidFound = false;
             const char* bssid = nullptr;
             gboolean pbcFound = false;
             const GPtrArray *aps;
@@ -1009,7 +1008,7 @@ namespace WPEFramework
                         gsize size;
                         std::string ssidStr;
 
-                        NMLOG_DEBUG("Access point flag value : %u", flags);
+                        //NMLOG_DEBUG("Access point flag value : %u", flags);
                         if ((flags & NM_802_11_AP_FLAGS_WPS_PBC) && (wpsApCount < MAX_WPS_AP_COUNT))
                         {
                             clock_gettime(CLOCK_MONOTONIC, &endTime);
@@ -1040,6 +1039,7 @@ namespace WPEFramework
 
             if(!pbcFound || wpsStop.load())
             {
+                NMLOG_ERROR("PBC enabled SSID not found in the scan results");
                 g_main_context_pop_thread_default(m_wpsContext);
                 g_main_context_release(m_wpsContext);
                 if (m_wpsContext) {
@@ -1087,155 +1087,118 @@ namespace WPEFramework
                     }
                     return ;
                 }
-                NMLOG_DEBUG("wps pbc connect output = %s", wpaCliResult.c_str());
-                std::string wpaCliStatus = WPA_CLI_STATUS;
+
                 startTime = {}, endTime = {};
-                wpaCliResult.clear();
                 clock_gettime(CLOCK_MONOTONIC, &startTime);
-                while(!wpsStop.load())
+                while(!wpsConnect && !wpsStop.load())
                 {
-                    wpaCliResult = wifiManager::executeWpaCliCommand(wpaCliStatus);
-                    if (wpaCliResult == "ERROR")
+                    std::ifstream configFile(WPA_SUPPLICANT_CONF);
+                    if (!configFile.is_open())
                     {
-                        NMLOG_ERROR("WPS not able to fetch the connection status");
-                        continue;
-                    }
-                    NMLOG_DEBUG("wpacli status = %s", wpaCliResult.c_str());
-                    wpsConnect = (wpaCliResult.find("wpa_state=COMPLETED") != std::string::npos);
-                    clock_gettime(CLOCK_MONOTONIC, &endTime);
-                    timeDiff = (endTime.tv_sec - startTime.tv_sec);
-                    NMLOG_DEBUG("Time elapsed in getting state completed = %ld", timeDiff);
-                    if(wpsConnect || timeDiff > 20)
-                    {
-                        NMLOG_WARNING("WPS Connect status = %d; took %ld seconds", wpsConnect, (wpsPBCDuration + timeDiff));
-                        break;
-                    }
-                    sleep(3);
-                }
-            }
-
-            if(!wpsConnect)
-            {
-                g_main_context_pop_thread_default(m_wpsContext);
-                g_main_context_release(m_wpsContext);/* TODO: Need to disconnect the wpa_cli connection, as the libnm is not aware of the connection created by wpa_cli */
-                wpaCliCommand.clear();
-                wpaCliResult.clear();
-                wpaCliCommand = "wpa_cli -i " + std::string(nmUtils::wlanIface()) + " wps_cancel";
-                wpaCliResult = wifiManager::executeWpaCliCommand(wpaCliCommand);
-                if (wpaCliResult == "ERROR")
-                    NMLOG_ERROR("WPS cancel failed");
-                if (m_wpsContext) {
-                    g_main_context_unref(m_wpsContext);
-                    m_wpsContext = nullptr;
-                }
-                return;
-            }
-
-            count = 0;
-            while(count < 2 && !ssidFound)
-            {
-                std::ifstream configFile(WPA_SUPPLICANT_CONF);
-                if (!configFile.is_open())
-                {
-                    NMLOG_ERROR("WPS connected with an SSID but not able to fetch IP address");
-                    g_main_context_pop_thread_default(m_wpsContext);
-                    g_main_context_release(m_wpsContext);
-                    if (m_wpsContext) {
-                        g_main_context_unref(m_wpsContext);
-                        m_wpsContext = nullptr;
+                        NMLOG_ERROR("WPS connected with an SSID but not able to fetch IP address");
+                        g_main_context_pop_thread_default(m_wpsContext);
+                        g_main_context_release(m_wpsContext);
+                        if (m_wpsContext) {
+                            g_main_context_unref(m_wpsContext);
+                            m_wpsContext = nullptr;
+                        }
+                        return;
                     }
-                    return;
-                }
-
-                while (std::getline(configFile, line))
-                {
-                    NMLOG_DEBUG("Attempting to read the configuration to populate SSID specific information");
-                    size_t pos;
 
-                    // Fetch ssid value
-                    pos = line.find(ssidPattern);
-                    if (pos != std::string::npos)
+                    while (std::getline(configFile, line))
                     {
-                        pos += ssidPattern.length();
-                        size_t end = line.find('"', pos + 1);
-                        if (end == std::string::npos)
-                        {
-                            end = line.length();
-                        }
-                        ssid = line.substr(pos + 1, end - pos - 1);
-                        ssidFound = true;
-                        NMLOG_DEBUG("SSID found");
-                        continue;
-                    }
+                        NMLOG_DEBUG("Attempting to read the configuration to populate SSID specific information");
+                        size_t pos;
 
-                    if (ssidFound) {
-                        // Fetch security value
-                        pos = line.find(securityPattern);
+                        // Fetch ssid value
+                        pos = line.find(ssidPattern);
                         if (pos != std::string::npos)
                         {
-                            pos += securityPattern.length();
-                            size_t end = line.find(' ', pos);
+                            pos += ssidPattern.length();
+                            size_t end = line.find('"', pos + 1);
                             if (end == std::string::npos)
                             {
                                 end = line.length();
                             }
-                            security = line.substr(pos, end - pos);
+                            ssid = line.substr(pos + 1, end - pos - 1);
+                            wpsConnect = true;
+                            NMLOG_DEBUG("SSID found");
                             continue;
                         }
 
-                        // Fetch passphare value
-                        pos = line.find(passphrasePattern);
-                        if (pos != std::string::npos)
-                        {
-                            pos += passphrasePattern.length();
-                            size_t end = line.find('"', pos + 1);
-                            if (end == std::string::npos)
+                        if (wpsConnect) {
+                            // Fetch security value
+                            pos = line.find(securityPattern);
+                            if (pos != std::string::npos)
                             {
-                                end = line.length();
+                                pos += securityPattern.length();
+                                size_t end = line.find(' ', pos);
+                                if (end == std::string::npos)
+                                {
+                                    end = line.length();
+                                }
+                                security = line.substr(pos, end - pos);
+                                continue;
+                            }
+
+                            // Fetch passphare value
+                            pos = line.find(passphrasePattern);
+                            if (pos != std::string::npos)
+                            {
+                                pos += passphrasePattern.length();
+                                size_t end = line.find('"', pos + 1);
+                                if (end == std::string::npos)
+                                {
+                                    end = line.length();
+                                }
+                                passphrase = line.substr(pos + 1, end - pos - 1);
                             }
-                            passphrase = line.substr(pos + 1, end - pos - 1);
+                            NMLOG_DEBUG("Fetched SSID = %s, security = %s", ssid.c_str(), security.c_str());
                         }
-                        NMLOG_DEBUG("Fetched SSID = %s, security = %s", ssid.c_str(), security.c_str());
                     }
+                    configFile.close();
+                    clock_gettime(CLOCK_MONOTONIC, &endTime);
+                    timeDiff = (endTime.tv_sec - startTime.tv_sec);
+                    NMLOG_DEBUG("Time elapsed in getting SSID = %ld", timeDiff);
+                    if(ssid.empty() && timeDiff < 20)
+                    {
+                        NMLOG_INFO("connected successfully; attempting to retrive SSID info to persist");
+                        sleep(5);
+                    }
+                    else
+                        break;
                 }
-                configFile.close();
-                if(ssid.empty())
-                {
-                    count++;
-                    NMLOG_INFO("connected successfully; attempting to retrive SSID info to persist");
-                    sleep(5);
-                }
+            }
+            if(wpsConnect)
+            {
+                wifiData.ssid = ssid;
+                wifiData.passphrase = passphrase;
+                if(security == "WPA-PSK")
+                    wifiData.security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_PSK_AES;
+                else if(security == "WPA2-PSK")
+                    wifiData.security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA2_PSK_AES;
+                m_loop = g_main_loop_new(m_wpsContext, FALSE);
+                if(this->wifiConnect(wifiData))
+                    NMLOG_INFO("NetworkManager updated with WPS status - connected successfully");
                 else
                 {
-                    wifiData.ssid = ssid;
-                    wifiData.passphrase = passphrase;
-                    if(security == "WPA-PSK")
-                        wifiData.security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_PSK_AES;
-                    else if(security == "WPA2-PSK")
-                        wifiData.security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA2_PSK_AES;
-                    m_loop = g_main_loop_new(m_wpsContext, FALSE);
-                    if(this->wifiConnect(wifiData))
-                        NMLOG_INFO("NetworkManager updated with WPS status - connected successfully");
-                    else
+                    NMLOG_ERROR("NetworkManager is not able to sync up with underneath wpa_supplicant/hal regarding the WPS connect"); /* TODO: Need to disconnect the wpa_cli connection, as the libnm is not aware of the connection created by wpa_cli */
+                    wpaCliCommand.clear();
+                    wpaCliResult.clear();
+                    wpaCliCommand = "wpa_cli -i " + std::string(nmUtils::wlanIface()) + " wps_cancel";
+                    wpaCliResult = wifiManager::executeWpaCliCommand(wpaCliCommand);
+                    if (wpaCliResult == "ERROR")
                     {
-                        NMLOG_ERROR("NetworkManager is not able to sync up with underneath wpa_supplicant/hal regarding the WPS connect"); /* TODO: Need to disconnect the wpa_cli connection, as the libnm is not aware of the connection created by wpa_cli */
-                        wpaCliCommand.clear();
-                        wpaCliResult.clear();
-                        wpaCliCommand = "wpa_cli -i " + std::string(nmUtils::wlanIface()) + " wps_cancel";
-                        wpaCliResult = wifiManager::executeWpaCliCommand(wpaCliCommand);
-                        if (wpaCliResult == "ERROR")
-                        {
-                            NMLOG_ERROR("WPS cancel failed");
-                        }
+                        NMLOG_ERROR("WPS cancel failed");
                     }
-                    m_loop = g_main_loop_new(m_nmContext, FALSE);
                 }
+                m_loop = g_main_loop_new(m_nmContext, FALSE);
             }
             g_main_context_pop_thread_default(m_wpsContext);
             g_main_context_release(m_wpsContext);
             if (m_wpsContext) {
-                    g_main_context_unref(m_wpsContext);
-                    m_wpsContext = nullptr;
+                g_main_context_unref(m_wpsContext);
+                m_wpsContext = nullptr;
             }
             return;
         }