2009-03-12 23:04:52 +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
|
|
|
* Header file for the Packet buffer (packetbuf) management
|
2009-03-12 23:04:52 +00:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
2014-11-08 00:15:42 +00:00
|
|
|
/**
|
2017-09-29 20:18:48 +00:00
|
|
|
* \addtogroup net
|
2014-11-08 00:15:42 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-09-29 20:18:48 +00:00
|
|
|
* \defgroup packetbuf Packet buffer
|
2014-11-08 00:15:42 +00:00
|
|
|
* @{
|
|
|
|
*
|
2017-06-22 16:27:04 +00:00
|
|
|
* The packetbuf module does Contiki's buffer management.
|
2014-11-08 00:15:42 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-24 15:57:08 +00:00
|
|
|
#ifndef PACKETBUF_H_
|
|
|
|
#define PACKETBUF_H_
|
2009-03-12 23:04:52 +00:00
|
|
|
|
2017-09-04 14:22:34 +00:00
|
|
|
#include "contiki.h"
|
2013-12-12 22:58:52 +00:00
|
|
|
#include "net/linkaddr.h"
|
2017-05-18 07:56:16 +00:00
|
|
|
#include "net/mac/llsec802154.h"
|
2015-09-21 20:01:01 +00:00
|
|
|
#include "net/mac/tsch/tsch-conf.h"
|
2009-03-12 23:04:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief The size of the packetbuf, in bytes
|
|
|
|
*/
|
|
|
|
#ifdef PACKETBUF_CONF_SIZE
|
|
|
|
#define PACKETBUF_SIZE PACKETBUF_CONF_SIZE
|
|
|
|
#else
|
|
|
|
#define PACKETBUF_SIZE 128
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Clear and reset the packetbuf
|
|
|
|
*
|
|
|
|
* This function clears the packetbuf and resets all
|
|
|
|
* internal state pointers (header size, header pointer,
|
|
|
|
* external data pointer). It is used before preparing a
|
|
|
|
* packet in the packetbuf.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void packetbuf_clear(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Get a pointer to the data in the packetbuf
|
|
|
|
* \return Pointer to the packetbuf data
|
|
|
|
*
|
|
|
|
* This function is used to get a pointer to the data in
|
|
|
|
* the packetbuf. The data is either stored in the packetbuf,
|
|
|
|
* or referenced to an external location.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void *packetbuf_dataptr(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Get a pointer to the header in the packetbuf, for outbound packets
|
|
|
|
* \return Pointer to the packetbuf header
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void *packetbuf_hdrptr(void);
|
|
|
|
|
|
|
|
/**
|
2014-03-16 15:53:34 +00:00
|
|
|
* \brief Get the length of the header in the packetbuf
|
2009-03-12 23:04:52 +00:00
|
|
|
* \return Length of the header in the packetbuf
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
uint8_t packetbuf_hdrlen(void);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Get the length of the data in the packetbuf
|
|
|
|
* \return Length of the data in the packetbuf
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
uint16_t packetbuf_datalen(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Get the total length of the header and data in the packetbuf
|
|
|
|
* \return Length of data and header in the packetbuf
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
uint16_t packetbuf_totlen(void);
|
|
|
|
|
2016-12-13 14:32:21 +00:00
|
|
|
/**
|
|
|
|
* \brief Get the total length of the remaining space in the packetbuf
|
|
|
|
* \return Length of the remaining space in the packetbuf
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
uint16_t packetbuf_remaininglen(void);
|
|
|
|
|
2009-03-12 23:04:52 +00:00
|
|
|
/**
|
|
|
|
* \brief Set the length of the data in the packetbuf
|
|
|
|
* \param len The length of the data
|
|
|
|
*/
|
|
|
|
void packetbuf_set_datalen(uint16_t len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Copy from external data into the packetbuf
|
|
|
|
* \param from A pointer to the data from which to copy
|
|
|
|
* \param len The size of the data to copy
|
|
|
|
* \retval The number of bytes that was copied into the packetbuf
|
|
|
|
*
|
|
|
|
* This function copies data from a pointer into the
|
|
|
|
* packetbuf. If the data that is to be copied is larger
|
|
|
|
* than the packetbuf, only the data that fits in the
|
|
|
|
* packetbuf is copied. The number of bytes that could be
|
|
|
|
* copied into the rimbuf is returned.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int packetbuf_copyfrom(const void *from, uint16_t len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Copy the entire packetbuf to an external buffer
|
|
|
|
* \param to A pointer to the buffer to which the data is to be copied
|
|
|
|
* \retval The number of bytes that was copied to the external buffer
|
|
|
|
*
|
|
|
|
* This function copies the packetbuf to an external
|
|
|
|
* buffer. Both the data portion and the header portion of
|
2015-03-24 07:28:25 +00:00
|
|
|
* the packetbuf is copied.
|
2009-03-12 23:04:52 +00:00
|
|
|
*
|
|
|
|
* The external buffer to which the packetbuf is to be
|
|
|
|
* copied must be able to accomodate at least
|
2016-02-04 11:25:47 +00:00
|
|
|
* PACKETBUF_SIZE bytes. The number of
|
2009-03-12 23:04:52 +00:00
|
|
|
* bytes that was copied to the external buffer is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int packetbuf_copyto(void *to);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Extend the header of the packetbuf, for outbound packets
|
|
|
|
* \param size The number of bytes the header should be extended
|
|
|
|
* \retval Non-zero if the header could be extended, zero otherwise
|
|
|
|
*
|
|
|
|
* This function is used to allocate extra space in the
|
|
|
|
* header portion in the packetbuf, when preparing outbound
|
|
|
|
* packets for transmission. If the function is unable to
|
|
|
|
* allocate sufficient header space, the function returns
|
|
|
|
* zero and does not allocate anything.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int packetbuf_hdralloc(int size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Reduce the header in the packetbuf, for incoming packets
|
|
|
|
* \param size The number of bytes the header should be reduced
|
|
|
|
* \retval Non-zero if the header could be reduced, zero otherwise
|
|
|
|
*
|
|
|
|
* This function is used to remove the first part of the
|
|
|
|
* header in the packetbuf, when processing incoming
|
|
|
|
* packets. If the function is unable to remove the
|
|
|
|
* requested amount of header space, the function returns
|
|
|
|
* zero and does not allocate anything.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int packetbuf_hdrreduce(int size);
|
|
|
|
|
|
|
|
/* Packet attributes stuff below: */
|
|
|
|
|
|
|
|
typedef uint16_t packetbuf_attr_t;
|
|
|
|
|
|
|
|
struct packetbuf_attr {
|
|
|
|
packetbuf_attr_t val;
|
|
|
|
};
|
|
|
|
struct packetbuf_addr {
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_t addr;
|
2009-03-12 23:04:52 +00:00
|
|
|
};
|
|
|
|
|
2010-12-16 22:41:43 +00:00
|
|
|
#define PACKETBUF_ATTR_PACKET_TYPE_DATA 0
|
|
|
|
#define PACKETBUF_ATTR_PACKET_TYPE_ACK 1
|
|
|
|
#define PACKETBUF_ATTR_PACKET_TYPE_STREAM 2
|
2011-02-08 21:20:07 +00:00
|
|
|
#define PACKETBUF_ATTR_PACKET_TYPE_STREAM_END 3
|
|
|
|
#define PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP 4
|
2009-11-02 11:58:56 +00:00
|
|
|
|
2009-03-12 23:04:52 +00:00
|
|
|
enum {
|
|
|
|
PACKETBUF_ATTR_NONE,
|
2010-02-28 09:19:43 +00:00
|
|
|
|
|
|
|
/* Scope 0 attributes: used only on the local node. */
|
2009-03-12 23:04:52 +00:00
|
|
|
PACKETBUF_ATTR_CHANNEL,
|
2010-02-28 09:19:43 +00:00
|
|
|
PACKETBUF_ATTR_NETWORK_ID,
|
2009-03-12 23:04:52 +00:00
|
|
|
PACKETBUF_ATTR_LINK_QUALITY,
|
|
|
|
PACKETBUF_ATTR_RSSI,
|
|
|
|
PACKETBUF_ATTR_TIMESTAMP,
|
2017-11-15 14:54:32 +00:00
|
|
|
PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS,
|
2010-04-08 09:46:30 +00:00
|
|
|
PACKETBUF_ATTR_MAC_SEQNO,
|
2010-04-30 07:25:51 +00:00
|
|
|
PACKETBUF_ATTR_MAC_ACK,
|
2017-01-06 21:23:58 +00:00
|
|
|
PACKETBUF_ATTR_MAC_METADATA,
|
|
|
|
PACKETBUF_ATTR_MAC_NO_SRC_ADDR,
|
|
|
|
PACKETBUF_ATTR_MAC_NO_DEST_ADDR,
|
2015-09-21 20:01:01 +00:00
|
|
|
#if TSCH_WITH_LINK_SELECTOR
|
|
|
|
PACKETBUF_ATTR_TSCH_SLOTFRAME,
|
|
|
|
PACKETBUF_ATTR_TSCH_TIMESLOT,
|
|
|
|
#endif /* TSCH_WITH_LINK_SELECTOR */
|
2016-12-13 14:32:21 +00:00
|
|
|
|
2010-02-28 09:19:43 +00:00
|
|
|
/* Scope 1 attributes: used between two neighbors only. */
|
2013-05-24 13:54:55 +00:00
|
|
|
PACKETBUF_ATTR_FRAME_TYPE,
|
2015-12-17 11:13:32 +00:00
|
|
|
#if LLSEC802154_USES_AUX_HEADER
|
2013-05-24 11:40:01 +00:00
|
|
|
PACKETBUF_ATTR_SECURITY_LEVEL,
|
2015-12-17 11:13:32 +00:00
|
|
|
#endif /* LLSEC802154_USES_AUX_HEADER */
|
2013-06-24 06:49:11 +00:00
|
|
|
#if LLSEC802154_USES_EXPLICIT_KEYS
|
|
|
|
PACKETBUF_ATTR_KEY_ID_MODE,
|
|
|
|
PACKETBUF_ATTR_KEY_INDEX,
|
|
|
|
PACKETBUF_ATTR_KEY_SOURCE_BYTES_0_1,
|
|
|
|
#endif /* LLSEC802154_USES_EXPLICIT_KEYS */
|
2016-12-13 14:32:21 +00:00
|
|
|
|
2010-02-28 09:19:43 +00:00
|
|
|
/* Scope 2 attributes: used between end-to-end nodes. */
|
|
|
|
/* These must be last */
|
2009-03-12 23:04:52 +00:00
|
|
|
PACKETBUF_ADDR_SENDER,
|
|
|
|
PACKETBUF_ADDR_RECEIVER,
|
2014-12-01 14:17:00 +00:00
|
|
|
|
2009-03-12 23:04:52 +00:00
|
|
|
PACKETBUF_ATTR_MAX
|
|
|
|
};
|
|
|
|
|
2014-12-01 14:17:00 +00:00
|
|
|
#define PACKETBUF_NUM_ADDRS 2
|
2009-03-12 23:04:52 +00:00
|
|
|
#define PACKETBUF_NUM_ATTRS (PACKETBUF_ATTR_MAX - PACKETBUF_NUM_ADDRS)
|
|
|
|
#define PACKETBUF_ADDR_FIRST PACKETBUF_ADDR_SENDER
|
|
|
|
|
2010-05-27 20:28:29 +00:00
|
|
|
#define PACKETBUF_IS_ADDR(type) ((type) >= PACKETBUF_ADDR_FIRST)
|
2009-03-12 23:04:52 +00:00
|
|
|
|
|
|
|
int packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val);
|
|
|
|
packetbuf_attr_t packetbuf_attr(uint8_t type);
|
2013-12-12 22:58:52 +00:00
|
|
|
int packetbuf_set_addr(uint8_t type, const linkaddr_t *addr);
|
|
|
|
const linkaddr_t *packetbuf_addr(uint8_t type);
|
2009-03-12 23:04:52 +00:00
|
|
|
|
2014-08-04 11:34:49 +00:00
|
|
|
/**
|
|
|
|
* \brief Checks whether the current packet is a broadcast.
|
|
|
|
* \retval 0 iff current packet is not a broadcast
|
|
|
|
*/
|
|
|
|
int packetbuf_holds_broadcast(void);
|
|
|
|
|
2009-03-12 23:04:52 +00:00
|
|
|
void packetbuf_attr_clear(void);
|
|
|
|
|
|
|
|
void packetbuf_attr_copyto(struct packetbuf_attr *attrs,
|
2016-12-13 14:32:21 +00:00
|
|
|
struct packetbuf_addr *addrs);
|
2009-03-12 23:04:52 +00:00
|
|
|
void packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
|
2016-12-13 14:32:21 +00:00
|
|
|
struct packetbuf_addr *addrs);
|
2009-03-12 23:04:52 +00:00
|
|
|
|
|
|
|
#define PACKETBUF_ATTRIBUTES(...) { __VA_ARGS__ PACKETBUF_ATTR_LAST }
|
|
|
|
#define PACKETBUF_ATTR_LAST { PACKETBUF_ATTR_NONE, 0 }
|
|
|
|
|
|
|
|
#define PACKETBUF_ATTR_BIT 1
|
|
|
|
#define PACKETBUF_ATTR_BYTE 8
|
2014-04-11 15:16:59 +00:00
|
|
|
#define PACKETBUF_ADDRSIZE (LINKADDR_SIZE * PACKETBUF_ATTR_BYTE)
|
2009-03-12 23:04:52 +00:00
|
|
|
|
|
|
|
struct packetbuf_attrlist {
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t len;
|
|
|
|
};
|
|
|
|
|
2013-11-24 15:57:08 +00:00
|
|
|
#endif /* PACKETBUF_H_ */
|
2009-03-12 23:04:52 +00:00
|
|
|
/** @} */
|
|
|
|
/** @} */
|