Mistake on this page?
Report an issue in GitHub or email us
TDBStore.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 
17 #ifndef MBED_TDBSTORE_H
18 #define MBED_TDBSTORE_H
19 
20 #include <stdint.h>
21 #include <stdio.h>
22 #include "features/storage/kvstore/include/KVStore.h"
23 #include "features/storage/blockdevice/BlockDevice.h"
24 #include "features/storage/blockdevice/BufferedBlockDevice.h"
25 #include "PlatformMutex.h"
26 
27 namespace mbed {
28 
29 /** TDBStore class
30  *
31  * Lightweight Key Value storage over a block device
32  */
33 
34 class TDBStore : public KVStore {
35 public:
36 
37  static const uint32_t RESERVED_AREA_SIZE = 64;
38 
39  /**
40  * @brief Class constructor
41  *
42  * @param[in] bd Underlying block device. The BlockDevice
43  * can be any BlockDevice with flash characteristics.
44  * If using a BlockDevice without flash, such as SDBlockDevice,
45  * please add the FlashSimBlockDevice on top of it.
46  *
47  * @returns none
48  */
49  TDBStore(BlockDevice *bd);
50 
51  /**
52  * @brief Class destructor
53  *
54  * @returns none
55  */
56  virtual ~TDBStore();
57 
58  /**
59  * @brief Initialize TDBStore. If data exists, TDBStore will check the data integrity
60  * on initialize. If the integrity checks fails, the TDBStore will use GC to collect
61  * the available data and clean corrupted and erroneous records.
62  *
63  * @returns MBED_SUCCESS Success.
64  * @returns Negative error code on failure.
65  */
66  virtual int init();
67 
68  /**
69  * @brief Deinitialize TDBStore, release and free resources.
70  *
71  * @returns MBED_SUCCESS Success.
72  */
73  virtual int deinit();
74 
75 
76  /**
77  * @brief Reset TDBStore contents (clear all keys)
78  *
79  * @returns MBED_SUCCESS Success.
80  * MBED_ERROR_NOT_READY Not initialized.
81  * MBED_ERROR_READ_FAILED Unable to read from media.
82  * MBED_ERROR_WRITE_FAILED Unable to write to media.
83  */
84  virtual int reset();
85 
86  /**
87  * @brief Set one TDBStore item, given key and value.
88  *
89  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
90  * @param[in] buffer Value data buffer.
91  * @param[in] size Value data size.
92  * @param[in] create_flags Flag mask.
93  *
94  * @returns MBED_SUCCESS Success.
95  * MBED_ERROR_NOT_READY Not initialized.
96  * MBED_ERROR_READ_FAILED Unable to read from media.
97  * MBED_ERROR_WRITE_FAILED Unable to write to media.
98  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
99  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
100  * MBED_ERROR_MEDIA_FULL Not enough room on media.
101  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
102  */
103  virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags);
104 
105  /**
106  * @brief Get one TDBStore item by given key.
107  *
108  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
109  * @param[in] buffer Value data buffer.
110  * @param[in] buffer_size Value data buffer size.
111  * @param[out] actual_size Actual read size.
112  * @param[in] offset Offset to read from in data.
113  *
114  * @returns MBED_SUCCESS Success.
115  * MBED_ERROR_NOT_READY Not initialized.
116  * MBED_ERROR_READ_FAILED Unable to read from media.
117  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
118  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
119  * MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt.
120  * MBED_ERROR_ITEM_NOT_FOUND No such key.
121  */
122  virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL,
123  size_t offset = 0);
124 
125  /**
126  * @brief Get information of a given key. The returned info contains size and flags
127  *
128  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
129  * @param[out] info Returned information structure.
130  *
131  * @returns MBED_SUCCESS Success.
132  * MBED_ERROR_NOT_READY Not initialized.
133  * MBED_ERROR_READ_FAILED Unable to read from media.
134  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
135  * MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt.
136  * MBED_ERROR_ITEM_NOT_FOUND No such key.
137  */
138  virtual int get_info(const char *key, info_t *info);
139 
140  /**
141  * @brief Remove a TDBStore item by given key.
142  *
143  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
144  *
145  * @returns MBED_SUCCESS Success.
146  * MBED_ERROR_NOT_READY Not initialized.
147  * MBED_ERROR_READ_FAILED Unable to read from media.
148  * MBED_ERROR_WRITE_FAILED Unable to write to media.
149  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
150  * MBED_ERROR_MEDIA_FULL Not enough room on media.
151  * MBED_ERROR_ITEM_NOT_FOUND No such key.
152  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
153  */
154  virtual int remove(const char *key);
155 
156 
157  /**
158  * @brief Start an incremental TDBStore set sequence. This operation is blocking other operations.
159  * Any get/set/remove/iterator operation will be blocked until set_finalize is called.
160  *
161  * @param[out] handle Returned incremental set handle.
162  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
163  * @param[in] final_data_size Final value data size.
164  * @param[in] create_flags Flag mask.
165  *
166  * @returns MBED_SUCCESS Success.
167  * MBED_ERROR_NOT_READY Not initialized.
168  * MBED_ERROR_READ_FAILED Unable to read from media.
169  * MBED_ERROR_WRITE_FAILED Unable to write to media.
170  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
171  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
172  * MBED_ERROR_MEDIA_FULL Not enough room on media.
173  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
174  */
175  virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags);
176 
177  /**
178  * @brief Add data to incremental TDBStore set sequence. This operation is blocking other operations.
179  * Any get/set/remove operation will be blocked until set_finalize will be called.
180  *
181  * @param[in] handle Incremental set handle.
182  * @param[in] value_data Value data to add.
183  * @param[in] data_size Value data size.
184  *
185  * @returns MBED_SUCCESS Success.
186  * MBED_ERROR_NOT_READY Not initialized.
187  * MBED_ERROR_WRITE_FAILED Unable to write to media.
188  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
189  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
190  */
191  virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size);
192 
193  /**
194  * @brief Finalize an incremental KVStore set sequence.
195  *
196  * @param[in] handle Incremental set handle.
197  *
198  * @returns MBED_SUCCESS Success.
199  * MBED_ERROR_NOT_READY Not initialized.
200  * MBED_ERROR_WRITE_FAILED Unable to write to media.
201  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
202  */
203  virtual int set_finalize(set_handle_t handle);
204 
205  /**
206  * @brief Start an iteration over KVStore keys.
207  * There are no issues with any other operations while iterator is open.
208  *
209  * @param[out] it Returned iterator handle.
210  * @param[in] prefix Key prefix (null for all keys).
211  *
212  * @returns MBED_SUCCESS Success.
213  * MBED_ERROR_NOT_READY Not initialized.
214  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
215  */
216  virtual int iterator_open(iterator_t *it, const char *prefix = NULL);
217 
218  /**
219  * @brief Get next key in iteration.
220  * There are no issues with any other operations while iterator is open.
221  *
222  * @param[in] it Iterator handle.
223  * @param[in] key Buffer for returned key.
224  * @param[in] key_size Key buffer size.
225  *
226  * @returns MBED_SUCCESS Success.
227  * MBED_ERROR_NOT_READY Not initialized.
228  * MBED_ERROR_READ_FAILED Unable to read from block device.
229  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
230  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
231  * MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt.
232  * MBED_ERROR_ITEM_NOT_FOUND No more keys found.
233  */
234  virtual int iterator_next(iterator_t it, char *key, size_t key_size);
235 
236  /**
237  * @brief Close iteration.
238  *
239  * @param[in] it Iterator handle.
240  *
241  * @returns MBED_SUCCESS Success.
242  * MBED_ERROR_NOT_READY Not initialized.
243  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
244  */
245  virtual int iterator_close(iterator_t it);
246 
247  /**
248  * @brief Set data in reserved area, which is a special location for special data, such as ROT.
249  * The data written to reserved area can't be overwritten.
250  *
251  * @param[in] reserved_data Reserved data buffer.
252  * @param[in] reserved_data_buf_size
253  * Reserved data buffer size.
254  *
255  * @returns MBED_SUCCESS Success.
256  * MBED_ERROR_NOT_READY Not initialized.
257  * MBED_ERROR_READ_FAILED Unable to read from media.
258  * MBED_ERROR_WRITE_FAILED Unable to write to media.
259  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
260  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
261  */
262  virtual int reserved_data_set(const void *reserved_data, size_t reserved_data_buf_size);
263 
264  /**
265  * @brief Get data from reserved area, which is a special location for special data, such as ROT.
266  *
267  * @param[in] reserved_data Reserved data buffer.
268  * @param[in] reserved_data_buf_size
269  * Reserved data buffer size.
270  * @param[in] actual_data_size Return data size.
271  *
272  * @returns MBED_SUCCESS Success.
273  * MBED_ERROR_NOT_READY Not initialized.
274  * MBED_ERROR_READ_FAILED Unable to read from media.
275  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
276  * MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt.
277  * MBED_ERROR_ITEM_NOT_FOUND No reserved data was written.
278  */
279  virtual int reserved_data_get(void *reserved_data, size_t reserved_data_buf_size,
280  size_t *actual_data_size = 0);
281 
282 #if !defined(DOXYGEN_ONLY)
283 private:
284 
285  typedef struct {
286  uint32_t address;
287  size_t size;
288  } tdbstore_area_data_t;
289 
290  static const int _num_areas = 2;
291  static const int _max_open_iterators = 16;
292 
293  PlatformMutex _mutex;
294  PlatformMutex _inc_set_mutex;
295  void *_ram_table;
296  size_t _max_keys;
297  size_t _num_keys;
298  BlockDevice *_bd;
299  BufferedBlockDevice *_buff_bd;
300  uint32_t _free_space_offset;
301  uint32_t _master_record_offset;
302  uint32_t _master_record_size;
303  bool _is_initialized;
304  int _active_area;
305  uint16_t _active_area_version;
306  size_t _size;
307  tdbstore_area_data_t _area_params[_num_areas];
308  uint32_t _prog_size;
309  uint8_t *_work_buf;
310  char *_key_buf;
311  void *_inc_set_handle;
312  void *_iterator_table[_max_open_iterators];
313 
314  /**
315  * @brief Read a block from an area.
316  *
317  * @param[in] area Area.
318  * @param[in] offset Offset in area.
319  * @param[in] size Number of bytes to read.
320  * @param[in] buf Output buffer.
321  *
322  * @returns 0 for success, nonzero for failure.
323  */
324  int read_area(uint8_t area, uint32_t offset, uint32_t size, void *buf);
325 
326  /**
327  * @brief Write a block to an area.
328  *
329  * @param[in] area Area.
330  * @param[in] offset Offset in area.
331  * @param[in] size Number of bytes to write.
332  * @param[in] buf Input buffer.
333  *
334  * @returns 0 for success, non-zero for failure.
335  */
336  int write_area(uint8_t area, uint32_t offset, uint32_t size, const void *buf);
337 
338  /**
339  * @brief Reset an area (erase its start).
340  *
341  * @param[in] area Area.
342  *
343  * @returns 0 for success, nonzero for failure.
344  */
345  int reset_area(uint8_t area);
346 
347  /**
348  * @brief Erase an erase unit.
349  *
350  * @param[in] area Area.
351  * @param[in] offset Offset in area.
352  *
353  * @returns 0 for success, nonzero for failure.
354  */
355  int erase_erase_unit(uint8_t area, uint32_t offset);
356 
357  /**
358  * @brief Calculate addresses and sizes of areas.
359  */
360  void calc_area_params();
361 
362  /**
363  * @brief Read a TDBStore record from a given location.
364  *
365  * @param[in] area Area.
366  * @param[in] offset Offset of record in area.
367  * @param[out] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
368  * @param[out] data_buf Data buffer.
369  * @param[in] data_buf_size Data buffer size.
370  * @param[out] actual_data_size Actual data size.
371  * @param[in] data_offset Offset in data.
372  * @param[in] copy_key Copy key to user buffer.
373  * @param[in] copy_data Copy data to user buffer.
374  * @param[in] check_expected_key Check whether key belongs to this record.
375  * @param[in] calc_hash Calculate hash (on key).
376  * @param[out] hash Calculated hash.
377  * @param[out] flags Record flags.
378  * @param[out] next_offset Offset of next record.
379  *
380  * @returns 0 for success, nonzero for failure.
381  */
382  int read_record(uint8_t area, uint32_t offset, char *key,
383  void *data_buf, uint32_t data_buf_size,
384  uint32_t &actual_data_size, size_t data_offset, bool copy_key,
385  bool copy_data, bool check_expected_key, bool calc_hash,
386  uint32_t &hash, uint32_t &flags, uint32_t &next_offset);
387 
388  /**
389  * @brief Write a master record of a given area.
390  *
391  * @param[in] area Area.
392  * @param[in] version Area version.
393  * @param[out] next_offset Offset of next record.
394  *
395  * @returns 0 for success, nonzero for failure.
396  */
397  int write_master_record(uint8_t area, uint16_t version, uint32_t &next_offset);
398 
399  /**
400  * @brief Copy a record from one area to the opposite one.
401  *
402  * @param[in] from_area Area to copy record from.
403  * @param[in] from_offset Offset in source area.
404  * @param[in] to_offset Offset in destination area.
405  * @param[out] to_next_offset Offset of next record in destination area.
406  *
407  * @returns 0 for success, nonzero for failure.
408  */
409  int copy_record(uint8_t from_area, uint32_t from_offset, uint32_t to_offset,
410  uint32_t &to_next_offset);
411 
412  /**
413  * @brief Garbage collection (compact all records from active area to the standby one).
414  *
415  * @returns 0 for success, nonzero for failure.
416  */
417  int garbage_collection();
418 
419  /**
420  * @brief Return record size given key and data size.
421  *
422  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
423  * @param[in] data_size Data size.
424  *
425  * @returns record size.
426  */
427  uint32_t record_size(const char *key, uint32_t data_size);
428 
429  /**
430  * @brief Find a record given key
431  *
432  * @param[in] area Area.
433  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
434  * @param[out] offset Offset of record.
435  * @param[out] ram_table_ind Index in RAM table (target one if not found).
436  * @param[out] hash Calculated key hash.
437  *
438  * @returns 0 for success, nonzero for failure.
439  */
440  int find_record(uint8_t area, const char *key, uint32_t &offset,
441  uint32_t &ram_table_ind, uint32_t &hash);
442  /**
443  * @brief Actual logics of get API (also covers all other get APIs).
444  *
445  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
446  * @param[in] copy_data Copy data to user buffer.
447  * @param[in] data_buf Buffer to store data on.
448  * @param[in] data_buf_size Data buffer size (bytes).
449  * @param[out] actual_data_size Actual data size (bytes).
450  * @param[out] flags Flags.
451  *
452  * @returns 0 for success, nonzero for failure.
453  */
454  int do_get(const char *key, bool copy_data,
455  void *data_buf, uint32_t data_buf_size, uint32_t &actual_data_size,
456  uint32_t &flags);
457 
458  /**
459  * @brief Actual logics of set API (covers also the remove API).
460  *
461  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
462  * @param[in] data_buf Data buffer.
463  * @param[in] data_buf_size Data buffer size (bytes).
464  * @param[in] flags Flags.
465  *
466  * @returns 0 for success, nonzero for failure.
467  */
468  int do_set(const char *key, const void *data_buf, uint32_t data_buf_size, uint32_t flags);
469 
470  /**
471  * @brief Build RAM table and update _free_space_offset (scanning all the records in the area).
472  *
473  * @returns 0 for success, nonzero for failure.
474  */
475  int build_ram_table();
476 
477  /**
478  * @brief Increment maximum number of keys and reallocate RAM table accordingly.
479  *
480  * @param[out] ram_table Updated RAM table.
481  *
482  * @returns 0 for success, nonzero for failure.
483  */
484  int increment_max_keys(void **ram_table = 0);
485 
486  /**
487  * @brief Calculate offset from start of erase unit.
488  *
489  * @param[in] area Area.
490  * @param[in] offset Offset in area.
491  * @param[out] offset_from_start Offset from start of erase unit.
492  * @param[out] dist_to_end Distance to end of erase unit.
493  *
494  * @returns offset in erase unit.
495  */
496  void offset_in_erase_unit(uint8_t area, uint32_t offset, uint32_t &offset_from_start,
497  uint32_t &dist_to_end);
498 
499  /**
500  * @brief Check whether erase unit is erased (from offset until end of unit).
501  *
502  * @param[in] area Area.
503  * @param[in] offset Offset in area.
504  * @param[out] erased Unit is erased.
505  *
506  * @returns 0 for success, nonzero for failure.
507  */
508  int is_erase_unit_erased(uint8_t area, uint32_t offset, bool &erased);
509 
510  /**
511  * @brief Before writing a record, check whether you are crossing an erase unit.
512  * If you do, check if it's erased, and erase it if not.
513  *
514  * @param[in] area Area.
515  * @param[in] offset Offset in area.
516  * @param[in] size Write size.
517  * @param[in] force_check Force checking.
518  *
519  * @returns 0 for success, nonzero for failure.
520  */
521  int check_erase_before_write(uint8_t area, uint32_t offset, uint32_t size,
522  bool force_check = false);
523 
524  /**
525  * @brief Get data from reserved area - worker function.
526  *
527  * @param[in] reserved_data Reserved data buffer (0 to return nothing).
528  * @param[in] reserved_data_buf_size
529  * Reserved data buffer size.
530  * @param[in] actual_data_size Return data size.
531  *
532  * @returns 0 on success or a negative error code on failure
533  */
534  int do_reserved_data_get(void *reserved_data, size_t reserved_data_buf_size,
535  size_t *actual_data_size = 0);
536 
537  /**
538  * @brief Update all iterators after adding or deleting of keys.
539  *
540  * @param[in] added True if added, false if deleted.
541  * @param[in] ram_table_ind RAM table index.
542  *
543  * @returns none
544  */
545  void update_all_iterators(bool added, uint32_t ram_table_ind);
546 
547 #endif
548 
549 };
550 /** @}*/
551 
552 } // namespace mbed
553 
554 #endif
virtual int set_finalize(set_handle_t handle)
Finalize an incremental KVStore set sequence.
Holds key information.
Definition: KVStore.h:48
virtual ~TDBStore()
Class destructor.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:47
TDBStore(BlockDevice *bd)
Class constructor.
virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags)
Start an incremental TDBStore set sequence.
virtual int init()
Initialize TDBStore.
virtual int iterator_next(iterator_t it, char *key, size_t key_size)
Get next key in iteration.
virtual int iterator_open(iterator_t *it, const char *prefix=NULL)
Start an iteration over KVStore keys.
virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size)
Add data to incremental TDBStore set sequence.
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
Block device for allowing minimal read and program sizes (of 1) for the underlying BD...
KVStore class.
Definition: KVStore.h:30
virtual int deinit()
Deinitialize TDBStore, release and free resources.
TDBStore class.
Definition: TDBStore.h:34
virtual int get_info(const char *key, info_t *info)
Get information of a given key.
virtual int reset()
Reset TDBStore contents (clear all keys)
virtual int reserved_data_set(const void *reserved_data, size_t reserved_data_buf_size)
Set data in reserved area, which is a special location for special data, such as ROT.
virtual int iterator_close(iterator_t it)
Close iteration.
virtual int reserved_data_get(void *reserved_data, size_t reserved_data_buf_size, size_t *actual_data_size=0)
Get data from reserved area, which is a special location for special data, such as ROT...
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.