From c94de3eff8ef50932b7a8425a9618b7494249ccf Mon Sep 17 00:00:00 2001 From: gururaajar <83449026+gururaajar@users.noreply.github.com> Date: Mon, 25 Nov 2024 22:47:41 -0500 Subject: [PATCH 1/4] DELIA-66920 - GetWiFiSignalStrength is not return actual strength (#41) * DELIA-66920 - GetWiFiSignalStrength is not return actual strength Reason for change: Removed the internal mapping and returning the actual strength value Test Procedure: Check with curl command Risks: Low Priority: P1 Signed-off-by: Gururaaja ESR --- NetworkManagerRDKProxy.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/NetworkManagerRDKProxy.cpp b/NetworkManagerRDKProxy.cpp index 8099a3cc..c6541f36 100644 --- a/NetworkManagerRDKProxy.cpp +++ b/NetworkManagerRDKProxy.cpp @@ -1261,22 +1261,18 @@ const string CIDR_PREFIXES[CIDR_NETMASK_IP_LEN+1] = { else if (signalStrengthOut >= signalStrengthThresholdExcellent && signalStrengthOut < 0) { quality = WIFI_SIGNAL_EXCELLENT; - signalStrength = "100"; } else if (signalStrengthOut >= signalStrengthThresholdGood && signalStrengthOut < signalStrengthThresholdExcellent) { quality = WIFI_SIGNAL_GOOD; - signalStrength = "75"; } else if (signalStrengthOut >= signalStrengthThresholdFair && signalStrengthOut < signalStrengthThresholdGood) { quality = WIFI_SIGNAL_FAIR; - signalStrength = "50"; } else { quality = WIFI_SIGNAL_WEAK; - signalStrength = "25"; } NMLOG_INFO ("GetWiFiSignalStrength success"); From 2b7da13a472800a4bae545296703f046e1c62098 Mon Sep 17 00:00:00 2001 From: gururaajar <83449026+gururaajar@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:45:44 -0500 Subject: [PATCH 2/4] RDKE-352 - Modified the WPS API (#40) * RDKE-352 - Modified the WPS API Reason for change: Modified StartWPS API to use wpa_cli and wifiConnect API call to connect with wifi using WPS Test Procedure: Check with StartWPS curl command Risks: Low Priority: P1 Signed-off-by: Gururaaja ESR --- NetworkManagerGnomeWIFI.cpp | 222 ++++++++++++++++++++++++++++-------- NetworkManagerGnomeWIFI.h | 8 ++ 2 files changed, 183 insertions(+), 47 deletions(-) diff --git a/NetworkManagerGnomeWIFI.cpp b/NetworkManagerGnomeWIFI.cpp index 4d665b91..8fb62a70 100644 --- a/NetworkManagerGnomeWIFI.cpp +++ b/NetworkManagerGnomeWIFI.cpp @@ -171,21 +171,22 @@ namespace WPEFramework return wifiDevice; } - bool static getConnectedSSID(NMDeviceWifi *wifiDevice, std::string& ssidin) + bool static getConnectedSSID(NMDevice *device, std::string& ssidin) { GBytes *ssid; - NMAccessPoint *activeAP = nm_device_wifi_get_active_access_point(wifiDevice); - if(activeAP == NULL) { - return false; + NMDeviceState device_state = nm_device_get_state(device); + if (device_state == NM_DEVICE_STATE_ACTIVATED) + { + NMAccessPoint *activeAP = nm_device_wifi_get_active_access_point(NM_DEVICE_WIFI(device)); + ssid = nm_access_point_get_ssid(activeAP); + gsize size; + const guint8 *ssidData = static_cast(g_bytes_get_data(ssid, &size)); + std::string ssidTmp(reinterpret_cast(ssidData), size); + ssidin = ssidTmp; + NMLOG_INFO("connected ssid: %s", ssidin.c_str()); + return true; } - - ssid = nm_access_point_get_ssid(activeAP); - gsize size; - const guint8 *ssidData = static_cast(g_bytes_get_data(ssid, &size)); - std::string ssidTmp(reinterpret_cast(ssidData), size); - ssidin = ssidTmp; - NMLOG_INFO("connected ssid: %s", ssidin.c_str()); - return true; + return false; } static void getApInfo(NMAccessPoint *AccessPoint, Exchange::INetworkManager::WiFiSSIDInfo &wifiInfo) @@ -584,7 +585,7 @@ namespace WPEFramework return false; std::string activeSSID; - if(getConnectedSSID(NM_DEVICE_WIFI(device), activeSSID)) + if(getConnectedSSID(device, activeSSID)) { if(ssidInfo.ssid == activeSSID) { @@ -944,52 +945,179 @@ namespace WPEFramework return false; } - bool wifiManager::initiateWPS() + void wifiManager::wpsAction() { - Core::IWorkerPool::Instance().Submit(Core::ProxyType(Core::ProxyType::Create([&]() { - const GPtrArray *aps; - int count = 1, wpsConnected = 0; - if(!createClientNewConnection()) + FILE *fp = nullptr; + std::ifstream configFile(WPA_SUPPLICANT_CONF); + std::string line = ""; + std::string securityPattern = "key_mgmt="; + std::string ssidPattern = "ssid="; + std::string passphrasePattern = "psk="; + std::string security = "", ssid = "", passphrase = ""; + Exchange::INetworkManager::WiFiConnectTo wifiData = {}; + std::array buffer = {}; + std::string wpaCliResult = ""; + gboolean wpsConnect = false; + struct timespec startTime = {}, endTime = {}; + long timeDiff = 0; + + if (!g_main_context_acquire(wpsContext)) + { + NMLOG_ERROR("Failed to acquire wpsContext"); return; + } + + g_main_context_push_thread_default(wpsContext); + + std::string wpaCliCommand = "wpa_cli -i " + std::string(nmUtils::wlanIface()) + " wps_pbc"; + fp = popen(wpaCliCommand.c_str(), "r"); + if (fp == nullptr) + { + NMLOG_ERROR("WPS failed to connect with the SSID"); + g_main_context_pop_thread_default(wpsContext); + g_main_context_release(wpsContext); + return ; + } + pclose(fp); + std::string wpaCliStatus = WPA_CLI_STATUS; + clock_gettime(CLOCK_MONOTONIC, &startTime); + while(!wpsStop.load()) + { + fp = popen(wpaCliStatus.c_str(), "r"); + if (fp == nullptr) + { + NMLOG_ERROR("WPS not able to fetch the connection status"); + continue; + } + while (fgets(buffer.data(), buffer.size(), fp) != nullptr) + { + wpaCliResult += buffer.data(); + } + 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 before getting wifi connected = %ld", timeDiff); + if(wpsConnect || timeDiff > 120) + break; + pclose(fp); + sleep(5); + } + + if(!wpsConnect) + { + g_main_context_pop_thread_default(wpsContext); + g_main_context_release(wpsContext);/* TODO: Need to disconnect the wpa_cli connection, as the libnm is not aware of the connection created by wpa_cli */ + return; + } + + { + if (!configFile.is_open()) + { + NMLOG_ERROR("WPS connected with an SSID but not able to fetch IP address"); + g_main_context_pop_thread_default(wpsContext); + g_main_context_release(wpsContext); + return; + } - sleep(10); /* As we will get the ap info with NM_802_11_AP_FLAGS_WPS_PBC set after pressing the PBC button. - So we are waiting for 10 seconds here*/ - do{ - if(wifiScanRequest("")) + while (std::getline(configFile, line)) { - aps = nm_device_wifi_get_access_points(NM_DEVICE_WIFI(getNmDevice())); - for (guint i = 0; i < aps->len; i++) { - NMAccessPoint *ap = static_cast(g_ptr_array_index(aps, i)); - - guint32 flags = nm_access_point_get_flags(ap); - - NMLOG_INFO("AP Flags: 0x%x\n", flags); - - if (flags & NM_802_11_AP_FLAGS_WPS_PBC) { - Exchange::INetworkManager::WiFiConnectTo wifiData; - GBytes *ssid; - ssid = nm_access_point_get_ssid(ap); - gsize size; - const guint8 *ssidData = static_cast(g_bytes_get_data(ssid, &size)); - std::string ssidTmp(reinterpret_cast(ssidData), size); - wifiData.ssid = ssidTmp.c_str(); - NMLOG_INFO("connected ssid: %s", ssidTmp.c_str()); - if(wifiConnect(wifiData)) - wpsConnected = 1; - break; + size_t pos; + + // Fetch security value + pos = line.find(securityPattern); + if (pos != std::string::npos) + { + 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 ssid value + pos = line.find(ssidPattern); + if (pos != std::string::npos) + { + 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); + 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); + continue; } } - sleep(3); /* Waiting time between successive scan */ - count++; - }while(count <= 3 && !wpsConnected); - NMLOG_INFO("Completed scanning and wpsconnect status = %d", wpsConnected); - }))); + + configFile.close(); + 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; + } + if(this->wifiConnect(wifiData)) + NMLOG_INFO("WPS connected successfully"); + else + NMLOG_ERROR("WPS connect failed");/* TODO: Need to disconnect the wpa_cli connection, as the libnm is not aware of the connection created by wpa_cli */ + + g_main_context_pop_thread_default(wpsContext); + g_main_context_release(wpsContext); + if (wpsContext) { + g_main_context_unref(wpsContext); + wpsContext = nullptr; + } + return; + } + + bool wifiManager::initiateWPS() + { + if (!createClientNewConnection()) + return false; + + if(!wpsContext){ + if (wpsThread.joinable()) { + wpsThread.join(); + } + wpsContext = g_main_context_new(); + wpsThread = std::thread(&wifiManager::wpsAction, this); + } + else + NMLOG_INFO("Start WPS is already in progress"); + return true; } bool wifiManager::cancelWPS() { + NMLOG_INFO ("Stop WPS %s", __FUNCTION__); + wpsStop.store(true); + if (wpsThread.joinable()) { + wpsThread.join(); + } + if (wpsContext) { + g_main_context_unref(wpsContext); + wpsContext = nullptr; + } + wpsStop.store(false); return true; } diff --git a/NetworkManagerGnomeWIFI.h b/NetworkManagerGnomeWIFI.h index 74704015..eada5a8a 100644 --- a/NetworkManagerGnomeWIFI.h +++ b/NetworkManagerGnomeWIFI.h @@ -28,6 +28,10 @@ #include #include #include +#include + +#define WPA_SUPPLICANT_CONF "/opt/secure/wifi/wpa_supplicant.conf" +#define WPA_CLI_STATUS "wpa_cli status" namespace WPEFramework { @@ -55,6 +59,7 @@ namespace WPEFramework bool wait(GMainLoop *loop, int timeOutMs = 10000); // default maximium set as 10 sec bool initiateWPS(); bool cancelWPS(); + void wpsAction(); bool setInterfaceState(std::string interface, bool enabled); private: NMDevice *getNmDevice(); @@ -78,12 +83,15 @@ namespace WPEFramework void operator=(wifiManager const&) = delete; bool createClientNewConnection(); + std::atomic wpsStop = {false}; + std::thread wpsThread; public: NMClient *client; GMainLoop *loop; gboolean createNewConnection; GMainContext *nmContext = nullptr; + GMainContext *wpsContext = nullptr; const char* objectPath; NMDevice *wifidevice; GSource *source; From d8aec5428a56dd777498f69cf6f63ff33da36945 Mon Sep 17 00:00:00 2001 From: gururaajar <83449026+gururaajar@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:11:16 -0500 Subject: [PATCH 3/4] Initialized IPstatus to lost in NetworkManagerInternalEventHandler for IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_IPADDRESS (#42) --- NetworkManagerRDKProxy.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/NetworkManagerRDKProxy.cpp b/NetworkManagerRDKProxy.cpp index c6541f36..3ad434b0 100644 --- a/NetworkManagerRDKProxy.cpp +++ b/NetworkManagerRDKProxy.cpp @@ -457,11 +457,12 @@ namespace WPEFramework { IARM_BUS_NetSrvMgr_Iface_EventInterfaceIPAddress_t *e = (IARM_BUS_NetSrvMgr_Iface_EventInterfaceIPAddress_t*) data; interface = e->interface; - NMLOG_INFO ("IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_IPADDRESS :: %s -- %s", interface.c_str(), e->ip_address); + NMLOG_INFO ("IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_IPADDRESS: %s - %s - %s", interface.c_str(), e->ip_address, e->acquired?"Acquired":"Lost" +); if(interface == "eth0" || interface == "wlan0") { string ipversion("IPv4"); - Exchange::INetworkManager::IPStatus status; + Exchange::INetworkManager::IPStatus status = Exchange::INetworkManager::IP_LOST; if (e->is_ipv6) ipversion = "IPv6"; From c6c4485065fc91caf6ed8c1fd3a23e049e97fffa Mon Sep 17 00:00:00 2001 From: gururaajar <83449026+gururaajar@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:12:05 -0500 Subject: [PATCH 4/4] Updated the json and documentation for signal strength as string instead of integer (#43) Co-authored-by: Karunakaran A <48997923+karuna2git@users.noreply.github.com> --- NetworkManager.json | 4 ++-- docs/NetworkManagerPlugin.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/NetworkManager.json b/NetworkManager.json index dec2b65e..d0cbde9e 100644 --- a/NetworkManager.json +++ b/NetworkManager.json @@ -1257,8 +1257,8 @@ }, "quality":{ "summary": "Signal strength Quality", - "type": "integer", - "example": 123 + "type": "string", + "example": "Excellent" }, "success":{ "$ref": "#/definitions/success" diff --git a/docs/NetworkManagerPlugin.md b/docs/NetworkManagerPlugin.md index 9713c84d..44c375b0 100644 --- a/docs/NetworkManagerPlugin.md +++ b/docs/NetworkManagerPlugin.md @@ -1650,7 +1650,7 @@ This method takes no parameters. | result | object | | | result.ssid | string | The paired SSID | | result.strength | string | The RSSI value in dBm | -| result.quality | integer | Signal strength Quality | +| result.quality | string | Signal strength Quality | | result.success | boolean | Whether the request succeeded | ### Example @@ -1674,7 +1674,7 @@ This method takes no parameters. "result": { "ssid": "123412341234", "strength": "-27.000000", - "quality": 123, + "quality": "Excellent", "success": true } }