Skip to content

Commit fb46e5c

Browse files
committed
Refactor debug logging across bridge implementations
1 parent 9b4d93d commit fb46e5c

File tree

7 files changed

+48
-70
lines changed

7 files changed

+48
-70
lines changed

examples/simple_repeater/MyMesh.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,19 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
185185

186186
#if defined(WITH_BRIDGE)
187187
void setBridgeState(bool enable) override {
188-
if (enable == bridge.getState()) return;
189-
enable ? bridge.begin() : bridge.end();
188+
if (enable == bridge.isRunning()) return;
189+
if (enable)
190+
{
191+
bridge.begin();
192+
}
193+
else
194+
{
195+
bridge.end();
196+
}
190197
}
191198

192199
void restartBridge() override {
193-
if (!bridge.getState()) return;
200+
if (!bridge.isRunning()) return;
194201
bridge.end();
195202
bridge.begin();
196203
}

src/MeshCore.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
#define MESH_DEBUG_PRINTLN(...) {}
2929
#endif
3030

31+
#if BRIDGE_DEBUG && ARDUINO
32+
#define BRIDGE_DEBUG_PRINTLN(F, ...) Serial.printf("%s BRIDGE: " F, getLogDateTime(), ##__VA_ARGS__)
33+
#else
34+
#define BRIDGE_DEBUG_PRINTLN(...) {}
35+
#endif
36+
3137
namespace mesh {
3238

3339
#define BD_STARTUP_NORMAL 0 // getStartupReason() codes

src/helpers/AbstractBridge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class AbstractBridge {
2121
*
2222
* @return true if the bridge is initialized and running, false otherwise.
2323
*/
24-
virtual bool getState() const = 0;
24+
virtual bool isRunning() const = 0;
2525

2626
/**
2727
* @brief A method to be called on every main loop iteration.

src/helpers/bridges/BridgeBase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <Arduino.h>
44

5-
bool BridgeBase::getState() const {
5+
bool BridgeBase::isRunning() const {
66
return _initialized;
77
}
88

@@ -34,7 +34,7 @@ bool BridgeBase::validateChecksum(const uint8_t *data, size_t len, uint16_t rece
3434
void BridgeBase::handleReceivedPacket(mesh::Packet *packet) {
3535
// Guard against uninitialized state
3636
if (_initialized == false) {
37-
Serial.printf("%s: BRIDGE: RX packet received before initialization\n", getLogDateTime());
37+
BRIDGE_DEBUG_PRINTLN("RX packet received before initialization\n");
3838
_mgr->free(packet);
3939
return;
4040
}

src/helpers/bridges/BridgeBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class BridgeBase : public AbstractBridge {
2727
*
2828
* @return true if the bridge is initialized and running, false otherwise.
2929
*/
30-
bool getState() const override;
30+
bool isRunning() const override;
3131

3232
/**
3333
* @brief Common magic number used by all bridge implementations for packet identification

src/helpers/bridges/ESPNowBridge.cpp

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ ESPNowBridge::ESPNowBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTC
2727
}
2828

2929
void ESPNowBridge::begin() {
30-
Serial.printf("%s: ESPNOW BRIDGE: Initializing...\n", getLogDateTime());
30+
BRIDGE_DEBUG_PRINTLN("Initializing...\n");
3131

3232
// Initialize WiFi in station mode
3333
WiFi.mode(WIFI_STA);
3434

3535
// Set wifi channel
3636
if (esp_wifi_set_channel(_prefs->bridge_channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) {
37-
Serial.printf("%s: ESPNOW BRIDGE: Error setting WIFI channel to %d\n", getLogDateTime(), _prefs->bridge_channel);
37+
BRIDGE_DEBUG_PRINTLN("Error setting WIFI channel to %d\n", _prefs->bridge_channel);
3838
return;
3939
}
4040

4141
// Initialize ESP-NOW
4242
if (esp_now_init() != ESP_OK) {
43-
Serial.printf("%s: ESPNOW BRIDGE: Error initializing ESP-NOW\n", getLogDateTime());
43+
BRIDGE_DEBUG_PRINTLN("Error initializing ESP-NOW\n");
4444
return;
4545
}
4646

@@ -56,7 +56,7 @@ void ESPNowBridge::begin() {
5656
peerInfo.encrypt = false;
5757

5858
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
59-
Serial.printf("%s: ESPNOW BRIDGE: Failed to add broadcast peer\n", getLogDateTime());
59+
BRIDGE_DEBUG_PRINTLN("Failed to add broadcast peer\n");
6060
return;
6161
}
6262

@@ -65,12 +65,12 @@ void ESPNowBridge::begin() {
6565
}
6666

6767
void ESPNowBridge::end() {
68-
Serial.printf("%s: ESPNOW BRIDGE: Stopping...\n", getLogDateTime());
68+
BRIDGE_DEBUG_PRINTLN("Stopping...\n");
6969

7070
// Remove broadcast peer
7171
uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
7272
if (esp_now_del_peer(broadcastAddress) != ESP_OK) {
73-
Serial.printf("%s: ESPNOW BRIDGE: Error removing broadcast peer\n", getLogDateTime());
73+
BRIDGE_DEBUG_PRINTLN("Error removing broadcast peer\n");
7474
}
7575

7676
// Unregister callbacks
@@ -79,7 +79,7 @@ void ESPNowBridge::end() {
7979

8080
// Deinitialize ESP-NOW
8181
if (esp_now_deinit() != ESP_OK) {
82-
Serial.printf("%s: ESPNOW BRIDGE: Error deinitializing ESP-NOW\n", getLogDateTime());
82+
BRIDGE_DEBUG_PRINTLN("Error deinitializing ESP-NOW\n");
8383
}
8484

8585
// Turn off WiFi
@@ -103,26 +103,20 @@ void ESPNowBridge::xorCrypt(uint8_t *data, size_t len) {
103103
void ESPNowBridge::onDataRecv(const uint8_t *mac, const uint8_t *data, int32_t len) {
104104
// Ignore packets that are too small to contain header + checksum
105105
if (len < (BRIDGE_MAGIC_SIZE + BRIDGE_CHECKSUM_SIZE)) {
106-
#if MESH_PACKET_LOGGING
107-
Serial.printf("%s: ESPNOW BRIDGE: RX packet too small, len=%d\n", getLogDateTime(), len);
108-
#endif
106+
BRIDGE_DEBUG_PRINTLN("RX packet too small, len=%d\n", len);
109107
return;
110108
}
111109

112110
// Validate total packet size
113111
if (len > MAX_ESPNOW_PACKET_SIZE) {
114-
#if MESH_PACKET_LOGGING
115-
Serial.printf("%s: ESPNOW BRIDGE: RX packet too large, len=%d\n", getLogDateTime(), len);
116-
#endif
112+
BRIDGE_DEBUG_PRINTLN("RX packet too large, len=%d\n", len);
117113
return;
118114
}
119115

120116
// Check packet header magic
121117
uint16_t received_magic = (data[0] << 8) | data[1];
122118
if (received_magic != BRIDGE_PACKET_MAGIC) {
123-
#if MESH_PACKET_LOGGING
124-
Serial.printf("%s: ESPNOW BRIDGE: RX invalid magic 0x%04X\n", getLogDateTime(), received_magic);
125-
#endif
119+
BRIDGE_DEBUG_PRINTLN("RX invalid magic 0x%04X\n", received_magic);
126120
return;
127121
}
128122

@@ -140,16 +134,11 @@ void ESPNowBridge::onDataRecv(const uint8_t *mac, const uint8_t *data, int32_t l
140134

141135
if (!validateChecksum(decrypted + BRIDGE_CHECKSUM_SIZE, payloadLen, received_checksum)) {
142136
// Failed to decrypt - likely from a different network
143-
#if MESH_PACKET_LOGGING
144-
Serial.printf("%s: ESPNOW BRIDGE: RX checksum mismatch, rcv=0x%04X\n", getLogDateTime(),
145-
received_checksum);
146-
#endif
137+
BRIDGE_DEBUG_PRINTLN("RX checksum mismatch, rcv=0x%04X\n", received_checksum);
147138
return;
148139
}
149140

150-
#if MESH_PACKET_LOGGING
151-
Serial.printf("%s: ESPNOW BRIDGE: RX, payload_len=%d\n", getLogDateTime(), payloadLen);
152-
#endif
141+
BRIDGE_DEBUG_PRINTLN("RX, payload_len=%d\n", payloadLen);
153142

154143
// Create mesh packet
155144
mesh::Packet *pkt = _instance->_mgr->allocNew();
@@ -174,9 +163,7 @@ void ESPNowBridge::sendPacket(mesh::Packet *packet) {
174163

175164
// First validate the packet pointer
176165
if (!packet) {
177-
#if MESH_PACKET_LOGGING
178-
Serial.printf("%s: ESPNOW BRIDGE: TX invalid packet pointer\n", getLogDateTime());
179-
#endif
166+
BRIDGE_DEBUG_PRINTLN("TX invalid packet pointer\n");
180167
return;
181168
}
182169

@@ -187,10 +174,8 @@ void ESPNowBridge::sendPacket(mesh::Packet *packet) {
187174

188175
// Check if packet fits within our maximum payload size
189176
if (meshPacketLen > MAX_PAYLOAD_SIZE) {
190-
#if MESH_PACKET_LOGGING
191-
Serial.printf("%s: ESPNOW BRIDGE: TX packet too large (payload=%d, max=%d)\n", getLogDateTime(),
192-
meshPacketLen, MAX_PAYLOAD_SIZE);
193-
#endif
177+
BRIDGE_DEBUG_PRINTLN("TX packet too large (payload=%d, max=%d)\n", meshPacketLen,
178+
MAX_PAYLOAD_SIZE);
194179
return;
195180
}
196181

@@ -219,13 +204,11 @@ void ESPNowBridge::sendPacket(mesh::Packet *packet) {
219204
uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
220205
esp_err_t result = esp_now_send(broadcastAddress, buffer, totalPacketSize);
221206

222-
#if MESH_PACKET_LOGGING
223207
if (result == ESP_OK) {
224-
Serial.printf("%s: ESPNOW BRIDGE: TX, len=%d\n", getLogDateTime(), meshPacketLen);
208+
BRIDGE_DEBUG_PRINTLN("TX, len=%d\n", meshPacketLen);
225209
} else {
226-
Serial.printf("%s: ESPNOW BRIDGE: TX FAILED!\n", getLogDateTime());
210+
BRIDGE_DEBUG_PRINTLN("TX FAILED!\n");
227211
}
228-
#endif
229212
}
230213
}
231214

src/helpers/bridges/RS232Bridge.cpp

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ RS232Bridge::RS232Bridge(NodePrefs *prefs, Stream &serial, mesh::PacketManager *
88
: BridgeBase(prefs, mgr, rtc), _serial(&serial) {}
99

1010
void RS232Bridge::begin() {
11-
Serial.printf("%s: RS232 BRIDGE: Initializing at %d baud...\n", getLogDateTime(), _prefs->bridge_baud);
11+
BRIDGE_DEBUG_PRINTLN("Initializing at %d baud...\n", _prefs->bridge_baud);
1212
#if !defined(WITH_RS232_BRIDGE_RX) || !defined(WITH_RS232_BRIDGE_TX)
1313
#error "WITH_RS232_BRIDGE_RX and WITH_RS232_BRIDGE_TX must be defined"
1414
#endif
@@ -33,7 +33,7 @@ void RS232Bridge::begin() {
3333
}
3434

3535
void RS232Bridge::end() {
36-
Serial.printf("%s: RS232 BRIDGE: Stopping...\n", getLogDateTime());
36+
BRIDGE_DEBUG_PRINTLN("Stopping...\n");
3737
((HardwareSerial *)_serial)->end();
3838

3939
// Update bridge state
@@ -71,9 +71,7 @@ void RS232Bridge::loop() {
7171

7272
// Validate length field
7373
if (len > (MAX_TRANS_UNIT + 1)) {
74-
#if MESH_PACKET_LOGGING
75-
Serial.printf("%s: RS232 BRIDGE: RX invalid length %d, resetting\n", getLogDateTime(), len);
76-
#endif
74+
BRIDGE_DEBUG_PRINTLN("RX invalid length %d, resetting\n", len);
7775
_rx_buffer_pos = 0; // Invalid length, reset
7876
continue;
7977
}
@@ -82,30 +80,20 @@ void RS232Bridge::loop() {
8280
uint16_t received_checksum = (_rx_buffer[4 + len] << 8) | _rx_buffer[5 + len];
8381

8482
if (validateChecksum(_rx_buffer + 4, len, received_checksum)) {
85-
#if MESH_PACKET_LOGGING
86-
Serial.printf("%s: RS232 BRIDGE: RX, len=%d crc=0x%04x\n", getLogDateTime(), len,
87-
received_checksum);
88-
#endif
83+
BRIDGE_DEBUG_PRINTLN("RX, len=%d crc=0x%04x\n", len, received_checksum);
8984
mesh::Packet *pkt = _mgr->allocNew();
9085
if (pkt) {
9186
if (pkt->readFrom(_rx_buffer + 4, len)) {
9287
onPacketReceived(pkt);
9388
} else {
94-
#if MESH_PACKET_LOGGING
95-
Serial.printf("%s: RS232 BRIDGE: RX failed to parse packet\n", getLogDateTime());
96-
#endif
89+
BRIDGE_DEBUG_PRINTLN("RX failed to parse packet\n");
9790
_mgr->free(pkt);
9891
}
9992
} else {
100-
#if MESH_PACKET_LOGGING
101-
Serial.printf("%s: RS232 BRIDGE: RX failed to allocate packet\n", getLogDateTime());
102-
#endif
93+
BRIDGE_DEBUG_PRINTLN("RX failed to allocate packet\n");
10394
}
10495
} else {
105-
#if MESH_PACKET_LOGGING
106-
Serial.printf("%s: RS232 BRIDGE: RX checksum mismatch, rcv=0x%04x\n", getLogDateTime(),
107-
received_checksum);
108-
#endif
96+
BRIDGE_DEBUG_PRINTLN("RX checksum mismatch, rcv=0x%04x\n", received_checksum);
10997
}
11098
_rx_buffer_pos = 0; // Reset for next packet
11199
}
@@ -122,9 +110,7 @@ void RS232Bridge::sendPacket(mesh::Packet *packet) {
122110

123111
// First validate the packet pointer
124112
if (!packet) {
125-
#if MESH_PACKET_LOGGING
126-
Serial.printf("%s: RS232 BRIDGE: TX invalid packet pointer\n", getLogDateTime());
127-
#endif
113+
BRIDGE_DEBUG_PRINTLN("TX invalid packet pointer\n");
128114
return;
129115
}
130116

@@ -135,10 +121,8 @@ void RS232Bridge::sendPacket(mesh::Packet *packet) {
135121

136122
// Check if packet fits within our maximum payload size
137123
if (len > (MAX_TRANS_UNIT + 1)) {
138-
#if MESH_PACKET_LOGGING
139-
Serial.printf("%s: RS232 BRIDGE: TX packet too large (payload=%d, max=%d)\n", getLogDateTime(), len,
140-
MAX_TRANS_UNIT + 1);
141-
#endif
124+
BRIDGE_DEBUG_PRINTLN("TX packet too large (payload=%d, max=%d)\n", len,
125+
MAX_TRANS_UNIT + 1);
142126
return;
143127
}
144128

@@ -156,9 +140,7 @@ void RS232Bridge::sendPacket(mesh::Packet *packet) {
156140
// Send complete packet
157141
_serial->write(buffer, len + SERIAL_OVERHEAD);
158142

159-
#if MESH_PACKET_LOGGING
160-
Serial.printf("%s: RS232 BRIDGE: TX, len=%d crc=0x%04x\n", getLogDateTime(), len, checksum);
161-
#endif
143+
BRIDGE_DEBUG_PRINTLN("TX, len=%d crc=0x%04x\n", len, checksum);
162144
}
163145
}
164146

0 commit comments

Comments
 (0)