Contiki 2.5
rudolph2.c
Go to the documentation of this file.
1 /* XXX todo: add timeout so that hops_from_sink is reset to MAX
2  after a while. */
3 
4 /* XXX todo: use a ctimer to drive peridodic transmission: the current
5  way does not work if a queuebuf cannot be allocated. */
6 
7 /**
8  * \addtogroup rudolph2
9  * @{
10  */
11 
12 /*
13  * Copyright (c) 2007, Swedish Institute of Computer Science.
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the Institute nor the names of its contributors
25  * may be used to endorse or promote products derived from this software
26  * without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * This file is part of the Contiki operating system.
41  *
42  * $Id: rudolph2.c,v 1.7 2009/03/12 21:58:21 adamdunkels Exp $
43  */
44 
45 /**
46  * \file
47  * Rudolph2: a simple block data flooding protocol
48  * \author
49  * Adam Dunkels <adam@sics.se>
50  */
51 
52 #include <stdio.h>
53 #include <stddef.h> /* for offsetof */
54 
55 #include "net/rime.h"
56 #include "net/rime/polite.h"
57 #include "net/rime/rudolph2.h"
58 #include "cfs/cfs.h"
59 
60 #define SEND_INTERVAL CLOCK_SECOND / 2
61 #define STEADY_INTERVAL CLOCK_SECOND * 16
62 #define RESEND_INTERVAL SEND_INTERVAL * 4
63 #define NACK_TIMEOUT CLOCK_SECOND / 4
64 
65 struct rudolph2_hdr {
66  uint8_t type;
67  uint8_t hops_from_base;
68  uint16_t version;
69  uint16_t chunk;
70 };
71 
72 #define POLITE_HEADER 1
73 
74 #define HOPS_MAX 64
75 
76 enum {
77  TYPE_DATA,
78  TYPE_NACK,
79 };
80 
81 #define FLAG_LAST_SENT 0x01
82 #define FLAG_LAST_RECEIVED 0x02
83 #define FLAG_IS_STOPPED 0x04
84 
85 #define DEBUG 0
86 #if DEBUG
87 #include <stdio.h>
88 #define PRINTF(...) printf(__VA_ARGS__)
89 #else
90 #define PRINTF(...)
91 #endif
92 
93 #define LT(a, b) ((signed short)((a) - (b)) < 0)
94 
95 /*---------------------------------------------------------------------------*/
96 static int
97 read_data(struct rudolph2_conn *c, uint8_t *dataptr, int chunk)
98 {
99  int len = 0;
100 
101  if(c->cb->read_chunk) {
102  len = c->cb->read_chunk(c, chunk * RUDOLPH2_DATASIZE,
103  dataptr, RUDOLPH2_DATASIZE);
104  }
105  return len;
106 }
107 /*---------------------------------------------------------------------------*/
108 static int
109 format_data(struct rudolph2_conn *c, int chunk)
110 {
111  struct rudolph2_hdr *hdr;
112  int len;
113 
114  packetbuf_clear();
115  hdr = packetbuf_dataptr();
116  hdr->type = TYPE_DATA;
117  hdr->hops_from_base = c->hops_from_base;
118  hdr->version = c->version;
119  hdr->chunk = chunk;
120  len = read_data(c, (uint8_t *)hdr + sizeof(struct rudolph2_hdr), chunk);
121  packetbuf_set_datalen(sizeof(struct rudolph2_hdr) + len);
122 
123  return len;
124 }
125 /*---------------------------------------------------------------------------*/
126 static void
127 write_data(struct rudolph2_conn *c, int chunk, uint8_t *data, int datalen)
128 {
129  /* xxx Don't write any data if the application has been stopped. */
130  if(c->flags & FLAG_IS_STOPPED) {
131  return;
132  }
133 
134  if(chunk == 0) {
135  c->cb->write_chunk(c, 0, RUDOLPH2_FLAG_NEWFILE, data, 0);
136  }
137 
138  PRINTF("%d.%d: get %d bytes\n",
140  datalen);
141 
142 
143  if(datalen < RUDOLPH2_DATASIZE) {
144  PRINTF("%d.%d: get %d bytes, file complete\n",
146  datalen);
147  c->cb->write_chunk(c, chunk * RUDOLPH2_DATASIZE,
148  RUDOLPH2_FLAG_LASTCHUNK, data, datalen);
149  } else {
150  c->cb->write_chunk(c, chunk * RUDOLPH2_DATASIZE,
151  RUDOLPH2_FLAG_NONE, data, datalen);
152  }
153 }
154 /*---------------------------------------------------------------------------*/
155 static int
156 send_data(struct rudolph2_conn *c, clock_time_t interval)
157 {
158  int len;
159 
160  len = format_data(c, c->snd_nxt);
161  polite_send(&c->c, interval, POLITE_HEADER);
162  PRINTF("%d.%d: send_data chunk %d, rcv_nxt %d\n",
164  c->snd_nxt, c->rcv_nxt);
165 
166  return len;
167 }
168 /*---------------------------------------------------------------------------*/
169 static void
170 send_nack(struct rudolph2_conn *c)
171 {
172  struct rudolph2_hdr *hdr;
173  packetbuf_clear();
174  packetbuf_hdralloc(sizeof(struct rudolph2_hdr));
175  hdr = packetbuf_hdrptr();
176 
177  hdr->hops_from_base = c->hops_from_base;
178  hdr->type = TYPE_NACK;
179  hdr->version = c->version;
180  hdr->chunk = c->rcv_nxt;
181 
182  PRINTF("%d.%d: Sending nack for %d\n",
184  hdr->chunk);
185  polite_send(&c->c, NACK_TIMEOUT, POLITE_HEADER);
186 }
187 /*---------------------------------------------------------------------------*/
188 #if 0 /* Function below not currently used in the code */
189 static void
190 send_next(struct rudolph2_conn *c)
191 {
192  int len;
193  clock_time_t interval;
194 
195  if(c->flags & FLAG_LAST_SENT) {
196  interval = STEADY_INTERVAL;
197  } else {
198  interval = SEND_INTERVAL;
199  }
200 
201  len = send_data(c, interval);
202 
203  if(len < RUDOLPH2_DATASIZE) {
204  c->flags |= FLAG_LAST_SENT;
205  } else {
206  c->flags &= ~FLAG_LAST_SENT;
207  }
208 
209  if(c->nacks == 0 &&
210  len == RUDOLPH2_DATASIZE &&
211  c->snd_nxt + 1 < c->rcv_nxt) {
212  c->snd_nxt++;
213  }
214  c->nacks = 0;
215 }
216 #endif /* 0 */
217 /*---------------------------------------------------------------------------*/
218 static void
219 sent(struct polite_conn *polite)
220 {
221  /* struct rudolph2_conn *c = (struct rudolph2_conn *)polite;
222 
223  if((c->flags & FLAG_IS_STOPPED) == 0 &&
224  (c->flags & FLAG_LAST_RECEIVED)) {
225  if(c->snd_nxt < c->rcv_nxt) {
226  send_next(c);
227  } else {
228  send_data(c, STEADY_INTERVAL);
229  }
230  }*/
231 
232 }
233 /*---------------------------------------------------------------------------*/
234 static void
235 dropped(struct polite_conn *polite)
236 {
237  /* struct rudolph2_conn *c = (struct rudolph2_conn *)polite;
238  if((c->flags & FLAG_IS_STOPPED) == 0 &&
239  (c->flags & FLAG_LAST_RECEIVED)) {
240  if(c->snd_nxt + 1 < c->rcv_nxt) {
241  send_data(c, SEND_INTERVAL);
242  } else {
243  send_data(c, STEADY_INTERVAL);
244  }
245  }*/
246 }
247 /*---------------------------------------------------------------------------*/
248 static void
249 timed_send(void *ptr)
250 {
251  struct rudolph2_conn *c = (struct rudolph2_conn *)ptr;
252  clock_time_t interval;
253  int len;
254 
255  if((c->flags & FLAG_IS_STOPPED) == 0 &&
256  (c->flags & FLAG_LAST_RECEIVED)) {
257  /* if(c->snd_nxt + 1 < c->rcv_nxt) {
258  interval = SEND_INTERVAL;
259  } else {
260  interval = STEADY_INTERVAL;
261  }*/
262  /* send_data(c, interval);*/
263 
264  if(c->flags & FLAG_LAST_SENT) {
265  interval = STEADY_INTERVAL;
266  } else {
267  interval = SEND_INTERVAL;
268  }
269 
270 
271  len = send_data(c, interval);
272 
273  if(len < RUDOLPH2_DATASIZE) {
274  c->flags |= FLAG_LAST_SENT;
275  } else {
276  c->flags &= ~FLAG_LAST_SENT;
277  }
278 
279  if(c->nacks == 0 &&
280  len == RUDOLPH2_DATASIZE &&
281  c->snd_nxt + 1 < c->rcv_nxt) {
282  c->snd_nxt++;
283  }
284  c->nacks = 0;
285  ctimer_set(&c->t, interval, timed_send, c);
286  }
287 }
288 /*---------------------------------------------------------------------------*/
289 static void
290 recv(struct polite_conn *polite)
291 {
292  struct rudolph2_conn *c = (struct rudolph2_conn *)polite;
293  struct rudolph2_hdr *hdr = packetbuf_dataptr();
294 
295  /* Only accept NACKs from nodes that are farther away from the base
296  than us. */
297 
298  if(hdr->type == TYPE_NACK && hdr->hops_from_base > c->hops_from_base) {
299  c->nacks++;
300  PRINTF("%d.%d: Got NACK for %d:%d (%d:%d)\n",
302  hdr->version, hdr->chunk,
303  c->version, c->rcv_nxt);
304  if(hdr->version == c->version) {
305  if(hdr->chunk < c->rcv_nxt) {
306  c->snd_nxt = hdr->chunk;
307  send_data(c, SEND_INTERVAL);
308  }
309  } else if(LT(hdr->version, c->version)) {
310  c->snd_nxt = 0;
311  send_data(c, SEND_INTERVAL);
312  }
313  } else if(hdr->type == TYPE_DATA) {
314  if(hdr->hops_from_base < c->hops_from_base) {
315  /* Only accept data from nodes that are closer to the base than
316  us. */
317  c->hops_from_base = hdr->hops_from_base + 1;
318  if(LT(c->version, hdr->version)) {
319  PRINTF("%d.%d: rudolph2 new version %d, chunk %d\n",
321  hdr->version, hdr->chunk);
322  c->version = hdr->version;
323  c->snd_nxt = c->rcv_nxt = 0;
324  c->flags &= ~FLAG_LAST_RECEIVED;
325  c->flags &= ~FLAG_LAST_SENT;
326  if(hdr->chunk != 0) {
327  send_nack(c);
328  } else {
329  packetbuf_hdrreduce(sizeof(struct rudolph2_hdr));
330  write_data(c, 0, packetbuf_dataptr(), packetbuf_totlen());
331  }
332  } else if(hdr->version == c->version) {
333  PRINTF("%d.%d: got chunk %d snd_nxt %d rcv_nxt %d\n",
335  hdr->chunk, c->snd_nxt, c->rcv_nxt);
336 
337  if(hdr->chunk == c->rcv_nxt) {
338  int len;
339  packetbuf_hdrreduce(sizeof(struct rudolph2_hdr));
340  PRINTF("%d.%d: received chunk %d len %d\n",
342  hdr->chunk, packetbuf_totlen());
343  len = packetbuf_totlen();
344  write_data(c, hdr->chunk, packetbuf_dataptr(), packetbuf_totlen());
345  c->rcv_nxt++;
346  if(len < RUDOLPH2_DATASIZE) {
347  c->flags |= FLAG_LAST_RECEIVED;
348  send_data(c, RESEND_INTERVAL);
349  ctimer_set(&c->t, RESEND_INTERVAL, timed_send, c);
350  }
351  } else if(hdr->chunk > c->rcv_nxt) {
352  PRINTF("%d.%d: received chunk %d > %d, sending NACK\n",
354  hdr->chunk, c->rcv_nxt);
355  send_nack(c);
356  } else if(hdr->chunk < c->rcv_nxt) {
357  /* Ignore packets with a lower chunk number */
358  }
359  }
360  }
361  }
362 }
363 /*---------------------------------------------------------------------------*/
364 static const struct polite_callbacks polite = { recv, sent, dropped };
365 /*---------------------------------------------------------------------------*/
366 void
367 rudolph2_open(struct rudolph2_conn *c, uint16_t channel,
368  const struct rudolph2_callbacks *cb)
369 {
370  polite_open(&c->c, channel, &polite);
371  c->cb = cb;
372  c->version = 0;
373  c->hops_from_base = HOPS_MAX;
374 }
375 /*---------------------------------------------------------------------------*/
376 void
377 rudolph2_close(struct rudolph2_conn *c)
378 {
379  polite_close(&c->c);
380 }
381 /*---------------------------------------------------------------------------*/
382 void
383 rudolph2_send(struct rudolph2_conn *c, clock_time_t send_interval)
384 {
385  int len;
386 
387  c->hops_from_base = 0;
388  c->version++;
389  c->snd_nxt = 0;
390  len = RUDOLPH2_DATASIZE;
391  packetbuf_clear();
392  for(c->rcv_nxt = 0; len == RUDOLPH2_DATASIZE; c->rcv_nxt++) {
393  len = read_data(c, packetbuf_dataptr(), c->rcv_nxt);
394  }
395  c->flags = FLAG_LAST_RECEIVED;
396  /* printf("Highest chunk %d\n", c->rcv_nxt);*/
397  send_data(c, SEND_INTERVAL);
398  ctimer_set(&c->t, SEND_INTERVAL, timed_send, c);
399 }
400 /*---------------------------------------------------------------------------*/
401 void
402 rudolph2_stop(struct rudolph2_conn *c)
403 {
404  polite_cancel(&c->c);
405  c->flags |= FLAG_IS_STOPPED;
406 }
407 /*---------------------------------------------------------------------------*/
408 /** @} */