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