diff --git a/NetworkManager/LegacyPlugin_NetworkAPIs.cpp b/NetworkManager/LegacyPlugin_NetworkAPIs.cpp index eca0d45bad..153857c09b 100644 --- a/NetworkManager/LegacyPlugin_NetworkAPIs.cpp +++ b/NetworkManager/LegacyPlugin_NetworkAPIs.cpp @@ -68,6 +68,7 @@ namespace WPEFramework { _gNWInstance = this; m_defaultInterface = "wlan0"; + m_timer.connect(std::bind(&Network::subscribeToEvents, this)); RegisterLegacyMethods(); } @@ -105,6 +106,28 @@ namespace WPEFramework string callsign(NETWORK_MANAGER_CALLSIGN); + string token = ""; + + // TODO: use interfaces and remove token + auto security = m_service->QueryInterfaceByCallsign("SecurityAgent"); + if (security != nullptr) { + string payload = "http://localhost"; + if (security->CreateToken( + static_cast(payload.length()), + reinterpret_cast(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(callsign); if (interface != nullptr) { @@ -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 >("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 >(_T(NETWORK_MANAGER_CALLSIGN), _T(NETWORK_MANAGER_CALLSIGN), false, query); + + m_timer.start(5000); + return string(); } void Network::Deinitialize(PluginHost::IShell* /* service */) @@ -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) @@ -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) diff --git a/NetworkManager/LegacyPlugin_NetworkAPIs.h b/NetworkManager/LegacyPlugin_NetworkAPIs.h index 107c066f91..f4bd0c6da2 100644 --- a/NetworkManager/LegacyPlugin_NetworkAPIs.h +++ b/NetworkManager/LegacyPlugin_NetworkAPIs.h @@ -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. @@ -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(); @@ -84,6 +84,7 @@ namespace WPEFramework { PluginHost::IShell* m_service; std::shared_ptr> m_networkmanager; string m_defaultInterface; + NetworkManagerTimer m_timer; }; } // namespace Plugin } // namespace WPEFramework diff --git a/NetworkManager/LegacyPlugin_WiFiManagerAPIs.cpp b/NetworkManager/LegacyPlugin_WiFiManagerAPIs.cpp index f0f3395fc4..0ab3abac67 100644 --- a/NetworkManager/LegacyPlugin_WiFiManagerAPIs.cpp +++ b/NetworkManager/LegacyPlugin_WiFiManagerAPIs.cpp @@ -68,6 +68,7 @@ namespace WPEFramework , m_service(nullptr) { _gWiFiInstance = this; + m_timer.connect(std::bind(&WiFiManager::subscribeToEvents, this)); RegisterLegacyMethods(); } @@ -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("SecurityAgent"); + if (security != nullptr) { + string payload = "http://localhost"; + if (security->CreateToken( + static_cast(payload.length()), + reinterpret_cast(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(callsign); if (interface != nullptr) { @@ -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 >("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 >(_T(NETWORK_MANAGER_CALLSIGN), _T(NETWORK_MANAGER_CALLSIGN), false, query); + + m_timer.start(5000); + return string(); } void WiFiManager::Deinitialize(PluginHost::IShell* /* service */) @@ -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) @@ -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 */ diff --git a/NetworkManager/LegacyPlugin_WiFiManagerAPIs.h b/NetworkManager/LegacyPlugin_WiFiManagerAPIs.h index d6155ea5ec..300ca767b2 100644 --- a/NetworkManager/LegacyPlugin_WiFiManagerAPIs.h +++ b/NetworkManager/LegacyPlugin_WiFiManagerAPIs.h @@ -20,6 +20,7 @@ #pragma once #include "Module.h" +#include "NetworkManagerTimer.h" namespace WPEFramework { @@ -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> m_networkmanager; + NetworkManagerTimer m_timer; }; } // namespace Plugin } // namespace WPEFramework diff --git a/NetworkManager/NetworkManagerJsonRpc.cpp b/NetworkManager/NetworkManagerJsonRpc.cpp index 4d39ca4b97..0271ccf789 100644 --- a/NetworkManager/NetworkManagerJsonRpc.cpp +++ b/NetworkManager/NetworkManagerJsonRpc.cpp @@ -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 diff --git a/NetworkManager/NetworkManagerLogger.cpp b/NetworkManager/NetworkManagerLogger.cpp index 3432b75705..9ea3b9d534 100644 --- a/NetworkManager/NetworkManagerLogger.cpp +++ b/NetworkManager/NetworkManagerLogger.cpp @@ -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 } diff --git a/NetworkManager/NetworkManagerRDKProxy.cpp b/NetworkManager/NetworkManagerRDKProxy.cpp index 97bf449d3c..b98d99cc1e 100644 --- a/NetworkManager/NetworkManagerRDKProxy.cpp +++ b/NetworkManager/NetworkManagerRDKProxy.cpp @@ -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))) { @@ -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; @@ -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 diff --git a/NetworkManager/NetworkManagerTimer.h b/NetworkManager/NetworkManagerTimer.h new file mode 100644 index 0000000000..5fbeea3524 --- /dev/null +++ b/NetworkManager/NetworkManagerTimer.h @@ -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 callback) + { + onTimeoutCallback = callback; + } + + private: + void Timed() + { + if (onTimeoutCallback != nullptr) { + onTimeoutCallback(); + } + + if (m_isActive) { + if (m_isSingleShot) { + stop(); + } else { + start(); + } + } + } + + WPEFramework::Core::TimerType baseTimer; + NetworkManagerTimerJob m_timerJob; + bool m_isActive; + bool m_isSingleShot; + int m_intervalInMs; + + std::function onTimeoutCallback; + }; + } +} diff --git a/docs/api/NetworkManagerPlugin.md b/docs/api/NetworkManagerPlugin.md index 88b30ccfe1..e924017e39 100644 --- a/docs/api/NetworkManagerPlugin.md +++ b/docs/api/NetworkManagerPlugin.md @@ -2,7 +2,7 @@ # NetworkManager Plugin -**Version: [0.1.0]()** +**Version: [0.2.0]()** A NetworkManager plugin for Thunder framework. @@ -58,6 +58,8 @@ NetworkManager interface methods: | [GetAvailableInterfaces](#method.GetAvailableInterfaces) | Get device supported list of available interface including their state | | [GetPrimaryInterface](#method.GetPrimaryInterface) | Gets the primary/default network interface for the device | | [SetPrimaryInterface](#method.SetPrimaryInterface) | Sets the primary/default interface for the device | +| [SetInterfaceState](#method.SetInterfaceState) | Enable the interface | +| [GetInterfaceState](#method.GetInterfaceState) | Disable the interface | | [GetIPSettings](#method.GetIPSettings) | Gets the IP setting for the given interface | | [SetIPSettings](#method.SetIPSettings) | Sets the IP settings for the given interface | | [GetStunEndpoint](#method.GetStunEndpoint) | Get the STUN Endpoint that is used to identify public IP of the device | @@ -84,8 +86,6 @@ NetworkManager interface methods: | [GetWiFiSignalStrength](#method.GetWiFiSignalStrength) | Get WiFiSignalStrength of connected SSID | | [GetSupportedSecurityModes](#method.GetSupportedSecurityModes) | Returns the Wifi security modes that the device supports | | [SetLogLevel](#method.SetLogLevel) | Set Log level for more information | -| [EnableInterface](#method.EnableInterface) | Enable the interface | -| [DisableInterface](#method.DisableInterface) | Disable the interface | | [GetWifiState](#method.GetWifiState) | Returns the current Wifi State | @@ -247,6 +247,112 @@ Sets the primary/default interface for the device. This call fails if the interf } ``` + +## *SetInterfaceState [method](#head.Methods)* + +Enable or Disable the specified interface. + +### Events + +| Event | Description | +| :-------- | :-------- | +| [onInterfaceStateChange](#event.onInterfaceStateChange) | Triggered when interface’s status changes to enabled or disabled. | + +### Parameters + +| Name | Type | Description | +| :-------- | :-------- | :-------- | +| params | object | | +| params.interface | string | An interface, such as `eth0` or `wlan0`, depending upon availability of the given interface in `GetAvailableInterfaces` | +| params.enabled | boolean | Set the state of the interface to be Enabled or Disabled | + +### Result + +| Name | Type | Description | +| :-------- | :-------- | :-------- | +| result | object | | +| result.success | boolean | Whether the request succeeded | + +### Example + +#### Request + +```json +{ + "jsonrpc": "2.0", + "id": 42, + "method": "org.rdk.NetworkManager.SetInterfaceState", + "params": { + "interface": "wlan0", + "enabled": true + } +} +``` + +#### Response + +```json +{ + "jsonrpc": "2.0", + "id": 42, + "result": { + "success": true + } +} +``` + + +## *GetInterfaceState [method](#head.Methods)* + +Disable the specified interface. + +### Events + +No Events + +### Parameters + +| Name | Type | Description | +| :-------- | :-------- | :-------- | +| params | object | | +| params.interface | string | An interface, such as `eth0` or `wlan0`, depending upon availability of the given interface in `GetAvailableInterfaces` | + +### Result + +| Name | Type | Description | +| :-------- | :-------- | :-------- | +| result | object | | +| result.isEnabled | boolean | Whether the Interface is enabled or disabled | +| result.success | boolean | Whether the request succeeded | + +### Example + +#### Request + +```json +{ + "jsonrpc": "2.0", + "id": 42, + "method": "org.rdk.NetworkManager.GetInterfaceState", + "params": { + "interface": "wlan0" + } +} +``` + +#### Response + +```json +{ + "jsonrpc": "2.0", + "id": 42, + "result": { + "isEnabled": true, + "success": true + } +} +``` + ## *GetIPSettings [method](#head.Methods)* @@ -1696,110 +1802,6 @@ No Events } ``` - -## *EnableInterface [method](#head.Methods)* - -Enable the specified interface. - -### Events - -| Event | Description | -| :-------- | :-------- | -| [onInterfaceStateChange](#event.onInterfaceStateChange) | Triggered when interface’s status changes to enabled. | - -### Parameters - -| Name | Type | Description | -| :-------- | :-------- | :-------- | -| params | object | | -| params.interface | string | An interface, such as `eth0` or `wlan0`, depending upon availability of the given interface in `GetAvailableInterfaces` | - -### Result - -| Name | Type | Description | -| :-------- | :-------- | :-------- | -| result | object | | -| result.success | boolean | Whether the request succeeded | - -### Example - -#### Request - -```json -{ - "jsonrpc": "2.0", - "id": 42, - "method": "org.rdk.NetworkManager.EnableInterface", - "params": { - "interface": "wlan0" - } -} -``` - -#### Response - -```json -{ - "jsonrpc": "2.0", - "id": 42, - "result": { - "success": true - } -} -``` - - -## *DisableInterface [method](#head.Methods)* - -Disable the specified interface. - -### Events - -| Event | Description | -| :-------- | :-------- | -| [onInterfaceStateChange](#event.onInterfaceStateChange) | Triggered when interface’s status changes to disabled. | - -### Parameters - -| Name | Type | Description | -| :-------- | :-------- | :-------- | -| params | object | | -| params.interface | string | An interface, such as `eth0` or `wlan0`, depending upon availability of the given interface in `GetAvailableInterfaces` | - -### Result - -| Name | Type | Description | -| :-------- | :-------- | :-------- | -| result | object | | -| result.success | boolean | Whether the request succeeded | - -### Example - -#### Request - -```json -{ - "jsonrpc": "2.0", - "id": 42, - "method": "org.rdk.NetworkManager.DisableInterface", - "params": { - "interface": "wlan0" - } -} -``` - -#### Response - -```json -{ - "jsonrpc": "2.0", - "id": 42, - "result": { - "success": true - } -} -``` - ## *GetWifiState [method](#head.Methods)*