diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index ee4ddcd77..6a49294cd 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -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 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"); - nodeInfoModule->sendOurNodeInfo(mp->from, true); + 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, mp->channel); } 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 (positionModule) { - LOG_INFO("Sending position ping to 0x%x, wantReplies=%d\n", dest, wantReplies); - positionModule->sendOurPosition(dest, wantReplies); + LOG_INFO("Sending position ping to 0x%x, wantReplies=%d, channel=%d\n", dest, wantReplies, node->channel); + positionModule->sendOurPosition(dest, wantReplies, node->channel); } } else { if (nodeInfoModule) { - LOG_INFO("Sending nodeinfo ping to 0x%x, wantReplies=%d\n", dest, wantReplies); - nodeInfoModule->sendOurNodeInfo(dest, wantReplies); + LOG_INFO("Sending nodeinfo ping to 0x%x, wantReplies=%d, channel=%d\n", dest, wantReplies, node->channel); + nodeInfoModule->sendOurNodeInfo(dest, wantReplies, node->channel); } } } diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 3a7055aed..c620295c2 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -721,7 +721,7 @@ void NodeDB::updateUser(uint32_t nodeId, const meshtastic_User &p) void NodeDB::updateFrom(const meshtastic_MeshPacket &mp) { 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)); if (!info) { @@ -733,9 +733,22 @@ void NodeDB::updateFrom(const meshtastic_MeshPacket &mp) if (mp.rx_snr) 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 /// NOTE: This function might be called from an ISR meshtastic_NodeInfo *NodeDB::getNode(NodeNum n) diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 6ab4c46e5..2822ffbf6 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -113,6 +113,9 @@ class NodeDB /// pick a provisional nodenum we hope no one is using 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 meshtastic_NodeInfo *getNode(NodeNum n); diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index fc38f9f5e..db0509aca 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -173,6 +173,10 @@ ErrorCode Router::sendLocal(meshtastic_MeshPacket *p, RxSource 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); } } diff --git a/src/modules/NodeInfoModule.cpp b/src/modules/NodeInfoModule.cpp index 004ad75f0..cf8c17c3b 100644 --- a/src/modules/NodeInfoModule.cpp +++ b/src/modules/NodeInfoModule.cpp @@ -27,7 +27,7 @@ bool NodeInfoModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, mes 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 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->decoded.want_response = wantReplies; p->priority = meshtastic_MeshPacket_Priority_BACKGROUND; + if (channel > 0) { + LOG_DEBUG("sending ourNodeInfo to channel %d\n", channel); + p->channel = channel; + } + prevPacketId = p->id; service.sendToMesh(p); diff --git a/src/modules/NodeInfoModule.h b/src/modules/NodeInfoModule.h index 900d5983d..b10cccdf1 100644 --- a/src/modules/NodeInfoModule.h +++ b/src/modules/NodeInfoModule.h @@ -20,7 +20,7 @@ class NodeInfoModule : public ProtobufModule, private concurren /** * 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: /** Called to handle a particular incoming message diff --git a/src/modules/PositionModule.cpp b/src/modules/PositionModule.cpp index 92a8b4c0c..ade96fb8a 100644 --- a/src/modules/PositionModule.cpp +++ b/src/modules/PositionModule.cpp @@ -120,7 +120,7 @@ meshtastic_MeshPacket *PositionModule::allocReply() 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 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; prevPacketId = p->id; + if (channel > 0) + p->channel = channel; + service.sendToMesh(p, RX_SRC_LOCAL, true); } diff --git a/src/modules/PositionModule.h b/src/modules/PositionModule.h index 8e9c37087..aaa5c76c5 100644 --- a/src/modules/PositionModule.h +++ b/src/modules/PositionModule.h @@ -29,7 +29,7 @@ class PositionModule : public ProtobufModule, private concu /** * 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: /** Called to handle a particular incoming message