Contiki-NG
Loading...
Searching...
No Matches
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"
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 */
79void 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 */
90void *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 */
97void *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 */
104uint8_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 */
112uint16_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 */
119uint16_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 */
126uint16_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 */
132void 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 */
147int 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 */
165int 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 */
179int 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 */
193int packetbuf_hdrreduce(int size);
194
195/* Packet attributes stuff below: */
196
197typedef uint16_t packetbuf_attr_t;
198
199struct packetbuf_attr {
200 packetbuf_attr_t val;
201};
202struct packetbuf_addr {
203 linkaddr_t addr;
204};
205
206enum {
207 PACKETBUF_ATTR_NONE,
208
209 /* Scope 0 attributes: used only on the local node. */
210 PACKETBUF_ATTR_CHANNEL,
211 PACKETBUF_ATTR_NETWORK_ID,
212 PACKETBUF_ATTR_LINK_QUALITY,
213 PACKETBUF_ATTR_RSSI,
214 PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS,
215 PACKETBUF_ATTR_MAC_SEQNO,
216 PACKETBUF_ATTR_MAC_ACK,
217 PACKETBUF_ATTR_MAC_METADATA,
218 PACKETBUF_ATTR_MAC_NO_SRC_ADDR,
219 PACKETBUF_ATTR_MAC_NO_DEST_ADDR,
220#if TSCH_WITH_LINK_SELECTOR
221 PACKETBUF_ATTR_TSCH_SLOTFRAME,
222 PACKETBUF_ATTR_TSCH_TIMESLOT,
223 PACKETBUF_ATTR_TSCH_CHANNEL_OFFSET,
224#endif /* TSCH_WITH_LINK_SELECTOR */
225
226 /* Scope 1 attributes: used between two neighbors only. */
227 PACKETBUF_ATTR_FRAME_TYPE,
228#if LLSEC802154_USES_AUX_HEADER
229 PACKETBUF_ATTR_SECURITY_LEVEL,
230#endif /* LLSEC802154_USES_AUX_HEADER */
231#if LLSEC802154_USES_EXPLICIT_KEYS
232 PACKETBUF_ATTR_KEY_ID_MODE,
233 PACKETBUF_ATTR_KEY_INDEX,
234#endif /* LLSEC802154_USES_EXPLICIT_KEYS */
235
236#if LLSEC802154_USES_FRAME_COUNTER
237 PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1,
238 PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3,
239#endif /* LLSEC802154_USES_FRAME_COUNTER */
240
241 /* Scope 2 attributes: used between end-to-end nodes. */
242 /* These must be last */
243 PACKETBUF_ADDR_SENDER,
244 PACKETBUF_ADDR_RECEIVER,
245
246 PACKETBUF_ATTR_MAX
247};
248
249#define PACKETBUF_NUM_ADDRS 2
250#define PACKETBUF_NUM_ATTRS (PACKETBUF_ATTR_MAX - PACKETBUF_NUM_ADDRS)
251#define PACKETBUF_ADDR_FIRST PACKETBUF_ADDR_SENDER
252
253#define PACKETBUF_IS_ADDR(type) ((type) >= PACKETBUF_ADDR_FIRST)
254
255void packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val);
256packetbuf_attr_t packetbuf_attr(uint8_t type);
257void packetbuf_set_addr(uint8_t type, const linkaddr_t *addr);
258const linkaddr_t *packetbuf_addr(uint8_t type);
259
260/**
261 * \brief Checks whether the current packet is a broadcast.
262 * \retval true iff the current packet is a broadcast
263 */
265
266void packetbuf_attr_clear(void);
267
268void packetbuf_attr_copyto(struct packetbuf_attr *attrs,
269 struct packetbuf_addr *addrs);
270void packetbuf_attr_copyfrom(struct packetbuf_attr *attrs,
271 struct packetbuf_addr *addrs);
272
273#define PACKETBUF_ATTRIBUTES(...) { __VA_ARGS__ PACKETBUF_ATTR_LAST }
274#define PACKETBUF_ATTR_LAST { PACKETBUF_ATTR_NONE, 0 }
275
276#define PACKETBUF_ATTR_BIT 1
277#define PACKETBUF_ATTR_BYTE 8
278#define PACKETBUF_ADDRSIZE (LINKADDR_SIZE * PACKETBUF_ATTR_BYTE)
279
280#define PACKETBUF_ATTR_SECURITY_LEVEL_DEFAULT 0xffff
281
282struct packetbuf_attrlist {
283 uint8_t type;
284 uint8_t len;
285};
286
287#endif /* PACKETBUF_H_ */
288/** @} */
289/** @} */
LLSEC802154 Security related configuration.
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition packetbuf.c:136
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition packetbuf.c:143
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition packetbuf.c:167
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition packetbuf.c:155
uint8_t packetbuf_hdrlen(void)
Get the length of the header in the packetbuf.
Definition packetbuf.c:161
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
Definition packetbuf.c:84
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition packetbuf.c:149
bool packetbuf_holds_broadcast(void)
Checks whether the current packet is a broadcast.
Definition packetbuf.c:229
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition packetbuf.c:75
int packetbuf_copyto(void *to)
Copy the entire packetbuf to an external buffer.
Definition packetbuf.c:96
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition packetbuf.c:107
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition packetbuf.c:124
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.
Common functionality of 802.15.4-compliant llsec_drivers.
TSCH configuration.
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition uip-nd6.c:107