MyMesh.h 6.7 KB

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