Path: blob/main/usr.sbin/bluetooth/btpand/event.h
105866 views
/*1* event.h2*/34/*-5* SPDX-License-Identifier: BSD-2-Clause6*7* Copyright (c) 2009 Maksim Yevmenkin <[email protected]>8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18*19* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/313233/*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*/3738#ifndef _EVENT_H_39#define _EVENT_H_ 14041#define EV_READ 0x0242#define EV_WRITE 0x0443#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! */4748struct event49{50int fd;51short flags;52void (*cb)(int, short, void *);53void *cbarg;54struct timeval timeout;55struct timeval expire;5657#ifdef EVENT_DEBUG58char const *files[3];59int lines[3];60#endif6162TAILQ_ENTRY(event) next;63};6465void event_init (void);66int event_dispatch (void);6768void __event_set (struct event *, int, short,69void (*)(int, short, void *), void *);70int __event_add (struct event *, struct timeval const *);71int __event_del (struct event *);7273#ifdef EVENT_DEBUG74#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)7879#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)8586#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)9091static inline void92_event_set(char const *file, int line, struct event *ev, int fd, short flags,93void (*cb)(int, short, void *), void *cbarg)94{95event_log_debug("set %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p",96file, line, ev, fd, flags, cb, cbarg);9798ev->files[0] = file;99ev->lines[0] = line;100101__event_set(ev, fd, flags, cb, cbarg);102}103104static inline int105_event_add(char const *file, int line, struct event *ev,106struct timeval const *timeout) {107event_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,109ev->cbarg, timeout);110111ev->files[1] = file;112ev->lines[1] = line;113114return (__event_add(ev, timeout));115}116117static inline int118_event_del(char const *file, int line, struct event *ev)119{120event_log_debug("del %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p",121file, line, ev, ev->fd, ev->flags, ev->cb, ev->cbarg);122123ev->files[2] = file;124ev->lines[2] = line;125126return (__event_del(ev));127}128#else129#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...)133134#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)140141#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 */146147#endif /* ndef _EVENT_H_ */148149150