Tizen 2.1 base
[platform/framework/native/app-service.git] / src / FSys_AlarmService.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_AlarmService.cpp
20  * @brief               This is the implementation file for _AlarmService class.
21  */
22
23 #include <unique_ptr.h>
24 #include <new>
25 #include <appfw/app.h>
26
27 #include <FBaseRtLibrary.h>
28 #include <FBaseSysLog.h>
29
30 #include <FSec_AccessController.h>
31 #include <FSys_SystemTimeImpl.h>
32 #include <FAppPkg_PackageManagerImpl.h>
33 #include "FApp_CommunicationDispatcher.h"
34 #include "FSys_AlarmService.h"
35
36 using namespace std;
37
38 using namespace Tizen::App;
39 using namespace Tizen::App::Package;
40 using namespace Tizen::Io;
41 using namespace Tizen::System;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Runtime;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Security;
46
47 namespace {
48         const String _ALARM_ALARM_SERVICE_ID = L"osp.alarm.service";
49         const String _ALARM_SET_SINGLEALARM = L"osp.alarm.set.singlealarm";
50         const String _ALARM_SET_REPEATEDALARM = L"osp.alarm.set.repeatedalarm";
51         const String _ALARM_CANCEL = L"osp.alarm.cancel";
52         const String _ALARM_NONE = L"osp.alarm.none";
53         const String _ALARM_EXPIRY_EVENT = L"osp.alarm.expired.callback";
54         const String _ALARM_RESULT_OK = L"osp.alarm.result.ok";
55         const String _ALARM_RESULT_ERROR = L"osp.alarm.result.error";
56
57         static const long _SECOND_OF_MINUTE = 60;
58         static const long _ADJUST_MONTH_FOR_TM = 1;
59         static const long _ADJUST_YEAR_FOR_TM = 1900;
60         static const int _ALARM_SERVICE_ID = 0;
61         static const int _ALARM_COMMAND_ID = 1;
62         static const int _ALARM_CANCEL_ID = 2;
63         static const int _ALARM_START_TIME = 2;
64         static const int _ALARM_PERIOD = 3;
65         static const int _ALARM_END_TIME = 4;
66
67         static const wchar_t* ALARM_PLUGIN_LIBRARY_PATH = L"/opt/apps/aospd00043/lib/libosp-cond-alarm.so";
68 }
69
70 Tizen::System::_AlarmService* Tizen::System::_AlarmService::__pAlarmService = null;
71 typedef void (*OnAlarmForLaunch)(int alarmId);
72
73 _AlarmService::_AlarmService()
74         : _ICommunicationRequestListener()
75         , __pCommunicationDispatcher(null)
76         , __pAlarmHashMap(null)
77         , __endTime()
78 {
79         result r = E_SUCCESS;
80
81         __pCommunicationDispatcher = _CommunicationDispatcher::GetInstance();
82         SysTryCatch(NID_SYS, __pCommunicationDispatcher != null, r = E_SYSTEM, E_SYSTEM, "_CommunicationDispatcher initiate is failed");
83
84         r = __pCommunicationDispatcher->AddCommunicationEventListener(*this);
85         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "fail to add event listener");
86
87         if (!__pAlarmHashMap)
88         {
89                 __pAlarmHashMap = new (std::nothrow) MultiHashMap();
90                 SysTryCatch(NID_SYS, __pAlarmHashMap != null, r = E_SYSTEM, E_SYSTEM, "__pAlarmHashMap should not be null");
91
92                 r = __pAlarmHashMap->Construct();
93                 SysTryCatch(NID_SYS, !IsFailed(r), r= E_SYSTEM, E_SYSTEM, "[%s] Propagated.", GetErrorMessage(r));
94         }
95
96         //Checks for any alarm existence
97         alarm_cancel_all();
98
99 CATCH:
100         SetLastResult(r);
101 }
102
103 _AlarmService::~_AlarmService()
104 {
105         result r = E_SUCCESS;
106
107         SysTryCatch(NID_SYS, __pCommunicationDispatcher != null, r = E_SYSTEM, E_SYSTEM, "_CommunicationDispatcher is not ready");
108
109         r = __pCommunicationDispatcher->RemoveCommunicationEventListener(*this);
110         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "fail to remove event listener");
111
112         SysLog(NID_SYS, "Alarm Service destruct to cancel all alarms.");
113
114         //Cancel all the alarms before exiting
115         alarm_cancel_all();
116
117         delete __pAlarmHashMap;
118         __pAlarmHashMap = null;
119
120 CATCH:
121         SetLastResult(r);
122 }
123
124 _AlarmService*
125 _AlarmService::GetInstance(void)
126 {
127         if(__pAlarmService == null)
128         {
129                 __pAlarmService = new (std::nothrow) _AlarmService();
130         }
131         return __pAlarmService;
132 }
133
134 String
135 _AlarmService::GetId(void)
136 {
137         return _ALARM_ALARM_SERVICE_ID;
138 }
139
140 int
141 _AlarmService::ReserveAlarm(Tizen::Base::DateTime startTime, int period)
142 {
143         int reservedAlarmId = -1;
144         struct tm expireTime = {0,};
145         result r = E_SUCCESS;
146
147         int ret = 0;
148         char* packageName = null;
149         service_h service;
150
151         SysLog(NID_SYS, "Reserve time is %d/%d/%d %d:%d:%d", startTime.GetYear(), startTime.GetMonth(), startTime.GetDay(), startTime.GetHour(), startTime.GetMinute(), startTime.GetSecond());
152
153         expireTime.tm_year = startTime.GetYear() - _ADJUST_YEAR_FOR_TM;
154         expireTime.tm_mon = startTime.GetMonth() - _ADJUST_MONTH_FOR_TM;
155         expireTime.tm_mday = startTime.GetDay();
156         expireTime.tm_hour = startTime.GetHour();
157         expireTime.tm_min = startTime.GetMinute();
158         expireTime.tm_sec = startTime.GetSecond();
159
160         ret = service_create(&service);
161         SysTryCatch(NID_SYS, ret == SERVICE_ERROR_NONE, r = E_SYSTEM, r, "Failed to create service");
162
163         ret = app_get_package(&packageName);
164         SysTryCatch(NID_SYS, ret == APP_ERROR_NONE, r = E_SYSTEM, r, "Failed to get package name");
165
166         ret = service_set_operation(service, "osp.appsvc.operation.ALARM");
167         SysTryCatch(NID_SYS, ret == SERVICE_ERROR_NONE, r = E_SYSTEM, r, "Failed to set operation");
168
169         ret = service_set_package(service, packageName);
170         SysTryCatch(NID_SYS, ret == SERVICE_ERROR_NONE, r = E_SYSTEM, r, "Failed to create service for %s", packageName);
171
172         ret = alarm_schedule_at_date(service, &expireTime, period * _SECOND_OF_MINUTE, &reservedAlarmId);
173         SysTryCatch(NID_SYS, ret == ALARM_ERROR_NONE, r = E_SYSTEM, r, "Failed to set the Alarm time for %s", packageName);
174
175         SysLog(NID_SYS, "Alarm is reserved for %s. AlarmId is %d.", packageName, reservedAlarmId);
176
177 CATCH:
178         if(r == E_SYSTEM)
179         {
180                 if(packageName != null)
181                 {
182                         SysLog(NID_SYS, "Fail to reserve Alarm for %s", packageName);
183                 }
184                 reservedAlarmId = -1;
185         }
186
187         if(packageName != null)
188         {
189                 free (packageName);
190         }
191
192         if(service != null)
193         {
194                 service_destroy(service);
195         }
196
197         return reservedAlarmId;
198 }
199
200 result
201 _AlarmService::AddAlarmList(int alarmId, int period, String appId, DateTime* endTime)
202 {
203         result r = E_SUCCESS;
204
205         SysTryReturnResult(NID_SYS, __pAlarmHashMap != null, E_SYSTEM, "Alarm list does not initialized");
206         SysLog(NID_SYS, "New Alarm Id is added (%d, %d, %ls)", alarmId, period, appId.GetPointer());
207
208         Integer* reservedAlarmId = null;
209         Integer* alarmPeriod = null;
210         String* alarmAppId = null;
211
212         Integer* reverseAlarmId = null;
213         String* reverseAppId = null;
214
215         reservedAlarmId = new (std::nothrow) Integer(alarmId);
216         SysTryCatch(NID_SYS, reservedAlarmId != null, r = E_SYSTEM, E_SYSTEM, "reservedAlarmId should not be null");
217
218         alarmPeriod = new (std::nothrow) Integer(period);
219         SysTryCatch(NID_SYS, alarmPeriod != null, r = E_SYSTEM, E_SYSTEM, "alarmPeriod should not be null");
220
221         alarmAppId = new (std::nothrow) String(appId);
222         SysTryCatch(NID_SYS, alarmAppId != null, r = E_SYSTEM, E_SYSTEM, "alarmAppId should not be null");
223
224         //Forward
225         r = __pAlarmHashMap->Add(*reservedAlarmId, *alarmAppId);
226         SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm app id on the alarm list");
227
228         r = __pAlarmHashMap->Add(*reservedAlarmId, *alarmPeriod);
229         SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm period on the alarm list");
230
231         if(endTime != null)
232         {
233                 DateTime* alarmEndTime = new DateTime(*endTime);
234
235                 SysLog(NID_SYS, "Endtime is exist %d:%d:%d", alarmEndTime->GetHour(), alarmEndTime->GetMinute(), alarmEndTime->GetSecond());
236                 r = __pAlarmHashMap->Add(*reservedAlarmId, *alarmEndTime);
237                 SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm endtime on the alarm list");
238         }
239
240         //Reverse
241         reverseAlarmId = new (std::nothrow) Integer(alarmId);
242         SysTryCatch(NID_SYS, reverseAlarmId != null, r = E_SYSTEM, E_SYSTEM, "reverseAlarmId should not be null");
243
244         reverseAppId = new (std::nothrow) String(appId);
245         SysTryCatch(NID_SYS, reverseAppId != null, r = E_SYSTEM, E_SYSTEM, "reverseAppId should not be null");
246
247         r = __pAlarmHashMap->Add(*reverseAppId, *reverseAlarmId);
248         SysTryCatch(NID_SYS, r == E_SUCCESS, r, r, "Fail to add new alarm on the alarm list");
249 CATCH:
250         if(r != E_SUCCESS)
251         {
252                 if(reservedAlarmId != null)
253                 {               
254                         __pAlarmHashMap->Remove(*reservedAlarmId, false);
255                         delete reservedAlarmId;
256                 }
257
258                 if(alarmPeriod != null)
259                 {
260                         delete alarmPeriod;
261                 }
262
263                 if(alarmAppId != null)
264                 {
265                         delete alarmAppId;
266                 }
267
268                 if(reverseAppId != null)
269                 {
270                         __pAlarmHashMap->Remove(*reverseAppId, false);
271                         delete reverseAppId;
272                 }
273
274                 if(reverseAlarmId != null)
275                 {
276                         delete reverseAlarmId;
277                 }
278         }
279         return r;
280 }
281
282 result
283 _AlarmService::RemoveAlarmList(int alarmId)
284 {
285         result r = E_SUCCESS;
286         Integer requiredAlarmId(alarmId);
287         unique_ptr<IEnumerator>pValueEnum(null);
288
289         SysTryReturnResult(NID_SYS, __pAlarmHashMap != null, E_SYSTEM, "Alarm list does not initialized");
290
291         alarm_cancel(alarmId);
292         SysLog(NID_SYS, "AlarmId %d disarms the timer.", alarmId);
293
294         pValueEnum.reset(__pAlarmHashMap->GetValuesN(requiredAlarmId));
295
296         if(pValueEnum != null)
297         {
298                 SysLog(NID_SYS, "Reserved Alarms are exist");
299                 String* alarmAppId = null;
300                 r = pValueEnum->MoveNext();
301                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Reserved Alarm List is not valid.");
302                 alarmAppId = static_cast< String* >(pValueEnum->GetCurrent());
303
304                 if(alarmAppId != null)
305                 {
306                         SysLog(NID_SYS, "Alarm AppId is %S", alarmAppId->GetPointer());
307                         unique_ptr<IEnumerator>pAlarmEnum(__pAlarmHashMap->GetValuesN(*alarmAppId));
308                         if(pAlarmEnum != null)
309                         {
310                                 while(pAlarmEnum->MoveNext() == E_SUCCESS)
311                                 {
312                                         Integer* reverseAlarmId = static_cast< Integer* >(pAlarmEnum->GetCurrent());
313                                         if(reverseAlarmId != null)
314                                         {
315                                                 SysLog(NID_SYS, "Reserved Alarm is %d", reverseAlarmId->ToInt());
316                                                 if(reverseAlarmId->ToInt() == alarmId)
317                                                 {
318                                                         SysLog(NID_SYS, "Remove reservedAlarmId for reverse look-up");
319                                                          __pAlarmHashMap->Remove(*alarmAppId, *reverseAlarmId);
320                                                         delete reverseAlarmId;
321                                                         break;
322                                                 }
323                                         }
324                                 }
325                         }
326
327                         int count = 0;
328                         __pAlarmHashMap->GetCount(*alarmAppId, count);
329                         if(count == 0)
330                         {
331                                 SysLog(NID_SYS, "There is no reserved alarm for AppId:%S", alarmAppId->GetPointer());
332                                 __pAlarmHashMap->Remove(*alarmAppId, true);
333                         }
334                 }
335         }
336         r = __pAlarmHashMap->Remove(requiredAlarmId, true);
337         return r;
338 }
339
340 result
341 _AlarmService::RemoveAlarmList(String appId)
342 {
343         result r = E_SUCCESS;
344         Integer requiredAlarmId;
345         IEnumerator *pValueEnum = null;
346
347         SysTryReturnResult(NID_SYS, __pAlarmHashMap != null, E_SYSTEM, "Alarm list does not initialized");
348         pValueEnum = __pAlarmHashMap->GetValuesN(appId);
349
350         if(pValueEnum != null)
351         {
352                 SysLog(NID_SYS, "Reserved Alarms are exist for AppId:%S", appId.GetPointer());
353                 while(pValueEnum->MoveNext() == E_SUCCESS)
354                 {
355                         Integer* reverseAlarmId = static_cast< Integer* >(pValueEnum->GetCurrent());
356                         if(reverseAlarmId != null)
357                         {
358                                 SysLog(NID_SYS, "Reserved AlarmId is %d", reverseAlarmId->ToInt());
359                                 alarm_cancel(reverseAlarmId->ToInt());
360                                 __pAlarmHashMap->Remove(*reverseAlarmId, true);
361                         }
362                 }
363                 delete pValueEnum;
364         }
365         r = __pAlarmHashMap->Remove(appId, true);
366        
367         return r;
368 }
369
370 void
371 _AlarmService::OnRequestOccured(AppId appId, ArrayList* request, ArrayList* response)
372 {
373         result r = E_SUCCESS;
374
375         String* serviceId = null;
376         String* commandId = null;
377         String* result = null;
378         int reservedAlarmId = 0;
379
380         SysTryReturnVoidResult(NID_SYS, request != null && response != null, E_INVALID_ARG, "This is critical issue. Parameters are null");
381
382         serviceId = new (std::nothrow) String(_ALARM_ALARM_SERVICE_ID);
383         SysTryReturnVoidResult(NID_SYS, serviceId != null, E_SYSTEM, "This is critical issue. serviceId should not be null");
384
385         String* alarmCommand = null;
386
387         alarmCommand = (String*)request->GetAt(_ALARM_COMMAND_ID);
388         SysTryCatch(NID_SYS, alarmCommand != null, r = E_INVALID_ARG, E_INVALID_ARG, "Parameters has no command %x", alarmCommand);
389
390         if(*alarmCommand == _ALARM_SET_SINGLEALARM || *alarmCommand == _ALARM_SET_REPEATEDALARM)
391         {
392                 SysLog(NID_SYS, "Alarm setting request");
393                 int period = 0;
394
395                 if(*alarmCommand == _ALARM_SET_SINGLEALARM)
396                 {
397                         SysLog(NID_SYS, "Single Alarm setting request");
398                         commandId = new (std::nothrow) String(_ALARM_SET_SINGLEALARM);
399                         SysTryCatch(NID_SYS, commandId != null, r = E_SYSTEM, E_SYSTEM, "commandId should not be null");
400                 }
401                 else
402                 {
403                         SysLog(NID_SYS, "Repeated Alarm setting request");
404                         commandId = new (std::nothrow) String(_ALARM_SET_REPEATEDALARM);
405                         SysTryCatch(NID_SYS, commandId != null, r = E_SYSTEM, E_SYSTEM, "commandId should not be null");
406
407                         String* periodArg = (String*)request->GetAt(_ALARM_PERIOD);
408                         SysTryCatch(NID_SYS, commandId != null, r = E_SYSTEM, E_SYSTEM, "Period should not be null");
409                         Integer::Parse(*periodArg, period);
410                         SysLog(NID_SYS, "period value parse done");
411                 }
412
413                 DateTime startTime;
414                 DateTime currentTime;
415                 DateTime endTime;
416
417                 String* startTimeArg = (String*)request->GetAt(_ALARM_START_TIME);
418                 SysLog(NID_SYS, "Alarm requested start time is %S", startTimeArg->GetPointer());
419
420                 DateTime::Parse(*startTimeArg, startTime);
421                 SysLog(NID_SYS, "Starttime Parse result: %d-%d-%d %d:%d:%d", startTime.GetYear(), startTime.GetMonth(), startTime.GetDay(), startTime.GetHour(), startTime.GetMinute(), startTime.GetSecond());
422
423                 String* endTimeArg = (String*)request->GetAt(_ALARM_END_TIME);
424
425                 if(endTimeArg != null)
426                 {
427                         DateTime::Parse(*endTimeArg, endTime);
428                         SysLog(NID_SYS, "Endtime Parse result: %d-%d-%d %d:%d:%d", startTime.GetYear(), startTime.GetMonth(), startTime.GetDay(), startTime.GetHour(), startTime.GetMinute(), startTime.GetSecond());
429                 }
430
431                 r = _SystemTimeImpl::GetCurrentTime(WALL_TIME, currentTime);
432                 SysTryCatch(NID_SYS, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "Failed to get current Time.");
433
434                 SysTryCatch(NID_SYS, startTime.CompareTo(currentTime) > 0, r = E_SYSTEM, E_SYSTEM, "Requested time has to be greater then current time: %d-%d-%d %d:%d:%d", currentTime.GetYear(), currentTime.GetMonth(), currentTime.GetDay(), currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond());
435
436                 reservedAlarmId = ReserveAlarm(startTime, period);
437
438                 SysLog(NID_SYS, "Reserved AppId %S", appId.GetPointer());
439                 if(reservedAlarmId != -1)
440                 {
441                         if(endTimeArg == null)
442                         {
443                                 r = AddAlarmList(reservedAlarmId, period, appId, null);
444                         }
445                         else
446                         {
447                                 r = AddAlarmList(reservedAlarmId, period, appId, &endTime);
448                         }
449                 }
450                 else
451                 {
452                         r = E_SYSTEM;
453                 }
454         
455         }
456         else if(*alarmCommand == _ALARM_CANCEL)
457         {
458                 SysLog(NID_SYS, "Alarm Cancel request");
459
460                 int alarmId = 0;
461
462                 commandId = new (std::nothrow) String(_ALARM_CANCEL);
463                 SysTryCatch(NID_SYS, commandId != null, r = E_SYSTEM, E_SYSTEM, "commandId should not be null");
464
465                 String* removableAlarmId = (String*)request->GetAt(_ALARM_CANCEL_ID);
466
467                 r = Integer::Parse(*removableAlarmId, alarmId);
468                 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r,  "Failed to parse alarm id");
469
470                 r = RemoveAlarmList(alarmId);
471                 SysTryCatch(NID_SYS, r == E_SUCCESS, r, r,  "Failed to parse alarm id");
472
473         }
474         else
475         {
476                 commandId = new (std::nothrow) String(_ALARM_NONE);
477                 SysTryCatch(NID_SYS, commandId != null, r = E_SYSTEM, E_SYSTEM, "commandId should not be null");
478                 r = E_SYSTEM;
479         }
480
481 CATCH:
482         response->Add(*serviceId);
483
484         if(r == E_SUCCESS)
485         {
486                 result = new (std::nothrow) String(_ALARM_RESULT_OK);
487                 response->Add(*commandId);
488                 response->Add(*result);
489                 if(reservedAlarmId != -1)
490                 {
491                         String* tempAlarmId = new (std::nothrow) String();
492                         tempAlarmId->Append(reservedAlarmId);
493                         response->Add(tempAlarmId);
494                 }
495         }
496         else if(r == E_SYSTEM)
497         {
498                 result = new (std::nothrow) String(_ALARM_RESULT_ERROR);
499                 response->Add(*commandId);
500                 response->Add(*result);
501         }
502         else if(r == E_OBJ_NOT_FOUND)
503         {
504                 result = new (std::nothrow) String(_ALARM_RESULT_ERROR);
505                 response->Add(*commandId);
506                 response->Add(*result);
507         }
508         else
509         {
510                 result = new (std::nothrow) String(_ALARM_RESULT_ERROR);
511                 if(commandId == null)
512                 {
513                         commandId = new (std::nothrow) String(_ALARM_NONE);
514                 }
515                 response->Add(*commandId);
516                 response->Add(*result);
517         }
518
519         SetLastResult(r);
520 }
521
522 void
523 _AlarmService::OnAlarmExpired(int alarmId)
524 {
525         result r = E_SUCCESS;
526         String* pAppId = null;
527         Integer* pPeriod = null;
528         unique_ptr<IEnumerator> pValueEnum(null);
529         Integer requiredAlarmId(alarmId);
530         DateTime* endTime = null;
531
532         SysTryReturnVoidResult(NID_SYS, __pAlarmHashMap != null, E_SYSTEM, "Alarm list does not initialized");
533
534         pValueEnum.reset(__pAlarmHashMap->GetValuesN(requiredAlarmId));
535
536         if(pValueEnum != null)
537         {
538                 String alarmAppId;
539                 SysLog(NID_SYS, "Matching Alarm Id is %d \n", alarmId);
540                 r = pValueEnum->MoveNext();
541                 SysTryReturnVoidResult(NID_SYS,  r == E_SUCCESS, E_SYSTEM, "Alarm enum value is not valid.");
542                 pAppId = static_cast< String* >(pValueEnum->GetCurrent());
543                 alarmAppId.Append(*pAppId);
544                 r = pValueEnum->MoveNext();
545                 pPeriod = static_cast< Integer* >(pValueEnum->GetCurrent());
546                 SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Alarm enum value is not valid.");
547
548                 if(pValueEnum->MoveNext() == E_SUCCESS)
549                 {
550                         endTime = static_cast< DateTime* >(pValueEnum->GetCurrent());
551                 }
552
553                 SysLog(NID_SYS, "Reserved Alarm AppId:%ls, Period:%d\n", pAppId->GetPointer(), pPeriod->ToInt());
554
555                 if (pPeriod->ToInt() > 0)
556                 {
557                         if(endTime != null)
558                         {
559                                 DateTime currentTime;
560                                 _SystemTimeImpl::GetCurrentTime(WALL_TIME, currentTime);
561                                 SysLog(NID_SYS, "Current time: %d:%d:%d", currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond());
562                                 currentTime.AddMinutes(pPeriod->ToInt());
563                                 SysLog(NID_SYS, "Next time: %d:%d:%d", currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond());
564                                 SysLog(NID_SYS, "Endtime is exist %d:%d:%d", endTime->GetHour(), endTime->GetMinute(), endTime->GetSecond());
565
566                                 if (currentTime.CompareTo(*endTime) >= 0)
567                                 {
568                                         SysLog(NID_SYS, "Next time is greater then end time.");
569                                         RemoveAlarmList(alarmId);
570                                         pValueEnum->Reset();
571                                 }
572                         }
573                 }
574                 else if (pPeriod->ToInt() == 0)
575                 {
576                         RemoveAlarmList(alarmId);
577                 }
578                 else
579                 {
580                         pValueEnum->Reset();
581                         return;
582                 }
583
584                 SysLog(NID_SYS, "Alarm Id match and need to call IPC callback. (%S)", alarmAppId.GetPointer());
585                 ArrayList data;
586                 String serviceId(_ALARM_ALARM_SERVICE_ID);
587                 String commandId(_ALARM_EXPIRY_EVENT);
588                 String expiredAlarmId(requiredAlarmId.ToString());
589                 data.Add(serviceId);
590                 data.Add(commandId);
591                 data.Add(expiredAlarmId);
592
593                 r = __pCommunicationDispatcher->SendData(alarmAppId, data);
594                 SysLog(NID_SYS, "Alarm expire event is delivered to %S with %S, Result: %s",alarmAppId.GetPointer(), expiredAlarmId.GetPointer(), GetErrorMessage(r));
595         }
596         else
597         {
598                 SysLog(NID_SYS, "AlarmObj2 entry for alarm id not found: %d", alarmId);
599
600                 //For conditional launch
601                 SysLog(NID_SYS, "Start to load external lib");
602                 Library lib;
603                 OnAlarmForLaunch pOnAlarmForLaunch = null;
604                 r = lib.Construct(ALARM_PLUGIN_LIBRARY_PATH);
605
606                 if(r == E_SUCCESS)
607                 {
608                         SysLog(NID_SYS, "Open alarm condition library");
609                         pOnAlarmForLaunch = (OnAlarmForLaunch)lib.GetProcAddress(L"OnAlarmForLaunch");
610                         if(pOnAlarmForLaunch != null)
611                         {
612                                 SysLog(NID_SYS, "Function is found");
613                                 pOnAlarmForLaunch(alarmId);
614                                 SysLog(NID_SYS, "Requested to check current alarm id to AlarmConditionHandler %d", alarmId);
615                         }
616                         else
617                         {
618                                 SysLog(NID_SYS, "Fail to find alarm function");
619                         }
620                 }
621                 else
622                 {
623                         SysLog(NID_SYS, "Fail to open alarm condition library");
624                 }
625         }
626 }
627
628 void
629 _AlarmService::OnApplicationTerminated(const AppId& appId, _AppType type)
630 {
631         SysLog(NID_SYS, "Terminated AppId %S", appId.GetPointer());
632         RemoveAlarmList(appId);
633 }
634