Initial version of NextHopRouter

This commit is contained in:
GUVWAF
2023-10-02 19:31:23 +02:00
parent a6e4402e41
commit 0d6729b9eb
20 changed files with 316 additions and 36 deletions

View File

@@ -156,6 +156,17 @@ size_t NeighborInfoModule::cleanUpNeighbors()
// Update the neighbor list
for (uint i = 0; i < indices_to_remove.size(); i++) {
int index = indices_to_remove[i];
if (config.lora.next_hop_routing) {
// Clear all next hops of nodes that had this neighbor as next hop
for (unsigned int j = 0; j < nodeDB.getNumMeshNodes(); j++) {
meshtastic_NodeInfoLite *node = nodeDB.getMeshNodeByIndex(j);
if (node->next_hop == neighbors[index].node_id) {
node->next_hop = 0;
}
}
}
LOG_DEBUG("Removing neighbor with node ID 0x%x\n", neighbors[index].node_id);
for (int j = index; j < num_neighbors - 1; j++) {
neighbors[j] = neighbors[j + 1];
@@ -205,6 +216,8 @@ bool NeighborInfoModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp,
if (enabled) {
printNeighborInfo("RECEIVED", np);
updateNeighbors(mp, np);
if (config.lora.next_hop_routing)
updateNextHops(np);
}
// Allow others to handle this packet
return false;
@@ -229,6 +242,54 @@ void NeighborInfoModule::updateLastSentById(meshtastic_MeshPacket *p)
pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), &meshtastic_NeighborInfo_msg, updated);
}
/* Update the next hop for nodes in the database.
* Based on our own neighbors, and the neighbors of our neighbors.
*/
void NeighborInfoModule::updateNextHops(meshtastic_NeighborInfo *np)
{
LOG_DEBUG("Updating next hops based on received NeighborInfo packet\n");
meshtastic_NodeInfoLite *currentNode = nodeDB.getMeshNode(np->node_id);
// Check if the sender of this neighborInfo packet is a neighbor of ourselves
if (currentNode && isANeighbor(np->node_id)) {
currentNode->next_hop = np->node_id; // Set the next hop to the sender of this packet
for (uint8_t i = 0; i < np->neighbors_count; i++) {
if (isANeighbor(np->neighbors[i].node_id))
continue; // This node is a neighbor of ourselves
meshtastic_NodeInfoLite *neighborOfCurrentNode = nodeDB.getMeshNode(np->neighbors[i].node_id);
// Update next hop of this node to the sender of this packet, because it is the most recent neighbor
if (neighborOfCurrentNode)
neighborOfCurrentNode->next_hop = currentNode->num;
}
} else if (currentNode) { // Sender is not a neighbor
// Find common neighbors and use the most recent as next hop to this node
meshtastic_NodeInfoLite *currentNextHop = nodeDB.getMeshNode(currentNode->next_hop);
uint32_t maxLastHeard = currentNextHop ? currentNextHop->last_heard : 0;
for (uint8_t i = 0; i < np->neighbors_count; i++) {
meshtastic_NodeInfoLite *neighborOfCurrentNode = nodeDB.getMeshNode(np->neighbors[i].node_id);
if (neighborOfCurrentNode && isANeighbor(neighborOfCurrentNode->num)) {
// This neighbor was heard more recently than the current next hop
if (neighborOfCurrentNode->last_heard > maxLastHeard) {
currentNode->next_hop = neighborOfCurrentNode->num;
maxLastHeard = neighborOfCurrentNode->last_heard;
LOG_DEBUG("More recent node found, so update next_hop of %x to %x\n", currentNode->num,
neighborOfCurrentNode->num);
}
}
}
}
}
bool NeighborInfoModule::isANeighbor(NodeNum node_id)
{
for (int i = 0; i < *numNeighbors; i++) {
if (neighbors[i].node_id == node_id) {
return true;
}
}
return false;
}
void NeighborInfoModule::resetNeighbors()
{
*numNeighbors = 0;
@@ -308,4 +369,4 @@ bool NeighborInfoModule::saveProtoForModule()
okay &= nodeDB.saveProto(neighborInfoConfigFile, meshtastic_NeighborInfo_size, &meshtastic_NeighborInfo_msg, &neighborState);
return okay;
}
}

View File

@@ -20,8 +20,9 @@ class NeighborInfoModule : public ProtobufModule<meshtastic_NeighborInfo>, priva
bool saveProtoForModule();
// Let FloodingRouter call updateLastSentById upon rebroadcasting a NeighborInfo packet
// Let FloodingRouter/NexthopRouter call updateLastSentById upon rebroadcasting a NeighborInfo packet
friend class FloodingRouter;
friend class NextHopRouter;
protected:
// Note: this holds our local info.
@@ -70,6 +71,12 @@ class NeighborInfoModule : public ProtobufModule<meshtastic_NeighborInfo>, priva
/* update a NeighborInfo packet with our NodeNum as last_sent_by_id */
void updateLastSentById(meshtastic_MeshPacket *p);
/* update the next hop for nodes in the database */
void updateNextHops(meshtastic_NeighborInfo *np);
/* check if the given node number is a neighbor of us */
bool isANeighbor(NodeNum node_id);
void loadProtoForModule();
/* Does our periodic broadcast */

View File

@@ -9,8 +9,9 @@ class TraceRouteModule : public ProtobufModule<meshtastic_RouteDiscovery>
public:
TraceRouteModule();
// Let FloodingRouter call updateRoute upon rebroadcasting a TraceRoute request
// Let FloodingRouter/NextHopRouter call updateRoute upon rebroadcasting a TraceRoute request
friend class FloodingRouter;
friend class NextHopRouter;
protected:
bool handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_RouteDiscovery *r) override;