Skip to content

Commit

Permalink
RDKTV-30981 - Devices went to offline plui state (rdkcentral#5366)
Browse files Browse the repository at this point in the history
* RDKTV-30981 - Devices went to offline plui state

Reason for change: Created authorization token and passed the same for
json RPC call to networkmanager from network plugin.
Also added timer logic to subscribe for events.
Corrected SetInterfaceState and GetInterfaceState function calls.
Updated logger to print file name instead of function name.
Test Procedure: Do CDL and confirm the device boots up in online state
Risks: Low
Priority: P1
Signed-off-by: Gururaaja ESR <[email protected]>


* Update NetworkManagerPlugin.md
Co-authored-by: Karunakaran A <[email protected]>
  • Loading branch information
gururaajar authored Jun 1, 2024
1 parent d5dcd2b commit 2bda9e6
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 142 deletions.
42 changes: 30 additions & 12 deletions NetworkManager/LegacyPlugin_NetworkAPIs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace WPEFramework
{
_gNWInstance = this;
m_defaultInterface = "wlan0";
m_timer.connect(std::bind(&Network::subscribeToEvents, this));
RegisterLegacyMethods();
}

Expand Down Expand Up @@ -105,6 +106,28 @@ namespace WPEFramework

string callsign(NETWORK_MANAGER_CALLSIGN);

string token = "";

// TODO: use interfaces and remove token
auto security = m_service->QueryInterfaceByCallsign<PluginHost::IAuthenticate>("SecurityAgent");
if (security != nullptr) {
string payload = "http://localhost";
if (security->CreateToken(
static_cast<uint16_t>(payload.length()),
reinterpret_cast<const uint8_t*>(payload.c_str()),
token)
== Core::ERROR_NONE) {
std::cout << "DisplaySettings got security token" << std::endl;
} else {
std::cout << "DisplaySettings failed to get security token" << std::endl;
}
security->Release();
} else {
std::cout << "No security agent" << std::endl;
}

string query = "token=" + token;

auto interface = m_service->QueryInterfaceByCallsign<PluginHost::IShell>(callsign);
if (interface != nullptr)
{
Expand All @@ -123,16 +146,10 @@ namespace WPEFramework
}

Core::SystemInfo::SetEnvironment(_T("THUNDER_ACCESS"), (_T("127.0.0.1:9998")));
string query="token=";
m_networkmanager = make_shared<WPEFramework::JSONRPC::LinkType<WPEFramework::Core::JSON::IElement> >("org.rdk.NetworkManager", "");

/* Wait for Proxy stuff to be established */
sleep(3);

if (Core::ERROR_NONE != subscribeToEvents())
return string("Failed to Subscribe");
else
return string();
m_networkmanager = make_shared<WPEFramework::JSONRPC::LinkType<WPEFramework::Core::JSON::IElement> >(_T(NETWORK_MANAGER_CALLSIGN), _T(NETWORK_MANAGER_CALLSIGN), false, query);

m_timer.start(5000);
return string();
}

void Network::Deinitialize(PluginHost::IShell* /* service */)
Expand Down Expand Up @@ -754,7 +771,7 @@ const string CIDR_PREFIXES[CIDR_NETMASK_IP_LEN] = {
}

/** Private */
uint32_t Network::subscribeToEvents(void)
void Network::subscribeToEvents(void)
{
uint32_t errCode = Core::ERROR_GENERAL;
if (m_networkmanager)
Expand All @@ -781,7 +798,8 @@ const string CIDR_PREFIXES[CIDR_NETMASK_IP_LEN] = {
NMLOG_ERROR("Subscribe to onInternetStatusChange failed, errCode: %u", errCode);
}
}
return errCode;
if (errCode == Core::ERROR_NONE)
m_timer.stop();
}

string Network::getInterfaceMapping(const string & interface)
Expand Down
5 changes: 3 additions & 2 deletions NetworkManager/LegacyPlugin_NetworkAPIs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

#include "Module.h"
#include "core/Link.h"
#include "NetworkManagerTimer.h"

namespace WPEFramework {
namespace Plugin {

// This is a server for a JSONRPC communication channel.
// For a plugin to be capable to handle JSONRPC, inherit from PluginHost::JSONRPC.
// By inheriting from this class, the plugin realizes the interface PluginHost::IDispatcher.
Expand All @@ -30,7 +30,7 @@ namespace WPEFramework {

void RegisterLegacyMethods();
void UnregisterLegacyMethods();
uint32_t subscribeToEvents(void);
void subscribeToEvents(void);
static std::string getInterfaceMapping(const std::string &interface);
void activatePrimaryPlugin();

Expand Down Expand Up @@ -84,6 +84,7 @@ namespace WPEFramework {
PluginHost::IShell* m_service;
std::shared_ptr<WPEFramework::JSONRPC::LinkType<WPEFramework::Core::JSON::IElement>> m_networkmanager;
string m_defaultInterface;
NetworkManagerTimer m_timer;
};
} // namespace Plugin
} // namespace WPEFramework
40 changes: 28 additions & 12 deletions NetworkManager/LegacyPlugin_WiFiManagerAPIs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace WPEFramework
, m_service(nullptr)
{
_gWiFiInstance = this;
m_timer.connect(std::bind(&WiFiManager::subscribeToEvents, this));
RegisterLegacyMethods();
}

Expand Down Expand Up @@ -104,7 +105,27 @@ namespace WPEFramework
m_service->AddRef();

string callsign(NETWORK_MANAGER_CALLSIGN);
string token = "";

// TODO: use interfaces and remove token
auto security = m_service->QueryInterfaceByCallsign<PluginHost::IAuthenticate>("SecurityAgent");
if (security != nullptr) {
string payload = "http://localhost";
if (security->CreateToken(
static_cast<uint16_t>(payload.length()),
reinterpret_cast<const uint8_t*>(payload.c_str()),
token)
== Core::ERROR_NONE) {
std::cout << "DisplaySettings got security token" << std::endl;
} else {
std::cout << "DisplaySettings failed to get security token" << std::endl;
}
security->Release();
} else {
std::cout << "No security agent" << std::endl;
}

string query = "token=" + token;
auto interface = m_service->QueryInterfaceByCallsign<PluginHost::IShell>(callsign);
if (interface != nullptr)
{
Expand All @@ -123,16 +144,10 @@ namespace WPEFramework
}

Core::SystemInfo::SetEnvironment(_T("THUNDER_ACCESS"), (_T("127.0.0.1:9998")));
string query="token=";
m_networkmanager = make_shared<WPEFramework::JSONRPC::LinkType<WPEFramework::Core::JSON::IElement> >("org.rdk.NetworkManager", "");

/* Wait for Proxy stuff to be established */
sleep(3);

if (Core::ERROR_NONE != subscribeToEvents())
return string("Failed to Subscribe");
else
return string();
m_networkmanager = make_shared<WPEFramework::JSONRPC::LinkType<WPEFramework::Core::JSON::IElement> >(_T(NETWORK_MANAGER_CALLSIGN), _T(NETWORK_MANAGER_CALLSIGN), false, query);

m_timer.start(5000);
return string();
}

void WiFiManager::Deinitialize(PluginHost::IShell* /* service */)
Expand Down Expand Up @@ -472,7 +487,7 @@ namespace WPEFramework
}

/** Private */
uint32_t WiFiManager::subscribeToEvents(void)
void WiFiManager::subscribeToEvents(void)
{
uint32_t errCode = Core::ERROR_GENERAL;
if (m_networkmanager)
Expand All @@ -493,7 +508,8 @@ namespace WPEFramework
NMLOG_ERROR("Subscribe to onActiveInterfaceChange failed, errCode: %u", errCode);
}
}
return errCode;
if (errCode == Core::ERROR_NONE)
m_timer.stop();
}

/** Event Handling and Publishing */
Expand Down
4 changes: 3 additions & 1 deletion NetworkManager/LegacyPlugin_WiFiManagerAPIs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include "Module.h"
#include "NetworkManagerTimer.h"

namespace WPEFramework {

Expand Down Expand Up @@ -84,13 +85,14 @@ namespace WPEFramework {
private:
void RegisterLegacyMethods();
void UnregisterLegacyMethods();
uint32_t subscribeToEvents(void);
void subscribeToEvents(void);
static std::string getInterfaceMapping(const std::string &interface);
void activatePrimaryPlugin();

private:
PluginHost::IShell* m_service;
std::shared_ptr<WPEFramework::JSONRPC::LinkType<WPEFramework::Core::JSON::IElement>> m_networkmanager;
NetworkManagerTimer m_timer;
};
} // namespace Plugin
} // namespace WPEFramework
2 changes: 1 addition & 1 deletion NetworkManager/NetworkManagerJsonRpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ namespace WPEFramework
LOGINFOMETHOD();
uint32_t rc = Core::ERROR_GENERAL;
string interface = parameters["interface"].String();
bool enabled = parameters["enable"].Boolean();
bool enabled = parameters["enabled"].Boolean();
if (_NetworkManager)
rc = _NetworkManager->SetInterfaceState(interface, enabled);
else
Expand Down
2 changes: 1 addition & 1 deletion NetworkManager/NetworkManagerLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace NetworkManagerLogger {
gettimeofday(&tv, NULL);
lt = localtime(&tv.tv_sec);

printf("%.2d:%.2d:%.2d.%.6lld %-10s %s:%d : %s\n", lt->tm_hour, lt->tm_min, lt->tm_sec, (long long int)tv.tv_usec, levelMap[level], func, line, formattedLog);
printf("%.2d:%.2d:%.2d.%.6lld %-10s %s:%d : %s\n", lt->tm_hour, lt->tm_min, lt->tm_sec, (long long int)tv.tv_usec, levelMap[level], basename(file), line, formattedLog);
fflush(stdout);
#endif
}
Expand Down
11 changes: 5 additions & 6 deletions NetworkManager/NetworkManagerRDKProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ namespace WPEFramework
return rc;
}

iarmData.isInterfaceEnabled = true;
iarmData.isInterfaceEnabled = enable;
iarmData.persist = true;
if (IARM_RESULT_SUCCESS == IARM_Bus_Call (IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_setInterfaceEnabled, (void *)&iarmData, sizeof(iarmData)))
{
Expand All @@ -671,7 +671,7 @@ namespace WPEFramework
return rc;
}

uint32_t NetworkManagerImplementation::GetInterfaceState(const string& interface/* @in */, bool& isEnabled /* @out */)
uint32_t NetworkManagerImplementation::GetInterfaceState(const string& interface/* @in */, bool &isEnabled /* @out */)
{
LOG_ENTRY_FUNCTION();
uint32_t rc = Core::ERROR_RPC_CALL_FAILED;
Expand All @@ -689,11 +689,10 @@ namespace WPEFramework
return rc;
}

iarmData.isInterfaceEnabled = false;
iarmData.persist = true;
if (IARM_RESULT_SUCCESS == IARM_Bus_Call (IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_setInterfaceEnabled, (void *)&iarmData, sizeof(iarmData)))
if (IARM_RESULT_SUCCESS == IARM_Bus_Call (IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_isInterfaceEnabled, (void *)&iarmData, sizeof(iarmData)))
{
NMLOG_INFO ("Call to %s for %s success", IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_setInterfaceEnabled);
NMLOG_TRACE("Call to %s for %s success", IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETSRVMGR_API_isInterfaceEnabled);
isEnabled = iarmData.isInterfaceEnabled;
rc = Core::ERROR_NONE;
}
else
Expand Down
132 changes: 132 additions & 0 deletions NetworkManager/NetworkManagerTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

namespace WPEFramework {

namespace Plugin {
class NetworkManagerTimer {
private:
class NetworkManagerTimerJob {
private:
NetworkManagerTimerJob() = delete;
NetworkManagerTimerJob& operator=(const NetworkManagerTimerJob& RHS) = delete;

public:
NetworkManagerTimerJob(NetworkManagerTimer* tpt)
: m_tptimer(tpt)
{
}
NetworkManagerTimerJob(const NetworkManagerTimerJob& copy)
: m_tptimer(copy.m_tptimer)
{
}
~NetworkManagerTimerJob() {}

inline bool operator==(const NetworkManagerTimerJob& RHS) const
{
return (m_tptimer == RHS.m_tptimer);
}

public:
uint64_t Timed(const uint64_t scheduledTime)
{
if (m_tptimer) {
m_tptimer->Timed();
}
return 0;
}

private:
NetworkManagerTimer* m_tptimer;
};

public:
NetworkManagerTimer()
: baseTimer(64 * 1024, "TimerUtility")
, m_timerJob(this)
, m_isActive(false)
, m_isSingleShot(false)
, m_intervalInMs(-1)
{
}
~NetworkManagerTimer()
{
stop();
}

bool isActive()
{
return m_isActive;
}
void stop()
{
baseTimer.Revoke(m_timerJob);
m_isActive = false;
}
void start()
{
baseTimer.Revoke(m_timerJob);
baseTimer.Schedule(Core::Time::Now().Add(m_intervalInMs), m_timerJob);
m_isActive = true;
}
void start(int msec)
{
setInterval(msec);
start();
}
void setSingleShot(bool val)
{
m_isSingleShot = val;
}
void setInterval(int msec)
{
m_intervalInMs = msec;
}

void connect(std::function<void()> callback)
{
onTimeoutCallback = callback;
}

private:
void Timed()
{
if (onTimeoutCallback != nullptr) {
onTimeoutCallback();
}

if (m_isActive) {
if (m_isSingleShot) {
stop();
} else {
start();
}
}
}

WPEFramework::Core::TimerType<NetworkManagerTimerJob> baseTimer;
NetworkManagerTimerJob m_timerJob;
bool m_isActive;
bool m_isSingleShot;
int m_intervalInMs;

std::function<void()> onTimeoutCallback;
};
}
}
Loading

0 comments on commit 2bda9e6

Please sign in to comment.