51 #ifdef RUNICAST_CONF_REXMIT_TIME
52 #define REXMIT_TIME RUNICAST_CONF_REXMIT_TIME
54 #define REXMIT_TIME CLOCK_SECOND
57 static const struct packetbuf_attrlist attributes[] =
66 #define PRINTF(...) printf(__VA_ARGS__)
73 sent_by_stunicast(
struct stunicast_conn *stunicast,
int status,
int num_tx)
75 struct runicast_conn *c = (
struct runicast_conn *)stunicast;
77 PRINTF(
"runicast: sent_by_stunicast c->rxmit %d num_tx %d\n",
81 if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) == PACKETBUF_ATTR_PACKET_TYPE_DATA) {
86 RIMESTATS_ADD(rexmit);
87 PRINTF(
"%d.%d: runicast: sent_by_stunicast packet %u (%u) resent %u\n",
89 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID),
92 if(c->rxmit >= c->max_rxmit) {
93 RIMESTATS_ADD(timedout);
95 stunicast_cancel(&c->c);
97 c->u->timedout(c, stunicast_receiver(&c->c), c->rxmit);
100 PRINTF(
"%d.%d: runicast: packet %d timed out\n",
103 c->sndnxt = (c->sndnxt + 1) % (1 << RUNICAST_PACKET_ID_BITS);
114 recv_from_stunicast(
struct stunicast_conn *stunicast,
const linkaddr_t *from)
116 struct runicast_conn *c = (
struct runicast_conn *)stunicast;
119 PRINTF(
"%d.%d: runicast: recv_from_stunicast from %d.%d type %d seqno %d\n",
121 from->u8[0], from->u8[1],
122 packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE),
123 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID));
125 if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
126 PACKETBUF_ATTR_PACKET_TYPE_ACK) {
127 PRINTF(
"%d.%d: runicast: got ACK from %d.%d, seqno %d (%d)\n",
129 from->u8[0], from->u8[1],
130 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID),
132 if(packetbuf_attr(PACKETBUF_ATTR_PACKET_ID) == c->sndnxt) {
133 RIMESTATS_ADD(ackrx);
134 PRINTF(
"%d.%d: runicast: ACKed %d\n",
136 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID));
137 c->sndnxt = (c->sndnxt + 1) % (1 << RUNICAST_PACKET_ID_BITS);
139 stunicast_cancel(&c->c);
140 if(c->u->sent !=
NULL) {
141 c->u->sent(c, stunicast_receiver(&c->c), c->rxmit);
144 PRINTF(
"%d.%d: runicast: received bad ACK %d for %d\n",
146 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID),
148 RIMESTATS_ADD(badackrx);
150 }
else if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
151 PACKETBUF_ATTR_PACKET_TYPE_DATA) {
153 uint16_t packet_seqno;
156 RIMESTATS_ADD(reliablerx);
158 PRINTF(
"%d.%d: runicast: got packet %d\n",
160 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID));
162 packet_seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
166 q = queuebuf_new_from_packetbuf();
168 PRINTF(
"%d.%d: runicast: Sending ACK to %d.%d for %d\n",
170 from->u8[0], from->u8[1],
177 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE, PACKETBUF_ATTR_PACKET_TYPE_ACK);
178 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, packet_seqno);
179 stunicast_send(&c->c, from);
180 RIMESTATS_ADD(acktx);
182 queuebuf_to_packetbuf(q);
185 PRINTF(
"%d.%d: runicast: could not send ACK to %d.%d for %d: no queued buffers\n",
187 from->u8[0], from->u8[1],
190 if(c->u->recv !=
NULL) {
191 c->u->recv(c, from, packet_seqno);
196 static const struct stunicast_callbacks runicast = {recv_from_stunicast,
200 runicast_open(
struct runicast_conn *c, uint16_t channel,
201 const struct runicast_callbacks *u)
203 stunicast_open(&c->c, channel, &runicast);
204 channel_set_attributes(channel, attributes);
212 runicast_close(
struct runicast_conn *c)
214 stunicast_close(&c->c);
218 runicast_is_transmitting(
struct runicast_conn *c)
224 runicast_send(
struct runicast_conn *c,
const linkaddr_t *receiver,
225 uint8_t max_retransmissions)
228 if(runicast_is_transmitting(c)) {
229 PRINTF(
"%d.%d: runicast: already transmitting\n",
233 packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1);
234 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE, PACKETBUF_ATTR_PACKET_TYPE_DATA);
235 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, c->sndnxt);
236 packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS, 3);
237 c->max_rxmit = max_retransmissions;
240 RIMESTATS_ADD(reliabletx);
241 PRINTF(
"%d.%d: runicast: sending packet %d\n",
244 ret = stunicast_send_stubborn(&c->c, receiver, REXMIT_TIME);