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