Contiki 2.5
ipolite.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rimeipolite
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2007, Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the Institute nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * This file is part of the Contiki operating system.
35  *
36  * $Id: ipolite.c,v 1.17 2010/02/23 18:38:05 adamdunkels Exp $
37  */
38 
39 /**
40  * \file
41  * Ipolite Anonymous best effort local area BroadCast (ipolite)
42  * \author
43  * Adam Dunkels <adam@sics.se>
44  */
45 
46 #include "net/rime.h"
47 #include "net/rime/ipolite.h"
48 #include "lib/random.h"
49 
50 #include <string.h>
51 
52 #ifndef MAX
53 #define MAX(a, b) ((a) > (b)? (a) : (b))
54 #endif /* MAX */
55 
56 #ifndef MIN
57 #define MIN(a, b) ((a) < (b)? (a) : (b))
58 #endif /* MIN */
59 
60 #define DEBUG 0
61 #if DEBUG
62 #include <stdio.h>
63 #define PRINTF(...) printf(__VA_ARGS__)
64 #else
65 #define PRINTF(...)
66 #endif
67 
68 /*---------------------------------------------------------------------------*/
69 static void
70 recv(struct broadcast_conn *broadcast, const rimeaddr_t *from)
71 {
72  struct ipolite_conn *c = (struct ipolite_conn *)broadcast;
73  if(c->q != NULL &&
74  packetbuf_datalen() == queuebuf_datalen(c->q) &&
75  memcmp(packetbuf_dataptr(), queuebuf_dataptr(c->q),
76  MIN(c->hdrsize, packetbuf_datalen())) == 0) {
77  /* We received a copy of our own packet, so we increase the
78  duplicate counter. If it reaches its maximum, do not send out
79  our packet. */
80  c->dups++;
81  if(c->dups == c->maxdups) {
82  queuebuf_free(c->q);
83  c->q = NULL;
84  ctimer_stop(&c->t);
85  if(c->cb->dropped) {
86  c->cb->dropped(c);
87  }
88  }
89  }
90  if(c->cb->recv) {
91  c->cb->recv(c, from);
92  }
93 }
94 /*---------------------------------------------------------------------------*/
95 static void
96 sent(struct broadcast_conn *bc, int status, int num_tx)
97 {
98 
99 }
100 /*---------------------------------------------------------------------------*/
101 static void
102 send(void *ptr)
103 {
104  struct ipolite_conn *c = ptr;
105 
106  PRINTF("%d.%d: ipolite: send queuebuf %p\n",
108  c->q);
109 
110  if(c->q != NULL) {
111  queuebuf_to_packetbuf(c->q);
112  queuebuf_free(c->q);
113  c->q = NULL;
114  broadcast_send(&c->c);
115  if(c->cb->sent) {
116  c->cb->sent(c);
117  }
118  }
119 }
120 /*---------------------------------------------------------------------------*/
121 static const struct broadcast_callbacks broadcast = { recv, sent };
122 /*---------------------------------------------------------------------------*/
123 void
124 ipolite_open(struct ipolite_conn *c, uint16_t channel, uint8_t dups,
125  const struct ipolite_callbacks *cb)
126 {
127  broadcast_open(&c->c, channel, &broadcast);
128  c->cb = cb;
129  c->maxdups = dups;
130  PRINTF("ipolite open channel %d\n", channel);
131 }
132 /*---------------------------------------------------------------------------*/
133 void
135 {
136  broadcast_close(&c->c);
137  ctimer_stop(&c->t);
138  if(c->q != NULL) {
139  queuebuf_free(c->q);
140  c->q = NULL;
141  }
142 }
143 /*---------------------------------------------------------------------------*/
144 int
145 ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
146 {
147  if(c->q != NULL) {
148  /* If we are already about to send a packet, we cancel the old one. */
149  PRINTF("%d.%d: ipolite_send: cancel old send\n",
151  queuebuf_free(c->q);
152  }
153  c->dups = 0;
154  c->hdrsize = hdrsize;
155  if(interval == 0) {
156  PRINTF("%d.%d: ipolite_send: interval 0\n",
158  if(broadcast_send(&c->c)) {
159  if(c->cb->sent) {
160  c->cb->sent(c);
161  }
162  return 1;
163  }
164 
165  } else {
166  c->q = queuebuf_new_from_packetbuf();
167  if(c->q != NULL) {
168  ctimer_set(&c->t,
169  interval / 2 + (random_rand() % (interval / 2)),
170  send, c);
171  return 1;
172  }
173  PRINTF("%d.%d: ipolite_send: could not allocate queue buffer\n",
175  }
176  return 0;
177 }
178 /*---------------------------------------------------------------------------*/
179 void
181 {
182  ctimer_stop(&c->t);
183  if(c->q != NULL) {
184  queuebuf_free(c->q);
185  c->q = NULL;
186  }
187 }
188 /*---------------------------------------------------------------------------*/
189 /** @} */