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.
31 #include <sys/queue.h>
33 #include "xbee_neighbor.h"
34 #include "xbee_stats.h"
38 struct xbee_buf *xbee_buf_alloc(void)
40 struct xbee_buf *xbuf;
42 xbuf = malloc(sizeof(*xbuf));
45 memset(xbuf, 0, sizeof(*xbuf));
51 int xbee_buf_tailroom(struct xbee_buf *xbuf)
53 return XBEE_BUF_SIZE - xbuf->len - xbuf->offset;
56 char *xbee_buf_data(struct xbee_buf *xbuf, unsigned off)
60 return xbuf->buf + xbuf->offset + off;
63 char *xbee_buf_head(struct xbee_buf *xbuf)
65 return xbuf->buf + xbuf->offset;
68 char *xbee_buf_tail(struct xbee_buf *xbuf)
70 return xbuf->buf + xbuf->offset + xbuf->len;
73 void xbee_buf_enqueue(struct xbee_bufq *q, struct xbee_buf *xbuf)
75 CIRCLEQ_INSERT_TAIL(&q->xbq, xbuf, next);
80 struct xbee_buf *xbee_bufq_last(struct xbee_bufq *q)
82 if (CIRCLEQ_EMPTY(&q->xbq))
84 return CIRCLEQ_LAST(&q->xbq);
87 void xbee_bufq_init(struct xbee_bufq *q)
89 CIRCLEQ_INIT(&q->xbq);
94 void xbee_bufq_append(struct xbee_bufq *q, unsigned len)
96 struct xbee_buf *xbuf;
99 xbuf = CIRCLEQ_LAST(&q->xbq);
103 void xbee_bufq_flush(struct xbee_bufq *q)
105 struct xbee_buf *xbuf;
107 while (!CIRCLEQ_EMPTY(&q->xbq)) {
108 xbuf = CIRCLEQ_FIRST(&q->xbq);
109 CIRCLEQ_REMOVE(&q->xbq, xbuf, next);
116 char *xbee_bufq_data(struct xbee_bufq *q, unsigned off)
118 struct xbee_buf *xbuf;
124 CIRCLEQ_FOREACH(xbuf, &q->xbq, next) {
125 data = xbee_buf_data(xbuf, off);
134 /* drop data in front of queue */
135 int xbee_bufq_drop(struct xbee_bufq *q, unsigned len)
137 struct xbee_buf *xbuf;
142 while (!CIRCLEQ_EMPTY(&q->xbq)) {
143 xbuf = CIRCLEQ_FIRST(&q->xbq);
146 CIRCLEQ_REMOVE(&q->xbq, xbuf, next);
163 int xbee_bufq_copy(struct xbee_bufq *q, void *buf, unsigned len)
165 struct xbee_buf *xbuf;
166 unsigned dstoff = 0, copylen;
171 CIRCLEQ_FOREACH(xbuf, &q->xbq, next) {
175 memcpy(buf + dstoff, xbuf->buf + xbuf->offset, copylen);