/* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2026-07-08 Administrator the first version * 2026-07-09 Administrator fix: drop dead code after goto, write default KVs once, * properly init blob, deinit before re-init, check default * write result, guard TSDB partition lookup. */ #include #include #include #define DBG_ENABLE #define DBG_COLOR #define DBG_TAG "app_flashdb" #define DBG_LEVEL DBG_LOG #include /* must after DBG_LVL, DBG_TAG or other options */ #define FDB_LOG_TAG "[app_flashdb]" #define FLASH_FLAG_VALUE 0xAA /* device status structure */ Flashdb_typedef flashdb_value; static uint32_t boot_count = 0; static time_t boot_time[10] = {0, 1, 2, 3}; /* Mutex for FlashDB operations (replace __disable_irq) */ static rt_mutex_t flashdb_lock = RT_NULL; /* default KV nodes. * NOTE: every entry must reference a real object whose address/size is valid, * because FlashDB will memcpy the data from the pointer at the time it is set. */ static uint8_t default_flag = FLASH_FLAG_VALUE; static uint8_t default_tape_used = 0; static uint8_t default_filter_used = 0; static uint16_t default_tape_max = 50000; //50m/50000mm , static uint16_t default_filter_max = 1000; //10m static uint8_t default_freq_filter = 7; static uint8_t default_work_day = 0; static struct fdb_default_kv_node default_kv_table[] = { {"flag", &default_flag, sizeof(default_flag)}, {"boot_count", &boot_count, sizeof(boot_count)}, {"boot_time", &boot_time, sizeof(boot_time)}, {"tape_used", &default_tape_used, sizeof(default_tape_used)}, {"filter_used", &default_filter_used, sizeof(default_filter_used)}, {"tape_max", &default_tape_max, sizeof(default_tape_max)}, {"filter_max", &default_filter_max, sizeof(default_filter_max)}, {"freq_filter", &default_freq_filter, sizeof(default_freq_filter)}, {"worked_day", &default_work_day, sizeof(default_work_day)}, }; /* KVDB object */ static struct fdb_kvdb kvdb = { 0 }; /* TSDB object */ struct fdb_tsdb tsdb = { 0 }; /* counts for simulated timestamp */ static int counts = 0; static void lock(fdb_db_t db) { if (flashdb_lock != RT_NULL) { rt_mutex_take(flashdb_lock, RT_WAITING_FOREVER); } } static void unlock(fdb_db_t db) { if (flashdb_lock != RT_NULL) { rt_mutex_release(flashdb_lock); } } static fdb_time_t get_time(void) { /* Using the counts instead of timestamp. * Replace this with RTC time if needed. */ return ++counts; } static fdb_err_t write_default_kv(struct fdb_kvdb *db) { struct fdb_blob blob; fdb_err_t result = FDB_NO_ERR; for (size_t i = 0; i < sizeof(default_kv_table) / sizeof(default_kv_table[0]); i++) { result = fdb_kv_set_blob(db, default_kv_table[i].key, fdb_blob_make(&blob, default_kv_table[i].value, default_kv_table[i].value_len)); /* Yield CPU to let syswatch feed watchdog */ rt_thread_mdelay(1); if (result != FDB_NO_ERR) { LOG_E("fdb_kv_set_blob(\"%s\") failed: %d", default_kv_table[i].key, (int)result); break; } } return result; } int app_flashdb_init(void) { fdb_err_t result; #ifdef FDB_USING_KVDB { /* KVDB Sample */ struct fdb_default_kv default_kv; struct fdb_blob blob; struct fal_partition *kv_part; uint8_t format_retry = 0; /* Initialize mutex for FlashDB operations */ flashdb_lock = rt_mutex_create("flashdb_lock", RT_IPC_FLAG_FIFO); if (flashdb_lock == RT_NULL) { LOG_E("rt_mutex_create failed!"); return -1; } memset(&blob, 0, sizeof(blob)); default_kv.kvs = default_kv_table; default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]); /* get fdb_kv partition for fallback erase (only used on init failure) */ kv_part = fal_partition_find("fdb_kv"); if(kv_part==RT_NULL) { LOG_E("fdb_kv partition Not found!"); return -1; } LOG_I("fdb_kv partition: flash=%s offset=0x%08X len=0x%08X", kv_part->flash_name, kv_part->offset, kv_part->len); retry_init: /* set the lock and unlock function if you want */ fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_LOCK, (void *)lock); fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_UNLOCK, (void *)unlock); result = fdb_kvdb_init(&kvdb, "env", "fdb_kv", &default_kv, NULL); if (result != FDB_NO_ERR) { LOG_E("fdb_kvdb_init Failed:%d", result); return -1; } /* integrity check, if CRC error then erase and retry once */ result = fdb_kvdb_check(&kvdb); if (result != FDB_NO_ERR) { LOG_W("KVDB integrity check failed (CRC error), formatting fdb_kv partition..."); if (format_retry == 0 && kv_part != RT_NULL) { fdb_kvdb_deinit(&kvdb); fal_partition_erase(kv_part, 0, kv_part->len); format_retry = 1; goto retry_init; } } /* check the flag: 0xAA means user parameters have been stored before. */ memset(&blob, 0, sizeof(blob)); memset(&flashdb_value, 0, sizeof(flashdb_value)); fdb_kv_get_blob(&kvdb, "flag",fdb_blob_make(&blob,&flashdb_value.iSave,sizeof(flashdb_value.iSave))); if (blob.saved.len == 0 || flashdb_value.iSave != FLASH_FLAG_VALUE) { LOG_I("Flag invalid (0x%02X), initializing default parameters", flashdb_value.iSave); /* If we get here from a fresh boot the sectors are already formatted * by FlashDB itself, so we only need to write the default values. */ result = write_default_kv(&kvdb); if (result != FDB_NO_ERR) { LOG_E("write_default_kv failed: %d", (int)result); return -1; } LOG_I("Default parameters saved"); } else { LOG_I("Flag == 0xAA, loading existing parameters from FlashDB"); fdb_kv_get_blob(&kvdb, "boot_count", fdb_blob_make(&blob, &boot_count, sizeof(boot_count))); fdb_kv_get_blob(&kvdb, "boot_time", fdb_blob_make(&blob, &boot_time, sizeof(boot_time))); fdb_kv_get_blob(&kvdb, "tape_used", fdb_blob_make(&blob, &flashdb_value.tape_used, sizeof(flashdb_value.tape_used))); fdb_kv_get_blob(&kvdb, "tape_max", fdb_blob_make(&blob, &flashdb_value.tape_max, sizeof(flashdb_value.tape_max))); fdb_kv_get_blob(&kvdb, "filter_used",fdb_blob_make(&blob, &flashdb_value.filter_used,sizeof(flashdb_value.filter_used))); fdb_kv_get_blob(&kvdb, "freq_filter",fdb_blob_make(&blob, &flashdb_value.freq_filter,sizeof(flashdb_value.freq_filter))); fdb_kv_get_blob(&kvdb, "filter_max", fdb_blob_make(&blob, &flashdb_value.filter_max, sizeof(flashdb_value.filter_max))); FDB_INFO("boot_count:%d\n", boot_count); FDB_INFO("boot_time:%d\n", boot_time[0]); FDB_INFO("tape_used:%d\n", flashdb_value.tape_used); FDB_INFO("tape_max:%d\n", flashdb_value.tape_max); FDB_INFO("filter_used:%d\n", flashdb_value.filter_used); FDB_INFO("freq_filter:%d\n", flashdb_value.freq_filter); FDB_INFO("filter_max:%d\n", flashdb_value.filter_max); boot_count++; fdb_kv_set_blob(&kvdb, "boot_count", fdb_blob_make(&blob, &boot_count, sizeof(boot_count))); FDB_INFO("set the 'boot_count' value to %d\n", boot_count); } } #endif /* FDB_USING_KVDB */ #ifdef _FDB_USING_TSDB { /* TSDB Sample */ struct fal_partition *ts_part = fal_partition_find("fdb_ts"); uint8_t ts_format_retry = 0; if (ts_part == RT_NULL) { LOG_E("fdb_ts partition Not found!"); return -1; } LOG_I("fdb_ts partition: flash=%s offset=0x%08X len=0x%08X", ts_part->flash_name, ts_part->offset, ts_part->len); retry_ts_init: fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_LOCK, (void *)lock); fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_UNLOCK, (void *)unlock); result = fdb_tsdb_init(&tsdb, "log", "fdb_ts", get_time, 128, NULL); if (result != FDB_NO_ERR) { LOG_W("TSDB init failed, formatting fdb_ts partition..."); if (ts_format_retry == 0 && ts_part != RT_NULL) { fdb_tsdb_deinit(&tsdb); fal_partition_erase(ts_part, 0, ts_part->len); ts_format_retry = 1; goto retry_ts_init; } return -1; } /* read last saved time for simulated timestamp */ fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts); } #endif /* FDB_USING_TSDB */ return 0; } int app_flashdb_save(const char *key, void *value) { // 检查key是否为空 if (key == NULL) { LOG_E("key is NULL"); return -1; } // 检查value是否为空 if (value == NULL) { LOG_E("value is NULL"); return -1; } struct fdb_blob blob; memset(&blob, 0, sizeof(blob)); return fdb_kv_set_blob(&kvdb, key, fdb_blob_make(&blob, value, sizeof(value))); } int app_flashdb_load(const char *key, void *value) { // 检查key是否为空 if (key == NULL) { LOG_E("key is NULL"); return -1; } // 检查value是否为空 if (value == NULL) { LOG_E("value is NULL"); return -1; } struct fdb_blob blob; memset(&blob, 0, sizeof(blob)); return fdb_kv_get_blob(&kvdb, key, fdb_blob_make(&blob, value, sizeof(value))); }