MyMesh.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #pragma once
  2. #include <Arduino.h>
  3. #include <Mesh.h>
  4. #include "AbstractUITask.h"
  5. /*------------ Frame Protocol --------------*/
  6. #define FIRMWARE_VER_CODE 13
  7. #ifndef FIRMWARE_BUILD_DATE
  8. #define FIRMWARE_BUILD_DATE "6 Jun 2026"
  9. #endif
  10. #ifndef FIRMWARE_VERSION
  11. #define FIRMWARE_VERSION "v1.16.0"
  12. #endif
  13. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  14. #include <InternalFileSystem.h>
  15. #elif defined(RP2040_PLATFORM)
  16. #include <LittleFS.h>
  17. #elif defined(ESP32)
  18. #include <SPIFFS.h>
  19. #endif
  20. #include "DataStore.h"
  21. #include "NodePrefs.h"
  22. #include <RTClib.h>
  23. #include <helpers/ArduinoHelpers.h>
  24. #include <helpers/BaseSerialInterface.h>
  25. #include <helpers/IdentityStore.h>
  26. #include <helpers/SimpleMeshTables.h>
  27. #include <helpers/StaticPoolPacketManager.h>
  28. #include <target.h>
  29. /* ---------------------------------- CONFIGURATION ------------------------------------- */
  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 MAX_LORA_TX_POWER
  46. #define MAX_LORA_TX_POWER LORA_TX_POWER
  47. #endif
  48. #ifndef MAX_CONTACTS
  49. #define MAX_CONTACTS 100
  50. #endif
  51. #ifndef OFFLINE_QUEUE_SIZE
  52. #define OFFLINE_QUEUE_SIZE 16
  53. #endif
  54. #ifndef BLE_NAME_PREFIX
  55. #define BLE_NAME_PREFIX "MeshCore-"
  56. #endif
  57. #include <helpers/BaseChatMesh.h>
  58. #include <helpers/TransportKeyStore.h>
  59. /* -------------------------------------------------------------------------------------- */
  60. #define REQ_TYPE_GET_STATUS 0x01 // same as _GET_STATS
  61. #define REQ_TYPE_KEEP_ALIVE 0x02
  62. #define REQ_TYPE_GET_TELEMETRY_DATA 0x03
  63. struct AdvertPath {
  64. uint8_t pubkey_prefix[7];
  65. uint8_t path_len;
  66. char name[32];
  67. uint32_t recv_timestamp;
  68. uint8_t path[MAX_PATH_SIZE];
  69. };
  70. class MyMesh : public BaseChatMesh, public DataStoreHost {
  71. public:
  72. MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMeshTables &tables, DataStore& store, AbstractUITask* ui=NULL);
  73. void begin(bool has_display);
  74. void startInterface(BaseSerialInterface &serial);
  75. const char *getNodeName();
  76. NodePrefs *getNodePrefs();
  77. uint32_t getBLEPin();
  78. #if defined(ESP32)
  79. const char* getWifiSSID() const { return _prefs.wifi_ssid; }
  80. const char* getWifiPassword() const { return _prefs.wifi_pwd; }
  81. uint8_t getWifiPowerSaveMode() const { return _prefs.wifi_powersave; }
  82. #endif
  83. void loop();
  84. void handleCmdFrame(size_t len);
  85. bool advert();
  86. void enterCLIRescue();
  87. int getRecentlyHeard(AdvertPath dest[], int max_num);
  88. protected:
  89. float getAirtimeBudgetFactor() const override;
  90. int getInterferenceThreshold() const override;
  91. int calcRxDelay(float score, uint32_t air_time) const override;
  92. uint32_t getRetransmitDelay(const mesh::Packet *packet) override;
  93. uint32_t getDirectRetransmitDelay(const mesh::Packet *packet) override;
  94. uint8_t getExtraAckTransmitCount() const override;
  95. bool filterRecvFloodPacket(mesh::Packet* packet) override;
  96. bool allowPacketForward(const mesh::Packet* packet) override;
  97. void sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis);
  98. void sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis=0) override;
  99. void sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t delay_millis=0) override;
  100. void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;
  101. bool isAutoAddEnabled() const override;
  102. bool shouldAutoAddContactType(uint8_t type) const override;
  103. bool shouldOverwriteWhenFull() const override;
  104. uint8_t getAutoAddMaxHops() const override;
  105. void onContactsFull() override;
  106. void onContactOverwrite(const uint8_t* pub_key) override;
  107. bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
  108. void onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) override;
  109. void onContactPathUpdated(const ContactInfo &contact) override;
  110. ContactInfo* processAck(const uint8_t *data) override;
  111. void queueMessage(const ContactInfo &from, uint8_t txt_type, mesh::Packet *pkt, uint32_t sender_timestamp,
  112. const uint8_t *extra, int extra_len, const char *text);
  113. void onMessageRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp,
  114. const char *text) override;
  115. void onCommandDataRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp,
  116. const char *text) override;
  117. void onSignedMessageRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp,
  118. const uint8_t *sender_prefix, const char *text) override;
  119. void onChannelMessageRecv(const mesh::GroupChannel &channel, mesh::Packet *pkt, uint32_t timestamp,
  120. const char *text) override;
  121. void onChannelDataRecv(const mesh::GroupChannel &channel, mesh::Packet *pkt, uint16_t data_type,
  122. const uint8_t *data, size_t data_len) override;
  123. uint8_t onContactRequest(const ContactInfo &contact, uint32_t sender_timestamp, const uint8_t *data,
  124. uint8_t len, uint8_t *reply) override;
  125. void onContactResponse(const ContactInfo &contact, const uint8_t *data, uint8_t len) override;
  126. void onControlDataRecv(mesh::Packet *packet) override;
  127. void onRawDataRecv(mesh::Packet *packet) override;
  128. void onTraceRecv(mesh::Packet *packet, uint32_t tag, uint32_t auth_code, uint8_t flags,
  129. const uint8_t *path_snrs, const uint8_t *path_hashes, uint8_t path_len) override;
  130. uint32_t calcFloodTimeoutMillisFor(uint32_t pkt_airtime_millis) const override;
  131. uint32_t calcDirectTimeoutMillisFor(uint32_t pkt_airtime_millis, uint8_t path_len) const override;
  132. void onSendTimeout() override;
  133. // DataStoreHost methods
  134. bool onContactLoaded(const ContactInfo& contact) override { return addContact(contact); }
  135. bool getContactForSave(uint32_t idx, ContactInfo& contact) override { return getContactByIdx(idx, contact); }
  136. bool onChannelLoaded(uint8_t channel_idx, const ChannelDetails& ch) override { return setChannel(channel_idx, ch); }
  137. bool getChannelForSave(uint8_t channel_idx, ChannelDetails& ch) override { return getChannel(channel_idx, ch); }
  138. void clearPendingReqs() {
  139. pending_login = pending_status = pending_telemetry = pending_discovery = pending_req = 0;
  140. }
  141. public:
  142. void savePrefs() { _store->savePrefs(_prefs, sensors.node_lat, sensors.node_lon); }
  143. #if ENV_INCLUDE_GPS == 1
  144. void applyGpsPrefs() {
  145. sensors.setSettingValue("gps", _prefs.gps_enabled ? "1" : "0");
  146. if (_prefs.gps_interval > 0) {
  147. char interval_str[12]; // Max: 24 hours = 86400 seconds (5 digits + null)
  148. sprintf(interval_str, "%u", _prefs.gps_interval);
  149. sensors.setSettingValue("gps_interval", interval_str);
  150. }
  151. }
  152. #endif
  153. // To check if there is pending work
  154. bool hasPendingWork() const;
  155. private:
  156. void writeOKFrame();
  157. void writeErrFrame(uint8_t err_code);
  158. void writeDisabledFrame();
  159. void writeContactRespFrame(uint8_t code, const ContactInfo &contact);
  160. void updateContactFromFrame(ContactInfo &contact, uint32_t& last_mod, const uint8_t *frame, int len);
  161. void addToOfflineQueue(const uint8_t frame[], int len);
  162. int getFromOfflineQueue(uint8_t frame[]);
  163. int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) override {
  164. return _store->getBlobByKey(key, key_len, dest_buf);
  165. }
  166. bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], int len) override {
  167. return _store->putBlobByKey(key, key_len, src_buf, len);
  168. }
  169. void checkCLIRescueCmd();
  170. void checkSerialInterface();
  171. bool isValidClientRepeatFreq(uint32_t f) const;
  172. // helpers, short-cuts
  173. void saveChannels() { _store->saveChannels(this); }
  174. void saveContacts();
  175. DataStore* _store;
  176. NodePrefs _prefs;
  177. uint32_t pending_login;
  178. uint32_t pending_status;
  179. uint32_t pending_telemetry, pending_discovery; // pending _TELEMETRY_REQ
  180. uint32_t pending_req; // pending _BINARY_REQ
  181. BaseSerialInterface *_serial;
  182. AbstractUITask* _ui;
  183. ContactsIterator _iter;
  184. uint32_t _iter_filter_since;
  185. uint32_t _most_recent_lastmod;
  186. uint32_t _active_ble_pin;
  187. bool _iter_started;
  188. bool _cli_rescue;
  189. bool send_unscoped; // force un-scoped flood (instead of using send_scope)
  190. char cli_command[80];
  191. uint8_t app_target_ver;
  192. uint8_t *sign_data;
  193. uint32_t sign_data_len;
  194. unsigned long dirty_contacts_expiry;
  195. TransportKey send_scope;
  196. uint8_t cmd_frame[MAX_FRAME_SIZE + 1];
  197. uint8_t out_frame[MAX_FRAME_SIZE + 1];
  198. CayenneLPP telemetry;
  199. struct Frame {
  200. uint8_t len;
  201. uint8_t buf[MAX_FRAME_SIZE];
  202. bool isChannelMsg() const;
  203. };
  204. int offline_queue_len;
  205. Frame offline_queue[OFFLINE_QUEUE_SIZE];
  206. struct AckTableEntry {
  207. unsigned long msg_sent;
  208. uint32_t ack;
  209. ContactInfo* contact;
  210. };
  211. #define EXPECTED_ACK_TABLE_SIZE 8
  212. AckTableEntry expected_ack_table[EXPECTED_ACK_TABLE_SIZE]; // circular table
  213. int next_ack_idx;
  214. #define ADVERT_PATH_TABLE_SIZE 16
  215. AdvertPath advert_paths[ADVERT_PATH_TABLE_SIZE]; // circular table
  216. };
  217. extern MyMesh the_mesh;