diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 2e84843a..1dc5a76a 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -185,12 +185,19 @@ public: #if defined(WITH_BRIDGE) void setBridgeState(bool enable) override { - if (enable == bridge.getState()) return; - enable ? bridge.begin() : bridge.end(); + if (enable == bridge.isRunning()) return; + if (enable) + { + bridge.begin(); + } + else + { + bridge.end(); + } } void restartBridge() override { - if (!bridge.getState()) return; + if (!bridge.isRunning()) return; bridge.end(); bridge.begin(); } diff --git a/src/MeshCore.h b/src/MeshCore.h index d8886136..5c7e1760 100644 --- a/src/MeshCore.h +++ b/src/MeshCore.h @@ -28,6 +28,12 @@ #define MESH_DEBUG_PRINTLN(...) {} #endif +#if BRIDGE_DEBUG && ARDUINO +#define BRIDGE_DEBUG_PRINTLN(F, ...) Serial.printf("%s BRIDGE: " F, getLogDateTime(), ##__VA_ARGS__) +#else +#define BRIDGE_DEBUG_PRINTLN(...) {} +#endif + namespace mesh { #define BD_STARTUP_NORMAL 0 // getStartupReason() codes diff --git a/src/helpers/AbstractBridge.h b/src/helpers/AbstractBridge.h index 89e2dcdd..62284bd5 100644 --- a/src/helpers/AbstractBridge.h +++ b/src/helpers/AbstractBridge.h @@ -21,7 +21,7 @@ public: * * @return true if the bridge is initialized and running, false otherwise. */ - virtual bool getState() const = 0; + virtual bool isRunning() const = 0; /** * @brief A method to be called on every main loop iteration. diff --git a/src/helpers/bridges/BridgeBase.cpp b/src/helpers/bridges/BridgeBase.cpp index 527ec358..d2e2e5e0 100644 --- a/src/helpers/bridges/BridgeBase.cpp +++ b/src/helpers/bridges/BridgeBase.cpp @@ -2,7 +2,7 @@ #include -bool BridgeBase::getState() const { +bool BridgeBase::isRunning() const { return _initialized; } @@ -34,7 +34,7 @@ bool BridgeBase::validateChecksum(const uint8_t *data, size_t len, uint16_t rece void BridgeBase::handleReceivedPacket(mesh::Packet *packet) { // Guard against uninitialized state if (_initialized == false) { - Serial.printf("%s: BRIDGE: RX packet received before initialization\n", getLogDateTime()); + BRIDGE_DEBUG_PRINTLN("RX packet received before initialization\n"); _mgr->free(packet); return; } diff --git a/src/helpers/bridges/BridgeBase.h b/src/helpers/bridges/BridgeBase.h index 20b136e2..04c1564b 100644 --- a/src/helpers/bridges/BridgeBase.h +++ b/src/helpers/bridges/BridgeBase.h @@ -27,7 +27,7 @@ public: * * @return true if the bridge is initialized and running, false otherwise. */ - bool getState() const override; + bool isRunning() const override; /** * @brief Common magic number used by all bridge implementations for packet identification diff --git a/src/helpers/bridges/ESPNowBridge.cpp b/src/helpers/bridges/ESPNowBridge.cpp index a8a6fb53..b9eb1c10 100644 --- a/src/helpers/bridges/ESPNowBridge.cpp +++ b/src/helpers/bridges/ESPNowBridge.cpp @@ -27,20 +27,20 @@ ESPNowBridge::ESPNowBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTC } void ESPNowBridge::begin() { - Serial.printf("%s: ESPNOW BRIDGE: Initializing...\n", getLogDateTime()); + BRIDGE_DEBUG_PRINTLN("Initializing...\n"); // Initialize WiFi in station mode WiFi.mode(WIFI_STA); // Set wifi channel if (esp_wifi_set_channel(_prefs->bridge_channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) { - Serial.printf("%s: ESPNOW BRIDGE: Error setting WIFI channel to %d\n", getLogDateTime(), _prefs->bridge_channel); + BRIDGE_DEBUG_PRINTLN("Error setting WIFI channel to %d\n", _prefs->bridge_channel); return; } // Initialize ESP-NOW if (esp_now_init() != ESP_OK) { - Serial.printf("%s: ESPNOW BRIDGE: Error initializing ESP-NOW\n", getLogDateTime()); + BRIDGE_DEBUG_PRINTLN("Error initializing ESP-NOW\n"); return; } @@ -56,7 +56,7 @@ void ESPNowBridge::begin() { peerInfo.encrypt = false; if (esp_now_add_peer(&peerInfo) != ESP_OK) { - Serial.printf("%s: ESPNOW BRIDGE: Failed to add broadcast peer\n", getLogDateTime()); + BRIDGE_DEBUG_PRINTLN("Failed to add broadcast peer\n"); return; } @@ -65,12 +65,12 @@ void ESPNowBridge::begin() { } void ESPNowBridge::end() { - Serial.printf("%s: ESPNOW BRIDGE: Stopping...\n", getLogDateTime()); + BRIDGE_DEBUG_PRINTLN("Stopping...\n"); // Remove broadcast peer uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; if (esp_now_del_peer(broadcastAddress) != ESP_OK) { - Serial.printf("%s: ESPNOW BRIDGE: Error removing broadcast peer\n", getLogDateTime()); + BRIDGE_DEBUG_PRINTLN("Error removing broadcast peer\n"); } // Unregister callbacks @@ -79,7 +79,7 @@ void ESPNowBridge::end() { // Deinitialize ESP-NOW if (esp_now_deinit() != ESP_OK) { - Serial.printf("%s: ESPNOW BRIDGE: Error deinitializing ESP-NOW\n", getLogDateTime()); + BRIDGE_DEBUG_PRINTLN("Error deinitializing ESP-NOW\n"); } // Turn off WiFi @@ -103,26 +103,20 @@ void ESPNowBridge::xorCrypt(uint8_t *data, size_t len) { void ESPNowBridge::onDataRecv(const uint8_t *mac, const uint8_t *data, int32_t len) { // Ignore packets that are too small to contain header + checksum if (len < (BRIDGE_MAGIC_SIZE + BRIDGE_CHECKSUM_SIZE)) { -#if MESH_PACKET_LOGGING - Serial.printf("%s: ESPNOW BRIDGE: RX packet too small, len=%d\n", getLogDateTime(), len); -#endif + BRIDGE_DEBUG_PRINTLN("RX packet too small, len=%d\n", len); return; } // Validate total packet size if (len > MAX_ESPNOW_PACKET_SIZE) { -#if MESH_PACKET_LOGGING - Serial.printf("%s: ESPNOW BRIDGE: RX packet too large, len=%d\n", getLogDateTime(), len); -#endif + BRIDGE_DEBUG_PRINTLN("RX packet too large, len=%d\n", len); return; } // Check packet header magic uint16_t received_magic = (data[0] << 8) | data[1]; if (received_magic != BRIDGE_PACKET_MAGIC) { -#if MESH_PACKET_LOGGING - Serial.printf("%s: ESPNOW BRIDGE: RX invalid magic 0x%04X\n", getLogDateTime(), received_magic); -#endif + BRIDGE_DEBUG_PRINTLN("RX invalid magic 0x%04X\n", received_magic); return; } @@ -140,16 +134,11 @@ void ESPNowBridge::onDataRecv(const uint8_t *mac, const uint8_t *data, int32_t l if (!validateChecksum(decrypted + BRIDGE_CHECKSUM_SIZE, payloadLen, received_checksum)) { // Failed to decrypt - likely from a different network -#if MESH_PACKET_LOGGING - Serial.printf("%s: ESPNOW BRIDGE: RX checksum mismatch, rcv=0x%04X\n", getLogDateTime(), - received_checksum); -#endif + BRIDGE_DEBUG_PRINTLN("RX checksum mismatch, rcv=0x%04X\n", received_checksum); return; } -#if MESH_PACKET_LOGGING - Serial.printf("%s: ESPNOW BRIDGE: RX, payload_len=%d\n", getLogDateTime(), payloadLen); -#endif + BRIDGE_DEBUG_PRINTLN("RX, payload_len=%d\n", payloadLen); // Create mesh packet mesh::Packet *pkt = _instance->_mgr->allocNew(); @@ -174,9 +163,7 @@ void ESPNowBridge::sendPacket(mesh::Packet *packet) { // First validate the packet pointer if (!packet) { -#if MESH_PACKET_LOGGING - Serial.printf("%s: ESPNOW BRIDGE: TX invalid packet pointer\n", getLogDateTime()); -#endif + BRIDGE_DEBUG_PRINTLN("TX invalid packet pointer\n"); return; } @@ -187,10 +174,8 @@ void ESPNowBridge::sendPacket(mesh::Packet *packet) { // Check if packet fits within our maximum payload size if (meshPacketLen > MAX_PAYLOAD_SIZE) { -#if MESH_PACKET_LOGGING - Serial.printf("%s: ESPNOW BRIDGE: TX packet too large (payload=%d, max=%d)\n", getLogDateTime(), - meshPacketLen, MAX_PAYLOAD_SIZE); -#endif + BRIDGE_DEBUG_PRINTLN("TX packet too large (payload=%d, max=%d)\n", meshPacketLen, + MAX_PAYLOAD_SIZE); return; } @@ -219,13 +204,11 @@ void ESPNowBridge::sendPacket(mesh::Packet *packet) { uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; esp_err_t result = esp_now_send(broadcastAddress, buffer, totalPacketSize); -#if MESH_PACKET_LOGGING if (result == ESP_OK) { - Serial.printf("%s: ESPNOW BRIDGE: TX, len=%d\n", getLogDateTime(), meshPacketLen); + BRIDGE_DEBUG_PRINTLN("TX, len=%d\n", meshPacketLen); } else { - Serial.printf("%s: ESPNOW BRIDGE: TX FAILED!\n", getLogDateTime()); + BRIDGE_DEBUG_PRINTLN("TX FAILED!\n"); } -#endif } } diff --git a/src/helpers/bridges/RS232Bridge.cpp b/src/helpers/bridges/RS232Bridge.cpp index 70f4b7d8..554e8fcc 100644 --- a/src/helpers/bridges/RS232Bridge.cpp +++ b/src/helpers/bridges/RS232Bridge.cpp @@ -8,7 +8,7 @@ RS232Bridge::RS232Bridge(NodePrefs *prefs, Stream &serial, mesh::PacketManager * : BridgeBase(prefs, mgr, rtc), _serial(&serial) {} void RS232Bridge::begin() { - Serial.printf("%s: RS232 BRIDGE: Initializing at %d baud...\n", getLogDateTime(), _prefs->bridge_baud); + BRIDGE_DEBUG_PRINTLN("Initializing at %d baud...\n", _prefs->bridge_baud); #if !defined(WITH_RS232_BRIDGE_RX) || !defined(WITH_RS232_BRIDGE_TX) #error "WITH_RS232_BRIDGE_RX and WITH_RS232_BRIDGE_TX must be defined" #endif @@ -33,7 +33,7 @@ void RS232Bridge::begin() { } void RS232Bridge::end() { - Serial.printf("%s: RS232 BRIDGE: Stopping...\n", getLogDateTime()); + BRIDGE_DEBUG_PRINTLN("Stopping...\n"); ((HardwareSerial *)_serial)->end(); // Update bridge state @@ -71,9 +71,7 @@ void RS232Bridge::loop() { // Validate length field if (len > (MAX_TRANS_UNIT + 1)) { -#if MESH_PACKET_LOGGING - Serial.printf("%s: RS232 BRIDGE: RX invalid length %d, resetting\n", getLogDateTime(), len); -#endif + BRIDGE_DEBUG_PRINTLN("RX invalid length %d, resetting\n", len); _rx_buffer_pos = 0; // Invalid length, reset continue; } @@ -82,30 +80,20 @@ void RS232Bridge::loop() { uint16_t received_checksum = (_rx_buffer[4 + len] << 8) | _rx_buffer[5 + len]; if (validateChecksum(_rx_buffer + 4, len, received_checksum)) { -#if MESH_PACKET_LOGGING - Serial.printf("%s: RS232 BRIDGE: RX, len=%d crc=0x%04x\n", getLogDateTime(), len, - received_checksum); -#endif + BRIDGE_DEBUG_PRINTLN("RX, len=%d crc=0x%04x\n", len, received_checksum); mesh::Packet *pkt = _mgr->allocNew(); if (pkt) { if (pkt->readFrom(_rx_buffer + 4, len)) { onPacketReceived(pkt); } else { -#if MESH_PACKET_LOGGING - Serial.printf("%s: RS232 BRIDGE: RX failed to parse packet\n", getLogDateTime()); -#endif + BRIDGE_DEBUG_PRINTLN("RX failed to parse packet\n"); _mgr->free(pkt); } } else { -#if MESH_PACKET_LOGGING - Serial.printf("%s: RS232 BRIDGE: RX failed to allocate packet\n", getLogDateTime()); -#endif + BRIDGE_DEBUG_PRINTLN("RX failed to allocate packet\n"); } } else { -#if MESH_PACKET_LOGGING - Serial.printf("%s: RS232 BRIDGE: RX checksum mismatch, rcv=0x%04x\n", getLogDateTime(), - received_checksum); -#endif + BRIDGE_DEBUG_PRINTLN("RX checksum mismatch, rcv=0x%04x\n", received_checksum); } _rx_buffer_pos = 0; // Reset for next packet } @@ -122,9 +110,7 @@ void RS232Bridge::sendPacket(mesh::Packet *packet) { // First validate the packet pointer if (!packet) { -#if MESH_PACKET_LOGGING - Serial.printf("%s: RS232 BRIDGE: TX invalid packet pointer\n", getLogDateTime()); -#endif + BRIDGE_DEBUG_PRINTLN("TX invalid packet pointer\n"); return; } @@ -135,10 +121,8 @@ void RS232Bridge::sendPacket(mesh::Packet *packet) { // Check if packet fits within our maximum payload size if (len > (MAX_TRANS_UNIT + 1)) { -#if MESH_PACKET_LOGGING - Serial.printf("%s: RS232 BRIDGE: TX packet too large (payload=%d, max=%d)\n", getLogDateTime(), len, - MAX_TRANS_UNIT + 1); -#endif + BRIDGE_DEBUG_PRINTLN("TX packet too large (payload=%d, max=%d)\n", len, + MAX_TRANS_UNIT + 1); return; } @@ -156,9 +140,7 @@ void RS232Bridge::sendPacket(mesh::Packet *packet) { // Send complete packet _serial->write(buffer, len + SERIAL_OVERHEAD); -#if MESH_PACKET_LOGGING - Serial.printf("%s: RS232 BRIDGE: TX, len=%d crc=0x%04x\n", getLogDateTime(), len, checksum); -#endif + BRIDGE_DEBUG_PRINTLN("TX, len=%d crc=0x%04x\n", len, checksum); } }