XFusion API v1.3.0
载入中...
搜索中...
未找到
examples/storage/fal/fal_flashdb/main/xf_main.c

基于 xf_fal 的 flashdb 示例。

1
17/* ==================== [Includes] ========================================== */
18
19#include "xf_utils.h"
20#include "xf_osal.h"
21#include "xf_fal.h"
22#include "flashdb.h"
23
24/* ==================== [Defines] =========================================== */
25
26#define TAG "flashdb_example"
27
28#define KVDB_NAME CONFIG_PORT_FDB_XF_FAL_KVDB_PARTITION_NAME
29#define TSDB_NAME CONFIG_PORT_FDB_XF_FAL_TSDB_PARTITION_NAME
30
31/* ==================== [Typedefs] ========================================== */
32
33/* ==================== [Static Prototypes] ================================= */
34
35static fdb_time_t get_time(void);
36static void lock(fdb_db_t db);
37static void unlock(fdb_db_t db);
38
39/* 这些示例位于 `examples/storage/fal/fal_flashdb/components/FlashDB/samples` */
40extern void kvdb_basic_sample(fdb_kvdb_t kvdb);
41extern void kvdb_type_string_sample(fdb_kvdb_t kvdb);
42extern void kvdb_type_blob_sample(fdb_kvdb_t kvdb);
43extern void tsdb_sample(fdb_tsdb_t tsdb);
44
45static int flashdb_demo(void);
46
47/* ==================== [Static Variables] ================================== */
48
54 {
55 CONFIG_PORT_FDB_XF_FAL_KVDB_PARTITION_NAME,
56 CONFIG_PORT_FDB_XF_FAL_FLASH_NAME,
57 CONFIG_PORT_FDB_XF_FAL_KVDB_PARTITION_OFFSET,
58 CONFIG_PORT_FDB_XF_FAL_KVDB_PARTITION_LENGTH,
59 },
60 {
61 CONFIG_PORT_FDB_XF_FAL_TSDB_PARTITION_NAME,
62 CONFIG_PORT_FDB_XF_FAL_FLASH_NAME,
63 CONFIG_PORT_FDB_XF_FAL_TSDB_PARTITION_OFFSET,
64 CONFIG_PORT_FDB_XF_FAL_TSDB_PARTITION_LENGTH,
65 },
66};
67
68static uint32_t boot_count = 0;
69static time_t boot_time[10] = {0, 1, 2, 3};
70/* default KV nodes */
71static struct fdb_default_kv_node default_kv_table[] = {
72 {"username", "armink", 0}, /* string KV */
73 {"password", "123456", 0}, /* string KV */
74 {"boot_count", &boot_count, sizeof(boot_count)}, /* int type KV */
75 {"boot_time", &boot_time, sizeof(boot_time)}, /* int array type KV */
76};
77/* KVDB object */
78static struct fdb_kvdb kvdb = {0};
79/* TSDB object */
80struct fdb_tsdb tsdb = {0};
81/* counts for simulated timestamp */
82static int counts = 0;
83
84static xf_osal_mutex_t s_mutex = NULL;
85
86/* ==================== [Macros] ============================================ */
87
88/* ==================== [Global Functions] ================================== */
89
90void xf_main(void)
91{
92 xf_err_t xf_ret;
93 int flashdb_demo_ret;
94
95 /* 已初始化状态下无法注册 flash 设备和分区表 */
97
98 /*
99 如果 sc_default_flash_fdb_partition_table 内的 flash 设备不是已注册的设备,
100 需要先调用 xf_fal_register_flash_device() 注册 flash 设备。
101 本例中使用的是默认已注册的 flash 设备,无需再次注册。
102 */
103
104 /* 注册提供给 flashdb 使用的分区表 */
108 XF_CHECK(xf_ret != XF_OK, XF_RETURN_VOID, TAG, "An error occurred.");
109
110 xf_ret = xf_fal_init();
111 XF_CHECK(xf_ret != XF_OK, XF_RETURN_VOID, TAG, "An error occurred.");
112
114
115 flashdb_demo_ret = flashdb_demo();
116 XF_CHECK(flashdb_demo_ret != XF_OK, XF_RETURN_VOID, TAG, "An error occurred.");
117
118 XF_LOGI(TAG, "flashdb_demo done!");
119}
120
121/* ==================== [Static Functions] ================================== */
122
123static fdb_time_t get_time(void)
124{
125 /* Using the counts instead of timestamp.
126 * Please change this function to return RTC time.
127 */
128 return ++counts;
129}
130
131static int flashdb_demo(void)
132{
133 fdb_err_t fdb_ret;
134
135#ifdef FDB_USING_KVDB
136 { /* KVDB Sample */
137 struct fdb_default_kv default_kv;
138
139 default_kv.kvs = default_kv_table;
140 default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]);
141 /* set the lock and unlock function if you want */
142 fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_LOCK, lock);
143 fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_UNLOCK, unlock);
144 /* Key-Value database initialization
145 *
146 * &kvdb: database object
147 * "env": database name
148 * "fdb_kvdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table.
149 * Please change to YOUR partition name.
150 * &default_kv: The default KV nodes. It will auto add to KVDB when first initialize successfully.
151 * NULL: The user data if you need, now is empty.
152 */
153 fdb_ret = fdb_kvdb_init(&kvdb, "env", KVDB_NAME, &default_kv, NULL);
154
155 if (fdb_ret != FDB_NO_ERR) {
156 return -1;
157 }
158
159 /* run basic KV samples */
161 /* run string KV samples */
163 /* run blob KV samples */
165 }
166#endif /* FDB_USING_KVDB */
167
168#ifdef FDB_USING_TSDB
169 { /* TSDB Sample */
170 /* set the lock and unlock function if you want */
171 fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_LOCK, lock);
172 fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_UNLOCK, unlock);
173 /* Time series database initialization
174 *
175 * &tsdb: database object
176 * "log": database name
177 * "fdb_tsdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table.
178 * Please change to YOUR partition name.
179 * get_time: The get current timestamp function.
180 * 128: maximum length of each log
181 * NULL: The user data if you need, now is empty.
182 */
183 fdb_ret = fdb_tsdb_init(&tsdb, "log", TSDB_NAME, get_time, 128, NULL);
184 /* read last saved time for simulated timestamp */
185 fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
186
187 if (fdb_ret != FDB_NO_ERR) {
188 return -1;
189 }
190
191 /* run TSDB sample */
193 }
194#endif /* FDB_USING_TSDB */
195
196 return 0;
197}
198
199static void lock(fdb_db_t db)
200{
201 UNUSED(db);
202 if (s_mutex == NULL) {
203 xf_osal_mutex_attr_t mutex_attr = {
204 .name = "mutex",
205 };
206 s_mutex = xf_osal_mutex_create(&mutex_attr);
207 if (s_mutex == NULL) {
208 XF_LOGE(TAG, "xf mutex create error");
209 return;
210 }
211 }
213 return;
214}
215
216static void unlock(fdb_db_t db)
217{
218 UNUSED(db);
219 if (s_mutex != NULL) {
221 }
222 return;
223}
void xf_main(void)
Definition xf_main.c:28
xf_err_t xf_fal_deinit(void)
反初始化 FAL.
Definition xf_fal.c:293
xf_err_t xf_fal_init(void)
初始化 FAL.
Definition xf_fal.c:259
void xf_fal_show_part_table(void)
打印分区表信息。
Definition xf_fal.c:525
xf_err_t xf_fal_register_partition_table(const xf_fal_partition_t *p_table, size_t table_len)
注册一个分区表到 xf_fal 中。
Definition xf_fal.c:139
void * xf_osal_mutex_t
互斥锁句柄。
xf_err_t xf_osal_mutex_release(xf_osal_mutex_t mutex)
释放互斥锁。
xf_err_t xf_osal_mutex_acquire(xf_osal_mutex_t mutex, uint32_t timeout)
获取互斥锁。
xf_osal_mutex_t xf_osal_mutex_create(const xf_osal_mutex_attr_t *attr)
创建并初始化互斥锁。
#define XF_OSAL_WAIT_FOREVER
Definition xf_osal_def.h:30
#define XF_RETURN_VOID
Definition xf_check.h:47
#define XF_CHECK(condition, retval, tag, format,...)
xfusion 检查宏(条件 成立 时则输出日志后返回)。
Definition xf_check.h:112
int32_t xf_err_t
整形错误类型。 错误码具体值见 xf_err_code_t.
Definition xf_err.h:69
@ XF_OK
Definition xf_err.h:43
#define ARRAY_SIZE(arr)
ARRAY_SIZE - 获取数组 arr 中的元素数量。
Definition xf_predef.h:173
#define TAG
Definition xf_main.c:24
void kvdb_basic_sample(fdb_kvdb_t kvdb)
static void lock(fdb_db_t db)
Definition xf_main.c:199
static fdb_time_t get_time(void)
Definition xf_main.c:123
#define TSDB_NAME
Definition xf_main.c:29
struct fdb_tsdb tsdb
Definition xf_main.c:80
static int counts
Definition xf_main.c:82
void tsdb_sample(fdb_tsdb_t tsdb)
static int flashdb_demo(void)
Definition xf_main.c:131
void kvdb_type_string_sample(fdb_kvdb_t kvdb)
static void unlock(fdb_db_t db)
Definition xf_main.c:216
static uint32_t boot_count
Definition xf_main.c:68
static struct fdb_kvdb kvdb
Definition xf_main.c:78
static struct fdb_default_kv_node default_kv_table[]
Definition xf_main.c:71
void kvdb_type_blob_sample(fdb_kvdb_t kvdb)
static const xf_fal_partition_t sc_default_flash_fdb_partition_table[]
使用默认分区的空间注册一个提供给 FlashDB 使用的分区表。
Definition xf_main.c:53
static time_t boot_time[10]
Definition xf_main.c:69
#define KVDB_NAME
Definition xf_main.c:28
static xf_osal_mutex_t s_mutex
Definition xf_main.c:84
flash 分区结构体。
互斥锁的属性结构。
#define XF_LOGI(tag, format,...)
#define XF_LOGE(tag, format,...)
#define UNUSED(_x)