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