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