merged with private
[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
36 #include<dbus/dbus.h>
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 timer_t _alarm_create_timer()
53 {
54         timer_t timer = (timer_t) -1;
55         struct sigevent timer_event;
56
57         ALARM_MGR_LOG_PRINT("[alarm-server][timer]\n");
58
59         timer_event.sigev_notify = SIGEV_SIGNAL;
60         timer_event.sigev_signo = SIG_TIMER;
61         timer_event.sigev_value.sival_ptr = (void *)timer;
62
63         if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
64                 perror("create timer has failed\n");
65                 exit(1);
66         }
67
68         return timer;
69
70 }
71
72 bool _alarm_disable_timer(__alarm_server_context_t alarm_context)
73 {
74         struct itimerspec time_spec;
75
76         time_spec.it_value.tv_sec = 0;
77         time_spec.it_value.tv_nsec = 0;
78         time_spec.it_interval.tv_sec = time_spec.it_interval.tv_nsec = 0;
79
80         if (timer_settime(alarm_context.timer, 0, &time_spec, NULL) < 0) {
81                 perror("disable timer has failed\n");
82                 return false;
83         }
84
85         return true;
86 }
87
88 bool _alarm_set_timer(__alarm_server_context_t *alarm_context, timer_t timer,
89                       time_t due_time, alarm_id_t id)
90 {
91         struct itimerspec time_spec;
92         time_t current_time;
93         double interval;
94         char due_time_r[100] = { 0 };
95         struct tm ts_ret;
96         
97         extern int errno;
98
99         time(&current_time);
100
101         interval = difftime(due_time, current_time);
102         ALARM_MGR_LOG_PRINT("[alarm-server][timer]: remain time from "
103                             "current is %f , due_time is %d\n", interval,
104                             due_time);
105
106         /*set timer as absolute time */
107         /*we create dummy timer when the interval is longer than one day. */
108         /*the timer will be expired in half day. */
109
110         localtime_r(&due_time, &ts_ret);
111
112         if (interval > 60 * 60 * 24) {
113                 interval = 60 * 60 * 12;
114                 g_dummy_timer_is_set = true;
115                 strftime(due_time_r, 30, "%c", &ts_ret);
116                 ALARM_MGR_LOG_PRINT("create dummy alarm timer(%d), "
117                                     "due_time(%s) \n", timer, due_time_r);
118         } else
119                 g_dummy_timer_is_set = false;
120
121         time_spec.it_value.tv_sec = due_time;
122         time_spec.it_value.tv_nsec = 0;
123         time_spec.it_interval.tv_sec = time_spec.it_interval.tv_nsec = 0;
124
125         if (interval > 0
126             && timer_settime(timer, TIMER_ABSTIME, &time_spec, NULL) != 0) {
127                 ALARM_MGR_EXCEPTION_PRINT("set timer has failed : timer(%d), "
128                                           "due_time(%u) , errno(%d)\n", timer,
129                                           due_time, errno);
130                 return false;
131         }
132         /* we set c_due_time to due_time due to allow newly created alarm can 
133            be schedlued when its interval is less than the current alarm */
134         alarm_context->c_due_time = due_time;
135         return true;
136 }
137
138 int _set_sys_time(time_t _time)
139 {
140         struct tm *_tm;
141         struct tm result;
142         _tm = gmtime_r(&_time, &result);
143
144         stime(&_time);
145
146         return 1;
147 }
148
149 int _set_time(time_t _time)
150 {
151         ALARM_MGR_LOG_PRINT("ENTER FUNC _set_time(%d)", _time);
152         int ret = 1;
153         ret = _set_rtc_time(_time);
154         _set_sys_time(_time);
155
156         /* inoti (broadcasting without data 
157          * send system noti that time has changed */
158
159         return 0;
160 }