【Alios-things筆記】EMW3060 Flash用戶數據存儲KV

[KV介紹]
KV組件是AliOS Things中一個以Key-Value方式進行持久化存儲的輕量級組件,主要爲基於nor flash的小型MCU設備(Micro Control Unit)提供通用的Key-Value持久化存儲接口。KV組件支持寫平衡(磨損平衡)、掉電保護特性,且具有相當低的footprint。

Alios-things中的kv實現位於kernel/rhino/fs/kv/目錄下,通過查看kv.mk文件可以找到主要文件是kvmgr.c和kv_osal.c文件。
在EMW3060模塊的配置中,使用了KV組件,其描述信息位於platform/mcu/moc108/moc108.mk文件中

... ...
#啓用rhino.fs.kv組件
$(NAME)_COMPONENTS += libc rhino yloop rhino.fs.kv alicrypto digest_algorithm

# kv組件相關的配置
#使能hal_flash.c中關於kv組件的操作
GLOBAL_DEFINES += CONFIG_AOS_KV_MULTIPTN_MODE
#定義KV組件將數據存儲到FLASH的那個分區,這裏使用HAL_PARTITION_PARAMETER_2分區(具體的分區信息可以到board/mk3060/board.c文件中查看)
GLOBAL_DEFINES += CONFIG_AOS_KV_PTN=6
#第二分區
GLOBAL_DEFINES += CONFIG_AOS_KV_SECOND_PTN=7
#每個Flash塊的大小
GLOBAL_DEFINES += CONFIG_AOS_KV_PTN_SIZE=4096
#KV組件可操作的FLASH總大小
GLOBAL_DEFINES += CONFIG_AOS_KV_BUFFER_SIZE=8192
... ...

kv組件提供如下幾個接口

/**
 * Add a new KV pair.
 *
 * @param[in]  key    the key of the KV pair.
 * @param[in]  value  the value of the KV pair.
 * @param[in]  len    the length of the value.
 * @param[in]  sync   save the KV pair to flash right now (should always be 1).
 *
 * @return  0 on success, negative error on failure.
 */
int aos_kv_set(const char *key, const void *value, int len, int sync);

/**
 * Get the KV pair's value stored in buffer by its key.
 *
 * @note: the buffer_len should be larger than the real length of the value,
 *        otherwise buffer would be NULL.
 *
 * @param[in]      key         the key of the KV pair to get.
 * @param[out]     buffer      the memory to store the value.
 * @param[in-out]  buffer_len  in: the length of the input buffer.
 *                             out: the real length of the value.
 *
 * @return  0 on success, negative error on failure.
 */
int aos_kv_get(const char *key, void *buffer, int *buffer_len);

/**
 * Delete the KV pair by its key.
 *
 * @param[in]  key  the key of the KV pair to delete.
 *
 * @return  0 on success, negative error on failure.
 */
int aos_kv_del(const char *key);

/**
 - Delete the KV pair by its prefix.
 -  * @param[in]  prefix  the prefix of the kv pair which is need to delete.
 -  * @return  0 on success, negative error on failure.
 */
int aos_kv_del_by_prefix(const char *prefix);

注意事項:

  • 開發者需要實現相關flash hal層接口;
  • 開發者需通過在Makefile中聲明組件依賴關係$(NAME)_COMPONENTS += modules.fs.kv;
  • 開發者需通過CONFIG_AOS_KV_PTN宏定義指定KV組件所使用的flash分區號;
  • 若開發者所使用的flash介質的最小擦除單位大於4096 bytes,需調整KV組件內的邏輯塊大小(默認爲4096 bytes);
  • 開發者需通過CONFIG_AOS_KV_BUFFER_SIZE宏定義指定KV組件所使用的flash分區大小(不能小於2個kv組件的邏輯塊大小,默認值爲8192 bytes);
  • kv鍵值長度限制:
    最大鍵(key)長度小於255 bytes;
    最大值(value)長度可通過ITEM_MAX_VAL_LEN宏定義進行設置,預設值爲512 bytes。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章