diff --git a/storage/kvstore/include/kvstore/KVMap.h b/storage/kvstore/include/kvstore/KVMap.h deleted file mode 100644 index 0d030b9..0000000 --- a/storage/kvstore/include/kvstore/KVMap.h +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright (c) 2018 ARM Limited. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - * Licensed under the Apache License, Version 2.0 (the License); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef _KV_MAP -#define _KV_MAP - -#include "kvstore/KVStore.h" -#include "platform/PlatformMutex.h" -#include "platform/SingletonPtr.h" -#include "blockdevice/BlockDevice.h" -#include "filesystem/FileSystem.h" - -namespace mbed { - -#define MAX_ATTACHED_KVS 3 - -/** - * This structure represent a KVStore partition configuration - */ -typedef struct { - /** - * A Pointer to main instance of the KVStore partition. - * This is also the instance KVStore global API should work with. - * This must not be NULL in a working partition configuration. - */ - KVStore *kvstore_main_instance; - /** - * A pointer Internal store of the KVStore partition. - * If no rollback protection is required the pointer may be NULL. - */ - KVStore *internal_store; - /** - * A pointer external store of the KVStore partition. - * The pointer can be NULL if external store has been omitted - */ - KVStore *external_store; - /** - * A pointer Internal FlashIAP BlockDevice of the KVStore partition. - * The pointer can be NULL if internal store has been omitted - */ - BlockDevice *internal_bd; - /** - * A pointer external BlockDevice of the KVStore partition. - * The pointer can be NULL if external store has been omitted - */ - BlockDevice *external_bd; - /** - * A pointer external FileSystem of the KVStore partition. - * The pointer can be NULL if FileSystemStore has not been configured. - */ - FileSystem *external_fs; - /** - * This is a flag masking value for the KVStore global API. - * The Global API will mask the input flags base on this value to - * prevent errors in case the user choose an different security level. - */ - uint32_t flags_mask; -} kvstore_config_t; - -/** - * This structure maps between a string name and a partition configuration. - */ -typedef struct { - /** - * Partition name string - */ - char *partition_name; - /** - * Configuration struct. - */ - kvstore_config_t *kv_config; -} kv_map_entry_t; - -/** KVMap class - * - * Singleton class to manage the mapping of KVStore partition and its naming. - */ -class KVMap : private mbed::NonCopyable { -public: - - /** - * @brief As a singleton, return the single instance of the class. - * This class is a singleton for the following reasons: - * - Ease of use, so you don't have to coordinate instantiations. - * - Lazy instantiation of internal data, (which we can't achieve with simple static classes). - * - * @returns Singleton instance reference. - */ - static KVMap &get_instance() - { - // Use this implementation of singleton (Meyer's) rather than the one that allocates - // the instance on the heap because it ensures destruction at program end (preventing warnings - // from memory checking tools, such as valgrind). - static KVMap instance; - return instance; - } - - ~KVMap(); - - /** - * @brief Initializes KVMap - * - * @return 0 on success, negative error code on failure - */ - int init(); - - /** - * @brief Attach a KVStore partition configuration, and add it to the KVMap array - * - * @param partition_name String parameter contains the partition name. - * @param kv_config A configuration struct created by the kv_config or by the user. - * @return 0 on success, negative error code on failure - */ - int attach(const char *partition_name, kvstore_config_t *kv_config); - - /** - * @brief Detach a KVStore partition configuration from the KVMap array, - * and deinitialize its components - * - * @param partition_name String parameter contains the partition name. - * @return 0 on success, negative error code on failure - */ - int detach(const char *partition_name); - - /** - * @brief Deinitialize the KVMap array, and deinitialize all the attached partitions. - * - * @return 0 on success, negative error code on failure - */ - int deinit(); - - /** - * @brief Full name lookup, and then break it into KVStore instance and key - * - * @param[in] full_name String parameter contains the partition name to look for. - * The String should be formated as follow "/partition name/key". The key is optional. - * @param[out] kv_instance Returns the main KVStore instance associated with the required partition name. - * @param[out] key_index Returns an index to the first character of the key. - * @param[out] flags_mask Return the flag masking for the current configuration - * @return 0 on success, negative error code on failure - */ - int lookup(const char *full_name, mbed::KVStore **kv_instance, size_t *key_index, uint32_t *flags_mask = NULL); - - /** - * @brief Getter for the internal KVStore instance. - * - * @param name String parameter contains the /partition name/. - * - * @return Pointer to the internal kvstore on success, - * NULL on failure or if not exist - */ - KVStore *get_internal_kv_instance(const char *name); - /** - * @brief Getter for the external KVStore instance. - * - * @param name String parameter contains the /partition name/. - * - * @return Pointer to the external kvstore on success, - * NULL on failure or if not exist - */ - KVStore *get_external_kv_instance(const char *name); - /** - * @brief Getter for the main KVStore instance. - * - * @param name String parameter contains the /partition name/. - * - * @return Pointer to the main kvstore on success, - * NULL on failure or if not exist - */ - KVStore *get_main_kv_instance(const char *name); - /** - * @brief Getter for the internal BlockDevice instance. - * - * @param name String parameter contains the /partition name/. - * - * @return Pointer to the internal BlockDevice on success, - * NULL on failure or if not exist - */ - BlockDevice *get_internal_blockdevice_instance(const char *name); - /** - * @brief Getter for the external BlockDevice instance. - * - * @param name String parameter contains the /partition name/. - * - * @return Pointer to the external BlockDevice on success, - * NULL on failure or if not exist - */ - BlockDevice *get_external_blockdevice_instance(const char *name); - /** - * @brief Getter for the external FileSystem instance. - * - * @param name String parameter contains the /partition name/. - * - * @return Pointer to the external FileSystem on success, - * NULL on failure or if not exist - */ - FileSystem *get_external_filesystem_instance(const char *name); - -#if !defined(DOXYGEN_ONLY) -private: - - /** - * @brief Deinitialize all components of a partition configuration struct. - * - * @param partition Partition configuration struct. - */ - void deinit_partition(kv_map_entry_t *partition); - - /** - * @brief Full name lookup, and then break it into KVStore config and key - * - * @param[in] full_name String parameter contains the /partition name/key. - * @param[out] kv_config Returns The configuration struct associated with the partition name - * @param[out] key_index Returns an index to the first character of the key. - * @return 0 on success, negative error code on failure - */ - int config_lookup(const char *full_name, kvstore_config_t **kv_config, size_t *key_index); - - // Attachment table - kv_map_entry_t _kv_map_table[MAX_ATTACHED_KVS]; - int _kv_num_attached_kvs; - int _is_initialized; - SingletonPtr _mutex; -#endif -}; -} -#endif diff --git a/storage/kvstore/include/kvstore/kvstore_global_api.h b/storage/kvstore/include/kvstore/kvstore_global_api.h deleted file mode 100644 index 12cfe06..0000000 --- a/storage/kvstore/include/kvstore/kvstore_global_api.h +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (c) 2018 ARM Limited. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - * Licensed under the Apache License, Version 2.0 (the License); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef _KVSTORE_STATIC_API -#define _KVSTORE_STATIC_API - -#include "stddef.h" -#include "stdint.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct _opaque_kv_key_iterator *kv_iterator_t; - -#define KV_WRITE_ONCE_FLAG (1 << 0) -#define KV_REQUIRE_CONFIDENTIALITY_FLAG (1 << 1) -#define KV_RESERVED_FLAG (1 << 2) -#define KV_REQUIRE_REPLAY_PROTECTION_FLAG (1 << 3) - -#define KV_MAX_KEY_LENGTH 128 - -/** - * The key size - */ -typedef struct info { - /** - * The key size - */ - size_t size; - /* - * The Key flags, possible flags combination: - * WRITE_ONCE_FLAG, - * REQUIRE_CONFIDENTIALITY_FLAG, - * REQUIRE_REPLAY_PROTECTION_FLAG - */ - uint32_t flags; -} kv_info_t; - -/** - * @brief Set one KVStore item, given key and value. - * - * @param[in] full_name_key /Partition_path/Key. Must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'. - * @param[in] buffer Value data buffer. - * @param[in] size Value data size. - * @param[in] create_flags Flag mask. - * - * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances - */ -int kv_set(const char *full_name_key, const void *buffer, size_t size, uint32_t create_flags); - -/** - * @brief Get one KVStore item by given key. - * - * @param[in] full_name_key /Partition_path/Key. Must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'. - * @param[in] buffer Value data buffer. - * @param[in] buffer_size Value data buffer size. - * @param[out] actual_size Actual read size. - * - * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances - */ -int kv_get(const char *full_name_key, void *buffer, size_t buffer_size, size_t *actual_size); - -/** - * @brief Get information of a given key.The returned info contains size and flags - * - * @param[in] full_name_key /Partition_path/Key. Must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'. - * @param[out] info Returned information structure. - * - * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances - */ -int kv_get_info(const char *full_name_key, kv_info_t *info); - -/** - * @brief Remove a KVStore item by given key. - * - * @param[in] full_name_key /Partition_path/Key. Must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'. - * - * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances - */ -int kv_remove(const char *full_name_key); - -/** - * @brief Start an iteration over KVStore keys to find all the entries - * that fit the full_prefix. There are no issues with any other operations while - * iterator is open. - * - * @param[out] it Allocating iterator handle. - * Do not forget to call kv_iterator_close - * to deallocate the memory. - * @param[in] full_prefix full_prefix Partition/Key prefix. If - * empty key or NULL pointer, all keys - * will match. - * - * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances - */ -int kv_iterator_open(kv_iterator_t *it, const char *full_prefix); - -/** - * @brief Get next key in iteration that matches the prefix. There are no issues with any - * other operations while iterator is open. - * - * @param[in] it Iterator handle. - * @param[in] key Buffer for returned key. - * @param[in] key_size Key buffer size. - * - * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances - */ -int kv_iterator_next(kv_iterator_t it, char *key, size_t key_size); - -/** - * @brief Close iteration and deallocate the iterator handle. - * - * @param[in] it Iterator handle. - * - * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances - */ -int kv_iterator_close(kv_iterator_t it); - -/** - * @brief Remove all keys and related data from a specified partition. - * - * @param[in] kvstore_path /Partition/ - * - * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances - */ -int kv_reset(const char *kvstore_path); - -#ifdef __cplusplus -} // closing brace for extern "C" -#endif -#endif diff --git a/storage/kvstore/kvstore_global_api/include/kvstore_global_api/KVMap.h b/storage/kvstore/kvstore_global_api/include/kvstore_global_api/KVMap.h new file mode 100644 index 0000000..0d030b9 --- /dev/null +++ b/storage/kvstore/kvstore_global_api/include/kvstore_global_api/KVMap.h @@ -0,0 +1,239 @@ +/* + * Copyright (c) 2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _KV_MAP +#define _KV_MAP + +#include "kvstore/KVStore.h" +#include "platform/PlatformMutex.h" +#include "platform/SingletonPtr.h" +#include "blockdevice/BlockDevice.h" +#include "filesystem/FileSystem.h" + +namespace mbed { + +#define MAX_ATTACHED_KVS 3 + +/** + * This structure represent a KVStore partition configuration + */ +typedef struct { + /** + * A Pointer to main instance of the KVStore partition. + * This is also the instance KVStore global API should work with. + * This must not be NULL in a working partition configuration. + */ + KVStore *kvstore_main_instance; + /** + * A pointer Internal store of the KVStore partition. + * If no rollback protection is required the pointer may be NULL. + */ + KVStore *internal_store; + /** + * A pointer external store of the KVStore partition. + * The pointer can be NULL if external store has been omitted + */ + KVStore *external_store; + /** + * A pointer Internal FlashIAP BlockDevice of the KVStore partition. + * The pointer can be NULL if internal store has been omitted + */ + BlockDevice *internal_bd; + /** + * A pointer external BlockDevice of the KVStore partition. + * The pointer can be NULL if external store has been omitted + */ + BlockDevice *external_bd; + /** + * A pointer external FileSystem of the KVStore partition. + * The pointer can be NULL if FileSystemStore has not been configured. + */ + FileSystem *external_fs; + /** + * This is a flag masking value for the KVStore global API. + * The Global API will mask the input flags base on this value to + * prevent errors in case the user choose an different security level. + */ + uint32_t flags_mask; +} kvstore_config_t; + +/** + * This structure maps between a string name and a partition configuration. + */ +typedef struct { + /** + * Partition name string + */ + char *partition_name; + /** + * Configuration struct. + */ + kvstore_config_t *kv_config; +} kv_map_entry_t; + +/** KVMap class + * + * Singleton class to manage the mapping of KVStore partition and its naming. + */ +class KVMap : private mbed::NonCopyable { +public: + + /** + * @brief As a singleton, return the single instance of the class. + * This class is a singleton for the following reasons: + * - Ease of use, so you don't have to coordinate instantiations. + * - Lazy instantiation of internal data, (which we can't achieve with simple static classes). + * + * @returns Singleton instance reference. + */ + static KVMap &get_instance() + { + // Use this implementation of singleton (Meyer's) rather than the one that allocates + // the instance on the heap because it ensures destruction at program end (preventing warnings + // from memory checking tools, such as valgrind). + static KVMap instance; + return instance; + } + + ~KVMap(); + + /** + * @brief Initializes KVMap + * + * @return 0 on success, negative error code on failure + */ + int init(); + + /** + * @brief Attach a KVStore partition configuration, and add it to the KVMap array + * + * @param partition_name String parameter contains the partition name. + * @param kv_config A configuration struct created by the kv_config or by the user. + * @return 0 on success, negative error code on failure + */ + int attach(const char *partition_name, kvstore_config_t *kv_config); + + /** + * @brief Detach a KVStore partition configuration from the KVMap array, + * and deinitialize its components + * + * @param partition_name String parameter contains the partition name. + * @return 0 on success, negative error code on failure + */ + int detach(const char *partition_name); + + /** + * @brief Deinitialize the KVMap array, and deinitialize all the attached partitions. + * + * @return 0 on success, negative error code on failure + */ + int deinit(); + + /** + * @brief Full name lookup, and then break it into KVStore instance and key + * + * @param[in] full_name String parameter contains the partition name to look for. + * The String should be formated as follow "/partition name/key". The key is optional. + * @param[out] kv_instance Returns the main KVStore instance associated with the required partition name. + * @param[out] key_index Returns an index to the first character of the key. + * @param[out] flags_mask Return the flag masking for the current configuration + * @return 0 on success, negative error code on failure + */ + int lookup(const char *full_name, mbed::KVStore **kv_instance, size_t *key_index, uint32_t *flags_mask = NULL); + + /** + * @brief Getter for the internal KVStore instance. + * + * @param name String parameter contains the /partition name/. + * + * @return Pointer to the internal kvstore on success, + * NULL on failure or if not exist + */ + KVStore *get_internal_kv_instance(const char *name); + /** + * @brief Getter for the external KVStore instance. + * + * @param name String parameter contains the /partition name/. + * + * @return Pointer to the external kvstore on success, + * NULL on failure or if not exist + */ + KVStore *get_external_kv_instance(const char *name); + /** + * @brief Getter for the main KVStore instance. + * + * @param name String parameter contains the /partition name/. + * + * @return Pointer to the main kvstore on success, + * NULL on failure or if not exist + */ + KVStore *get_main_kv_instance(const char *name); + /** + * @brief Getter for the internal BlockDevice instance. + * + * @param name String parameter contains the /partition name/. + * + * @return Pointer to the internal BlockDevice on success, + * NULL on failure or if not exist + */ + BlockDevice *get_internal_blockdevice_instance(const char *name); + /** + * @brief Getter for the external BlockDevice instance. + * + * @param name String parameter contains the /partition name/. + * + * @return Pointer to the external BlockDevice on success, + * NULL on failure or if not exist + */ + BlockDevice *get_external_blockdevice_instance(const char *name); + /** + * @brief Getter for the external FileSystem instance. + * + * @param name String parameter contains the /partition name/. + * + * @return Pointer to the external FileSystem on success, + * NULL on failure or if not exist + */ + FileSystem *get_external_filesystem_instance(const char *name); + +#if !defined(DOXYGEN_ONLY) +private: + + /** + * @brief Deinitialize all components of a partition configuration struct. + * + * @param partition Partition configuration struct. + */ + void deinit_partition(kv_map_entry_t *partition); + + /** + * @brief Full name lookup, and then break it into KVStore config and key + * + * @param[in] full_name String parameter contains the /partition name/key. + * @param[out] kv_config Returns The configuration struct associated with the partition name + * @param[out] key_index Returns an index to the first character of the key. + * @return 0 on success, negative error code on failure + */ + int config_lookup(const char *full_name, kvstore_config_t **kv_config, size_t *key_index); + + // Attachment table + kv_map_entry_t _kv_map_table[MAX_ATTACHED_KVS]; + int _kv_num_attached_kvs; + int _is_initialized; + SingletonPtr _mutex; +#endif +}; +} +#endif diff --git a/storage/kvstore/kvstore_global_api/include/kvstore_global_api/kvstore_global_api.h b/storage/kvstore/kvstore_global_api/include/kvstore_global_api/kvstore_global_api.h new file mode 100644 index 0000000..12cfe06 --- /dev/null +++ b/storage/kvstore/kvstore_global_api/include/kvstore_global_api/kvstore_global_api.h @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _KVSTORE_STATIC_API +#define _KVSTORE_STATIC_API + +#include "stddef.h" +#include "stdint.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct _opaque_kv_key_iterator *kv_iterator_t; + +#define KV_WRITE_ONCE_FLAG (1 << 0) +#define KV_REQUIRE_CONFIDENTIALITY_FLAG (1 << 1) +#define KV_RESERVED_FLAG (1 << 2) +#define KV_REQUIRE_REPLAY_PROTECTION_FLAG (1 << 3) + +#define KV_MAX_KEY_LENGTH 128 + +/** + * The key size + */ +typedef struct info { + /** + * The key size + */ + size_t size; + /* + * The Key flags, possible flags combination: + * WRITE_ONCE_FLAG, + * REQUIRE_CONFIDENTIALITY_FLAG, + * REQUIRE_REPLAY_PROTECTION_FLAG + */ + uint32_t flags; +} kv_info_t; + +/** + * @brief Set one KVStore item, given key and value. + * + * @param[in] full_name_key /Partition_path/Key. Must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'. + * @param[in] buffer Value data buffer. + * @param[in] size Value data size. + * @param[in] create_flags Flag mask. + * + * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances + */ +int kv_set(const char *full_name_key, const void *buffer, size_t size, uint32_t create_flags); + +/** + * @brief Get one KVStore item by given key. + * + * @param[in] full_name_key /Partition_path/Key. Must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'. + * @param[in] buffer Value data buffer. + * @param[in] buffer_size Value data buffer size. + * @param[out] actual_size Actual read size. + * + * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances + */ +int kv_get(const char *full_name_key, void *buffer, size_t buffer_size, size_t *actual_size); + +/** + * @brief Get information of a given key.The returned info contains size and flags + * + * @param[in] full_name_key /Partition_path/Key. Must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'. + * @param[out] info Returned information structure. + * + * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances + */ +int kv_get_info(const char *full_name_key, kv_info_t *info); + +/** + * @brief Remove a KVStore item by given key. + * + * @param[in] full_name_key /Partition_path/Key. Must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'. + * + * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances + */ +int kv_remove(const char *full_name_key); + +/** + * @brief Start an iteration over KVStore keys to find all the entries + * that fit the full_prefix. There are no issues with any other operations while + * iterator is open. + * + * @param[out] it Allocating iterator handle. + * Do not forget to call kv_iterator_close + * to deallocate the memory. + * @param[in] full_prefix full_prefix Partition/Key prefix. If + * empty key or NULL pointer, all keys + * will match. + * + * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances + */ +int kv_iterator_open(kv_iterator_t *it, const char *full_prefix); + +/** + * @brief Get next key in iteration that matches the prefix. There are no issues with any + * other operations while iterator is open. + * + * @param[in] it Iterator handle. + * @param[in] key Buffer for returned key. + * @param[in] key_size Key buffer size. + * + * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances + */ +int kv_iterator_next(kv_iterator_t it, char *key, size_t key_size); + +/** + * @brief Close iteration and deallocate the iterator handle. + * + * @param[in] it Iterator handle. + * + * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances + */ +int kv_iterator_close(kv_iterator_t it); + +/** + * @brief Remove all keys and related data from a specified partition. + * + * @param[in] kvstore_path /Partition/ + * + * @returns MBED_SUCCESS on success or an error code from underlying KVStore instances + */ +int kv_reset(const char *kvstore_path); + +#ifdef __cplusplus +} // closing brace for extern "C" +#endif +#endif diff --git a/storage/kvstore/kvstore_global_api/mbed_lib.json b/storage/kvstore/kvstore_global_api/mbed_lib.json new file mode 100644 index 0000000..7facc6b --- /dev/null +++ b/storage/kvstore/kvstore_global_api/mbed_lib.json @@ -0,0 +1,3 @@ +{ + "name": "kv-global-api" +} diff --git a/storage/kvstore/kvstore_global_api/source/KVMap.cpp b/storage/kvstore/kvstore_global_api/source/KVMap.cpp new file mode 100644 index 0000000..e0617ea --- /dev/null +++ b/storage/kvstore/kvstore_global_api/source/KVMap.cpp @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "kvstore/KVStore.h" +#include "kvstore_global_api/KVMap.h" +#include "kv_config/kv_config.h" +#include +#include "string.h" +#include "mbed_error.h" + +namespace mbed { + +KVMap::~KVMap() +{ + deinit(); +} + +int KVMap::init() +{ + int ret = MBED_SUCCESS; + + _mutex->lock(); + + if (_is_initialized) { + goto exit; + } + + _kv_num_attached_kvs = 0; + memset(&_kv_map_table, 0, sizeof(_kv_map_table)); + + _is_initialized = 1; + +exit: + _mutex->unlock(); + return ret; +} + +int KVMap::attach(const char *partition_name, kvstore_config_t *kv_config) +{ + int ret = MBED_SUCCESS; + char *kv_partition_name = NULL; + + _mutex->lock(); + + if (!_is_initialized) { + ret = MBED_ERROR_NOT_READY; + goto exit; + } + + if (_kv_num_attached_kvs >= MAX_ATTACHED_KVS) { + ret = MBED_ERROR_OUT_OF_MEMORY; + goto exit; + } + + kv_partition_name = new char[strlen(partition_name) + 1]; + strcpy(kv_partition_name, partition_name); + _kv_map_table[_kv_num_attached_kvs].partition_name = kv_partition_name; + _kv_map_table[_kv_num_attached_kvs].kv_config = kv_config; + _kv_num_attached_kvs++; + +exit: + _mutex->unlock(); + return ret; +} + +void KVMap::deinit_partition(kv_map_entry_t *partition) +{ + if (partition->kv_config == NULL) { + return; + } + + if (partition->kv_config->external_store != NULL) { + partition->kv_config->external_store->deinit(); + } + + // TODO: this should be removed after FS APIs are standardized + if (partition->kv_config->external_fs != NULL) { + partition->kv_config->external_fs->unmount(); + } + + if (partition->kv_config->internal_store != NULL) { + partition->kv_config->internal_store->deinit(); + } + + if (partition->kv_config->kvstore_main_instance != NULL) { + partition->kv_config->kvstore_main_instance->deinit(); + } + + delete [] partition->partition_name; + partition->partition_name = NULL; + partition->kv_config = NULL; +} + + +int KVMap::detach(const char *partition_name) +{ + int ret = MBED_SUCCESS; + + _mutex->lock(); + + if (!_is_initialized) { + ret = MBED_ERROR_NOT_READY; + goto exit; + } + + ret = MBED_ERROR_ITEM_NOT_FOUND; + for (int i = 0; i < _kv_num_attached_kvs; i++) { + + if (strcmp(partition_name, _kv_map_table[i].partition_name) != 0) { + continue; + } + + deinit_partition(&_kv_map_table[i]); + + memcpy(&_kv_map_table[i], &_kv_map_table[i + 1], sizeof(kv_map_entry_t) * (MAX_ATTACHED_KVS - i - 1)); + _kv_map_table[MAX_ATTACHED_KVS - 1].partition_name = NULL; + _kv_map_table[MAX_ATTACHED_KVS - 1].kv_config->kvstore_main_instance = NULL; + _kv_num_attached_kvs--; + ret = MBED_SUCCESS; + break; + } + +exit: + _mutex->unlock(); + return ret; +} + +int KVMap::deinit() +{ + int ret = MBED_SUCCESS; + + _mutex->lock(); + + if (!_is_initialized) { + ret = MBED_ERROR_NOT_READY; + goto exit; + } + + for (int i = 0; i < _kv_num_attached_kvs; i++) { + + if (_kv_map_table[i].kv_config->kvstore_main_instance == NULL) { + goto exit; + } + + deinit_partition(&_kv_map_table[i]); + } + +exit: + _kv_num_attached_kvs = 0; + _mutex->unlock(); + return ret; +} + +// Full name lookup and then break it into KVStore instance and key +int KVMap::lookup(const char *full_name, KVStore **kv_instance, size_t *key_index, uint32_t *flags_mask) +{ + _mutex->lock(); + + kvstore_config_t *kv_config; + int ret = config_lookup(full_name, &kv_config, key_index); + if (ret != MBED_SUCCESS) { + goto exit; + } + + *kv_instance = kv_config->kvstore_main_instance; + if (flags_mask != NULL) { + *flags_mask = kv_config->flags_mask; + } + +exit: + _mutex->unlock(); + return ret; +} + +// Full name lookup and then break it into KVStore configuration struct and key +int KVMap::config_lookup(const char *full_name, kvstore_config_t **kv_config, size_t *key_index) +{ + int ret = MBED_SUCCESS; + int delimiter_index; + int i; + const char *delimiter_position; + + const char *temp_str = full_name; + + if (!_is_initialized) { + ret = MBED_ERROR_NOT_READY; + goto exit; + } + + if (temp_str != NULL) { + *key_index = 0; + if (*temp_str == '/') { + temp_str++; + (*key_index)++; + } + + delimiter_position = strchr(temp_str, '/'); + if (delimiter_position == NULL) { //delimiter not found + delimiter_index = -1; + *kv_config = _kv_map_table[0].kv_config; + goto exit; + } + } else { + delimiter_index = -1; + *kv_config = _kv_map_table[0].kv_config; + goto exit; + } + + + delimiter_index = delimiter_position - temp_str; + for (i = 0; i < _kv_num_attached_kvs; i++) { + + if (strncmp(temp_str, _kv_map_table[i].partition_name, delimiter_index) != 0) { + continue; + } + + *kv_config = _kv_map_table[i].kv_config; + break; + } + if (i == _kv_num_attached_kvs) { + ret = MBED_ERROR_ITEM_NOT_FOUND; + goto exit; + } +exit: + if (ret == MBED_SUCCESS) { + //if success extract the key + *key_index = *key_index + delimiter_index + 1; + } + return ret; +} + +KVStore *KVMap::get_internal_kv_instance(const char *name) +{ + + _mutex->lock(); + + kvstore_config_t *kv_config; + size_t key_index = 0; + + int ret = config_lookup(name, &kv_config, &key_index); + + _mutex->unlock(); + + return ret != MBED_SUCCESS ? NULL : kv_config->internal_store; +} + +KVStore *KVMap::get_external_kv_instance(const char *name) +{ + + _mutex->lock(); + + kvstore_config_t *kv_config; + size_t key_index = 0; + + int ret = config_lookup(name, &kv_config, &key_index); + + _mutex->unlock(); + + return ret != MBED_SUCCESS ? NULL : kv_config->external_store; +} + +KVStore *KVMap::get_main_kv_instance(const char *name) +{ + + _mutex->lock(); + + kvstore_config_t *kv_config; + size_t key_index = 0; + + int ret = config_lookup(name, &kv_config, &key_index); + + _mutex->unlock(); + + return ret != MBED_SUCCESS ? NULL : kv_config->kvstore_main_instance; +} + +BlockDevice *KVMap::get_internal_blockdevice_instance(const char *name) +{ + + _mutex->lock(); + + kvstore_config_t *kv_config; + size_t key_index = 0; + + int ret = config_lookup(name, &kv_config, &key_index); + + _mutex->unlock(); + + return ret != MBED_SUCCESS ? NULL : kv_config->internal_bd; +} + +BlockDevice *KVMap::get_external_blockdevice_instance(const char *name) +{ + + _mutex->lock(); + + kvstore_config_t *kv_config; + size_t key_index = 0; + + int ret = config_lookup(name, &kv_config, &key_index); + + _mutex->unlock(); + + return ret != MBED_SUCCESS ? NULL : kv_config->external_bd; +} + +FileSystem *KVMap::get_external_filesystem_instance(const char *name) +{ + + _mutex->lock(); + + kvstore_config_t *kv_config; + size_t key_index = 0; + + int ret = config_lookup(name, &kv_config, &key_index); + + _mutex->unlock(); + + return ret != MBED_SUCCESS ? NULL : kv_config->external_fs; +} + +} // namespace mbed diff --git a/storage/kvstore/kvstore_global_api/source/kvstore_global_api.cpp b/storage/kvstore/kvstore_global_api/source/kvstore_global_api.cpp new file mode 100644 index 0000000..828983f --- /dev/null +++ b/storage/kvstore/kvstore_global_api/source/kvstore_global_api.cpp @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "kvstore_global_api/kvstore_global_api.h" + +#include "kv_config/kv_config.h" +#include "kvstore_global_api/KVMap.h" +#include "kvstore/KVStore.h" +#include "mbed_error.h" + +using namespace mbed; + +// iterator handle +struct _opaque_kv_key_iterator { + bool iterator_is_open; + KVStore *kvstore_intance; + KVStore::iterator_t *iterator_handle; + char *path; +}; + +int kv_set(const char *full_name_key, const void *buffer, size_t size, uint32_t create_flags) +{ + int ret = kv_init_storage_config(); + if (MBED_SUCCESS != ret) { + return ret; + } + + KVMap &kv_map = KVMap::get_instance(); + KVStore *kv_instance = NULL; + uint32_t flags_mask = 0; + size_t key_index = 0; + ret = kv_map.lookup(full_name_key, &kv_instance, &key_index, &flags_mask); + if (ret != MBED_SUCCESS) { + return ret; + } + + ret = kv_instance->set(full_name_key + key_index, buffer, size, create_flags & flags_mask); + return ret; +} + +int kv_get(const char *full_name_key, void *buffer, size_t buffer_size, size_t *actual_size) +{ + int ret = kv_init_storage_config(); + if (MBED_SUCCESS != ret) { + return ret; + } + + KVMap &kv_map = KVMap::get_instance(); + KVStore *kv_instance = NULL; + size_t key_index = 0; + ret = kv_map.lookup(full_name_key, &kv_instance, &key_index); + if (ret != MBED_SUCCESS) { + return ret; + } + + return kv_instance->get(full_name_key + key_index, buffer, buffer_size, actual_size); +} + +int kv_get_info(const char *full_name_key, kv_info_t *info) +{ + int ret = kv_init_storage_config(); + if (MBED_SUCCESS != ret) { + return ret; + } + + KVMap &kv_map = KVMap::get_instance(); + KVStore *kv_instance = NULL; + size_t key_index = 0; + ret = kv_map.lookup(full_name_key, &kv_instance, &key_index); + if (ret != MBED_SUCCESS) { + return ret; + } + + KVStore::info_t inner_info; + ret = kv_instance->get_info(full_name_key + key_index, &inner_info); + if (MBED_SUCCESS != ret) { + return ret; + } + info->flags = inner_info.flags; + info->size = inner_info.size; + return ret; +} + +int kv_remove(const char *full_name_key) +{ + int ret = kv_init_storage_config(); + if (MBED_SUCCESS != ret) { + return ret; + } + + KVMap &kv_map = KVMap::get_instance(); + KVStore *kv_instance = NULL; + size_t key_index = 0; + ret = kv_map.lookup(full_name_key, &kv_instance, &key_index); + if (ret != MBED_SUCCESS) { + return ret; + } + + return kv_instance->remove(full_name_key + key_index); +} + +int kv_iterator_open(kv_iterator_t *it, const char *full_prefix) +{ + if (it == NULL) { + return MBED_ERROR_INVALID_ARGUMENT; + } + + int ret = kv_init_storage_config(); + if (MBED_SUCCESS != ret) { + return ret; + } + + (*it) = new _opaque_kv_key_iterator; + if (*it == NULL) { + return MBED_ERROR_FAILED_OPERATION; + } + (*it)->iterator_is_open = false; + + KVMap &kv_map = KVMap::get_instance(); + KVStore *kv_instance = NULL; + size_t key_index = 0; + ret = kv_map.lookup(full_prefix, &kv_instance, &key_index); + if (ret != MBED_SUCCESS) { + delete (*it); + return ret; + } + + (*it)->kvstore_intance = kv_instance; + KVStore::iterator_t *inner_it = new KVStore::iterator_t; + ret = kv_instance->iterator_open(inner_it, full_prefix + key_index); + if (MBED_SUCCESS != ret) { + delete inner_it; + delete (*it); + return ret; + } + + (*it)->iterator_handle = inner_it; + (*it)->iterator_is_open = true; + (*it)->path = new char[key_index + 1]; + strncpy((*it)->path, full_prefix, key_index); + (*it)->path[key_index] = '\0'; + return ret; + +} + +int kv_iterator_next(kv_iterator_t it, char *key, size_t key_size) +{ + if (!it->iterator_is_open) { + return MBED_ERROR_INVALID_ARGUMENT; + } + + strcpy(key, it->path); + int path_len = strlen(key); + return it->kvstore_intance->iterator_next(*it->iterator_handle, key + path_len, key_size - path_len); +} + +int kv_iterator_close(kv_iterator_t it) +{ + if (!it->iterator_is_open) { + return MBED_ERROR_INVALID_ARGUMENT; + } + + int ret = it->kvstore_intance->iterator_close(*it->iterator_handle); + + delete it->iterator_handle; + delete[] it->path; + delete it; + + return ret; +} + +int kv_reset(const char *kvstore_name) +{ + int ret = kv_init_storage_config(); + if (MBED_SUCCESS != ret) { + return ret; + } + + KVMap &kv_map = KVMap::get_instance(); + KVStore *kv_instance = NULL; + size_t key_index = 0; + ret = kv_map.lookup(kvstore_name, &kv_instance, &key_index); + if (ret != MBED_SUCCESS) { + return ret; + } + + ret = kv_instance->reset(); + + return ret; + +} diff --git a/storage/kvstore/mbed_lib.json b/storage/kvstore/mbed_lib.json index 7facc6b..4c2e065 100644 --- a/storage/kvstore/mbed_lib.json +++ b/storage/kvstore/mbed_lib.json @@ -1,3 +1,3 @@ { - "name": "kv-global-api" + "name": "kvstore" } diff --git a/storage/kvstore/source/KVMap.cpp b/storage/kvstore/source/KVMap.cpp deleted file mode 100644 index 506f0fb..0000000 --- a/storage/kvstore/source/KVMap.cpp +++ /dev/null @@ -1,335 +0,0 @@ -/* - * Copyright (c) 2018 ARM Limited. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - * Licensed under the Apache License, Version 2.0 (the License); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "kvstore/KVStore.h" -#include "kvstore/KVMap.h" -#include "kv_config/kv_config.h" -#include -#include "string.h" -#include "mbed_error.h" - -namespace mbed { - -KVMap::~KVMap() -{ - deinit(); -} - -int KVMap::init() -{ - int ret = MBED_SUCCESS; - - _mutex->lock(); - - if (_is_initialized) { - goto exit; - } - - _kv_num_attached_kvs = 0; - memset(&_kv_map_table, 0, sizeof(_kv_map_table)); - - _is_initialized = 1; - -exit: - _mutex->unlock(); - return ret; -} - -int KVMap::attach(const char *partition_name, kvstore_config_t *kv_config) -{ - int ret = MBED_SUCCESS; - char *kv_partition_name = NULL; - - _mutex->lock(); - - if (!_is_initialized) { - ret = MBED_ERROR_NOT_READY; - goto exit; - } - - if (_kv_num_attached_kvs >= MAX_ATTACHED_KVS) { - ret = MBED_ERROR_OUT_OF_MEMORY; - goto exit; - } - - kv_partition_name = new char[strlen(partition_name) + 1]; - strcpy(kv_partition_name, partition_name); - _kv_map_table[_kv_num_attached_kvs].partition_name = kv_partition_name; - _kv_map_table[_kv_num_attached_kvs].kv_config = kv_config; - _kv_num_attached_kvs++; - -exit: - _mutex->unlock(); - return ret; -} - -void KVMap::deinit_partition(kv_map_entry_t *partition) -{ - if (partition->kv_config == NULL) { - return; - } - - if (partition->kv_config->external_store != NULL) { - partition->kv_config->external_store->deinit(); - } - - // TODO: this should be removed after FS APIs are standardized - if (partition->kv_config->external_fs != NULL) { - partition->kv_config->external_fs->unmount(); - } - - if (partition->kv_config->internal_store != NULL) { - partition->kv_config->internal_store->deinit(); - } - - if (partition->kv_config->kvstore_main_instance != NULL) { - partition->kv_config->kvstore_main_instance->deinit(); - } - - delete [] partition->partition_name; - partition->partition_name = NULL; - partition->kv_config = NULL; -} - - -int KVMap::detach(const char *partition_name) -{ - int ret = MBED_SUCCESS; - - _mutex->lock(); - - if (!_is_initialized) { - ret = MBED_ERROR_NOT_READY; - goto exit; - } - - ret = MBED_ERROR_ITEM_NOT_FOUND; - for (int i = 0; i < _kv_num_attached_kvs; i++) { - - if (strcmp(partition_name, _kv_map_table[i].partition_name) != 0) { - continue; - } - - deinit_partition(&_kv_map_table[i]); - - memcpy(&_kv_map_table[i], &_kv_map_table[i + 1], sizeof(kv_map_entry_t) * (MAX_ATTACHED_KVS - i - 1)); - _kv_map_table[MAX_ATTACHED_KVS - 1].partition_name = NULL; - _kv_map_table[MAX_ATTACHED_KVS - 1].kv_config->kvstore_main_instance = NULL; - _kv_num_attached_kvs--; - ret = MBED_SUCCESS; - break; - } - -exit: - _mutex->unlock(); - return ret; -} - -int KVMap::deinit() -{ - int ret = MBED_SUCCESS; - - _mutex->lock(); - - if (!_is_initialized) { - ret = MBED_ERROR_NOT_READY; - goto exit; - } - - for (int i = 0; i < _kv_num_attached_kvs; i++) { - - if (_kv_map_table[i].kv_config->kvstore_main_instance == NULL) { - goto exit; - } - - deinit_partition(&_kv_map_table[i]); - } - -exit: - _kv_num_attached_kvs = 0; - _mutex->unlock(); - return ret; -} - -// Full name lookup and then break it into KVStore instance and key -int KVMap::lookup(const char *full_name, KVStore **kv_instance, size_t *key_index, uint32_t *flags_mask) -{ - _mutex->lock(); - - kvstore_config_t *kv_config; - int ret = config_lookup(full_name, &kv_config, key_index); - if (ret != MBED_SUCCESS) { - goto exit; - } - - *kv_instance = kv_config->kvstore_main_instance; - if (flags_mask != NULL) { - *flags_mask = kv_config->flags_mask; - } - -exit: - _mutex->unlock(); - return ret; -} - -// Full name lookup and then break it into KVStore configuration struct and key -int KVMap::config_lookup(const char *full_name, kvstore_config_t **kv_config, size_t *key_index) -{ - int ret = MBED_SUCCESS; - int delimiter_index; - int i; - const char *delimiter_position; - - const char *temp_str = full_name; - - if (!_is_initialized) { - ret = MBED_ERROR_NOT_READY; - goto exit; - } - - if (temp_str != NULL) { - *key_index = 0; - if (*temp_str == '/') { - temp_str++; - (*key_index)++; - } - - delimiter_position = strchr(temp_str, '/'); - if (delimiter_position == NULL) { //delimiter not found - delimiter_index = -1; - *kv_config = _kv_map_table[0].kv_config; - goto exit; - } - } else { - delimiter_index = -1; - *kv_config = _kv_map_table[0].kv_config; - goto exit; - } - - - delimiter_index = delimiter_position - temp_str; - for (i = 0; i < _kv_num_attached_kvs; i++) { - - if (strncmp(temp_str, _kv_map_table[i].partition_name, delimiter_index) != 0) { - continue; - } - - *kv_config = _kv_map_table[i].kv_config; - break; - } - if (i == _kv_num_attached_kvs) { - ret = MBED_ERROR_ITEM_NOT_FOUND; - goto exit; - } -exit: - if (ret == MBED_SUCCESS) { - //if success extract the key - *key_index = *key_index + delimiter_index + 1; - } - return ret; -} - -KVStore *KVMap::get_internal_kv_instance(const char *name) -{ - - _mutex->lock(); - - kvstore_config_t *kv_config; - size_t key_index = 0; - - int ret = config_lookup(name, &kv_config, &key_index); - - _mutex->unlock(); - - return ret != MBED_SUCCESS ? NULL : kv_config->internal_store; -} - -KVStore *KVMap::get_external_kv_instance(const char *name) -{ - - _mutex->lock(); - - kvstore_config_t *kv_config; - size_t key_index = 0; - - int ret = config_lookup(name, &kv_config, &key_index); - - _mutex->unlock(); - - return ret != MBED_SUCCESS ? NULL : kv_config->external_store; -} - -KVStore *KVMap::get_main_kv_instance(const char *name) -{ - - _mutex->lock(); - - kvstore_config_t *kv_config; - size_t key_index = 0; - - int ret = config_lookup(name, &kv_config, &key_index); - - _mutex->unlock(); - - return ret != MBED_SUCCESS ? NULL : kv_config->kvstore_main_instance; -} - -BlockDevice *KVMap::get_internal_blockdevice_instance(const char *name) -{ - - _mutex->lock(); - - kvstore_config_t *kv_config; - size_t key_index = 0; - - int ret = config_lookup(name, &kv_config, &key_index); - - _mutex->unlock(); - - return ret != MBED_SUCCESS ? NULL : kv_config->internal_bd; -} - -BlockDevice *KVMap::get_external_blockdevice_instance(const char *name) -{ - - _mutex->lock(); - - kvstore_config_t *kv_config; - size_t key_index = 0; - - int ret = config_lookup(name, &kv_config, &key_index); - - _mutex->unlock(); - - return ret != MBED_SUCCESS ? NULL : kv_config->external_bd; -} - -FileSystem *KVMap::get_external_filesystem_instance(const char *name) -{ - - _mutex->lock(); - - kvstore_config_t *kv_config; - size_t key_index = 0; - - int ret = config_lookup(name, &kv_config, &key_index); - - _mutex->unlock(); - - return ret != MBED_SUCCESS ? NULL : kv_config->external_fs; -} - -} // namespace mbed diff --git a/storage/kvstore/source/kvstore_global_api.cpp b/storage/kvstore/source/kvstore_global_api.cpp deleted file mode 100644 index 9a9dcc7..0000000 --- a/storage/kvstore/source/kvstore_global_api.cpp +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Copyright (c) 2018 ARM Limited. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - * Licensed under the Apache License, Version 2.0 (the License); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "kvstore_global_api.h" - -#include "kv_config/kv_config.h" -#include "kvstore/KVMap.h" -#include "kvstore/KVStore.h" -#include "mbed_error.h" - -using namespace mbed; - -// iterator handle -struct _opaque_kv_key_iterator { - bool iterator_is_open; - KVStore *kvstore_intance; - KVStore::iterator_t *iterator_handle; - char *path; -}; - -int kv_set(const char *full_name_key, const void *buffer, size_t size, uint32_t create_flags) -{ - int ret = kv_init_storage_config(); - if (MBED_SUCCESS != ret) { - return ret; - } - - KVMap &kv_map = KVMap::get_instance(); - KVStore *kv_instance = NULL; - uint32_t flags_mask = 0; - size_t key_index = 0; - ret = kv_map.lookup(full_name_key, &kv_instance, &key_index, &flags_mask); - if (ret != MBED_SUCCESS) { - return ret; - } - - ret = kv_instance->set(full_name_key + key_index, buffer, size, create_flags & flags_mask); - return ret; -} - -int kv_get(const char *full_name_key, void *buffer, size_t buffer_size, size_t *actual_size) -{ - int ret = kv_init_storage_config(); - if (MBED_SUCCESS != ret) { - return ret; - } - - KVMap &kv_map = KVMap::get_instance(); - KVStore *kv_instance = NULL; - size_t key_index = 0; - ret = kv_map.lookup(full_name_key, &kv_instance, &key_index); - if (ret != MBED_SUCCESS) { - return ret; - } - - return kv_instance->get(full_name_key + key_index, buffer, buffer_size, actual_size); -} - -int kv_get_info(const char *full_name_key, kv_info_t *info) -{ - int ret = kv_init_storage_config(); - if (MBED_SUCCESS != ret) { - return ret; - } - - KVMap &kv_map = KVMap::get_instance(); - KVStore *kv_instance = NULL; - size_t key_index = 0; - ret = kv_map.lookup(full_name_key, &kv_instance, &key_index); - if (ret != MBED_SUCCESS) { - return ret; - } - - KVStore::info_t inner_info; - ret = kv_instance->get_info(full_name_key + key_index, &inner_info); - if (MBED_SUCCESS != ret) { - return ret; - } - info->flags = inner_info.flags; - info->size = inner_info.size; - return ret; -} - -int kv_remove(const char *full_name_key) -{ - int ret = kv_init_storage_config(); - if (MBED_SUCCESS != ret) { - return ret; - } - - KVMap &kv_map = KVMap::get_instance(); - KVStore *kv_instance = NULL; - size_t key_index = 0; - ret = kv_map.lookup(full_name_key, &kv_instance, &key_index); - if (ret != MBED_SUCCESS) { - return ret; - } - - return kv_instance->remove(full_name_key + key_index); -} - -int kv_iterator_open(kv_iterator_t *it, const char *full_prefix) -{ - if (it == NULL) { - return MBED_ERROR_INVALID_ARGUMENT; - } - - int ret = kv_init_storage_config(); - if (MBED_SUCCESS != ret) { - return ret; - } - - (*it) = new _opaque_kv_key_iterator; - if (*it == NULL) { - return MBED_ERROR_FAILED_OPERATION; - } - (*it)->iterator_is_open = false; - - KVMap &kv_map = KVMap::get_instance(); - KVStore *kv_instance = NULL; - size_t key_index = 0; - ret = kv_map.lookup(full_prefix, &kv_instance, &key_index); - if (ret != MBED_SUCCESS) { - delete (*it); - return ret; - } - - (*it)->kvstore_intance = kv_instance; - KVStore::iterator_t *inner_it = new KVStore::iterator_t; - ret = kv_instance->iterator_open(inner_it, full_prefix + key_index); - if (MBED_SUCCESS != ret) { - delete inner_it; - delete (*it); - return ret; - } - - (*it)->iterator_handle = inner_it; - (*it)->iterator_is_open = true; - (*it)->path = new char[key_index + 1]; - strncpy((*it)->path, full_prefix, key_index); - (*it)->path[key_index] = '\0'; - return ret; - -} - -int kv_iterator_next(kv_iterator_t it, char *key, size_t key_size) -{ - if (!it->iterator_is_open) { - return MBED_ERROR_INVALID_ARGUMENT; - } - - strcpy(key, it->path); - int path_len = strlen(key); - return it->kvstore_intance->iterator_next(*it->iterator_handle, key + path_len, key_size - path_len); -} - -int kv_iterator_close(kv_iterator_t it) -{ - if (!it->iterator_is_open) { - return MBED_ERROR_INVALID_ARGUMENT; - } - - int ret = it->kvstore_intance->iterator_close(*it->iterator_handle); - - delete it->iterator_handle; - delete[] it->path; - delete it; - - return ret; -} - -int kv_reset(const char *kvstore_name) -{ - int ret = kv_init_storage_config(); - if (MBED_SUCCESS != ret) { - return ret; - } - - KVMap &kv_map = KVMap::get_instance(); - KVStore *kv_instance = NULL; - size_t key_index = 0; - ret = kv_map.lookup(kvstore_name, &kv_instance, &key_index); - if (ret != MBED_SUCCESS) { - return ret; - } - - ret = kv_instance->reset(); - - return ret; - -}