platform_config.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "nvs.h"
  5. #include "assert.h"
  6. #include "cJSON.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #define PARSE_PARAM(S,P,C,V) do { \
  11. char *__p; \
  12. if ((__p = strcasestr(S, P)) && (__p = strchr(__p, C))) V = atoi(__p+1); \
  13. } while (0)
  14. #define PARSE_PARAM_FLOAT(S,P,C,V) do { \
  15. char *__p; \
  16. if ((__p = strcasestr(S, P)) && (__p = strchr(__p, C))) V = atof(__p+1); \
  17. } while (0)
  18. #define PARSE_PARAM_STR(S,P,C,V,I) do { \
  19. char *__p; \
  20. if ((__p = strstr(S, P)) && (__p = strchr(__p, C))) { \
  21. while (*++__p == ' '); \
  22. sscanf(__p,"%" #I "[^,]", V); \
  23. } \
  24. } while (0)
  25. #define DECLARE_SET_DEFAULT(t) void config_set_default_## t (const char *key, t value);
  26. #define DECLARE_GET_NUM(t) esp_err_t config_get_## t (const char *key, t * value);
  27. #ifndef FREE_RESET
  28. #define FREE_RESET(p) if(p!=NULL) { free(p); p=NULL; }
  29. #endif
  30. DECLARE_SET_DEFAULT(uint8_t);
  31. DECLARE_SET_DEFAULT(uint16_t);
  32. DECLARE_SET_DEFAULT(uint32_t);
  33. DECLARE_SET_DEFAULT(int8_t);
  34. DECLARE_SET_DEFAULT(int16_t);
  35. DECLARE_SET_DEFAULT(int32_t);
  36. DECLARE_GET_NUM(uint8_t);
  37. DECLARE_GET_NUM(uint16_t);
  38. DECLARE_GET_NUM(uint32_t);
  39. DECLARE_GET_NUM(int8_t);
  40. DECLARE_GET_NUM(int16_t);
  41. DECLARE_GET_NUM(int32_t);
  42. bool config_has_changes();
  43. void config_commit_to_nvs();
  44. void config_start_timer();
  45. void config_init();
  46. void * config_alloc_get_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size);
  47. void * config_alloc_get_str(const char *key, char *lead, char *fallback);
  48. cJSON * config_alloc_get_cjson(const char *key);
  49. esp_err_t config_set_cjson_str_and_free(const char *key, cJSON *value);
  50. esp_err_t config_set_cjson(const char *key, cJSON *value, bool free_cjson);
  51. void config_get_uint16t_from_str(const char *key, uint16_t *value, uint16_t default_value);
  52. void config_delete_key(const char *key);
  53. void config_set_default(nvs_type_t type, const char *key, const void * default_value, size_t blob_size);
  54. void * config_alloc_get(nvs_type_t nvs_type, const char *key) ;
  55. bool wait_for_commit();
  56. char * config_alloc_get_json(bool bFormatted);
  57. esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, const void * value);
  58. nvs_type_t config_get_item_type(cJSON * entry);
  59. void * config_safe_alloc_get_entry_value(nvs_type_t nvs_type, cJSON * entry);
  60. cJSON* cjson_update_number(cJSON** root, const char* key, int value);
  61. cJSON* cjson_update_string(cJSON** root, const char* key, const char* value);
  62. #ifdef __cplusplus
  63. }
  64. #endif