2007-03-31 18:31:27 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup rimequeuebuf
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-02-28 16:38:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. 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.
|
|
|
|
* 3. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE OR 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2010-11-25 08:43:59 +00:00
|
|
|
* $Id: queuebuf.c,v 1.5 2010/11/25 08:43:59 adamdunkels Exp $
|
2007-02-28 16:38:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2007-03-29 23:18:47 +00:00
|
|
|
* Implementation of the Rime queue buffers
|
2007-02-28 16:38:51 +00:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
2007-05-15 08:09:21 +00:00
|
|
|
#include "contiki-net.h"
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2007-03-15 09:56:15 +00:00
|
|
|
#include <string.h> /* for memcpy() */
|
|
|
|
|
2007-03-29 22:22:20 +00:00
|
|
|
#ifdef QUEUEBUF_CONF_REF_NUM
|
|
|
|
#define QUEUEBUF_REF_NUM QUEUEBUF_CONF_REF_NUM
|
|
|
|
#else
|
2007-02-28 16:38:51 +00:00
|
|
|
#define QUEUEBUF_REF_NUM 2
|
2007-03-29 22:22:20 +00:00
|
|
|
#endif
|
2007-02-28 16:38:51 +00:00
|
|
|
|
|
|
|
struct queuebuf {
|
2010-11-25 08:43:59 +00:00
|
|
|
#if QUEUEBUF_DEBUG
|
|
|
|
struct queuebuf *next;
|
|
|
|
const char *file;
|
|
|
|
int line;
|
|
|
|
clock_time_t time;
|
|
|
|
#endif /* QUEUEBUF_DEBUG */
|
2008-02-24 22:05:27 +00:00
|
|
|
uint16_t len;
|
2010-04-30 07:29:08 +00:00
|
|
|
uint8_t data[PACKETBUF_SIZE];
|
2009-03-12 21:58:20 +00:00
|
|
|
struct packetbuf_attr attrs[PACKETBUF_NUM_ATTRS];
|
|
|
|
struct packetbuf_addr addrs[PACKETBUF_NUM_ADDRS];
|
2007-02-28 16:38:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct queuebuf_ref {
|
2008-02-24 22:05:27 +00:00
|
|
|
uint16_t len;
|
|
|
|
uint8_t *ref;
|
2009-03-12 21:58:20 +00:00
|
|
|
uint8_t hdr[PACKETBUF_HDR_SIZE];
|
2008-02-24 22:05:27 +00:00
|
|
|
uint8_t hdrlen;
|
2007-02-28 16:38:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MEMB(bufmem, struct queuebuf, QUEUEBUF_NUM);
|
|
|
|
MEMB(refbufmem, struct queuebuf_ref, QUEUEBUF_REF_NUM);
|
2007-03-31 18:31:27 +00:00
|
|
|
|
2010-11-25 08:43:59 +00:00
|
|
|
#if QUEUEBUF_DEBUG
|
|
|
|
#include "lib/list.h"
|
|
|
|
LIST(queuebuf_list);
|
|
|
|
#endif /* QUEUEBUF_DEBUG */
|
|
|
|
|
2007-03-31 18:31:27 +00:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2010-02-08 21:10:32 +00:00
|
|
|
#ifdef QUEUEBUF_CONF_STATS
|
|
|
|
#define QUEUEBUF_STATS QUEUEBUF_CONF_STATS
|
2010-06-24 09:48:21 +00:00
|
|
|
#else
|
|
|
|
#define QUEUEBUF_STATS 0
|
2010-02-08 21:10:32 +00:00
|
|
|
#endif /* QUEUEBUF_CONF_STATS */
|
|
|
|
|
2007-05-15 08:09:21 +00:00
|
|
|
#if QUEUEBUF_STATS
|
2008-02-24 22:05:27 +00:00
|
|
|
uint8_t queuebuf_len, queuebuf_ref_len, queuebuf_max_len;
|
2007-05-15 08:09:21 +00:00
|
|
|
#endif /* QUEUEBUF_STATS */
|
|
|
|
|
2007-02-28 16:38:51 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
queuebuf_init(void)
|
|
|
|
{
|
|
|
|
memb_init(&bufmem);
|
|
|
|
memb_init(&refbufmem);
|
2007-05-15 08:09:21 +00:00
|
|
|
#if QUEUEBUF_STATS
|
|
|
|
queuebuf_max_len = QUEUEBUF_NUM;
|
|
|
|
#endif /* QUEUEBUF_STATS */
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-11-25 08:43:59 +00:00
|
|
|
#if QUEUEBUF_DEBUG
|
|
|
|
struct queuebuf *
|
|
|
|
queuebuf_new_from_packetbuf_debug(const char *file, int line)
|
|
|
|
#else /* QUEUEBUF_DEBUG */
|
2007-02-28 16:38:51 +00:00
|
|
|
struct queuebuf *
|
2009-03-12 21:58:20 +00:00
|
|
|
queuebuf_new_from_packetbuf(void)
|
2010-11-25 08:43:59 +00:00
|
|
|
#endif /* QUEUEBUF_DEBUG */
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
struct queuebuf *buf;
|
|
|
|
struct queuebuf_ref *rbuf;
|
|
|
|
|
2009-03-12 21:58:20 +00:00
|
|
|
if(packetbuf_is_reference()) {
|
2007-02-28 16:38:51 +00:00
|
|
|
rbuf = memb_alloc(&refbufmem);
|
|
|
|
if(rbuf != NULL) {
|
2007-05-15 08:09:21 +00:00
|
|
|
#if QUEUEBUF_STATS
|
|
|
|
++queuebuf_ref_len;
|
|
|
|
#endif /* QUEUEBUF_STATS */
|
2009-03-12 21:58:20 +00:00
|
|
|
rbuf->len = packetbuf_datalen();
|
|
|
|
rbuf->ref = packetbuf_reference_ptr();
|
|
|
|
rbuf->hdrlen = packetbuf_copyto_hdr(rbuf->hdr);
|
2007-03-31 18:31:27 +00:00
|
|
|
} else {
|
2009-03-12 21:58:20 +00:00
|
|
|
PRINTF("queuebuf_new_from_packetbuf: could not allocate a reference queuebuf\n");
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
return (struct queuebuf *)rbuf;
|
|
|
|
} else {
|
|
|
|
buf = memb_alloc(&bufmem);
|
|
|
|
if(buf != NULL) {
|
2010-11-25 08:43:59 +00:00
|
|
|
#if QUEUEBUF_DEBUG
|
|
|
|
list_add(queuebuf_list, buf);
|
|
|
|
buf->file = file;
|
|
|
|
buf->line = line;
|
|
|
|
buf->time = clock_time();
|
|
|
|
#endif /* QUEUEBUF_DEBUG */
|
2007-05-15 08:09:21 +00:00
|
|
|
#if QUEUEBUF_STATS
|
|
|
|
++queuebuf_len;
|
2010-04-30 07:29:08 +00:00
|
|
|
PRINTF("queuebuf len %d\n", queuebuf_len);
|
2010-10-04 20:26:01 +00:00
|
|
|
printf("#A q=%d\n", queuebuf_len);
|
2007-05-15 08:09:21 +00:00
|
|
|
if(queuebuf_len == queuebuf_max_len + 1) {
|
|
|
|
memb_free(&bufmem, buf);
|
|
|
|
queuebuf_len--;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* QUEUEBUF_STATS */
|
2009-03-12 21:58:20 +00:00
|
|
|
buf->len = packetbuf_copyto(buf->data);
|
|
|
|
packetbuf_attr_copyto(buf->attrs, buf->addrs);
|
2007-03-31 18:31:27 +00:00
|
|
|
} else {
|
2009-03-12 21:58:20 +00:00
|
|
|
PRINTF("queuebuf_new_from_packetbuf: could not allocate a queuebuf\n");
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2011-06-15 14:10:07 +00:00
|
|
|
queuebuf_update_attr_from_packetbuf(struct queuebuf *buf)
|
|
|
|
{
|
|
|
|
packetbuf_attr_copyto(buf->attrs, buf->addrs);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2007-02-28 16:38:51 +00:00
|
|
|
queuebuf_free(struct queuebuf *buf)
|
|
|
|
{
|
|
|
|
if(memb_inmemb(&bufmem, buf)) {
|
|
|
|
memb_free(&bufmem, buf);
|
2007-05-15 08:09:21 +00:00
|
|
|
#if QUEUEBUF_STATS
|
|
|
|
--queuebuf_len;
|
2010-10-04 20:26:01 +00:00
|
|
|
printf("#A q=%d\n", queuebuf_len);
|
2007-05-15 08:09:21 +00:00
|
|
|
#endif /* QUEUEBUF_STATS */
|
2010-11-25 08:43:59 +00:00
|
|
|
#if QUEUEBUF_DEBUG
|
|
|
|
list_remove(queuebuf_list, buf);
|
|
|
|
#endif /* QUEUEBUF_DEBUG */
|
2007-02-28 16:38:51 +00:00
|
|
|
} else if(memb_inmemb(&refbufmem, buf)) {
|
|
|
|
memb_free(&refbufmem, buf);
|
2007-05-15 08:09:21 +00:00
|
|
|
#if QUEUEBUF_STATS
|
|
|
|
--queuebuf_ref_len;
|
|
|
|
#endif /* QUEUEBUF_STATS */
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2009-03-12 21:58:20 +00:00
|
|
|
queuebuf_to_packetbuf(struct queuebuf *b)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
struct queuebuf_ref *r;
|
2010-04-30 07:29:08 +00:00
|
|
|
|
2007-02-28 16:38:51 +00:00
|
|
|
if(memb_inmemb(&bufmem, b)) {
|
2009-03-12 21:58:20 +00:00
|
|
|
packetbuf_copyfrom(b->data, b->len);
|
|
|
|
packetbuf_attr_copyfrom(b->attrs, b->addrs);
|
2007-02-28 16:38:51 +00:00
|
|
|
} else if(memb_inmemb(&refbufmem, b)) {
|
|
|
|
r = (struct queuebuf_ref *)b;
|
2009-03-12 21:58:20 +00:00
|
|
|
packetbuf_clear();
|
|
|
|
packetbuf_copyfrom(r->ref, r->len);
|
|
|
|
packetbuf_hdralloc(r->hdrlen);
|
|
|
|
memcpy(packetbuf_hdrptr(), r->hdr, r->hdrlen);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-20 12:26:23 +00:00
|
|
|
void *
|
|
|
|
queuebuf_dataptr(struct queuebuf *b)
|
|
|
|
{
|
|
|
|
struct queuebuf_ref *r;
|
|
|
|
|
|
|
|
if(memb_inmemb(&bufmem, b)) {
|
|
|
|
return b->data;
|
|
|
|
} else if(memb_inmemb(&refbufmem, b)) {
|
|
|
|
r = (struct queuebuf_ref *)b;
|
|
|
|
return r->ref;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
queuebuf_datalen(struct queuebuf *b)
|
|
|
|
{
|
|
|
|
return b->len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-06-14 06:55:26 +00:00
|
|
|
rimeaddr_t *
|
2010-04-30 07:29:08 +00:00
|
|
|
queuebuf_addr(struct queuebuf *b, uint8_t type)
|
|
|
|
{
|
2010-05-02 12:21:34 +00:00
|
|
|
return &b->addrs[type - PACKETBUF_ADDR_FIRST].addr;
|
2010-04-30 07:29:08 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-10-04 20:26:01 +00:00
|
|
|
packetbuf_attr_t
|
|
|
|
queuebuf_attr(struct queuebuf *b, uint8_t type)
|
|
|
|
{
|
|
|
|
return b->attrs[type].val;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-11-25 08:43:59 +00:00
|
|
|
void
|
|
|
|
queuebuf_debug_print(void)
|
|
|
|
{
|
|
|
|
#if QUEUEBUF_DEBUG
|
|
|
|
struct queuebuf *q;
|
|
|
|
printf("queuebuf_list: ");
|
|
|
|
for(q = list_head(queuebuf_list); q != NULL;
|
|
|
|
q = list_item_next(q)) {
|
|
|
|
printf("%s,%d,%lu ", q->file, q->line, q->time);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
#endif /* QUEUEBUF_DEBUG */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-31 18:31:27 +00:00
|
|
|
/** @} */
|