You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "znvs.hpp"
  2. #include <string.h>
  3. #include "zsimple_flash.hpp"
  4. using namespace iflytop;
  5. using namespace std;
  6. #define TAG "ZNVS"
  7. #define MARK_S 0x12345678
  8. #define MARK_E 0x87654321
  9. #ifdef IFLYTOP_NVS_CONFIG_FLASH_SECTOR
  10. ZNVS& ZNVS::ins() {
  11. static ZNVS s_ins;
  12. return s_ins;
  13. }
  14. void ZNVS::initialize() { zsimple_flash_init(IFLYTOP_NVS_CONFIG_FLASH_SECTOR); }
  15. bool ZNVS::alloc_config(const char* key, uint8_t* data, size_t datalen) {
  16. ZASSERT(m_index_num < ZARRAY_SIZE(m_index));
  17. m_index[m_index_num].name = key;
  18. m_index[m_index_num].len = datalen;
  19. m_index[m_index_num].defaultval = data;
  20. m_index_num++;
  21. return true;
  22. }
  23. bool ZNVS::init_config() {
  24. int configtotallen = 0;
  25. for (int i = 0; i < m_index_num; i++) {
  26. configtotallen += m_index[i].len;
  27. }
  28. configtotallen = configtotallen + sizeof(config_t) /*head*/ + 4 /*tail*/;
  29. m_cfgcache = (config_t*)malloc(configtotallen);
  30. ZASSERT(m_cfgcache);
  31. m_cfgcachesize = configtotallen;
  32. zsimple_flash_read((uint8_t*)m_cfgcache, m_cfgcachesize);
  33. if (m_cfgcache->config_start != MARK_S || m_cfgcache->data_size != (m_cfgcachesize - sizeof(config_t))) {
  34. ZLOGW(TAG, "config uninitialized, initialize it");
  35. memset(m_cfgcache, 0, m_cfgcachesize);
  36. m_cfgcache->config_start = MARK_S;
  37. m_cfgcache->data_size = m_cfgcachesize - sizeof(config_t);
  38. flush_all_to_configcache();
  39. zsimple_flash_write((uint8_t*)m_cfgcache, m_cfgcachesize);
  40. } else {
  41. ZLOGI(TAG, "config initialized successfully");
  42. }
  43. inited = true;
  44. return true;
  45. }
  46. void ZNVS::flush_all_to_configcache() {
  47. for (int i = 0; i < m_index_num; i++) {
  48. memcpy(m_cfgcache->data, m_index[i].defaultval, m_index[i].len);
  49. }
  50. }
  51. bool ZNVS::get_config(const char* key, uint8_t* data, size_t len) {
  52. ZASSERT(inited);
  53. int dataoffset = 0;
  54. for (int i = 0; i < m_index_num; i++) {
  55. if (strcmp(m_index[i].name, key) == 0) {
  56. ZASSERT(len == m_index[i].len);
  57. memcpy(data, m_cfgcache->data + dataoffset, m_index[i].len);
  58. return true;
  59. }
  60. dataoffset += m_index[i].len;
  61. }
  62. return false;
  63. }
  64. bool ZNVS::set_config(const char* key, uint8_t* data, size_t len) {
  65. ZASSERT(inited);
  66. int dataoffset = 0;
  67. for (int i = 0; i < m_index_num; i++) {
  68. if (strcmp(m_index[i].name, key) == 0) {
  69. memcpy(m_cfgcache->data + dataoffset, data, len);
  70. return true;
  71. }
  72. dataoffset += m_index[i].len;
  73. }
  74. return false;
  75. }
  76. void ZNVS::flush() { zsimple_flash_write((uint8_t*)m_cfgcache, m_cfgcachesize); }
  77. #endif