Contiki-Inga 3.x
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  */
36 
37 /**
38  * \file
39  * Packet queue management
40  * \author
41  * Adam Dunkels <adam@sics.se>
42  */
43 
44 #include "sys/ctimer.h"
45 #include "net/packetqueue.h"
46 
47 /*---------------------------------------------------------------------------*/
48 void
50 {
51  list_init(*q->list);
52  memb_init(q->memb);
53 }
54 /*---------------------------------------------------------------------------*/
55 static void
56 remove_queued_packet(void *item)
57 {
58  struct packetqueue_item *i = item;
59  struct packetqueue *q = i->queue;
60 
61  list_remove(*q->list, i);
62  queuebuf_free(i->buf);
63  ctimer_stop(&i->lifetimer);
64  memb_free(q->memb, i);
65  /* printf("removing queued packet due to timeout\n");*/
66 }
67 /*---------------------------------------------------------------------------*/
68 int
69 packetqueue_enqueue_packetbuf(struct packetqueue *q, clock_time_t lifetime,
70  void *ptr)
71 {
72  struct packetqueue_item *i;
73 
74  /* Allocate a memory block to hold the packet queue item. */
75  i = memb_alloc(q->memb);
76 
77  if(i == NULL) {
78  return 0;
79  }
80 
81  /* Allocate a queuebuf and copy the contents of the packetbuf into it. */
82  i->buf = queuebuf_new_from_packetbuf();
83 
84  if(i->buf == NULL) {
85  memb_free(q->memb, i);
86  return 0;
87  }
88 
89  i->queue = q;
90  i->ptr = ptr;
91 
92  /* Setup a ctimer that removes the packet from the queue when its
93  lifetime expires. If the lifetime is zero, we do not set a
94  lifetimer. */
95  if(lifetime > 0) {
96  ctimer_set(&i->lifetimer, lifetime, remove_queued_packet, i);
97  }
98 
99  /* Add the item to the queue. */
100  list_add(*q->list, i);
101 
102  return 1;
103 }
104 /*---------------------------------------------------------------------------*/
105 struct packetqueue_item *
107 {
108  return list_head(*q->list);
109 }
110 /*---------------------------------------------------------------------------*/
111 void
113 {
114  struct packetqueue_item *i;
115 
116  i = list_head(*q->list);
117  if(i != NULL) {
118  list_remove(*q->list, i);
119  queuebuf_free(i->buf);
120  ctimer_stop(&i->lifetimer);
121  memb_free(q->memb, i);
122  }
123 }
124 /*---------------------------------------------------------------------------*/
125 int
127 {
128  return list_length(*q->list);
129 }
130 /*---------------------------------------------------------------------------*/
131 struct queuebuf *
133 {
134  if(i != NULL) {
135  return i->buf;
136  } else {
137  return NULL;
138  }
139 }
140 /*---------------------------------------------------------------------------*/
141 void *
143 {
144  if(i != NULL) {
145  return i->ptr;
146  } else {
147  return NULL;
148  }
149 }
150 /*---------------------------------------------------------------------------*/
151 /** @} */