Skip to content

Commit

Permalink
Set original hop limit in header flags
Browse files Browse the repository at this point in the history
  • Loading branch information
GUVWAF committed Oct 13, 2023
1 parent 3776064 commit 44dc270
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 78 deletions.
36 changes: 17 additions & 19 deletions src/mesh/NextHopRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ErrorCode NextHopRouter::send(meshtastic_MeshPacket *p)
// Add any messages _we_ send to the seen message list (so we will ignore all retransmissions we see)
wasSeenRecently(p); // FIXME, move this to a sniffSent method

p->next_hop = getNextHop(p->to, p->current_relayer); // set the next hop
p->next_hop = getNextHop(p->to, p->relay_node); // set the next hop
LOG_DEBUG("Setting next hop for packet with dest %x to %x\n", p->to, p->next_hop);

return Router::send(p);
Expand All @@ -19,7 +19,7 @@ ErrorCode NextHopRouter::send(meshtastic_MeshPacket *p)
bool NextHopRouter::shouldFilterReceived(const meshtastic_MeshPacket *p)
{
if (wasSeenRecently(p)) { // Note: this will also add a recent packet record
if (p->next_hop == getNodeNum()) {
if (p->next_hop == (uint8_t)(getNodeNum() & 0xFF)) {
LOG_DEBUG("Ignoring incoming msg, because we've already seen it.\n");
} else {
LOG_DEBUG("Ignoring incoming msg, because we've already seen it and cancel any outgoing packets.\n");
Expand All @@ -36,24 +36,24 @@ void NextHopRouter::sniffReceived(const meshtastic_MeshPacket *p, const meshtast
bool isAck =
((c && c->error_reason == meshtastic_Routing_Error_NONE)); // consider only ROUTING_APP message without error as ACK
if (isAck) {
// Update next-hop of this successful transmission to current relayer, but ONLY if "from" is not 0 or ourselves (means
// Update next-hop of this successful transmission to the relay node, but ONLY if "from" is not 0 or ourselves (means
// implicit ACK or someone is relaying our ACK)
if (p->from != 0 && p->from != getNodeNum()) {
if (p->current_relayer) {
if (p->relay_node) {
meshtastic_NodeInfoLite *sentTo = nodeDB.getMeshNode(p->from);
if (sentTo) {
LOG_DEBUG("Update next hop of %x to %x based on received ACK.\n", p->from, p->current_relayer);
sentTo->next_hop = p->current_relayer;
LOG_DEBUG("Update next hop of 0x%x to 0x%x based on received ACK.\n", p->from, p->relay_node);
sentTo->next_hop = p->relay_node;
}
}
}
}

if (config.device.role != meshtastic_Config_DeviceConfig_Role_CLIENT_MUTE) {
if ((p->to != getNodeNum()) && (getFrom(p) != getNodeNum())) {
if (p->next_hop == getNodeNum()) {
if (p->next_hop == (uint8_t)(getNodeNum() & 0xFF)) {
meshtastic_MeshPacket *tosend = packetPool.allocCopy(*p); // keep a copy because we will be sending it
LOG_INFO("Relaying received next-hop message coming from %x\n", p->current_relayer);
LOG_INFO("Relaying received next-hop message coming from %x\n", p->relay_node);

if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
// If it is a traceRoute request, update the route that it went via me
Expand All @@ -71,8 +71,7 @@ void NextHopRouter::sniffReceived(const meshtastic_MeshPacket *p, const meshtast
LOG_DEBUG("No preference for next hop, using FloodingRouter\n");
FloodingRouter::sniffReceived(p, c);
} else if (p->to == NODENUM_BROADCAST) {
// TODO how to handle broadcast messages?
LOG_DEBUG("TODO: Broadcast next-hop message\n");
// TODO: Smarter way of handling broadcasts
FloodingRouter::sniffReceived(p, c);
}
}
Expand All @@ -84,23 +83,22 @@ void NextHopRouter::sniffReceived(const meshtastic_MeshPacket *p, const meshtast
}

/**
* Get the next hop for a destination, given the current relayer
* Get the next hop for a destination, given the relay node
* @return the node number of the next hop, 0 if no preference (fallback to FloodingRouter)
*/
uint32_t NextHopRouter::getNextHop(NodeNum to, NodeNum current_relayer)
uint8_t NextHopRouter::getNextHop(NodeNum to, uint8_t relay_node)
{
meshtastic_NodeInfoLite *node = nodeDB.getMeshNode(to);
if (node) {
// We are careful not to return the current relayer as the next hop
if (node->next_hop != current_relayer) {
LOG_DEBUG("Next hop for %x is %x\n", to, node->next_hop);
// We are careful not to return the relay node as the next hop
if (node->next_hop != relay_node) {
LOG_DEBUG("Next hop for 0x%x is 0x%x\n", to, node->next_hop);
return node->next_hop;
} else {
LOG_WARN("Next hop for %x is %x, which is the same as current relayer; setting as no preference\n", to,
node->next_hop);
return 0;
LOG_WARN("Next hop for 0x%x is 0x%x, same as relayer; setting as no preference\n", to, node->next_hop);
return NO_NEXT_HOP_PREFERENCE;
}
} else {
return 0;
return NO_NEXT_HOP_PREFERENCE;
}
}
8 changes: 5 additions & 3 deletions src/mesh/NextHopRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/*
Router which only relays if it is the next hop for a packet.
The next hop is set by the current relayer of a packet, which bases this on information from either the NeighborInfoModule, or a
The next hop is set by the relay node of a packet, which bases this on information from either the NeighborInfoModule, or a
previous successful delivery via flooding. It is only used for DMs and not used for broadcasts. Using the NeighborInfoModule, it
can derive the next hop of neighbors and that of neighbors of neighbors. For others, it has no information in the beginning,
which results into falling back to the FloodingRouter. Upon successful delivery via flooding, it updates the next hop of the
Expand Down Expand Up @@ -40,10 +40,12 @@ class NextHopRouter : public FloodingRouter
*/
virtual void sniffReceived(const meshtastic_MeshPacket *p, const meshtastic_Routing *c) override;

constexpr static uint8_t NO_NEXT_HOP_PREFERENCE = 0;

private:
/**
* Get the next hop for a destination, given the current relayer
* Get the next hop for a destination, given the relay node
* @return the node number of the next hop, 0 if no preference (fallback to FloodingRouter)
*/
uint32_t getNextHop(NodeNum to, NodeNum current_relayer);
uint8_t getNextHop(NodeNum to, uint8_t relay_node);
};
15 changes: 5 additions & 10 deletions src/mesh/NodeDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,11 @@ void NodeDB::updateFrom(const meshtastic_MeshPacket &mp)
if (mp.decoded.portnum == meshtastic_PortNum_NODEINFO_APP) {
info->channel = mp.channel;
}

// If this packet didn't travel any hops, then it was sent directly to us, so we know what to use as next hop to this node
if (mp.original_hop_limit == mp.hop_limit) {
info->next_hop = (uint8_t)(mp.from & 0xFF);
}
}
}

Expand All @@ -814,16 +819,6 @@ meshtastic_NodeInfoLite *NodeDB::getMeshNode(NodeNum n)
return NULL;
}

// Find a node in the database that matches the last byte, return 0 if not found
NodeNum NodeDB::findMatchingNodeNum(uint8_t last_byte)
{
for (int i = 0; i < *numMeshNodes; i++)
if ((uint8_t)(meshNodes[i].num & 0xFF) == last_byte)
return meshNodes[i].num;

return 0;
}

/// Find a node in our DB, create an empty NodeInfo if missing
meshtastic_NodeInfoLite *NodeDB::getOrCreateMeshNode(NodeNum n)
{
Expand Down
1 change: 0 additions & 1 deletion src/mesh/NodeDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class NodeDB
}

meshtastic_NodeInfoLite *getMeshNode(NodeNum n);
NodeNum findMatchingNodeNum(uint8_t last_byte);
size_t getNumMeshNodes() { return *numMeshNodes; }

private:
Expand Down
5 changes: 3 additions & 2 deletions src/mesh/RadioInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,9 @@ size_t RadioInterface::beginSending(meshtastic_MeshPacket *p)
p->hop_limit = HOP_RELIABLE;
}
h->flags = p->hop_limit | (p->want_ack ? PACKET_FLAGS_WANT_ACK_MASK : 0);
h->next_hop = (p->next_hop & 0xFF); // set last byte of next_hop
h->current_relayer = (p->current_relayer & 0xFF); // set last byte of current_relayer
h->flags |= (p->original_hop_limit & PACKET_FLAGS_HOP_MASK) << PACKET_FLAGS_ORIG_HOP_SHIFT;
h->next_hop = p->next_hop;
h->relay_node = p->relay_node;

// if the sender nodenum is zero, that means uninitialized
assert(h->from);
Expand Down
6 changes: 4 additions & 2 deletions src/mesh/RadioInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#define PACKET_FLAGS_HOP_MASK 0x07
#define PACKET_FLAGS_WANT_ACK_MASK 0x08
#define PACKET_FLAGS_ORIG_HOP_MASK 0x70
#define PACKET_FLAGS_ORIG_HOP_SHIFT 4

/**
* This structure has to exactly match the wire layout when sent over the radio link. Used to keep compatibility
Expand All @@ -35,8 +37,8 @@ typedef struct {
// Last byte of the NodeNum of the next-hop for this packet
uint8_t next_hop;

// Last byte of the NodeNum of the current relayer of this packet
uint8_t current_relayer;
// Last byte of the NodeNum of the node that will relay/relayed this packet
uint8_t relay_node;
} PacketHeader;

/**
Expand Down
5 changes: 3 additions & 2 deletions src/mesh/RadioLibInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,10 @@ void RadioLibInterface::handleReceiveInterrupt()
mp->channel = h->channel;
assert(HOP_MAX <= PACKET_FLAGS_HOP_MASK); // If hopmax changes, carefully check this code
mp->hop_limit = h->flags & PACKET_FLAGS_HOP_MASK;
mp->original_hop_limit = (h->flags & PACKET_FLAGS_ORIG_HOP_MASK) >> PACKET_FLAGS_ORIG_HOP_SHIFT;
mp->want_ack = !!(h->flags & PACKET_FLAGS_WANT_ACK_MASK);
mp->next_hop = nodeDB.findMatchingNodeNum(h->next_hop);
mp->current_relayer = nodeDB.findMatchingNodeNum(h->current_relayer);
mp->next_hop = h->next_hop;
mp->relay_node = h->relay_node;

addReceiveMetadata(mp);

Expand Down
16 changes: 8 additions & 8 deletions src/mesh/ReliableRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ bool ReliableRouter::shouldFilterReceived(const meshtastic_MeshPacket *p)
i->second.nextTxMsec += iface->getPacketTime(p);
}

/* Resend implicit ACKs for repeated packets (current relayer is the same as original transmitter)
* this way if an implicit ACK is dropped and a packet is resent we'll rebroadcast again.
* Resending real ACKs is omitted, as you might receive a packet multiple times due to flooding and
* flooding this ACK back to the original sender already adds redundancy. */
if (wasSeenRecently(p, false) && p->current_relayer == p->from && !MeshModule::currentReply && p->to != nodeDB.getNodeNum()) {
/* Resend implicit ACKs for repeated packets (hop limit is the original setting) this way if an implicit ACK is dropped and a
* packet is resent we'll rebroadcast again. Resending real ACKs is omitted, as you might receive a packet multiple times due
* to flooding and flooding this ACK back to the original sender already adds redundancy. */
if (wasSeenRecently(p, false) && (p->original_hop_limit == p->hop_limit) && !MeshModule::currentReply &&
p->to != nodeDB.getNodeNum()) {
// retransmission on broadcast has hop_limit still equal to HOP_RELIABLE
LOG_DEBUG("Resending implicit ack for a repeated floodmsg\n");
meshtastic_MeshPacket *tosend = packetPool.allocCopy(*p);
Expand Down Expand Up @@ -224,12 +224,12 @@ int32_t ReliableRouter::doRetransmissions()

if (config.lora.next_hop_routing && p.numRetransmissions == 1) {
// Last retransmission, reset next_hop (fallback to FloodingRouter)
p.packet->next_hop = 0;
p.packet->next_hop = NO_NEXT_HOP_PREFERENCE;
// Also reset it in the nodeDB
meshtastic_NodeInfoLite *sentTo = nodeDB.getMeshNode(p.packet->to);
if (sentTo) {
LOG_DEBUG("Resetting next hop for packet with dest %x\n", p.packet->to);
sentTo->next_hop = 0;
LOG_DEBUG("Resetting next hop for packet with dest 0x%x\n", p.packet->to);
sentTo->next_hop = NO_NEXT_HOP_PREFERENCE;
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/mesh/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ ErrorCode Router::send(meshtastic_MeshPacket *p)
// the lora we need to make sure we have replaced it with our local address
p->from = getFrom(p);

p->current_relayer = getNodeNum(); // set the current relayer to us
// If we are the original transmitter, set the original hop limit
if (p->from == nodeDB.getNodeNum())
p->original_hop_limit = config.lora.hop_limit ? config.lora.hop_limit : HOP_RELIABLE;

p->relay_node = (uint8_t)(getNodeNum() & 0xFF); // set the current relayer to us

// If the packet hasn't yet been encrypted, do so now (it might already be encrypted if we are just forwarding it)

Expand Down
8 changes: 2 additions & 6 deletions src/mesh/generated/meshtastic/config.pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ typedef enum _meshtastic_Config_DeviceConfig_Role {
or any other packet type. They will simply rebroadcast any mesh packets on the same frequency, channel num, spread factor, and coding rate. */
meshtastic_Config_DeviceConfig_Role_REPEATER = 4,
/* Tracker device role
Position Mesh packets will be prioritized higher and sent more frequently by default.
When used in conjunction with power.is_power_saving = true, nodes will wake up,
send position, and then sleep for position.position_broadcast_secs seconds. */
Position Mesh packets will be prioritized higher and sent more frequently by default. */
meshtastic_Config_DeviceConfig_Role_TRACKER = 5,
/* Sensor device role
Telemetry Mesh packets will be prioritized higher and sent more frequently by default.
When used in conjunction with power.is_power_saving = true, nodes will wake up,
send environment telemetry, and then sleep for telemetry.environment_update_interval seconds. */
Telemetry Mesh packets will be prioritized higher and sent more frequently by default. */
meshtastic_Config_DeviceConfig_Role_SENSOR = 6
} meshtastic_Config_DeviceConfig_Role;

Expand Down
8 changes: 4 additions & 4 deletions src/mesh/generated/meshtastic/deviceonly.pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ typedef struct _meshtastic_NodeInfoLite {
meshtastic_DeviceMetrics device_metrics;
/* local channel index we heard that node on. Only populated if its not the default channel. */
uint8_t channel;
/* Node number of the node to use as a next hop in order to reach this node. */
uint32_t next_hop;
/* Last byte of the node number of the node to use as a next hop in order to reach this node. */
uint8_t next_hop;
} meshtastic_NodeInfoLite;

/* The on-disk saved channels */
Expand Down Expand Up @@ -317,8 +317,8 @@ extern const pb_msgdesc_t meshtastic_NodeRemoteHardwarePin_msg;

/* Maximum encoded size of messages (where known) */
#define meshtastic_ChannelFile_size 638
#define meshtastic_DeviceState_size 17490
#define meshtastic_NodeInfoLite_size 157
#define meshtastic_DeviceState_size 17193
#define meshtastic_NodeInfoLite_size 154
#define meshtastic_NodeRemoteHardwarePin_size 29
#define meshtastic_OEMStore_size 3220
#define meshtastic_PositionLite_size 28
Expand Down
25 changes: 14 additions & 11 deletions src/mesh/generated/meshtastic/mesh.pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,13 @@ typedef struct _meshtastic_MeshPacket {
int32_t rx_rssi;
/* Describe if this message is delayed */
meshtastic_MeshPacket_Delayed delayed;
/* Node number of the node that should be used as the next hop in routing.
Only the last byte is sent in the packet header. */
uint32_t next_hop;
/* Node number of the node that is currently relaying this packet.
Only the last byte is sent in the packet header. */
uint32_t current_relayer;
/* Last byte of the node number of the node that should be used as the next hop in routing. */
uint8_t next_hop;
/* Last byte of the node number of the node that will relay/relayed this packet. */
uint8_t relay_node;
/* The hop limit setting of the original transmitter.
Useful to determine the amount of hops a packet traveled upon reception, which is this value minus the `hop_limit` with which it arrived. */
uint32_t original_hop_limit;
} meshtastic_MeshPacket;

/* The bluetooth to device link:
Expand Down Expand Up @@ -864,7 +865,7 @@ extern "C" {
#define meshtastic_Data_init_default {_meshtastic_PortNum_MIN, {0, {0}}, 0, 0, 0, 0, 0, 0}
#define meshtastic_Waypoint_init_default {0, 0, 0, 0, 0, "", "", 0}
#define meshtastic_MqttClientProxyMessage_init_default {"", 0, {{0, {0}}}, 0}
#define meshtastic_MeshPacket_init_default {0, 0, 0, 0, {meshtastic_Data_init_default}, 0, 0, 0, 0, 0, _meshtastic_MeshPacket_Priority_MIN, 0, _meshtastic_MeshPacket_Delayed_MIN, 0, 0}
#define meshtastic_MeshPacket_init_default {0, 0, 0, 0, {meshtastic_Data_init_default}, 0, 0, 0, 0, 0, _meshtastic_MeshPacket_Priority_MIN, 0, _meshtastic_MeshPacket_Delayed_MIN, 0, 0, 0}
#define meshtastic_NodeInfo_init_default {0, false, meshtastic_User_init_default, false, meshtastic_Position_init_default, 0, 0, false, meshtastic_DeviceMetrics_init_default, 0}
#define meshtastic_MyNodeInfo_init_default {0, 0, 0}
#define meshtastic_LogRecord_init_default {"", 0, "", _meshtastic_LogRecord_Level_MIN}
Expand All @@ -882,7 +883,7 @@ extern "C" {
#define meshtastic_Data_init_zero {_meshtastic_PortNum_MIN, {0, {0}}, 0, 0, 0, 0, 0, 0}
#define meshtastic_Waypoint_init_zero {0, 0, 0, 0, 0, "", "", 0}
#define meshtastic_MqttClientProxyMessage_init_zero {"", 0, {{0, {0}}}, 0}
#define meshtastic_MeshPacket_init_zero {0, 0, 0, 0, {meshtastic_Data_init_zero}, 0, 0, 0, 0, 0, _meshtastic_MeshPacket_Priority_MIN, 0, _meshtastic_MeshPacket_Delayed_MIN, 0, 0}
#define meshtastic_MeshPacket_init_zero {0, 0, 0, 0, {meshtastic_Data_init_zero}, 0, 0, 0, 0, 0, _meshtastic_MeshPacket_Priority_MIN, 0, _meshtastic_MeshPacket_Delayed_MIN, 0, 0, 0}
#define meshtastic_NodeInfo_init_zero {0, false, meshtastic_User_init_zero, false, meshtastic_Position_init_zero, 0, 0, false, meshtastic_DeviceMetrics_init_zero, 0}
#define meshtastic_MyNodeInfo_init_zero {0, 0, 0}
#define meshtastic_LogRecord_init_zero {"", 0, "", _meshtastic_LogRecord_Level_MIN}
Expand Down Expand Up @@ -961,7 +962,8 @@ extern "C" {
#define meshtastic_MeshPacket_rx_rssi_tag 12
#define meshtastic_MeshPacket_delayed_tag 13
#define meshtastic_MeshPacket_next_hop_tag 14
#define meshtastic_MeshPacket_current_relayer_tag 15
#define meshtastic_MeshPacket_relay_node_tag 15
#define meshtastic_MeshPacket_original_hop_limit_tag 16
#define meshtastic_NodeInfo_num_tag 1
#define meshtastic_NodeInfo_user_tag 2
#define meshtastic_NodeInfo_position_tag 3
Expand Down Expand Up @@ -1118,7 +1120,8 @@ X(a, STATIC, SINGULAR, UENUM, priority, 11) \
X(a, STATIC, SINGULAR, INT32, rx_rssi, 12) \
X(a, STATIC, SINGULAR, UENUM, delayed, 13) \
X(a, STATIC, SINGULAR, UINT32, next_hop, 14) \
X(a, STATIC, SINGULAR, UINT32, current_relayer, 15)
X(a, STATIC, SINGULAR, UINT32, relay_node, 15) \
X(a, STATIC, SINGULAR, UINT32, original_hop_limit, 16)
#define meshtastic_MeshPacket_CALLBACK NULL
#define meshtastic_MeshPacket_DEFAULT NULL
#define meshtastic_MeshPacket_payload_variant_decoded_MSGTYPE meshtastic_Data
Expand Down Expand Up @@ -1283,7 +1286,7 @@ extern const pb_msgdesc_t meshtastic_DeviceMetadata_msg;
#define meshtastic_DeviceMetadata_size 46
#define meshtastic_FromRadio_size 510
#define meshtastic_LogRecord_size 81
#define meshtastic_MeshPacket_size 333
#define meshtastic_MeshPacket_size 334
#define meshtastic_MqttClientProxyMessage_size 501
#define meshtastic_MyNodeInfo_size 18
#define meshtastic_NeighborInfo_size 268
Expand Down

0 comments on commit 44dc270

Please sign in to comment.