xref: /freebsd/usr.sbin/bluetooth/btpand/event.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*
2  * event.h
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * Copyright (c) 2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 
34 /*
35  * Hack to provide libevent (see devel/libevent port) like API.
36  * Should be removed if FreeBSD ever decides to import libevent into base.
37  */
38 
39 #ifndef	_EVENT_H_
40 #define	_EVENT_H_	1
41 
42 #define	EV_READ         0x02
43 #define	EV_WRITE        0x04
44 #define	EV_PERSIST      0x10		/* Persistent event */
45 #define	EV_PENDING	(1 << 13)	/* internal use only! */
46 #define	EV_HAS_TIMEOUT	(1 << 14)	/* internal use only! */
47 #define	EV_CURRENT	(1 << 15)	/* internal use only! */
48 
49 struct event
50 {
51 	int			fd;
52 	short			flags;
53 	void			(*cb)(int, short, void *);
54 	void			*cbarg;
55 	struct timeval		timeout;
56 	struct timeval		expire;
57 
58 #ifdef	EVENT_DEBUG
59 	char const		*files[3];
60 	int			lines[3];
61 #endif
62 
63 	TAILQ_ENTRY(event)	next;
64 };
65 
66 void	event_init	(void);
67 int	event_dispatch	(void);
68 
69 void	__event_set	(struct event *, int, short,
70 			 void (*)(int, short, void *), void *);
71 int	__event_add	(struct event *, struct timeval const *);
72 int	__event_del	(struct event *);
73 
74 #ifdef	EVENT_DEBUG
75 #define event_log_err(fmt, args...)	syslog(LOG_ERR, fmt, ##args)
76 #define event_log_info(fmt, args...)	syslog(LOG_INFO, fmt, ##args)
77 #define event_log_notice(fmt, args...)	syslog(LOG_NOTICE, fmt, ##args)
78 #define event_log_debug(fmt, args...)	syslog(LOG_DEBUG, fmt, ##args)
79 
80 #define	event_set(ev, fd, flags, cb, cbarg) \
81 	_event_set(__FILE__, __LINE__, ev, fd, flags, cb, cbarg)
82 #define	event_add(ev, timeout) \
83 	_event_add(__FILE__, __LINE__, ev, timeout)
84 #define	event_del(ev) \
85 	_event_del(__FILE__, __LINE__, ev)
86 
87 #define	evtimer_set(ev, cb, cbarg) \
88 	_event_set(__FILE__, __LINE__, ev, -1, 0, cb, cbarg)
89 #define	evtimer_add(ev, timeout) \
90 	_event_add(__FILE__, __LINE__, ev, timeout)
91 
92 static inline void
93 _event_set(char const *file, int line, struct event *ev, int fd, short flags,
94 		void (*cb)(int, short, void *), void *cbarg)
95 {
96 	event_log_debug("set %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p",
97 		file, line, ev, fd, flags, cb, cbarg);
98 
99 	ev->files[0] = file;
100 	ev->lines[0] = line;
101 
102 	__event_set(ev, fd, flags, cb, cbarg);
103 }
104 
105 static inline int
106 _event_add(char const *file, int line, struct event *ev,
107 		struct timeval const *timeout) {
108 	event_log_debug("add %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p, " \
109 		"timeout=%p", file, line, ev, ev->fd, ev->flags, ev->cb,
110 		ev->cbarg, timeout);
111 
112 	ev->files[1] = file;
113 	ev->lines[1] = line;
114 
115 	return (__event_add(ev, timeout));
116 }
117 
118 static inline int
119 _event_del(char const *file, int line, struct event *ev)
120 {
121 	event_log_debug("del %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p",
122 		file, line, ev, ev->fd, ev->flags, ev->cb, ev->cbarg);
123 
124 	ev->files[2] = file;
125 	ev->lines[2] = line;
126 
127 	return (__event_del(ev));
128 }
129 #else
130 #define event_log_err(fmt, args...)
131 #define event_log_info(fmt, args...)
132 #define event_log_notice(fmt, args...)
133 #define event_log_debug(fmt, args...)
134 
135 #define	event_set(ev, fd, flags, cb, cbarg) \
136 	__event_set(ev, fd, flags, cb, cbarg)
137 #define	event_add(ev, timeout) \
138 	__event_add(ev, timeout)
139 #define	event_del(ev) \
140 	__event_del(ev)
141 
142 #define	evtimer_set(ev, cb, cbarg) \
143 	__event_set(ev, -1, 0, cb, cbarg)
144 #define	evtimer_add(ev, timeout) \
145 	__event_add(ev, timeout)
146 #endif	/* EVENT_DEBUG */
147 
148 #endif	/* ndef _EVENT_H_ */
149