summaryrefslogtreecommitdiff
path: root/alarm-manager-timer.c
blob: bf847b3cb0734354061d8a8874303bdcc9d2276f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 *  alarm-manager
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Venkatesha Sarpangala <sarpangala.v@samsung.com>, Jayoun Lee <airjany@samsung.com>,
 * Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */




#define _BSD_SOURCE		/*gmtime_r requires */

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<signal.h>
#include<string.h>
#include<sys/types.h>
#include<errno.h>

#include<dbus/dbus.h>
#include<glib.h>

#include"alarm.h"
#include"alarm-internal.h"

extern bool g_dummy_timer_is_set;

bool _alarm_destory_timer(timer_t timer)
{
	if (timer_delete(timer) == 0)
		return true;
	else
		return false;
}

timer_t _alarm_create_timer()
{
	timer_t timer = (timer_t) -1;
	struct sigevent timer_event;

	ALARM_MGR_LOG_PRINT("[alarm-server][timer]\n");

	timer_event.sigev_notify = SIGEV_SIGNAL;
	timer_event.sigev_signo = SIG_TIMER;
	timer_event.sigev_value.sival_ptr = (void *)timer;

	if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
		perror("create timer has failed\n");
		exit(1);
	}

	return timer;

}

bool _alarm_disable_timer(__alarm_server_context_t alarm_context)
{
	struct itimerspec time_spec;

	time_spec.it_value.tv_sec = 0;
	time_spec.it_value.tv_nsec = 0;
	time_spec.it_interval.tv_sec = time_spec.it_interval.tv_nsec = 0;

	if (timer_settime(alarm_context.timer, 0, &time_spec, NULL) < 0) {
		perror("disable timer has failed\n");
		return false;
	}

	return true;
}

bool _alarm_set_timer(__alarm_server_context_t *alarm_context, timer_t timer,
		      time_t due_time, alarm_id_t id)
{
	struct itimerspec time_spec;
	time_t current_time;
	double interval;
	char due_time_r[100] = { 0 };
	struct tm ts_ret;
	
	extern int errno;

	time(&current_time);

	interval = difftime(due_time, current_time);
	ALARM_MGR_LOG_PRINT("[alarm-server][timer]: remain time from "
			    "current is %f , due_time is %d\n", interval,
			    due_time);

	/*set timer as absolute time */
	/*we create dummy timer when the interval is longer than one day. */
	/*the timer will be expired in half day. */

	localtime_r(&due_time, &ts_ret);

	if (interval > 60 * 60 * 24) {
		interval = 60 * 60 * 12;
		g_dummy_timer_is_set = true;
		strftime(due_time_r, 30, "%c", &ts_ret);
		ALARM_MGR_LOG_PRINT("create dummy alarm timer(%d), "
				    "due_time(%s) \n", timer, due_time_r);
	} else
		g_dummy_timer_is_set = false;

	time_spec.it_value.tv_sec = due_time;
	time_spec.it_value.tv_nsec = 0;
	time_spec.it_interval.tv_sec = time_spec.it_interval.tv_nsec = 0;

	if (interval > 0
	    && timer_settime(timer, TIMER_ABSTIME, &time_spec, NULL) != 0) {
		ALARM_MGR_EXCEPTION_PRINT("set timer has failed : timer(%d), "
					  "due_time(%u) , errno(%d)\n", timer,
					  due_time, errno);
		return false;
	}
	/* we set c_due_time to due_time due to allow newly created alarm can 
	   be schedlued when its interval is less than the current alarm */
	alarm_context->c_due_time = due_time;
	return true;
}

int _set_sys_time(time_t _time)
{
	struct tm *_tm;
	struct tm result;
	/* Ignore return value of gmtime_r(). */
	(void) gmtime_r(&_time, &result);

	stime(&_time);

	return 1;
}

int _set_time(time_t _time)
{
	ALARM_MGR_LOG_PRINT("ENTER FUNC _set_time(%d)", _time);
	int ret = 1;
	ret = _set_rtc_time(_time);
	_set_sys_time(_time);

	/* inoti (broadcasting without data 
	 * send system noti that time has changed */

	return 0;
}