Contiki 3.x
packetbuf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Rime buffer (packetbuf) management
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup packetbuf
42  * @{
43  */
44 
45 #include <string.h>
46 
47 #include "contiki-net.h"
48 #include "net/packetbuf.h"
49 #include "net/rime/rime.h"
50 #include "sys/cc.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 hdrlen;
58 
59 /* The declarations below ensure that the packet buffer is aligned on
60  an even 32-bit boundary. On some platforms (most notably the
61  msp430 or OpenRISC), having a potentially misaligned packet buffer may lead to
62  problems when accessing words. */
63 static uint32_t packetbuf_aligned[(PACKETBUF_SIZE + 3) / 4];
64 static uint8_t *packetbuf = (uint8_t *)packetbuf_aligned;
65 
66 #define DEBUG 0
67 #if DEBUG
68 #include <stdio.h>
69 #define PRINTF(...) printf(__VA_ARGS__)
70 #else
71 #define PRINTF(...)
72 #endif
73 
74 /*---------------------------------------------------------------------------*/
75 void
77 {
78  buflen = bufptr = 0;
79  hdrlen = 0;
80 
81  packetbuf_attr_clear();
82 }
83 /*---------------------------------------------------------------------------*/
84 int
85 packetbuf_copyfrom(const void *from, uint16_t len)
86 {
87  uint16_t l;
88 
90  l = MIN(PACKETBUF_SIZE, len);
91  memcpy(packetbuf, from, l);
92  buflen = l;
93  return l;
94 }
95 /*---------------------------------------------------------------------------*/
96 void
98 {
99  int16_t i;
100 
101  if(bufptr) {
102  /* shift data to the left */
103  for(i = 0; i < buflen; i++) {
104  packetbuf[hdrlen + i] = packetbuf[packetbuf_hdrlen() + i];
105  }
106  bufptr = 0;
107  }
108 }
109 /*---------------------------------------------------------------------------*/
110 int
112 {
113  if(hdrlen + buflen > PACKETBUF_SIZE) {
114  return 0;
115  }
116  memcpy(to, packetbuf_hdrptr(), hdrlen);
117  memcpy((uint8_t *)to + hdrlen, packetbuf_dataptr(), buflen);
118  return hdrlen + buflen;
119 }
120 /*---------------------------------------------------------------------------*/
121 int
123 {
124  int16_t i;
125 
126  if(size + packetbuf_totlen() > PACKETBUF_SIZE) {
127  return 0;
128  }
129 
130  /* shift data to the right */
131  for(i = packetbuf_totlen() - 1; i >= 0; i--) {
132  packetbuf[i + size] = packetbuf[i];
133  }
134  hdrlen += size;
135  return 1;
136 }
137 /*---------------------------------------------------------------------------*/
138 int
140 {
141  if(buflen < size) {
142  return 0;
143  }
144 
145  bufptr += size;
146  buflen -= size;
147  return 1;
148 }
149 /*---------------------------------------------------------------------------*/
150 void
152 {
153  PRINTF("packetbuf_set_len: len %d\n", len);
154  buflen = len;
155 }
156 /*---------------------------------------------------------------------------*/
157 void *
159 {
160  return packetbuf + packetbuf_hdrlen();
161 }
162 /*---------------------------------------------------------------------------*/
163 void *
165 {
166  return packetbuf;
167 }
168 /*---------------------------------------------------------------------------*/
169 uint16_t
171 {
172  return buflen;
173 }
174 /*---------------------------------------------------------------------------*/
175 uint8_t
177 {
178  return bufptr + hdrlen;
179 }
180 /*---------------------------------------------------------------------------*/
181 uint16_t
183 {
184  return packetbuf_hdrlen() + packetbuf_datalen();
185 }
186 /*---------------------------------------------------------------------------*/
187 void
188 packetbuf_attr_clear(void)
189 {
190  int i;
191  memset(packetbuf_attrs, 0, sizeof(packetbuf_attrs));
192  for(i = 0; i < PACKETBUF_NUM_ADDRS; ++i) {
193  linkaddr_copy(&packetbuf_addrs[i].addr, &linkaddr_null);
194  }
195 }
196 /*---------------------------------------------------------------------------*/
197 void
198 packetbuf_attr_copyto(struct packetbuf_attr *attrs,
199  struct packetbuf_addr *addrs)
200 {
201  memcpy(attrs, packetbuf_attrs, sizeof(packetbuf_attrs));
202  memcpy(addrs, packetbuf_addrs, sizeof(packetbuf_addrs));
203 }
204 /*---------------------------------------------------------------------------*/
205 void
206 packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
207  struct packetbuf_addr *addrs)
208 {
209  memcpy(packetbuf_attrs, attrs, sizeof(packetbuf_attrs));
210  memcpy(packetbuf_addrs, addrs, sizeof(packetbuf_addrs));
211 }
212 /*---------------------------------------------------------------------------*/
213 #if !PACKETBUF_CONF_ATTRS_INLINE
214 int
215 packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
216 {
217  packetbuf_attrs[type].val = val;
218  return 1;
219 }
220 /*---------------------------------------------------------------------------*/
221 packetbuf_attr_t
222 packetbuf_attr(uint8_t type)
223 {
224  return packetbuf_attrs[type].val;
225 }
226 /*---------------------------------------------------------------------------*/
227 int
228 packetbuf_set_addr(uint8_t type, const linkaddr_t *addr)
229 {
230  linkaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr);
231  return 1;
232 }
233 /*---------------------------------------------------------------------------*/
234 const linkaddr_t *
235 packetbuf_addr(uint8_t type)
236 {
237  return &packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr;
238 }
239 /*---------------------------------------------------------------------------*/
240 #endif /* PACKETBUF_CONF_ATTRS_INLINE */
241 int
243 {
244  return linkaddr_cmp(&packetbuf_addrs[PACKETBUF_ADDR_RECEIVER - PACKETBUF_ADDR_FIRST].addr, &linkaddr_null);
245 }
246 /*---------------------------------------------------------------------------*/
247 
248 /** @} */
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:158
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:76
Default definitions of C compiler quirk work-arounds.
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition: packetbuf.h:66
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:182
const linkaddr_t linkaddr_null
The null Rime address.
Definition: eth-conf.c:37
int packetbuf_holds_broadcast(void)
Checks whether the current packet is a broadcast.
Definition: packetbuf.c:242
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:164
Header file for the Rime buffer (packetbuf) management
void packetbuf_compact(void)
Compact the packetbuf.
Definition: packetbuf.c:97
Header file for the Rime stack
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:151
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
Definition: packetbuf.c:85
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:170
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:122
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:139
uint8_t packetbuf_hdrlen(void)
Get the length of the header in the packetbuf.
Definition: packetbuf.c:176
int packetbuf_copyto(void *to)
Copy the entire packetbuf to an external buffer.
Definition: packetbuf.c:111