fds.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /**
  2. * Copyright (c) 2015 - 2018, 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_SUCCESS = NRF_SUCCESS, //!< The operation completed successfully.
  77. FDS_ERR_OPERATION_TIMEOUT, //!< Error. The operation timed out.
  78. FDS_ERR_NOT_INITIALIZED, //!< Error. The module has not been initialized.
  79. FDS_ERR_UNALIGNED_ADDR, //!< Error. The input data is not aligned to a word boundary.
  80. FDS_ERR_INVALID_ARG, //!< Error. The parameter contains invalid data.
  81. FDS_ERR_NULL_ARG, //!< Error. The parameter is NULL.
  82. FDS_ERR_NO_OPEN_RECORDS, //!< Error. The record is not open, so it cannot be closed.
  83. FDS_ERR_NO_SPACE_IN_FLASH, //!< Error. There is no space in flash memory.
  84. FDS_ERR_NO_SPACE_IN_QUEUES, //!< Error. There is no space in the internal queues.
  85. FDS_ERR_RECORD_TOO_LARGE, //!< Error. The record exceeds the maximum allowed size.
  86. FDS_ERR_NOT_FOUND, //!< Error. The record was not found.
  87. FDS_ERR_NO_PAGES, //!< Error. No flash pages are available.
  88. FDS_ERR_USER_LIMIT_REACHED, //!< Error. The maximum number of users has been reached.
  89. FDS_ERR_CRC_CHECK_FAILED, //!< Error. The CRC check failed.
  90. FDS_ERR_BUSY, //!< Error. The underlying flash subsystem was busy.
  91. FDS_ERR_INTERNAL, //!< Error. An internal error occurred.
  92. };
  93. /**@brief The record metadata as stored in flash.
  94. * @warning Do not edit or reorder the fields in this structure.
  95. */
  96. typedef struct
  97. {
  98. uint16_t record_key; //!< The record key.
  99. uint16_t length_words; //!< The length of the record data (in 4-byte words).
  100. uint16_t file_id; //!< The ID of the file that the record belongs to.
  101. uint16_t crc16; //!< CRC16-CCITT check value.
  102. /* The CRC is calculated over the entire record as stored in flash,
  103. * including the record metadata except the CRC field itself.
  104. */
  105. uint32_t record_id; //!< The unique record ID (32 bits).
  106. } fds_header_t;
  107. /**@brief The record descriptor structure that is used to manipulate records.
  108. *
  109. * This structure is used by the FDS module. You must provide the descriptor to the module when
  110. * you manipulate existing records. However, you should never modify it or use any of its fields.
  111. *
  112. * @note Never reuse the same descriptor for different records.
  113. */
  114. typedef struct
  115. {
  116. uint32_t record_id; //!< The unique record ID.
  117. uint32_t const * p_record; //!< The last known location of the record in flash.
  118. uint16_t gc_run_count; //!< Number of times garbage collection has been run.
  119. bool record_is_open; //!< Whether the record is currently open.
  120. } fds_record_desc_t;
  121. /**@brief Structure that can be used to read the contents of a record stored in flash.
  122. *
  123. * This structure does not reflect the physical layout of a record in flash, but it points
  124. * to the locations where the record header (metadata) and the record data are stored.
  125. */
  126. typedef struct
  127. {
  128. fds_header_t const * p_header; //!< Location of the record header in flash.
  129. void const * p_data; //!< Location of the record data in flash.
  130. } fds_flash_record_t;
  131. /**@brief A record to be written to flash. */
  132. typedef struct
  133. {
  134. uint16_t file_id; //!< The ID of the file that the record belongs to.
  135. uint16_t key; //!< The record key.
  136. struct
  137. {
  138. void const * p_data;
  139. uint32_t length_words;
  140. } data;
  141. } fds_record_t;
  142. /**@brief A token to a reserved space in flash, created by @ref fds_reserve.
  143. *
  144. * This token can be used to write the record in the reserved space (@ref fds_record_write_reserved)
  145. * or to cancel the reservation (@ref fds_reserve_cancel).
  146. */
  147. typedef struct
  148. {
  149. uint16_t page; //!< The logical ID of the page where space was reserved.
  150. uint16_t length_words; //!< The amount of space reserved (in 4-byte words).
  151. } fds_reserve_token_t;
  152. /**@brief A token to keep information about the progress of @ref fds_record_find,
  153. * @ref fds_record_find_by_key, and @ref fds_record_find_in_file.
  154. *
  155. * @note Always zero-initialize the token before using it for the first time.
  156. * @note Never reuse the same token to search for different records.
  157. */
  158. typedef struct
  159. {
  160. uint32_t const * p_addr;
  161. uint16_t page;
  162. } fds_find_token_t;
  163. /**@brief FDS event IDs.
  164. */
  165. typedef enum
  166. {
  167. FDS_EVT_INIT, //!< Event for @ref fds_init.
  168. FDS_EVT_WRITE, //!< Event for @ref fds_record_write and @ref fds_record_write_reserved.
  169. FDS_EVT_UPDATE, //!< Event for @ref fds_record_update.
  170. FDS_EVT_DEL_RECORD, //!< Event for @ref fds_record_delete.
  171. FDS_EVT_DEL_FILE, //!< Event for @ref fds_file_delete.
  172. FDS_EVT_GC //!< Event for @ref fds_gc.
  173. } fds_evt_id_t;
  174. ANON_UNIONS_ENABLE;
  175. /**@brief An FDS event. */
  176. typedef struct
  177. {
  178. fds_evt_id_t id; //!< The event ID. See @ref fds_evt_id_t.
  179. ret_code_t result; //!< The result of the operation related to this event.
  180. union
  181. {
  182. struct
  183. {
  184. uint32_t record_id;
  185. uint16_t file_id;
  186. uint16_t record_key;
  187. bool is_record_updated;
  188. } write; //!< Information for @ref FDS_EVT_WRITE and @ref FDS_EVT_UPDATE events.
  189. struct
  190. {
  191. uint32_t record_id;
  192. uint16_t file_id;
  193. uint16_t record_key;
  194. } del; //!< Information for @ref FDS_EVT_DEL_RECORD and @ref FDS_EVT_DEL_FILE events.
  195. };
  196. } fds_evt_t;
  197. ANON_UNIONS_DISABLE;
  198. /**@brief File system statistics. */
  199. typedef struct
  200. {
  201. uint16_t pages_available; //!< The number of pages available.
  202. uint16_t open_records; //!< The number of open records.
  203. uint16_t valid_records; //!< The number of valid records.
  204. uint16_t dirty_records; //!< The number of deleted ("dirty") records.
  205. uint16_t words_reserved; //!< The number of words reserved by @ref fds_reserve().
  206. /**@brief The number of words written to flash, including those reserved for future writes. */
  207. uint16_t words_used;
  208. /**@brief The largest number of free contiguous words in the file system.
  209. *
  210. * This number indicates the largest record that can be stored by FDS.
  211. * It takes into account all reservations for future writes.
  212. */
  213. uint16_t largest_contig;
  214. /**@brief The largest number of words that can be reclaimed by garbage collection.
  215. *
  216. * The actual amount of space freed by garbage collection might be less than this value if
  217. * records are open while garbage collection is run.
  218. */
  219. uint16_t freeable_words;
  220. /**@brief Filesystem corruption has been detected.
  221. *
  222. * One or more corrupted records were detected. FDS will heal the filesystem automatically
  223. * next time garbage collection is run, but some data may be lost.
  224. *
  225. * @note: This flag is unrelated to CRC failures.
  226. */
  227. bool corruption;
  228. } fds_stat_t;
  229. /**@brief FDS event handler function prototype.
  230. *
  231. * @param p_evt The event.
  232. */
  233. typedef void (*fds_cb_t)(fds_evt_t const * p_evt);
  234. /**@brief Function for registering an FDS event handler.
  235. *
  236. * The maximum amount of handlers that can be registered can be configured by changing the value
  237. * of @ref FDS_MAX_USERS in fds_config.h.
  238. *
  239. * @param[in] cb The event handler function.
  240. *
  241. * @retval FDS_SUCCESS If the event handler was registered successfully.
  242. * @retval FDS_ERR_USER_LIMIT_REACHED If the maximum number of registered callbacks is reached.
  243. */
  244. ret_code_t fds_register(fds_cb_t cb);
  245. /**@brief Function for initializing the module.
  246. *
  247. * This function initializes the module and installs the file system (unless it is installed
  248. * already).
  249. *
  250. * This function is asynchronous. Completion is reported through an event. Make sure to call
  251. * @ref fds_register before calling @ref fds_init so that you receive the completion event.
  252. *
  253. * @retval FDS_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 an event that is sent to
  276. * 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.
  281. *
  282. * @retval FDS_SUCCESS If the operation was queued successfully.
  283. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  284. * @retval FDS_ERR_NULL_ARG If @p p_record is NULL.
  285. * @retval FDS_ERR_INVALID_ARG If the file ID or the record key is invalid.
  286. * @retval FDS_ERR_UNALIGNED_ADDR If the record data is not aligned to a 4 byte boundary.
  287. * @retval FDS_ERR_RECORD_TOO_LARGE If the record data exceeds the maximum length.
  288. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full or there are more record
  289. * chunks than can be buffered.
  290. * @retval FDS_ERR_NO_SPACE_IN_FLASH If there is not enough free space in flash to store the
  291. * record.
  292. */
  293. ret_code_t fds_record_write(fds_record_desc_t * p_desc,
  294. fds_record_t const * p_record);
  295. /**@brief Function for reserving space in flash.
  296. *
  297. * This function can be used to reserve space in flash memory. To write a record into the reserved
  298. * space, use @ref fds_record_write_reserved. Alternatively, use @ref fds_reserve_cancel to cancel
  299. * a reservation.
  300. *
  301. * Note that this function does not write any data to flash.
  302. *
  303. * @param[out] p_token A token that can be used to write a record in the reserved space or
  304. * cancel the reservation.
  305. * @param[in] length_words The length of the record data (in 4-byte words).
  306. *
  307. * @retval FDS_SUCCESS If the flash space was reserved successfully.
  308. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  309. * @retval FDS_ERR_NULL_ARG If @p p_token is NULL instead of a valid token address.
  310. * @retval FDS_ERR_RECORD_TOO_LARGE If the record length exceeds the maximum length.
  311. * @retval FDS_ERR_NO_SPACE_IN_FLASH If there is not enough free space in flash to store the
  312. * record.
  313. */
  314. ret_code_t fds_reserve(fds_reserve_token_t * p_token, uint16_t length_words);
  315. /**@brief Function for canceling an @ref fds_reserve operation.
  316. *
  317. * @param[in] p_token The token that identifies the reservation, produced by @ref fds_reserve.
  318. *
  319. * @retval FDS_SUCCESS If the reservation was canceled.
  320. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  321. * @retval FDS_ERR_NULL_ARG If @p p_token is NULL instead of a valid token address.
  322. * @retval FDS_ERR_INVALID_ARG If @p p_token contains invalid data.
  323. */
  324. ret_code_t fds_reserve_cancel(fds_reserve_token_t * p_token);
  325. /**@brief Function for writing a record to a space in flash that was reserved using
  326. * @ref fds_reserve.
  327. *
  328. * There are no restrictions on the file ID and the record key, except that the record key must be
  329. * different from @ref FDS_RECORD_KEY_DIRTY and the file ID must be different from
  330. * @ref FDS_FILE_ID_INVALID. In particular, no restrictions are made regarding the uniqueness of
  331. * the file ID or the record key. All records with the same file ID are grouped into one file.
  332. * If no file with the specified ID exists, it is created. There can be multiple records with the
  333. * same record key in a file.
  334. *
  335. * Record data can consist of multiple chunks. The data must be aligned to a 4 byte boundary, and
  336. * because it is not buffered internally, it must be kept in memory until the callback for the
  337. * operation has been received. The length of the data must not exceed @ref FDS_VIRTUAL_PAGE_SIZE
  338. * words minus 14 bytes.
  339. *
  340. * This function is asynchronous. Completion is reported through an event that is sent to the
  341. * registered event handler function.
  342. *
  343. * @note
  344. * This function behaves similarly to @ref fds_record_write, with the exception that it never
  345. * fails with the error @ref FDS_ERR_NO_SPACE_IN_FLASH.
  346. *
  347. * @param[out] p_desc The descriptor of the record that was written. Pass NULL if you do not
  348. * need the descriptor.
  349. * @param[in] p_record The record to be written to flash.
  350. * @param[in] p_token The token that identifies the space reserved in flash.
  351. *
  352. * @retval FDS_SUCCESS If the operation was queued successfully.
  353. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  354. * @retval FDS_ERR_NULL_ARG If @p p_token is NULL instead of a valid token address.
  355. * @retval FDS_ERR_INVALID_ARG If the file ID or the record key is invalid.
  356. * @retval FDS_ERR_UNALIGNED_ADDR If the record data is not aligned to a 4 byte boundary.
  357. * @retval FDS_ERR_RECORD_TOO_LARGE If the record data exceeds the maximum length.
  358. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full or there are more record
  359. * chunks than can be buffered.
  360. */
  361. ret_code_t fds_record_write_reserved(fds_record_desc_t * p_desc,
  362. fds_record_t const * p_record,
  363. fds_reserve_token_t const * p_token);
  364. /**@brief Function for deleting a record.
  365. *
  366. * Deleted records cannot be located using @ref fds_record_find, @ref fds_record_find_by_key, or
  367. * @ref fds_record_find_in_file. Additionally, they can no longer be opened using
  368. * @ref fds_record_open.
  369. *
  370. * Note that deleting a record does not free the space it occupies in flash memory.
  371. * To reclaim flash space used by deleted records, call @ref fds_gc to run garbage collection.
  372. *
  373. * This function is asynchronous. Completion is reported through an event that is sent to the
  374. * registered event handler function.
  375. *
  376. * @param[in] p_desc The descriptor of the record that should be deleted.
  377. *
  378. * @retval FDS_SUCCESS If the operation was queued successfully.
  379. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  380. * @retval FDS_ERR_NULL_ARG If the specified record descriptor @p p_desc is NULL.
  381. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full.
  382. */
  383. ret_code_t fds_record_delete(fds_record_desc_t * p_desc);
  384. /**@brief Function for deleting all records in a file.
  385. *
  386. * This function deletes a file, including all its records. Deleted records cannot be located
  387. * using @ref fds_record_find, @ref fds_record_find_by_key, or @ref fds_record_find_in_file.
  388. * Additionally, they can no longer be opened using @ref fds_record_open.
  389. *
  390. * Note that deleting records does not free the space they occupy in flash memory.
  391. * To reclaim flash space used by deleted records, call @ref fds_gc to run garbage collection.
  392. *
  393. * This function is asynchronous. Completion is reported through an event that is sent to the
  394. * registered event handler function.
  395. *
  396. * @param[in] file_id The ID of the file to be deleted.
  397. *
  398. * @retval FDS_SUCCESS If the operation was queued successfully.
  399. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  400. * @retval FDS_ERR_INVALID_ARG If the specified @p file_id is invalid.
  401. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full.
  402. */
  403. ret_code_t fds_file_delete(uint16_t file_id);
  404. /**@brief Function for updating a record.
  405. *
  406. * Updating a record first writes a new record (@p p_record) to flash and then deletes the
  407. * old record (identified by @p p_desc).
  408. *
  409. * There are no restrictions on the file ID and the record key, except that the record key must be
  410. * different from @ref FDS_RECORD_KEY_DIRTY and the file ID must be different from
  411. * @ref FDS_FILE_ID_INVALID. In particular, no restrictions are made regarding the uniqueness of
  412. * the file ID or the record key. All records with the same file ID are grouped into one file.
  413. * If no file with the specified ID exists, it is created. There can be multiple records with the
  414. * same record key in a file.
  415. *
  416. * Record data can consist of multiple chunks. The data must be aligned to a 4 byte boundary, and
  417. * because it is not buffered internally, it must be kept in memory until the callback for the
  418. * operation has been received. The length of the data must not exceed @ref FDS_VIRTUAL_PAGE_SIZE
  419. * words minus 14 bytes.
  420. *
  421. * This function is asynchronous. Completion is reported through an event that is sent to the
  422. * registered event handler function.
  423. *
  424. * @param[in, out] p_desc The descriptor of the record to update. When the function
  425. * returns with FDS_SUCCESS, this parameter contains the
  426. * descriptor of the newly written record.
  427. * @param[in] p_record The updated record to be written to flash.
  428. *
  429. * @retval FDS_SUCCESS If the operation was queued successfully.
  430. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  431. * @retval FDS_ERR_INVALID_ARG If the file ID or the record key is invalid.
  432. * @retval FDS_ERR_UNALIGNED_ADDR If the record data is not aligned to a 4 byte boundary.
  433. * @retval FDS_ERR_RECORD_TOO_LARGE If the record data exceeds the maximum length.
  434. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full or there are more record
  435. * chunks than can be buffered.
  436. * @retval FDS_ERR_NO_SPACE_IN_FLASH If there is not enough free space in flash to store the
  437. * updated record.
  438. */
  439. ret_code_t fds_record_update(fds_record_desc_t * p_desc,
  440. fds_record_t const * p_record);
  441. /**@brief Function for iterating through all records in flash.
  442. *
  443. * To search for the next record, call the function again and supply the same @ref fds_find_token_t
  444. * structure to resume searching from the last record that was found.
  445. *
  446. * Note that the order with which records are iterated is not defined.
  447. *
  448. * @param[out] p_desc The descriptor of the record that was found.
  449. * @param[out] p_token A token containing information about the progress of the operation.
  450. *
  451. * @retval FDS_SUCCESS If a record was found.
  452. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  453. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_token is NULL.
  454. * @retval FDS_ERR_NOT_FOUND If no matching record was found.
  455. */
  456. ret_code_t fds_record_iterate(fds_record_desc_t * p_desc,
  457. fds_find_token_t * p_token);
  458. /**@brief Function for searching for records with a given record key in a file.
  459. *
  460. * This function finds the first record in a file that has the given record key. To search for the
  461. * next record with the same key in the file, call the function again and supply the same
  462. * @ref fds_find_token_t structure to resume searching from the last record that was found.
  463. *
  464. * @param[in] file_id The file ID.
  465. * @param[in] record_key The record key.
  466. * @param[out] p_desc The descriptor of the record that was found.
  467. * @param[out] p_token A token containing information about the progress of the operation.
  468. *
  469. * @retval FDS_SUCCESS If a record was found.
  470. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  471. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_token is NULL.
  472. * @retval FDS_ERR_NOT_FOUND If no matching record was found.
  473. */
  474. ret_code_t fds_record_find(uint16_t file_id,
  475. uint16_t record_key,
  476. fds_record_desc_t * p_desc,
  477. fds_find_token_t * p_token);
  478. /**@brief Function for searching for records with a given record key.
  479. *
  480. * This function finds the first record with a given record key, independent of the file it
  481. * belongs to. To search for the next record with the same key, call the function again and supply
  482. * the same @ref fds_find_token_t structure to resume searching from the last record that was found.
  483. *
  484. * @param[in] record_key The record key.
  485. * @param[out] p_desc The descriptor of the record that was found.
  486. * @param[out] p_token A token containing information about the progress of the operation.
  487. *
  488. * @retval FDS_SUCCESS If a record was found.
  489. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  490. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_token is NULL.
  491. * @retval FDS_ERR_NOT_FOUND If no record with the given key was found.
  492. */
  493. ret_code_t fds_record_find_by_key(uint16_t record_key,
  494. fds_record_desc_t * p_desc,
  495. fds_find_token_t * p_token);
  496. /**@brief Function for searching for any record in a file.
  497. *
  498. * This function finds the first record in a file, independent of its record key.
  499. * To search for the next record in the same file, call the function again and supply the same
  500. * @ref fds_find_token_t structure to resume searching from the last record that was found.
  501. *
  502. * @param[in] file_id The file ID.
  503. * @param[out] p_desc The descriptor of the record that was found.
  504. * @param[out] p_token A token containing information about the progress of the operation.
  505. *
  506. * @retval FDS_SUCCESS If a record was found.
  507. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  508. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_token is NULL.
  509. * @retval FDS_ERR_NOT_FOUND If no matching record was found.
  510. */
  511. ret_code_t fds_record_find_in_file(uint16_t file_id,
  512. fds_record_desc_t * p_desc,
  513. fds_find_token_t * p_token);
  514. /**@brief Function for opening a record for reading.
  515. *
  516. * This function opens a record that is stored in flash, so that it can be read. The function
  517. * initializes an @ref fds_flash_record_t structure, which can be used to access the record data as
  518. * well as its associated metadata. The pointers provided in the @ref fds_flash_record_t structure
  519. * are pointers to flash memory.
  520. *
  521. * Opening a record with @ref fds_record_open prevents garbage collection to run on the virtual
  522. * flash page in which record is stored, so that the contents of the memory pointed by fields in
  523. * @ref fds_flash_record_t are guaranteed to remain unmodified as long as the record is kept open.
  524. *
  525. * When you are done reading a record, call @ref fds_record_close to close it. Garbage collection
  526. * can then reclaim space on the virtual page where the record is stored. Note that you must
  527. * provide the same descriptor for @ref fds_record_close as you did for this function.
  528. *
  529. * @param[in] p_desc The descriptor of the record to open.
  530. * @param[out] p_flash_record The record, as stored in flash.
  531. *
  532. * @retval FDS_SUCCESS If the record was opened successfully.
  533. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_flash_record is NULL.
  534. * @retval FDS_ERR_NOT_FOUND If the record was not found. It might have been deleted, or
  535. * it might not have been written yet.
  536. * @retval FDS_ERR_CRC_CHECK_FAILED If the CRC check for the record failed.
  537. */
  538. ret_code_t fds_record_open(fds_record_desc_t * p_desc,
  539. fds_flash_record_t * p_flash_record);
  540. /**@brief Function for closing a record.
  541. *
  542. * Closing a record allows garbage collection to run on the virtual page in which the record is
  543. * stored (if no other records remain open on that page). The descriptor passed as an argument
  544. * must be the same as the one used to open the record using @ref fds_record_open.
  545. *
  546. * Note that closing a record does not invalidate its descriptor. You can still supply the
  547. * descriptor to all functions that accept a record descriptor as a parameter.
  548. *
  549. * @param[in] p_desc The descriptor of the record to close.
  550. *
  551. * @retval FDS_SUCCESS If the record was closed successfully.
  552. * @retval FDS_ERR_NULL_ARG If @p p_desc is NULL.
  553. * @retval FDS_ERR_NO_OPEN_RECORDS If the record is not open.
  554. * @retval FDS_ERR_NOT_FOUND If the record could not be found.
  555. */
  556. ret_code_t fds_record_close(fds_record_desc_t * p_desc);
  557. /**@brief Function for running garbage collection.
  558. *
  559. * Garbage collection reclaims the flash space that is occupied by records that have been deleted,
  560. * or that failed to be completely written due to, for example, a power loss.
  561. *
  562. * This function is asynchronous. Completion is reported through an event that is sent to the
  563. * registered event handler function.
  564. *
  565. * @retval FDS_SUCCESS If the operation was queued successfully.
  566. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  567. * @retval FDS_ERR_NO_SPACE_IN_QUEUES If the operation queue is full.
  568. */
  569. ret_code_t fds_gc(void);
  570. /**@brief Function for obtaining a descriptor from a record ID.
  571. *
  572. * This function can be used to reconstruct a descriptor from a record ID, like the one that is
  573. * passed to the callback function.
  574. *
  575. * @note
  576. * This function does not check whether a record with the given record ID exists.
  577. * If a non-existing record ID is supplied, the resulting descriptor is invalid and will cause
  578. * other functions to fail when it is supplied as parameter.
  579. *
  580. * @param[out] p_desc The descriptor of the record with the given record ID.
  581. * @param[in] record_id The record ID for which a descriptor should be returned.
  582. *
  583. * @retval FDS_SUCCESS If a descriptor was returned.
  584. * @retval FDS_ERR_NULL_ARG If @p p_desc is NULL.
  585. */
  586. ret_code_t fds_descriptor_from_rec_id(fds_record_desc_t * p_desc,
  587. uint32_t record_id);
  588. /**@brief Function for obtaining a record ID from a record descriptor.
  589. *
  590. * This function can be used to extract a record ID from a descriptor. For example, you could use
  591. * it in the callback function to compare the record ID of an event to the record IDs of the
  592. * records for which you have a descriptor.
  593. *
  594. * @warning
  595. * This function does not check whether the record descriptor is valid. If the descriptor is not
  596. * initialized or has been tampered with, the resulting record ID might be invalid.
  597. *
  598. * @param[in] p_desc The descriptor from which the record ID should be extracted.
  599. * @param[out] p_record_id The record ID that is contained in the given descriptor.
  600. *
  601. * @retval FDS_SUCCESS If a record ID was returned.
  602. * @retval FDS_ERR_NULL_ARG If @p p_desc or @p p_record_id is NULL.
  603. */
  604. ret_code_t fds_record_id_from_desc(fds_record_desc_t const * p_desc,
  605. uint32_t * p_record_id);
  606. /**@brief Function for retrieving file system statistics.
  607. *
  608. * This function retrieves file system statistics, such as the number of open records, the space
  609. * that can be reclaimed by garbage collection, and others.
  610. *
  611. * @param[out] p_stat File system statistics.
  612. *
  613. * @retval FDS_SUCCESS If the statistics were returned successfully.
  614. * @retval FDS_ERR_NOT_INITIALIZED If the module is not initialized.
  615. * @retval FDS_ERR_NULL_ARG If @p p_stat is NULL.
  616. */
  617. ret_code_t fds_stat(fds_stat_t * p_stat);
  618. /** @} */
  619. #ifdef __cplusplus
  620. }
  621. #endif
  622. #endif // FDS_H__