tizen 2.3.1 release
[framework/appfw/alarm-manager.git] / alarm-manager-timer.c
1 /*
2  *  alarm-manager
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Venkatesha Sarpangala <sarpangala.v@samsung.com>, Jayoun Lee <airjany@samsung.com>,
7  * Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24
25
26 #define _BSD_SOURCE             /*gmtime_r requires */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <time.h>
31 #include <signal.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <errno.h>
35 #include <sys/timerfd.h>
36
37 #include <glib.h>
38
39 #include "alarm.h"
40 #include "alarm-internal.h"
41
42 extern bool g_dummy_timer_is_set;
43
44 bool _alarm_destory_timer(timer_t timer)
45 {
46         if (timer_delete(timer) == 0)
47                 return true;
48         else
49                 return false;
50 }
51
52 bool _alarm_disable_timer(__alarm_server_context_t alarm_context)
53 {
54         struct itimerspec time_spec;
55
56         time_spec.it_value.tv_sec = 0;
57         time_spec.it_value.tv_nsec = 0;
58         time_spec.it_interval.tv_sec = time_spec.it_interval.tv_nsec = 0;
59
60         if (timerfd_settime(alarm_context.timer, 0, &time_spec, NULL) < 0) {
61                 perror("disable timer has failed\n");
62                 return false;
63         }
64
65         return true;
66 }
67
68 bool _alarm_set_timer(__alarm_server_context_t *alarm_context, int timer, time_t due_time)
69 {
70         struct itimerspec time_spec;
71         time_t current_time;
72         double interval;
73         char due_time_r[100] = { 0 };
74         struct tm ts_ret;
75         extern int errno;
76
77         time(&current_time);
78
79         interval = difftime(due_time, current_time);
80         ALARM_MGR_LOG_PRINT("[alarm-server][timer]: remain time from current is %f , due_time is %d.", interval, due_time);
81
82         /*set timer as absolute time */
83         /*we create dummy timer when the interval is longer than one day. */
84         /*the timer will be expired in half day. */
85
86         localtime_r(&due_time, &ts_ret);
87
88         if (interval > 60 * 60 * 24) {
89                 interval = 60 * 60 * 12;
90                 g_dummy_timer_is_set = true;
91                 strftime(due_time_r, 30, "%c", &ts_ret);
92                 ALARM_MGR_LOG_PRINT("create dummy alarm timer(%d), due_time(%s)", timer, due_time_r);
93         }
94         else {
95                 g_dummy_timer_is_set = false;
96         }
97
98         time_spec.it_value.tv_sec = due_time;
99         time_spec.it_value.tv_nsec = 0;
100         time_spec.it_interval.tv_sec = time_spec.it_interval.tv_nsec = 0;
101
102         if (interval > 0 && timerfd_settime(timer, TFD_TIMER_ABSTIME, &time_spec, NULL) != 0) {
103                 ALARM_MGR_EXCEPTION_PRINT("set timer has failed : timer(%d), due_time(%u) , errno(%d).", timer, due_time, errno);
104                 return false;
105         }
106         /* we set c_due_time to due_time due to allow newly created alarm can
107            be schedlued when its interval is less than the current alarm */
108         alarm_context->c_due_time = due_time;
109         return true;
110 }
111
112 int _set_sys_time(time_t _time)
113 {
114         struct tm result;
115         /* Ignore return value of gmtime_r(). */
116         (void) gmtime_r(&_time, &result);
117
118         stime(&_time);
119
120         return 1;
121 }
122
123 int _set_time(time_t _time)
124 {
125         ALARM_MGR_LOG_PRINT("ENTER FUNC _set_time(%d)", _time);
126         _set_rtc_time(_time);
127         //_set_sys_time(_time);
128
129         /* inoti (broadcasting without data 
130          * send system noti that time has changed */
131
132         return 0;
133 }