Contiki 3.x
packetqueue.c
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  * Packet queue management
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup packetqueue
42  * @{
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 /** @} */
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
Header file for the packetqueue module
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
struct packetqueue_item * packetqueue_first(struct packetqueue *q)
Access the first item on the packet buffer.
Definition: packetqueue.c:107
#define NULL
The null pointer.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
void * packetqueue_ptr(struct packetqueue_item *i)
Access the user-defined pointer in a packet queue item.
Definition: packetqueue.c:143
int list_length(list_t list)
Get the length of a list.
Definition: list.c:275
Representation of an item in a packet queue.
Definition: packetqueue.h:86
void list_init(list_t list)
Initialize a list.
Definition: list.c:66
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
int packetqueue_len(struct packetqueue *q)
Get the length of the packet queue.
Definition: packetqueue.c:127
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:143
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
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:142