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