00001 /** 00002 * \addtogroup rime 00003 * @{ 00004 */ 00005 00006 /** 00007 * \defgroup packetqueue Packet queue 00008 * @{ 00009 * 00010 * The packetqueue module handles a list of queued packets. 00011 * 00012 */ 00013 00014 /* 00015 * Copyright (c) 2009, Swedish Institute of Computer Science. 00016 * All rights reserved. 00017 * 00018 * Redistribution and use in source and binary forms, with or without 00019 * modification, are permitted provided that the following conditions 00020 * are met: 00021 * 1. Redistributions of source code must retain the above copyright 00022 * notice, this list of conditions and the following disclaimer. 00023 * 2. Redistributions in binary form must reproduce the above copyright 00024 * notice, this list of conditions and the following disclaimer in the 00025 * documentation and/or other materials provided with the distribution. 00026 * 3. Neither the name of the Institute nor the names of its contributors 00027 * may be used to endorse or promote products derived from this software 00028 * without specific prior written permission. 00029 * 00030 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00031 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00032 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00033 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00034 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00035 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00036 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00037 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00038 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00039 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00040 * SUCH DAMAGE. 00041 * 00042 * This file is part of the Contiki operating system. 00043 * 00044 * $Id: packetqueue.h,v 1.1 2010/06/14 19:19:16 adamdunkels Exp $ 00045 */ 00046 00047 /** 00048 * \file 00049 * Header file for the packetqueue module 00050 * \author 00051 * Adam Dunkels <adam@sics.se> 00052 */ 00053 00054 #ifndef __PACKETQUEUE_H__ 00055 #define __PACKETQUEUE_H__ 00056 00057 #include "lib/list.h" 00058 #include "lib/memb.h" 00059 00060 #include "sys/ctimer.h" 00061 00062 #include "net/packetbuf.h" 00063 #include "net/queuebuf.h" 00064 00065 /** 00066 * \brief Representation of a packet queue. 00067 * 00068 * This structure holds the state of a packet queue. It is 00069 * an opaque structure with no user-visible elements. 00070 */ 00071 struct packetqueue { 00072 list_t *list; 00073 struct memb *memb; 00074 }; 00075 00076 /** 00077 * \brief Representation of an item in a packet queue. 00078 * 00079 * This structure holds the state of a packet queue. It is 00080 * an opaque structure with no user-visible elements. The 00081 * function packetqueue_queuebuf() is used to extract a 00082 * \ref queuebuf "queubuf" from the item. The function 00083 * packetqueue_ptr() is used to extract the opaque pointer 00084 * that was registered with the 00085 * packetqueue_enqueue_packetbuf() function. 00086 */ 00087 struct packetqueue_item { 00088 struct packetqueue_item *next; 00089 struct queuebuf *buf; 00090 struct packetqueue *queue; 00091 struct ctimer lifetimer; 00092 void *ptr; 00093 }; 00094 00095 00096 /** 00097 * \brief Define a packet queue. 00098 * \param name The variable name of the packet queue 00099 * \param size The maximum size of the packet queue 00100 * 00101 * This statement defines a packet queue. A packet queue 00102 * is defined on a per-module basis. 00103 * 00104 */ 00105 #define PACKETQUEUE(name, size) LIST(name##_list); \ 00106 MEMB(name##_memb, struct packetqueue_item, size); \ 00107 static struct packetqueue name = { &name##_list, \ 00108 &name##_memb } 00109 00110 /** 00111 * \name Packet queue functions. 00112 * @{ 00113 */ 00114 /** 00115 * \brief Initialize a packet queue. 00116 * \param q A pointer to a struct packetqueue that was defined with PACKETQUEUE(). 00117 * 00118 * This function initializes a packetqueue that has 00119 * previously been defined with PACKETQUEUE(). 00120 * 00121 */ 00122 void packetqueue_init(struct packetqueue *q); 00123 00124 00125 /** 00126 * \brief Enqueue a packetbuf on a packet queue. 00127 * \param q A pointer to a struct packetqueue. 00128 * \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. 00129 * \param ptr An opaque, user-defined pointer that can be used to identify the packet when it later is dequeued. 00130 * \retval Zero If memory could not be allocated for the packet. 00131 * \retval Non-zero If the packet was successfully enqueued. 00132 * 00133 * 00134 * This function enqueues the \ref packetbuf "packetbuf" 00135 * to the packet queue pointed to by the q parameter. The 00136 * packet queue must previously have been defined with 00137 * PACKETQUEUE() and initialized with packetqueue_init(). 00138 * 00139 * Each packet queue item has a maximum lifetime. When the 00140 * lifetime expires, the packet queue item is 00141 * automatically removed from the packet queue. If the 00142 * lifetime parameter is given as zero, the packet never 00143 * times out from the packet queue. 00144 * 00145 * Each packet queue item is tagged with a user-defined 00146 * pointer. This pointer can be used to identify packets 00147 * as they later are dequeued from the queue. This is 00148 * useful if two modules is using the same packet queue: 00149 * the modules can use the pointer to distinguish to which 00150 * module a dequeued packet belongs. 00151 * 00152 */ 00153 int packetqueue_enqueue_packetbuf(struct packetqueue *q, clock_time_t lifetime, 00154 void *ptr); 00155 00156 /** 00157 * \brief Access the first item on the packet buffer. 00158 * \param q A pointer to a struct packetqueue. 00159 * \return A pointer to the first item on the packet queue. 00160 * 00161 * This function returns the first item on the packet 00162 * queue. The packet queue is unchanged by this 00163 * function. To dequeue the first item on the list, use 00164 * the packetqueue_dequeue() function. 00165 * 00166 */ 00167 struct packetqueue_item *packetqueue_first(struct packetqueue *q); 00168 00169 /** 00170 * \brief Remove the first item on the packet buffer. 00171 * \param q A pointer to a struct packetqueue. 00172 * 00173 * This function removes the first item on the packet 00174 * queue. The function does not return the first item: to 00175 * access the first item, the packetqueue_first() function 00176 * must have been used prior to calling 00177 * packetqueue_dequeue(). 00178 * 00179 */ 00180 void packetqueue_dequeue(struct packetqueue *q); 00181 00182 /** 00183 * \brief Get the length of the packet queue 00184 * \param q A pointer to a struct packetqueue. 00185 * \return The number of packets queued on the packet queue 00186 * 00187 * This function returns the number of packets that are 00188 * queued on the packet queue. 00189 * 00190 */ 00191 int packetqueue_len(struct packetqueue *q); 00192 00193 /** 00194 * @} 00195 */ 00196 00197 /** 00198 * \name Packet queue item functions 00199 * @{ 00200 */ 00201 00202 /** 00203 * \brief Access the queuebuf in a packet queue item. 00204 * \param i A packet queue item, obtained with packetqueue_first(). 00205 * \return A pointer to the queuebuf in the packet queue item. 00206 */ 00207 struct queuebuf *packetqueue_queuebuf(struct packetqueue_item *i); 00208 /** 00209 * \brief Access the user-defined pointer in a packet queue item. 00210 * \param i A packet queue item, obtained with packetqueue_first(). 00211 * \return A pointer to the user-defined pointer in the packet queue item. 00212 */ 00213 00214 void *packetqueue_ptr(struct packetqueue_item *i); 00215 /** 00216 * @} 00217 */ 00218 00219 00220 #endif /* __PACKETQUEUE_H__ */ 00221 00222 /** @} */ 00223 /** @} */