MeshCore.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 void sleep(uint32_t secs) { /* no op */ }
  51. virtual uint32_t getGpio() { return 0; }
  52. virtual void setGpio(uint32_t values) {}
  53. virtual uint8_t getStartupReason() const = 0;
  54. virtual bool getBootloaderVersion(char* version, size_t max_len) { return false; }
  55. virtual bool startOTAUpdate(const char* id, char reply[]) { return false; } // not supported
  56. // Power management interface (boards with power management override these)
  57. virtual bool isExternalPowered() { return false; }
  58. virtual uint16_t getBootVoltage() { return 0; }
  59. virtual uint32_t getResetReason() const { return 0; }
  60. virtual const char* getResetReasonString(uint32_t reason) { return "Not available"; }
  61. virtual uint8_t getShutdownReason() const { return 0; }
  62. virtual const char* getShutdownReasonString(uint8_t reason) { return "Not available"; }
  63. };
  64. /**
  65. * An abstraction of the device's Realtime Clock.
  66. */
  67. class RTCClock {
  68. uint32_t last_unique;
  69. protected:
  70. RTCClock() { last_unique = 0; }
  71. public:
  72. /**
  73. * \returns the current time. in UNIX epoch seconds.
  74. */
  75. virtual uint32_t getCurrentTime() = 0;
  76. /**
  77. * \param time current time in UNIX epoch seconds.
  78. */
  79. virtual void setCurrentTime(uint32_t time) = 0;
  80. /**
  81. * override in classes that need to periodically update internal state
  82. */
  83. virtual void tick() { /* no op */}
  84. uint32_t getCurrentTimeUnique() {
  85. uint32_t t = getCurrentTime();
  86. if (t <= last_unique) {
  87. return ++last_unique;
  88. }
  89. return last_unique = t;
  90. }
  91. };
  92. }