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