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.
44  *
45  * @returns none
46  */
47  TDBStore(BlockDevice *bd);
48 
49  /**
50  * @brief Class destructor
51  *
52  * @returns none
53  */
54  virtual ~TDBStore();
55 
56  /**
57  * @brief Initialize TDBStore. If data exists, TDBStore will check the data integrity
58  * on initialize. If the integrity checks fails, the TDBStore will use GC to collect
59  * the available data and clean corrupted and erroneous records.
60  *
61  * @returns MBED_SUCCESS Success.
62  * @returns Negative error code on failure.
63  */
64  virtual int init();
65 
66  /**
67  * @brief Deinitialize TDBStore, release and free resources.
68  *
69  * @returns MBED_SUCCESS Success.
70  */
71  virtual int deinit();
72 
73 
74  /**
75  * @brief Reset TDBStore contents (clear all keys) and reserved data
76  *
77  * @returns MBED_SUCCESS Success.
78  * MBED_ERROR_NOT_READY Not initialized.
79  * MBED_ERROR_READ_FAILED Unable to read from media.
80  * MBED_ERROR_WRITE_FAILED Unable to write to media.
81  */
82  virtual int reset();
83 
84  /**
85  * @brief Set one TDBStore item, given key and value.
86  *
87  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
88  * @param[in] buffer Value data buffer.
89  * @param[in] size Value data size.
90  * @param[in] create_flags Flag mask.
91  *
92  * @returns MBED_SUCCESS Success.
93  * MBED_ERROR_NOT_READY Not initialized.
94  * MBED_ERROR_READ_FAILED Unable to read from media.
95  * MBED_ERROR_WRITE_FAILED Unable to write to media.
96  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
97  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
98  * MBED_ERROR_MEDIA_FULL Not enough room on media.
99  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
100  */
101  virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags);
102 
103  /**
104  * @brief Get one TDBStore item by given key.
105  *
106  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
107  * @param[in] buffer Value data buffer.
108  * @param[in] buffer_size Value data buffer size.
109  * @param[out] actual_size Actual read size.
110  * @param[in] offset Offset to read from in data.
111  *
112  * @returns MBED_SUCCESS Success.
113  * MBED_ERROR_NOT_READY Not initialized.
114  * MBED_ERROR_READ_FAILED Unable to read from media.
115  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
116  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
117  * MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt.
118  * MBED_ERROR_ITEM_NOT_FOUND No such key.
119  */
120  virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL,
121  size_t offset = 0);
122 
123  /**
124  * @brief Get information of a given key. The returned info contains size and flags
125  *
126  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
127  * @param[out] info Returned information structure.
128  *
129  * @returns MBED_SUCCESS Success.
130  * MBED_ERROR_NOT_READY Not initialized.
131  * MBED_ERROR_READ_FAILED Unable to read from media.
132  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
133  * MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt.
134  * MBED_ERROR_ITEM_NOT_FOUND No such key.
135  */
136  virtual int get_info(const char *key, info_t *info);
137 
138  /**
139  * @brief Remove a TDBStore item by given key.
140  *
141  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
142  *
143  * @returns MBED_SUCCESS Success.
144  * MBED_ERROR_NOT_READY Not initialized.
145  * MBED_ERROR_READ_FAILED Unable to read from media.
146  * MBED_ERROR_WRITE_FAILED Unable to write to media.
147  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
148  * MBED_ERROR_MEDIA_FULL Not enough room on media.
149  * MBED_ERROR_ITEM_NOT_FOUND No such key.
150  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
151  */
152  virtual int remove(const char *key);
153 
154 
155  /**
156  * @brief Start an incremental TDBStore set sequence. This operation is blocking other operations.
157  * Any get/set/remove/iterator operation will be blocked until set_finalize is called.
158  *
159  * @param[out] handle Returned incremental set handle.
160  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
161  * @param[in] final_data_size Final value data size.
162  * @param[in] create_flags Flag mask.
163  *
164  * @returns MBED_SUCCESS Success.
165  * MBED_ERROR_NOT_READY Not initialized.
166  * MBED_ERROR_READ_FAILED Unable to read from media.
167  * MBED_ERROR_WRITE_FAILED Unable to write to media.
168  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
169  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
170  * MBED_ERROR_MEDIA_FULL Not enough room on media.
171  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
172  */
173  virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags);
174 
175  /**
176  * @brief Add data to incremental TDBStore set sequence. This operation is blocking other operations.
177  * Any get/set/remove operation will be blocked until set_finalize will be called.
178  *
179  * @param[in] handle Incremental set handle.
180  * @param[in] value_data Value data to add.
181  * @param[in] data_size Value data size.
182  *
183  * @returns MBED_SUCCESS Success.
184  * MBED_ERROR_NOT_READY Not initialized.
185  * MBED_ERROR_WRITE_FAILED Unable to write to media.
186  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
187  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
188  */
189  virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size);
190 
191  /**
192  * @brief Finalize an incremental KVStore set sequence.
193  *
194  * @param[in] handle Incremental set handle.
195  *
196  * @returns MBED_SUCCESS Success.
197  * MBED_ERROR_NOT_READY Not initialized.
198  * MBED_ERROR_WRITE_FAILED Unable to write to media.
199  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
200  */
201  virtual int set_finalize(set_handle_t handle);
202 
203  /**
204  * @brief Start an iteration over KVStore keys.
205  * There are no issues with any other operations while iterator is open.
206  *
207  * @param[out] it Returned iterator handle.
208  * @param[in] prefix Key prefix (null for all keys).
209  *
210  * @returns MBED_SUCCESS Success.
211  * MBED_ERROR_NOT_READY Not initialized.
212  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
213  */
214  virtual int iterator_open(iterator_t *it, const char *prefix = NULL);
215 
216  /**
217  * @brief Get next key in iteration.
218  * There are no issues with any other operations while iterator is open.
219  *
220  * @param[in] it Iterator handle.
221  * @param[in] key Buffer for returned key.
222  * @param[in] key_size Key buffer size.
223  *
224  * @returns MBED_SUCCESS Success.
225  * MBED_ERROR_NOT_READY Not initialized.
226  * MBED_ERROR_READ_FAILED Unable to read from block device.
227  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
228  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
229  * MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt.
230  * MBED_ERROR_ITEM_NOT_FOUND No more keys found.
231  */
232  virtual int iterator_next(iterator_t it, char *key, size_t key_size);
233 
234  /**
235  * @brief Close iteration.
236  *
237  * @param[in] it Iterator handle.
238  *
239  * @returns MBED_SUCCESS Success.
240  * MBED_ERROR_NOT_READY Not initialized.
241  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
242  */
243  virtual int iterator_close(iterator_t it);
244 
245  /**
246  * @brief Set data in reserved area, which is a special location for special data, such as ROT.
247  * The data written to reserved area can't be overwritten.
248  *
249  * @param[in] reserved_data Reserved data buffer.
250  * @param[in] reserved_data_buf_size
251  * Reserved data buffer size.
252  *
253  * @returns MBED_SUCCESS Success.
254  * MBED_ERROR_NOT_READY Not initialized.
255  * MBED_ERROR_READ_FAILED Unable to read from media.
256  * MBED_ERROR_WRITE_FAILED Unable to write to media.
257  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
258  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
259  */
260  virtual int reserved_data_set(const void *reserved_data, size_t reserved_data_buf_size);
261 
262  /**
263  * @brief Get data from reserved area, which is a special location for special data, such as ROT.
264  *
265  * @param[in] reserved_data Reserved data buffer.
266  * @param[in] reserved_data_buf_size
267  * Reserved data buffer size.
268  * @param[in] actual_data_size Return data size.
269  *
270  * @returns MBED_SUCCESS Success.
271  * MBED_ERROR_NOT_READY Not initialized.
272  * MBED_ERROR_READ_FAILED Unable to read from media.
273  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
274  * MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt.
275  * MBED_ERROR_ITEM_NOT_FOUND No reserved data was written.
276  */
277  virtual int reserved_data_get(void *reserved_data, size_t reserved_data_buf_size,
278  size_t *actual_data_size = 0);
279 
280 #if !defined(DOXYGEN_ONLY)
281 private:
282 
283  typedef struct {
284  uint32_t address;
285  size_t size;
286  } tdbstore_area_data_t;
287 
288  static const int _num_areas = 2;
289  static const int _max_open_iterators = 16;
290 
291  PlatformMutex _mutex;
292  PlatformMutex _inc_set_mutex;
293  void *_ram_table;
294  size_t _max_keys;
295  size_t _num_keys;
296  BlockDevice *_bd;
297  BufferedBlockDevice *_buff_bd;
298  uint32_t _free_space_offset;
299  uint32_t _master_record_offset;
300  uint32_t _master_record_size;
301  bool _is_initialized;
302  int _active_area;
303  uint16_t _active_area_version;
304  size_t _size;
305  tdbstore_area_data_t _area_params[_num_areas];
306  uint32_t _prog_size;
307  uint8_t *_work_buf;
308  size_t _work_buf_size;
309  char *_key_buf;
310  void *_inc_set_handle;
311  void *_iterator_table[_max_open_iterators];
312 
313  /**
314  * @brief Read a block from an area.
315  *
316  * @param[in] area Area.
317  * @param[in] offset Offset in area.
318  * @param[in] size Number of bytes to read.
319  * @param[in] buf Output buffer.
320  *
321  * @returns 0 for success, nonzero for failure.
322  */
323  int read_area(uint8_t area, uint32_t offset, uint32_t size, void *buf);
324 
325  /**
326  * @brief Write a block to an area.
327  *
328  * @param[in] area Area.
329  * @param[in] offset Offset in area.
330  * @param[in] size Number of bytes to write.
331  * @param[in] buf Input buffer.
332  *
333  * @returns 0 for success, non-zero for failure.
334  */
335  int write_area(uint8_t area, uint32_t offset, uint32_t size, const void *buf);
336 
337  /**
338  * @brief Reset an area (erase its start).
339  * This erases master record, but preserves the
340  * reserved area data.
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[out] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
369  * @param[out] 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 Before writing a record, check whether you are crossing an erase unit.
502  * If you do, check if it's erased, and erase it if not.
503  *
504  * @param[in] area Area.
505  * @param[in] offset Offset in area.
506  * @param[in] size Write size.
507  * @param[in] force_check Force checking.
508  *
509  * @returns 0 for success, nonzero for failure.
510  */
511  int check_erase_before_write(uint8_t area, uint32_t offset, uint32_t size,
512  bool force_check = false);
513 
514  /**
515  * @brief Get data from reserved area - worker function.
516  * This verifies that reserved data on both areas have
517  * correct checksums. If given pointer is not NULL, also
518  * write the reserved data to buffer. If checksums are not
519  * valid, return error code, and don't write anything to any
520  * pointers.
521  *
522  * @param[out] reserved_data Reserved data buffer (NULL to return nothing).
523  * @param[in] reserved_data_buf_size
524  * Reserved data buffer size.
525  * @param[out] actual_data_size If not NULL, return actual data size.
526  * @param[out] copy_trailer If not NULL, copy the trailer content to given buffer.
527  *
528  * @returns 0 on success or a negative error code on failure
529  */
530  int do_reserved_data_get(void *reserved_data, size_t reserved_data_buf_size,
531  size_t *actual_data_size = 0, void *copy_trailer = 0);
532 
533  /**
534  * @brief Update all iterators after adding or deleting of keys.
535  *
536  * @param[in] added True if added, false if deleted.
537  * @param[in] ram_table_ind RAM table index.
538  *
539  * @returns none
540  */
541  void update_all_iterators(bool added, uint32_t ram_table_ind);
542 
543 #endif
544 
545 };
546 /** @}*/
547 
548 } // namespace mbed
549 
550 #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.