Skip to content

Commit

Permalink
cache logic added
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuhammedrafi committed Aug 28, 2024
1 parent f0963a1 commit 8a41e60
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 14 deletions.
44 changes: 44 additions & 0 deletions NetworkManagerImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,50 @@ namespace WPEFramework
{
namespace Plugin
{
/* Class to store and manage cached data */
template<typename DataType>
class Cache {
public:
Cache() : is_set(false) {}

Cache& operator=(const DataType& value) {
std::lock_guard<std::mutex> lock(mutex);
this->value = value;
is_set.store(true);
return *this;
}

Cache& operator=(DataType&& value) {
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex);
return value;
}

DataType& getValue() {
std::lock_guard<std::mutex> lock(mutex);
return value;
}

private:
DataType value;
std::atomic<bool> is_set;
mutable std::mutex mutex;
};

class NetworkManagerImplementation : public Exchange::INetworkManager
{
enum NetworkEvents {
Expand Down
81 changes: 67 additions & 14 deletions NetworkManagerRDKProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "NetworkManagerConnectivity.h"
#include "WiFiSignalStrengthMonitor.h"
#include "libIBus.h"
#include <algorithm>
#include <string>

using namespace WPEFramework;
using namespace WPEFramework::Plugin;
Expand Down Expand Up @@ -363,6 +365,11 @@ namespace WPEFramework
const float signalStrengthThresholdFair = -67.0f;
NetworkManagerImplementation* _instance = nullptr;

Cache<Exchange::INetworkManager::WiFiState> WiFiStateCache;
Cache<Exchange::INetworkManager::IPAddressInfo> ipv4Cache;
Cache<Exchange::INetworkManager::IPAddressInfo> ipv6Cache;
Cache<std::string> activeIfaceCache;

Exchange::INetworkManager::WiFiState to_wifi_state(WiFiStatusCode_t code) {
switch (code)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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:
Expand Down Expand Up @@ -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;
}
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */)
{
Expand All @@ -810,35 +831,66 @@ 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);
result.m_v6LinkLocal = "";
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;
Expand Down Expand Up @@ -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 *)&param, sizeof(param)))
{
state = to_wifi_state(param.data.wifiStatus);
WiFiStateCache = state;
rc = Core::ERROR_NONE;
}
return rc;
Expand Down

0 comments on commit 8a41e60

Please sign in to comment.