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