2007-03-31 18:31:27 +00:00
|
|
|
/**
|
2009-03-12 23:04:52 +00:00
|
|
|
* \addtogroup packetbuf
|
2007-03-31 18:31:27 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2009-03-12 23:04:52 +00:00
|
|
|
* Rime buffer (packetbuf) management
|
2007-02-28 16:38:51 +00:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "contiki-net.h"
|
2010-06-14 19:19:16 +00:00
|
|
|
#include "net/packetbuf.h"
|
2013-12-12 19:50:07 +00:00
|
|
|
#include "net/rime/rime.h"
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2009-03-12 23:04:52 +00:00
|
|
|
struct packetbuf_attr packetbuf_attrs[PACKETBUF_NUM_ATTRS];
|
|
|
|
struct packetbuf_addr packetbuf_addrs[PACKETBUF_NUM_ADDRS];
|
2008-02-25 02:14:34 +00:00
|
|
|
|
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;
|
2009-03-23 19:37:45 +00:00
|
|
|
|
|
|
|
/* The declarations below ensure that the packet buffer is aligned on
|
|
|
|
an even 16-bit boundary. On some platforms (most notably the
|
|
|
|
msp430), having apotentially misaligned packet buffer may lead to
|
|
|
|
problems when accessing 16-bit values. */
|
|
|
|
static uint16_t packetbuf_aligned[(PACKETBUF_SIZE + PACKETBUF_HDR_SIZE) / 2 + 1];
|
2009-03-24 07:14:12 +00:00
|
|
|
static uint8_t *packetbuf = (uint8_t *)packetbuf_aligned;
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2009-03-12 23:04:52 +00:00
|
|
|
static uint8_t *packetbufptr;
|
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
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_clear(void)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
buflen = bufptr = 0;
|
2009-03-12 23:04:52 +00:00
|
|
|
hdrptr = PACKETBUF_HDR_SIZE;
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbufptr = &packetbuf[PACKETBUF_HDR_SIZE];
|
|
|
|
packetbuf_attr_clear();
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-02-06 07:48:52 +00:00
|
|
|
void
|
|
|
|
packetbuf_clear_hdr(void)
|
|
|
|
{
|
|
|
|
hdrptr = PACKETBUF_HDR_SIZE;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-02-28 16:38:51 +00:00
|
|
|
int
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_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
|
|
|
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_clear();
|
|
|
|
l = len > PACKETBUF_SIZE? PACKETBUF_SIZE: len;
|
|
|
|
memcpy(packetbufptr, from, l);
|
2007-02-28 16:38:51 +00:00
|
|
|
buflen = l;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-13 10:28:16 +00:00
|
|
|
void
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_compact(void)
|
2007-03-13 10:28:16 +00:00
|
|
|
{
|
2008-04-02 14:49:21 +00:00
|
|
|
int i, len;
|
|
|
|
|
2009-03-12 23:04:52 +00:00
|
|
|
if(packetbuf_is_reference()) {
|
|
|
|
memcpy(&packetbuf[PACKETBUF_HDR_SIZE], packetbuf_reference_ptr(),
|
|
|
|
packetbuf_datalen());
|
2012-11-26 18:47:11 +00:00
|
|
|
} else if(bufptr > 0) {
|
2009-03-12 23:04:52 +00:00
|
|
|
len = packetbuf_datalen() + PACKETBUF_HDR_SIZE;
|
2010-03-29 21:53:04 +00:00
|
|
|
for(i = PACKETBUF_HDR_SIZE; i < len; i++) {
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf[i] = packetbuf[bufptr + i];
|
2008-04-02 14:49:21 +00:00
|
|
|
}
|
|
|
|
|
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
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_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;
|
2009-03-12 23:04:52 +00:00
|
|
|
PRINTF("packetbuf_write_hdr: header:\n");
|
|
|
|
for(i = hdrptr; i < PACKETBUF_HDR_SIZE; ++i) {
|
|
|
|
PRINTF("0x%02x, ", packetbuf[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 */
|
2009-03-12 23:04:52 +00:00
|
|
|
memcpy(to, packetbuf + hdrptr, PACKETBUF_HDR_SIZE - hdrptr);
|
|
|
|
return PACKETBUF_HDR_SIZE - hdrptr;
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_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;
|
2009-03-12 23:04:52 +00:00
|
|
|
for(i = hdrptr; i < PACKETBUF_HDR_SIZE; ++i) {
|
|
|
|
bufferptr += sprintf(bufferptr, "0x%02x, ", packetbuf[i]);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
2009-03-12 23:04:52 +00:00
|
|
|
PRINTF("packetbuf_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) {
|
2009-03-12 23:04:52 +00:00
|
|
|
bufferptr += sprintf(bufferptr, "0x%02x, ", packetbufptr[i]);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
2009-03-12 23:04:52 +00:00
|
|
|
PRINTF("packetbuf_write: data: %s\n", buffer);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
2007-03-24 16:42:36 +00:00
|
|
|
#endif /* DEBUG_LEVEL */
|
2010-05-02 14:59:11 +00:00
|
|
|
if(PACKETBUF_HDR_SIZE - hdrptr + buflen > PACKETBUF_SIZE) {
|
|
|
|
/* Too large packet */
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-12 23:04:52 +00:00
|
|
|
memcpy(to, packetbuf + hdrptr, PACKETBUF_HDR_SIZE - hdrptr);
|
|
|
|
memcpy((uint8_t *)to + PACKETBUF_HDR_SIZE - hdrptr, packetbufptr + bufptr,
|
2007-02-28 16:38:51 +00:00
|
|
|
buflen);
|
2009-03-12 23:04:52 +00:00
|
|
|
return PACKETBUF_HDR_SIZE - hdrptr + buflen;
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_hdralloc(int size)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2010-05-02 14:59:11 +00:00
|
|
|
if(hdrptr >= size && packetbuf_totlen() + size <= PACKETBUF_SIZE) {
|
2007-02-28 16:38:51 +00:00
|
|
|
hdrptr -= size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-03-29 21:53:04 +00:00
|
|
|
void
|
|
|
|
packetbuf_hdr_remove(int size)
|
|
|
|
{
|
|
|
|
hdrptr += size;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-02-28 16:38:51 +00:00
|
|
|
int
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_hdrreduce(int size)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
if(buflen < size) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-03-29 21:53:04 +00:00
|
|
|
|
2007-02-28 16:38:51 +00:00
|
|
|
bufptr += size;
|
|
|
|
buflen -= size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_set_datalen(uint16_t len)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
PRINTF("packetbuf_set_len: len %d\n", len);
|
2007-02-28 16:38:51 +00:00
|
|
|
buflen = len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void *
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_dataptr(void)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
return (void *)(&packetbuf[bufptr + PACKETBUF_HDR_SIZE]);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void *
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_hdrptr(void)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
return (void *)(&packetbuf[hdrptr]);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_reference(void *ptr, uint16_t len)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_clear();
|
|
|
|
packetbufptr = ptr;
|
2007-02-28 16:38:51 +00:00
|
|
|
buflen = len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_is_reference(void)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
return packetbufptr != &packetbuf[PACKETBUF_HDR_SIZE];
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void *
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_reference_ptr(void)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
return packetbufptr;
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-24 22:05:27 +00:00
|
|
|
uint16_t
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_datalen(void)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-24 22:05:27 +00:00
|
|
|
uint8_t
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_hdrlen(void)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2014-03-16 15:53:34 +00:00
|
|
|
uint8_t hdrlen;
|
|
|
|
|
|
|
|
hdrlen = PACKETBUF_HDR_SIZE - hdrptr;
|
|
|
|
if(hdrlen) {
|
|
|
|
/* outbound packet */
|
|
|
|
return hdrlen;
|
|
|
|
} else {
|
|
|
|
/* inbound packet */
|
|
|
|
return bufptr;
|
|
|
|
}
|
2007-03-13 10:28:16 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-24 22:05:27 +00:00
|
|
|
uint16_t
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_totlen(void)
|
2007-03-13 10:28:16 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
return packetbuf_hdrlen() + packetbuf_datalen();
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-25 02:14:34 +00:00
|
|
|
void
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_attr_clear(void)
|
2008-02-25 02:14:34 +00:00
|
|
|
{
|
|
|
|
int i;
|
2009-03-12 23:04:52 +00:00
|
|
|
for(i = 0; i < PACKETBUF_NUM_ATTRS; ++i) {
|
|
|
|
packetbuf_attrs[i].val = 0;
|
2008-02-25 02:14:34 +00:00
|
|
|
}
|
2009-03-12 23:04:52 +00:00
|
|
|
for(i = 0; i < PACKETBUF_NUM_ADDRS; ++i) {
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_copy(&packetbuf_addrs[i].addr, &linkaddr_null);
|
2008-02-25 02:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_attr_copyto(struct packetbuf_attr *attrs,
|
|
|
|
struct packetbuf_addr *addrs)
|
2008-02-25 02:14:34 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
memcpy(attrs, packetbuf_attrs, sizeof(packetbuf_attrs));
|
|
|
|
memcpy(addrs, packetbuf_addrs, sizeof(packetbuf_addrs));
|
2008-02-25 02:14:34 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
|
|
|
|
struct packetbuf_addr *addrs)
|
2008-02-25 02:14:34 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
memcpy(packetbuf_attrs, attrs, sizeof(packetbuf_attrs));
|
|
|
|
memcpy(packetbuf_addrs, addrs, sizeof(packetbuf_addrs));
|
2008-02-25 02:14:34 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-03-12 23:04:52 +00:00
|
|
|
#if !PACKETBUF_CONF_ATTRS_INLINE
|
2008-02-25 02:14:34 +00:00
|
|
|
int
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
|
2008-02-25 02:14:34 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
/* packetbuf_attrs[type].type = type; */
|
|
|
|
packetbuf_attrs[type].val = val;
|
2008-02-25 02:14:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_attr_t
|
|
|
|
packetbuf_attr(uint8_t type)
|
2008-02-25 02:14:34 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
return packetbuf_attrs[type].val;
|
2008-02-25 02:14:34 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2013-12-12 22:58:52 +00:00
|
|
|
packetbuf_set_addr(uint8_t type, const linkaddr_t *addr)
|
2008-02-25 02:14:34 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
/* packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].type = type; */
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr);
|
2008-02-25 02:14:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-12-12 22:58:52 +00:00
|
|
|
const linkaddr_t *
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_addr(uint8_t type)
|
2008-02-25 02:14:34 +00:00
|
|
|
{
|
2009-03-12 23:04:52 +00:00
|
|
|
return &packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr;
|
2008-02-25 02:14:34 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-03-12 23:04:52 +00:00
|
|
|
#endif /* PACKETBUF_CONF_ATTRS_INLINE */
|
2007-03-31 18:31:27 +00:00
|
|
|
/** @} */
|