SensorMesh.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #pragma once
  2. #include <Arduino.h> // needed for PlatformIO
  3. #include <Mesh.h>
  4. #include "TimeSeriesData.h"
  5. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  6. #include <InternalFileSystem.h>
  7. #elif defined(RP2040_PLATFORM)
  8. #include <LittleFS.h>
  9. #elif defined(ESP32)
  10. #include <SPIFFS.h>
  11. #endif
  12. #include <helpers/ArduinoHelpers.h>
  13. #include <helpers/StaticPoolPacketManager.h>
  14. #include <helpers/SimpleMeshTables.h>
  15. #include <helpers/IdentityStore.h>
  16. #include <helpers/AdvertDataHelpers.h>
  17. #include <helpers/TxtDataHelpers.h>
  18. #include <helpers/CommonCLI.h>
  19. #include <RTClib.h>
  20. #include <target.h>
  21. #define PERM_ACL_ROLE_MASK 3 // lower 2 bits
  22. #define PERM_ACL_GUEST 0
  23. #define PERM_ACL_READ_ONLY 1
  24. #define PERM_ACL_READ_WRITE 2
  25. #define PERM_ACL_ADMIN 3
  26. #define PERM_RESERVED1 (1 << 2)
  27. #define PERM_RESERVED2 (1 << 3)
  28. #define PERM_RESERVED3 (1 << 4)
  29. #define PERM_RESERVED4 (1 << 5)
  30. #define PERM_RECV_ALERTS_LO (1 << 6) // low priority alerts
  31. #define PERM_RECV_ALERTS_HI (1 << 7) // high priority alerts
  32. struct ContactInfo {
  33. mesh::Identity id;
  34. uint8_t permissions;
  35. int8_t out_path_len;
  36. uint8_t out_path[MAX_PATH_SIZE];
  37. uint8_t shared_secret[PUB_KEY_SIZE];
  38. uint32_t last_timestamp; // by THEIR clock (transient)
  39. uint32_t last_activity; // by OUR clock (transient)
  40. bool isAdmin() const { return (permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_ADMIN; }
  41. };
  42. #ifndef FIRMWARE_BUILD_DATE
  43. #define FIRMWARE_BUILD_DATE "2 Jul 2025"
  44. #endif
  45. #ifndef FIRMWARE_VERSION
  46. #define FIRMWARE_VERSION "v1.7.2"
  47. #endif
  48. #define FIRMWARE_ROLE "sensor"
  49. #define MAX_CONTACTS 20
  50. #define MAX_SEARCH_RESULTS 8
  51. #define MAX_CONCURRENT_ALERTS 4
  52. class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
  53. public:
  54. SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables);
  55. void begin(FILESYSTEM* fs);
  56. void loop();
  57. void handleCommand(uint32_t sender_timestamp, char* command, char* reply);
  58. // CommonCLI callbacks
  59. const char* getFirmwareVer() override { return FIRMWARE_VERSION; }
  60. const char* getBuildDate() override { return FIRMWARE_BUILD_DATE; }
  61. const char* getRole() override { return FIRMWARE_ROLE; }
  62. const char* getNodeName() { return _prefs.node_name; }
  63. NodePrefs* getNodePrefs() { return &_prefs; }
  64. void savePrefs() override { _cli.savePrefs(_fs); }
  65. bool formatFileSystem() override;
  66. void sendSelfAdvertisement(int delay_millis) override;
  67. void updateAdvertTimer() override;
  68. void updateFloodAdvertTimer() override;
  69. void setLoggingOn(bool enable) override { }
  70. void eraseLogFile() override { }
  71. void dumpLogFile() override { }
  72. void setTxPower(uint8_t power_dbm) override;
  73. void formatNeighborsReply(char *reply) override {
  74. strcpy(reply, "not supported");
  75. }
  76. const uint8_t* getSelfIdPubKey() override { return self_id.pub_key; }
  77. void clearStats() override { }
  78. void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override;
  79. float getTelemValue(uint8_t channel, uint8_t type);
  80. protected:
  81. // current telemetry data queries
  82. float getVoltage(uint8_t channel) { return getTelemValue(channel, LPP_VOLTAGE); }
  83. float getCurrent(uint8_t channel) { return getTelemValue(channel, LPP_CURRENT); }
  84. float getPower(uint8_t channel) { return getTelemValue(channel, LPP_POWER); }
  85. float getTemperature(uint8_t channel) { return getTelemValue(channel, LPP_TEMPERATURE); }
  86. float getRelativeHumidity(uint8_t channel) { return getTelemValue(channel, LPP_RELATIVE_HUMIDITY); }
  87. float getBarometricPressure(uint8_t channel) { return getTelemValue(channel, LPP_BAROMETRIC_PRESSURE); }
  88. float getAltitude(uint8_t channel) { return getTelemValue(channel, LPP_ALTITUDE); }
  89. bool getGPS(uint8_t channel, float& lat, float& lon, float& alt);
  90. // alerts
  91. enum AlertPriority { LOW_PRI_ALERT, HIGH_PRI_ALERT };
  92. struct Trigger {
  93. uint32_t timestamp;
  94. AlertPriority pri;
  95. uint32_t expected_acks[4];
  96. int8_t curr_contact_idx;
  97. uint8_t attempt;
  98. unsigned long send_expiry;
  99. char text[MAX_PACKET_PAYLOAD];
  100. Trigger() { text[0] = 0; }
  101. bool isTriggered() const { return text[0] != 0; }
  102. };
  103. void alertIf(bool condition, Trigger& t, AlertPriority pri, const char* text);
  104. virtual void onSensorDataRead() = 0; // for app to implement
  105. virtual int querySeriesData(uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg dest[], int max_num) = 0; // for app to implement
  106. virtual bool handleCustomCommand(uint32_t sender_timestamp, char* command, char* reply) { return false; }
  107. // Mesh overrides
  108. float getAirtimeBudgetFactor() const override;
  109. bool allowPacketForward(const mesh::Packet* packet) override;
  110. int calcRxDelay(float score, uint32_t air_time) const override;
  111. uint32_t getRetransmitDelay(const mesh::Packet* packet) override;
  112. uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override;
  113. int getInterferenceThreshold() const override;
  114. int getAGCResetInterval() const override;
  115. void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override;
  116. int searchPeersByHash(const uint8_t* hash) override;
  117. void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
  118. void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
  119. 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;
  120. void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override;
  121. private:
  122. FILESYSTEM* _fs;
  123. unsigned long next_local_advert, next_flood_advert;
  124. NodePrefs _prefs;
  125. CommonCLI _cli;
  126. uint8_t reply_data[MAX_PACKET_PAYLOAD];
  127. ContactInfo contacts[MAX_CONTACTS];
  128. int num_contacts;
  129. unsigned long dirty_contacts_expiry;
  130. CayenneLPP telemetry;
  131. uint32_t last_read_time;
  132. int matching_peer_indexes[MAX_SEARCH_RESULTS];
  133. int num_alert_tasks;
  134. Trigger* alert_tasks[MAX_CONCURRENT_ALERTS];
  135. unsigned long set_radio_at, revert_radio_at;
  136. float pending_freq;
  137. float pending_bw;
  138. uint8_t pending_sf;
  139. uint8_t pending_cr;
  140. void loadContacts();
  141. void saveContacts();
  142. uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data);
  143. uint8_t handleRequest(uint8_t perms, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len);
  144. mesh::Packet* createSelfAdvert();
  145. ContactInfo* getContact(const uint8_t* pubkey, int key_len);
  146. ContactInfo* putContact(const mesh::Identity& id, uint8_t init_perms);
  147. bool applyContactPermissions(const uint8_t* pubkey, int key_len, uint8_t perms);
  148. void sendAlert(ContactInfo* c, Trigger* t);
  149. };