xbee: add a new module
[aversive.git] / modules / devices / radio / xbee / 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 #ifndef _XBEE_H_
29 #define _XBEE_H_
30
31 #include "xbee_neighbor.h"
32 #include "xbee_atcmd.h"
33 #include "xbee_stats.h"
34 #include "xbee_rxtx.h"
35
36 /* Callback when receiving data on a specific channel. The arguments of the
37  * function are the xbee device, the channel ID, the type of the frame (example:
38  * XBEE_TYPE_ATRESP), the pointer to the frame, the length of the frame, and an
39  * opaque pointer (reserved for user). The given length exludes the xbee_hdr
40  * structure (delimiter, len, type, id) and the cksum.  */
41 typedef int8_t (xbee_rx_cb_t)(struct xbee_dev *dev, int channel, int type,
42         void *frame, unsigned len, void *opaque);
43
44 /* an xbee queue */
45 struct xbee_channel {
46         int registered;
47         xbee_rx_cb_t *rx_cb;
48         void *arg;
49 };
50
51 #define XBEE_DEFAULT_CHANNEL 0
52 #define XBEE_MAX_CHANNEL 16
53 #define XBEE_CHANNEL_ANY XBEE_MAX_CHANNEL
54
55 /* structure identifying a xbee device */
56 struct xbee_dev {
57         FILE *file;
58         struct xbee_channel channel[XBEE_MAX_CHANNEL];
59         uint8_t frame_len;
60         char frame[XBEE_MAX_FRAME_LEN];
61         struct xbee_neigh_list neigh_list;
62 #ifdef CONFIG_MODULE_XBEE_STATS
63         struct xbee_stats stats;
64 #endif
65 };
66
67 /* initialize xbee library */
68 int xbee_init(void);
69
70 /* open an xbee device */
71 int xbee_open(struct xbee_dev *dev, FILE *xbee_file);
72
73 /* closes an xbee device */
74 int xbee_close(struct xbee_dev *dev);
75
76 /* Register a channel, return the ID of the channel or a negative
77  * value on error. The rx_cb is a pointer to a function that will be
78  * called by xbee_read() when a frame is received for this channel. If
79  * rx_cb is NULL, no callback will occur. The "channel" argument can
80  * be XBEE_CHANNEL_ANY to let the library choose the channel, or a
81  * channel number to request a specific one. */
82 int xbee_register_channel(struct xbee_dev *dev, int channel,
83                           xbee_rx_cb_t *rx_cb, void *opaque);
84
85 /* This function (re)sets the opaque pointer on a registered
86  * channel. The function returns 0 on success and -1 on error (channel
87  * not registered). As the opaque pointer can already be set after a
88  * call to xbee_register_channel(), this function is only useful if
89  * the opaque pointer has to be modified. */
90 int xbee_set_opaque(struct xbee_dev *dev, int channel, void *opaque);
91
92 /* Unregister a channel, return 0 on success */
93 int xbee_unregister_channel(struct xbee_dev *dev, int channel_id);
94
95 /* read data from device fd and put it in queue */
96 int xbee_read(struct xbee_dev *dev);
97
98 /* process all data in queue */
99 int xbee_process_queue(struct xbee_dev *dev);
100
101 #endif /* _XBEE_H_ */