4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #define _BSD_SOURCE /*for localtime_r*/
36 #include<dbus/dbus-glib-lowlevel.h>
38 #if !GLIB_CHECK_VERSION (2, 31, 0)
39 #include <glib/gmacros.h>
43 /*#include "global-gconf.h"*/
45 #include"alarm-internal.h"
47 /*#include"limo-event-delivery.h"*/
52 #include <security-server.h>
55 #include <vconf-keys.h>
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 */
65 #include "tapi_misc_ext.h"
67 #include "tapi_misc_data.h"
68 #include "tapi_power.h"
73 __alarm_server_context_t alarm_context;
74 bool g_dummy_timer_is_set = FALSE;
76 GSList *g_scheduled_alarm_list = NULL;
78 GSList *g_expired_alarm_list = NULL;
80 #ifndef RTC_WKALM_BOOT_SET
81 #define RTC_WKALM_BOOT_SET _IOW('p', 0x80, struct rtc_wkalrm)
85 bool enable_power_on_alarm;
87 time_t ab_due_time = -1;
88 bool poweron_alarm_expired = false;
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.
95 #define __WAKEUP_USING_RTC__
96 #ifdef __WAKEUP_USING_RTC__
98 #include <linux/rtc.h>
99 #include <sys/ioctl.h>
102 static const char default_rtc[] = "/dev/rtc1";
103 static const char power_rtc[] = "/dev/rtc0";
105 #endif /*__WAKEUP_USING_RTC__*/
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,
112 static bool __alarm_remove_from_list(int pid, alarm_id_t alarm_id,
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);
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();
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,
152 static void __rtc_set()
154 #ifdef __WAKEUP_USING_RTC__
156 const char *rtc = default_rtc;
157 const char *rtc0 = power_rtc;
159 struct rtc_time rtc_tm;
160 struct rtc_wkalrm rtc_wk;
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");
170 fd = open(rtc, O_RDONLY);
172 fd = open(rtc0, O_RDONLY);
174 ALARM_MGR_EXCEPTION_PRINT("RTC open failed.\n");
179 /* Read the RTC time/date */
180 int retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
182 ALARM_MGR_EXCEPTION_PRINT("RTC_RD_TIME ioctl failed");
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,
192 ALARM_MGR_LOG_PRINT("alarm_context.c_due_time is %d\n", \
193 alarm_context.c_due_time);
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);
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));
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);
214 retval = ioctl(fd, RTC_WKALM_SET, &rtc_wk);
216 if (errno == ENOTTY) {
217 ALARM_MGR_EXCEPTION_PRINT("\nAlarm IRQs not"
220 ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
224 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is setted");
226 /* Enable alarm interrupts */
227 retval = ioctl(fd, RTC_AIE_ON, 0);
229 ALARM_MGR_EXCEPTION_PRINT("RTC_AIE_ON ioctl failed");
233 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is on");
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");
240 #endif /* __WAKEUP_USING_RTC__ */
243 int _set_rtc_time(time_t _time)
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, };
254 fd0 = open(rtc0, O_RDONLY);
255 fd1 = open(rtc1, O_RDONLY);
258 ALARM_MGR_LOG_PRINT("error to open /dev/rtc0.");
263 ALARM_MGR_LOG_PRINT("error to open /dev/rtc1.");
267 memset(&rtc_tm, 0, sizeof(struct rtc_time));
270 (void) gmtime_r(&_time, &time_r);
272 /* Write the RTC time/date 2008:05:21 19:20:00 */
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;
283 retval0 = ioctl(fd0, RTC_SET_TIME, &rtc_tm);
286 ALARM_MGR_LOG_PRINT("error to ioctl fd0.");
293 retval1 = ioctl(fd1, RTC_SET_TIME, &rtc_tm);
296 ALARM_MGR_LOG_PRINT("error to ioctl fd1.");
304 bool __alarm_clean_list()
308 for (iter = alarm_context.alarms; iter != NULL;
309 iter = g_slist_next(iter)) {
313 g_slist_free(alarm_context.alarms);
317 static void __alarm_generate_alarm_id(__alarm_info_t *__alarm_info, alarm_id_t *alarm_id)
319 bool unique_id = false;
320 __alarm_info_t *entry = NULL;
324 /* FIXME: alarm id must be unique. */
325 __alarm_info->alarm_id = (int)(void *)__alarm_info;
326 ALARM_MGR_LOG_PRINT("__alarm_info->alarm_id is %d", \
327 __alarm_info->alarm_id);
329 while (unique_id == false) {
332 for (iter = alarm_context.alarms; iter != NULL;
333 iter = g_slist_next(iter)) {
335 if (entry->alarm_id == __alarm_info->alarm_id) {
336 __alarm_info->alarm_id++;
342 *alarm_id = __alarm_info->alarm_id;
347 static bool __alarm_add_to_list(__alarm_info_t *__alarm_info)
350 alarm_info_t *alarm_info = &__alarm_info->alarm_info;
351 __alarm_info_t *entry = NULL;
356 ALARM_MGR_LOG_PRINT("[alarm-server]: before add\n");
357 for (iter = alarm_context.alarms; iter != NULL;
358 iter = g_slist_next(iter)) {
360 /*ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n",
364 alarm_context.alarms =
365 g_slist_append(alarm_context.alarms, __alarm_info);
367 ALARM_MGR_LOG_PRINT("[alarm-server]: after add\n");
368 for (iter = alarm_context.alarms; iter != NULL;
369 iter = g_slist_next(iter)) {
371 ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n",
375 if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
376 _save_alarms(__alarm_info);
382 static bool __alarm_update_in_list(int pid, alarm_id_t alarm_id,
383 __alarm_info_t *__alarm_info,
387 alarm_info_t *alarm_info = &__alarm_info->alarm_info;
389 __alarm_info_t *entry = NULL;
391 for (iter = alarm_context.alarms; iter != NULL;
392 iter = g_slist_next(iter)) {
394 if (entry->alarm_id == alarm_id) {
397 __alarm_info->quark_app_unique_name =
398 entry->quark_app_unique_name;
399 __alarm_info->quark_dst_service_name =
400 entry->quark_dst_service_name;
401 memcpy(entry, __alarm_info, sizeof(__alarm_info_t));
409 *error_code = ERR_ALARM_INVALID_ID;
413 if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
414 _update_alarms(__alarm_info);
420 static bool __alarm_remove_from_list(int pid, alarm_id_t alarm_id,
425 alarm_info_t *alarm_info = NULL;
428 __alarm_info_t *entry = NULL;
431 ALARM_MGR_LOG_PRINT("[alarm-server]: before del : alarm id(%d)\n",
434 for (iter = alarm_context.alarms; iter != NULL;
435 iter = g_slist_next(iter)) {
437 if (entry->alarm_id == alarm_id) {
438 alarm_info = &entry->alarm_info;
440 ALARM_MGR_LOG_PRINT("[alarm-server]: "
441 "__alarm_remove_from_list : alarm id(%d)\n",
444 if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
445 _delete_alarms(alarm_id);
448 alarm_context.alarms =
449 g_slist_remove(alarm_context.alarms, iter->data);
456 ALARM_MGR_LOG_PRINT("[alarm-server]: after del\n");
460 *error_code = ERR_ALARM_INVALID_ID;
467 static bool __alarm_set_start_and_end_time(alarm_info_t *alarm_info,
468 __alarm_info_t *__alarm_info)
470 alarm_date_t *start = &alarm_info->start;
471 alarm_date_t *end = &alarm_info->end;
473 struct tm alarm_tm = { 0, };
475 if (start->year != 0) {
476 alarm_tm.tm_year = start->year - 1900;
477 alarm_tm.tm_mon = start->month - 1;
478 alarm_tm.tm_mday = start->day;
480 alarm_tm.tm_hour = start->hour;
481 alarm_tm.tm_min = start->min;
482 alarm_tm.tm_sec = start->sec;
484 __alarm_info->start = mktime(&alarm_tm);
486 __alarm_info->start = 0;
489 if (end->year != 0) {
490 alarm_tm.tm_year = end->year - 1900;
491 alarm_tm.tm_mon = end->month - 1;
492 alarm_tm.tm_mday = end->day;
494 alarm_tm.tm_hour = end->hour;
495 alarm_tm.tm_min = end->min;
496 alarm_tm.tm_sec = end->sec;
498 __alarm_info->end = mktime(&alarm_tm);
500 __alarm_info->end = 0;
507 static bool alarm_get_tz_info(int *gmt_idx, int *dst)
509 GConfValue *value1 = NULL;
510 GConfValue *value2 = NULL;
511 GConfClient* gConfClient = NULL;
514 gConfClient = gconf_client_get_default();
517 value1 = gconf_client_get(gConfClient, SETTINGS_CLOCKTIMEZONE,
520 ALARM_MGR_LOG_PRINT("__on_system_time_changed:
521 gconf_client_get() failed:
522 error:[%s]\n", err->message);
526 *gmt_idx = gconf_value_get_int(value1);
527 ALARM_MGR_LOG_PRINT("gconf return gmt_idx =%d\n ", *gmt_idx);
529 value2 = gconf_client_get(gConfClient,
530 SETTINGS_DAYLIGHTSTATUS, &err);
532 ALARM_MGR_LOG_PRINT("__on_system_time_changed:
533 gconf_client_get() failed: error:[%s]\n", err->message);
538 *dst = gconf_value_get_int(value2);
539 ALARM_MGR_LOG_PRINT("gconf return dst =%d\n ", *dst);
541 if(gConfClient != NULL) {
542 g_object_unref(gConfClient);
547 ALARM_MGR_LOG_PRINT("check the gconf setting failed!!!!! \n ");
550 gconf_value_free(value1);
554 gconf_value_free(value2);
562 static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
565 time_t min_time = -1;
568 __alarm_info_t *entry = NULL;
569 struct tm *p_time = NULL ;
570 struct tm due_time_result ;
571 struct tm fixed_time = { 0, };
573 for (iter = alarm_context.alarms; iter != NULL;
574 iter = g_slist_next(iter)) {
576 alarm_info_t *alarm_info = &(entry->alarm_info);
577 if (alarm_info->alarm_type & ALARM_TYPE_RELATIVE) {
578 /*diff_time ó¸® */
580 entry->due_time += diff_time;
582 alarm_date_t *start = &alarm_info->start; /**< start
584 alarm_date_t *end = &alarm_info->end;;
585 /**< end time of the alarm */
588 p_time = localtime_r(&entry->due_time, &due_time_result);
589 if (p_time != NULL) {
590 start->year = p_time->tm_year + 1900;
591 start->month = p_time->tm_mon + 1;
592 start->day = p_time->tm_mday;
593 start->hour = p_time->tm_hour;
594 start->min = p_time->tm_min;
595 start->sec = p_time->tm_sec;
597 end->year = p_time->tm_year + 1900;
598 end->month = p_time->tm_mon + 1;
599 end->day = p_time->tm_mday;
602 memset(&fixed_time, 0, sizeof(fixed_time));
603 fixed_time.tm_year = p_time->tm_year;
604 fixed_time.tm_mon = p_time->tm_mon;
605 fixed_time.tm_mday = p_time->tm_mday;
606 fixed_time.tm_hour = 0;
607 fixed_time.tm_min = 0;
608 fixed_time.tm_sec = 0;
610 entry->start = mktime(&fixed_time);
612 fixed_time.tm_hour = 23;
613 fixed_time.tm_min = 59;
614 fixed_time.tm_sec = 59;
616 entry->end = mktime(&fixed_time);
618 ALARM_MGR_LOG_PRINT("alarm_info->alarm_type is "
619 "ALARM_TYPE_RELATIVE\n");
621 _update_alarms(entry);
624 _alarm_next_duetime(entry);
625 ALARM_MGR_LOG_PRINT("entry->due_time is %d\n", entry->due_time);
628 _alarm_time(¤t_time);
630 for (iter = alarm_context.alarms; iter != NULL;
631 iter = g_slist_next(iter)) {
633 due_time = entry->due_time;
637 ALARM_MGR_LOG_PRINT("alarm[%d] with duetime(%u) at "
638 "current(%u)\n", entry->alarm_id, due_time, current_time);
639 if (due_time == 0) { /* 0 means this alarm has been
644 interval = difftime(due_time, current_time);
647 ALARM_MGR_LOG_PRINT("this may be error.. alarm[%d]\n", \
652 interval = difftime(due_time, min_time);
654 if ((interval < 0) || min_time == -1) {
660 alarm_context.c_due_time = min_time;
665 static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
666 int pid,char *bundle_data, int *error_code){
671 char due_time_r[100] = { 0 };
672 char proc_file[512] = { 0 };
673 char process_name[512] = { 0 };
674 char app_name[512] = { 0 };
676 char *proc_name_ptr = NULL;
681 char caller_appid[512];
682 bundle_raw *b_data = NULL;
685 __alarm_info_t *__alarm_info = NULL;
687 __alarm_info = malloc(sizeof(__alarm_info_t));
688 if (__alarm_info == NULL) {
689 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, malloc "
690 "failed. it seems to be OOM\n", pid);
691 *error_code = -1; /* -1 means that system
692 failed internally. */
695 __alarm_info->pid = pid;
696 __alarm_info->alarm_id = -1;
699 /* we should consider to check whether pid is running or Not
701 memset(process_name, '\0', 512);
702 memset(proc_file, '\0', 512);
703 snprintf(proc_file, 512, "/proc/%d/cmdline", pid);
705 fd = open(proc_file, O_RDONLY);
706 if (fd < 0) { /* failure */
707 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
708 "killed, so we failed to get proc file(%s) and do not create "
709 "alarm_info\n", pid, proc_file);
710 *error_code = -1; /*-1 means that system failed
715 ret = read(fd, process_name, 512);
719 ALARM_MGR_EXCEPTION_PRINT("Unable to get application name\n");
724 while (process_name[i] != '\0') {
725 if (process_name[i] == ' ') {
726 process_name[i] = '\0';
732 word = strtok_r(process_name, "/", &proc_name_ptr);
733 while (word != NULL) {
734 memset(app_name, 0, 512);
735 snprintf(app_name, 512, "%s", word);
736 word = strtok_r(NULL, "/", &proc_name_ptr);
738 __alarm_info->quark_app_unique_name =
739 g_quark_from_string(app_name);
742 b = bundle_decode((bundle_raw *)bundle_data, strlen(bundle_data));
743 ret = aul_app_get_appid_bypid(pid, caller_appid, 512);
745 bundle_add(b, "__ALARM_MGR_CALLER_APPID", caller_appid);
747 bundle_encode(b, &b_data, &datalen);
749 __alarm_info->quark_bundle=g_quark_from_string(b_data);
750 __alarm_info->quark_app_service_name = g_quark_from_string("null");
751 __alarm_info->quark_dst_service_name = g_quark_from_string("null");
752 __alarm_info->quark_app_service_name_mod = g_quark_from_string("null");
753 __alarm_info->quark_dst_service_name_mod = g_quark_from_string("null");
761 __alarm_set_start_and_end_time(alarm_info, __alarm_info);
762 memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
763 __alarm_generate_alarm_id(__alarm_info, alarm_id);
765 _alarm_time(¤t_time);
767 if (alarm_context.c_due_time < current_time) {
768 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
769 "(%d) is less than current time(%d)", alarm_context.c_due_time,
771 alarm_context.c_due_time = -1;
774 due_time = _alarm_next_duetime(__alarm_info);
775 if (__alarm_add_to_list(__alarm_info) == false) {
782 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
783 "due_time is 0, alarm(%d) \n", *alarm_id);
785 } else if (current_time == due_time) {
786 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
787 "current_time(%d) is same as due_time(%d)", current_time,
790 }else if (difftime(due_time, current_time) < 0){
791 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: Expired Due Time.[Due time=%d, Current Time=%d]!!!Do not add to schedule list\n", due_time, current_time);
794 localtime_r(&due_time, &ts_ret);
795 strftime(due_time_r, 30, "%c", &ts_ret);
796 ALARM_MGR_LOG_PRINT("[alarm-server]:Create a new alarm: "
797 "alarm(%d) due_time(%s)", *alarm_id,
801 ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
802 "due_time(%d)", alarm_context.c_due_time, due_time);
804 if (alarm_context.c_due_time == -1
805 || due_time < alarm_context.c_due_time) {
806 _clear_scheduled_alarm_list();
807 _add_to_scheduled_alarm_list(__alarm_info);
808 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
810 alarm_context.c_due_time = due_time;
812 } else if (due_time == alarm_context.c_due_time) {
813 _add_to_scheduled_alarm_list(__alarm_info);
821 if (enable_power_on_alarm) {
822 __alarm_power_on(0, enable_power_on_alarm, NULL);
830 static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
831 int pid, char *app_service_name, char *app_service_name_mod,
832 const char *dst_service_name,const char *dst_service_name_mod, int *error_code)
837 char proc_file[256] = { 0 };
838 char process_name[512] = { 0 };
839 char app_name[256] = { 0 };
841 char *proc_name_ptr = NULL;
843 __alarm_info_t *__alarm_info = NULL;
845 __alarm_info = malloc(sizeof(__alarm_info_t));
846 if (__alarm_info == NULL) {
847 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, malloc "
848 "failed. it seems to be OOM\n", pid);
849 *error_code = -1; /* -1 means that system
850 failed internally. */
853 __alarm_info->pid = pid;
854 __alarm_info->alarm_id = -1;
856 /* we should consider to check whether pid is running or Not
858 memset(process_name, '\0', 512);
859 memset(proc_file, '\0', 256);
860 snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
865 fd = open(proc_file, O_RDONLY);
866 if (fd < 0) { /* failure */
867 __alarm_info->quark_app_unique_name =
868 g_quark_from_string("unknown");
869 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
870 "killed, so we failed to get proc file(%s) and do not create "
871 "alarm_info\n", pid, proc_file);
872 *error_code = -1; /*-1 means that system failed
877 ret = read(fd, process_name, 512);
879 *error_code = -1; /*-1 means that system failed
886 while (process_name[i] != '\0') {
887 if (process_name[i] == ' ') {
888 process_name[i] = '\0';
893 /* if (readlink(proc_file, process_name, 256)!=-1) */
896 word = strtok_r(process_name, "/", &proc_name_ptr);
897 while (word != NULL) {
898 memset(app_name, 0, 256);
899 snprintf(app_name, 256, "%s", word);
900 word = strtok_r(NULL, "/", &proc_name_ptr);
902 __alarm_info->quark_app_unique_name =
903 g_quark_from_string(app_name);
906 __alarm_info->quark_app_service_name =
907 g_quark_from_string(app_service_name);
908 __alarm_info->quark_app_service_name_mod =
909 g_quark_from_string(app_service_name_mod);
910 __alarm_info->quark_dst_service_name =
911 g_quark_from_string(dst_service_name);
912 __alarm_info->quark_dst_service_name_mod =
913 g_quark_from_string(dst_service_name_mod);
914 __alarm_info->quark_bundle = g_quark_from_string("null");
917 __alarm_set_start_and_end_time(alarm_info, __alarm_info);
918 memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
919 __alarm_generate_alarm_id(__alarm_info, alarm_id);
921 _alarm_time(¤t_time);
923 ALARM_MGR_LOG_PRINT("[alarm-server]:pid=%d, app_unique_name=%s, "
924 "app_service_name=%s,dst_service_name=%s, c_due_time=%d", \
925 pid, g_quark_to_string(__alarm_info->quark_app_unique_name), \
926 g_quark_to_string(__alarm_info->quark_app_service_name), \
927 g_quark_to_string(__alarm_info->quark_dst_service_name), \
928 alarm_context.c_due_time);
930 if (alarm_context.c_due_time < current_time) {
931 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
932 "(%d) is less than current time(%d)", alarm_context.c_due_time,
934 alarm_context.c_due_time = -1;
937 due_time = _alarm_next_duetime(__alarm_info);
938 if (__alarm_add_to_list(__alarm_info) == false) {
944 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
945 "due_time is 0, alarm(%d) \n", *alarm_id);
947 } else if (current_time == due_time) {
948 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
949 "current_time(%d) is same as due_time(%d)", current_time,
952 }else if (difftime(due_time, current_time) < 0){
953 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: Expired Due Time.[Due time=%d, Current Time=%d]!!!Do not add to schedule list\n", due_time, current_time);
956 char due_time_r[100] = { 0 };
958 localtime_r(&due_time, &ts_ret);
959 strftime(due_time_r, 30, "%c", &ts_ret);
960 ALARM_MGR_LOG_PRINT("[alarm-server]:Create a new alarm: "
961 "alarm(%d) due_time(%s)", *alarm_id,
965 ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
966 "due_time(%d)", alarm_context.c_due_time, due_time);
968 if (alarm_context.c_due_time == -1
969 || due_time < alarm_context.c_due_time) {
970 _clear_scheduled_alarm_list();
971 _add_to_scheduled_alarm_list(__alarm_info);
972 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
974 alarm_context.c_due_time = due_time;
976 } else if (due_time == alarm_context.c_due_time) {
977 _add_to_scheduled_alarm_list(__alarm_info);
985 if (enable_power_on_alarm) {
986 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
987 * platform with app-server.because __alarm_power_on(..) fuction don't
988 * use first parameter internally, we set this value to 0(zero)
990 __alarm_power_on(0, enable_power_on_alarm, NULL);
996 static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
997 alarm_info_t *alarm_info, int *error_code)
1002 __alarm_info_t *__alarm_info = NULL;
1003 bool result = false;
1005 __alarm_info = malloc(sizeof(__alarm_info_t));
1006 if (__alarm_info == NULL) {
1007 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, "
1008 "malloc failed. it seems to be OOM\n", pid);
1009 *error_code = -1; /*-1 means that system failed
1014 __alarm_info->pid = pid;
1015 __alarm_info->alarm_id = alarm_id;
1017 /* we should consider to check whether pid is running or Not
1020 __alarm_info->quark_app_service_name =
1021 g_quark_from_string(app_service_name);
1022 __alarm_set_start_and_end_time(alarm_info, __alarm_info);
1023 memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
1025 _alarm_time(¤t_time);
1027 if (alarm_context.c_due_time < current_time) {
1028 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
1029 "(%d) is less than current time(%d)", alarm_context.c_due_time,
1031 alarm_context.c_due_time = -1;
1034 due_time = _alarm_next_duetime(__alarm_info);
1035 if (!__alarm_update_in_list(pid, alarm_id, __alarm_info, error_code)) {
1037 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: requested alarm_id "
1038 "(%d) does not exist. so this value is invalid id.", alarm_id);
1041 /* ALARM_MGR_LOG_PRINT("[alarm-server]:request_pid=%d, alarm_id=%d,
1042 * app_unique_name=%s, app_service_name=%s, dst_service_name=%s,
1043 * c_due_time=%d", pid, alarm_id, g_quark_to_string
1044 * (__alarm_info->quark_app_unique_name), g_quark_to_string
1045 * (__alarm_info->quark_app_service_name), g_quark_to_string
1046 * (__alarm_info->quark_dst_service_name), alarm_context.c_due_time);
1049 result = _remove_from_scheduled_alarm_list(pid, alarm_id);
1051 if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
1052 /*there is no scheduled alarm */
1053 _alarm_disable_timer(alarm_context);
1056 ALARM_MGR_LOG_PRINT("[alarm-server]:Update alarm: alarm(%d)\n",
1063 if (enable_power_on_alarm) {
1064 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in
1065 * a platform with app-server.because __alarm_power_on(..) fuction don't
1066 * use first parameter internally, we set this value to 0(zero)
1068 __alarm_power_on(0, enable_power_on_alarm, NULL);
1072 if (due_time == 0) {
1073 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1080 if (due_time == 0) {
1081 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1082 "due_time is 0, alarm(%d)\n", alarm_id);
1085 } else if (current_time == due_time) {
1086 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1087 "current_time(%d) is same as due_time(%d)", current_time,
1091 }else if (difftime(due_time, current_time)< 0){
1092 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: Expired Due Time.[Due time=%d, Current Time=%d]!!!Do not add to schedule list\n", due_time, current_time);
1096 char due_time_r[100] = { 0 };
1098 localtime_r(&due_time, &ts_ret);
1099 strftime(due_time_r, 30, "%c", &ts_ret);
1100 ALARM_MGR_LOG_PRINT("[alarm-server]:Update alarm: alarm(%d) "
1101 "due_time(%s)\n", alarm_id, due_time_r);
1104 ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
1105 "due_time(%d)", alarm_context.c_due_time, due_time);
1107 if (alarm_context.c_due_time == -1
1108 || due_time < alarm_context.c_due_time) {
1109 _clear_scheduled_alarm_list();
1110 _add_to_scheduled_alarm_list(__alarm_info);
1111 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
1113 alarm_context.c_due_time = due_time;
1114 ALARM_MGR_LOG_PRINT("[alarm-server1]:alarm_context.c_due_time "
1115 "(%d), due_time(%d)", alarm_context.c_due_time, due_time);
1116 } else if (due_time == alarm_context.c_due_time) {
1117 _add_to_scheduled_alarm_list(__alarm_info);
1118 ALARM_MGR_LOG_PRINT("[alarm-server2]:alarm_context.c_due_time "
1119 "(%d), due_time(%d)", alarm_context.c_due_time, due_time);
1126 if (enable_power_on_alarm) {
1127 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID)
1128 * in a platform with app-server.because __alarm_power_on(..) fuction
1129 * don't use first parameter internally, we set this value to 0(zero)
1131 __alarm_power_on(0, enable_power_on_alarm, NULL);
1139 static bool __alarm_delete(int pid, alarm_id_t alarm_id, int *error_code)
1141 bool result = false;
1143 ALARM_MGR_LOG_PRINT("[alarm-server]:delete alarm: alarm(%d) pid(%d)\n",\
1145 result = _remove_from_scheduled_alarm_list(pid, alarm_id);
1147 if (!__alarm_remove_from_list(pid, alarm_id, error_code)) {
1149 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:delete alarm: "
1150 "alarm(%d) pid(%d) has failed with error_code(%d)\n",
1151 alarm_id, pid, *error_code);
1155 if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
1156 _alarm_disable_timer(alarm_context);
1164 if (enable_power_on_alarm) {
1165 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
1166 * platform with app-server.because __alarm_power_on(..) fuction don't
1167 * use first parameter internally, we set this value to 0(zero)
1169 __alarm_power_on(0, enable_power_on_alarm, NULL);
1176 static bool __alarm_power_on(int app_id, bool on_off, int *error_code)
1179 time_t min_time = 0;
1180 time_t current_time = 0;
1181 struct rtc_time rtc_tm = { 0, };
1182 struct tm min_time_r = { 0, };
1186 enable_power_on_alarm = on_off;
1187 /*_update_power_on(on_off); */ /*currently its empty*/
1189 fd = open(power_rtc, O_RDONLY);
1191 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc0\n");
1195 if (on_off == true) {
1196 if (_alarm_find_mintime_power_on(&min_time) == true) {
1198 ab_due_time = min_time;
1200 min_time = min_time - 60;
1202 _alarm_time(¤t_time);
1204 if (min_time <= current_time)
1205 min_time = current_time + 5;
1207 gmtime_r(&min_time, &min_time_r);
1209 ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
1211 min_time_r.tm_year,\
1213 min_time_r.tm_mday,\
1214 min_time_r.tm_hour,\
1216 ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
1218 min_time_r.tm_year,\
1220 min_time_r.tm_mday,\
1221 min_time_r.tm_hour,\
1224 rtc_tm.tm_mday = min_time_r.tm_mday;
1225 rtc_tm.tm_mon = min_time_r.tm_mon;
1226 rtc_tm.tm_year = min_time_r.tm_year;
1227 rtc_tm.tm_hour = min_time_r.tm_hour;
1228 rtc_tm.tm_min = min_time_r.tm_min;
1229 rtc_tm.tm_sec = min_time_r.tm_sec;
1230 /*set_info.time_zone = 0; */
1231 /*set_info.u_interval.day_of_week = 0; */
1233 /*ALARM_MGR_LOG_PRINT("####__alarm_power_on : %d %d
1234 %d %d %d\n",set_info.year,set_info.month,set_info.day,
1235 set_info.hour,set_info.minute); */
1237 ALARM_MGR_LOG_PRINT("due_time : %d \n", ab_due_time);
1239 ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
1240 "%d-%d-%d, %02d:%02d:%02d.\n",
1241 rtc_tm.tm_mday, rtc_tm.tm_mon + 1,
1242 rtc_tm.tm_year + 1900, rtc_tm.tm_hour,
1243 rtc_tm.tm_min, rtc_tm.tm_sec);
1245 retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
1247 if (errno == ENOTTY) {
1248 ALARM_MGR_EXCEPTION_PRINT(
1249 "\n...Alarm IRQs not supported.\n");
1251 ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
1255 ALARM_MGR_LOG_PRINT("[alarm-server]RTC "
1256 "alarm(POWER ON) is setted");
1258 /* Enable alarm interrupts */
1259 retval = ioctl(fd, RTC_AIE_ON, 0);
1261 ALARM_MGR_EXCEPTION_PRINT(
1262 "RTC_AIE_ON ioctl failed");
1266 ALARM_MGR_LOG_PRINT("[alarm-server]RTC(POWER ON) "
1270 retval = ioctl(fd, RTC_AIE_OFF, 0);
1272 ALARM_MGR_LOG_PRINT("__alarm_power_on : off\n");
1273 retval = ioctl(fd, RTC_AIE_OFF, 0);
1277 #endif /* #ifdef __ALARM_BOOT */
1282 static bool __alarm_power_off(int app_id, int *error_code)
1286 #endif /* #ifdef __ALARM_BOOT */
1290 static bool __alarm_check_next_duetime(int app_id, int *error_code)
1293 time_t current_time;
1296 _alarm_time(¤t_time);
1298 interval = ab_due_time - current_time;
1300 ALARM_MGR_LOG_PRINT("due_time : %d / current_time %d\n", ab_due_time,
1303 if (interval > 0 && interval <= 60)
1307 #else /* #ifdef __ALARM_BOOT */
1312 static void __alarm_send_noti_to_application(const char *app_service_name,
1313 alarm_id_t alarm_id)
1317 char service_name[MAX_SERVICE_NAME_LEN];
1318 char object_name[MAX_SERVICE_NAME_LEN];
1320 DBusMessage *message;
1321 DBusMessageIter iter;
1323 if (app_service_name == NULL || strlen(app_service_name) == 0) {
1324 ALARM_MGR_EXCEPTION_PRINT("This alarm destination is "
1328 memset(service_name, 0, MAX_SERVICE_NAME_LEN);
1329 memcpy(service_name, app_service_name, strlen(app_service_name));
1331 snprintf(object_name, MAX_SERVICE_NAME_LEN,
1332 "/org/tizen/alarm/client");
1334 ALARM_MGR_LOG_PRINT("[alarm server][send expired_alarm(alarm_id=%d)to"
1335 "app_service_name(%s), object_name(%s), interface_name(%s)]\n",\
1336 alarm_id, service_name, object_name, "org.tizen.alarm.client");
1338 message = dbus_message_new_method_call(service_name,
1340 "org.tizen.alarm.client",
1342 if (message == NULL) {
1343 ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
1344 "dbus_message_new_method_call faild. maybe OOM!.\n");
1345 ALARM_MGR_EXCEPTION_PRINT("[alarm server] so we cannot "
1346 "send expired alarm to %s\n", service_name);
1350 dbus_message_set_no_reply(message, TRUE);
1351 /* if(service_name[0]==':') */
1352 /* we don't need auto activation in a case that
1353 destination_app_service_name starts with a charactor like (:) */
1354 dbus_message_set_auto_start(message, FALSE);
1356 dbus_message_iter_init_append(message, &iter);
1357 if (!dbus_message_iter_append_basic
1358 (&iter, DBUS_TYPE_INT32, &alarm_id)) {
1359 dbus_message_unref(message);
1360 ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
1361 "dbus_message_iter_append_basic faild. maybe OOM!.\n");
1365 dbus_connection_send(dbus_g_connection_get_connection
1366 (alarm_context.bus), message, NULL);
1367 dbus_connection_flush(dbus_g_connection_get_connection
1368 (alarm_context.bus));
1369 dbus_message_unref(message);
1373 static void __alarm_expired()
1375 const char *destination_app_service_name = NULL;
1376 alarm_id_t alarm_id = -1;
1378 __alarm_info_t *__alarm_info = NULL;
1379 char alarm_id_val[32]={0,};
1384 ALARM_MGR_LOG_PRINT("[alarm-server]: Enter \n");
1386 time_t current_time;
1389 _alarm_time(¤t_time);
1391 interval = difftime(alarm_context.c_due_time, current_time);
1392 ALARM_MGR_LOG_PRINT("[alarm-server]: c_due_time(%d), "
1393 "current_time(%d), interval(%d)\n", alarm_context.c_due_time,
1394 current_time, interval);
1396 if (alarm_context.c_due_time > current_time + 1) {
1397 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: False Alarm. due time is (%d) seconds future\n",
1398 alarm_context.c_due_time - current_time);
1401 // 10 seconds is maximum permitted delay from timer expire to this function
1402 if (alarm_context.c_due_time + 10 < current_time) {
1403 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: False Alarm. due time is (%d) seconds past\n",
1404 current_time - alarm_context.c_due_time);
1408 GSList *iter = NULL;
1409 __scheduled_alarm_t *alarm = NULL;
1411 for (iter = g_scheduled_alarm_list; iter != NULL;
1412 iter = g_slist_next(iter)) {
1414 alarm_id = alarm->alarm_id;
1416 __alarm_info = alarm->__alarm_info;
1418 app_pid = __alarm_info->pid;
1421 (g_quark_to_string(__alarm_info->quark_bundle),
1424 b_len = strlen(g_quark_to_string(__alarm_info->quark_bundle));
1426 b = bundle_decode((bundle_raw *)g_quark_to_string(__alarm_info->quark_bundle), b_len);
1430 ALARM_MGR_EXCEPTION_PRINT("Error!!!..Unable to decode the bundle!!\n");
1434 snprintf(alarm_id_val,31,"%d",alarm_id);
1436 if (bundle_add(b,"http://tizen.org/appcontrol/data/alarm_id", alarm_id_val)){
1437 ALARM_MGR_EXCEPTION_PRINT("Unable to add alarm id to the bundle\n");
1441 appid = appsvc_get_appid(b);
1442 if( (__alarm_info->alarm_info.alarm_type & ALARM_TYPE_NOLAUNCH) && !aul_app_is_running(appid))
1444 ALARM_MGR_EXCEPTION_PRINT("This alarm is ignored\n");
1448 if ( appsvc_run_service(b, 0, NULL, NULL) < 0)
1450 ALARM_MGR_EXCEPTION_PRINT("Unable to run app svc\n");
1454 ALARM_MGR_LOG_PRINT("Successfuly ran app svc\n");
1465 (g_quark_to_string(__alarm_info->quark_dst_service_name),
1467 ALARM_MGR_LOG_PRINT("[alarm-server]:destination is "
1468 "null, so we send expired alarm to %s(%u)\n",\
1470 __alarm_info->quark_app_service_name),
1471 __alarm_info->quark_app_service_name);
1472 destination_app_service_name = g_quark_to_string(
1473 __alarm_info->quark_app_service_name_mod);
1475 ALARM_MGR_LOG_PRINT("[alarm-server]:destination "
1478 __alarm_info->quark_dst_service_name),
1479 __alarm_info->quark_dst_service_name);
1480 destination_app_service_name = g_quark_to_string(
1481 __alarm_info->quark_dst_service_name_mod);
1485 /* orginally this code had if(__alarm_info->app_id==21) in a
1486 platform with app-server. */
1487 /*if(__alarm_info->quark_dst_service_name ==
1488 g_quark_from_string (WAKEUP_ALARM_APP_ID)) */
1490 (g_quark_to_string(__alarm_info->quark_dst_service_name),
1491 WAKEUP_ALARM_APP_ID) == 0) {
1493 fd = open(power_rtc, O_RDONLY);
1495 ALARM_MGR_LOG_PRINT("cannot open /dev/rtc0\n");
1497 ioctl(fd, RTC_AIE_OFF, 0);
1504 * we should consider a situation that
1505 * destination_app_service_name is owner_name like (:xxxx) and
1506 * application's pid which registered this alarm was killed.In that case,
1507 * we don't need to send the expire event because the process was killed.
1508 * this causes needless message to be sent.
1510 ALARM_MGR_LOG_PRINT("[alarm-server]: "
1511 "destination_app_service_name :%s, app_pid=%d\n",
1512 destination_app_service_name, app_pid);
1513 /* the following is a code that checks the above situation.
1514 please verify this code. */
1516 if (dbus_bus_name_has_owner(
1517 dbus_g_connection_get_connection(alarm_context.bus),
1518 destination_app_service_name, NULL) == FALSE) {
1519 __expired_alarm_t *expire_info;
1520 char appid[MAX_SERVICE_NAME_LEN] = { 0, };
1521 char alarm_id_str[32] = { 0, };
1523 if (__alarm_info->alarm_info.alarm_type & ALARM_TYPE_WITHCB) {
1524 __alarm_remove_from_list(__alarm_info->pid, alarm_id, NULL);
1528 expire_info = malloc(sizeof(__expired_alarm_t));
1529 if (G_UNLIKELY(NULL == expire_info)){
1530 ALARM_MGR_ASSERT_PRINT("[alarm-server]:Malloc failed!Can't notify alarm expiry info\n");
1533 memset(expire_info, '\0', sizeof(__expired_alarm_t));
1534 strncpy(expire_info->service_name,
1535 destination_app_service_name,
1536 MAX_SERVICE_NAME_LEN-1);
1537 expire_info->alarm_id = alarm_id;
1538 g_expired_alarm_list =
1539 g_slist_append(g_expired_alarm_list, expire_info);
1543 (g_quark_to_string(__alarm_info->quark_dst_service_name),
1545 strncpy(appid,g_quark_to_string(__alarm_info->quark_app_service_name)+6,strlen(g_quark_to_string(__alarm_info->quark_app_service_name))-6);
1549 strncpy(appid,g_quark_to_string(__alarm_info->quark_dst_service_name)+6,strlen(g_quark_to_string(__alarm_info->quark_dst_service_name))-6);
1552 snprintf(alarm_id_str, 31, "%d", alarm_id);
1554 ALARM_MGR_LOG_PRINT("before aul_launch appid(%s) "
1555 "alarm_id_str(%s)\n", appid, alarm_id_str);
1558 kb = bundle_create();
1559 bundle_add(kb, "__ALARM_MGR_ID", alarm_id_str);
1560 aul_launch_app(appid, kb);
1563 ALARM_MGR_LOG_PRINT(
1564 "before alarm_send_noti_to_application\n");
1565 __alarm_send_noti_to_application(
1566 destination_app_service_name, alarm_id);
1569 ALARM_MGR_LOG_PRINT("after __alarm_send_noti_to_application\n");
1571 /* if( !(__alarm_info->alarm_info.alarm_type
1572 & ALARM_TYPE_VOLATILE) ) {
1573 __alarm_remove_from_list(__alarm_info->pid,
1577 if (__alarm_info->alarm_info.mode.repeat
1578 == ALARM_REPEAT_MODE_ONCE) {
1579 /* _alarm_next_duetime(__alarm_info);*/
1580 /* _update_alarms(__alarm_info);*/
1581 __alarm_remove_from_list(__alarm_info->pid, alarm_id,
1585 _alarm_next_duetime(__alarm_info);
1586 /* _update_alarms(__alarm_info);*/
1592 _clear_scheduled_alarm_list();
1593 alarm_context.c_due_time = -1;
1595 ALARM_MGR_LOG_PRINT("[alarm-server]: Leave \n");
1598 static gboolean __alarm_handler_idle()
1600 if (g_dummy_timer_is_set == true) {
1601 ALARM_MGR_LOG_PRINT("dummy alarm timer has expired\n");
1603 ALARM_MGR_LOG_PRINT("__alarm_handler \n");
1604 ALARM_MGR_LOG_PRINT("__alarm_handler \n");
1616 if (enable_power_on_alarm) {
1617 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID)
1618 *in a platform with app-server.because __alarm_power_on(..) fuction
1619 *don't use first parameter internally, we set this value to 0(zero)
1621 __alarm_power_on(0, enable_power_on_alarm, NULL);
1624 pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
1629 static void __alarm_handler(int sigNum, siginfo_t *pSigInfo, void *pUContext)
1632 pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
1634 /* we moved __alarm_expired() function to __alarm_handler_idle GSource
1635 because of signal safety. */
1636 g_idle_add_full(G_PRIORITY_HIGH_IDLE, __alarm_handler_idle, NULL, NULL);
1639 static void __clean_registry()
1642 /*TODO:remove all db entries */
1645 static bool __alarm_manager_reset()
1648 _alarm_disable_timer(alarm_context);
1650 __alarm_clean_list();
1652 _clear_scheduled_alarm_list();
1658 static void __on_system_time_changed(keynode_t *node, void *data)
1664 _alarm_disable_timer(alarm_context);
1667 _time = vconf_keynode_get_int(node);
1669 vconf_get_int(VCONFKEY_SYSTEM_TIMECHANGE, (int *)&_time);
1672 _alarm_time(&before);
1673 diff_time = difftime(_time, before);
1677 ALARM_MGR_EXCEPTION_PRINT("New time is %s, diff_time is %f\n", ctime(&_time), diff_time);
1679 ALARM_MGR_LOG_PRINT("[alarm-server] System time has been changed\n");
1680 ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n",
1681 alarm_context.c_due_time);
1685 vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED,(int)diff_time);
1687 __alarm_update_due_time_of_all_items_in_list(diff_time);
1689 ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n",
1690 alarm_context.c_due_time);
1691 _clear_scheduled_alarm_list();
1696 if (enable_power_on_alarm) {
1697 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
1698 APP_ID) in a platform with app-server. because _alarm_power_
1699 on(..) fuction don't use first parameter internally, we set
1700 this value to 0(zero)
1702 __alarm_power_on(0, enable_power_on_alarm, NULL);
1708 static void __on_system_time_external_changed(keynode_t *node, void *data)
1711 time_t cur_time = 0;
1713 _alarm_disable_timer(alarm_context);
1716 diff_time = vconf_keynode_get_dbl(node);
1718 vconf_get_dbl(VCONFKEY_SYSTEM_TIMECHANGE_EXTERNAL, &diff_time);
1722 _alarm_time(&cur_time);
1724 ALARM_MGR_EXCEPTION_PRINT("diff_time is %f, New time is %s\n", diff_time, ctime(&cur_time));
1726 ALARM_MGR_LOG_PRINT("[alarm-server] System time has been changed externally\n");
1727 ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n",
1728 alarm_context.c_due_time);
1730 // set rtc time only because the linux time is set externally
1731 _set_rtc_time(cur_time);
1733 vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED,(int)diff_time);
1735 __alarm_update_due_time_of_all_items_in_list(diff_time);
1737 ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n",
1738 alarm_context.c_due_time);
1739 _clear_scheduled_alarm_list();
1744 if (enable_power_on_alarm) {
1745 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
1746 APP_ID) in a platform with app-server. because _alarm_power_
1747 on(..) fuction don't use first parameter internally, we set
1748 this value to 0(zero)
1750 __alarm_power_on(0, enable_power_on_alarm, NULL);
1756 static void __on_time_zone_changed(keynode_t *node, void *data)
1758 double diff_time = 0;
1760 _alarm_disable_timer(alarm_context);
1764 ALARM_MGR_LOG_PRINT("[alarm-server] time zone has been changed\n");
1765 ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n", alarm_context.c_due_time);
1767 __alarm_update_due_time_of_all_items_in_list(diff_time);
1769 ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n", alarm_context.c_due_time);
1770 _clear_scheduled_alarm_list();
1775 if (enable_power_on_alarm) {
1776 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
1777 APP_ID) in a platform with app-server. because _alarm_power_
1778 on(..) fuction don't use first parameter internally, we set
1779 this value to 0(zero)
1781 __alarm_power_on(0, enable_power_on_alarm, NULL);
1788 gboolean alarm_manager_alarm_set_rtc_time(void *pObject, int pid,
1789 int year, int mon, int day,
1790 int hour, int min, int sec, char *e_cookie,
1792 guchar *cookie = NULL;
1795 gboolean result = true;
1797 const char *rtc = power_rtc;
1799 struct rtc_time rtc_tm = {0,};
1800 struct rtc_wkalrm rtc_wk;
1801 struct tm *alarm_tm = NULL;
1802 struct tm due_tm = {0,};
1806 *return_code = ALARMMGR_RESULT_SUCCESS;
1809 cookie = g_base64_decode(e_cookie, &size);
1813 *return_code = ERR_ALARM_NO_PERMISSION;
1814 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
1818 retval = security_server_check_privilege_by_cookie((const char *)cookie, "alarm-manager::alarm", "w");
1820 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1821 ALARM_MGR_EXCEPTION_PRINT(
1822 "%s", "Write access has been denied by smack\n");
1824 #ifdef __ALLOW_NO_PRIVILEGE
1825 ALARM_MGR_EXCEPTION_PRINT("Error has occurred in security_server_check_privilege()\n");
1827 *return_code = ERR_ALARM_NO_PERMISSION;
1830 #ifdef __ALLOW_NO_PRIVILEGE
1835 /*extract day of the week, day in the year &
1836 daylight saving time from system*/
1839 alarm_tm = localtime(&ctime);
1841 alarm_tm->tm_year = year;
1842 alarm_tm->tm_mon = mon;
1843 alarm_tm->tm_mday = day;
1844 alarm_tm->tm_hour = hour;
1845 alarm_tm->tm_min = min;
1846 alarm_tm->tm_sec = sec;
1848 /*convert to calendar time representation*/
1849 time_t rtc_time = mktime(alarm_tm);
1851 /*convert to Coordinated Universal Time (UTC)*/
1852 gmtime_r(&rtc_time, &due_tm);
1854 fd = open(rtc, O_RDONLY);
1856 ALARM_MGR_EXCEPTION_PRINT("RTC open failed.\n");
1858 *return_code = ERR_ALARM_SYSTEM_FAIL;
1862 /* Read the RTC time/date */
1863 retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
1865 ALARM_MGR_EXCEPTION_PRINT("RTC_RD_TIME ioctl failed");
1868 *return_code = ERR_ALARM_SYSTEM_FAIL;
1872 rtc_tm.tm_mday = due_tm.tm_mday;
1873 rtc_tm.tm_mon = due_tm.tm_mon;
1874 rtc_tm.tm_year = due_tm.tm_year;
1875 rtc_tm.tm_hour = due_tm.tm_hour;
1876 rtc_tm.tm_min = due_tm.tm_min;
1877 rtc_tm.tm_sec = due_tm.tm_sec;
1879 memcpy(&rtc_wk.time, &rtc_tm, sizeof(rtc_tm));
1884 retval = ioctl(fd, RTC_WKALM_BOOT_SET, &rtc_wk);
1886 if (errno == ENOTTY) {
1887 ALARM_MGR_EXCEPTION_PRINT("\nAlarm IRQs not"
1890 ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
1893 *return_code = ERR_ALARM_SYSTEM_FAIL;
1896 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is setted");
1897 /* Enable alarm interrupts */
1898 retval = ioctl(fd, RTC_AIE_ON, 0);
1900 ALARM_MGR_EXCEPTION_PRINT("RTC_AIE_ON ioctl failed");
1902 *return_code = ERR_ALARM_SYSTEM_FAIL;
1917 gboolean alarm_manager_alarm_create_appsvc(void *pObject, int pid,
1919 int start_month, int start_day,
1920 int start_hour, int start_min,
1921 int start_sec, int end_year, int end_month,
1922 int end_day, int mode_day_of_week,
1923 int mode_repeat, int alarm_type,
1925 char *bundle_data, char *e_cookie,
1926 int *alarm_id, int *return_code)
1928 alarm_info_t alarm_info;
1929 guchar *cookie = NULL;
1932 gboolean result = true;
1934 alarm_info.start.year = start_year;
1935 alarm_info.start.month = start_month;
1936 alarm_info.start.day = start_day;
1937 alarm_info.start.hour = start_hour;
1938 alarm_info.start.min = start_min;
1939 alarm_info.start.sec = start_sec;
1941 alarm_info.end.year = end_year;
1942 alarm_info.end.month = end_month;
1943 alarm_info.end.day = end_day;
1945 alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
1946 alarm_info.mode.repeat = mode_repeat;
1948 alarm_info.alarm_type = alarm_type;
1949 alarm_info.reserved_info = reserved_info;
1953 cookie = g_base64_decode(e_cookie, &size);
1957 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
1961 retval = security_server_check_privilege_by_cookie((const char *)cookie, "alarm-manager::alarm", "w");
1963 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1964 ALARM_MGR_EXCEPTION_PRINT(
1965 "%s", "Write access has been denied by smack\n");
1967 #ifdef __ALLOW_NO_PRIVILEGE
1968 ALARM_MGR_EXCEPTION_PRINT("Error has occurred in security_server_check_privilege()\n");
1973 #ifdef __ALLOW_NO_PRIVILEGE
1977 result = __alarm_create_appsvc(&alarm_info, alarm_id, pid,
1978 bundle_data, return_code);
1979 if (false == result)
1981 ALARM_MGR_EXCEPTION_PRINT("Unable to create alarm!\n");
1993 gboolean alarm_manager_alarm_create(void *pObject, int pid,
1994 char *app_service_name, char *app_service_name_mod, int start_year,
1995 int start_month, int start_day,
1996 int start_hour, int start_min,
1997 int start_sec, int end_year, int end_month,
1998 int end_day, int mode_day_of_week,
1999 int mode_repeat, int alarm_type,
2001 char *reserved_service_name, char *reserved_service_name_mod, char *e_cookie,
2002 int *alarm_id, int *return_code)
2004 alarm_info_t alarm_info;
2009 alarm_info.start.year = start_year;
2010 alarm_info.start.month = start_month;
2011 alarm_info.start.day = start_day;
2012 alarm_info.start.hour = start_hour;
2013 alarm_info.start.min = start_min;
2014 alarm_info.start.sec = start_sec;
2016 alarm_info.end.year = end_year;
2017 alarm_info.end.month = end_month;
2018 alarm_info.end.day = end_day;
2020 alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
2021 alarm_info.mode.repeat = mode_repeat;
2023 alarm_info.alarm_type = alarm_type;
2024 alarm_info.reserved_info = reserved_info;
2028 cookie = g_base64_decode(e_cookie, &size);
2030 retval = security_server_check_privilege_by_cookie((const char *)cookie, "alarm-manager::alarm", "w");
2032 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
2033 ALARM_MGR_EXCEPTION_PRINT(
2034 "%s", "Write access has been denied by smack\n");
2036 #ifdef __ALLOW_NO_PRIVILEGE
2037 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
2042 #ifdef __ALLOW_NO_PRIVILEGE
2046 /* return valule and return_code should be checked */
2047 __alarm_create(&alarm_info, alarm_id, pid, app_service_name,app_service_name_mod,
2048 reserved_service_name, reserved_service_name_mod, return_code);
2056 gboolean alarm_manager_alarm_delete(void *pObject, int pid, alarm_id_t alarm_id,
2057 char *e_cookie, int *return_code)
2063 cookie = g_base64_decode(e_cookie, &size);
2065 retval = security_server_check_privilege_by_cookie((const char *)cookie, "alarm-manager::alarm", "w");
2067 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
2068 ALARM_MGR_EXCEPTION_PRINT(
2069 "%s", "Write access has been denied by smack\n");
2071 #ifdef __ALLOW_NO_PRIVILEGE
2072 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
2077 #ifdef __ALLOW_NO_PRIVILEGE
2081 __alarm_delete(pid, alarm_id, return_code);
2089 gboolean alarm_manager_alarm_power_on(void *pObject, int pid, bool on_off,
2093 return __alarm_power_on(pid, on_off, return_code);
2096 gboolean alarm_manager_alarm_power_off(void *pObject, int pid, int *return_code)
2099 return __alarm_power_off(pid, return_code);
2102 bool alarm_manager_alarm_check_next_duetime(void *pObject, int pid,
2105 return __alarm_check_next_duetime(pid, return_code);
2108 gboolean alarm_manager_alarm_update(void *pObject, int pid,
2109 char *app_service_name, alarm_id_t alarm_id,
2110 int start_year, int start_month,
2111 int start_day, int start_hour,
2112 int start_min, int start_sec, int end_year,
2113 int end_month, int end_day,
2114 int mode_day_of_week, int mode_repeat,
2115 int alarm_type, int reserved_info,
2118 alarm_info_t alarm_info;
2119 alarm_info.start.year = start_year;
2120 alarm_info.start.month = start_month;
2121 alarm_info.start.day = start_day;
2122 alarm_info.start.hour = start_hour;
2123 alarm_info.start.min = start_min;
2124 alarm_info.start.sec = start_sec;
2126 alarm_info.end.year = end_year;
2127 alarm_info.end.month = end_month;
2128 alarm_info.end.day = end_day;
2130 alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
2131 alarm_info.mode.repeat = mode_repeat;
2133 alarm_info.alarm_type = alarm_type;
2134 alarm_info.reserved_info = reserved_info;
2138 __alarm_update(pid, app_service_name, alarm_id, &alarm_info,
2144 gboolean alarm_manager_alarm_get_number_of_ids(void *pObject, int pid,
2148 GSList *gs_iter = NULL;
2149 GQuark quark_app_unique_name; /* the fullpath of pid(pid) is
2150 converted to quark value. */
2151 char proc_file[256] = { 0 };
2152 char process_name[512] = { 0 };
2153 char app_name[256] = { 0 };
2155 __alarm_info_t *entry = NULL;
2156 char *proc_name_ptr = NULL;
2161 /* we should consider to check whether pid is running or Not
2163 memset(process_name, '\0', 512);
2164 memset(proc_file, '\0', 256);
2165 snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
2170 fd = open(proc_file, O_RDONLY);
2171 if (fd < 0) { /* failure */
2172 quark_app_unique_name = g_quark_from_string("unknown");
2173 memcpy(app_name, "unknown", strlen("unknown") + 1);
2175 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
2176 "killed, so we failed to get proc file(%s) \n",
2178 *return_code = -1; /* -1 means that system
2179 failed internally. */
2182 ret = read(fd, process_name, 512);
2184 *return_code = -1; /* -1 means that system
2185 failed internally. */
2190 while (process_name[i] != '\0') {
2191 if (process_name[i] == ' ') {
2192 process_name[i] = '\0';
2197 /*if (readlink(proc_file, process_name, 256)!=-1) */
2200 word = strtok_r(process_name, "/", &proc_name_ptr);
2201 while (word != NULL) {
2202 memset(app_name, 0, 256);
2203 snprintf(app_name, 256, "%s", word);
2204 word = strtok_r(NULL, "/", &proc_name_ptr);
2206 quark_app_unique_name = g_quark_from_string(app_name);
2209 ALARM_MGR_LOG_PRINT("called for app(pid:%d, name=%s)\n",
2212 for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2213 gs_iter = g_slist_next(gs_iter)) {
2214 entry = gs_iter->data;
2215 ALARM_MGR_LOG_PRINT("alarm_manager_alarm_get_number_of_ids(): "
2216 "app_name=%s,quark_app_unique_name=%s\n",
2217 app_name, g_quark_to_string(entry->quark_app_unique_name));
2220 g_quark_to_string(entry->quark_app_unique_name)) == 0
2221 && strcmp(app_name, "unknown") != 0) {
2223 ALARM_MGR_LOG_PRINT("inc number of alarms of app "
2224 "(pid:%d, name:%s) is %d\n",
2225 pid, app_name, *num_of_ids);
2229 ALARM_MGR_LOG_PRINT("number of alarms of app(pid:%d, name:%s) is %d\n",
2230 pid, app_name, *num_of_ids);
2234 gboolean alarm_manager_alarm_get_list_of_ids(void *pObject, int pid,
2235 int max_number_of_ids,
2236 GArray **arr, int *num_of_ids,
2239 GSList *gs_iter = NULL;
2240 GQuark quark_app_unique_name; /* the fullpath of pid(pid) is converted
2242 char proc_file[256] = { 0 };
2243 char process_name[512] = { 0 };
2244 char app_name[256] = { 0 };
2246 __alarm_info_t *entry = NULL;
2248 char *proc_name_ptr = NULL;
2252 GArray *garray = NULL;
2256 garray = g_array_new(false, true, sizeof(alarm_id_t));
2258 /* we should check that there is a resource leak.
2259 * Now we don't have free code for g_array_new().
2261 if (max_number_of_ids <= 0) {
2263 ALARM_MGR_EXCEPTION_PRINT("called for pid(%d), but "
2264 "max_number_of_ids(%d) is less than 0.\n",
2265 pid, max_number_of_ids);
2269 /* we should consider to check whether pid is running or Not
2271 memset(process_name, '\0', 512);
2272 memset(proc_file, '\0', 256);
2273 snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
2275 fd = open(proc_file, O_RDONLY);
2276 if (fd < 0) { /* failure */
2277 quark_app_unique_name = g_quark_from_string("unknown");
2278 memcpy(app_name, "unknown", strlen("unknown") + 1);
2280 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
2281 "killed, so we failed to get proc file(%s)\n", pid, proc_file);
2283 /* -1 means that system failed internally. */
2286 ret = read(fd, process_name, 512);
2289 /* -1 means that system failed internally. */
2294 while (process_name[i] != '\0') {
2295 if (process_name[i] == ' ') {
2296 process_name[i] = '\0';
2301 /* if (readlink(proc_file, process_name, 256)!=-1) */
2304 word = strtok_r(process_name, "/", &proc_name_ptr);
2305 while (word != NULL) {
2306 memset(app_name, 0, 256);
2307 snprintf(app_name, 256, "%s", word);
2308 word = strtok_r(NULL, "/", &proc_name_ptr);
2310 quark_app_unique_name = g_quark_from_string(app_name);
2313 ALARM_MGR_LOG_PRINT("called for app(pid:%d, name=%s)\n",
2316 for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2317 gs_iter = g_slist_next(gs_iter)) {
2318 entry = gs_iter->data;
2321 g_quark_to_string(entry->quark_app_unique_name)) == 0
2322 && strcmp(app_name, "unknown") != 0) {
2323 g_array_append_val(garray, entry->alarm_id);
2327 *num_of_ids = index;
2334 gboolean alarm_manager_alarm_get_appsvc_info(void *pObject, int pid, alarm_id_t alarm_id,
2335 char *e_cookie, gchar **b_data, int *return_code)
2339 GSList *gs_iter = NULL;
2340 __alarm_info_t *entry = NULL;
2342 guchar *cookie = NULL;
2346 ALARM_MGR_LOG_PRINT("called for pid(%d) and alarm_id(%d)\n", pid,
2349 cookie = g_base64_decode(e_cookie, &size);
2353 *return_code = ERR_ALARM_SYSTEM_FAIL;
2354 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
2358 retval = security_server_check_privilege_by_cookie((const char *)cookie, "alarm-manager::alarm", "r");
2360 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
2361 ALARM_MGR_EXCEPTION_PRINT(
2362 "%s", "Read access has been denied by smack\n");
2364 #ifdef __ALLOW_NO_PRIVILEGE
2365 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
2368 *return_code = ERR_ALARM_NO_PERMISSION;
2380 for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2381 gs_iter = g_slist_next(gs_iter)) {
2382 entry = gs_iter->data;
2383 if (entry->alarm_id == alarm_id) {
2385 *b_data = g_strdup(g_quark_to_string(entry->quark_bundle));
2391 if ( *b_data && strlen(*b_data) == 4 && strncmp(*b_data,"null",4) == 0){
2392 ALARM_MGR_EXCEPTION_PRINT("Regular alarm,not svc alarm");
2394 *return_code = ERR_ALARM_INVALID_TYPE;
2398 *return_code = ERR_ALARM_INVALID_ID;
2407 gboolean alarm_manager_alarm_get_info(void *pObject, int pid,
2408 alarm_id_t alarm_id, int *start_year,
2409 int *start_month, int *start_day,
2410 int *start_hour, int *start_min,
2411 int *start_sec, int *end_year,
2412 int *end_month, int *end_day,
2413 int *mode_day_of_week, int *mode_repeat,
2414 int *alarm_type, int *reserved_info,
2417 ALARM_MGR_LOG_PRINT("called for pid(%d) and alarm_id(%d)\n", pid,
2420 GSList *gs_iter = NULL;
2421 __alarm_info_t *entry = NULL;
2423 alarm_info_t *alarm_info = NULL;
2426 for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2427 gs_iter = g_slist_next(gs_iter)) {
2428 entry = gs_iter->data;
2429 if (entry->alarm_id == alarm_id) {
2430 alarm_info = &(entry->alarm_info);
2435 if (alarm_info == NULL)
2437 ALARM_MGR_EXCEPTION_PRINT("alarm id(%d) was not found\n",
2439 *return_code = ERR_ALARM_INVALID_ID;
2441 ALARM_MGR_LOG_PRINT("alarm was found\n");
2442 *start_year = alarm_info->start.year;
2443 *start_month = alarm_info->start.month;
2444 *start_day = alarm_info->start.day;
2445 *start_hour = alarm_info->start.hour;
2446 *start_min = alarm_info->start.min;
2447 *start_sec = alarm_info->start.sec;
2449 *end_year = alarm_info->end.year;
2450 *end_year = alarm_info->end.month;
2451 *end_year = alarm_info->end.day;
2453 *mode_day_of_week = alarm_info->mode.u_interval.day_of_week;
2454 *mode_repeat = alarm_info->mode.repeat;
2456 *alarm_type = alarm_info->alarm_type;
2457 *reserved_info = alarm_info->reserved_info;
2464 gboolean alarm_manager_alarm_get_next_duetime(void *pObject, int pid,
2465 alarm_id_t alarm_id, time_t* duetime,
2468 ALARM_MGR_LOG_PRINT("called for pid(%d) and alarm_id(%d)\n", pid,
2471 GSList *gs_iter = NULL;
2472 __alarm_info_t *entry = NULL;
2473 __alarm_info_t *find_item = NULL;
2477 for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2478 gs_iter = g_slist_next(gs_iter)) {
2479 entry = gs_iter->data;
2480 if (entry->alarm_id == alarm_id) {
2486 if (find_item == NULL)
2488 ALARM_MGR_EXCEPTION_PRINT("alarm id(%d) was not found\n",
2490 *return_code = ERR_ALARM_INVALID_ID;
2492 ALARM_MGR_LOG_PRINT("alarm was found\n");
2493 *duetime = _alarm_next_duetime(find_item);
2499 #include "alarm-skeleton.h"
2501 typedef struct AlarmManagerObject AlarmManagerObject;
2502 typedef struct AlarmManagerObjectClass AlarmManagerObjectClass;
2503 GType Server_Object_get_type(void);
2504 struct AlarmManagerObject {
2507 struct AlarmManagerObjectClass {
2508 GObjectClass parent;
2511 #define DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT 0
2513 #define ALARM_MANAGER_TYPE_OBJECT (Server_Object_get_type())
2514 #define ALARM_MANAGER_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST \
2515 ((object), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObject))
2516 #define ALARM_MANAGER_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST \
2517 ((klass), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObjectClass))
2518 #define ALARM_MANAGER_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE \
2519 ((object), ALARM_MANAGER_TYPE_OBJECT))
2520 #define ALARM_MANAGER_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE \
2521 ((klass), ALARM_MANAGER_TYPE_OBJECT))
2522 #define ALARM_MANAGER_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS \
2523 ((obj), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObjectClass))
2524 G_DEFINE_TYPE(AlarmManagerObject, Server_Object, G_TYPE_OBJECT)
2525 static void Server_Object_init(AlarmManagerObject * obj)
2530 static void Server_Object_class_init(AlarmManagerObjectClass *klass)
2535 static void __initialize_timer()
2537 struct sigaction sig_timer;
2538 sigemptyset(&sig_timer.sa_mask);
2539 sig_timer.sa_flags = SA_SIGINFO;
2540 sig_timer.sa_sigaction = (void *)__alarm_handler;
2541 sigaction(SIG_TIMER, &sig_timer, NULL);
2543 alarm_context.timer = _alarm_create_timer();
2547 static void __initialize_alarm_list()
2550 alarm_context.alarms = NULL;
2551 alarm_context.c_due_time = -1;
2553 _load_alarms_from_registry();
2555 __rtc_set(); /*Set RTC1 Alarm with alarm due time for alarm-manager initialization*/
2560 if (enable_power_on_alarm) {
2561 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
2562 * platform with app-server. because __alarm_power_on(..) fuction don't
2563 * use first parameter internally, we set this value to 0(zero)
2565 __alarm_power_on(0, enable_power_on_alarm, NULL);
2570 static void __initialize_scheduled_alarm_list()
2572 _init_scheduled_alarm_list();
2576 static void __hibernation_leave_callback()
2579 __initialize_scheduled_alarm_list();
2581 __alarm_clean_list();
2583 __initialize_alarm_list();
2586 static bool __initialize_noti()
2589 int fd = heynoti_init();
2591 ALARM_MGR_EXCEPTION_PRINT("fail to heynoti_init\n");
2594 heynoti_subscribe(fd, "HIBERNATION_LEAVE", __hibernation_leave_callback,
2596 ret = heynoti_attach_handler(fd);
2598 ALARM_MGR_EXCEPTION_PRINT("heynoti_attach_handler fail");
2601 if (vconf_notify_key_changed
2602 (VCONFKEY_SYSTEM_TIMECHANGE, __on_system_time_changed, NULL) < 0) {
2603 ALARM_MGR_LOG_PRINT(
2604 "Failed to add callback for time changing event\n");
2606 /*system state change noti ó¸® */
2608 if (vconf_notify_key_changed
2609 (VCONFKEY_SETAPPL_TIMEZONE_ID, __on_time_zone_changed, NULL) < 0) {
2610 ALARM_MGR_LOG_PRINT(
2611 "Failed to add callback for time zone changing event\n");
2614 if (vconf_notify_key_changed
2615 (VCONFKEY_SYSTEM_TIMECHANGE_EXTERNAL, __on_system_time_external_changed, NULL) < 0) {
2616 ALARM_MGR_LOG_PRINT(
2617 "Failed to add callback for time external changing event\n");
2624 static DBusHandlerResult __alarm_server_filter(DBusConnection *connection,
2625 DBusMessage *message,
2629 if (dbus_message_is_signal
2630 (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
2635 __expired_alarm_t *expire_info;
2637 dbus_message_get_args(message,
2639 DBUS_TYPE_STRING, &service,
2640 DBUS_TYPE_STRING, &old_owner,
2641 DBUS_TYPE_STRING, &new_owner,
2644 for (entry = g_expired_alarm_list; entry; entry = entry->next) {
2646 expire_info = (__expired_alarm_t *) entry->data;
2648 ALARM_MGR_LOG_PRINT("service_name(%s), service(%s)", expire_info->service_name, service);
2650 if (strcmp(expire_info->service_name, service)
2652 ALARM_MGR_EXCEPTION_PRINT(
2653 "__alarm_server_filter : "
2654 "service name(%s) alarm_id (%d)\n",
2655 expire_info->service_name,\
2656 expire_info->alarm_id);
2658 __alarm_send_noti_to_application(
2659 expire_info->service_name,
2660 expire_info->alarm_id);
2661 g_expired_alarm_list =
2662 g_slist_remove(g_expired_alarm_list,
2669 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2672 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2675 static bool __initialize_dbus()
2677 GError *error = NULL;
2678 GObject *obj = NULL;
2679 DBusGConnection *connection = NULL;
2681 int request_name_result = 0;
2683 dbus_g_object_type_install_info(ALARM_MANAGER_TYPE_OBJECT,
2684 &dbus_glib_alarm_manager_object_info);
2686 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
2689 ALARM_MGR_EXCEPTION_PRINT("dbus_g_bus_get failed\n");
2693 dbus_error_init(&derror);
2695 request_name_result =
2696 dbus_bus_request_name(dbus_g_connection_get_connection(connection),
2697 "org.tizen.alarm.manager",
2698 DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &derror);
2699 if (dbus_error_is_set(&derror)) { /* failure */
2700 ALARM_MGR_EXCEPTION_PRINT("Failed to dbus_bus_request_name "
2701 "(org.tizen.alarm.manager): %s\n", derror.message);
2702 dbus_error_free(&derror);
2706 if (!(obj = g_object_new(ALARM_MANAGER_TYPE_OBJECT, NULL))) {
2707 ALARM_MGR_EXCEPTION_PRINT("Could not allocate new object\n");
2711 dbus_g_connection_register_g_object(connection,
2712 "/org/tizen/alarm/manager",
2715 /*dbus_bus_add_match (dbus_g_connection_get_connection(connection),
2716 "type='signal',member='NameOwnerChanged'",NULL); */
2718 dbus_bus_add_match(dbus_g_connection_get_connection(connection),
2719 "type='signal',sender='" DBUS_SERVICE_DBUS
2720 "',path='" DBUS_PATH_DBUS
2721 "',interface='" DBUS_INTERFACE_DBUS
2722 "',member='NameOwnerChanged'", NULL);
2724 if (!dbus_connection_add_filter
2725 (dbus_g_connection_get_connection(connection),
2726 __alarm_server_filter, NULL, NULL)) {
2727 ALARM_MGR_EXCEPTION_PRINT("add __expire_alarm_filter failed\n");
2732 alarm_context.bus = connection;
2736 #define ALARMMGR_DB_FILE "/opt/dbspace/.alarmmgr.db"
2737 sqlite3 *alarmmgr_db;
2738 #define QUERY_CREATE_TABLE_ALARMMGR "create table alarmmgr \
2739 (alarm_id integer primary key,\
2743 app_unique_name text,\
2744 app_service_name text,\
2745 app_service_name_mod text,\
2753 day_of_week integer,\
2755 alarm_type integer,\
2756 reserved_info integer,\
2757 dst_service_name text, \
2758 dst_service_name_mod text \
2761 static bool __initialize_db()
2763 char *error_message = NULL;
2766 if (access("/opt/dbspace/.alarmmgr.db", F_OK) == 0) {
2768 db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db,
2769 DB_UTIL_REGISTER_HOOK_METHOD);
2771 if (ret != SQLITE_OK) {
2772 ALARM_MGR_EXCEPTION_PRINT(
2773 "====>>>> connect menu_db [%s] failed!\n",
2782 db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db,
2783 DB_UTIL_REGISTER_HOOK_METHOD);
2785 if (ret != SQLITE_OK) {
2786 ALARM_MGR_EXCEPTION_PRINT(
2787 "====>>>> connect menu_db [%s] failed!\n",
2793 sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL,\
2795 ALARM_MGR_EXCEPTION_PRINT("Don't execute query = %s, "
2796 "error message = %s\n", QUERY_CREATE_TABLE_ALARMMGR,
2804 static void __initialize()
2813 fp = fopen("/proc/alarm_boot", "r");
2818 size = fread(&temp, 1, 1, fp);
2823 alarm_boot = atoi(temp);
2831 struct rtc_time rtc_tm;
2834 fd = open(power_rtc, O_RDONLY);
2836 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc0\n");
2839 retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
2841 ALARM_MGR_EXCEPTION_PRINT("RTC_RD_TIME ioctl failed");
2847 fd2 = open(default_rtc, O_RDWR);
2849 retval = ioctl(fd2, RTC_SET_TIME, &rtc_tm);
2851 ALARM_MGR_EXCEPTION_PRINT("RTC_SET_TIME ioctl failed");
2856 // ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc1\n");
2859 fd = open(power_rtc, O_RDWR);
2861 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc0\n");
2864 retval = ioctl(fd, RTC_SET_TIME, &rtc_tm);
2866 ALARM_MGR_EXCEPTION_PRINT("RTC_SET_TIME ioctl failed");
2873 __initialize_timer();
2874 if (__initialize_dbus() == false) { /* because dbus's initialize
2875 failed, we cannot continue any more. */
2876 ALARM_MGR_EXCEPTION_PRINT("because __initialize_dbus failed, "
2877 "alarm-server cannot be runned.\n");
2880 __initialize_scheduled_alarm_list();
2882 __initialize_alarm_list();
2883 __initialize_noti();
2888 static bool __check_false_alarm()
2890 time_t current_time;
2893 _alarm_time(¤t_time);
2895 interval = ab_due_time - current_time;
2897 ALARM_MGR_LOG_PRINT("due_time : %d / current_time %d\n", \
2898 alarm_context.c_due_time, current_time);
2900 if (interval > 0 && interval <= 30) {
2902 } else if (!poweron_alarm_expired) {
2903 /* originally, first arguement's value was 121(app_id) which means
2904 * alarm_booting ui application.and this application's dbus-service
2905 * name had a org.tizen.alarmboot.ui in a platform with app-server.
2906 * so we set "org.tizen.alarmboot.ui.ALARM" instead of 121.
2908 __alarm_send_noti_to_application(
2909 WAKEUP_ALARMBOOTING_APP_ID, -1);
2919 GMainLoop *mainloop = NULL;
2921 ALARM_MGR_LOG_PRINT("Enter main loop\n");
2923 mainloop = g_main_loop_new(NULL, FALSE);
2929 __check_false_alarm();
2933 g_main_loop_run(mainloop);