Contiki 2.5
rucb.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, 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  * $Id: rucb.c,v 1.11 2009/11/08 19:40:18 adamdunkels Exp $
32  */
33 
34 /**
35  * \file
36  * Reliable unicast bulk transfer
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  */
40 
41 #include "net/rime/rucb.h"
42 #include "net/rime.h"
43 #include <string.h>
44 
45 #define MAX_TRANSMISSIONS 8
46 
47 #define DEBUG 0
48 #if DEBUG
49 #include <stdio.h>
50 #define PRINTF(...) printf(__VA_ARGS__)
51 #else
52 #define PRINTF(...)
53 #endif
54 
55 #include "sys/timetable.h"
56 /*---------------------------------------------------------------------------*/
57 static int
58 read_data(struct rucb_conn *c)
59 {
60  int len = 0;
62  if(c->u->read_chunk) {
63  len = c->u->read_chunk(c, c->chunk * RUCB_DATASIZE,
64  packetbuf_dataptr(), RUCB_DATASIZE);
65  }
67  return len;
68 }
69 /*---------------------------------------------------------------------------*/
70 static void
71 acked(struct runicast_conn *ruc, const rimeaddr_t *to, uint8_t retransmissions)
72 {
73  struct rucb_conn *c = (struct rucb_conn *)ruc;
74  PRINTF("%d.%d: rucb acked\n",
76  c->chunk++;
77  if(read_data(c) > 0) {
78  runicast_send(&c->c, &c->receiver, MAX_TRANSMISSIONS);
79  /* {
80  extern struct timetable cc2420_timetable;
81  timetable_print(&cc2420_timetable);
82  }*/
83  }
84 }
85 /*---------------------------------------------------------------------------*/
86 static void
87 timedout(struct runicast_conn *ruc, const rimeaddr_t *to, uint8_t retransmissions)
88 {
89  struct rucb_conn *c = (struct rucb_conn *)ruc;
90  PRINTF("%d.%d: rucb timedout\n",
92  if(c->u->timedout) {
93  c->u->timedout(c);
94  }
95 }
96 /*---------------------------------------------------------------------------*/
97 static void
98 recv(struct runicast_conn *ruc, const rimeaddr_t *from, uint8_t seqno)
99 {
100  struct rucb_conn *c = (struct rucb_conn *)ruc;
101 
102  PRINTF("%d.%d: rucb: recv from %d.%d len %d\n",
104  from->u8[0], from->u8[1], packetbuf_totlen());
105 
106  if(seqno == c->last_seqno) {
107  return;
108  }
109  c->last_seqno = seqno;
110 
111  if(rimeaddr_cmp(&c->sender, &rimeaddr_null)) {
112  rimeaddr_copy(&c->sender, from);
113  c->u->write_chunk(c, 0, RUCB_FLAG_NEWFILE, packetbuf_dataptr(), 0);
114  c->chunk = 0;
115  }
116 
117 
118  if(rimeaddr_cmp(&c->sender, from)) {
119  int datalen = packetbuf_datalen();
120 
121  if(datalen < RUCB_DATASIZE) {
122  PRINTF("%d.%d: get %d bytes, file complete\n",
124  datalen);
125  c->u->write_chunk(c, c->chunk * RUCB_DATASIZE,
126  RUCB_FLAG_LASTCHUNK, packetbuf_dataptr(), datalen);
127  } else {
128  c->u->write_chunk(c, c->chunk * RUCB_DATASIZE,
129  RUCB_FLAG_NONE, packetbuf_dataptr(), datalen);
130  }
131  c->chunk++;
132  }
133 
134  if(packetbuf_datalen() < RUCB_DATASIZE) {
135  rimeaddr_copy(&c->sender, &rimeaddr_null);
136  }
137 }
138 /*---------------------------------------------------------------------------*/
139 static const struct runicast_callbacks ruc = {recv, acked, timedout};
140 /*---------------------------------------------------------------------------*/
141 void
142 rucb_open(struct rucb_conn *c, uint16_t channel,
143  const struct rucb_callbacks *u)
144 {
145  rimeaddr_copy(&c->sender, &rimeaddr_null);
146  runicast_open(&c->c, channel, &ruc);
147  c->u = u;
148  c->last_seqno = -1;
149 }
150 /*---------------------------------------------------------------------------*/
151 void
152 rucb_close(struct rucb_conn *c)
153 {
154  runicast_close(&c->c);
155 }
156 /*---------------------------------------------------------------------------*/
157 int
158 rucb_send(struct rucb_conn *c, const rimeaddr_t *receiver)
159 {
160  c->chunk = 0;
161  read_data(c);
162  rimeaddr_copy(&c->receiver, receiver);
163  rimeaddr_copy(&c->sender, &rimeaddr_node_addr);
164  runicast_send(&c->c, receiver, MAX_TRANSMISSIONS);
165  return 0;
166 }
167 /*---------------------------------------------------------------------------*/