Contiki 3.x
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  */
32 
33 /**
34  * \file
35  * Reliable unicast bulk transfer
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "net/rime/rucb.h"
41 #include "net/rime/rime.h"
42 #include <string.h>
43 
44 #define MAX_TRANSMISSIONS 8
45 
46 #define DEBUG 0
47 #if DEBUG
48 #include <stdio.h>
49 #define PRINTF(...) printf(__VA_ARGS__)
50 #else
51 #define PRINTF(...)
52 #endif
53 /*---------------------------------------------------------------------------*/
54 static int
55 read_data(struct rucb_conn *c)
56 {
57  int len = 0;
59  if(c->u->read_chunk) {
60  len = c->u->read_chunk(c, c->chunk * RUCB_DATASIZE,
61  packetbuf_dataptr(), RUCB_DATASIZE);
62  }
64  return len;
65 }
66 /*---------------------------------------------------------------------------*/
67 static void
68 acked(struct runicast_conn *ruc, const linkaddr_t *to, uint8_t retransmissions)
69 {
70  struct rucb_conn *c = (struct rucb_conn *)ruc;
71  int len;
72  PRINTF("%d.%d: rucb acked\n",
74  c->chunk++;
75  len = read_data(c);
76  if(len == 0 && c->last_size == 0) {
77  /* Nothing more to do */
78  return;
79  }
80 
81  if(len >= 0) {
82  runicast_send(&c->c, &c->receiver, MAX_TRANSMISSIONS);
83  c->last_size = len;
84 
85  }
86 }
87 /*---------------------------------------------------------------------------*/
88 static void
89 timedout(struct runicast_conn *ruc, const linkaddr_t *to, uint8_t retransmissions)
90 {
91  struct rucb_conn *c = (struct rucb_conn *)ruc;
92  PRINTF("%d.%d: rucb timedout\n",
94  if(c->u->timedout) {
95  c->u->timedout(c);
96  }
97 }
98 /*---------------------------------------------------------------------------*/
99 static void
100 recv(struct runicast_conn *ruc, const linkaddr_t *from, uint8_t seqno)
101 {
102  struct rucb_conn *c = (struct rucb_conn *)ruc;
103 
104  PRINTF("%d.%d: rucb: recv from %d.%d len %d\n",
106  from->u8[0], from->u8[1], packetbuf_totlen());
107 
108  if(seqno == c->last_seqno) {
109  return;
110  }
111  c->last_seqno = seqno;
112 
113  if(linkaddr_cmp(&c->sender, &linkaddr_null)) {
114  linkaddr_copy(&c->sender, from);
115  c->u->write_chunk(c, 0, RUCB_FLAG_NEWFILE, packetbuf_dataptr(), 0);
116  c->chunk = 0;
117  }
118 
119 
120  if(linkaddr_cmp(&c->sender, from)) {
121  int datalen = packetbuf_datalen();
122 
123  if(datalen < RUCB_DATASIZE) {
124  PRINTF("%d.%d: get %d bytes, file complete\n",
126  datalen);
127  c->u->write_chunk(c, c->chunk * RUCB_DATASIZE,
128  RUCB_FLAG_LASTCHUNK, packetbuf_dataptr(), datalen);
129  } else {
130  c->u->write_chunk(c, c->chunk * RUCB_DATASIZE,
131  RUCB_FLAG_NONE, packetbuf_dataptr(), datalen);
132  }
133  c->chunk++;
134  }
135 
136  if(packetbuf_datalen() < RUCB_DATASIZE) {
137  linkaddr_copy(&c->sender, &linkaddr_null);
138  }
139 }
140 /*---------------------------------------------------------------------------*/
141 static const struct runicast_callbacks ruc = {recv, acked, timedout};
142 /*---------------------------------------------------------------------------*/
143 void
144 rucb_open(struct rucb_conn *c, uint16_t channel,
145  const struct rucb_callbacks *u)
146 {
147  linkaddr_copy(&c->sender, &linkaddr_null);
148  runicast_open(&c->c, channel, &ruc);
149  c->u = u;
150  c->last_seqno = -1;
151  c->last_size = -1;
152 }
153 /*---------------------------------------------------------------------------*/
154 void
155 rucb_close(struct rucb_conn *c)
156 {
157  runicast_close(&c->c);
158 }
159 /*---------------------------------------------------------------------------*/
160 int
161 rucb_send(struct rucb_conn *c, const linkaddr_t *receiver)
162 {
163  c->chunk = 0;
164  read_data(c);
165  linkaddr_copy(&c->receiver, receiver);
166  linkaddr_copy(&c->sender, &linkaddr_node_addr);
167  runicast_send(&c->c, receiver, MAX_TRANSMISSIONS);
168  return 0;
169 }
170 /*---------------------------------------------------------------------------*/
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
const linkaddr_t linkaddr_null
The null Rime address.
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:200
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:260
Header file for the Rime stack
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:77
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
Header file for the reliable unicast bulk transfer module