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