2020-04-17 16:48:54 +00:00
|
|
|
#include "Router.h"
|
2020-05-10 00:51:20 +00:00
|
|
|
#include "CryptoEngine.h"
|
2020-10-07 23:28:57 +00:00
|
|
|
#include "RTC.h"
|
2020-04-17 16:48:54 +00:00
|
|
|
#include "configuration.h"
|
|
|
|
#include "mesh-pb-constants.h"
|
2020-05-19 00:57:58 +00:00
|
|
|
#include <NodeDB.h>
|
2020-04-17 16:48:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Router todo
|
|
|
|
*
|
2020-04-17 18:52:20 +00:00
|
|
|
* DONE: Implement basic interface and use it elsewhere in app
|
2020-04-17 16:48:54 +00:00
|
|
|
* Add naive flooding mixin (& drop duplicate rx broadcasts), add tools for sending broadcasts with incrementing sequence #s
|
|
|
|
* Add an optional adjacent node only 'send with ack' mixin. If we timeout waiting for the ack, call handleAckTimeout(packet)
|
|
|
|
* Add DSR mixin
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
|
|
|
#define MAX_RX_FROMRADIO \
|
|
|
|
4 // max number of packets destined to our queue, we dispatch packets quickly so it doesn't need to be big
|
|
|
|
|
|
|
|
// I think this is right, one packet for each of the three fifos + one packet being currently assembled for TX or RX
|
2020-06-13 15:27:44 +00:00
|
|
|
// And every TX packet might have a retransmission packet or an ack alive at any moment
|
2020-04-17 16:48:54 +00:00
|
|
|
#define MAX_PACKETS \
|
2020-06-13 15:27:44 +00:00
|
|
|
(MAX_RX_TOPHONE + MAX_RX_FROMRADIO + 2 * MAX_TX_QUEUE + \
|
2020-04-17 16:48:54 +00:00
|
|
|
2) // max number of packets which can be in flight (either queued from reception or queued for sending)
|
|
|
|
|
2020-07-10 20:52:26 +00:00
|
|
|
// static MemoryPool<MeshPacket> staticPool(MAX_PACKETS);
|
|
|
|
static MemoryDynamic<MeshPacket> staticPool;
|
2020-06-12 18:53:59 +00:00
|
|
|
|
|
|
|
Allocator<MeshPacket> &packetPool = staticPool;
|
2020-04-17 16:48:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* Currently we only allow one interface, that may change in the future
|
|
|
|
*/
|
2020-10-09 06:16:51 +00:00
|
|
|
Router::Router() : concurrency::OSThread("Router"), fromRadioQueue(MAX_RX_FROMRADIO)
|
2020-06-17 02:55:14 +00:00
|
|
|
{
|
2020-07-10 20:52:26 +00:00
|
|
|
// This is called pre main(), don't touch anything here, the following code is not safe
|
2020-06-17 23:04:37 +00:00
|
|
|
|
|
|
|
/* DEBUG_MSG("Size of NodeInfo %d\n", sizeof(NodeInfo));
|
2020-06-17 02:55:14 +00:00
|
|
|
DEBUG_MSG("Size of SubPacket %d\n", sizeof(SubPacket));
|
2020-06-17 23:04:37 +00:00
|
|
|
DEBUG_MSG("Size of MeshPacket %d\n", sizeof(MeshPacket)); */
|
2020-06-17 02:55:14 +00:00
|
|
|
}
|
2020-04-17 16:48:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* do idle processing
|
|
|
|
* Mostly looking in our incoming rxPacket queue and calling handleReceived.
|
|
|
|
*/
|
2020-10-09 06:16:51 +00:00
|
|
|
uint32_t Router::runOnce()
|
2020-04-17 16:48:54 +00:00
|
|
|
{
|
|
|
|
MeshPacket *mp;
|
|
|
|
while ((mp = fromRadioQueue.dequeuePtr(0)) != NULL) {
|
2020-05-23 16:24:22 +00:00
|
|
|
perhapsHandleReceived(mp);
|
2020-04-17 16:48:54 +00:00
|
|
|
}
|
2020-10-09 06:16:51 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-04-17 16:48:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:17 +00:00
|
|
|
/// Generate a unique packet id
|
|
|
|
// FIXME, move this someplace better
|
|
|
|
PacketId generatePacketId()
|
|
|
|
{
|
|
|
|
static uint32_t i; // Note: trying to keep this in noinit didn't help for working across reboots
|
|
|
|
static bool didInit = false;
|
|
|
|
|
2020-06-03 20:46:31 +00:00
|
|
|
assert(sizeof(PacketId) == 4 || sizeof(PacketId) == 1); // only supported values
|
|
|
|
uint32_t numPacketId = sizeof(PacketId) == 1 ? UINT8_MAX : UINT32_MAX; // 0 is consider invalid
|
2020-06-03 20:15:45 +00:00
|
|
|
|
2020-05-19 18:56:17 +00:00
|
|
|
if (!didInit) {
|
|
|
|
didInit = true;
|
2020-06-06 20:16:36 +00:00
|
|
|
|
|
|
|
// pick a random initial sequence number at boot (to prevent repeated reboots always starting at 0)
|
|
|
|
// Note: we mask the high order bit to ensure that we never pass a 'negative' number to random
|
|
|
|
i = random(numPacketId & 0x7fffffff);
|
|
|
|
DEBUG_MSG("Initial packet id %u, numPacketId %u\n", i, numPacketId);
|
2020-05-19 18:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
2020-06-03 20:46:31 +00:00
|
|
|
PacketId id = (i % numPacketId) + 1; // return number between 1 and numPacketId (ie - never zero)
|
2020-06-03 20:15:45 +00:00
|
|
|
myNodeInfo.current_packet_id = id; // Kinda crufty - we keep updating this so the phone can see a current value
|
|
|
|
return id;
|
2020-05-19 18:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MeshPacket *Router::allocForSending()
|
|
|
|
{
|
|
|
|
MeshPacket *p = packetPool.allocZeroed();
|
|
|
|
|
|
|
|
p->which_payload = MeshPacket_decoded_tag; // Assume payload is decoded at start.
|
|
|
|
p->from = nodeDB.getNodeNum();
|
|
|
|
p->to = NODENUM_BROADCAST;
|
|
|
|
p->hop_limit = HOP_RELIABLE;
|
|
|
|
p->id = generatePacketId();
|
2020-10-09 02:01:13 +00:00
|
|
|
p->rx_time = getValidTime(RTCQualityFromNet); // Just in case we process the packet locally - make sure it has a valid timestamp
|
2020-05-19 18:56:17 +00:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-05-21 23:34:16 +00:00
|
|
|
ErrorCode Router::sendLocal(MeshPacket *p)
|
|
|
|
{
|
|
|
|
if (p->to == nodeDB.getNodeNum()) {
|
|
|
|
DEBUG_MSG("Enqueuing internal message for the receive queue\n");
|
|
|
|
fromRadioQueue.enqueue(p);
|
|
|
|
return ERRNO_OK;
|
|
|
|
} else
|
|
|
|
return send(p);
|
|
|
|
}
|
|
|
|
|
2020-04-17 16:48:54 +00:00
|
|
|
/**
|
|
|
|
* Send a packet on a suitable interface. This routine will
|
|
|
|
* later free() the packet to pool. This routine is not allowed to stall.
|
2020-05-10 00:51:20 +00:00
|
|
|
* If the txmit queue is full it might return an error.
|
2020-04-17 16:48:54 +00:00
|
|
|
*/
|
|
|
|
ErrorCode Router::send(MeshPacket *p)
|
|
|
|
{
|
2020-05-21 23:34:16 +00:00
|
|
|
assert(p->to != nodeDB.getNodeNum()); // should have already been handled by sendLocal
|
|
|
|
|
2020-05-23 22:48:23 +00:00
|
|
|
PacketId nakId = p->decoded.which_ack == SubPacket_fail_id_tag ? p->decoded.ack.fail_id : 0;
|
|
|
|
assert(
|
|
|
|
!nakId); // I don't think we ever send 0hop naks over the wire (other than to the phone), test that assumption with assert
|
|
|
|
|
2020-05-21 23:34:16 +00:00
|
|
|
// Never set the want_ack flag on broadcast packets sent over the air.
|
|
|
|
if (p->to == NODENUM_BROADCAST)
|
|
|
|
p->want_ack = false;
|
|
|
|
|
|
|
|
// If the packet hasn't yet been encrypted, do so now (it might already be encrypted if we are just forwarding it)
|
|
|
|
|
|
|
|
assert(p->which_payload == MeshPacket_encrypted_tag ||
|
|
|
|
p->which_payload == MeshPacket_decoded_tag); // I _think_ all packets should have a payload by now
|
|
|
|
|
|
|
|
// First convert from protobufs to raw bytes
|
|
|
|
if (p->which_payload == MeshPacket_decoded_tag) {
|
|
|
|
static uint8_t bytes[MAX_RHPACKETLEN]; // we have to use a scratch buffer because a union
|
|
|
|
|
|
|
|
size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), SubPacket_fields, &p->decoded);
|
|
|
|
|
|
|
|
assert(numbytes <= MAX_RHPACKETLEN);
|
|
|
|
crypto->encrypt(p->from, p->id, numbytes, bytes);
|
|
|
|
|
|
|
|
// Copy back into the packet and set the variant type
|
|
|
|
memcpy(p->encrypted.bytes, bytes, numbytes);
|
|
|
|
p->encrypted.size = numbytes;
|
|
|
|
p->which_payload = MeshPacket_encrypted_tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iface) {
|
|
|
|
// DEBUG_MSG("Sending packet via interface fr=0x%x,to=0x%x,id=%d\n", p->from, p->to, p->id);
|
|
|
|
return iface->send(p);
|
2020-05-19 18:56:17 +00:00
|
|
|
} else {
|
2020-05-21 23:34:16 +00:00
|
|
|
DEBUG_MSG("Dropping packet - no interfaces - fr=0x%x,to=0x%x,id=%d\n", p->from, p->to, p->id);
|
|
|
|
packetPool.release(p);
|
|
|
|
return ERRNO_NO_INTERFACES;
|
2020-04-23 19:48:00 +00:00
|
|
|
}
|
2020-04-17 16:48:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 00:57:58 +00:00
|
|
|
/**
|
|
|
|
* Every (non duplicate) packet this node receives will be passed through this method. This allows subclasses to
|
|
|
|
* update routing tables etc... based on what we overhear (even for messages not destined to our node)
|
|
|
|
*/
|
2020-05-21 19:47:08 +00:00
|
|
|
void Router::sniffReceived(const MeshPacket *p)
|
2020-05-19 00:57:58 +00:00
|
|
|
{
|
2020-06-14 22:30:42 +00:00
|
|
|
DEBUG_MSG("FIXME-update-db Sniffing packet\n");
|
|
|
|
// FIXME, update nodedb here for any packet that passes through us
|
2020-05-19 00:57:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:17 +00:00
|
|
|
bool Router::perhapsDecode(MeshPacket *p)
|
2020-04-17 16:48:54 +00:00
|
|
|
{
|
2020-05-19 18:56:17 +00:00
|
|
|
if (p->which_payload == MeshPacket_decoded_tag)
|
|
|
|
return true; // If packet was already decoded just return
|
2020-04-17 16:48:54 +00:00
|
|
|
|
2020-05-19 18:56:17 +00:00
|
|
|
assert(p->which_payload == MeshPacket_encrypted_tag);
|
2020-05-10 00:51:20 +00:00
|
|
|
|
2020-05-19 00:57:58 +00:00
|
|
|
// FIXME - someday don't send routing packets encrypted. That would allow us to route for other channels without
|
|
|
|
// being able to decrypt their data.
|
2020-05-10 00:51:20 +00:00
|
|
|
// Try to decrypt the packet if we can
|
|
|
|
static uint8_t bytes[MAX_RHPACKETLEN];
|
|
|
|
memcpy(bytes, p->encrypted.bytes,
|
|
|
|
p->encrypted.size); // we have to copy into a scratch buffer, because these bytes are a union with the decoded protobuf
|
|
|
|
crypto->decrypt(p->from, p->id, p->encrypted.size, bytes);
|
|
|
|
|
|
|
|
// Take those raw bytes and convert them back into a well structured protobuf we can understand
|
|
|
|
if (!pb_decode_from_bytes(bytes, p->encrypted.size, SubPacket_fields, &p->decoded)) {
|
2020-05-19 18:56:17 +00:00
|
|
|
DEBUG_MSG("Invalid protobufs in received mesh packet!\n");
|
|
|
|
return false;
|
2020-05-10 00:51:20 +00:00
|
|
|
} else {
|
2020-05-19 18:56:17 +00:00
|
|
|
// parsing was successful
|
2020-05-10 00:51:20 +00:00
|
|
|
p->which_payload = MeshPacket_decoded_tag;
|
2020-05-19 18:56:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeNum Router::getNodeNum()
|
|
|
|
{
|
|
|
|
return nodeDB.getNodeNum();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle any packet that is received by an interface on this node.
|
|
|
|
* Note: some packets may merely being passed through this node and will be forwarded elsewhere.
|
|
|
|
*/
|
|
|
|
void Router::handleReceived(MeshPacket *p)
|
|
|
|
{
|
|
|
|
// Also, we should set the time from the ISR and it should have msec level resolution
|
2020-10-09 02:01:13 +00:00
|
|
|
p->rx_time = getValidTime(RTCQualityFromNet); // store the arrival timestamp for the phone
|
2020-05-19 18:56:17 +00:00
|
|
|
|
|
|
|
// Take those raw bytes and convert them back into a well structured protobuf we can understand
|
|
|
|
if (perhapsDecode(p)) {
|
|
|
|
// parsing was successful, queue for our recipient
|
2020-05-10 00:51:20 +00:00
|
|
|
|
2020-05-19 00:57:58 +00:00
|
|
|
sniffReceived(p);
|
2020-05-19 18:56:17 +00:00
|
|
|
|
|
|
|
if (p->to == NODENUM_BROADCAST || p->to == getNodeNum()) {
|
2020-06-14 22:30:42 +00:00
|
|
|
printPacket("Delivering rx packet", p);
|
2020-05-19 00:57:58 +00:00
|
|
|
notifyPacketReceived.notifyObservers(p);
|
|
|
|
}
|
2020-05-10 00:51:20 +00:00
|
|
|
}
|
2020-05-23 16:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Router::perhapsHandleReceived(MeshPacket *p)
|
|
|
|
{
|
2020-05-23 17:01:36 +00:00
|
|
|
assert(radioConfig.has_preferences);
|
2020-05-24 19:58:36 +00:00
|
|
|
bool ignore = is_in_repeated(radioConfig.preferences.ignore_incoming, p->from);
|
2020-05-23 17:01:36 +00:00
|
|
|
|
2020-05-24 19:58:36 +00:00
|
|
|
if (ignore)
|
2020-05-23 17:01:36 +00:00
|
|
|
DEBUG_MSG("Ignoring incoming message, 0x%x is in our ignore list\n", p->from);
|
2020-05-25 02:23:50 +00:00
|
|
|
else if (ignore |= shouldFilterReceived(p)) {
|
|
|
|
// DEBUG_MSG("Incoming message was filtered 0x%x\n", p->from);
|
|
|
|
}
|
2020-05-23 17:01:36 +00:00
|
|
|
|
|
|
|
// Note: we avoid calling shouldFilterReceived if we are supposed to ignore certain nodes - because some overrides might
|
|
|
|
// cache/learn of the existence of nodes (i.e. FloodRouter) that they should not
|
2020-05-24 19:58:36 +00:00
|
|
|
if (!ignore)
|
2020-05-23 16:24:22 +00:00
|
|
|
handleReceived(p);
|
2020-05-10 00:51:20 +00:00
|
|
|
|
2020-04-17 16:48:54 +00:00
|
|
|
packetPool.release(p);
|
|
|
|
}
|