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