Merge pull request #1422 from kkrentz/packetbuf-cleanup

packetbuf cleanup
This commit is contained in:
Simon Duquennoy 2016-04-19 17:23:47 +02:00
commit ba72f0a131
6 changed files with 31 additions and 187 deletions

View File

@ -47,23 +47,22 @@
#include "contiki-net.h" #include "contiki-net.h"
#include "net/packetbuf.h" #include "net/packetbuf.h"
#include "net/rime/rime.h" #include "net/rime/rime.h"
#include "sys/cc.h"
struct packetbuf_attr packetbuf_attrs[PACKETBUF_NUM_ATTRS]; struct packetbuf_attr packetbuf_attrs[PACKETBUF_NUM_ATTRS];
struct packetbuf_addr packetbuf_addrs[PACKETBUF_NUM_ADDRS]; struct packetbuf_addr packetbuf_addrs[PACKETBUF_NUM_ADDRS];
static uint16_t buflen, bufptr; static uint16_t buflen, bufptr;
static uint8_t hdrptr; static uint8_t hdrlen;
/* The declarations below ensure that the packet buffer is aligned on /* The declarations below ensure that the packet buffer is aligned on
an even 32-bit boundary. On some platforms (most notably the an even 32-bit boundary. On some platforms (most notably the
msp430 or OpenRISC), having a potentially misaligned packet buffer may lead to msp430 or OpenRISC), having a potentially misaligned packet buffer may lead to
problems when accessing words. */ problems when accessing words. */
static uint32_t packetbuf_aligned[(PACKETBUF_SIZE + PACKETBUF_HDR_SIZE + 3) / 4]; static uint32_t packetbuf_aligned[(PACKETBUF_SIZE + 3) / 4];
static uint8_t *packetbuf = (uint8_t *)packetbuf_aligned; static uint8_t *packetbuf = (uint8_t *)packetbuf_aligned;
static uint8_t *packetbufptr;
#define DEBUG 0 #define DEBUG 0
#if DEBUG #if DEBUG
#include <stdio.h> #include <stdio.h>
@ -77,26 +76,19 @@ void
packetbuf_clear(void) packetbuf_clear(void)
{ {
buflen = bufptr = 0; buflen = bufptr = 0;
hdrptr = PACKETBUF_HDR_SIZE; hdrlen = 0;
packetbufptr = &packetbuf[PACKETBUF_HDR_SIZE];
packetbuf_attr_clear(); packetbuf_attr_clear();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void
packetbuf_clear_hdr(void)
{
hdrptr = PACKETBUF_HDR_SIZE;
}
/*---------------------------------------------------------------------------*/
int int
packetbuf_copyfrom(const void *from, uint16_t len) packetbuf_copyfrom(const void *from, uint16_t len)
{ {
uint16_t l; uint16_t l;
packetbuf_clear(); packetbuf_clear();
l = len > PACKETBUF_SIZE? PACKETBUF_SIZE: len; l = MIN(PACKETBUF_SIZE, len);
memcpy(packetbufptr, from, l); memcpy(packetbuf, from, l);
buflen = l; buflen = l;
return l; return l;
} }
@ -104,82 +96,43 @@ packetbuf_copyfrom(const void *from, uint16_t len)
void void
packetbuf_compact(void) packetbuf_compact(void)
{ {
int i, len; int16_t i;
if(bufptr > 0) { if(bufptr) {
len = packetbuf_datalen() + PACKETBUF_HDR_SIZE; /* shift data to the left */
for(i = PACKETBUF_HDR_SIZE; i < len; i++) { for(i = 0; i < buflen; i++) {
packetbuf[i] = packetbuf[bufptr + i]; packetbuf[hdrlen + i] = packetbuf[packetbuf_hdrlen() + i];
} }
bufptr = 0; bufptr = 0;
} }
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int int
packetbuf_copyto_hdr(uint8_t *to)
{
#if DEBUG_LEVEL > 0
{
int 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, packetbuf + hdrptr, PACKETBUF_HDR_SIZE - hdrptr);
return PACKETBUF_HDR_SIZE - hdrptr;
}
/*---------------------------------------------------------------------------*/
int
packetbuf_copyto(void *to) packetbuf_copyto(void *to)
{ {
#if DEBUG_LEVEL > 0 if(hdrlen + buflen > PACKETBUF_SIZE) {
{
int i;
char buffer[1000];
char *bufferptr = buffer;
int bufferlen = 0;
bufferptr[0] = 0;
for(i = hdrptr; i < PACKETBUF_HDR_SIZE; ++i) {
bufferptr += sprintf(bufferptr, "0x%02x, ", packetbuf[i]);
}
PRINTF("packetbuf_write: header: %s\n", buffer);
bufferptr = buffer;
bufferptr[0] = 0;
for(i = bufptr; ((i < buflen + bufptr) && (bufferlen < (sizeof(buffer) - 10))); ++i) {
bufferlen += sprintf(bufferptr + bufferlen, "0x%02x, ", packetbufptr[i]);
}
PRINTF("packetbuf_write: data: %s\n", buffer);
}
#endif /* DEBUG_LEVEL */
if(PACKETBUF_HDR_SIZE - hdrptr + buflen > PACKETBUF_SIZE) {
/* Too large packet */
return 0; return 0;
} }
memcpy(to, packetbuf + hdrptr, PACKETBUF_HDR_SIZE - hdrptr); memcpy(to, packetbuf_hdrptr(), hdrlen);
memcpy((uint8_t *)to + PACKETBUF_HDR_SIZE - hdrptr, packetbufptr + bufptr, memcpy((uint8_t *)to + hdrlen, packetbuf_dataptr(), buflen);
buflen); return hdrlen + buflen;
return PACKETBUF_HDR_SIZE - hdrptr + buflen;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int int
packetbuf_hdralloc(int size) packetbuf_hdralloc(int size)
{ {
if(hdrptr >= size && packetbuf_totlen() + size <= PACKETBUF_SIZE) { int16_t i;
hdrptr -= size;
return 1; if(size + packetbuf_totlen() > PACKETBUF_SIZE) {
return 0;
} }
return 0;
} /* shift data to the right */
/*---------------------------------------------------------------------------*/ for(i = packetbuf_totlen() - 1; i >= 0; i--) {
void packetbuf[i + size] = packetbuf[i];
packetbuf_hdr_remove(int size) }
{ hdrlen += size;
hdrptr += size; return 1;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int int
@ -204,13 +157,13 @@ packetbuf_set_datalen(uint16_t len)
void * void *
packetbuf_dataptr(void) packetbuf_dataptr(void)
{ {
return (void *)(&packetbuf[bufptr + PACKETBUF_HDR_SIZE]); return packetbuf + packetbuf_hdrlen();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void * void *
packetbuf_hdrptr(void) packetbuf_hdrptr(void)
{ {
return (void *)(&packetbuf[hdrptr]); return packetbuf;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
uint16_t uint16_t
@ -222,16 +175,7 @@ packetbuf_datalen(void)
uint8_t uint8_t
packetbuf_hdrlen(void) packetbuf_hdrlen(void)
{ {
uint8_t hdrlen; return bufptr + hdrlen;
hdrlen = PACKETBUF_HDR_SIZE - hdrptr;
if(hdrlen) {
/* outbound packet */
return hdrlen;
} else {
/* inbound packet */
return bufptr;
}
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
uint16_t uint16_t
@ -244,9 +188,7 @@ void
packetbuf_attr_clear(void) packetbuf_attr_clear(void)
{ {
int i; int i;
for(i = 0; i < PACKETBUF_NUM_ATTRS; ++i) { memset(packetbuf_attrs, 0, sizeof(packetbuf_attrs));
packetbuf_attrs[i].val = 0;
}
for(i = 0; i < PACKETBUF_NUM_ADDRS; ++i) { for(i = 0; i < PACKETBUF_NUM_ADDRS; ++i) {
linkaddr_copy(&packetbuf_addrs[i].addr, &linkaddr_null); linkaddr_copy(&packetbuf_addrs[i].addr, &linkaddr_null);
} }
@ -272,7 +214,6 @@ packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
int int
packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val) packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
{ {
/* packetbuf_attrs[type].type = type; */
packetbuf_attrs[type].val = val; packetbuf_attrs[type].val = val;
return 1; return 1;
} }
@ -286,7 +227,6 @@ packetbuf_attr(uint8_t type)
int int
packetbuf_set_addr(uint8_t type, const linkaddr_t *addr) packetbuf_set_addr(uint8_t type, const linkaddr_t *addr)
{ {
/* packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].type = type; */
linkaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr); linkaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr);
return 1; return 1;
} }

View File

@ -66,15 +66,6 @@
#define PACKETBUF_SIZE 128 #define PACKETBUF_SIZE 128
#endif #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 48
#endif
#ifdef PACKETBUF_CONF_WITH_PACKET_TYPE #ifdef PACKETBUF_CONF_WITH_PACKET_TYPE
#define PACKETBUF_WITH_PACKET_TYPE PACKETBUF_CONF_WITH_PACKET_TYPE #define PACKETBUF_WITH_PACKET_TYPE PACKETBUF_CONF_WITH_PACKET_TYPE
#else #else
@ -92,21 +83,6 @@
*/ */
void packetbuf_clear(void); void packetbuf_clear(void);
/**
* \brief Clear and reset the header of the packetbuf
*
* This function clears the header of the packetbuf and
* resets all the internal state pointers pertaining to
* the header (header size, header pointer, but not
* external data pointer). It is used before after sending
* a packet in the packetbuf, to be able to reuse the
* packet buffer for a later retransmission.
*
*/
void packetbuf_clear_hdr(void);
void packetbuf_hdr_remove(int bytes);
/** /**
* \brief Get a pointer to the data in the packetbuf * \brief Get a pointer to the data in the packetbuf
* \return Pointer to the packetbuf data * \return Pointer to the packetbuf data
@ -115,15 +91,6 @@ void packetbuf_hdr_remove(int bytes);
* the packetbuf. The data is either stored in the packetbuf, * the packetbuf. The data is either stored in the packetbuf,
* or referenced to an external location. * 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); void *packetbuf_dataptr(void);
@ -131,11 +98,6 @@ void *packetbuf_dataptr(void);
* \brief Get a pointer to the header in the packetbuf, for outbound packets * \brief Get a pointer to the header in the packetbuf, for outbound packets
* \return Pointer to the packetbuf header * \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); void *packetbuf_hdrptr(void);
@ -143,12 +105,6 @@ void *packetbuf_hdrptr(void);
* \brief Get the length of the header in the packetbuf * \brief Get the length of the header in the packetbuf
* \return Length of the header in the packetbuf * \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); uint8_t packetbuf_hdrlen(void);
@ -157,17 +113,6 @@ uint8_t packetbuf_hdrlen(void);
* \brief Get the length of the data in the packetbuf * \brief Get the length of the data in the packetbuf
* \return 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); uint16_t packetbuf_datalen(void);
@ -181,10 +126,6 @@ uint16_t packetbuf_totlen(void);
/** /**
* \brief Set the length of the data in the packetbuf * \brief Set the length of the data in the packetbuf
* \param len The length of the data * \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); void packetbuf_set_datalen(uint16_t len);
@ -228,29 +169,13 @@ int packetbuf_copyfrom(const void *from, uint16_t len);
* *
* The external buffer to which the packetbuf is to be * The external buffer to which the packetbuf is to be
* copied must be able to accomodate at least * copied must be able to accomodate at least
* (PACKETBUF_SIZE + PACKETBUF_HDR_SIZE) bytes. The number of * PACKETBUF_SIZE bytes. The number of
* bytes that was copied to the external buffer is * bytes that was copied to the external buffer is
* returned. * returned.
* *
*/ */
int packetbuf_copyto(void *to); 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 * \brief Extend the header of the packetbuf, for outbound packets
* \param size The number of bytes the header should be extended * \param size The number of bytes the header should be extended
@ -284,11 +209,9 @@ int packetbuf_hdrreduce(int size);
typedef uint16_t packetbuf_attr_t; typedef uint16_t packetbuf_attr_t;
struct packetbuf_attr { struct packetbuf_attr {
/* uint8_t type; */
packetbuf_attr_t val; packetbuf_attr_t val;
}; };
struct packetbuf_addr { struct packetbuf_addr {
/* uint8_t type; */
linkaddr_t addr; linkaddr_t addr;
}; };
@ -403,15 +326,9 @@ enum {
extern struct packetbuf_attr packetbuf_attrs[]; extern struct packetbuf_attr packetbuf_attrs[];
extern struct packetbuf_addr packetbuf_addrs[]; 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 linkaddr_t *addr);
static const linkaddr_t *packetbuf_addr(uint8_t type);
static inline int static inline int
packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val) packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
{ {
/* packetbuf_attrs[type].type = type; */
packetbuf_attrs[type].val = val; packetbuf_attrs[type].val = val;
return 1; return 1;
} }
@ -424,7 +341,6 @@ packetbuf_attr(uint8_t type)
static inline int static inline int
packetbuf_set_addr(uint8_t type, const linkaddr_t *addr) packetbuf_set_addr(uint8_t type, const linkaddr_t *addr)
{ {
/* packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].type = type; */
linkaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr); linkaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr);
return 1; return 1;
} }

View File

@ -138,15 +138,12 @@ typedef unsigned short uip_stats_t;
/* Network setup. The new NETSTACK interface requires RF230BB (as does ip4) */ /* Network setup. The new NETSTACK interface requires RF230BB (as does ip4) */
#if RF230BB #if RF230BB
#undef PACKETBUF_CONF_HDR_SIZE //Use the packetbuf default for header size
/* TX routine passes the cca/ack result in the return parameter */ /* TX routine passes the cca/ack result in the return parameter */
#define RDC_CONF_HARDWARE_ACK 1 #define RDC_CONF_HARDWARE_ACK 1
/* TX routine does automatic cca and optional backoff */ /* TX routine does automatic cca and optional backoff */
#define RDC_CONF_HARDWARE_CSMA 1 #define RDC_CONF_HARDWARE_CSMA 1
/* Allow MCU sleeping between channel checks */ /* Allow MCU sleeping between channel checks */
#define RDC_CONF_MCU_SLEEP 0 #define RDC_CONF_MCU_SLEEP 0
#else
#define PACKETBUF_CONF_HDR_SIZE 0 //RF230 combined driver/mac handles headers internally
#endif /*RF230BB */ #endif /*RF230BB */
#if NETSTACK_CONF_WITH_IPV6 #if NETSTACK_CONF_WITH_IPV6

View File

@ -204,11 +204,6 @@ extern void mac_log_802_15_4_rx(const uint8_t* buffer, size_t total_len);
/* Network setup. The new NETSTACK interface requires RF230BB (as does ip4) */ /* Network setup. The new NETSTACK interface requires RF230BB (as does ip4) */
/* These mostly have no effect when the Jackdaw is a repeater (CONTIKI_NO_NET=1 using fakeuip.c) */ /* These mostly have no effect when the Jackdaw is a repeater (CONTIKI_NO_NET=1 using fakeuip.c) */
#if RF230BB
#else
#define PACKETBUF_CONF_HDR_SIZE 0 //RF230 combined driver/mac handles headers internally
#endif /*RF230BB */
#if NETSTACK_CONF_WITH_IPV6 #if NETSTACK_CONF_WITH_IPV6
#define LINKADDR_CONF_SIZE 8 #define LINKADDR_CONF_SIZE 8
#define UIP_CONF_ICMP6 1 #define UIP_CONF_ICMP6 1

View File

@ -85,7 +85,6 @@ void clock_adjust_ticks(clock_time_t howmany);
#define CLIF #define CLIF
#define LINKADDR_CONF_SIZE 8 #define LINKADDR_CONF_SIZE 8
#define PACKETBUF_CONF_HDR_SIZE 48 /* Choose a buffersize != 0 for the messages which should be sended over the wireless interface */
/* Uncomment this lines to activate the specific drivers */ /* Uncomment this lines to activate the specific drivers */
//#define NETSTACK_CONF_NETWORK rime_driver //#define NETSTACK_CONF_NETWORK rime_driver

View File

@ -88,7 +88,6 @@ void clock_adjust_ticks(clock_time_t howmany);
#endif #endif
#define LINKADDR_CONF_SIZE 8 #define LINKADDR_CONF_SIZE 8
#define PACKETBUF_CONF_HDR_SIZE 0
//define NETSTACK_CONF_WITH_IPV6 1 //Let the makefile do this, allows hello-world to compile //define NETSTACK_CONF_WITH_IPV6 1 //Let the makefile do this, allows hello-world to compile
#if NETSTACK_CONF_WITH_IPV6 #if NETSTACK_CONF_WITH_IPV6
@ -100,7 +99,6 @@ void clock_adjust_ticks(clock_time_t howmany);
/* The new NETSTACK interface requires RF230BB */ /* The new NETSTACK interface requires RF230BB */
#if RF230BB #if RF230BB
#define SICSLOWPAN_CONF_COMPRESSION SICSLOWPAN_COMPRESSION_HC06 #define SICSLOWPAN_CONF_COMPRESSION SICSLOWPAN_COMPRESSION_HC06
#undef PACKETBUF_CONF_HDR_SIZE //RF230BB takes the packetbuf default for header size
#define UIP_CONF_LLH_LEN 0 #define UIP_CONF_LLH_LEN 0
/* No radio cycling */ /* No radio cycling */
@ -126,7 +124,6 @@ void clock_adjust_ticks(clock_time_t howmany);
#else #else
/* Original combined RF230/mac code will not compile with current contiki stack */ /* Original combined RF230/mac code will not compile with current contiki stack */
//#define PACKETBUF_CONF_HDR_SIZE 0 //RF230 handles headers internally
//FTH081105 //FTH081105
#define SICSLOWPAN_CONF_COMPRESSION SICSLOWPAN_COMPRESSION_HC06 #define SICSLOWPAN_CONF_COMPRESSION SICSLOWPAN_COMPRESSION_HC06
#define SICSLOWPAN_CONF_MAXAGE 5 #define SICSLOWPAN_CONF_MAXAGE 5