MyMesh.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #pragma once
  2. #include <Arduino.h> // needed for PlatformIO
  3. #include <Mesh.h>
  4. #if defined(NRF52_PLATFORM)
  5. #include <InternalFileSystem.h>
  6. #elif defined(RP2040_PLATFORM)
  7. #include <LittleFS.h>
  8. #elif defined(ESP32)
  9. #include <SPIFFS.h>
  10. #endif
  11. #include <helpers/ArduinoHelpers.h>
  12. #include <helpers/StaticPoolPacketManager.h>
  13. #include <helpers/SimpleMeshTables.h>
  14. #include <helpers/IdentityStore.h>
  15. #include <helpers/AdvertDataHelpers.h>
  16. #include <helpers/TxtDataHelpers.h>
  17. #include <helpers/CommonCLI.h>
  18. #include <helpers/StatsFormatHelper.h>
  19. #include <helpers/ClientACL.h>
  20. #include <RTClib.h>
  21. #include <target.h>
  22. /* ------------------------------ Config -------------------------------- */
  23. #ifndef FIRMWARE_BUILD_DATE
  24. #define FIRMWARE_BUILD_DATE "20 Mar 2026"
  25. #endif
  26. #ifndef FIRMWARE_VERSION
  27. #define FIRMWARE_VERSION "v1.14.1"
  28. #endif
  29. #ifndef LORA_FREQ
  30. #define LORA_FREQ 915.0
  31. #endif
  32. #ifndef LORA_BW
  33. #define LORA_BW 250
  34. #endif
  35. #ifndef LORA_SF
  36. #define LORA_SF 10
  37. #endif
  38. #ifndef LORA_CR
  39. #define LORA_CR 5
  40. #endif
  41. #ifndef LORA_TX_POWER
  42. #define LORA_TX_POWER 20
  43. #endif
  44. #ifndef ADVERT_NAME
  45. #define ADVERT_NAME "Test BBS"
  46. #endif
  47. #ifndef ADVERT_LAT
  48. #define ADVERT_LAT 0.0
  49. #endif
  50. #ifndef ADVERT_LON
  51. #define ADVERT_LON 0.0
  52. #endif
  53. #ifndef ADMIN_PASSWORD
  54. #define ADMIN_PASSWORD "password"
  55. #endif
  56. #ifndef MAX_UNSYNCED_POSTS
  57. #define MAX_UNSYNCED_POSTS 32
  58. #endif
  59. #ifndef SERVER_RESPONSE_DELAY
  60. #define SERVER_RESPONSE_DELAY 300
  61. #endif
  62. #ifndef TXT_ACK_DELAY
  63. #define TXT_ACK_DELAY 200
  64. #endif
  65. #define FIRMWARE_ROLE "room_server"
  66. #define PACKET_LOG_FILE "/packet_log"
  67. #define MAX_POST_TEXT_LEN (160-9)
  68. struct PostInfo {
  69. mesh::Identity author;
  70. uint32_t post_timestamp; // by OUR clock
  71. char text[MAX_POST_TEXT_LEN+1];
  72. };
  73. class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
  74. FILESYSTEM* _fs;
  75. uint32_t last_millis;
  76. uint64_t uptime_millis;
  77. unsigned long next_local_advert, next_flood_advert;
  78. bool _logging;
  79. NodePrefs _prefs;
  80. ClientACL acl;
  81. CommonCLI _cli;
  82. unsigned long dirty_contacts_expiry;
  83. uint8_t reply_data[MAX_PACKET_PAYLOAD];
  84. unsigned long next_push;
  85. uint16_t _num_posted, _num_post_pushes;
  86. int next_client_idx; // for round-robin polling
  87. int next_post_idx;
  88. PostInfo posts[MAX_UNSYNCED_POSTS]; // cyclic queue
  89. CayenneLPP telemetry;
  90. unsigned long set_radio_at, revert_radio_at;
  91. float pending_freq;
  92. float pending_bw;
  93. uint8_t pending_sf;
  94. uint8_t pending_cr;
  95. int matching_peer_indexes[MAX_CLIENTS];
  96. void addPost(ClientInfo* client, const char* postData);
  97. void pushPostToClient(ClientInfo* client, PostInfo& post);
  98. uint8_t getUnsyncedCount(ClientInfo* client);
  99. bool processAck(const uint8_t *data);
  100. mesh::Packet* createSelfAdvert();
  101. File openAppend(const char* fname);
  102. int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len);
  103. protected:
  104. float getAirtimeBudgetFactor() const override {
  105. return _prefs.airtime_factor;
  106. }
  107. void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;
  108. void logRx(mesh::Packet* pkt, int len, float score) override;
  109. void logTx(mesh::Packet* pkt, int len) override;
  110. void logTxFail(mesh::Packet* pkt, int len) override;
  111. int calcRxDelay(float score, uint32_t air_time) const override;
  112. const char* getLogDateTime() override;
  113. uint32_t getRetransmitDelay(const mesh::Packet* packet) override;
  114. uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override;
  115. int getInterferenceThreshold() const override {
  116. return _prefs.interference_threshold;
  117. }
  118. int getAGCResetInterval() const override {
  119. return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
  120. }
  121. uint8_t getExtraAckTransmitCount() const override {
  122. return _prefs.multi_acks;
  123. }
  124. bool allowPacketForward(const mesh::Packet* packet) override;
  125. void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override;
  126. int searchPeersByHash(const uint8_t* hash) override ;
  127. void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
  128. void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
  129. 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;
  130. void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override;
  131. #if ENV_INCLUDE_GPS == 1
  132. void applyGpsPrefs() {
  133. sensors.setSettingValue("gps", _prefs.gps_enabled?"1":"0");
  134. }
  135. #endif
  136. public:
  137. MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables);
  138. void begin(FILESYSTEM* fs);
  139. const char* getFirmwareVer() override { return FIRMWARE_VERSION; }
  140. const char* getBuildDate() override { return FIRMWARE_BUILD_DATE; }
  141. const char* getRole() override { return FIRMWARE_ROLE; }
  142. const char* getNodeName() { return _prefs.node_name; }
  143. NodePrefs* getNodePrefs() {
  144. return &_prefs;
  145. }
  146. void savePrefs() override {
  147. _cli.savePrefs(_fs);
  148. }
  149. void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override;
  150. bool formatFileSystem() override;
  151. void sendSelfAdvertisement(int delay_millis, bool flood) override;
  152. void updateAdvertTimer() override;
  153. void updateFloodAdvertTimer() override;
  154. void setLoggingOn(bool enable) override { _logging = enable; }
  155. void eraseLogFile() override {
  156. _fs->remove(PACKET_LOG_FILE);
  157. }
  158. void dumpLogFile() override;
  159. void setTxPower(int8_t power_dbm) override;
  160. void formatNeighborsReply(char *reply) override {
  161. strcpy(reply, "not supported");
  162. }
  163. void formatStatsReply(char *reply, size_t reply_size) override;
  164. void formatRadioStatsReply(char *reply, size_t reply_size) override;
  165. void formatPacketStatsReply(char *reply, size_t reply_size) override;
  166. void formatMemoryReply(char *reply, size_t reply_size) override;
  167. mesh::LocalIdentity& getSelfId() override { return self_id; }
  168. static bool saveFilter(ClientInfo* client);
  169. void saveIdentity(const mesh::LocalIdentity& new_id) override;
  170. void clearStats() override;
  171. void handleCommand(uint32_t sender_timestamp, char* command, char* reply);
  172. void loop();
  173. };