2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FSys_AlarmManager.cpp
19 * @brief This is the implementation file for _AlarmManager class.
22 #include <unique_ptr.h>
24 #include <appfw/app.h>
26 #include <appsvc/appsvc.h>
28 #include <FBaseSysLog.h>
29 #include <FSysSystemTime.h>
31 #include <FBase_NativeError.h>
33 #include <FBase_StringConverter.h>
35 #include "inc/FSys_AlarmManager.h"
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Io;
42 namespace Tizen { namespace System
44 static const long _SECOND_OF_MINUTE = 60;
46 _AlarmManager* _AlarmManager::__pAlarmManager = null;
48 _AlarmManager::_AlarmManager()
49 : __pAlarmHashMap(null)
52 int ret = pthread_mutex_init(&__lock, null);
53 SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, E_SYSTEM, "It is failed to init mutex.");
55 __alarmList.Construct();
59 __pAlarmHashMap = new (std::nothrow) MultiHashMap();
60 SysTryCatch(NID_SYS, __pAlarmHashMap != null, r = E_SYSTEM, E_SYSTEM, "__pAlarmHashMap should not be null");
62 r = __pAlarmHashMap->Construct();
63 SysTryCatch(NID_SYS, r == E_SUCCESS, r= E_SYSTEM, E_SYSTEM, "[%s] Propagated.", GetErrorMessage(r));
66 //Checks for any alarm existence
73 _AlarmManager::~_AlarmManager()
75 int ret = pthread_mutex_destroy(&__lock);
78 SysLogException(NID_SYS, E_SYSTEM, "It is failed to destroy mutex.");
81 delete __pAlarmHashMap;
82 __pAlarmHashMap = null;
84 //Cancel all the alarms before exiting
89 _AlarmManager::GetInstance(void)
91 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
92 if(__pAlarmManager == null)
94 pthread_once(&onceBlock, InitSingleton);
96 return __pAlarmManager;
100 _AlarmManager::InitSingleton(void)
102 _AlarmManager* pAlarmManager = new (std::nothrow) _AlarmManager();
103 SysTryReturnVoidResult(NID_SYS, pAlarmManager, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
104 GetErrorMessage(E_OUT_OF_MEMORY));
106 __pAlarmManager = pAlarmManager;
107 std::atexit(DestroySingleton);
111 _AlarmManager::DestroySingleton(void)
113 delete __pAlarmManager;
117 _AlarmManager::ReserveAlarm(Tizen::Base::String appId, Tizen::Base::DateTime startTime, int period)
119 int reservedAlarmId = -1;
120 result r = E_SUCCESS;
125 SysLog(NID_SYS, "Reserve time is %d/%d/%d %d:%d:%d", startTime.GetYear(), startTime.GetMonth(), startTime.GetDay(), startTime.GetHour(), startTime.GetMinute(), startTime.GetSecond());
127 bundle* pBundle = null;
128 alarm_entry_t* pAlarmInfo = null;
129 alarm_date_t expireTime = {0,};
131 std::unique_ptr<char[]> pId(_StringConverter::CopyToCharArrayN(appId));
133 pBundle = bundle_create();
134 SysTryCatch(NID_SYS, pBundle != null, r = E_SYSTEM, r, "It is failed to create bundle");
136 ret = appsvc_set_operation(pBundle,"osp.operation.ALARM");
137 SysTryCatch(NID_SYS, ret == SERVICE_ERROR_NONE, r = E_SYSTEM, r, "It is failed to set operation");
139 ret = appsvc_set_appid(pBundle, (const char*)(pId.get()));
140 SysTryCatch(NID_SYS, ret == SERVICE_ERROR_NONE, r = E_SYSTEM, r, "It is failed to set appid for %ls", appId.GetPointer());
142 pAlarmInfo = alarmmgr_create_alarm();
143 SysTryCatch(NID_SYS, pAlarmInfo != null, r = E_SYSTEM, r, "It is failed to create alarm entry");
145 expireTime.year = startTime.GetYear();
146 expireTime.month = startTime.GetMonth();
147 expireTime.day = startTime.GetDay();
148 expireTime.hour = startTime.GetHour();
149 expireTime.min = startTime.GetMinute();
150 expireTime.sec = startTime.GetSecond();
152 ret = alarmmgr_set_time(pAlarmInfo, expireTime);
153 SysTryCatch(NID_SYS, ret == ALARMMGR_RESULT_SUCCESS, r = E_SYSTEM, r, "It is failed to set new alarm time");
157 SysLog(NID_SYS, "It is repeated alarm and period time is %d minutes", period);
158 ret = alarmmgr_set_repeat_mode(pAlarmInfo, ALARM_REPEAT_MODE_REPEAT, period * _SECOND_OF_MINUTE);
159 SysTryCatch(NID_SYS, ret == ALARMMGR_RESULT_SUCCESS, r = E_SYSTEM, r, "It is failed to set repeat mode");
162 ret = alarmmgr_set_type(pAlarmInfo, ALARM_TYPE_NOLAUNCH);
163 SysTryCatch(NID_SYS, ret == ALARMMGR_RESULT_SUCCESS, r = E_SYSTEM, r, "It is failed to set repeat mode");
165 ret = alarmmgr_add_alarm_appsvc_with_localtime(pAlarmInfo,(void *)pBundle, &reservedAlarmId);
166 SysTryCatch(NID_SYS, ret == ALARMMGR_RESULT_SUCCESS, r = E_SYSTEM, r, "Alarm creation failed!!! Alrmgr error code is %d", ret);
171 reservedAlarmId = -1;
173 if(pAlarmInfo != null)
175 alarmmgr_free_alarm(pAlarmInfo);
180 bundle_free(pBundle);
183 return reservedAlarmId;
187 _AlarmManager::AddAlarmList(int alarmId, int period, String appId, DateTime* endTime)
189 result r = E_SUCCESS;
191 SysTryReturnResult(NID_SYS, __pAlarmHashMap != null, E_SYSTEM, "Alarm list does not initialized");
192 SysLog(NID_SYS, "New Alarm Id is added (%d, %d, %ls)", alarmId, period, appId.GetPointer());
194 Integer* reservedAlarmId = null;
195 Integer* alarmPeriod = null;
196 String* alarmAppId = null;
198 Integer* reverseAlarmId = null;
199 String* reverseAppId = null;
201 reservedAlarmId = new (std::nothrow) Integer(alarmId);
202 SysTryCatch(NID_SYS, reservedAlarmId != null, r = E_SYSTEM, E_SYSTEM, "reservedAlarmId should not be null");
204 alarmPeriod = new (std::nothrow) Integer(period);
205 SysTryCatch(NID_SYS, alarmPeriod != null, r = E_SYSTEM, E_SYSTEM, "alarmPeriod should not be null");
207 alarmAppId = new (std::nothrow) String(appId);
208 SysTryCatch(NID_SYS, alarmAppId != null, r = E_SYSTEM, E_SYSTEM, "alarmAppId should not be null");
211 r = __pAlarmHashMap->Add(*reservedAlarmId, *alarmAppId);
212 SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm app id on the alarm list");
214 r = __pAlarmHashMap->Add(*reservedAlarmId, *alarmPeriod);
215 SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm period on the alarm list");
219 DateTime* alarmEndTime = new DateTime(*endTime);
221 SysLog(NID_SYS, "Endtime exists %d:%d:%d", alarmEndTime->GetHour(), alarmEndTime->GetMinute(), alarmEndTime->GetSecond());
222 r = __pAlarmHashMap->Add(*reservedAlarmId, *alarmEndTime);
223 SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm endtime on the alarm list");
227 reverseAlarmId = new (std::nothrow) Integer(alarmId);
228 SysTryCatch(NID_SYS, reverseAlarmId != null, r = E_SYSTEM, E_SYSTEM, "reverseAlarmId should not be null");
230 reverseAppId = new (std::nothrow) String(appId);
231 SysTryCatch(NID_SYS, reverseAppId != null, r = E_SYSTEM, E_SYSTEM, "reverseAppId should not be null");
233 r = __pAlarmHashMap->Add(*reverseAppId, *reverseAlarmId);
234 SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm on the alarm list");
239 if(reservedAlarmId != null)
241 __pAlarmHashMap->Remove(*reservedAlarmId, false);
242 delete reservedAlarmId;
245 if(alarmPeriod != null)
250 if(alarmAppId != null)
255 if(reverseAppId != null)
257 __pAlarmHashMap->Remove(*reverseAppId, false);
261 if(reverseAlarmId != null)
263 delete reverseAlarmId;
270 _AlarmManager::RemoveAlarmList(int alarmId)
272 result r = E_SUCCESS;
273 Integer requiredAlarmId(alarmId);
274 std::unique_ptr<IEnumerator>pValueEnum(null);
276 SysTryReturnResult(NID_SYS, __pAlarmHashMap != null, E_SYSTEM, "Alarm list does not initialized");
278 alarm_cancel(alarmId);
279 SysLog(NID_SYS, "AlarmId %d disarms the timer.", alarmId);
281 pValueEnum.reset(__pAlarmHashMap->GetValuesN(requiredAlarmId));
283 if(pValueEnum != null)
285 SysLog(NID_SYS, "Reserved Alarms are exist");
286 String* alarmAppId = null;
287 r = pValueEnum->MoveNext();
288 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Reserved Alarm List is not valid.");
289 alarmAppId = static_cast< String* >(pValueEnum->GetCurrent());
291 if(alarmAppId != null)
293 SysLog(NID_SYS, "Alarm AppId is %S", alarmAppId->GetPointer());
294 std::unique_ptr<IEnumerator>pAlarmEnum(__pAlarmHashMap->GetValuesN(*alarmAppId));
295 if(pAlarmEnum != null)
297 while(pAlarmEnum->MoveNext() == E_SUCCESS)
299 Integer* reverseAlarmId = static_cast< Integer* >(pAlarmEnum->GetCurrent());
300 if(reverseAlarmId != null)
302 SysLog(NID_SYS, "Reserved Alarm is %d", reverseAlarmId->ToInt());
303 if(reverseAlarmId->ToInt() == alarmId)
305 SysLog(NID_SYS, "Remove reservedAlarmId for reverse look-up");
306 __pAlarmHashMap->Remove(*alarmAppId, *reverseAlarmId);
307 delete reverseAlarmId;
315 __pAlarmHashMap->GetCount(*alarmAppId, count);
318 SysLog(NID_SYS, "There is no more reserved alarm for AppId:%S", alarmAppId->GetPointer());
319 __pAlarmHashMap->Remove(*alarmAppId, true);
323 r = __pAlarmHashMap->Remove(requiredAlarmId, true);
329 _AlarmManager::RemoveAlarmList(String appId)
331 result r = E_SUCCESS;
332 Integer requiredAlarmId;
333 IEnumerator *pValueEnum = null;
335 SysTryReturnResult(NID_SYS, __pAlarmHashMap != null, E_SYSTEM, "Alarm list does not initialized");
336 pValueEnum = __pAlarmHashMap->GetValuesN(appId);
338 if(pValueEnum != null)
340 SysLog(NID_SYS, "Reserved Alarms are exist for AppId:%S", appId.GetPointer());
341 while(pValueEnum->MoveNext() == E_SUCCESS)
343 Integer* reverseAlarmId = static_cast< Integer* >(pValueEnum->GetCurrent());
344 if(reverseAlarmId != null)
346 SysLog(NID_SYS, "Reserved AlarmId is %d", reverseAlarmId->ToInt());
347 alarm_cancel(reverseAlarmId->ToInt());
348 __pAlarmHashMap->Remove(*reverseAlarmId, true);
353 r = __pAlarmHashMap->Remove(appId, true);
359 _AlarmManager::RegisterAlarm(_AlarmImpl* pAlarmImpl)
361 SysTryReturnResult(NID_SYS, pAlarmImpl != null, E_INVALID_ARG, "There is no alarmImpl.");
362 Integer alarmId(pAlarmImpl->__alarmId.value);
363 SysTryReturnResult(NID_SYS, __alarmList.ContainsKey(alarmId) == false, E_OBJ_ALREADY_EXIST, "Required Alarm[%d] is already registered.", pAlarmImpl->__alarmId.value);
365 result r = E_SUCCESS;
366 ArrayList requestMessage;
367 ArrayList responseMessage;
374 int reservedAlarmId = 0;
375 int period = pAlarmImpl->GetPeriod();
377 Tizen::App::App* pApp = null;
379 DateTime currentTime;
380 DateTime startTime = pAlarmImpl->GetStartTime();
381 const DateTime* pEndTime = pAlarmImpl->GetEndTime();
385 startTime.AddMilliseconds(-1 * startTime.GetMillisecond());
386 r = SystemTime::GetCurrentTime(WALL_TIME, currentTime);
387 currentTime.AddMilliseconds(-1 * currentTime.GetMillisecond());
389 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get current time.");
390 SysTryReturnResult(NID_SYS, DateTime::Compare(currentTime, startTime) < 0, E_INVALID_ARG, "Designated start time has to be greater than current time.");
391 SysTryReturnResult(NID_SYS, period > -1, E_INVALID_ARG, "Period has to greater than -1");
393 ret = pthread_mutex_lock(&__lock);
394 SysTryCatch(NID_SYS, ret == 0, r = E_INVALID_ARG, r, "It is failed to lock mutex.");
399 endTime.AddMilliseconds(-1 * endTime.GetMillisecond());
401 SysTryCatch(NID_SYS, DateTime::Compare(startTime, endTime) < 0, r = E_INVALID_ARG, r, "Designated end time is less than start time.");
403 //End Alarm validation check
405 pApp = Tizen::App::App::GetInstance();
406 SysTryCatch(NID_SYS, pApp != null, r = E_SYSTEM, r, "[%s] A system error has been occurred. App::GetInstance() failed.", GetErrorMessage(E_SYSTEM));
407 appId = pApp->GetAppId();
409 reservedAlarmId = ReserveAlarm(appId, startTime, period);
410 SysTryCatch(NID_SYS, reservedAlarmId != -1, r = E_SYSTEM, r, "It is failed to register alarm.");
412 SysLog(NID_SYS, "Reserved AppId %ls, alarmId: %d", appId.GetPointer(), reservedAlarmId);
416 r = AddAlarmList(reservedAlarmId, period, appId, null);
420 r = AddAlarmList(reservedAlarmId, period, appId, (DateTime*)(&endTime));
422 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to add new alarm on the alarm list.");
424 pAlarmImpl->__alarmId.value = reservedAlarmId;
425 r = __alarmList.Add(&(pAlarmImpl->__alarmId), pAlarmImpl);
428 ret = pthread_mutex_unlock(&__lock);
429 SysTryReturnResult(NID_SYS, ret == 0, E_INVALID_ARG, "It is failed to unlock mutex.");
434 _AlarmManager::UnregisterAlarm(_AlarmImpl* pAlarmImpl)
436 result r = E_SUCCESS;
439 SysLog(NID_SYS, "Alarm Cancel request");
440 SysTryCatch(NID_SYS, pAlarmImpl != null, r = E_INVALID_ARG, r, "There is no alarmImpl.");
442 ret = pthread_mutex_lock(&__lock);
443 SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "It is failed to lock mutex.");
445 alarmId.value = pAlarmImpl->__alarmId.value;
446 SysTryCatch(NID_SYS, __alarmList.ContainsKey(alarmId) == true, r = E_OBJ_NOT_FOUND, r, "There is no registered alarm.");
448 r = __alarmList.Remove(alarmId);
449 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to remove reserved alarmImpl instance.");
451 r = RemoveAlarmList(alarmId.ToInt());
452 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to remove reserved alarm list.");
455 ret = pthread_mutex_unlock(&__lock);
456 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It is failed to unlock mutex.");
461 _AlarmManager::UpdateAlarm(_AlarmImpl* pAlarmImpl)
463 result r = E_SUCCESS;
466 SysTryCatch(NID_SYS, pAlarmImpl != null, r = E_INVALID_ARG, r, "There is no alarmImpl.");
468 ret = pthread_mutex_lock(&__lock);
469 SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "It is failed to lock mutex.");
471 alarmId.value = pAlarmImpl->__alarmId.value;
472 SysTryCatch(NID_SYS, __alarmList.ContainsKey(alarmId) == true, r = E_OBJ_NOT_FOUND, r, "There is no registered alarm.");
474 r = UnregisterAlarm(pAlarmImpl);
475 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to unregister reserved alarm list.");
477 r = RegisterAlarm(pAlarmImpl);
479 ret = pthread_mutex_unlock(&__lock);
480 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It is failed to unlock mutex.");
485 _AlarmManager::OnAlarmExpired(int alarmId)
487 result r = E_SUCCESS;
489 String* pAppId = null;
490 Integer* pPeriod = null;
492 std::unique_ptr<IEnumerator> pValueEnum(null);
493 Integer reservedAlarmId(alarmId);
494 DateTime* endTime = null;
495 _AlarmImpl* pAlarmImpl = null;
497 SysTryCatch(NID_SYS, __pAlarmHashMap != null, r = E_SYSTEM, r, "Alarm list does not initialized");
498 pValueEnum.reset(__pAlarmHashMap->GetValuesN(reservedAlarmId));
500 SysLog(NID_SYS, "Alarm expire event is delivered. alarm id is %d", alarmId);
502 pAlarmImpl = (_AlarmImpl*)__alarmList.GetValue(reservedAlarmId);
503 if(pAlarmImpl == null)
505 SysLog(NID_SYS, "pAlarmImpl of reserved alarm[%d] is not found.", alarmId);
508 if(pValueEnum != null)
510 ret = pthread_mutex_lock(&__lock);
511 SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "It is failed to lock mutex.");
513 SysLog(NID_SYS, "Matching Alarm Id is %d \n", alarmId);
514 r = pValueEnum->MoveNext();
515 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "Alarm enum value is not valid.");
516 pAppId = static_cast< String* >(pValueEnum->GetCurrent());
517 alarmAppId.Append(*pAppId);
518 r = pValueEnum->MoveNext();
519 pPeriod = static_cast< Integer* >(pValueEnum->GetCurrent());
520 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "Alarm enum value is not valid.");
522 if(pValueEnum->MoveNext() == E_SUCCESS)
524 endTime = static_cast< DateTime* >(pValueEnum->GetCurrent());
527 SysLog(NID_SYS, "Reserved Alarm AppId:%ls, Period:%d\n", pAppId->GetPointer(), pPeriod->ToInt());
529 if (pPeriod->ToInt() > 0)
533 DateTime currentTime;
535 SystemTime::GetCurrentTime(WALL_TIME, currentTime);
536 currentTime.AddMilliseconds(-1 * currentTime.GetMillisecond()); //Remove millisecond
537 currentTime.AddMinutes(pPeriod->ToInt());
538 SysLog(NID_SYS, "Next time[%d min]: %d:%d:%d:%d", pPeriod->ToInt(), currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond(), currentTime.GetMillisecond());
539 SysLog(NID_SYS, "Endtime exists %d:%d:%d:%d", endTime->GetHour(), endTime->GetMinute(), endTime->GetSecond(), endTime->GetMillisecond());
541 if (currentTime.CompareTo(*endTime) > 0)
543 SysLog(NID_SYS, "Next time is greater than end time.");
544 RemoveAlarmList(alarmId);
547 if(pAlarmImpl != null)
549 pAlarmImpl->__alarmId.value = 0;
554 else if (pPeriod->ToInt() == 0)
556 RemoveAlarmList(alarmId);
558 if(pAlarmImpl != null)
560 pAlarmImpl->__alarmId.value = 0;
568 ret = pthread_mutex_unlock(&__lock);
571 if(pAlarmImpl != null)
573 pAlarmImpl->OnAlarmExpired(alarmId);
576 if(__pAlarmHashMap->ContainsKey(reservedAlarmId) == false)
578 ret = pthread_mutex_lock(&__lock);
579 SysLog(NID_SYS, "Remove an alarm list[%d].", reservedAlarmId.value);
580 r = __alarmList.Remove(reservedAlarmId);
581 ret = pthread_mutex_unlock(&__lock);
586 ret = pthread_mutex_unlock(&__lock);
587 SysTryReturnVoidResult(NID_SYS, ret == 0, E_SYSTEM, "It is failed to unlock mutex.");