Contiki 3.x
packetbuf.h
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  * Header file for the Rime buffer (packetbuf) management
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rime
42  * @{
43  */
44 
45 /**
46  * \defgroup packetbuf Rime buffer management
47  * @{
48  *
49  * The packetbuf module does Rime's buffer management.
50  */
51 
52 #ifndef PACKETBUF_H_
53 #define PACKETBUF_H_
54 
55 #include "contiki-conf.h"
56 #include "net/linkaddr.h"
57 #include "net/llsec/llsec802154.h"
58 
59 /**
60  * \brief The size of the packetbuf, in bytes
61  */
62 #ifdef PACKETBUF_CONF_SIZE
63 #define PACKETBUF_SIZE PACKETBUF_CONF_SIZE
64 #else
65 #define PACKETBUF_SIZE 128
66 #endif
67 
68 /**
69  * \brief The size of the packetbuf header, in bytes
70  */
71 #ifdef PACKETBUF_CONF_HDR_SIZE
72 #define PACKETBUF_HDR_SIZE PACKETBUF_CONF_HDR_SIZE
73 #else
74 #define PACKETBUF_HDR_SIZE 48
75 #endif
76 
77 /**
78  * \brief Clear and reset the packetbuf
79  *
80  * This function clears the packetbuf and resets all
81  * internal state pointers (header size, header pointer,
82  * external data pointer). It is used before preparing a
83  * packet in the packetbuf.
84  *
85  */
86 void packetbuf_clear(void);
87 
88 /**
89  * \brief Clear and reset the header of the packetbuf
90  *
91  * This function clears the header of the packetbuf and
92  * resets all the internal state pointers pertaining to
93  * the header (header size, header pointer, but not
94  * external data pointer). It is used before after sending
95  * a packet in the packetbuf, to be able to reuse the
96  * packet buffer for a later retransmission.
97  *
98  */
99 void packetbuf_clear_hdr(void);
100 
101 void packetbuf_hdr_remove(int bytes);
102 
103 /**
104  * \brief Get a pointer to the data in the packetbuf
105  * \return Pointer to the packetbuf data
106  *
107  * This function is used to get a pointer to the data in
108  * the packetbuf. The data is either stored in the packetbuf,
109  * or referenced to an external location.
110  *
111  * For outbound packets, the packetbuf consists of two
112  * parts: header and data. The header is accessed with the
113  * packetbuf_hdrptr() function.
114  *
115  * For incoming packets, both the packet header and the
116  * packet data is stored in the data portion of the
117  * packetbuf. Thus this function is used to get a pointer to
118  * the header for incoming packets.
119  *
120  */
121 void *packetbuf_dataptr(void);
122 
123 /**
124  * \brief Get a pointer to the header in the packetbuf, for outbound packets
125  * \return Pointer to the packetbuf header
126  *
127  * For outbound packets, the packetbuf consists of two
128  * parts: header and data. This function is used to get a
129  * pointer to the header in the packetbuf. The header is
130  * stored in the packetbuf.
131  *
132  */
133 void *packetbuf_hdrptr(void);
134 
135 /**
136  * \brief Get the length of the header in the packetbuf
137  * \return Length of the header in the packetbuf
138  *
139  * For outbound packets, the packetbuf consists of two
140  * parts: header and data. This function is used to get
141  * the length of the header in the packetbuf. The header is
142  * stored in the packetbuf and accessed via the
143  * packetbuf_hdrptr() function.
144  *
145  */
146 uint8_t packetbuf_hdrlen(void);
147 
148 
149 /**
150  * \brief Get the length of the data in the packetbuf
151  * \return Length of the data in the packetbuf
152  *
153  * For outbound packets, the packetbuf consists of two
154  * parts: header and data. This function is used to get
155  * the length of the data in the packetbuf. The data is
156  * stored in the packetbuf and accessed via the
157  * packetbuf_dataptr() function.
158  *
159  * For incoming packets, both the packet header and the
160  * packet data is stored in the data portion of the
161  * packetbuf. This function is then used to get the total
162  * length of the packet - both header and data.
163  *
164  */
165 uint16_t packetbuf_datalen(void);
166 
167 /**
168  * \brief Get the total length of the header and data in the packetbuf
169  * \return Length of data and header in the packetbuf
170  *
171  */
172 uint16_t packetbuf_totlen(void);
173 
174 /**
175  * \brief Set the length of the data in the packetbuf
176  * \param len The length of the data
177  *
178  * For outbound packets, the packetbuf consists of two
179  * parts: header and data. This function is used to set
180  * the length of the data in the packetbuf.
181  */
182 void packetbuf_set_datalen(uint16_t len);
183 
184 /**
185  * \brief Point the packetbuf to external data
186  * \param ptr A pointer to the external data
187  * \param len The length of the external data
188  *
189  * For outbound packets, the packetbuf consists of two
190  * parts: header and data. This function is used to make
191  * the packetbuf point to external data. The function also
192  * specifies the length of the external data that the
193  * packetbuf references.
194  */
195 void packetbuf_reference(void *ptr, uint16_t len);
196 
197 /**
198  * \brief Check if the packetbuf references external data
199  * \retval Non-zero if the packetbuf references external data, zero otherwise.
200  *
201  * For outbound packets, the packetbuf consists of two
202  * parts: header and data. This function is used to check
203  * if the packetbuf points to external data that has
204  * previously been referenced with packetbuf_reference().
205  *
206  */
207 int packetbuf_is_reference(void);
208 
209 /**
210  * \brief Get a pointer to external data referenced by the packetbuf
211  * \retval A pointer to the external data
212  *
213  * For outbound packets, the packetbuf consists of two
214  * parts: header and data. The data may point to external
215  * data that has previously been referenced with
216  * packetbuf_reference(). This function is used to get a
217  * pointer to the external data.
218  *
219  */
220 void *packetbuf_reference_ptr(void);
221 
222 /**
223  * \brief Compact the packetbuf
224  *
225  * This function compacts the packetbuf by copying the data
226  * portion of the packetbuf so that becomes consecutive to
227  * the header. It also copies external data that has
228  * previously been referenced with packetbuf_reference()
229  * into the packetbuf.
230  *
231  * This function is called by the Rime code before a
232  * packet is to be sent by a device driver. This assures
233  * that the entire packet is consecutive in memory.
234  *
235  */
236 void packetbuf_compact(void);
237 
238 /**
239  * \brief Copy from external data into the packetbuf
240  * \param from A pointer to the data from which to copy
241  * \param len The size of the data to copy
242  * \retval The number of bytes that was copied into the packetbuf
243  *
244  * This function copies data from a pointer into the
245  * packetbuf. If the data that is to be copied is larger
246  * than the packetbuf, only the data that fits in the
247  * packetbuf is copied. The number of bytes that could be
248  * copied into the rimbuf is returned.
249  *
250  */
251 int packetbuf_copyfrom(const void *from, uint16_t len);
252 
253 /**
254  * \brief Copy the entire packetbuf to an external buffer
255  * \param to A pointer to the buffer to which the data is to be copied
256  * \retval The number of bytes that was copied to the external buffer
257  *
258  * This function copies the packetbuf to an external
259  * buffer. Both the data portion and the header portion of
260  * the packetbuf is copied. If the packetbuf referenced
261  * external data (referenced with packetbuf_reference()) the
262  * external data is copied.
263  *
264  * The external buffer to which the packetbuf is to be
265  * copied must be able to accomodate at least
266  * (PACKETBUF_SIZE + PACKETBUF_HDR_SIZE) bytes. The number of
267  * bytes that was copied to the external buffer is
268  * returned.
269  *
270  */
271 int packetbuf_copyto(void *to);
272 
273 /**
274  * \brief Copy the header portion of the packetbuf to an external buffer
275  * \param to A pointer to the buffer to which the data is to be copied
276  * \retval The number of bytes that was copied to the external buffer
277  *
278  * This function copies the header portion of the packetbuf
279  * to an external buffer.
280  *
281  * The external buffer to which the packetbuf is to be
282  * copied must be able to accomodate at least
283  * PACKETBUF_HDR_SIZE bytes. The number of bytes that was
284  * copied to the external buffer is returned.
285  *
286  */
287 int packetbuf_copyto_hdr(uint8_t *to);
288 
289 /**
290  * \brief Extend the header of the packetbuf, for outbound packets
291  * \param size The number of bytes the header should be extended
292  * \retval Non-zero if the header could be extended, zero otherwise
293  *
294  * This function is used to allocate extra space in the
295  * header portion in the packetbuf, when preparing outbound
296  * packets for transmission. If the function is unable to
297  * allocate sufficient header space, the function returns
298  * zero and does not allocate anything.
299  *
300  */
301 int packetbuf_hdralloc(int size);
302 
303 /**
304  * \brief Reduce the header in the packetbuf, for incoming packets
305  * \param size The number of bytes the header should be reduced
306  * \retval Non-zero if the header could be reduced, zero otherwise
307  *
308  * This function is used to remove the first part of the
309  * header in the packetbuf, when processing incoming
310  * packets. If the function is unable to remove the
311  * requested amount of header space, the function returns
312  * zero and does not allocate anything.
313  *
314  */
315 int packetbuf_hdrreduce(int size);
316 
317 /* Packet attributes stuff below: */
318 
319 typedef uint16_t packetbuf_attr_t;
320 
321 struct packetbuf_attr {
322 /* uint8_t type; */
323  packetbuf_attr_t val;
324 };
325 struct packetbuf_addr {
326 /* uint8_t type; */
327  linkaddr_t addr;
328 };
329 
330 #define PACKETBUF_ATTR_PACKET_TYPE_DATA 0
331 #define PACKETBUF_ATTR_PACKET_TYPE_ACK 1
332 #define PACKETBUF_ATTR_PACKET_TYPE_STREAM 2
333 #define PACKETBUF_ATTR_PACKET_TYPE_STREAM_END 3
334 #define PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP 4
335 
336 enum {
337  PACKETBUF_ATTR_NONE,
338 
339  /* Scope 0 attributes: used only on the local node. */
340  PACKETBUF_ATTR_CHANNEL,
341  PACKETBUF_ATTR_NETWORK_ID,
342  PACKETBUF_ATTR_LINK_QUALITY,
343  PACKETBUF_ATTR_RSSI,
344  PACKETBUF_ATTR_TIMESTAMP,
345  PACKETBUF_ATTR_RADIO_TXPOWER,
346  PACKETBUF_ATTR_LISTEN_TIME,
347  PACKETBUF_ATTR_TRANSMIT_TIME,
348  PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS,
349  PACKETBUF_ATTR_MAC_SEQNO,
350  PACKETBUF_ATTR_MAC_ACK,
351  PACKETBUF_ATTR_IS_CREATED_AND_SECURED,
352 
353  /* Scope 1 attributes: used between two neighbors only. */
354  PACKETBUF_ATTR_RELIABLE,
355  PACKETBUF_ATTR_PACKET_ID,
356  PACKETBUF_ATTR_PACKET_TYPE,
357  PACKETBUF_ATTR_REXMIT,
358  PACKETBUF_ATTR_MAX_REXMIT,
359  PACKETBUF_ATTR_NUM_REXMIT,
360  PACKETBUF_ATTR_PENDING,
361  PACKETBUF_ATTR_FRAME_TYPE,
362 #if LLSEC802154_SECURITY_LEVEL
363  PACKETBUF_ATTR_SECURITY_LEVEL,
364  PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1,
365  PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3,
366 #if LLSEC802154_USES_EXPLICIT_KEYS
367  PACKETBUF_ATTR_KEY_ID_MODE,
368  PACKETBUF_ATTR_KEY_INDEX,
369  PACKETBUF_ATTR_KEY_SOURCE_BYTES_0_1,
370 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
371 #endif /* LLSEC802154_SECURITY_LEVEL */
372 
373  /* Scope 2 attributes: used between end-to-end nodes. */
374  PACKETBUF_ATTR_HOPS,
375  PACKETBUF_ATTR_TTL,
376  PACKETBUF_ATTR_EPACKET_ID,
377  PACKETBUF_ATTR_EPACKET_TYPE,
378  PACKETBUF_ATTR_ERELIABLE,
379 
380  /* These must be last */
381  PACKETBUF_ADDR_SENDER,
382  PACKETBUF_ADDR_RECEIVER,
383  PACKETBUF_ADDR_ESENDER,
384  PACKETBUF_ADDR_ERECEIVER,
385 
386  PACKETBUF_ATTR_MAX
387 };
388 
389 /* Define surrogates when 802.15.4 security is off */
390 #if !LLSEC802154_SECURITY_LEVEL
391 enum {
392  PACKETBUF_ATTR_SECURITY_LEVEL,
393  PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1,
394  PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3
395 };
396 #endif /* LLSEC802154_SECURITY_LEVEL */
397 
398 /* Define surrogates when not using explicit keys */
399 #if !LLSEC802154_USES_EXPLICIT_KEYS
400 enum {
401  PACKETBUF_ATTR_KEY_ID_MODE,
402  PACKETBUF_ATTR_KEY_INDEX,
403  PACKETBUF_ATTR_KEY_SOURCE_BYTES_0_1
404 };
405 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
406 
407 #define PACKETBUF_NUM_ADDRS 4
408 #define PACKETBUF_NUM_ATTRS (PACKETBUF_ATTR_MAX - PACKETBUF_NUM_ADDRS)
409 #define PACKETBUF_ADDR_FIRST PACKETBUF_ADDR_SENDER
410 
411 #define PACKETBUF_IS_ADDR(type) ((type) >= PACKETBUF_ADDR_FIRST)
412 
413 #if PACKETBUF_CONF_ATTRS_INLINE
414 
415 extern struct packetbuf_attr packetbuf_attrs[];
416 extern struct packetbuf_addr packetbuf_addrs[];
417 
418 static int packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val);
419 static packetbuf_attr_t packetbuf_attr(uint8_t type);
420 static int packetbuf_set_addr(uint8_t type, const linkaddr_t *addr);
421 static const linkaddr_t *packetbuf_addr(uint8_t type);
422 
423 static inline int
424 packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
425 {
426 /* packetbuf_attrs[type].type = type; */
427  packetbuf_attrs[type].val = val;
428  return 1;
429 }
430 static inline packetbuf_attr_t
431 packetbuf_attr(uint8_t type)
432 {
433  return packetbuf_attrs[type].val;
434 }
435 
436 static inline int
437 packetbuf_set_addr(uint8_t type, const linkaddr_t *addr)
438 {
439 /* packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].type = type; */
440  linkaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr);
441  return 1;
442 }
443 
444 static inline const linkaddr_t *
445 packetbuf_addr(uint8_t type)
446 {
447  return &packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr;
448 }
449 #else /* PACKETBUF_CONF_ATTRS_INLINE */
450 int packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val);
451 packetbuf_attr_t packetbuf_attr(uint8_t type);
452 int packetbuf_set_addr(uint8_t type, const linkaddr_t *addr);
453 const linkaddr_t *packetbuf_addr(uint8_t type);
454 #endif /* PACKETBUF_CONF_ATTRS_INLINE */
455 
456 void packetbuf_attr_clear(void);
457 
458 void packetbuf_attr_copyto(struct packetbuf_attr *attrs,
459  struct packetbuf_addr *addrs);
460 void packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
461  struct packetbuf_addr *addrs);
462 
463 #define PACKETBUF_ATTRIBUTES(...) { __VA_ARGS__ PACKETBUF_ATTR_LAST }
464 #define PACKETBUF_ATTR_LAST { PACKETBUF_ATTR_NONE, 0 }
465 
466 #define PACKETBUF_ATTR_BIT 1
467 #define PACKETBUF_ATTR_BYTE 8
468 #define PACKETBUF_ADDRSIZE (LINKADDR_SIZE * PACKETBUF_ATTR_BYTE)
469 
470 struct packetbuf_attrlist {
471  uint8_t type;
472  uint8_t len;
473 };
474 
475 #endif /* PACKETBUF_H_ */
476 /** @} */
477 /** @} */
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:172
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
Definition: packetbuf.c:93
uint8_t packetbuf_hdrlen(void)
Get the length of the header in the packetbuf.
Definition: packetbuf.c:245
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:200
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:260
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:213
void * packetbuf_reference_ptr(void)
Get a pointer to external data referenced by the packetbuf.
Definition: packetbuf.c:233
int packetbuf_copyto(void *to)
Copy the entire packetbuf to an external buffer.
Definition: packetbuf.c:140
void packetbuf_compact(void)
Compact the packetbuf.
Definition: packetbuf.c:105
void packetbuf_clear_hdr(void)
Clear and reset the header of the packetbuf.
Definition: packetbuf.c:87
int packetbuf_copyto_hdr(uint8_t *to)
Copy the header portion of the packetbuf to an external buffer.
Definition: packetbuf.c:123
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:77
void packetbuf_reference(void *ptr, uint16_t len)
Point the packetbuf to external data.
Definition: packetbuf.c:219
Common functionality of 802.15.4-compliant llsec_drivers.
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
int packetbuf_is_reference(void)
Check if the packetbuf references external data.
Definition: packetbuf.c:227
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:188
Header file for the Rime address representation