xbee: add a standalone makefile
[protos/xbee.git] / xbee_buf.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 #define XBEE_BUF_SIZE 0x200
29
30 /* a xbee data buffer */
31 struct xbee_buf {
32         CIRCLEQ_ENTRY(xbee_buf) next;
33         unsigned offset;
34         unsigned len;
35         char buf[XBEE_BUF_SIZE];
36 };
37
38 /* queue of xbee_buf */
39 CIRCLEQ_HEAD(xbufq, xbee_buf);
40
41 struct xbee_bufq {
42         struct xbufq xbq;
43         unsigned len;
44         unsigned nseg;
45 };
46
47 /* allocate a new xbee_buf */
48 struct xbee_buf *xbee_buf_alloc(void);
49
50 /* return the number of remaining bytes in xbee_buf */
51 int xbee_buf_tailroom(struct xbee_buf *xbuf);
52
53 /* return the pointer to data at offset 'off', or NULL if off > xbuf->len */
54 char *xbee_buf_data(struct xbee_buf *xbuf, unsigned off);
55
56 /* return the first data of a xbuf (also works if len is 0) */
57 char *xbee_buf_head(struct xbee_buf *xbuf);
58
59 /* return the pointer just after data of a xbuf (also works if len is 0) */
60 char *xbee_buf_tail(struct xbee_buf *xbuf);
61
62 /* enqueue a xbuf in a xbufq */
63 void xbee_buf_enqueue(struct xbee_bufq *q, struct xbee_buf *xbuf);
64
65
66
67 /* return the last xbuf of a queue, or NULL if the queue is empty */
68 struct xbee_buf *xbee_bufq_last(struct xbee_bufq *q);
69
70 /* initialize a xbuf queue */
71 void xbee_bufq_init(struct xbee_bufq *q);
72
73 /* flush a xbuf queue */
74 void xbee_bufq_flush(struct xbee_bufq *q);
75
76 /* append data in queue (just update lens), user should memcpy first */
77 void xbee_bufq_append(struct xbee_bufq *q, unsigned len);
78
79 /* return the pointer to data at offset 'off', or NULL if off > q->len */
80 char *xbee_bufq_data(struct xbee_bufq *q, unsigned off);
81
82 /* drop data in front of queue */
83 int xbee_bufq_drop(struct xbee_bufq *q, unsigned len);
84
85 /* copy data in front of queue in a linear buffer */
86 int xbee_bufq_copy(struct xbee_bufq *q, void *buf, unsigned len);