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_AlarmManager.cpp
20 * @brief This is the implementation file for _AlarmManager class.
23 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FSysSystemTime.h>
28 #include <FBase_NativeError.h>
29 #include <FIo_AppServiceIpcMessages.h>
31 #include "FSys_AlarmManager.h"
32 #include "FSys_CommunicationDispatcherClient.h"
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Io;
39 namespace Tizen { namespace System
42 static const wchar_t* _ALARM_SERVICE_ID = L"osp.alarm.service";
43 static const wchar_t* _ALARM_SET_SINGLEALARM = L"osp.alarm.set.singlealarm";
44 static const wchar_t* _ALARM_SET_REPEATEDALARM = L"osp.alarm.set.repeatedalarm";
45 static const wchar_t* _ALARM_CANCEL = L"osp.alarm.cancel";
46 static const wchar_t* _ALARM_EXPIRED_EVENT = L"osp.alarm.expired.callback";
47 static const wchar_t* _ALARM_RESULT_OK = L"osp.alarm.result.ok";
49 const int _ALARM_SERVICE_IPC_MESSAGE_SERVICE_ID = 0;
50 const int _ALARM_SERVICE_IPC_MESSAGE_COMMAND_ID = 1;
51 const int _ALARM_SERVICE_RESPONSE_DATA = 2;
52 const int _ALARM_SERVICE_ALARM_ID = 3;
54 _AlarmManager* _AlarmManager::pAlarmManager = null;
56 _AlarmManager::_AlarmManager()
60 static String ALARM_SERVICE_ID(_ALARM_SERVICE_ID);
61 __alarmList.Construct();
63 _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
64 SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is It is It is failed to get CommunicationDispatcherClient.");
66 r = pCommunicationDispatcherClient->RegisterCommunicationListener(ALARM_SERVICE_ID, *this);
67 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is It is It is failed to register on CommunicationDispatcherClient.");
69 __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
75 _AlarmManager::~_AlarmManager()
80 _AlarmManager::GetInstance(void)
82 if(pAlarmManager == null)
84 pAlarmManager = new (std::nothrow) _AlarmManager();
90 _AlarmManager::RegisterAlarm(_AlarmImpl* pAlarmImpl)
92 SysTryReturnResult(NID_SYS, pAlarmImpl != null, E_INVALID_ARG, "There is no alarmImpl.");
93 Integer alarmId(pAlarmImpl->__alarmId.value);
94 SysTryReturnResult(NID_SYS, __alarmList.ContainsKey(alarmId) == false, E_OBJ_ALREADY_EXIST, "Required Alarm[%d] is already registered.", pAlarmImpl->__alarmId.value);
97 ArrayList requestMessage;
98 ArrayList responseMessage;
104 int period = pAlarmImpl->GetPeriod();
106 String serviceId = _ALARM_SERVICE_ID;
107 String commandId = _ALARM_SET_SINGLEALARM;
108 if(period > 0) //If period is 0, alarm is operated as Single alarm.
110 commandId = _ALARM_SET_REPEATEDALARM;
113 DateTime currentTime;
114 DateTime startTime = pAlarmImpl->GetStartTime();
115 const DateTime* pEndTime = pAlarmImpl->GetEndTime();
118 SysTryReturnResult(NID_SYS, __pIpcClient != null, E_INVALID_STATE, "This instance has not been constructed as yet.");
121 r = SystemTime::GetCurrentTime(WALL_TIME, currentTime);
122 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is It is failed to get current time");
123 SysTryReturnResult(NID_SYS, DateTime::Compare(currentTime, startTime) < 0, E_INVALID_ARG, "Designated start time has to be greater then current time");
124 SysTryReturnResult(NID_SYS, period > -1, E_INVALID_ARG, "Period has to greater then -1");
128 SysTryReturnResult(NID_SYS, DateTime::Compare(startTime, *pEndTime) < 0, E_INVALID_ARG, "Designated end time is less then start time");
130 //End Alarm validation check
132 //IPC data construction
133 r = requestMessage.Construct();
134 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to create request instance.");
135 r = responseMessage.Construct();
136 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to create response instance.");
138 r = requestMessage.Add(serviceId);
139 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to add service id.");
140 r = requestMessage.Add(commandId);
141 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to add command id.");
142 startTimeStr = startTime.ToString();
143 r = requestMessage.Add(startTimeStr);
144 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to add start time value.");
145 periodStr.Append(period);
146 r = requestMessage.Add(periodStr);
147 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to add period value.");
151 endTimeStr = pEndTime->ToString();
152 r = requestMessage.Add(endTimeStr);
153 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to add end time value.");
156 std::unique_ptr<IoService_Request> pRequest(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
159 r = __pIpcClient->SendRequest(pRequest.get());
160 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to send ipc message.");
161 SysLog(NID_SYS, "Alarm reqeust is deliverted to osp-app-service successfully.");
163 //IPC data response check
165 String* pResult = (String*)responseMessage.GetAt(_ALARM_SERVICE_RESPONSE_DATA);
166 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result.");
167 SysTryReturnResult(NID_SYS, *pResult == _ALARM_RESULT_OK, E_SYSTEM, "It is It is failed to register alarm. [%ls]", pResult->GetPointer());
169 String* pAlarmId = (String*)responseMessage.GetAt(_ALARM_SERVICE_ALARM_ID);
170 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no alarmId.");
171 SysLog(NID_SYS, "Reserved alarm id is %ls from osp-app-service.", pAlarmId->GetPointer());
173 int registeredAlarmId = 0;
174 Integer::Parse(*pAlarmId, registeredAlarmId);
176 pAlarmImpl->__alarmId.value = registeredAlarmId;
177 r = __alarmList.Add(&(pAlarmImpl->__alarmId), pAlarmImpl);
182 _AlarmManager::UnregisterAlarm(_AlarmImpl* pAlarmImpl)
184 result r = E_SUCCESS;
185 SysTryReturnResult(NID_SYS, pAlarmImpl != null, E_INVALID_ARG, "There is no alarmImpl.");
187 Integer alarmId(pAlarmImpl->__alarmId.value);
188 SysTryReturnResult(NID_SYS, __alarmList.ContainsKey(alarmId) == true, E_OBJ_NOT_FOUND, "There is no registered alarm.");
189 r = __alarmList.Remove(alarmId);
191 ArrayList requestMessage;
192 ArrayList responseMessage;
193 String serviceId(_ALARM_SERVICE_ID);
194 String commandId(_ALARM_CANCEL);
197 SysTryReturnResult(NID_SYS, __pIpcClient != null, E_INVALID_STATE, "This instance has not been constructed as yet.");
199 //IPC data construction
200 r = requestMessage.Construct();
201 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create request instance");
202 r = responseMessage.Construct();
203 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create response instance");
205 r = requestMessage.Add(serviceId);
206 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add service id");
207 r = requestMessage.Add(commandId);
208 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add command id");
211 alarmIdStr.Append(alarmId.value);
213 r = requestMessage.Add(alarmIdStr);
214 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add alarm id value");
217 std::unique_ptr<IoService_Request> pRequest(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
219 r = __pIpcClient->SendRequest(pRequest.get());
220 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send ipc message.");
221 SysLog(NID_SYS, "Alarm cancel reqeust is deliverted to osp-app-service successfully");
223 //IPC data response check
224 String* pResult = (String*)responseMessage.GetAt(_ALARM_SERVICE_RESPONSE_DATA);
226 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "It is failed to receive Ipc response");
227 SysTryReturnResult(NID_SYS, *pResult == _ALARM_RESULT_OK, E_SYSTEM, "It is failed to cancel alarm");
233 _AlarmManager::UpdateAlarm(_AlarmImpl* pAlarmImpl)
235 result r = E_SUCCESS;
236 SysTryReturnResult(NID_SYS, pAlarmImpl != null, E_INVALID_ARG, "There is no alarmImpl.");
238 Integer alarmId(pAlarmImpl->__alarmId.value);
239 SysTryReturnResult(NID_SYS, __alarmList.ContainsKey(alarmId) == true, E_OBJ_NOT_FOUND, "There is no registered alarm.");
241 UnregisterAlarm(pAlarmImpl);
242 RegisterAlarm(pAlarmImpl);
247 _AlarmManager::OnDataReceived(const Tizen::Base::Collection::ArrayList& data)
249 SysLog(NID_SYS, "IPC message is delivered.");
252 String* pServiceId = (String*) data.GetAt(_ALARM_SERVICE_IPC_MESSAGE_SERVICE_ID);
253 String* pCommandId = (String*) data.GetAt(_ALARM_SERVICE_IPC_MESSAGE_COMMAND_ID);
254 String* pData = (String*) data.GetAt(_ALARM_SERVICE_RESPONSE_DATA);
256 SysLog(NID_SYS, "IPC message is delivered.");
257 String serviceId(_ALARM_SERVICE_ID);
258 String commandId(_ALARM_EXPIRED_EVENT);
260 SysTryReturnVoidResult(NID_SYS, pServiceId != null && pCommandId != null && pData != null, E_SYSTEM, "There is no device data.");
262 if(*pServiceId == serviceId && *pCommandId == commandId)
264 SysLog(NID_SYS, "Alarm expire event is delivered.");
266 Integer::Parse(*pData, alarmId);
268 Integer reservedAlarmId(alarmId);
270 _AlarmImpl* pAlarmImpl = (_AlarmImpl*)__alarmList.GetValue(reservedAlarmId);
271 if(pAlarmImpl != null)
273 SysLog(NID_SYS, "Reserved Alarm[%d] is found.", alarmId);
274 pAlarmImpl->OnAlarmExpired(alarmId);