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.
23 #define _BSD_SOURCE /*localtime_r requires */
34 #include"alarm-internal.h"
35 #define WAKEUP_ALARM_APP_ID "com.samsung.alarm.ALARM" /*alarm ui
36 application's alarm's dbus_service name instead of 21 value */
37 #define DST_TIME_DIFF 1
39 extern __alarm_server_context_t alarm_context;
40 extern GSList *g_scheduled_alarm_list;
42 static time_t __alarm_next_duetime_once(__alarm_info_t *__alarm_info);
43 static time_t __alarm_next_duetime_repeat(__alarm_info_t *__alarm_info);
44 static time_t __alarm_next_duetime_annually(__alarm_info_t *__alarm_info);
45 static time_t __alarm_next_duetime_monthly(__alarm_info_t *__alarm_info);
46 static time_t __alarm_next_duetime_weekly(__alarm_info_t *__alarm_info);
47 static bool __find_next_alarm_to_be_scheduled(time_t *min_due_time);
48 bool _alarm_schedule(void);
50 bool _clear_scheduled_alarm_list()
52 g_slist_free_full(g_scheduled_alarm_list, g_free);
53 g_scheduled_alarm_list = NULL;
58 bool _init_scheduled_alarm_list()
60 _clear_scheduled_alarm_list();
65 bool _add_to_scheduled_alarm_list(__alarm_info_t *__alarm_info)
68 * 20080328. Sewook Park(sewook7.park@samsung.com)
69 * When multiple alarms are expired at same time, dbus rpc call for alarm
70 * ui should be invoked first.(Ui conflicting manager cannot manage the
71 * different kinds of alarm popups(wake up alarm/org alarm) correctly,
72 * when they are displayed at same time)So when arranging the schedule
73 * alarm list, wake up alarm element is located ahead.
79 __scheduled_alarm_t *alarm = NULL;
80 __scheduled_alarm_t *entry = NULL;
82 alarm = g_malloc(sizeof(__scheduled_alarm_t));
88 alarm->alarm_id = __alarm_info->alarm_id;
89 alarm->pid = __alarm_info->pid;
90 alarm->__alarm_info = __alarm_info;
92 SECURE_LOGD("%s :alarm->pid =%d, app_service_name=%s(%u)\n",
93 __FUNCTION__, alarm->pid,
94 g_quark_to_string(alarm->__alarm_info->quark_app_service_name),
95 alarm->__alarm_info->quark_app_service_name);
97 if (alarm->__alarm_info->quark_app_service_name != g_quark_from_string(WAKEUP_ALARM_APP_ID)) {
98 g_scheduled_alarm_list = g_slist_append(g_scheduled_alarm_list, alarm);
101 for (iter = g_scheduled_alarm_list; iter != NULL; iter = g_slist_next(iter)) {
104 if (entry->__alarm_info->quark_app_service_name != g_quark_from_string(WAKEUP_ALARM_APP_ID)) {
111 g_scheduled_alarm_list = g_slist_append(g_scheduled_alarm_list, alarm);
112 ALARM_MGR_LOG_PRINT("appended : prior is %d\tcount is %d\n", prior, count);
115 g_scheduled_alarm_list = g_slist_insert(g_scheduled_alarm_list, alarm, count - 1);
116 ALARM_MGR_LOG_PRINT("appended : prior is %d\tcount is %d\n", prior, count);
123 bool _remove_from_scheduled_alarm_list(int pid, alarm_id_t alarm_id)
127 __scheduled_alarm_t *alarm = NULL;
129 for (iter = g_scheduled_alarm_list; iter != NULL; iter = g_slist_next(iter)) {
131 if (alarm->alarm_id == alarm_id) {
132 g_scheduled_alarm_list = g_slist_remove(g_scheduled_alarm_list, iter->data);
139 if (g_slist_length(g_scheduled_alarm_list) == 0) {
140 alarm_context.c_due_time = -1;
146 static time_t __alarm_next_duetime_once(__alarm_info_t *__alarm_info)
149 time_t due_time_tmp = 0;
150 time_t current_time = 0;
151 struct tm duetime_tm;
155 alarm_info_t *alarm_info = &__alarm_info->alarm_info;
156 alarm_date_t *start = &alarm_info->start;
160 localtime_r(¤t_time, &duetime_tm);
161 duetime_tm.tm_hour = start->hour;
162 duetime_tm.tm_min = start->min;
163 duetime_tm.tm_sec = start->sec;
165 current_dst = duetime_tm.tm_isdst;
166 duetime_tm.tm_isdst = -1;
168 if (start->year == 0 && start->month == 0 && start->day == 0)
170 due_time = mktime(&duetime_tm);
171 if (!(due_time > current_time)) {
172 due_time = due_time + 60 * 60 * 24;
174 } else /*specific date*/ {
175 duetime_tm.tm_year = start->year - 1900;
176 duetime_tm.tm_mon = start->month - 1;
177 duetime_tm.tm_mday = start->day;
178 due_time = mktime(&duetime_tm);
181 if (due_time <= current_time) {
182 ALARM_MGR_EXCEPTION_PRINT("duetime is less than or equal to current time. current_dst = %d", current_dst);
183 duetime_tm.tm_isdst = 0; // DST off
185 due_time_tmp = mktime(&duetime_tm);
186 localtime_r(&due_time_tmp, &tmp_tm);
188 ALARM_MGR_LOG_PRINT("%d:%d:%d. duetime = %d", tmp_tm.tm_hour, tmp_tm.tm_min, tmp_tm.tm_sec, due_time);
189 if (tmp_tm.tm_hour == start->hour && tmp_tm.tm_min == start->min && tmp_tm.tm_sec == start->sec ) {
190 due_time = due_time_tmp;
191 ALARM_MGR_EXCEPTION_PRINT("due_time = %d",due_time);
195 localtime_r(&due_time, &tmp_tm);
196 ALARM_MGR_LOG_PRINT("%d:%d:%d. current_dst = %d, duetime_dst = %d", tmp_tm.tm_hour, tmp_tm.tm_min, tmp_tm.tm_sec, current_dst, tmp_tm.tm_isdst);
198 if (current_dst == 1 && tmp_tm.tm_isdst == 1 && tmp_tm.tm_hour == start->hour + 1) {
199 // When the calculated duetime is forwarded 1hour due to DST, Adds 23hours.
200 due_time += 60 * 60 * 23;
201 localtime_r(&due_time, &duetime_tm);
202 ALARM_MGR_EXCEPTION_PRINT("due_time = %d",due_time);
206 ALARM_MGR_EXCEPTION_PRINT("Final due_time = %d, %s",due_time, ctime(&due_time));
210 static time_t __alarm_next_duetime_repeat(__alarm_info_t *__alarm_info)
213 time_t current_time = 0;
214 struct tm duetime_tm;
216 alarm_info_t *alarm_info = &__alarm_info->alarm_info;
217 alarm_date_t *start = &alarm_info->start;
220 /*localtime_r(¤t_time, &duetime_tm); */
222 duetime_tm.tm_hour = start->hour;
223 duetime_tm.tm_min = start->min;
224 duetime_tm.tm_sec = start->sec;
226 duetime_tm.tm_year = start->year - 1900;
227 duetime_tm.tm_mon = start->month - 1;
228 duetime_tm.tm_mday = start->day;
229 duetime_tm.tm_isdst = -1;
231 due_time = mktime(&duetime_tm);
233 while (__alarm_info->start > due_time || current_time >= due_time) {
234 due_time += alarm_info->mode.u_interval.interval;
236 /* due_time = mktime(&duetime_tm); */
238 localtime_r(&due_time, &duetime_tm);
240 start->year = duetime_tm.tm_year + 1900;
241 start->month = duetime_tm.tm_mon + 1;
242 start->day = duetime_tm.tm_mday;
243 start->hour = duetime_tm.tm_hour;
244 start->min = duetime_tm.tm_min;
245 start->sec = duetime_tm.tm_sec;
251 static time_t __alarm_next_duetime_annually(__alarm_info_t *__alarm_info)
254 time_t current_time = 0;
255 struct tm duetime_tm;
257 alarm_info_t *alarm_info = &__alarm_info->alarm_info;
258 alarm_date_t *start = &alarm_info->start;
261 localtime_r(¤t_time, &duetime_tm);
262 duetime_tm.tm_hour = start->hour;
263 duetime_tm.tm_min = start->min;
264 duetime_tm.tm_sec = start->sec;
266 if (start->year != 0) {
267 duetime_tm.tm_year = start->year - 1900;
270 duetime_tm.tm_mon = start->month - 1;
271 duetime_tm.tm_mday = start->day;
273 due_time = mktime(&duetime_tm);
275 while (__alarm_info->start > due_time || current_time > due_time) {
276 duetime_tm.tm_year += 1;
277 due_time = mktime(&duetime_tm);
284 static time_t __alarm_next_duetime_monthly(__alarm_info_t *__alarm_info)
287 time_t current_time = 0;
288 struct tm duetime_tm;
290 alarm_info_t *alarm_info = &__alarm_info->alarm_info;
291 alarm_date_t *start = &alarm_info->start;
294 localtime_r(¤t_time, &duetime_tm);
295 duetime_tm.tm_hour = start->hour;
296 duetime_tm.tm_min = start->min;
297 duetime_tm.tm_sec = start->sec;
299 if (start->year != 0) {
300 duetime_tm.tm_year = start->year - 1900;
303 if (start->month != 0) {
305 duetime_tm.tm_mon = start->month - 1;
308 duetime_tm.tm_mday = start->day;
310 due_time = mktime(&duetime_tm);
312 while (__alarm_info->start > due_time || current_time > due_time) {
313 duetime_tm.tm_mon += 1;
314 if (duetime_tm.tm_mon == 12) {
315 duetime_tm.tm_mon = 0;
316 duetime_tm.tm_year += 1;
318 due_time = mktime(&duetime_tm);
325 static time_t __alarm_next_duetime_weekly(__alarm_info_t *__alarm_info)
328 time_t current_time = 0;
329 struct tm duetime_tm;
336 alarm_info_t *alarm_info = &__alarm_info->alarm_info;
337 alarm_date_t *start = &alarm_info->start;
338 alarm_mode_t *mode = &alarm_info->mode;
342 localtime_r(¤t_time, &duetime_tm);
343 wday = duetime_tm.tm_wday;
344 duetime_tm.tm_hour = start->hour;
345 duetime_tm.tm_min = start->min;
346 duetime_tm.tm_sec = start->sec;
347 current_dst = duetime_tm.tm_isdst;
349 duetime_tm.tm_isdst = -1;
351 if (__alarm_info->start != 0) {
352 if (__alarm_info->start >= current_time) {
353 duetime_tm.tm_year = start->year - 1900;
354 duetime_tm.tm_mon = start->month - 1;
355 duetime_tm.tm_mday = start->day;
359 due_time = mktime(&duetime_tm);
360 localtime_r(&due_time, &tmp_tm);
361 ALARM_MGR_EXCEPTION_PRINT("%d:%d:%d. duetime = %d, isdst = %d", tmp_tm.tm_hour, tmp_tm.tm_min, tmp_tm.tm_sec, due_time, tmp_tm.tm_isdst);
363 if (due_time <= current_time) {
364 ALARM_MGR_EXCEPTION_PRINT("duetime is less than or equal to current time. current_dst = %d", current_dst);
365 duetime_tm.tm_isdst = 0;
367 due_time = mktime(&duetime_tm);
368 localtime_r(&due_time, &tmp_tm);
370 SECURE_LOGD("%d:%d:%d. duetime = %d", tmp_tm.tm_hour, tmp_tm.tm_min, tmp_tm.tm_sec, due_time);
371 if (tmp_tm.tm_hour != start->hour || tmp_tm.tm_min != start->min || tmp_tm.tm_sec != start->sec ) {
372 duetime_tm.tm_hour = start->hour;
373 duetime_tm.tm_min = start->min;
374 duetime_tm.tm_sec = start->sec;
375 duetime_tm.tm_isdst = -1;
376 due_time = mktime(&duetime_tm);
377 ALARM_MGR_EXCEPTION_PRINT("due_time = %d",due_time);
381 if (current_dst == 1 && tmp_tm.tm_isdst == 1 && tmp_tm.tm_hour == start->hour + 1) {
382 // When the calculated duetime is forwarded 1hour due to DST, Adds 23hours.
383 due_time += 60 * 60 * 23;
384 localtime_r(&due_time, &duetime_tm);
385 ALARM_MGR_EXCEPTION_PRINT("due_time = %d",due_time);
389 // Gets the dst before calculating the duedate as interval
390 localtime_r(&due_time, &before_tm);
391 SECURE_LOGD("before_dst = %d", before_tm.tm_isdst);
393 wday = duetime_tm.tm_wday;
395 ALARM_MGR_EXCEPTION_PRINT("current_time(%d) due_time(%d)", current_time, due_time);
397 /* CQ defect(72810) : only one time alarm function is not working
398 under all recurrence_disabled. */
399 if (due_time > current_time && mode->u_interval.day_of_week == 0) {
403 if (current_time >= due_time || !(mode->u_interval.day_of_week & 1 << wday)) {
412 while (!(mode->u_interval.day_of_week & 1 << day) && interval < 8) {
421 ALARM_MGR_LOG_PRINT("interval : %d\n", interval);
422 due_time += 60 * 60 * 24 * interval;
425 // Gets the dst after calculating the duedate as interval
426 localtime_r(&due_time, &after_tm);
427 SECURE_LOGD("after_dst = %d", after_tm.tm_isdst);
429 // Revise the duetime as difference in tm_isdst
430 if (before_tm.tm_isdst == 1 && after_tm.tm_isdst == 0) {
431 due_time += 60 * 60; // Add an hour
432 } else if (before_tm.tm_isdst == 0 && after_tm.tm_isdst == 1) {
433 due_time -= 60 * 60; // Subtract an hour
436 ALARM_MGR_EXCEPTION_PRINT("Final due_time = %d", due_time);
440 time_t _alarm_next_duetime(__alarm_info_t *__alarm_info)
443 time_t current_time = 0;
445 struct tm *cur_tm = NULL ;
446 struct tm *due_tm = NULL ;
448 alarm_info_t *alarm_info = &__alarm_info->alarm_info;
449 alarm_mode_t *mode = &alarm_info->mode;
452 cur_tm = localtime(¤t_time);
453 if (cur_tm->tm_isdst > 0)
456 ALARM_MGR_LOG_PRINT("mode->repeat is %d\n", mode->repeat);
458 if (mode->repeat == ALARM_REPEAT_MODE_ONCE) {
459 due_time = __alarm_next_duetime_once(__alarm_info);
460 } else if (mode->repeat == ALARM_REPEAT_MODE_REPEAT) {
461 due_time = __alarm_next_duetime_repeat(__alarm_info);
462 } else if (mode->repeat == ALARM_REPEAT_MODE_ANNUALLY) {
463 due_time = __alarm_next_duetime_annually(__alarm_info);
464 } else if (mode->repeat == ALARM_REPEAT_MODE_MONTHLY) {
465 due_time = __alarm_next_duetime_monthly(__alarm_info);
466 } else if (mode->repeat == ALARM_REPEAT_MODE_WEEKLY) {
467 due_time = __alarm_next_duetime_weekly(__alarm_info);
469 ALARM_MGR_EXCEPTION_PRINT("repeat mode(%d) is wrong\n",
474 if (mode->repeat != ALARM_REPEAT_MODE_WEEKLY && mode->repeat != ALARM_REPEAT_MODE_ONCE) {
475 due_tm = localtime(&due_time);
476 if (is_dst==0 && due_tm->tm_isdst==1){
477 ALARM_MGR_LOG_PRINT("DST alarm found, enable\n");
478 due_tm->tm_hour = due_tm->tm_hour - DST_TIME_DIFF;
479 } else if (is_dst==1 && due_tm->tm_isdst==0){
480 ALARM_MGR_LOG_PRINT("DST alarm found. disable\n");
481 due_tm->tm_hour = due_tm->tm_hour + DST_TIME_DIFF;
483 due_time = mktime(due_tm);
486 ALARM_MGR_EXCEPTION_PRINT("alarm_id: %d, next duetime: %d", __alarm_info->alarm_id, due_time);
488 if (__alarm_info->end != 0 && __alarm_info->end < due_time) {
489 ALARM_MGR_LOG_PRINT("due time > end time");
490 __alarm_info->due_time = 0;
493 __alarm_info->due_time = due_time;
498 static bool __find_next_alarm_to_be_scheduled(time_t *min_due_time)
501 time_t min_time = -1;
504 __alarm_info_t *entry = NULL;
508 for (iter = alarm_context.alarms; iter != NULL;
509 iter = g_slist_next(iter)) {
511 due_time = entry->due_time;
515 SECURE_LOGD("alarm[%d] with duetime(%u) at current(%u) pid: (%d)\n",
516 entry->alarm_id, due_time, current_time, entry->pid);
517 if (due_time == 0) /*0 means this alarm has been disabled*/ {
521 interval = difftime(due_time, current_time);
523 if (interval <= 0) /*2008.08.06 when the alarm expires, it may makes an error.*/ {
524 ALARM_MGR_EXCEPTION_PRINT("The duetime of alarm(%d) is OVER.", entry->alarm_id);
528 interval = difftime(due_time, min_time);
530 if ((interval < 0) || min_time == -1) {
536 *min_due_time = min_time;
540 bool _alarm_schedule()
545 __alarm_info_t *entry = NULL;
547 __find_next_alarm_to_be_scheduled(&min_time);
549 if (min_time == -1) {
550 ALARM_MGR_LOG_PRINT("[alarm-server][schedule]: There is no alarm to be scheduled.");
552 for (iter = alarm_context.alarms; iter != NULL; iter = g_slist_next(iter)) {
554 due_time = entry->due_time;
556 if (due_time == min_time) {
557 _add_to_scheduled_alarm_list(entry);
560 _alarm_set_timer(&alarm_context, alarm_context.timer, min_time);