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