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 */
33 #include <sys/types.h>
35 #include <sys/timerfd.h>
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 bool _alarm_disable_timer(__alarm_server_context_t alarm_context)
54 struct itimerspec time_spec;
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;
60 if (timerfd_settime(alarm_context.timer, 0, &time_spec, NULL) < 0) {
61 perror("disable timer has failed\n");
68 bool _alarm_set_timer(__alarm_server_context_t *alarm_context, int timer, time_t due_time)
70 struct itimerspec time_spec;
73 char due_time_r[100] = { 0 };
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);
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. */
86 localtime_r(&due_time, &ts_ret);
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);
95 g_dummy_timer_is_set = false;
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;
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);
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;
112 int _set_sys_time(time_t _time)
115 /* Ignore return value of gmtime_r(). */
116 (void) gmtime_r(&_time, &result);
123 int _set_time(time_t _time)
125 ALARM_MGR_LOG_PRINT("ENTER FUNC _set_time(%d)", _time);
126 _set_rtc_time(_time);
127 //_set_sys_time(_time);
129 /* inoti (broadcasting without data
130 * send system noti that time has changed */