MeshCore.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #include <stdint.h>
  3. #include <math.h>
  4. #define MAX_HASH_SIZE 8
  5. #define PUB_KEY_SIZE 32
  6. #define PRV_KEY_SIZE 64
  7. #define SEED_SIZE 32
  8. #define SIGNATURE_SIZE 64
  9. #define MAX_ADVERT_DATA_SIZE 32
  10. #define CIPHER_KEY_SIZE 16
  11. #define CIPHER_BLOCK_SIZE 16
  12. // V1
  13. #define CIPHER_MAC_SIZE 2
  14. #define PATH_HASH_SIZE 1
  15. #define MAX_PACKET_PAYLOAD 184
  16. #define MAX_GROUP_DATA_LENGTH (MAX_PACKET_PAYLOAD - CIPHER_BLOCK_SIZE - 3)
  17. #define MAX_PATH_SIZE 64
  18. #define MAX_TRANS_UNIT 255
  19. #if MESH_DEBUG && ARDUINO
  20. #include <Arduino.h>
  21. #define MESH_DEBUG_PRINT(F, ...) Serial.printf("DEBUG: " F, ##__VA_ARGS__)
  22. #define MESH_DEBUG_PRINTLN(F, ...) Serial.printf("DEBUG: " F "\n", ##__VA_ARGS__)
  23. #else
  24. #define MESH_DEBUG_PRINT(...) {}
  25. #define MESH_DEBUG_PRINTLN(...) {}
  26. #endif
  27. #if BRIDGE_DEBUG && ARDUINO
  28. #define BRIDGE_DEBUG_PRINTLN(F, ...) Serial.printf("%s BRIDGE: " F, getLogDateTime(), ##__VA_ARGS__)
  29. #else
  30. #define BRIDGE_DEBUG_PRINTLN(...) {}
  31. #endif
  32. namespace mesh {
  33. #define BD_STARTUP_NORMAL 0 // getStartupReason() codes
  34. #define BD_STARTUP_RX_PACKET 1
  35. class MainBoard {
  36. public:
  37. virtual uint16_t getBattMilliVolts() = 0;
  38. virtual float getMCUTemperature() { return NAN; }
  39. virtual bool setAdcMultiplier(float multiplier) { return false; };
  40. virtual float getAdcMultiplier() const { return 0.0f; }
  41. virtual const char* getManufacturerName() const = 0;
  42. virtual void onBeforeTransmit() { }
  43. virtual void onAfterTransmit() { }
  44. virtual void reboot() = 0;
  45. virtual void powerOff() { /* no op */ }
  46. // Called by example setup() functions to signal that boot is complete.
  47. // Boards may override to stop a boot-indicator LED sequence or similar.
  48. // Default no-op: boards that don't care need not implement anything.
  49. virtual void onBootComplete() { /* no op */ }
  50. virtual uint32_t getIRQGpio() { return -1; } // not supported. Returns DIO1 (SX1262) and DIO0 (SX127x)
  51. virtual void sleep(uint32_t secs) { /* no op */ }
  52. virtual uint32_t getGpio() { return 0; }
  53. virtual void setGpio(uint32_t values) {}
  54. virtual uint8_t getStartupReason() const = 0;
  55. virtual bool getBootloaderVersion(char* version, size_t max_len) { return false; }
  56. virtual bool startOTAUpdate(const char* id, char reply[]) { return false; } // not supported
  57. // Power management interface (boards with power management override these)
  58. virtual bool isExternalPowered() { return false; }
  59. virtual uint16_t getBootVoltage() { return 0; }
  60. virtual uint32_t getResetReason() const { return 0; }
  61. virtual const char* getResetReasonString(uint32_t reason) { return "Not available"; }
  62. virtual uint8_t getShutdownReason() const { return 0; }
  63. virtual const char* getShutdownReasonString(uint8_t reason) { return "Not available"; }
  64. };
  65. /**
  66. * An abstraction of the device's Realtime Clock.
  67. */
  68. class RTCClock {
  69. uint32_t last_unique;
  70. protected:
  71. RTCClock() { last_unique = 0; }
  72. public:
  73. /**
  74. * \returns the current time. in UNIX epoch seconds.
  75. */
  76. virtual uint32_t getCurrentTime() = 0;
  77. /**
  78. * \param time current time in UNIX epoch seconds.
  79. */
  80. virtual void setCurrentTime(uint32_t time) = 0;
  81. /**
  82. * override in classes that need to periodically update internal state
  83. */
  84. virtual void tick() { /* no op */}
  85. uint32_t getCurrentTimeUnique() {
  86. uint32_t t = getCurrentTime();
  87. if (t <= last_unique) {
  88. return ++last_unique;
  89. }
  90. return last_unique = t;
  91. }
  92. };
  93. }