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.

88 lines
2.5 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
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. ZNVS& ZNVS::ins() {
  10. static ZNVS s_ins;
  11. return s_ins;
  12. }
  13. void ZNVS::initialize(int32_t flash_sector) { zsimple_flash_init(flash_sector); }
  14. bool ZNVS::alloc_config(const char* key, uint8_t* data, size_t datalen) {
  15. ZASSERT(m_index_num < ZARRAY_SIZE(m_index));
  16. m_index[m_index_num].name = key;
  17. m_index[m_index_num].len = datalen;
  18. m_index[m_index_num].defaultval = data;
  19. m_index_num++;
  20. return true;
  21. }
  22. bool ZNVS::init_config() {
  23. int configtotallen = 0;
  24. for (int i = 0; i < m_index_num; i++) {
  25. configtotallen += m_index[i].len;
  26. }
  27. configtotallen = configtotallen + sizeof(config_t) /*head*/ + 4 /*tail*/;
  28. m_cfgcache = (config_t*)malloc(configtotallen);
  29. ZASSERT(m_cfgcache);
  30. m_cfgcachesize = configtotallen;
  31. zsimple_flash_read((uint8_t*)m_cfgcache, m_cfgcachesize);
  32. if (m_cfgcache->config_start != MARK_S || m_cfgcache->data_size != (m_cfgcachesize - sizeof(config_t))) {
  33. ZLOGW(TAG, "config uninitialized, initialize it");
  34. memset(m_cfgcache, 0, m_cfgcachesize);
  35. m_cfgcache->config_start = MARK_S;
  36. m_cfgcache->data_size = m_cfgcachesize - sizeof(config_t);
  37. flush_all_to_configcache();
  38. zsimple_flash_write((uint8_t*)m_cfgcache, m_cfgcachesize);
  39. } else {
  40. ZLOGI(TAG, "config initialized successfully");
  41. }
  42. inited = true;
  43. return true;
  44. }
  45. void ZNVS::flush_all_to_configcache() {
  46. for (int i = 0; i < m_index_num; i++) {
  47. memcpy(m_cfgcache->data, m_index[i].defaultval, m_index[i].len);
  48. }
  49. }
  50. bool ZNVS::get_config(const char* key, uint8_t* data, size_t len) {
  51. ZASSERT(inited);
  52. int dataoffset = 0;
  53. for (int i = 0; i < m_index_num; i++) {
  54. if (strcmp(m_index[i].name, key) == 0) {
  55. ZASSERT(len == m_index[i].len);
  56. memcpy(data, m_cfgcache->data + dataoffset, m_index[i].len);
  57. return true;
  58. }
  59. dataoffset += m_index[i].len;
  60. }
  61. return false;
  62. }
  63. bool ZNVS::set_config(const char* key, uint8_t* data, size_t len) {
  64. ZASSERT(inited);
  65. int dataoffset = 0;
  66. for (int i = 0; i < m_index_num; i++) {
  67. if (strcmp(m_index[i].name, key) == 0) {
  68. memcpy(m_cfgcache->data + dataoffset, data, len);
  69. return true;
  70. }
  71. dataoffset += m_index[i].len;
  72. }
  73. return false;
  74. }
  75. void ZNVS::flush() {
  76. ZLOGI(TAG, "flush config to flash");
  77. zsimple_flash_write((uint8_t*)m_cfgcache, m_cfgcachesize);
  78. ZLOGI(TAG, "flush config to flash done");
  79. }