2007-03-31 18:31:27 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup rimebuf
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
*
|
2008-06-30 19:57:10 +00:00
|
|
|
* $Id: rimebuf.c,v 1.16 2008/06/30 19:57:10 adamdunkels Exp $
|
2007-02-28 16:38:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2007-03-24 16:42:36 +00:00
|
|
|
* Rime buffer (rimebuf) management
|
2007-02-28 16:38:51 +00:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "contiki-net.h"
|
|
|
|
#include "net/rime/rimebuf.h"
|
|
|
|
#include "net/rime.h"
|
|
|
|
|
2008-02-25 02:14:34 +00:00
|
|
|
struct rimebuf_attr rimebuf_attrs[RIMEBUF_NUM_ATTRS];
|
|
|
|
struct rimebuf_addr rimebuf_addrs[RIMEBUF_NUM_ADDRS];
|
|
|
|
|
|
|
|
const char *rimebuf_attr_strings[] =
|
|
|
|
{
|
|
|
|
"RIMEBUF_ATTR_NONE",
|
|
|
|
"RIMEBUF_ATTR_CHANNEL",
|
|
|
|
"RIMEBUF_ATTR_PACKET_ID",
|
|
|
|
"RIMEBUF_ATTR_PACKET_TYPE",
|
|
|
|
"RIMEBUF_ATTR_EPACKET_ID",
|
|
|
|
"RIMEBUF_ATTR_EPACKET_TYPE",
|
|
|
|
"RIMEBUF_ATTR_HOPS",
|
|
|
|
"RIMEBUF_ATTR_TTL",
|
|
|
|
"RIMEBUF_ATTR_REXMIT",
|
|
|
|
"RIMEBUF_ATTR_MAX_REXMIT",
|
|
|
|
"RIMEBUF_ATTR_NUM_REXMIT",
|
|
|
|
"RIMEBUF_ATTR_LINK_QUALITY",
|
2008-06-30 19:57:10 +00:00
|
|
|
"RIMEBUF_ATTR_RSSI",
|
|
|
|
"RIMEBUF_ATTR_TIMESTAMP",
|
|
|
|
"RIMEBUF_ATTR_NETWORK_ID",
|
2008-02-25 02:14:34 +00:00
|
|
|
|
|
|
|
"RIMEBUF_ATTR_RELIABLE",
|
|
|
|
"RIMEBUF_ATTR_ERELIABLE",
|
|
|
|
|
|
|
|
"RIMEBUF_ADDR_SENDER",
|
|
|
|
"RIMEBUF_ADDR_RECEIVER",
|
|
|
|
"RIMEBUF_ADDR_ESENDER",
|
|
|
|
"RIMEBUF_ADDR_ERECEIVER",
|
|
|
|
|
|
|
|
"RIMEBUF_ATTR_MAX",
|
|
|
|
};
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2008-02-24 22:05:27 +00:00
|
|
|
static uint16_t buflen, bufptr;
|
|
|
|
static uint8_t hdrptr;
|
|
|
|
static uint8_t rimebuf[RIMEBUF_SIZE + RIMEBUF_HDR_SIZE];
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2008-02-24 22:05:27 +00:00
|
|
|
static uint8_t *rimebufptr;
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2007-03-29 23:18:22 +00:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2007-02-28 16:38:51 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rimebuf_clear(void)
|
|
|
|
{
|
|
|
|
buflen = bufptr = 0;
|
2007-03-13 10:28:16 +00:00
|
|
|
hdrptr = RIMEBUF_HDR_SIZE;
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2007-03-13 10:28:16 +00:00
|
|
|
rimebufptr = &rimebuf[RIMEBUF_HDR_SIZE];
|
2008-04-03 09:07:28 +00:00
|
|
|
rimebuf_attr_clear();
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2008-02-24 22:05:27 +00:00
|
|
|
rimebuf_copyfrom(const void *from, uint16_t len)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2008-02-24 22:05:27 +00:00
|
|
|
uint16_t l;
|
2007-02-28 16:38:51 +00:00
|
|
|
|
|
|
|
rimebuf_clear();
|
2007-03-13 10:28:16 +00:00
|
|
|
l = len > RIMEBUF_SIZE? RIMEBUF_SIZE: len;
|
2007-02-28 16:38:51 +00:00
|
|
|
memcpy(rimebufptr, from, l);
|
|
|
|
buflen = l;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-13 10:28:16 +00:00
|
|
|
void
|
2007-03-14 00:30:11 +00:00
|
|
|
rimebuf_compact(void)
|
2007-03-13 10:28:16 +00:00
|
|
|
{
|
2008-04-02 14:49:21 +00:00
|
|
|
int i, len;
|
|
|
|
|
2007-03-13 10:28:16 +00:00
|
|
|
if(rimebuf_is_reference()) {
|
|
|
|
memcpy(&rimebuf[RIMEBUF_HDR_SIZE], rimebuf_reference_ptr(),
|
|
|
|
rimebuf_datalen());
|
2008-04-02 14:49:21 +00:00
|
|
|
} else if (bufptr > 0) {
|
|
|
|
len = rimebuf_datalen() + RIMEBUF_HDR_SIZE;
|
|
|
|
for (i = RIMEBUF_HDR_SIZE; i < len; i++) {
|
|
|
|
rimebuf[i] = rimebuf[bufptr + i];
|
|
|
|
}
|
|
|
|
|
2007-03-14 00:30:11 +00:00
|
|
|
bufptr = 0;
|
2007-03-13 10:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-02-28 16:38:51 +00:00
|
|
|
int
|
2008-02-24 22:05:27 +00:00
|
|
|
rimebuf_copyto_hdr(uint8_t *to)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2007-03-24 16:42:36 +00:00
|
|
|
#if DEBUG_LEVEL > 0
|
|
|
|
{
|
2007-02-28 16:38:51 +00:00
|
|
|
int i;
|
2007-03-29 23:18:22 +00:00
|
|
|
PRINTF("rimebuf_write_hdr: header:\n");
|
2007-03-13 10:28:16 +00:00
|
|
|
for(i = hdrptr; i < RIMEBUF_HDR_SIZE; ++i) {
|
2007-03-29 23:18:22 +00:00
|
|
|
PRINTF("0x%02x, ", rimebuf[i]);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
2007-03-29 23:18:22 +00:00
|
|
|
PRINTF("\n");
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
2007-03-24 16:42:36 +00:00
|
|
|
#endif /* DEBUG_LEVEL */
|
2007-03-13 10:28:16 +00:00
|
|
|
memcpy(to, rimebuf + hdrptr, RIMEBUF_HDR_SIZE - hdrptr);
|
|
|
|
return RIMEBUF_HDR_SIZE - hdrptr;
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2007-11-17 18:01:00 +00:00
|
|
|
rimebuf_copyto(void *to)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2007-03-24 16:42:36 +00:00
|
|
|
#if DEBUG_LEVEL > 0
|
|
|
|
{
|
2007-02-28 16:38:51 +00:00
|
|
|
int i;
|
|
|
|
char buffer[1000];
|
|
|
|
char *bufferptr = buffer;
|
|
|
|
|
|
|
|
bufferptr[0] = 0;
|
2007-03-13 10:28:16 +00:00
|
|
|
for(i = hdrptr; i < RIMEBUF_HDR_SIZE; ++i) {
|
|
|
|
bufferptr += sprintf(bufferptr, "0x%02x, ", rimebuf[i]);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
2007-03-29 23:18:22 +00:00
|
|
|
PRINTF("rimebuf_write: header: %s\n", buffer);
|
2007-02-28 16:38:51 +00:00
|
|
|
bufferptr = buffer;
|
|
|
|
bufferptr[0] = 0;
|
|
|
|
for(i = bufptr; i < buflen + bufptr; ++i) {
|
2007-03-13 10:28:16 +00:00
|
|
|
bufferptr += sprintf(bufferptr, "0x%02x, ", rimebufptr[i]);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
2007-03-29 23:18:22 +00:00
|
|
|
PRINTF("rimebuf_write: data: %s\n", buffer);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
2007-03-24 16:42:36 +00:00
|
|
|
#endif /* DEBUG_LEVEL */
|
2007-03-13 10:28:16 +00:00
|
|
|
memcpy(to, rimebuf + hdrptr, RIMEBUF_HDR_SIZE - hdrptr);
|
2008-02-24 22:05:27 +00:00
|
|
|
memcpy((uint8_t *)to + RIMEBUF_HDR_SIZE - hdrptr, rimebufptr + bufptr,
|
2007-02-28 16:38:51 +00:00
|
|
|
buflen);
|
2007-03-13 10:28:16 +00:00
|
|
|
return RIMEBUF_HDR_SIZE - hdrptr + buflen;
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2007-03-23 10:46:35 +00:00
|
|
|
rimebuf_hdralloc(int size)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
if(hdrptr > size) {
|
|
|
|
hdrptr -= size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
hdrptr = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
rimebuf_hdrreduce(int size)
|
|
|
|
{
|
|
|
|
if(buflen < size) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufptr += size;
|
|
|
|
buflen -= size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2008-02-24 22:05:27 +00:00
|
|
|
rimebuf_set_datalen(uint16_t len)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2007-03-29 23:18:22 +00:00
|
|
|
PRINTF("rimebuf_set_len: len %d\n", len);
|
2007-02-28 16:38:51 +00:00
|
|
|
buflen = len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void *
|
|
|
|
rimebuf_dataptr(void)
|
|
|
|
{
|
2007-03-13 10:28:16 +00:00
|
|
|
return (void *)(&rimebuf[bufptr + RIMEBUF_HDR_SIZE]);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void *
|
|
|
|
rimebuf_hdrptr(void)
|
|
|
|
{
|
2007-03-13 10:28:16 +00:00
|
|
|
return (void *)(&rimebuf[hdrptr]);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2008-02-24 22:05:27 +00:00
|
|
|
rimebuf_reference(void *ptr, uint16_t len)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
rimebuf_clear();
|
|
|
|
rimebufptr = ptr;
|
|
|
|
buflen = len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
rimebuf_is_reference(void)
|
|
|
|
{
|
2007-03-13 10:28:16 +00:00
|
|
|
return rimebufptr != &rimebuf[RIMEBUF_HDR_SIZE];
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void *
|
|
|
|
rimebuf_reference_ptr(void)
|
|
|
|
{
|
|
|
|
return rimebufptr;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-24 22:05:27 +00:00
|
|
|
uint16_t
|
2007-03-13 10:28:16 +00:00
|
|
|
rimebuf_datalen(void)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-24 22:05:27 +00:00
|
|
|
uint8_t
|
2007-02-28 16:38:51 +00:00
|
|
|
rimebuf_hdrlen(void)
|
|
|
|
{
|
2007-03-13 10:28:16 +00:00
|
|
|
return RIMEBUF_HDR_SIZE - hdrptr;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-24 22:05:27 +00:00
|
|
|
uint16_t
|
2007-03-13 10:28:16 +00:00
|
|
|
rimebuf_totlen(void)
|
|
|
|
{
|
|
|
|
return rimebuf_hdrlen() + rimebuf_datalen();
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-25 02:14:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
rimebuf_attr_clear(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < RIMEBUF_NUM_ATTRS; ++i) {
|
2008-04-02 14:49:21 +00:00
|
|
|
/* rimebuf_attrs[i].type = RIMEBUF_ATTR_NONE; */
|
|
|
|
rimebuf_attrs[i].val = 0;
|
2008-02-25 02:14:34 +00:00
|
|
|
}
|
|
|
|
for(i = 0; i < RIMEBUF_NUM_ADDRS; ++i) {
|
2008-04-02 14:49:21 +00:00
|
|
|
/* rimebuf_addrs[i].type = RIMEBUF_ATTR_NONE; */
|
2008-03-20 09:40:31 +00:00
|
|
|
rimeaddr_copy(&rimebuf_addrs[i].addr, &rimeaddr_null);
|
2008-02-25 02:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rimebuf_attr_copyto(struct rimebuf_attr *attrs,
|
|
|
|
struct rimebuf_addr *addrs)
|
|
|
|
{
|
|
|
|
memcpy(attrs, rimebuf_attrs, sizeof(rimebuf_attrs));
|
|
|
|
memcpy(addrs, rimebuf_addrs, sizeof(rimebuf_addrs));
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rimebuf_attr_copyfrom(struct rimebuf_attr *attrs,
|
|
|
|
struct rimebuf_addr *addrs)
|
|
|
|
{
|
|
|
|
memcpy(rimebuf_attrs, attrs, sizeof(rimebuf_attrs));
|
|
|
|
memcpy(rimebuf_addrs, addrs, sizeof(rimebuf_addrs));
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if !RIMEBUF_CONF_ATTRS_INLINE
|
|
|
|
int
|
|
|
|
rimebuf_set_attr(uint8_t type, const rimebuf_attr_t val)
|
|
|
|
{
|
2008-04-02 14:49:21 +00:00
|
|
|
/* rimebuf_attrs[type].type = type; */
|
2008-02-25 02:14:34 +00:00
|
|
|
rimebuf_attrs[type].val = val;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rimebuf_attr_t
|
|
|
|
rimebuf_attr(uint8_t type)
|
|
|
|
{
|
|
|
|
return rimebuf_attrs[type].val;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
rimebuf_set_addr(uint8_t type, const rimeaddr_t *addr)
|
|
|
|
{
|
2008-04-02 14:49:21 +00:00
|
|
|
/* rimebuf_addrs[type - RIMEBUF_ADDR_FIRST].type = type; */
|
|
|
|
rimeaddr_copy(&rimebuf_addrs[type - RIMEBUF_ADDR_FIRST].addr, addr);
|
2008-02-25 02:14:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const rimeaddr_t *
|
|
|
|
rimebuf_addr(uint8_t type)
|
|
|
|
{
|
2008-04-02 14:49:21 +00:00
|
|
|
return &rimebuf_addrs[type - RIMEBUF_ADDR_FIRST].addr;
|
2008-02-25 02:14:34 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#endif /* RIMEBUF_CONF_ATTRS_INLINE */
|
2007-03-31 18:31:27 +00:00
|
|
|
/** @} */
|