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