45bc393fbf892eeec04313bd3d35002ed4d5c1f9
[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                 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r,
180                         "It is failed to get the active id from registry");
181                 SysLog(NID_SYS, "Get the active id from registry [%ls].", __activeAppId.GetPointer());
182         }
183
184         if (__command == _POWER_BRIGHTNESS_CHANGE)
185         {
186                 SysTryCatch(NID_SYS, __activeAppId == __currentAppId, r = E_SUCCESS, r,
187                         "It is not active application[%ls]. Current Active App is [%ls].", __currentAppId.GetPointer(), __activeAppId.GetPointer());
188                 int brightness = 0;
189                 String* brightnessBuffer = (String*)request.GetAt(_POWER_COMMAND_ID + 1);
190                 Integer::Parse(*brightnessBuffer, brightness);
191
192                 __pRegistry->RemoveValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, (String)__currentAppId);
193
194                 r = __pRegistry->AddValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, __currentAppId, brightness);
195                 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r,
196                         "It is failed to add requested App Id(%ls) and Brightness(%d) on the managed app list.", __currentAppId.GetPointer(), brightness);
197                 r = ChangeBrightness();
198         }
199         else if(__command == _POWER_BRIGHTNESS_RESTORE)
200         {
201                 SysLog(NID_SYS, "Request bright restore");
202                 __pRegistry->RemoveValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, (String)__currentAppId);
203                 r = ChangeBrightness();
204         }
205         else
206         {
207                 SysLogException(NID_SYS, E_SYSTEM, "Required Command[%ls] is not proccess on _PowerManager.", __command.GetPointer());
208                 r = E_SYSTEM;
209         }
210
211         isSuccess = true;
212 CATCH:
213         if(r == E_SUCCESS)
214         {
215                 __result = _POWER_OK;
216         }
217         else
218         {
219                 __result = _POWER_ERROR;
220         }
221
222         response->Add(__serviceId);
223         response->Add(__command);
224         response->Add(__result);
225
226         ArrayList* temp = const_cast<ArrayList*>(&request);
227         temp->RemoveAll(true);
228
229         __pRegistry->Flush();
230         return isSuccess;
231 }
232
233 void
234 _PowerManagerStub::OnApplicationTerminated(const AppId& appId, int pid)
235 {
236         SysLog(NID_SYS, "Terminated app [%ls]", appId.GetPointer());
237         String requiredAppId;
238         appId.SubString(0, 10, requiredAppId);
239         __pRegistry->RemoveValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, (String)requiredAppId);
240         __pRegistry->Flush();
241         ChangeBrightness();
242 }
243
244 void
245 _PowerManagerStub::OnActiveAppChanged(const AppId& appId)
246 {
247         AppId smallAppId;
248         appId.SubString(0, 10, smallAppId);
249         SysLog(NID_SYS, "Active app [%ls], current [%ls] ", smallAppId.GetPointer(), __activeAppId.GetPointer());
250         if(__activeAppId != smallAppId)
251         {
252                 __activeAppId = smallAppId;
253                 result r = __pRegistry->AddValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, _POWER_MANAGER_REGISTRY_ACTIVE_ID, (String)__activeAppId);
254                 if (r == E_KEY_ALREADY_EXIST)
255                 {
256                         __pRegistry->SetValue(_POWER_MANAGER_REGISTRY_BRIGHTNESS_SECTION, _POWER_MANAGER_REGISTRY_ACTIVE_ID, (String)__activeAppId);
257                 }
258                 __pRegistry->Flush();
259                 ChangeBrightness();
260         }
261         else
262         {
263                 SysLog(NID_SYS, "Current App[%ls] is already actived.", __activeAppId.GetPointer());
264         }
265 }