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