Fixed set brightness fail.
[framework/osp/common-service.git] / src / system / FSys_PowerManagerStub.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file         FSys_PowerManagerStub.cpp
20  * @brief       This is the implementation for the _PowerManagerStub class.
21  */
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"
29
30 using namespace std;
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;
36
37 namespace {
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";
47 }
48
49 _PowerManagerStub* _PowerManagerStub::__pPowerManagerStub = null;
50
51 _PowerManagerStub::_PowerManagerStub(void)
52         : __pIpcServer(null)
53         , __serviceId(POWER_MANAGER_SERVICE_ID)
54         , __pRegistry(null)
55 {
56 }
57
58 _PowerManagerStub::~_PowerManagerStub(void)
59 {
60         if (__pIpcServer)
61         {
62                 __pIpcServer->Stop();
63         }
64
65         if (__pRegistry)
66         {
67                 __pRegistry->Flush();
68                 delete __pRegistry;
69         }
70 }
71
72 _PowerManagerStub*
73 _PowerManagerStub::GetInstance()
74 {
75         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
76
77         if (__pPowerManagerStub == null)
78         {
79                 pthread_once(&onceBlock, InitSingleton);
80         }
81         return __pPowerManagerStub;
82 }
83
84 void
85 _PowerManagerStub::InitSingleton(void)
86 {
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);
93 }
94
95
96 void
97 _PowerManagerStub::DestroySingleton(void)
98 {
99         delete __pPowerManagerStub;
100 }
101
102 result
103 _PowerManagerStub::Construct(void)
104 {
105         unique_ptr<_IpcServer> pIpcServer(new (std::nothrow) _IpcServer());
106         SysTryReturnResult(NID_SYS, pIpcServer, E_OUT_OF_MEMORY, "Memory is insufficient.");
107
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.");
110
111         r = pIpcServer->Start();
112         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to start.");
113
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.");
117
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));
120
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");
123
124         __pRegistry = new (std::nothrow) Registry();
125         SysTryReturn(NID_SYS, __pRegistry, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,"Memory is insufficient.");
126
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));
129
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));
132
133         __pIpcServer = move(pIpcServer);
134
135         return E_SUCCESS;
136 }
137
138 result
139 _PowerManagerStub::ChangeBrightness()
140 {
141         result r = E_FAILURE;
142         int brightness = -1;
143
144         r = __pRegistry->GetValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, (String)__activeAppId, brightness);
145         if (r == E_SUCCESS)
146         {
147                 r = _PowerManager::ChangeBrightness(brightness);
148                 SysLog(NID_SYS, "change brightness %d", brightness);
149         }
150
151         if (r != E_SUCCESS)
152         {
153                 r = _PowerManager::ChangeBrightness(0);
154                 SysLog(NID_SYS, "change brightness system value.");
155         }
156
157         return r;
158 }
159 void
160 _PowerManagerStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message)
161 {
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()
166 }
167
168 bool
169 _PowerManagerStub::OnRequestOccured(const ArrayList& request, ArrayList* response)
170 {
171         bool isSuccess = false;
172         result r = E_SUCCESS;
173
174         __command = *((String*)request.GetAt(_POWER_COMMAND_ID));
175
176         if (__activeAppId.IsEmpty())
177         {
178                 r = __pRegistry->GetValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, _POWER_MANAGER_REGISTRY_ACTIVE_ID, __activeAppId);
179                 if (r == E_SUCCESS)
180                 {
181                         SysLog(NID_SYS, "Get the active id from registry [%ls].", __activeAppId.GetPointer());
182                 }
183                 else
184                 {
185                         SysLog(NID_SYS, "It is failed to get the active id from registry");
186                 }
187         }
188
189         if (__command == _POWER_BRIGHTNESS_CHANGE)
190         {
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());
193                 int brightness = 0;
194                 String* brightnessBuffer = (String*)request.GetAt(_POWER_COMMAND_ID + 1);
195                 Integer::Parse(*brightnessBuffer, brightness);
196
197                 __pRegistry->RemoveValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, (String)__currentAppId);
198
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();
203         }
204         else if(__command == _POWER_BRIGHTNESS_RESTORE)
205         {
206                 SysLog(NID_SYS, "Request bright restore");
207                 __pRegistry->RemoveValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, (String)__currentAppId);
208                 r = ChangeBrightness();
209         }
210         else
211         {
212                 SysLogException(NID_SYS, E_SYSTEM, "Required Command[%ls] is not proccess on _PowerManager.", __command.GetPointer());
213                 r = E_SYSTEM;
214         }
215
216         isSuccess = true;
217 CATCH:
218         if(r == E_SUCCESS)
219         {
220                 __result = _POWER_OK;
221         }
222         else
223         {
224                 __result = _POWER_ERROR;
225         }
226
227         response->Add(__serviceId);
228         response->Add(__command);
229         response->Add(__result);
230
231         ArrayList* temp = const_cast<ArrayList*>(&request);
232         temp->RemoveAll(true);
233
234         __pRegistry->Flush();
235         return isSuccess;
236 }
237
238 void
239 _PowerManagerStub::OnApplicationTerminated(const AppId& appId, int pid)
240 {
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();
246         ChangeBrightness();
247 }
248
249 void
250 _PowerManagerStub::OnActiveAppChanged(const AppId& appId)
251 {
252         AppId smallAppId;
253         appId.SubString(0, 10, smallAppId);
254         SysLog(NID_SYS, "Active app [%ls], current [%ls] ", smallAppId.GetPointer(), __activeAppId.GetPointer());
255         if(__activeAppId != smallAppId)
256         {
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)
260                 {
261                         __pRegistry->SetValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, _POWER_MANAGER_REGISTRY_ACTIVE_ID, (String)__activeAppId);
262                 }
263                 __pRegistry->Flush();
264                 ChangeBrightness();
265         }
266         else
267         {
268                 SysLog(NID_SYS, "Current App[%ls] is already actived.", __activeAppId.GetPointer());
269         }
270 }