fixed prevent issues
[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) {
696                 ret = read(fd, process_name, 512);
697                 close(fd);
698                 if (ret <=0)
699                 {
700                         ALARM_MGR_EXCEPTION_PRINT("Unable to get application name\n");
701                         *error_code = -1;
702                         free(__alarm_info);
703                         return false;
704                 }
705                 while (process_name[i] != '\0') {
706                         if (process_name[i] == ' ') {
707                                 process_name[i] = '\0';
708                                 break;
709                         }
710                         i++;
711                 }
712
713                 word = strtok_r(process_name, "/", &proc_name_ptr);
714                 while (word != NULL) {
715                         memset(app_name, 0, 512);
716                         snprintf(app_name, 512, "%s", word);
717                         word = strtok_r(NULL, "/", &proc_name_ptr);
718                 }
719                 __alarm_info->quark_app_unique_name =
720                     g_quark_from_string(app_name);
721         } else {                /* failure */
722                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
723                                           "killed, so we failed to get proc file(%s) and do not create "
724                                           "alarm_info\n", pid, proc_file);
725                 *error_code = -1;       /*-1 means that system failed
726                                                         internally.*/
727                 free(__alarm_info);
728                 return false;
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) {
842                 ret = read(fd, process_name, 512);
843                 close(fd);
844                 while (process_name[i] != '\0') {
845                         if (process_name[i] == ' ') {
846                                 process_name[i] = '\0';
847                                 break;
848                         }
849                         i++;
850                 }
851                 /* if (readlink(proc_file, process_name, 256)!=-1) */
852                 /*success */
853
854                 word = strtok_r(process_name, "/", &proc_name_ptr);
855                 while (word != NULL) {
856                         memset(app_name, 0, 256);
857                         snprintf(app_name, 256, "%s", word);
858                         word = strtok_r(NULL, "/", &proc_name_ptr);
859                 }
860                 __alarm_info->quark_app_unique_name =
861                     g_quark_from_string(app_name);
862         } else {                /* failure */
863
864                 __alarm_info->quark_app_unique_name =
865                     g_quark_from_string("unknown");
866                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
867                                           "killed, so we failed to get proc file(%s) and do not create "
868                                           "alarm_info\n", pid, proc_file);
869                 *error_code = -1;       /*-1 means that system failed
870                                                         internally.*/
871                 free(__alarm_info);
872                 return false;
873         }
874
875         __alarm_info->quark_app_service_name =
876             g_quark_from_string(app_service_name);
877         __alarm_info->quark_app_service_name_mod =
878         g_quark_from_string(app_service_name_mod);
879         __alarm_info->quark_dst_service_name =
880             g_quark_from_string(dst_service_name);
881         __alarm_info->quark_dst_service_name_mod =
882             g_quark_from_string(dst_service_name_mod);
883         __alarm_info->quark_bundle = g_quark_from_string("null");
884
885
886         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
887         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
888
889         time(&current_time);
890
891         ALARM_MGR_LOG_PRINT("[alarm-server]:pid=%d, app_unique_name=%s, "
892                 "app_service_name=%s,dst_service_name=%s, c_due_time=%d", \
893                 pid, g_quark_to_string(__alarm_info->quark_app_unique_name), \
894                 g_quark_to_string(__alarm_info->quark_app_service_name), \
895                 g_quark_to_string(__alarm_info->quark_dst_service_name), \
896                             alarm_context.c_due_time);
897
898         if (alarm_context.c_due_time < current_time) {
899                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
900                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
901                                           current_time);
902                 alarm_context.c_due_time = -1;
903         }
904
905         due_time = _alarm_next_duetime(__alarm_info);
906         if (__alarm_add_to_list(__alarm_info, alarm_id) == false) {
907                 free(__alarm_info);
908                 return false;
909         }
910
911         if (due_time == 0) {
912                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
913                 "due_time is 0, alarm(%d) \n", *alarm_id);
914                 return true;
915         } else if (current_time == due_time) {
916                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
917                      "current_time(%d) is same as due_time(%d)", current_time,
918                      due_time);
919                 return true;
920         }else if (difftime(due_time, current_time) <  0){
921                 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);
922                 return true;
923         } else {
924                 char due_time_r[100] = { 0 };
925                 struct tm ts_ret;
926                 localtime_r(&due_time, &ts_ret);
927                 strftime(due_time_r, 30, "%c", &ts_ret);
928                 ALARM_MGR_LOG_PRINT("[alarm-server]:Create a new alarm: "
929                                     "alarm(%d) due_time(%s)", *alarm_id,
930                                     due_time_r);
931         }
932
933         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
934                             "due_time(%d)", alarm_context.c_due_time, due_time);
935
936         if (alarm_context.c_due_time == -1
937             || due_time < alarm_context.c_due_time) {
938                 _clear_scheduled_alarm_list();
939                 _add_to_scheduled_alarm_list(__alarm_info);
940                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
941                                  *alarm_id);
942                 alarm_context.c_due_time = due_time;
943
944         } else if (due_time == alarm_context.c_due_time) {
945                 _add_to_scheduled_alarm_list(__alarm_info);
946
947         }
948
949         __rtc_set();
950
951 #ifdef __ALARM_BOOT
952         /*alarm boot */
953         if (enable_power_on_alarm) {
954                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
955                  * platform with app-server.because __alarm_power_on(..) fuction don't
956                  * use first parameter internally, we set this value to 0(zero)
957                  */
958                 __alarm_power_on(0, enable_power_on_alarm, NULL);
959         }
960 #endif
961         return true;
962 }
963
964 static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
965                            alarm_info_t *alarm_info, int *error_code)
966 {
967         time_t current_time;
968         time_t due_time;
969
970         __alarm_info_t *__alarm_info = NULL;
971         bool result = false;
972
973         __alarm_info = malloc(sizeof(__alarm_info_t));
974         if (__alarm_info == NULL) {
975                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, "
976                         "malloc failed. it seems to be OOM\n", pid);
977                 *error_code = -1;       /*-1 means that system failed
978                                                 internally.*/
979                 return false;
980         }
981
982         __alarm_info->pid = pid;
983         __alarm_info->alarm_id = alarm_id;
984
985         /* we should consider to check whether  pid is running or Not
986          */
987
988         __alarm_info->quark_app_service_name =
989             g_quark_from_string(app_service_name);
990         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
991         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
992
993         time(&current_time);
994
995         if (alarm_context.c_due_time < current_time) {
996                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
997                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
998                                           current_time);
999                 alarm_context.c_due_time = -1;
1000         }
1001
1002         due_time = _alarm_next_duetime(__alarm_info);
1003         if (!__alarm_update_in_list(pid, alarm_id, __alarm_info, error_code)) {
1004                 free(__alarm_info);
1005                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: requested alarm_id "
1006                 "(%d) does not exist. so this value is invalid id.", alarm_id);
1007                 return false;
1008         }
1009         /* ALARM_MGR_LOG_PRINT("[alarm-server]:request_pid=%d, alarm_id=%d,
1010          * app_unique_name=%s, app_service_name=%s, dst_service_name=%s,
1011          * c_due_time=%d", pid, alarm_id, g_quark_to_string
1012          * (__alarm_info->quark_app_unique_name), g_quark_to_string
1013          * (__alarm_info->quark_app_service_name), g_quark_to_string
1014          * (__alarm_info->quark_dst_service_name), alarm_context.c_due_time);
1015          */
1016
1017         result = _remove_from_scheduled_alarm_list(pid, alarm_id);
1018
1019         if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
1020                 /*there is no scheduled alarm */
1021                 _alarm_disable_timer(alarm_context);
1022                 _alarm_schedule();
1023
1024                 ALARM_MGR_LOG_PRINT("[alarm-server]:Update alarm: alarm(%d)\n",
1025                                     alarm_id);
1026
1027                 __rtc_set();
1028
1029 #ifdef __ALARM_BOOT
1030                 /*alarm boot */
1031                 if (enable_power_on_alarm) {
1032                         /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in
1033                          * a platform with app-server.because __alarm_power_on(..) fuction don't
1034                          * use first parameter internally, we set this value to 0(zero)
1035                          */
1036                         __alarm_power_on(0, enable_power_on_alarm, NULL);
1037                 }
1038 #endif
1039
1040                 if (due_time == 0) {
1041                         ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1042                                         "due_time is 0\n");
1043                 }
1044                 free(__alarm_info);
1045                 return true;
1046         }
1047
1048         if (due_time == 0) {
1049                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1050                                 "due_time is 0, alarm(%d)\n", alarm_id);
1051                 free(__alarm_info);
1052                 return true;
1053         } else if (current_time == due_time) {
1054                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1055                 "current_time(%d) is same as due_time(%d)", current_time,
1056                 due_time);
1057                 free(__alarm_info);
1058                 return true;
1059         }else if (difftime(due_time, current_time)< 0){
1060                 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);
1061                 free(__alarm_info);
1062                 return true;
1063         } else {
1064                 char due_time_r[100] = { 0 };
1065                 struct tm ts_ret;
1066                 localtime_r(&due_time, &ts_ret);
1067                 strftime(due_time_r, 30, "%c", &ts_ret);
1068                 ALARM_MGR_LOG_PRINT("[alarm-server]:Update alarm: alarm(%d) "
1069                                     "due_time(%s)\n", alarm_id, due_time_r);
1070         }
1071
1072         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
1073                             "due_time(%d)", alarm_context.c_due_time, due_time);
1074
1075         if (alarm_context.c_due_time == -1
1076             || due_time < alarm_context.c_due_time) {
1077                 _clear_scheduled_alarm_list();
1078                 _add_to_scheduled_alarm_list(__alarm_info);
1079                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
1080                                  alarm_id);
1081                 alarm_context.c_due_time = due_time;
1082                 ALARM_MGR_LOG_PRINT("[alarm-server1]:alarm_context.c_due_time "
1083                      "(%d), due_time(%d)", alarm_context.c_due_time, due_time);
1084         } else if (due_time == alarm_context.c_due_time) {
1085                 _add_to_scheduled_alarm_list(__alarm_info);
1086                 ALARM_MGR_LOG_PRINT("[alarm-server2]:alarm_context.c_due_time "
1087                      "(%d), due_time(%d)", alarm_context.c_due_time, due_time);
1088         }
1089
1090         __rtc_set();
1091
1092 #ifdef __ALARM_BOOT
1093         /*alarm boot */
1094         if (enable_power_on_alarm) {
1095                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID)
1096                  * in a platform with app-server.because __alarm_power_on(..) fuction
1097                  * don't use first parameter internally, we set this value to 0(zero)
1098                  */
1099                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1100         }
1101 #endif
1102         free(__alarm_info);
1103
1104         return true;
1105 }
1106
1107 static bool __alarm_delete(int pid, alarm_id_t alarm_id, int *error_code)
1108 {
1109         bool result = false;
1110
1111         ALARM_MGR_LOG_PRINT("[alarm-server]:delete alarm: alarm(%d) pid(%d)\n",\
1112                             alarm_id, pid);
1113         result = _remove_from_scheduled_alarm_list(pid, alarm_id);
1114
1115         if (!__alarm_remove_from_list(pid, alarm_id, error_code)) {
1116
1117                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:delete alarm: "
1118                                           "alarm(%d) pid(%d) has failed with error_code(%d)\n",
1119                                           alarm_id, pid, *error_code);
1120                 return false;
1121         }
1122
1123         if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
1124                 _alarm_disable_timer(alarm_context);
1125                 _alarm_schedule();
1126         }
1127
1128         __rtc_set();
1129
1130 #ifdef __ALARM_BOOT
1131         /*alarm boot */
1132         if (enable_power_on_alarm) {
1133                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
1134                  * platform with app-server.because __alarm_power_on(..) fuction don't
1135                  * use first parameter internally, we set this value to 0(zero)
1136                  */
1137                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1138         }
1139 #endif
1140
1141         return true;
1142 }
1143
1144 static bool __alarm_power_on(int app_id, bool on_off, int *error_code)
1145 {
1146 #ifdef __ALARM_BOOT
1147         time_t min_time = 0;
1148         time_t current_time = 0;
1149         struct tm *temp_info = NULL;
1150         struct rtc_time rtc_tm = { 0, };
1151         struct tm min_time_r = { 0, };
1152         int fd = 0;
1153         int retval;
1154
1155         enable_power_on_alarm = on_off;
1156         /*_update_power_on(on_off); */ /*currently its empty*/
1157
1158         fd = open(power_rtc, O_RDONLY);
1159         if (fd < 0) {
1160                 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc0\n");
1161                 return false;
1162         }
1163
1164         if (on_off == true) {
1165                 if (_alarm_find_mintime_power_on(&min_time) == true) {
1166
1167                         ab_due_time = min_time;
1168
1169                         min_time = min_time - 60;
1170
1171                         time(&current_time);
1172
1173                         if (min_time <= current_time)
1174                                 min_time = current_time + 5;
1175
1176                         temp_info = gmtime_r(&min_time, &min_time_r);
1177
1178                         ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
1179                                                 "%d\n", \
1180                                            min_time_r.tm_year,\
1181                                            min_time_r.tm_mon,\
1182                                            min_time_r.tm_mday,\
1183                                            min_time_r.tm_hour,\
1184                                            min_time_r.tm_min);
1185                         ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
1186                                                 "%d\n", \
1187                                            min_time_r.tm_year,\
1188                                            min_time_r.tm_mon,\
1189                                            min_time_r.tm_mday,\
1190                                            min_time_r.tm_hour,\
1191                                            min_time_r.tm_min);
1192
1193                         rtc_tm.tm_mday = min_time_r.tm_mday;
1194                         rtc_tm.tm_mon = min_time_r.tm_mon;
1195                         rtc_tm.tm_year = min_time_r.tm_year;
1196                         rtc_tm.tm_hour = min_time_r.tm_hour;
1197                         rtc_tm.tm_min = min_time_r.tm_min;
1198                         rtc_tm.tm_sec = min_time_r.tm_sec;
1199                         /*set_info.time_zone = 0; */
1200                         /*set_info.u_interval.day_of_week = 0; */
1201
1202                         /*ALARM_MGR_LOG_PRINT("####__alarm_power_on : %d %d
1203                         %d %d %d\n",set_info.year,set_info.month,set_info.day,
1204                         set_info.hour,set_info.minute); */
1205
1206                         ALARM_MGR_LOG_PRINT("due_time : %d \n", ab_due_time);
1207
1208                         ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
1209                                             "%d-%d-%d, %02d:%02d:%02d.\n",
1210                                             rtc_tm.tm_mday, rtc_tm.tm_mon + 1,
1211                                 rtc_tm.tm_year + 1900, rtc_tm.tm_hour,
1212                                 rtc_tm.tm_min, rtc_tm.tm_sec);
1213
1214                         retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
1215                         if (retval == -1) {
1216                                 if (errno == ENOTTY) {
1217                                         ALARM_MGR_EXCEPTION_PRINT(
1218                                         "\n...Alarm IRQs not supported.\n");
1219                                 }
1220                                 ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
1221                                 close(fd);
1222                                 return false;
1223                         }
1224                         ALARM_MGR_LOG_PRINT("[alarm-server]RTC "
1225                                             "alarm(POWER ON) is setted");
1226
1227                         /* Enable alarm interrupts */
1228                         retval = ioctl(fd, RTC_AIE_ON, 0);
1229                         if (retval == -1) {
1230                                 ALARM_MGR_EXCEPTION_PRINT(
1231                                     "RTC_AIE_ON ioctl failed");
1232                                 close(fd);
1233                                 return false;
1234                         }
1235                         ALARM_MGR_LOG_PRINT("[alarm-server]RTC(POWER ON) "
1236                                             "alarm is on");
1237
1238                 } else
1239                         retval = ioctl(fd, RTC_AIE_OFF, 0);
1240         } else {
1241                 ALARM_MGR_LOG_PRINT("__alarm_power_on : off\n");
1242                 retval = ioctl(fd, RTC_AIE_OFF, 0);
1243         }
1244
1245         close(fd);
1246 #endif                          /* #ifdef __ALARM_BOOT */
1247
1248         return true;
1249 }
1250
1251 static bool __alarm_power_off(int app_id, int *error_code)
1252 {
1253 #ifdef __ALARM_BOOT
1254
1255 #endif                          /* #ifdef __ALARM_BOOT */
1256         return true;
1257 }
1258
1259 static bool __alarm_check_next_duetime(int app_id, int *error_code)
1260 {
1261 #ifdef __ALARM_BOOT
1262         time_t current_time;
1263         time_t interval;
1264
1265         time(&current_time);
1266
1267         interval = ab_due_time - current_time;
1268
1269         ALARM_MGR_LOG_PRINT("due_time : %d / current_time %d\n", ab_due_time,
1270                             current_time);
1271
1272         if (interval > 0 && interval <= 60)
1273                 return true;
1274         else
1275                 return false;
1276 #else                           /* #ifdef __ALARM_BOOT */
1277         return true;
1278 #endif
1279 }
1280
1281 static void __alarm_send_noti_to_application(const char *app_service_name,
1282                                              alarm_id_t alarm_id)
1283 {
1284
1285
1286         char service_name[MAX_SERVICE_NAME_LEN];
1287         char object_name[MAX_SERVICE_NAME_LEN];
1288
1289         DBusMessage *message;
1290         DBusMessageIter iter;
1291
1292         if (app_service_name == NULL || strlen(app_service_name) == 0) {
1293                 ALARM_MGR_EXCEPTION_PRINT("This alarm destination is "
1294                                           "invalid\n");
1295                 return;
1296         }
1297         memset(service_name, 0, MAX_SERVICE_NAME_LEN);
1298         memcpy(service_name, app_service_name, strlen(app_service_name));
1299
1300         snprintf(object_name, MAX_SERVICE_NAME_LEN,
1301                  "/org/tizen/alarm/client");
1302
1303         ALARM_MGR_LOG_PRINT("[alarm server][send expired_alarm(alarm_id=%d)to"
1304         "app_service_name(%s), object_name(%s), interface_name(%s)]\n",\
1305         alarm_id, service_name, object_name, "org.tizen.alarm.client");
1306
1307         message = dbus_message_new_method_call(service_name,
1308                                                object_name,
1309                                                "org.tizen.alarm.client",
1310                                                "alarm_expired");
1311         if (message == NULL) {
1312                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
1313                         "dbus_message_new_method_call faild. maybe OOM!.\n");
1314                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] so we cannot "
1315                         "send expired alarm to %s\n", service_name);
1316                 return;
1317         }
1318
1319         dbus_message_set_no_reply(message, TRUE);
1320         /*      if(service_name[0]==':') */
1321         /* we don't need auto activation in a case that
1322            destination_app_service_name starts with a charactor like (:) */
1323         dbus_message_set_auto_start(message, FALSE);
1324
1325         dbus_message_iter_init_append(message, &iter);
1326         if (!dbus_message_iter_append_basic
1327                 (&iter, DBUS_TYPE_INT32, &alarm_id)) {
1328                 dbus_message_unref(message);
1329                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
1330                 "dbus_message_iter_append_basic faild. maybe OOM!.\n");
1331                 return;
1332         }
1333
1334         dbus_connection_send(dbus_g_connection_get_connection
1335                              (alarm_context.bus), message, NULL);
1336         dbus_connection_flush(dbus_g_connection_get_connection
1337                               (alarm_context.bus));
1338         dbus_message_unref(message);
1339
1340 }
1341
1342 static void __alarm_expired()
1343 {
1344         const char *destination_app_service_name = NULL;
1345         alarm_id_t alarm_id = -1;
1346         int app_pid = 0;
1347         __alarm_info_t *__alarm_info = NULL;
1348         char alarm_id_val[32]={0,};
1349         int b_len = 0;
1350         bundle *b = NULL;
1351
1352         ALARM_MGR_LOG_PRINT("[alarm-server]: Enter \n");
1353
1354         time_t current_time;
1355         double interval;
1356
1357         time(&current_time);
1358
1359         interval = difftime(alarm_context.c_due_time, current_time);
1360         ALARM_MGR_LOG_PRINT("[alarm-server]: c_due_time(%d), "
1361                 "current_time(%d), interval(%d)\n", alarm_context.c_due_time,
1362                 current_time, interval);
1363
1364         if (alarm_context.c_due_time > current_time) {
1365                 ALARM_MGR_LOG_PRINT("[alarm-server]: False Alarm\n");
1366                 goto done;
1367         }
1368
1369         GSList *iter = NULL;
1370         __scheduled_alarm_t *alarm = NULL;
1371
1372         for (iter = g_scheduled_alarm_list; iter != NULL;
1373              iter = g_slist_next(iter)) {
1374                 alarm = iter->data;
1375                 alarm_id = alarm->alarm_id;
1376
1377                 __alarm_info = alarm->__alarm_info;
1378
1379                 app_pid = __alarm_info->pid;
1380
1381                 if (strncmp
1382                     (g_quark_to_string(__alarm_info->quark_bundle),
1383                      "null", 4) != 0) {
1384
1385                                 b_len = strlen(g_quark_to_string(__alarm_info->quark_bundle));
1386
1387                                 b = bundle_decode((bundle_raw *)g_quark_to_string(__alarm_info->quark_bundle), b_len);
1388
1389                                 if (NULL == b)
1390                                 {
1391                                         ALARM_MGR_EXCEPTION_PRINT("Error!!!..Unable to decode the bundle!!\n");
1392                                 }
1393                                 else
1394                                 {
1395                                         snprintf(alarm_id_val,31,"%d",alarm_id);
1396
1397                                         if (bundle_add(b,"http://tizen.org/appcontrol/data/alarm_id", alarm_id_val)){
1398                                                 ALARM_MGR_EXCEPTION_PRINT("Unable to add alarm id to the bundle\n");
1399                                         }
1400                                         else
1401                                         {
1402                                                 if ( appsvc_run_service(b, 0, NULL, NULL) < 0)
1403                                                 {
1404                                                         ALARM_MGR_EXCEPTION_PRINT("Unable to run app svc\n");
1405                                                 }
1406                                                 else
1407                                                 {
1408                                                         ALARM_MGR_LOG_PRINT("Successfuly ran app svc\n");
1409                                                 }
1410                                         }
1411                                         bundle_free(b);
1412                                 }
1413
1414                 }
1415                 else
1416                 {
1417                         if (strncmp
1418                             (g_quark_to_string(__alarm_info->quark_dst_service_name),
1419                              "null",4) == 0) {
1420                                 ALARM_MGR_LOG_PRINT("[alarm-server]:destination is "
1421                                 "null, so we send expired alarm to %s(%u)\n",\
1422                                         g_quark_to_string(
1423                                         __alarm_info->quark_app_service_name),
1424                                         __alarm_info->quark_app_service_name);
1425                                         destination_app_service_name = g_quark_to_string(
1426                                         __alarm_info->quark_app_service_name_mod);
1427                         } else {
1428                                 ALARM_MGR_LOG_PRINT("[alarm-server]:destination "
1429                                                     ":%s(%u)\n",
1430                                         g_quark_to_string(
1431                                         __alarm_info->quark_dst_service_name),
1432                                         __alarm_info->quark_dst_service_name);
1433                                         destination_app_service_name = g_quark_to_string(
1434                                                 __alarm_info->quark_dst_service_name_mod);
1435                         }
1436
1437 #ifdef __ALARM_BOOT
1438                         /* orginally this code had if(__alarm_info->app_id==21) in a
1439                            platform with app-server. */
1440                         /*if(__alarm_info->quark_dst_service_name  ==
1441                            g_quark_from_string (WAKEUP_ALARM_APP_ID)) */
1442                         if (strcmp
1443                             (g_quark_to_string(__alarm_info->quark_dst_service_name),
1444                              WAKEUP_ALARM_APP_ID) == 0) {
1445                                 int fd = 0;
1446                                 fd = open(power_rtc, O_RDONLY);
1447                                 if (fd < 0) {
1448                                         ALARM_MGR_LOG_PRINT("cannot open /dev/rtc0\n");
1449                                 } else {
1450                                         ioctl(fd, RTC_AIE_OFF, 0);
1451                                         close(fd);
1452                                 }
1453                         }
1454 #endif
1455
1456                         /*
1457                          * we should consider a situation that
1458                          * destination_app_service_name is owner_name like (:xxxx) and
1459                          * application's pid which registered this alarm was killed.In that case,
1460                          * we don't need to send the expire event because the process was killed.
1461                          * this causes needless message to be sent.
1462                          */
1463                         ALARM_MGR_LOG_PRINT("[alarm-server]: "
1464                                             "destination_app_service_name :%s, app_pid=%d\n",
1465                                             destination_app_service_name, app_pid);
1466                         /* the following is a code that checks the above situation.
1467                            please verify this code. */
1468
1469                         if (dbus_bus_name_has_owner(
1470                              dbus_g_connection_get_connection(alarm_context.bus),
1471                              destination_app_service_name, NULL) == FALSE) {
1472                                 __expired_alarm_t *expire_info;
1473                                 char appid[MAX_SERVICE_NAME_LEN] = { 0, };
1474                                 char alarm_id_str[32] = { 0, };
1475
1476                                 expire_info = malloc(sizeof(__expired_alarm_t));
1477                                 if (G_UNLIKELY(NULL == expire_info)){
1478                                         ALARM_MGR_ASSERT_PRINT("[alarm-server]:Malloc failed!Can't notify alarm expiry info\n");
1479                                         goto done;
1480                                 }
1481                                 bzero(expire_info, sizeof (expire_info));
1482                                 strncpy(expire_info->service_name,
1483                                         destination_app_service_name,
1484                                         MAX_SERVICE_NAME_LEN-1);
1485                                 expire_info->alarm_id = alarm_id;
1486                                 g_expired_alarm_list =
1487                                     g_slist_append(g_expired_alarm_list, expire_info);
1488
1489
1490                                 if (strncmp
1491                                         (g_quark_to_string(__alarm_info->quark_dst_service_name),
1492                                              "null",4) == 0) {
1493                                         strncpy(appid,g_quark_to_string(__alarm_info->quark_app_service_name),strlen(g_quark_to_string(__alarm_info->quark_app_service_name))-6);
1494                                 }
1495                                 else
1496                                 {
1497                                         strncpy(appid,g_quark_to_string(__alarm_info->quark_dst_service_name),strlen(g_quark_to_string(__alarm_info->quark_dst_service_name))-6);
1498                                 }
1499
1500                                 snprintf(alarm_id_str, 31, "%d", alarm_id);
1501
1502                                 ALARM_MGR_LOG_PRINT("before aul_launch appid(%s) "
1503                                         "alarm_id_str(%s)\n", appid, alarm_id_str);
1504
1505                                 bundle *kb;
1506                                 kb = bundle_create();
1507                                 bundle_add(kb, "__ALARM_MGR_ID", alarm_id_str);
1508                                 aul_launch_app(appid, kb);
1509                                 bundle_free(kb);
1510                         } else {
1511                                 ALARM_MGR_LOG_PRINT(
1512                                         "before alarm_send_noti_to_application\n");
1513                                 __alarm_send_noti_to_application(
1514                                              destination_app_service_name, alarm_id);
1515                         }
1516                 }
1517                 ALARM_MGR_LOG_PRINT("after __alarm_send_noti_to_application\n");
1518
1519 /*              if( !(__alarm_info->alarm_info.alarm_type
1520                                         & ALARM_TYPE_VOLATILE) ) {
1521                         __alarm_remove_from_list(__alarm_info->pid,
1522                                                         alarm_id, NULL);
1523                 }
1524                 else */
1525                 if (__alarm_info->alarm_info.mode.repeat
1526                     == ALARM_REPEAT_MODE_ONCE) {
1527 /*                      _alarm_next_duetime(__alarm_info);*/
1528 /*                      _update_alarms(__alarm_info);*/
1529                         __alarm_remove_from_list(__alarm_info->pid, alarm_id,
1530                                                  NULL);
1531                 } else {
1532
1533                         _alarm_next_duetime(__alarm_info);
1534 /*                      _update_alarms(__alarm_info);*/
1535                 }
1536
1537         }
1538
1539  done:
1540         _clear_scheduled_alarm_list();
1541         alarm_context.c_due_time = -1;
1542
1543         ALARM_MGR_LOG_PRINT("[alarm-server]: Leave  \n");
1544 }
1545
1546 static gboolean __alarm_handler_idle()
1547 {
1548         if (g_dummy_timer_is_set == true) {
1549                 ALARM_MGR_LOG_PRINT("dummy alarm timer has expired\n");
1550         } else {
1551                 ALARM_MGR_LOG_PRINT("__alarm_handler \n");
1552                 ALARM_MGR_LOG_PRINT("__alarm_handler \n");
1553
1554                 __alarm_expired();
1555
1556         }
1557
1558         _alarm_schedule();
1559
1560         __rtc_set();
1561
1562 #ifdef __ALARM_BOOT
1563         /*alarm boot */
1564         if (enable_power_on_alarm) {
1565                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID)
1566                  *in a platform with app-server.because __alarm_power_on(..) fuction
1567                  *don't use first parameter internally, we set this value to 0(zero)
1568                  */
1569                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1570         }
1571 #endif
1572         pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
1573         return false;
1574
1575 }
1576
1577 static void __alarm_handler(int sigNum, siginfo_t *pSigInfo, void *pUContext)
1578 {
1579
1580         pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
1581
1582         /* we moved __alarm_expired() function to __alarm_handler_idle GSource
1583            because of signal safety. */
1584         g_idle_add_full(G_PRIORITY_HIGH_IDLE, __alarm_handler_idle, NULL, NULL);
1585 }
1586
1587 static void __clean_registry()
1588 {
1589
1590         /*TODO:remove all db entries */
1591 }
1592
1593 static bool __alarm_manager_reset()
1594 {
1595
1596         _alarm_disable_timer(alarm_context);
1597
1598         __alarm_clean_list();
1599
1600         _clear_scheduled_alarm_list();
1601         __clean_registry();
1602
1603         return true;
1604 }
1605
1606 static void __on_system_time_changed(keynode_t *node, void *data)
1607 {
1608         double diff_time;
1609         time_t _time;
1610         time_t before;
1611
1612         _alarm_disable_timer(alarm_context);
1613
1614         if (node) {
1615                 _time = vconf_keynode_get_int(node);
1616         } else {
1617                 vconf_get_int(VCONFKEY_SYSTEM_TIMECHANGE, (int *)&_time);
1618         }
1619
1620         time(&before);
1621         diff_time = difftime(_time, before);
1622
1623         tzset();
1624
1625         ALARM_MGR_ASSERT_PRINT("diff_time is %f\n", diff_time);
1626
1627         ALARM_MGR_LOG_PRINT("[alarm-server] System time has been changed\n");
1628         ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n",
1629                             alarm_context.c_due_time);
1630
1631         _set_time(_time);
1632
1633         vconf_set_dbl(VCONFKEY_SYSTEM_TIMEDIFF, diff_time);
1634         vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED,(int)diff_time);
1635
1636         __alarm_update_due_time_of_all_items_in_list(diff_time);
1637
1638         ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n",
1639                             alarm_context.c_due_time);
1640         _clear_scheduled_alarm_list();
1641         _alarm_schedule();
1642         __rtc_set();
1643 #ifdef __ALARM_BOOT
1644         /*alarm boot */
1645         if (enable_power_on_alarm) {
1646 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
1647 APP_ID) in a platform with app-server. because _alarm_power_
1648 on(..) fuction don't use first parameter internally, we set
1649 this value to 0(zero)
1650 */
1651                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1652         }
1653 #endif
1654         return;
1655 }
1656
1657 gboolean alarm_manager_alarm_set_rtc_time(void *pObject, int pid,
1658                                 int year, int mon, int day,
1659                                 int hour, int min, int sec, char *e_cookie,
1660                                 int *return_code){
1661         guchar *cookie = NULL;
1662         gsize size;
1663         int retval = 0;
1664         gboolean result = true;
1665         gid_t call_gid;
1666
1667         const char *rtc = power_rtc;
1668         int fd = 0;
1669         struct rtc_time rtc_tm = {0,};
1670         struct rtc_wkalrm rtc_wk;
1671         struct tm *alarm_tm = NULL;
1672         struct tm due_tm = {0,};
1673
1674
1675         if (return_code){
1676                 *return_code = ALARMMGR_RESULT_SUCCESS;
1677         }
1678
1679         cookie = g_base64_decode(e_cookie, &size);
1680         if (NULL == cookie)
1681         {
1682                 if (return_code)
1683                         *return_code = ERR_ALARM_NO_PERMISSION;
1684                 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
1685                 return true;
1686         }
1687
1688         call_gid = security_server_get_gid("alarm");
1689
1690         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
1691
1692         retval = security_server_check_privilege((const char *)cookie, call_gid);
1693         if (retval < 0) {
1694                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1695                         ALARM_MGR_EXCEPTION_PRINT(
1696                                 "%s", "access has been denied\n");
1697                 }
1698                 ALARM_MGR_EXCEPTION_PRINT("Error has occurred in security_server_check_privilege()\n");
1699                 if (return_code)
1700                         *return_code = ERR_ALARM_NO_PERMISSION;
1701         }
1702         else {
1703
1704                 /*extract day of the week, day in the year &
1705                 daylight saving time from system*/
1706                 time_t ctime;
1707                 ctime = time(NULL);
1708                 alarm_tm = localtime(&ctime);
1709
1710                 alarm_tm->tm_year = year;
1711                 alarm_tm->tm_mon = mon;
1712                 alarm_tm->tm_mday = day;
1713                 alarm_tm->tm_hour = hour;
1714                 alarm_tm->tm_min = min;
1715                 alarm_tm->tm_sec = sec;
1716
1717                 /*convert to calendar time representation*/
1718                 time_t rtc_time = mktime(alarm_tm);
1719
1720                 /*convert to Coordinated Universal Time (UTC)*/
1721                 gmtime_r(&rtc_time, &due_tm);
1722
1723                 fd = open(rtc, O_RDONLY);
1724                 if (fd == -1) {
1725                         ALARM_MGR_EXCEPTION_PRINT("RTC open failed.\n");
1726                         if (return_code)
1727                                 *return_code = ERR_ALARM_SYSTEM_FAIL;
1728                         return result;
1729                 }
1730
1731                 /* Read the RTC time/date */
1732                 retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
1733                 if (retval == -1) {
1734                         ALARM_MGR_EXCEPTION_PRINT("RTC_RD_TIME ioctl failed");
1735                         close(fd);
1736                         if (return_code)
1737                                 *return_code = ERR_ALARM_SYSTEM_FAIL;
1738                         return result;
1739                 }
1740
1741                 rtc_tm.tm_mday = due_tm.tm_mday;
1742                 rtc_tm.tm_mon = due_tm.tm_mon;
1743                 rtc_tm.tm_year = due_tm.tm_year;
1744                 rtc_tm.tm_hour = due_tm.tm_hour;
1745                 rtc_tm.tm_min = due_tm.tm_min;
1746                 rtc_tm.tm_sec = due_tm.tm_sec;
1747
1748                 memcpy(&rtc_wk.time, &rtc_tm, sizeof(rtc_tm));
1749
1750                 rtc_wk.enabled = 1;
1751                 rtc_wk.pending = 0;
1752
1753                 retval = ioctl(fd, RTC_WKALM_BOOT_SET, &rtc_wk);
1754                 if (retval == -1) {
1755                         if (errno == ENOTTY) {
1756                                 ALARM_MGR_EXCEPTION_PRINT("\nAlarm IRQs not"
1757                                                           "supported.\n");
1758                         }
1759                         ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
1760                         close(fd);
1761                         if (return_code)
1762                                 *return_code = ERR_ALARM_SYSTEM_FAIL;
1763                 }
1764                 else{
1765                         ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is setted");
1766                         /* Enable alarm interrupts */
1767                         retval = ioctl(fd, RTC_AIE_ON, 0);
1768                         if (retval == -1) {
1769                                 ALARM_MGR_EXCEPTION_PRINT("RTC_AIE_ON ioctl failed");
1770                                 if (return_code)
1771                                         *return_code = ERR_ALARM_SYSTEM_FAIL;
1772                         }
1773                         close(fd);
1774                 }
1775         }
1776
1777         if (cookie){
1778                 g_free(cookie);
1779                 cookie = NULL;
1780         }
1781
1782         return result;
1783
1784 }
1785
1786 gboolean alarm_manager_alarm_create_appsvc(void *pObject, int pid,
1787                                     int start_year,
1788                                     int start_month, int start_day,
1789                                     int start_hour, int start_min,
1790                                     int start_sec, int end_year, int end_month,
1791                                     int end_day, int mode_day_of_week,
1792                                     int mode_repeat, int alarm_type,
1793                                     int reserved_info,
1794                                     char *bundle_data, char *e_cookie,
1795                                     int *alarm_id, int *return_code)
1796 {
1797         alarm_info_t alarm_info;
1798         guchar *cookie = NULL;
1799         gsize size;
1800         int retval = 0;
1801         gid_t call_gid;
1802         gboolean result = true;
1803
1804         alarm_info.start.year = start_year;
1805         alarm_info.start.month = start_month;
1806         alarm_info.start.day = start_day;
1807         alarm_info.start.hour = start_hour;
1808         alarm_info.start.min = start_min;
1809         alarm_info.start.sec = start_sec;
1810
1811         alarm_info.end.year = end_year;
1812         alarm_info.end.month = end_month;
1813         alarm_info.end.day = end_day;
1814
1815         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
1816         alarm_info.mode.repeat = mode_repeat;
1817
1818         alarm_info.alarm_type = alarm_type;
1819         alarm_info.reserved_info = reserved_info;
1820
1821         *return_code = 0;
1822
1823         cookie = g_base64_decode(e_cookie, &size);
1824         if (NULL == cookie)
1825         {
1826                 *return_code = -1;
1827                 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
1828                 return false;
1829         }
1830
1831         call_gid = security_server_get_gid("alarm");
1832
1833         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
1834
1835         retval = security_server_check_privilege((const char *)cookie, call_gid);
1836         if (retval < 0) {
1837                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1838                         ALARM_MGR_EXCEPTION_PRINT(
1839                                 "%s", "access has been denied\n");
1840                 }
1841                 ALARM_MGR_EXCEPTION_PRINT("Error has occurred in security_server_check_privilege()\n");
1842                 *return_code = -1;
1843                 result = false;
1844         }
1845         else {
1846                 result = __alarm_create_appsvc(&alarm_info, alarm_id, pid,
1847                                bundle_data, return_code);
1848                 if (false == result)
1849                 {
1850                         ALARM_MGR_EXCEPTION_PRINT("Unable to create alarm!\n");
1851                 }
1852         }
1853
1854         if (cookie){
1855                 g_free(cookie);
1856                 cookie = NULL;
1857         }
1858
1859         return result;
1860 }
1861
1862 gboolean alarm_manager_alarm_create(void *pObject, int pid,
1863                                     char *app_service_name, char *app_service_name_mod,  int start_year,
1864                                     int start_month, int start_day,
1865                                     int start_hour, int start_min,
1866                                     int start_sec, int end_year, int end_month,
1867                                     int end_day, int mode_day_of_week,
1868                                     int mode_repeat, int alarm_type,
1869                                     int reserved_info,
1870                                     char *reserved_service_name, char *reserved_service_name_mod, char *e_cookie,
1871                                     int *alarm_id, int *return_code)
1872 {
1873         alarm_info_t alarm_info;
1874         guchar *cookie;
1875         gsize size;
1876         int retval;
1877         gid_t call_gid;
1878
1879         alarm_info.start.year = start_year;
1880         alarm_info.start.month = start_month;
1881         alarm_info.start.day = start_day;
1882         alarm_info.start.hour = start_hour;
1883         alarm_info.start.min = start_min;
1884         alarm_info.start.sec = start_sec;
1885
1886         alarm_info.end.year = end_year;
1887         alarm_info.end.month = end_month;
1888         alarm_info.end.day = end_day;
1889
1890         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
1891         alarm_info.mode.repeat = mode_repeat;
1892
1893         alarm_info.alarm_type = alarm_type;
1894         alarm_info.reserved_info = reserved_info;
1895
1896         *return_code = 0;
1897
1898         cookie = g_base64_decode(e_cookie, &size);
1899         call_gid = security_server_get_gid("alarm");
1900
1901         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
1902
1903         retval = security_server_check_privilege((const char *)cookie, call_gid);
1904         if (retval < 0) {
1905                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1906                         ALARM_MGR_EXCEPTION_PRINT(
1907                                 "%s", "access has been denied\n");
1908                 }
1909                 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
1910
1911                 *return_code = -1;
1912         }
1913
1914         else {
1915                 /* return valule and return_code should be checked */
1916                 __alarm_create(&alarm_info, alarm_id, pid, app_service_name,app_service_name_mod,
1917                                reserved_service_name, reserved_service_name_mod, return_code);
1918         }
1919
1920         g_free(cookie);
1921
1922         return true;
1923 }
1924
1925 gboolean alarm_manager_alarm_delete(void *pObject, int pid, alarm_id_t alarm_id,
1926                                     char *e_cookie, int *return_code)
1927 {
1928         guchar *cookie;
1929         gsize size;
1930         int retval;
1931         gid_t call_gid;
1932
1933         cookie = g_base64_decode(e_cookie, &size);
1934         call_gid = security_server_get_gid("alarm");
1935
1936         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
1937
1938         retval = security_server_check_privilege((const char *)cookie, call_gid);
1939         if (retval < 0) {
1940                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1941                         ALARM_MGR_EXCEPTION_PRINT("%s",
1942                                                   "access has been denied\n");
1943                 }
1944                 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
1945
1946                 *return_code = -1;
1947         }
1948
1949         else {
1950                 __alarm_delete(pid, alarm_id, return_code);
1951         }
1952
1953         g_free(cookie);
1954
1955         return true;
1956 }
1957
1958 gboolean alarm_manager_alarm_power_on(void *pObject, int pid, bool on_off,
1959                                       int *return_code)
1960 {
1961
1962         return __alarm_power_on(pid, on_off, return_code);
1963 }
1964
1965 gboolean alarm_manager_alarm_power_off(void *pObject, int pid, int *return_code)
1966 {
1967
1968         return __alarm_power_off(pid, return_code);
1969 }
1970
1971 bool alarm_manager_alarm_check_next_duetime(void *pObject, int pid,
1972                                             int *return_code)
1973 {
1974         return __alarm_check_next_duetime(pid, return_code);
1975 }
1976
1977 gboolean alarm_manager_alarm_update(void *pObject, int pid,
1978                                     char *app_service_name, alarm_id_t alarm_id,
1979                                     int start_year, int start_month,
1980                                     int start_day, int start_hour,
1981                                     int start_min, int start_sec, int end_year,
1982                                     int end_month, int end_day,
1983                                     int mode_day_of_week, int mode_repeat,
1984                                     int alarm_type, int reserved_info,
1985                                     int *return_code)
1986 {
1987         alarm_info_t alarm_info;
1988         alarm_info.start.year = start_year;
1989         alarm_info.start.month = start_month;
1990         alarm_info.start.day = start_day;
1991         alarm_info.start.hour = start_hour;
1992         alarm_info.start.min = start_min;
1993         alarm_info.start.sec = start_sec;
1994
1995         alarm_info.end.year = end_year;
1996         alarm_info.end.month = end_month;
1997         alarm_info.end.day = end_day;
1998
1999         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
2000         alarm_info.mode.repeat = mode_repeat;
2001
2002         alarm_info.alarm_type = alarm_type;
2003         alarm_info.reserved_info = reserved_info;
2004
2005         *return_code = 0;
2006
2007         __alarm_update(pid, app_service_name, alarm_id, &alarm_info,
2008                        return_code);
2009
2010         return true;
2011 }
2012
2013 gboolean alarm_manager_alarm_get_number_of_ids(void *pObject, int pid,
2014                                                int *num_of_ids,
2015                                                int *return_code)
2016 {
2017         GSList *gs_iter = NULL;
2018         GQuark quark_app_unique_name;   /* the fullpath of pid(pid) is
2019                                            converted  to quark value. */
2020         char proc_file[256] = { 0 };
2021         char process_name[512] = { 0 };
2022         char app_name[256] = { 0 };
2023         char *word = NULL;
2024         __alarm_info_t *entry = NULL;
2025         char *proc_name_ptr = NULL;
2026
2027         *num_of_ids = 0;
2028         *return_code = 0;
2029
2030         /* we should consider to check whether  pid is running or Not
2031          */
2032         memset(process_name, '\0', 512);
2033         memset(proc_file, '\0', 256);
2034         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
2035
2036         int fd;
2037         int ret;
2038         int i = 0;
2039         fd = open(proc_file, O_RDONLY);
2040         if (fd > 0) {
2041                 ret = read(fd, process_name, 512);
2042                 close(fd);
2043                 while (process_name[i] != '\0') {
2044                         if (process_name[i] == ' ') {
2045                                 process_name[i] = '\0';
2046                                 break;
2047                         }
2048                         i++;
2049                 }
2050                 /*if (readlink(proc_file, process_name, 256)!=-1) */
2051                 /*success */
2052
2053                 word = strtok_r(process_name, "/", &proc_name_ptr);
2054                 while (word != NULL) {
2055                         memset(app_name, 0, 256);
2056                         snprintf(app_name, 256, "%s", word);
2057                         word = strtok_r(NULL, "/", &proc_name_ptr);
2058                 }
2059                 quark_app_unique_name = g_quark_from_string(app_name);
2060         } else {                /* failure */
2061
2062                 quark_app_unique_name = g_quark_from_string("unknown");
2063                 memcpy(app_name, "unknown", strlen("unknown") + 1);
2064
2065                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
2066                                           "killed, so we failed to get proc file(%s) \n",
2067                                           pid, proc_file);
2068                 *return_code = -1;      /* -1 means that system
2069                                            failed internally. */
2070                 return true;
2071         }
2072
2073         ALARM_MGR_LOG_PRINT("called for  app(pid:%d, name=%s)\n",
2074                             pid, app_name);
2075
2076         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2077              gs_iter = g_slist_next(gs_iter)) {
2078                 entry = gs_iter->data;
2079                 ALARM_MGR_LOG_PRINT("alarm_manager_alarm_get_number_of_ids(): "
2080                                     "app_name=%s,quark_app_unique_name=%s\n",
2081                 app_name, g_quark_to_string(entry->quark_app_unique_name));
2082                 if (strcmp
2083                     (app_name,
2084                      g_quark_to_string(entry->quark_app_unique_name)) == 0
2085                     && strcmp(app_name, "unknown") != 0) {
2086                         (*num_of_ids)++;
2087                         ALARM_MGR_LOG_PRINT("inc number of alarms of app "
2088                                             "(pid:%d, name:%s) is %d\n",
2089                                             pid, app_name, *num_of_ids);
2090                 }
2091         }
2092         *return_code = 0;
2093         ALARM_MGR_LOG_PRINT("number of alarms of app(pid:%d, name:%s) is %d\n",
2094                             pid, app_name, *num_of_ids);
2095         return true;
2096 }
2097
2098 gboolean alarm_manager_alarm_get_list_of_ids(void *pObject, int pid,
2099                                              int max_number_of_ids,
2100                                              GArray **arr, int *num_of_ids,
2101                                              int *return_code)
2102 {
2103         GSList *gs_iter = NULL;
2104         GQuark quark_app_unique_name;   /* the fullpath of pid(pid) is converted
2105                                            to quark value. */
2106         char proc_file[256] = { 0 };
2107         char process_name[512] = { 0 };
2108         char app_name[256] = { 0 };
2109         char *word = NULL;
2110         __alarm_info_t *entry = NULL;
2111         int index = 0;
2112         char *proc_name_ptr = NULL;
2113         int fd;
2114         int ret;
2115         int i = 0;
2116         GArray *garray = NULL;
2117
2118         *return_code = 0;
2119
2120         garray = g_array_new(false, true, sizeof(alarm_id_t));
2121
2122         /* we should check that there is a resource leak.
2123          * Now we don't have free code for g_array_new().
2124          */
2125         if (max_number_of_ids <= 0) {
2126                 *arr = garray;
2127                 ALARM_MGR_EXCEPTION_PRINT("called for  pid(%d), but "
2128                                           "max_number_of_ids(%d) is less than 0.\n",
2129                                           pid, max_number_of_ids);
2130                 return true;
2131         }
2132
2133         /* we should consider to check whether  pid is running or Not
2134          */
2135         memset(process_name, '\0', 512);
2136         memset(proc_file, '\0', 256);
2137         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
2138
2139         fd = open(proc_file, O_RDONLY);
2140         if (fd > 0) {
2141                 ret = read(fd, process_name, 512);
2142                 close(fd);
2143                 while (process_name[i] != '\0') {
2144                         if (process_name[i] == ' ') {
2145                                 process_name[i] = '\0';
2146                                 break;
2147                         }
2148                         i++;
2149                 }
2150                 /* if (readlink(proc_file, process_name, 256)!=-1) */
2151                 /*success */
2152
2153                 word = strtok_r(process_name, "/", &proc_name_ptr);
2154                 while (word != NULL) {
2155                         memset(app_name, 0, 256);
2156                         snprintf(app_name, 256, "%s", word);
2157                         word = strtok_r(NULL, "/", &proc_name_ptr);
2158                 }
2159                 quark_app_unique_name = g_quark_from_string(app_name);
2160         } else {                /* failure */
2161
2162                 quark_app_unique_name = g_quark_from_string("unknown");
2163                 memcpy(app_name, "unknown", strlen("unknown") + 1);
2164
2165                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
2166                 "killed, so we failed to get proc file(%s)\n", pid, proc_file);
2167                 *return_code = -1;
2168                 /* -1 means that system failed internally. */
2169                 return true;
2170         }
2171
2172         ALARM_MGR_LOG_PRINT("called for  app(pid:%d, name=%s)\n",
2173                             pid, app_name);
2174
2175         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2176              gs_iter = g_slist_next(gs_iter)) {
2177                 entry = gs_iter->data;
2178                 if (strcmp
2179                     (app_name,
2180                      g_quark_to_string(entry->quark_app_unique_name)) == 0
2181                     && strcmp(app_name, "unknown") != 0) {
2182                         g_array_append_val(garray, entry->alarm_id);
2183                 }
2184         }
2185
2186         *num_of_ids = index;
2187
2188         *arr = garray;
2189
2190         return true;
2191 }
2192
2193 gboolean alarm_manager_alarm_get_appsvc_info(void *pObject, int pid, alarm_id_t alarm_id,
2194                                 char *e_cookie, gchar **b_data, int *return_code)
2195 {
2196         bool found = false;
2197
2198         GSList *gs_iter = NULL;
2199         __alarm_info_t *entry = NULL;
2200
2201         guchar *cookie = NULL;
2202         gsize size;
2203         int retval = 0;
2204         gid_t call_gid;
2205
2206         ALARM_MGR_LOG_PRINT("called for  pid(%d) and alarm_id(%d)\n", pid,
2207                             alarm_id);
2208
2209         cookie = g_base64_decode(e_cookie, &size);
2210         if (NULL == cookie)
2211         {
2212                 if (return_code)
2213                         *return_code = ERR_ALARM_SYSTEM_FAIL;
2214                 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
2215                 return true;
2216         }
2217         call_gid = security_server_get_gid("alarm");
2218
2219         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
2220
2221         retval = security_server_check_privilege((const char *)cookie, call_gid);
2222         if (retval < 0) {
2223                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
2224                         ALARM_MGR_EXCEPTION_PRINT(
2225                                 "%s", "access has been denied\n");
2226                 }
2227                 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
2228
2229                 if (return_code)
2230                         *return_code = ERR_ALARM_NO_PERMISSION;
2231
2232                 if (cookie)
2233                         g_free(cookie);
2234
2235                 return true;
2236         }
2237
2238         if (return_code)
2239                 *return_code = 0;
2240
2241         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2242              gs_iter = g_slist_next(gs_iter)) {
2243                 entry = gs_iter->data;
2244                 if (entry->alarm_id == alarm_id) {
2245                         found = true;
2246                         *b_data = g_strdup(g_quark_to_string(entry->quark_bundle));
2247                         break;
2248                 }
2249         }
2250
2251         if (found) {
2252                 if ( *b_data && strlen(*b_data) == 4 && strncmp(*b_data,"null",4) == 0){
2253                         ALARM_MGR_EXCEPTION_PRINT("Regular alarm,not svc alarm");
2254                         if (return_code)
2255                                 *return_code = ERR_ALARM_INVALID_TYPE;
2256                 }
2257         } else {
2258                 if (return_code)
2259                         *return_code = ERR_ALARM_INVALID_ID;
2260         }
2261
2262         if (cookie)
2263                 g_free(cookie);
2264
2265         return true;
2266 }
2267
2268 gboolean alarm_manager_alarm_get_info(void *pObject, int pid,
2269                                       alarm_id_t alarm_id, int *start_year,
2270                                       int *start_month, int *start_day,
2271                                       int *start_hour, int *start_min,
2272                                       int *start_sec, int *end_year,
2273                                       int *end_month, int *end_day,
2274                                       int *mode_day_of_week, int *mode_repeat,
2275                                       int *alarm_type, int *reserved_info,
2276                                       int *return_code)
2277 {
2278         ALARM_MGR_LOG_PRINT("called for  pid(%d) and alarm_id(%d)\n", pid,
2279                             alarm_id);
2280
2281         GSList *gs_iter = NULL;
2282         __alarm_info_t *entry = NULL;
2283
2284         alarm_info_t *alarm_info = NULL;
2285         *return_code = 0;
2286
2287         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2288              gs_iter = g_slist_next(gs_iter)) {
2289                 entry = gs_iter->data;
2290                 if (entry->alarm_id == alarm_id) {
2291                         alarm_info = &(entry->alarm_info);
2292                         break;
2293                 }
2294         }
2295
2296         if (alarm_info == NULL)
2297         {
2298                 ALARM_MGR_EXCEPTION_PRINT("alarm id(%d) was not found\n",
2299                                           alarm_id);
2300                 *return_code = ERR_ALARM_INVALID_ID;
2301         } else {
2302                 ALARM_MGR_LOG_PRINT("alarm was found\n");
2303                 *start_year = alarm_info->start.year;
2304                 *start_month = alarm_info->start.month;
2305                 *start_day = alarm_info->start.day;
2306                 *start_hour = alarm_info->start.hour;
2307                 *start_min = alarm_info->start.min;
2308                 *start_sec = alarm_info->start.sec;
2309
2310                 *end_year = alarm_info->end.year;
2311                 *end_year = alarm_info->end.month;
2312                 *end_year = alarm_info->end.day;
2313
2314                 *mode_day_of_week = alarm_info->mode.u_interval.day_of_week;
2315                 *mode_repeat = alarm_info->mode.repeat;
2316
2317                 *alarm_type = alarm_info->alarm_type;
2318                 *reserved_info = alarm_info->reserved_info;
2319
2320                 *return_code = 0;
2321         }
2322         return true;
2323 }
2324
2325 #include "alarm-skeleton.h"
2326
2327 typedef struct AlarmManagerObject AlarmManagerObject;
2328 typedef struct AlarmManagerObjectClass AlarmManagerObjectClass;
2329 GType Server_Object_get_type(void);
2330 struct AlarmManagerObject {
2331         GObject parent;
2332 };
2333 struct AlarmManagerObjectClass {
2334         GObjectClass parent;
2335 };
2336
2337 #define DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT 0
2338
2339 #define ALARM_MANAGER_TYPE_OBJECT              (Server_Object_get_type())
2340 #define ALARM_MANAGER_OBJECT(object)           (G_TYPE_CHECK_INSTANCE_CAST \
2341 ((object), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObject))
2342 #define ALARM_MANAGER_OBJECT_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST \
2343 ((klass), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObjectClass))
2344 #define ALARM_MANAGER_IS_OBJECT(object)        (G_TYPE_CHECK_INSTANCE_TYPE \
2345 ((object), ALARM_MANAGER_TYPE_OBJECT))
2346 #define ALARM_MANAGER_IS_OBJECT_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE \
2347 ((klass), ALARM_MANAGER_TYPE_OBJECT))
2348 #define ALARM_MANAGER_OBJECT_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS \
2349 ((obj), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObjectClass))
2350 G_DEFINE_TYPE(AlarmManagerObject, Server_Object, G_TYPE_OBJECT)
2351 static void Server_Object_init(AlarmManagerObject * obj)
2352 {
2353         ;
2354 }
2355
2356 static void Server_Object_class_init(AlarmManagerObjectClass *klass)
2357 {
2358         ;
2359 }
2360
2361 static void __initialize_timer()
2362 {
2363         struct sigaction sig_timer;
2364         sigemptyset(&sig_timer.sa_mask);
2365         sig_timer.sa_flags = SA_SIGINFO;
2366         sig_timer.sa_sigaction = (void *)__alarm_handler;
2367         sigaction(SIG_TIMER, &sig_timer, NULL);
2368
2369         alarm_context.timer = _alarm_create_timer();
2370
2371 }
2372
2373 static void __initialize_alarm_list()
2374 {
2375
2376         alarm_context.alarms = NULL;
2377         alarm_context.c_due_time = -1;
2378
2379         _load_alarms_from_registry();
2380
2381         __rtc_set();    /*Set RTC1 Alarm with alarm due time for alarm-manager initialization*/
2382
2383         /*alarm boot */
2384 #ifdef __ALARM_BOOT
2385         /*alarm boot */
2386         if (enable_power_on_alarm) {
2387                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
2388                  * platform with app-server. because __alarm_power_on(..) fuction don't
2389                  * use first parameter internally, we set this value to 0(zero)
2390                  */
2391                 __alarm_power_on(0, enable_power_on_alarm, NULL);
2392         }
2393 #endif
2394 }
2395
2396 static void __initialize_scheduled_alarm_lsit()
2397 {
2398         _init_scheduled_alarm_list();
2399 }
2400
2401
2402 static void __hibernation_leave_callback()
2403 {
2404
2405         __initialize_scheduled_alarm_lsit();
2406
2407         __alarm_clean_list();
2408
2409         __initialize_alarm_list();
2410 }
2411
2412 static bool __initialize_noti()
2413 {
2414
2415         int fd = heynoti_init();
2416         if (fd < 0) {
2417                 ALARM_MGR_EXCEPTION_PRINT("fail to heynoti_init\n");
2418                 return false;
2419         }
2420         heynoti_subscribe(fd, "HIBERNATION_LEAVE", __hibernation_leave_callback,
2421                           NULL);
2422         heynoti_attach_handler(fd);
2423
2424         if (vconf_notify_key_changed
2425             (VCONFKEY_SYSTEM_TIMECHANGE, __on_system_time_changed, NULL) < 0) {
2426                 ALARM_MGR_LOG_PRINT(
2427                         "Failed to add callback for time changing event\n");
2428         }
2429         /*system state change noti ÃƒÂ³Â¸Â® */
2430
2431         return true;
2432 }
2433
2434
2435 static DBusHandlerResult __alarm_server_filter(DBusConnection *connection,
2436                                                DBusMessage *message,
2437                                                void *user_data)
2438 {
2439
2440         if (dbus_message_is_signal
2441             (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
2442                 char *service;
2443                 char *old_owner;
2444                 char *new_owner;
2445                 GSList *entry;
2446                 __expired_alarm_t *expire_info;
2447
2448                 dbus_message_get_args(message,
2449                                       NULL,
2450                                       DBUS_TYPE_STRING, &service,
2451                                       DBUS_TYPE_STRING, &old_owner,
2452                                       DBUS_TYPE_STRING, &new_owner,
2453                                       DBUS_TYPE_INVALID);
2454
2455                 for (entry = g_expired_alarm_list; entry; entry = entry->next) {
2456                         if (entry->data) {
2457                                 expire_info = (__expired_alarm_t *) entry->data;
2458
2459                                 if (strcmp(expire_info->service_name, service)
2460                                     == 0) {
2461                                         ALARM_MGR_EXCEPTION_PRINT(
2462                                         "__alarm_server_filter : "
2463                                              "service name(%s) alarm_id (%d)\n",
2464                                              expire_info->service_name,\
2465                                              expire_info->alarm_id);
2466
2467                                         __alarm_send_noti_to_application(
2468                                              expire_info->service_name,
2469                                              expire_info->alarm_id);
2470                                         g_expired_alarm_list =
2471                                             g_slist_remove(g_expired_alarm_list,
2472                                                            entry->data);
2473                                         free(expire_info);
2474                                 }
2475                         }
2476                 }
2477
2478                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2479         }
2480
2481         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2482 }
2483
2484 static bool __initialize_dbus()
2485 {
2486         GError *error = NULL;
2487         GObject *obj = NULL;
2488         DBusGConnection *connection = NULL;
2489         DBusError derror;
2490         int request_name_result = 0;
2491
2492         dbus_g_object_type_install_info(ALARM_MANAGER_TYPE_OBJECT,
2493                                         &dbus_glib_alarm_manager_object_info);
2494
2495         connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
2496         if (!connection) {
2497
2498                 ALARM_MGR_EXCEPTION_PRINT("dbus_g_bus_get failed\n");
2499                 return false;
2500         }
2501
2502         dbus_error_init(&derror);
2503
2504         request_name_result =
2505             dbus_bus_request_name(dbus_g_connection_get_connection(connection),
2506                                   "org.tizen.alarm.manager",
2507                                   DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &derror);
2508         if (dbus_error_is_set(&derror)) {       /* failure */
2509                 ALARM_MGR_EXCEPTION_PRINT("Failed to dbus_bus_request_name "
2510                 "(org.tizen.alarm.manager): %s\n", derror.message);
2511                 dbus_error_free(&derror);
2512                 return false;
2513         }
2514
2515         if (!(obj = g_object_new(ALARM_MANAGER_TYPE_OBJECT, NULL))) {
2516                 ALARM_MGR_EXCEPTION_PRINT("Could not allocate new object\n");
2517                 return false;
2518         }
2519
2520         dbus_g_connection_register_g_object(connection,
2521                                             "/org/tizen/alarm/manager",
2522                                             G_OBJECT(obj));
2523
2524         /*dbus_bus_add_match (dbus_g_connection_get_connection(connection),
2525            "type='signal',member='NameOwnerChanged'",NULL); */
2526
2527         dbus_bus_add_match(dbus_g_connection_get_connection(connection),
2528                            "type='signal',sender='" DBUS_SERVICE_DBUS
2529                            "',path='" DBUS_PATH_DBUS
2530                            "',interface='" DBUS_INTERFACE_DBUS
2531                            "',member='NameOwnerChanged'", NULL);
2532
2533         if (!dbus_connection_add_filter
2534             (dbus_g_connection_get_connection(connection),
2535              __alarm_server_filter, NULL, NULL)) {
2536                 ALARM_MGR_EXCEPTION_PRINT("add __expire_alarm_filter failed\n");
2537
2538                 return false;
2539         }
2540
2541         alarm_context.bus = connection;
2542         return true;
2543 }
2544
2545 #define ALARMMGR_DB_FILE "/opt/dbspace/.alarmmgr.db"
2546 sqlite3 *alarmmgr_db;
2547 #define QUERY_CREATE_TABLE_ALARMMGR "create table alarmmgr \
2548                                 (alarm_id integer primary key,\
2549                                                 start integer,\
2550                                                 end integer,\
2551                                                 pid integer,\
2552                                                 app_unique_name text,\
2553                                                 app_service_name text,\
2554                                                 app_service_name_mod text,\
2555                                                 bundle text, \
2556                                                 year integer,\
2557                                                 month integer,\
2558                                                 day integer,\
2559                                                 hour integer,\
2560                                                 min integer,\
2561                                                 sec integer,\
2562                                                 day_of_week integer,\
2563                                                 repeat integer,\
2564                                                 alarm_type integer,\
2565                                                 reserved_info integer,\
2566                                                 dst_service_name text, \
2567                                                 dst_service_name_mod text \
2568                                                 )"
2569
2570 static bool __initialize_db()
2571 {
2572         char *error_message = NULL;
2573         int ret;
2574
2575         if (access("/opt/dbspace/.alarmmgr.db", F_OK) == 0) {
2576                 ret =
2577                     db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db,
2578                                  DB_UTIL_REGISTER_HOOK_METHOD);
2579
2580                 if (ret != SQLITE_OK) {
2581                         ALARM_MGR_EXCEPTION_PRINT(
2582                             "====>>>> connect menu_db [%s] failed!\n",
2583                              ALARMMGR_DB_FILE);
2584                         return false;
2585                 }
2586
2587                 return true;
2588         }
2589
2590         ret =
2591             db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db,
2592                          DB_UTIL_REGISTER_HOOK_METHOD);
2593
2594         if (ret != SQLITE_OK) {
2595                 ALARM_MGR_EXCEPTION_PRINT(
2596                     "====>>>> connect menu_db [%s] failed!\n",
2597                      ALARMMGR_DB_FILE);
2598                 return false;
2599         }
2600
2601         if (SQLITE_OK !=
2602             sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL,\
2603                          &error_message)) {
2604                 ALARM_MGR_EXCEPTION_PRINT("Don't execute query = %s, "
2605                 "error message = %s\n", QUERY_CREATE_TABLE_ALARMMGR,
2606                                           error_message);
2607                 return false;
2608         }
2609
2610         return true;
2611 }
2612
2613 static void __initialize()
2614 {
2615
2616         g_type_init();
2617 #ifdef __ALARM_BOOT
2618         FILE *fp;
2619         char temp[2];
2620         int size;
2621
2622         fp = fopen("/proc/alarm_boot", "r");
2623         if (fp == NULL)
2624                 alarm_boot = 0;
2625         else {
2626
2627                 size = fread(&temp, 1, 1, fp);
2628                 if (size != 1)
2629                         alarm_boot = 0;
2630                 else {
2631                         temp[1] = 0;
2632                         alarm_boot = atoi(temp);
2633                 }
2634                 fclose(fp);
2635         }
2636 #endif
2637
2638         int fd;
2639         int fd2;
2640         struct rtc_time rtc_tm;
2641         int retval;
2642
2643         fd = open(power_rtc, O_RDONLY);
2644         if (fd < 0) {
2645                 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc0\n");
2646                 return;
2647         }
2648         retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
2649         close(fd);
2650
2651         fd2 = open(default_rtc, O_RDWR);
2652         if (fd2 < 0) {
2653                 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc1\n");
2654                 return;
2655         }
2656         retval = ioctl(fd2, RTC_SET_TIME, &rtc_tm);
2657         close(fd2);
2658
2659         __initialize_timer();
2660         if (__initialize_dbus() == false) {     /* because dbus's initialize
2661                                         failed, we cannot continue any more. */
2662                 ALARM_MGR_EXCEPTION_PRINT("because __initialize_dbus failed, "
2663                                           "alarm-server cannot be runned.\n");
2664                 exit(1);
2665         }
2666         __initialize_scheduled_alarm_lsit();
2667         __initialize_db();
2668         __initialize_alarm_list();
2669         __initialize_noti();
2670
2671 }
2672
2673 #ifdef __ALARM_BOOT
2674 static bool __check_false_alarm()
2675 {
2676         time_t current_time;
2677         time_t interval;
2678
2679         time(&current_time);
2680
2681         interval = ab_due_time - current_time;
2682
2683         ALARM_MGR_LOG_PRINT("due_time : %d / current_time %d\n", \
2684                             alarm_context.c_due_time, current_time);
2685
2686         if (interval > 0 && interval <= 30) {
2687                 return true;
2688         } else if (!poweron_alarm_expired) {
2689                 /* originally, first arguement's value was 121(app_id) which means 
2690                  * alarm_booting ui application.and this application's dbus-service 
2691                  * name had a org.tizen.alarmboot.ui in a platform with app-server.
2692                  * so we set "org.tizen.alarmboot.ui.ALARM" instead of 121.
2693                  */
2694                 __alarm_send_noti_to_application(
2695                         WAKEUP_ALARMBOOTING_APP_ID, -1);
2696                 return false;
2697         }
2698
2699         return true;
2700 }
2701 #endif
2702
2703 int main()
2704 {
2705         GMainLoop *mainloop = NULL;
2706
2707         ALARM_MGR_LOG_PRINT("Enter main loop\n");
2708
2709         mainloop = g_main_loop_new(NULL, FALSE);
2710
2711         __initialize();
2712
2713 #ifdef __ALARM_BOOT
2714         if (alarm_boot){
2715                 __check_false_alarm();
2716         }
2717 #endif
2718
2719         g_main_loop_run(mainloop);
2720
2721         return 0;
2722 }