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"
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 /**
70  * \brief Clear and reset the packetbuf
71  *
72  * This function clears the packetbuf and resets all
73  * internal state pointers (header size, header pointer,
74  * external data pointer). It is used before preparing a
75  * packet in the packetbuf.
76  *
77  */
78 void packetbuf_clear(void);
79 
80 /**
81  * \brief Get a pointer to the data in the packetbuf
82  * \return Pointer to the packetbuf data
83  *
84  * This function is used to get a pointer to the data in
85  * the packetbuf. The data is either stored in the packetbuf,
86  * or referenced to an external location.
87  *
88  */
89 void *packetbuf_dataptr(void);
90 
91 /**
92  * \brief Get a pointer to the header in the packetbuf, for outbound packets
93  * \return Pointer to the packetbuf header
94  *
95  */
96 void *packetbuf_hdrptr(void);
97 
98 /**
99  * \brief Get the length of the header in the packetbuf
100  * \return Length of the header in the packetbuf
101  *
102  */
103 uint8_t packetbuf_hdrlen(void);
104 
105 
106 /**
107  * \brief Get the length of the data in the packetbuf
108  * \return Length of the data in the packetbuf
109  *
110  */
111 uint16_t packetbuf_datalen(void);
112 
113 /**
114  * \brief Get the total length of the header and data in the packetbuf
115  * \return Length of data and header in the packetbuf
116  *
117  */
118 uint16_t packetbuf_totlen(void);
119 
120 /**
121  * \brief Get the total length of the remaining space in the packetbuf
122  * \return Length of the remaining space in the packetbuf
123  *
124  */
125 uint16_t packetbuf_remaininglen(void);
126 
127 /**
128  * \brief Set the length of the data in the packetbuf
129  * \param len The length of the data
130  */
131 void packetbuf_set_datalen(uint16_t len);
132 
133 /**
134  * \brief Copy from external data into the packetbuf
135  * \param from A pointer to the data from which to copy
136  * \param len The size of the data to copy
137  * \retval The number of bytes that was copied into the packetbuf
138  *
139  * This function copies data from a pointer into the
140  * packetbuf. If the data that is to be copied is larger
141  * than the packetbuf, only the data that fits in the
142  * packetbuf is copied. The number of bytes that could be
143  * copied into the rimbuf is returned.
144  *
145  */
146 int packetbuf_copyfrom(const void *from, uint16_t len);
147 
148 /**
149  * \brief Copy the entire packetbuf to an external buffer
150  * \param to A pointer to the buffer to which the data is to be copied
151  * \retval The number of bytes that was copied to the external buffer
152  *
153  * This function copies the packetbuf to an external
154  * buffer. Both the data portion and the header portion of
155  * the packetbuf is copied.
156  *
157  * The external buffer to which the packetbuf is to be
158  * copied must be able to accomodate at least
159  * PACKETBUF_SIZE bytes. The number of
160  * bytes that was copied to the external buffer is
161  * returned.
162  *
163  */
164 int packetbuf_copyto(void *to);
165 
166 /**
167  * \brief Extend the header of the packetbuf, for outbound packets
168  * \param size The number of bytes the header should be extended
169  * \retval Non-zero if the header could be extended, zero otherwise
170  *
171  * This function is used to allocate extra space in the
172  * header portion in the packetbuf, when preparing outbound
173  * packets for transmission. If the function is unable to
174  * allocate sufficient header space, the function returns
175  * zero and does not allocate anything.
176  *
177  */
178 int packetbuf_hdralloc(int size);
179 
180 /**
181  * \brief Reduce the header in the packetbuf, for incoming packets
182  * \param size The number of bytes the header should be reduced
183  * \retval Non-zero if the header could be reduced, zero otherwise
184  *
185  * This function is used to remove the first part of the
186  * header in the packetbuf, when processing incoming
187  * packets. If the function is unable to remove the
188  * requested amount of header space, the function returns
189  * zero and does not allocate anything.
190  *
191  */
192 int packetbuf_hdrreduce(int size);
193 
194 /* Packet attributes stuff below: */
195 
196 typedef uint16_t packetbuf_attr_t;
197 
198 struct packetbuf_attr {
199  packetbuf_attr_t val;
200 };
201 struct packetbuf_addr {
202  linkaddr_t addr;
203 };
204 
205 #define PACKETBUF_ATTR_PACKET_TYPE_DATA 0
206 #define PACKETBUF_ATTR_PACKET_TYPE_ACK 1
207 #define PACKETBUF_ATTR_PACKET_TYPE_STREAM 2
208 #define PACKETBUF_ATTR_PACKET_TYPE_STREAM_END 3
209 #define PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP 4
210 
211 enum {
212  PACKETBUF_ATTR_NONE,
213 
214  /* Scope 0 attributes: used only on the local node. */
215  PACKETBUF_ATTR_CHANNEL,
216  PACKETBUF_ATTR_NETWORK_ID,
217  PACKETBUF_ATTR_LINK_QUALITY,
218  PACKETBUF_ATTR_RSSI,
219  PACKETBUF_ATTR_TIMESTAMP,
220  PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS,
221  PACKETBUF_ATTR_MAC_SEQNO,
222  PACKETBUF_ATTR_MAC_ACK,
223  PACKETBUF_ATTR_MAC_METADATA,
224  PACKETBUF_ATTR_MAC_NO_SRC_ADDR,
225  PACKETBUF_ATTR_MAC_NO_DEST_ADDR,
226 #if TSCH_WITH_LINK_SELECTOR
227  PACKETBUF_ATTR_TSCH_SLOTFRAME,
228  PACKETBUF_ATTR_TSCH_TIMESLOT,
229 #endif /* TSCH_WITH_LINK_SELECTOR */
230 
231  /* Scope 1 attributes: used between two neighbors only. */
232  PACKETBUF_ATTR_FRAME_TYPE,
233 #if LLSEC802154_USES_AUX_HEADER
234  PACKETBUF_ATTR_SECURITY_LEVEL,
235 #endif /* LLSEC802154_USES_AUX_HEADER */
236 #if LLSEC802154_USES_EXPLICIT_KEYS
237  PACKETBUF_ATTR_KEY_ID_MODE,
238  PACKETBUF_ATTR_KEY_INDEX,
239 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
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 
255 int packetbuf_set_attr(uint8_t type, const packetbuf_attr_t val);
256 packetbuf_attr_t packetbuf_attr(uint8_t type);
257 int packetbuf_set_addr(uint8_t type, const linkaddr_t *addr);
258 const linkaddr_t *packetbuf_addr(uint8_t type);
259 
260 /**
261  * \brief Checks whether the current packet is a broadcast.
262  * \retval 0 iff current packet is not a broadcast
263  */
264 int packetbuf_holds_broadcast(void);
265 
266 void packetbuf_attr_clear(void);
267 
268 void packetbuf_attr_copyto(struct packetbuf_attr *attrs,
269  struct packetbuf_addr *addrs);
270 void 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 struct packetbuf_attrlist {
281  uint8_t type;
282  uint8_t len;
283 };
284 
285 #endif /* PACKETBUF_H_ */
286 /** @} */
287 /** @} */
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: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
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
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