Contiki 2.5
packetqueue.c
Go to the documentation of this file.
1 /**
2  * \addtogroup packetqueue
3  * @{
4  */
5 /*
6  * Copyright (c) 2009, Swedish Institute of Computer Science.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the Institute nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * This file is part of the Contiki operating system.
34  *
35  * $Id: packetqueue.c,v 1.1 2010/06/14 19:19:16 adamdunkels Exp $
36  */
37 
38 /**
39  * \file
40  * Packet queue management
41  * \author
42  * Adam Dunkels <adam@sics.se>
43  */
44 
45 #include "sys/ctimer.h"
46 #include "net/packetqueue.h"
47 
48 /*---------------------------------------------------------------------------*/
49 void
51 {
52  list_init(*q->list);
53  memb_init(q->memb);
54 }
55 /*---------------------------------------------------------------------------*/
56 static void
57 remove_queued_packet(void *item)
58 {
59  struct packetqueue_item *i = item;
60  struct packetqueue *q = i->queue;
61 
62  list_remove(*q->list, i);
63  queuebuf_free(i->buf);
64  ctimer_stop(&i->lifetimer);
65  memb_free(q->memb, i);
66  /* printf("removing queued packet due to timeout\n");*/
67 }
68 /*---------------------------------------------------------------------------*/
69 int
70 packetqueue_enqueue_packetbuf(struct packetqueue *q, clock_time_t lifetime,
71  void *ptr)
72 {
73  struct packetqueue_item *i;
74 
75  /* Allocate a memory block to hold the packet queue item. */
76  i = memb_alloc(q->memb);
77 
78  if(i == NULL) {
79  return 0;
80  }
81 
82  /* Allocate a queuebuf and copy the contents of the packetbuf into it. */
83  i->buf = queuebuf_new_from_packetbuf();
84 
85  if(i->buf == NULL) {
86  memb_free(q->memb, i);
87  return 0;
88  }
89 
90  i->queue = q;
91  i->ptr = ptr;
92 
93  /* Setup a ctimer that removes the packet from the queue when its
94  lifetime expires. If the lifetime is zero, we do not set a
95  lifetimer. */
96  if(lifetime > 0) {
97  ctimer_set(&i->lifetimer, lifetime, remove_queued_packet, i);
98  }
99 
100  /* Add the item to the queue. */
101  list_add(*q->list, i);
102 
103  return 1;
104 }
105 /*---------------------------------------------------------------------------*/
106 struct packetqueue_item *
108 {
109  return list_head(*q->list);
110 }
111 /*---------------------------------------------------------------------------*/
112 void
114 {
115  struct packetqueue_item *i;
116 
117  i = list_head(*q->list);
118  if(i != NULL) {
119  list_remove(*q->list, i);
120  queuebuf_free(i->buf);
121  ctimer_stop(&i->lifetimer);
122  memb_free(q->memb, i);
123  }
124 }
125 /*---------------------------------------------------------------------------*/
126 int
128 {
129  return list_length(*q->list);
130 }
131 /*---------------------------------------------------------------------------*/
132 struct queuebuf *
134 {
135  if(i != NULL) {
136  return i->buf;
137  } else {
138  return NULL;
139  }
140 }
141 /*---------------------------------------------------------------------------*/
142 void *
144 {
145  if(i != NULL) {
146  return i->ptr;
147  } else {
148  return NULL;
149  }
150 }
151 /*---------------------------------------------------------------------------*/
152 /** @} */