Update PowerManager [dep:osp-common-service, osp-appfw, osp-appservice]
authorHokwon Song <hokwon.song@samsung.com>
Thu, 25 Jul 2013 03:20:06 +0000 (12:20 +0900)
committerHokwon Song <hokwon.song@samsung.com>
Thu, 25 Jul 2013 03:21:17 +0000 (12:21 +0900)
Change-Id: Ibbcf47098d2526e5f5e98bd9c4b3c4a703bd705c
Signed-off-by: Hokwon Song <hokwon.song@samsung.com>
inc/CommonService.h
inc/FSys_PowerManagerStub.h [new file with mode: 0644]
src/CommonService.cpp
src/system/CMakeLists.txt
src/system/FSys_PowerManagerStub.cpp [new file with mode: 0644]

index 3933815..e3f2f6d 100644 (file)
@@ -38,6 +38,7 @@ namespace Tizen { namespace System
 class _SettingService;
 class _RuntimeInfoStub;
 class _DeviceManagerStub;
+class _PowerManagerStub;
 }}
 
 /**
@@ -68,6 +69,7 @@ private:
        Tizen::System::_SettingService* __pSettingService;
        Tizen::System::_RuntimeInfoStub* __pRuntimeInfoStub;
        Tizen::System::_DeviceManagerStub* __pDeviceManagerStub;
+       Tizen::System::_PowerManagerStub* __pPowerManagerStub;
 };
 
 #endif // _COMMON_SERVICE_H_
diff --git a/inc/FSys_PowerManagerStub.h b/inc/FSys_PowerManagerStub.h
new file mode 100644 (file)
index 0000000..d515854
--- /dev/null
@@ -0,0 +1,85 @@
+//
+// 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 _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:
+       static void InitSingleton(void);
+       static void DestroySingleton(void);
+
+       _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;
+       Tizen::Base::String     __serviceId;
+       Tizen::Base::String     __command;
+       Tizen::Base::String     __result;
+}; // _PowerManagerStub
+
+}} // Tizen::System
+
+#endif // _FSYS_POWER_MANAGER_STUB_H_
index c4aa585..c9f0062 100644 (file)
@@ -24,6 +24,7 @@
 #include "system/FSys_SettingService.h"
 #include "FSys_RuntimeInfoStub.h"
 #include "FSys_DeviceManagerStub.h"
+#include "FSys_PowerManagerStub.h"
 
 using namespace std;
 using namespace Tizen::App;
@@ -36,6 +37,7 @@ CommonService::CommonService(void)
        , __pSettingService(null)
        , __pRuntimeInfoStub(null)
        , __pDeviceManagerStub(null)
+       , __pPowerManagerStub(null)
 {      
 }
 
@@ -109,5 +111,6 @@ CommonService::InitializeServices(void)
        {
                __pDeviceManagerStub = _DeviceManagerStub::GetInstance();
        }
+       __pPowerManagerStub = _PowerManagerStub::GetInstance();
 }
 
index eb416f4..5d0ae74 100644 (file)
@@ -11,6 +11,7 @@ SET (${this_target}_SOURCE_FILES
        FSys_SettingService.cpp
        FSys_RuntimeInfoStub.cpp
        FSys_DeviceManagerStub.cpp
+       FSys_PowerManagerStub.cpp
        FSys_SystemServiceIpcEventForAsync.cpp
 )
 
diff --git a/src/system/FSys_PowerManagerStub.cpp b/src/system/FSys_PowerManagerStub.cpp
new file mode 100644 (file)
index 0000000..dcc162a
--- /dev/null
@@ -0,0 +1,242 @@
+//
+// 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 <FBaseSysLog.h>
+#include <FIo_IpcServer.h>
+#include <FIo_AppServiceIpcMessages.h>
+#include <FSystem.h>
+
+#include "FApp_AppManagerImpl.h"
+#include "FSys_PowerManagerStub.h"
+#include "FSys_PowerManager.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)
+       , __serviceId(POWER_MANAGER_SERVICE_ID)
+{
+       result 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();
+       }
+
+       __appBrightnessList.RemoveAll();
+}
+
+_PowerManagerStub*
+_PowerManagerStub::GetInstance()
+{
+       static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
+
+       if (__pPowerManagerStub == null)
+       {
+               pthread_once(&onceBlock, InitSingleton);
+       }
+       return __pPowerManagerStub;
+}
+
+void
+_PowerManagerStub::InitSingleton(void)
+{
+       std::unique_ptr< _PowerManagerStub > pPowerManagerStub(new (std::nothrow) _PowerManagerStub());
+       SysTryReturn(NID_SYS, pPowerManagerStub, ,E_OUT_OF_MEMORY, "It is not enough memory.");
+       result r = pPowerManagerStub->Construct();
+       SysTryReturn(NID_SYS, !IsFailed(r), , r, "[%s] It is failed to get PowerManagerCommunicationStub.", GetErrorMessage(r));
+       __pPowerManagerStub = pPowerManagerStub.release();
+       atexit(DestroySingleton);
+}
+
+
+void
+_PowerManagerStub::DestroySingleton(void)
+{
+       delete __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.");
+
+       _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");
+
+       __pIpcServer = move(pIpcServer);
+
+       return E_SUCCESS;
+}
+
+result
+_PowerManagerStub::ChangeBrightness()
+{
+       result r = E_FAILURE;
+
+        if(__appBrightnessList.GetCount() > 0)
+        {
+                Integer* brightness = (Integer*)__appBrightnessList.GetValue(__activeAppId);
+
+                if(brightness != null)
+                {
+                        r = _PowerManager::ChangeBrightness(brightness->ToInt());
+                       SysLog(NID_SYS, "change brightness %d", brightness->ToInt());
+                }
+        }
+       if (IsFailed(r))
+       {
+               r = _PowerManager::ChangeBrightness(0);
+               SysLog(NID_SYS, "change brightness system value.");
+       }
+
+       return r;
+}
+void
+_PowerManagerStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message)
+{
+       __currentAppId = server.GetClientPackageId();
+       IPC_BEGIN_MESSAGE_MAP(_PowerManagerStub, message)
+               IPC_MESSAGE_HANDLER_EX(IoService_Request, &server, OnRequestOccured)
+       IPC_END_MESSAGE_MAP_EX()
+}
+
+bool
+_PowerManagerStub::OnRequestOccured(const ArrayList& request, ArrayList* response)
+{
+       bool isSuccess = false;
+       result r = E_SUCCESS;
+
+       __command = *((String*)request.GetAt(_POWER_COMMAND_ID));
+
+       if (__command == _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(__command == _POWER_BRIGHTNESS_RESTORE)
+       {
+               SysLog(NID_SYS, "Request bright restore");
+               __appBrightnessList.Remove(__currentAppId, true);
+               r = ChangeBrightness();
+       }
+       else
+       {
+               SysLogException(NID_SYS, E_SYSTEM, "Required Command[%ls] is not proccess on _PowerManager.", __command.GetPointer());
+               r = E_SYSTEM;
+       }
+
+       isSuccess = true;
+CATCH:
+       if(r == E_SUCCESS)
+       {
+               __result = _POWER_OK;
+       }
+       else
+       {
+               __result = _POWER_ERROR;
+       }
+
+       response->Add(__serviceId);
+       response->Add(__command);
+       response->Add(__result);
+
+       ArrayList* temp = const_cast<ArrayList*>(&request);
+       temp->RemoveAll(true);
+       return isSuccess;
+}
+
+void
+_PowerManagerStub::OnApplicationTerminated(const AppId& appId, int pid)
+{
+       SysLog(NID_SYS, "Terminated app [%ls]", appId.GetPointer());
+       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);
+       SysLog(NID_SYS, "Active app [%ls]", appId.GetPointer());
+       if(__activeAppId != smallAppId)
+       {
+               __activeAppId = smallAppId;
+               ChangeBrightness();
+       }
+       else
+       {
+               SysLog(NID_SYS, "Current App[%ls] is already actived.", __activeAppId.GetPointer());
+       }
+}