MyMesh.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #pragma once
  2. #include <Arduino.h>
  3. #include <Mesh.h>
  4. #include <helpers/CommonCLI.h>
  5. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  6. #include <InternalFileSystem.h>
  7. #elif defined(RP2040_PLATFORM)
  8. #include <LittleFS.h>
  9. #elif defined(ESP32)
  10. #include <SPIFFS.h>
  11. #endif
  12. #include <helpers/ArduinoHelpers.h>
  13. #include <helpers/StaticPoolPacketManager.h>
  14. #include <helpers/SimpleMeshTables.h>
  15. #include <helpers/IdentityStore.h>
  16. #include <helpers/AdvertDataHelpers.h>
  17. #include <helpers/TxtDataHelpers.h>
  18. #include <helpers/ClientACL.h>
  19. #include <RTClib.h>
  20. #include <target.h>
  21. #ifdef WITH_RS232_BRIDGE
  22. #include "helpers/bridges/RS232Bridge.h"
  23. #define WITH_BRIDGE
  24. #endif
  25. #ifdef WITH_ESPNOW_BRIDGE
  26. #include "helpers/bridges/ESPNowBridge.h"
  27. #define WITH_BRIDGE
  28. #endif
  29. #ifdef WITH_BRIDGE
  30. extern AbstractBridge* bridge;
  31. #endif
  32. struct RepeaterStats {
  33. uint16_t batt_milli_volts;
  34. uint16_t curr_tx_queue_len;
  35. int16_t noise_floor;
  36. int16_t last_rssi;
  37. uint32_t n_packets_recv;
  38. uint32_t n_packets_sent;
  39. uint32_t total_air_time_secs;
  40. uint32_t total_up_time_secs;
  41. uint32_t n_sent_flood, n_sent_direct;
  42. uint32_t n_recv_flood, n_recv_direct;
  43. uint16_t err_events; // was 'n_full_events'
  44. int16_t last_snr; // x 4
  45. uint16_t n_direct_dups, n_flood_dups;
  46. uint32_t total_rx_air_time_secs;
  47. };
  48. #ifndef MAX_CLIENTS
  49. #define MAX_CLIENTS 32
  50. #endif
  51. struct NeighbourInfo {
  52. mesh::Identity id;
  53. uint32_t advert_timestamp;
  54. uint32_t heard_timestamp;
  55. int8_t snr; // multiplied by 4, user should divide to get float value
  56. };
  57. #ifndef FIRMWARE_BUILD_DATE
  58. #define FIRMWARE_BUILD_DATE "2 Oct 2025"
  59. #endif
  60. #ifndef FIRMWARE_VERSION
  61. #define FIRMWARE_VERSION "v1.9.1"
  62. #endif
  63. #define FIRMWARE_ROLE "repeater"
  64. #define PACKET_LOG_FILE "/packet_log"
  65. class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
  66. FILESYSTEM* _fs;
  67. unsigned long next_local_advert, next_flood_advert;
  68. bool _logging;
  69. NodePrefs _prefs;
  70. CommonCLI _cli;
  71. uint8_t reply_data[MAX_PACKET_PAYLOAD];
  72. ClientACL acl;
  73. unsigned long dirty_contacts_expiry;
  74. #if MAX_NEIGHBOURS
  75. NeighbourInfo neighbours[MAX_NEIGHBOURS];
  76. #endif
  77. CayenneLPP telemetry;
  78. unsigned long set_radio_at, revert_radio_at;
  79. float pending_freq;
  80. float pending_bw;
  81. uint8_t pending_sf;
  82. uint8_t pending_cr;
  83. int matching_peer_indexes[MAX_CLIENTS];
  84. #if defined(WITH_RS232_BRIDGE)
  85. RS232Bridge bridge;
  86. #elif defined(WITH_ESPNOW_BRIDGE)
  87. ESPNowBridge bridge;
  88. #endif
  89. void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
  90. uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data);
  91. int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len);
  92. mesh::Packet* createSelfAdvert();
  93. File openAppend(const char* fname);
  94. protected:
  95. float getAirtimeBudgetFactor() const override {
  96. return _prefs.airtime_factor;
  97. }
  98. bool allowPacketForward(const mesh::Packet* packet) override;
  99. const char* getLogDateTime() override;
  100. void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;
  101. void logRx(mesh::Packet* pkt, int len, float score) override;
  102. void logTx(mesh::Packet* pkt, int len) override;
  103. void logTxFail(mesh::Packet* pkt, int len) override;
  104. int calcRxDelay(float score, uint32_t air_time) const override;
  105. uint32_t getRetransmitDelay(const mesh::Packet* packet) override;
  106. uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override;
  107. int getInterferenceThreshold() const override {
  108. return _prefs.interference_threshold;
  109. }
  110. int getAGCResetInterval() const override {
  111. return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
  112. }
  113. uint8_t getExtraAckTransmitCount() const override {
  114. return _prefs.multi_acks;
  115. }
  116. void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override;
  117. int searchPeersByHash(const uint8_t* hash) override;
  118. void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
  119. void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len);
  120. void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
  121. bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
  122. public:
  123. MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables);
  124. void begin(FILESYSTEM* fs);
  125. const char* getFirmwareVer() override { return FIRMWARE_VERSION; }
  126. const char* getBuildDate() override { return FIRMWARE_BUILD_DATE; }
  127. const char* getRole() override { return FIRMWARE_ROLE; }
  128. const char* getNodeName() { return _prefs.node_name; }
  129. NodePrefs* getNodePrefs() {
  130. return &_prefs;
  131. }
  132. void savePrefs() override {
  133. _cli.savePrefs(_fs);
  134. }
  135. void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override;
  136. bool formatFileSystem() override;
  137. void sendSelfAdvertisement(int delay_millis) override;
  138. void updateAdvertTimer() override;
  139. void updateFloodAdvertTimer() override;
  140. void setLoggingOn(bool enable) override { _logging = enable; }
  141. void eraseLogFile() override {
  142. _fs->remove(PACKET_LOG_FILE);
  143. }
  144. void dumpLogFile() override;
  145. void setTxPower(uint8_t power_dbm) override;
  146. void formatNeighborsReply(char *reply) override;
  147. void removeNeighbor(const uint8_t* pubkey, int key_len) override;
  148. mesh::LocalIdentity& getSelfId() override { return self_id; }
  149. void saveIdentity(const mesh::LocalIdentity& new_id) override;
  150. void clearStats() override;
  151. void handleCommand(uint32_t sender_timestamp, char* command, char* reply);
  152. void loop();
  153. };