Mbed OS Reference
Loading...
Searching...
No Matches
KVMap.h
1/*
2 * Copyright (c) 2018 ARM Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef _KV_MAP
17#define _KV_MAP
18
19#include "kvstore/KVStore.h"
20#include "rtos/Mutex.h"
21#include "platform/SingletonPtr.h"
22#include "blockdevice/BlockDevice.h"
23#include "filesystem/FileSystem.h"
24
25namespace mbed {
26
27#define MAX_ATTACHED_KVS 3
28
29/**
30 * This structure represent a KVStore partition configuration
31 */
32typedef struct {
33 /**
34 * A Pointer to main instance of the KVStore partition.
35 * This is also the instance KVStore global API should work with.
36 * This must not be NULL in a working partition configuration.
37 */
39 /**
40 * A pointer Internal store of the KVStore partition.
41 * If no rollback protection is required the pointer may be NULL.
42 */
44 /**
45 * A pointer external store of the KVStore partition.
46 * The pointer can be NULL if external store has been omitted
47 */
49 /**
50 * A pointer Internal FlashIAP BlockDevice of the KVStore partition.
51 * The pointer can be NULL if internal store has been omitted
52 */
54 /**
55 * A pointer external BlockDevice of the KVStore partition.
56 * The pointer can be NULL if external store has been omitted
57 */
59 /**
60 * A pointer external FileSystem of the KVStore partition.
61 * The pointer can be NULL if FileSystemStore has not been configured.
62 */
64 /**
65 * This is a flag masking value for the KVStore global API.
66 * The Global API will mask the input flags base on this value to
67 * prevent errors in case the user choose an different security level.
68 */
69 uint32_t flags_mask;
71
72/**
73 * This structure maps between a string name and a partition configuration.
74 */
75typedef struct {
76 /**
77 * Partition name string
78 */
80 /**
81 * Configuration struct.
82 */
85
86/** KVMap class
87 *
88 * Singleton class to manage the mapping of KVStore partition and its naming.
89 */
90class KVMap : private mbed::NonCopyable<KVMap> {
91public:
92
93 /**
94 * @brief As a singleton, return the single instance of the class.
95 * This class is a singleton for the following reasons:
96 * - Ease of use, so you don't have to coordinate instantiations.
97 * - Lazy instantiation of internal data, (which we can't achieve with simple static classes).
98 *
99 * @returns Singleton instance reference.
100 */
102 {
103 // Use this implementation of singleton (Meyer's) rather than the one that allocates
104 // the instance on the heap because it ensures destruction at program end (preventing warnings
105 // from memory checking tools, such as valgrind).
106 static KVMap instance;
107 return instance;
108 }
109
110 ~KVMap();
111
112 /**
113 * @brief Initializes KVMap
114 *
115 * @return 0 on success, negative error code on failure
116 */
117 int init();
118
119 /**
120 * @brief Attach a KVStore partition configuration, and add it to the KVMap array
121 *
122 * @param partition_name String parameter contains the partition name.
123 * @param kv_config A configuration struct created by the kv_config or by the user.
124 * @return 0 on success, negative error code on failure
125 */
126 int attach(const char *partition_name, kvstore_config_t *kv_config);
127
128 /**
129 * @brief Detach a KVStore partition configuration from the KVMap array,
130 * and deinitialize its components
131 *
132 * @param partition_name String parameter contains the partition name.
133 * @return 0 on success, negative error code on failure
134 */
135 int detach(const char *partition_name);
136
137 /**
138 * @brief Deinitialize the KVMap array, and deinitialize all the attached partitions.
139 *
140 * @return 0 on success, negative error code on failure
141 */
142 int deinit();
143
144 /**
145 * @brief Full name lookup, and then break it into KVStore instance and key
146 *
147 * @param[in] full_name String parameter contains the partition name to look for.
148 * The String should be formated as follow "/partition name/key". The key is optional.
149 * @param[out] kv_instance Returns the main KVStore instance associated with the required partition name.
150 * @param[out] key_index Returns an index to the first character of the key.
151 * @param[out] flags_mask Return the flag masking for the current configuration
152 * @return 0 on success, negative error code on failure
153 */
154 int lookup(const char *full_name, mbed::KVStore **kv_instance, size_t *key_index, uint32_t *flags_mask = NULL);
155
156 /**
157 * @brief Getter for the internal KVStore instance.
158 *
159 * @param name String parameter contains the /partition name/.
160 *
161 * @return Pointer to the internal kvstore on success,
162 * NULL on failure or if not exist
163 */
165 /**
166 * @brief Getter for the external KVStore instance.
167 *
168 * @param name String parameter contains the /partition name/.
169 *
170 * @return Pointer to the external kvstore on success,
171 * NULL on failure or if not exist
172 */
174 /**
175 * @brief Getter for the main KVStore instance.
176 *
177 * @param name String parameter contains the /partition name/.
178 *
179 * @return Pointer to the main kvstore on success,
180 * NULL on failure or if not exist
181 */
182 KVStore *get_main_kv_instance(const char *name);
183 /**
184 * @brief Getter for the internal BlockDevice instance.
185 *
186 * @param name String parameter contains the /partition name/.
187 *
188 * @return Pointer to the internal BlockDevice on success,
189 * NULL on failure or if not exist
190 */
192 /**
193 * @brief Getter for the external BlockDevice instance.
194 *
195 * @param name String parameter contains the /partition name/.
196 *
197 * @return Pointer to the external BlockDevice on success,
198 * NULL on failure or if not exist
199 */
201 /**
202 * @brief Getter for the external FileSystem instance.
203 *
204 * @param name String parameter contains the /partition name/.
205 *
206 * @return Pointer to the external FileSystem on success,
207 * NULL on failure or if not exist
208 */
210
211#if !defined(DOXYGEN_ONLY)
212private:
213
214 /**
215 * @brief Deinitialize all components of a partition configuration struct.
216 *
217 * @param partition Partition configuration struct.
218 */
219 void deinit_partition(kv_map_entry_t *partition);
220
221 /**
222 * @brief Full name lookup, and then break it into KVStore config and key
223 *
224 * @param[in] full_name String parameter contains the /partition name/key.
225 * @param[out] kv_config Returns The configuration struct associated with the partition name
226 * @param[out] key_index Returns an index to the first character of the key.
227 * @return 0 on success, negative error code on failure
228 */
229 int config_lookup(const char *full_name, kvstore_config_t **kv_config, size_t *key_index);
230
231 // Attachment table
232 kv_map_entry_t _kv_map_table[MAX_ATTACHED_KVS];
233 int _kv_num_attached_kvs;
234 int _is_initialized;
236#endif
237};
238}
239#endif
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
A file system object.
Definition: FileSystem.h:50
KVMap class.
Definition: KVMap.h:90
int init()
Initializes KVMap.
BlockDevice * get_external_blockdevice_instance(const char *name)
Getter for the external BlockDevice instance.
int detach(const char *partition_name)
Detach a KVStore partition configuration from the KVMap array, and deinitialize its components.
int deinit()
Deinitialize the KVMap array, and deinitialize all the attached partitions.
BlockDevice * get_internal_blockdevice_instance(const char *name)
Getter for the internal BlockDevice instance.
KVStore * get_external_kv_instance(const char *name)
Getter for the external KVStore instance.
int lookup(const char *full_name, mbed::KVStore **kv_instance, size_t *key_index, uint32_t *flags_mask=NULL)
Full name lookup, and then break it into KVStore instance and key.
KVStore * get_internal_kv_instance(const char *name)
Getter for the internal KVStore instance.
FileSystem * get_external_filesystem_instance(const char *name)
Getter for the external FileSystem instance.
KVStore * get_main_kv_instance(const char *name)
Getter for the main KVStore instance.
int attach(const char *partition_name, kvstore_config_t *kv_config)
Attach a KVStore partition configuration, and add it to the KVMap array.
static KVMap & get_instance()
As a singleton, return the single instance of the class.
Definition: KVMap.h:101
KVStore class.
Definition: KVStore.h:38
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
Utility class for creating and using a singleton.
Definition: SingletonPtr.h:88
This structure maps between a string name and a partition configuration.
Definition: KVMap.h:75
kvstore_config_t * kv_config
Configuration struct.
Definition: KVMap.h:83
char * partition_name
Partition name string.
Definition: KVMap.h:79
This structure represent a KVStore partition configuration.
Definition: KVMap.h:32
BlockDevice * external_bd
A pointer external BlockDevice of the KVStore partition.
Definition: KVMap.h:58
FileSystem * external_fs
A pointer external FileSystem of the KVStore partition.
Definition: KVMap.h:63
KVStore * internal_store
A pointer Internal store of the KVStore partition.
Definition: KVMap.h:43
BlockDevice * internal_bd
A pointer Internal FlashIAP BlockDevice of the KVStore partition.
Definition: KVMap.h:53
KVStore * external_store
A pointer external store of the KVStore partition.
Definition: KVMap.h:48
uint32_t flags_mask
This is a flag masking value for the KVStore global API.
Definition: KVMap.h:69
KVStore * kvstore_main_instance
A Pointer to main instance of the KVStore partition.
Definition: KVMap.h:38