Contiki 2.6
|
00001 /* 00002 * Copyright (c) 2006, Swedish Institute of Computer Science. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the Institute nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 * 00029 * This file is part of the Contiki operating system. 00030 * 00031 * $Id: rucb.c,v 1.11 2009/11/08 19:40:18 adamdunkels Exp $ 00032 */ 00033 00034 /** 00035 * \file 00036 * Reliable unicast bulk transfer 00037 * \author 00038 * Adam Dunkels <adam@sics.se> 00039 */ 00040 00041 #include "net/rime/rucb.h" 00042 #include "net/rime.h" 00043 #include <string.h> 00044 00045 #define MAX_TRANSMISSIONS 8 00046 00047 #define DEBUG 0 00048 #if DEBUG 00049 #include <stdio.h> 00050 #define PRINTF(...) printf(__VA_ARGS__) 00051 #else 00052 #define PRINTF(...) 00053 #endif 00054 00055 #include "sys/timetable.h" 00056 /*---------------------------------------------------------------------------*/ 00057 static int 00058 read_data(struct rucb_conn *c) 00059 { 00060 int len = 0; 00061 packetbuf_clear(); 00062 if(c->u->read_chunk) { 00063 len = c->u->read_chunk(c, c->chunk * RUCB_DATASIZE, 00064 packetbuf_dataptr(), RUCB_DATASIZE); 00065 } 00066 packetbuf_set_datalen(len); 00067 return len; 00068 } 00069 /*---------------------------------------------------------------------------*/ 00070 static void 00071 acked(struct runicast_conn *ruc, const rimeaddr_t *to, uint8_t retransmissions) 00072 { 00073 struct rucb_conn *c = (struct rucb_conn *)ruc; 00074 int len; 00075 PRINTF("%d.%d: rucb acked\n", 00076 rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]); 00077 c->chunk++; 00078 len = read_data(c); 00079 if(len == 0 && c->last_size == 0) { 00080 /* Nothing more to do */ 00081 return; 00082 } 00083 00084 if(len >= 0) { 00085 runicast_send(&c->c, &c->receiver, MAX_TRANSMISSIONS); 00086 c->last_size = len; 00087 00088 /* { 00089 extern struct timetable cc2420_timetable; 00090 timetable_print(&cc2420_timetable); 00091 }*/ 00092 } 00093 } 00094 /*---------------------------------------------------------------------------*/ 00095 static void 00096 timedout(struct runicast_conn *ruc, const rimeaddr_t *to, uint8_t retransmissions) 00097 { 00098 struct rucb_conn *c = (struct rucb_conn *)ruc; 00099 PRINTF("%d.%d: rucb timedout\n", 00100 rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]); 00101 if(c->u->timedout) { 00102 c->u->timedout(c); 00103 } 00104 } 00105 /*---------------------------------------------------------------------------*/ 00106 static void 00107 recv(struct runicast_conn *ruc, const rimeaddr_t *from, uint8_t seqno) 00108 { 00109 struct rucb_conn *c = (struct rucb_conn *)ruc; 00110 00111 PRINTF("%d.%d: rucb: recv from %d.%d len %d\n", 00112 rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1], 00113 from->u8[0], from->u8[1], packetbuf_totlen()); 00114 00115 if(seqno == c->last_seqno) { 00116 return; 00117 } 00118 c->last_seqno = seqno; 00119 00120 if(rimeaddr_cmp(&c->sender, &rimeaddr_null)) { 00121 rimeaddr_copy(&c->sender, from); 00122 c->u->write_chunk(c, 0, RUCB_FLAG_NEWFILE, packetbuf_dataptr(), 0); 00123 c->chunk = 0; 00124 } 00125 00126 00127 if(rimeaddr_cmp(&c->sender, from)) { 00128 int datalen = packetbuf_datalen(); 00129 00130 if(datalen < RUCB_DATASIZE) { 00131 PRINTF("%d.%d: get %d bytes, file complete\n", 00132 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], 00133 datalen); 00134 c->u->write_chunk(c, c->chunk * RUCB_DATASIZE, 00135 RUCB_FLAG_LASTCHUNK, packetbuf_dataptr(), datalen); 00136 } else { 00137 c->u->write_chunk(c, c->chunk * RUCB_DATASIZE, 00138 RUCB_FLAG_NONE, packetbuf_dataptr(), datalen); 00139 } 00140 c->chunk++; 00141 } 00142 00143 if(packetbuf_datalen() < RUCB_DATASIZE) { 00144 rimeaddr_copy(&c->sender, &rimeaddr_null); 00145 } 00146 } 00147 /*---------------------------------------------------------------------------*/ 00148 static const struct runicast_callbacks ruc = {recv, acked, timedout}; 00149 /*---------------------------------------------------------------------------*/ 00150 void 00151 rucb_open(struct rucb_conn *c, uint16_t channel, 00152 const struct rucb_callbacks *u) 00153 { 00154 rimeaddr_copy(&c->sender, &rimeaddr_null); 00155 runicast_open(&c->c, channel, &ruc); 00156 c->u = u; 00157 c->last_seqno = -1; 00158 c->last_size = -1; 00159 } 00160 /*---------------------------------------------------------------------------*/ 00161 void 00162 rucb_close(struct rucb_conn *c) 00163 { 00164 runicast_close(&c->c); 00165 } 00166 /*---------------------------------------------------------------------------*/ 00167 int 00168 rucb_send(struct rucb_conn *c, const rimeaddr_t *receiver) 00169 { 00170 c->chunk = 0; 00171 read_data(c); 00172 rimeaddr_copy(&c->receiver, receiver); 00173 rimeaddr_copy(&c->sender, &rimeaddr_node_addr); 00174 runicast_send(&c->c, receiver, MAX_TRANSMISSIONS); 00175 return 0; 00176 } 00177 /*---------------------------------------------------------------------------*/