2020-05-19 18:56:17 +00:00
|
|
|
#include "ReliableRouter.h"
|
|
|
|
#include "MeshTypes.h"
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "mesh-pb-constants.h"
|
|
|
|
|
|
|
|
// ReliableRouter::ReliableRouter() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the message is want_ack, then add it to a list of packets to retransmit.
|
|
|
|
* If we run out of retransmissions, send a nak packet towards the original client to indicate failure.
|
|
|
|
*/
|
|
|
|
ErrorCode ReliableRouter::send(MeshPacket *p)
|
|
|
|
{
|
|
|
|
if (p->want_ack) {
|
2020-05-21 23:34:16 +00:00
|
|
|
// If someone asks for acks on broadcast, we need the hop limit to be at least one, so that first node that receives our
|
|
|
|
// message will rebroadcast
|
|
|
|
if (p->to == NODENUM_BROADCAST && p->hop_limit == 0)
|
|
|
|
p->hop_limit = 1;
|
|
|
|
|
2020-05-19 18:56:17 +00:00
|
|
|
auto copy = packetPool.allocCopy(*p);
|
|
|
|
startRetransmission(copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FloodingRouter::send(p);
|
|
|
|
}
|
|
|
|
|
2020-05-25 18:55:42 +00:00
|
|
|
bool ReliableRouter::shouldFilterReceived(const MeshPacket *p)
|
|
|
|
{
|
|
|
|
if (p->to == NODENUM_BROADCAST && p->from == getNodeNum()) {
|
2020-06-14 22:30:42 +00:00
|
|
|
printPacket("Rx someone rebroadcasting for us", p);
|
2020-05-25 18:55:42 +00:00
|
|
|
|
|
|
|
// We are seeing someone rebroadcast one of our broadcast attempts.
|
|
|
|
// If this is the first time we saw this, cancel any retransmissions we have queued up and generate an internal ack for
|
|
|
|
// the original sending process.
|
|
|
|
if (stopRetransmission(p->from, p->id)) {
|
|
|
|
DEBUG_MSG("Someone is retransmitting for us, generate implicit ack\n");
|
|
|
|
sendAckNak(true, p->from, p->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FloodingRouter::shouldFilterReceived(p);
|
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:17 +00:00
|
|
|
/**
|
|
|
|
* If we receive a want_ack packet (do not check for wasSeenRecently), send back an ack (this might generate multiple ack sends in
|
|
|
|
* case the our first ack gets lost)
|
|
|
|
*
|
|
|
|
* If we receive an ack packet (do check wasSeenRecently), clear out any retransmissions and
|
|
|
|
* forward the ack to the application layer.
|
|
|
|
*
|
|
|
|
* If we receive a nak packet (do check wasSeenRecently), clear out any retransmissions
|
|
|
|
* and forward the nak to the application layer.
|
|
|
|
*
|
|
|
|
* Otherwise, let superclass handle it.
|
|
|
|
*/
|
2020-05-23 16:24:22 +00:00
|
|
|
void ReliableRouter::sniffReceived(const MeshPacket *p)
|
2020-05-19 18:56:17 +00:00
|
|
|
{
|
2020-05-21 19:47:41 +00:00
|
|
|
NodeNum ourNode = getNodeNum();
|
|
|
|
|
2020-05-25 18:55:42 +00:00
|
|
|
if (p->to == ourNode) { // ignore ack/nak/want_ack packets that are not address to us (we only handle 0 hop reliability
|
|
|
|
// - not DSR routing)
|
2020-05-19 18:56:17 +00:00
|
|
|
if (p->want_ack) {
|
|
|
|
sendAckNak(true, p->from, p->id);
|
|
|
|
}
|
|
|
|
|
2020-05-23 16:24:22 +00:00
|
|
|
// If the payload is valid, look for ack/nak
|
2020-05-19 18:56:17 +00:00
|
|
|
|
2020-05-23 16:24:22 +00:00
|
|
|
PacketId ackId = p->decoded.which_ack == SubPacket_success_id_tag ? p->decoded.ack.success_id : 0;
|
|
|
|
PacketId nakId = p->decoded.which_ack == SubPacket_fail_id_tag ? p->decoded.ack.fail_id : 0;
|
2020-05-19 18:56:17 +00:00
|
|
|
|
2020-05-25 18:55:42 +00:00
|
|
|
// We intentionally don't check wasSeenRecently, because it is harmless to delete non existent retransmission records
|
|
|
|
if (ackId || nakId) {
|
2020-05-23 16:24:22 +00:00
|
|
|
if (ackId) {
|
|
|
|
DEBUG_MSG("Received a ack=%d, stopping retransmissions\n", ackId);
|
|
|
|
stopRetransmission(p->to, ackId);
|
|
|
|
} else {
|
|
|
|
DEBUG_MSG("Received a nak=%d, stopping retransmissions\n", nakId);
|
|
|
|
stopRetransmission(p->to, nakId);
|
2020-05-19 18:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle the packet as normal
|
2020-05-23 16:24:22 +00:00
|
|
|
FloodingRouter::sniffReceived(p);
|
2020-05-19 18:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an ack or a nak packet back towards whoever sent idFrom
|
|
|
|
*/
|
|
|
|
void ReliableRouter::sendAckNak(bool isAck, NodeNum to, PacketId idFrom)
|
|
|
|
{
|
|
|
|
auto p = allocForSending();
|
|
|
|
p->hop_limit = 0; // Assume just immediate neighbors for now
|
|
|
|
p->to = to;
|
2020-12-13 07:59:26 +00:00
|
|
|
DEBUG_MSG("Sending an ack=0x%x,to=0x%x,idFrom=0x%x,id=0x%x\n", isAck, to, idFrom, p->id);
|
2020-05-19 18:56:17 +00:00
|
|
|
|
|
|
|
if (isAck) {
|
|
|
|
p->decoded.ack.success_id = idFrom;
|
|
|
|
p->decoded.which_ack = SubPacket_success_id_tag;
|
|
|
|
} else {
|
|
|
|
p->decoded.ack.fail_id = idFrom;
|
|
|
|
p->decoded.which_ack = SubPacket_fail_id_tag;
|
|
|
|
}
|
|
|
|
|
2020-05-21 23:34:16 +00:00
|
|
|
sendLocal(p); // we sometimes send directly to the local node
|
2020-05-19 18:56:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 21:54:47 +00:00
|
|
|
#define NUM_RETRANSMISSIONS 3
|
|
|
|
|
|
|
|
PendingPacket::PendingPacket(MeshPacket *p)
|
|
|
|
{
|
|
|
|
packet = p;
|
|
|
|
numRetransmissions = NUM_RETRANSMISSIONS - 1; // We subtract one, because we assume the user just did the first send
|
|
|
|
}
|
|
|
|
|
2020-05-23 22:48:23 +00:00
|
|
|
PendingPacket *ReliableRouter::findPendingPacket(GlobalPacketId key)
|
|
|
|
{
|
|
|
|
auto old = pending.find(key); // If we have an old record, someone messed up because id got reused
|
|
|
|
if (old != pending.end()) {
|
|
|
|
return &old->second;
|
|
|
|
} else
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-05-19 18:56:17 +00:00
|
|
|
/**
|
|
|
|
* Stop any retransmissions we are doing of the specified node/packet ID pair
|
|
|
|
*/
|
2020-05-21 19:47:41 +00:00
|
|
|
bool ReliableRouter::stopRetransmission(NodeNum from, PacketId id)
|
2020-05-19 21:54:47 +00:00
|
|
|
{
|
|
|
|
auto key = GlobalPacketId(from, id);
|
2020-05-21 22:55:57 +00:00
|
|
|
return stopRetransmission(key);
|
2020-05-19 21:54:47 +00:00
|
|
|
}
|
2020-05-19 18:56:17 +00:00
|
|
|
|
2020-05-21 19:47:41 +00:00
|
|
|
bool ReliableRouter::stopRetransmission(GlobalPacketId key)
|
2020-05-19 21:54:47 +00:00
|
|
|
{
|
2020-05-23 22:48:23 +00:00
|
|
|
auto old = findPendingPacket(key);
|
|
|
|
if (old) {
|
2020-05-19 21:54:47 +00:00
|
|
|
auto numErased = pending.erase(key);
|
|
|
|
assert(numErased == 1);
|
2020-05-23 22:48:23 +00:00
|
|
|
packetPool.release(old->packet);
|
2020-05-21 19:47:41 +00:00
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
2020-05-19 21:54:47 +00:00
|
|
|
}
|
2020-05-23 22:48:23 +00:00
|
|
|
|
2020-05-19 18:56:17 +00:00
|
|
|
/**
|
|
|
|
* Add p to the list of packets to retransmit occasionally. We will free it once we stop retransmitting.
|
|
|
|
*/
|
2020-05-27 22:31:32 +00:00
|
|
|
PendingPacket *ReliableRouter::startRetransmission(MeshPacket *p)
|
2020-05-19 21:54:47 +00:00
|
|
|
{
|
|
|
|
auto id = GlobalPacketId(p);
|
|
|
|
auto rec = PendingPacket(p);
|
|
|
|
|
2020-11-12 09:49:04 +00:00
|
|
|
setNextTx(&rec);
|
2020-05-19 21:54:47 +00:00
|
|
|
stopRetransmission(p->from, p->id);
|
|
|
|
pending[id] = rec;
|
2020-05-27 22:31:32 +00:00
|
|
|
|
|
|
|
return &pending[id];
|
2020-05-19 21:54:47 +00:00
|
|
|
}
|
2020-05-19 18:56:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do any retransmissions that are scheduled (FIXME - for the time being called from loop)
|
|
|
|
*/
|
2020-10-10 01:57:57 +00:00
|
|
|
int32_t ReliableRouter::doRetransmissions()
|
2020-05-19 21:54:47 +00:00
|
|
|
{
|
2020-09-05 19:34:48 +00:00
|
|
|
uint32_t now = millis();
|
2020-10-10 01:57:57 +00:00
|
|
|
int32_t d = INT32_MAX;
|
2020-05-19 21:54:47 +00:00
|
|
|
|
|
|
|
// FIXME, we should use a better datastructure rather than walking through this map.
|
|
|
|
// for(auto el: pending) {
|
|
|
|
for (auto it = pending.begin(), nextIt = it; it != pending.end(); it = nextIt) {
|
|
|
|
++nextIt; // we use this odd pattern because we might be deleting it...
|
|
|
|
auto &p = it->second;
|
|
|
|
|
|
|
|
// FIXME, handle 51 day rolloever here!!!
|
|
|
|
if (p.nextTxMsec <= now) {
|
|
|
|
if (p.numRetransmissions == 0) {
|
2020-05-21 22:55:57 +00:00
|
|
|
DEBUG_MSG("Reliable send failed, returning a nak fr=0x%x,to=0x%x,id=%d\n", p.packet->from, p.packet->to,
|
|
|
|
p.packet->id);
|
2020-05-19 21:54:47 +00:00
|
|
|
sendAckNak(false, p.packet->from, p.packet->id);
|
2020-05-23 22:48:23 +00:00
|
|
|
// Note: we don't stop retransmission here, instead the Nak packet gets processed in sniffReceived - which
|
|
|
|
// allows the DSR version to still be able to look at the PendingPacket
|
2020-05-25 18:55:42 +00:00
|
|
|
stopRetransmission(it->first);
|
2020-05-19 21:54:47 +00:00
|
|
|
} else {
|
2020-05-21 22:55:57 +00:00
|
|
|
DEBUG_MSG("Sending reliable retransmission fr=0x%x,to=0x%x,id=%d, tries left=%d\n", p.packet->from, p.packet->to,
|
|
|
|
p.packet->id, p.numRetransmissions);
|
|
|
|
|
|
|
|
// Note: we call the superclass version because we don't want to have our version of send() add a new
|
|
|
|
// retransmission record
|
|
|
|
FloodingRouter::send(packetPool.allocCopy(*p.packet));
|
2020-05-19 21:54:47 +00:00
|
|
|
|
|
|
|
// Queue again
|
|
|
|
--p.numRetransmissions;
|
2020-11-12 09:49:04 +00:00
|
|
|
setNextTx(&p);
|
2020-05-19 21:54:47 +00:00
|
|
|
}
|
2020-11-12 09:49:04 +00:00
|
|
|
} else {
|
2020-10-10 01:57:57 +00:00
|
|
|
// Not yet time
|
|
|
|
int32_t t = p.nextTxMsec - now;
|
|
|
|
|
|
|
|
d = min(t, d);
|
|
|
|
}
|
2020-05-19 21:54:47 +00:00
|
|
|
}
|
2020-10-10 01:57:57 +00:00
|
|
|
|
|
|
|
return d;
|
2020-05-19 21:54:47 +00:00
|
|
|
}
|