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