+++ /dev/null
-/*
- * Copyright (c) 2011, Olivier MATZ <zer0@droids-corp.org>
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the University of California, Berkeley nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <sys/queue.h>
-
-#include "xbee_neighbor.h"
-#include "xbee_stats.h"
-#include "xbee_buf.h"
-#include "xbee.h"
-
-struct xbee_buf *xbee_buf_alloc(void)
-{
- struct xbee_buf *xbuf;
-
- xbuf = malloc(sizeof(*xbuf));
- if (xbuf == NULL)
- return NULL;
- memset(xbuf, 0, sizeof(*xbuf));
- xbuf->offset = 0;
- xbuf->len = 0;
- return xbuf;
-}
-
-int xbee_buf_tailroom(struct xbee_buf *xbuf)
-{
- return XBEE_BUF_SIZE - xbuf->len - xbuf->offset;
-}
-
-char *xbee_buf_data(struct xbee_buf *xbuf, unsigned off)
-{
- if (off >= xbuf->len)
- return NULL;
- return xbuf->buf + xbuf->offset + off;
-}
-
-char *xbee_buf_head(struct xbee_buf *xbuf)
-{
- return xbuf->buf + xbuf->offset;
-}
-
-char *xbee_buf_tail(struct xbee_buf *xbuf)
-{
- return xbuf->buf + xbuf->offset + xbuf->len;
-}
-
-void xbee_buf_enqueue(struct xbee_bufq *q, struct xbee_buf *xbuf)
-{
- CIRCLEQ_INSERT_TAIL(&q->xbq, xbuf, next);
- q->len += xbuf->len;
- q->nseg++;
-}
-
-struct xbee_buf *xbee_bufq_last(struct xbee_bufq *q)
-{
- if (CIRCLEQ_EMPTY(&q->xbq))
- return NULL;
- return CIRCLEQ_LAST(&q->xbq);
-}
-
-void xbee_bufq_init(struct xbee_bufq *q)
-{
- CIRCLEQ_INIT(&q->xbq);
- q->len = 0;
- q->nseg = 0;
-}
-
-void xbee_bufq_append(struct xbee_bufq *q, unsigned len)
-{
- struct xbee_buf *xbuf;
-
- q->len += len;
- xbuf = CIRCLEQ_LAST(&q->xbq);
- xbuf->len += len;
-}
-
-void xbee_bufq_flush(struct xbee_bufq *q)
-{
- struct xbee_buf *xbuf;
-
- while (!CIRCLEQ_EMPTY(&q->xbq)) {
- xbuf = CIRCLEQ_FIRST(&q->xbq);
- CIRCLEQ_REMOVE(&q->xbq, xbuf, next);
- q->nseg --;
- q->len -= xbuf->len;
- free(xbuf);
- }
-}
-
-char *xbee_bufq_data(struct xbee_bufq *q, unsigned off)
-{
- struct xbee_buf *xbuf;
- char *data = NULL;
-
- if (off >= q->len)
- return NULL;
-
- CIRCLEQ_FOREACH(xbuf, &q->xbq, next) {
- data = xbee_buf_data(xbuf, off);
- if (data != NULL)
- return data;
- off -= xbuf->len;
- }
-
- return data;
-}
-
-/* drop data in front of queue */
-int xbee_bufq_drop(struct xbee_bufq *q, unsigned len)
-{
- struct xbee_buf *xbuf;
-
- if (len > q->len)
- return -1;
-
- while (!CIRCLEQ_EMPTY(&q->xbq)) {
- xbuf = CIRCLEQ_FIRST(&q->xbq);
- if (xbuf->len > len)
- break;
- CIRCLEQ_REMOVE(&q->xbq, xbuf, next);
- len -= xbuf->len;
- q->nseg --;
- q->len -= xbuf->len;
- free(xbuf);
- xbuf = NULL;
- }
-
- if (xbuf != NULL) {
- xbuf->len -= len;
- xbuf->offset += len;
- q->len -= len;
- }
-
- return 0;
-}
-
-int xbee_bufq_copy(struct xbee_bufq *q, void *buf, unsigned len)
-{
- struct xbee_buf *xbuf;
- unsigned dstoff = 0, copylen;
-
- if (len > q->len)
- return -1;
-
- CIRCLEQ_FOREACH(xbuf, &q->xbq, next) {
- copylen = len;
- if (xbuf->len < len)
- copylen = xbuf->len;
- memcpy(buf + dstoff, xbuf->buf + xbuf->offset, copylen);
- len -= copylen;
- if (len == 0)
- break;
- dstoff += copylen;
- }
-
- return 0;
-}
-