Contiki 2.5
rudolph0.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rudolph0
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: rudolph0.c,v 1.11 2009/03/12 21:58:21 adamdunkels Exp $
37  */
38 
39 /**
40  * \file
41  * Rudolph0: a simple block data flooding protocol
42  * \author
43  * Adam Dunkels <adam@sics.se>
44  */
45 
46 #include <stddef.h> /* for offsetof */
47 
48 #include "net/rime.h"
49 #include "net/rime/rudolph0.h"
50 
51 #define STEADY_TIME CLOCK_SECOND * 2
52 
53 #define DEFAULT_SEND_INTERVAL CLOCK_SECOND / 2
54 enum {
55  TYPE_DATA,
56  TYPE_NACK,
57 };
58 
59 enum {
60  STATE_RECEIVER,
61  STATE_SENDER,
62 };
63 
64 #define VERSION_LT(a, b) ((signed char)((a) - (b)) < 0)
65 
66 #define DEBUG 0
67 #if DEBUG
68 #include <stdio.h>
69 #define PRINTF(...) printf(__VA_ARGS__)
70 #else
71 #define PRINTF(...)
72 #endif
73 
74 /*---------------------------------------------------------------------------*/
75 static void
76 read_new_datapacket(struct rudolph0_conn *c)
77 {
78  int len = 0;
79 
80  if(c->cb->read_chunk) {
81  len = c->cb->read_chunk(c, c->current.h.chunk * RUDOLPH0_DATASIZE,
82  c->current.data, RUDOLPH0_DATASIZE);
83  }
84  c->current.datalen = len;
85 
86  PRINTF("read_new_datapacket len %d\n", len);
87 }
88 /*---------------------------------------------------------------------------*/
89 static void
90 send_nack(struct rudolph0_conn *c)
91 {
92  struct rudolph0_hdr *hdr;
94  packetbuf_hdralloc(sizeof(struct rudolph0_hdr));
95  hdr = packetbuf_hdrptr();
96 
97  hdr->type = TYPE_NACK;
98  hdr->version = c->current.h.version;
99  hdr->chunk = c->current.h.chunk;
100 
101  PRINTF("Sending nack for %d:%d\n", hdr->version, hdr->chunk);
102  polite_send(&c->nackc, c->send_interval / 2, sizeof(struct rudolph0_hdr));
103 }
104 /*---------------------------------------------------------------------------*/
105 static void
106 sent(struct stbroadcast_conn *stbroadcast)
107 {
108  struct rudolph0_conn *c = (struct rudolph0_conn *)stbroadcast;
109 
110  if(c->current.datalen == RUDOLPH0_DATASIZE) {
111  c->current.h.chunk++;
112  PRINTF("Sending data chunk %d next time\n", c->current.h.chunk);
113  read_new_datapacket(c);
114  } else {
115  stbroadcast_set_timer(&c->c, STEADY_TIME);
116  PRINTF("Steady: Sending the same data chunk next time datalen %d, %d\n",
117  c->current.datalen, RUDOLPH0_DATASIZE);
118  }
119 }
120 /*---------------------------------------------------------------------------*/
121 static void
122 recv(struct stbroadcast_conn *stbroadcast)
123 {
124  struct rudolph0_conn *c = (struct rudolph0_conn *)stbroadcast;
125  struct rudolph0_datapacket *p = packetbuf_dataptr();
126 
127  if(p->h.type == TYPE_DATA) {
128  if(c->current.h.version != p->h.version) {
129  PRINTF("rudolph0 new version %d\n", p->h.version);
130  c->current.h.version = p->h.version;
131  c->current.h.chunk = 0;
132  c->cb->write_chunk(c, 0, RUDOLPH0_FLAG_NEWFILE, p->data, 0);
133  if(p->h.chunk != 0) {
134  send_nack(c);
135  } else {
136  c->cb->write_chunk(c, 0, RUDOLPH0_FLAG_NONE, p->data, p->datalen);
137  c->current.h.chunk++;
138  }
139  } else if(p->h.version == c->current.h.version) {
140  if(p->h.chunk == c->current.h.chunk) {
141  PRINTF("received chunk %d\n", p->h.chunk);
142  if(p->datalen < RUDOLPH0_DATASIZE) {
143  c->cb->write_chunk(c, c->current.h.chunk * RUDOLPH0_DATASIZE,
144  RUDOLPH0_FLAG_LASTCHUNK, p->data, p->datalen);
145  } else {
146  c->cb->write_chunk(c, c->current.h.chunk * RUDOLPH0_DATASIZE,
147  RUDOLPH0_FLAG_NONE, p->data, p->datalen);
148  }
149  c->current.h.chunk++;
150 
151  } else if(p->h.chunk > c->current.h.chunk) {
152  PRINTF("received chunk %d > %d, sending NACK\n", p->h.chunk, c->current.h.chunk);
153  send_nack(c);
154  }
155  } else { /* p->h.version < c->current.h.version */
156  /* Ignore packets with old version */
157  }
158  }
159 }
160 /*---------------------------------------------------------------------------*/
161 static void
162 recv_nack(struct polite_conn *polite)
163 {
164  struct rudolph0_conn *c = (struct rudolph0_conn *)
165  ((char *)polite - offsetof(struct rudolph0_conn,
166  nackc));
167  struct rudolph0_datapacket *p = packetbuf_dataptr();
168 
169  if(p->h.type == TYPE_NACK && c->state == STATE_SENDER) {
170  if(p->h.version == c->current.h.version) {
171  PRINTF("Reseting chunk to %d\n", p->h.chunk);
172  c->current.h.chunk = p->h.chunk;
173  } else {
174  PRINTF("Wrong version, reseting chunk to 0\n");
175  c->current.h.chunk = 0;
176  }
177  read_new_datapacket(c);
178  stbroadcast_set_timer(&c->c, c->send_interval);
179  }
180 }
181 /*---------------------------------------------------------------------------*/
182 static const struct polite_callbacks polite = { recv_nack, 0, 0 };
183 static const struct stbroadcast_callbacks stbroadcast = { recv, sent };
184 /*---------------------------------------------------------------------------*/
185 void
186 rudolph0_open(struct rudolph0_conn *c, uint16_t channel,
187  const struct rudolph0_callbacks *cb)
188 {
189  stbroadcast_open(&c->c, channel, &stbroadcast);
190  polite_open(&c->nackc, channel + 1, &polite);
191  c->cb = cb;
192  c->current.h.version = 0;
193  c->state = STATE_RECEIVER;
194  c->send_interval = DEFAULT_SEND_INTERVAL;
195 }
196 /*---------------------------------------------------------------------------*/
197 void
198 rudolph0_close(struct rudolph0_conn *c)
199 {
200  stbroadcast_close(&c->c);
201  polite_close(&c->nackc);
202 }
203 /*---------------------------------------------------------------------------*/
204 void
205 rudolph0_send(struct rudolph0_conn *c, clock_time_t send_interval)
206 {
207  c->state = STATE_SENDER;
208  c->current.h.version++;
209  c->current.h.version++;
210  c->current.h.chunk = 0;
211  c->current.h.type = TYPE_DATA;
212  read_new_datapacket(c);
213  packetbuf_reference(&c->current, sizeof(struct rudolph0_datapacket));
214  c->send_interval = send_interval;
215  stbroadcast_send_stubborn(&c->c, c->send_interval);
216 }
217 /*---------------------------------------------------------------------------*/
218 void
219 rudolph0_force_restart(struct rudolph0_conn *c)
220 {
221  c->current.h.chunk = 0;
222  send_nack(c);
223 }
224 /*---------------------------------------------------------------------------*/
225 void
226 rudolph0_stop(struct rudolph0_conn *c)
227 {
228  stbroadcast_cancel(&c->c);
229 }
230 /*---------------------------------------------------------------------------*/
231 int
232 rudolph0_version(struct rudolph0_conn *c)
233 {
234  return c->current.h.version;
235 }
236 /*---------------------------------------------------------------------------*/
237 void
238 rudolph0_set_version(struct rudolph0_conn *c, int version)
239 {
240  c->current.h.version = version;
241 }
242 /*---------------------------------------------------------------------------*/
243 /** @} */