tizen 2.3 release
[framework/appfw/alarm-manager.git] / alarm-manager-schedule.c
1 /*
2  *  alarm-manager
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Venkatesha Sarpangala <sarpangala.v@samsung.com>, Jayoun Lee <airjany@samsung.com>,
7  * Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #define _BSD_SOURCE             /*localtime_r requires */
24 #include<stdio.h>
25 #include<stdlib.h>
26 #include<time.h>
27 #include<signal.h>
28 #include<string.h>
29 #include<sys/types.h>
30
31 #include<glib.h>
32
33 #include"alarm.h"
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
38
39 extern __alarm_server_context_t alarm_context;
40 extern GSList *g_scheduled_alarm_list;
41
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);
49
50 bool _clear_scheduled_alarm_list()
51 {
52         g_slist_free_full(g_scheduled_alarm_list, g_free);
53         g_scheduled_alarm_list = NULL;
54
55         return true;
56 }
57
58 bool _init_scheduled_alarm_list()
59 {
60         _clear_scheduled_alarm_list();
61
62         return true;
63 }
64
65 bool _add_to_scheduled_alarm_list(__alarm_info_t *__alarm_info)
66 {
67 /*
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.
74  */
75
76         bool prior = false;
77         gint count = 0;
78         GSList *iter = NULL;
79         __scheduled_alarm_t *alarm = NULL;
80         __scheduled_alarm_t *entry = NULL;
81
82         alarm = g_malloc(sizeof(__scheduled_alarm_t));
83         if (alarm == NULL) {
84                 return false;
85         }
86
87         alarm->used = true;
88         alarm->alarm_id = __alarm_info->alarm_id;
89         alarm->pid = __alarm_info->pid;
90         alarm->__alarm_info = __alarm_info;
91
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);
96
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);
99         }
100         else {
101                 for (iter = g_scheduled_alarm_list; iter != NULL; iter = g_slist_next(iter)) {
102                         count++;
103                         entry = iter->data;
104                         if (entry->__alarm_info->quark_app_service_name != g_quark_from_string(WAKEUP_ALARM_APP_ID)) {
105                                 prior = true;
106                                 break;
107                         }
108                 }
109
110                 if (!prior) {
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);
113                 }
114                 else {
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);
117                 }
118         }
119
120         return true;
121 }
122
123 bool _remove_from_scheduled_alarm_list(int pid, alarm_id_t alarm_id)
124 {
125         bool result = false;
126         GSList *iter = NULL;
127         __scheduled_alarm_t *alarm = NULL;
128
129         for (iter = g_scheduled_alarm_list; iter != NULL; iter = g_slist_next(iter)) {
130                 alarm = iter->data;
131                 if (alarm->alarm_id == alarm_id) {
132                         g_scheduled_alarm_list = g_slist_remove(g_scheduled_alarm_list, iter->data);
133                         g_free(alarm);
134                         result = true;
135                         break;
136                 }
137         }
138
139         if (g_slist_length(g_scheduled_alarm_list) == 0) {
140                 alarm_context.c_due_time = -1;
141         }
142
143         return result;
144 }
145
146 static time_t __alarm_next_duetime_once(__alarm_info_t *__alarm_info)
147 {
148         time_t due_time = 0;
149         time_t due_time_tmp = 0;
150         time_t current_time = 0;
151         struct tm duetime_tm;
152         struct tm tmp_tm;
153         int current_dst = 0;
154
155         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
156         alarm_date_t *start = &alarm_info->start;
157
158         tzset();
159         time(&current_time);
160         localtime_r(&current_time, &duetime_tm);
161         duetime_tm.tm_hour = start->hour;
162         duetime_tm.tm_min = start->min;
163         duetime_tm.tm_sec = start->sec;
164
165         current_dst = duetime_tm.tm_isdst;
166         duetime_tm.tm_isdst = -1;
167
168         if (start->year == 0 && start->month == 0 && start->day == 0)
169                 /*any date */  {
170                 due_time = mktime(&duetime_tm);
171                 if (!(due_time > current_time)) {
172                         due_time = due_time + 60 * 60 * 24;
173                 }
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);
179         }
180
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
184
185                 due_time_tmp = mktime(&duetime_tm);
186                 localtime_r(&due_time_tmp, &tmp_tm);
187
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);
192                 }
193         }
194         else {
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);
197
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);
203                 }
204         }
205
206         ALARM_MGR_EXCEPTION_PRINT("Final due_time = %d, %s",due_time, ctime(&due_time));
207         return due_time;
208 }
209
210 static time_t __alarm_next_duetime_repeat(__alarm_info_t *__alarm_info)
211 {
212         time_t due_time = 0;
213         time_t current_time = 0;
214         struct tm duetime_tm;
215
216         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
217         alarm_date_t *start = &alarm_info->start;
218
219         time(&current_time);
220         /*localtime_r(&current_time, &duetime_tm); */
221
222         duetime_tm.tm_hour = start->hour;
223         duetime_tm.tm_min = start->min;
224         duetime_tm.tm_sec = start->sec;
225
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;
230
231         due_time = mktime(&duetime_tm);
232
233         while (__alarm_info->start > due_time || current_time >= due_time) {
234                 due_time += alarm_info->mode.u_interval.interval;
235
236                 /* due_time = mktime(&duetime_tm); */
237         }
238         localtime_r(&due_time, &duetime_tm);
239
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;
246
247         return due_time;
248
249 }
250
251 static time_t __alarm_next_duetime_annually(__alarm_info_t *__alarm_info)
252 {
253         time_t due_time = 0;
254         time_t current_time = 0;
255         struct tm duetime_tm;
256
257         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
258         alarm_date_t *start = &alarm_info->start;
259
260         time(&current_time);
261         localtime_r(&current_time, &duetime_tm);
262         duetime_tm.tm_hour = start->hour;
263         duetime_tm.tm_min = start->min;
264         duetime_tm.tm_sec = start->sec;
265
266         if (start->year != 0) {
267                 duetime_tm.tm_year = start->year - 1900;
268         }
269
270         duetime_tm.tm_mon = start->month - 1;
271         duetime_tm.tm_mday = start->day;
272
273         due_time = mktime(&duetime_tm);
274
275         while (__alarm_info->start > due_time || current_time > due_time) {
276                 duetime_tm.tm_year += 1;
277                 due_time = mktime(&duetime_tm);
278         }
279
280         return due_time;
281
282 }
283
284 static time_t __alarm_next_duetime_monthly(__alarm_info_t *__alarm_info)
285 {
286         time_t due_time = 0;
287         time_t current_time = 0;
288         struct tm duetime_tm;
289
290         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
291         alarm_date_t *start = &alarm_info->start;
292
293         time(&current_time);
294         localtime_r(&current_time, &duetime_tm);
295         duetime_tm.tm_hour = start->hour;
296         duetime_tm.tm_min = start->min;
297         duetime_tm.tm_sec = start->sec;
298
299         if (start->year != 0) {
300                 duetime_tm.tm_year = start->year - 1900;
301         }
302
303         if (start->month != 0) {
304
305                 duetime_tm.tm_mon = start->month - 1;
306         }
307
308         duetime_tm.tm_mday = start->day;
309
310         due_time = mktime(&duetime_tm);
311
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;
317                 }
318                 due_time = mktime(&duetime_tm);
319         }
320
321         return due_time;
322
323 }
324
325 static time_t __alarm_next_duetime_weekly(__alarm_info_t *__alarm_info)
326 {
327         time_t due_time = 0;
328         time_t current_time = 0;
329         struct tm duetime_tm;
330         struct tm tmp_tm;
331         int wday;
332         int current_dst = 0;
333         struct tm before_tm;
334         struct tm after_tm;
335
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;
339
340         tzset();
341         time(&current_time);
342         localtime_r(&current_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;
348
349         duetime_tm.tm_isdst = -1;
350
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;
356                 }
357         }
358
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);
362
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;
366
367                 due_time = mktime(&duetime_tm);
368                 localtime_r(&due_time, &tmp_tm);
369
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);
378                 }
379         }
380         else {
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);
386                 }
387         }
388
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);
392
393         wday = duetime_tm.tm_wday;
394
395         ALARM_MGR_EXCEPTION_PRINT("current_time(%d) due_time(%d)", current_time, due_time);
396
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) {
400                 return due_time;
401         }
402
403         if (current_time >= due_time || !(mode->u_interval.day_of_week & 1 << wday)) {
404                 int day = wday + 1;
405                 int interval = 1;
406                 /*this week */
407
408                 if (day == 7) {
409                         day = 0;
410                 }
411
412                 while (!(mode->u_interval.day_of_week & 1 << day) && interval < 8) {
413                         day += 1;
414                         interval += 1;
415
416                         if (day == 7) {
417                                 day = 0;
418                         }
419                 }
420
421                 ALARM_MGR_LOG_PRINT("interval : %d\n", interval);
422                 due_time += 60 * 60 * 24 * interval;
423         }
424
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);
428
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
434         }
435
436         ALARM_MGR_EXCEPTION_PRINT("Final due_time = %d", due_time);
437         return due_time;
438 }
439
440 time_t _alarm_next_duetime(__alarm_info_t *__alarm_info)
441 {
442         int is_dst=0;
443         time_t current_time = 0;
444         time_t due_time = 0;
445         struct tm *cur_tm = NULL ;
446         struct tm *due_tm = NULL ;
447
448         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
449         alarm_mode_t *mode = &alarm_info->mode;
450
451         time(&current_time);
452         cur_tm = localtime(&current_time);
453         if (cur_tm->tm_isdst > 0)
454                 is_dst = 1;
455
456         ALARM_MGR_LOG_PRINT("mode->repeat is %d\n", mode->repeat);
457
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);
468         } else {
469                 ALARM_MGR_EXCEPTION_PRINT("repeat mode(%d) is wrong\n",
470                                           mode->repeat);
471                 return 0;
472         }
473
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;
482                 }
483                 due_time = mktime(due_tm);
484         }
485
486          ALARM_MGR_EXCEPTION_PRINT("alarm_id: %d, next duetime: %d", __alarm_info->alarm_id, due_time);
487
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;
491                 return 0;
492         }
493         __alarm_info->due_time = due_time;
494
495         return due_time;
496 }
497
498 static bool __find_next_alarm_to_be_scheduled(time_t *min_due_time)
499 {
500         time_t current_time;
501         time_t min_time = -1;
502         time_t due_time;
503         GSList *iter = NULL;
504         __alarm_info_t *entry = NULL;
505
506         time(&current_time);
507
508         for (iter = alarm_context.alarms; iter != NULL;
509              iter = g_slist_next(iter)) {
510                 entry = iter->data;
511                 due_time = entry->due_time;
512
513                 double interval = 0;
514
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*/ {
518                         continue;
519                 }
520
521                 interval = difftime(due_time, current_time);
522
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);
525                         continue;
526                 }
527
528                 interval = difftime(due_time, min_time);
529
530                 if ((interval < 0) || min_time == -1) {
531                         min_time = due_time;
532                 }
533
534         }
535
536         *min_due_time = min_time;
537         return true;
538 }
539
540 bool _alarm_schedule()
541 {
542         time_t due_time = 0;
543         time_t min_time = 0;
544         GSList *iter = NULL;
545         __alarm_info_t *entry = NULL;
546
547         __find_next_alarm_to_be_scheduled(&min_time);
548
549         if (min_time == -1) {
550                 ALARM_MGR_LOG_PRINT("[alarm-server][schedule]: There is no alarm to be scheduled.");
551         } else {
552                 for (iter = alarm_context.alarms; iter != NULL; iter = g_slist_next(iter)) {
553                         entry = iter->data;
554                         due_time = entry->due_time;
555
556                         if (due_time == min_time) {
557                                 _add_to_scheduled_alarm_list(entry);
558                         }
559                 }
560                 _alarm_set_timer(&alarm_context, alarm_context.timer, min_time);
561         }
562
563         return true;
564 }