Contiki-NG
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  * Packet 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 "sys/cc.h"
50 
51 struct packetbuf_attr packetbuf_attrs[PACKETBUF_NUM_ATTRS];
52 struct packetbuf_addr packetbuf_addrs[PACKETBUF_NUM_ADDRS];
53 
54 
55 static uint16_t buflen, bufptr;
56 static uint8_t hdrlen;
57 
58 /* The declarations below ensure that the packet buffer is aligned on
59  an even 32-bit boundary. On some platforms (most notably the
60  msp430 or OpenRISC), having a potentially misaligned packet buffer may lead to
61  problems when accessing words. */
62 static uint32_t packetbuf_aligned[(PACKETBUF_SIZE + 3) / 4];
63 static uint8_t *packetbuf = (uint8_t *)packetbuf_aligned;
64 
65 #define DEBUG 0
66 #if DEBUG
67 #include <stdio.h>
68 #define PRINTF(...) printf(__VA_ARGS__)
69 #else
70 #define PRINTF(...)
71 #endif
72 
73 /*---------------------------------------------------------------------------*/
74 void
76 {
77  buflen = bufptr = 0;
78  hdrlen = 0;
79 
80  packetbuf_attr_clear();
81 }
82 /*---------------------------------------------------------------------------*/
83 int
84 packetbuf_copyfrom(const void *from, uint16_t len)
85 {
86  uint16_t l;
87 
89  l = MIN(PACKETBUF_SIZE, len);
90  memcpy(packetbuf, from, l);
91  buflen = l;
92  return l;
93 }
94 /*---------------------------------------------------------------------------*/
95 int
97 {
98  if(hdrlen + buflen > PACKETBUF_SIZE) {
99  return 0;
100  }
101  memcpy(to, packetbuf_hdrptr(), hdrlen);
102  memcpy((uint8_t *)to + hdrlen, packetbuf_dataptr(), buflen);
103  return hdrlen + buflen;
104 }
105 /*---------------------------------------------------------------------------*/
106 int
108 {
109  int16_t i;
110 
111  if(size + packetbuf_totlen() > PACKETBUF_SIZE) {
112  return 0;
113  }
114 
115  /* shift data to the right */
116  for(i = packetbuf_totlen() - 1; i >= 0; i--) {
117  packetbuf[i + size] = packetbuf[i];
118  }
119  hdrlen += size;
120  return 1;
121 }
122 /*---------------------------------------------------------------------------*/
123 int
125 {
126  if(buflen < size) {
127  return 0;
128  }
129 
130  bufptr += size;
131  buflen -= size;
132  return 1;
133 }
134 /*---------------------------------------------------------------------------*/
135 void
137 {
138  PRINTF("packetbuf_set_len: len %d\n", len);
139  buflen = len;
140 }
141 /*---------------------------------------------------------------------------*/
142 void *
144 {
145  return packetbuf + packetbuf_hdrlen();
146 }
147 /*---------------------------------------------------------------------------*/
148 void *
150 {
151  return packetbuf;
152 }
153 /*---------------------------------------------------------------------------*/
154 uint16_t
156 {
157  return buflen;
158 }
159 /*---------------------------------------------------------------------------*/
160 uint8_t
162 {
163  return bufptr + hdrlen;
164 }
165 /*---------------------------------------------------------------------------*/
166 uint16_t
168 {
169  return packetbuf_hdrlen() + packetbuf_datalen();
170 }
171 /*---------------------------------------------------------------------------*/
172 uint16_t
174 {
175  return PACKETBUF_SIZE - packetbuf_totlen();
176 }
177 /*---------------------------------------------------------------------------*/
178 void
179 packetbuf_attr_clear(void)
180 {
181  int i;
182  memset(packetbuf_attrs, 0, sizeof(packetbuf_attrs));
183  for(i = 0; i < PACKETBUF_NUM_ADDRS; ++i) {
184  linkaddr_copy(&packetbuf_addrs[i].addr, &linkaddr_null);
185  }
186 }
187 /*---------------------------------------------------------------------------*/
188 void
189 packetbuf_attr_copyto(struct packetbuf_attr *attrs,
190  struct packetbuf_addr *addrs)
191 {
192  memcpy(attrs, packetbuf_attrs, sizeof(packetbuf_attrs));
193  memcpy(addrs, packetbuf_addrs, sizeof(packetbuf_addrs));
194 }
195 /*---------------------------------------------------------------------------*/
196 void
197 packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
198  struct packetbuf_addr *addrs)
199 {
200  memcpy(packetbuf_attrs, attrs, sizeof(packetbuf_attrs));
201  memcpy(packetbuf_addrs, addrs, sizeof(packetbuf_addrs));
202 }
203 /*---------------------------------------------------------------------------*/
204 int
205 packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
206 {
207  packetbuf_attrs[type].val = val;
208  return 1;
209 }
210 /*---------------------------------------------------------------------------*/
211 packetbuf_attr_t
212 packetbuf_attr(uint8_t type)
213 {
214  return packetbuf_attrs[type].val;
215 }
216 /*---------------------------------------------------------------------------*/
217 int
218 packetbuf_set_addr(uint8_t type, const linkaddr_t *addr)
219 {
220  linkaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr);
221  return 1;
222 }
223 /*---------------------------------------------------------------------------*/
224 const linkaddr_t *
225 packetbuf_addr(uint8_t type)
226 {
227  return &packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr;
228 }
229 /*---------------------------------------------------------------------------*/
230 int
232 {
233  return linkaddr_cmp(&packetbuf_addrs[PACKETBUF_ADDR_RECEIVER - PACKETBUF_ADDR_FIRST].addr, &linkaddr_null);
234 }
235 /*---------------------------------------------------------------------------*/
236 
237 /** @} */
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:143
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:75
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:116
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:107
uint16_t packetbuf_remaininglen(void)
Get the total length of the remaining space in the packetbuf.
Definition: packetbuf.c:173
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:124
int packetbuf_copyto(void *to)
Copy the entire packetbuf to an external buffer.
Definition: packetbuf.c:96
uint8_t packetbuf_hdrlen(void)
Get the length of the header in the packetbuf.
Definition: packetbuf.c:161
const linkaddr_t linkaddr_null
The null link-layer address.
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:155
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:167
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition: packetbuf.h:66
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
Definition: packetbuf.c:84
int packetbuf_holds_broadcast(void)
Checks whether the current packet is a broadcast.
Definition: packetbuf.c:231
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition: linkaddr.c:63
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:149
Header file for the Packet buffer (packetbuf) management
Default definitions of C compiler quirk work-arounds.
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:136