7ddeb62e484790427ad341a01495047b3fc5bee6
[platform/framework/native/appfw.git] / src / system / FSys_AlarmManager.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FSys_AlarmManager.cpp
19  * @brief               This is the implementation file for _AlarmManager class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <appfw/app.h>
25 #include <alarm.h>
26 #include <appsvc/appsvc.h>
27
28 #include <FBaseSysLog.h>
29 #include <FSysSystemTime.h>
30 #include <FAppApp.h>
31 #include <FBaseColArrayList.h>
32
33 #include <FBase_NativeError.h>
34 #include <FBase_StringConverter.h>
35
36 #include "inc/FSys_AlarmManager.h"
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Runtime;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Io;
42
43 namespace Tizen { namespace System
44 {
45 static const long _SECOND_OF_MINUTE = 60;
46
47 _AlarmManager* _AlarmManager::__pAlarmManager = null;
48
49 _AlarmManager::_AlarmManager()
50         : __pAlarmHashMap(null)
51 {
52         result r = E_SUCCESS;
53         int ret = pthread_mutex_init(&__lock, null);
54         SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, E_SYSTEM, "It is failed to init mutex.");
55
56         __alarmList.Construct();
57
58         if (!__pAlarmHashMap)
59         {
60                 __pAlarmHashMap = new (std::nothrow) MultiHashMap();
61                 SysTryCatch(NID_SYS, __pAlarmHashMap != null, r = E_SYSTEM, E_SYSTEM, "__pAlarmHashMap should not be null");
62
63                 r = __pAlarmHashMap->Construct();
64                 SysTryCatch(NID_SYS, r == E_SUCCESS, r= E_SYSTEM, E_SYSTEM, "[%s] Propagated.", GetErrorMessage(r));
65         }
66
67         //Checks for any alarm existence
68         alarm_cancel_all();
69
70 CATCH:
71         SetLastResult(r);
72 }
73
74 _AlarmManager::~_AlarmManager()
75 {
76         int ret = pthread_mutex_destroy(&__lock);
77         if(ret != 0)
78         {
79                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to destroy mutex.");
80         }
81
82         delete __pAlarmHashMap;
83         __pAlarmHashMap = null;
84
85         //Cancel all the alarms before exiting
86         alarm_cancel_all();
87 }
88
89 _AlarmManager*
90 _AlarmManager::GetInstance(void)
91 {
92         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
93         if(__pAlarmManager == null)
94         {
95                 pthread_once(&onceBlock, InitSingleton);
96         }
97         return __pAlarmManager;
98 }
99
100 void
101 _AlarmManager::InitSingleton(void)
102 {
103         _AlarmManager* pAlarmManager = new (std::nothrow) _AlarmManager();
104         SysTryReturnVoidResult(NID_SYS, pAlarmManager, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
105                                                    GetErrorMessage(E_OUT_OF_MEMORY));
106
107         __pAlarmManager = pAlarmManager;
108         std::atexit(DestroySingleton);
109 }
110
111 void
112 _AlarmManager::DestroySingleton(void)
113 {
114         delete __pAlarmManager;
115 }
116
117 int
118 _AlarmManager::ReserveAlarm(Tizen::Base::String appId, Tizen::Base::DateTime startTime, int period)
119 {
120         int reservedAlarmId = -1;
121         result r = E_SUCCESS;
122
123         int ret = 0;
124         service_h service;
125
126         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
128         bundle* pBundle = null;
129         alarm_entry_t* pAlarmInfo = null;
130         alarm_date_t expireTime = {0,};
131
132         std::unique_ptr<char[]> pId(_StringConverter::CopyToCharArrayN(appId));
133
134         pBundle = bundle_create();
135         SysTryCatch(NID_SYS, pBundle != null, r = E_SYSTEM, r, "It is failed to create bundle");
136
137         ret = appsvc_set_operation(pBundle,"osp.operation.ALARM");
138         SysTryCatch(NID_SYS, ret == SERVICE_ERROR_NONE, r = E_SYSTEM, r, "It is failed to set operation");
139
140         ret = appsvc_set_appid(pBundle, (const char*)(pId.get()));
141         SysTryCatch(NID_SYS, ret == SERVICE_ERROR_NONE, r = E_SYSTEM, r, "It is failed to set appid for %ls", appId.GetPointer());
142
143         pAlarmInfo = alarmmgr_create_alarm();
144         SysTryCatch(NID_SYS, pAlarmInfo != null, r = E_SYSTEM, r, "It is failed to create alarm entry");
145
146         expireTime.year = startTime.GetYear();
147         expireTime.month = startTime.GetMonth();
148         expireTime.day = startTime.GetDay();
149         expireTime.hour = startTime.GetHour();
150         expireTime.min = startTime.GetMinute();
151         expireTime.sec = startTime.GetSecond();
152
153         ret = alarmmgr_set_time(pAlarmInfo, expireTime);
154         SysTryCatch(NID_SYS, ret == ALARMMGR_RESULT_SUCCESS, r = E_SYSTEM, r, "It is failed to set new alarm time");
155
156         if(period > 0)
157         {
158                 SysLog(NID_SYS, "It is repeated alarm and period time is %d minutes", period);
159                 ret = alarmmgr_set_repeat_mode(pAlarmInfo, ALARM_REPEAT_MODE_REPEAT, period * _SECOND_OF_MINUTE);
160                 SysTryCatch(NID_SYS, ret == ALARMMGR_RESULT_SUCCESS, r = E_SYSTEM, r, "It is failed to set repeat mode");
161         }
162
163         ret = alarmmgr_set_type(pAlarmInfo, ALARM_TYPE_NOLAUNCH);
164         SysTryCatch(NID_SYS, ret == ALARMMGR_RESULT_SUCCESS, r = E_SYSTEM, r, "It is failed to set repeat mode");
165
166         ret = alarmmgr_add_alarm_appsvc_with_localtime(pAlarmInfo,(void *)pBundle, &reservedAlarmId);
167         SysTryCatch(NID_SYS, ret == ALARMMGR_RESULT_SUCCESS, r = E_SYSTEM, r, "Alarm creation failed!!! Alrmgr error code is %d", ret);
168
169 CATCH:
170         if(r == E_SYSTEM)
171         {
172                 reservedAlarmId = -1;
173         }
174         if(pAlarmInfo != null)
175         {
176                 alarmmgr_free_alarm(pAlarmInfo);
177         }
178
179         if(pBundle != null)
180         {
181                 bundle_free(pBundle);
182         }
183
184         return reservedAlarmId;
185 }
186
187 result
188 _AlarmManager::AddAlarmList(int alarmId, int period, String appId, DateTime* endTime)
189 {
190         result r = E_SUCCESS;
191
192         SysTryReturnResult(NID_SYS, __pAlarmHashMap != null, E_SYSTEM, "Alarm list does not initialized");
193         SysLog(NID_SYS, "New Alarm Id is added (%d, %d, %ls)", alarmId, period, appId.GetPointer());
194
195         Integer* reservedAlarmId = null;
196         Integer* alarmPeriod = null;
197         String* alarmAppId = null;
198
199         Integer* reverseAlarmId = null;
200         String* reverseAppId = null;
201
202         reservedAlarmId = new (std::nothrow) Integer(alarmId);
203         SysTryCatch(NID_SYS, reservedAlarmId != null, r = E_SYSTEM, E_SYSTEM, "reservedAlarmId should not be null");
204
205         alarmPeriod = new (std::nothrow) Integer(period);
206         SysTryCatch(NID_SYS, alarmPeriod != null, r = E_SYSTEM, E_SYSTEM, "alarmPeriod should not be null");
207
208         alarmAppId = new (std::nothrow) String(appId);
209         SysTryCatch(NID_SYS, alarmAppId != null, r = E_SYSTEM, E_SYSTEM, "alarmAppId should not be null");
210
211         //Forward
212         r = __pAlarmHashMap->Add(*reservedAlarmId, *alarmAppId);
213         SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm app id on the alarm list");
214
215         r = __pAlarmHashMap->Add(*reservedAlarmId, *alarmPeriod);
216         SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm period on the alarm list");
217
218         if(endTime != null)
219         {
220                 DateTime* alarmEndTime = new DateTime(*endTime);
221
222                 SysLog(NID_SYS, "Endtime exists %d:%d:%d", alarmEndTime->GetHour(), alarmEndTime->GetMinute(), alarmEndTime->GetSecond());
223                 r = __pAlarmHashMap->Add(*reservedAlarmId, *alarmEndTime);
224                 SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm endtime on the alarm list");
225         }
226
227         //Reverse
228         reverseAlarmId = new (std::nothrow) Integer(alarmId);
229         SysTryCatch(NID_SYS, reverseAlarmId != null, r = E_SYSTEM, E_SYSTEM, "reverseAlarmId should not be null");
230
231         reverseAppId = new (std::nothrow) String(appId);
232         SysTryCatch(NID_SYS, reverseAppId != null, r = E_SYSTEM, E_SYSTEM, "reverseAppId should not be null");
233
234         r = __pAlarmHashMap->Add(*reverseAppId, *reverseAlarmId);
235         SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm on the alarm list");
236
237 CATCH:
238         if(r != E_SUCCESS)
239         {
240                 if(reservedAlarmId != null)
241                 {
242                         __pAlarmHashMap->Remove(*reservedAlarmId, false);
243                         delete reservedAlarmId;
244                 }
245
246                 if(alarmPeriod != null)
247                 {
248                         delete alarmPeriod;
249                 }
250
251                 if(alarmAppId != null)
252                 {
253                         delete alarmAppId;
254                 }
255
256                 if(reverseAppId != null)
257                 {
258                         __pAlarmHashMap->Remove(*reverseAppId, false);
259                         delete reverseAppId;
260                 }
261
262                 if(reverseAlarmId != null)
263                 {
264                         delete reverseAlarmId;
265                 }
266         }
267         return r;
268 }
269
270 result
271 _AlarmManager::RemoveAlarmList(int alarmId)
272 {
273         result r = E_SUCCESS;
274         Integer requiredAlarmId(alarmId);
275         std::unique_ptr<IEnumerator>pValueEnum(null);
276
277         SysTryReturnResult(NID_SYS, __pAlarmHashMap != null, E_SYSTEM, "Alarm list does not initialized");
278
279         alarm_cancel(alarmId);
280         SysLog(NID_SYS, "AlarmId %d disarms the timer.", alarmId);
281
282         pValueEnum.reset(__pAlarmHashMap->GetValuesN(requiredAlarmId));
283
284         if(pValueEnum != null)
285         {
286                 SysLog(NID_SYS, "Reserved Alarms are exist");
287                 String* alarmAppId = null;
288                 r = pValueEnum->MoveNext();
289                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Reserved Alarm List is not valid.");
290                 alarmAppId = static_cast< String* >(pValueEnum->GetCurrent());
291
292                 if(alarmAppId != null)
293                 {
294                         SysLog(NID_SYS, "Alarm AppId is %S", alarmAppId->GetPointer());
295                         std::unique_ptr<IEnumerator>pAlarmEnum(__pAlarmHashMap->GetValuesN(*alarmAppId));
296                         if(pAlarmEnum != null)
297                         {
298                                 while(pAlarmEnum->MoveNext() == E_SUCCESS)
299                                 {
300                                         Integer* reverseAlarmId = static_cast< Integer* >(pAlarmEnum->GetCurrent());
301                                         if(reverseAlarmId != null)
302                                         {
303                                                 SysLog(NID_SYS, "Reserved Alarm is %d", reverseAlarmId->ToInt());
304                                                 if(reverseAlarmId->ToInt() == alarmId)
305                                                 {
306                                                         SysLog(NID_SYS, "Remove reservedAlarmId for reverse look-up");
307                                                          __pAlarmHashMap->Remove(*alarmAppId, *reverseAlarmId);
308                                                         delete reverseAlarmId;
309                                                         break;
310                                                 }
311                                         }
312                                 }
313                         }
314
315                         int count = 0;
316                         __pAlarmHashMap->GetCount(*alarmAppId, count);
317                         if(count == 0)
318                         {
319                                 SysLog(NID_SYS, "There is no more reserved alarm for AppId:%S", alarmAppId->GetPointer());
320                                 __pAlarmHashMap->Remove(*alarmAppId, true);
321                         }
322                 }
323         }
324         r = __pAlarmHashMap->Remove(requiredAlarmId, true);
325
326         return r;
327 }
328
329 result
330 _AlarmManager::RemoveAlarmList(String appId)
331 {
332         result r = E_SUCCESS;
333         Integer requiredAlarmId;
334         IEnumerator *pValueEnum = null;
335
336         SysTryReturnResult(NID_SYS, __pAlarmHashMap != null, E_SYSTEM, "Alarm list does not initialized");
337         pValueEnum = __pAlarmHashMap->GetValuesN(appId);
338
339         if(pValueEnum != null)
340         {
341                 SysLog(NID_SYS, "Reserved Alarms are exist for AppId:%S", appId.GetPointer());
342                 while(pValueEnum->MoveNext() == E_SUCCESS)
343                 {
344                         Integer* reverseAlarmId = static_cast< Integer* >(pValueEnum->GetCurrent());
345                         if(reverseAlarmId != null)
346                         {
347                                 SysLog(NID_SYS, "Reserved AlarmId is %d", reverseAlarmId->ToInt());
348                                 alarm_cancel(reverseAlarmId->ToInt());
349                                 __pAlarmHashMap->Remove(*reverseAlarmId, true);
350                         }
351                 }
352                 delete pValueEnum;
353         }
354         r = __pAlarmHashMap->Remove(appId, true);
355
356         return r;
357 }
358
359 result
360 _AlarmManager::RegisterAlarm(_AlarmImpl* pAlarmImpl)
361 {
362         SysTryReturnResult(NID_SYS, pAlarmImpl != null, E_INVALID_ARG, "There is no alarmImpl.");
363         Integer alarmId(pAlarmImpl->__alarmId.value);
364         SysTryReturnResult(NID_SYS, __alarmList.ContainsKey(alarmId) == false, E_OBJ_ALREADY_EXIST, "Required Alarm[%d] is already registered.", pAlarmImpl->__alarmId.value);
365
366         result r = E_SUCCESS;
367         ArrayList requestMessage;
368         ArrayList responseMessage;
369
370         String startTimeStr;
371         String periodStr;
372         String endTimeStr;
373         String appId;
374
375         int reservedAlarmId = 0;
376         int period = pAlarmImpl->GetPeriod();
377         int ret = 0;
378         Tizen::App::App* pApp = null;
379  
380         DateTime currentTime;
381         DateTime startTime = pAlarmImpl->GetStartTime();
382         const DateTime* pEndTime = pAlarmImpl->GetEndTime();
383         DateTime endTime;
384
385         //Argument check
386         startTime.AddMilliseconds(-1 * startTime.GetMillisecond());
387         r = SystemTime::GetCurrentTime(WALL_TIME, currentTime);
388         currentTime.AddMilliseconds(-1 * currentTime.GetMillisecond());
389
390         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get current time.");
391         SysTryReturnResult(NID_SYS, DateTime::Compare(currentTime, startTime) < 0, E_INVALID_ARG, "Designated start time has to be greater than current time.");
392         SysTryReturnResult(NID_SYS, period > -1, E_INVALID_ARG, "Period has to greater than -1");
393
394         ret = pthread_mutex_lock(&__lock);
395         SysTryCatch(NID_SYS, ret == 0, r = E_INVALID_ARG, r, "It is failed to lock mutex.");
396
397         if(pEndTime != null)
398         {
399                 endTime = *pEndTime;
400                 endTime.AddMilliseconds(-1 * endTime.GetMillisecond());
401
402                 SysTryCatch(NID_SYS, DateTime::Compare(startTime, endTime) < 0, r = E_INVALID_ARG, r, "Designated end time is less than start time.");
403         }
404         //End Alarm validation check
405
406         pApp = Tizen::App::App::GetInstance();
407         SysTryCatch(NID_SYS, pApp != null, r = E_SYSTEM, r, "[%s] A system error has been occurred. App::GetInstance() failed.", GetErrorMessage(E_SYSTEM));
408         appId = pApp->GetAppId();
409
410         reservedAlarmId = ReserveAlarm(appId, startTime, period);
411         SysTryCatch(NID_SYS, reservedAlarmId != -1, r = E_SYSTEM, r, "It is failed to register alarm.");
412
413         SysLog(NID_SYS, "Reserved AppId %ls, alarmId: %d", appId.GetPointer(), reservedAlarmId);
414
415         if(pEndTime == null)
416         {
417                 r = AddAlarmList(reservedAlarmId, period, appId, null);
418         }
419         else
420         {
421                 r = AddAlarmList(reservedAlarmId, period, appId, (DateTime*)(&endTime));
422         }
423         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to add new alarm on the alarm list.");
424
425         pAlarmImpl->__alarmId.value = reservedAlarmId;
426         r = __alarmList.Add(&(pAlarmImpl->__alarmId), pAlarmImpl);
427
428 CATCH:
429         ret = pthread_mutex_unlock(&__lock);
430         SysTryReturnResult(NID_SYS, ret == 0, E_INVALID_ARG, "It is failed to unlock mutex.");
431         return r;
432 }
433
434 result
435 _AlarmManager::UnregisterAlarm(_AlarmImpl* pAlarmImpl)
436 {
437         result r = E_SUCCESS;
438         Integer alarmId;
439         int ret = 0;
440         SysLog(NID_SYS, "Alarm Cancel request");
441         SysTryCatch(NID_SYS, pAlarmImpl != null, r = E_INVALID_ARG, r, "There is no alarmImpl.");
442
443         ret = pthread_mutex_lock(&__lock);
444         SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "It is failed to lock mutex.");
445
446         alarmId.value = pAlarmImpl->__alarmId.value;
447         SysTryCatch(NID_SYS, __alarmList.ContainsKey(alarmId) == true, r = E_OBJ_NOT_FOUND, r, "There is no registered alarm.");
448
449         r = __alarmList.Remove(alarmId);
450         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to remove reserved alarmImpl instance.");
451
452         r = RemoveAlarmList(alarmId.ToInt());
453         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to remove reserved alarm list.");
454
455 CATCH:
456         ret = pthread_mutex_unlock(&__lock);
457         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It is failed to unlock mutex.");
458         return r;
459 }
460
461 result
462 _AlarmManager::UpdateAlarm(_AlarmImpl* pAlarmImpl)
463 {
464         result r = E_SUCCESS;
465         Integer alarmId;
466         SysTryReturnResult(NID_SYS, pAlarmImpl != null, E_SYSTEM, "There is no alarmImpl.");
467
468         alarmId.value = pAlarmImpl->__alarmId.value;
469         SysTryReturnResult(NID_SYS, __alarmList.ContainsKey(alarmId) == true, E_SYSTEM, "There is no registered alarm.");
470
471         r = UnregisterAlarm(pAlarmImpl);
472         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to unregister reserved alarm list.");
473
474         r = RegisterAlarm(pAlarmImpl);
475         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to register alarm.");
476         return r;
477 }
478
479 void
480 _AlarmManager::OnAlarmExpired(int alarmId)
481 {
482         result r = E_SUCCESS;
483         int ret = 0;
484         String* pAppId = null;
485         Integer* pPeriod = null;
486         String alarmAppId;
487         std::unique_ptr<IEnumerator> pValueEnum(null);
488         Integer reservedAlarmId(alarmId);
489         DateTime* endTime = null;
490         _AlarmImpl* pAlarmImpl = null;
491
492         SysTryCatch(NID_SYS, __pAlarmHashMap != null, r = E_SYSTEM, r, "Alarm list does not initialized");
493
494         ret = pthread_mutex_lock(&__lock);
495         SysTryReturnVoidResult(NID_SYS, ret == 0, E_SYSTEM, "It is failed to lock mutex.");
496
497         pValueEnum.reset(__pAlarmHashMap->GetValuesN(reservedAlarmId));
498
499         SysLog(NID_SYS, "Alarm expire event is delivered. alarm id is %d", alarmId);
500
501         pAlarmImpl = (_AlarmImpl*)__alarmList.GetValue(reservedAlarmId);
502         if(pAlarmImpl == null)
503         {
504                 SysLog(NID_SYS, "pAlarmImpl of reserved alarm[%d] is not found.", alarmId);
505         }
506
507         if(pValueEnum != null)
508         {
509                 SysLog(NID_SYS, "Matching Alarm Id is %d \n", alarmId);
510
511                 r = pValueEnum->MoveNext();
512                 SysTryCatch(NID_SYS,  r == E_SUCCESS, r = E_SYSTEM, r, "Alarm enum value is not valid.");
513                 pAppId = static_cast< String* >(pValueEnum->GetCurrent());
514                 alarmAppId.Append(*pAppId);
515                 r = pValueEnum->MoveNext();
516                 pPeriod = static_cast< Integer* >(pValueEnum->GetCurrent());
517                 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "Alarm enum value is not valid.");
518
519                 if(pValueEnum->MoveNext() == E_SUCCESS)
520                 {
521                         endTime = static_cast< DateTime* >(pValueEnum->GetCurrent());
522                 }
523
524                 SysLog(NID_SYS, "Reserved Alarm AppId:%ls, Period:%d\n", pAppId->GetPointer(), pPeriod->ToInt());
525
526                 if (pPeriod->ToInt() > 0)
527                 {
528                         if(endTime != null)
529                         {
530                                 DateTime currentTime;
531
532                                 SystemTime::GetCurrentTime(WALL_TIME, currentTime);
533                                 currentTime.AddMilliseconds(-1 * currentTime.GetMillisecond()); //Remove millisecond
534                                 currentTime.AddMinutes(pPeriod->ToInt());
535                                 SysLog(NID_SYS, "Next time[%d min]: %d:%d:%d:%d", pPeriod->ToInt(), currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond(), currentTime.GetMillisecond());
536                                 SysLog(NID_SYS, "Endtime exists %d:%d:%d:%d", endTime->GetHour(), endTime->GetMinute(), endTime->GetSecond(), endTime->GetMillisecond());
537
538                                 if (currentTime.CompareTo(*endTime) > 0)
539                                 {
540                                         SysLog(NID_SYS, "Next time is greater than end time.");
541                                         RemoveAlarmList(alarmId);
542                                         pValueEnum->Reset();
543
544                                         if(pAlarmImpl != null)
545                                         {
546                                                 pAlarmImpl->__alarmId.value = 0;
547                                         }
548                                 }
549                         }
550                 }
551                 else if (pPeriod->ToInt() == 0)
552                 {
553                         RemoveAlarmList(alarmId);
554
555                         if(pAlarmImpl != null)
556                         {
557                                 pAlarmImpl->__alarmId.value = 0;
558                         }
559                 }
560                 else
561                 {
562                         pValueEnum->Reset();
563                 }
564
565         }
566
567         ret = pthread_mutex_unlock(&__lock);
568
569         if(pAlarmImpl != null)
570         {
571                 pAlarmImpl->OnAlarmExpired(alarmId);
572         }
573
574         if(__pAlarmHashMap->ContainsKey(reservedAlarmId) == false)
575         {
576                 ret = pthread_mutex_lock(&__lock);
577                 SysLog(NID_SYS, "Remove an alarm list[%d].", reservedAlarmId.value);
578                 r = __alarmList.Remove(reservedAlarmId);
579                 ret = pthread_mutex_unlock(&__lock);
580                 SetLastResult(r);
581         }
582
583         return;
584
585 CATCH:
586         ret = pthread_mutex_unlock(&__lock);
587         SysTryReturnVoidResult(NID_SYS, ret == 0, E_SYSTEM, "It is failed to unlock mutex.");
588 }
589
590 } } // Tizen::System