Add nodedb channel handling (#2384)

* send ourNodeInfo to channel we got a message we heared someone new

* store node-channel into nodeDB

* use channel from nodeDb to send local messages

* update protobufs

* fmt and fix braces

* respect requested channel for local send, only store channel while getting a nodeinfo packet

---------

Co-authored-by: Thomas Göttgens <tgoettgens@gmail.com>
Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
sbias 2023-03-29 13:51:22 +02:00 committed by GitHub
parent 82ba59765c
commit 26d18244f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 39 additions and 11 deletions

View File

@ -77,8 +77,8 @@ int MeshService::handleFromRadio(const meshtastic_MeshPacket *mp)
nodeDB.updateFrom(*mp); // update our DB state based off sniffing every RX packet from the radio nodeDB.updateFrom(*mp); // update our DB state based off sniffing every RX packet from the radio
if (mp->which_payload_variant == meshtastic_MeshPacket_decoded_tag && !nodeDB.getNode(mp->from)->has_user && nodeInfoModule) { if (mp->which_payload_variant == meshtastic_MeshPacket_decoded_tag && !nodeDB.getNode(mp->from)->has_user && nodeInfoModule) {
LOG_INFO("Heard a node we don't know, sending NodeInfo and asking for a response.\n"); LOG_INFO("Heard a node on channel %d we don't know, sending NodeInfo and asking for a response.\n", mp->channel);
nodeInfoModule->sendOurNodeInfo(mp->from, true); nodeInfoModule->sendOurNodeInfo(mp->from, true, mp->channel);
} }
printPacket("Forwarding to phone", mp); printPacket("Forwarding to phone", mp);
@ -242,13 +242,13 @@ void MeshService::sendNetworkPing(NodeNum dest, bool wantReplies)
if (node->has_position && (node->position.latitude_i != 0 || node->position.longitude_i != 0)) { if (node->has_position && (node->position.latitude_i != 0 || node->position.longitude_i != 0)) {
if (positionModule) { if (positionModule) {
LOG_INFO("Sending position ping to 0x%x, wantReplies=%d\n", dest, wantReplies); LOG_INFO("Sending position ping to 0x%x, wantReplies=%d, channel=%d\n", dest, wantReplies, node->channel);
positionModule->sendOurPosition(dest, wantReplies); positionModule->sendOurPosition(dest, wantReplies, node->channel);
} }
} else { } else {
if (nodeInfoModule) { if (nodeInfoModule) {
LOG_INFO("Sending nodeinfo ping to 0x%x, wantReplies=%d\n", dest, wantReplies); LOG_INFO("Sending nodeinfo ping to 0x%x, wantReplies=%d, channel=%d\n", dest, wantReplies, node->channel);
nodeInfoModule->sendOurNodeInfo(dest, wantReplies); nodeInfoModule->sendOurNodeInfo(dest, wantReplies, node->channel);
} }
} }
} }

View File

@ -721,7 +721,7 @@ void NodeDB::updateUser(uint32_t nodeId, const meshtastic_User &p)
void NodeDB::updateFrom(const meshtastic_MeshPacket &mp) void NodeDB::updateFrom(const meshtastic_MeshPacket &mp)
{ {
if (mp.which_payload_variant == meshtastic_MeshPacket_decoded_tag && mp.from) { if (mp.which_payload_variant == meshtastic_MeshPacket_decoded_tag && mp.from) {
LOG_DEBUG("Update DB node 0x%x, rx_time=%u\n", mp.from, mp.rx_time); LOG_DEBUG("Update DB node 0x%x, rx_time=%u, channel=%d\n", mp.from, mp.rx_time, mp.channel);
meshtastic_NodeInfo *info = getOrCreateNode(getFrom(&mp)); meshtastic_NodeInfo *info = getOrCreateNode(getFrom(&mp));
if (!info) { if (!info) {
@ -733,9 +733,22 @@ void NodeDB::updateFrom(const meshtastic_MeshPacket &mp)
if (mp.rx_snr) if (mp.rx_snr)
info->snr = mp.rx_snr; // keep the most recent SNR we received for this node. info->snr = mp.rx_snr; // keep the most recent SNR we received for this node.
if (mp.decoded.portnum == meshtastic_PortNum_NODEINFO_APP) {
info->channel = mp.channel;
}
} }
} }
uint8_t NodeDB::getNodeChannel(NodeNum n)
{
meshtastic_NodeInfo *info = getNode(n);
if (!info) {
return 0; // defaults to PRIMARY
}
return info->channel;
}
/// Find a node in our DB, return null for missing /// Find a node in our DB, return null for missing
/// NOTE: This function might be called from an ISR /// NOTE: This function might be called from an ISR
meshtastic_NodeInfo *NodeDB::getNode(NodeNum n) meshtastic_NodeInfo *NodeDB::getNode(NodeNum n)

View File

@ -113,6 +113,9 @@ class NodeDB
/// pick a provisional nodenum we hope no one is using /// pick a provisional nodenum we hope no one is using
void pickNewNodeNum(); void pickNewNodeNum();
// get channel channel index we heard a nodeNum on, defaults to 0 if not found
uint8_t getNodeChannel(NodeNum n);
/// Find a node in our DB, return null for missing /// Find a node in our DB, return null for missing
meshtastic_NodeInfo *getNode(NodeNum n); meshtastic_NodeInfo *getNode(NodeNum n);

View File

@ -173,6 +173,10 @@ ErrorCode Router::sendLocal(meshtastic_MeshPacket *p, RxSource src)
handleReceived(p, src); handleReceived(p, src);
} }
if (p->channel) // don't override if a channel was requested
p->channel = nodeDB.getNodeChannel(p->to);
LOG_DEBUG("localSend to channel %d\n", p->channel);
return send(p); return send(p);
} }
} }

View File

@ -27,7 +27,7 @@ bool NodeInfoModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, mes
return false; // Let others look at this message also if they want return false; // Let others look at this message also if they want
} }
void NodeInfoModule::sendOurNodeInfo(NodeNum dest, bool wantReplies) void NodeInfoModule::sendOurNodeInfo(NodeNum dest, bool wantReplies, uint8_t channel)
{ {
// cancel any not yet sent (now stale) position packets // cancel any not yet sent (now stale) position packets
if (prevPacketId) // if we wrap around to zero, we'll simply fail to cancel in that rare case (no big deal) if (prevPacketId) // if we wrap around to zero, we'll simply fail to cancel in that rare case (no big deal)
@ -38,6 +38,11 @@ void NodeInfoModule::sendOurNodeInfo(NodeNum dest, bool wantReplies)
p->to = dest; p->to = dest;
p->decoded.want_response = wantReplies; p->decoded.want_response = wantReplies;
p->priority = meshtastic_MeshPacket_Priority_BACKGROUND; p->priority = meshtastic_MeshPacket_Priority_BACKGROUND;
if (channel > 0) {
LOG_DEBUG("sending ourNodeInfo to channel %d\n", channel);
p->channel = channel;
}
prevPacketId = p->id; prevPacketId = p->id;
service.sendToMesh(p); service.sendToMesh(p);

View File

@ -20,7 +20,7 @@ class NodeInfoModule : public ProtobufModule<meshtastic_User>, private concurren
/** /**
* Send our NodeInfo into the mesh * Send our NodeInfo into the mesh
*/ */
void sendOurNodeInfo(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); void sendOurNodeInfo(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false, uint8_t channel = 0);
protected: protected:
/** Called to handle a particular incoming message /** Called to handle a particular incoming message

View File

@ -120,7 +120,7 @@ meshtastic_MeshPacket *PositionModule::allocReply()
return allocDataProtobuf(p); return allocDataProtobuf(p);
} }
void PositionModule::sendOurPosition(NodeNum dest, bool wantReplies) void PositionModule::sendOurPosition(NodeNum dest, bool wantReplies, uint8_t channel)
{ {
// cancel any not yet sent (now stale) position packets // cancel any not yet sent (now stale) position packets
if (prevPacketId) // if we wrap around to zero, we'll simply fail to cancel in that rare case (no big deal) if (prevPacketId) // if we wrap around to zero, we'll simply fail to cancel in that rare case (no big deal)
@ -135,6 +135,9 @@ void PositionModule::sendOurPosition(NodeNum dest, bool wantReplies)
p->priority = meshtastic_MeshPacket_Priority_BACKGROUND; p->priority = meshtastic_MeshPacket_Priority_BACKGROUND;
prevPacketId = p->id; prevPacketId = p->id;
if (channel > 0)
p->channel = channel;
service.sendToMesh(p, RX_SRC_LOCAL, true); service.sendToMesh(p, RX_SRC_LOCAL, true);
} }

View File

@ -29,7 +29,7 @@ class PositionModule : public ProtobufModule<meshtastic_Position>, private concu
/** /**
* Send our position into the mesh * Send our position into the mesh
*/ */
void sendOurPosition(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); void sendOurPosition(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false, uint8_t channel = 0);
protected: protected:
/** Called to handle a particular incoming message /** Called to handle a particular incoming message