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