MyMesh.h 5.7 KB

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