xbee: add a standalone makefile
[protos/xbee.git] / xbee.h
1 /*
2  * Copyright (c) 2011, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /* Callback when receiving data on a specific channel. The arguments
29  * of the function are the xbee device, the channel ID, the type of
30  * the frame (example: XBEE_TYPE_ATRESP), the pointer to the frame,
31  * the length of the frame, and an opaque pointer (reserved for user) */
32 typedef void (xbee_rx_cb_t)(struct xbee_dev *, int, int, void *,
33                             unsigned, void *);
34
35 /* an xbee queue */
36 struct xbee_channel {
37         int registered;
38         xbee_rx_cb_t *rx_cb;
39         void *arg;
40 };
41
42 #define XBEE_DEFAULT_CHANNEL 0
43 #define XBEE_MAX_CHANNEL 16
44 #define XBEE_CHANNEL_ANY XBEE_MAX_CHANNEL
45
46 /* structure identifying a xbee device */
47 struct xbee_dev {
48         const char *name;
49         int fd;
50         unsigned baud;
51         struct xbee_bufq queue;
52         struct xbee_channel channel[XBEE_MAX_CHANNEL];
53         struct xbee_stats stats;
54         struct xbee_neigh_list neigh_list;
55 };
56
57 /* initialize xbee library */
58 int xbee_init(void);
59
60 /* open an xbee device */
61 struct xbee_dev *xbee_open(const char *devname, unsigned baudrate);
62
63 /* closes an xbee device */
64 int xbee_close(struct xbee_dev *dev);
65
66 /* Register a channel, return the ID of the channel or a negative
67  * value on error. The rx_cb is a pointer to a function that will be
68  * called by xbee_read() when a frame is received for this channel. If
69  * rx_cb is NULL, no callback will occur. The "channel" argument can
70  * be XBEE_CHANNEL_ANY to let the library choose the channel, or a
71  * channel number to request a specific one. */
72 int xbee_register_channel(struct xbee_dev *dev, int channel,
73                           xbee_rx_cb_t *rx_cb, void *opaque);
74
75 /* Unregister a channel, return 0 on success */
76 int xbee_unregister_channel(struct xbee_dev *dev, int channel_id);
77
78 /* read data from device fd and put it in queue */
79 int xbee_read(struct xbee_dev *dev);
80
81 /* process all data in queue */
82 int xbee_process_queue(struct xbee_dev *dev);