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.
|
|
|
|
*
|
2007-03-24 16:42:36 +00:00
|
|
|
* $Id: rimebuf.c,v 1.6 2007/03/24 16:42:36 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"
|
|
|
|
|
|
|
|
|
|
|
|
static u16_t buflen, bufptr;
|
2007-03-13 10:28:16 +00:00
|
|
|
static u8_t hdrptr;
|
|
|
|
static u8_t rimebuf[RIMEBUF_SIZE + RIMEBUF_HDR_SIZE];
|
2007-02-28 16:38:51 +00:00
|
|
|
|
|
|
|
static u8_t *rimebufptr;
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
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];
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
rimebuf_copyfrom(u8_t *from, u16_t len)
|
|
|
|
{
|
|
|
|
u16_t l;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if(rimebuf_is_reference()) {
|
|
|
|
memcpy(&rimebuf[RIMEBUF_HDR_SIZE], rimebuf_reference_ptr(),
|
|
|
|
rimebuf_datalen());
|
2007-03-14 00:30:11 +00:00
|
|
|
} else {
|
|
|
|
memcpy(&rimebuf[RIMEBUF_HDR_SIZE], &rimebuf[bufptr + RIMEBUF_HDR_SIZE],
|
|
|
|
rimebuf_datalen());
|
|
|
|
bufptr = 0;
|
2007-03-13 10:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-02-28 16:38:51 +00:00
|
|
|
int
|
|
|
|
rimebuf_copyto_hdr(u8_t *to)
|
|
|
|
{
|
2007-03-24 16:42:36 +00:00
|
|
|
#if DEBUG_LEVEL > 0
|
|
|
|
{
|
2007-02-28 16:38:51 +00:00
|
|
|
int i;
|
|
|
|
DEBUGF(0, "rimebuf_write_hdr: header:\n");
|
2007-03-13 10:28:16 +00:00
|
|
|
for(i = hdrptr; i < RIMEBUF_HDR_SIZE; ++i) {
|
|
|
|
DEBUGF(0, "0x%02x, ", rimebuf[i]);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
DEBUGF(0, "\n");
|
|
|
|
}
|
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
|
|
|
|
rimebuf_copyto(u8_t *to)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
DEBUGF(0, "rimebuf_write: header: %s\n", buffer);
|
|
|
|
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
|
|
|
}
|
|
|
|
DEBUGF(0, "rimebuf_write: data: %s\n", buffer);
|
|
|
|
}
|
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);
|
|
|
|
memcpy(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
|
2007-03-13 10:28:16 +00:00
|
|
|
rimebuf_set_datalen(u16_t len)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
DEBUGF(0, "rimebuf_set_len: len %d\n", len);
|
|
|
|
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
|
|
|
|
rimebuf_reference(void *ptr, u16_t len)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
u16_t
|
2007-03-13 10:28:16 +00:00
|
|
|
rimebuf_datalen(void)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
u8_t
|
|
|
|
rimebuf_hdrlen(void)
|
|
|
|
{
|
2007-03-13 10:28:16 +00:00
|
|
|
return RIMEBUF_HDR_SIZE - hdrptr;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
u16_t
|
|
|
|
rimebuf_totlen(void)
|
|
|
|
{
|
|
|
|
return rimebuf_hdrlen() + rimebuf_datalen();
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|