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.h"
23 #include "BlockDevice.h"
24 #include "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  bool _variant_bd_erase_unit_size;
312  void *_inc_set_handle;
313  void *_iterator_table[_max_open_iterators];
314 
315  /**
316  * @brief Read a block from an area.
317  *
318  * @param[in] area Area.
319  * @param[in] offset Offset in area.
320  * @param[in] size Number of bytes to read.
321  * @param[in] buf Output buffer.
322  *
323  * @returns 0 for success, nonzero for failure.
324  */
325  int read_area(uint8_t area, uint32_t offset, uint32_t size, void *buf);
326 
327  /**
328  * @brief Write a block to an area.
329  *
330  * @param[in] area Area.
331  * @param[in] offset Offset in area.
332  * @param[in] size Number of bytes to write.
333  * @param[in] buf Input buffer.
334  *
335  * @returns 0 for success, non-zero for failure.
336  */
337  int write_area(uint8_t area, uint32_t offset, uint32_t size, const void *buf);
338 
339  /**
340  * @brief Reset an area (erase its start).
341  *
342  * @param[in] area Area.
343  *
344  * @returns 0 for success, nonzero for failure.
345  */
346  int reset_area(uint8_t area);
347 
348  /**
349  * @brief Erase an erase unit.
350  *
351  * @param[in] area Area.
352  * @param[in] offset Offset in area.
353  *
354  * @returns 0 for success, nonzero for failure.
355  */
356  int erase_erase_unit(uint8_t area, uint32_t offset);
357 
358  /**
359  * @brief Calculate addresses and sizes of areas.
360  */
361  void calc_area_params();
362 
363  /**
364  * @brief Read a TDBStore record from a given location.
365  *
366  * @param[in] area Area.
367  * @param[in] offset Offset of record in area.
368  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
369  * @param[in] data_buf Data buffer.
370  * @param[in] data_buf_size Data buffer size.
371  * @param[out] actual_data_size Actual data size.
372  * @param[in] data_offset Offset in data.
373  * @param[in] copy_key Copy key to user buffer.
374  * @param[in] copy_data Copy data to user buffer.
375  * @param[in] check_expected_key Check whether key belongs to this record.
376  * @param[in] calc_hash Calculate hash (on key).
377  * @param[out] hash Calculated hash.
378  * @param[out] flags Record flags.
379  * @param[out] next_offset Offset of next record.
380  *
381  * @returns 0 for success, nonzero for failure.
382  */
383  int read_record(uint8_t area, uint32_t offset, char *key,
384  void *data_buf, uint32_t data_buf_size,
385  uint32_t &actual_data_size, size_t data_offset, bool copy_key,
386  bool copy_data, bool check_expected_key, bool calc_hash,
387  uint32_t &hash, uint32_t &flags, uint32_t &next_offset);
388 
389  /**
390  * @brief Write a master record of a given area.
391  *
392  * @param[in] area Area.
393  * @param[in] version Area version.
394  * @param[out] next_offset Offset of next record.
395  *
396  * @returns 0 for success, nonzero for failure.
397  */
398  int write_master_record(uint8_t area, uint16_t version, uint32_t &next_offset);
399 
400  /**
401  * @brief Copy a record from one area to the opposite one.
402  *
403  * @param[in] from_area Area to copy record from.
404  * @param[in] from_offset Offset in source area.
405  * @param[in] to_offset Offset in destination area.
406  * @param[out] to_next_offset Offset of next record in destination area.
407  *
408  * @returns 0 for success, nonzero for failure.
409  */
410  int copy_record(uint8_t from_area, uint32_t from_offset, uint32_t to_offset,
411  uint32_t &to_next_offset);
412 
413  /**
414  * @brief Garbage collection (compact all records from active area to the standby one).
415  *
416  * @returns 0 for success, nonzero for failure.
417  */
418  int garbage_collection();
419 
420  /**
421  * @brief Return record size given key and data size.
422  *
423  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
424  * @param[in] data_size Data size.
425  *
426  * @returns record size.
427  */
428  uint32_t record_size(const char *key, uint32_t data_size);
429 
430  /**
431  * @brief Find a record given key
432  *
433  * @param[in] area Area.
434  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
435  * @param[out] offset Offset of record.
436  * @param[out] ram_table_ind Index in RAM table (target one if not found).
437  * @param[out] hash Calculated key hash.
438  *
439  * @returns 0 for success, nonzero for failure.
440  */
441  int find_record(uint8_t area, const char *key, uint32_t &offset,
442  uint32_t &ram_table_ind, uint32_t &hash);
443  /**
444  * @brief Actual logics of get API (also covers all other get APIs).
445  *
446  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
447  * @param[in] copy_data Copy data to user buffer.
448  * @param[in] data_buf Buffer to store data on.
449  * @param[in] data_buf_size Data buffer size (bytes).
450  * @param[out] actual_data_size Actual data size (bytes).
451  * @param[out] flags Flags.
452  *
453  * @returns 0 for success, nonzero for failure.
454  */
455  int do_get(const char *key, bool copy_data,
456  void *data_buf, uint32_t data_buf_size, uint32_t &actual_data_size,
457  uint32_t &flags);
458 
459  /**
460  * @brief Actual logics of set API (covers also the remove API).
461  *
462  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
463  * @param[in] data_buf Data buffer.
464  * @param[in] data_buf_size Data buffer size (bytes).
465  * @param[in] flags Flags.
466  *
467  * @returns 0 for success, nonzero for failure.
468  */
469  int do_set(const char *key, const void *data_buf, uint32_t data_buf_size, uint32_t flags);
470 
471  /**
472  * @brief Build RAM table and update _free_space_offset (scanning all the records in the area).
473  *
474  * @returns 0 for success, nonzero for failure.
475  */
476  int build_ram_table();
477 
478  /**
479  * @brief Increment maximum number of keys and reallocate RAM table accordingly.
480  *
481  * @param[out] ram_table Updated RAM table.
482  *
483  * @returns 0 for success, nonzero for failure.
484  */
485  int increment_max_keys(void **ram_table = 0);
486 
487  /**
488  * @brief Calculate offset from start of erase unit.
489  *
490  * @param[in] area Area.
491  * @param[in] offset Offset in area.
492  * @param[out] offset_from_start Offset from start of erase unit.
493  * @param[out] dist_to_end Distance to end of erase unit.
494  *
495  * @returns offset in erase unit.
496  */
497  void offset_in_erase_unit(uint8_t area, uint32_t offset, uint32_t &offset_from_start,
498  uint32_t &dist_to_end);
499 
500  /**
501  * @brief Check whether erase unit is erased (from offset until end of unit).
502  *
503  * @param[in] area Area.
504  * @param[in] offset Offset in area.
505  * @param[out] erased Unit is erased.
506  *
507  * @returns 0 for success, nonzero for failure.
508  */
509  int is_erase_unit_erased(uint8_t area, uint32_t offset, bool &erased);
510 
511  /**
512  * @brief Before writing a record, check whether you are crossing an erase unit.
513  * If you do, check if it's erased, and erase it if not.
514  *
515  * @param[in] area Area.
516  * @param[in] offset Offset in area.
517  * @param[in] size Write size.
518  * @param[in] force_check Force checking.
519  *
520  * @returns 0 for success, nonzero for failure.
521  */
522  int check_erase_before_write(uint8_t area, uint32_t offset, uint32_t size,
523  bool force_check = false);
524 
525  /**
526  * @brief Get data from reserved area - worker function.
527  *
528  * @param[in] reserved_data Reserved data buffer (0 to return nothing).
529  * @param[in] reserved_data_buf_size
530  * Reserved data buffer size.
531  * @param[in] actual_data_size Return data size.
532  *
533  * @returns 0 on success or a negative error code on failure
534  */
535  int do_reserved_data_get(void *reserved_data, size_t reserved_data_buf_size,
536  size_t *actual_data_size = 0);
537 
538  /**
539  * @brief Update all iterators after adding or deleting of keys.
540  *
541  * @param[in] added True if added, false if deleted.
542  * @param[in] ram_table_ind RAM table index.
543  *
544  * @returns none
545  */
546  void update_all_iterators(bool added, uint32_t ram_table_ind);
547 
548 #endif
549 
550 };
551 /** @}*/
552 
553 } // namespace mbed
554 
555 #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.