BridgeBase.h 3.9 KB

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