From fb8fab739aa2986a58d6298c37263409c328cefa Mon Sep 17 00:00:00 2001 From: adamdunkels Date: Thu, 12 Mar 2009 23:04:52 +0000 Subject: [PATCH] Renamed the rimebuf module to packetbuf to signal that the module is used outside of a pure Rime context (e.g., the sicslowpan code uses it). --- core/net/rime/{rimebuf.c => packetbuf.c} | 224 ++++++------ core/net/rime/packetbuf.h | 422 +++++++++++++++++++++++ core/net/rime/rimebuf.h | 422 ----------------------- 3 files changed, 534 insertions(+), 534 deletions(-) rename core/net/rime/{rimebuf.c => packetbuf.c} (51%) create mode 100644 core/net/rime/packetbuf.h delete mode 100644 core/net/rime/rimebuf.h diff --git a/core/net/rime/rimebuf.c b/core/net/rime/packetbuf.c similarity index 51% rename from core/net/rime/rimebuf.c rename to core/net/rime/packetbuf.c index 63a294088..c476cb008 100644 --- a/core/net/rime/rimebuf.c +++ b/core/net/rime/packetbuf.c @@ -1,5 +1,5 @@ /** - * \addtogroup rimebuf + * \addtogroup packetbuf * @{ */ @@ -33,12 +33,12 @@ * * This file is part of the Contiki operating system. * - * $Id: rimebuf.c,v 1.18 2009/03/11 20:33:17 adamdunkels Exp $ + * $Id: packetbuf.c,v 1.1 2009/03/12 23:04:52 adamdunkels Exp $ */ /** * \file - * Rime buffer (rimebuf) management + * Rime buffer (packetbuf) management * \author * Adam Dunkels */ @@ -46,51 +46,51 @@ #include #include "contiki-net.h" -#include "net/rime/rimebuf.h" +#include "net/rime/packetbuf.h" #include "net/rime.h" -struct rimebuf_attr rimebuf_attrs[RIMEBUF_NUM_ATTRS]; -struct rimebuf_addr rimebuf_addrs[RIMEBUF_NUM_ADDRS]; +struct packetbuf_attr packetbuf_attrs[PACKETBUF_NUM_ATTRS]; +struct packetbuf_addr packetbuf_addrs[PACKETBUF_NUM_ADDRS]; -const char *rimebuf_attr_strings[] = +const char *packetbuf_attr_strings[] = { - "RIMEBUF_ATTR_NONE", - "RIMEBUF_ATTR_CHANNEL", - "RIMEBUF_ATTR_PACKET_ID", - "RIMEBUF_ATTR_PACKET_TYPE", - "RIMEBUF_ATTR_EPACKET_ID", - "RIMEBUF_ATTR_EPACKET_TYPE", - "RIMEBUF_ATTR_HOPS", - "RIMEBUF_ATTR_TTL", - "RIMEBUF_ATTR_REXMIT", - "RIMEBUF_ATTR_MAX_REXMIT", - "RIMEBUF_ATTR_NUM_REXMIT", - "RIMEBUF_ATTR_LINK_QUALITY", - "RIMEBUF_ATTR_RSSI", - "RIMEBUF_ATTR_TIMESTAMP", - "RIMEBUF_ATTR_RADIO_TXPOWER", + "PACKETBUF_ATTR_NONE", + "PACKETBUF_ATTR_CHANNEL", + "PACKETBUF_ATTR_PACKET_ID", + "PACKETBUF_ATTR_PACKET_TYPE", + "PACKETBUF_ATTR_EPACKET_ID", + "PACKETBUF_ATTR_EPACKET_TYPE", + "PACKETBUF_ATTR_HOPS", + "PACKETBUF_ATTR_TTL", + "PACKETBUF_ATTR_REXMIT", + "PACKETBUF_ATTR_MAX_REXMIT", + "PACKETBUF_ATTR_NUM_REXMIT", + "PACKETBUF_ATTR_LINK_QUALITY", + "PACKETBUF_ATTR_RSSI", + "PACKETBUF_ATTR_TIMESTAMP", + "PACKETBUF_ATTR_RADIO_TXPOWER", - "RIMEBUF_ATTR_LISTEN_ENERGY", - "RIMEBUF_ATTR_TRANSMIT_ENERGY", + "PACKETBUF_ATTR_LISTEN_ENERGY", + "PACKETBUF_ATTR_TRANSMIT_ENERGY", - "RIMEBUF_ATTR_NETWORK_ID", + "PACKETBUF_ATTR_NETWORK_ID", - "RIMEBUF_ATTR_RELIABLE", - "RIMEBUF_ATTR_ERELIABLE", + "PACKETBUF_ATTR_RELIABLE", + "PACKETBUF_ATTR_ERELIABLE", - "RIMEBUF_ADDR_SENDER", - "RIMEBUF_ADDR_RECEIVER", - "RIMEBUF_ADDR_ESENDER", - "RIMEBUF_ADDR_ERECEIVER", + "PACKETBUF_ADDR_SENDER", + "PACKETBUF_ADDR_RECEIVER", + "PACKETBUF_ADDR_ESENDER", + "PACKETBUF_ADDR_ERECEIVER", - "RIMEBUF_ATTR_MAX", + "PACKETBUF_ATTR_MAX", }; static uint16_t buflen, bufptr; static uint8_t hdrptr; -static uint8_t rimebuf[RIMEBUF_SIZE + RIMEBUF_HDR_SIZE]; +static uint8_t packetbuf[PACKETBUF_SIZE + PACKETBUF_HDR_SIZE]; -static uint8_t *rimebufptr; +static uint8_t *packetbufptr; #define DEBUG 0 #if DEBUG @@ -102,39 +102,39 @@ static uint8_t *rimebufptr; /*---------------------------------------------------------------------------*/ void -rimebuf_clear(void) +packetbuf_clear(void) { buflen = bufptr = 0; - hdrptr = RIMEBUF_HDR_SIZE; + hdrptr = PACKETBUF_HDR_SIZE; - rimebufptr = &rimebuf[RIMEBUF_HDR_SIZE]; - rimebuf_attr_clear(); + packetbufptr = &packetbuf[PACKETBUF_HDR_SIZE]; + packetbuf_attr_clear(); } /*---------------------------------------------------------------------------*/ int -rimebuf_copyfrom(const void *from, uint16_t len) +packetbuf_copyfrom(const void *from, uint16_t len) { uint16_t l; - rimebuf_clear(); - l = len > RIMEBUF_SIZE? RIMEBUF_SIZE: len; - memcpy(rimebufptr, from, l); + packetbuf_clear(); + l = len > PACKETBUF_SIZE? PACKETBUF_SIZE: len; + memcpy(packetbufptr, from, l); buflen = l; return l; } /*---------------------------------------------------------------------------*/ void -rimebuf_compact(void) +packetbuf_compact(void) { int i, len; - if(rimebuf_is_reference()) { - memcpy(&rimebuf[RIMEBUF_HDR_SIZE], rimebuf_reference_ptr(), - rimebuf_datalen()); + if(packetbuf_is_reference()) { + memcpy(&packetbuf[PACKETBUF_HDR_SIZE], packetbuf_reference_ptr(), + packetbuf_datalen()); } else if (bufptr > 0) { - len = rimebuf_datalen() + RIMEBUF_HDR_SIZE; - for (i = RIMEBUF_HDR_SIZE; i < len; i++) { - rimebuf[i] = rimebuf[bufptr + i]; + len = packetbuf_datalen() + PACKETBUF_HDR_SIZE; + for (i = PACKETBUF_HDR_SIZE; i < len; i++) { + packetbuf[i] = packetbuf[bufptr + i]; } bufptr = 0; @@ -142,24 +142,24 @@ rimebuf_compact(void) } /*---------------------------------------------------------------------------*/ int -rimebuf_copyto_hdr(uint8_t *to) +packetbuf_copyto_hdr(uint8_t *to) { #if DEBUG_LEVEL > 0 { int i; - PRINTF("rimebuf_write_hdr: header:\n"); - for(i = hdrptr; i < RIMEBUF_HDR_SIZE; ++i) { - PRINTF("0x%02x, ", rimebuf[i]); + PRINTF("packetbuf_write_hdr: header:\n"); + for(i = hdrptr; i < PACKETBUF_HDR_SIZE; ++i) { + PRINTF("0x%02x, ", packetbuf[i]); } PRINTF("\n"); } #endif /* DEBUG_LEVEL */ - memcpy(to, rimebuf + hdrptr, RIMEBUF_HDR_SIZE - hdrptr); - return RIMEBUF_HDR_SIZE - hdrptr; + memcpy(to, packetbuf + hdrptr, PACKETBUF_HDR_SIZE - hdrptr); + return PACKETBUF_HDR_SIZE - hdrptr; } /*---------------------------------------------------------------------------*/ int -rimebuf_copyto(void *to) +packetbuf_copyto(void *to) { #if DEBUG_LEVEL > 0 { @@ -168,26 +168,26 @@ rimebuf_copyto(void *to) char *bufferptr = buffer; bufferptr[0] = 0; - for(i = hdrptr; i < RIMEBUF_HDR_SIZE; ++i) { - bufferptr += sprintf(bufferptr, "0x%02x, ", rimebuf[i]); + for(i = hdrptr; i < PACKETBUF_HDR_SIZE; ++i) { + bufferptr += sprintf(bufferptr, "0x%02x, ", packetbuf[i]); } - PRINTF("rimebuf_write: header: %s\n", buffer); + PRINTF("packetbuf_write: header: %s\n", buffer); bufferptr = buffer; bufferptr[0] = 0; for(i = bufptr; i < buflen + bufptr; ++i) { - bufferptr += sprintf(bufferptr, "0x%02x, ", rimebufptr[i]); + bufferptr += sprintf(bufferptr, "0x%02x, ", packetbufptr[i]); } - PRINTF("rimebuf_write: data: %s\n", buffer); + PRINTF("packetbuf_write: data: %s\n", buffer); } #endif /* DEBUG_LEVEL */ - memcpy(to, rimebuf + hdrptr, RIMEBUF_HDR_SIZE - hdrptr); - memcpy((uint8_t *)to + RIMEBUF_HDR_SIZE - hdrptr, rimebufptr + bufptr, + memcpy(to, packetbuf + hdrptr, PACKETBUF_HDR_SIZE - hdrptr); + memcpy((uint8_t *)to + PACKETBUF_HDR_SIZE - hdrptr, packetbufptr + bufptr, buflen); - return RIMEBUF_HDR_SIZE - hdrptr + buflen; + return PACKETBUF_HDR_SIZE - hdrptr + buflen; } /*---------------------------------------------------------------------------*/ int -rimebuf_hdralloc(int size) +packetbuf_hdralloc(int size) { if(hdrptr > size) { hdrptr -= size; @@ -198,7 +198,7 @@ rimebuf_hdralloc(int size) } /*---------------------------------------------------------------------------*/ int -rimebuf_hdrreduce(int size) +packetbuf_hdrreduce(int size) { if(buflen < size) { return 0; @@ -210,123 +210,123 @@ rimebuf_hdrreduce(int size) } /*---------------------------------------------------------------------------*/ void -rimebuf_set_datalen(uint16_t len) +packetbuf_set_datalen(uint16_t len) { - PRINTF("rimebuf_set_len: len %d\n", len); + PRINTF("packetbuf_set_len: len %d\n", len); buflen = len; } /*---------------------------------------------------------------------------*/ void * -rimebuf_dataptr(void) +packetbuf_dataptr(void) { - return (void *)(&rimebuf[bufptr + RIMEBUF_HDR_SIZE]); + return (void *)(&packetbuf[bufptr + PACKETBUF_HDR_SIZE]); } /*---------------------------------------------------------------------------*/ void * -rimebuf_hdrptr(void) +packetbuf_hdrptr(void) { - return (void *)(&rimebuf[hdrptr]); + return (void *)(&packetbuf[hdrptr]); } /*---------------------------------------------------------------------------*/ void -rimebuf_reference(void *ptr, uint16_t len) +packetbuf_reference(void *ptr, uint16_t len) { - rimebuf_clear(); - rimebufptr = ptr; + packetbuf_clear(); + packetbufptr = ptr; buflen = len; } /*---------------------------------------------------------------------------*/ int -rimebuf_is_reference(void) +packetbuf_is_reference(void) { - return rimebufptr != &rimebuf[RIMEBUF_HDR_SIZE]; + return packetbufptr != &packetbuf[PACKETBUF_HDR_SIZE]; } /*---------------------------------------------------------------------------*/ void * -rimebuf_reference_ptr(void) +packetbuf_reference_ptr(void) { - return rimebufptr; + return packetbufptr; } /*---------------------------------------------------------------------------*/ uint16_t -rimebuf_datalen(void) +packetbuf_datalen(void) { return buflen; } /*---------------------------------------------------------------------------*/ uint8_t -rimebuf_hdrlen(void) +packetbuf_hdrlen(void) { - return RIMEBUF_HDR_SIZE - hdrptr; + return PACKETBUF_HDR_SIZE - hdrptr; } /*---------------------------------------------------------------------------*/ uint16_t -rimebuf_totlen(void) +packetbuf_totlen(void) { - return rimebuf_hdrlen() + rimebuf_datalen(); + return packetbuf_hdrlen() + packetbuf_datalen(); } /*---------------------------------------------------------------------------*/ void -rimebuf_attr_clear(void) +packetbuf_attr_clear(void) { int i; - for(i = 0; i < RIMEBUF_NUM_ATTRS; ++i) { -/* rimebuf_attrs[i].type = RIMEBUF_ATTR_NONE; */ - rimebuf_attrs[i].val = 0; + for(i = 0; i < PACKETBUF_NUM_ATTRS; ++i) { +/* packetbuf_attrs[i].type = PACKETBUF_ATTR_NONE; */ + packetbuf_attrs[i].val = 0; } - for(i = 0; i < RIMEBUF_NUM_ADDRS; ++i) { -/* rimebuf_addrs[i].type = RIMEBUF_ATTR_NONE; */ - rimeaddr_copy(&rimebuf_addrs[i].addr, &rimeaddr_null); + for(i = 0; i < PACKETBUF_NUM_ADDRS; ++i) { +/* packetbuf_addrs[i].type = PACKETBUF_ATTR_NONE; */ + rimeaddr_copy(&packetbuf_addrs[i].addr, &rimeaddr_null); } } /*---------------------------------------------------------------------------*/ void -rimebuf_attr_copyto(struct rimebuf_attr *attrs, - struct rimebuf_addr *addrs) +packetbuf_attr_copyto(struct packetbuf_attr *attrs, + struct packetbuf_addr *addrs) { - memcpy(attrs, rimebuf_attrs, sizeof(rimebuf_attrs)); - memcpy(addrs, rimebuf_addrs, sizeof(rimebuf_addrs)); + memcpy(attrs, packetbuf_attrs, sizeof(packetbuf_attrs)); + memcpy(addrs, packetbuf_addrs, sizeof(packetbuf_addrs)); } /*---------------------------------------------------------------------------*/ void -rimebuf_attr_copyfrom(struct rimebuf_attr *attrs, - struct rimebuf_addr *addrs) +packetbuf_attr_copyfrom(struct packetbuf_attr *attrs, + struct packetbuf_addr *addrs) { - memcpy(rimebuf_attrs, attrs, sizeof(rimebuf_attrs)); - memcpy(rimebuf_addrs, addrs, sizeof(rimebuf_addrs)); + memcpy(packetbuf_attrs, attrs, sizeof(packetbuf_attrs)); + memcpy(packetbuf_addrs, addrs, sizeof(packetbuf_addrs)); } /*---------------------------------------------------------------------------*/ -#if !RIMEBUF_CONF_ATTRS_INLINE +#if !PACKETBUF_CONF_ATTRS_INLINE int -rimebuf_set_attr(uint8_t type, const rimebuf_attr_t val) +packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val) { -/* rimebuf_attrs[type].type = type; */ - rimebuf_attrs[type].val = val; +/* packetbuf_attrs[type].type = type; */ + packetbuf_attrs[type].val = val; return 1; } /*---------------------------------------------------------------------------*/ -rimebuf_attr_t -rimebuf_attr(uint8_t type) +packetbuf_attr_t +packetbuf_attr(uint8_t type) { - return rimebuf_attrs[type].val; + return packetbuf_attrs[type].val; } /*---------------------------------------------------------------------------*/ int -rimebuf_set_addr(uint8_t type, const rimeaddr_t *addr) +packetbuf_set_addr(uint8_t type, const rimeaddr_t *addr) { -/* rimebuf_addrs[type - RIMEBUF_ADDR_FIRST].type = type; */ - rimeaddr_copy(&rimebuf_addrs[type - RIMEBUF_ADDR_FIRST].addr, addr); +/* packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].type = type; */ + rimeaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr); return 1; } /*---------------------------------------------------------------------------*/ const rimeaddr_t * -rimebuf_addr(uint8_t type) +packetbuf_addr(uint8_t type) { - return &rimebuf_addrs[type - RIMEBUF_ADDR_FIRST].addr; + return &packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr; } /*---------------------------------------------------------------------------*/ -#endif /* RIMEBUF_CONF_ATTRS_INLINE */ +#endif /* PACKETBUF_CONF_ATTRS_INLINE */ /** @} */ diff --git a/core/net/rime/packetbuf.h b/core/net/rime/packetbuf.h new file mode 100644 index 000000000..9d32a5273 --- /dev/null +++ b/core/net/rime/packetbuf.h @@ -0,0 +1,422 @@ +/** + * \addtogroup rime + * @{ + */ + +/** + * \defgroup packetbuf Rime buffer management + * @{ + * + * The packetbuf module does Rime's buffer management. + */ + +/* + * 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. + * + * $Id: packetbuf.h,v 1.1 2009/03/12 23:04:52 adamdunkels Exp $ + */ + +/** + * \file + * Header file for the Rime buffer (packetbuf) management + * \author + * Adam Dunkels + */ + +#ifndef __PACKETBUF_H__ +#define __PACKETBUF_H__ + +#include "contiki-conf.h" +#include "net/rime/rimeaddr.h" + +/** + * \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 The size of the packetbuf header, in bytes + */ +#ifdef PACKETBUF_CONF_HDR_SIZE +#define PACKETBUF_HDR_SIZE PACKETBUF_CONF_HDR_SIZE +#else +#define PACKETBUF_HDR_SIZE 32 +#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. + * + * For outbound packets, the packetbuf consists of two + * parts: header and data. The header is accessed with the + * packetbuf_hdrptr() function. + * + * For incoming packets, both the packet header and the + * packet data is stored in the data portion of the + * packetbuf. Thus this function is used to get a pointer to + * the header for incoming packets. + * + */ +void *packetbuf_dataptr(void); + +/** + * \brief Get a pointer to the header in the packetbuf, for outbound packets + * \return Pointer to the packetbuf header + * + * For outbound packets, the packetbuf consists of two + * parts: header and data. This function is used to get a + * pointer to the header in the packetbuf. The header is + * stored in the packetbuf. + * + */ +void *packetbuf_hdrptr(void); + +/** + * \brief Get the length of the header in the packetbuf, for outbound packets + * \return Length of the header in the packetbuf + * + * For outbound packets, the packetbuf consists of two + * parts: header and data. This function is used to get + * the length of the header in the packetbuf. The header is + * stored in the packetbuf and accessed via the + * packetbuf_hdrptr() function. + * + */ +uint8_t packetbuf_hdrlen(void); + + +/** + * \brief Get the length of the data in the packetbuf + * \return Length of the data in the packetbuf + * + * For outbound packets, the packetbuf consists of two + * parts: header and data. This function is used to get + * the length of the data in the packetbuf. The data is + * stored in the packetbuf and accessed via the + * packetbuf_dataptr() function. + * + * For incoming packets, both the packet header and the + * packet data is stored in the data portion of the + * packetbuf. This function is then used to get the total + * length of the packet - both header and data. + * + */ +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); + +/** + * \brief Set the length of the data in the packetbuf + * \param len The length of the data + * + * For outbound packets, the packetbuf consists of two + * parts: header and data. This function is used to set + * the length of the data in the packetbuf. + */ +void packetbuf_set_datalen(uint16_t len); + +/** + * \brief Point the packetbuf to external data + * \param ptr A pointer to the external data + * \param len The length of the external data + * + * For outbound packets, the packetbuf consists of two + * parts: header and data. This function is used to make + * the packetbuf point to external data. The function also + * specifies the length of the external data that the + * packetbuf references. + */ +void packetbuf_reference(void *ptr, uint16_t len); + +/** + * \brief Check if the packetbuf references external data + * \retval Non-zero if the packetbuf references external data, zero otherwise. + * + * For outbound packets, the packetbuf consists of two + * parts: header and data. This function is used to check + * if the packetbuf points to external data that has + * previously been referenced with packetbuf_reference(). + * + */ +int packetbuf_is_reference(void); + +/** + * \brief Get a pointer to external data referenced by the packetbuf + * \retval A pointer to the external data + * + * For outbound packets, the packetbuf consists of two + * parts: header and data. The data may point to external + * data that has previously been referenced with + * packetbuf_reference(). This function is used to get a + * pointer to the external data. + * + */ +void *packetbuf_reference_ptr(void); + +/** + * \brief Compact the packetbuf + * + * This function compacts the packetbuf by copying the data + * portion of the packetbuf so that becomes consecutive to + * the header. It also copies external data that has + * previously been referenced with packetbuf_reference() + * into the packetbuf. + * + * This function is called by the Rime code before a + * packet is to be sent by a device driver. This assures + * that the entire packet is consecutive in memory. + * + */ +void packetbuf_compact(void); + +/** + * \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 + * the packetbuf is copied. If the packetbuf referenced + * external data (referenced with packetbuf_reference()) the + * external data is copied. + * + * The external buffer to which the packetbuf is to be + * copied must be able to accomodate at least + * (PACKETBUF_SIZE + PACKETBUF_HDR_SIZE) bytes. The number of + * bytes that was copied to the external buffer is + * returned. + * + */ +int packetbuf_copyto(void *to); + +/** + * \brief Copy the header portion of the 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 header portion of the packetbuf + * to an external buffer. + * + * The external buffer to which the packetbuf is to be + * copied must be able to accomodate at least + * PACKETBUF_HDR_SIZE bytes. The number of bytes that was + * copied to the external buffer is returned. + * + */ +int packetbuf_copyto_hdr(uint8_t *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 { +/* uint8_t type; */ + packetbuf_attr_t val; +}; +struct packetbuf_addr { +/* uint8_t type; */ + rimeaddr_t addr; +}; + +extern const char *packetbuf_attr_strings[]; + +#define PACKETBUF_ATTR_PACKET_TYPE_DATA 0 +#define PACKETBUF_ATTR_PACKET_TYPE_ACK 1 +enum { + PACKETBUF_ATTR_NONE, + PACKETBUF_ATTR_CHANNEL, + PACKETBUF_ATTR_PACKET_ID, + PACKETBUF_ATTR_PACKET_TYPE, + PACKETBUF_ATTR_EPACKET_ID, + PACKETBUF_ATTR_EPACKET_TYPE, + PACKETBUF_ATTR_HOPS, + PACKETBUF_ATTR_TTL, + PACKETBUF_ATTR_REXMIT, + PACKETBUF_ATTR_MAX_REXMIT, + PACKETBUF_ATTR_NUM_REXMIT, + PACKETBUF_ATTR_LINK_QUALITY, + PACKETBUF_ATTR_RSSI, + PACKETBUF_ATTR_TIMESTAMP, + PACKETBUF_ATTR_RADIO_TXPOWER, + + PACKETBUF_ATTR_LISTEN_TIME, + PACKETBUF_ATTR_TRANSMIT_TIME, + + PACKETBUF_ATTR_NETWORK_ID, + + PACKETBUF_ATTR_RELIABLE, + PACKETBUF_ATTR_ERELIABLE, + + PACKETBUF_ADDR_SENDER, + PACKETBUF_ADDR_RECEIVER, + PACKETBUF_ADDR_ESENDER, + PACKETBUF_ADDR_ERECEIVER, + + PACKETBUF_ATTR_MAX +}; + +#define PACKETBUF_NUM_ADDRS 4 +#define PACKETBUF_NUM_ATTRS (PACKETBUF_ATTR_MAX - PACKETBUF_NUM_ADDRS) +#define PACKETBUF_ADDR_FIRST PACKETBUF_ADDR_SENDER + + +#if PACKETBUF_CONF_ATTRS_INLINE + +extern struct packetbuf_attr packetbuf_attrs[]; +extern struct packetbuf_addr packetbuf_addrs[]; + +static int packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val); +static packetbuf_attr_t packetbuf_attr(uint8_t type); +static int packetbuf_set_addr(uint8_t type, const rimeaddr_t *addr); +static const rimeaddr_t *packetbuf_addr(uint8_t type); + +static inline int +packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val) +{ +/* packetbuf_attrs[type].type = type; */ + packetbuf_attrs[type].val = val; + return 1; +} +static inline packetbuf_attr_t +packetbuf_attr(uint8_t type) +{ + return packetbuf_attrs[type].val; +} + +static inline int +packetbuf_set_addr(uint8_t type, const rimeaddr_t *addr) +{ +/* packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].type = type; */ + rimeaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr); + return 1; +} + +static inline const rimeaddr_t * +packetbuf_addr(uint8_t type) +{ + return &packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr; +} +#else /* PACKETBUF_CONF_ATTRS_INLINE */ +int packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val); +packetbuf_attr_t packetbuf_attr(uint8_t type); +int packetbuf_set_addr(uint8_t type, const rimeaddr_t *addr); +const rimeaddr_t *packetbuf_addr(uint8_t type); +#endif /* PACKETBUF_CONF_ATTRS_INLINE */ + +void packetbuf_attr_clear(void); +int packetbuf_attr_isset(uint8_t type); + +void packetbuf_attr_copyto(struct packetbuf_attr *attrs, + struct packetbuf_addr *addrs); +void packetbuf_attr_copyfrom(struct packetbuf_attr *attrs, + struct packetbuf_addr *addrs); + +#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 +#define PACKETBUF_ADDRSIZE (sizeof(rimeaddr_t) * PACKETBUF_ATTR_BYTE) + +struct packetbuf_attrlist { + uint8_t type; + uint8_t len; +}; + +#endif /* __PACKETBUF_H__ */ +/** @} */ +/** @} */ diff --git a/core/net/rime/rimebuf.h b/core/net/rime/rimebuf.h deleted file mode 100644 index 9d47a0385..000000000 --- a/core/net/rime/rimebuf.h +++ /dev/null @@ -1,422 +0,0 @@ -/** - * \addtogroup rime - * @{ - */ - -/** - * \defgroup rimebuf Rime buffer management - * @{ - * - * The rimebuf module does Rime's buffer management. - */ - -/* - * 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. - * - * $Id: rimebuf.h,v 1.19 2009/03/11 20:33:18 adamdunkels Exp $ - */ - -/** - * \file - * Header file for the Rime buffer (rimebuf) management - * \author - * Adam Dunkels - */ - -#ifndef __RIMEBUF_H__ -#define __RIMEBUF_H__ - -#include "contiki-conf.h" -#include "net/rime/rimeaddr.h" - -/** - * \brief The size of the rimebuf, in bytes - */ -#ifdef RIMEBUF_CONF_SIZE -#define RIMEBUF_SIZE RIMEBUF_CONF_SIZE -#else -#define RIMEBUF_SIZE 128 -#endif - -/** - * \brief The size of the rimebuf header, in bytes - */ -#ifdef RIMEBUF_CONF_HDR_SIZE -#define RIMEBUF_HDR_SIZE RIMEBUF_CONF_HDR_SIZE -#else -#define RIMEBUF_HDR_SIZE 32 -#endif - -/** - * \brief Clear and reset the rimebuf - * - * This function clears the rimebuf and resets all - * internal state pointers (header size, header pointer, - * external data pointer). It is used before preparing a - * packet in the rimebuf. - * - */ -void rimebuf_clear(void); - -/** - * \brief Get a pointer to the data in the rimebuf - * \return Pointer to the rimebuf data - * - * This function is used to get a pointer to the data in - * the rimebuf. The data is either stored in the rimebuf, - * or referenced to an external location. - * - * For outbound packets, the rimebuf consists of two - * parts: header and data. The header is accessed with the - * rimebuf_hdrptr() function. - * - * For incoming packets, both the packet header and the - * packet data is stored in the data portion of the - * rimebuf. Thus this function is used to get a pointer to - * the header for incoming packets. - * - */ -void *rimebuf_dataptr(void); - -/** - * \brief Get a pointer to the header in the rimebuf, for outbound packets - * \return Pointer to the rimebuf header - * - * For outbound packets, the rimebuf consists of two - * parts: header and data. This function is used to get a - * pointer to the header in the rimebuf. The header is - * stored in the rimebuf. - * - */ -void *rimebuf_hdrptr(void); - -/** - * \brief Get the length of the header in the rimebuf, for outbound packets - * \return Length of the header in the rimebuf - * - * For outbound packets, the rimebuf consists of two - * parts: header and data. This function is used to get - * the length of the header in the rimebuf. The header is - * stored in the rimebuf and accessed via the - * rimebuf_hdrptr() function. - * - */ -uint8_t rimebuf_hdrlen(void); - - -/** - * \brief Get the length of the data in the rimebuf - * \return Length of the data in the rimebuf - * - * For outbound packets, the rimebuf consists of two - * parts: header and data. This function is used to get - * the length of the data in the rimebuf. The data is - * stored in the rimebuf and accessed via the - * rimebuf_dataptr() function. - * - * For incoming packets, both the packet header and the - * packet data is stored in the data portion of the - * rimebuf. This function is then used to get the total - * length of the packet - both header and data. - * - */ -uint16_t rimebuf_datalen(void); - -/** - * \brief Get the total length of the header and data in the rimebuf - * \return Length of data and header in the rimebuf - * - */ -uint16_t rimebuf_totlen(void); - -/** - * \brief Set the length of the data in the rimebuf - * \param len The length of the data - * - * For outbound packets, the rimebuf consists of two - * parts: header and data. This function is used to set - * the length of the data in the rimebuf. - */ -void rimebuf_set_datalen(uint16_t len); - -/** - * \brief Point the rimebuf to external data - * \param ptr A pointer to the external data - * \param len The length of the external data - * - * For outbound packets, the rimebuf consists of two - * parts: header and data. This function is used to make - * the rimebuf point to external data. The function also - * specifies the length of the external data that the - * rimebuf references. - */ -void rimebuf_reference(void *ptr, uint16_t len); - -/** - * \brief Check if the rimebuf references external data - * \retval Non-zero if the rimebuf references external data, zero otherwise. - * - * For outbound packets, the rimebuf consists of two - * parts: header and data. This function is used to check - * if the rimebuf points to external data that has - * previously been referenced with rimebuf_reference(). - * - */ -int rimebuf_is_reference(void); - -/** - * \brief Get a pointer to external data referenced by the rimebuf - * \retval A pointer to the external data - * - * For outbound packets, the rimebuf consists of two - * parts: header and data. The data may point to external - * data that has previously been referenced with - * rimebuf_reference(). This function is used to get a - * pointer to the external data. - * - */ -void *rimebuf_reference_ptr(void); - -/** - * \brief Compact the rimebuf - * - * This function compacts the rimebuf by copying the data - * portion of the rimebuf so that becomes consecutive to - * the header. It also copies external data that has - * previously been referenced with rimebuf_reference() - * into the rimebuf. - * - * This function is called by the Rime code before a - * packet is to be sent by a device driver. This assures - * that the entire packet is consecutive in memory. - * - */ -void rimebuf_compact(void); - -/** - * \brief Copy from external data into the rimebuf - * \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 rimebuf - * - * This function copies data from a pointer into the - * rimebuf. If the data that is to be copied is larger - * than the rimebuf, only the data that fits in the - * rimebuf is copied. The number of bytes that could be - * copied into the rimbuf is returned. - * - */ -int rimebuf_copyfrom(const void *from, uint16_t len); - -/** - * \brief Copy the entire rimebuf 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 rimebuf to an external - * buffer. Both the data portion and the header portion of - * the rimebuf is copied. If the rimebuf referenced - * external data (referenced with rimebuf_reference()) the - * external data is copied. - * - * The external buffer to which the rimebuf is to be - * copied must be able to accomodate at least - * (RIMEBUF_SIZE + RIMEBUF_HDR_SIZE) bytes. The number of - * bytes that was copied to the external buffer is - * returned. - * - */ -int rimebuf_copyto(void *to); - -/** - * \brief Copy the header portion of the rimebuf 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 header portion of the rimebuf - * to an external buffer. - * - * The external buffer to which the rimebuf is to be - * copied must be able to accomodate at least - * RIMEBUF_HDR_SIZE bytes. The number of bytes that was - * copied to the external buffer is returned. - * - */ -int rimebuf_copyto_hdr(uint8_t *to); - -/** - * \brief Extend the header of the rimebuf, 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 rimebuf, 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 rimebuf_hdralloc(int size); - -/** - * \brief Reduce the header in the rimebuf, 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 rimebuf, 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 rimebuf_hdrreduce(int size); - -/* Packet attributes stuff below: */ - -typedef uint16_t rimebuf_attr_t; - -struct rimebuf_attr { -/* uint8_t type; */ - rimebuf_attr_t val; -}; -struct rimebuf_addr { -/* uint8_t type; */ - rimeaddr_t addr; -}; - -extern const char *rimebuf_attr_strings[]; - -#define RIMEBUF_ATTR_PACKET_TYPE_DATA 0 -#define RIMEBUF_ATTR_PACKET_TYPE_ACK 1 -enum { - RIMEBUF_ATTR_NONE, - RIMEBUF_ATTR_CHANNEL, - RIMEBUF_ATTR_PACKET_ID, - RIMEBUF_ATTR_PACKET_TYPE, - RIMEBUF_ATTR_EPACKET_ID, - RIMEBUF_ATTR_EPACKET_TYPE, - RIMEBUF_ATTR_HOPS, - RIMEBUF_ATTR_TTL, - RIMEBUF_ATTR_REXMIT, - RIMEBUF_ATTR_MAX_REXMIT, - RIMEBUF_ATTR_NUM_REXMIT, - RIMEBUF_ATTR_LINK_QUALITY, - RIMEBUF_ATTR_RSSI, - RIMEBUF_ATTR_TIMESTAMP, - RIMEBUF_ATTR_RADIO_TXPOWER, - - RIMEBUF_ATTR_LISTEN_TIME, - RIMEBUF_ATTR_TRANSMIT_TIME, - - RIMEBUF_ATTR_NETWORK_ID, - - RIMEBUF_ATTR_RELIABLE, - RIMEBUF_ATTR_ERELIABLE, - - RIMEBUF_ADDR_SENDER, - RIMEBUF_ADDR_RECEIVER, - RIMEBUF_ADDR_ESENDER, - RIMEBUF_ADDR_ERECEIVER, - - RIMEBUF_ATTR_MAX -}; - -#define RIMEBUF_NUM_ADDRS 4 -#define RIMEBUF_NUM_ATTRS (RIMEBUF_ATTR_MAX - RIMEBUF_NUM_ADDRS) -#define RIMEBUF_ADDR_FIRST RIMEBUF_ADDR_SENDER - - -#if RIMEBUF_CONF_ATTRS_INLINE - -extern struct rimebuf_attr rimebuf_attrs[]; -extern struct rimebuf_addr rimebuf_addrs[]; - -static int rimebuf_set_attr(uint8_t type, const rimebuf_attr_t val); -static rimebuf_attr_t rimebuf_attr(uint8_t type); -static int rimebuf_set_addr(uint8_t type, const rimeaddr_t *addr); -static const rimeaddr_t *rimebuf_addr(uint8_t type); - -static inline int -rimebuf_set_attr(uint8_t type, const rimebuf_attr_t val) -{ -/* rimebuf_attrs[type].type = type; */ - rimebuf_attrs[type].val = val; - return 1; -} -static inline rimebuf_attr_t -rimebuf_attr(uint8_t type) -{ - return rimebuf_attrs[type].val; -} - -static inline int -rimebuf_set_addr(uint8_t type, const rimeaddr_t *addr) -{ -/* rimebuf_addrs[type - RIMEBUF_ADDR_FIRST].type = type; */ - rimeaddr_copy(&rimebuf_addrs[type - RIMEBUF_ADDR_FIRST].addr, addr); - return 1; -} - -static inline const rimeaddr_t * -rimebuf_addr(uint8_t type) -{ - return &rimebuf_addrs[type - RIMEBUF_ADDR_FIRST].addr; -} -#else /* RIMEBUF_CONF_ATTRS_INLINE */ -int rimebuf_set_attr(uint8_t type, const rimebuf_attr_t val); -rimebuf_attr_t rimebuf_attr(uint8_t type); -int rimebuf_set_addr(uint8_t type, const rimeaddr_t *addr); -const rimeaddr_t *rimebuf_addr(uint8_t type); -#endif /* RIMEBUF_CONF_ATTRS_INLINE */ - -void rimebuf_attr_clear(void); -int rimebuf_attr_isset(uint8_t type); - -void rimebuf_attr_copyto(struct rimebuf_attr *attrs, - struct rimebuf_addr *addrs); -void rimebuf_attr_copyfrom(struct rimebuf_attr *attrs, - struct rimebuf_addr *addrs); - -#define RIMEBUF_ATTRIBUTES(...) { __VA_ARGS__ RIMEBUF_ATTR_LAST } -#define RIMEBUF_ATTR_LAST { RIMEBUF_ATTR_NONE, 0 } - -#define RIMEBUF_ATTR_BIT 1 -#define RIMEBUF_ATTR_BYTE 8 -#define RIMEBUF_ADDRSIZE (sizeof(rimeaddr_t) * RIMEBUF_ATTR_BYTE) - -struct rimebuf_attrlist { - uint8_t type; - uint8_t len; -}; - -#endif /* __RIMEBUF_H__ */ -/** @} */ -/** @} */