rucb.c

Go to the documentation of this file.
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   PRINTF("%d.%d: rucb acked\n",
00075          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]);
00076   c->chunk++;
00077   if(read_data(c) > 0) {
00078     runicast_send(&c->c, &c->receiver, MAX_TRANSMISSIONS);
00079     /*    {
00080       extern struct timetable cc2420_timetable;
00081       timetable_print(&cc2420_timetable);
00082       }*/
00083   }
00084 }
00085 /*---------------------------------------------------------------------------*/
00086 static void
00087 timedout(struct runicast_conn *ruc, const rimeaddr_t *to, uint8_t retransmissions)
00088 {
00089   struct rucb_conn *c = (struct rucb_conn *)ruc;
00090   PRINTF("%d.%d: rucb timedout\n",
00091          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]);
00092   if(c->u->timedout) {
00093     c->u->timedout(c);
00094   }
00095 }
00096 /*---------------------------------------------------------------------------*/
00097 static void
00098 recv(struct runicast_conn *ruc, const rimeaddr_t *from, uint8_t seqno)
00099 {
00100   struct rucb_conn *c = (struct rucb_conn *)ruc;
00101 
00102   PRINTF("%d.%d: rucb: recv from %d.%d len %d\n",
00103          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00104          from->u8[0], from->u8[1], packetbuf_totlen());
00105 
00106   if(seqno == c->last_seqno) {
00107     return;
00108   }
00109   c->last_seqno = seqno;
00110 
00111   if(rimeaddr_cmp(&c->sender, &rimeaddr_null)) {
00112     rimeaddr_copy(&c->sender, from);
00113     c->u->write_chunk(c, 0, RUCB_FLAG_NEWFILE, packetbuf_dataptr(), 0);
00114     c->chunk = 0;
00115   }
00116 
00117 
00118   if(rimeaddr_cmp(&c->sender, from)) {
00119     int datalen = packetbuf_datalen();
00120 
00121     if(datalen < RUCB_DATASIZE) {
00122       PRINTF("%d.%d: get %d bytes, file complete\n",
00123              rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00124              datalen);
00125       c->u->write_chunk(c, c->chunk * RUCB_DATASIZE,
00126                          RUCB_FLAG_LASTCHUNK, packetbuf_dataptr(), datalen);
00127     } else {
00128       c->u->write_chunk(c, c->chunk * RUCB_DATASIZE,
00129                         RUCB_FLAG_NONE, packetbuf_dataptr(), datalen);
00130     }
00131     c->chunk++;
00132   }
00133 
00134   if(packetbuf_datalen() < RUCB_DATASIZE) {
00135     rimeaddr_copy(&c->sender, &rimeaddr_null);
00136   }
00137 }
00138 /*---------------------------------------------------------------------------*/
00139 static const struct runicast_callbacks ruc = {recv, acked, timedout};
00140 /*---------------------------------------------------------------------------*/
00141 void
00142 rucb_open(struct rucb_conn *c, uint16_t channel,
00143           const struct rucb_callbacks *u)
00144 {
00145   rimeaddr_copy(&c->sender, &rimeaddr_null);
00146   runicast_open(&c->c, channel, &ruc);
00147   c->u = u;
00148   c->last_seqno = -1;
00149 }
00150 /*---------------------------------------------------------------------------*/
00151 void
00152 rucb_close(struct rucb_conn *c)
00153 {
00154   runicast_close(&c->c);
00155 }
00156 /*---------------------------------------------------------------------------*/
00157 int
00158 rucb_send(struct rucb_conn *c, const rimeaddr_t *receiver)
00159 {
00160   c->chunk = 0;
00161   read_data(c);
00162   rimeaddr_copy(&c->receiver, receiver);
00163   rimeaddr_copy(&c->sender, &rimeaddr_node_addr);
00164   runicast_send(&c->c, receiver, MAX_TRANSMISSIONS);
00165   return 0;
00166 }
00167 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:31 2011 for Contiki 2.5 by  doxygen 1.6.1