app_flashdb.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2026-07-08 Administrator the first version
  9. * 2026-07-09 Administrator fix: drop dead code after goto, write default KVs once,
  10. * properly init blob, deinit before re-init, check default
  11. * write result, guard TSDB partition lookup.
  12. */
  13. #include <app_flashdb.h>
  14. #include <control.h>
  15. #include <string.h>
  16. #define DBG_ENABLE
  17. #define DBG_COLOR
  18. #define DBG_TAG "app_flashdb"
  19. #define DBG_LEVEL DBG_LOG
  20. #include <rtdbg.h> /* must after DBG_LVL, DBG_TAG or other options */
  21. #define FDB_LOG_TAG "[app_flashdb]"
  22. #define FLASH_FLAG_VALUE 0xAA
  23. /* device status structure */
  24. Flashdb_typedef flashdb_value;
  25. static uint32_t boot_count = 0;
  26. static time_t boot_time[10] = {0, 1, 2, 3};
  27. /* Mutex for FlashDB operations (replace __disable_irq) */
  28. static rt_mutex_t flashdb_lock = RT_NULL;
  29. /* default KV nodes.
  30. * NOTE: every entry must reference a real object whose address/size is valid,
  31. * because FlashDB will memcpy the data from the pointer at the time it is set.
  32. */
  33. static uint8_t default_flag = FLASH_FLAG_VALUE;
  34. static uint8_t default_tape_used = 0;
  35. static uint8_t default_filter_used = 0;
  36. static uint16_t default_tape_max = 50000; //50m/50000mm ,
  37. static uint16_t default_filter_max = 1000; //10m
  38. static uint8_t default_freq_filter = 7;
  39. static uint8_t default_work_day = 0;
  40. static struct fdb_default_kv_node default_kv_table[] = {
  41. {"flag", &default_flag, sizeof(default_flag)},
  42. {"boot_count", &boot_count, sizeof(boot_count)},
  43. {"boot_time", &boot_time, sizeof(boot_time)},
  44. {"tape_used", &default_tape_used, sizeof(default_tape_used)},
  45. {"filter_used", &default_filter_used, sizeof(default_filter_used)},
  46. {"tape_max", &default_tape_max, sizeof(default_tape_max)},
  47. {"filter_max", &default_filter_max, sizeof(default_filter_max)},
  48. {"freq_filter", &default_freq_filter, sizeof(default_freq_filter)},
  49. {"worked_day", &default_work_day, sizeof(default_work_day)},
  50. };
  51. /* KVDB object */
  52. static struct fdb_kvdb kvdb = { 0 };
  53. /* TSDB object */
  54. struct fdb_tsdb tsdb = { 0 };
  55. /* counts for simulated timestamp */
  56. static int counts = 0;
  57. static void lock(fdb_db_t db)
  58. {
  59. if (flashdb_lock != RT_NULL) {
  60. rt_mutex_take(flashdb_lock, RT_WAITING_FOREVER);
  61. }
  62. }
  63. static void unlock(fdb_db_t db)
  64. {
  65. if (flashdb_lock != RT_NULL) {
  66. rt_mutex_release(flashdb_lock);
  67. }
  68. }
  69. static fdb_time_t get_time(void)
  70. {
  71. /* Using the counts instead of timestamp.
  72. * Replace this with RTC time if needed.
  73. */
  74. return ++counts;
  75. }
  76. static fdb_err_t write_default_kv(struct fdb_kvdb *db)
  77. {
  78. struct fdb_blob blob;
  79. fdb_err_t result = FDB_NO_ERR;
  80. for (size_t i = 0; i < sizeof(default_kv_table) / sizeof(default_kv_table[0]); i++) {
  81. result = fdb_kv_set_blob(db,
  82. default_kv_table[i].key,
  83. fdb_blob_make(&blob,
  84. default_kv_table[i].value,
  85. default_kv_table[i].value_len));
  86. /* Yield CPU to let syswatch feed watchdog */
  87. rt_thread_mdelay(1);
  88. if (result != FDB_NO_ERR) {
  89. LOG_E("fdb_kv_set_blob(\"%s\") failed: %d",
  90. default_kv_table[i].key, (int)result);
  91. break;
  92. }
  93. }
  94. return result;
  95. }
  96. int app_flashdb_init(void)
  97. {
  98. fdb_err_t result;
  99. #ifdef FDB_USING_KVDB
  100. { /* KVDB Sample */
  101. struct fdb_default_kv default_kv;
  102. struct fdb_blob blob;
  103. struct fal_partition *kv_part;
  104. uint8_t format_retry = 0;
  105. /* Initialize mutex for FlashDB operations */
  106. flashdb_lock = rt_mutex_create("flashdb_lock", RT_IPC_FLAG_FIFO);
  107. if (flashdb_lock == RT_NULL) {
  108. LOG_E("rt_mutex_create failed!");
  109. return -1;
  110. }
  111. memset(&blob, 0, sizeof(blob));
  112. default_kv.kvs = default_kv_table;
  113. default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]);
  114. /* get fdb_kv partition for fallback erase (only used on init failure) */
  115. kv_part = fal_partition_find("fdb_kv");
  116. if(kv_part==RT_NULL)
  117. {
  118. LOG_E("fdb_kv partition Not found!");
  119. return -1;
  120. }
  121. LOG_I("fdb_kv partition: flash=%s offset=0x%08X len=0x%08X",
  122. kv_part->flash_name, kv_part->offset, kv_part->len);
  123. retry_init:
  124. /* set the lock and unlock function if you want */
  125. fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_LOCK, (void *)lock);
  126. fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_UNLOCK, (void *)unlock);
  127. result = fdb_kvdb_init(&kvdb, "env", "fdb_kv", &default_kv, NULL);
  128. if (result != FDB_NO_ERR) {
  129. LOG_E("fdb_kvdb_init Failed:%d", result);
  130. return -1;
  131. }
  132. /* integrity check, if CRC error then erase and retry once */
  133. result = fdb_kvdb_check(&kvdb);
  134. if (result != FDB_NO_ERR) {
  135. LOG_W("KVDB integrity check failed (CRC error), formatting fdb_kv partition...");
  136. if (format_retry == 0 && kv_part != RT_NULL) {
  137. fdb_kvdb_deinit(&kvdb);
  138. fal_partition_erase(kv_part, 0, kv_part->len);
  139. format_retry = 1;
  140. goto retry_init;
  141. }
  142. }
  143. /* check the flag: 0xAA means user parameters have been stored before. */
  144. memset(&blob, 0, sizeof(blob));
  145. memset(&flashdb_value, 0, sizeof(flashdb_value));
  146. fdb_kv_get_blob(&kvdb, "flag",fdb_blob_make(&blob,&flashdb_value.iSave,sizeof(flashdb_value.iSave)));
  147. if (blob.saved.len == 0 || flashdb_value.iSave != FLASH_FLAG_VALUE) {
  148. LOG_I("Flag invalid (0x%02X), initializing default parameters", flashdb_value.iSave);
  149. /* If we get here from a fresh boot the sectors are already formatted
  150. * by FlashDB itself, so we only need to write the default values. */
  151. result = write_default_kv(&kvdb);
  152. if (result != FDB_NO_ERR) {
  153. LOG_E("write_default_kv failed: %d", (int)result);
  154. return -1;
  155. }
  156. LOG_I("Default parameters saved");
  157. } else {
  158. LOG_I("Flag == 0xAA, loading existing parameters from FlashDB");
  159. fdb_kv_get_blob(&kvdb, "boot_count", fdb_blob_make(&blob, &boot_count, sizeof(boot_count)));
  160. fdb_kv_get_blob(&kvdb, "boot_time", fdb_blob_make(&blob, &boot_time, sizeof(boot_time)));
  161. fdb_kv_get_blob(&kvdb, "tape_used", fdb_blob_make(&blob, &flashdb_value.tape_used, sizeof(flashdb_value.tape_used)));
  162. fdb_kv_get_blob(&kvdb, "tape_max", fdb_blob_make(&blob, &flashdb_value.tape_max, sizeof(flashdb_value.tape_max)));
  163. fdb_kv_get_blob(&kvdb, "filter_used",fdb_blob_make(&blob, &flashdb_value.filter_used,sizeof(flashdb_value.filter_used)));
  164. fdb_kv_get_blob(&kvdb, "freq_filter",fdb_blob_make(&blob, &flashdb_value.freq_filter,sizeof(flashdb_value.freq_filter)));
  165. fdb_kv_get_blob(&kvdb, "filter_max", fdb_blob_make(&blob, &flashdb_value.filter_max, sizeof(flashdb_value.filter_max)));
  166. FDB_INFO("boot_count:%d\n", boot_count);
  167. FDB_INFO("boot_time:%d\n", boot_time[0]);
  168. FDB_INFO("tape_used:%d\n", flashdb_value.tape_used);
  169. FDB_INFO("tape_max:%d\n", flashdb_value.tape_max);
  170. FDB_INFO("filter_used:%d\n", flashdb_value.filter_used);
  171. FDB_INFO("freq_filter:%d\n", flashdb_value.freq_filter);
  172. FDB_INFO("filter_max:%d\n", flashdb_value.filter_max);
  173. boot_count++;
  174. fdb_kv_set_blob(&kvdb, "boot_count",
  175. fdb_blob_make(&blob, &boot_count, sizeof(boot_count)));
  176. FDB_INFO("set the 'boot_count' value to %d\n", boot_count);
  177. }
  178. }
  179. #endif /* FDB_USING_KVDB */
  180. #ifdef _FDB_USING_TSDB
  181. { /* TSDB Sample */
  182. struct fal_partition *ts_part = fal_partition_find("fdb_ts");
  183. uint8_t ts_format_retry = 0;
  184. if (ts_part == RT_NULL) {
  185. LOG_E("fdb_ts partition Not found!");
  186. return -1;
  187. }
  188. LOG_I("fdb_ts partition: flash=%s offset=0x%08X len=0x%08X",
  189. ts_part->flash_name, ts_part->offset, ts_part->len);
  190. retry_ts_init:
  191. fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_LOCK, (void *)lock);
  192. fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_UNLOCK, (void *)unlock);
  193. result = fdb_tsdb_init(&tsdb, "log", "fdb_ts", get_time, 128, NULL);
  194. if (result != FDB_NO_ERR) {
  195. LOG_W("TSDB init failed, formatting fdb_ts partition...");
  196. if (ts_format_retry == 0 && ts_part != RT_NULL) {
  197. fdb_tsdb_deinit(&tsdb);
  198. fal_partition_erase(ts_part, 0, ts_part->len);
  199. ts_format_retry = 1;
  200. goto retry_ts_init;
  201. }
  202. return -1;
  203. }
  204. /* read last saved time for simulated timestamp */
  205. fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
  206. }
  207. #endif /* FDB_USING_TSDB */
  208. return 0;
  209. }
  210. int app_flashdb_save(const char *key, void *value)
  211. {
  212. // 检查key是否为空
  213. if (key == NULL) {
  214. LOG_E("key is NULL");
  215. return -1;
  216. }
  217. // 检查value是否为空
  218. if (value == NULL) {
  219. LOG_E("value is NULL");
  220. return -1;
  221. }
  222. struct fdb_blob blob;
  223. memset(&blob, 0, sizeof(blob));
  224. return fdb_kv_set_blob(&kvdb, key, fdb_blob_make(&blob, value, sizeof(value)));
  225. }
  226. int app_flashdb_load(const char *key, void *value)
  227. {
  228. // 检查key是否为空
  229. if (key == NULL) {
  230. LOG_E("key is NULL");
  231. return -1;
  232. }
  233. // 检查value是否为空
  234. if (value == NULL) {
  235. LOG_E("value is NULL");
  236. return -1;
  237. }
  238. struct fdb_blob blob;
  239. memset(&blob, 0, sizeof(blob));
  240. return fdb_kv_get_blob(&kvdb, key, fdb_blob_make(&blob, value, sizeof(value)));
  241. }