2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FSys_PowerManagerStub.cpp
20 * @brief This is the implementation for the _PowerManagerStub class.
22 #include <FBaseSysLog.h>
23 #include <FIo_IpcServer.h>
24 #include <FIo_AppServiceIpcMessages.h>
25 #include <FIoRegistry.h>
26 #include "FApp_AppManagerImpl.h"
27 #include "FSys_PowerManagerStub.h"
28 #include "FSys_PowerManager.h"
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Io;
34 using namespace Tizen::System;
35 using namespace Tizen::App;
38 static const wchar_t* POWER_MANAGER_SERVICE_ID = L"osp.sys.ipcserver.powermanager";
39 static const wchar_t* _POWER_BRIGHTNESS_CHANGE = L"osp.system.command.power.change.brightness";
40 static const wchar_t* _POWER_BRIGHTNESS_RESTORE = L"osp.system.command.power.restore.brightness";
41 static const int _POWER_COMMAND_ID = 1;
42 static const wchar_t* _POWER_OK = L"osp.system.result.ok";
43 static const wchar_t* _POWER_ERROR = L"osp.system.result.error";
44 static const wchar_t* _POWER_MANAGER_REGISTRY_NAME = L"/tmp/sys.powermanager.ini";
45 static const wchar_t* _POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION = L"Brightness";
46 static const wchar_t* _POWER_MANAGER_REGISTRY_ACTIVE_ID = L"ActiveId";
49 _PowerManagerStub* _PowerManagerStub::__pPowerManagerStub = null;
51 _PowerManagerStub::_PowerManagerStub(void)
53 , __serviceId(POWER_MANAGER_SERVICE_ID)
58 _PowerManagerStub::~_PowerManagerStub(void)
73 _PowerManagerStub::GetInstance()
75 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
77 if (__pPowerManagerStub == null)
79 pthread_once(&onceBlock, InitSingleton);
81 return __pPowerManagerStub;
85 _PowerManagerStub::InitSingleton(void)
87 std::unique_ptr< _PowerManagerStub > pPowerManagerStub(new (std::nothrow) _PowerManagerStub());
88 SysTryReturn(NID_SYS, pPowerManagerStub, ,E_OUT_OF_MEMORY, "It is not enough memory.");
89 result r = pPowerManagerStub->Construct();
90 SysTryReturn(NID_SYS, !IsFailed(r), , r, "[%s] It is failed to get PowerManagerCommunicationStub.", GetErrorMessage(r));
91 __pPowerManagerStub = pPowerManagerStub.release();
92 atexit(DestroySingleton);
97 _PowerManagerStub::DestroySingleton(void)
99 delete __pPowerManagerStub;
103 _PowerManagerStub::Construct(void)
105 unique_ptr<_IpcServer> pIpcServer(new (std::nothrow) _IpcServer());
106 SysTryReturnResult(NID_SYS, pIpcServer, E_OUT_OF_MEMORY, "Memory is insufficient.");
108 result r = pIpcServer->Construct(String(POWER_MANAGER_SERVICE_ID), *this, true);
109 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to construct.");
111 r = pIpcServer->Start();
112 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to start.");
114 _AppManagerImpl* pAppManagerImpl = null;
115 pAppManagerImpl = _AppManagerImpl::GetInstance();
116 SysTryReturn(NID_SYS, pAppManagerImpl != null, E_SYSTEM , E_SYSTEM, "It is failed to get _AppManagerImpl class.");
118 r = pAppManagerImpl->AddActiveAppEventListener(*this);
119 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));
121 r = pAppManagerImpl->AddAppEventListener(*this);
122 SysTryReturn(NID_SYS, r == E_SUCCESS || r == E_OBJ_ALREADY_EXIST, E_SYSTEM, E_SYSTEM, "It is failed to add app event listener");
124 __pRegistry = new (std::nothrow) Registry();
125 SysTryReturn(NID_SYS, __pRegistry, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,"Memory is insufficient.");
127 r = __pRegistry->Construct(_POWER_MANAGER_REGISTRY_NAME, "a+");
128 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "It is failed to open power manager registry.[%s]", GetErrorMessage(r));
130 r = __pRegistry->AddSection(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION);
131 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "It is failed to add power manager registry brightness section.[%s]", GetErrorMessage(r));
133 __pIpcServer = move(pIpcServer);
139 _PowerManagerStub::ChangeBrightness()
141 result r = E_FAILURE;
144 r = __pRegistry->GetValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, (String)__activeAppId, brightness);
147 r = _PowerManager::ChangeBrightness(brightness);
148 SysLog(NID_SYS, "change brightness %d", brightness);
153 r = _PowerManager::ChangeBrightness(0);
154 SysLog(NID_SYS, "change brightness system value.");
160 _PowerManagerStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message)
162 __currentAppId = server.GetClientPackageId();
163 IPC_BEGIN_MESSAGE_MAP(_PowerManagerStub, message)
164 IPC_MESSAGE_HANDLER_EX(IoService_Request, &server, OnRequestOccured)
165 IPC_END_MESSAGE_MAP_EX()
169 _PowerManagerStub::OnRequestOccured(const ArrayList& request, ArrayList* response)
171 bool isSuccess = false;
172 result r = E_SUCCESS;
174 __command = *((String*)request.GetAt(_POWER_COMMAND_ID));
176 if (__activeAppId.IsEmpty())
178 r = __pRegistry->GetValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, _POWER_MANAGER_REGISTRY_ACTIVE_ID, __activeAppId);
181 SysLog(NID_SYS, "Get the active id from registry [%ls].", __activeAppId.GetPointer());
185 SysLog(NID_SYS, "It is failed to get the active id from registry");
189 if (__command == _POWER_BRIGHTNESS_CHANGE)
191 SysTryCatch(NID_SYS, __activeAppId == __currentAppId, r = E_SUCCESS, r,
192 "It is not active application[%ls]. Current Active App is [%ls].", __currentAppId.GetPointer(), __activeAppId.GetPointer());
194 String* brightnessBuffer = (String*)request.GetAt(_POWER_COMMAND_ID + 1);
195 Integer::Parse(*brightnessBuffer, brightness);
197 __pRegistry->RemoveValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, (String)__currentAppId);
199 r = __pRegistry->AddValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, __currentAppId, brightness);
200 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r,
201 "It is failed to add requested App Id(%ls) and Brightness(%d) on the managed app list.", __currentAppId.GetPointer(), brightness);
202 r = ChangeBrightness();
204 else if(__command == _POWER_BRIGHTNESS_RESTORE)
206 SysLog(NID_SYS, "Request bright restore");
207 __pRegistry->RemoveValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, (String)__currentAppId);
208 r = ChangeBrightness();
212 SysLogException(NID_SYS, E_SYSTEM, "Required Command[%ls] is not proccess on _PowerManager.", __command.GetPointer());
220 __result = _POWER_OK;
224 __result = _POWER_ERROR;
227 response->Add(__serviceId);
228 response->Add(__command);
229 response->Add(__result);
231 ArrayList* temp = const_cast<ArrayList*>(&request);
232 temp->RemoveAll(true);
234 __pRegistry->Flush();
239 _PowerManagerStub::OnApplicationTerminated(const AppId& appId, int pid)
241 SysLog(NID_SYS, "Terminated app [%ls]", appId.GetPointer());
242 String requiredAppId;
243 appId.SubString(0, 10, requiredAppId);
244 __pRegistry->RemoveValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, (String)requiredAppId);
245 __pRegistry->Flush();
250 _PowerManagerStub::OnActiveAppChanged(const AppId& appId)
253 appId.SubString(0, 10, smallAppId);
254 SysLog(NID_SYS, "Active app [%ls], current [%ls] ", smallAppId.GetPointer(), __activeAppId.GetPointer());
255 if(__activeAppId != smallAppId)
257 __activeAppId = smallAppId;
258 result r = __pRegistry->AddValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, _POWER_MANAGER_REGISTRY_ACTIVE_ID, (String)__activeAppId);
259 if (r == E_KEY_ALREADY_EXIST)
261 __pRegistry->SetValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, _POWER_MANAGER_REGISTRY_ACTIVE_ID, (String)__activeAppId);
263 __pRegistry->Flush();
268 SysLog(NID_SYS, "Current App[%ls] is already actived.", __activeAppId.GetPointer());