00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00081
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