upload tizen1.0 source
[framework/appfw/alarm-manager.git] / alarm-manager.c
1 /*
2  *  alarm-manager
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Venkatesha Sarpangala <sarpangala.v@samsung.com>, Jayoun Lee <airjany@samsung.com>,
7  * Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24
25 #define _BSD_SOURCE /*for localtime_r*/
26 #include<stdio.h>
27 #include<stdlib.h>
28 #include<time.h>
29 #include<signal.h>
30 #include<string.h>
31 #include<sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include<dbus/dbus.h>
36 #include<dbus/dbus-glib-lowlevel.h>
37 #include<glib.h>
38 #include <glib/gmacros.h>
39
40 /*#include "global-gconf.h"*/
41 #include"alarm.h"
42 #include"alarm-internal.h"
43
44 /*#include"limo-event-delivery.h"*/
45
46 #include <aul.h>
47 #include <bundle.h>
48 #include <heynoti.h>
49 #include <security-server.h>
50 #include <db-util.h>
51 #include <vconf.h>
52 #include <vconf-keys.h>
53
54 #define SIG_TIMER 0x32
55 #define WAKEUP_ALARM_APP_ID       "org.tizen.alarm.ALARM"
56         /* alarm ui application's alarm's dbus_service name instead of 21
57            (alarm application's app_id) value */
58 #define WAKEUP_ALARMBOOTING_APP_ID      "org.tizen.alarmboot.ui.ALARM"
59 /*alrmbooting ui application's dbus_service name instead of 121(alarmbooting
60         application's app_id) value */
61 /*
62 #include "tapi_misc_ext.h"
63 #include "TelMisc.h"
64 #include "tapi_misc_data.h"
65 #include "tapi_power.h"
66 */
67
68 #include "pmapi.h"
69
70 __alarm_server_context_t alarm_context;
71 bool g_dummy_timer_is_set = FALSE;
72
73 GSList *g_scheduled_alarm_list = NULL;
74
75 GSList *g_expired_alarm_list = NULL;
76
77 #ifdef __ALARM_BOOT
78 bool enable_power_on_alarm;
79 bool alarm_boot;
80 time_t ab_due_time = -1;
81 bool poweron_alarm_expired = false;
82 #endif
83
84 /*      2008. 6. 3 sewook7.park
85        When the alarm becoms sleeping mode, alarm timer is not expired.
86        So using RTC, phone is awaken before alarm rings.
87 */
88 #define __WAKEUP_USING_RTC__
89 #ifdef __WAKEUP_USING_RTC__
90 #include<errno.h>
91 #include <linux/rtc.h>
92 #include <sys/ioctl.h>
93 #include <fcntl.h>
94
95 static const char default_rtc[] = "/dev/rtc1";
96 static const char power_rtc[] = "/dev/rtc0";
97
98 #endif                          /*__WAKEUP_USING_RTC__*/
99
100 static bool __alarm_add_to_list(__alarm_info_t *__alarm_info,
101                                 alarm_id_t *alarm_id);
102 static bool __alarm_update_in_list(int pid, alarm_id_t alarm_id,
103                                    __alarm_info_t *__alarm_info,
104                                    int *error_code);
105 static bool __alarm_remove_from_list(int pid, alarm_id_t alarm_id,
106                                      int *error_code);
107 static bool __alarm_set_start_and_end_time(alarm_info_t *alarm_info,
108                                            __alarm_info_t *__alarm_info);
109 static bool __alarm_update_due_time_of_all_items_in_list(double diff_time);
110 static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
111                            int pid, char *app_service_name, char *app_service_name_mod,
112                            const char *dst_service_name, const char *dst_service_name_mod, int *error_code);
113 static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
114                            int pid, char *bundle_data, int *error_code);
115
116 static bool __alarm_delete(int pid, alarm_id_t alarm_id, int *error_code);
117 static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
118                            alarm_info_t *alarm_info, int *error_code);
119 static bool __alarm_power_on(int app_id, bool on_off, int *error_code);
120 static bool __alarm_power_off(int app_id, int *error_code);
121 static bool __alarm_check_next_duetime(int app_id, int *error_code);
122 static void __alarm_send_noti_to_application(const char *app_service_name,
123                                              alarm_id_t alarm_id);
124 static void __alarm_expired();
125 static gboolean __alarm_handler_idle();
126 static void __alarm_handler(int sigNum, siginfo_t *pSigInfo, void *pUContext);
127 static void __clean_registry();
128 static bool __alarm_manager_reset();
129 static void __on_system_time_changed(keynode_t *node, void *data);
130 static void __initialize_timer();
131 static void __initialize_alarm_list();
132 static void __initialize_scheduled_alarm_lsit();
133 static void __hibernation_leave_callback();
134 static bool __initialize_noti();
135
136 static bool __initialize_dbus();
137 static bool __initialize_db();
138 static void __initialize();
139 static bool __check_false_alarm();
140 static DBusHandlerResult __alarm_server_filter(DBusConnection *connection,
141                                                DBusMessage *message,
142                                                void *user_data);
143
144 static void __rtc_set()
145 {
146 #ifdef __WAKEUP_USING_RTC__
147
148         const char *rtc = default_rtc;
149         int fd = 0;
150         struct rtc_time rtc_tm;
151         struct rtc_wkalrm rtc_wk;
152         struct tm due_tm;
153
154 #ifdef _SIMUL                   /*if build is simulator, we don't need to set
155                                    RTC because RTC does not work in simulator.*/
156         ALARM_MGR_EXCEPTION_PRINT("because it is simulator's mode, "
157                                   "we don't set RTC.\n");
158         return;
159 #endif
160
161         fd = open(rtc, O_RDONLY);
162         if (fd == -1) {
163                 ALARM_MGR_EXCEPTION_PRINT("RTC open failed.\n");
164                 return;
165         }
166
167         /* Read the RTC time/date */
168         int retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
169         if (retval == -1) {
170                 ALARM_MGR_EXCEPTION_PRINT("RTC_RD_TIME ioctl failed");
171                 close(fd);
172                 return;
173         }
174
175         ALARM_MGR_LOG_PRINT("\n\nCurrent RTC date/time is %d-%d-%d, "
176                 "%02d:%02d:%02d.\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1, 
177                 rtc_tm.tm_year + 1900, rtc_tm.tm_hour, rtc_tm.tm_min, 
178                 rtc_tm.tm_sec);
179
180         ALARM_MGR_LOG_PRINT("alarm_context.c_due_time is %d\n", \
181                             alarm_context.c_due_time);
182
183         if (alarm_context.c_due_time != -1) {
184                 time_t due_time = alarm_context.c_due_time - 1;
185                 gmtime_r(&due_time, &due_tm);
186
187                 rtc_tm.tm_mday = due_tm.tm_mday;
188                 rtc_tm.tm_mon = due_tm.tm_mon;
189                 rtc_tm.tm_year = due_tm.tm_year;
190                 rtc_tm.tm_hour = due_tm.tm_hour;
191                 rtc_tm.tm_min = due_tm.tm_min;
192                 rtc_tm.tm_sec = due_tm.tm_sec;
193                 memcpy(&rtc_wk.time, &rtc_tm, sizeof(rtc_tm));
194                 rtc_wk.enabled = 1;
195                 rtc_wk.pending = 0;
196
197                 ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
198                         "%d-%d-%d, %02d:%02d:%02d.\n", rtc_tm.tm_mday, 
199                         rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900, 
200                         rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
201
202                 retval = ioctl(fd, RTC_WKALM_SET, &rtc_wk);
203                 if (retval == -1) {
204                         if (errno == ENOTTY) {
205                                 ALARM_MGR_EXCEPTION_PRINT("\nAlarm IRQs not"
206                                                           "supported.\n");
207                         }
208                         ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
209                         close(fd);
210                         return;
211                 }
212                 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is setted");
213
214                 /* Enable alarm interrupts */
215                 retval = ioctl(fd, RTC_AIE_ON, 0);
216                 if (retval == -1) {
217                         ALARM_MGR_EXCEPTION_PRINT("RTC_AIE_ON ioctl failed");
218                         close(fd);
219                         return;
220                 }
221                 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is on");
222         } else
223                 ALARM_MGR_LOG_PRINT("[alarm-server]alarm_context.c_due_time is"
224                                     "less than 10 sec. RTC alarm does not need to be set\n");
225
226         close(fd);
227
228 #endif                          /* __WAKEUP_USING_RTC__ */
229 }
230
231 int _set_rtc_time(time_t _time)
232 {
233         int fd0 = 0;
234         int fd1 = 0;
235         int retval0 = 0;
236         int retval1 = 0;
237         struct rtc_time rtc_tm;
238         const char *rtc0 = power_rtc;
239         const char *rtc1 = default_rtc;
240         struct tm *_tm;
241         struct tm time_r;
242
243         fd0 = open(rtc0, O_RDONLY);
244         fd1 = open(rtc1, O_RDONLY);
245
246         if (fd0 == -1) {
247                 ALARM_MGR_LOG_PRINT("error to open /dev/rtc0.");
248                 perror("\t");
249         }
250
251         if (fd1 == -1) {
252                 ALARM_MGR_LOG_PRINT("error to open /dev/rtc1.");
253                 perror("\t");
254         }
255
256         memset(&rtc_tm, 0, sizeof(struct rtc_time));
257
258
259         _tm = gmtime_r(&_time, &time_r);
260
261         /* Write the RTC time/date 2008:05:21 19:20:00 */
262
263
264         rtc_tm.tm_mday = time_r.tm_mday;
265         rtc_tm.tm_mon = time_r.tm_mon;
266         rtc_tm.tm_year = time_r.tm_year;
267         rtc_tm.tm_hour = time_r.tm_hour;
268         rtc_tm.tm_min = time_r.tm_min;
269         rtc_tm.tm_sec = 0;
270         
271
272         retval0 = ioctl(fd0, RTC_SET_TIME, &rtc_tm);
273
274         if (retval0 == -1) {
275                 close(fd0);
276                 ALARM_MGR_LOG_PRINT("error to ioctl fd0.");
277                 perror("\t");
278         }
279         close(fd0);
280
281         retval1 = ioctl(fd1, RTC_SET_TIME, &rtc_tm);
282
283         if (retval1 == -1) {
284                 close(fd1);
285                 ALARM_MGR_LOG_PRINT("error to ioctl fd1.");
286                 perror("\t");
287         }
288         close(fd1);
289
290         return 1;
291 }
292
293 bool __alarm_clean_list()
294 {
295         GSList *iter = NULL;
296
297         for (iter = alarm_context.alarms; iter != NULL;
298              iter = g_slist_next(iter)) {
299                 free(iter->data);
300         }
301
302         g_slist_free(alarm_context.alarms);
303         return true;
304 }
305
306 static bool __alarm_add_to_list(__alarm_info_t *__alarm_info,
307                                 alarm_id_t *alarm_id)
308 {
309
310         bool unique_id = false;
311         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
312         __alarm_info_t *entry = NULL;
313
314         GSList *iter = NULL;
315
316         /* FIXME: alarm id must be unique. */
317         __alarm_info->alarm_id = (int)(void *)__alarm_info;
318         ALARM_MGR_LOG_PRINT("__alarm_info->alarm_id is %d", \
319                             __alarm_info->alarm_id);
320
321         while (unique_id == false) {
322                 unique_id = true;
323
324                 for (iter = alarm_context.alarms; iter != NULL;
325                      iter = g_slist_next(iter)) {
326                         entry = iter->data;
327                         if (entry->alarm_id == __alarm_info->alarm_id) {
328                                 __alarm_info->alarm_id++;
329                                 unique_id = false;
330                         }
331                 }
332
333         }
334
335         /* list alarms */
336         ALARM_MGR_LOG_PRINT("[alarm-server]: before add\n");
337         for (iter = alarm_context.alarms; iter != NULL;
338              iter = g_slist_next(iter)) {
339                 entry = iter->data;
340                 /*ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n", 
341                    entry->alarm_id); */
342         }
343
344         alarm_context.alarms =
345             g_slist_append(alarm_context.alarms, __alarm_info);
346         /*list alarms */
347         ALARM_MGR_LOG_PRINT("[alarm-server]: after add\n");
348         for (iter = alarm_context.alarms; iter != NULL;
349              iter = g_slist_next(iter)) {
350                 entry = iter->data;
351                 ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n",
352                                     entry->alarm_id);
353         }
354
355         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
356                 _save_alarms(__alarm_info);
357         }
358
359         *alarm_id = __alarm_info->alarm_id;
360
361         return true;
362 }
363
364 static bool __alarm_update_in_list(int pid, alarm_id_t alarm_id,
365                                    __alarm_info_t *__alarm_info,
366                                    int *error_code)
367 {
368         bool found = false;
369         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
370         GSList *iter = NULL;
371         __alarm_info_t *entry = NULL;
372
373         for (iter = alarm_context.alarms; iter != NULL;
374              iter = g_slist_next(iter)) {
375                 entry = iter->data;
376                 if (entry->alarm_id == alarm_id) {
377
378                         found = true;
379                         __alarm_info->quark_app_unique_name =
380                             entry->quark_app_unique_name;
381                         __alarm_info->quark_dst_service_name =
382                             entry->quark_dst_service_name;
383                         memcpy(entry, __alarm_info, sizeof(__alarm_info_t));
384
385                         break;
386                 }
387         }
388
389         if (!found) {
390                 if (error_code)
391                         *error_code = ERR_ALARM_INVALID_ID;
392                 return false;
393         }
394
395         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
396                 _update_alarms(__alarm_info);
397         }
398
399         return true;
400 }
401
402 static bool __alarm_remove_from_list(int pid, alarm_id_t alarm_id,
403                                      int *error_code)
404 {
405         bool found = false;
406
407         alarm_info_t *alarm_info = NULL;
408
409         GSList *iter = NULL;
410         __alarm_info_t *entry = NULL;
411
412         /*list alarms */
413         ALARM_MGR_LOG_PRINT("[alarm-server]: before del : alarm id(%d)\n",
414                             alarm_id);
415
416         for (iter = alarm_context.alarms; iter != NULL;
417              iter = g_slist_next(iter)) {
418                 entry = iter->data;
419                 if (entry->alarm_id == alarm_id) {
420                         alarm_info = &entry->alarm_info;
421
422                         ALARM_MGR_LOG_PRINT("[alarm-server]: "
423                                             "__alarm_remove_from_list : alarm id(%d)\n",
424                                             entry->alarm_id);
425
426                         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
427                                 _delete_alarms(alarm_id);
428                         }
429
430                         alarm_context.alarms =
431                             g_slist_remove(alarm_context.alarms, iter->data);
432                         found = true;
433                         break;
434                 }
435
436         }
437
438         ALARM_MGR_LOG_PRINT("[alarm-server]: after del\n");
439
440         if (!found) {
441                 if (error_code)
442                         *error_code = ERR_ALARM_INVALID_ID;
443                 return false;
444         }
445
446         return true;
447 }
448
449 static bool __alarm_set_start_and_end_time(alarm_info_t *alarm_info,
450                                            __alarm_info_t *__alarm_info)
451 {
452         alarm_date_t *start = &alarm_info->start;
453         alarm_date_t *end = &alarm_info->end;
454
455         struct tm alarm_tm = { 0, };
456
457         if (start->year != 0) {
458                 alarm_tm.tm_year = start->year - 1900;
459                 alarm_tm.tm_mon = start->month - 1;
460                 alarm_tm.tm_mday = start->day;
461
462                 alarm_tm.tm_hour = 0;
463                 alarm_tm.tm_min = 0;
464                 alarm_tm.tm_sec = 0;
465
466                 __alarm_info->start = mktime(&alarm_tm);
467         } else {
468                 __alarm_info->start = 0;
469         }
470
471         if (end->year != 0) {
472                 alarm_tm.tm_year = end->year - 1900;
473                 alarm_tm.tm_mon = end->month - 1;
474                 alarm_tm.tm_mday = end->day;
475
476                 alarm_tm.tm_hour = 23;
477                 alarm_tm.tm_min = 59;
478                 alarm_tm.tm_sec = 59;
479
480                 __alarm_info->end = mktime(&alarm_tm);
481         } else {
482                 __alarm_info->end = 0;
483         }
484
485         return true;
486 }
487
488 /*
489 static bool alarm_get_tz_info(int *gmt_idx, int *dst)
490 {
491         GConfValue *value1 = NULL;
492         GConfValue *value2 = NULL;
493         GConfClient* gConfClient = NULL;
494         GError* err = NULL;             
495         
496         gConfClient = gconf_client_get_default();
497
498         if(gConfClient) {
499                 value1 = gconf_client_get(gConfClient, SETTINGS_CLOCKTIMEZONE, 
500                                                                         &err);
501                 if (err) {
502                         ALARM_MGR_LOG_PRINT("__on_system_time_changed:
503                         gconf_client_get() failed:
504                         error:[%s]\n", err->message);
505                         g_error_free(err);
506                         err = NULL;
507                 }
508                 *gmt_idx = gconf_value_get_int(value1);
509                 ALARM_MGR_LOG_PRINT("gconf return gmt_idx =%d\n ", *gmt_idx);
510         
511                 value2 = gconf_client_get(gConfClient, 
512                         SETTINGS_DAYLIGHTSTATUS, &err);
513                 if (err) {
514                         ALARM_MGR_LOG_PRINT("__on_system_time_changed:
515                 gconf_client_get() failed: error:[%s]\n", err->message);
516                 g_error_free(err);
517                 err = NULL;
518         }
519         
520         *dst = gconf_value_get_int(value2);
521         ALARM_MGR_LOG_PRINT("gconf return dst =%d\n ", *dst);
522         
523         if(gConfClient != NULL) {
524                 g_object_unref(gConfClient);
525                 gConfClient = NULL;
526                 }
527         }
528         else
529                 ALARM_MGR_LOG_PRINT("check the gconf setting failed!!!!! \n ");
530         
531         if(value1) {
532                 gconf_value_free(value1);
533                 value1 = NULL;
534         }
535         if(value2) {
536                 gconf_value_free(value2);
537                 value2 = NULL;
538         }
539
540         return true;
541 }
542 */
543
544 static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
545 {
546         time_t current_time;
547         time_t min_time = -1;
548         time_t due_time = 0;
549         GSList *iter = NULL;
550         __alarm_info_t *entry = NULL;
551         struct tm *p_time = NULL ;
552         struct tm due_time_result ;
553         struct tm fixed_time ;
554
555         for (iter = alarm_context.alarms; iter != NULL;
556              iter = g_slist_next(iter)) {
557                 entry = iter->data;
558                 alarm_info_t *alarm_info = &(entry->alarm_info);
559                 if (alarm_info->alarm_type & ALARM_TYPE_RELATIVE) {
560                         /*diff_time ÃƒÂ³Â¸Â® */
561
562                         entry->due_time += diff_time;
563
564                         alarm_date_t *start = &alarm_info->start; /**< start 
565                                                         time of the alarm */
566                         alarm_date_t *end = &alarm_info->end;;
567                                                 /**< end time of the alarm */
568
569                         tzset();
570                         p_time = localtime_r(&entry->due_time, &due_time_result);
571                         if (p_time != NULL) {
572                                 start->year = p_time->tm_year + 1900;
573                                 start->month = p_time->tm_mon + 1;
574                                 start->day = p_time->tm_mday;
575                                 start->hour = p_time->tm_hour;
576                                 start->min = p_time->tm_min;
577                                 start->sec = p_time->tm_sec;
578
579                                 end->year = p_time->tm_year + 1900;
580                                 end->month = p_time->tm_mon + 1;
581                                 end->day = p_time->tm_mday;
582
583                                 
584                                 memset(&fixed_time, 0, sizeof(fixed_time));
585                                 fixed_time.tm_year = p_time->tm_year;
586                                 fixed_time.tm_mon = p_time->tm_mon;
587                                 fixed_time.tm_mday = p_time->tm_mday;
588                                 fixed_time.tm_hour = 0;
589                                 fixed_time.tm_min = 0;
590                                 fixed_time.tm_sec = 0;
591                         }
592                         entry->start = mktime(&fixed_time);
593
594                         fixed_time.tm_hour = 23;
595                         fixed_time.tm_min = 59;
596                         fixed_time.tm_sec = 59;
597
598                         entry->end = mktime(&fixed_time);
599
600                         ALARM_MGR_LOG_PRINT("alarm_info->alarm_type is "
601                                             "ALARM_TYPE_RELATIVE\n");
602
603                         _update_alarms(entry);
604                 }
605
606                 _alarm_next_duetime(entry);
607                 ALARM_MGR_LOG_PRINT("entry->due_time is %d\n", entry->due_time);
608         }
609
610         time(&current_time);
611
612         for (iter = alarm_context.alarms; iter != NULL;
613              iter = g_slist_next(iter)) {
614                 entry = iter->data;
615                 due_time = entry->due_time;
616
617                 double interval = 0;
618
619                 ALARM_MGR_LOG_PRINT("alarm[%d] with duetime(%u) at "
620                 "current(%u)\n", entry->alarm_id, due_time, current_time);
621                 if (due_time == 0) {    /* 0 means this alarm has been 
622                                            disabled */
623                         continue;
624                 }
625
626                 interval = difftime(due_time, current_time);
627
628                 if (interval <= 0) {
629                         ALARM_MGR_LOG_PRINT("this may be error.. alarm[%d]\n", \
630                                             entry->alarm_id);
631                         continue;
632                 }
633
634                 interval = difftime(due_time, min_time);
635
636                 if ((interval < 0) || min_time == -1) {
637                         min_time = due_time;
638                 }
639
640         }
641
642         alarm_context.c_due_time = min_time;
643
644         return true;
645 }
646
647 static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
648                            int pid,char *bundle_data, int *error_code){
649
650         time_t current_time;
651         time_t due_time;
652         struct tm ts_ret;
653         char due_time_r[100] = { 0 };
654         char proc_file[512] = { 0 };
655         char process_name[512] = { 0 };
656         char app_name[512] = { 0 };
657         char *word = NULL;
658         char *proc_name_ptr = NULL;
659         int fd = 0;
660         int ret = 0;
661         int i = 0;
662
663         __alarm_info_t *__alarm_info = NULL;
664
665         __alarm_info = malloc(sizeof(__alarm_info_t));
666         if (__alarm_info == NULL) {
667                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, malloc "
668                                           "failed. it seems to be OOM\n", pid);
669                 *error_code = -1;       /* -1 means that system 
670                                            failed internally. */
671                 return false;
672         }
673         __alarm_info->pid = pid;
674         __alarm_info->alarm_id = -1;
675
676
677         /* we should consider to check whether  pid is running or Not
678          */
679         memset(process_name, '\0', 512);
680         memset(proc_file, '\0', 512);
681         snprintf(proc_file, 512, "/proc/%d/cmdline", pid);
682
683         fd = open(proc_file, O_RDONLY);
684         if (fd > 0) {
685                 ret = read(fd, process_name, 512);
686                 close(fd);
687                 if (ret <=0)
688                 {
689                         ALARM_MGR_EXCEPTION_PRINT("Unable to get application name\n");
690                         *error_code = -1;
691                         free(__alarm_info);
692                         return false;
693                 }
694                 while (process_name[i] != '\0') {
695                         if (process_name[i] == ' ') {
696                                 process_name[i] = '\0';
697                                 break;
698                         }
699                         i++;
700                 }
701
702                 word = strtok_r(process_name, "/", &proc_name_ptr);
703                 while (word != NULL) {
704                         memset(app_name, 0, 512);
705                         snprintf(app_name, 512, "%s", word);
706                         word = strtok_r(NULL, "/", &proc_name_ptr);
707                 }
708                 __alarm_info->quark_app_unique_name =
709                     g_quark_from_string(app_name);
710         } else {                /* failure */
711                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
712                                           "killed, so we failed to get proc file(%s) and do not create "
713                                           "alarm_info\n", pid, proc_file);
714                 *error_code = -1;       /*-1 means that system failed 
715                                                         internally.*/
716                 free(__alarm_info);
717                 return false;
718         }
719
720         __alarm_info->quark_bundle=g_quark_from_string(bundle_data);
721         __alarm_info->quark_app_service_name = g_quark_from_string("null");
722         __alarm_info->quark_dst_service_name = g_quark_from_string("null");
723
724         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
725         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
726
727         time(&current_time);
728
729         if (alarm_context.c_due_time < current_time) {
730                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
731                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
732                                           current_time);
733                 alarm_context.c_due_time = -1;
734         }
735
736         due_time = _alarm_next_duetime(__alarm_info);
737         if (__alarm_add_to_list(__alarm_info, alarm_id) == false) {
738                 free(__alarm_info);
739                 *error_code = -1;
740                 return false;
741         }
742
743         if (due_time == 0) {
744                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
745                 "due_time is 0, alarm(%d) \n", *alarm_id);
746                 return true;
747         } else if (current_time == due_time) {
748                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
749                      "current_time(%d) is same as due_time(%d)", current_time,
750                      due_time);
751                 return true;
752         }else if (difftime(due_time, current_time) <  0){
753                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: Expired Due Time.[Due time=%d, Current Time=%d]!!!Do not add to schedule list\n", due_time, current_time);
754                 return true;
755         }else {
756                 localtime_r(&due_time, &ts_ret);
757                 strftime(due_time_r, 30, "%c", &ts_ret);
758                 ALARM_MGR_LOG_PRINT("[alarm-server]:Create a new alarm: "
759                                     "alarm(%d) due_time(%s)", *alarm_id,
760                                     due_time_r);
761         }
762
763         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
764                             "due_time(%d)", alarm_context.c_due_time, due_time);
765
766         if (alarm_context.c_due_time == -1
767             || due_time < alarm_context.c_due_time) {
768                 _clear_scheduled_alarm_list();
769                 _add_to_scheduled_alarm_list(__alarm_info);
770                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
771                                  *alarm_id);
772                 alarm_context.c_due_time = due_time;
773
774         } else if (due_time == alarm_context.c_due_time) {
775                 _add_to_scheduled_alarm_list(__alarm_info);
776
777         }
778
779         __rtc_set();
780
781 #ifdef __ALARM_BOOT
782         /*alarm boot */
783         if (enable_power_on_alarm) {
784                 __alarm_power_on(0, enable_power_on_alarm, NULL);
785         }
786 #endif
787
788         return true;
789
790 }
791
792 static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
793                            int pid, char *app_service_name, char *app_service_name_mod,
794                            const char *dst_service_name,const char *dst_service_name_mod,  int *error_code)
795 {
796
797         time_t current_time;
798         time_t due_time;
799         char proc_file[256] = { 0 };
800         char process_name[512] = { 0 };
801         char app_name[256] = { 0 };
802         char *word = NULL;
803         char *proc_name_ptr = NULL;
804
805         __alarm_info_t *__alarm_info = NULL;
806
807         __alarm_info = malloc(sizeof(__alarm_info_t));
808         if (__alarm_info == NULL) {
809                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, malloc "
810                                           "failed. it seems to be OOM\n", pid);
811                 *error_code = -1;       /* -1 means that system 
812                                            failed internally. */
813                 return false;
814         }
815         __alarm_info->pid = pid;
816         __alarm_info->alarm_id = -1;
817
818         /* we should consider to check whether  pid is running or Not
819          */
820         memset(process_name, '\0', 512);
821         memset(proc_file, '\0', 256);
822         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
823
824         int fd;
825         int ret;
826         int i = 0;
827         fd = open(proc_file, O_RDONLY);
828         if (fd > 0) {
829                 ret = read(fd, process_name, 512);
830                 close(fd);
831                 while (process_name[i] != '\0') {
832                         if (process_name[i] == ' ') {
833                                 process_name[i] = '\0';
834                                 break;
835                         }
836                         i++;
837                 }
838                 /* if (readlink(proc_file, process_name, 256)!=-1) */
839                 /*success */
840
841                 word = strtok_r(process_name, "/", &proc_name_ptr);
842                 while (word != NULL) {
843                         memset(app_name, 0, 256);
844                         snprintf(app_name, 256, "%s", word);
845                         word = strtok_r(NULL, "/", &proc_name_ptr);
846                 }
847                 __alarm_info->quark_app_unique_name =
848                     g_quark_from_string(app_name);
849         } else {                /* failure */
850
851                 __alarm_info->quark_app_unique_name =
852                     g_quark_from_string("unknown");
853                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
854                                           "killed, so we failed to get proc file(%s) and do not create "
855                                           "alarm_info\n", pid, proc_file);
856                 *error_code = -1;       /*-1 means that system failed 
857                                                         internally.*/
858                 free(__alarm_info);
859                 return false;
860         }
861
862         __alarm_info->quark_app_service_name =
863             g_quark_from_string(app_service_name);
864         __alarm_info->quark_app_service_name_mod =
865         g_quark_from_string(app_service_name_mod);
866         __alarm_info->quark_dst_service_name =
867             g_quark_from_string(dst_service_name);
868         __alarm_info->quark_dst_service_name_mod =
869             g_quark_from_string(dst_service_name_mod);
870         __alarm_info->quark_bundle = g_quark_from_string("null");
871
872
873         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
874         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
875
876         time(&current_time);
877
878         ALARM_MGR_LOG_PRINT("[alarm-server]:pid=%d, app_unique_name=%s, "
879                 "app_service_name=%s,dst_service_name=%s, c_due_time=%d", \
880                 pid, g_quark_to_string(__alarm_info->quark_app_unique_name), \
881                 g_quark_to_string(__alarm_info->quark_app_service_name), \
882                 g_quark_to_string(__alarm_info->quark_dst_service_name), \
883                             alarm_context.c_due_time);
884
885         if (alarm_context.c_due_time < current_time) {
886                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
887                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
888                                           current_time);
889                 alarm_context.c_due_time = -1;
890         }
891
892         due_time = _alarm_next_duetime(__alarm_info);
893         if (__alarm_add_to_list(__alarm_info, alarm_id) == false) {
894                 free(__alarm_info);
895                 return false;
896         }
897
898         if (due_time == 0) {
899                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
900                 "due_time is 0, alarm(%d) \n", *alarm_id);
901                 return true;
902         } else if (current_time == due_time) {
903                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
904                      "current_time(%d) is same as due_time(%d)", current_time,
905                      due_time);
906                 return true;
907         }else if (difftime(due_time, current_time) <  0){
908                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: Expired Due Time.[Due time=%d, Current Time=%d]!!!Do not add to schedule list\n", due_time, current_time);
909                 return true;
910         } else {
911                 char due_time_r[100] = { 0 };
912                 struct tm ts_ret;
913                 localtime_r(&due_time, &ts_ret);
914                 strftime(due_time_r, 30, "%c", &ts_ret);
915                 ALARM_MGR_LOG_PRINT("[alarm-server]:Create a new alarm: "
916                                     "alarm(%d) due_time(%s)", *alarm_id,
917                                     due_time_r);
918         }
919
920         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
921                             "due_time(%d)", alarm_context.c_due_time, due_time);
922
923         if (alarm_context.c_due_time == -1
924             || due_time < alarm_context.c_due_time) {
925                 _clear_scheduled_alarm_list();
926                 _add_to_scheduled_alarm_list(__alarm_info);
927                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
928                                  *alarm_id);
929                 alarm_context.c_due_time = due_time;
930
931         } else if (due_time == alarm_context.c_due_time) {
932                 _add_to_scheduled_alarm_list(__alarm_info);
933
934         }
935
936         __rtc_set();
937
938 #ifdef __ALARM_BOOT
939         /*alarm boot */
940         if (enable_power_on_alarm) {
941                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
942                  * platform with app-server.because __alarm_power_on(..) fuction don't 
943                  * use first parameter internally, we set this value to 0(zero)
944                  */
945                 __alarm_power_on(0, enable_power_on_alarm, NULL);
946         }
947 #endif
948         return true;
949 }
950
951 static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
952                            alarm_info_t *alarm_info, int *error_code)
953 {
954         time_t current_time;
955         time_t due_time;
956
957         __alarm_info_t *__alarm_info = NULL;
958         bool result = false;
959
960         __alarm_info = malloc(sizeof(__alarm_info_t));
961         if (__alarm_info == NULL) {
962                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, "
963                         "malloc failed. it seems to be OOM\n", pid);
964                 *error_code = -1;       /*-1 means that system failed 
965                                                 internally.*/
966                 return false;
967         }
968
969         __alarm_info->pid = pid;
970         __alarm_info->alarm_id = alarm_id;
971
972         /* we should consider to check whether  pid is running or Not
973          */
974
975         __alarm_info->quark_app_service_name =
976             g_quark_from_string(app_service_name);
977         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
978         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
979
980         time(&current_time);
981
982         if (alarm_context.c_due_time < current_time) {
983                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
984                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
985                                           current_time);
986                 alarm_context.c_due_time = -1;
987         }
988
989         due_time = _alarm_next_duetime(__alarm_info);
990         if (!__alarm_update_in_list(pid, alarm_id, __alarm_info, error_code)) {
991                 free(__alarm_info);
992                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: requested alarm_id "
993                 "(%d) does not exist. so this value is invalid id.", alarm_id);
994                 return false;
995         }
996         /* ALARM_MGR_LOG_PRINT("[alarm-server]:request_pid=%d, alarm_id=%d, 
997          * app_unique_name=%s, app_service_name=%s, dst_service_name=%s, 
998          * c_due_time=%d", pid, alarm_id, g_quark_to_string
999          * (__alarm_info->quark_app_unique_name), g_quark_to_string
1000          * (__alarm_info->quark_app_service_name), g_quark_to_string
1001          * (__alarm_info->quark_dst_service_name), alarm_context.c_due_time);
1002          */
1003
1004         result = _remove_from_scheduled_alarm_list(pid, alarm_id);
1005
1006         if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
1007                 /*there is no scheduled alarm */
1008                 _alarm_disable_timer(alarm_context);
1009                 _alarm_schedule();
1010
1011                 ALARM_MGR_LOG_PRINT("[alarm-server]:Update alarm: alarm(%d)\n",
1012                                     alarm_id);
1013
1014                 __rtc_set();
1015
1016 #ifdef __ALARM_BOOT
1017                 /*alarm boot */
1018                 if (enable_power_on_alarm) {
1019                         /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in 
1020                          * a platform with app-server.because __alarm_power_on(..) fuction don't
1021                          * use first parameter internally, we set this value to 0(zero)
1022                          */
1023                         __alarm_power_on(0, enable_power_on_alarm, NULL);
1024                 }
1025 #endif
1026
1027                 if (due_time == 0) {
1028                         ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1029                                         "due_time is 0\n");
1030                 }
1031                 free(__alarm_info);
1032                 return true;
1033         }
1034
1035         if (due_time == 0) {
1036                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1037                                 "due_time is 0, alarm(%d)\n", alarm_id);
1038                 free(__alarm_info);
1039                 return true;
1040         } else if (current_time == due_time) {
1041                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1042                 "current_time(%d) is same as due_time(%d)", current_time,
1043                 due_time);
1044                 free(__alarm_info);
1045                 return true;
1046         }else if (difftime(due_time, current_time)< 0){
1047                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: Expired Due Time.[Due time=%d, Current Time=%d]!!!Do not add to schedule list\n", due_time, current_time);
1048                 return true;
1049         } else {
1050                 char due_time_r[100] = { 0 };
1051                 struct tm ts_ret;
1052                 localtime_r(&due_time, &ts_ret);
1053                 strftime(due_time_r, 30, "%c", &ts_ret);
1054                 ALARM_MGR_LOG_PRINT("[alarm-server]:Update alarm: alarm(%d) "
1055                                     "due_time(%s)\n", alarm_id, due_time_r);
1056         }
1057
1058         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
1059                             "due_time(%d)", alarm_context.c_due_time, due_time);
1060
1061         if (alarm_context.c_due_time == -1
1062             || due_time < alarm_context.c_due_time) {
1063                 _clear_scheduled_alarm_list();
1064                 _add_to_scheduled_alarm_list(__alarm_info);
1065                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
1066                                  alarm_id);
1067                 alarm_context.c_due_time = due_time;
1068                 ALARM_MGR_LOG_PRINT("[alarm-server1]:alarm_context.c_due_time "
1069                      "(%d), due_time(%d)", alarm_context.c_due_time, due_time);
1070         } else if (due_time == alarm_context.c_due_time) {
1071                 _add_to_scheduled_alarm_list(__alarm_info);
1072                 ALARM_MGR_LOG_PRINT("[alarm-server2]:alarm_context.c_due_time "
1073                      "(%d), due_time(%d)", alarm_context.c_due_time, due_time);
1074         }
1075
1076         __rtc_set();
1077
1078 #ifdef __ALARM_BOOT
1079         /*alarm boot */
1080         if (enable_power_on_alarm) {
1081                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) 
1082                  * in a platform with app-server.because __alarm_power_on(..) fuction 
1083                  * don't use first parameter internally, we set this value to 0(zero)
1084                  */
1085                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1086         }
1087 #endif
1088
1089         return true;
1090 }
1091
1092 static bool __alarm_delete(int pid, alarm_id_t alarm_id, int *error_code)
1093 {
1094         bool result = false;
1095
1096         ALARM_MGR_LOG_PRINT("[alarm-server]:delete alarm: alarm(%d) pid(%d)\n",\
1097                             alarm_id, pid);
1098         result = _remove_from_scheduled_alarm_list(pid, alarm_id);
1099
1100         if (!__alarm_remove_from_list(pid, alarm_id, error_code)) {
1101
1102                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:delete alarm: "
1103                                           "alarm(%d) pid(%d) has failed with error_code(%d)\n",
1104                                           alarm_id, pid, *error_code);
1105                 return false;
1106         }
1107
1108         if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
1109                 _alarm_disable_timer(alarm_context);
1110                 _alarm_schedule();
1111         }
1112
1113         __rtc_set();
1114
1115 #ifdef __ALARM_BOOT
1116         /*alarm boot */
1117         if (enable_power_on_alarm) {
1118                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a 
1119                  * platform with app-server.because __alarm_power_on(..) fuction don't 
1120                  * use first parameter internally, we set this value to 0(zero)
1121                  */
1122                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1123         }
1124 #endif
1125
1126         return true;
1127 }
1128
1129 static bool __alarm_power_on(int app_id, bool on_off, int *error_code)
1130 {
1131 #ifdef __ALARM_BOOT
1132         time_t min_time = 0;
1133         time_t current_time = 0;
1134         struct tm *temp_info;
1135         struct rtc_time rtc_tm = { 0, };
1136         struct tm min_time_r;
1137         int fd = 0;
1138         int retval;
1139
1140         enable_power_on_alarm = on_off;
1141         /*_update_power_on(on_off); */ /*currently its empty*/
1142
1143         fd = open(power_rtc, O_RDONLY);
1144         if (fd < 0) {
1145                 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc0\n");
1146                 return false;
1147         }
1148
1149         if (on_off == true) {
1150                 if (_alarm_find_mintime_power_on(&min_time) == true) {
1151
1152                         ab_due_time = min_time;
1153
1154                         min_time = min_time - 60;
1155
1156                         time(&current_time);
1157
1158                         if (min_time <= current_time)
1159                                 min_time = current_time + 5;
1160
1161                         temp_info = gmtime_r(&min_time, &min_time_r);
1162
1163                         ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
1164                                                 "%d\n", \
1165                                            min_time_r.tm_year,\
1166                                            min_time_r.tm_mon,\
1167                                            min_time_r.tm_mday,\
1168                                            min_time_r.tm_hour,\
1169                                            min_time_r.tm_min);
1170                         ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
1171                                                 "%d\n", \
1172                                            min_time_r.tm_year,\
1173                                            min_time_r.tm_mon,\
1174                                            min_time_r.tm_mday,\
1175                                            min_time_r.tm_hour,\
1176                                            min_time_r.tm_min);
1177
1178                         rtc_tm.tm_mday = min_time_r.tm_mday;
1179                         rtc_tm.tm_mon = min_time_r.tm_mon;
1180                         rtc_tm.tm_year = min_time_r.tm_year;
1181                         rtc_tm.tm_hour = min_time_r.tm_hour;
1182                         rtc_tm.tm_min = min_time_r.tm_min;
1183                         rtc_tm.tm_sec = min_time_r.tm_sec;
1184                         /*set_info.time_zone = 0; */
1185                         /*set_info.u_interval.day_of_week = 0; */
1186
1187                         /*ALARM_MGR_LOG_PRINT("####__alarm_power_on : %d %d 
1188                         %d %d %d\n",set_info.year,set_info.month,set_info.day,
1189                         set_info.hour,set_info.minute); */
1190
1191                         ALARM_MGR_LOG_PRINT("due_time : %d \n", ab_due_time);
1192
1193                         ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
1194                                             "%d-%d-%d, %02d:%02d:%02d.\n",
1195                                             rtc_tm.tm_mday, rtc_tm.tm_mon + 1,
1196                                 rtc_tm.tm_year + 1900, rtc_tm.tm_hour, 
1197                                 rtc_tm.tm_min, rtc_tm.tm_sec);
1198
1199                         retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
1200                         if (retval == -1) {
1201                                 if (errno == ENOTTY) {
1202                                         ALARM_MGR_EXCEPTION_PRINT(
1203                                         "\n...Alarm IRQs not supported.\n");
1204                                 }
1205                                 ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
1206                                 close(fd);
1207                                 return false;
1208                         }
1209                         ALARM_MGR_LOG_PRINT("[alarm-server]RTC "
1210                                             "alarm(POWER ON) is setted");
1211
1212                         /* Enable alarm interrupts */
1213                         retval = ioctl(fd, RTC_AIE_ON, 0);
1214                         if (retval == -1) {
1215                                 ALARM_MGR_EXCEPTION_PRINT(
1216                                     "RTC_AIE_ON ioctl failed");
1217                                 close(fd);
1218                                 return false;
1219                         }
1220                         ALARM_MGR_LOG_PRINT("[alarm-server]RTC(POWER ON) "
1221                                             "alarm is on");
1222
1223                 } else
1224                         retval = ioctl(fd, RTC_AIE_OFF, 0);
1225         } else {
1226                 ALARM_MGR_LOG_PRINT("__alarm_power_on : off\n");
1227                 retval = ioctl(fd, RTC_AIE_OFF, 0);
1228         }
1229
1230         close(fd);
1231 #endif                          /* #ifdef __ALARM_BOOT */
1232
1233         return true;
1234 }
1235
1236 static bool __alarm_power_off(int app_id, int *error_code)
1237 {
1238 #ifdef __ALARM_BOOT
1239
1240 #endif                          /* #ifdef __ALARM_BOOT */
1241         return true;
1242 }
1243
1244 static bool __alarm_check_next_duetime(int app_id, int *error_code)
1245 {
1246 #ifdef __ALARM_BOOT
1247         time_t current_time;
1248         time_t interval;
1249
1250         time(&current_time);
1251
1252         interval = ab_due_time - current_time;
1253
1254         ALARM_MGR_LOG_PRINT("due_time : %d / current_time %d\n", ab_due_time,
1255                             current_time);
1256
1257         if (interval > 0 && interval <= 60)
1258                 return true;
1259         else
1260                 return false;
1261 #else                           /* #ifdef __ALARM_BOOT */
1262         return true;
1263 #endif
1264 }
1265
1266 static void __alarm_send_noti_to_application(const char *app_service_name,
1267                                              alarm_id_t alarm_id)
1268 {
1269
1270
1271         char service_name[MAX_SERVICE_NAME_LEN];
1272         char object_name[MAX_SERVICE_NAME_LEN];
1273
1274         DBusMessage *message;
1275         DBusMessageIter iter;
1276
1277         if (app_service_name == NULL || strlen(app_service_name) == 0) {
1278                 ALARM_MGR_EXCEPTION_PRINT("This alarm destination is "
1279                                           "invalid\n");
1280                 return;
1281         }
1282         memset(service_name, 0, MAX_SERVICE_NAME_LEN);
1283         memcpy(service_name, app_service_name, strlen(app_service_name));
1284
1285         snprintf(object_name, MAX_SERVICE_NAME_LEN,
1286                  "/org/tizen/alarm/client");
1287
1288         ALARM_MGR_LOG_PRINT("[alarm server][send expired_alarm(alarm_id=%d)to"
1289         "app_service_name(%s), object_name(%s), interface_name(%s)]\n",\
1290         alarm_id, service_name, object_name, "org.tizen.alarm.client");
1291
1292         message = dbus_message_new_method_call(service_name,
1293                                                object_name,
1294                                                "org.tizen.alarm.client",
1295                                                "alarm_expired");
1296         if (message == NULL) {
1297                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
1298                         "dbus_message_new_method_call faild. maybe OOM!.\n");
1299                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] so we cannot "
1300                         "send expired alarm to %s\n", service_name);
1301                 return;
1302         }
1303
1304         dbus_message_set_no_reply(message, TRUE);
1305         /*      if(service_name[0]==':') */
1306         /* we don't need auto activation in a case that 
1307            destination_app_service_name starts with a charactor like (:) */
1308         dbus_message_set_auto_start(message, FALSE);
1309
1310         dbus_message_iter_init_append(message, &iter);
1311         if (!dbus_message_iter_append_basic 
1312                 (&iter, DBUS_TYPE_INT32, &alarm_id)) {
1313                 dbus_message_unref(message);
1314                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
1315                 "dbus_message_iter_append_basic faild. maybe OOM!.\n");
1316                 return;
1317         }
1318
1319         dbus_connection_send(dbus_g_connection_get_connection
1320                              (alarm_context.bus), message, NULL);
1321         dbus_connection_flush(dbus_g_connection_get_connection
1322                               (alarm_context.bus));
1323         dbus_message_unref(message);
1324
1325 }
1326
1327 static void __alarm_expired()
1328 {
1329         const char *destination_app_service_name = NULL;
1330         alarm_id_t alarm_id = -1;
1331         int app_pid = 0;
1332         __alarm_info_t *__alarm_info = NULL;
1333         char alarm_id_val[32]={0,};
1334         int b_len = 0;
1335         bundle *b = NULL;
1336
1337         ALARM_MGR_LOG_PRINT("[alarm-server]: Enter \n");
1338
1339         time_t current_time;
1340         double interval;
1341
1342         time(&current_time);
1343
1344         interval = difftime(alarm_context.c_due_time, current_time);
1345         ALARM_MGR_LOG_PRINT("[alarm-server]: c_due_time(%d), "
1346                 "current_time(%d), interval(%d)\n", alarm_context.c_due_time, 
1347                 current_time, interval);
1348
1349         if (alarm_context.c_due_time <= current_time - 1) {
1350                 ALARM_MGR_LOG_PRINT("Wrong alarm is expired\n");
1351                 goto done;
1352         }
1353
1354         GSList *iter = NULL;
1355         __scheduled_alarm_t *alarm = NULL;
1356
1357         for (iter = g_scheduled_alarm_list; iter != NULL;
1358              iter = g_slist_next(iter)) {
1359                 alarm = iter->data;
1360                 alarm_id = alarm->alarm_id;
1361
1362                 __alarm_info = alarm->__alarm_info;
1363
1364                 app_pid = __alarm_info->pid;
1365
1366                 if (strncmp
1367                     (g_quark_to_string(__alarm_info->quark_bundle),
1368                      "null", 4) != 0) {
1369
1370                                 b_len = strlen(g_quark_to_string(__alarm_info->quark_bundle));
1371
1372                                 b = bundle_decode((bundle_raw *)g_quark_to_string(__alarm_info->quark_bundle), b_len);
1373
1374                                 if (NULL == b)
1375                                 {
1376                                         ALARM_MGR_EXCEPTION_PRINT("Error!!!..Unable to decode the bundle!!\n");
1377                                 }
1378                                 else
1379                                 {
1380                                         snprintf(alarm_id_val,31,"%d",alarm_id);
1381
1382                                         if (bundle_add(b,"http://tizen.org/appsvc/data/alarm_id", alarm_id_val)){
1383                                                 ALARM_MGR_EXCEPTION_PRINT("Unable to add alarm id to the bundle\n");
1384                                         }
1385                                         else
1386                                         {
1387                                                 if ( appsvc_run_service(b, 0, NULL, NULL) < 0)
1388                                                 {
1389                                                         ALARM_MGR_EXCEPTION_PRINT("Unable to run app svc\n");
1390                                                 }
1391                                                 else
1392                                                 {
1393                                                         ALARM_MGR_LOG_PRINT("Successfuly ran app svc\n");
1394                                                 }
1395                                         }
1396                                         bundle_free(b);
1397                                 }
1398
1399                 }
1400                 else
1401                 {
1402                         if (strncmp
1403                             (g_quark_to_string(__alarm_info->quark_dst_service_name),
1404                              "null",4) == 0) {
1405                                 ALARM_MGR_LOG_PRINT("[alarm-server]:destination is "
1406                                 "null, so we send expired alarm to %s(%u)\n",\
1407                                         g_quark_to_string(
1408                                         __alarm_info->quark_app_service_name),
1409                                         __alarm_info->quark_app_service_name); 
1410                                         destination_app_service_name = g_quark_to_string(
1411                                         __alarm_info->quark_app_service_name_mod);
1412                         } else {
1413                                 ALARM_MGR_LOG_PRINT("[alarm-server]:destination "
1414                                                     ":%s(%u)\n",
1415                                         g_quark_to_string(
1416                                         __alarm_info->quark_dst_service_name), 
1417                                         __alarm_info->quark_dst_service_name);
1418                                         destination_app_service_name = g_quark_to_string(
1419                                                 __alarm_info->quark_dst_service_name_mod);
1420                         }
1421
1422 #ifdef __ALARM_BOOT
1423                         /* orginally this code had if(__alarm_info->app_id==21) in a 
1424                            platform with app-server. */
1425                         /*if(__alarm_info->quark_dst_service_name  == 
1426                            g_quark_from_string (WAKEUP_ALARM_APP_ID)) */
1427                         if (strcmp
1428                             (g_quark_to_string(__alarm_info->quark_dst_service_name),
1429                              WAKEUP_ALARM_APP_ID) == 0) {
1430                                 int fd = 0;
1431                                 fd = open(power_rtc, O_RDONLY);
1432                                 if (fd < 0) {
1433                                         ALARM_MGR_LOG_PRINT("cannot open /dev/rtc0\n");
1434                                 } else {
1435                                         ioctl(fd, RTC_AIE_OFF, 0);
1436                                         close(fd);
1437                                 }
1438                         }
1439 #endif
1440
1441                         /* 
1442                          * we should consider a situation that 
1443                          * destination_app_service_name is owner_name like (:xxxx) and
1444                          * application's pid which registered this alarm was killed.In that case,
1445                          * we don't need to send the expire event because the process was killed.
1446                          * this causes needless message to be sent.
1447                          */
1448                         ALARM_MGR_LOG_PRINT("[alarm-server]: "
1449                                             "destination_app_service_name :%s, app_pid=%d\n",
1450                                             destination_app_service_name, app_pid);
1451                         /* the following is a code that checks the above situation. 
1452                            please verify this code. */
1453
1454                         if (dbus_bus_name_has_owner(
1455                              dbus_g_connection_get_connection(alarm_context.bus),
1456                              destination_app_service_name, NULL) == FALSE) {
1457                                 __expired_alarm_t *expire_info;
1458                                 char pkg_name[MAX_SERVICE_NAME_LEN] = { 0, };
1459                                 char alarm_id_str[32] = { 0, };
1460
1461                                 expire_info = malloc(sizeof(__expired_alarm_t));
1462                                 if (G_UNLIKELY(NULL == expire_info)){
1463                                         ALARM_MGR_ASSERT_PRINT("[alarm-server]:Malloc failed!Can't notify alarm expiry info\n");
1464                                         goto done;
1465                                 }
1466                                 strncpy(expire_info->service_name,
1467                                         destination_app_service_name,
1468                                         MAX_SERVICE_NAME_LEN);
1469                                 expire_info->alarm_id = alarm_id;
1470                                 g_expired_alarm_list =
1471                                     g_slist_append(g_expired_alarm_list, expire_info);
1472
1473
1474                                 if (strncmp
1475                                         (g_quark_to_string(__alarm_info->quark_dst_service_name),
1476                                              "null",4) == 0) {
1477                                         strncpy(pkg_name,g_quark_to_string(__alarm_info->quark_app_service_name),strlen(g_quark_to_string(__alarm_info->quark_app_service_name))-6);
1478                                 }
1479                                 else
1480                                 {
1481                                         strncpy(pkg_name,g_quark_to_string(__alarm_info->quark_dst_service_name),strlen(g_quark_to_string(__alarm_info->quark_dst_service_name))-6);
1482                                 }
1483
1484                                 snprintf(alarm_id_str, 31, "%d", alarm_id);
1485
1486                                 ALARM_MGR_LOG_PRINT("before aul_launch pkg_name(%s) "
1487                                         "alarm_id_str(%s)\n", pkg_name, alarm_id_str);
1488
1489                                 bundle *kb;
1490                                 kb = bundle_create();
1491                                 bundle_add(kb, "__ALARM_MGR_ID", alarm_id_str);
1492                                 aul_launch_app(pkg_name, kb);
1493                                 bundle_free(kb);
1494                         } else {
1495                                 ALARM_MGR_LOG_PRINT(
1496                                         "before alarm_send_noti_to_application\n");
1497                                 __alarm_send_noti_to_application(
1498                                              destination_app_service_name, alarm_id);
1499                         }
1500                 }
1501                 ALARM_MGR_LOG_PRINT("after __alarm_send_noti_to_application\n");
1502
1503 /*              if( !(__alarm_info->alarm_info.alarm_type 
1504                                         & ALARM_TYPE_VOLATILE) ) {
1505                         __alarm_remove_from_list(__alarm_info->pid, 
1506                                                         alarm_id, NULL);
1507                 }
1508                 else */
1509                 if (__alarm_info->alarm_info.mode.repeat
1510                     == ALARM_REPEAT_MODE_ONCE) {
1511 /*                      _alarm_next_duetime(__alarm_info);*/
1512 /*                      _update_alarms(__alarm_info);*/
1513                         __alarm_remove_from_list(__alarm_info->pid, alarm_id,
1514                                                  NULL);
1515                 } else {
1516
1517                         _alarm_next_duetime(__alarm_info);
1518 /*                      _update_alarms(__alarm_info);*/
1519                 }
1520
1521         }
1522
1523  done:
1524         _clear_scheduled_alarm_list();
1525         alarm_context.c_due_time = -1;
1526
1527         ALARM_MGR_LOG_PRINT("[alarm-server]: Leave  \n");
1528 }
1529
1530 static gboolean __alarm_handler_idle()
1531 {
1532         if (g_dummy_timer_is_set == true) {
1533                 ALARM_MGR_LOG_PRINT("dummy alarm timer has expired\n");
1534         } else {
1535                 ALARM_MGR_LOG_PRINT("__alarm_handler \n");
1536                 ALARM_MGR_LOG_PRINT("__alarm_handler \n");
1537
1538                 __alarm_expired();
1539
1540         }
1541
1542         _alarm_schedule();
1543
1544         __rtc_set();
1545
1546 #ifdef __ALARM_BOOT
1547         /*alarm boot */
1548         if (enable_power_on_alarm) {
1549                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) 
1550                  *in a platform with app-server.because __alarm_power_on(..) fuction 
1551                  *don't use first parameter internally, we set this value to 0(zero)
1552                  */
1553                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1554         }
1555 #endif
1556         pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
1557         return false;
1558
1559 }
1560
1561 static void __alarm_handler(int sigNum, siginfo_t *pSigInfo, void *pUContext)
1562 {
1563
1564         pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
1565
1566         /* we moved __alarm_expired() function to __alarm_handler_idle GSource
1567            because of signal safety. */
1568         g_idle_add_full(G_PRIORITY_HIGH_IDLE, __alarm_handler_idle, NULL, NULL);
1569 }
1570
1571 static void __clean_registry()
1572 {
1573
1574         /*TODO:remove all db entries */
1575 }
1576
1577 static bool __alarm_manager_reset()
1578 {
1579
1580         _alarm_disable_timer(alarm_context);
1581
1582         __alarm_clean_list();
1583
1584         _clear_scheduled_alarm_list();
1585         __clean_registry();
1586
1587         return true;
1588 }
1589
1590 static void __on_system_time_changed(keynode_t *node, void *data)
1591 {
1592         double diff_time;
1593         time_t _time;
1594         time_t before;
1595
1596         _alarm_disable_timer(alarm_context);
1597
1598         if (node) {
1599                 _time = vconf_keynode_get_int(node);
1600         } else {
1601                 vconf_get_int(VCONFKEY_SYSTEM_TIMECHANGE, (int *)&_time);
1602         }
1603
1604         time(&before);
1605         diff_time = difftime(_time, before);
1606
1607         tzset();
1608
1609         ALARM_MGR_ASSERT_PRINT("diff_time is %f\n", diff_time);
1610
1611         ALARM_MGR_LOG_PRINT("[alarm-server] System time has been changed\n");
1612         ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n",
1613                             alarm_context.c_due_time);
1614
1615         _set_time(_time);
1616         __alarm_update_due_time_of_all_items_in_list(diff_time);
1617
1618         ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n",
1619                             alarm_context.c_due_time);
1620         _clear_scheduled_alarm_list();
1621         _alarm_schedule();
1622         __rtc_set();
1623 #ifdef __ALARM_BOOT
1624         /*alarm boot */
1625         if (enable_power_on_alarm) {
1626 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
1627 APP_ID) in a platform with app-server. because _alarm_power_
1628 on(..) fuction don't use first parameter internally, we set 
1629 this value to 0(zero)
1630 */
1631                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1632         }
1633 #endif
1634         return;
1635 }
1636
1637
1638 gboolean alarm_manager_alarm_create_appsvc(void *pObject, int pid,
1639                                     int start_year,
1640                                     int start_month, int start_day,
1641                                     int start_hour, int start_min,
1642                                     int start_sec, int end_year, int end_month,
1643                                     int end_day, int mode_day_of_week,
1644                                     int mode_repeat, int alarm_type,
1645                                     int reserved_info,
1646                                     char *bundle_data, char *e_cookie,
1647                                     int *alarm_id, int *return_code)
1648 {
1649         alarm_info_t alarm_info;
1650         guchar *cookie = NULL;
1651         gsize size;
1652         int retval = 0;
1653         gid_t call_gid;
1654         gboolean result = true;
1655
1656         alarm_info.start.year = start_year;
1657         alarm_info.start.month = start_month;
1658         alarm_info.start.day = start_day;
1659         alarm_info.start.hour = start_hour;
1660         alarm_info.start.min = start_min;
1661         alarm_info.start.sec = start_sec;
1662
1663         alarm_info.end.year = end_year;
1664         alarm_info.end.month = end_month;
1665         alarm_info.end.day = end_day;
1666
1667         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
1668         alarm_info.mode.repeat = mode_repeat;
1669
1670         alarm_info.alarm_type = alarm_type;
1671         alarm_info.reserved_info = reserved_info;
1672
1673         *return_code = 0;
1674
1675         cookie = g_base64_decode(e_cookie, &size);
1676         if (NULL == cookie)
1677         {
1678                 *return_code = -1;
1679                 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
1680                 return false;
1681         }
1682         
1683         call_gid = security_server_get_gid("alarm");
1684
1685         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
1686
1687         retval = security_server_check_privilege((const char *)cookie, call_gid);
1688         if (retval < 0) {
1689                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1690                         ALARM_MGR_EXCEPTION_PRINT(
1691                                 "%s", "access has been denied\n");
1692                 }
1693                 ALARM_MGR_EXCEPTION_PRINT("Error has occurred in security_server_check_privilege()\n");
1694                 *return_code = -1;
1695                 result = false;
1696         }
1697         else {
1698                 result = __alarm_create_appsvc(&alarm_info, alarm_id, pid,
1699                                bundle_data, return_code);
1700                 if (false == result)
1701                 {
1702                         ALARM_MGR_EXCEPTION_PRINT("Unable to create alarm!\n");
1703                 }
1704         }
1705
1706         if (cookie){
1707                 g_free(cookie);
1708                 cookie = NULL;
1709         }
1710
1711         return result;
1712 }
1713
1714 gboolean alarm_manager_alarm_create(void *pObject, int pid,
1715                                     char *app_service_name, char *app_service_name_mod,  int start_year,
1716                                     int start_month, int start_day,
1717                                     int start_hour, int start_min,
1718                                     int start_sec, int end_year, int end_month,
1719                                     int end_day, int mode_day_of_week,
1720                                     int mode_repeat, int alarm_type,
1721                                     int reserved_info,
1722                                     char *reserved_service_name, char *reserved_service_name_mod, char *e_cookie,
1723                                     int *alarm_id, int *return_code)
1724 {
1725         alarm_info_t alarm_info;
1726         guchar *cookie;
1727         gsize size;
1728         int retval;
1729         gid_t call_gid;
1730
1731         alarm_info.start.year = start_year;
1732         alarm_info.start.month = start_month;
1733         alarm_info.start.day = start_day;
1734         alarm_info.start.hour = start_hour;
1735         alarm_info.start.min = start_min;
1736         alarm_info.start.sec = start_sec;
1737
1738         alarm_info.end.year = end_year;
1739         alarm_info.end.month = end_month;
1740         alarm_info.end.day = end_day;
1741
1742         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
1743         alarm_info.mode.repeat = mode_repeat;
1744
1745         alarm_info.alarm_type = alarm_type;
1746         alarm_info.reserved_info = reserved_info;
1747
1748         *return_code = 0;
1749
1750         cookie = g_base64_decode(e_cookie, &size);
1751         call_gid = security_server_get_gid("alarm");
1752
1753         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
1754
1755         retval = security_server_check_privilege((const char *)cookie, call_gid);
1756         if (retval < 0) {
1757                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1758                         ALARM_MGR_EXCEPTION_PRINT(
1759                                 "%s", "access has been denied\n");
1760                 }
1761                 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
1762
1763                 *return_code = -1;
1764         }
1765
1766         else {
1767                 /* return valule and return_code should be checked */
1768                 __alarm_create(&alarm_info, alarm_id, pid, app_service_name,app_service_name_mod,
1769                                reserved_service_name, reserved_service_name_mod, return_code);
1770         }
1771
1772         g_free(cookie);
1773
1774         return true;
1775 }
1776
1777 gboolean alarm_manager_alarm_delete(void *pObject, int pid, alarm_id_t alarm_id,
1778                                     char *e_cookie, int *return_code)
1779 {
1780         guchar *cookie;
1781         gsize size;
1782         int retval;
1783         gid_t call_gid;
1784
1785         cookie = g_base64_decode(e_cookie, &size);
1786         call_gid = security_server_get_gid("alarm");
1787
1788         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
1789
1790         retval = security_server_check_privilege((const char *)cookie, call_gid);
1791         if (retval < 0) {
1792                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1793                         ALARM_MGR_EXCEPTION_PRINT("%s",
1794                                                   "access has been denied\n");
1795                 }
1796                 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
1797
1798                 *return_code = -1;
1799         }
1800
1801         else {
1802                 __alarm_delete(pid, alarm_id, return_code);
1803         }
1804
1805         g_free(cookie);
1806
1807         return true;
1808 }
1809
1810 gboolean alarm_manager_alarm_power_on(void *pObject, int pid, bool on_off,
1811                                       int *return_code)
1812 {
1813
1814         return __alarm_power_on(pid, on_off, return_code);
1815 }
1816
1817 gboolean alarm_manager_alarm_power_off(void *pObject, int pid, int *return_code)
1818 {
1819
1820         return __alarm_power_off(pid, return_code);
1821 }
1822
1823 bool alarm_manager_alarm_check_next_duetime(void *pObject, int pid,
1824                                             int *return_code)
1825 {
1826         return __alarm_check_next_duetime(pid, return_code);
1827 }
1828
1829 gboolean alarm_manager_alarm_update(void *pObject, int pid,
1830                                     char *app_service_name, alarm_id_t alarm_id,
1831                                     int start_year, int start_month,
1832                                     int start_day, int start_hour,
1833                                     int start_min, int start_sec, int end_year,
1834                                     int end_month, int end_day,
1835                                     int mode_day_of_week, int mode_repeat,
1836                                     int alarm_type, int reserved_info,
1837                                     int *return_code)
1838 {
1839         alarm_info_t alarm_info;
1840         alarm_info.start.year = start_year;
1841         alarm_info.start.month = start_month;
1842         alarm_info.start.day = start_day;
1843         alarm_info.start.hour = start_hour;
1844         alarm_info.start.min = start_min;
1845         alarm_info.start.sec = start_sec;
1846
1847         alarm_info.end.year = end_year;
1848         alarm_info.end.month = end_month;
1849         alarm_info.end.day = end_day;
1850
1851         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
1852         alarm_info.mode.repeat = mode_repeat;
1853
1854         alarm_info.alarm_type = alarm_type;
1855         alarm_info.reserved_info = reserved_info;
1856
1857         *return_code = 0;
1858
1859         __alarm_update(pid, app_service_name, alarm_id, &alarm_info,
1860                        return_code);
1861
1862         return true;
1863 }
1864
1865 gboolean alarm_manager_alarm_get_number_of_ids(void *pObject, int pid,
1866                                                int *num_of_ids,
1867                                                int *return_code)
1868 {
1869         GSList *gs_iter = NULL;
1870         GQuark quark_app_unique_name;   /* the fullpath of pid(pid) is 
1871                                            converted  to quark value. */
1872         char proc_file[256] = { 0 };
1873         char process_name[512] = { 0 };
1874         char app_name[256] = { 0 };
1875         char *word = NULL;
1876         __alarm_info_t *entry = NULL;
1877         char *proc_name_ptr = NULL;
1878
1879         *num_of_ids = 0;
1880         *return_code = 0;
1881
1882         /* we should consider to check whether  pid is running or Not
1883          */
1884         memset(process_name, '\0', 512);
1885         memset(proc_file, '\0', 256);
1886         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
1887
1888         int fd;
1889         int ret;
1890         int i = 0;
1891         fd = open(proc_file, O_RDONLY);
1892         if (fd > 0) {
1893                 ret = read(fd, process_name, 512);
1894                 close(fd);
1895                 while (process_name[i] != '\0') {
1896                         if (process_name[i] == ' ') {
1897                                 process_name[i] = '\0';
1898                                 break;
1899                         }
1900                         i++;
1901                 }
1902                 /*if (readlink(proc_file, process_name, 256)!=-1) */
1903                 /*success */
1904
1905                 word = strtok_r(process_name, "/", &proc_name_ptr);
1906                 while (word != NULL) {
1907                         memset(app_name, 0, 256);
1908                         snprintf(app_name, 256, "%s", word);
1909                         word = strtok_r(NULL, "/", &proc_name_ptr);
1910                 }
1911                 quark_app_unique_name = g_quark_from_string(app_name);
1912         } else {                /* failure */
1913
1914                 quark_app_unique_name = g_quark_from_string("unknown");
1915                 memcpy(app_name, "unknown", strlen("unknown") + 1);
1916
1917                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
1918                                           "killed, so we failed to get proc file(%s) \n",
1919                                           pid, proc_file);
1920                 *return_code = -1;      /* -1 means that system 
1921                                            failed internally. */
1922                 return true;
1923         }
1924
1925         ALARM_MGR_LOG_PRINT("called for  app(pid:%d, name=%s)\n",
1926                             pid, app_name);
1927
1928         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
1929              gs_iter = g_slist_next(gs_iter)) {
1930                 entry = gs_iter->data;
1931                 ALARM_MGR_LOG_PRINT("alarm_manager_alarm_get_number_of_ids(): "
1932                                     "app_name=%s,quark_app_unique_name=%s\n",
1933                 app_name, g_quark_to_string(entry->quark_app_unique_name));
1934                 if (strcmp
1935                     (app_name,
1936                      g_quark_to_string(entry->quark_app_unique_name)) == 0
1937                     && strcmp(app_name, "unknown") != 0) {
1938                         (*num_of_ids)++;
1939                         ALARM_MGR_LOG_PRINT("inc number of alarms of app "
1940                                             "(pid:%d, name:%s) is %d\n",
1941                                             pid, app_name, *num_of_ids);
1942                 }
1943         }
1944         *return_code = 0;
1945         ALARM_MGR_LOG_PRINT("number of alarms of app(pid:%d, name:%s) is %d\n",
1946                             pid, app_name, *num_of_ids);
1947         return true;
1948 }
1949
1950 gboolean alarm_manager_alarm_get_list_of_ids(void *pObject, int pid,
1951                                              int max_number_of_ids,
1952                                              GArray **arr, int *num_of_ids,
1953                                              int *return_code)
1954 {
1955         GSList *gs_iter = NULL;
1956         GQuark quark_app_unique_name;   /* the fullpath of pid(pid) is converted
1957                                            to quark value. */
1958         char proc_file[256] = { 0 };
1959         char process_name[512] = { 0 };
1960         char app_name[256] = { 0 };
1961         char *word = NULL;
1962         __alarm_info_t *entry = NULL;
1963         int index = 0;
1964         char *proc_name_ptr = NULL;
1965         int fd;
1966         int ret;
1967         int i = 0;
1968         GArray *garray = NULL;
1969
1970         *return_code = 0;
1971
1972         garray = g_array_new(false, true, sizeof(alarm_id_t));
1973
1974         /* we should check that there is a resource leak.
1975          * Now we don't have free code for g_array_new().
1976          */
1977         if (max_number_of_ids <= 0) {
1978                 *arr = garray;
1979                 ALARM_MGR_EXCEPTION_PRINT("called for  pid(%d), but "
1980                                           "max_number_of_ids(%d) is less than 0.\n",
1981                                           pid, max_number_of_ids);
1982                 return true;
1983         }
1984
1985         /* we should consider to check whether  pid is running or Not
1986          */
1987         memset(process_name, '\0', 512);
1988         memset(proc_file, '\0', 256);
1989         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
1990
1991         fd = open(proc_file, O_RDONLY);
1992         if (fd > 0) {
1993                 ret = read(fd, process_name, 512);
1994                 close(fd);
1995                 while (process_name[i] != '\0') {
1996                         if (process_name[i] == ' ') {
1997                                 process_name[i] = '\0';
1998                                 break;
1999                         }
2000                         i++;
2001                 }
2002                 /* if (readlink(proc_file, process_name, 256)!=-1) */
2003                 /*success */
2004
2005                 word = strtok_r(process_name, "/", &proc_name_ptr);
2006                 while (word != NULL) {
2007                         memset(app_name, 0, 256);
2008                         snprintf(app_name, 256, "%s", word);
2009                         word = strtok_r(NULL, "/", &proc_name_ptr);
2010                 }
2011                 quark_app_unique_name = g_quark_from_string(app_name);
2012         } else {                /* failure */
2013
2014                 quark_app_unique_name = g_quark_from_string("unknown");
2015                 memcpy(app_name, "unknown", strlen("unknown") + 1);
2016
2017                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
2018                 "killed, so we failed to get proc file(%s)\n", pid, proc_file);
2019                 *return_code = -1;
2020                 /* -1 means that system failed internally. */
2021                 return true;
2022         }
2023
2024         ALARM_MGR_LOG_PRINT("called for  app(pid:%d, name=%s)\n",
2025                             pid, app_name);
2026
2027         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2028              gs_iter = g_slist_next(gs_iter)) {
2029                 entry = gs_iter->data;
2030                 if (strcmp
2031                     (app_name,
2032                      g_quark_to_string(entry->quark_app_unique_name)) == 0
2033                     && strcmp(app_name, "unknown") != 0) {
2034                         g_array_append_val(garray, entry->alarm_id);
2035                 }
2036         }
2037
2038         *num_of_ids = index;
2039
2040         *arr = garray;
2041
2042         return true;
2043 }
2044
2045 gboolean alarm_manager_alarm_get_appsvc_info(void *pObject, int pid, alarm_id_t alarm_id,
2046                                 char *e_cookie, gchar **b_data, int *return_code)
2047 {
2048         bool found = false;
2049
2050         GSList *gs_iter = NULL;
2051         __alarm_info_t *entry = NULL;
2052
2053         guchar *cookie = NULL;
2054         gsize size;
2055         int retval = 0;
2056         gid_t call_gid;
2057
2058         ALARM_MGR_LOG_PRINT("called for  pid(%d) and alarm_id(%d)\n", pid,
2059                             alarm_id);
2060
2061         cookie = g_base64_decode(e_cookie, &size);
2062         if (NULL == cookie)
2063         {
2064                 if (return_code)
2065                         *return_code = ERR_ALARM_SYSTEM_FAIL;
2066                 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
2067                 return true;
2068         }
2069         call_gid = security_server_get_gid("alarm");
2070
2071         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
2072
2073         retval = security_server_check_privilege((const char *)cookie, call_gid);
2074         if (retval < 0) {
2075                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
2076                         ALARM_MGR_EXCEPTION_PRINT(
2077                                 "%s", "access has been denied\n");
2078                 }
2079                 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
2080
2081                 if (return_code)
2082                         *return_code = ERR_ALARM_NO_PERMISSION;
2083
2084                 if (cookie)
2085                         g_free(cookie);
2086
2087                 return true;
2088         }
2089
2090         if (return_code)
2091                 *return_code = 0;
2092
2093         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2094              gs_iter = g_slist_next(gs_iter)) {
2095                 entry = gs_iter->data;
2096                 if (entry->alarm_id == alarm_id) {
2097                         found = true;
2098                         *b_data = g_strdup(g_quark_to_string(entry->quark_bundle));
2099                         break;
2100                 }
2101         }
2102
2103         if (found) {
2104                 if ( *b_data && strlen(*b_data) == 4 && strncmp(*b_data,"null",4) == 0){
2105                         ALARM_MGR_EXCEPTION_PRINT("Regular alarm,not svc alarm");
2106                         if (return_code)
2107                                 *return_code = ERR_ALARM_INVALID_TYPE;
2108                 }
2109         } else {
2110                 if (return_code)
2111                         *return_code = ERR_ALARM_INVALID_ID;
2112         }
2113
2114         if (cookie)
2115                 g_free(cookie);
2116
2117         return true;
2118 }
2119
2120 gboolean alarm_manager_alarm_get_info(void *pObject, int pid,
2121                                       alarm_id_t alarm_id, int *start_year,
2122                                       int *start_month, int *start_day,
2123                                       int *start_hour, int *start_min,
2124                                       int *start_sec, int *end_year,
2125                                       int *end_month, int *end_day,
2126                                       int *mode_day_of_week, int *mode_repeat,
2127                                       int *alarm_type, int *reserved_info,
2128                                       int *return_code)
2129 {
2130         ALARM_MGR_LOG_PRINT("called for  pid(%d) and alarm_id(%d)\n", pid,
2131                             alarm_id);
2132
2133         GSList *gs_iter = NULL;
2134         __alarm_info_t *entry = NULL;
2135
2136         alarm_info_t *alarm_info = NULL;
2137         *return_code = 0;
2138
2139         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2140              gs_iter = g_slist_next(gs_iter)) {
2141                 entry = gs_iter->data;
2142                 if (entry->alarm_id == alarm_id) {
2143                         alarm_info = &(entry->alarm_info);
2144                         break;
2145                 }
2146         }
2147
2148         if (alarm_info == NULL)
2149         {
2150                 ALARM_MGR_EXCEPTION_PRINT("alarm id(%d) was not found\n",
2151                                           alarm_id);
2152                 *return_code = ERR_ALARM_INVALID_ID;
2153         } else {
2154                 ALARM_MGR_LOG_PRINT("alarm was found\n");
2155                 *start_year = alarm_info->start.year;
2156                 *start_month = alarm_info->start.month;
2157                 *start_day = alarm_info->start.day;
2158                 *start_hour = alarm_info->start.hour;
2159                 *start_min = alarm_info->start.min;
2160                 *start_sec = alarm_info->start.sec;
2161
2162                 *end_year = alarm_info->end.year;
2163                 *end_year = alarm_info->end.month;
2164                 *end_year = alarm_info->end.day;
2165
2166                 *mode_day_of_week = alarm_info->mode.u_interval.day_of_week;
2167                 *mode_repeat = alarm_info->mode.repeat;
2168
2169                 *alarm_type = alarm_info->alarm_type;
2170                 *reserved_info = alarm_info->reserved_info;
2171
2172                 *return_code = 0;
2173         }
2174         return true;
2175 }
2176
2177 #include "alarm-skeleton.h"
2178
2179 typedef struct AlarmManagerObject AlarmManagerObject;
2180 typedef struct AlarmManagerObjectClass AlarmManagerObjectClass;
2181 GType Server_Object_get_type(void);
2182 struct AlarmManagerObject {
2183         GObject parent;
2184 };
2185 struct AlarmManagerObjectClass {
2186         GObjectClass parent;
2187 };
2188
2189 #define DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT 0
2190
2191 #define ALARM_MANAGER_TYPE_OBJECT              (Server_Object_get_type())
2192 #define ALARM_MANAGER_OBJECT(object)           (G_TYPE_CHECK_INSTANCE_CAST \
2193 ((object), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObject))
2194 #define ALARM_MANAGER_OBJECT_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST \
2195 ((klass), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObjectClass))
2196 #define ALARM_MANAGER_IS_OBJECT(object)        (G_TYPE_CHECK_INSTANCE_TYPE \
2197 ((object), ALARM_MANAGER_TYPE_OBJECT))
2198 #define ALARM_MANAGER_IS_OBJECT_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE \
2199 ((klass), ALARM_MANAGER_TYPE_OBJECT))
2200 #define ALARM_MANAGER_OBJECT_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS \
2201 ((obj), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObjectClass))
2202 G_DEFINE_TYPE(AlarmManagerObject, Server_Object, G_TYPE_OBJECT)
2203 static void Server_Object_init(AlarmManagerObject * obj)
2204 {
2205         ;
2206 }
2207
2208 static void Server_Object_class_init(AlarmManagerObjectClass *klass)
2209 {
2210         ;
2211 }
2212
2213 static void __initialize_timer()
2214 {
2215         struct sigaction sig_timer;
2216         sigemptyset(&sig_timer.sa_mask);
2217         sig_timer.sa_flags = SA_SIGINFO;
2218         sig_timer.sa_sigaction = (void *)__alarm_handler;
2219         sigaction(SIG_TIMER, &sig_timer, NULL);
2220
2221         alarm_context.timer = _alarm_create_timer();
2222
2223 }
2224
2225 static void __initialize_alarm_list()
2226 {
2227
2228         alarm_context.alarms = NULL;
2229         alarm_context.c_due_time = -1;
2230
2231         _load_alarms_from_registry();
2232
2233         __rtc_set();    /*Set RTC1 Alarm with alarm due time for alarm-manager initialization*/
2234
2235         /*alarm boot */
2236 #ifdef __ALARM_BOOT
2237         /*alarm boot */
2238         if (enable_power_on_alarm) {
2239                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
2240                  * platform with app-server. because __alarm_power_on(..) fuction don't 
2241                  * use first parameter internally, we set this value to 0(zero)
2242                  */
2243                 __alarm_power_on(0, enable_power_on_alarm, NULL);
2244         }
2245 #endif
2246 }
2247
2248 static void __initialize_scheduled_alarm_lsit()
2249 {
2250         _init_scheduled_alarm_list();
2251 }
2252
2253
2254 static void __hibernation_leave_callback()
2255 {
2256
2257         __initialize_scheduled_alarm_lsit();
2258
2259         __alarm_clean_list();
2260
2261         __initialize_alarm_list();
2262 }
2263
2264 static bool __initialize_noti()
2265 {
2266
2267         int fd = heynoti_init();
2268         if (fd < 0) {
2269                 ALARM_MGR_EXCEPTION_PRINT("fail to heynoti_init\n");
2270                 return false;
2271         }
2272         heynoti_subscribe(fd, "HIBERNATION_LEAVE", __hibernation_leave_callback,
2273                           NULL);
2274         heynoti_attach_handler(fd);
2275
2276         if (vconf_notify_key_changed
2277             (VCONFKEY_SYSTEM_TIMECHANGE, __on_system_time_changed, NULL) < 0) {
2278                 ALARM_MGR_LOG_PRINT(
2279                         "Failed to add callback for time changing event\n");
2280         }
2281         /*system state change noti ÃƒÂ³Â¸Â® */
2282
2283         return true;
2284 }
2285
2286
2287 static DBusHandlerResult __alarm_server_filter(DBusConnection *connection,
2288                                                DBusMessage *message,
2289                                                void *user_data)
2290 {
2291
2292         if (dbus_message_is_signal
2293             (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
2294                 char *service;
2295                 char *old_owner;
2296                 char *new_owner;
2297                 GSList *entry;
2298                 __expired_alarm_t *expire_info;
2299
2300                 dbus_message_get_args(message,
2301                                       NULL,
2302                                       DBUS_TYPE_STRING, &service,
2303                                       DBUS_TYPE_STRING, &old_owner,
2304                                       DBUS_TYPE_STRING, &new_owner,
2305                                       DBUS_TYPE_INVALID);
2306
2307                 for (entry = g_expired_alarm_list; entry; entry = entry->next) {
2308                         if (entry->data) {
2309                                 expire_info = (__expired_alarm_t *) entry->data;
2310
2311                                 if (strcmp(expire_info->service_name, service)
2312                                     == 0) {
2313                                         ALARM_MGR_EXCEPTION_PRINT(
2314                                         "__alarm_server_filter : "
2315                                              "service name(%s) alarm_id (%d)\n",
2316                                              expire_info->service_name,\
2317                                              expire_info->alarm_id);
2318
2319                                         __alarm_send_noti_to_application(
2320                                              expire_info->service_name,
2321                                              expire_info->alarm_id);
2322                                         g_expired_alarm_list =
2323                                             g_slist_remove(g_expired_alarm_list,
2324                                                            entry->data);
2325                                         free(expire_info);
2326                                 }
2327                         }
2328                 }
2329
2330                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2331         }
2332
2333         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2334 }
2335
2336 static bool __initialize_dbus()
2337 {
2338         GError *error = NULL;
2339         GObject *obj = NULL;
2340         DBusGConnection *connection = NULL;
2341         DBusError derror;
2342         int request_name_result = 0;
2343
2344         dbus_g_object_type_install_info(ALARM_MANAGER_TYPE_OBJECT,
2345                                         &dbus_glib_alarm_manager_object_info);
2346
2347         connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
2348         if (!connection) {
2349
2350                 ALARM_MGR_EXCEPTION_PRINT("dbus_g_bus_get failed\n");
2351                 return false;
2352         }
2353
2354         dbus_error_init(&derror);
2355
2356         request_name_result =
2357             dbus_bus_request_name(dbus_g_connection_get_connection(connection),
2358                                   "org.tizen.alarm.manager",
2359                                   DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &derror);
2360         if (dbus_error_is_set(&derror)) {       /* failure */
2361                 ALARM_MGR_EXCEPTION_PRINT("Failed to dbus_bus_request_name "
2362                 "(org.tizen.alarm.manager): %s\n", derror.message);
2363                 dbus_error_free(&derror);
2364                 return false;
2365         }
2366
2367         if (!(obj = g_object_new(ALARM_MANAGER_TYPE_OBJECT, NULL))) {
2368                 ALARM_MGR_EXCEPTION_PRINT("Could not allocate new object\n");
2369                 return false;
2370         }
2371
2372         dbus_g_connection_register_g_object(connection,
2373                                             "/org/tizen/alarm/manager",
2374                                             G_OBJECT(obj));
2375
2376         /*dbus_bus_add_match (dbus_g_connection_get_connection(connection),
2377            "type='signal',member='NameOwnerChanged'",NULL); */
2378
2379         dbus_bus_add_match(dbus_g_connection_get_connection(connection),
2380                            "type='signal',sender='" DBUS_SERVICE_DBUS
2381                            "',path='" DBUS_PATH_DBUS
2382                            "',interface='" DBUS_INTERFACE_DBUS
2383                            "',member='NameOwnerChanged'", NULL);
2384
2385         if (!dbus_connection_add_filter
2386             (dbus_g_connection_get_connection(connection),
2387              __alarm_server_filter, NULL, NULL)) {
2388                 ALARM_MGR_EXCEPTION_PRINT("add __expire_alarm_filter failed\n");
2389
2390                 return false;
2391         }
2392
2393         alarm_context.bus = connection;
2394         return true;
2395 }
2396
2397 #define ALARMMGR_DB_FILE "/opt/dbspace/.alarmmgr.db"
2398 sqlite3 *alarmmgr_db;
2399 #define QUERY_CREATE_TABLE_ALARMMGR "create table alarmmgr \
2400                                 (alarm_id integer primary key,\
2401                                                 start integer,\
2402                                                 end integer,\
2403                                                 pid integer,\
2404                                                 app_unique_name text,\
2405                                                 app_service_name text,\
2406                                                 app_service_name_mod text,\
2407                                                 bundle text, \
2408                                                 year integer,\
2409                                                 month integer,\
2410                                                 day integer,\
2411                                                 hour integer,\
2412                                                 min integer,\
2413                                                 sec integer,\
2414                                                 day_of_week integer,\
2415                                                 repeat integer,\
2416                                                 alarm_type integer,\
2417                                                 reserved_info integer,\
2418                                                 dst_service_name text, \
2419                                                 dst_service_name_mod text \
2420                                                 )"
2421
2422 static bool __initialize_db()
2423 {
2424         char *error_message = NULL;
2425         int ret;
2426
2427         if (access("/opt/dbspace/.alarmmgr.db", F_OK) == 0) {
2428                 ret =
2429                     db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db,
2430                                  DB_UTIL_REGISTER_HOOK_METHOD);
2431
2432                 if (ret != SQLITE_OK) {
2433                         ALARM_MGR_EXCEPTION_PRINT(
2434                             "====>>>> connect menu_db [%s] failed!\n",
2435                              ALARMMGR_DB_FILE);
2436                         return false;
2437                 }
2438
2439                 return true;
2440         }
2441
2442         ret =
2443             db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db,
2444                          DB_UTIL_REGISTER_HOOK_METHOD);
2445
2446         if (ret != SQLITE_OK) {
2447                 ALARM_MGR_EXCEPTION_PRINT(
2448                     "====>>>> connect menu_db [%s] failed!\n",
2449                      ALARMMGR_DB_FILE);
2450                 return false;
2451         }
2452
2453         if (SQLITE_OK !=
2454             sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL,\
2455                          &error_message)) {
2456                 ALARM_MGR_EXCEPTION_PRINT("Don't execute query = %s, "
2457                 "error message = %s\n", QUERY_CREATE_TABLE_ALARMMGR, 
2458                                           error_message);
2459                 return false;
2460         }
2461
2462         return true;
2463 }
2464
2465 static void __initialize()
2466 {
2467
2468         g_type_init();
2469 #ifdef __ALARM_BOOT
2470         FILE *fp;
2471         char temp[2];
2472         int size;
2473
2474         fp = fopen("/proc/alarm_boot", "r");
2475         if (fp == NULL)
2476                 alarm_boot = 0;
2477         else {
2478
2479                 size = fread(&temp, 1, 1, fp);
2480                 if (size != 1)
2481                         alarm_boot = 0;
2482                 else {
2483                         temp[1] = 0;
2484                         alarm_boot = atoi(temp);
2485                 }
2486                 fclose(fp);
2487         }
2488 #endif
2489
2490         int fd;
2491         int fd2;
2492         struct rtc_time rtc_tm;
2493         int retval;
2494
2495         fd = open(power_rtc, O_RDONLY);
2496         if (fd < 0) {
2497                 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc0\n");
2498                 return;
2499         }
2500         retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
2501         close(fd);
2502
2503         fd2 = open(default_rtc, O_RDWR);
2504         if (fd2 < 0) {
2505                 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc1\n");
2506                 return;
2507         }
2508         retval = ioctl(fd2, RTC_SET_TIME, &rtc_tm);
2509         close(fd2);
2510
2511         __initialize_timer();
2512         if (__initialize_dbus() == false) {     /* because dbus's initialize 
2513                                         failed, we cannot continue any more. */
2514                 ALARM_MGR_EXCEPTION_PRINT("because __initialize_dbus failed, "
2515                                           "alarm-server cannot be runned.\n");
2516                 exit(1);
2517         }
2518         __initialize_scheduled_alarm_lsit();
2519         __initialize_db();
2520         __initialize_alarm_list();
2521         __initialize_noti();
2522
2523 }
2524
2525 #ifdef __ALARM_BOOT
2526 static bool __check_false_alarm()
2527 {
2528         time_t current_time;
2529         time_t interval;
2530
2531         time(&current_time);
2532
2533         interval = ab_due_time - current_time;
2534
2535         ALARM_MGR_LOG_PRINT("due_time : %d / current_time %d\n", \
2536                             alarm_context.c_due_time, current_time);
2537
2538         if (interval > 0 && interval <= 30) {
2539                 return true;
2540         } else if (!poweron_alarm_expired) {
2541                 /* originally, first arguement's value was 121(app_id) which means 
2542                  * alarm_booting ui application.and this application's dbus-service 
2543                  * name had a org.tizen.alarmboot.ui in a platform with app-server.
2544                  * so we set "org.tizen.alarmboot.ui.ALARM" instead of 121.
2545                  */
2546                 __alarm_send_noti_to_application(
2547                         WAKEUP_ALARMBOOTING_APP_ID, -1);
2548                 return false;
2549         }
2550
2551         return true;
2552 }
2553 #endif
2554
2555 int main()
2556 {
2557         GMainLoop *mainloop = NULL;
2558
2559         ALARM_MGR_LOG_PRINT("Enter main loop\n");
2560
2561         mainloop = g_main_loop_new(NULL, FALSE);
2562
2563         __initialize();
2564
2565 #ifdef __ALARM_BOOT
2566         if (alarm_boot){
2567                 __check_false_alarm();
2568         }
2569 #endif
2570
2571         g_main_loop_run(mainloop);
2572
2573         return 0;
2574 }