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 #include "net/mac/tsch/tsch-conf.h"
59 
60 /**
61  * \brief The size of the packetbuf, in bytes
62  */
63 #ifdef PACKETBUF_CONF_SIZE
64 #define PACKETBUF_SIZE PACKETBUF_CONF_SIZE
65 #else
66 #define PACKETBUF_SIZE 128
67 #endif
68 
69 #ifdef PACKETBUF_CONF_WITH_PACKET_TYPE
70 #define PACKETBUF_WITH_PACKET_TYPE PACKETBUF_CONF_WITH_PACKET_TYPE
71 #else
72 #define PACKETBUF_WITH_PACKET_TYPE NETSTACK_CONF_WITH_RIME
73 #endif
74 
75 /**
76  * \brief Clear and reset the packetbuf
77  *
78  * This function clears the packetbuf and resets all
79  * internal state pointers (header size, header pointer,
80  * external data pointer). It is used before preparing a
81  * packet in the packetbuf.
82  *
83  */
84 void packetbuf_clear(void);
85 
86 /**
87  * \brief Get a pointer to the data in the packetbuf
88  * \return Pointer to the packetbuf data
89  *
90  * This function is used to get a pointer to the data in
91  * the packetbuf. The data is either stored in the packetbuf,
92  * or referenced to an external location.
93  *
94  */
95 void *packetbuf_dataptr(void);
96 
97 /**
98  * \brief Get a pointer to the header in the packetbuf, for outbound packets
99  * \return Pointer to the packetbuf header
100  *
101  */
102 void *packetbuf_hdrptr(void);
103 
104 /**
105  * \brief Get the length of the header in the packetbuf
106  * \return Length of the header in the packetbuf
107  *
108  */
109 uint8_t packetbuf_hdrlen(void);
110 
111 
112 /**
113  * \brief Get the length of the data in the packetbuf
114  * \return Length of the data in the packetbuf
115  *
116  */
117 uint16_t packetbuf_datalen(void);
118 
119 /**
120  * \brief Get the total length of the header and data in the packetbuf
121  * \return Length of data and header in the packetbuf
122  *
123  */
124 uint16_t packetbuf_totlen(void);
125 
126 /**
127  * \brief Set the length of the data in the packetbuf
128  * \param len The length of the data
129  */
130 void packetbuf_set_datalen(uint16_t len);
131 
132 /**
133  * \brief Compact the packetbuf
134  *
135  * This function compacts the packetbuf by copying the data
136  * portion of the packetbuf so that becomes consecutive to
137  * the header.
138  *
139  * This function is called by the Rime code before a
140  * packet is to be sent by a device driver. This assures
141  * that the entire packet is consecutive in memory.
142  *
143  */
144 void packetbuf_compact(void);
145 
146 /**
147  * \brief Copy from external data into the packetbuf
148  * \param from A pointer to the data from which to copy
149  * \param len The size of the data to copy
150  * \retval The number of bytes that was copied into the packetbuf
151  *
152  * This function copies data from a pointer into the
153  * packetbuf. If the data that is to be copied is larger
154  * than the packetbuf, only the data that fits in the
155  * packetbuf is copied. The number of bytes that could be
156  * copied into the rimbuf is returned.
157  *
158  */
159 int packetbuf_copyfrom(const void *from, uint16_t len);
160 
161 /**
162  * \brief Copy the entire packetbuf to an external buffer
163  * \param to A pointer to the buffer to which the data is to be copied
164  * \retval The number of bytes that was copied to the external buffer
165  *
166  * This function copies the packetbuf to an external
167  * buffer. Both the data portion and the header portion of
168  * the packetbuf is copied.
169  *
170  * The external buffer to which the packetbuf is to be
171  * copied must be able to accomodate at least
172  * PACKETBUF_SIZE bytes. The number of
173  * bytes that was copied to the external buffer is
174  * returned.
175  *
176  */
177 int packetbuf_copyto(void *to);
178 
179 /**
180  * \brief Extend the header of the packetbuf, for outbound packets
181  * \param size The number of bytes the header should be extended
182  * \retval Non-zero if the header could be extended, zero otherwise
183  *
184  * This function is used to allocate extra space in the
185  * header portion in the packetbuf, when preparing outbound
186  * packets for transmission. If the function is unable to
187  * allocate sufficient header space, the function returns
188  * zero and does not allocate anything.
189  *
190  */
191 int packetbuf_hdralloc(int size);
192 
193 /**
194  * \brief Reduce the header in the packetbuf, for incoming packets
195  * \param size The number of bytes the header should be reduced
196  * \retval Non-zero if the header could be reduced, zero otherwise
197  *
198  * This function is used to remove the first part of the
199  * header in the packetbuf, when processing incoming
200  * packets. If the function is unable to remove the
201  * requested amount of header space, the function returns
202  * zero and does not allocate anything.
203  *
204  */
205 int packetbuf_hdrreduce(int size);
206 
207 /* Packet attributes stuff below: */
208 
209 typedef uint16_t packetbuf_attr_t;
210 
211 struct packetbuf_attr {
212  packetbuf_attr_t val;
213 };
214 struct packetbuf_addr {
215  linkaddr_t addr;
216 };
217 
218 #define PACKETBUF_ATTR_PACKET_TYPE_DATA 0
219 #define PACKETBUF_ATTR_PACKET_TYPE_ACK 1
220 #define PACKETBUF_ATTR_PACKET_TYPE_STREAM 2
221 #define PACKETBUF_ATTR_PACKET_TYPE_STREAM_END 3
222 #define PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP 4
223 
224 enum {
225  PACKETBUF_ATTR_NONE,
226 
227  /* Scope 0 attributes: used only on the local node. */
228  PACKETBUF_ATTR_CHANNEL,
229  PACKETBUF_ATTR_NETWORK_ID,
230  PACKETBUF_ATTR_LINK_QUALITY,
231  PACKETBUF_ATTR_RSSI,
232  PACKETBUF_ATTR_TIMESTAMP,
233  PACKETBUF_ATTR_RADIO_TXPOWER,
234  PACKETBUF_ATTR_LISTEN_TIME,
235  PACKETBUF_ATTR_TRANSMIT_TIME,
236  PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS,
237  PACKETBUF_ATTR_MAC_SEQNO,
238  PACKETBUF_ATTR_MAC_ACK,
239  PACKETBUF_ATTR_IS_CREATED_AND_SECURED,
240 #if TSCH_WITH_LINK_SELECTOR
241  PACKETBUF_ATTR_TSCH_SLOTFRAME,
242  PACKETBUF_ATTR_TSCH_TIMESLOT,
243 #endif /* TSCH_WITH_LINK_SELECTOR */
244 
245  /* Scope 1 attributes: used between two neighbors only. */
246 #if PACKETBUF_WITH_PACKET_TYPE
247  PACKETBUF_ATTR_PACKET_TYPE,
248 #endif
249 #if NETSTACK_CONF_WITH_RIME
250  PACKETBUF_ATTR_PACKET_ID,
251  PACKETBUF_ATTR_RELIABLE,
252  PACKETBUF_ATTR_REXMIT,
253  PACKETBUF_ATTR_MAX_REXMIT,
254  PACKETBUF_ATTR_NUM_REXMIT,
255 #endif /* NETSTACK_CONF_WITH_RIME */
256  PACKETBUF_ATTR_PENDING,
257  PACKETBUF_ATTR_FRAME_TYPE,
258 #if LLSEC802154_USES_AUX_HEADER
259  PACKETBUF_ATTR_SECURITY_LEVEL,
260 #endif /* LLSEC802154_USES_AUX_HEADER */
261 #if LLSEC802154_USES_FRAME_COUNTER
262  PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1,
263  PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3,
264 #endif /* LLSEC802154_USES_FRAME_COUNTER */
265 #if LLSEC802154_USES_EXPLICIT_KEYS
266  PACKETBUF_ATTR_KEY_ID_MODE,
267  PACKETBUF_ATTR_KEY_INDEX,
268  PACKETBUF_ATTR_KEY_SOURCE_BYTES_0_1,
269 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
270 
271  /* Scope 2 attributes: used between end-to-end nodes. */
272 #if NETSTACK_CONF_WITH_RIME
273  PACKETBUF_ATTR_HOPS,
274  PACKETBUF_ATTR_TTL,
275  PACKETBUF_ATTR_EPACKET_ID,
276  PACKETBUF_ATTR_EPACKET_TYPE,
277  PACKETBUF_ATTR_ERELIABLE,
278 #endif /* NETSTACK_CONF_WITH_RIME */
279 
280  /* These must be last */
281  PACKETBUF_ADDR_SENDER,
282  PACKETBUF_ADDR_RECEIVER,
283 #if NETSTACK_CONF_WITH_RIME
284  PACKETBUF_ADDR_ESENDER,
285  PACKETBUF_ADDR_ERECEIVER,
286 #endif /* NETSTACK_CONF_WITH_RIME */
287 
288  PACKETBUF_ATTR_MAX
289 };
290 
291 #if NETSTACK_CONF_WITH_RIME
292 #define PACKETBUF_NUM_ADDRS 4
293 #else /* NETSTACK_CONF_WITH_RIME */
294 #define PACKETBUF_NUM_ADDRS 2
295 #endif /* NETSTACK_CONF_WITH_RIME */
296 #define PACKETBUF_NUM_ATTRS (PACKETBUF_ATTR_MAX - PACKETBUF_NUM_ADDRS)
297 #define PACKETBUF_ADDR_FIRST PACKETBUF_ADDR_SENDER
298 
299 #define PACKETBUF_IS_ADDR(type) ((type) >= PACKETBUF_ADDR_FIRST)
300 
301 #if PACKETBUF_CONF_ATTRS_INLINE
302 
303 extern struct packetbuf_attr packetbuf_attrs[];
304 extern struct packetbuf_addr packetbuf_addrs[];
305 
306 static inline int
307 packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
308 {
309  packetbuf_attrs[type].val = val;
310  return 1;
311 }
312 static inline packetbuf_attr_t
313 packetbuf_attr(uint8_t type)
314 {
315  return packetbuf_attrs[type].val;
316 }
317 
318 static inline int
319 packetbuf_set_addr(uint8_t type, const linkaddr_t *addr)
320 {
321  linkaddr_copy(&packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr, addr);
322  return 1;
323 }
324 
325 static inline const linkaddr_t *
326 packetbuf_addr(uint8_t type)
327 {
328  return &packetbuf_addrs[type - PACKETBUF_ADDR_FIRST].addr;
329 }
330 #else /* PACKETBUF_CONF_ATTRS_INLINE */
331 int packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val);
332 packetbuf_attr_t packetbuf_attr(uint8_t type);
333 int packetbuf_set_addr(uint8_t type, const linkaddr_t *addr);
334 const linkaddr_t *packetbuf_addr(uint8_t type);
335 #endif /* PACKETBUF_CONF_ATTRS_INLINE */
336 
337 /**
338  * \brief Checks whether the current packet is a broadcast.
339  * \retval 0 iff current packet is not a broadcast
340  */
341 int packetbuf_holds_broadcast(void);
342 
343 void packetbuf_attr_clear(void);
344 
345 void packetbuf_attr_copyto(struct packetbuf_attr *attrs,
346  struct packetbuf_addr *addrs);
347 void packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
348  struct packetbuf_addr *addrs);
349 
350 #define PACKETBUF_ATTRIBUTES(...) { __VA_ARGS__ PACKETBUF_ATTR_LAST }
351 #define PACKETBUF_ATTR_LAST { PACKETBUF_ATTR_NONE, 0 }
352 
353 #define PACKETBUF_ATTR_BIT 1
354 #define PACKETBUF_ATTR_BYTE 8
355 #define PACKETBUF_ADDRSIZE (LINKADDR_SIZE * PACKETBUF_ATTR_BYTE)
356 
357 struct packetbuf_attrlist {
358  uint8_t type;
359  uint8_t len;
360 };
361 
362 #endif /* PACKETBUF_H_ */
363 /** @} */
364 /** @} */
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
TSCH configuration
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:182
Common functionality of 802.15.4-compliant llsec_drivers.
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
void packetbuf_compact(void)
Compact the packetbuf.
Definition: packetbuf.c:97
Header file for the Rime address representation
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
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