Contiki-NG
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 Packet buffer (packetbuf) management
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup net
42  * @{
43  */
44 
45 /**
46  * \defgroup packetbuf Packet buffer
47  * @{
48  *
49  * The packetbuf module does Contiki's buffer management.
50  */
51 
52 #ifndef PACKETBUF_H_
53 #define PACKETBUF_H_
54 
55 #include "contiki.h"
56 #include "net/linkaddr.h"
57 #include "net/mac/llsec802154.h"
59 #include "net/mac/tsch/tsch-conf.h"
60 
61 /**
62  * \brief The size of the packetbuf, in bytes
63  */
64 #ifdef PACKETBUF_CONF_SIZE
65 #define PACKETBUF_SIZE PACKETBUF_CONF_SIZE
66 #else
67 #define PACKETBUF_SIZE 128
68 #endif
69 
70 /**
71  * \brief Clear and reset the packetbuf
72  *
73  * This function clears the packetbuf and resets all
74  * internal state pointers (header size, header pointer,
75  * external data pointer). It is used before preparing a
76  * packet in the packetbuf.
77  *
78  */
79 void packetbuf_clear(void);
80 
81 /**
82  * \brief Get a pointer to the data in the packetbuf
83  * \return Pointer to the packetbuf data
84  *
85  * This function is used to get a pointer to the data in
86  * the packetbuf. The data is either stored in the packetbuf,
87  * or referenced to an external location.
88  *
89  */
90 void *packetbuf_dataptr(void);
91 
92 /**
93  * \brief Get a pointer to the header in the packetbuf, for outbound packets
94  * \return Pointer to the packetbuf header
95  *
96  */
97 void *packetbuf_hdrptr(void);
98 
99 /**
100  * \brief Get the length of the header in the packetbuf
101  * \return Length of the header in the packetbuf
102  *
103  */
104 uint8_t packetbuf_hdrlen(void);
105 
106 
107 /**
108  * \brief Get the length of the data in the packetbuf
109  * \return Length of the data in the packetbuf
110  *
111  */
112 uint16_t packetbuf_datalen(void);
113 
114 /**
115  * \brief Get the total length of the header and data in the packetbuf
116  * \return Length of data and header in the packetbuf
117  *
118  */
119 uint16_t packetbuf_totlen(void);
120 
121 /**
122  * \brief Get the total length of the remaining space in the packetbuf
123  * \return Length of the remaining space in the packetbuf
124  *
125  */
126 uint16_t packetbuf_remaininglen(void);
127 
128 /**
129  * \brief Set the length of the data in the packetbuf
130  * \param len The length of the data
131  */
132 void packetbuf_set_datalen(uint16_t len);
133 
134 /**
135  * \brief Copy from external data into the packetbuf
136  * \param from A pointer to the data from which to copy
137  * \param len The size of the data to copy
138  * \retval The number of bytes that was copied into the packetbuf
139  *
140  * This function copies data from a pointer into the
141  * packetbuf. If the data that is to be copied is larger
142  * than the packetbuf, only the data that fits in the
143  * packetbuf is copied. The number of bytes that could be
144  * copied into the rimbuf is returned.
145  *
146  */
147 int packetbuf_copyfrom(const void *from, uint16_t len);
148 
149 /**
150  * \brief Copy the entire packetbuf to an external buffer
151  * \param to A pointer to the buffer to which the data is to be copied
152  * \retval The number of bytes that was copied to the external buffer
153  *
154  * This function copies the packetbuf to an external
155  * buffer. Both the data portion and the header portion of
156  * the packetbuf is copied.
157  *
158  * The external buffer to which the packetbuf is to be
159  * copied must be able to accomodate at least
160  * PACKETBUF_SIZE bytes. The number of
161  * bytes that was copied to the external buffer is
162  * returned.
163  *
164  */
165 int packetbuf_copyto(void *to);
166 
167 /**
168  * \brief Extend the header of the packetbuf, for outbound packets
169  * \param size The number of bytes the header should be extended
170  * \retval Non-zero if the header could be extended, zero otherwise
171  *
172  * This function is used to allocate extra space in the
173  * header portion in the packetbuf, when preparing outbound
174  * packets for transmission. If the function is unable to
175  * allocate sufficient header space, the function returns
176  * zero and does not allocate anything.
177  *
178  */
179 int packetbuf_hdralloc(int size);
180 
181 /**
182  * \brief Reduce the header in the packetbuf, for incoming packets
183  * \param size The number of bytes the header should be reduced
184  * \retval Non-zero if the header could be reduced, zero otherwise
185  *
186  * This function is used to remove the first part of the
187  * header in the packetbuf, when processing incoming
188  * packets. If the function is unable to remove the
189  * requested amount of header space, the function returns
190  * zero and does not allocate anything.
191  *
192  */
193 int packetbuf_hdrreduce(int size);
194 
195 /* Packet attributes stuff below: */
196 
197 typedef uint16_t packetbuf_attr_t;
198 
199 struct packetbuf_attr {
200  packetbuf_attr_t val;
201 };
202 struct packetbuf_addr {
203  linkaddr_t addr;
204 };
205 
206 #define PACKETBUF_ATTR_PACKET_TYPE_DATA 0
207 #define PACKETBUF_ATTR_PACKET_TYPE_ACK 1
208 #define PACKETBUF_ATTR_PACKET_TYPE_STREAM 2
209 #define PACKETBUF_ATTR_PACKET_TYPE_STREAM_END 3
210 #define PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP 4
211 
212 enum {
213  PACKETBUF_ATTR_NONE,
214 
215  /* Scope 0 attributes: used only on the local node. */
216  PACKETBUF_ATTR_CHANNEL,
217  PACKETBUF_ATTR_NETWORK_ID,
218  PACKETBUF_ATTR_LINK_QUALITY,
219  PACKETBUF_ATTR_RSSI,
220  PACKETBUF_ATTR_TIMESTAMP,
221  PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS,
222  PACKETBUF_ATTR_MAC_SEQNO,
223  PACKETBUF_ATTR_MAC_ACK,
224  PACKETBUF_ATTR_MAC_METADATA,
225  PACKETBUF_ATTR_MAC_NO_SRC_ADDR,
226  PACKETBUF_ATTR_MAC_NO_DEST_ADDR,
227 #if TSCH_WITH_LINK_SELECTOR
228  PACKETBUF_ATTR_TSCH_SLOTFRAME,
229  PACKETBUF_ATTR_TSCH_TIMESLOT,
230 #endif /* TSCH_WITH_LINK_SELECTOR */
231 
232  /* Scope 1 attributes: used between two neighbors only. */
233  PACKETBUF_ATTR_FRAME_TYPE,
234 #if LLSEC802154_USES_AUX_HEADER
235  PACKETBUF_ATTR_SECURITY_LEVEL,
236 #endif /* LLSEC802154_USES_AUX_HEADER */
237 #if LLSEC802154_USES_EXPLICIT_KEYS
238  PACKETBUF_ATTR_KEY_ID_MODE,
239  PACKETBUF_ATTR_KEY_INDEX,
240 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
241 
242 #if LLSEC802154_USES_FRAME_COUNTER
243  PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1,
244  PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3,
245 #endif /* LLSEC802154_USES_FRAME_COUNTER */
246 
247  /* Scope 2 attributes: used between end-to-end nodes. */
248  /* These must be last */
249  PACKETBUF_ADDR_SENDER,
250  PACKETBUF_ADDR_RECEIVER,
251 
252  PACKETBUF_ATTR_MAX
253 };
254 
255 #define PACKETBUF_NUM_ADDRS 2
256 #define PACKETBUF_NUM_ATTRS (PACKETBUF_ATTR_MAX - PACKETBUF_NUM_ADDRS)
257 #define PACKETBUF_ADDR_FIRST PACKETBUF_ADDR_SENDER
258 
259 #define PACKETBUF_IS_ADDR(type) ((type) >= PACKETBUF_ADDR_FIRST)
260 
261 int packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val);
262 packetbuf_attr_t packetbuf_attr(uint8_t type);
263 int packetbuf_set_addr(uint8_t type, const linkaddr_t *addr);
264 const linkaddr_t *packetbuf_addr(uint8_t type);
265 
266 /**
267  * \brief Checks whether the current packet is a broadcast.
268  * \retval 0 iff current packet is not a broadcast
269  */
270 int packetbuf_holds_broadcast(void);
271 
272 void packetbuf_attr_clear(void);
273 
274 void packetbuf_attr_copyto(struct packetbuf_attr *attrs,
275  struct packetbuf_addr *addrs);
276 void packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
277  struct packetbuf_addr *addrs);
278 
279 #define PACKETBUF_ATTRIBUTES(...) { __VA_ARGS__ PACKETBUF_ATTR_LAST }
280 #define PACKETBUF_ATTR_LAST { PACKETBUF_ATTR_NONE, 0 }
281 
282 #define PACKETBUF_ATTR_BIT 1
283 #define PACKETBUF_ATTR_BYTE 8
284 #define PACKETBUF_ADDRSIZE (LINKADDR_SIZE * PACKETBUF_ATTR_BYTE)
285 
286 #define PACKETBUF_ATTR_SECURITY_LEVEL_DEFAULT 0xffff
287 
288 struct packetbuf_attrlist {
289  uint8_t type;
290  uint8_t len;
291 };
292 
293 #endif /* PACKETBUF_H_ */
294 /** @} */
295 /** @} */
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:143
TSCH configuration.
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:107
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
Header file for the link-layer address representation
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:124
LLSEC802154 Security related configuration
Common functionality of 802.15.4-compliant llsec_drivers.
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
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
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 * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:149
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:136