summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bluetooth/btpand/event.c
blob: 159f90c862d4cfa24bf1d359cd0041ba3548a836 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * event.h
 */

/*-
 * Copyright (c) 2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* $FreeBSD$ */

/*
 * Hack to provide libevent (see devel/libevent port) like API.
 * Should be removed if FreeBSD ever decides to import libevent into base.
 */

#include <sys/select.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>

#include "event.h"
#include "btpand.h"

#define		__event_link(ev) \
do { \
		TAILQ_INSERT_TAIL(&pending, ev, next); \
		ev->flags |= EV_PENDING; \
} while (0)

static void	tv_add(struct timeval *, struct timeval const *);
static void	tv_sub(struct timeval *, struct timeval const *);
static int	tv_cmp(struct timeval const *, struct timeval const *);
static int	__event_dispatch(void);
static void	__event_add_current(struct event *);
static void	__event_del_current(struct event *);


static TAILQ_HEAD(, event)	pending;
static TAILQ_HEAD(, event)	current;

void
event_init(void)
{
	TAILQ_INIT(&pending);
}

int
event_dispatch(void)
{
	while (__event_dispatch() == 0)
		;

	return (-1);
}

static int
__event_dispatch(void)
{
	fd_set			r, w;
	int			nfd;
	struct event		*ev;
	struct timeval		now, timeout, t;

	FD_ZERO(&r);
	FD_ZERO(&w);

	nfd = 0;

	gettimeofday(&now, NULL);

	timeout.tv_sec = 10;	/* arbitrary */
	timeout.tv_usec = 0;

	TAILQ_INIT(&current);

	/*
	 * Build fd_set's
	 */

	event_log_debug("%s: building fd set...", __func__);

	while (!TAILQ_EMPTY(&pending)) {
		ev = TAILQ_FIRST(&pending);
		event_del(ev);

		if (ev->flags & EV_HAS_TIMEOUT) {
			if (tv_cmp(&now, &ev->expire) >= 0)
				t.tv_sec = t.tv_usec = 0;
			else {
				t = ev->expire;
				tv_sub(&t, &now);
			}

			if (tv_cmp(&t, &timeout) < 0)
				timeout = t;
		}

		if (ev->fd >= 0) {
			if (ev->flags & EV_READ) {
				FD_SET(ev->fd, &r);
				nfd = (nfd > ev->fd) ? nfd : ev->fd;
			}

			if (ev->flags & EV_WRITE) {
				FD_SET(ev->fd, &w);
				nfd = (nfd > ev->fd) ? nfd : ev->fd;
			}
		}

		__event_add_current(ev);
	}

	event_log_debug("%s: waiting for events...", __func__);

	nfd = select(nfd + 1, &r, &w, NULL, &timeout);
	if (nfd < 0)
		return (-1);

	/*
	 * Process current pending
	 */

	event_log_debug("%s: processing events...", __func__);

	gettimeofday(&now, NULL);

	while (!TAILQ_EMPTY(&current)) {
		ev = TAILQ_FIRST(&current);
		__event_del_current(ev);

		/* check if fd is ready for reading/writing */
		if (nfd > 0 && ev->fd >= 0) {
			if (FD_ISSET(ev->fd, &r) || FD_ISSET(ev->fd, &w)) {
				if (ev->flags & EV_PERSIST) {
					if (ev->flags & EV_HAS_TIMEOUT)
						event_add(ev, &ev->timeout);
					else
						event_add(ev, NULL);
				}

				nfd --;

				event_log_debug("%s: calling %p(%d, %p), " \
					"ev=%p", __func__, ev->cb, ev->fd,
					ev->cbarg, ev);

				(ev->cb)(ev->fd,
					(ev->flags & (EV_READ|EV_WRITE)),
					ev->cbarg);

				continue;
			}
		}

		/* if event has no timeout - just requeue */
		if ((ev->flags & EV_HAS_TIMEOUT) == 0) {
			event_add(ev, NULL);
			continue;
		}

		/* check if event has expired */
		if (tv_cmp(&now, &ev->expire) >= 0) {
			if (ev->flags & EV_PERSIST) 
				event_add(ev, &ev->timeout);

			event_log_debug("%s: calling %p(%d, %p), ev=%p",
				__func__, ev->cb, ev->fd, ev->cbarg, ev);

			(ev->cb)(ev->fd,
				(ev->flags & (EV_READ|EV_WRITE)),
				ev->cbarg);

			continue;
		}

		assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
		__event_link(ev);
	}

	return (0);
}

void
__event_set(struct event *ev, int fd, short flags,
	void (*cb)(int, short, void *), void *cbarg)
{
	ev->fd = fd;
	ev->flags = flags;
	ev->cb = cb;
	ev->cbarg = cbarg;
}

int
__event_add(struct event *ev, const struct timeval *timeout)
{
	assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);

	if (timeout != NULL) {
		gettimeofday(&ev->expire, NULL);
		tv_add(&ev->expire, timeout);
		ev->timeout = *timeout;
		ev->flags |= EV_HAS_TIMEOUT;
	} else
		ev->flags &= ~EV_HAS_TIMEOUT;

	__event_link(ev);

	return (0);
}

int
__event_del(struct event *ev)
{
	assert((ev->flags & EV_CURRENT) == 0);

	if ((ev->flags & EV_PENDING) != 0) {
		TAILQ_REMOVE(&pending, ev, next);
		ev->flags &= ~EV_PENDING;
	}

	return (0);
}

static void
__event_add_current(struct event *ev)
{
	assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);

	TAILQ_INSERT_TAIL(&current, ev, next);
	ev->flags |= EV_CURRENT;
}

static void
__event_del_current(struct event *ev)
{
	assert((ev->flags & (EV_CURRENT|EV_PENDING)) == EV_CURRENT);

	TAILQ_REMOVE(&current, ev, next);
	ev->flags &= ~EV_CURRENT;
}

static void
tv_add(struct timeval *a, struct timeval const *b)
{
	a->tv_sec += b->tv_sec;
	a->tv_usec += b->tv_usec;

	if(a->tv_usec >= 1000000) {
		a->tv_usec -= 1000000;
		a->tv_sec += 1;
	}
}

static void
tv_sub(struct timeval *a, struct timeval const *b)
{
	if (a->tv_usec < b->tv_usec) {
		a->tv_usec += 1000000;
		a->tv_sec -= 1;
	}

	a->tv_usec -= b->tv_usec;
	a->tv_sec -= b->tv_sec;
}

static int
tv_cmp(struct timeval const *a, struct timeval const *b)
{
 	if (a->tv_sec > b->tv_sec)
		return (1);

	if (a->tv_sec < b->tv_sec)
		return (-1);

	if (a->tv_usec > b->tv_usec)
		return (1);

	if (a->tv_usec < b->tv_usec)
		return (-1);

	return (0);
}

OpenPOWER on IntegriCloud