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.
26 #define _BSD_SOURCE /*gmtime_r requires */
40 #include"alarm-internal.h"
42 extern bool g_dummy_timer_is_set;
44 bool _alarm_destory_timer(timer_t timer)
46 if (timer_delete(timer) == 0)
52 timer_t _alarm_create_timer()
54 timer_t timer = (timer_t) -1;
55 struct sigevent timer_event;
57 ALARM_MGR_LOG_PRINT("[alarm-server][timer]\n");
59 timer_event.sigev_notify = SIGEV_SIGNAL;
60 timer_event.sigev_signo = SIG_TIMER;
61 timer_event.sigev_value.sival_ptr = (void *)timer;
63 if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
64 perror("create timer has failed\n");
72 bool _alarm_disable_timer(__alarm_server_context_t alarm_context)
74 struct itimerspec time_spec;
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;
80 if (timer_settime(alarm_context.timer, 0, &time_spec, NULL) < 0) {
81 perror("disable timer has failed\n");
88 bool _alarm_set_timer(__alarm_server_context_t *alarm_context, timer_t timer,
89 time_t due_time, alarm_id_t id)
91 struct itimerspec time_spec;
94 char due_time_r[100] = { 0 };
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,
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. */
110 localtime_r(&due_time, &ts_ret);
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);
119 g_dummy_timer_is_set = false;
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;
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,
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;
138 int _set_sys_time(time_t _time)
142 /* Ignore return value of gmtime_r(). */
143 (void) gmtime_r(&_time, &result);
150 int _set_time(time_t _time)
152 ALARM_MGR_LOG_PRINT("ENTER FUNC _set_time(%d)", _time);
154 ret = _set_rtc_time(_time);
155 _set_sys_time(_time);
157 /* inoti (broadcasting without data
158 * send system noti that time has changed */