|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "helpers/AbstractBridge.h" |
| 4 | +#include "helpers/SimpleMeshTables.h" |
| 5 | + |
| 6 | +#include <RTClib.h> |
| 7 | + |
| 8 | +/** |
| 9 | + * @brief Base class implementing common bridge functionality |
| 10 | + * |
| 11 | + * This class provides common functionality used by different bridge implementations |
| 12 | + * like packet tracking, checksum calculation, timestamping, and duplicate detection. |
| 13 | + * |
| 14 | + * Features: |
| 15 | + * - Fletcher-16 checksum calculation for data integrity |
| 16 | + * - Packet duplicate detection using SimpleMeshTables |
| 17 | + * - Common timestamp formatting for debug logging |
| 18 | + * - Shared packet management and queuing logic |
| 19 | + */ |
| 20 | +class BridgeBase : public AbstractBridge { |
| 21 | +public: |
| 22 | + virtual ~BridgeBase() = default; |
| 23 | + |
| 24 | + /** |
| 25 | + * @brief Common magic number used by all bridge implementations for packet identification |
| 26 | + * |
| 27 | + * This magic number is placed at the beginning of bridge packets to identify |
| 28 | + * them as mesh bridge packets and provide frame synchronization. |
| 29 | + */ |
| 30 | + static constexpr uint16_t BRIDGE_PACKET_MAGIC = 0xC03E; |
| 31 | + |
| 32 | + /** |
| 33 | + * @brief Common field sizes used by bridge implementations |
| 34 | + * |
| 35 | + * These constants define the size of common packet fields used across bridges. |
| 36 | + * BRIDGE_MAGIC_SIZE is used by all bridges for packet identification. |
| 37 | + * BRIDGE_LENGTH_SIZE is used by bridges that need explicit length fields (like RS232). |
| 38 | + * BRIDGE_CHECKSUM_SIZE is used by all bridges for Fletcher-16 checksums. |
| 39 | + */ |
| 40 | + static constexpr uint16_t BRIDGE_MAGIC_SIZE = sizeof(BRIDGE_PACKET_MAGIC); |
| 41 | + static constexpr uint16_t BRIDGE_LENGTH_SIZE = sizeof(uint16_t); |
| 42 | + static constexpr uint16_t BRIDGE_CHECKSUM_SIZE = sizeof(uint16_t); |
| 43 | + |
| 44 | + /** |
| 45 | + * @brief Default delay in milliseconds for scheduling inbound packet processing |
| 46 | + * |
| 47 | + * It provides a buffer to prevent immediate processing conflicts in the mesh network. |
| 48 | + * Used in handleReceivedPacket() as: millis() + BRIDGE_DELAY |
| 49 | + */ |
| 50 | + static constexpr uint16_t BRIDGE_DELAY = 500; // TODO: maybe too high ? |
| 51 | + |
| 52 | +protected: |
| 53 | + /** Packet manager for allocating and queuing mesh packets */ |
| 54 | + mesh::PacketManager *_mgr; |
| 55 | + |
| 56 | + /** RTC clock for timestamping debug messages */ |
| 57 | + mesh::RTCClock *_rtc; |
| 58 | + |
| 59 | + /** Tracks seen packets to prevent loops in broadcast communications */ |
| 60 | + SimpleMeshTables _seen_packets; |
| 61 | + |
| 62 | + /** |
| 63 | + * @brief Constructs a BridgeBase instance |
| 64 | + * |
| 65 | + * @param mgr PacketManager for allocating and queuing packets |
| 66 | + * @param rtc RTCClock for timestamping debug messages |
| 67 | + */ |
| 68 | + BridgeBase(mesh::PacketManager *mgr, mesh::RTCClock *rtc) : _mgr(mgr), _rtc(rtc) {} |
| 69 | + |
| 70 | + /** |
| 71 | + * @brief Gets formatted date/time string for logging |
| 72 | + * |
| 73 | + * Format: "HH:MM:SS - DD/MM/YYYY U" |
| 74 | + * |
| 75 | + * @return Formatted date/time string |
| 76 | + */ |
| 77 | + const char *getLogDateTime(); |
| 78 | + |
| 79 | + /** |
| 80 | + * @brief Calculate Fletcher-16 checksum |
| 81 | + * |
| 82 | + * Based on: https://en.wikipedia.org/wiki/Fletcher%27s_checksum |
| 83 | + * Used to verify data integrity of received packets |
| 84 | + * |
| 85 | + * @param data Pointer to data to calculate checksum for |
| 86 | + * @param len Length of data in bytes |
| 87 | + * @return Calculated Fletcher-16 checksum |
| 88 | + */ |
| 89 | + static uint16_t fletcher16(const uint8_t *data, size_t len); |
| 90 | + |
| 91 | + /** |
| 92 | + * @brief Validate received checksum against calculated checksum |
| 93 | + * |
| 94 | + * @param data Pointer to data to validate |
| 95 | + * @param len Length of data in bytes |
| 96 | + * @param received_checksum Checksum received with data |
| 97 | + * @return true if checksum is valid, false otherwise |
| 98 | + */ |
| 99 | + bool validateChecksum(const uint8_t *data, size_t len, uint16_t received_checksum); |
| 100 | + |
| 101 | + /** |
| 102 | + * @brief Common packet handling for received packets |
| 103 | + * |
| 104 | + * Implements the standard pattern used by all bridges: |
| 105 | + * - Check if packet was seen before using _seen_packets.hasSeen() |
| 106 | + * - Queue packet for mesh processing if not seen before |
| 107 | + * - Free packet if already seen to prevent duplicates |
| 108 | + * |
| 109 | + * @param packet The received mesh packet |
| 110 | + */ |
| 111 | + void handleReceivedPacket(mesh::Packet *packet); |
| 112 | +}; |
0 commit comments