From 2bb4146c58b4f1f9f35270fa6371dc9fbd793093 Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Wed, 22 Jan 2025 15:52:21 +0000 Subject: [PATCH 01/16] PowerManagerClient library implementation --- .gitignore | 2 + CMakeLists.txt | 3 + Source/CMakeLists.txt | 4 + Source/powermanager/CMakeLists.txt | 79 ++ Source/powermanager/Module.cpp | 22 + Source/powermanager/Module.h | 34 + Source/powermanager/powermanager_client.cpp | 953 ++++++++++++++++++++ Source/powermanager/powermanager_client.h | 243 +++++ 8 files changed, 1340 insertions(+) create mode 100644 Source/powermanager/CMakeLists.txt create mode 100644 Source/powermanager/Module.cpp create mode 100644 Source/powermanager/Module.h create mode 100644 Source/powermanager/powermanager_client.cpp create mode 100644 Source/powermanager/powermanager_client.h diff --git a/.gitignore b/.gitignore index 9ed30d6a..1d22f629 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,5 @@ Source/playerinfo/player_info/player_info */*/*/*/*tests */*/generated */*/*/*/lib*.a +.cache +compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt index ace9de77..b1f29e6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,9 @@ option(CDMI "Include OpenCDM interface." OFF) option(CRYPTOGRAPHY "Include the cryptography library." OFF) +# TODO: enable conditionally from recipe file +option(POWERMANAGER + "Include the powermanager COMRPC abstraction library." ON) option(INSTALL_TESTS "Install the test applications" OFF) diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index af7578a5..9d87f181 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -58,3 +58,7 @@ endif() if(LOCALTRACER) add_subdirectory(localtracer) endif() + +if(POWERMANAGER) + add_subdirectory(powermanager) +endif() diff --git a/Source/powermanager/CMakeLists.txt b/Source/powermanager/CMakeLists.txt new file mode 100644 index 00000000..22210cde --- /dev/null +++ b/Source/powermanager/CMakeLists.txt @@ -0,0 +1,79 @@ +# If not stated otherwise in this file or this component's LICENSE file the +# following copyright and licenses apply: +# +# Copyright 2025 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. + +cmake_minimum_required(VERSION 3.3) + +find_package(WPEFramework) + +project(PowerManagerClient) + +project_version(4.4.1) + +set(TARGET ${NAMESPACE}${PROJECT_NAME}) + +message("Setup ${TARGET} v${PROJECT_VERSION}") + +find_package(${NAMESPACE}Core REQUIRED) +find_package(${NAMESPACE}COM REQUIRED) +find_package(CompileSettingsDebug CONFIG REQUIRED) + +set(PUBLIC_HEADERS "powermanager_client.h") + +add_library(${TARGET} SHARED + Module.cpp + powermanager_client.cpp +) + +target_link_libraries(${TARGET} + PRIVATE + ${NAMESPACE}Core::${NAMESPACE}Core + ${NAMESPACE}COM::${NAMESPACE}COM + CompileSettingsDebug::CompileSettingsDebug + ) + +set_target_properties(${TARGET} PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + FRAMEWORK FALSE + PUBLIC_HEADER "${PUBLIC_HEADERS}" # specify the public headers + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + ) + +target_include_directories( ${TARGET} + PUBLIC + $ + $ + ) + +install( + TARGETS ${TARGET} EXPORT ${TARGET}Targets # for downstream dependencies + ARCHIVE DESTINATION lib COMPONENT libs # static lib + LIBRARY DESTINATION lib COMPONENT libs # shared lib + RUNTIME DESTINATION bin COMPONENT libs # binaries + FRAMEWORK DESTINATION bin COMPONENT libs # for mac + PUBLIC_HEADER DESTINATION include/${NAMESPACE}/powermanager COMPONENT devel # headers for mac (note the different component -> different package) + INCLUDES DESTINATION include/${NAMESPACE}/powermanager # headers +) + +InstallCMakeConfig( + TARGETS ${TARGET}) + +InstallPackageConfig( + TARGETS ${TARGET} + DESCRIPTION "communications channel abstraction to get the device properties") + diff --git a/Source/powermanager/Module.cpp b/Source/powermanager/Module.cpp new file mode 100644 index 00000000..e924dc30 --- /dev/null +++ b/Source/powermanager/Module.cpp @@ -0,0 +1,22 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2025 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. + */ + +#include "Module.h" + +MODULE_NAME_DECLARATION(BUILD_REFERENCE) diff --git a/Source/powermanager/Module.h b/Source/powermanager/Module.h new file mode 100644 index 00000000..341c7448 --- /dev/null +++ b/Source/powermanager/Module.h @@ -0,0 +1,34 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2025 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. + */ + +#pragma once + +#ifndef MODULE_NAME +#define MODULE_NAME ClientLibrary_PowerManager +#endif + +#include +#include +#include +#include + +#if defined(__WINDOWS__) && defined(POWERMANAGER_EXPORTS) +#undef EXTERNAL +#define EXTERNAL EXTERNAL_EXPORT +#endif diff --git a/Source/powermanager/powermanager_client.cpp b/Source/powermanager/powermanager_client.cpp new file mode 100644 index 00000000..aca120f9 --- /dev/null +++ b/Source/powermanager/powermanager_client.cpp @@ -0,0 +1,953 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2025 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. + */ + +#include + +#include +#include + +#include +#include + +#include "powermanager_client.h" + +using namespace WPEFramework; +using PowerState = WPEFramework::Exchange::IPowerManager::PowerState; +using WakeupSrcType = WPEFramework::Exchange::IPowerManager::WakeupSrcType; +using WakeupReason = WPEFramework::Exchange::IPowerManager::WakeupReason; +using SystemMode = WPEFramework::Exchange::IPowerManager::SystemMode; +using ThermalTemperature = WPEFramework::Exchange::IPowerManager::ThermalTemperature; + +namespace /*unnamed*/ { + +const std::unordered_map& powerStateMap() +{ + static const std::unordered_map map = { + { PowerState::POWER_STATE_UNKNOWN, POWER_STATE_UNKNOWN }, + { PowerState::POWER_STATE_OFF, POWER_STATE_OFF }, + { PowerState::POWER_STATE_STANDBY, POWER_STATE_STANDBY }, + { PowerState::POWER_STATE_ON, POWER_STATE_ON }, + { PowerState::POWER_STATE_STANDBY_LIGHT_SLEEP, POWER_STATE_STANDBY_LIGHT_SLEEP }, + { PowerState::POWER_STATE_STANDBY_DEEP_SLEEP, POWER_STATE_STANDBY_DEEP_SLEEP }, + }; + + return map; +} + +PowerManager_PowerState_t convert(const PowerState from) +{ + auto& map = powerStateMap(); + auto it = map.find(from); + return (it != map.end()) ? it->second : POWER_STATE_UNKNOWN; +} + +PowerState convert(const PowerManager_PowerState_t from) +{ + auto& map = powerStateMap(); + + for (const auto& kv : map) { + if (kv.second == from) { + return kv.first; + } + } + return PowerState::POWER_STATE_UNKNOWN; +} + +// TODO: re-check if required +const std::unordered_map& wakeupSrcTypeMap() +{ + static const std::unordered_map map = { + { WakeupSrcType::WAKEUP_SRC_UNKNOWN, WAKEUP_SRC_UNKNOWN }, + { WakeupSrcType::WAKEUP_SRC_VOICE, WAKEUP_SRC_VOICE }, + { WakeupSrcType::WAKEUP_SRC_PRESENCEDETECTED, WAKEUP_SRC_PRESENCEDETECTED }, + { WakeupSrcType::WAKEUP_SRC_BLUETOOTH, WAKEUP_SRC_BLUETOOTH }, + { WakeupSrcType::WAKEUP_SRC_RF4CE, WAKEUP_SRC_RF4CE }, + { WakeupSrcType::WAKEUP_SRC_WIFI, WAKEUP_SRC_WIFI }, + { WakeupSrcType::WAKEUP_SRC_IR, WAKEUP_SRC_IR }, + { WakeupSrcType::WAKEUP_SRC_POWERKEY, WAKEUP_SRC_POWERKEY }, + { WakeupSrcType::WAKEUP_SRC_TIMER, WAKEUP_SRC_TIMER }, + { WakeupSrcType::WAKEUP_SRC_CEC, WAKEUP_SRC_CEC }, + { WakeupSrcType::WAKEUP_SRC_LAN, WAKEUP_SRC_LAN }, + }; + return map; +} + +PowerManager_WakeupSrcType_t convert(const WakeupSrcType from) +{ + auto& map = wakeupSrcTypeMap(); + auto it = map.find(from); + return (it != map.end()) ? it->second : WAKEUP_SRC_UNKNOWN; +} + +WakeupSrcType convert(const PowerManager_WakeupSrcType_t from) +{ + auto& map = wakeupSrcTypeMap(); + + for (const auto& kv : map) { + if (kv.second == from) { + return kv.first; + } + } + return WakeupSrcType::WAKEUP_SRC_UNKNOWN; +} + +const std::unordered_map& wakeupReasonMap() +{ + static const std::unordered_map map = { + { WakeupReason::WAKEUP_REASON_UNKNOWN, WAKEUP_REASON_UNKNOWN }, + { WakeupReason::WAKEUP_REASON_IR, WAKEUP_REASON_IR }, + { WakeupReason::WAKEUP_REASON_BLUETOOTH, WAKEUP_REASON_BLUETOOTH }, + { WakeupReason::WAKEUP_REASON_RF4CE, WAKEUP_REASON_RF4CE }, + { WakeupReason::WAKEUP_REASON_GPIO, WAKEUP_REASON_GPIO }, + { WakeupReason::WAKEUP_REASON_LAN, WAKEUP_REASON_LAN }, + { WakeupReason::WAKEUP_REASON_WIFI, WAKEUP_REASON_WIFI }, + { WakeupReason::WAKEUP_REASON_TIMER, WAKEUP_REASON_TIMER }, + { WakeupReason::WAKEUP_REASON_FRONTPANEL, WAKEUP_REASON_FRONTPANEL }, + { WakeupReason::WAKEUP_REASON_WATCHDOG, WAKEUP_REASON_WATCHDOG }, + { WakeupReason::WAKEUP_REASON_SOFTWARERESET, WAKEUP_REASON_SOFTWARERESET }, + { WakeupReason::WAKEUP_REASON_THERMALRESET, WAKEUP_REASON_THERMALRESET }, + { WakeupReason::WAKEUP_REASON_WARMRESET, WAKEUP_REASON_WARMRESET }, + { WakeupReason::WAKEUP_REASON_COLDBOOT, WAKEUP_REASON_COLDBOOT }, + { WakeupReason::WAKEUP_REASON_STRAUTHFAIL, WAKEUP_REASON_STRAUTHFAIL }, + { WakeupReason::WAKEUP_REASON_CEC, WAKEUP_REASON_CEC }, + { WakeupReason::WAKEUP_REASON_PRESENCE, WAKEUP_REASON_PRESENCE }, + { WakeupReason::WAKEUP_REASON_VOICE, WAKEUP_REASON_VOICE }, + }; + return map; +} + +PowerManager_WakeupReason_t convert(const WakeupReason from) +{ + auto& map = wakeupReasonMap(); + auto it = map.find(from); + return (it != map.end()) ? it->second : WAKEUP_REASON_UNKNOWN; +} + +// TODO: re-check required ? +WakeupReason convert(const PowerManager_WakeupReason_t from) +{ + auto& map = wakeupReasonMap(); + + for (const auto& kv : map) { + if (kv.second == from) { + return kv.first; + } + } + return WakeupReason::WAKEUP_REASON_UNKNOWN; +} + +const std::unordered_map& systemModeMap() +{ + static const std::unordered_map map = { + { SYSTEM_MODE_UNKNOWN, SystemMode::SYSTEM_MODE_UNKNOWN }, + { SYSTEM_MODE_NORMAL, SystemMode::SYSTEM_MODE_NORMAL }, + { SYSTEM_MODE_EAS, SystemMode::SYSTEM_MODE_EAS }, + { SYSTEM_MODE_WAREHOUSE, SystemMode::SYSTEM_MODE_WAREHOUSE }, + }; + return map; +} + +SystemMode convert(const PowerManager_SystemMode_t from) +{ + auto& map = systemModeMap(); + auto it = map.find(from); + return (it != map.end()) ? it->second : SystemMode::SYSTEM_MODE_UNKNOWN; +} + +const std::unordered_map& thermalTemperatureMap() +{ + static const std::unordered_map map = { + { ThermalTemperature::THERMAL_TEMPERATURE_UNKNOWN, THERMAL_TEMPERATURE_UNKNOWN }, + { ThermalTemperature::THERMAL_TEMPERATURE_NORMAL, THERMAL_TEMPERATURE_NORMAL }, + { ThermalTemperature::THERMAL_TEMPERATURE_HIGH, THERMAL_TEMPERATURE_HIGH }, + { ThermalTemperature::THERMAL_TEMPERATURE_CRITICAL, THERMAL_TEMPERATURE_CRITICAL }, + }; + return map; +} + +PowerManager_ThermalTemperature_t convert(const ThermalTemperature from) +{ + auto& map = thermalTemperatureMap(); + auto it = map.find(from); + return (it != map.end()) ? it->second : THERMAL_TEMPERATURE_UNKNOWN; +} + +static string Callsign() +{ + static constexpr const TCHAR Default[] = _T("org.rdk.PowerManager"); + return (Default); +} + +class PowerManagerClient : public RPC::SmartInterfaceType { +private: + using BaseClass = RPC::SmartInterfaceType; + using PowerModeChangedCallbacks = std::map; + using PowerModePreChangeCallbacks = std::map; + using DeepSleepTimeoutCallbacks = std::map; + using NetworkStandbyModeChangedCallbacks = std::map; + using ThermalModeChangedCallbacks = std::map; + using RebootBeginCallbacks = std::map; + + class Notification : public Exchange::IPowerManager::INotification { + private: + PowerManagerClient& _parent; + + public: + Notification(PowerManagerClient& parent) + : _parent(parent) + { + } + + virtual void OnPowerModeChanged(const PowerState& currentState, const PowerState& newState) override + { + _parent.NotifyPowerModeChanged(currentState, newState); + } + + virtual void OnPowerModePreChange(const PowerState& currentState, const PowerState& newState) override + { + _parent.NotifyPowerModePreChange(currentState, newState); + } + + virtual void OnDeepSleepTimeout(const int& wakeupTimeout) override + { + _parent.NotifyDeepSleepTimeout(wakeupTimeout); + } + + virtual void OnNetworkStandbyModeChanged(const bool& enabled) override + { + _parent.NotifyNetworkStandbyModeChanged(enabled); + } + + virtual void OnThermalModeChanged(const ThermalTemperature& currentThermalLevel, const ThermalTemperature& newThermalLevel, const float& currentTemperature) override + { + _parent.NotifyThermalModeChanged(currentThermalLevel, newThermalLevel, currentTemperature); + } + + virtual void OnRebootBegin(const string& rebootReasonCustom, const string& rebootReasonOther, const string& rebootRequestor) override + { + _parent.NotifyRebootBegin(rebootReasonCustom, rebootReasonOther, rebootRequestor); + } + + BEGIN_INTERFACE_MAP(Notification) + INTERFACE_ENTRY(Exchange::IPowerManager::INotification) + END_INTERFACE_MAP + }; + + PowerManagerClient() + : BaseClass() + , _lock() + , _powerManagerInterface(nullptr) + , _powerManagerNotification(*this) + { + ASSERT(_singleton == nullptr); + _singleton = this; + + uint32_t result = BaseClass::Open(RPC::CommunicationTimeOut, BaseClass::Connector(), Callsign()); + + PluginHost::IShell* controllerInterface = BaseClass::ControllerInterface(); + + // avoid compiler warnings + (void)controllerInterface; + } + + ~PowerManagerClient() + { + BaseClass::Close(Core::infinite); + ASSERT(_singleton != nullptr); + _singleton = nullptr; + } + + virtual void Operational(const bool upAndRunning) override + { + _lock.Lock(); + + if (upAndRunning) { + // PowerManager is Activated + if (_powerManagerInterface == nullptr) { + _powerManagerInterface = BaseClass::Interface(); + if (_powerManagerInterface != nullptr) { + _powerManagerInterface->Register(&_powerManagerNotification); + } + } + } else { + // PowerManager is Deactivated + if (_powerManagerInterface != nullptr) { + _powerManagerInterface->Unregister(&_powerManagerNotification); + _powerManagerInterface->Release(); + _powerManagerInterface = nullptr; + } + } + + _lock.Unlock(); + } + +public: + static PowerManagerClient& Instance() + { + static PowerManagerClient* instance = new PowerManagerClient; + ASSERT(instance != nullptr); + return *instance; + } + + // TODO: foresee CRASH if instance is used after Dispose + static void Dispose() + { + ASSERT(_singleton != nullptr); + if (_singleton != nullptr) { + delete _singleton; + } + } + + uint32_t GetPowerState(PowerManager_PowerState_t* currentState, PowerManager_PowerState_t* previousState) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + PowerState _currentState = PowerState::POWER_STATE_UNKNOWN; + PowerState _previousState = PowerState::POWER_STATE_UNKNOWN; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->GetPowerState(_currentState, _previousState); + } + + _lock.Unlock(); + + if (Core::ERROR_NONE == result) { + *currentState = convert(_currentState); + *previousState = convert(_previousState); + } + + return result; + } + + uint32_t SetPowerState(const int keyCode, const PowerManager_PowerState_t powerState, const char* reason) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + PowerState _powerState = convert(powerState); + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->SetPowerState(keyCode, _powerState, reason); + } + + _lock.Unlock(); + + return result; + } + + uint32_t GetThermalState(float* currentTemperature) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->GetThermalState(*currentTemperature); + } + + _lock.Unlock(); + + return result; + } + + uint32_t SetTemperatureThresholds(float high, float critical) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->SetTemperatureThresholds(high, critical); + } + + _lock.Unlock(); + + return result; + } + + uint32_t GetTemperatureThresholds(float* high, float* critical) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->GetTemperatureThresholds(*high, *critical); + } + + _lock.Unlock(); + + return result; + } + + uint32_t SetOvertempGraceInterval(const int graceInterval) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->SetOvertempGraceInterval(graceInterval); + } + + _lock.Unlock(); + + return result; + } + + uint32_t GetOvertempGraceInterval(int* graceInterval) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->GetOvertempGraceInterval(*graceInterval); + } + + _lock.Unlock(); + + return result; + } + + uint32_t SetDeepSleepTimer(const int timeOut) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->SetDeepSleepTimer(timeOut); + } + + _lock.Unlock(); + + return result; + } + + uint32_t GetLastWakeupReason(PowerManager_WakeupReason_t* wakeupReason) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + WakeupReason _wakeupReason = WakeupReason::WAKEUP_REASON_UNKNOWN; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->GetLastWakeupReason(_wakeupReason); + } + + _lock.Unlock(); + + if (Core::ERROR_NONE == result) { + *wakeupReason = convert(_wakeupReason); + } + + return result; + } + + uint32_t GetLastWakeupKeyCode(int* keycode) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + _powerManagerInterface->GetLastWakeupKeyCode(*keycode); + } + + _lock.Unlock(); + + return result; + } + + uint32_t Reboot(const char* rebootRequestor, const char* rebootReasonCustom, const char* rebootReasonOther) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->Reboot(rebootRequestor, rebootReasonCustom, rebootReasonOther); + } + + _lock.Unlock(); + + return result; + } + + uint32_t SetNetworkStandbyMode(const bool standbyMode) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->SetNetworkStandbyMode(standbyMode); + } + + _lock.Unlock(); + + return result; + } + uint32_t GetNetworkStandbyMode(bool* standbyMode) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->GetNetworkStandbyMode(*standbyMode); + } + + _lock.Unlock(); + + return result; + } + + uint32_t SetWakeupSrcConfig(const int powerMode, const int wakeSrcType, int config) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->SetWakeupSrcConfig(powerMode, wakeSrcType, config); + } + + _lock.Unlock(); + + return result; + } + + uint32_t GetWakeupSrcConfig(int& powerMode, int& srcType, int& config) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->GetWakeupSrcConfig(powerMode, srcType, config); + } + + _lock.Unlock(); + + return result; + } + + uint32_t SetSystemMode(const PowerManager_SystemMode_t currentMode, const PowerManager_SystemMode_t newMode) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + SystemMode _currentMode = convert(currentMode); + SystemMode _newMode = convert(newMode); + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->SetSystemMode(_currentMode, _newMode); + } + + _lock.Unlock(); + + return result; + } + + uint32_t GetPowerStateBeforeReboot(PowerManager_PowerState_t* powerStateBeforeReboot) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + PowerState _powerStateBeforeReboot = PowerState::POWER_STATE_UNKNOWN; + + _lock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->GetPowerStateBeforeReboot(_powerStateBeforeReboot); + } + + _lock.Unlock(); + + if (Core::ERROR_NONE == result) { + *powerStateBeforeReboot = convert(_powerStateBeforeReboot); + } + + return result; + } + + void NotifyPowerModeChanged(const PowerState& currentState, const PowerState& newState) + { + PowerManager_PowerState_t _currentState = convert(currentState); + PowerManager_PowerState_t _newState = convert(newState); + _lock.Lock(); + + for (auto& index : _powerModeChanedCallbacks) { + index.first(_currentState, _newState, index.second); + } + + _lock.Unlock(); + } + + void NotifyPowerModePreChange(const PowerState& currentState, const PowerState& newState) + { + PowerManager_PowerState_t _currentState = convert(currentState); + PowerManager_PowerState_t _newState = convert(newState); + _lock.Lock(); + + for (auto& index : _powerModePreChangeCallbacks) { + index.first(_currentState, _newState, index.second); + } + + _lock.Unlock(); + } + + void NotifyDeepSleepTimeout(const int& wakeupTimeout) + { + _lock.Lock(); + + for (auto& index : _deepSleepTimeoutCallbacks) { + index.first(wakeupTimeout, index.second); + } + + _lock.Unlock(); + } + + void NotifyNetworkStandbyModeChanged(const bool& enabled) + { + _lock.Lock(); + + for (auto& index : _networkStandbyModeChangedCallbacks) { + index.first(enabled, index.second); + } + + _lock.Unlock(); + } + + void NotifyThermalModeChanged(const ThermalTemperature& currentThermalLevel, const ThermalTemperature& newThermalLevel, const float& currentTemperature) + { + PowerManager_ThermalTemperature_t _currentThermalLevel = convert(currentThermalLevel); + PowerManager_ThermalTemperature_t _newThermalLevel = convert(newThermalLevel); + + _lock.Lock(); + + for (auto& index : _thermalModeChangedCallbacks) { + index.first(_currentThermalLevel, _newThermalLevel, currentTemperature, index.second); + } + + _lock.Unlock(); + } + + void NotifyRebootBegin(const string& rebootReasonCustom, const string& rebootReasonOther, const string& rebootRequestor) + { + _lock.Lock(); + + for (auto& index : _rebootBeginCallbacks) { + index.first(rebootReasonCustom.c_str(), rebootReasonOther.c_str(), rebootRequestor.c_str(), index.second); + } + + _lock.Unlock(); + } + + // Generic template function for callback register + template + uint32_t RegisterCallback(std::map& callbackMap, CallbackType callback, void* userdata) + { + uint32_t result = Core::ERROR_ALREADY_CONNECTED; + + ASSERT(callback != nullptr); + + _lock.Lock(); + if (callbackMap.find(callback) == callbackMap.end()) { + callbackMap.emplace(std::piecewise_construct, + std::forward_as_tuple(callback), + std::forward_as_tuple(userdata)); + + result = Core::ERROR_NONE; + } + _lock.Unlock(); + + return result; + } + + // Generic template function for callback unregister + template + uint32_t UnRegisterCallback(std::map& callbackMap, CallbackType callback) + { + uint32_t result = Core::ERROR_ALREADY_RELEASED; + + ASSERT(callback != nullptr); + + _lock.Lock(); + + auto it = callbackMap.find(callback); + + if (it != callbackMap.end()) { + callbackMap.erase(it); + result = Core::ERROR_NONE; + } + + _lock.Unlock(); + + return (result); + } + + uint32_t RegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback, void* userdata) + { + return RegisterCallback(_powerModeChanedCallbacks, callback, userdata); + } + + uint32_t UnRegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback) + { + return UnRegisterCallback(_powerModeChanedCallbacks, callback); + } + + uint32_t RegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback, void* userdata) + { + return RegisterCallback(_powerModePreChangeCallbacks, callback, userdata); + } + + uint32_t UnRegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback) + { + return UnRegisterCallback(_powerModePreChangeCallbacks, callback); + } + + uint32_t RegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback, void* userdata) + { + return RegisterCallback(_deepSleepTimeoutCallbacks, callback, userdata); + } + + uint32_t UnRegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback) + { + return UnRegisterCallback(_deepSleepTimeoutCallbacks, callback); + } + + uint32_t RegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback, void* userdata) + { + return RegisterCallback(_networkStandbyModeChangedCallbacks, callback, userdata); + } + + uint32_t UnRegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback) + { + return UnRegisterCallback(_networkStandbyModeChangedCallbacks, callback); + } + + uint32_t RegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback, void* userdata) + { + return RegisterCallback(_thermalModeChangedCallbacks, callback, userdata); + } + + uint32_t UnRegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback) + { + return UnRegisterCallback(_thermalModeChangedCallbacks, callback); + } + + uint32_t RegisterRebootBeginCallback(PowerManager_RebootBeginCb callback, void* userdata) + { + return RegisterCallback(_rebootBeginCallbacks, callback, userdata); + } + + uint32_t UnRegisterRebootBeginCallback(PowerManager_RebootBeginCb callback) + { + return UnRegisterCallback(_rebootBeginCallbacks, callback); + } + +private: + static PowerManagerClient* _singleton; + Core::CriticalSection _lock; + Exchange::IPowerManager* _powerManagerInterface; // remote PowerManager plugin interface + Core::Sink _powerManagerNotification; + + // containers for notification registertion + PowerModeChangedCallbacks _powerModeChanedCallbacks; + PowerModePreChangeCallbacks _powerModePreChangeCallbacks; + DeepSleepTimeoutCallbacks _deepSleepTimeoutCallbacks; + NetworkStandbyModeChangedCallbacks _networkStandbyModeChangedCallbacks; + ThermalModeChangedCallbacks _thermalModeChangedCallbacks; + RebootBeginCallbacks _rebootBeginCallbacks; +}; +} // nameless namespace + +PowerManagerClient* PowerManagerClient::_singleton = nullptr; + +extern "C" { + +uint32_t PowerManager_GetPowerState(PowerManager_PowerState_t* currentState, PowerManager_PowerState_t* previousState) +{ + ASSERT(currentState != nullptr); + ASSERT(previousState != nullptr); + return PowerManagerClient::Instance().GetPowerState(currentState, previousState); +} + +uint32_t PowerManager_SetPowerState(const int keyCode, const PowerManager_PowerState_t powerstate, const char* reason) +{ + return PowerManagerClient::Instance().SetPowerState(keyCode, powerstate, reason); +} + +uint32_t PowerManager_GetThermalState(float* currentTemperature) +{ + ASSERT(currentTemperature != nullptr); + return PowerManagerClient::Instance().GetThermalState(currentTemperature); +} + +uint32_t PowerManager_SetTemperatureThresholds(float high, float critical) +{ + return PowerManagerClient::Instance().SetTemperatureThresholds(high, critical); +} + +uint32_t PowerManager_GetTemperatureThresholds(float* high, float* critical) +{ + ASSERT(high != nullptr); + ASSERT(critical != nullptr); + return PowerManagerClient::Instance().GetTemperatureThresholds(high, critical); +} + +uint32_t PowerManager_SetOvertempGraceInterval(const int graceInterval) +{ + return PowerManagerClient::Instance().SetOvertempGraceInterval(graceInterval); +} + +uint32_t PowerManager_GetOvertempGraceInterval(int* graceInterval /* @out */) +{ + ASSERT(graceInterval != nullptr); + return PowerManagerClient::Instance().GetOvertempGraceInterval(graceInterval); +} + +uint32_t PowerManager_SetDeepSleepTimer(const int timeOut) +{ + return PowerManagerClient::Instance().SetDeepSleepTimer(timeOut); +} + +uint32_t PowerManager_GetLastWakeupReason(PowerManager_WakeupReason_t* wakeupReason) +{ + ASSERT(wakeupReason != nullptr); + return PowerManagerClient::Instance().GetLastWakeupReason(wakeupReason); +} + +uint32_t PowerManager_GetLastWakeupKeyCode(int* keycode) +{ + ASSERT(keycode != nullptr) + return PowerManagerClient::Instance().GetLastWakeupKeyCode(keycode); +} + +uint32_t PowerManager_Reboot(const char* rebootRequestor, const char* rebootReasonCustom, const char* rebootReasonOther) +{ + ASSERT(rebootRequestor != nullptr) + ASSERT(rebootReasonCustom != nullptr) + ASSERT(rebootReasonOther != nullptr) + return PowerManagerClient::Instance().Reboot(rebootRequestor, rebootReasonCustom, rebootReasonOther); +} + +uint32_t PowerManager_SetNetworkStandbyMode(const bool standbyMode) +{ + return PowerManagerClient::Instance().SetNetworkStandbyMode(standbyMode); +} + +uint32_t PowerManager_GetNetworkStandbyMode(bool* standbyMode) +{ + ASSERT(standbyMode != nullptr); + return PowerManagerClient::Instance().GetNetworkStandbyMode(standbyMode); +} + +uint32_t PowerManager_SetWakeupSrcConfig(const int powerMode, const int wakeSrcType, int config) +{ + return PowerManagerClient::Instance().SetWakeupSrcConfig(powerMode, wakeSrcType, config); +} + +uint32_t PowerManager_GetWakeupSrcConfig(int* powerMode, int* srcType, int* config) +{ + ASSERT(powerMode != nullptr); + ASSERT(srcType != nullptr); + ASSERT(config != nullptr); + return PowerManagerClient::Instance().GetWakeupSrcConfig(*powerMode, *srcType, *config); +} + +uint32_t PowerManager_SetSystemMode(const PowerManager_SystemMode_t currentMode, const PowerManager_SystemMode_t newMode) +{ + return PowerManagerClient::Instance().SetSystemMode(currentMode, newMode); +} + +uint32_t PowerManager_GetPowerStateBeforeReboot(PowerManager_PowerState_t* powerStateBeforeReboot) +{ + ASSERT(powerStateBeforeReboot != nullptr); + return PowerManagerClient::Instance().GetPowerStateBeforeReboot(powerStateBeforeReboot); +} + +uint32_t RegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback, void* userdata) +{ + return PowerManagerClient::Instance().RegisterPowerModeChangedCallback(callback, userdata); +} + +uint32_t UnRegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback) +{ + return PowerManagerClient::Instance().UnRegisterPowerModeChangedCallback(callback); +} + +uint32_t RegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback, void* userdata) +{ + return PowerManagerClient::Instance().RegisterPowerModePreChangeCallback(callback, userdata); +} + +uint32_t UnRegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback) +{ + return PowerManagerClient::Instance().UnRegisterPowerModePreChangeCallback(callback); +} + +uint32_t RegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback, void* userdata) +{ + return PowerManagerClient::Instance().RegisterDeepSleepTimeoutCallback(callback, userdata); +} + +uint32_t UnRegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback) +{ + return PowerManagerClient::Instance().UnRegisterDeepSleepTimeoutCallback(callback); +} + +uint32_t RegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback, void* userdata) +{ + return PowerManagerClient::Instance().RegisterNetworkStandbyModeChangedCallback(callback, userdata); +} + +uint32_t UnRegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback) +{ + return PowerManagerClient::Instance().UnRegisterNetworkStandbyModeChangedCallback(callback); +} + +uint32_t RegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback, void* userdata) +{ + return PowerManagerClient::Instance().RegisterThermalModeChangedCallback(callback, userdata); +} + +uint32_t UnRegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback) +{ + return PowerManagerClient::Instance().UnRegisterThermalModeChangedCallback(callback); +} + +uint32_t RegisterRebootBeginCallback(PowerManager_RebootBeginCb callback, void* userdata) +{ + return PowerManagerClient::Instance().RegisterRebootBeginCallback(callback, userdata); +} + +uint32_t UnRegisterRebootBeginCallback(PowerManager_RebootBeginCb callback) +{ + return PowerManagerClient::Instance().UnRegisterRebootBeginCallback(callback); +} +void PowerManager_Dispose() +{ + PowerManagerClient::Dispose(); +} +} // extern "C" diff --git a/Source/powermanager/powermanager_client.h b/Source/powermanager/powermanager_client.h new file mode 100644 index 00000000..c155a9d4 --- /dev/null +++ b/Source/powermanager/powermanager_client.h @@ -0,0 +1,243 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2025 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. + */ +#ifndef POWERMANAGER_CLIENT_H +#define POWERMANAGER_CLIENT_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum PowerManager_PowerState { + POWER_STATE_UNKNOWN = 0 /* UNKNOWN */, + POWER_STATE_OFF = 1 /* OFF */, + POWER_STATE_STANDBY = 2 /* STANDBY */, + POWER_STATE_ON = 3 /* ON */, + POWER_STATE_STANDBY_LIGHT_SLEEP = 4 /* LIGHT_SLEEP */, + POWER_STATE_STANDBY_DEEP_SLEEP = 5 /* DEEP_SLEEP */ +} PowerManager_PowerState_t; + +typedef enum PowerManager_ThermalTemperature { + THERMAL_TEMPERATURE_UNKNOWN = 0 /* UNKNOWN Thermal Temperature */, + THERMAL_TEMPERATURE_NORMAL = 1 /* Normal Thermal Temperature */, + THERMAL_TEMPERATURE_HIGH = 2 /* High Thermal Temperature */, + THERMAL_TEMPERATURE_CRITICAL = 4 /* Critial Thermal Temperature */ +} PowerManager_ThermalTemperature_t; + +typedef enum PowerManager_WakeupSrcType { + WAKEUP_SRC_UNKNOWN = 0 /* UNKNOWN */, + WAKEUP_SRC_VOICE = 1 /* VOICE */, + WAKEUP_SRC_PRESENCEDETECTED = 2 /* PRESENCEDETECTED */, + WAKEUP_SRC_BLUETOOTH = 3 /* BLUETOOTH */, + WAKEUP_SRC_WIFI = 4 /* WIFI */, + WAKEUP_SRC_IR = 5 /* IR */, + WAKEUP_SRC_POWERKEY = 6 /* POWERKEY */, + WAKEUP_SRC_TIMER = 7 /* TIMER */, + WAKEUP_SRC_CEC = 8 /* CEC */, + WAKEUP_SRC_LAN = 9 /* LAN */, + WAKEUP_SRC_RF4CE = 10 /* RF4CE */ +} PowerManager_WakeupSrcType_t; + +typedef enum PowerManager_WakeupReason { + WAKEUP_REASON_UNKNOWN = 0 /* UNKNOWN */, + WAKEUP_REASON_IR = 1 /* IR */, + WAKEUP_REASON_BLUETOOTH = 2 /* BLUETOOTH */, + WAKEUP_REASON_RF4CE = 3 /* RF4CE */, + WAKEUP_REASON_GPIO = 4 /* GPIO */, + WAKEUP_REASON_LAN = 5 /* LAN */, + WAKEUP_REASON_WIFI = 6 /* WIFI */, + WAKEUP_REASON_TIMER = 7 /* TIMER */, + WAKEUP_REASON_FRONTPANEL = 8 /* FRONTPANEL */, + WAKEUP_REASON_WATCHDOG = 9 /* WATCHDOG */, + WAKEUP_REASON_SOFTWARERESET = 10 /* SOFTWARERESET */, + WAKEUP_REASON_THERMALRESET = 11 /* THERMALRESET */, + WAKEUP_REASON_WARMRESET = 12 /* WARMRESET */, + WAKEUP_REASON_COLDBOOT = 13 /* COLDBOOT */, + WAKEUP_REASON_STRAUTHFAIL = 14 /* STR_AUTH_FAIL */, + WAKEUP_REASON_CEC = 15 /* CEC */, + WAKEUP_REASON_PRESENCE = 16 /* PRESENCE */, + WAKEUP_REASON_VOICE = 17 /* VOICE */ +} PowerManager_WakeupReason_t; + +typedef enum PowerManager_SystemMode { + SYSTEM_MODE_UNKNOWN = 0 /* UNKNOWN */, + SYSTEM_MODE_NORMAL = 1 /* NORMAL */, + SYSTEM_MODE_EAS = 2 /* EAS */, + SYSTEM_MODE_WAREHOUSE = 3 /* WAREHOUSE */ +} PowerManager_SystemMode_t; + +/** Sets Power State . */ +// @text setPowerState +// @brief Set Power State +// @param powerState: Set power to this state +// @param reason: Reason for moving to the power state +uint32_t PowerManager_GetPowerState(PowerManager_PowerState_t* currentState, PowerManager_PowerState_t* previousState); + +/** Gets the Power State.*/ +// @text getPowerState +// @brief Get Power State +// @param powerState: Get current power state +uint32_t PowerManager_SetPowerState(const int keyCode, const PowerManager_PowerState_t powerstate, const char* reason); + +/** Gets the current Thermal state.*/ +// @text getThermalState +// @brief Get Current Thermal State (temperature) +// @param currentTemperature: current temperature +uint32_t PowerManager_GetThermalState(float* currentTemperature /* @out */); + +/** Sets the Temperature Thresholds.*/ +// @text setTemperatureThresholds +// @brief Set Temperature Thresholds +// @param high: high threshold +// @param critical : critical threshold +uint32_t PowerManager_SetTemperatureThresholds(float high /* @in */, float critical /* @in */); + +/** Gets the current Temperature Thresholds.*/ +// @text getTemperatureThresholds +// @brief Get Temperature Thresholds +// @param high: high threshold +// @param critical : critical threshold +uint32_t PowerManager_GetTemperatureThresholds(float* high /* @out */, float* critical /* @out */); + +/** Sets the current Temperature Grace interval.*/ +// @property +// @text PowerManager_SetOvertempGraceInterval +// @brief Set Temperature Thresholds +// @param graceInterval: interval in secs? +uint32_t PowerManager_SetOvertempGraceInterval(const int graceInterval /* @in */); + +/** Gets the current Temperature Thresholds.*/ +// @property +// @text PowerManager_GetOvertempGraceInterval +// @brief Get Temperature Grace interval +// @param graceInterval: interval in secs? +uint32_t PowerManager_GetOvertempGraceInterval(int* graceInterval /* @out */); + +/** Set Deep Sleep Timer for later wakeup */ +// @property +// @text setDeepSleepTimer +// @brief Set Deep sleep timer for timeOut period +// @param timeOut: deep sleep timeout +uint32_t PowerManager_SetDeepSleepTimer(const int timeOut /* @in */); + +/** Get Last Wakeup reason */ +// @property +// @text getLastWakeupReason +// @brief Get Last Wake up reason +// @param wakeupReason: wake up reason +uint32_t PowerManager_GetLastWakeupReason(PowerManager_WakeupReason_t* wakeupReason /* @out */); + +/** Get Last Wakeup key code */ +// @property +// @text getLastWakeupKeyCode +// @brief Get the key code that can be used for wakeup +// @param keycode: Key code for wakeup +uint32_t PowerManager_GetLastWakeupKeyCode(int* keycode /* @out */); + +/** Perform Reboot */ +// @text reboot +// @brief Reboot device +uint32_t PowerManager_Reboot(const char* rebootRequestor /* @in */, const char* rebootReasonCustom /* @in */, const char* rebootReasonOther /* @in */); + +/** Set Network Standby Mode */ +// @property +// @text setNetworkStandbyMode +// @brief Set the standby mode for Network +// @param standbyMode: Network standby mode +uint32_t PowerManager_SetNetworkStandbyMode(const bool standbyMode /* @in */); + +/** Get Network Standby Mode */ +// @text getNetworkStandbyMode +// @brief Get the standby mode for Network +// @param standbyMode: Network standby mode +uint32_t PowerManager_GetNetworkStandbyMode(bool* standbyMode /* @out */); + +/** Set Wakeup source configuration */ +// @text setWakeupSrcConfig +// @brief Set the source configuration for device wakeup +// @param powerMode: power mode +// @param wakeSrcType: source type +// @param config: config +uint32_t PowerManager_SetWakeupSrcConfig(const int powerMode /* @in */, const int wakeSrcType /* @in */, int config /* @in */); + +/** Get Wakeup source configuration */ +// @text getWakeupSrcConfig +// @brief Get the source configuration for device wakeup +// @param powerMode: power mode +// @param srcType: source type +// @param config: config +uint32_t PowerManager_GetWakeupSrcConfig(int* powerMode /* @out */, int* srcType /* @out */, int* config /* @out */); + +/** Initiate System mode change */ +// @text PowerManager_SetSystemMode +// @brief System mode change +// @param oldMode: old mode +// @param newMode: new mode +uint32_t PowerManager_SetSystemMode(const PowerManager_SystemMode_t currentMode /* @in */, const PowerManager_SystemMode_t newMode /* @in */); + +/** Get Power State before reboot */ +// @text PowerManager_GetPowerStateBeforeReboot +// @brief Get Power state before reboot +// @param powerStateBeforeReboot: power state +uint32_t PowerManager_GetPowerStateBeforeReboot(PowerManager_PowerState_t* powerStateBeforeReboot /* @out */); + +/** Delete / Release Power Manager Plugin Client instance including RPC instance */ +// @text PowerManager_Dispose +void PowerManager_Dispose(); + +/* Callback data types for event notifications from power manager plugin */ +typedef void (*PowerManager_PowerModeChangedCb)(const PowerManager_PowerState_t currentState, const PowerManager_PowerState_t newState, void* userdata); +typedef void (*PowerManager_PowerModePreChangeCb)(const PowerManager_PowerState_t currentState, const PowerManager_PowerState_t newState, void* userdata); +typedef void (*PowerManager_DeepSleepTimeoutCb)(const int wakeupTimeout, void* userdata); +typedef void (*PowerManager_NetworkStandbyModeChangedCb)(const bool enabled, void* userdata); +typedef void (*PowerManager_ThermalModeChangedCb)(const PowerManager_ThermalTemperature_t currentThermalLevel, const PowerManager_ThermalTemperature_t newThermalLevel, const float currentTemperature, void* userdata); +typedef void (*PowerManager_RebootBeginCb)(const char* rebootReasonCustom, const char* rebootReasonOther, const char* rebootRequestor, void* userdata); + +/** Register for PowerMode changed callback */ +uint32_t RegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback, void* userdata); +/** UnRegister (previously registered) PowerMode changed callback */ +uint32_t UnRegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback); +/** Register for PowerMode pre-change callback */ +uint32_t RegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback, void* userdata); +/** UnRegister (previously registered) PowerMode pre-change callback */ +uint32_t UnRegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback); +/** Register for PowerMode pre-change callback */ +uint32_t RegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback, void* userdata); +/** UnRegister (previously registered) DeepSleep Timeout callback */ +uint32_t UnRegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback); +/** Register for Network Standby Mode changed event - only on XIone */ +uint32_t RegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback, void* userdata); +/** UnRegister (previously registered) Network Standby Mode changed callback */ +uint32_t UnRegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback); +/** Register for Thermal Mode changed event callback */ +uint32_t RegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback, void* userdata); +/** UnRegister (previously registered) Thermal Mode changed event callback */ +uint32_t UnRegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback); +/** Register for reboot start event callback */ +uint32_t RegisterRebootBeginCallback(PowerManager_RebootBeginCb callback, void* userdata); +/** UnRegister (previously registered) reboot start event callback */ +uint32_t UnRegisterRebootBeginCallback(PowerManager_RebootBeginCb callback); + +#ifdef __cplusplus +}; // extern "C" +#endif + +#endif // POWERMANAGER_CLIENT_H From 89b3c4fbe7624a806b442180668ec58c25a08a0c Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Fri, 24 Jan 2025 11:53:48 +0530 Subject: [PATCH 02/16] resolve blackduck scan failure updated NOTICE as per this comment - https://github.com/rdkcentral/ThunderClientLibraries/pull/289#issuecomment-2610063972 --- NOTICE | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NOTICE b/NOTICE index fd4f8ed1..f0bbef9d 100644 --- a/NOTICE +++ b/NOTICE @@ -31,4 +31,5 @@ Licensed under the MIT License Copyright (C) 2015,2019 Metrological Licensed under the BSD-2 License - +Copyright 2025 RDK Management +Licensed under the Apache License, Version 2.0 From e526e27e81cf9f1aa1c50d421e40cdf41cd19df6 Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Tue, 28 Jan 2025 11:19:14 +0000 Subject: [PATCH 03/16] cc review rework --- Source/powermanager/powermanager_client.cpp | 23 ++++++++------------- Source/powermanager/powermanager_client.h | 12 +++++------ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Source/powermanager/powermanager_client.cpp b/Source/powermanager/powermanager_client.cpp index aca120f9..40feb673 100644 --- a/Source/powermanager/powermanager_client.cpp +++ b/Source/powermanager/powermanager_client.cpp @@ -258,12 +258,7 @@ class PowerManagerClient : public RPC::SmartInterfaceType _powerManagerNotification; // containers for notification registertion - PowerModeChangedCallbacks _powerModeChanedCallbacks; + PowerModeChangedCallbacks _powerModeChangedCallbacks; PowerModePreChangeCallbacks _powerModePreChangeCallbacks; DeepSleepTimeoutCallbacks _deepSleepTimeoutCallbacks; NetworkStandbyModeChangedCallbacks _networkStandbyModeChangedCallbacks; @@ -840,15 +835,15 @@ uint32_t PowerManager_GetLastWakeupReason(PowerManager_WakeupReason_t* wakeupRea uint32_t PowerManager_GetLastWakeupKeyCode(int* keycode) { - ASSERT(keycode != nullptr) + ASSERT(keycode != nullptr); return PowerManagerClient::Instance().GetLastWakeupKeyCode(keycode); } uint32_t PowerManager_Reboot(const char* rebootRequestor, const char* rebootReasonCustom, const char* rebootReasonOther) { - ASSERT(rebootRequestor != nullptr) - ASSERT(rebootReasonCustom != nullptr) - ASSERT(rebootReasonOther != nullptr) + ASSERT(rebootRequestor != nullptr); + ASSERT(rebootReasonCustom != nullptr); + ASSERT(rebootReasonOther != nullptr); return PowerManagerClient::Instance().Reboot(rebootRequestor, rebootReasonCustom, rebootReasonOther); } diff --git a/Source/powermanager/powermanager_client.h b/Source/powermanager/powermanager_client.h index c155a9d4..45185056 100644 --- a/Source/powermanager/powermanager_client.h +++ b/Source/powermanager/powermanager_client.h @@ -84,17 +84,17 @@ typedef enum PowerManager_SystemMode { SYSTEM_MODE_WAREHOUSE = 3 /* WAREHOUSE */ } PowerManager_SystemMode_t; +/** Gets the Power State.*/ +// @text getPowerState +// @brief Get Power State +// @param powerState: Get current power state +uint32_t PowerManager_GetPowerState(PowerManager_PowerState_t* currentState, PowerManager_PowerState_t* previousState); + /** Sets Power State . */ // @text setPowerState // @brief Set Power State // @param powerState: Set power to this state // @param reason: Reason for moving to the power state -uint32_t PowerManager_GetPowerState(PowerManager_PowerState_t* currentState, PowerManager_PowerState_t* previousState); - -/** Gets the Power State.*/ -// @text getPowerState -// @brief Get Power State -// @param powerState: Get current power state uint32_t PowerManager_SetPowerState(const int keyCode, const PowerManager_PowerState_t powerstate, const char* reason); /** Gets the current Thermal state.*/ From af919ef8aeda66a716e0096444be25ebfbb99513 Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Thu, 30 Jan 2025 12:46:09 +0530 Subject: [PATCH 04/16] disable powermanager client by default --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1f29e6e..24fbee4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,9 +43,8 @@ option(CDMI "Include OpenCDM interface." OFF) option(CRYPTOGRAPHY "Include the cryptography library." OFF) -# TODO: enable conditionally from recipe file option(POWERMANAGER - "Include the powermanager COMRPC abstraction library." ON) + "Include the powermanager COMRPC abstraction library." OFF) option(INSTALL_TESTS "Install the test applications" OFF) From f6b8333edce1a51fb0ca4a6c8e17cea8aad48478 Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Thu, 30 Jan 2025 07:57:56 +0000 Subject: [PATCH 05/16] ASSERT on failure to open com rpc channel --- Source/powermanager/powermanager_client.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/powermanager/powermanager_client.cpp b/Source/powermanager/powermanager_client.cpp index 40feb673..80fa22b2 100644 --- a/Source/powermanager/powermanager_client.cpp +++ b/Source/powermanager/powermanager_client.cpp @@ -17,6 +17,7 @@ * limitations under the License. */ +#include "core/Trace.h" #include #include @@ -258,7 +259,8 @@ class PowerManagerClient : public RPC::SmartInterfaceType Date: Thu, 30 Jan 2025 11:59:25 +0000 Subject: [PATCH 06/16] dsmgr testing complete --- Source/powermanager/powermanager_client.cpp | 24 +++++++++--------- Source/powermanager/powermanager_client.h | 27 ++++++++++++--------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Source/powermanager/powermanager_client.cpp b/Source/powermanager/powermanager_client.cpp index 80fa22b2..3bf664b1 100644 --- a/Source/powermanager/powermanager_client.cpp +++ b/Source/powermanager/powermanager_client.cpp @@ -884,62 +884,62 @@ uint32_t PowerManager_GetPowerStateBeforeReboot(PowerManager_PowerState_t* power return PowerManagerClient::Instance().GetPowerStateBeforeReboot(powerStateBeforeReboot); } -uint32_t RegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback, void* userdata) +uint32_t PowerManager_RegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback, void* userdata) { return PowerManagerClient::Instance().RegisterPowerModeChangedCallback(callback, userdata); } -uint32_t UnRegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback) +uint32_t PowerManager_UnRegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback) { return PowerManagerClient::Instance().UnRegisterPowerModeChangedCallback(callback); } -uint32_t RegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback, void* userdata) +uint32_t PowerManager_RegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback, void* userdata) { return PowerManagerClient::Instance().RegisterPowerModePreChangeCallback(callback, userdata); } -uint32_t UnRegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback) +uint32_t PowerManager_UnRegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback) { return PowerManagerClient::Instance().UnRegisterPowerModePreChangeCallback(callback); } -uint32_t RegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback, void* userdata) +uint32_t PowerManager_RegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback, void* userdata) { return PowerManagerClient::Instance().RegisterDeepSleepTimeoutCallback(callback, userdata); } -uint32_t UnRegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback) +uint32_t PowerManager_UnRegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback) { return PowerManagerClient::Instance().UnRegisterDeepSleepTimeoutCallback(callback); } -uint32_t RegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback, void* userdata) +uint32_t PowerManager_RegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback, void* userdata) { return PowerManagerClient::Instance().RegisterNetworkStandbyModeChangedCallback(callback, userdata); } -uint32_t UnRegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback) +uint32_t PowerManager_UnRegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback) { return PowerManagerClient::Instance().UnRegisterNetworkStandbyModeChangedCallback(callback); } -uint32_t RegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback, void* userdata) +uint32_t PowerManager_RegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback, void* userdata) { return PowerManagerClient::Instance().RegisterThermalModeChangedCallback(callback, userdata); } -uint32_t UnRegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback) +uint32_t PowerManager_UnRegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback) { return PowerManagerClient::Instance().UnRegisterThermalModeChangedCallback(callback); } -uint32_t RegisterRebootBeginCallback(PowerManager_RebootBeginCb callback, void* userdata) +uint32_t PowerManager_RegisterRebootBeginCallback(PowerManager_RebootBeginCb callback, void* userdata) { return PowerManagerClient::Instance().RegisterRebootBeginCallback(callback, userdata); } -uint32_t UnRegisterRebootBeginCallback(PowerManager_RebootBeginCb callback) +uint32_t PowerManager_UnRegisterRebootBeginCallback(PowerManager_RebootBeginCb callback) { return PowerManagerClient::Instance().UnRegisterRebootBeginCallback(callback); } diff --git a/Source/powermanager/powermanager_client.h b/Source/powermanager/powermanager_client.h index 45185056..7f3aba09 100644 --- a/Source/powermanager/powermanager_client.h +++ b/Source/powermanager/powermanager_client.h @@ -84,6 +84,9 @@ typedef enum PowerManager_SystemMode { SYSTEM_MODE_WAREHOUSE = 3 /* WAREHOUSE */ } PowerManager_SystemMode_t; +#define POWER_MANAGER_ERROR_NONE 0 +#define POWER_MANAGER_ERROR_GENERAL 1 + /** Gets the Power State.*/ // @text getPowerState // @brief Get Power State @@ -212,29 +215,29 @@ typedef void (*PowerManager_ThermalModeChangedCb)(const PowerManager_ThermalTemp typedef void (*PowerManager_RebootBeginCb)(const char* rebootReasonCustom, const char* rebootReasonOther, const char* rebootRequestor, void* userdata); /** Register for PowerMode changed callback */ -uint32_t RegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback, void* userdata); +uint32_t PowerManager_RegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback, void* userdata); /** UnRegister (previously registered) PowerMode changed callback */ -uint32_t UnRegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback); +uint32_t PowerManager_UnRegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback); /** Register for PowerMode pre-change callback */ -uint32_t RegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback, void* userdata); +uint32_t PowerManager_RegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback, void* userdata); /** UnRegister (previously registered) PowerMode pre-change callback */ -uint32_t UnRegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback); +uint32_t PowerManager_UnRegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback); /** Register for PowerMode pre-change callback */ -uint32_t RegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback, void* userdata); +uint32_t PowerManager_RegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback, void* userdata); /** UnRegister (previously registered) DeepSleep Timeout callback */ -uint32_t UnRegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback); +uint32_t PowerManager_UnRegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback); /** Register for Network Standby Mode changed event - only on XIone */ -uint32_t RegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback, void* userdata); +uint32_t PowerManager_RegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback, void* userdata); /** UnRegister (previously registered) Network Standby Mode changed callback */ -uint32_t UnRegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback); +uint32_t PowerManager_UnRegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback); /** Register for Thermal Mode changed event callback */ -uint32_t RegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback, void* userdata); +uint32_t PowerManager_RegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback, void* userdata); /** UnRegister (previously registered) Thermal Mode changed event callback */ -uint32_t UnRegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback); +uint32_t PowerManager_UnRegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback); /** Register for reboot start event callback */ -uint32_t RegisterRebootBeginCallback(PowerManager_RebootBeginCb callback, void* userdata); +uint32_t PowerManager_RegisterRebootBeginCallback(PowerManager_RebootBeginCb callback, void* userdata); /** UnRegister (previously registered) reboot start event callback */ -uint32_t UnRegisterRebootBeginCallback(PowerManager_RebootBeginCb callback); +uint32_t PowerManager_UnRegisterRebootBeginCallback(PowerManager_RebootBeginCb callback); #ifdef __cplusplus }; // extern "C" From 142faa56eb035f38b827d5b6ebe32c8ab0ea56c7 Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Fri, 31 Jan 2025 15:07:59 +0530 Subject: [PATCH 07/16] interface changes for enhanced instance management and add operational state tracking - PowerManager Init & Term, avoids crash if API(s) are called after `Dispose` (older API now removed) - Introduced isOperational and it's state change callback --- Source/powermanager/powermanager_client.h | 57 +++++++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/Source/powermanager/powermanager_client.h b/Source/powermanager/powermanager_client.h index 7f3aba09..493cd3ca 100644 --- a/Source/powermanager/powermanager_client.h +++ b/Source/powermanager/powermanager_client.h @@ -86,6 +86,54 @@ typedef enum PowerManager_SystemMode { #define POWER_MANAGER_ERROR_NONE 0 #define POWER_MANAGER_ERROR_GENERAL 1 +#define POWER_MANAGER_ERROR_UNAVAILABLE 2 + +/** + * @brief Initializes the Power Manager client. + * + * This function creates an instance of the Power Manager client and increments the client reference count. + * It ensures that the Power Manager client is ready for use. + * + * @details + * - If the client instance does not already exist, it will be created. + * - The client reference count is incremented each time this function is called. + * + * @see PowerManager_ClientTerm + */ +void PowerManager_ClientInit(); + +/** + * @brief Terminates the Power Manager client. + * + * This function decrements the client reference count and, if the count reaches zero, destroys the Power Manager client instance. + * This includes performing any necessary RPC calls to clean up the plugin resources. + * + * @details + * - If the client reference count is greater than one, this function only decrements the count. + * - When the reference count reaches zero, the client instance is destroyed, and all associated resources are released. + * - Ensure that this function is called once for every call to `PowerManager_ClientInit`. + * + * @see PowerManager_ClientInit + */ +void PowerManager_ClientTerm(); + +/** + * @brief Checks if the Power Manager plugin is active & operational + * + * This function determines whether the Power Manager interface is operational and ready to handle requests. + * It can be used to verify the availability of the Power Manager client before initiating operations that depend on it. + * + * @return `true` if the Power Manager interface is active and operational, otherwise `false`. + * + * @details + * - Use this function to confirm the operational status of the Power Manager plugin. + * - Calling this function is NOT MANDATORY but optional + * - Clients can register for notifications about state changes using `PowerManager_RegisterOperationalStateChangeCallback`. + * - If the Power Manager interface is not active, subsequent Power Manager operations will fail with the error `POWER_MANAGER_ERROR_UNAVAILABLE`. + * + * @see PowerManager_RegisterOperationalStateChangeCallback + */ +bool PowerManager_IsOperational(); /** Gets the Power State.*/ // @text getPowerState @@ -202,11 +250,8 @@ uint32_t PowerManager_SetSystemMode(const PowerManager_SystemMode_t currentMode // @param powerStateBeforeReboot: power state uint32_t PowerManager_GetPowerStateBeforeReboot(PowerManager_PowerState_t* powerStateBeforeReboot /* @out */); -/** Delete / Release Power Manager Plugin Client instance including RPC instance */ -// @text PowerManager_Dispose -void PowerManager_Dispose(); - /* Callback data types for event notifications from power manager plugin */ +typedef void (*PowerManager_OperationalStateChangeCb)(bool isOperational, void* userdata); typedef void (*PowerManager_PowerModeChangedCb)(const PowerManager_PowerState_t currentState, const PowerManager_PowerState_t newState, void* userdata); typedef void (*PowerManager_PowerModePreChangeCb)(const PowerManager_PowerState_t currentState, const PowerManager_PowerState_t newState, void* userdata); typedef void (*PowerManager_DeepSleepTimeoutCb)(const int wakeupTimeout, void* userdata); @@ -238,6 +283,10 @@ uint32_t PowerManager_UnRegisterThermalModeChangedCallback(PowerManager_ThermalM uint32_t PowerManager_RegisterRebootBeginCallback(PowerManager_RebootBeginCb callback, void* userdata); /** UnRegister (previously registered) reboot start event callback */ uint32_t PowerManager_UnRegisterRebootBeginCallback(PowerManager_RebootBeginCb callback); +/** Register for PowerManager plugin operational state change event callback, for initial state use `PowerManager_IsOperational` call */ +uint32_t PowerManager_RegisterOperationalStateChangeCallback(PowerManager_OperationalStateChangeCb callback, void* userdata); +/** UnRegister (previously registered) PowerManager plugin operational state change event callback */ +uint32_t PowerManager_UnRegisterOperationalStateChangeCallback(PowerManager_OperationalStateChangeCb callback); #ifdef __cplusplus }; // extern "C" From 4cf331ba1eabee1a0d4ea93d0ad2193376572fdd Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Wed, 22 Jan 2025 15:52:21 +0000 Subject: [PATCH 08/16] PowerManagerClient library implementation update copyright in cmake file dsmgr testing complete interface changes for enhanced instance management and add operational state tracking - PowerManager Init & Term, avoids crash if API(s) are called after `Dispose` (older API now removed) - Introduced isOperational and it's state change callback --- Source/powermanager/CMakeLists.txt | 6 +- ...anager_client.cpp => power_controller.cpp} | 326 ++++++++++-------- ...wermanager_client.h => power_controller.h} | 144 ++++---- 3 files changed, 264 insertions(+), 212 deletions(-) rename Source/powermanager/{powermanager_client.cpp => power_controller.cpp} (62%) rename Source/powermanager/{powermanager_client.h => power_controller.h} (55%) diff --git a/Source/powermanager/CMakeLists.txt b/Source/powermanager/CMakeLists.txt index 22210cde..7498e9d7 100644 --- a/Source/powermanager/CMakeLists.txt +++ b/Source/powermanager/CMakeLists.txt @@ -31,11 +31,11 @@ find_package(${NAMESPACE}Core REQUIRED) find_package(${NAMESPACE}COM REQUIRED) find_package(CompileSettingsDebug CONFIG REQUIRED) -set(PUBLIC_HEADERS "powermanager_client.h") +set(PUBLIC_HEADERS "power_controller.h") add_library(${TARGET} SHARED Module.cpp - powermanager_client.cpp + power_controller.cpp ) target_link_libraries(${TARGET} @@ -75,5 +75,5 @@ InstallCMakeConfig( InstallPackageConfig( TARGETS ${TARGET} - DESCRIPTION "communications channel abstraction to get the device properties") + DESCRIPTION "communications channel abstraction for powermanager plugin") diff --git a/Source/powermanager/powermanager_client.cpp b/Source/powermanager/power_controller.cpp similarity index 62% rename from Source/powermanager/powermanager_client.cpp rename to Source/powermanager/power_controller.cpp index 3bf664b1..10e123cc 100644 --- a/Source/powermanager/powermanager_client.cpp +++ b/Source/powermanager/power_controller.cpp @@ -17,7 +17,6 @@ * limitations under the License. */ -#include "core/Trace.h" #include #include @@ -26,7 +25,7 @@ #include #include -#include "powermanager_client.h" +#include "power_controller.h" using namespace WPEFramework; using PowerState = WPEFramework::Exchange::IPowerManager::PowerState; @@ -37,9 +36,9 @@ using ThermalTemperature = WPEFramework::Exchange::IPowerManager::ThermalTempera namespace /*unnamed*/ { -const std::unordered_map& powerStateMap() +const std::unordered_map& powerStateMap() { - static const std::unordered_map map = { + static const std::unordered_map map = { { PowerState::POWER_STATE_UNKNOWN, POWER_STATE_UNKNOWN }, { PowerState::POWER_STATE_OFF, POWER_STATE_OFF }, { PowerState::POWER_STATE_STANDBY, POWER_STATE_STANDBY }, @@ -51,14 +50,14 @@ const std::unordered_map& powerStateMap() return map; } -PowerManager_PowerState_t convert(const PowerState from) +PowerController_PowerState_t convert(const PowerState from) { auto& map = powerStateMap(); auto it = map.find(from); return (it != map.end()) ? it->second : POWER_STATE_UNKNOWN; } -PowerState convert(const PowerManager_PowerState_t from) +PowerState convert(const PowerController_PowerState_t from) { auto& map = powerStateMap(); @@ -70,10 +69,9 @@ PowerState convert(const PowerManager_PowerState_t from) return PowerState::POWER_STATE_UNKNOWN; } -// TODO: re-check if required -const std::unordered_map& wakeupSrcTypeMap() +const std::unordered_map& wakeupSrcTypeMap() { - static const std::unordered_map map = { + static const std::unordered_map map = { { WakeupSrcType::WAKEUP_SRC_UNKNOWN, WAKEUP_SRC_UNKNOWN }, { WakeupSrcType::WAKEUP_SRC_VOICE, WAKEUP_SRC_VOICE }, { WakeupSrcType::WAKEUP_SRC_PRESENCEDETECTED, WAKEUP_SRC_PRESENCEDETECTED }, @@ -89,14 +87,14 @@ const std::unordered_map& wakeupSrc return map; } -PowerManager_WakeupSrcType_t convert(const WakeupSrcType from) +PowerController_WakeupSrcType_t convert(const WakeupSrcType from) { auto& map = wakeupSrcTypeMap(); auto it = map.find(from); return (it != map.end()) ? it->second : WAKEUP_SRC_UNKNOWN; } -WakeupSrcType convert(const PowerManager_WakeupSrcType_t from) +WakeupSrcType convert(const PowerController_WakeupSrcType_t from) { auto& map = wakeupSrcTypeMap(); @@ -108,9 +106,9 @@ WakeupSrcType convert(const PowerManager_WakeupSrcType_t from) return WakeupSrcType::WAKEUP_SRC_UNKNOWN; } -const std::unordered_map& wakeupReasonMap() +const std::unordered_map& wakeupReasonMap() { - static const std::unordered_map map = { + static const std::unordered_map map = { { WakeupReason::WAKEUP_REASON_UNKNOWN, WAKEUP_REASON_UNKNOWN }, { WakeupReason::WAKEUP_REASON_IR, WAKEUP_REASON_IR }, { WakeupReason::WAKEUP_REASON_BLUETOOTH, WAKEUP_REASON_BLUETOOTH }, @@ -133,15 +131,14 @@ const std::unordered_map& wakeupReaso return map; } -PowerManager_WakeupReason_t convert(const WakeupReason from) +PowerController_WakeupReason_t convert(const WakeupReason from) { auto& map = wakeupReasonMap(); auto it = map.find(from); return (it != map.end()) ? it->second : WAKEUP_REASON_UNKNOWN; } -// TODO: re-check required ? -WakeupReason convert(const PowerManager_WakeupReason_t from) +WakeupReason convert(const PowerController_WakeupReason_t from) { auto& map = wakeupReasonMap(); @@ -153,9 +150,9 @@ WakeupReason convert(const PowerManager_WakeupReason_t from) return WakeupReason::WAKEUP_REASON_UNKNOWN; } -const std::unordered_map& systemModeMap() +const std::unordered_map& systemModeMap() { - static const std::unordered_map map = { + static const std::unordered_map map = { { SYSTEM_MODE_UNKNOWN, SystemMode::SYSTEM_MODE_UNKNOWN }, { SYSTEM_MODE_NORMAL, SystemMode::SYSTEM_MODE_NORMAL }, { SYSTEM_MODE_EAS, SystemMode::SYSTEM_MODE_EAS }, @@ -164,16 +161,16 @@ const std::unordered_map& systemModeMap() return map; } -SystemMode convert(const PowerManager_SystemMode_t from) +SystemMode convert(const PowerController_SystemMode_t from) { auto& map = systemModeMap(); auto it = map.find(from); return (it != map.end()) ? it->second : SystemMode::SYSTEM_MODE_UNKNOWN; } -const std::unordered_map& thermalTemperatureMap() +const std::unordered_map& thermalTemperatureMap() { - static const std::unordered_map map = { + static const std::unordered_map map = { { ThermalTemperature::THERMAL_TEMPERATURE_UNKNOWN, THERMAL_TEMPERATURE_UNKNOWN }, { ThermalTemperature::THERMAL_TEMPERATURE_NORMAL, THERMAL_TEMPERATURE_NORMAL }, { ThermalTemperature::THERMAL_TEMPERATURE_HIGH, THERMAL_TEMPERATURE_HIGH }, @@ -182,7 +179,7 @@ const std::unordered_map& return map; } -PowerManager_ThermalTemperature_t convert(const ThermalTemperature from) +PowerController_ThermalTemperature_t convert(const ThermalTemperature from) { auto& map = thermalTemperatureMap(); auto it = map.find(from); @@ -195,22 +192,23 @@ static string Callsign() return (Default); } -class PowerManagerClient : public RPC::SmartInterfaceType { +class PowerController : public RPC::SmartInterfaceType { private: using BaseClass = RPC::SmartInterfaceType; - using PowerModeChangedCallbacks = std::map; - using PowerModePreChangeCallbacks = std::map; - using DeepSleepTimeoutCallbacks = std::map; - using NetworkStandbyModeChangedCallbacks = std::map; - using ThermalModeChangedCallbacks = std::map; - using RebootBeginCallbacks = std::map; + using OperationalStateChangeCallbacks = std::map; + using PowerModeChangedCallbacks = std::map; + using PowerModePreChangeCallbacks = std::map; + using DeepSleepTimeoutCallbacks = std::map; + using NetworkStandbyModeChangedCallbacks = std::map; + using ThermalModeChangedCallbacks = std::map; + using RebootBeginCallbacks = std::map; class Notification : public Exchange::IPowerManager::INotification { private: - PowerManagerClient& _parent; + PowerController& _parent; public: - Notification(PowerManagerClient& parent) + Notification(PowerController& parent) : _parent(parent) { } @@ -250,24 +248,20 @@ class PowerManagerClient : public RPC::SmartInterfaceTypeUnregister(&_powerManagerNotification); _powerManagerInterface->Release(); @@ -291,27 +285,48 @@ class PowerManagerClient : public RPC::SmartInterfaceType 0) { + _nClients--; + } + if (0 == _nClients && nullptr != _instance) { + delete _instance; + _instance = nullptr; } + _lock.Unlock(); } - uint32_t GetPowerState(PowerManager_PowerState_t* currentState, PowerManager_PowerState_t* previousState) + static PowerController& Instance() + { + ASSERT(nullptr != _instance); + return *_instance; + } + + uint32_t GetPowerState(PowerController_PowerState_t* currentState, PowerController_PowerState_t* previousState) { uint32_t result = Core::ERROR_UNAVAILABLE; @@ -334,7 +349,7 @@ class PowerManagerClient : public RPC::SmartInterfaceType _powerManagerNotification; // containers for notification registertion + OperationalStateChangeCallbacks _operationalStateChangeCallbacks; PowerModeChangedCallbacks _powerModeChangedCallbacks; PowerModePreChangeCallbacks _powerModePreChangeCallbacks; DeepSleepTimeoutCallbacks _deepSleepTimeoutCallbacks; NetworkStandbyModeChangedCallbacks _networkStandbyModeChangedCallbacks; ThermalModeChangedCallbacks _thermalModeChangedCallbacks; RebootBeginCallbacks _rebootBeginCallbacks; + + bool _shutdown; }; } // nameless namespace -PowerManagerClient* PowerManagerClient::_singleton = nullptr; +PowerController* PowerController::_instance = nullptr; +int PowerController::_nClients = 0; +Core::CriticalSection PowerController::_lock; extern "C" { -uint32_t PowerManager_GetPowerState(PowerManager_PowerState_t* currentState, PowerManager_PowerState_t* previousState) +void PowerController_Init() +{ + PowerController::Init(); +} + +void PowerController_Term() +{ + PowerController::Term(); +} + +bool PowerController_IsOperational() +{ + return PowerController::Instance().IsOperational(); +} + +uint32_t PowerController_GetPowerState(PowerController_PowerState_t* currentState, PowerController_PowerState_t* previousState) { ASSERT(currentState != nullptr); ASSERT(previousState != nullptr); - return PowerManagerClient::Instance().GetPowerState(currentState, previousState); + return PowerController::Instance().GetPowerState(currentState, previousState); } -uint32_t PowerManager_SetPowerState(const int keyCode, const PowerManager_PowerState_t powerstate, const char* reason) +uint32_t PowerController_SetPowerState(const int keyCode, const PowerController_PowerState_t powerstate, const char* reason) { - return PowerManagerClient::Instance().SetPowerState(keyCode, powerstate, reason); + return PowerController::Instance().SetPowerState(keyCode, powerstate, reason); } -uint32_t PowerManager_GetThermalState(float* currentTemperature) +uint32_t PowerController_GetThermalState(float* currentTemperature) { ASSERT(currentTemperature != nullptr); - return PowerManagerClient::Instance().GetThermalState(currentTemperature); + return PowerController::Instance().GetThermalState(currentTemperature); } -uint32_t PowerManager_SetTemperatureThresholds(float high, float critical) +uint32_t PowerController_SetTemperatureThresholds(float high, float critical) { - return PowerManagerClient::Instance().SetTemperatureThresholds(high, critical); + return PowerController::Instance().SetTemperatureThresholds(high, critical); } -uint32_t PowerManager_GetTemperatureThresholds(float* high, float* critical) +uint32_t PowerController_GetTemperatureThresholds(float* high, float* critical) { ASSERT(high != nullptr); ASSERT(critical != nullptr); - return PowerManagerClient::Instance().GetTemperatureThresholds(high, critical); + return PowerController::Instance().GetTemperatureThresholds(high, critical); } -uint32_t PowerManager_SetOvertempGraceInterval(const int graceInterval) +uint32_t PowerController_SetOvertempGraceInterval(const int graceInterval) { - return PowerManagerClient::Instance().SetOvertempGraceInterval(graceInterval); + return PowerController::Instance().SetOvertempGraceInterval(graceInterval); } -uint32_t PowerManager_GetOvertempGraceInterval(int* graceInterval /* @out */) +uint32_t PowerController_GetOvertempGraceInterval(int* graceInterval /* @out */) { ASSERT(graceInterval != nullptr); - return PowerManagerClient::Instance().GetOvertempGraceInterval(graceInterval); + return PowerController::Instance().GetOvertempGraceInterval(graceInterval); } -uint32_t PowerManager_SetDeepSleepTimer(const int timeOut) +uint32_t PowerController_SetDeepSleepTimer(const int timeOut) { - return PowerManagerClient::Instance().SetDeepSleepTimer(timeOut); + return PowerController::Instance().SetDeepSleepTimer(timeOut); } -uint32_t PowerManager_GetLastWakeupReason(PowerManager_WakeupReason_t* wakeupReason) +uint32_t PowerController_GetLastWakeupReason(PowerController_WakeupReason_t* wakeupReason) { ASSERT(wakeupReason != nullptr); - return PowerManagerClient::Instance().GetLastWakeupReason(wakeupReason); + return PowerController::Instance().GetLastWakeupReason(wakeupReason); } -uint32_t PowerManager_GetLastWakeupKeyCode(int* keycode) +uint32_t PowerController_GetLastWakeupKeyCode(int* keycode) { ASSERT(keycode != nullptr); - return PowerManagerClient::Instance().GetLastWakeupKeyCode(keycode); + return PowerController::Instance().GetLastWakeupKeyCode(keycode); } -uint32_t PowerManager_Reboot(const char* rebootRequestor, const char* rebootReasonCustom, const char* rebootReasonOther) +uint32_t PowerController_Reboot(const char* rebootRequestor, const char* rebootReasonCustom, const char* rebootReasonOther) { ASSERT(rebootRequestor != nullptr); ASSERT(rebootReasonCustom != nullptr); ASSERT(rebootReasonOther != nullptr); - return PowerManagerClient::Instance().Reboot(rebootRequestor, rebootReasonCustom, rebootReasonOther); + return PowerController::Instance().Reboot(rebootRequestor, rebootReasonCustom, rebootReasonOther); } -uint32_t PowerManager_SetNetworkStandbyMode(const bool standbyMode) +uint32_t PowerController_SetNetworkStandbyMode(const bool standbyMode) { - return PowerManagerClient::Instance().SetNetworkStandbyMode(standbyMode); + return PowerController::Instance().SetNetworkStandbyMode(standbyMode); } -uint32_t PowerManager_GetNetworkStandbyMode(bool* standbyMode) +uint32_t PowerController_GetNetworkStandbyMode(bool* standbyMode) { ASSERT(standbyMode != nullptr); - return PowerManagerClient::Instance().GetNetworkStandbyMode(standbyMode); + return PowerController::Instance().GetNetworkStandbyMode(standbyMode); } -uint32_t PowerManager_SetWakeupSrcConfig(const int powerMode, const int wakeSrcType, int config) +uint32_t PowerController_SetWakeupSrcConfig(const int powerMode, const int wakeSrcType, int config) { - return PowerManagerClient::Instance().SetWakeupSrcConfig(powerMode, wakeSrcType, config); + return PowerController::Instance().SetWakeupSrcConfig(powerMode, wakeSrcType, config); } -uint32_t PowerManager_GetWakeupSrcConfig(int* powerMode, int* srcType, int* config) +uint32_t PowerController_GetWakeupSrcConfig(int* powerMode, int* srcType, int* config) { ASSERT(powerMode != nullptr); ASSERT(srcType != nullptr); ASSERT(config != nullptr); - return PowerManagerClient::Instance().GetWakeupSrcConfig(*powerMode, *srcType, *config); + return PowerController::Instance().GetWakeupSrcConfig(*powerMode, *srcType, *config); } -uint32_t PowerManager_SetSystemMode(const PowerManager_SystemMode_t currentMode, const PowerManager_SystemMode_t newMode) +uint32_t PowerController_SetSystemMode(const PowerController_SystemMode_t currentMode, const PowerController_SystemMode_t newMode) { - return PowerManagerClient::Instance().SetSystemMode(currentMode, newMode); + return PowerController::Instance().SetSystemMode(currentMode, newMode); } -uint32_t PowerManager_GetPowerStateBeforeReboot(PowerManager_PowerState_t* powerStateBeforeReboot) +uint32_t PowerController_GetPowerStateBeforeReboot(PowerController_PowerState_t* powerStateBeforeReboot) { ASSERT(powerStateBeforeReboot != nullptr); - return PowerManagerClient::Instance().GetPowerStateBeforeReboot(powerStateBeforeReboot); + return PowerController::Instance().GetPowerStateBeforeReboot(powerStateBeforeReboot); +} + +uint32_t PowerController_RegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback, void* userdata) +{ + return PowerController::Instance().RegisterOperationalStateChangedCallback(callback, userdata); } -uint32_t PowerManager_RegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback, void* userdata) +uint32_t PowerController_UnRegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback) { - return PowerManagerClient::Instance().RegisterPowerModeChangedCallback(callback, userdata); + return PowerController::Instance().UnRegisterOperationalStateChangedCallback(callback); } -uint32_t PowerManager_UnRegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback) +uint32_t PowerController_RegisterPowerModeChangedCallback(PowerController_PowerModeChangedCb callback, void* userdata) { - return PowerManagerClient::Instance().UnRegisterPowerModeChangedCallback(callback); + return PowerController::Instance().RegisterPowerModeChangedCallback(callback, userdata); } -uint32_t PowerManager_RegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback, void* userdata) +uint32_t PowerController_UnRegisterPowerModeChangedCallback(PowerController_PowerModeChangedCb callback) { - return PowerManagerClient::Instance().RegisterPowerModePreChangeCallback(callback, userdata); + return PowerController::Instance().UnRegisterPowerModeChangedCallback(callback); } -uint32_t PowerManager_UnRegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback) +uint32_t PowerController_RegisterPowerModePreChangeCallback(PowerController_PowerModePreChangeCb callback, void* userdata) { - return PowerManagerClient::Instance().UnRegisterPowerModePreChangeCallback(callback); + return PowerController::Instance().RegisterPowerModePreChangeCallback(callback, userdata); } -uint32_t PowerManager_RegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback, void* userdata) +uint32_t PowerController_UnRegisterPowerModePreChangeCallback(PowerController_PowerModePreChangeCb callback) { - return PowerManagerClient::Instance().RegisterDeepSleepTimeoutCallback(callback, userdata); + return PowerController::Instance().UnRegisterPowerModePreChangeCallback(callback); } -uint32_t PowerManager_UnRegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback) +uint32_t PowerController_RegisterDeepSleepTimeoutCallback(PowerController_DeepSleepTimeoutCb callback, void* userdata) { - return PowerManagerClient::Instance().UnRegisterDeepSleepTimeoutCallback(callback); + return PowerController::Instance().RegisterDeepSleepTimeoutCallback(callback, userdata); } -uint32_t PowerManager_RegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback, void* userdata) +uint32_t PowerController_UnRegisterDeepSleepTimeoutCallback(PowerController_DeepSleepTimeoutCb callback) { - return PowerManagerClient::Instance().RegisterNetworkStandbyModeChangedCallback(callback, userdata); + return PowerController::Instance().UnRegisterDeepSleepTimeoutCallback(callback); } -uint32_t PowerManager_UnRegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback) +uint32_t PowerController_RegisterNetworkStandbyModeChangedCallback(PowerController_NetworkStandbyModeChangedCb callback, void* userdata) { - return PowerManagerClient::Instance().UnRegisterNetworkStandbyModeChangedCallback(callback); + return PowerController::Instance().RegisterNetworkStandbyModeChangedCallback(callback, userdata); } -uint32_t PowerManager_RegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback, void* userdata) +uint32_t PowerController_UnRegisterNetworkStandbyModeChangedCallback(PowerController_NetworkStandbyModeChangedCb callback) { - return PowerManagerClient::Instance().RegisterThermalModeChangedCallback(callback, userdata); + return PowerController::Instance().UnRegisterNetworkStandbyModeChangedCallback(callback); } -uint32_t PowerManager_UnRegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback) +uint32_t PowerController_RegisterThermalModeChangedCallback(PowerController_ThermalModeChangedCb callback, void* userdata) { - return PowerManagerClient::Instance().UnRegisterThermalModeChangedCallback(callback); + return PowerController::Instance().RegisterThermalModeChangedCallback(callback, userdata); } -uint32_t PowerManager_RegisterRebootBeginCallback(PowerManager_RebootBeginCb callback, void* userdata) +uint32_t PowerController_UnRegisterThermalModeChangedCallback(PowerController_ThermalModeChangedCb callback) { - return PowerManagerClient::Instance().RegisterRebootBeginCallback(callback, userdata); + return PowerController::Instance().UnRegisterThermalModeChangedCallback(callback); } -uint32_t PowerManager_UnRegisterRebootBeginCallback(PowerManager_RebootBeginCb callback) +uint32_t PowerController_RegisterRebootBeginCallback(PowerController_RebootBeginCb callback, void* userdata) { - return PowerManagerClient::Instance().UnRegisterRebootBeginCallback(callback); + return PowerController::Instance().RegisterRebootBeginCallback(callback, userdata); } -void PowerManager_Dispose() + +uint32_t PowerController_UnRegisterRebootBeginCallback(PowerController_RebootBeginCb callback) { - PowerManagerClient::Dispose(); + return PowerController::Instance().UnRegisterRebootBeginCallback(callback); } + } // extern "C" diff --git a/Source/powermanager/powermanager_client.h b/Source/powermanager/power_controller.h similarity index 55% rename from Source/powermanager/powermanager_client.h rename to Source/powermanager/power_controller.h index 493cd3ca..a882f206 100644 --- a/Source/powermanager/powermanager_client.h +++ b/Source/powermanager/power_controller.h @@ -26,23 +26,23 @@ extern "C" { #endif -typedef enum PowerManager_PowerState { +typedef enum PowerController_PowerState { POWER_STATE_UNKNOWN = 0 /* UNKNOWN */, POWER_STATE_OFF = 1 /* OFF */, POWER_STATE_STANDBY = 2 /* STANDBY */, POWER_STATE_ON = 3 /* ON */, POWER_STATE_STANDBY_LIGHT_SLEEP = 4 /* LIGHT_SLEEP */, POWER_STATE_STANDBY_DEEP_SLEEP = 5 /* DEEP_SLEEP */ -} PowerManager_PowerState_t; +} PowerController_PowerState_t; -typedef enum PowerManager_ThermalTemperature { +typedef enum PowerController_ThermalTemperature { THERMAL_TEMPERATURE_UNKNOWN = 0 /* UNKNOWN Thermal Temperature */, THERMAL_TEMPERATURE_NORMAL = 1 /* Normal Thermal Temperature */, THERMAL_TEMPERATURE_HIGH = 2 /* High Thermal Temperature */, THERMAL_TEMPERATURE_CRITICAL = 4 /* Critial Thermal Temperature */ -} PowerManager_ThermalTemperature_t; +} PowerController_ThermalTemperature_t; -typedef enum PowerManager_WakeupSrcType { +typedef enum PowerController_WakeupSrcType { WAKEUP_SRC_UNKNOWN = 0 /* UNKNOWN */, WAKEUP_SRC_VOICE = 1 /* VOICE */, WAKEUP_SRC_PRESENCEDETECTED = 2 /* PRESENCEDETECTED */, @@ -54,9 +54,9 @@ typedef enum PowerManager_WakeupSrcType { WAKEUP_SRC_CEC = 8 /* CEC */, WAKEUP_SRC_LAN = 9 /* LAN */, WAKEUP_SRC_RF4CE = 10 /* RF4CE */ -} PowerManager_WakeupSrcType_t; +} PowerController_WakeupSrcType_t; -typedef enum PowerManager_WakeupReason { +typedef enum PowerController_WakeupReason { WAKEUP_REASON_UNKNOWN = 0 /* UNKNOWN */, WAKEUP_REASON_IR = 1 /* IR */, WAKEUP_REASON_BLUETOOTH = 2 /* BLUETOOTH */, @@ -75,47 +75,45 @@ typedef enum PowerManager_WakeupReason { WAKEUP_REASON_CEC = 15 /* CEC */, WAKEUP_REASON_PRESENCE = 16 /* PRESENCE */, WAKEUP_REASON_VOICE = 17 /* VOICE */ -} PowerManager_WakeupReason_t; +} PowerController_WakeupReason_t; -typedef enum PowerManager_SystemMode { +typedef enum PowerController_SystemMode { SYSTEM_MODE_UNKNOWN = 0 /* UNKNOWN */, SYSTEM_MODE_NORMAL = 1 /* NORMAL */, SYSTEM_MODE_EAS = 2 /* EAS */, SYSTEM_MODE_WAREHOUSE = 3 /* WAREHOUSE */ -} PowerManager_SystemMode_t; +} PowerController_SystemMode_t; #define POWER_MANAGER_ERROR_NONE 0 #define POWER_MANAGER_ERROR_GENERAL 1 #define POWER_MANAGER_ERROR_UNAVAILABLE 2 /** - * @brief Initializes the Power Manager client. + * @brief Initializes the Power Controller. * - * This function creates an instance of the Power Manager client and increments the client reference count. - * It ensures that the Power Manager client is ready for use. + * This function creates an instance of the PowerManager plugin client interface and increments the client instance count. * * @details - * - If the client instance does not already exist, it will be created. - * - The client reference count is incremented each time this function is called. + * - If the Power Controller instance does not already exist, it will be created. + * - The instance count is incremented each time this function is called. * - * @see PowerManager_ClientTerm + * @see PowerController_Term */ -void PowerManager_ClientInit(); +void PowerController_Init(); /** - * @brief Terminates the Power Manager client. + * @brief Terminates the Power Controller. * - * This function decrements the client reference count and, if the count reaches zero, destroys the Power Manager client instance. - * This includes performing any necessary RPC calls to clean up the plugin resources. + * This function decrements client instance count attempts to delete Power Controller instance * * @details - * - If the client reference count is greater than one, this function only decrements the count. - * - When the reference count reaches zero, the client instance is destroyed, and all associated resources are released. - * - Ensure that this function is called once for every call to `PowerManager_ClientInit`. + * - If the controller reference count is greater than one, this function only decrements the count. + * - When the reference count reaches zero, the controller instance is destroyed, and all associated resources are released (PowerManager plugin client instance). + * - Ensure that this function is called once for every call to `PowerController_Init`. * - * @see PowerManager_ClientInit + * @see PowerController_Init */ -void PowerManager_ClientTerm(); +void PowerController_Term(); /** * @brief Checks if the Power Manager plugin is active & operational @@ -128,98 +126,98 @@ void PowerManager_ClientTerm(); * @details * - Use this function to confirm the operational status of the Power Manager plugin. * - Calling this function is NOT MANDATORY but optional - * - Clients can register for notifications about state changes using `PowerManager_RegisterOperationalStateChangeCallback`. + * - Clients can register for notifications about state changes using `PowerController_RegisterOperationalStateChangeCallback`. * - If the Power Manager interface is not active, subsequent Power Manager operations will fail with the error `POWER_MANAGER_ERROR_UNAVAILABLE`. * - * @see PowerManager_RegisterOperationalStateChangeCallback + * @see PowerController_RegisterOperationalStateChangeCallback */ -bool PowerManager_IsOperational(); +bool PowerController_IsOperational(); /** Gets the Power State.*/ // @text getPowerState // @brief Get Power State // @param powerState: Get current power state -uint32_t PowerManager_GetPowerState(PowerManager_PowerState_t* currentState, PowerManager_PowerState_t* previousState); +uint32_t PowerController_GetPowerState(PowerController_PowerState_t* currentState, PowerController_PowerState_t* previousState); /** Sets Power State . */ // @text setPowerState // @brief Set Power State // @param powerState: Set power to this state // @param reason: Reason for moving to the power state -uint32_t PowerManager_SetPowerState(const int keyCode, const PowerManager_PowerState_t powerstate, const char* reason); +uint32_t PowerController_SetPowerState(const int keyCode, const PowerController_PowerState_t powerstate, const char* reason); /** Gets the current Thermal state.*/ // @text getThermalState // @brief Get Current Thermal State (temperature) // @param currentTemperature: current temperature -uint32_t PowerManager_GetThermalState(float* currentTemperature /* @out */); +uint32_t PowerController_GetThermalState(float* currentTemperature /* @out */); /** Sets the Temperature Thresholds.*/ // @text setTemperatureThresholds // @brief Set Temperature Thresholds // @param high: high threshold // @param critical : critical threshold -uint32_t PowerManager_SetTemperatureThresholds(float high /* @in */, float critical /* @in */); +uint32_t PowerController_SetTemperatureThresholds(float high /* @in */, float critical /* @in */); /** Gets the current Temperature Thresholds.*/ // @text getTemperatureThresholds // @brief Get Temperature Thresholds // @param high: high threshold // @param critical : critical threshold -uint32_t PowerManager_GetTemperatureThresholds(float* high /* @out */, float* critical /* @out */); +uint32_t PowerController_GetTemperatureThresholds(float* high /* @out */, float* critical /* @out */); /** Sets the current Temperature Grace interval.*/ // @property -// @text PowerManager_SetOvertempGraceInterval +// @text PowerController_SetOvertempGraceInterval // @brief Set Temperature Thresholds // @param graceInterval: interval in secs? -uint32_t PowerManager_SetOvertempGraceInterval(const int graceInterval /* @in */); +uint32_t PowerController_SetOvertempGraceInterval(const int graceInterval /* @in */); /** Gets the current Temperature Thresholds.*/ // @property -// @text PowerManager_GetOvertempGraceInterval +// @text PowerController_GetOvertempGraceInterval // @brief Get Temperature Grace interval // @param graceInterval: interval in secs? -uint32_t PowerManager_GetOvertempGraceInterval(int* graceInterval /* @out */); +uint32_t PowerController_GetOvertempGraceInterval(int* graceInterval /* @out */); /** Set Deep Sleep Timer for later wakeup */ // @property // @text setDeepSleepTimer // @brief Set Deep sleep timer for timeOut period // @param timeOut: deep sleep timeout -uint32_t PowerManager_SetDeepSleepTimer(const int timeOut /* @in */); +uint32_t PowerController_SetDeepSleepTimer(const int timeOut /* @in */); /** Get Last Wakeup reason */ // @property // @text getLastWakeupReason // @brief Get Last Wake up reason // @param wakeupReason: wake up reason -uint32_t PowerManager_GetLastWakeupReason(PowerManager_WakeupReason_t* wakeupReason /* @out */); +uint32_t PowerController_GetLastWakeupReason(PowerController_WakeupReason_t* wakeupReason /* @out */); /** Get Last Wakeup key code */ // @property // @text getLastWakeupKeyCode // @brief Get the key code that can be used for wakeup // @param keycode: Key code for wakeup -uint32_t PowerManager_GetLastWakeupKeyCode(int* keycode /* @out */); +uint32_t PowerController_GetLastWakeupKeyCode(int* keycode /* @out */); /** Perform Reboot */ // @text reboot // @brief Reboot device -uint32_t PowerManager_Reboot(const char* rebootRequestor /* @in */, const char* rebootReasonCustom /* @in */, const char* rebootReasonOther /* @in */); +uint32_t PowerController_Reboot(const char* rebootRequestor /* @in */, const char* rebootReasonCustom /* @in */, const char* rebootReasonOther /* @in */); /** Set Network Standby Mode */ // @property // @text setNetworkStandbyMode // @brief Set the standby mode for Network // @param standbyMode: Network standby mode -uint32_t PowerManager_SetNetworkStandbyMode(const bool standbyMode /* @in */); +uint32_t PowerController_SetNetworkStandbyMode(const bool standbyMode /* @in */); /** Get Network Standby Mode */ // @text getNetworkStandbyMode // @brief Get the standby mode for Network // @param standbyMode: Network standby mode -uint32_t PowerManager_GetNetworkStandbyMode(bool* standbyMode /* @out */); +uint32_t PowerController_GetNetworkStandbyMode(bool* standbyMode /* @out */); /** Set Wakeup source configuration */ // @text setWakeupSrcConfig @@ -227,7 +225,7 @@ uint32_t PowerManager_GetNetworkStandbyMode(bool* standbyMode /* @out */); // @param powerMode: power mode // @param wakeSrcType: source type // @param config: config -uint32_t PowerManager_SetWakeupSrcConfig(const int powerMode /* @in */, const int wakeSrcType /* @in */, int config /* @in */); +uint32_t PowerController_SetWakeupSrcConfig(const int powerMode /* @in */, const int wakeSrcType /* @in */, int config /* @in */); /** Get Wakeup source configuration */ // @text getWakeupSrcConfig @@ -235,58 +233,58 @@ uint32_t PowerManager_SetWakeupSrcConfig(const int powerMode /* @in */, const in // @param powerMode: power mode // @param srcType: source type // @param config: config -uint32_t PowerManager_GetWakeupSrcConfig(int* powerMode /* @out */, int* srcType /* @out */, int* config /* @out */); +uint32_t PowerController_GetWakeupSrcConfig(int* powerMode /* @out */, int* srcType /* @out */, int* config /* @out */); /** Initiate System mode change */ -// @text PowerManager_SetSystemMode +// @text PowerController_SetSystemMode // @brief System mode change // @param oldMode: old mode // @param newMode: new mode -uint32_t PowerManager_SetSystemMode(const PowerManager_SystemMode_t currentMode /* @in */, const PowerManager_SystemMode_t newMode /* @in */); +uint32_t PowerController_SetSystemMode(const PowerController_SystemMode_t currentMode /* @in */, const PowerController_SystemMode_t newMode /* @in */); /** Get Power State before reboot */ -// @text PowerManager_GetPowerStateBeforeReboot +// @text PowerController_GetPowerStateBeforeReboot // @brief Get Power state before reboot // @param powerStateBeforeReboot: power state -uint32_t PowerManager_GetPowerStateBeforeReboot(PowerManager_PowerState_t* powerStateBeforeReboot /* @out */); +uint32_t PowerController_GetPowerStateBeforeReboot(PowerController_PowerState_t* powerStateBeforeReboot /* @out */); /* Callback data types for event notifications from power manager plugin */ -typedef void (*PowerManager_OperationalStateChangeCb)(bool isOperational, void* userdata); -typedef void (*PowerManager_PowerModeChangedCb)(const PowerManager_PowerState_t currentState, const PowerManager_PowerState_t newState, void* userdata); -typedef void (*PowerManager_PowerModePreChangeCb)(const PowerManager_PowerState_t currentState, const PowerManager_PowerState_t newState, void* userdata); -typedef void (*PowerManager_DeepSleepTimeoutCb)(const int wakeupTimeout, void* userdata); -typedef void (*PowerManager_NetworkStandbyModeChangedCb)(const bool enabled, void* userdata); -typedef void (*PowerManager_ThermalModeChangedCb)(const PowerManager_ThermalTemperature_t currentThermalLevel, const PowerManager_ThermalTemperature_t newThermalLevel, const float currentTemperature, void* userdata); -typedef void (*PowerManager_RebootBeginCb)(const char* rebootReasonCustom, const char* rebootReasonOther, const char* rebootRequestor, void* userdata); - +typedef void (*PowerController_OperationalStateChangeCb)(bool isOperational, void* userdata); +typedef void (*PowerController_PowerModeChangedCb)(const PowerController_PowerState_t currentState, const PowerController_PowerState_t newState, void* userdata); +typedef void (*PowerController_PowerModePreChangeCb)(const PowerController_PowerState_t currentState, const PowerController_PowerState_t newState, void* userdata); +typedef void (*PowerController_DeepSleepTimeoutCb)(const int wakeupTimeout, void* userdata); +typedef void (*PowerController_NetworkStandbyModeChangedCb)(const bool enabled, void* userdata); +typedef void (*PowerController_ThermalModeChangedCb)(const PowerController_ThermalTemperature_t currentThermalLevel, const PowerController_ThermalTemperature_t newThermalLevel, const float currentTemperature, void* userdata); +typedef void (*PowerController_RebootBeginCb)(const char* rebootReasonCustom, const char* rebootReasonOther, const char* rebootRequestor, void* userdata); + +/** Register for PowerManager plugin operational state change event callback, for initial state use `PowerController_IsOperational` call */ +uint32_t PowerController_RegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback, void* userdata); +/** UnRegister (previously registered) PowerManager plugin operational state change event callback */ +uint32_t PowerController_UnRegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback); /** Register for PowerMode changed callback */ -uint32_t PowerManager_RegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback, void* userdata); +uint32_t PowerController_RegisterPowerModeChangedCallback(PowerController_PowerModeChangedCb callback, void* userdata); /** UnRegister (previously registered) PowerMode changed callback */ -uint32_t PowerManager_UnRegisterPowerModeChangedCallback(PowerManager_PowerModeChangedCb callback); +uint32_t PowerController_UnRegisterPowerModeChangedCallback(PowerController_PowerModeChangedCb callback); /** Register for PowerMode pre-change callback */ -uint32_t PowerManager_RegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback, void* userdata); +uint32_t PowerController_RegisterPowerModePreChangeCallback(PowerController_PowerModePreChangeCb callback, void* userdata); /** UnRegister (previously registered) PowerMode pre-change callback */ -uint32_t PowerManager_UnRegisterPowerModePreChangeCallback(PowerManager_PowerModePreChangeCb callback); +uint32_t PowerController_UnRegisterPowerModePreChangeCallback(PowerController_PowerModePreChangeCb callback); /** Register for PowerMode pre-change callback */ -uint32_t PowerManager_RegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback, void* userdata); +uint32_t PowerController_RegisterDeepSleepTimeoutCallback(PowerController_DeepSleepTimeoutCb callback, void* userdata); /** UnRegister (previously registered) DeepSleep Timeout callback */ -uint32_t PowerManager_UnRegisterDeepSleepTimeoutCallback(PowerManager_DeepSleepTimeoutCb callback); +uint32_t PowerController_UnRegisterDeepSleepTimeoutCallback(PowerController_DeepSleepTimeoutCb callback); /** Register for Network Standby Mode changed event - only on XIone */ -uint32_t PowerManager_RegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback, void* userdata); +uint32_t PowerController_RegisterNetworkStandbyModeChangedCallback(PowerController_NetworkStandbyModeChangedCb callback, void* userdata); /** UnRegister (previously registered) Network Standby Mode changed callback */ -uint32_t PowerManager_UnRegisterNetworkStandbyModeChangedCallback(PowerManager_NetworkStandbyModeChangedCb callback); +uint32_t PowerController_UnRegisterNetworkStandbyModeChangedCallback(PowerController_NetworkStandbyModeChangedCb callback); /** Register for Thermal Mode changed event callback */ -uint32_t PowerManager_RegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback, void* userdata); +uint32_t PowerController_RegisterThermalModeChangedCallback(PowerController_ThermalModeChangedCb callback, void* userdata); /** UnRegister (previously registered) Thermal Mode changed event callback */ -uint32_t PowerManager_UnRegisterThermalModeChangedCallback(PowerManager_ThermalModeChangedCb callback); +uint32_t PowerController_UnRegisterThermalModeChangedCallback(PowerController_ThermalModeChangedCb callback); /** Register for reboot start event callback */ -uint32_t PowerManager_RegisterRebootBeginCallback(PowerManager_RebootBeginCb callback, void* userdata); +uint32_t PowerController_RegisterRebootBeginCallback(PowerController_RebootBeginCb callback, void* userdata); /** UnRegister (previously registered) reboot start event callback */ -uint32_t PowerManager_UnRegisterRebootBeginCallback(PowerManager_RebootBeginCb callback); -/** Register for PowerManager plugin operational state change event callback, for initial state use `PowerManager_IsOperational` call */ -uint32_t PowerManager_RegisterOperationalStateChangeCallback(PowerManager_OperationalStateChangeCb callback, void* userdata); -/** UnRegister (previously registered) PowerManager plugin operational state change event callback */ -uint32_t PowerManager_UnRegisterOperationalStateChangeCallback(PowerManager_OperationalStateChangeCb callback); +uint32_t PowerController_UnRegisterRebootBeginCallback(PowerController_RebootBeginCb callback); #ifdef __cplusplus }; // extern "C" From 9e95292803d0be1228a1ad1947e93aa36035beee Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Wed, 12 Feb 2025 10:20:05 +0530 Subject: [PATCH 09/16] review rework --- CMakeLists.txt | 2 +- Source/CMakeLists.txt | 4 +- .../CMakeLists.txt | 6 +- .../Module.cpp | 0 .../Module.h | 1 - .../power_controller.cpp | 79 +++++++++++-------- .../power_controller.h | 31 +++++--- 7 files changed, 70 insertions(+), 53 deletions(-) rename Source/{powermanager => powercontroller}/CMakeLists.txt (89%) rename Source/{powermanager => powercontroller}/Module.cpp (100%) rename Source/{powermanager => powercontroller}/Module.h (97%) rename Source/{powermanager => powercontroller}/power_controller.cpp (92%) rename Source/{powermanager => powercontroller}/power_controller.h (91%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 24fbee4b..5f2dc90a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,7 @@ option(CDMI "Include OpenCDM interface." OFF) option(CRYPTOGRAPHY "Include the cryptography library." OFF) -option(POWERMANAGER +option(POWERCONTROLLER "Include the powermanager COMRPC abstraction library." OFF) option(INSTALL_TESTS "Install the test applications" OFF) diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 9d87f181..4cc4cb1d 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -59,6 +59,6 @@ if(LOCALTRACER) add_subdirectory(localtracer) endif() -if(POWERMANAGER) - add_subdirectory(powermanager) +if(POWERCONTROLLER) + add_subdirectory(powercontroller) endif() diff --git a/Source/powermanager/CMakeLists.txt b/Source/powercontroller/CMakeLists.txt similarity index 89% rename from Source/powermanager/CMakeLists.txt rename to Source/powercontroller/CMakeLists.txt index 7498e9d7..786bffc1 100644 --- a/Source/powermanager/CMakeLists.txt +++ b/Source/powercontroller/CMakeLists.txt @@ -19,7 +19,7 @@ cmake_minimum_required(VERSION 3.3) find_package(WPEFramework) -project(PowerManagerClient) +project(PowerController) project_version(4.4.1) @@ -66,8 +66,8 @@ install( LIBRARY DESTINATION lib COMPONENT libs # shared lib RUNTIME DESTINATION bin COMPONENT libs # binaries FRAMEWORK DESTINATION bin COMPONENT libs # for mac - PUBLIC_HEADER DESTINATION include/${NAMESPACE}/powermanager COMPONENT devel # headers for mac (note the different component -> different package) - INCLUDES DESTINATION include/${NAMESPACE}/powermanager # headers + PUBLIC_HEADER DESTINATION include/${NAMESPACE}/powercontroller COMPONENT devel # headers for mac (note the different component -> different package) + INCLUDES DESTINATION include/${NAMESPACE}/powercontroller # headers ) InstallCMakeConfig( diff --git a/Source/powermanager/Module.cpp b/Source/powercontroller/Module.cpp similarity index 100% rename from Source/powermanager/Module.cpp rename to Source/powercontroller/Module.cpp diff --git a/Source/powermanager/Module.h b/Source/powercontroller/Module.h similarity index 97% rename from Source/powermanager/Module.h rename to Source/powercontroller/Module.h index 341c7448..c9c13219 100644 --- a/Source/powermanager/Module.h +++ b/Source/powercontroller/Module.h @@ -25,7 +25,6 @@ #include #include -#include #include #if defined(__WINDOWS__) && defined(POWERMANAGER_EXPORTS) diff --git a/Source/powermanager/power_controller.cpp b/Source/powercontroller/power_controller.cpp similarity index 92% rename from Source/powermanager/power_controller.cpp rename to Source/powercontroller/power_controller.cpp index 10e123cc..bc313d08 100644 --- a/Source/powermanager/power_controller.cpp +++ b/Source/powercontroller/power_controller.cpp @@ -17,11 +17,11 @@ * limitations under the License. */ +// std includes +#include #include -#include -#include - +// Thunder includes #include #include @@ -186,11 +186,7 @@ PowerController_ThermalTemperature_t convert(const ThermalTemperature from) return (it != map.end()) ? it->second : THERMAL_TEMPERATURE_UNKNOWN; } -static string Callsign() -{ - static constexpr const TCHAR Default[] = _T("org.rdk.PowerManager"); - return (Default); -} +static constexpr const TCHAR callSign[] = _T("org.rdk.PowerManager"); class PowerController : public RPC::SmartInterfaceType { private: @@ -213,6 +209,12 @@ class PowerController : public RPC::SmartInterfaceType { } + Notification(const Notification&) = delete; // Delete copy constructor + Notification& operator=(const Notification&) = delete; // Delete copy assignment operator + + Notification(Notification&&) = delete; // Delete move constructor + Notification& operator=(Notification&&) = delete; // Delete move assignment operator + virtual void OnPowerModeChanged(const PowerState& currentState, const PowerState& newState) override { _parent.NotifyPowerModeChanged(currentState, newState); @@ -254,13 +256,17 @@ class PowerController : public RPC::SmartInterfaceType , _powerManagerNotification(*this) , _shutdown(false) { - uint32_t res = BaseClass::Open(RPC::CommunicationTimeOut, BaseClass::Connector(), Callsign()); + uint32_t res = BaseClass::Open(RPC::CommunicationTimeOut, BaseClass::Connector(), callSign); ASSERT(Core::ERROR_NONE == res); + if (Core::ERROR_NONE != res) { + std::cerr << "Unexpected, /tmp/communicator comm channel is always expected\n"; + } } ~PowerController() { _shutdown = true; + /* Close destroys _powerManagerInterface too */ BaseClass::Close(Core::infinite); } @@ -274,6 +280,9 @@ class PowerController : public RPC::SmartInterfaceType _powerManagerInterface = BaseClass::Interface(); if (_powerManagerInterface != nullptr) { _powerManagerInterface->Register(&_powerManagerNotification); + } else { + // Internal error powerManager is running, but QueryInterface failed for it ? + std::cerr << "Unexpected, powerManager is activated, but interface null ?\n"; } } } else { @@ -282,6 +291,8 @@ class PowerController : public RPC::SmartInterfaceType _powerManagerInterface->Unregister(&_powerManagerNotification); _powerManagerInterface->Release(); _powerManagerInterface = nullptr; + } else { + std::cerr << "Unexpected, powerManager just deactivated, but interface already null ?\n"; } } @@ -330,20 +341,20 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - PowerState _currentState = PowerState::POWER_STATE_UNKNOWN; - PowerState _previousState = PowerState::POWER_STATE_UNKNOWN; + PowerState currentState_ = PowerState::POWER_STATE_UNKNOWN; + PowerState previousState_ = PowerState::POWER_STATE_UNKNOWN; _lock.Lock(); if (_powerManagerInterface) { - result = _powerManagerInterface->GetPowerState(_currentState, _previousState); + result = _powerManagerInterface->GetPowerState(currentState_, previousState_); } _lock.Unlock(); if (Core::ERROR_NONE == result) { - *currentState = convert(_currentState); - *previousState = convert(_previousState); + *currentState = convert(currentState_); + *previousState = convert(previousState_); } return result; @@ -353,12 +364,12 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - PowerState _powerState = convert(powerState); + PowerState powerState_ = convert(powerState); _lock.Lock(); if (_powerManagerInterface) { - result = _powerManagerInterface->SetPowerState(keyCode, _powerState, reason); + result = _powerManagerInterface->SetPowerState(keyCode, powerState_, reason); } _lock.Unlock(); @@ -459,18 +470,18 @@ class PowerController : public RPC::SmartInterfaceType uint32_t GetLastWakeupReason(PowerController_WakeupReason_t* wakeupReason) { uint32_t result = Core::ERROR_UNAVAILABLE; - WakeupReason _wakeupReason = WakeupReason::WAKEUP_REASON_UNKNOWN; + WakeupReason wakeupReason_ = WakeupReason::WAKEUP_REASON_UNKNOWN; _lock.Lock(); if (_powerManagerInterface) { - result = _powerManagerInterface->GetLastWakeupReason(_wakeupReason); + result = _powerManagerInterface->GetLastWakeupReason(wakeupReason_); } _lock.Unlock(); if (Core::ERROR_NONE == result) { - *wakeupReason = convert(_wakeupReason); + *wakeupReason = convert(wakeupReason_); } return result; @@ -568,13 +579,13 @@ class PowerController : public RPC::SmartInterfaceType uint32_t SetSystemMode(const PowerController_SystemMode_t currentMode, const PowerController_SystemMode_t newMode) { uint32_t result = Core::ERROR_UNAVAILABLE; - SystemMode _currentMode = convert(currentMode); - SystemMode _newMode = convert(newMode); + SystemMode currentMode_ = convert(currentMode); + SystemMode newMode_ = convert(newMode); _lock.Lock(); if (_powerManagerInterface) { - result = _powerManagerInterface->SetSystemMode(_currentMode, _newMode); + result = _powerManagerInterface->SetSystemMode(currentMode_, newMode_); } _lock.Unlock(); @@ -585,18 +596,18 @@ class PowerController : public RPC::SmartInterfaceType uint32_t GetPowerStateBeforeReboot(PowerController_PowerState_t* powerStateBeforeReboot) { uint32_t result = Core::ERROR_UNAVAILABLE; - PowerState _powerStateBeforeReboot = PowerState::POWER_STATE_UNKNOWN; + PowerState powerStateBeforeReboot_ = PowerState::POWER_STATE_UNKNOWN; _lock.Lock(); if (_powerManagerInterface) { - result = _powerManagerInterface->GetPowerStateBeforeReboot(_powerStateBeforeReboot); + result = _powerManagerInterface->GetPowerStateBeforeReboot(powerStateBeforeReboot_); } _lock.Unlock(); if (Core::ERROR_NONE == result) { - *powerStateBeforeReboot = convert(_powerStateBeforeReboot); + *powerStateBeforeReboot = convert(powerStateBeforeReboot_); } return result; @@ -604,12 +615,12 @@ class PowerController : public RPC::SmartInterfaceType void NotifyPowerModeChanged(const PowerState& currentState, const PowerState& newState) { - PowerController_PowerState_t _currentState = convert(currentState); - PowerController_PowerState_t _newState = convert(newState); + PowerController_PowerState_t currentState_ = convert(currentState); + PowerController_PowerState_t newState_ = convert(newState); _lock.Lock(); for (auto& index : _powerModeChangedCallbacks) { - index.first(_currentState, _newState, index.second); + index.first(currentState_, newState_, index.second); } _lock.Unlock(); @@ -617,12 +628,12 @@ class PowerController : public RPC::SmartInterfaceType void NotifyPowerModePreChange(const PowerState& currentState, const PowerState& newState) { - PowerController_PowerState_t _currentState = convert(currentState); - PowerController_PowerState_t _newState = convert(newState); + PowerController_PowerState_t currentState_ = convert(currentState); + PowerController_PowerState_t newState_ = convert(newState); _lock.Lock(); for (auto& index : _powerModePreChangeCallbacks) { - index.first(_currentState, _newState, index.second); + index.first(currentState_, newState_, index.second); } _lock.Unlock(); @@ -652,13 +663,13 @@ class PowerController : public RPC::SmartInterfaceType void NotifyThermalModeChanged(const ThermalTemperature& currentThermalLevel, const ThermalTemperature& newThermalLevel, const float& currentTemperature) { - PowerController_ThermalTemperature_t _currentThermalLevel = convert(currentThermalLevel); - PowerController_ThermalTemperature_t _newThermalLevel = convert(newThermalLevel); + PowerController_ThermalTemperature_t currentThermalLevel_ = convert(currentThermalLevel); + PowerController_ThermalTemperature_t newThermalLevel_ = convert(newThermalLevel); _lock.Lock(); for (auto& index : _thermalModeChangedCallbacks) { - index.first(_currentThermalLevel, _newThermalLevel, currentTemperature, index.second); + index.first(currentThermalLevel_, newThermalLevel_, currentTemperature, index.second); } _lock.Unlock(); diff --git a/Source/powermanager/power_controller.h b/Source/powercontroller/power_controller.h similarity index 91% rename from Source/powermanager/power_controller.h rename to Source/powercontroller/power_controller.h index a882f206..8ecad407 100644 --- a/Source/powermanager/power_controller.h +++ b/Source/powercontroller/power_controller.h @@ -84,9 +84,9 @@ typedef enum PowerController_SystemMode { SYSTEM_MODE_WAREHOUSE = 3 /* WAREHOUSE */ } PowerController_SystemMode_t; -#define POWER_MANAGER_ERROR_NONE 0 -#define POWER_MANAGER_ERROR_GENERAL 1 -#define POWER_MANAGER_ERROR_UNAVAILABLE 2 +#define POWER_CONTROLLER_ERROR_NONE 0 +#define POWER_CONTROLLER_ERROR_GENERAL 1 +#define POWER_CONTROLLER_ERROR_UNAVAILABLE 2 /** * @brief Initializes the Power Controller. @@ -127,7 +127,7 @@ void PowerController_Term(); * - Use this function to confirm the operational status of the Power Manager plugin. * - Calling this function is NOT MANDATORY but optional * - Clients can register for notifications about state changes using `PowerController_RegisterOperationalStateChangeCallback`. - * - If the Power Manager interface is not active, subsequent Power Manager operations will fail with the error `POWER_MANAGER_ERROR_UNAVAILABLE`. + * - If the Power Manager interface is not active, subsequent Power Manager operations will fail with the error `POWER_CONTROLLER_ERROR_UNAVAILABLE`. * * @see PowerController_RegisterOperationalStateChangeCallback */ @@ -137,14 +137,15 @@ bool PowerController_IsOperational(); // @text getPowerState // @brief Get Power State // @param powerState: Get current power state -uint32_t PowerController_GetPowerState(PowerController_PowerState_t* currentState, PowerController_PowerState_t* previousState); +uint32_t PowerController_GetPowerState(PowerController_PowerState_t* currentState /* @out */, PowerController_PowerState_t* previousState /* @out */); /** Sets Power State . */ // @text setPowerState // @brief Set Power State +// @param keyCode: NA for most platfroms, to be depricated // @param powerState: Set power to this state -// @param reason: Reason for moving to the power state -uint32_t PowerController_SetPowerState(const int keyCode, const PowerController_PowerState_t powerstate, const char* reason); +// @param reason: null terminated string stating reason for for state change +uint32_t PowerController_SetPowerState(const int keyCode /* @in */, const PowerController_PowerState_t powerstate /* @in */, const char* reason /* @in */); /** Gets the current Thermal state.*/ // @text getThermalState @@ -173,7 +174,7 @@ uint32_t PowerController_GetTemperatureThresholds(float* high /* @out */, float* // @param graceInterval: interval in secs? uint32_t PowerController_SetOvertempGraceInterval(const int graceInterval /* @in */); -/** Gets the current Temperature Thresholds.*/ +/** Gets the grace interval for over-temperature.*/ // @property // @text PowerController_GetOvertempGraceInterval // @brief Get Temperature Grace interval @@ -201,9 +202,12 @@ uint32_t PowerController_GetLastWakeupReason(PowerController_WakeupReason_t* wak // @param keycode: Key code for wakeup uint32_t PowerController_GetLastWakeupKeyCode(int* keycode /* @out */); -/** Perform Reboot */ +/** Request Reboot with PowerManager */ // @text reboot // @brief Reboot device +// @param rebootRequestor: null terminated string identifier for the entity requesting the reboot. +// @param rebootReasonCustom: custom-defined reason for the reboot, provided as a null terminated string. +// @param rebootReasonOther: null terminated string describing any other reasons for the reboot. uint32_t PowerController_Reboot(const char* rebootRequestor /* @in */, const char* rebootReasonCustom /* @in */, const char* rebootReasonOther /* @in */); /** Set Network Standby Mode */ @@ -238,13 +242,13 @@ uint32_t PowerController_GetWakeupSrcConfig(int* powerMode /* @out */, int* srcT /** Initiate System mode change */ // @text PowerController_SetSystemMode // @brief System mode change -// @param oldMode: old mode +// @param oldMode: current mode // @param newMode: new mode uint32_t PowerController_SetSystemMode(const PowerController_SystemMode_t currentMode /* @in */, const PowerController_SystemMode_t newMode /* @in */); -/** Get Power State before reboot */ +/** Get Power State before last reboot */ // @text PowerController_GetPowerStateBeforeReboot -// @brief Get Power state before reboot +// @brief Get Power state before last reboot // @param powerStateBeforeReboot: power state uint32_t PowerController_GetPowerStateBeforeReboot(PowerController_PowerState_t* powerStateBeforeReboot /* @out */); @@ -257,6 +261,9 @@ typedef void (*PowerController_NetworkStandbyModeChangedCb)(const bool enabled, typedef void (*PowerController_ThermalModeChangedCb)(const PowerController_ThermalTemperature_t currentThermalLevel, const PowerController_ThermalTemperature_t newThermalLevel, const float currentTemperature, void* userdata); typedef void (*PowerController_RebootBeginCb)(const char* rebootReasonCustom, const char* rebootReasonOther, const char* rebootRequestor, void* userdata); +/* Type defines for callbacks / notifications */ +/* userdata in all callbacks are opque, clients can use to have context to callbacks */ + /** Register for PowerManager plugin operational state change event callback, for initial state use `PowerController_IsOperational` call */ uint32_t PowerController_RegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback, void* userdata); /** UnRegister (previously registered) PowerManager plugin operational state change event callback */ From 76dd97c0d4c4f4964ba4bd37f7593c57cd038c86 Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Thu, 13 Feb 2025 16:43:46 +0530 Subject: [PATCH 10/16] minor --- Source/powercontroller/power_controller.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/powercontroller/power_controller.cpp b/Source/powercontroller/power_controller.cpp index bc313d08..456209ef 100644 --- a/Source/powercontroller/power_controller.cpp +++ b/Source/powercontroller/power_controller.cpp @@ -259,7 +259,7 @@ class PowerController : public RPC::SmartInterfaceType uint32_t res = BaseClass::Open(RPC::CommunicationTimeOut, BaseClass::Connector(), callSign); ASSERT(Core::ERROR_NONE == res); if (Core::ERROR_NONE != res) { - std::cerr << "Unexpected, /tmp/communicator comm channel is always expected\n"; + std::cerr << "Unexpected, /tmp/communicator com channel open failed. Is Thunder running?\n"; } } @@ -274,6 +274,8 @@ class PowerController : public RPC::SmartInterfaceType { _lock.Lock(); + std::cout << "PowerController::Operational (" << callSign << ") " << upAndRunning << std::endl; + if (upAndRunning) { // Communicatior opened && PowerManager is Activated if (_powerManagerInterface == nullptr) { From 0c4a54ca65a6c62d568aed4b4b5da9e08996ae86 Mon Sep 17 00:00:00 2001 From: skamath <161743589+skamath@users.noreply.github.com> Date: Fri, 14 Feb 2025 17:40:14 +0530 Subject: [PATCH 11/16] minor, added success and failure markers --- Source/powercontroller/power_controller.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/powercontroller/power_controller.cpp b/Source/powercontroller/power_controller.cpp index 456209ef..f5117782 100644 --- a/Source/powercontroller/power_controller.cpp +++ b/Source/powercontroller/power_controller.cpp @@ -259,7 +259,7 @@ class PowerController : public RPC::SmartInterfaceType uint32_t res = BaseClass::Open(RPC::CommunicationTimeOut, BaseClass::Connector(), callSign); ASSERT(Core::ERROR_NONE == res); if (Core::ERROR_NONE != res) { - std::cerr << "Unexpected, /tmp/communicator com channel open failed. Is Thunder running?\n"; + std::cerr << "FATAL, /tmp/communicato com channel open failed. Is Thunder running?\n"; } } @@ -282,9 +282,10 @@ class PowerController : public RPC::SmartInterfaceType _powerManagerInterface = BaseClass::Interface(); if (_powerManagerInterface != nullptr) { _powerManagerInterface->Register(&_powerManagerNotification); + std::cout << "PowerController successfully established COM-RPC connection with PowerManager plugin\n"; } else { // Internal error powerManager is running, but QueryInterface failed for it ? - std::cerr << "Unexpected, powerManager is activated, but interface null ?\n"; + std::cerr << "PowerController failed to establish COM-RPC connection with PowerManager plugin\n"; } } } else { From a33d551fb050a3d5550563409448857cd37b8ee3 Mon Sep 17 00:00:00 2001 From: skamath <161743589+skamath@users.noreply.github.com> Date: Fri, 14 Feb 2025 17:41:02 +0530 Subject: [PATCH 12/16] typo --- Source/powercontroller/power_controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/powercontroller/power_controller.cpp b/Source/powercontroller/power_controller.cpp index f5117782..6e00578b 100644 --- a/Source/powercontroller/power_controller.cpp +++ b/Source/powercontroller/power_controller.cpp @@ -259,7 +259,7 @@ class PowerController : public RPC::SmartInterfaceType uint32_t res = BaseClass::Open(RPC::CommunicationTimeOut, BaseClass::Connector(), callSign); ASSERT(Core::ERROR_NONE == res); if (Core::ERROR_NONE != res) { - std::cerr << "FATAL, /tmp/communicato com channel open failed. Is Thunder running?\n"; + std::cerr << "FATAL, /tmp/communicator com channel open failed. Is Thunder running?\n"; } } From 5d575b1ed3f357aded2160b621b558921b05c8a3 Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Sat, 8 Mar 2025 01:06:46 +0530 Subject: [PATCH 13/16] RDK-55706: PowerModePreChange handling --- Source/powercontroller/power_controller.cpp | 693 ++++++++++++++++---- Source/powercontroller/power_controller.h | 98 ++- 2 files changed, 658 insertions(+), 133 deletions(-) diff --git a/Source/powercontroller/power_controller.cpp b/Source/powercontroller/power_controller.cpp index 6e00578b..b4c6ad3f 100644 --- a/Source/powercontroller/power_controller.cpp +++ b/Source/powercontroller/power_controller.cpp @@ -18,8 +18,10 @@ */ // std includes +#include #include -#include +#include +#include // Thunder includes #include @@ -188,18 +190,179 @@ PowerController_ThermalTemperature_t convert(const ThermalTemperature from) static constexpr const TCHAR callSign[] = _T("org.rdk.PowerManager"); +// Templated Clallback list avoid code duplication, for individual callback types +// This class expects mechanism to register / unregister for individual & unique notifications with PowerManager +// via RegisterNotificationLocked and UnregisterNotificationLocked methods. To be implemented in PowerController (i,e PARENT) +template +class CallbackList : public std::list { + PARENT& _parent; + bool _registered; + +public: + CallbackList(PARENT& parent) + : _parent(parent) + , _registered(false) + { + } + + // Locked method expected to be called from locked context + uint32_t RegisterCallbackLocked(typename CallbackType::Type callback, void* userdata) + { + uint32_t result = Core::ERROR_ALREADY_CONNECTED; + + auto it = std::find_if(this->begin(), this->end(), [&callback](const CallbackType& cb) { + return cb.callback == callback; + }); + + if (it == this->end()) { + this->emplace_back(callback, userdata); + result = Core::ERROR_NONE; + RegisterNotificationLocked(); + } + + return result; + } + + // Locked method expected to be called from locked context + uint32_t UnRegisterCallbackLocked(typename CallbackType::Type callback) + { + uint32_t result = Core::ERROR_ALREADY_RELEASED; + + auto it = std::find_if(this->begin(), this->end(), [&callback](const CallbackType& cb) { + return cb.callback == callback; + }); + + if (it != this->end()) { + this->erase(it); + result = Core::ERROR_NONE; + UnregisterNotificationLocked(false); + } + + return result; + } + + // Locked method expected to be called from locked context + inline void RegisterNotificationLocked() + { + if (!_registered && !this->empty() && _parent.IsActivatedLocked()) { + _registered = _parent.template RegisterNotificationLocked(); + } + } + + // Locked method expected to be called from locked context + // @param forced A boolean indicating whether to forcefully unregister from notification + // regardless of the callback list's state / unregister status. + // This is required to handle PowerManager restart scenarios. + inline void UnregisterNotificationLocked(const bool forced) + { + if (_registered && _parent.IsActivatedLocked() && (forced || this->empty())) { + bool registered = !_parent.template UnregisterNotificationLocked(); + _registered = forced || registered; + } + } +}; + +struct OperationalStateChangeCb { + using Type = PowerController_OperationalStateChangeCb; + Type callback; + void* userdata; + + OperationalStateChangeCb(Type cb, void* ud) + : callback(cb) + , userdata(ud) + { + } +}; + +struct NetworkStandbyModeChangedCb { + using Type = PowerController_NetworkStandbyModeChangedCb; + Type callback; + void* userdata; + + NetworkStandbyModeChangedCb(Type cb, void* ud) + : callback(cb) + , userdata(ud) + { + } +}; + +struct PowerModePreChangedCb { + using Type = PowerController_PowerModePreChangeCb; + Type callback; + void* userdata; + + PowerModePreChangedCb(Type cb, void* ud) + : callback(cb) + , userdata(ud) + { + } +}; + +struct PowerModeChangedCb { + using Type = PowerController_PowerModeChangedCb; + Type callback; + void* userdata; + + PowerModeChangedCb(Type cb, void* ud) + : callback(cb) + , userdata(ud) + { + } +}; + +struct DeepSleepTimeoutCb { + using Type = PowerController_DeepSleepTimeoutCb; + Type callback; + void* userdata; + + DeepSleepTimeoutCb(Type cb, void* ud) + : callback(cb) + , userdata(ud) + { + } +}; + +struct ThermalModeChangedCb { + using Type = PowerController_ThermalModeChangedCb; + Type callback; + void* userdata; + + ThermalModeChangedCb(Type cb, void* ud) + : callback(cb) + , userdata(ud) + { + } +}; + +struct RebootBeginCb { + using Type = PowerController_RebootBeginCb; + Type callback; + void* userdata; + + RebootBeginCb(Type cb, void* ud) + : callback(cb) + , userdata(ud) + { + } +}; + class PowerController : public RPC::SmartInterfaceType { private: using BaseClass = RPC::SmartInterfaceType; - using OperationalStateChangeCallbacks = std::map; - using PowerModeChangedCallbacks = std::map; - using PowerModePreChangeCallbacks = std::map; - using DeepSleepTimeoutCallbacks = std::map; - using NetworkStandbyModeChangedCallbacks = std::map; - using ThermalModeChangedCallbacks = std::map; - using RebootBeginCallbacks = std::map; - - class Notification : public Exchange::IPowerManager::INotification { + using OperationalStateChangeCallbacks = CallbackList; + using PowerModePreChangeCallbacks = CallbackList; + using PowerModeChangedCallbacks = CallbackList; + using DeepSleepTimeoutCallbacks = CallbackList; + using NetworkStandbyModeChangedCallbacks = CallbackList; + using ThermalModeChangedCallbacks = CallbackList; + using RebootBeginCallbacks = CallbackList; + + class Notification : public Exchange::IPowerManager::IRebootNotification, + public Exchange::IPowerManager::IModeChangedNotification, + public Exchange::IPowerManager::IModePreChangeNotification, + public Exchange::IPowerManager::IDeepSleepTimeoutNotification, + public Exchange::IPowerManager::INetworkStandbyModeChangedNotification, + public Exchange::IPowerManager::IThermalModeChangedNotification { private: PowerController& _parent; @@ -220,9 +383,9 @@ class PowerController : public RPC::SmartInterfaceType _parent.NotifyPowerModeChanged(currentState, newState); } - virtual void OnPowerModePreChange(const PowerState& currentState, const PowerState& newState) override + virtual void OnPowerModePreChange(const PowerState& currentState, const PowerState& newState, const int transactionId, const int stateChangeAfter) override { - _parent.NotifyPowerModePreChange(currentState, newState); + _parent.NotifyPowerModePreChange(currentState, newState, transactionId, stateChangeAfter); } virtual void OnDeepSleepTimeout(const int& wakeupTimeout) override @@ -246,21 +409,36 @@ class PowerController : public RPC::SmartInterfaceType } BEGIN_INTERFACE_MAP(Notification) - INTERFACE_ENTRY(Exchange::IPowerManager::INotification) + INTERFACE_ENTRY(Exchange::IPowerManager::IRebootNotification) + INTERFACE_ENTRY(Exchange::IPowerManager::IModePreChangeNotification) + INTERFACE_ENTRY(Exchange::IPowerManager::IModeChangedNotification) + INTERFACE_ENTRY(Exchange::IPowerManager::IDeepSleepTimeoutNotification) + INTERFACE_ENTRY(Exchange::IPowerManager::INetworkStandbyModeChangedNotification) + INTERFACE_ENTRY(Exchange::IPowerManager::IThermalModeChangedNotification) END_INTERFACE_MAP + + template + inline T* baseInterface() + { + static_assert(std::is_base_of(), "base type mismatch"); + return static_cast(this); + } }; PowerController() : BaseClass() , _powerManagerInterface(nullptr) , _powerManagerNotification(*this) + , _operationalStateChangeCallbacks(*this) + , _powerModePreChangeCallbacks(*this) + , _powerModeChangedCallbacks(*this) + , _deepSleepTimeoutCallbacks(*this) + , _networkStandbyModeChangedCallbacks(*this) + , _thermalModeChangedCallbacks(*this) + , _rebootBeginCallbacks(*this) , _shutdown(false) { - uint32_t res = BaseClass::Open(RPC::CommunicationTimeOut, BaseClass::Connector(), callSign); - ASSERT(Core::ERROR_NONE == res); - if (Core::ERROR_NONE != res) { - std::cerr << "FATAL, /tmp/communicator com channel open failed. Is Thunder running?\n"; - } + Connect(); } ~PowerController() @@ -272,16 +450,19 @@ class PowerController : public RPC::SmartInterfaceType virtual void Operational(const bool upAndRunning) override { - _lock.Lock(); + _apiLock.Lock(); - std::cout << "PowerController::Operational (" << callSign << ") " << upAndRunning << std::endl; + // avoid misleading log during shutdown + if (!upAndRunning && _shutdown) { + std::cout << "PowerController::Operational (" << callSign << ") " << upAndRunning << std::endl; + } if (upAndRunning) { // Communicatior opened && PowerManager is Activated - if (_powerManagerInterface == nullptr) { + if (nullptr == _powerManagerInterface) { _powerManagerInterface = BaseClass::Interface(); if (_powerManagerInterface != nullptr) { - _powerManagerInterface->Register(&_powerManagerNotification); + RegisterNotificationsLocked(); std::cout << "PowerController successfully established COM-RPC connection with PowerManager plugin\n"; } else { // Internal error powerManager is running, but QueryInterface failed for it ? @@ -290,40 +471,120 @@ class PowerController : public RPC::SmartInterfaceType } } else { // PowerManager is Deactivated || Communicator closed - if (_powerManagerInterface != nullptr) { - _powerManagerInterface->Unregister(&_powerManagerNotification); + if (nullptr != _powerManagerInterface) { + UnregisterNotificationsLocked(); _powerManagerInterface->Release(); _powerManagerInterface = nullptr; } else { std::cerr << "Unexpected, powerManager just deactivated, but interface already null ?\n"; } } + _apiLock.Unlock(); + _callbackLock.Lock(); // avoid notifying operational state changed if shuting down because of Term if (!_shutdown) { - for (auto& index : _operationalStateChangeCallbacks) { - index.first(upAndRunning, index.second); + for (auto& cb : _operationalStateChangeCallbacks) { + cb.callback(upAndRunning, cb.userdata); } } + _callbackLock.Unlock(); + } + + // Locked method expected to be called from locked context + void RegisterNotificationsLocked() + { + _powerModeChangedCallbacks.RegisterNotificationLocked(); + _powerModePreChangeCallbacks.RegisterNotificationLocked(); + _deepSleepTimeoutCallbacks.RegisterNotificationLocked(); + _networkStandbyModeChangedCallbacks.RegisterNotificationLocked(); + _thermalModeChangedCallbacks.RegisterNotificationLocked(); + _rebootBeginCallbacks.RegisterNotificationLocked(); + } - _lock.Unlock(); + // Locked method expected to be called from locked context + void UnregisterNotificationsLocked() + { + _powerModeChangedCallbacks.UnregisterNotificationLocked(true); + _powerModePreChangeCallbacks.UnregisterNotificationLocked(true); + _deepSleepTimeoutCallbacks.UnregisterNotificationLocked(true); + _networkStandbyModeChangedCallbacks.UnregisterNotificationLocked(true); + _thermalModeChangedCallbacks.UnregisterNotificationLocked(true); + _rebootBeginCallbacks.UnregisterNotificationLocked(true); + } + + inline bool IsConnected() const + { + return (~0 != ConnectionId()); } public: + // Locked method expected to be called from locked context + inline bool IsActivatedLocked() const + { + return (nullptr != _powerManagerInterface); + } + + uint32_t Connect() + { + uint32_t status = Core::ERROR_NONE; + std::string errMsg = ""; + + _apiLock.Lock(); + do { + if (!IsConnected()) { + uint32_t res = BaseClass::Open(RPC::CommunicationTimeOut, BaseClass::Connector(), callSign); + if (Core::ERROR_NONE != res) { + std::cerr << "/tmp/communicator com channel open failed. Is Thunder running?\n"; + errMsg = "COM-RPC channel open failed"; + status = Core::ERROR_UNAVAILABLE; + break; + } + } + + if (nullptr == _powerManagerInterface) { + errMsg = "PowerManager plugin is not activated yet"; + status = Core::ERROR_NOT_EXIST; + } + } while (false); + + _apiLock.Unlock(); + + std::cout << "PowerController::Connect (" << callSign << ") status: " << status << ", errMsg: \"" << errMsg << "\"" << std::endl; + + return status; + } + + // Locked method expected to be called from locked context (take care in specializations too) + template + bool RegisterNotificationLocked() + { + // static_assert(std::false_type::value, "Specialization required for CallbackType"); + return false; + } + + // Locked method expected to be called from locked context (take care in specializations too) + template + bool UnregisterNotificationLocked() + { + // static_assert(std::false_type::value, "Specialization required for CallbackType"); + return false; + } + static void Init() { - _lock.Lock(); + _apiLock.Lock(); if (nullptr == _instance) { ASSERT(0 == _nClients); _instance = new PowerController(); } _nClients++; - _lock.Unlock(); + _apiLock.Unlock(); } static void Term() { - _lock.Lock(); + _apiLock.Lock(); if (_nClients > 0) { _nClients--; } @@ -331,7 +592,7 @@ class PowerController : public RPC::SmartInterfaceType delete _instance; _instance = nullptr; } - _lock.Unlock(); + _apiLock.Unlock(); } static PowerController& Instance() @@ -347,13 +608,13 @@ class PowerController : public RPC::SmartInterfaceType PowerState currentState_ = PowerState::POWER_STATE_UNKNOWN; PowerState previousState_ = PowerState::POWER_STATE_UNKNOWN; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->GetPowerState(currentState_, previousState_); } - _lock.Unlock(); + _apiLock.Unlock(); if (Core::ERROR_NONE == result) { *currentState = convert(currentState_); @@ -369,13 +630,13 @@ class PowerController : public RPC::SmartInterfaceType PowerState powerState_ = convert(powerState); - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->SetPowerState(keyCode, powerState_, reason); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -384,13 +645,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->GetThermalState(*currentTemperature); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -399,13 +660,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->SetTemperatureThresholds(high, critical); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -414,13 +675,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->GetTemperatureThresholds(*high, *critical); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -429,13 +690,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->SetOvertempGraceInterval(graceInterval); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -444,13 +705,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->GetOvertempGraceInterval(*graceInterval); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -459,13 +720,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->SetDeepSleepTimer(timeOut); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -475,13 +736,13 @@ class PowerController : public RPC::SmartInterfaceType uint32_t result = Core::ERROR_UNAVAILABLE; WakeupReason wakeupReason_ = WakeupReason::WAKEUP_REASON_UNKNOWN; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->GetLastWakeupReason(wakeupReason_); } - _lock.Unlock(); + _apiLock.Unlock(); if (Core::ERROR_NONE == result) { *wakeupReason = convert(wakeupReason_); @@ -494,13 +755,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { _powerManagerInterface->GetLastWakeupKeyCode(*keycode); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -509,13 +770,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->Reboot(rebootRequestor, rebootReasonCustom, rebootReasonOther); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -524,13 +785,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->SetNetworkStandbyMode(standbyMode); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -538,13 +799,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->GetNetworkStandbyMode(*standbyMode); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -553,13 +814,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->SetWakeupSrcConfig(powerMode, wakeSrcType, config); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -568,13 +829,13 @@ class PowerController : public RPC::SmartInterfaceType { uint32_t result = Core::ERROR_UNAVAILABLE; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->GetWakeupSrcConfig(powerMode, srcType, config); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -585,13 +846,13 @@ class PowerController : public RPC::SmartInterfaceType SystemMode currentMode_ = convert(currentMode); SystemMode newMode_ = convert(newMode); - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->SetSystemMode(currentMode_, newMode_); } - _lock.Unlock(); + _apiLock.Unlock(); return result; } @@ -601,13 +862,13 @@ class PowerController : public RPC::SmartInterfaceType uint32_t result = Core::ERROR_UNAVAILABLE; PowerState powerStateBeforeReboot_ = PowerState::POWER_STATE_UNKNOWN; - _lock.Lock(); + _apiLock.Lock(); if (_powerManagerInterface) { result = _powerManagerInterface->GetPowerStateBeforeReboot(powerStateBeforeReboot_); } - _lock.Unlock(); + _apiLock.Unlock(); if (Core::ERROR_NONE == result) { *powerStateBeforeReboot = convert(powerStateBeforeReboot_); @@ -616,52 +877,112 @@ class PowerController : public RPC::SmartInterfaceType return result; } + uint32_t AddPowerModePreChangeClient(const std::string& clientName, uint32_t& clientId) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _apiLock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->AddPowerModePreChangeClient(clientName, clientId); + } + + _apiLock.Unlock(); + + return result; + } + + uint32_t RemovePowerModePreChangeClient(const uint32_t clientId) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _apiLock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->RemovePowerModePreChangeClient(clientId); + } + + _apiLock.Unlock(); + + return result; + } + + uint32_t DelayPowerModeChangeBy(const uint32_t clientId, const int transactionId, const int delay) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _apiLock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->DelayPowerModeChangeBy(clientId, transactionId, delay); + } + + _apiLock.Unlock(); + + return result; + } + + uint32_t PowerModePreChangeComplete(const uint32_t clientId, const int transactionId) + { + uint32_t result = Core::ERROR_UNAVAILABLE; + + _apiLock.Lock(); + + if (_powerManagerInterface) { + result = _powerManagerInterface->PowerModePreChangeComplete(clientId, transactionId); + } + + _apiLock.Unlock(); + + return result; + } + void NotifyPowerModeChanged(const PowerState& currentState, const PowerState& newState) { PowerController_PowerState_t currentState_ = convert(currentState); PowerController_PowerState_t newState_ = convert(newState); - _lock.Lock(); + _callbackLock.Lock(); - for (auto& index : _powerModeChangedCallbacks) { - index.first(currentState_, newState_, index.second); + for (auto& cb : _powerModeChangedCallbacks) { + cb.callback(currentState_, newState_, cb.userdata); } - _lock.Unlock(); + _callbackLock.Unlock(); } - void NotifyPowerModePreChange(const PowerState& currentState, const PowerState& newState) + void NotifyPowerModePreChange(const PowerState& currentState, const PowerState& newState, const int transactionId, const int stateChangeAfter) { PowerController_PowerState_t currentState_ = convert(currentState); PowerController_PowerState_t newState_ = convert(newState); - _lock.Lock(); + _callbackLock.Lock(); - for (auto& index : _powerModePreChangeCallbacks) { - index.first(currentState_, newState_, index.second); + for (auto& cb : _powerModePreChangeCallbacks) { + cb.callback(currentState_, newState_, transactionId, stateChangeAfter, cb.userdata); } - _lock.Unlock(); + _callbackLock.Unlock(); } void NotifyDeepSleepTimeout(const int& wakeupTimeout) { - _lock.Lock(); + _callbackLock.Lock(); - for (auto& index : _deepSleepTimeoutCallbacks) { - index.first(wakeupTimeout, index.second); + for (auto& cb : _deepSleepTimeoutCallbacks) { + cb.callback(wakeupTimeout, cb.userdata); } - _lock.Unlock(); + _callbackLock.Unlock(); } void NotifyNetworkStandbyModeChanged(const bool& enabled) { - _lock.Lock(); + _callbackLock.Lock(); - for (auto& index : _networkStandbyModeChangedCallbacks) { - index.first(enabled, index.second); + for (auto& cb : _networkStandbyModeChangedCallbacks) { + cb.callback(enabled, cb.userdata); } - _lock.Unlock(); + _callbackLock.Unlock(); } void NotifyThermalModeChanged(const ThermalTemperature& currentThermalLevel, const ThermalTemperature& newThermalLevel, const float& currentTemperature) @@ -669,66 +990,61 @@ class PowerController : public RPC::SmartInterfaceType PowerController_ThermalTemperature_t currentThermalLevel_ = convert(currentThermalLevel); PowerController_ThermalTemperature_t newThermalLevel_ = convert(newThermalLevel); - _lock.Lock(); + _callbackLock.Lock(); - for (auto& index : _thermalModeChangedCallbacks) { - index.first(currentThermalLevel_, newThermalLevel_, currentTemperature, index.second); + for (auto& cb : _thermalModeChangedCallbacks) { + cb.callback(currentThermalLevel_, newThermalLevel_, currentTemperature, cb.userdata); } - _lock.Unlock(); + _callbackLock.Unlock(); } void NotifyRebootBegin(const string& rebootReasonCustom, const string& rebootReasonOther, const string& rebootRequestor) { - _lock.Lock(); + _callbackLock.Lock(); - for (auto& index : _rebootBeginCallbacks) { - index.first(rebootReasonCustom.c_str(), rebootReasonOther.c_str(), rebootRequestor.c_str(), index.second); + for (auto& cb : _rebootBeginCallbacks) { + cb.callback(rebootReasonCustom.c_str(), rebootReasonOther.c_str(), rebootRequestor.c_str(), cb.userdata); } - _lock.Unlock(); + _callbackLock.Unlock(); } // Generic template function for callback register - template - uint32_t RegisterCallback(std::map& callbackMap, CallbackType callback, void* userdata) + template + uint32_t RegisterCallback(CallbackList& callbacklist, typename CallbackType::Type callback, void* userdata) { - uint32_t result = Core::ERROR_ALREADY_CONNECTED; + uint32_t result = Core::ERROR_INVALID_PARAMETER; - ASSERT(callback != nullptr); + ASSERT(nullptr != callback); - _lock.Lock(); - if (callbackMap.find(callback) == callbackMap.end()) { - callbackMap.emplace(std::piecewise_construct, - std::forward_as_tuple(callback), - std::forward_as_tuple(userdata)); + if (nullptr != callback) { + _callbackLock.Lock(); - result = Core::ERROR_NONE; + result = callbacklist.RegisterCallbackLocked(callback, userdata); + + _callbackLock.Unlock(); } - _lock.Unlock(); return result; } // Generic template function for callback unregister - template - uint32_t UnRegisterCallback(std::map& callbackMap, CallbackType callback) + template + uint32_t UnRegisterCallback(CallbackList& callbacklist, typename CallbackType::Type callback) { - uint32_t result = Core::ERROR_ALREADY_RELEASED; + uint32_t result = Core::ERROR_INVALID_PARAMETER; - ASSERT(callback != nullptr); + ASSERT(nullptr != callback); - _lock.Lock(); + if (nullptr != callback) { + _callbackLock.Lock(); - auto it = callbackMap.find(callback); + result = callbacklist.UnRegisterCallbackLocked(callback); - if (it != callbackMap.end()) { - callbackMap.erase(it); - result = Core::ERROR_NONE; + _callbackLock.Unlock(); } - _lock.Unlock(); - return (result); } @@ -805,7 +1121,8 @@ class PowerController : public RPC::SmartInterfaceType private: static int _nClients; // Init() count static PowerController* _instance; - static Core::CriticalSection _lock; + static Core::CriticalSection _apiLock; + static Core::CriticalSection _callbackLock; Exchange::IPowerManager* _powerManagerInterface; // remote PowerManager plugin interface Core::Sink _powerManagerNotification; @@ -821,11 +1138,98 @@ class PowerController : public RPC::SmartInterfaceType bool _shutdown; }; +template <> +bool PowerController::RegisterNotificationLocked() +{ + // Operational state change notification is managed by SmartInterfaceType + return true; +} + +template <> +bool PowerController::RegisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Register(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::RegisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Register(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::RegisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Register(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::RegisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Register(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::RegisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Register(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::RegisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Register(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::UnregisterNotificationLocked() +{ + // Operational state change notification is managed by SmartInterfaceType + return true; +} + +template <> +bool PowerController::UnregisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Unregister(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::UnregisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Unregister(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::UnregisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Unregister(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::UnregisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Unregister(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::UnregisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Unregister(_powerManagerNotification.baseInterface()); +} + +template <> +bool PowerController::UnregisterNotificationLocked() +{ + return Core::ERROR_NONE == _powerManagerInterface->Unregister(_powerManagerNotification.baseInterface()); +} + } // nameless namespace -PowerController* PowerController::_instance = nullptr; -int PowerController::_nClients = 0; -Core::CriticalSection PowerController::_lock; +/* static */ PowerController* PowerController::_instance = nullptr; +/* static */ int PowerController::_nClients = 0; +/* static */ Core::CriticalSection PowerController::_apiLock; +/* static */ Core::CriticalSection PowerController::_callbackLock; extern "C" { @@ -839,6 +1243,11 @@ void PowerController_Term() PowerController::Term(); } +uint32_t PowerController_Connect() +{ + return PowerController::Instance().Connect(); +} + bool PowerController_IsOperational() { return PowerController::Instance().IsOperational(); @@ -846,8 +1255,8 @@ bool PowerController_IsOperational() uint32_t PowerController_GetPowerState(PowerController_PowerState_t* currentState, PowerController_PowerState_t* previousState) { - ASSERT(currentState != nullptr); - ASSERT(previousState != nullptr); + ASSERT(nullptr != currentState); + ASSERT(nullptr != previousState); return PowerController::Instance().GetPowerState(currentState, previousState); } @@ -858,7 +1267,7 @@ uint32_t PowerController_SetPowerState(const int keyCode, const PowerController_ uint32_t PowerController_GetThermalState(float* currentTemperature) { - ASSERT(currentTemperature != nullptr); + ASSERT(nullptr != currentTemperature); return PowerController::Instance().GetThermalState(currentTemperature); } @@ -869,8 +1278,8 @@ uint32_t PowerController_SetTemperatureThresholds(float high, float critical) uint32_t PowerController_GetTemperatureThresholds(float* high, float* critical) { - ASSERT(high != nullptr); - ASSERT(critical != nullptr); + ASSERT(nullptr != high); + ASSERT(nullptr != critical); return PowerController::Instance().GetTemperatureThresholds(high, critical); } @@ -881,7 +1290,7 @@ uint32_t PowerController_SetOvertempGraceInterval(const int graceInterval) uint32_t PowerController_GetOvertempGraceInterval(int* graceInterval /* @out */) { - ASSERT(graceInterval != nullptr); + ASSERT(nullptr != graceInterval); return PowerController::Instance().GetOvertempGraceInterval(graceInterval); } @@ -892,21 +1301,21 @@ uint32_t PowerController_SetDeepSleepTimer(const int timeOut) uint32_t PowerController_GetLastWakeupReason(PowerController_WakeupReason_t* wakeupReason) { - ASSERT(wakeupReason != nullptr); + ASSERT(nullptr != wakeupReason); return PowerController::Instance().GetLastWakeupReason(wakeupReason); } uint32_t PowerController_GetLastWakeupKeyCode(int* keycode) { - ASSERT(keycode != nullptr); + ASSERT(nullptr != keycode); return PowerController::Instance().GetLastWakeupKeyCode(keycode); } uint32_t PowerController_Reboot(const char* rebootRequestor, const char* rebootReasonCustom, const char* rebootReasonOther) { - ASSERT(rebootRequestor != nullptr); - ASSERT(rebootReasonCustom != nullptr); - ASSERT(rebootReasonOther != nullptr); + ASSERT(nullptr != rebootRequestor); + ASSERT(nullptr != rebootReasonCustom); + ASSERT(nullptr != rebootReasonOther); return PowerController::Instance().Reboot(rebootRequestor, rebootReasonCustom, rebootReasonOther); } @@ -928,9 +1337,9 @@ uint32_t PowerController_SetWakeupSrcConfig(const int powerMode, const int wakeS uint32_t PowerController_GetWakeupSrcConfig(int* powerMode, int* srcType, int* config) { - ASSERT(powerMode != nullptr); - ASSERT(srcType != nullptr); - ASSERT(config != nullptr); + ASSERT(nullptr != powerMode); + ASSERT(nullptr != srcType); + ASSERT(nullptr != config); return PowerController::Instance().GetWakeupSrcConfig(*powerMode, *srcType, *config); } @@ -941,10 +1350,32 @@ uint32_t PowerController_SetSystemMode(const PowerController_SystemMode_t curren uint32_t PowerController_GetPowerStateBeforeReboot(PowerController_PowerState_t* powerStateBeforeReboot) { - ASSERT(powerStateBeforeReboot != nullptr); + ASSERT(nullptr != powerStateBeforeReboot); return PowerController::Instance().GetPowerStateBeforeReboot(powerStateBeforeReboot); } +uint32_t PowerController_AddPowerModePreChangeClient(const char* clientName, uint32_t* clientId) +{ + ASSERT(nullptr != clientName); + ASSERT(nullptr != clientId); + return PowerController::Instance().AddPowerModePreChangeClient(clientName, *clientId); +} + +uint32_t PowerController_RemovePowerModePreChangeClient(const uint32_t clientId) +{ + return PowerController::Instance().RemovePowerModePreChangeClient(clientId); +} + +uint32_t PowerController_DelayPowerModeChangeBy(const uint32_t clientId, const int transactionId, const int delayPeriod) +{ + return PowerController::Instance().DelayPowerModeChangeBy(clientId, transactionId, delayPeriod); +} + +uint32_t PowerController_PowerModePreChangeComplete(const uint32_t clientId, const int transactionId) +{ + return PowerController::Instance().PowerModePreChangeComplete(clientId, transactionId); +} + uint32_t PowerController_RegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback, void* userdata) { return PowerController::Instance().RegisterOperationalStateChangedCallback(callback, userdata); diff --git a/Source/powercontroller/power_controller.h b/Source/powercontroller/power_controller.h index 8ecad407..5dfed80c 100644 --- a/Source/powercontroller/power_controller.h +++ b/Source/powercontroller/power_controller.h @@ -87,6 +87,7 @@ typedef enum PowerController_SystemMode { #define POWER_CONTROLLER_ERROR_NONE 0 #define POWER_CONTROLLER_ERROR_GENERAL 1 #define POWER_CONTROLLER_ERROR_UNAVAILABLE 2 +#define POWER_CONTROLLER_ERROR_NOT_EXIST 43 /** * @brief Initializes the Power Controller. @@ -96,11 +97,28 @@ typedef enum PowerController_SystemMode { * @details * - If the Power Controller instance does not already exist, it will be created. * - The instance count is incremented each time this function is called. + * - To check if the Power Controller is operational, use `PowerController_IsOperational` + * - If the Power Controller is not operational, clients can use `PowerController_Connect` to establish RPC connection with the Power Manager plugin. * * @see PowerController_Term */ void PowerController_Init(); +/** + * @brief PowerController attempts to connect to the Power Manager plugin. + * + * This function connects to the Power Manager plugin. + * + * @details + * - This function is used to connect to the Power Manager plugin. + * - This is the fist function that should be called after `PowerController_Init`. + * + * @return `POWER_CONTROLLER_ERROR_NONE` on success. + * @return `POWER_CONTROLLER_ERROR_UNAVAILABLE` if Thunder RPC server is not running / error establishing RPC communication channel. + * @return `POWER_CONTROLLER_ERROR_NOT_EXIST` if the PowerManager plugin is not actavated yet. + */ +uint32_t PowerController_Connect(); + /** * @brief Terminates the Power Controller. * @@ -252,17 +270,93 @@ uint32_t PowerController_SetSystemMode(const PowerController_SystemMode_t curren // @param powerStateBeforeReboot: power state uint32_t PowerController_GetPowerStateBeforeReboot(PowerController_PowerState_t* powerStateBeforeReboot /* @out */); +/** Engage a client in power mode change operation. */ +// @text PowerController_RemovePowerModePreChangeClient +// @brief - Register a client to engage in power mode state changes. +// - Added client should call either +// - `PowerModePreChangeComplete` API to inform power manager that this client has completed its pre-change operation. +// - Or `DelayPowerModeChangeBy` API to delay the power mode change. +// - If the client does not call `PowerModePreChangeComplete` API, the power mode change will complete +// after the maximum delay `stateChangeAfter` seconds (as received in `OnPowerModePreChange` event). +// - Clients are required to re-register if the PowerManager plugin restarts. Therefore, it is essential for clients to register +// for operational state changes using `PowerController_RegisterOperationalStateChangeCallback`. +// +// IMPORTANT: ** IT'S A BUG IF CLIENT `Unregister` FROM `IModePreChangeNotification` BEFORE DISENGAGING ITSELF ** +// always make sure to call `RemovePowerModePreChangeClient` before calling `Unregister` from `IModePreChangeNotification`. +// +// @param clientName: Name of the client as null terminated string +// @param clientId: Unique identifier for the client to be used while acknowledging the pre-change operation (`PowerModePreChangeComplete`) +// or to delay the power mode change (`DelayPowerModeChangeBy`) +uint32_t PowerController_AddPowerModePreChangeClient(const char *clientName /* @in */, uint32_t* clientId /* @out */); + +/** Disengage a client from the power mode change operation. */ +// @text PowerController_RemovePowerModePreChangeClient +// @brief Removes a registered client from participating in power mode pre-change operations. +// NOTE client will still continue to receive pre-change notifications. +// @param clientId: Unique identifier for the client. See `AddPowerModePreChangeClient` +uint32_t PowerController_RemovePowerModePreChangeClient(const uint32_t clientId /* @in */); + +/** Power prechange activity completed */ +// @text PowerController_PowerModePreChangeComplete +// @brief Pre power mode handling complete for given client and transation id +// @param clientId: Unique identifier for the client, as received in AddPowerModePreChangeClient +// @param transactionId: transaction id as received in OnPowerModePreChange +uint32_t PowerController_PowerModePreChangeComplete(const uint32_t clientId /* @in */, const int transactionId /* @in */); + +/** Delay Powermode change by given time */ +// @text PowerController_DelayPowerModeChangeBy +// @brief Delay Powermode change by given time. If different clients provide different values of delay, then the maximum of these values is used. +// @param clientId: Unique identifier for the client, as received in AddPowerModePreChangeClient +// @param transactionId: transaction id as received in OnPowerModePreChange +// @param delayPeriod: delay in seconds +uint32_t PowerController_DelayPowerModeChangeBy(const uint32_t clientId /* @in */, const int transactionId /* @in */, const int delayPeriod /* @in */); + /* Callback data types for event notifications from power manager plugin */ +// @brief Operational state changed event +// @param isOperational: true if PowerManager plugin is activated, false otherwise +// @param userdata: opaque data, client can use it to have context to callbacks typedef void (*PowerController_OperationalStateChangeCb)(bool isOperational, void* userdata); + +// @brief Power mode changed +// @param currentState: Current Power State +// @param newState: New Power State +// @param userdata: opaque data, client can use it to have context to callbacks typedef void (*PowerController_PowerModeChangedCb)(const PowerController_PowerState_t currentState, const PowerController_PowerState_t newState, void* userdata); -typedef void (*PowerController_PowerModePreChangeCb)(const PowerController_PowerState_t currentState, const PowerController_PowerState_t newState, void* userdata); + +// @brief Power mode Pre-change event +// @param currentState: Current Power State +// @param newState: Changing power state to this New Power State +// @param transactionId: transactionId to be used when invoking prePowerChangeComplete() / delayPowerModeChangeBy API +// @param stateChangeAfter: seconds after which the actual power mode will be applied. +// @param userdata: opaque data, client can use it to have context to callbacks +typedef void (*PowerController_PowerModePreChangeCb)(const PowerController_PowerState_t currentState, const PowerController_PowerState_t newState, const int transactionId, const int stateChangeAfter, void* userdata); + +// @brief Deep sleep timeout event +// @param wakeupTimeout: Deep sleep wakeup timeout in seconds +// @param userdata: opaque data, client can use it to have context to callbacks typedef void (*PowerController_DeepSleepTimeoutCb)(const int wakeupTimeout, void* userdata); + +// @brief Network Standby Mode changed event - only on XIone +// @param enabled: network standby enabled or disabled +// @param userdata: opaque data, client can use it to have context to callbacks typedef void (*PowerController_NetworkStandbyModeChangedCb)(const bool enabled, void* userdata); + +// @brief Thermal Mode changed event +// @param currentThermalLevel: current thermal level +// @param newThermalLevel: new thermal level +// @param currentTemperature: current temperature +// @param userdata: opaque data, client can use it to have context to callbacks typedef void (*PowerController_ThermalModeChangedCb)(const PowerController_ThermalTemperature_t currentThermalLevel, const PowerController_ThermalTemperature_t newThermalLevel, const float currentTemperature, void* userdata); + +// @brief Reboot begin event +// @param rebootReasonCustom: Reboot reason custom +// @param rebootReasonOther: Reboot reason other +// @param rebootRequestor: Reboot requested by +// @param userdata: opaque data, client can use it to have context to callbacks typedef void (*PowerController_RebootBeginCb)(const char* rebootReasonCustom, const char* rebootReasonOther, const char* rebootRequestor, void* userdata); /* Type defines for callbacks / notifications */ -/* userdata in all callbacks are opque, clients can use to have context to callbacks */ +/* userdata in all callbacks are opque, clients can use it to have context to callbacks */ /** Register for PowerManager plugin operational state change event callback, for initial state use `PowerController_IsOperational` call */ uint32_t PowerController_RegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback, void* userdata); From 1bb94513ccc32e51439181b34ec7a5e7c94bc501 Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Tue, 11 Mar 2025 10:49:01 +0530 Subject: [PATCH 14/16] fix unregister flow --- Source/powercontroller/power_controller.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Source/powercontroller/power_controller.cpp b/Source/powercontroller/power_controller.cpp index b4c6ad3f..f1c4f28c 100644 --- a/Source/powercontroller/power_controller.cpp +++ b/Source/powercontroller/power_controller.cpp @@ -256,8 +256,17 @@ class CallbackList : public std::list { inline void UnregisterNotificationLocked(const bool forced) { if (_registered && _parent.IsActivatedLocked() && (forced || this->empty())) { - bool registered = !_parent.template UnregisterNotificationLocked(); - _registered = forced || registered; + bool unregistered = _parent.template UnregisterNotificationLocked(); + + // --------------------------------------- + // | forced | unregistered | _registered | + // |--------|--------------|-------------| + // | 0 | 0 | 1 | + // | 0 | 1 | 0 | + // | 1 | 0 | 0 | + // | 1 | 1 | 0 | + // --------------------------------------- + _registered = !forced && !unregistered; } } }; From b3e92e9890bee1242ca0f4e9f420b290c3fad628 Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Tue, 11 Mar 2025 17:29:46 +0530 Subject: [PATCH 15/16] Add EXTERNAL visibility --- Source/powercontroller/power_controller.h | 89 +++++++++++++---------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/Source/powercontroller/power_controller.h b/Source/powercontroller/power_controller.h index 5dfed80c..dc1782fa 100644 --- a/Source/powercontroller/power_controller.h +++ b/Source/powercontroller/power_controller.h @@ -22,6 +22,17 @@ #include #include +#undef EXTERNAL +#if defined(WIN32) || defined(_WINDOWS) || defined (__CYGWIN__) || defined(_WIN64) +#ifdef DEVICEINFO_EXPORTS +#define EXTERNAL __declspec(dllexport) +#else +#define EXTERNAL __declspec(dllimport) +#pragma comment(lib, "deviceinfo.lib") +#endif +#else +#define EXTERNAL __attribute__((visibility("default"))) +#endif #ifdef __cplusplus extern "C" { #endif @@ -102,7 +113,7 @@ typedef enum PowerController_SystemMode { * * @see PowerController_Term */ -void PowerController_Init(); +EXTERNAL void PowerController_Init(); /** * @brief PowerController attempts to connect to the Power Manager plugin. @@ -117,7 +128,7 @@ void PowerController_Init(); * @return `POWER_CONTROLLER_ERROR_UNAVAILABLE` if Thunder RPC server is not running / error establishing RPC communication channel. * @return `POWER_CONTROLLER_ERROR_NOT_EXIST` if the PowerManager plugin is not actavated yet. */ -uint32_t PowerController_Connect(); +EXTERNAL uint32_t PowerController_Connect(); /** * @brief Terminates the Power Controller. @@ -131,7 +142,7 @@ uint32_t PowerController_Connect(); * * @see PowerController_Init */ -void PowerController_Term(); +EXTERNAL void PowerController_Term(); /** * @brief Checks if the Power Manager plugin is active & operational @@ -149,13 +160,13 @@ void PowerController_Term(); * * @see PowerController_RegisterOperationalStateChangeCallback */ -bool PowerController_IsOperational(); +EXTERNAL bool PowerController_IsOperational(); /** Gets the Power State.*/ // @text getPowerState // @brief Get Power State // @param powerState: Get current power state -uint32_t PowerController_GetPowerState(PowerController_PowerState_t* currentState /* @out */, PowerController_PowerState_t* previousState /* @out */); +EXTERNAL uint32_t PowerController_GetPowerState(PowerController_PowerState_t* currentState /* @out */, PowerController_PowerState_t* previousState /* @out */); /** Sets Power State . */ // @text setPowerState @@ -163,62 +174,62 @@ uint32_t PowerController_GetPowerState(PowerController_PowerState_t* currentStat // @param keyCode: NA for most platfroms, to be depricated // @param powerState: Set power to this state // @param reason: null terminated string stating reason for for state change -uint32_t PowerController_SetPowerState(const int keyCode /* @in */, const PowerController_PowerState_t powerstate /* @in */, const char* reason /* @in */); +EXTERNAL uint32_t PowerController_SetPowerState(const int keyCode /* @in */, const PowerController_PowerState_t powerstate /* @in */, const char* reason /* @in */); /** Gets the current Thermal state.*/ // @text getThermalState // @brief Get Current Thermal State (temperature) // @param currentTemperature: current temperature -uint32_t PowerController_GetThermalState(float* currentTemperature /* @out */); +EXTERNAL uint32_t PowerController_GetThermalState(float* currentTemperature /* @out */); /** Sets the Temperature Thresholds.*/ // @text setTemperatureThresholds // @brief Set Temperature Thresholds // @param high: high threshold // @param critical : critical threshold -uint32_t PowerController_SetTemperatureThresholds(float high /* @in */, float critical /* @in */); +EXTERNAL uint32_t PowerController_SetTemperatureThresholds(float high /* @in */, float critical /* @in */); /** Gets the current Temperature Thresholds.*/ // @text getTemperatureThresholds // @brief Get Temperature Thresholds // @param high: high threshold // @param critical : critical threshold -uint32_t PowerController_GetTemperatureThresholds(float* high /* @out */, float* critical /* @out */); +EXTERNAL uint32_t PowerController_GetTemperatureThresholds(float* high /* @out */, float* critical /* @out */); /** Sets the current Temperature Grace interval.*/ // @property // @text PowerController_SetOvertempGraceInterval // @brief Set Temperature Thresholds // @param graceInterval: interval in secs? -uint32_t PowerController_SetOvertempGraceInterval(const int graceInterval /* @in */); +EXTERNAL uint32_t PowerController_SetOvertempGraceInterval(const int graceInterval /* @in */); /** Gets the grace interval for over-temperature.*/ // @property // @text PowerController_GetOvertempGraceInterval // @brief Get Temperature Grace interval // @param graceInterval: interval in secs? -uint32_t PowerController_GetOvertempGraceInterval(int* graceInterval /* @out */); +EXTERNAL uint32_t PowerController_GetOvertempGraceInterval(int* graceInterval /* @out */); /** Set Deep Sleep Timer for later wakeup */ // @property // @text setDeepSleepTimer // @brief Set Deep sleep timer for timeOut period // @param timeOut: deep sleep timeout -uint32_t PowerController_SetDeepSleepTimer(const int timeOut /* @in */); +EXTERNAL uint32_t PowerController_SetDeepSleepTimer(const int timeOut /* @in */); /** Get Last Wakeup reason */ // @property // @text getLastWakeupReason // @brief Get Last Wake up reason // @param wakeupReason: wake up reason -uint32_t PowerController_GetLastWakeupReason(PowerController_WakeupReason_t* wakeupReason /* @out */); +EXTERNAL uint32_t PowerController_GetLastWakeupReason(PowerController_WakeupReason_t* wakeupReason /* @out */); /** Get Last Wakeup key code */ // @property // @text getLastWakeupKeyCode // @brief Get the key code that can be used for wakeup // @param keycode: Key code for wakeup -uint32_t PowerController_GetLastWakeupKeyCode(int* keycode /* @out */); +EXTERNAL uint32_t PowerController_GetLastWakeupKeyCode(int* keycode /* @out */); /** Request Reboot with PowerManager */ // @text reboot @@ -226,20 +237,20 @@ uint32_t PowerController_GetLastWakeupKeyCode(int* keycode /* @out */); // @param rebootRequestor: null terminated string identifier for the entity requesting the reboot. // @param rebootReasonCustom: custom-defined reason for the reboot, provided as a null terminated string. // @param rebootReasonOther: null terminated string describing any other reasons for the reboot. -uint32_t PowerController_Reboot(const char* rebootRequestor /* @in */, const char* rebootReasonCustom /* @in */, const char* rebootReasonOther /* @in */); +EXTERNAL uint32_t PowerController_Reboot(const char* rebootRequestor /* @in */, const char* rebootReasonCustom /* @in */, const char* rebootReasonOther /* @in */); /** Set Network Standby Mode */ // @property // @text setNetworkStandbyMode // @brief Set the standby mode for Network // @param standbyMode: Network standby mode -uint32_t PowerController_SetNetworkStandbyMode(const bool standbyMode /* @in */); +EXTERNAL uint32_t PowerController_SetNetworkStandbyMode(const bool standbyMode /* @in */); /** Get Network Standby Mode */ // @text getNetworkStandbyMode // @brief Get the standby mode for Network // @param standbyMode: Network standby mode -uint32_t PowerController_GetNetworkStandbyMode(bool* standbyMode /* @out */); +EXTERNAL uint32_t PowerController_GetNetworkStandbyMode(bool* standbyMode /* @out */); /** Set Wakeup source configuration */ // @text setWakeupSrcConfig @@ -247,7 +258,7 @@ uint32_t PowerController_GetNetworkStandbyMode(bool* standbyMode /* @out */); // @param powerMode: power mode // @param wakeSrcType: source type // @param config: config -uint32_t PowerController_SetWakeupSrcConfig(const int powerMode /* @in */, const int wakeSrcType /* @in */, int config /* @in */); +EXTERNAL uint32_t PowerController_SetWakeupSrcConfig(const int powerMode /* @in */, const int wakeSrcType /* @in */, int config /* @in */); /** Get Wakeup source configuration */ // @text getWakeupSrcConfig @@ -255,20 +266,20 @@ uint32_t PowerController_SetWakeupSrcConfig(const int powerMode /* @in */, const // @param powerMode: power mode // @param srcType: source type // @param config: config -uint32_t PowerController_GetWakeupSrcConfig(int* powerMode /* @out */, int* srcType /* @out */, int* config /* @out */); +EXTERNAL uint32_t PowerController_GetWakeupSrcConfig(int* powerMode /* @out */, int* srcType /* @out */, int* config /* @out */); /** Initiate System mode change */ // @text PowerController_SetSystemMode // @brief System mode change // @param oldMode: current mode // @param newMode: new mode -uint32_t PowerController_SetSystemMode(const PowerController_SystemMode_t currentMode /* @in */, const PowerController_SystemMode_t newMode /* @in */); +EXTERNAL uint32_t PowerController_SetSystemMode(const PowerController_SystemMode_t currentMode /* @in */, const PowerController_SystemMode_t newMode /* @in */); /** Get Power State before last reboot */ // @text PowerController_GetPowerStateBeforeReboot // @brief Get Power state before last reboot // @param powerStateBeforeReboot: power state -uint32_t PowerController_GetPowerStateBeforeReboot(PowerController_PowerState_t* powerStateBeforeReboot /* @out */); +EXTERNAL uint32_t PowerController_GetPowerStateBeforeReboot(PowerController_PowerState_t* powerStateBeforeReboot /* @out */); /** Engage a client in power mode change operation. */ // @text PowerController_RemovePowerModePreChangeClient @@ -287,21 +298,21 @@ uint32_t PowerController_GetPowerStateBeforeReboot(PowerController_PowerState_t* // @param clientName: Name of the client as null terminated string // @param clientId: Unique identifier for the client to be used while acknowledging the pre-change operation (`PowerModePreChangeComplete`) // or to delay the power mode change (`DelayPowerModeChangeBy`) -uint32_t PowerController_AddPowerModePreChangeClient(const char *clientName /* @in */, uint32_t* clientId /* @out */); +EXTERNAL uint32_t PowerController_AddPowerModePreChangeClient(const char *clientName /* @in */, uint32_t* clientId /* @out */); /** Disengage a client from the power mode change operation. */ // @text PowerController_RemovePowerModePreChangeClient // @brief Removes a registered client from participating in power mode pre-change operations. // NOTE client will still continue to receive pre-change notifications. // @param clientId: Unique identifier for the client. See `AddPowerModePreChangeClient` -uint32_t PowerController_RemovePowerModePreChangeClient(const uint32_t clientId /* @in */); +EXTERNAL uint32_t PowerController_RemovePowerModePreChangeClient(const uint32_t clientId /* @in */); /** Power prechange activity completed */ // @text PowerController_PowerModePreChangeComplete // @brief Pre power mode handling complete for given client and transation id // @param clientId: Unique identifier for the client, as received in AddPowerModePreChangeClient // @param transactionId: transaction id as received in OnPowerModePreChange -uint32_t PowerController_PowerModePreChangeComplete(const uint32_t clientId /* @in */, const int transactionId /* @in */); +EXTERNAL uint32_t PowerController_PowerModePreChangeComplete(const uint32_t clientId /* @in */, const int transactionId /* @in */); /** Delay Powermode change by given time */ // @text PowerController_DelayPowerModeChangeBy @@ -309,7 +320,7 @@ uint32_t PowerController_PowerModePreChangeComplete(const uint32_t clientId /* @ // @param clientId: Unique identifier for the client, as received in AddPowerModePreChangeClient // @param transactionId: transaction id as received in OnPowerModePreChange // @param delayPeriod: delay in seconds -uint32_t PowerController_DelayPowerModeChangeBy(const uint32_t clientId /* @in */, const int transactionId /* @in */, const int delayPeriod /* @in */); +EXTERNAL uint32_t PowerController_DelayPowerModeChangeBy(const uint32_t clientId /* @in */, const int transactionId /* @in */, const int delayPeriod /* @in */); /* Callback data types for event notifications from power manager plugin */ // @brief Operational state changed event @@ -359,33 +370,33 @@ typedef void (*PowerController_RebootBeginCb)(const char* rebootReasonCustom, co /* userdata in all callbacks are opque, clients can use it to have context to callbacks */ /** Register for PowerManager plugin operational state change event callback, for initial state use `PowerController_IsOperational` call */ -uint32_t PowerController_RegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback, void* userdata); +EXTERNAL uint32_t PowerController_RegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback, void* userdata); /** UnRegister (previously registered) PowerManager plugin operational state change event callback */ -uint32_t PowerController_UnRegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback); +EXTERNAL uint32_t PowerController_UnRegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback); /** Register for PowerMode changed callback */ -uint32_t PowerController_RegisterPowerModeChangedCallback(PowerController_PowerModeChangedCb callback, void* userdata); +EXTERNAL uint32_t PowerController_RegisterPowerModeChangedCallback(PowerController_PowerModeChangedCb callback, void* userdata); /** UnRegister (previously registered) PowerMode changed callback */ -uint32_t PowerController_UnRegisterPowerModeChangedCallback(PowerController_PowerModeChangedCb callback); +EXTERNAL uint32_t PowerController_UnRegisterPowerModeChangedCallback(PowerController_PowerModeChangedCb callback); /** Register for PowerMode pre-change callback */ -uint32_t PowerController_RegisterPowerModePreChangeCallback(PowerController_PowerModePreChangeCb callback, void* userdata); +EXTERNAL uint32_t PowerController_RegisterPowerModePreChangeCallback(PowerController_PowerModePreChangeCb callback, void* userdata); /** UnRegister (previously registered) PowerMode pre-change callback */ -uint32_t PowerController_UnRegisterPowerModePreChangeCallback(PowerController_PowerModePreChangeCb callback); +EXTERNAL uint32_t PowerController_UnRegisterPowerModePreChangeCallback(PowerController_PowerModePreChangeCb callback); /** Register for PowerMode pre-change callback */ -uint32_t PowerController_RegisterDeepSleepTimeoutCallback(PowerController_DeepSleepTimeoutCb callback, void* userdata); +EXTERNAL uint32_t PowerController_RegisterDeepSleepTimeoutCallback(PowerController_DeepSleepTimeoutCb callback, void* userdata); /** UnRegister (previously registered) DeepSleep Timeout callback */ -uint32_t PowerController_UnRegisterDeepSleepTimeoutCallback(PowerController_DeepSleepTimeoutCb callback); +EXTERNAL uint32_t PowerController_UnRegisterDeepSleepTimeoutCallback(PowerController_DeepSleepTimeoutCb callback); /** Register for Network Standby Mode changed event - only on XIone */ -uint32_t PowerController_RegisterNetworkStandbyModeChangedCallback(PowerController_NetworkStandbyModeChangedCb callback, void* userdata); +EXTERNAL uint32_t PowerController_RegisterNetworkStandbyModeChangedCallback(PowerController_NetworkStandbyModeChangedCb callback, void* userdata); /** UnRegister (previously registered) Network Standby Mode changed callback */ -uint32_t PowerController_UnRegisterNetworkStandbyModeChangedCallback(PowerController_NetworkStandbyModeChangedCb callback); +EXTERNAL uint32_t PowerController_UnRegisterNetworkStandbyModeChangedCallback(PowerController_NetworkStandbyModeChangedCb callback); /** Register for Thermal Mode changed event callback */ -uint32_t PowerController_RegisterThermalModeChangedCallback(PowerController_ThermalModeChangedCb callback, void* userdata); +EXTERNAL uint32_t PowerController_RegisterThermalModeChangedCallback(PowerController_ThermalModeChangedCb callback, void* userdata); /** UnRegister (previously registered) Thermal Mode changed event callback */ -uint32_t PowerController_UnRegisterThermalModeChangedCallback(PowerController_ThermalModeChangedCb callback); +EXTERNAL uint32_t PowerController_UnRegisterThermalModeChangedCallback(PowerController_ThermalModeChangedCb callback); /** Register for reboot start event callback */ -uint32_t PowerController_RegisterRebootBeginCallback(PowerController_RebootBeginCb callback, void* userdata); +EXTERNAL uint32_t PowerController_RegisterRebootBeginCallback(PowerController_RebootBeginCb callback, void* userdata); /** UnRegister (previously registered) reboot start event callback */ -uint32_t PowerController_UnRegisterRebootBeginCallback(PowerController_RebootBeginCb callback); +EXTERNAL uint32_t PowerController_UnRegisterRebootBeginCallback(PowerController_RebootBeginCb callback); #ifdef __cplusplus }; // extern "C" From 42645efd79e6e26a9c30cd7b01a1745582dd6c80 Mon Sep 17 00:00:00 2001 From: Shrinivas Kamath Date: Tue, 11 Mar 2025 18:38:49 +0530 Subject: [PATCH 16/16] review rework --- Source/powercontroller/power_controller.cpp | 6 +++--- Source/powercontroller/power_controller.h | 23 ++++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Source/powercontroller/power_controller.cpp b/Source/powercontroller/power_controller.cpp index f1c4f28c..ebadd21b 100644 --- a/Source/powercontroller/power_controller.cpp +++ b/Source/powercontroller/power_controller.cpp @@ -190,7 +190,7 @@ PowerController_ThermalTemperature_t convert(const ThermalTemperature from) static constexpr const TCHAR callSign[] = _T("org.rdk.PowerManager"); -// Templated Clallback list avoid code duplication, for individual callback types +// Templated Callback list avoid code duplication, for individual callback types // This class expects mechanism to register / unregister for individual & unique notifications with PowerManager // via RegisterNotificationLocked and UnregisterNotificationLocked methods. To be implemented in PowerController (i,e PARENT) template @@ -447,7 +447,7 @@ class PowerController : public RPC::SmartInterfaceType , _rebootBeginCallbacks(*this) , _shutdown(false) { - Connect(); + (void)Connect(); } ~PowerController() @@ -544,7 +544,7 @@ class PowerController : public RPC::SmartInterfaceType if (!IsConnected()) { uint32_t res = BaseClass::Open(RPC::CommunicationTimeOut, BaseClass::Connector(), callSign); if (Core::ERROR_NONE != res) { - std::cerr << "/tmp/communicator com channel open failed. Is Thunder running?\n"; + std::cerr << "COM-RPC channel open failed. Is Thunder running?\n"; errMsg = "COM-RPC channel open failed"; status = Core::ERROR_UNAVAILABLE; break; diff --git a/Source/powercontroller/power_controller.h b/Source/powercontroller/power_controller.h index dc1782fa..6bc23382 100644 --- a/Source/powercontroller/power_controller.h +++ b/Source/powercontroller/power_controller.h @@ -108,8 +108,10 @@ typedef enum PowerController_SystemMode { * @details * - If the Power Controller instance does not already exist, it will be created. * - The instance count is incremented each time this function is called. - * - To check if the Power Controller is operational, use `PowerController_IsOperational` - * - If the Power Controller is not operational, clients can use `PowerController_Connect` to establish RPC connection with the Power Manager plugin. + * - After Init, & before making any PowerController request client needs to ensure + * - Power Manager plugin is activated and operational via `PowerController_IsOperational`. + * - If not operational, clients can use this Connect API to establish COM-RPC connection with the Power Manager plugin. + * - If there us any failure in Connect all PowerController requests will fail with `POWER_CONTROLLER_ERROR_UNAVAILABLE` (Except for callback register / unregister APIs). * * @see PowerController_Term */ @@ -122,11 +124,15 @@ EXTERNAL void PowerController_Init(); * * @details * - This function is used to connect to the Power Manager plugin. - * - This is the fist function that should be called after `PowerController_Init`. + * - Before making any PowerController request client needs to ensure + * - Power Manager plugin is activated and operational via `PowerController_IsOperational`. + * - If not operational, clients can use this Connect API to establish COM-RPC connection with the Power Manager plugin. + * - If there us any failure in Connect all PowerController requests will fail with `POWER_CONTROLLER_ERROR_UNAVAILABLE` (Except for callback register / unregister APIs). + * - In case of failure this API should be called again with brief delay. * * @return `POWER_CONTROLLER_ERROR_NONE` on success. * @return `POWER_CONTROLLER_ERROR_UNAVAILABLE` if Thunder RPC server is not running / error establishing RPC communication channel. - * @return `POWER_CONTROLLER_ERROR_NOT_EXIST` if the PowerManager plugin is not actavated yet. + * @return `POWER_CONTROLLER_ERROR_NOT_EXIST` if the PowerManager plugin is not activated yet. */ EXTERNAL uint32_t PowerController_Connect(); @@ -150,6 +156,8 @@ EXTERNAL void PowerController_Term(); * This function determines whether the Power Manager interface is operational and ready to handle requests. * It can be used to verify the availability of the Power Manager client before initiating operations that depend on it. * + * IMPORTANT - This is the first function that should be called after `PowerController_Init`. + * * @return `true` if the Power Manager interface is active and operational, otherwise `false`. * * @details @@ -157,6 +165,7 @@ EXTERNAL void PowerController_Term(); * - Calling this function is NOT MANDATORY but optional * - Clients can register for notifications about state changes using `PowerController_RegisterOperationalStateChangeCallback`. * - If the Power Manager interface is not active, subsequent Power Manager operations will fail with the error `POWER_CONTROLLER_ERROR_UNAVAILABLE`. + * - Therefore in failure cases, clients can use `PowerController_Connect` to establish COM-RPC connection with the Power Manager plugin. * * @see PowerController_RegisterOperationalStateChangeCallback */ @@ -282,9 +291,9 @@ EXTERNAL uint32_t PowerController_SetSystemMode(const PowerController_SystemMode EXTERNAL uint32_t PowerController_GetPowerStateBeforeReboot(PowerController_PowerState_t* powerStateBeforeReboot /* @out */); /** Engage a client in power mode change operation. */ -// @text PowerController_RemovePowerModePreChangeClient +// @text PowerController_AddPowerModePreChangeClient // @brief - Register a client to engage in power mode state changes. -// - Added client should call either +// - When `PowerModePreChange` event is received, then added client should call either // - `PowerModePreChangeComplete` API to inform power manager that this client has completed its pre-change operation. // - Or `DelayPowerModeChangeBy` API to delay the power mode change. // - If the client does not call `PowerModePreChangeComplete` API, the power mode change will complete @@ -367,7 +376,7 @@ typedef void (*PowerController_ThermalModeChangedCb)(const PowerController_Therm typedef void (*PowerController_RebootBeginCb)(const char* rebootReasonCustom, const char* rebootReasonOther, const char* rebootRequestor, void* userdata); /* Type defines for callbacks / notifications */ -/* userdata in all callbacks are opque, clients can use it to have context to callbacks */ +/* userdata in all callbacks are opaque, clients can use it to have context to callbacks */ /** Register for PowerManager plugin operational state change event callback, for initial state use `PowerController_IsOperational` call */ EXTERNAL uint32_t PowerController_RegisterOperationalStateChangeCallback(PowerController_OperationalStateChangeCb callback, void* userdata);