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
|
2017-06-22 16:27:04 +00:00
|
|
|
* Packet buffer (packetbuf) management
|
2007-02-28 16:38:51 +00:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
2014-11-08 00:15:42 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup packetbuf
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-02-28 16:38:51 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "contiki-net.h"
|
2010-06-14 19:19:16 +00:00
|
|
|
#include "net/packetbuf.h"
|
2016-02-03 16:09:13 +00:00
|
|
|
#include "sys/cc.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;
|
2016-02-04 11:25:47 +00:00
|
|
|
static uint8_t hdrlen;
|
2009-03-23 19:37:45 +00:00
|
|
|
|
|
|
|
/* The declarations below ensure that the packet buffer is aligned on
|
2014-12-03 18:06:00 +00: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. */
|
2016-02-04 11:25:47 +00:00
|
|
|
static uint32_t packetbuf_aligned[(PACKETBUF_SIZE + 3) / 4];
|
2009-03-24 07:14:12 +00:00
|
|
|
static uint8_t *packetbuf = (uint8_t *)packetbuf_aligned;
|
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;
|
2016-02-04 11:25:47 +00:00
|
|
|
hdrlen = 0;
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_attr_clear();
|
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();
|
2016-02-03 16:09:13 +00:00
|
|
|
l = MIN(PACKETBUF_SIZE, len);
|
2016-02-04 11:25:47 +00:00
|
|
|
memcpy(packetbuf, from, l);
|
2007-02-28 16:38:51 +00:00
|
|
|
buflen = l;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2009-03-12 23:04:52 +00:00
|
|
|
packetbuf_copyto(void *to)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2016-02-04 11:25:47 +00:00
|
|
|
if(hdrlen + buflen > PACKETBUF_SIZE) {
|
2010-05-02 14:59:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-02-04 11:25:47 +00:00
|
|
|
memcpy(to, packetbuf_hdrptr(), hdrlen);
|
|
|
|
memcpy((uint8_t *)to + hdrlen, packetbuf_dataptr(), buflen);
|
|
|
|
return hdrlen + 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
|
|
|
{
|
2016-02-04 11:25:47 +00:00
|
|
|
int16_t i;
|
|
|
|
|
|
|
|
if(size + packetbuf_totlen() > PACKETBUF_SIZE) {
|
|
|
|
return 0;
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
2016-02-04 11:25:47 +00:00
|
|
|
|
|
|
|
/* shift data to the right */
|
|
|
|
for(i = packetbuf_totlen() - 1; i >= 0; i--) {
|
|
|
|
packetbuf[i + size] = packetbuf[i];
|
|
|
|
}
|
|
|
|
hdrlen += size;
|
|
|
|
return 1;
|
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
|
|
|
{
|
2016-02-04 11:25:47 +00:00
|
|
|
return packetbuf + packetbuf_hdrlen();
|
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
|
|
|
{
|
2016-02-04 11:25:47 +00:00
|
|
|
return packetbuf;
|
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
|
|
|
{
|
2016-02-04 11:25:47 +00:00
|
|
|
return bufptr + hdrlen;
|
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
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-12-13 14:32:21 +00:00
|
|
|
uint16_t
|
|
|
|
packetbuf_remaininglen(void)
|
|
|
|
{
|
|
|
|
return PACKETBUF_SIZE - packetbuf_totlen();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
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;
|
2016-02-03 15:59:22 +00:00
|
|
|
memset(packetbuf_attrs, 0, sizeof(packetbuf_attrs));
|
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,
|
2016-12-13 14:32:21 +00:00
|
|
|
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,
|
2016-12-13 14:32:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
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].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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2014-08-04 11:34:49 +00:00
|
|
|
int
|
|
|
|
packetbuf_holds_broadcast(void)
|
|
|
|
{
|
|
|
|
return linkaddr_cmp(&packetbuf_addrs[PACKETBUF_ADDR_RECEIVER - PACKETBUF_ADDR_FIRST].addr, &linkaddr_null);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
2007-03-31 18:31:27 +00:00
|
|
|
/** @} */
|