From: Hokwon Song Date: Tue, 25 Jun 2013 07:23:42 +0000 (+0900) Subject: Add power manager stup. X-Git-Tag: submit/tizen_2.2/20130714.152833~4^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bff3b3155215779edf7634f752c697a2f54f4f89;p=framework%2Fosp%2Fcommon-service.git Add power manager stup. Change-Id: Ie52ef5b50e2e9e8be46a8bbf76c84ba1623f95a3 Signed-off-by: Hokwon Song --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 1843966..3b17bd7 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ SET (${this_target}_SOURCE_FILES src/CommonServiceEntry.cpp src/FSys_DeviceManagerStub.cpp src/FSys_DeviceManagerServiceIpcEventForAsync.cpp + src/FSys_PowerManagerStub.cpp ) ## SET EXTRA COMPILER FLAGS diff --git a/inc/CommonService.h b/inc/CommonService.h index 2a803da..72af473 100644 --- a/inc/CommonService.h +++ b/inc/CommonService.h @@ -36,6 +36,7 @@ class _UiManagerStub; namespace Tizen { namespace System { class _DeviceManagerStub; +class _PowerManagerStub; }} /** * @class CommonService @@ -63,6 +64,7 @@ private: Tizen::App::_PackageManagerStub* __pPackageManagerStub; std::unique_ptr __pUiManagerStub; Tizen::System::_DeviceManagerStub* __pDeviceManagerStub; + Tizen::System::_PowerManagerStub* __pPowerManagerStub; }; #endif // _COMMON_SERVICE_H_ diff --git a/inc/FSys_IPowerManager.h b/inc/FSys_IPowerManager.h new file mode 100644 index 0000000..4a2fc05 --- /dev/null +++ b/inc/FSys_IPowerManager.h @@ -0,0 +1,45 @@ +// +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// 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. +// + +/** + * @file FSys_IPowerManager.h + * @brief This is the header file of the _IPowerManager class. + * + * This header file contains the declarations of the _IPowerManager class. + */ +#include +#include + +#ifndef _FSYS_INTERNAL_IPOWER_MANAGER_H_ +#define _FSYS_INTERNAL_IPOWER_MANAGER_H_ + +namespace Tizen { namespace System { + +/** + * @class _IPowerManager + * @brief This class contains implementaion of device control. + * @since 2.1 + */ +class _OSP_EXPORT_ _IPowerManager +{ +protected: + virtual ~_IPowerManager(void) {}; +public: + virtual result ChangeBrightness(int brightness) = 0; +}; //_IPowerManager + +}} //Tizen::System +#endif /* _FSYS_INTERNAL_IPOWER_MANAGER_H_ */ diff --git a/inc/FSys_PowerManagerStub.h b/inc/FSys_PowerManagerStub.h new file mode 100644 index 0000000..b8288ef --- /dev/null +++ b/inc/FSys_PowerManagerStub.h @@ -0,0 +1,84 @@ +// +// Open Service Platform +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// 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. +// + +/** + * @file FSys_PowerManagerStub.h + * @brief This is the header file of the _PowerManagerStub class. + * + * This header file contains the declarations of the _PowerManagerStub class. + */ + +#ifndef _FSYS_POWER_MANAGER_STUB_H_ +#define _FSYS_POWER_MANAGER_STUB_H_ + +#include +#include +#include +#include +#include +#include "FApp_IAppEventListener.h" + +namespace Tizen { namespace System +{ + +class _IPowerManager; + +class _OSP_EXPORT_ _PowerManagerStub + : public Tizen::Base::Object + , public Tizen::Io::_IIpcServerEventListener + , public Tizen::App::_IAppEventListener + , public Tizen::App::IActiveAppEventListener +{ +public: + static _PowerManagerStub* GetInstance(void); + + virtual ~_PowerManagerStub(void); + virtual void OnActiveAppChanged(const Tizen::App::AppId& appId); +private: + _PowerManagerStub(void); + result Construct(void); + + result ChangeBrightness(void); + + _PowerManagerStub(const _PowerManagerStub& rhs); + _PowerManagerStub& operator =(const _PowerManagerStub& rhs); + + bool OnRequestOccured(const Tizen::Base::Collection::ArrayList& request, Tizen::Base::Collection::ArrayList* response); + + virtual void OnIpcServerStarted(const Tizen::Io::_IpcServer& server) {} + virtual void OnIpcServerStopped(const Tizen::Io::_IpcServer& server) {} + virtual void OnIpcClientConnected(const Tizen::Io::_IpcServer& server, int clientId) {} + virtual void OnIpcClientDisconnected(const Tizen::Io::_IpcServer&server, int clientId) {} + virtual void OnIpcRequestReceived(Tizen::Io::_IpcServer& server, const IPC::Message& message); + + virtual void OnApplicationLaunched(const Tizen::App::AppId& appId, int pid) {} + virtual void OnApplicationTerminated(const Tizen::App::AppId& appId, int pid); + +private: + std::unique_ptr __pIpcServer; + static _PowerManagerStub* __pPowerManagerStub; + Tizen::Base::Runtime::Mutex __Mutex; + Tizen::App::AppId __activeAppId; + Tizen::App::AppId __currentAppId; + Tizen::Base::Collection::HashMap __appBrightnessList; + void* __pSystemServiceDllHandle; + _IPowerManager* __pIPowerManager; +}; // _PowerManagerStub + +}} // Tizen::System + +#endif // _FSYS_POWER_MANAGER_STUB_H_ diff --git a/src/CommonService.cpp b/src/CommonService.cpp index 92c8f0d..045f37d 100644 --- a/src/CommonService.cpp +++ b/src/CommonService.cpp @@ -18,9 +18,10 @@ #include #include "CommonService.h" -#include "FSys_DeviceManagerStub.h" #include "FApp_PackageManagerStub.h" #include "FUi_UiManagerStub.h" +#include "FSys_PowerManagerStub.h" +#include "FSys_DeviceManagerStub.h" using namespace std; using namespace Tizen::App; @@ -31,6 +32,7 @@ using namespace Tizen::Ui; CommonService::CommonService(void) : __pPackageManagerStub(null) , __pDeviceManagerStub(null) + , __pPowerManagerStub(null) { } @@ -38,7 +40,15 @@ CommonService::CommonService(void) CommonService::~CommonService(void) { delete __pPackageManagerStub; - delete __pDeviceManagerStub; + + if (__pDeviceManagerStub != null) + { + delete __pDeviceManagerStub; + } + if (__pPowerManagerStub != null) + { + delete __pPowerManagerStub; + } } ServiceApp* @@ -125,5 +135,8 @@ CommonService::InitializeServices(void) __pUiManagerStub = move(pUiManagerStub); __pDeviceManagerStub = _DeviceManagerStub::CreateInstanceN(); - SysTryReturnVoidResult(NID_UI, __pDeviceManagerStub, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryReturnVoidResult(NID_SYS, __pDeviceManagerStub, r, "[%s] Propagating.", GetErrorMessage(r)); + + __pPowerManagerStub = _PowerManagerStub::GetInstance(); + SysTryReturnVoidResult(NID_SYS, __pPowerManagerStub, r, "[%s] Propagating.", GetErrorMessage(r)); } diff --git a/src/FSys_DeviceManagerStub.cpp b/src/FSys_DeviceManagerStub.cpp index 7cd0e9d..87691c7 100644 --- a/src/FSys_DeviceManagerStub.cpp +++ b/src/FSys_DeviceManagerStub.cpp @@ -51,8 +51,8 @@ _DeviceManagerServiceIpcEventForAsync* _DeviceManagerStub::__pDeviceManagerServi _DeviceManagerStub* _DeviceManagerStub::__pDeviceManagerStub = null; _DeviceManagerStub::_DeviceManagerStub(void) - : __pSystemServiceDllHandle(null) - , __pIpcServer(null) + : __pIpcServer(null) + , __pSystemServiceDllHandle(null) , __pIDeviceManager(null) { result r = E_SUCCESS; diff --git a/src/FSys_PowerManagerStub.cpp b/src/FSys_PowerManagerStub.cpp new file mode 100644 index 0000000..75e312a --- /dev/null +++ b/src/FSys_PowerManagerStub.cpp @@ -0,0 +1,256 @@ +// +// Open Service Platform +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// 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. +// + +/** + * @file FSys_PowerManagerStub.cpp + * @brief This is the implementation for the _PowerManagerStub class. + */ +#include +#include +#include +#include +#include + +#include "FApp_AppManagerImpl.h" +#include "FSys_IPowerManager.h" +#include "FSys_PowerManagerStub.h" + +using namespace std; +using namespace Tizen::Base; +using namespace Tizen::Base::Collection; +using namespace Tizen::Io; +using namespace Tizen::System; +using namespace Tizen::App; + +namespace { + static const wchar_t* POWER_MANAGER_SERVICE_ID = L"osp.sys.ipcserver.powermanager"; + static const wchar_t* POWER_MANAGER_SERVICE_MUTEX_ID = L"osp.sys.ipcserver.devicemanager"; + static const wchar_t* _POWER_BRIGHTNESS_CHANGE = L"osp.system.command.power.change.brightness"; + static const wchar_t* _POWER_BRIGHTNESS_RESTORE = L"osp.system.command.power.restore.brightness"; + static const int _POWER_COMMAND_ID = 1; + static const wchar_t* _POWER_OK = L"osp.system.result.ok"; + static const wchar_t* _POWER_ERROR = L"osp.system.result.error"; +} + +_PowerManagerStub* _PowerManagerStub::__pPowerManagerStub = null; + +_PowerManagerStub::_PowerManagerStub(void) + : __pIpcServer(null) + , __pSystemServiceDllHandle(null) + , __pIPowerManager(null) +{ + result r = E_SUCCESS; + r = __Mutex.Create(POWER_MANAGER_SERVICE_MUTEX_ID); + SysTryReturn(NID_SYS, r == E_SUCCESS, , r, "It is failed to create mutex. [%s] Propaged.", r); + + r = __appBrightnessList.Construct(); + SysTryReturn(NID_SYS, r == E_SUCCESS, , r, "It is failed to initiate power manager app list."); +} + +_PowerManagerStub::~_PowerManagerStub(void) +{ + if (__pIpcServer) + { + __pIpcServer->Stop(); + } + + if (__pSystemServiceDllHandle) + { + dlclose(__pSystemServiceDllHandle); + } + + __appBrightnessList.RemoveAll(); + + result r = __Mutex.Release(); + SysTryReturn(NID_SYS, r == E_SUCCESS, , r, "It is failed to release mutex. [%s] Propaged.", r); +} + +_PowerManagerStub* +_PowerManagerStub::GetInstance() +{ + if (__pPowerManagerStub == null) + { + __pPowerManagerStub = new (std::nothrow) _PowerManagerStub(); + result r = __pPowerManagerStub->Construct(); + SysTryReturn(NID_SYS, !IsFailed(r), null, r, "[%s] It is failed to get PowerManagerCommunicationStub.", GetErrorMessage(r)); + } + return __pPowerManagerStub; +} + +result +_PowerManagerStub::Construct(void) +{ + unique_ptr<_IpcServer> pIpcServer(new (std::nothrow) _IpcServer()); + SysTryReturnResult(NID_SYS, pIpcServer, E_OUT_OF_MEMORY, "Memory is insufficient."); + + result r = pIpcServer->Construct(String(POWER_MANAGER_SERVICE_ID), *this, true); + SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to construct."); + + r = pIpcServer->Start(); + SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to start."); + + __pIpcServer = move(pIpcServer); + + __pSystemServiceDllHandle = dlopen("libosp-system-server.so", RTLD_LAZY); + + if (!__pSystemServiceDllHandle) + { + SysLog(NID_SYS, "It is failed to load system service (%s).", dlerror()); + return E_SYSTEM; + } + + _IPowerManager* (*pFnPowerManager_CreateInstance)(void) = null; + pFnPowerManager_CreateInstance = reinterpret_cast< _IPowerManager* (*)(void) >(dlsym(__pSystemServiceDllHandle, "PowerManager_CreateInstance")); + SysTryReturn(NID_SYS, pFnPowerManager_CreateInstance, E_SYSTEM, E_SYSTEM, "It is failed to find PowerManager_CreateInstance."); + + __pIPowerManager = pFnPowerManager_CreateInstance(); + SysTryReturn(NID_SYS, __pIPowerManager, E_SYSTEM, E_SYSTEM,"It is failed to create PowerManager instance."); + + _AppManagerImpl* pAppManagerImpl = null; + pAppManagerImpl = _AppManagerImpl::GetInstance(); + SysTryReturn(NID_SYS, pAppManagerImpl != null, E_SYSTEM , E_SYSTEM, "It is failed to get _AppManagerImpl class."); + + r = pAppManagerImpl->AddActiveAppEventListener(*this); + SysTryReturn(NID_SYS, r == E_SUCCESS || r == E_OBJ_ALREADY_EXIST, E_SYSTEM, E_SYSTEM, "[%s] It is failed to add active app event listener",GetErrorMessage(r)); + + r = pAppManagerImpl->AddAppEventListener(*this); + SysTryReturn(NID_SYS, r == E_SUCCESS || r == E_OBJ_ALREADY_EXIST, E_SYSTEM, E_SYSTEM, "It is failed to add app event listener"); + + return E_SUCCESS; +} + +result +_PowerManagerStub::ChangeBrightness() +{ + result r = E_FAILURE; + + if(__appBrightnessList.GetCount() > 0) + { + Integer* brightness = (Integer*)__appBrightnessList.GetValue(__activeAppId); + + if(brightness != null) + { + r = __pIPowerManager->ChangeBrightness(brightness->ToInt()); + } + } + if (IsFailed(r)) + { + r = __pIPowerManager->ChangeBrightness(0); + } + + return r; +} +void +_PowerManagerStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message) +{ + __Mutex.Acquire(); + __currentAppId = server.GetClientPackageId(); + IPC_BEGIN_MESSAGE_MAP(_PowerManagerStub, message) + IPC_MESSAGE_HANDLER_EX(IoService_Request, &server, OnRequestOccured) + IPC_END_MESSAGE_MAP_EX() + __Mutex.Release(); +} + +bool +_PowerManagerStub::OnRequestOccured(const ArrayList& request, ArrayList* response) +{ + bool isSuccess = false; + result r = E_SUCCESS; + + std::unique_ptr< String > pServiceId(new (std::nothrow) String(POWER_MANAGER_SERVICE_ID)); + std::unique_ptr< String > pCommand(new (std::nothrow) String(*((String*)request.GetAt(_POWER_COMMAND_ID)))); + SysTryCatch(NID_SYS, pCommand != null, r = E_SYSTEM, r, "pCommand is empty."); + + if(*pCommand == String(_POWER_BRIGHTNESS_CHANGE)) + { + SysTryCatch(NID_SYS, __activeAppId == __currentAppId, r = E_SUCCESS, r, + "It is not active application[%ls]. Current Active App is [%ls].", __currentAppId.GetPointer(), __activeAppId.GetPointer()); + int brightness = 0; + String* brightnessBuffer = (String*)request.GetAt(_POWER_COMMAND_ID + 1); + Integer::Parse(*brightnessBuffer, brightness); + + __appBrightnessList.Remove(__currentAppId, true); + + unique_ptr pBrightness(new (std::nothrow) Integer(brightness)); + unique_ptr pAppId(new (std::nothrow) String(__currentAppId)); + + r = __appBrightnessList.Add(*pAppId, *pBrightness); + SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, + "It is failed to add requested App Id(%ls) and Brightness(%d) on the managed app list.", pAppId->GetPointer(), pBrightness->ToInt()); + pAppId.release(); + pBrightness.release(); + r = ChangeBrightness(); + } + else if(*pCommand == _POWER_BRIGHTNESS_RESTORE) + { + __appBrightnessList.Remove(__currentAppId, true); + r = ChangeBrightness(); + } + else + { + SysLogException(NID_SYS, E_SYSTEM, "Required pCommand[%ls] is not proccess on _PowerManager.", pCommand->GetPointer()); + r = E_SYSTEM; + } + + isSuccess = true; +CATCH: + unique_ptr pResultId(new (std::nothrow) String()); + SysTryReturn(NID_SYS, pResultId != null,false, E_SYSTEM, "It is failed to allocate memory for response message creating."); + + if(r == E_SUCCESS) + { + pResultId->Append(_POWER_OK); + } + else + { + pResultId->Append(_POWER_ERROR); + } + + response->Add(*pServiceId.release()); + response->Add(*pCommand.release()); + response->Add(*pResultId.release()); + + ArrayList* temp = const_cast(&request); + temp->RemoveAll(true); + return isSuccess; +} + +void +_PowerManagerStub::OnApplicationTerminated(const AppId& appId, int pid) +{ + String requiredAppId; + appId.SubString(0, 10, requiredAppId); + __appBrightnessList.Remove(requiredAppId, true); + ChangeBrightness(); +} + +void +_PowerManagerStub::OnActiveAppChanged(const AppId& appId) +{ + AppId smallAppId; + appId.SubString(0, 10, smallAppId); + + if(__activeAppId != smallAppId) + { + __activeAppId = smallAppId; + ChangeBrightness(); + } + else + { + SysLog(NID_SYS, "Current App[%ls] is already actived.", __activeAppId.GetPointer()); + } +}