diff --git a/include/common/spsp_espnow.hpp b/include/common/spsp_espnow.hpp index 9795be9..119e4c0 100644 --- a/include/common/spsp_espnow.hpp +++ b/include/common/spsp_espnow.hpp @@ -171,6 +171,8 @@ namespace SPSP::LocalLayers::ESPNOW * * `rtndBr` and `connBr` may be the same pointers. * + * Automatically resubscribes to all topics. + * * @param rtndBr Retained bridge peer info (for reconnection) * @param connBr Connected bridge peer info storage (if connection * successful and `connBr` != nullptr) diff --git a/src/common/spsp_espnow.cpp b/src/common/spsp_espnow.cpp index d0f5a7d..ed199c4 100644 --- a/src/common/spsp_espnow.cpp +++ b/src/common/spsp_espnow.cpp @@ -109,83 +109,89 @@ namespace SPSP::LocalLayers::ESPNOW bool ESPNOW::connectToBridge(BridgeConnInfoRTC* rtndBr, BridgeConnInfoRTC* connBr) { - // Mutex - const std::scoped_lock lock(m_mutex); + { // Mutex + const std::scoped_lock lock(m_mutex); - if (rtndBr != nullptr) { - // Reconnect to retained bridge - m_bestBridge = *rtndBr; - m_wifi.setChannel(m_bestBridge.ch); + if (rtndBr != nullptr) { + // Reconnect to retained bridge + m_bestBridge = *rtndBr; + m_wifi.setChannel(m_bestBridge.ch); - if (connBr != nullptr) { - *connBr = *rtndBr; - } + if (connBr != nullptr) { + *connBr = *rtndBr; + } - SPSP_LOGI("Reconnected to bridge: %s", - m_bestBridge.addr.str.c_str()); + SPSP_LOGI("Reconnected to bridge: %s", + m_bestBridge.addr.str.c_str()); - return true; - } + return true; + } - SPSP_LOGD("Connect to bridge: connecting..."); + SPSP_LOGD("Connect to bridge: connecting..."); - // Get country restrictions - auto channelRestrictions = m_wifi.getChannelRestrictions(); - uint8_t lowCh = channelRestrictions.low; - uint8_t highCh = channelRestrictions.high; + // Get country restrictions + auto channelRestrictions = m_wifi.getChannelRestrictions(); + uint8_t lowCh = channelRestrictions.low; + uint8_t highCh = channelRestrictions.high; - SPSP_LOGI("Connect to bridge: channels %u - %u", lowCh, highCh); + SPSP_LOGI("Connect to bridge: channels %u - %u", lowCh, highCh); - // Clear previous results - m_bestBridge = {}; + // Clear previous results + m_bestBridge = {}; - // Prepare message - LocalMessageT msg = {}; - msg.addr = LocalAddrT::broadcast(); - msg.type = LocalMessageType::PROBE_REQ; - msg.payload = SPSP::VERSION; + // Prepare message + LocalMessageT msg = {}; + msg.addr = LocalAddrT::broadcast(); + msg.type = LocalMessageType::PROBE_REQ; + msg.payload = SPSP::VERSION; - // Convert to raw data - std::string data; - m_serdes.serialize(msg, data); + // Convert to raw data + std::string data; + m_serdes.serialize(msg, data); - // Promise/mutex bucket - auto bucketId = this->getBucketIdFromLocalAddr(msg.addr); + // Promise/mutex bucket + auto bucketId = this->getBucketIdFromLocalAddr(msg.addr); - // Probe all channels - for (uint8_t ch = lowCh; ch <= highCh; ch++) { - auto future = m_sendingPromises[bucketId].get_future(); + // Probe all channels + for (uint8_t ch = lowCh; ch <= highCh; ch++) { + auto future = m_sendingPromises[bucketId].get_future(); - m_wifi.setChannel(ch); - this->sendRaw(msg.addr, data); + m_wifi.setChannel(ch); + this->sendRaw(msg.addr, data); - SPSP_LOGD("Connect to bridge: waiting for callback"); + SPSP_LOGD("Connect to bridge: waiting for callback"); - // Wait for callback to finish - future.get(); + // Wait for callback to finish + future.get(); - // Reset promise - m_sendingPromises[bucketId] = std::promise{}; + // Reset promise + m_sendingPromises[bucketId] = std::promise{}; - // Sleep - std::this_thread::sleep_for(m_conf.connectToBridgeChannelWaiting); - } + // Sleep + std::this_thread::sleep_for(m_conf.connectToBridgeChannelWaiting); + } - // No response - if (m_bestBridge.empty()) { - SPSP_LOGE("Connect to bridge: no response from bridge"); - return false; - } + // No response + if (m_bestBridge.empty()) { + SPSP_LOGE("Connect to bridge: no response from bridge"); + return false; + } - // New best bridge is available - switch to it's channel - m_wifi.setChannel(m_bestBridge.ch); + // New best bridge is available - switch to it's channel + m_wifi.setChannel(m_bestBridge.ch); - SPSP_LOGI("Connected to bridge: %s on channel %u (%d dBm)", - m_bestBridge.addr.str.c_str(), m_bestBridge.ch, - m_bestBridge.rssi); + SPSP_LOGI("Connected to bridge: %s on channel %u (%d dBm)", + m_bestBridge.addr.str.c_str(), m_bestBridge.ch, + m_bestBridge.rssi); - if (connBr != nullptr) { - *connBr = m_bestBridge.toRTC(); + if (connBr != nullptr) { + *connBr = m_bestBridge.toRTC(); + } + } // Mutex + + // Resubscribe to all topics + if (this->nodeConnected()) { + this->getNode()->resubscribeAll(); } return true;