fds.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef FDS_H__
  13. #define FDS_H__
  14. /**
  15. * @defgroup flash_data_storage Flash Data Storage
  16. * @ingroup app_common
  17. * @{
  18. *
  19. * @brief Flash Data Storage (FDS).
  20. *
  21. * @details Flash Data Storage is a minimalistic, record-oriented file system for the on-chip
  22. * flash. Files are stored as a collection of records of variable length. FDS supports
  23. * synchronous read operations and asynchronous write operations (write, update,
  24. * and delete). FDS can be used from multiple threads.
  25. */
  26. #include <stdint.h>
  27. #include <stdbool.h>
  28. #include "sdk_errors.h"
  29. /**@brief Invalid file ID.
  30. *
  31. * This value must not be used as a file ID by the application.
  32. */
  33. #define FDS_FILE_ID_INVALID (0xFFFF)
  34. /**@brief Record key for deleted records.
  35. *
  36. * This key is used to flag a record as "dirty", which means that it should be removed during
  37. * the next garbage collection. This value must not be used as a record key by the application.
  38. */
  39. #define FDS_RECORD_KEY_DIRTY (0x0000)
  40. /**@brief FDS return values.
  41. */
  42. enum
  43. {
  44. FDS_SUCCESS = NRF_SUCCESS, //!< The operation completed successfully.
  45. FDS_ERR_OPERATION_TIMEOUT, //!< Error. The operation timed out.
  46. FDS_ERR_NOT_INITIALIZED, //!< Error. The module has not been initialized.
  47. FDS_ERR_UNALIGNED_ADDR, //!< Error. The input data is not aligned to a word boundary.
  48. FDS_ERR_INVALID_ARG, //!< Error. The parameter contains invalid data.
  49. FDS_ERR_NULL_ARG, //!< Error. The parameter is NULL.
  50. FDS_ERR_NO_OPEN_RECORDS, //!< Error. The record is not open, so it cannot be closed.
  51. FDS_ERR_NO_SPACE_IN_FLASH, //!< Error. There is no space in flash memory.
  52. FDS_ERR_NO_SPACE_IN_QUEUES, //!< Error. There is no space in the internal queues.
  53. FDS_ERR_RECORD_TOO_LARGE, //!< Error. The record exceeds the maximum allowed size.
  54. FDS_ERR_NOT_FOUND, //!< Error. The record was not found.
  55. FDS_ERR_NO_PAGES, //!< Error. No flash pages are available.
  56. FDS_ERR_USER_LIMIT_REACHED, //!< Error. The maximum number of users has been reached.
  57. FDS_ERR_CRC_CHECK_FAILED, //!< Error. The CRC check failed.
  58. FDS_ERR_BUSY, //!< Error. The underlying flash subsystem was busy.
  59. FDS_ERR_INTERNAL, //!< Error. An internal error occurred.
  60. };
  61. /**@brief Part of the record metadata.
  62. *
  63. * Contains the record key and the length of the record data.
  64. */
  65. typedef struct
  66. {
  67. uint16_t record_key; //!< The record key (must be in the range 0x0001 - 0xBFFF).
  68. uint16_t length_words; //!< The length of the record data (in 4-byte words).
  69. } fds_tl_t;
  70. /**@brief Part of the record metadata.
  71. *
  72. * Contains the ID of the file that the record belongs to and the CRC16 check value of the record.
  73. */
  74. typedef struct
  75. {
  76. uint16_t file_id; //!< The ID of the file that the record belongs to.
  77. /**@brief CRC16 check value.
  78. *
  79. * The CRC is calculated over the entire record as stored in flash (including the record
  80. * metadata except the CRC field itself). The CRC standard employed is CRC-16-CCITT.
  81. */
  82. uint16_t crc16;
  83. } fds_ic_t;
  84. /**@brief The record metadata as stored in flash.
  85. */
  86. typedef struct
  87. {
  88. fds_tl_t tl; //!< See @ref fds_tl_t.
  89. fds_ic_t ic; //!< See @ref fds_ic_t.
  90. uint32_t record_id; //!< The unique record ID (32 bits).
  91. } fds_header_t;
  92. /**@brief The record descriptor structure that is used to manipulate records.
  93. *
  94. * This structure is used by the FDS module. You must provide the descriptor to the module when
  95. * you manipulate existing records. However, you should never modify it or use any of its fields.
  96. *
  97. * @note Never reuse the same descriptor for different records.
  98. */
  99. typedef struct
  100. {
  101. uint32_t record_id; //!< The unique record ID.
  102. uint32_t const * p_record; //!< The last known location of the record in flash.
  103. uint16_t gc_run_count; //!< Number of times garbage collection has been run.
  104. bool record_is_open; //!< Whether the record is currently open.
  105. } fds_record_desc_t;
  106. /**@brief Structure that can be used to read the contents of a record stored in flash.
  107. *
  108. * This structure does not reflect the physical layout of a record in flash, but it points
  109. * to the locations where the record header (metadata) and the record data are stored.
  110. */
  111. typedef struct
  112. {
  113. fds_header_t const * p_header; //!< Location of the record header in flash.
  114. void const * p_data; //!< Location of the record data in flash.
  115. } fds_flash_record_t;
  116. /**@brief A chunk of record data to be written to flash.
  117. *
  118. * @p p_data must be aligned to a word boundary. Make sure to keep it in
  119. * memory until the operation has completed, which is indicated by the respective FDS event.
  120. */
  121. typedef struct
  122. {
  123. void const * p_data; //!< Pointer to the data to store. Must be word-aligned.
  124. uint16_t length_words; //!< Length of data pointed to by @p p_data (in 4-byte words).
  125. } fds_record_chunk_t;
  126. /**@brief A record to be written to flash.
  127. */
  128. typedef struct
  129. {
  130. uint16_t file_id; //!< The ID of the file that the record belongs to.
  131. uint16_t key; //!< The record key.
  132. struct
  133. {
  134. fds_record_chunk_t const * p_chunks; //!< The chunks that make up the record data.
  135. uint16_t num_chunks; //!< The number of chunks that make up the data.
  136. } data;
  137. } fds_record_t;
  138. /**@brief A token to a reserved space in flash, created by @ref fds_reserve.
  139. *
  140. * This token can be used to write the record in the reserved space (@ref fds_record_write_reserved)
  141. * or to cancel the reservation (@ref fds_reserve_cancel).
  142. */
  143. typedef struct
  144. {
  145. uint16_t page; //!< The logical ID of the page where space was reserved.
  146. uint16_t length_words; //!< The amount of space reserved (in 4-byte words).
  147. } fds_reserve_token_t;
  148. /**@brief A token to keep information about the progress of @ref fds_record_find,
  149. * @ref fds_record_find_by_key, and @ref fds_record_find_in_file.
  150. *
  151. * @note Always zero-initialize the token before using it for the first time.
  152. * @note Never reuse the same token to search for different records.
  153. */
  154. typedef struct
  155. {
  156. uint32_t const * p_addr;
  157. uint16_t page;
  158. } fds_find_token_t;
  159. /**@brief FDS event IDs.
  160. */
  161. typedef enum
  162. {
  163. FDS_EVT_INIT, //!< Event for @ref fds_init.
  164. FDS_EVT_WRITE, //!< Event for @ref fds_record_write and @ref fds_record_write_reserved.
  165. FDS_EVT_UPDATE, //!< Event for @ref fds_record_update.
  166. FDS_EVT_DEL_RECORD, //!< Event for @ref fds_record_delete.
  167. FDS_EVT_DEL_FILE, //!< Event for @ref fds_file_delete.
  168. FDS_EVT_GC //!< Event for @ref fds_gc.
  169. } fds_evt_id_t;
  170. #if defined(__CC_ARM)
  171. #pragma push
  172. #pragma anon_unions
  173. #elif defined(__ICCARM__)
  174. #pragma language=extended
  175. #elif defined(__GNUC__)
  176. /* anonymous unions are enabled by default */
  177. #endif
  178. /**@brief An FDS event.
  179. */
  180. typedef struct
  181. {
  182. fds_evt_id_t id; //!< The event ID. See @ref fds_evt_id_t.
  183. ret_code_t result; //!< The result of the operation related to this event.
  184. union
  185. {
  186. struct
  187. {
  188. /* Currently not used. */
  189. uint16_t pages_not_mounted;
  190. } init;
  191. struct
  192. {
  193. uint32_t record_id;
  194. uint16_t file_id;
  195. uint16_t record_key;
  196. bool is_record_updated;
  197. } write; //!< Information for @ref FDS_EVT_WRITE and @ref FDS_EVT_UPDATE events.
  198. struct
  199. {
  200. uint32_t record_id;
  201. uint16_t file_id;
  202. uint16_t record_key;
  203. uint16_t records_deleted_count;
  204. } del; //!< Information for @ref FDS_EVT_DEL_RECORD and @ref FDS_EVT_DEL_FILE events.
  205. struct
  206. {
  207. /* Currently not used. */
  208. uint16_t pages_skipped;
  209. uint16_t space_reclaimed;
  210. } gc;
  211. };
  212. } fds_evt_t;
  213. #if defined(__CC_ARM)
  214. #pragma pop
  215. #elif defined(__ICCARM__)
  216. /* leave anonymous unions enabled */
  217. #elif defined(__GNUC__)
  218. /* anonymous unions are enabled by default */
  219. #endif
  220. /**@brief File system statistics. */
  221. typedef struct
  222. {
  223. uint16_t open_records; //!< The number of open records.
  224. uint16_t valid_records; //!< The number of valid records.
  225. uint16_t dirty_records; //!< The number of deleted ("dirty") records.
  226. uint16_t words_reserved; //!< The number of words reserved by @ref fds_reserve().
  227. /**@brief The number of words written to flash, including those reserved for future writes.
  228. */
  229. uint16_t words_used;
  230. /**@brief The largest number of free contiguous words in the file system.
  231. *
  232. * This number determines the largest record that can be stored by FDS.
  233. * It takes into account all reservations for future writes.
  234. */
  235. uint16_t largest_contig;
  236. /**@brief The largest number of words that can be reclaimed by garbage collection.
  237. *
  238. * The actual amount of space freed by garbage collection might be less than this value if
  239. * records are open while garbage collection is run.
  240. */
  241. uint16_t freeable_words;
  242. } fds_stat_t;
  243. /**@brief FDS event handler function prototype.
  244. *
  245. * @param p_evt The event.
  246. */
  247. typedef void (*fds_cb_t)(fds_evt_t const * const p_evt);
  248. /**@brief Function for registering an FDS event handler.
  249. *
  250. * The maximum amount of handlers that can be registered can be configured by changing the value
  251. * of @ref FDS_MAX_USERS in fds_config.h.
  252. *
  253. * @param[in] cb The event handler function.
  254. *
  255. * @retval FDS_SUCCESS If the event handler was registered successfully.
  256. * @retval FDS_ERR_USER_LIMIT_REACHED If the maximum number of registered callbacks is reached.
  257. */
  258. ret_code_t fds_register(fds_cb_t cb);
  259. /**@brief Function for initializing the module.
  260. *
  261. * This function initializes the module and installs the file system (unless it is installed
  262. * already).
  263. *
  264. * This function is asynchronous. Completion is reported through an event. Make sure to call
  265. * @ref fds_register before calling @ref fds_init so that you receive the completion event.
  266. *
  267. * @retval FDS_SUCCESS If the operation was queued successfully.
  268. * @retval FDS_ERR_NO_PAGES If there is no space available in flash memory to install the
  269. * file system.
  270. */
  271. ret_code_t fds_init(void);
  272. /**@brief Function for writing a record to flash.
  273. *
  274. * There are no restrictions on the file ID and the record key, except that the record key must be
  275. * different from @ref FDS_RECORD_KEY_DIRTY and the file ID must be different from
  276. * @ref FDS_FILE_ID_INVALID. In particular, no restrictions are made regarding the uniqueness of
  277. * the file ID or the record key. All records with the same file ID are grouped into one file.
  278. * If no file with the specified ID exists, it is created. There can be multiple records with the
  279. * same record key in a file.
  280. *
  281. * Record data can consist of multiple chunks. The data must be aligned to a 4 byte boundary, and
  282. * because it is not buffered internally, it must be kept in memory until the callback for the
  283. * operation has been received. The length of the data must not exceed @ref FDS_VIRTUAL_PAGE_SIZE
  284. * words minus 14 bytes.
  285. *
  286. * This function is asynchronous. Completion is reported through an event that is sent to
  287. * the registered event handler function.
  288. *
  289. * @param[out] p_desc The descriptor of the record that was written. Pass NULL if you do not
  290. * need the descriptor.
  291. * @param[in] p_record The record to be written to flash.
  292. *
  293. * @retval FDS_SUCCESS If the operation was queued successfully.
  294. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  295. * @retval FDS_ERR_NULL_ARG If @p p_record is NULL.
  296. * @retval FDS_ERR_INVALID_ARG If the file ID or the record key is invalid.
  297. * @retval FDS_ERR_UNALIGNED_ADDR If the record data is not aligned to a 4 byte boundary.
  298. * @retval FDS_ERR_RECORD_TOO_LARGE If the record data exceeds the maximum length.
  299. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full or there are more record
  300. * chunks than can be buffered.
  301. * @retval FDS_ERR_NO_SPACE_IN_FLASH If there is not enough free space in flash to store the
  302. * record.
  303. */
  304. ret_code_t fds_record_write(fds_record_desc_t * const p_desc,
  305. fds_record_t const * const p_record);
  306. /**@brief Function for reserving space in flash.
  307. *
  308. * This function can be used to reserve space in flash memory. To write a record into the reserved
  309. * space, use @ref fds_record_write_reserved. Alternatively, use @ref fds_reserve_cancel to cancel
  310. * a reservation.
  311. *
  312. * Note that this function does not write any data to flash.
  313. *
  314. * @param[out] p_token A token that can be used to write a record in the reserved space or
  315. * cancel the reservation.
  316. * @param[in] length_words The length of the record data (in 4-byte words).
  317. *
  318. * @retval FDS_SUCCESS If the flash space was reserved successfully.
  319. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  320. * @retval FDS_ERR_NULL_ARG If @p p_token is NULL instead of a valid token address.
  321. * @retval FDS_ERR_RECORD_TOO_LARGE If the record length exceeds the maximum length.
  322. * @retval FDS_ERR_NO_SPACE_IN_FLASH If there is not enough free space in flash to store the
  323. * record.
  324. */
  325. ret_code_t fds_reserve(fds_reserve_token_t * const p_token, uint16_t length_words);
  326. /**@brief Function for canceling an @ref fds_reserve operation.
  327. *
  328. * @param[in] p_token The token that identifies the reservation, produced by @ref fds_reserve.
  329. *
  330. * @retval FDS_SUCCESS If the reservation was canceled.
  331. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  332. * @retval FDS_ERR_NULL_ARG If @p p_token is NULL instead of a valid token address.
  333. * @retval FDS_ERR_INVALID_ARG If @p p_token contains invalid data.
  334. */
  335. ret_code_t fds_reserve_cancel(fds_reserve_token_t * const p_token);
  336. /**@brief Function for writing a record to a space in flash that was reserved using
  337. * @ref fds_reserve.
  338. *
  339. * There are no restrictions on the file ID and the record key, except that the record key must be
  340. * different from @ref FDS_RECORD_KEY_DIRTY and the file ID must be different from
  341. * @ref FDS_FILE_ID_INVALID. In particular, no restrictions are made regarding the uniqueness of
  342. * the file ID or the record key. All records with the same file ID are grouped into one file.
  343. * If no file with the specified ID exists, it is created. There can be multiple records with the
  344. * same record key in a file.
  345. *
  346. * Record data can consist of multiple chunks. The data must be aligned to a 4 byte boundary, and
  347. * because it is not buffered internally, it must be kept in memory until the callback for the
  348. * operation has been received. The length of the data must not exceed @ref FDS_VIRTUAL_PAGE_SIZE
  349. * words minus 14 bytes.
  350. *
  351. * This function is asynchronous. Completion is reported through an event that is sent to the
  352. * registered event handler function.
  353. *
  354. * @note
  355. * This function behaves similarly to @ref fds_record_write, with the exception that it never
  356. * fails with the error @ref FDS_ERR_NO_SPACE_IN_FLASH.
  357. *
  358. * @param[out] p_desc The descriptor of the record that was written. Pass NULL if you do not
  359. * need the descriptor.
  360. * @param[in] p_record The record to be written to flash.
  361. * @param[in] p_token The token that identifies the space reserved in flash.
  362. *
  363. * @retval FDS_SUCCESS If the operation was queued successfully.
  364. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  365. * @retval FDS_ERR_NULL_ARG If @p p_token is NULL instead of a valid token address.
  366. * @retval FDS_ERR_INVALID_ARG If the file ID or the record key is invalid.
  367. * @retval FDS_ERR_UNALIGNED_ADDR If the record data is not aligned to a 4 byte boundary.
  368. * @retval FDS_ERR_RECORD_TOO_LARGE If the record data exceeds the maximum length.
  369. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full or there are more record
  370. * chunks than can be buffered.
  371. */
  372. ret_code_t fds_record_write_reserved(fds_record_desc_t * const p_desc,
  373. fds_record_t const * const p_record,
  374. fds_reserve_token_t const * const p_token);
  375. /**@brief Function for deleting a record.
  376. *
  377. * Deleted records cannot be located using @ref fds_record_find, @ref fds_record_find_by_key, or
  378. * @ref fds_record_find_in_file. Additionally, they can no longer be opened using
  379. * @ref fds_record_open.
  380. *
  381. * Note that deleting a record does not free the space it occupies in flash memory.
  382. * To reclaim flash space used by deleted records, call @ref fds_gc to run garbage collection.
  383. *
  384. * This function is asynchronous. Completion is reported through an event that is sent to the
  385. * registered event handler function.
  386. *
  387. * @param[in] p_desc The descriptor of the record that should be deleted.
  388. *
  389. * @retval FDS_SUCCESS If the operation was queued successfully.
  390. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  391. * @retval FDS_ERR_NULL_ARG If the specified record descriptor @p p_desc is NULL.
  392. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full.
  393. */
  394. ret_code_t fds_record_delete(fds_record_desc_t * const p_desc);
  395. /**@brief Function for deleting all records in a file.
  396. *
  397. * This function deletes a file, including all its records. Deleted records cannot be located
  398. * using @ref fds_record_find, @ref fds_record_find_by_key, or @ref fds_record_find_in_file.
  399. * Additionally, they can no longer be opened using @ref fds_record_open.
  400. *
  401. * Note that deleting records does not free the space they occupy in flash memory.
  402. * To reclaim flash space used by deleted records, call @ref fds_gc to run garbage collection.
  403. *
  404. * This function is asynchronous. Completion is reported through an event that is sent to the
  405. * registered event handler function.
  406. *
  407. * @param[in] file_id The ID of the file to be deleted.
  408. *
  409. * @retval FDS_SUCCESS If the operation was queued successfully.
  410. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  411. * @retval FDS_ERR_INVALID_ARG If the specified @p file_id is invalid.
  412. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full.
  413. */
  414. ret_code_t fds_file_delete(uint16_t file_id);
  415. /**@brief Function for updating a record.
  416. *
  417. * Updating a record first writes a new record (@p p_record) to flash and then deletes the
  418. * old record (identified by @p p_desc).
  419. *
  420. * There are no restrictions on the file ID and the record key, except that the record key must be
  421. * different from @ref FDS_RECORD_KEY_DIRTY and the file ID must be different from
  422. * @ref FDS_FILE_ID_INVALID. In particular, no restrictions are made regarding the uniqueness of
  423. * the file ID or the record key. All records with the same file ID are grouped into one file.
  424. * If no file with the specified ID exists, it is created. There can be multiple records with the
  425. * same record key in a file.
  426. *
  427. * Record data can consist of multiple chunks. The data must be aligned to a 4 byte boundary, and
  428. * because it is not buffered internally, it must be kept in memory until the callback for the
  429. * operation has been received. The length of the data must not exceed @ref FDS_VIRTUAL_PAGE_SIZE
  430. * words minus 14 bytes.
  431. *
  432. * This function is asynchronous. Completion is reported through an event that is sent to the
  433. * registered event handler function.
  434. *
  435. * @param[in, out] p_desc The descriptor of the record to update. When the function
  436. * returns with FDS_SUCCESS, this parameter contains the
  437. * descriptor of the newly written record.
  438. * @param[in] p_record The updated record to be written to flash.
  439. *
  440. * @retval FDS_SUCCESS If the operation was queued successfully.
  441. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  442. * @retval FDS_ERR_INVALID_ARG If the file ID or the record key is invalid.
  443. * @retval FDS_ERR_UNALIGNED_ADDR If the record data is not aligned to a 4 byte boundary.
  444. * @retval FDS_ERR_RECORD_TOO_LARGE If the record data exceeds the maximum length.
  445. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full or there are more record
  446. * chunks than can be buffered.
  447. * @retval FDS_ERR_NO_SPACE_IN_FLASH If there is not enough free space in flash to store the
  448. * updated record.
  449. */
  450. ret_code_t fds_record_update(fds_record_desc_t * const p_desc,
  451. fds_record_t const * const p_record);
  452. /**@brief Function for iterating through all records in flash.
  453. *
  454. * To search for the next record, call the function again and supply the same @ref fds_find_token_t
  455. * structure to resume searching from the last record that was found.
  456. *
  457. * Note that the order with which records are iterated is not defined.
  458. *
  459. * @param[out] p_desc The descriptor of the record that was found.
  460. * @param[out] p_token A token containing information about the progress of the operation.
  461. *
  462. * @retval FDS_SUCCESS If a record was found.
  463. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  464. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_token is NULL.
  465. * @retval FDS_ERR_NOT_FOUND If no matching record was found.
  466. */
  467. ret_code_t fds_record_iterate(fds_record_desc_t * const p_desc,
  468. fds_find_token_t * const p_token);
  469. /**@brief Function for searching for records with a given record key in a file.
  470. *
  471. * This function finds the first record in a file that has the given record key. To search for the
  472. * next record with the same key in the file, call the function again and supply the same
  473. * @ref fds_find_token_t structure to resume searching from the last record that was found.
  474. *
  475. * @param[in] file_id The file ID.
  476. * @param[in] record_key The record key.
  477. * @param[out] p_desc The descriptor of the record that was found.
  478. * @param[out] p_token A token containing information about the progress of the operation.
  479. *
  480. * @retval FDS_SUCCESS If a record was found.
  481. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  482. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_token is NULL.
  483. * @retval FDS_ERR_NOT_FOUND If no matching record was found.
  484. */
  485. ret_code_t fds_record_find(uint16_t file_id,
  486. uint16_t record_key,
  487. fds_record_desc_t * const p_desc,
  488. fds_find_token_t * const p_token);
  489. /**@brief Function for searching for records with a given record key.
  490. *
  491. * This function finds the first record with a given record key, independent of the file it
  492. * belongs to. To search for the next record with the same key, call the function again and supply
  493. * the same @ref fds_find_token_t structure to resume searching from the last record that was found.
  494. *
  495. * @param[in] record_key The record key.
  496. * @param[out] p_desc The descriptor of the record that was found.
  497. * @param[out] p_token A token containing information about the progress of the operation.
  498. *
  499. * @retval FDS_SUCCESS If a record was found.
  500. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  501. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_token is NULL.
  502. * @retval FDS_ERR_NOT_FOUND If no record with the given key was found.
  503. */
  504. ret_code_t fds_record_find_by_key(uint16_t record_key,
  505. fds_record_desc_t * const p_desc,
  506. fds_find_token_t * const p_token);
  507. /**@brief Function for searching for any record in a file.
  508. *
  509. * This function finds the first record in a file, independent of its record key.
  510. * To search for the next record in the same file, call the function again and supply the same
  511. * @ref fds_find_token_t structure to resume searching from the last record that was found.
  512. *
  513. * @param[in] file_id The file ID.
  514. * @param[out] p_desc The descriptor of the record that was found.
  515. * @param[out] p_token A token containing information about the progress of the operation.
  516. *
  517. * @retval FDS_SUCCESS If a record was found.
  518. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  519. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_token is NULL.
  520. * @retval FDS_ERR_NOT_FOUND If no matching record was found.
  521. */
  522. ret_code_t fds_record_find_in_file(uint16_t file_id,
  523. fds_record_desc_t * const p_desc,
  524. fds_find_token_t * const p_token);
  525. /**@brief Function for opening a record for reading.
  526. *
  527. * This function opens a record that is stored in flash, so that it can be read. The function
  528. * initializes an @ref fds_flash_record_t structure, which can be used to access the record data as
  529. * well as its associated metadata. The pointers provided in the @ref fds_flash_record_t structure
  530. * are pointers to flash memory.
  531. *
  532. * Opening a record with @ref fds_record_open prevents garbage collection to run on the virtual
  533. * flash page in which record is stored, so that the contents of the memory pointed by fields in
  534. * @ref fds_flash_record_t are guaranteed to remain unmodified as long as the record is kept open.
  535. *
  536. * When you are done reading a record, call @ref fds_record_close to close it. Garbage collection
  537. * can then reclaim space on the virtual page where the record is stored. Note that you must
  538. * provide the same descriptor for @ref fds_record_close as you did for this function.
  539. *
  540. * @param[in] p_desc The descriptor of the record to open.
  541. * @param[out] p_flash_record The record, as stored in flash.
  542. *
  543. * @retval FDS_SUCCESS If the record was opened successfully.
  544. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_flash_record is NULL.
  545. * @retval FDS_ERR_NOT_FOUND If the record was not found. It might have been deleted, or
  546. * it might not have been written yet.
  547. * @retval FDS_ERR_CRC_CHECK_FAILED If the CRC check for the record failed.
  548. */
  549. ret_code_t fds_record_open(fds_record_desc_t * const p_desc,
  550. fds_flash_record_t * const p_flash_record);
  551. /**@brief Function for closing a record.
  552. *
  553. * Closing a record allows garbage collection to run on the virtual page in which the record is
  554. * stored (if no other records remain open on that page). The descriptor passed as an argument
  555. * must be the same as the one used to open the record using @ref fds_record_open.
  556. *
  557. * Note that closing a record does not invalidate its descriptor. You can still supply the
  558. * descriptor to all functions that accept a record descriptor as a parameter.
  559. *
  560. * @param[in] p_desc The descriptor of the record to close.
  561. *
  562. * @retval FDS_SUCCESS If the record was closed successfully.
  563. * @retval FDS_ERR_NULL_ARG If @p p_desc is NULL.
  564. * @retval FDS_ERR_NO_OPEN_RECORDS If the record is not open.
  565. * @retval FDS_ERR_NOT_FOUND If the record could not be found.
  566. */
  567. ret_code_t fds_record_close(fds_record_desc_t * const p_desc);
  568. /**@brief Function for running garbage collection.
  569. *
  570. * Garbage collection reclaims the flash space that is occupied by records that have been deleted,
  571. * or that failed to be completely written due to, for example, a power loss.
  572. *
  573. * This function is asynchronous. Completion is reported through an event that is sent to the
  574. * registered event handler function.
  575. *
  576. * @retval FDS_SUCCESS If the operation was queued successfully.
  577. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  578. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full.
  579. */
  580. ret_code_t fds_gc(void);
  581. /**@brief Function for obtaining a descriptor from a record ID.
  582. *
  583. * This function can be used to reconstruct a descriptor from a record ID, like the one that is
  584. * passed to the callback function.
  585. *
  586. * @note
  587. * This function does not check whether a record with the given record ID exists.
  588. * If a non-existing record ID is supplied, the resulting descriptor is invalid and will cause
  589. * other functions to fail when it is supplied as parameter.
  590. *
  591. * @param[out] p_desc The descriptor of the record with the given record ID.
  592. * @param[in] record_id The record ID for which a descriptor should be returned.
  593. *
  594. * @retval FDS_SUCCESS If a descriptor was returned.
  595. * @retval FDS_ERR_NULL_ARG If @p p_desc is NULL.
  596. */
  597. ret_code_t fds_descriptor_from_rec_id(fds_record_desc_t * const p_desc,
  598. uint32_t record_id);
  599. /**@brief Function for obtaining a record ID from a record descriptor.
  600. *
  601. * This function can be used to extract a record ID from a descriptor. For example, you could use
  602. * it in the callback function to compare the record ID of an event to the record IDs of the
  603. * records for which you have a descriptor.
  604. *
  605. * @warning
  606. * This function does not check whether the record descriptor is valid. If the descriptor is not
  607. * initialized or has been tampered with, the resulting record ID might be invalid.
  608. *
  609. * @param[in] p_desc The descriptor from which the record ID should be extracted.
  610. * @param[out] p_record_id The record ID that is contained in the given descriptor.
  611. *
  612. * @retval FDS_SUCCESS If a record ID was returned.
  613. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_record_id is NULL.
  614. */
  615. ret_code_t fds_record_id_from_desc(fds_record_desc_t const * const p_desc,
  616. uint32_t * const p_record_id);
  617. /**@brief Function for retrieving file system statistics.
  618. *
  619. * This function retrieves file system statistics, such as the number of open records, the space
  620. * that can be reclaimed by garbage collection, and others.
  621. *
  622. * @param[out] p_stat File system statistics.
  623. *
  624. * @retval FDS_SUCCESS If the statistics were returned successfully.
  625. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  626. * @retval FDS_ERR_NULL_ARG If @p p_stat is NULL.
  627. */
  628. ret_code_t fds_stat(fds_stat_t * const p_stat);
  629. #if defined(FDS_CRC_ENABLED)
  630. /**@brief Function for enabling and disabling CRC verification for write operations.
  631. *
  632. * CRC verification ensures that data that is queued for writing does not change before the write
  633. * actually happens. Use this function to enable or disable CRC verification. If verification is
  634. * enabled, the error @ref FDS_ERR_CRC_CHECK_FAILED is returned in the event for
  635. * @ref fds_record_write, @ref fds_record_write_reserved, or @ref fds_record_update if
  636. * verification fails.
  637. *
  638. * @note
  639. * CRC verification is enabled or disabled globally, thus for all users of the FDS module.
  640. *
  641. * @param[in] enabled 1 to enable CRC verification. 0 to disable CRC verification.
  642. *
  643. * @retval FDS_SUCCESS If CRC verification was enabled or disabled successfully.
  644. */
  645. ret_code_t fds_verify_crc_on_writes(bool enabled);
  646. #endif
  647. /** @} */
  648. #endif // FDS_H__