Contiki-Inga 3.x
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  */
37 
38 /**
39  * \file
40  * Ipolite Anonymous best effort local area BroadCast (ipolite)
41  * \author
42  * Adam Dunkels <adam@sics.se>
43  */
44 
45 #include "net/rime/rime.h"
46 #include "net/rime/ipolite.h"
47 #include "lib/random.h"
48 
49 #include <string.h>
50 
51 #ifndef MAX
52 #define MAX(a, b) ((a) > (b)? (a) : (b))
53 #endif /* MAX */
54 
55 #ifndef MIN
56 #define MIN(a, b) ((a) < (b)? (a) : (b))
57 #endif /* MIN */
58 
59 #define DEBUG 0
60 #if DEBUG
61 #include <stdio.h>
62 #define PRINTF(...) printf(__VA_ARGS__)
63 #else
64 #define PRINTF(...)
65 #endif
66 
67 /*---------------------------------------------------------------------------*/
68 static void
69 recv(struct broadcast_conn *broadcast, const linkaddr_t *from)
70 {
71  struct ipolite_conn *c = (struct ipolite_conn *)broadcast;
72  if(c->q != NULL &&
73  packetbuf_datalen() == queuebuf_datalen(c->q) &&
74  memcmp(packetbuf_dataptr(), queuebuf_dataptr(c->q),
75  MIN(c->hdrsize, packetbuf_datalen())) == 0) {
76  /* We received a copy of our own packet, so we increase the
77  duplicate counter. If it reaches its maximum, do not send out
78  our packet. */
79  c->dups++;
80  if(c->dups == c->maxdups) {
81  queuebuf_free(c->q);
82  c->q = NULL;
83  ctimer_stop(&c->t);
84  if(c->cb->dropped) {
85  c->cb->dropped(c);
86  }
87  }
88  }
89  if(c->cb->recv) {
90  c->cb->recv(c, from);
91  }
92 }
93 /*---------------------------------------------------------------------------*/
94 static void
95 sent(struct broadcast_conn *bc, int status, int num_tx)
96 {
97 
98 }
99 /*---------------------------------------------------------------------------*/
100 static void
101 send(void *ptr)
102 {
103  struct ipolite_conn *c = ptr;
104 
105  PRINTF("%d.%d: ipolite: send queuebuf %p\n",
107  c->q);
108 
109  if(c->q != NULL) {
110  queuebuf_to_packetbuf(c->q);
111  queuebuf_free(c->q);
112  c->q = NULL;
113  broadcast_send(&c->c);
114  if(c->cb->sent) {
115  c->cb->sent(c);
116  }
117  }
118 }
119 /*---------------------------------------------------------------------------*/
120 static const struct broadcast_callbacks broadcast = { recv, sent };
121 /*---------------------------------------------------------------------------*/
122 void
123 ipolite_open(struct ipolite_conn *c, uint16_t channel, uint8_t dups,
124  const struct ipolite_callbacks *cb)
125 {
126  broadcast_open(&c->c, channel, &broadcast);
127  c->cb = cb;
128  c->maxdups = dups;
129  PRINTF("ipolite open channel %d\n", channel);
130 }
131 /*---------------------------------------------------------------------------*/
132 void
134 {
135  broadcast_close(&c->c);
136  ctimer_stop(&c->t);
137  if(c->q != NULL) {
138  queuebuf_free(c->q);
139  c->q = NULL;
140  }
141 }
142 /*---------------------------------------------------------------------------*/
143 int
144 ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
145 {
146  if(c->q != NULL) {
147  /* If we are already about to send a packet, we cancel the old one. */
148  PRINTF("%d.%d: ipolite_send: cancel old send\n",
150  queuebuf_free(c->q);
151  }
152  c->dups = 0;
153  c->hdrsize = hdrsize;
154  if(interval == 0) {
155  PRINTF("%d.%d: ipolite_send: interval 0\n",
157  if(broadcast_send(&c->c)) {
158  if(c->cb->sent) {
159  c->cb->sent(c);
160  }
161  return 1;
162  }
163 
164  } else {
165  c->q = queuebuf_new_from_packetbuf();
166  if(c->q != NULL) {
167  ctimer_set(&c->t,
168  interval / 2 + (random_rand() % (interval / 2)),
169  send, c);
170  return 1;
171  }
172  PRINTF("%d.%d: ipolite_send: could not allocate queue buffer\n",
174  }
175  return 0;
176 }
177 /*---------------------------------------------------------------------------*/
178 void
180 {
181  ctimer_stop(&c->t);
182  if(c->q != NULL) {
183  queuebuf_free(c->q);
184  c->q = NULL;
185  }
186 }
187 /*---------------------------------------------------------------------------*/
188 /** @} */