diff --git a/src/mesh/Channels.cpp b/src/mesh/Channels.cpp index 4bdd9e674..116ad14ad 100644 --- a/src/mesh/Channels.cpp +++ b/src/mesh/Channels.cpp @@ -314,6 +314,15 @@ void Channels::setChannel(const meshtastic_Channel &c) old = c; // slam in the new settings/role } +int8_t Channels::getIndexByHash(ChannelHash channelHash) +{ + for (int i = 0; i < getNumChannels(); i++) + if (getHash(i) == channelHash) + return i; + + return -1; +} + bool Channels::anyMqttEnabled() { #if USERPREFS_EVENT_MODE diff --git a/src/mesh/Channels.h b/src/mesh/Channels.h index b0c9b3d07..81da8277c 100644 --- a/src/mesh/Channels.h +++ b/src/mesh/Channels.h @@ -92,6 +92,9 @@ class Channels // Returns true if any of our channels have enabled MQTT uplink or downlink bool anyMqttEnabled(); + /** Return the channel index for the specified channel hash, or -1 for not found */ + int8_t getIndexByHash(ChannelHash channelHash); + private: /** Given a channel index, change to use the crypto key specified by that index * @@ -99,9 +102,6 @@ class Channels */ int16_t setCrypto(ChannelIndex chIndex); - /** Return the channel index for the specified channel hash, or -1 for not found */ - int8_t getIndexByHash(ChannelHash channelHash); - /** Given a channel number, return the (0 to 255) hash for that channel * If no suitable channel could be found, return -1 * diff --git a/src/modules/NodeInfoModule.cpp b/src/modules/NodeInfoModule.cpp index c46ccaf59..e9b8d9f78 100644 --- a/src/modules/NodeInfoModule.cpp +++ b/src/modules/NodeInfoModule.cpp @@ -1,6 +1,7 @@ #include "NodeInfoModule.h" #include "Default.h" #include "MeshService.h" +#include "Channels.h" #include "NodeDB.h" #include "RTC.h" #include "Router.h" @@ -53,10 +54,11 @@ void NodeInfoModule::sendOurNodeInfo(NodeNum dest, bool wantReplies, uint8_t cha LOG_DEBUG("Send ourNodeInfo to channel %d", channel); p->channel = channel; } -#ifdef USERPREFS_CONFIG_DISCOVERY_CHANNEL +#ifdef USERPREFS_CONFIG_DISCOVERY_CHANNEL_HASH // If this is a broadcast over the default channel, we can safely change this to the discovery channel if defined if (dest == NODENUM_BROADCAST && channel == 0) { - p->channel = USERPREFS_CONFIG_DISCOVERY_CHANNEL; + int8_t discoveryChannelIdx = channels.getIndexByHash((ChannelHash)USERPREFS_CONFIG_DISCOVERY_CHANNEL_HASH); + p->channel = discoveryChannelIdx > 0 ? discoveryChannelIdx : 0; } #endif diff --git a/src/modules/PositionModule.cpp b/src/modules/PositionModule.cpp index 0a7bae335..1f637ed3f 100644 --- a/src/modules/PositionModule.cpp +++ b/src/modules/PositionModule.cpp @@ -317,7 +317,7 @@ void PositionModule::sendOurPosition() LOG_INFO("Send pos@%x:6 to mesh (wantReplies=%d)", localPosition.timestamp, requestReplies); sendOurPosition(NODENUM_BROADCAST, requestReplies); -#ifdef USERPREFS_CONFIG_DISCOVERY_CHANNEL +#ifdef USERPREFS_CONFIG_DISCOVERY_CHANNEL_HASH // If the user wants discovery on a different channel, send only nodeinfo to that channel // sendOurNodeInfo() will automatically handle routing a nodeinfo packet to the correct channel assert(nodeInfoModule);