packetbuf.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup packetbuf
00003  * @{
00004  */
00005 
00006 /*
00007  * Copyright (c) 2006, Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  * 3. Neither the name of the Institute nor the names of its contributors
00019  *    may be used to endorse or promote products derived from this software
00020  *    without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * This file is part of the Contiki operating system.
00035  *
00036  * $Id: packetbuf.c,v 1.1 2010/06/14 19:19:16 adamdunkels Exp $
00037  */
00038 
00039 /**
00040  * \file
00041  *         Rime buffer (packetbuf) management
00042  * \author
00043  *         Adam Dunkels <adam@sics.se>
00044  */
00045 
00046 #include <string.h>
00047 
00048 #include "contiki-net.h"
00049 #include "net/packetbuf.h"
00050 #include "net/rime.h"
00051 
00052 struct packetbuf_attr packetbuf_attrs[PACKETBUF_NUM_ATTRS];
00053 struct packetbuf_addr packetbuf_addrs[PACKETBUF_NUM_ADDRS];
00054 
00055 
00056 static uint16_t buflen, bufptr;
00057 static uint8_t hdrptr;
00058 
00059 /* The declarations below ensure that the packet buffer is aligned on
00060    an even 16-bit boundary. On some platforms (most notably the
00061    msp430), having apotentially misaligned packet buffer may lead to
00062    problems when accessing 16-bit values. */
00063 static uint16_t packetbuf_aligned[(PACKETBUF_SIZE + PACKETBUF_HDR_SIZE) / 2 + 1];
00064 static uint8_t *packetbuf = (uint8_t *)packetbuf_aligned;
00065 
00066 static uint8_t *packetbufptr;
00067 
00068 #define DEBUG 0
00069 #if DEBUG
00070 #include <stdio.h>
00071 #define PRINTF(...) printf(__VA_ARGS__)
00072 #else
00073 #define PRINTF(...)
00074 #endif
00075 
00076 /*---------------------------------------------------------------------------*/
00077 void
00078 packetbuf_clear(void)
00079 {
00080   buflen = bufptr = 0;
00081   hdrptr = PACKETBUF_HDR_SIZE;
00082 
00083   packetbufptr = &packetbuf[PACKETBUF_HDR_SIZE];
00084   packetbuf_attr_clear();
00085 }
00086 /*---------------------------------------------------------------------------*/
00087 void
00088 packetbuf_clear_hdr(void)
00089 {
00090   hdrptr = PACKETBUF_HDR_SIZE;
00091 }
00092 /*---------------------------------------------------------------------------*/
00093 int
00094 packetbuf_copyfrom(const void *from, uint16_t len)
00095 {
00096   uint16_t l;
00097 
00098   packetbuf_clear();
00099   l = len > PACKETBUF_SIZE? PACKETBUF_SIZE: len;
00100   memcpy(packetbufptr, from, l);
00101   buflen = l;
00102   return l;
00103 }
00104 /*---------------------------------------------------------------------------*/
00105 void
00106 packetbuf_compact(void)
00107 {
00108   int i, len;
00109 
00110   if(packetbuf_is_reference()) {
00111     memcpy(&packetbuf[PACKETBUF_HDR_SIZE], packetbuf_reference_ptr(),
00112            packetbuf_datalen());
00113   } else if (bufptr > 0) {
00114     len = packetbuf_datalen() + PACKETBUF_HDR_SIZE;
00115     for(i = PACKETBUF_HDR_SIZE; i < len; i++) {
00116       packetbuf[i] = packetbuf[bufptr + i];
00117     }
00118 
00119     bufptr = 0;
00120   }
00121 }
00122 /*---------------------------------------------------------------------------*/
00123 int
00124 packetbuf_copyto_hdr(uint8_t *to)
00125 {
00126 #if DEBUG_LEVEL > 0
00127   {
00128     int i;
00129     PRINTF("packetbuf_write_hdr: header:\n");
00130     for(i = hdrptr; i < PACKETBUF_HDR_SIZE; ++i) {
00131       PRINTF("0x%02x, ", packetbuf[i]);
00132     }
00133     PRINTF("\n");
00134   }
00135 #endif /* DEBUG_LEVEL */
00136   memcpy(to, packetbuf + hdrptr, PACKETBUF_HDR_SIZE - hdrptr);
00137   return PACKETBUF_HDR_SIZE - hdrptr;
00138 }
00139 /*---------------------------------------------------------------------------*/
00140 int
00141 packetbuf_copyto(void *to)
00142 {
00143 #if DEBUG_LEVEL > 0
00144   {
00145     int i;
00146     char buffer[1000];
00147     char *bufferptr = buffer;
00148     
00149     bufferptr[0] = 0;
00150     for(i = hdrptr; i < PACKETBUF_HDR_SIZE; ++i) {
00151       bufferptr += sprintf(bufferptr, "0x%02x, ", packetbuf[i]);
00152     }
00153     PRINTF("packetbuf_write: header: %s\n", buffer);
00154     bufferptr = buffer;
00155     bufferptr[0] = 0;
00156     for(i = bufptr; i < buflen + bufptr; ++i) {
00157       bufferptr += sprintf(bufferptr, "0x%02x, ", packetbufptr[i]);
00158     }
00159     PRINTF("packetbuf_write: data: %s\n", buffer);
00160   }
00161 #endif /* DEBUG_LEVEL */
00162   if(PACKETBUF_HDR_SIZE - hdrptr + buflen > PACKETBUF_SIZE) {
00163     /* Too large packet */
00164     return 0;
00165   }
00166   memcpy(to, packetbuf + hdrptr, PACKETBUF_HDR_SIZE - hdrptr);
00167   memcpy((uint8_t *)to + PACKETBUF_HDR_SIZE - hdrptr, packetbufptr + bufptr,
00168          buflen);
00169   return PACKETBUF_HDR_SIZE - hdrptr + buflen;
00170 }
00171 /*---------------------------------------------------------------------------*/
00172 int
00173 packetbuf_hdralloc(int size)
00174 {
00175   if(hdrptr >= size && packetbuf_totlen() + size <= PACKETBUF_SIZE) {
00176     hdrptr -= size;
00177     return 1;
00178   }
00179   return 0;
00180 }
00181 /*---------------------------------------------------------------------------*/
00182 void
00183 packetbuf_hdr_remove(int size)
00184 {
00185   hdrptr += size;
00186 }
00187 /*---------------------------------------------------------------------------*/
00188 int
00189 packetbuf_hdrreduce(int size)
00190 {
00191   if(buflen < size) {
00192     return 0;
00193   }
00194 
00195   bufptr += size;
00196   buflen -= size;
00197   return 1;
00198 }
00199 /*---------------------------------------------------------------------------*/
00200 void
00201 packetbuf_set_datalen(uint16_t len)
00202 {
00203   PRINTF("packetbuf_set_len: len %d\n", len);
00204   buflen = len;
00205 }
00206 /*---------------------------------------------------------------------------*/
00207 void *
00208 packetbuf_dataptr(void)
00209 {
00210   return (void *)(&packetbuf[bufptr + PACKETBUF_HDR_SIZE]);
00211 }
00212 /*---------------------------------------------------------------------------*/
00213 void *
00214 packetbuf_hdrptr(void)
00215 {
00216   return (void *)(&packetbuf[hdrptr]);
00217 }
00218 /*---------------------------------------------------------------------------*/
00219 void
00220 packetbuf_reference(void *ptr, uint16_t len)
00221 {
00222   packetbuf_clear();
00223   packetbufptr = ptr;
00224   buflen = len;
00225 }
00226 /*---------------------------------------------------------------------------*/
00227 int
00228 packetbuf_is_reference(void)
00229 {
00230   return packetbufptr != &packetbuf[PACKETBUF_HDR_SIZE];
00231 }
00232 /*---------------------------------------------------------------------------*/
00233 void *
00234 packetbuf_reference_ptr(void)
00235 {
00236   return packetbufptr;
00237 }
00238 /*---------------------------------------------------------------------------*/
00239 uint16_t
00240 packetbuf_datalen(void)
00241 {
00242   return buflen;
00243 }
00244 /*---------------------------------------------------------------------------*/
00245 uint8_t
00246 packetbuf_hdrlen(void)
00247 {
00248   return PACKETBUF_HDR_SIZE - hdrptr;
00249 }
00250 /*---------------------------------------------------------------------------*/
00251 uint16_t
00252 packetbuf_totlen(void)
00253 {
00254   return packetbuf_hdrlen() + packetbuf_datalen();
00255 }
00256 /*---------------------------------------------------------------------------*/
00257 void
00258 packetbuf_attr_clear(void)
00259 {
00260   int i;
00261   for(i = 0; i < PACKETBUF_NUM_ATTRS; ++i) {
00262     packetbuf_attrs[i].val = 0;
00263   }
00264   for(i = 0; i < PACKETBUF_NUM_ADDRS; ++i) {
00265     rimeaddr_copy(&packetbuf_addrs[i].addr, &rimeaddr_null);
00266   }
00267 }
00268 /*---------------------------------------------------------------------------*/
00269 void
00270 packetbuf_attr_copyto(struct packetbuf_attr *attrs,
00271                     struct packetbuf_addr *addrs)
00272 {
00273   memcpy(attrs, packetbuf_attrs, sizeof(packetbuf_attrs));
00274   memcpy(addrs, packetbuf_addrs, sizeof(packetbuf_addrs));
00275 }
00276 /*---------------------------------------------------------------------------*/
00277 void
00278 packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
00279                       struct packetbuf_addr *addrs)
00280 {
00281   memcpy(packetbuf_attrs, attrs, sizeof(packetbuf_attrs));
00282   memcpy(packetbuf_addrs, addrs, sizeof(packetbuf_addrs));
00283 }
00284 /*---------------------------------------------------------------------------*/
00285 #if !PACKETBUF_CONF_ATTRS_INLINE
00286 int
00287 packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
00288 {
00289 /*   packetbuf_attrs[type].type = type; */
00290   packetbuf_attrs[type].val = val;
00291   return 1;
00292 }
00293 /*---------------------------------------------------------------------------*/
00294 packetbuf_attr_t
00295 packetbuf_attr(uint8_t type)
00296 {
00297   return packetbuf_attrs[type].val;
00298 }
00299 /*---------------------------------------------------------------------------*/
00300 int
00301 packetbuf_set_addr(uint8_t type, const rimeaddr_t *addr)
00302 {
00303 /*   packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].type = type; */
00304   rimeaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr);
00305   return 1;
00306 }
00307 /*---------------------------------------------------------------------------*/
00308 const rimeaddr_t *
00309 packetbuf_addr(uint8_t type)
00310 {
00311   return &packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr;
00312 }
00313 /*---------------------------------------------------------------------------*/
00314 #endif /* PACKETBUF_CONF_ATTRS_INLINE */
00315 /** @} */

Generated on Mon Apr 11 14:23:29 2011 for Contiki 2.5 by  doxygen 1.6.1