From 9ecef917b2af39067b9821bc173f17e711d1cd01 Mon Sep 17 00:00:00 2001 From: cmuhammedrafi <103924677+cmuhammedrafi@users.noreply.github.com> Date: Wed, 13 Mar 2024 23:43:22 +0530 Subject: [PATCH] NetworkManger connectivitymonitor static class removed. (#5016) * connectivity monitor singleton class removed * wifisignal strength logic update * connectivity monitor call backfunction removed * RDK proxy error fix --- NetworkManager/service/NetworkConnectivity.cpp | 16 ++++------------ NetworkManager/service/NetworkConnectivity.h | 17 ++--------------- .../service/NetworkManagerGnomeProxy.cpp | 1 + .../service/NetworkManagerImplementation.cpp | 18 ++++++++---------- .../service/NetworkManagerImplementation.h | 5 +++++ .../service/NetworkManagerRDKProxy.cpp | 2 +- .../service/WifiSignalStrengthMonitor.cpp | 14 +++----------- .../service/WifiSignalStrengthMonitor.h | 16 ++-------------- 8 files changed, 26 insertions(+), 63 deletions(-) diff --git a/NetworkManager/service/NetworkConnectivity.cpp b/NetworkManager/service/NetworkConnectivity.cpp index 17262ab736..e34688dc1c 100644 --- a/NetworkManager/service/NetworkConnectivity.cpp +++ b/NetworkManager/service/NetworkConnectivity.cpp @@ -14,12 +14,12 @@ #include #include "Module.h" #include "NetworkConnectivity.h" - +#include "NetworkManagerImplementation.h" namespace WPEFramework { namespace Plugin { - ConnectivityMonitor* ConnectivityMonitor::connectivityMonitor = nullptr; + extern NetworkManagerImplementation* _instance; bool EndpointCache::isEndpointCashFileExist() { @@ -438,14 +438,6 @@ namespace WPEFramework { return InternetConnectionState; } - void ConnectivityMonitor::registerConnectivityMonitorCallback(NetworkManagerImplementation * impl) - { - if(impl != nullptr) - networkManagerImpl = impl; - else - NMLOG_ERROR("NetworkManagerImplementation pointer null"); - } - bool ConnectivityMonitor::doContinuousConnectivityMonitoring(int timeoutInSeconds) { if(!isConnectivityMonitorEndpointSet()) @@ -591,11 +583,11 @@ namespace WPEFramework { InternetConnectionState = testConnectivity(getConnectivityMonitorEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, NSM_IPRESOLVE_WHATEVER); if(g_internetState.load() != InternetConnectionState) { - if(networkManagerImpl != nullptr) + if(_instance != nullptr) { Exchange::INetworkManager::InternetStatus oldState = static_cast(g_internetState.load()); Exchange::INetworkManager::InternetStatus newstate = static_cast(InternetConnectionState); - networkManagerImpl->ReportInternetStatusChangedEvent(oldState , newstate); + _instance->ReportInternetStatusChangedEvent(oldState , newstate); NMLOG_TRACE("ReportInternetStatusChangedEvent called"); } else diff --git a/NetworkManager/service/NetworkConnectivity.h b/NetworkManager/service/NetworkConnectivity.h index 59f319611b..8b2144093c 100644 --- a/NetworkManager/service/NetworkConnectivity.h +++ b/NetworkManager/service/NetworkConnectivity.h @@ -9,7 +9,6 @@ #include #include "Module.h" #include "NetworkManagerLogger.h" -#include "NetworkManagerImplementation.h" #define CAPTIVEPORTAL_MAX_LEN 512 #define DEFAULT_MONITOR_TIMEOUT 60 // in seconds @@ -102,16 +101,6 @@ namespace WPEFramework { class ConnectivityMonitor : public Connectivity { public: - static ConnectivityMonitor* getInstance() - { - if (connectivityMonitor == nullptr) { - static ConnectivityMonitor instance; - connectivityMonitor = &instance; - } - return connectivityMonitor; - } - - void registerConnectivityMonitorCallback(NetworkManagerImplementation * impl); bool isConnectedToInternet(nsm_ipversion ipversion); nsm_internetState getInternetConnectionState(nsm_ipversion ipversion); std::string getCaptivePortalURI(); @@ -126,16 +115,17 @@ namespace WPEFramework { void signalConnectivityMonitor(); void resetConnectivityCache() { g_internetState = nsm_internetState::UNKNOWN;} - private: ConnectivityMonitor() : stopFlag(false), threadRunning(false), isContinuesMonitoringNeeded(false) { setConnectivityMonitorEndpoints(getConnectivityDefaultEndpoints()); } ~ConnectivityMonitor() { + NMLOG_INFO("~ConnectivityMonitor"); stopContinuousConnectivityMonitoring(); } + private: ConnectivityMonitor(const ConnectivityMonitor&) = delete; ConnectivityMonitor& operator=(const ConnectivityMonitor&) = delete; void connectivityMonitorFunction(); @@ -153,9 +143,6 @@ namespace WPEFramework { std::mutex mutex_; std::mutex endpointMutex; std::atomic g_internetState = {nsm_internetState::UNKNOWN}; - public: - static ConnectivityMonitor *connectivityMonitor; - NetworkManagerImplementation *networkManagerImpl = nullptr; }; } // namespace Plugin } // namespace WPEFramework diff --git a/NetworkManager/service/NetworkManagerGnomeProxy.cpp b/NetworkManager/service/NetworkManagerGnomeProxy.cpp index 2a1ca45c47..4334d5bc0d 100755 --- a/NetworkManager/service/NetworkManagerGnomeProxy.cpp +++ b/NetworkManager/service/NetworkManagerGnomeProxy.cpp @@ -29,6 +29,7 @@ namespace WPEFramework void NetworkManagerImplementation::platform_init() { + ::_instance = this; GError *error = NULL; GMainContext *context = g_main_context_new(); // initialize the NMClient object diff --git a/NetworkManager/service/NetworkManagerImplementation.cpp b/NetworkManager/service/NetworkManagerImplementation.cpp index 7470420487..f2431d982f 100644 --- a/NetworkManager/service/NetworkManagerImplementation.cpp +++ b/NetworkManager/service/NetworkManagerImplementation.cpp @@ -43,9 +43,6 @@ namespace WPEFramework /* Name says it all */ platform_init(); - /* set the callback function of connectivity monitor*/ - ConnectivityMonitor::getInstance()->registerConnectivityMonitorCallback(this); - WifiSignalStrengthMonitor::getInstance()->registerWifiSignalStrengthNotify(this); /* Initialize STUN Endpoints */ m_stunEndPoint = "stun.l.google.com"; m_stunPort = 19302; @@ -57,6 +54,7 @@ namespace WPEFramework NetworkManagerImplementation::~NetworkManagerImplementation() { + connectivityMonitor.stopContinuousConnectivityMonitoring(); LOG_ENTRY_FUNCTION(); } @@ -141,7 +139,7 @@ namespace WPEFramework endpoints.push_back(config.connectivity.endpoint_5.Value().c_str()); //set connectivity endpoint - ConnectivityMonitor::getInstance()->setConnectivityMonitorEndpoints(endpoints); + connectivityMonitor.setConnectivityMonitorEndpoints(endpoints); } else NMLOG_ERROR("Plugin configuration read error !"); @@ -178,7 +176,7 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::GetConnectivityTestEndpoints(IStringIterator*& endPoints/* @out */) const { LOG_ENTRY_FUNCTION(); - std::vector tmpEndPoints = ConnectivityMonitor::getInstance()->getConnectivityMonitorEndpoints(); + std::vector tmpEndPoints = connectivityMonitor.getConnectivityMonitorEndpoints(); endPoints = (Core::Service::Create(tmpEndPoints)); return Core::ERROR_NONE; @@ -196,7 +194,7 @@ namespace WPEFramework { tmpEndPoints.push_back(endPoint); } - ConnectivityMonitor::getInstance()->setConnectivityMonitorEndpoints(tmpEndPoints); + connectivityMonitor.setConnectivityMonitorEndpoints(tmpEndPoints); } return Core::ERROR_NONE; } @@ -212,7 +210,7 @@ namespace WPEFramework else if(0 == strcasecmp("IPv6", ipversion.c_str())) tmpVersion = NSM_IPRESOLVE_V6; - isconnected = ConnectivityMonitor::getInstance()->getInternetConnectionState(tmpVersion); + isconnected = connectivityMonitor.getInternetConnectionState(tmpVersion); if (FULLY_CONNECTED == isconnected) result = INTERNET_FULLY_CONNECTED; else if (CAPTIVE_PORTAL == isconnected) @@ -229,7 +227,7 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::GetCaptivePortalURI(string &endPoints/* @out */) const { LOG_ENTRY_FUNCTION(); - endPoints = ConnectivityMonitor::getInstance()->getCaptivePortalURI(); + endPoints = connectivityMonitor.getCaptivePortalURI(); return Core::ERROR_NONE; } @@ -237,7 +235,7 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::StartConnectivityMonitoring(const uint32_t interval/* @in */) { LOG_ENTRY_FUNCTION(); - if (ConnectivityMonitor::getInstance()->doContinuousConnectivityMonitoring(interval)) + if (connectivityMonitor.doContinuousConnectivityMonitoring(interval)) return Core::ERROR_NONE; else return Core::ERROR_GENERAL; @@ -247,7 +245,7 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::StopConnectivityMonitoring(void) const { LOG_ENTRY_FUNCTION(); - if (ConnectivityMonitor::getInstance()->stopContinuousConnectivityMonitoring()) + if (connectivityMonitor.stopContinuousConnectivityMonitoring()) return Core::ERROR_NONE; else return Core::ERROR_GENERAL; diff --git a/NetworkManager/service/NetworkManagerImplementation.h b/NetworkManager/service/NetworkManagerImplementation.h index 5107398be6..f9228fa5ec 100644 --- a/NetworkManager/service/NetworkManagerImplementation.h +++ b/NetworkManager/service/NetworkManagerImplementation.h @@ -31,6 +31,8 @@ using namespace std; //#include #include "INetworkManager.h" #include "NetworkManagerLogger.h" +#include "WifiSignalStrengthMonitor.h" +#include "NetworkConnectivity.h" #include "StunClient.h" #define LOG_ENTRY_FUNCTION() { NMLOG_TRACE("Entering=%s", __FUNCTION__ ); } @@ -231,6 +233,9 @@ namespace WPEFramework uint16_t m_stunPort; uint16_t m_stunBindTimeout; uint16_t m_stunCacheTimeout; + public: + WifiSignalStrengthMonitor wifiSignalStrengthMonitor; + mutable ConnectivityMonitor connectivityMonitor; }; } } diff --git a/NetworkManager/service/NetworkManagerRDKProxy.cpp b/NetworkManager/service/NetworkManagerRDKProxy.cpp index 9a68db2b16..e6177fd27d 100644 --- a/NetworkManager/service/NetworkManagerRDKProxy.cpp +++ b/NetworkManager/service/NetworkManagerRDKProxy.cpp @@ -468,7 +468,7 @@ namespace WPEFramework state = to_wifi_state(e->data.wifiStateChange.state); if(e->data.wifiStateChange.state == WIFI_CONNECTED) - WifiSignalStrengthMonitor::getInstance()->startWifiSignalStrengthMonitor(DEFAULT_WIFI_SIGNAL_TEST_INTERVAL_SEC); + ::_instance->wifiSignalStrengthMonitor.startWifiSignalStrengthMonitor(DEFAULT_WIFI_SIGNAL_TEST_INTERVAL_SEC); ::_instance->ReportWiFiStateChangedEvent(state); break; } diff --git a/NetworkManager/service/WifiSignalStrengthMonitor.cpp b/NetworkManager/service/WifiSignalStrengthMonitor.cpp index 3cf225dd37..f448e00e71 100644 --- a/NetworkManager/service/WifiSignalStrengthMonitor.cpp +++ b/NetworkManager/service/WifiSignalStrengthMonitor.cpp @@ -14,12 +14,10 @@ namespace WPEFramework { namespace Plugin { - - WifiSignalStrengthMonitor* WifiSignalStrengthMonitor::wifiSignalStrengthMonitor = nullptr; - static const float signalStrengthThresholdExcellent = -50.0f; static const float signalStrengthThresholdGood = -60.0f; static const float signalStrengthThresholdFair = -67.0f; + extern NetworkManagerImplementation* _instance; std::string WifiSignalStrengthMonitor::retrieveValues(const char *command, const char* keyword, char *output_buffer, size_t output_buffer_size) { @@ -117,7 +115,7 @@ namespace WPEFramework string ssid = ""; string signalStrength; Exchange::INetworkManager::WiFiSignalQuality newSignalQuality; - if (networkManagerImpl != nullptr) + if (_instance != nullptr) { NMLOG_TRACE("checking WiFi signal strength"); getSignalData(ssid, newSignalQuality, signalStrength); @@ -125,7 +123,7 @@ namespace WPEFramework { NMLOG_INFO("Notifying WiFiSignalStrengthChangedEvent ...%s", signalStrength.c_str()); oldSignalQuality = newSignalQuality; - networkManagerImpl->ReportWiFiSignalStrengthChangedEvent(ssid, signalStrength, newSignalQuality); + _instance->ReportWiFiSignalStrengthChangedEvent(ssid, signalStrength, newSignalQuality); } if(newSignalQuality == Exchange::INetworkManager::WIFI_SIGNAL_DISCONNECTED) @@ -142,11 +140,5 @@ namespace WPEFramework } isRunning = false; } - - void WifiSignalStrengthMonitor::registerWifiSignalStrengthNotify(NetworkManagerImplementation* nmImpl) - { - NMLOG_INFO("Registered WifiSignalStrength Notification !"); - networkManagerImpl = nmImpl; - } } } \ No newline at end of file diff --git a/NetworkManager/service/WifiSignalStrengthMonitor.h b/NetworkManager/service/WifiSignalStrengthMonitor.h index 8b86563409..19f757b49a 100644 --- a/NetworkManager/service/WifiSignalStrengthMonitor.h +++ b/NetworkManager/service/WifiSignalStrengthMonitor.h @@ -26,7 +26,6 @@ #include #include "Module.h" #include "NetworkManagerLogger.h" -#include "NetworkManagerImplementation.h" #define DEFAULT_WIFI_SIGNAL_TEST_INTERVAL_SEC 60 namespace WPEFramework @@ -36,26 +35,15 @@ namespace WPEFramework class WifiSignalStrengthMonitor { public: - static WifiSignalStrengthMonitor *wifiSignalStrengthMonitor; - WifiSignalStrengthMonitor(): networkManagerImpl(nullptr), isRunning(false) {} + WifiSignalStrengthMonitor():isRunning(false) {} + ~WifiSignalStrengthMonitor(){ NMLOG_INFO("~WifiSignalStrengthMonitor"); } void startWifiSignalStrengthMonitor(int interval); void getSignalData(std::string &ssid, Exchange::INetworkManager::WiFiSignalQuality &quality, std::string &strengthOut); - void registerWifiSignalStrengthNotify(NetworkManagerImplementation* nmImpl); - static WifiSignalStrengthMonitor* getInstance() - { - if (wifiSignalStrengthMonitor == nullptr) { - static WifiSignalStrengthMonitor instance; - wifiSignalStrengthMonitor = &instance; - } - return wifiSignalStrengthMonitor; - } private: std::string retrieveValues(const char *command, const char* keyword, char *output_buffer, size_t output_buffer_size); - NetworkManagerImplementation* networkManagerImpl; std::thread monitorThread; std::atomic stopThread; std::atomic isRunning; - void monitorThreadFunction(int interval); }; }