MyMesh.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #pragma once
  2. #include <Arduino.h>
  3. #include <Mesh.h>
  4. #ifdef DISPLAY_CLASS
  5. #include "UITask.h"
  6. #endif
  7. /*------------ Frame Protocol --------------*/
  8. #define FIRMWARE_VER_CODE 6
  9. #ifndef FIRMWARE_BUILD_DATE
  10. #define FIRMWARE_BUILD_DATE "2 Jul 2025"
  11. #endif
  12. #ifndef FIRMWARE_VERSION
  13. #define FIRMWARE_VERSION "v1.7.2"
  14. #endif
  15. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  16. #include <InternalFileSystem.h>
  17. #elif defined(RP2040_PLATFORM)
  18. #include <LittleFS.h>
  19. #elif defined(ESP32)
  20. #include <SPIFFS.h>
  21. #endif
  22. #include "DataStore.h"
  23. #include "NodePrefs.h"
  24. #include <RTClib.h>
  25. #include <helpers/ArduinoHelpers.h>
  26. #include <helpers/BaseSerialInterface.h>
  27. #include <helpers/IdentityStore.h>
  28. #include <helpers/SimpleMeshTables.h>
  29. #include <helpers/StaticPoolPacketManager.h>
  30. #include <target.h>
  31. /* ---------------------------------- CONFIGURATION ------------------------------------- */
  32. #ifndef LORA_FREQ
  33. #define LORA_FREQ 915.0
  34. #endif
  35. #ifndef LORA_BW
  36. #define LORA_BW 250
  37. #endif
  38. #ifndef LORA_SF
  39. #define LORA_SF 10
  40. #endif
  41. #ifndef LORA_CR
  42. #define LORA_CR 5
  43. #endif
  44. #ifndef LORA_TX_POWER
  45. #define LORA_TX_POWER 20
  46. #endif
  47. #ifndef MAX_LORA_TX_POWER
  48. #define MAX_LORA_TX_POWER LORA_TX_POWER
  49. #endif
  50. #ifndef MAX_CONTACTS
  51. #define MAX_CONTACTS 100
  52. #endif
  53. #ifndef OFFLINE_QUEUE_SIZE
  54. #define OFFLINE_QUEUE_SIZE 16
  55. #endif
  56. #ifndef BLE_NAME_PREFIX
  57. #define BLE_NAME_PREFIX "MeshCore-"
  58. #endif
  59. #include <helpers/BaseChatMesh.h>
  60. /* -------------------------------------------------------------------------------------- */
  61. #define REQ_TYPE_GET_STATUS 0x01 // same as _GET_STATS
  62. #define REQ_TYPE_KEEP_ALIVE 0x02
  63. #define REQ_TYPE_GET_TELEMETRY_DATA 0x03
  64. class MyMesh : public BaseChatMesh, public DataStoreHost {
  65. public:
  66. MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMeshTables &tables, DataStore& store);
  67. void begin(bool has_display);
  68. void startInterface(BaseSerialInterface &serial);
  69. const char *getNodeName();
  70. NodePrefs *getNodePrefs();
  71. uint32_t getBLEPin();
  72. void loop();
  73. void handleCmdFrame(size_t len);
  74. bool advert();
  75. void enterCLIRescue();
  76. protected:
  77. float getAirtimeBudgetFactor() const override;
  78. int getInterferenceThreshold() const override;
  79. int calcRxDelay(float score, uint32_t air_time) const override;
  80. void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;
  81. bool isAutoAddEnabled() const override;
  82. void onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) override;
  83. void onContactPathUpdated(const ContactInfo &contact) override;
  84. bool processAck(const uint8_t *data) override;
  85. void queueMessage(const ContactInfo &from, uint8_t txt_type, mesh::Packet *pkt, uint32_t sender_timestamp,
  86. const uint8_t *extra, int extra_len, const char *text);
  87. void onMessageRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp,
  88. const char *text) override;
  89. void onCommandDataRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp,
  90. const char *text) override;
  91. void onSignedMessageRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp,
  92. const uint8_t *sender_prefix, const char *text) override;
  93. void onChannelMessageRecv(const mesh::GroupChannel &channel, mesh::Packet *pkt, uint32_t timestamp,
  94. const char *text) override;
  95. uint8_t onContactRequest(const ContactInfo &contact, uint32_t sender_timestamp, const uint8_t *data,
  96. uint8_t len, uint8_t *reply) override;
  97. void onContactResponse(const ContactInfo &contact, const uint8_t *data, uint8_t len) override;
  98. void onRawDataRecv(mesh::Packet *packet) override;
  99. void onTraceRecv(mesh::Packet *packet, uint32_t tag, uint32_t auth_code, uint8_t flags,
  100. const uint8_t *path_snrs, const uint8_t *path_hashes, uint8_t path_len) override;
  101. uint32_t calcFloodTimeoutMillisFor(uint32_t pkt_airtime_millis) const override;
  102. uint32_t calcDirectTimeoutMillisFor(uint32_t pkt_airtime_millis, uint8_t path_len) const override;
  103. void onSendTimeout() override;
  104. // DataStoreHost methods
  105. bool onContactLoaded(const ContactInfo& contact) override { return addContact(contact); }
  106. bool getContactForSave(uint32_t idx, ContactInfo& contact) override { return getContactByIdx(idx, contact); }
  107. bool onChannelLoaded(uint8_t channel_idx, const ChannelDetails& ch) override { return setChannel(channel_idx, ch); }
  108. bool getChannelForSave(uint8_t channel_idx, ChannelDetails& ch) override { return getChannel(channel_idx, ch); }
  109. private:
  110. void writeOKFrame();
  111. void writeErrFrame(uint8_t err_code);
  112. void writeDisabledFrame();
  113. void writeContactRespFrame(uint8_t code, const ContactInfo &contact);
  114. void updateContactFromFrame(ContactInfo &contact, const uint8_t *frame, int len);
  115. void addToOfflineQueue(const uint8_t frame[], int len);
  116. int getFromOfflineQueue(uint8_t frame[]);
  117. int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) override {
  118. return _store->getBlobByKey(key, key_len, dest_buf);
  119. }
  120. bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], int len) override {
  121. return _store->putBlobByKey(key, key_len, src_buf, len);
  122. }
  123. void checkCLIRescueCmd();
  124. void checkSerialInterface();
  125. // helpers, short-cuts
  126. void savePrefs() { _store->savePrefs(_prefs, sensors.node_lat, sensors.node_lon); }
  127. void saveChannels() { _store->saveChannels(this); }
  128. void saveContacts() { _store->saveContacts(this); }
  129. private:
  130. DataStore* _store;
  131. NodePrefs _prefs;
  132. uint32_t pending_login;
  133. uint32_t pending_status;
  134. uint32_t pending_telemetry; // pending _TELEMETRY_REQ
  135. uint32_t pending_req; // pending _BINARY_REQ
  136. BaseSerialInterface *_serial;
  137. ContactsIterator _iter;
  138. uint32_t _iter_filter_since;
  139. uint32_t _most_recent_lastmod;
  140. uint32_t _active_ble_pin;
  141. bool _iter_started;
  142. bool _cli_rescue;
  143. char cli_command[80];
  144. uint8_t app_target_ver;
  145. uint8_t *sign_data;
  146. uint32_t sign_data_len;
  147. unsigned long dirty_contacts_expiry;
  148. uint8_t cmd_frame[MAX_FRAME_SIZE + 1];
  149. uint8_t out_frame[MAX_FRAME_SIZE + 1];
  150. CayenneLPP telemetry;
  151. struct Frame {
  152. uint8_t len;
  153. uint8_t buf[MAX_FRAME_SIZE];
  154. };
  155. int offline_queue_len;
  156. Frame offline_queue[OFFLINE_QUEUE_SIZE];
  157. struct AckTableEntry {
  158. unsigned long msg_sent;
  159. uint32_t ack;
  160. };
  161. #define EXPECTED_ACK_TABLE_SIZE 8
  162. AckTableEntry expected_ack_table[EXPECTED_ACK_TABLE_SIZE]; // circular table
  163. int next_ack_idx;
  164. struct AdvertPath {
  165. uint8_t pubkey_prefix[7];
  166. uint8_t path_len;
  167. uint32_t recv_timestamp;
  168. uint8_t path[MAX_PATH_SIZE];
  169. };
  170. #define ADVERT_PATH_TABLE_SIZE 16
  171. AdvertPath advert_paths[ADVERT_PATH_TABLE_SIZE]; // circular table
  172. };
  173. extern MyMesh the_mesh;
  174. #ifdef DISPLAY_CLASS
  175. extern UITask ui_task;
  176. #endif