2 * Copyright (c) 2011, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
32 #include <sys/queue.h>
34 #include "xbee_neighbor.h"
35 #include "xbee_stats.h"
39 struct xbee_buf *xbee_buf_alloc(void)
41 struct xbee_buf *xbuf;
43 xbuf = malloc(sizeof(*xbuf));
46 memset(xbuf, 0, sizeof(*xbuf));
52 int xbee_buf_tailroom(struct xbee_buf *xbuf)
54 return XBEE_BUF_SIZE - xbuf->len - xbuf->offset;
57 char *xbee_buf_data(struct xbee_buf *xbuf, unsigned off)
61 return xbuf->buf + xbuf->offset + off;
64 char *xbee_buf_head(struct xbee_buf *xbuf)
66 return xbuf->buf + xbuf->offset;
69 char *xbee_buf_tail(struct xbee_buf *xbuf)
71 return xbuf->buf + xbuf->offset + xbuf->len;
74 void xbee_buf_enqueue(struct xbee_bufq *q, struct xbee_buf *xbuf)
76 CIRCLEQ_INSERT_TAIL(&q->xbq, xbuf, next);
81 struct xbee_buf *xbee_bufq_last(struct xbee_bufq *q)
83 if (CIRCLEQ_EMPTY(&q->xbq))
85 return CIRCLEQ_LAST(&q->xbq);
88 void xbee_bufq_init(struct xbee_bufq *q)
90 CIRCLEQ_INIT(&q->xbq);
95 void xbee_bufq_append(struct xbee_bufq *q, unsigned len)
97 struct xbee_buf *xbuf;
100 xbuf = CIRCLEQ_LAST(&q->xbq);
104 void xbee_bufq_flush(struct xbee_bufq *q)
106 struct xbee_buf *xbuf;
108 while (!CIRCLEQ_EMPTY(&q->xbq)) {
109 xbuf = CIRCLEQ_FIRST(&q->xbq);
110 CIRCLEQ_REMOVE(&q->xbq, xbuf, next);
117 char *xbee_bufq_data(struct xbee_bufq *q, unsigned off)
119 struct xbee_buf *xbuf;
125 CIRCLEQ_FOREACH(xbuf, &q->xbq, next) {
126 data = xbee_buf_data(xbuf, off);
135 /* drop data in front of queue */
136 int xbee_bufq_drop(struct xbee_bufq *q, unsigned len)
138 struct xbee_buf *xbuf;
143 while (!CIRCLEQ_EMPTY(&q->xbq)) {
144 xbuf = CIRCLEQ_FIRST(&q->xbq);
147 CIRCLEQ_REMOVE(&q->xbq, xbuf, next);
164 int xbee_bufq_copy(struct xbee_bufq *q, void *buf, unsigned len)
166 struct xbee_buf *xbuf;
167 unsigned dstoff = 0, copylen;
172 CIRCLEQ_FOREACH(xbuf, &q->xbq, next) {
176 memcpy(buf + dstoff, xbuf->buf + xbuf->offset, copylen);