【Alios-things筆記】EMW3060 Flash使用者資料儲存KV
阿新 • • 發佈:2018-12-19
[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。