--- /dev/null
+//
+// 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 <unique_ptr.h>
+#include <FBase.h>
+#include <FSystem.h>
+#include <FIo_IIpcServerEventListener.h>
+#include <FAppIActiveAppEventListener.h>
+#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<Tizen::Io::_IpcServer> __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_
--- /dev/null
+//
+// 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 <dlfcn.h>
+#include <FBaseSysLog.h>
+#include <FIo_IpcServer.h>
+#include <FIo_AppServiceIpcMessages.h>
+#include <FSystem.h>
+
+#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<Integer> pBrightness(new (std::nothrow) Integer(brightness));
+ unique_ptr<String> 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<String> 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<ArrayList*>(&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());
+ }
+}