/*
- * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <string.h>
#include <sys/types.h>
#include <errno.h>
+#include <stdint.h>
#include <sys/timerfd.h>
#include <glib.h>
#include "alarm.h"
#include "alarm-internal.h"
+#include "alarm-manager-dbus.h"
-extern bool g_dummy_timer_is_set;
+extern __alarm_server_context_t alarm_context;
-void _alarm_disable_timer(__alarm_server_context_t alarm_context)
+bool g_dummy_timer_is_set = false;
+
+static gboolean __alarm_handler_idle(gpointer user_data)
+{
+ GPollFD *gpollfd = (GPollFD *) user_data;
+ uint64_t exp;
+ time_t current_time;
+
+#ifdef TIZEN_TEST_GCOV
+ void __gcov_flush(void);
+ __gcov_flush();
+#endif
+
+ if (gpollfd == NULL) {
+ ALARM_MGR_EXCEPTION_PRINT("gpollfd is NULL");
+ return false;
+ }
+
+ if (read(gpollfd->fd, &exp, sizeof(uint64_t)) < 0) {
+ ALARM_MGR_EXCEPTION_PRINT("Reading the fd is failed.");
+ return false;
+ }
+
+ ALARM_MGR_LOG_PRINT("Lock the display not to enter LCD OFF");
+ if (_display_lock_state(DEVICED_LCD_OFF, DEVICED_STAY_CUR_STATE, 0) != ALARMMGR_RESULT_SUCCESS)
+ ALARM_MGR_EXCEPTION_PRINT("_display_lock_state() is failed");
+
+ if (g_dummy_timer_is_set == true) {
+ ALARM_MGR_LOG_PRINT("dummy alarm timer has expired.");
+ } else {
+ ALARM_MGR_LOG_PRINT("__alarm_handler_idle");
+ _alarm_expired();
+ }
+
+ _alarm_schedule();
+
+ /*
+ * Previous alarm can be expired late as tolerance of RTC.
+ * In this case, Expire alarms forcibly if real duetime is same to current time.
+ */
+ time(¤t_time);
+ if (alarm_context.c_due_time == current_time) {
+ ALARM_MGR_LOG_PRINT("Expire alarms forcibly when duetime is same to current time(%ld).", current_time);
+ _alarm_expired();
+ _alarm_schedule();
+ }
+
+ _rtc_set();
+
+ ALARM_MGR_LOG_PRINT("Unlock the display from LCD OFF");
+ if (_display_unlock_state(DEVICED_LCD_OFF, DEVICED_SLEEP_MARGIN) != ALARMMGR_RESULT_SUCCESS)
+ ALARM_MGR_EXCEPTION_PRINT("_display_unlock_state() is failed");
+
+ return false;
+}
+
+
+static void __timer_glib_finalize(GSource *src)
+{
+ GSList *fd_list;
+ GPollFD *tmp;
+
+ fd_list = src->poll_fds;
+ do {
+ tmp = (GPollFD *) fd_list->data;
+ g_free(tmp);
+
+ fd_list = fd_list->next;
+ } while (fd_list);
+
+ return;
+}
+
+static gboolean __timer_glib_check(GSource *src)
+{
+ GSList *fd_list;
+ GPollFD *tmp;
+
+ fd_list = src->poll_fds;
+ do {
+ tmp = (GPollFD *) fd_list->data;
+ if (tmp->revents & (G_IO_IN | G_IO_PRI))
+ return TRUE;
+
+ fd_list = fd_list->next;
+ } while (fd_list);
+
+ return FALSE;
+}
+
+static gboolean __timer_glib_dispatch(GSource *src, GSourceFunc callback,
+ gpointer data)
+{
+ callback(data);
+ return TRUE;
+}
+
+static gboolean __timer_glib_prepare(GSource *src, gint *timeout)
+{
+ return FALSE;
+}
+
+GSourceFuncs funcs = {
+ .prepare = __timer_glib_prepare,
+ .check = __timer_glib_check,
+ .dispatch = __timer_glib_dispatch,
+ .finalize = __timer_glib_finalize
+};
+
+int _initialize_timer()
+{
+ int fd;
+ GSource *src;
+ GPollFD *gpollfd;
+ int ret;
+
+ fd = timerfd_create(CLOCK_REALTIME, 0);
+ if (fd == -1) {
+ ALARM_MGR_EXCEPTION_PRINT("timerfd_create() is failed.\n");
+ return -1;
+ }
+ src = g_source_new(&funcs, sizeof(GSource));
+
+ gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
+ if (gpollfd == NULL) {
+ ALARM_MGR_EXCEPTION_PRINT("Out of memory\n");
+ return -1;
+ }
+ gpollfd->events = G_IO_IN;
+ gpollfd->fd = fd;
+
+ g_source_add_poll(src, gpollfd);
+ g_source_set_callback(src, (GSourceFunc) __alarm_handler_idle,
+ (gpointer) gpollfd, NULL);
+ g_source_set_priority(src, G_PRIORITY_HIGH);
+
+ ret = g_source_attach(src, NULL);
+ if (ret == 0) {
+ ALARM_MGR_EXCEPTION_PRINT("g_source_attach() is failed.\n");
+ return -1;
+ }
+
+ g_source_unref(src);
+
+ return fd;
+}
+
+void _alarm_disable_timer()
{
struct itimerspec time_spec;
ALARM_MGR_EXCEPTION_PRINT("timerfd_settime has failed : errno(%d).", errno);
}
-void _alarm_set_timer(__alarm_server_context_t *alarm_context, int timer, time_t due_time)
+void _alarm_set_timer(int timer, time_t due_time)
{
struct itimerspec time_spec;
time_t current_time;
double interval;
char due_time_r[100] = { 0 };
struct tm ts_ret;
- extern int errno;
time(¤t_time);
/* we set c_due_time to due_time due to allow newly created alarm can
be schedlued when its interval is less than the current alarm */
- alarm_context->c_due_time = due_time;
+ alarm_context.c_due_time = due_time;
}
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
-#include <sys/timerfd.h>
-#include <poll.h>
-#include <stdint.h>
#include <errno.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
static int gfd = -1;
__alarm_server_context_t alarm_context;
-bool g_dummy_timer_is_set = FALSE;
+
+extern bool g_dummy_timer_is_set;
GSList *g_scheduled_alarm_list;
GSList *g_expired_alarm_list;
static bool __alarm_delete(uid_t uid, alarm_id_t alarm_id, int *error_code);
static bool __alarm_update(uid_t uid, alarm_id_t alarm_id,
alarm_info_t *alarm_info, int update_flag, int *error_code);
-static void __alarm_expired();
-static gboolean __alarm_handler_idle(gpointer user_data);
static void __on_system_time_external_changed(keynode_t *node, void *data);
-static void __initialize_timer();
static void __initialize_alarm_list();
static void __initialize_scheduled_alarm_list();
static void __initialize_noti();
}
}
-static void __rtc_set()
+void _rtc_set()
{
if (_APPFW_FEATURE_WAKEUP_USING_RTC) {
const char *rtc = default_rtc;
if (alarm_context.c_due_time == -1 || __alarm_info->due_time < alarm_context.c_due_time) {
_clear_scheduled_alarm_list();
_add_to_scheduled_alarm_list(__alarm_info);
- _alarm_set_timer(&alarm_context, alarm_context.timer, __alarm_info->due_time);
- __rtc_set();
+ _alarm_set_timer(alarm_context.timer, __alarm_info->due_time);
+ _rtc_set();
} else if (__alarm_info->due_time == alarm_context.c_due_time) {
_add_to_scheduled_alarm_list(__alarm_info);
}
if (alarm_context.c_due_time == -1 || __alarm_info->due_time < alarm_context.c_due_time) {
_clear_scheduled_alarm_list();
_add_to_scheduled_alarm_list(__alarm_info);
- _alarm_set_timer(&alarm_context, alarm_context.timer, __alarm_info->due_time);
- __rtc_set();
+ _alarm_set_timer(alarm_context.timer, __alarm_info->due_time);
+ _rtc_set();
} else if (__alarm_info->due_time == alarm_context.c_due_time) {
_add_to_scheduled_alarm_list(__alarm_info);
}
if (alarm_context.c_due_time == -1 || __alarm_info->due_time < alarm_context.c_due_time) {
_clear_scheduled_alarm_list();
_add_to_scheduled_alarm_list(__alarm_info);
- _alarm_set_timer(&alarm_context, alarm_context.timer, __alarm_info->due_time);
- __rtc_set();
+ _alarm_set_timer(alarm_context.timer, __alarm_info->due_time);
+ _rtc_set();
} else if (__alarm_info->due_time == alarm_context.c_due_time) {
_add_to_scheduled_alarm_list(__alarm_info);
}
result = _remove_from_scheduled_alarm_list(uid, alarm_id);
if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
/*there is no scheduled alarm */
- _alarm_disable_timer(alarm_context);
+ _alarm_disable_timer();
_alarm_schedule();
ALARM_MGR_LOG_PRINT("[alarm-server]:Update alarm: alarm(%d).", alarm_id);
- __rtc_set();
+ _rtc_set();
if (__alarm_info->due_time == 0)
ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: due_time is 0.");
if (alarm_context.c_due_time == -1 || __alarm_info->due_time < alarm_context.c_due_time) {
_clear_scheduled_alarm_list();
_add_to_scheduled_alarm_list(__alarm_info);
- _alarm_set_timer(&alarm_context, alarm_context.timer, __alarm_info->due_time);
+ _alarm_set_timer(alarm_context.timer, __alarm_info->due_time);
ALARM_MGR_LOG_PRINT("[alarm-server1]:alarm_context.c_due_time "
"(%ld), due_time(%ld)", alarm_context.c_due_time, __alarm_info->due_time);
} else if (__alarm_info->due_time == alarm_context.c_due_time) {
"(%ld), due_time(%ld)", alarm_context.c_due_time, __alarm_info->due_time);
}
- __rtc_set();
+ _rtc_set();
return true;
}
}
if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
- _alarm_disable_timer(alarm_context);
+ _alarm_disable_timer();
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
}
return true;
return 0;
}
-static void __alarm_expired()
+void _alarm_expired()
{
int ret;
bool is_app = false;
ALARM_MGR_LOG_PRINT("[alarm-server]: Leave");
}
-static gboolean __alarm_handler_idle(gpointer user_data)
-{
- GPollFD *gpollfd = (GPollFD *) user_data;
- uint64_t exp;
- time_t current_time;
-
-#ifdef TIZEN_TEST_GCOV
- void __gcov_flush(void);
- __gcov_flush();
-#endif
-
- if (gpollfd == NULL) {
- ALARM_MGR_EXCEPTION_PRINT("gpollfd is NULL");
- return false;
- }
-
- if (read(gpollfd->fd, &exp, sizeof(uint64_t)) < 0) {
- ALARM_MGR_EXCEPTION_PRINT("Reading the fd is failed.");
- return false;
- }
-
- ALARM_MGR_LOG_PRINT("Lock the display not to enter LCD OFF");
- if (_display_lock_state(DEVICED_LCD_OFF, DEVICED_STAY_CUR_STATE, 0) != ALARMMGR_RESULT_SUCCESS)
- ALARM_MGR_EXCEPTION_PRINT("_display_lock_state() is failed");
-
- if (g_dummy_timer_is_set == true) {
- ALARM_MGR_LOG_PRINT("dummy alarm timer has expired.");
- } else {
- ALARM_MGR_LOG_PRINT("__alarm_handler_idle");
- __alarm_expired();
- }
-
- _alarm_schedule();
-
- /*
- * Previous alarm can be expired late as tolerance of RTC.
- * In this case, Expire alarms forcibly if real duetime is same to current time.
- */
- time(¤t_time);
- if (alarm_context.c_due_time == current_time) {
- ALARM_MGR_LOG_PRINT("Expire alarms forcibly when duetime is same to current time(%ld).", current_time);
- __alarm_expired();
- _alarm_schedule();
- }
-
- __rtc_set();
-
- ALARM_MGR_LOG_PRINT("Unlock the display from LCD OFF");
- if (_display_unlock_state(DEVICED_LCD_OFF, DEVICED_SLEEP_MARGIN) != ALARMMGR_RESULT_SUCCESS)
- ALARM_MGR_EXCEPTION_PRINT("_display_unlock_state() is failed");
-
- return false;
-}
static void __on_system_time_external_changed(keynode_t *node, void *data)
{
double diff_time = 0.0;
time_t cur_time = 0;
- _alarm_disable_timer(alarm_context);
+ _alarm_disable_timer();
if (node) {
diff_time = vconf_keynode_get_dbl(node);
ALARM_MGR_EXCEPTION_PRINT("Failed to change both OS time and RTC");
_clear_scheduled_alarm_list();
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
return;
}
alarm_context.c_due_time);
_clear_scheduled_alarm_list();
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
return;
}
}
if (is_restored) {
- _alarm_disable_timer(alarm_context);
+ _alarm_disable_timer();
_clear_scheduled_alarm_list();
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
}
}
_alarm_disable_timer(alarm_context);
_clear_scheduled_alarm_list();
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
}
}
}
if (is_deleted && (g_slist_length(g_scheduled_alarm_list) == 0)) {
- _alarm_disable_timer(alarm_context);
+ _alarm_disable_timer();
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
}
}
ALARM_MGR_LOG_PRINT("dummy alarm timer has expired.");
} else {
ALARM_MGR_LOG_PRINT("due_time=%ld is expired.", alarm_info->due_time);
- __alarm_expired();
+ _alarm_expired();
}
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
if (_display_unlock_state(DEVICED_LCD_OFF, DEVICED_SLEEP_MARGIN) != ALARMMGR_RESULT_SUCCESS)
ALARM_MGR_EXCEPTION_PRINT("_display_unlock_state() is failed");
_clear_scheduled_alarm_list();
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
#ifdef _APPFW_FEATURE_ALARM_MANAGER_MODULE_LOG
char *timebuf = ctime((const time_t *)&new_time);
g_variant_get(parameters, "(i)", &time_sec);
- _alarm_disable_timer(alarm_context); /* Disable the timer to reschedule the alarm before the time is changed. */
+ _alarm_disable_timer(); /* Disable the timer to reschedule the alarm before the time is changed. */
tzset();
gettimeofday(&cur_time, NULL);
ALARM_MGR_EXCEPTION_PRINT("Failed to change both OS time and RTC");
_clear_scheduled_alarm_list();
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
return ERR_ALARM_SYSTEM_FAIL;
}
g_variant_get(parameters, "(uuuu)", &new_sec, &new_nsec, &req_sec, &req_nsec);
- _alarm_disable_timer(alarm_context); /* Disable the timer to reschedule the alarm before the time is changed. */
+ _alarm_disable_timer(); /* Disable the timer to reschedule the alarm before the time is changed. */
tzset();
clock_gettime(CLOCK_REALTIME, &cur_time);
ALARM_MGR_EXCEPTION_PRINT("Failed to change both OS time and RTC");
_clear_scheduled_alarm_list();
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
return ERR_ALARM_SYSTEM_FAIL;
}
tzset();
/* Rescheduling alarms */
- _alarm_disable_timer(alarm_context);
+ _alarm_disable_timer();
__alarm_update_due_time_of_all_items_in_list(0);
ALARM_MGR_LOG_PRINT("next expiring due_time is %ld", alarm_context.c_due_time);
_clear_scheduled_alarm_list();
_alarm_schedule();
- __rtc_set();
+ _rtc_set();
vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED, 0);
b = bundle_create();
}
if (is_deleted && (g_slist_length(g_scheduled_alarm_list) == 0)) {
- _alarm_disable_timer(alarm_context);
+ _alarm_disable_timer();
_alarm_schedule();
}
__save_module_log("DELETE ALL", log_message);
#endif
- __rtc_set();
+ _rtc_set();
return ALARMMGR_RESULT_SUCCESS;
}
return ALARMMGR_RESULT_SUCCESS;
}
-static void __timer_glib_finalize(GSource *src)
-{
- GSList *fd_list;
- GPollFD *tmp;
-
- fd_list = src->poll_fds;
- do {
- tmp = (GPollFD *) fd_list->data;
- g_free(tmp);
-
- fd_list = fd_list->next;
- } while (fd_list);
-
- return;
-}
-
-static gboolean __timer_glib_check(GSource *src)
-{
- GSList *fd_list;
- GPollFD *tmp;
-
- fd_list = src->poll_fds;
- do {
- tmp = (GPollFD *) fd_list->data;
- if (tmp->revents & (POLLIN | POLLPRI))
- return TRUE;
-
- fd_list = fd_list->next;
- } while (fd_list);
-
- return FALSE;
-}
-
-static gboolean __timer_glib_dispatch(GSource *src, GSourceFunc callback,
- gpointer data)
-{
- callback(data);
- return TRUE;
-}
-
-static gboolean __timer_glib_prepare(GSource *src, gint *timeout)
-{
- return FALSE;
-}
-
-GSourceFuncs funcs = {
- .prepare = __timer_glib_prepare,
- .check = __timer_glib_check,
- .dispatch = __timer_glib_dispatch,
- .finalize = __timer_glib_finalize
-};
-
-static void __initialize_timer()
-{
- int fd;
- GSource *src;
- GPollFD *gpollfd;
- int ret;
-
- fd = timerfd_create(CLOCK_REALTIME, 0);
- if (fd == -1) {
- ALARM_MGR_EXCEPTION_PRINT("timerfd_create() is failed.\n");
- exit(1);
- }
- src = g_source_new(&funcs, sizeof(GSource));
-
- gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
- if (gpollfd == NULL) {
- ALARM_MGR_EXCEPTION_PRINT("Out of memory\n");
- exit(1);
- }
- gpollfd->events = POLLIN;
- gpollfd->fd = fd;
-
- g_source_add_poll(src, gpollfd);
- g_source_set_callback(src, (GSourceFunc) __alarm_handler_idle,
- (gpointer) gpollfd, NULL);
- g_source_set_priority(src, G_PRIORITY_HIGH);
-
- ret = g_source_attach(src, NULL);
- if (ret == 0) {
- ALARM_MGR_EXCEPTION_PRINT("g_source_attach() is failed.\n");
- return;
- }
-
- g_source_unref(src);
-
- alarm_context.timer = fd;
-}
-
static void __initialize_alarm_list()
{
alarm_context.alarms = NULL;
_load_alarms_from_db();
- __rtc_set(); /*Set RTC1 Alarm with alarm due time for alarm-manager initialization*/
+ _rtc_set(); /*Set RTC1 Alarm with alarm due time for alarm-manager initialization*/
}
static void __initialize_scheduled_alarm_list()
vconf_get_int(VCONFKEY_ALARM_EXPIRE_MODE, &expire_mode);
ALARM_MGR_LOG_PRINT("alarm_expire_mode : %d", expire_mode);
- __initialize_timer();
+ alarm_context.timer = _initialize_timer();
+ if (alarm_context.timer == -1) {
+ ALARM_MGR_EXCEPTION_PRINT("because _initialize_timer failed, "
+ "alarm-server cannot be runned.\n");
+ exit(1);
+ }
+
if (_initialize_dbus() == false) {
/* because dbus's initialize
* failed, we cannot continue any more. */