From 3692632373e8e82dba92bec56f9e6082b430a829 Mon Sep 17 00:00:00 2001 From: Anand Kandasamy Date: Sun, 6 Dec 2020 14:50:31 -0500 Subject: [PATCH] Cleanup versions support in RDKServices --- Bluetooth/Bluetooth.cpp | 19 +++-------- SystemServices/SystemServices.cpp | 22 +++---------- helpers/AbstractPlugin.h | 52 ++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 38 deletions(-) diff --git a/Bluetooth/Bluetooth.cpp b/Bluetooth/Bluetooth.cpp index 1736b3f676..afac19c6ba 100644 --- a/Bluetooth/Bluetooth.cpp +++ b/Bluetooth/Bluetooth.cpp @@ -32,7 +32,7 @@ // For example, the exposed "startScan" method is mapped to "startScanWrapper()" and that one calls to "startDeviceDiscovery()" internally, // which finally calls to "BTRMGR_StartDeviceDiscovery()" in Bluetooth Manager. -const short WPEFramework::Plugin::Bluetooth::API_VERSION_NUMBER_MAJOR = 1; // corresponds to org.rdk.Bluetooth_5 +const short WPEFramework::Plugin::Bluetooth::API_VERSION_NUMBER_MAJOR = 2; // corresponds to org.rdk.Bluetooth_5 const short WPEFramework::Plugin::Bluetooth::API_VERSION_NUMBER_MINOR = 0; const string WPEFramework::Plugin::Bluetooth::SERVICE_NAME = "org.rdk.Bluetooth"; const string WPEFramework::Plugin::Bluetooth::METHOD_START_SCAN = "startScan"; @@ -132,15 +132,13 @@ namespace WPEFramework } Bluetooth::Bluetooth() - : AbstractPlugin() + : AbstractPlugin(Bluetooth::API_VERSION_NUMBER_MAJOR) // pass the current supported version , m_apiVersionNumber(API_VERSION_NUMBER_MAJOR) , m_discoveryRunning(false) , m_discoveryTimer(this) { LOGINFO(); - Core::JSONRPC::Handler& systemVersion_2 = JSONRPC::CreateHandler({ 2 }, *this); - Bluetooth::_instance = this; registerMethod(METHOD_GET_API_VERSION_NUMBER, &Bluetooth::getApiVersionNumber, this); registerMethod(METHOD_START_SCAN, &Bluetooth::startScanWrapper, this); @@ -165,8 +163,9 @@ namespace WPEFramework registerMethod(METHOD_GET_AUDIO_INFO, &Bluetooth::getMediaTrackInfoWrapper, this); registerMethod(METHOD_GET_STATUS_SUPPORT, &Bluetooth::getStatusSupportWrapper, this); - systemVersion_2.Register(METHOD_SET_PROPERTIES, &Bluetooth::setPropertiesWrapper, this); - systemVersion_2.Register(METHOD_GET_PROPERTIES, &Bluetooth::getPropertiesWrapper, this); + //version 2 APIs + registerMethod(METHOD_SET_PROPERTIES, &Bluetooth::setPropertiesWrapper, this, {2}); + registerMethod(METHOD_GET_PROPERTIES, &Bluetooth::getPropertiesWrapper, this, {2}); BTRMGR_Result_t rc = BTRMGR_RESULT_SUCCESS; rc = BTRMGR_Init(); @@ -183,14 +182,6 @@ namespace WPEFramework { LOGINFO(); - Core::JSONRPC::Handler* systemVersion_2 = JSONRPC::GetHandler(2); - if (systemVersion_2) { - systemVersion_2->Unregister(METHOD_SET_PROPERTIES); - systemVersion_2->Unregister(METHOD_GET_PROPERTIES); - } - else - LOGERR("Failed to get handler for version 2"); - Bluetooth::_instance = nullptr; if (m_executionThread.joinable()) diff --git a/SystemServices/SystemServices.cpp b/SystemServices/SystemServices.cpp index 83b610bdc8..b920b05606 100644 --- a/SystemServices/SystemServices.cpp +++ b/SystemServices/SystemServices.cpp @@ -248,11 +248,9 @@ namespace WPEFramework { * Register SystemService module as wpeframework plugin */ SystemServices::SystemServices() - : AbstractPlugin() + : AbstractPlugin(2) , m_cacheService(SYSTEM_SERVICE_SETTINGS_FILE) { - Core::JSONRPC::Handler& systemVersion_2 = JSONRPC::CreateHandler({ 2 }, *this); - SystemServices::_instance = this; //Initialise timer with interval and callback function. @@ -358,26 +356,16 @@ namespace WPEFramework { registerMethod("getNetworkStandbyMode", &SystemServices::getNetworkStandbyMode, this); registerMethod("getPowerStateIsManagedByDevice", &SystemServices::getPowerStateIsManagedByDevice, this); - systemVersion_2.Register(_T("getTimeZones"), &SystemServices::getTimeZones, this); + // version 2 APIs + registerMethod(_T("getTimeZones"), &SystemServices::getTimeZones, this, {2}); #ifdef ENABLE_DEEP_SLEEP - systemVersion_2.Register(_T("getWakeupReason"),&SystemServices::getWakeupReason, this); + registerMethod(_T("getWakeupReason"),&SystemServices::getWakeupReason, this, {2}); #endif - systemVersion_2.Register("uploadLogs", &SystemServices::uploadLogs, this); + registerMethod("uploadLogs", &SystemServices::uploadLogs, this, {2}); } SystemServices::~SystemServices() { - Core::JSONRPC::Handler* systemVersion_2 = JSONRPC::GetHandler(2); - if (systemVersion_2) { - systemVersion_2->Unregister("getTimeZones"); -#ifdef ENABLE_DEEP_SLEEP - systemVersion_2->Unregister("getWakeupReason"); -#endif - systemVersion_2->Unregister("uploadLogs"); - } - else - LOGERR("Failed to get handler for version 2"); - SystemServices::_instance = nullptr; } diff --git a/helpers/AbstractPlugin.h b/helpers/AbstractPlugin.h index 53376101b4..a14108b984 100644 --- a/helpers/AbstractPlugin.h +++ b/helpers/AbstractPlugin.h @@ -19,7 +19,7 @@ #pragma once -//#include "Module.h" +#include namespace WPEFramework { @@ -54,26 +54,63 @@ namespace WPEFramework { //End methods protected: + + //registerMethod to register a method in all versions template void registerMethod(const string& methodName, const METHOD& method, REALOBJECT* objectPtr) { - Register(methodName, method, objectPtr); - m_registeredMethods.push_back(methodName); + for(uint8_t ver = 1; ver <= m_currVersion; ver++) + { + auto handler = m_versionHandlers.find(ver); + if(handler != m_versionHandlers.end()) + handler->second->Register(methodName, method, objectPtr); + } + } + + //registerMethod to register a method in specific versions + template + void registerMethod(const string& methodName, const METHOD& method, REALOBJECT* objectPtr, const std::vector versions) + { + for(auto ver : versions) + { + auto handler = m_versionHandlers.find(ver); + if(handler != m_versionHandlers.end()) + handler->second->Register(methodName, method, objectPtr); + } } public: - AbstractPlugin() : PluginHost::JSONRPC() + AbstractPlugin() : PluginHost::JSONRPC(), m_currVersion(1) { LOGINFO(); + // For default constructor assume that only version 1 is supported. + // Also version 1 handler would always be the current object. + m_versionHandlers[1] = GetHandler(1); + registerMethod("getQuirks", &AbstractPlugin::getQuirks, this); } - virtual ~AbstractPlugin() + AbstractPlugin(const uint8_t currVersion) : PluginHost::JSONRPC(), m_currVersion(currVersion) { LOGINFO(); - for (auto& i : m_registeredMethods) { Unregister(i); } + // Create handlers for all the versions upto m_currVersion + m_versionHandlers[1] = GetHandler(1); + for(uint8_t i = 2; i <= m_currVersion; i++) + { + std::vector vec; + vec.push_back(i); + CreateHandler(vec); + m_versionHandlers[i] = GetHandler(i); + } + + registerMethod("getQuirks", &AbstractPlugin::getQuirks, this); + } + + virtual ~AbstractPlugin() + { + LOGINFO(); } //Build QueryInterface implementation, specifying all possible interfaces to be returned. @@ -101,7 +138,8 @@ namespace WPEFramework { return(string()); } private: - std::vector m_registeredMethods; + std::unordered_map m_versionHandlers; + uint8_t m_currVersion; // current supported version }; } // namespace Plugin } // namespace WPEFramework