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