Contiki 2.5
packetbuf.c
Go to the documentation of this file.
1 /**
2  * \addtogroup packetbuf
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2006, Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the Institute nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * This file is part of the Contiki operating system.
35  *
36  * $Id: packetbuf.c,v 1.1 2010/06/14 19:19:16 adamdunkels Exp $
37  */
38 
39 /**
40  * \file
41  * Rime buffer (packetbuf) management
42  * \author
43  * Adam Dunkels <adam@sics.se>
44  */
45 
46 #include <string.h>
47 
48 #include "contiki-net.h"
49 #include "net/packetbuf.h"
50 #include "net/rime.h"
51 
52 struct packetbuf_attr packetbuf_attrs[PACKETBUF_NUM_ATTRS];
53 struct packetbuf_addr packetbuf_addrs[PACKETBUF_NUM_ADDRS];
54 
55 
56 static uint16_t buflen, bufptr;
57 static uint8_t hdrptr;
58 
59 /* The declarations below ensure that the packet buffer is aligned on
60  an even 16-bit boundary. On some platforms (most notably the
61  msp430), having apotentially misaligned packet buffer may lead to
62  problems when accessing 16-bit values. */
63 static uint16_t packetbuf_aligned[(PACKETBUF_SIZE + PACKETBUF_HDR_SIZE) / 2 + 1];
64 static uint8_t *packetbuf = (uint8_t *)packetbuf_aligned;
65 
66 static uint8_t *packetbufptr;
67 
68 #define DEBUG 0
69 #if DEBUG
70 #include <stdio.h>
71 #define PRINTF(...) printf(__VA_ARGS__)
72 #else
73 #define PRINTF(...)
74 #endif
75 
76 /*---------------------------------------------------------------------------*/
77 void
79 {
80  buflen = bufptr = 0;
81  hdrptr = PACKETBUF_HDR_SIZE;
82 
83  packetbufptr = &packetbuf[PACKETBUF_HDR_SIZE];
84  packetbuf_attr_clear();
85 }
86 /*---------------------------------------------------------------------------*/
87 void
89 {
90  hdrptr = PACKETBUF_HDR_SIZE;
91 }
92 /*---------------------------------------------------------------------------*/
93 int
94 packetbuf_copyfrom(const void *from, uint16_t len)
95 {
96  uint16_t l;
97 
99  l = len > PACKETBUF_SIZE? PACKETBUF_SIZE: len;
100  memcpy(packetbufptr, from, l);
101  buflen = l;
102  return l;
103 }
104 /*---------------------------------------------------------------------------*/
105 void
107 {
108  int i, len;
109 
110  if(packetbuf_is_reference()) {
111  memcpy(&packetbuf[PACKETBUF_HDR_SIZE], packetbuf_reference_ptr(),
113  } else if (bufptr > 0) {
115  for(i = PACKETBUF_HDR_SIZE; i < len; i++) {
116  packetbuf[i] = packetbuf[bufptr + i];
117  }
118 
119  bufptr = 0;
120  }
121 }
122 /*---------------------------------------------------------------------------*/
123 int
125 {
126 #if DEBUG_LEVEL > 0
127  {
128  int i;
129  PRINTF("packetbuf_write_hdr: header:\n");
130  for(i = hdrptr; i < PACKETBUF_HDR_SIZE; ++i) {
131  PRINTF("0x%02x, ", packetbuf[i]);
132  }
133  PRINTF("\n");
134  }
135 #endif /* DEBUG_LEVEL */
136  memcpy(to, packetbuf + hdrptr, PACKETBUF_HDR_SIZE - hdrptr);
137  return PACKETBUF_HDR_SIZE - hdrptr;
138 }
139 /*---------------------------------------------------------------------------*/
140 int
142 {
143 #if DEBUG_LEVEL > 0
144  {
145  int i;
146  char buffer[1000];
147  char *bufferptr = buffer;
148 
149  bufferptr[0] = 0;
150  for(i = hdrptr; i < PACKETBUF_HDR_SIZE; ++i) {
151  bufferptr += sprintf(bufferptr, "0x%02x, ", packetbuf[i]);
152  }
153  PRINTF("packetbuf_write: header: %s\n", buffer);
154  bufferptr = buffer;
155  bufferptr[0] = 0;
156  for(i = bufptr; i < buflen + bufptr; ++i) {
157  bufferptr += sprintf(bufferptr, "0x%02x, ", packetbufptr[i]);
158  }
159  PRINTF("packetbuf_write: data: %s\n", buffer);
160  }
161 #endif /* DEBUG_LEVEL */
162  if(PACKETBUF_HDR_SIZE - hdrptr + buflen > PACKETBUF_SIZE) {
163  /* Too large packet */
164  return 0;
165  }
166  memcpy(to, packetbuf + hdrptr, PACKETBUF_HDR_SIZE - hdrptr);
167  memcpy((uint8_t *)to + PACKETBUF_HDR_SIZE - hdrptr, packetbufptr + bufptr,
168  buflen);
169  return PACKETBUF_HDR_SIZE - hdrptr + buflen;
170 }
171 /*---------------------------------------------------------------------------*/
172 int
174 {
175  if(hdrptr >= size && packetbuf_totlen() + size <= PACKETBUF_SIZE) {
176  hdrptr -= size;
177  return 1;
178  }
179  return 0;
180 }
181 /*---------------------------------------------------------------------------*/
182 void
183 packetbuf_hdr_remove(int size)
184 {
185  hdrptr += size;
186 }
187 /*---------------------------------------------------------------------------*/
188 int
190 {
191  if(buflen < size) {
192  return 0;
193  }
194 
195  bufptr += size;
196  buflen -= size;
197  return 1;
198 }
199 /*---------------------------------------------------------------------------*/
200 void
202 {
203  PRINTF("packetbuf_set_len: len %d\n", len);
204  buflen = len;
205 }
206 /*---------------------------------------------------------------------------*/
207 void *
209 {
210  return (void *)(&packetbuf[bufptr + PACKETBUF_HDR_SIZE]);
211 }
212 /*---------------------------------------------------------------------------*/
213 void *
215 {
216  return (void *)(&packetbuf[hdrptr]);
217 }
218 /*---------------------------------------------------------------------------*/
219 void
220 packetbuf_reference(void *ptr, uint16_t len)
221 {
222  packetbuf_clear();
223  packetbufptr = ptr;
224  buflen = len;
225 }
226 /*---------------------------------------------------------------------------*/
227 int
229 {
230  return packetbufptr != &packetbuf[PACKETBUF_HDR_SIZE];
231 }
232 /*---------------------------------------------------------------------------*/
233 void *
235 {
236  return packetbufptr;
237 }
238 /*---------------------------------------------------------------------------*/
239 uint16_t
241 {
242  return buflen;
243 }
244 /*---------------------------------------------------------------------------*/
245 uint8_t
247 {
248  return PACKETBUF_HDR_SIZE - hdrptr;
249 }
250 /*---------------------------------------------------------------------------*/
251 uint16_t
253 {
254  return packetbuf_hdrlen() + packetbuf_datalen();
255 }
256 /*---------------------------------------------------------------------------*/
257 void
258 packetbuf_attr_clear(void)
259 {
260  int i;
261  for(i = 0; i < PACKETBUF_NUM_ATTRS; ++i) {
262  packetbuf_attrs[i].val = 0;
263  }
264  for(i = 0; i < PACKETBUF_NUM_ADDRS; ++i) {
265  rimeaddr_copy(&packetbuf_addrs[i].addr, &rimeaddr_null);
266  }
267 }
268 /*---------------------------------------------------------------------------*/
269 void
270 packetbuf_attr_copyto(struct packetbuf_attr *attrs,
271  struct packetbuf_addr *addrs)
272 {
273  memcpy(attrs, packetbuf_attrs, sizeof(packetbuf_attrs));
274  memcpy(addrs, packetbuf_addrs, sizeof(packetbuf_addrs));
275 }
276 /*---------------------------------------------------------------------------*/
277 void
278 packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
279  struct packetbuf_addr *addrs)
280 {
281  memcpy(packetbuf_attrs, attrs, sizeof(packetbuf_attrs));
282  memcpy(packetbuf_addrs, addrs, sizeof(packetbuf_addrs));
283 }
284 /*---------------------------------------------------------------------------*/
285 #if !PACKETBUF_CONF_ATTRS_INLINE
286 int
287 packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
288 {
289 /* packetbuf_attrs[type].type = type; */
290  packetbuf_attrs[type].val = val;
291  return 1;
292 }
293 /*---------------------------------------------------------------------------*/
294 packetbuf_attr_t
295 packetbuf_attr(uint8_t type)
296 {
297  return packetbuf_attrs[type].val;
298 }
299 /*---------------------------------------------------------------------------*/
300 int
301 packetbuf_set_addr(uint8_t type, const rimeaddr_t *addr)
302 {
303 /* packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].type = type; */
304  rimeaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr);
305  return 1;
306 }
307 /*---------------------------------------------------------------------------*/
308 const rimeaddr_t *
309 packetbuf_addr(uint8_t type)
310 {
311  return &packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr;
312 }
313 /*---------------------------------------------------------------------------*/
314 #endif /* PACKETBUF_CONF_ATTRS_INLINE */
315 /** @} */