Configurator.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include "State.pb.h"
  3. #include "configuration.pb.h"
  4. #include "esp_log.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/event_groups.h"
  7. #include "freertos/semphr.h"
  8. #include "freertos/timers.h"
  9. #include "status.pb.h"
  10. #include <strings.h>
  11. #include "accessors.h"
  12. #ifdef __cplusplus
  13. #include <cstdlib>
  14. #include <string>
  15. #include <vector>
  16. extern "C" {
  17. #endif
  18. #define PLATFORM_GET_PTR(base, sname) \
  19. { \
  20. (base && (base)->##has_##(sname) ? &(base)->sname : NULL)
  21. #define PLATFORM_DEVICES PLATFORM_GET_PTR(platform)
  22. void configurator_load();
  23. bool configurator_waitcommit();
  24. bool configurator_has_changes();
  25. bool configurator_lock();
  26. void configurator_unlock();
  27. void configurator_raise_changed();
  28. void configurator_raise_state_changed();
  29. bool configurator_has_changes();
  30. bool configurator_waitcommit();
  31. void* configurator_alloc_get_config(size_t* sz);
  32. bool configurator_parse_config(void* buffer, size_t buffer_size);
  33. void configurator_reset_configuration();
  34. pb_type_t configurator_get_field_type(const pb_msgdesc_t* desc, uint32_t tag);
  35. bool configurator_set_string(
  36. const pb_msgdesc_t* desc, uint32_t field_tag, void* message, const char* value);
  37. extern sys_Config* platform;
  38. extern sys_State* sys_state;
  39. #ifdef __cplusplus
  40. }
  41. #endif
  42. #ifdef __cplusplus
  43. namespace PlatformConfig {
  44. class Configurator {
  45. private:
  46. static const int MaxDelay;
  47. static const int LockMaxWait;
  48. EXT_RAM_ATTR static TimerHandle_t _timer;
  49. EXT_RAM_ATTR static SemaphoreHandle_t _mutex;
  50. EXT_RAM_ATTR static SemaphoreHandle_t _state_mutex;
  51. EXT_RAM_ATTR static EventGroupHandle_t _group;
  52. bool SetGroupBit(int bit_num, bool flag);
  53. void ResetModified();
  54. void ResetStateModified();
  55. sys_Config _root;
  56. sys_State _sys_state;
  57. public:
  58. sys_Config* Root() { return &_root; }
  59. sys_State* RootState() { return &_sys_state; }
  60. bool WaitForCommit();
  61. bool Lock();
  62. bool LockState();
  63. void Unlock();
  64. void UnlockState();
  65. void* AllocGetConfigBuffer(size_t* sz);
  66. static void* AllocGetConfigBuffer(size_t* sz, sys_Config* config);
  67. static void ResetStructure(sys_Config* config);
  68. bool LoadDecodeState();
  69. void CommitChanges();
  70. bool Commit();
  71. static bool Commit(sys_Config* config);
  72. bool CommitState();
  73. static bool CommitState(sys_State* state);
  74. sys_Config* AllocDefaultStruct();
  75. static bool ApplyTargetSettings(sys_Config* conf_root);
  76. bool ApplyTargetSettings();
  77. bool HasStateChanges();
  78. bool LoadDecodeBuffer(void* buffer, size_t buffer_size);
  79. void InitLoadConfig(const char* filename);
  80. void InitLoadConfig(const char* filename, sys_Config* conf_root, bool noinit = false);
  81. static bool LoadDecode(
  82. void* buffer, size_t buffer_size, sys_Config* conf_root, bool noinit = false);
  83. Configurator() {
  84. _mutex = xSemaphoreCreateMutex();
  85. _state_mutex = xSemaphoreCreateMutex();
  86. _group = xEventGroupCreate();
  87. }
  88. void RaiseStateModified();
  89. void RaiseModified();
  90. bool HasChanges();
  91. ~Configurator() {}
  92. };
  93. } // namespace PlatformConfig
  94. extern PlatformConfig::Configurator configurator;
  95. #endif