ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
fds.h
Go to the documentation of this file.
1 
40 #ifndef FDS_H__
41 #define FDS_H__
42 
56 #include <stdint.h>
57 #include <stdbool.h>
58 #include "sdk_errors.h"
59 #include "app_util_platform.h"
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 
70 #define FDS_FILE_ID_INVALID (0xFFFF)
71 
72 
78 #define FDS_RECORD_KEY_DIRTY (0x0000)
79 
83 #define FDS_BOUND_NOT_SET (0)
84 
85 
88 enum
89 {
90  FDS_SUCCESS = NRF_SUCCESS,
106 };
107 
108 
112 typedef struct
113 {
114  uint16_t record_key;
115  uint16_t length_words;
116  uint16_t file_id;
117  uint16_t crc16;
118  /* The CRC is calculated over the entire record as stored in flash,
119  * including the record metadata except the CRC field itself.
120  */
121  uint32_t record_id;
122 } fds_header_t;
123 
124 
132 typedef struct
133 {
134  uint32_t record_id;
135  uint32_t const * p_record;
136  uint16_t gc_run_count;
139 
140 
146 typedef struct
147 {
149  void const * p_data;
151 
152 
154 typedef struct
155 {
156  uint16_t file_id;
157  uint16_t key;
158  struct
159  {
160  void const * p_data;
161  uint32_t length_words;
162  } data;
163 } fds_record_t;
164 
165 
171 typedef struct
172 {
173  uint16_t page;
174  uint16_t length_words;
176 
177 
184 typedef struct
185 {
186  uint32_t const * p_addr;
187  uint16_t page;
189 
190 
193 typedef enum
194 {
200  FDS_EVT_GC
202 
203 
205 
207 typedef struct
208 {
210  ret_code_t result;
211  union
212  {
213  struct
214  {
215  uint32_t record_id;
216  uint16_t file_id;
217  uint16_t record_key;
219  } write;
220  struct
221  {
222  uint32_t record_id;
223  uint16_t file_id;
224  uint16_t record_key;
225  } del;
226  };
227 } fds_evt_t;
228 
230 
231 
233 typedef struct
234 {
235  uint16_t pages_available;
236  uint16_t open_records;
237  uint16_t valid_records;
238  uint16_t dirty_records;
239  uint16_t words_reserved;
240 
242  uint16_t words_used;
243 
249  uint16_t largest_contig;
250 
256  uint16_t freeable_words;
257 
266 } fds_stat_t;
267 
268 
273 typedef void (*fds_cb_t)(fds_evt_t const * p_evt);
274 
275 
286 ret_code_t fds_register(fds_cb_t cb);
287 
288 
301 ret_code_t fds_init(void);
302 
303 
342  fds_record_t const * p_record);
343 
344 
364 ret_code_t fds_reserve(fds_reserve_token_t * p_token, uint16_t length_words);
365 
366 
377 
378 
417  fds_record_t const * p_record,
418  fds_reserve_token_t const * p_token);
419 
420 
441 
442 
462 ret_code_t fds_file_delete(uint16_t file_id);
463 
464 
502  fds_record_t const * p_record);
503 
504 
521  fds_find_token_t * p_token);
522 
523 
540 ret_code_t fds_record_find(uint16_t file_id,
541  uint16_t record_key,
542  fds_record_desc_t * p_desc,
543  fds_find_token_t * p_token);
544 
545 
561 ret_code_t fds_record_find_by_key(uint16_t record_key,
562  fds_record_desc_t * p_desc,
563  fds_find_token_t * p_token);
564 
565 
581 ret_code_t fds_record_find_in_file(uint16_t file_id,
582  fds_record_desc_t * p_desc,
583  fds_find_token_t * p_token);
584 
585 
610 ret_code_t fds_record_open(fds_record_desc_t * p_desc,
611  fds_flash_record_t * p_flash_record);
612 
613 
630 ret_code_t fds_record_close(fds_record_desc_t * p_desc);
631 
632 
645 ret_code_t fds_gc(void);
646 
647 
665  uint32_t record_id);
666 
667 
684 ret_code_t fds_record_id_from_desc(fds_record_desc_t const * p_desc,
685  uint32_t * p_record_id);
686 
687 
699 ret_code_t fds_stat(fds_stat_t * p_stat);
700 
720 ret_code_t flash_bounds_set (const uint32_t start_addr, const uint32_t end_addr);
721 
725 #ifdef __cplusplus
726 }
727 #endif
728 
729 #endif // FDS_H__
ret_code_t fds_record_write(fds_record_desc_t *p_desc, fds_record_t const *p_record)
Function for writing a record to flash.
ret_code_t fds_record_find(uint16_t file_id, uint16_t record_key, fds_record_desc_t *p_desc, fds_find_token_t *p_token)
Function for searching for records with a given record key in a file.
ret_code_t fds_init(void)
Function for initializing the module.
ret_code_t fds_record_write_reserved(fds_record_desc_t *p_desc, fds_record_t const *p_record, fds_reserve_token_t const *p_token)
Function for writing a record to a space in flash that was reserved using fds_reserve.
ret_code_t fds_reserve(fds_reserve_token_t *p_token, uint16_t length_words)
Function for reserving space in flash.
ANON_UNIONS_ENABLE
Definition: fds.h:204
ret_code_t fds_reserve_cancel(fds_reserve_token_t *p_token)
Function for canceling an fds_reserve operation.
ret_code_t fds_stat(fds_stat_t *p_stat)
Function for retrieving file system statistics.
ret_code_t fds_record_id_from_desc(fds_record_desc_t const *p_desc, uint32_t *p_record_id)
Function for obtaining a record ID from a record descriptor.
void(* fds_cb_t)(fds_evt_t const *p_evt)
FDS event handler function prototype.
Definition: fds.h:273
ret_code_t flash_bounds_set(const uint32_t start_addr, const uint32_t end_addr)
Set physical boundaries for FDS.
ret_code_t fds_descriptor_from_rec_id(fds_record_desc_t *p_desc, uint32_t record_id)
Function for obtaining a descriptor from a record ID.
ret_code_t fds_record_iterate(fds_record_desc_t *p_desc, fds_find_token_t *p_token)
Function for iterating through all records in flash.
ret_code_t fds_file_delete(uint16_t file_id)
Function for deleting all records in a file.
ret_code_t fds_gc(void)
Function for running garbage collection.
fds_evt_id_t
FDS event IDs.
Definition: fds.h:194
ret_code_t fds_record_find_in_file(uint16_t file_id, fds_record_desc_t *p_desc, fds_find_token_t *p_token)
Function for searching for any record in a file.
ret_code_t fds_record_update(fds_record_desc_t *p_desc, fds_record_t const *p_record)
Function for updating a record.
ret_code_t fds_record_open(fds_record_desc_t *p_desc, fds_flash_record_t *p_flash_record)
Function for opening a record for reading.
ret_code_t fds_record_find_by_key(uint16_t record_key, fds_record_desc_t *p_desc, fds_find_token_t *p_token)
Function for searching for records with a given record key.
ret_code_t fds_register(fds_cb_t cb)
Function for registering an FDS event handler.
ANON_UNIONS_DISABLE
Definition: fds.h:229
ret_code_t fds_record_close(fds_record_desc_t *p_desc)
Function for closing a record.
ret_code_t fds_record_delete(fds_record_desc_t *p_desc)
Function for deleting a record.
@ FDS_ERR_NOT_FOUND
Error. The record was not found.
Definition: fds.h:100
@ FDS_ERR_INTERNAL
Error. An internal error occurred.
Definition: fds.h:105
@ FDS_ERR_INVALID_ARG
Error. The parameter contains invalid data.
Definition: fds.h:94
@ FDS_ERR_NO_PAGES
Error. No flash pages are available.
Definition: fds.h:101
@ FDS_ERR_USER_LIMIT_REACHED
Error. The maximum number of users has been reached.
Definition: fds.h:102
@ FDS_ERR_NO_SPACE_IN_QUEUES
Error. There is no space in the internal queues.
Definition: fds.h:98
@ FDS_ERR_RECORD_TOO_LARGE
Error. The record exceeds the maximum allowed size.
Definition: fds.h:99
@ FDS_ERR_NULL_ARG
Error. The parameter is NULL.
Definition: fds.h:95
@ FDS_ERR_NO_OPEN_RECORDS
Error. The record is not open, so it cannot be closed.
Definition: fds.h:96
@ FDS_ERR_UNALIGNED_ADDR
Error. The input data is not aligned to a word boundary.
Definition: fds.h:93
@ FDS_ERR_BUSY
Error. The underlying flash subsystem was busy.
Definition: fds.h:104
@ FDS_ERR_NO_SPACE_IN_FLASH
Error. There is no space in flash memory.
Definition: fds.h:97
@ FDS_ERR_OPERATION_TIMEOUT
Error. The operation timed out.
Definition: fds.h:91
@ FDS_SUCCESS
The operation completed successfully.
Definition: fds.h:90
@ FDS_ERR_CRC_CHECK_FAILED
Error. The CRC check failed.
Definition: fds.h:103
@ FDS_ERR_NOT_INITIALIZED
Error. The module has not been initialized.
Definition: fds.h:92
@ FDS_EVT_DEL_FILE
Event for fds_file_delete.
Definition: fds.h:199
@ FDS_EVT_INIT
Event for fds_init.
Definition: fds.h:195
@ FDS_EVT_GC
Event for fds_gc.
Definition: fds.h:200
@ FDS_EVT_DEL_RECORD
Event for fds_record_delete.
Definition: fds.h:198
@ FDS_EVT_UPDATE
Event for fds_record_update.
Definition: fds.h:197
@ FDS_EVT_WRITE
Event for fds_record_write and fds_record_write_reserved.
Definition: fds.h:196
An FDS event.
Definition: fds.h:208
fds_evt_id_t id
The event ID. See fds_evt_id_t.
Definition: fds.h:209
ret_code_t result
The result of the operation related to this event.
Definition: fds.h:210
uint32_t record_id
Definition: fds.h:215
uint16_t file_id
Definition: fds.h:216
uint16_t record_key
Definition: fds.h:217
bool is_record_updated
Definition: fds.h:218
A token to keep information about the progress of fds_record_find, fds_record_find_by_key,...
Definition: fds.h:185
uint32_t const * p_addr
Definition: fds.h:186
uint16_t page
Definition: fds.h:187
Structure that can be used to read the contents of a record stored in flash.
Definition: fds.h:147
fds_header_t const * p_header
Location of the record header in flash.
Definition: fds.h:148
void const * p_data
Location of the record data in flash.
Definition: fds.h:149
The record metadata as stored in flash.
Definition: fds.h:113
uint16_t crc16
CRC16-CCITT check value.
Definition: fds.h:117
uint32_t record_id
The unique record ID (32 bits).
Definition: fds.h:121
uint16_t file_id
The ID of the file that the record belongs to.
Definition: fds.h:116
uint16_t length_words
The length of the record data (in 4-byte words).
Definition: fds.h:115
uint16_t record_key
The record key.
Definition: fds.h:114
The record descriptor structure that is used to manipulate records.
Definition: fds.h:133
uint16_t gc_run_count
Number of times garbage collection has been run.
Definition: fds.h:136
uint32_t record_id
The unique record ID.
Definition: fds.h:134
bool record_is_open
Whether the record is currently open.
Definition: fds.h:137
uint32_t const * p_record
The last known location of the record in flash.
Definition: fds.h:135
A record to be written to flash.
Definition: fds.h:155
uint32_t length_words
Definition: fds.h:161
uint16_t file_id
The ID of the file that the record belongs to.
Definition: fds.h:156
void const * p_data
Definition: fds.h:160
uint16_t key
The record key.
Definition: fds.h:157
A token to a reserved space in flash, created by fds_reserve.
Definition: fds.h:172
uint16_t length_words
The amount of space reserved (in 4-byte words).
Definition: fds.h:174
uint16_t page
The logical ID of the page where space was reserved.
Definition: fds.h:173
File system statistics.
Definition: fds.h:234
uint16_t words_reserved
The number of words reserved by fds_reserve().
Definition: fds.h:239
bool corruption
Filesystem corruption has been detected.
Definition: fds.h:265
uint16_t open_records
The number of open records.
Definition: fds.h:236
uint16_t freeable_words
The largest number of words that can be reclaimed by garbage collection.
Definition: fds.h:256
uint16_t valid_records
The number of valid records.
Definition: fds.h:237
uint16_t pages_available
The number of pages available.
Definition: fds.h:235
uint16_t largest_contig
The largest number of free contiguous words in the file system.
Definition: fds.h:249
uint16_t words_used
The number of words written to flash, including those reserved for future writes.
Definition: fds.h:242
uint16_t dirty_records
The number of deleted ("dirty") records.
Definition: fds.h:238