Contiki 3.x
packetqueue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, 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 packetqueue module
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rime
42  * @{
43  */
44 
45 /**
46  * \defgroup packetqueue Packet queue
47  * @{
48  *
49  * The packetqueue module handles a list of queued packets.
50  *
51  */
52 
53 #ifndef PACKETQUEUE_H_
54 #define PACKETQUEUE_H_
55 
56 #include "lib/list.h"
57 #include "lib/memb.h"
58 
59 #include "sys/ctimer.h"
60 
61 #include "net/packetbuf.h"
62 #include "net/queuebuf.h"
63 
64 /**
65  * \brief Representation of a packet queue.
66  *
67  * This structure holds the state of a packet queue. It is
68  * an opaque structure with no user-visible elements.
69  */
70 struct packetqueue {
71  list_t *list;
72  struct memb *memb;
73 };
74 
75 /**
76  * \brief Representation of an item in a packet queue.
77  *
78  * This structure holds the state of a packet queue. It is
79  * an opaque structure with no user-visible elements. The
80  * function packetqueue_queuebuf() is used to extract a
81  * \ref queuebuf "queubuf" from the item. The function
82  * packetqueue_ptr() is used to extract the opaque pointer
83  * that was registered with the
84  * packetqueue_enqueue_packetbuf() function.
85  */
87  struct packetqueue_item *next;
88  struct queuebuf *buf;
89  struct packetqueue *queue;
90  struct ctimer lifetimer;
91  void *ptr;
92 };
93 
94 
95 /**
96  * \brief Define a packet queue.
97  * \param name The variable name of the packet queue
98  * \param size The maximum size of the packet queue
99  *
100  * This statement defines a packet queue. A packet queue
101  * is defined on a per-module basis.
102  *
103  */
104 #define PACKETQUEUE(name, size) LIST(name##_list); \
105  MEMB(name##_memb, struct packetqueue_item, size); \
106  static struct packetqueue name = { &name##_list, \
107  &name##_memb }
108 
109 /**
110  * \name Packet queue functions.
111  * @{
112  */
113 /**
114  * \brief Initialize a packet queue.
115  * \param q A pointer to a struct packetqueue that was defined with PACKETQUEUE().
116  *
117  * This function initializes a packetqueue that has
118  * previously been defined with PACKETQUEUE().
119  *
120  */
121 void packetqueue_init(struct packetqueue *q);
122 
123 
124 /**
125  * \brief Enqueue a packetbuf on a packet queue.
126  * \param q A pointer to a struct packetqueue.
127  * \param lifetime The maximum time that the packet should stay in the packet queue, or zero if the packet should stay on the packet queue indefinitely.
128  * \param ptr An opaque, user-defined pointer that can be used to identify the packet when it later is dequeued.
129  * \retval Zero If memory could not be allocated for the packet.
130  * \retval Non-zero If the packet was successfully enqueued.
131  *
132  *
133  * This function enqueues the \ref packetbuf "packetbuf"
134  * to the packet queue pointed to by the q parameter. The
135  * packet queue must previously have been defined with
136  * PACKETQUEUE() and initialized with packetqueue_init().
137  *
138  * Each packet queue item has a maximum lifetime. When the
139  * lifetime expires, the packet queue item is
140  * automatically removed from the packet queue. If the
141  * lifetime parameter is given as zero, the packet never
142  * times out from the packet queue.
143  *
144  * Each packet queue item is tagged with a user-defined
145  * pointer. This pointer can be used to identify packets
146  * as they later are dequeued from the queue. This is
147  * useful if two modules is using the same packet queue:
148  * the modules can use the pointer to distinguish to which
149  * module a dequeued packet belongs.
150  *
151  */
152 int packetqueue_enqueue_packetbuf(struct packetqueue *q, clock_time_t lifetime,
153  void *ptr);
154 
155 /**
156  * \brief Access the first item on the packet buffer.
157  * \param q A pointer to a struct packetqueue.
158  * \return A pointer to the first item on the packet queue.
159  *
160  * This function returns the first item on the packet
161  * queue. The packet queue is unchanged by this
162  * function. To dequeue the first item on the list, use
163  * the packetqueue_dequeue() function.
164  *
165  */
167 
168 /**
169  * \brief Remove the first item on the packet buffer.
170  * \param q A pointer to a struct packetqueue.
171  *
172  * This function removes the first item on the packet
173  * queue. The function does not return the first item: to
174  * access the first item, the packetqueue_first() function
175  * must have been used prior to calling
176  * packetqueue_dequeue().
177  *
178  */
179 void packetqueue_dequeue(struct packetqueue *q);
180 
181 /**
182  * \brief Get the length of the packet queue
183  * \param q A pointer to a struct packetqueue.
184  * \return The number of packets queued on the packet queue
185  *
186  * This function returns the number of packets that are
187  * queued on the packet queue.
188  *
189  */
190 int packetqueue_len(struct packetqueue *q);
191 
192 /**
193  * @}
194  */
195 
196 /**
197  * \name Packet queue item functions
198  * @{
199  */
200 
201 /**
202  * \brief Access the queuebuf in a packet queue item.
203  * \param i A packet queue item, obtained with packetqueue_first().
204  * \return A pointer to the queuebuf in the packet queue item.
205  */
206 struct queuebuf *packetqueue_queuebuf(struct packetqueue_item *i);
207 /**
208  * \brief Access the user-defined pointer in a packet queue item.
209  * \param i A packet queue item, obtained with packetqueue_first().
210  * \return A pointer to the user-defined pointer in the packet queue item.
211  */
212 
213 void *packetqueue_ptr(struct packetqueue_item *i);
214 /**
215  * @}
216  */
217 
218 
219 #endif /* PACKETQUEUE_H_ */
220 
221 /** @} */
222 /** @} */
Linked list manipulation routines.
void ** list_t
The linked list type.
Definition: list.h:133
Header file for the Rime buffer (packetbuf) management
struct packetqueue_item * packetqueue_first(struct packetqueue *q)
Access the first item on the packet buffer.
Definition: packetqueue.c:107
void * packetqueue_ptr(struct packetqueue_item *i)
Access the user-defined pointer in a packet queue item.
Definition: packetqueue.c:143
Representation of an item in a packet queue.
Definition: packetqueue.h:86
int packetqueue_len(struct packetqueue *q)
Get the length of the packet queue.
Definition: packetqueue.c:127
Header file for the Rime queue buffer management
struct queuebuf * packetqueue_queuebuf(struct packetqueue_item *i)
Access the queuebuf in a packet queue item.
Definition: packetqueue.c:133
int packetqueue_enqueue_packetbuf(struct packetqueue *q, clock_time_t lifetime, void *ptr)
Enqueue a packetbuf on a packet queue.
Definition: packetqueue.c:70
Header file for the callback timer
void packetqueue_init(struct packetqueue *q)
Initialize a packet queue.
Definition: packetqueue.c:50
Representation of a packet queue.
Definition: packetqueue.h:70
void packetqueue_dequeue(struct packetqueue *q)
Remove the first item on the packet buffer.
Definition: packetqueue.c:113
Memory block allocation routines.