MeshCore.h 3.7 KB

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