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_SystemService.cpp
20 * @brief This is the implementation file for _SystemService class.
22 #include <unique_ptr.h>
27 #include <system_info.h>
29 #include <FBaseSysLog.h>
31 #include <FApp_AppManagerImpl.h>
32 #include <FBase_StringConverter.h>
33 #include <FSec_AccessController.h>
35 #include "FApp_CommunicationDispatcher.h"
36 #include "FSys_SystemService.h"
40 using namespace Tizen::App;
41 using namespace Tizen::Io;
42 using namespace Tizen::System;
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Base::Utility;
46 using namespace Tizen::Security;
49 static const wchar_t* _SYSTEM_SERVICE_ID = L"osp.system.service";
50 static const wchar_t* _SYSTEM_COMMAND_CHANGE_TIME = L"osp.system.command.change.time";
51 static const wchar_t* _SYSTEM_COMMAND_CHANGE_BRIGHTNESS = L"osp.system.command.change.brightness";
52 static const wchar_t* _SYSTEM_COMMAND_RESTORE_BRIGHTNESS = L"osp.system.command.restore.brightness";
53 static const wchar_t* _SYSTEM_COMMAND_GET_IMEI = L"osp.system.command.get.imei";
54 static const wchar_t* _SYSTEM_RESULT_OK = L"osp.system.result.ok";
55 static const wchar_t* _SYSTEM_RESULT_ERROR = L"osp.system.result.error";
56 static const wchar_t* _SYSTEM_RESULT_INVALID = L"osp.system.result.invalid";
57 static const wchar_t* _SYSTEM_RESULT_PRIVILEGED = L"osp.system.result.privilege";
58 static const int _SYSTEM_COMMAND_ID = 1;
59 static const int _SYSTEM_COMMAND_ARG = 2;
62 Tizen::System::_SystemService* Tizen::System::_SystemService::__pSystemService = null;
65 DeviceBatteryCallback(int percent, void* userData)
67 device_battery_warn_e batteryLevel = DEVICE_BATTERY_WARN_EMPTY;
68 int ret = device_battery_get_warning_status(&batteryLevel);
69 if(ret == DEVICE_ERROR_NONE && batteryLevel < DEVICE_BATTERY_WARN_NORMAL)
71 _SystemService* pSystemService = _SystemService::GetInstance();
72 if(pSystemService != null)
74 pSystemService->RestoreBrightness();
79 _SystemService::_SystemService()
80 : _ICommunicationRequestListener()
81 , __pCommunicationDispatcher(null)
85 _AppManagerImpl* pAppManagerImpl = null;
87 __pCommunicationDispatcher = _CommunicationDispatcher::GetInstance();
88 SysTryCatch(NID_SYS, __pCommunicationDispatcher != null, r = E_SYSTEM, E_SYSTEM, "_CommunicationDispatcher initiate is failed");
90 r = __pCommunicationDispatcher->AddCommunicationEventListener(*this);
91 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "fail to add event listener");
93 __appBrightnessList.Construct();
94 pAppManagerImpl = _AppManagerImpl::GetInstance();
95 SysTryCatch(NID_SYS, pAppManagerImpl != null, r = E_SYSTEM, E_SYSTEM, "It is failed to get _AppManagerImpl class.");
97 r = pAppManagerImpl->AddActiveAppEventListener(*this);
98 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "It is failed to add active app event listener");
100 ret = device_battery_set_cb(DeviceBatteryCallback, null);
101 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "It is failed to add battery event listener");
107 _SystemService::~_SystemService()
110 result r = E_SUCCESS;
111 _AppManagerImpl* pAppManagerImpl = null;
112 SysTryCatch(NID_SYS, __pCommunicationDispatcher != null, r = E_SYSTEM, E_SYSTEM, "_CommunicationDispatcher is not ready");
114 r = __pCommunicationDispatcher->RemoveCommunicationEventListener(*this);
115 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "fail to remove event listener");
117 ret = device_battery_unset_cb();
118 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "It is failed to remove battery event listener");
120 pAppManagerImpl = _AppManagerImpl::GetInstance();
121 SysTryCatch(NID_SYS, pAppManagerImpl != null, r = E_SYSTEM, E_SYSTEM, "It is failed to get _AppManagerImpl class.");
123 r = pAppManagerImpl->RemoveActiveAppEventListener(*this);
124 SysTryCatch(NID_SYS, r == E_SUCCESS, , E_SYSTEM, "It is failed to remove active app listener.");
126 __appBrightnessList.RemoveAll(true);
133 _SystemService::GetInstance(void)
135 if(__pSystemService == null)
137 __pSystemService = new (std::nothrow) _SystemService();
139 return __pSystemService;
143 _SystemService::GetId(void)
145 return _SYSTEM_SERVICE_ID;
149 _SystemService::RestoreBrightness(void)
152 Integer* pBrightness = null;
153 String convertedActiveAppId;
154 __activeAppId.SubString( 0, 10, convertedActiveAppId);
156 SysLog(NID_SYS, "Active App is %ls", __activeAppId.GetPointer());
158 pBrightness = (Integer*)(__appBrightnessList.GetValue(convertedActiveAppId));
160 if(pBrightness != null)
162 //Change application's brightness
163 SysLog(NID_SYS, "Application's brightness is %d", pBrightness->ToInt());
164 ret = device_set_brightness(0, pBrightness->ToInt());
165 if(ret != DEVICE_ERROR_NONE)
167 SysLogException(NID_SYS, E_SYSTEM, "It is failed to change screen brightness");
172 //Restore system brightness
173 SysLog(NID_SYS, "Restore to System brightness");
174 ret = device_set_brightness_from_settings(0);
175 if(ret != DEVICE_ERROR_NONE)
177 SysLogException(NID_SYS, E_SYSTEM, "It is failed to restore screen brightness");
184 _SystemService::OnActiveAppChanged(const AppId& appId)
186 __activeAppId = appId;
191 _SystemService::OnRequestOccured(AppId appId, ArrayList* request, ArrayList* response)
194 result r = E_SUCCESS;
195 StringComparer strComparer;
196 String* resultId = null;
197 String* command = null;
198 String* serviceId = null;
199 String* commandId = null;
200 String* returnValue = null;
202 SysLog(NID_APP, "Request is forwarded to SystemService");
203 SysTryCatch(NID_SYS, request != null && response != null, r = E_INVALID_ARG, E_INVALID_ARG, "Parameters are null");
205 command = (String*)request->GetAt(_SYSTEM_COMMAND_ID);
206 SysTryCatch(NID_SYS, command!= null, r = E_INVALID_ARG, E_INVALID_ARG, "Parameters has no command %x, %x", command);
208 serviceId = new (std::nothrow) String(_SYSTEM_SERVICE_ID);
209 commandId = new (std::nothrow) String(_SYSTEM_COMMAND_CHANGE_TIME);
211 if(*command == _SYSTEM_COMMAND_GET_IMEI)
213 SysLog(NID_SYS, "Get IMEI");
216 r = _AccessController::CheckSystemPrivilege(appId, _PRV_USERIDENTITY);
220 SysLogException(NID_SYS, E_PRIVILEGE_DENIED, "Application[%ls] does not have enought privilege.", appId.GetPointer());
221 resultId = new (std::nothrow) String(_SYSTEM_RESULT_PRIVILEGED);
226 ret = system_info_get_value_string(SYSTEM_INFO_KEY_MOBILE_DEVICE_ID, &pImei);
227 if(ret != SYSTEM_INFO_ERROR_NONE || pImei == null)
229 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get IMEI.");
230 resultId = new (std::nothrow) String(_SYSTEM_RESULT_ERROR);
234 resultId = new (std::nothrow) String(_SYSTEM_RESULT_OK);
235 returnValue = new (std::nothrow) String(pImei);
240 else if(*command == _SYSTEM_COMMAND_CHANGE_BRIGHTNESS)
242 SysLog(NID_SYS, "Change brightness");
244 String* brightnessBuffer = (String*)request->GetAt(_SYSTEM_COMMAND_ARG);
246 if(brightnessBuffer == null)
248 resultId = new (std::nothrow) String(_SYSTEM_RESULT_ERROR);
252 Integer::Parse(*brightnessBuffer, brightness);
253 SysLog(NID_SYS, "requested brightness is %d", brightness);
255 __appBrightnessList.Remove(appId, true);
256 unique_ptr<Integer> pBrightness(new (std::nothrow) Integer(brightness));
257 unique_ptr<String> pAppId(new (std::nothrow) String(appId));
259 SysTryCatch(NID_SYS, pBrightness != null && pAppId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "It is failed to create appId or brightness.");
261 r = __appBrightnessList.Add(pAppId.get(), pBrightness.get());
263 SysTryCatch(NID_SYS, r == E_SUCCESS, , r, "It is failed to add appid and brightness.");
266 pBrightness.release();
268 SysLog(NID_SYS, "Current App is %ls, Active App is %ls", appId.GetPointer(), __activeAppId.GetPointer());
270 String convertedActiveAppId;
271 __activeAppId.SubString( 0, 10, convertedActiveAppId);
273 if(convertedActiveAppId == appId)
275 SysLog(NID_SYS, "Current application is active. So brightness is changed.");
276 ret = device_set_brightness(0, brightness);
277 SysTryCatch(NID_SYS, ret == DEVICE_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "It is failed to change brightness [%d].", brightness);
280 resultId = new (std::nothrow) String(_SYSTEM_RESULT_OK);
283 else if(*command == _SYSTEM_COMMAND_RESTORE_BRIGHTNESS)
285 SysLog(NID_SYS, "Restore brightness");
286 __appBrightnessList.Remove(appId);
287 String convertedActiveAppId;
288 __activeAppId.SubString( 0, 10, convertedActiveAppId);
290 if(convertedActiveAppId == appId)
292 SysLog(NID_SYS, "Current application is active. So brightness is restored.");
293 ret = device_set_brightness_from_settings(0);
294 SysTryCatch(NID_SYS, ret == DEVICE_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "It is failed to restore system brightness.");
296 resultId = new (std::nothrow) String(_SYSTEM_RESULT_OK);
299 else if(*command == _SYSTEM_COMMAND_CHANGE_TIME)
301 SysLog(NID_SYS, "Change system time");
302 r = _AccessController::CheckSystemPrivilege(appId, _PRV_SYSTEMSETTING_WRITE);
305 resultId = new (std::nothrow) String(_SYSTEM_RESULT_PRIVILEGED);
310 String* dateTime = (String*)request->GetAt(_SYSTEM_COMMAND_ARG);
311 char* datetime = null;
313 datetime = _StringConverter::CopyToCharArrayN(*dateTime);
316 sscanf(datetime, "%3d %2d %2d %2d:%2d:%2d", &(tTime.tm_year), &(tTime.tm_mon), &(tTime.tm_mday), &(tTime.tm_hour), &(tTime.tm_min), &(tTime.tm_sec));
317 time_t _tTime = mktime(&tTime);
318 if(stime(&_tTime) == 0)
320 resultId = new (std::nothrow) String(_SYSTEM_RESULT_OK);
324 resultId = new (std::nothrow) String(_SYSTEM_RESULT_ERROR);
330 resultId = new (std::nothrow) String(_SYSTEM_RESULT_ERROR);
336 resultId = new (std::nothrow) String(_SYSTEM_RESULT_INVALID);
342 resultId = new (std::nothrow) String(_SYSTEM_RESULT_ERROR);
345 response->Add(*serviceId);
346 response->Add(*commandId);
347 response->Add(*resultId);
349 if(returnValue != null)
351 response->Add(*returnValue);
357 _SystemService::OnApplicationTerminated(const AppId& appId, _AppType type)
359 SysLog(NID_SYS, "Application (%ls) is terminated.", appId.GetPointer());
360 __appBrightnessList.Remove(appId, true);