52 #ifdef RUNICAST_CONF_REXMIT_TIME
53 #define REXMIT_TIME RUNICAST_CONF_REXMIT_TIME
55 #define REXMIT_TIME CLOCK_SECOND
58 static const struct packetbuf_attrlist attributes[] =
67 #define PRINTF(...) printf(__VA_ARGS__)
74 sent_by_stunicast(
struct stunicast_conn *stunicast,
int status,
int num_tx)
76 struct runicast_conn *c = (
struct runicast_conn *)stunicast;
78 PRINTF(
"runicast: sent_by_stunicast c->rxmit %d num_tx %d\n",
82 if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) == PACKETBUF_ATTR_PACKET_TYPE_DATA) {
87 RIMESTATS_ADD(rexmit);
88 PRINTF(
"%d.%d: runicast: sent_by_stunicast packet %u (%u) resent %u\n",
90 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID),
93 if(c->rxmit >= c->max_rxmit) {
94 RIMESTATS_ADD(timedout);
96 stunicast_cancel(&c->c);
98 c->u->timedout(c, stunicast_receiver(&c->c), c->rxmit);
101 PRINTF(
"%d.%d: runicast: packet %d timed out\n",
104 c->sndnxt = (c->sndnxt + 1) % (1 << RUNICAST_PACKET_ID_BITS);
115 recv_from_stunicast(
struct stunicast_conn *stunicast,
const rimeaddr_t *from)
117 struct runicast_conn *c = (
struct runicast_conn *)stunicast;
120 PRINTF(
"%d.%d: runicast: recv_from_stunicast from %d.%d type %d seqno %d\n",
122 from->u8[0], from->u8[1],
123 packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE),
124 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID));
126 if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
127 PACKETBUF_ATTR_PACKET_TYPE_ACK) {
128 PRINTF(
"%d.%d: runicast: got ACK from %d.%d, seqno %d (%d)\n",
130 from->u8[0], from->u8[1],
131 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID),
133 if(packetbuf_attr(PACKETBUF_ATTR_PACKET_ID) == c->sndnxt) {
134 RIMESTATS_ADD(ackrx);
135 PRINTF(
"%d.%d: runicast: ACKed %d\n",
137 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID));
138 c->sndnxt = (c->sndnxt + 1) % (1 << RUNICAST_PACKET_ID_BITS);
140 stunicast_cancel(&c->c);
141 if(c->u->sent !=
NULL) {
142 c->u->sent(c, stunicast_receiver(&c->c), c->rxmit);
145 PRINTF(
"%d.%d: runicast: received bad ACK %d for %d\n",
147 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID),
149 RIMESTATS_ADD(badackrx);
151 }
else if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
152 PACKETBUF_ATTR_PACKET_TYPE_DATA) {
154 uint16_t packet_seqno;
157 RIMESTATS_ADD(reliablerx);
159 PRINTF(
"%d.%d: runicast: got packet %d\n",
161 packetbuf_attr(PACKETBUF_ATTR_PACKET_ID));
163 packet_seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
167 q = queuebuf_new_from_packetbuf();
169 PRINTF(
"%d.%d: runicast: Sending ACK to %d.%d for %d\n",
171 from->u8[0], from->u8[1],
178 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE, PACKETBUF_ATTR_PACKET_TYPE_ACK);
179 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, packet_seqno);
180 stunicast_send(&c->c, from);
181 RIMESTATS_ADD(acktx);
183 queuebuf_to_packetbuf(q);
186 PRINTF(
"%d.%d: runicast: could not send ACK to %d.%d for %d: no queued buffers\n",
188 from->u8[0], from->u8[1],
191 if(c->u->recv !=
NULL) {
192 c->u->recv(c, from, packet_seqno);
197 static const struct stunicast_callbacks runicast = {recv_from_stunicast,
201 runicast_open(
struct runicast_conn *c, uint16_t channel,
202 const struct runicast_callbacks *u)
204 stunicast_open(&c->c, channel, &runicast);
205 channel_set_attributes(channel, attributes);
213 runicast_close(
struct runicast_conn *c)
215 stunicast_close(&c->c);
219 runicast_is_transmitting(
struct runicast_conn *c)
225 runicast_send(
struct runicast_conn *c,
const rimeaddr_t *receiver,
226 uint8_t max_retransmissions)
229 if(runicast_is_transmitting(c)) {
230 PRINTF(
"%d.%d: runicast: already transmitting\n",
234 packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1);
235 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE, PACKETBUF_ATTR_PACKET_TYPE_DATA);
236 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, c->sndnxt);
237 packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS, 3);
238 c->max_rxmit = max_retransmissions;
241 RIMESTATS_ADD(reliabletx);
242 PRINTF(
"%d.%d: runicast: sending packet %d\n",
245 ret = stunicast_send_stubborn(&c->c, receiver, REXMIT_TIME);