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