fds.h 32 KB

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