From 8a41e60790f7f8fdd6a572d0e52ceb3fe512aad3 Mon Sep 17 00:00:00 2001 From: cmuhammedrafi Date: Fri, 23 Aug 2024 10:31:59 +0530 Subject: [PATCH] cache logic added --- NetworkManagerImplementation.h | 44 ++++++++++++++++++ NetworkManagerRDKProxy.cpp | 81 ++++++++++++++++++++++++++++------ 2 files changed, 111 insertions(+), 14 deletions(-) diff --git a/NetworkManagerImplementation.h b/NetworkManagerImplementation.h index 9c86b692..c9b769a1 100644 --- a/NetworkManagerImplementation.h +++ b/NetworkManagerImplementation.h @@ -41,6 +41,50 @@ namespace WPEFramework { namespace Plugin { + /* Class to store and manage cached data */ + template + class Cache { + public: + Cache() : is_set(false) {} + + Cache& operator=(const DataType& value) { + std::lock_guard lock(mutex); + this->value = value; + is_set.store(true); + return *this; + } + + Cache& operator=(DataType&& value) { + std::lock_guard lock(mutex); + this->value = std::move(value); + is_set.store(true); + return *this; + } + + bool isSet() const { + return is_set.load(); + } + + void reset() { + is_set.store(false); + } + + const DataType& getValue() const { + std::lock_guard lock(mutex); + return value; + } + + DataType& getValue() { + std::lock_guard lock(mutex); + return value; + } + + private: + DataType value; + std::atomic is_set; + mutable std::mutex mutex; + }; + class NetworkManagerImplementation : public Exchange::INetworkManager { enum NetworkEvents { diff --git a/NetworkManagerRDKProxy.cpp b/NetworkManagerRDKProxy.cpp index d8bce80e..1a29aaaa 100644 --- a/NetworkManagerRDKProxy.cpp +++ b/NetworkManagerRDKProxy.cpp @@ -2,6 +2,8 @@ #include "NetworkManagerConnectivity.h" #include "WiFiSignalStrengthMonitor.h" #include "libIBus.h" +#include +#include using namespace WPEFramework; using namespace WPEFramework::Plugin; @@ -363,6 +365,11 @@ namespace WPEFramework const float signalStrengthThresholdFair = -67.0f; NetworkManagerImplementation* _instance = nullptr; + Cache WiFiStateCache; + Cache ipv4Cache; + Cache ipv6Cache; + Cache activeIfaceCache; + Exchange::INetworkManager::WiFiState to_wifi_state(WiFiStatusCode_t code) { switch (code) { @@ -399,8 +406,8 @@ namespace WPEFramework return Exchange::INetworkManager::WIFI_STATE_INVALID_CREDENTIALS; case WIFI_AUTH_FAILED: return Exchange::INetworkManager::WIFI_STATE_AUTHENTICATION_FAILED; - case WIFI_NO_SSID: - return Exchange::INetworkManager::WIFI_STATE_SSID_NOT_FOUND; + case WIFI_NO_SSID: + return Exchange::INetworkManager::WIFI_STATE_SSID_NOT_FOUND; case WIFI_UNKNOWN: return Exchange::INetworkManager::WIFI_STATE_ERROR; } @@ -462,6 +469,10 @@ namespace WPEFramework if(interface == "eth0" || interface == "wlan0") { ::_instance->ReportIPAddressChangedEvent(interface, e->acquired, e->is_ipv6, string(e->ip_address)); + if(e->is_ipv6) + ipv6Cache.reset(); + else + ipv4Cache.reset(); } break; } @@ -479,6 +490,7 @@ namespace WPEFramework newInterface == ""; /* assigning "null" if the interface is not eth0 or wlan0 */ ::_instance->ReportActiveInterfaceChangedEvent(oldInterface, newInterface); + activeIfaceCache.reset(); break; } case IARM_BUS_WIFI_MGR_EVENT_onAvailableSSIDs: @@ -509,6 +521,7 @@ namespace WPEFramework Exchange::INetworkManager::WiFiState state = Exchange::INetworkManager::WIFI_STATE_DISCONNECTED; NMLOG_INFO("Event IARM_BUS_WIFI_MGR_EVENT_onWIFIStateChanged received; state=%d", e->data.wifiStateChange.state); state = to_wifi_state(e->data.wifiStateChange.state); + WiFiStateCache = state; ::_instance->ReportWiFiStateChangedEvent(state); break; } @@ -518,6 +531,7 @@ namespace WPEFramework Exchange::INetworkManager::WiFiState state = errorcode_to_wifi_state(e->data.wifiError.code); NMLOG_INFO("Event IARM_BUS_WIFI_MGR_EVENT_onError received; code=%d", e->data.wifiError.code); ::_instance->ReportWiFiStateChangedEvent(state); + WiFiStateCache.reset(); break; } default: @@ -663,12 +677,20 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::GetPrimaryInterface (string& interface /* @out */) { LOG_ENTRY_FUNCTION(); + if(activeIfaceCache.isSet()) + { + interface = activeIfaceCache.getValue(); + NMLOG_INFO ("default interface cahched value = %s", interface.c_str()); + return Core::ERROR_NONE; + } + uint32_t rc = Core::ERROR_RPC_CALL_FAILED; IARM_BUS_NetSrvMgr_DefaultRoute_t defaultRoute = {0}; if (IARM_RESULT_SUCCESS == IARM_Bus_Call(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_getDefaultInterface, (void*)&defaultRoute, sizeof(defaultRoute))) { NMLOG_INFO ("Call to %s for %s returned interface = %s, gateway = %s", IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_getDefaultInterface, defaultRoute.interface, defaultRoute.gateway); interface = m_defaultInterface = defaultRoute.interface; + activeIfaceCache = interface; rc = Core::ERROR_NONE; } else @@ -801,7 +823,6 @@ namespace WPEFramework return prefix_len; } - /* @brief Get IP Address Of the Interface */ uint32_t NetworkManagerImplementation::GetIPSettings(const string& interface /* @in */, const string& ipversion /* @in */, IPAddressInfo& result /* @out */) { @@ -810,17 +831,49 @@ namespace WPEFramework IARM_BUS_NetSrvMgr_Iface_Settings_t iarmData = { 0 }; /* Netsrvmgr returns eth0 & wlan0 as primary interface but when we want to set., we must set ETHERNET or WIFI*/ //TODO: Fix netsrvmgr to accept eth0 & wlan0 + + // Convert the string to uppercase + std::string ipversionNonConst = ipversion; + std::transform(ipversionNonConst.begin(), ipversionNonConst.end(), ipversionNonConst.begin(), ::toupper); + if ("wlan0" == interface) strncpy(iarmData.interface, "WIFI", INTERFACE_SIZE); else if ("eth0" == interface) strncpy(iarmData.interface, "ETHERNET", INTERFACE_SIZE); - strncpy(iarmData.ipversion, ipversion.c_str(), 16); + strncpy(iarmData.ipversion, ipversionNonConst.c_str(), 16); iarmData.isSupported = true; - NMLOG_INFO("NetworkManagerImplementation::GetIPSettings - Before Calling IARM"); - if (IARM_RESULT_SUCCESS == IARM_Bus_Call (IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_getIPSettings, (void *)&iarmData, sizeof(iarmData))) + if ("wlan0" == interface && WiFiStateCache.isSet() && WiFiStateCache.getValue() != Exchange::INetworkManager::WIFI_STATE_CONNECTED) + { + // TODO fix netsrvmgr will return ip even after wifi disconnected + NMLOG_WARNING("wifi not connected so address will be empty"); + rc = Core::ERROR_NONE; + } + else if(ipversionNonConst == "IPV4" && ipv4Cache.isSet() && interface == activeIfaceCache.getValue()) + { + NMLOG_INFO("Reading ipv4 cached value"); + result = ipv4Cache.getValue(); + rc = Core::ERROR_NONE; + } + else if(ipversionNonConst == "IPV6" && ipv6Cache.isSet() && interface == activeIfaceCache.getValue()) + { + NMLOG_INFO("Reading ipv6 cached value"); + result = ipv4Cache.getValue(); + rc = Core::ERROR_NONE; + } + else + { + if (IARM_RESULT_SUCCESS == IARM_Bus_Call (IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_getIPSettings, (void *)&iarmData, sizeof(iarmData))) + { + NMLOG_INFO("GetIPSettings - IARM Success"); + rc = Core::ERROR_NONE; + } + else + NMLOG_ERROR("GetIPSettings - Calling IARM Failed"); + } + + if (rc == Core::ERROR_NONE) { - NMLOG_INFO("NetworkManagerImplementation::GetIPSettings - IARM Success.. Filling the data"); result.m_ipAddrType = string(iarmData.ipversion); result.m_autoConfig = iarmData.autoconfig; result.m_dhcpServer = string(iarmData.dhcpserver,MAX_IP_ADDRESS_LEN - 1); @@ -828,17 +881,16 @@ namespace WPEFramework result.m_ipAddress = string(iarmData.ipaddress,MAX_IP_ADDRESS_LEN - 1); if (0 == strcasecmp("ipv4", iarmData.ipversion)) result.m_prefix = NetmaskToPrefix(iarmData.netmask); - else if (0 == strcasecmp("ipv6", iarmData.ipversion)) + else if (0 == strcasecmp("ipv6", iarmData.ipversion)) result.m_prefix = std::atoi(iarmData.netmask); result.m_gateway = string(iarmData.gateway,MAX_IP_ADDRESS_LEN - 1); result.m_primaryDns = string(iarmData.primarydns,MAX_IP_ADDRESS_LEN - 1); result.m_secondaryDns = string(iarmData.secondarydns,MAX_IP_ADDRESS_LEN - 1); - NMLOG_INFO("NetworkManagerImplementation::GetIPSettings - IARM Success.. Filled the data"); - rc = Core::ERROR_NONE; - } - else - { - NMLOG_ERROR("NetworkManagerImplementation::GetIPSettings - Calling IARM Failed"); + + if(ipversionNonConst == "IPV4") + ipv4Cache = result; + else + ipv6Cache = result; } return rc; @@ -1316,6 +1368,7 @@ const string CIDR_PREFIXES[CIDR_NETMASK_IP_LEN] = { if(IARM_RESULT_SUCCESS == IARM_Bus_Call(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_WIFI_MGR_API_getCurrentState, (void *)¶m, sizeof(param))) { state = to_wifi_state(param.data.wifiStatus); + WiFiStateCache = state; rc = Core::ERROR_NONE; } return rc;