11#include " ESPNowBridge.h"
22
3- #include < RTClib.h>
43#include < WiFi.h>
54#include < esp_wifi.h>
65
@@ -22,21 +21,8 @@ void ESPNowBridge::send_cb(const uint8_t *mac, esp_now_send_status_t status) {
2221 }
2322}
2423
25- // Fletcher16 checksum calculation
26- static uint16_t fletcher16 (const uint8_t *data, size_t len) {
27- uint16_t sum1 = 0 ;
28- uint16_t sum2 = 0 ;
29-
30- while (len--) {
31- sum1 = (sum1 + *data++) % 255 ;
32- sum2 = (sum2 + sum1) % 255 ;
33- }
34-
35- return (sum2 << 8 ) | sum1;
36- }
37-
3824ESPNowBridge::ESPNowBridge (mesh::PacketManager *mgr, mesh::RTCClock *rtc)
39- : _mgr (mgr), _rtc( rtc), _rx_buffer_pos(0 ) {
25+ : BridgeBase (mgr, rtc), _rx_buffer_pos(0 ) {
4026 _instance = this ;
4127}
4228
@@ -115,13 +101,12 @@ void ESPNowBridge::onDataRecv(const uint8_t *mac, const uint8_t *data, int32_t l
115101 // Validate checksum
116102 uint16_t received_checksum = (decrypted[0 ] << 8 ) | decrypted[1 ];
117103 const size_t payloadLen = encryptedDataLen - CHECKSUM_SIZE;
118- uint16_t calculated_checksum = fletcher16 (decrypted + CHECKSUM_SIZE, payloadLen);
119104
120- if (received_checksum != calculated_checksum ) {
105+ if (! validateChecksum (decrypted + CHECKSUM_SIZE, payloadLen, received_checksum) ) {
121106 // Failed to decrypt - likely from a different network
122107#if MESH_PACKET_LOGGING
123- Serial.printf (" %s: ESPNOW BRIDGE: RX checksum mismatch, rcv=0x%04X calc=0x%04X \n " , getLogDateTime (),
124- received_checksum, calculated_checksum );
108+ Serial.printf (" %s: ESPNOW BRIDGE: RX checksum mismatch, rcv=0x%04X\n " , getLogDateTime (),
109+ received_checksum);
125110#endif
126111 return ;
127112 }
@@ -146,23 +131,19 @@ void ESPNowBridge::onDataSent(const uint8_t *mac_addr, esp_now_send_status_t sta
146131}
147132
148133void ESPNowBridge::onPacketReceived (mesh::Packet *packet) {
149- if (!_seen_packets.hasSeen (packet)) {
150- _mgr->queueInbound (packet, 0 );
151- } else {
152- _mgr->free (packet);
153- }
134+ handleReceivedPacket (packet);
154135}
155136
156137void ESPNowBridge::onPacketTransmitted (mesh::Packet *packet) {
157- if (!_seen_packets.hasSeen (packet)) {
158-
159- // First validate the packet pointer
160- if (!packet) {
138+ // First validate the packet pointer
139+ if (!packet) {
161140#if MESH_PACKET_LOGGING
162- Serial.printf (" %s: ESPNOW BRIDGE: TX invalid packet pointer\n " , getLogDateTime ());
141+ Serial.printf (" %s: ESPNOW BRIDGE: TX invalid packet pointer\n " , getLogDateTime ());
163142#endif
164- return ;
165- }
143+ return ;
144+ }
145+
146+ if (!_seen_packets.hasSeen (packet)) {
166147
167148 // Create a temporary buffer just for size calculation and reuse for actual writing
168149 uint8_t sizingBuffer[MAX_PAYLOAD_SIZE];
@@ -212,13 +193,4 @@ void ESPNowBridge::onPacketTransmitted(mesh::Packet *packet) {
212193 }
213194}
214195
215- const char *ESPNowBridge::getLogDateTime () {
216- static char tmp[32 ];
217- uint32_t now = _rtc->getCurrentTime ();
218- DateTime dt = DateTime (now);
219- sprintf (tmp, " %02d:%02d:%02d - %d/%d/%d U" , dt.hour (), dt.minute (), dt.second (), dt.day (), dt.month (),
220- dt.year ());
221- return tmp;
222- }
223-
224196#endif
0 commit comments