peer_database.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 PEER_DATABASE_H__
  13. #define PEER_DATABASE_H__
  14. #include <stdint.h>
  15. #include "peer_manager_types.h"
  16. #include "peer_manager_internal.h"
  17. #include "sdk_errors.h"
  18. /**
  19. * @cond NO_DOXYGEN
  20. * @defgroup peer_database Peer Database
  21. * @ingroup peer_manager
  22. * @{
  23. * @brief An internal module of @ref peer_manager. A module for simple management of reading and
  24. * writing of peer data into persistent storage.
  25. *
  26. */
  27. #define PDB_WRITE_BUF_SIZE (sizeof(pm_peer_data_bonding_t))
  28. /**@brief Events that can come from the peer_database module.
  29. */
  30. typedef enum
  31. {
  32. PDB_EVT_WRITE_BUF_STORED, /**< A @ref pdb_write_buf_store operation has completed successfully. */
  33. PDB_EVT_RAW_STORED, /**< A @ref pdb_raw_store operation has completed successfully. */
  34. PDB_EVT_RAW_STORE_FAILED, /**< A @ref pdb_raw_store operation has failed. */
  35. PDB_EVT_CLEARED, /**< A @ref pdb_clear operation has completed successfully. */
  36. PDB_EVT_CLEAR_FAILED, /**< A @ref pdb_clear operation has failed. */
  37. PDB_EVT_PEER_FREED, /**< A @ref pdb_peer_free operation has completed successfully. All associated data has been erased. */
  38. PDB_EVT_PEER_FREE_FAILED, /**< A @ref pdb_peer_free operation has failed. */
  39. PDB_EVT_COMPRESSED, /**< A compress procedure has completed. */
  40. PDB_EVT_ERROR_NO_MEM, /**< An operation is blocked because the flash is full. It will be reattempted automatically after the next compress procedure. */
  41. PDB_EVT_ERROR_UNEXPECTED, /**< An unexpected error occurred. This is a fatal error. */
  42. } pdb_evt_id_t;
  43. /**@brief Events that can come from the peer_database module.
  44. */
  45. typedef struct
  46. {
  47. pdb_evt_id_t evt_id; /**< The event that has happened. */
  48. pm_peer_id_t peer_id; /**< The id of the peer the event pertains to. */
  49. pm_peer_data_id_t data_id; /**< The data the event pertains to. */
  50. union
  51. {
  52. struct
  53. {
  54. bool update; /**< If true, an existing value was overwritten. */
  55. } write_buf_stored_evt; /**< Additional information pertaining to the @ref PDB_EVT_WRITE_BUF_STORED event. */
  56. struct
  57. {
  58. pm_store_token_t store_token; /**< A token identifying the store operation this event pertains to. */
  59. } raw_stored_evt; /**< Additional information pertaining to the @ref PDB_EVT_RAW_STORED event. */
  60. struct
  61. {
  62. pm_store_token_t store_token; /**< A token identifying the store operation this event pertains to. */
  63. ret_code_t err_code; /**< Error code specifying what went wrong. */
  64. } error_raw_store_evt; /**< Additional information pertaining to the @ref PDB_EVT_RAW_STORE_FAILED event. */
  65. struct
  66. {
  67. ret_code_t err_code; /**< The error that occurred. */
  68. } clear_failed_evt; /**< Additional information pertaining to the @ref PDB_EVT_CLEAR_FAILED event. */
  69. struct
  70. {
  71. ret_code_t err_code; /**< The error that occurred. */
  72. } peer_free_failed_evt; /**< Additional information pertaining to the @ref PDB_EVT_PEER_FREE_FAILED event. */
  73. struct
  74. {
  75. ret_code_t err_code; /**< The unexpected error that occurred. */
  76. } error_unexpected; /**< Additional information pertaining to the @ref PDB_EVT_ERROR_UNEXPECTED event. */
  77. } params;
  78. } pdb_evt_t;
  79. /**@brief Event handler for events from the peer_data_storage module.
  80. *
  81. * @param[in] p_event The event that has happened.
  82. */
  83. typedef void (*pdb_evt_handler_t)(pdb_evt_t const * p_event);
  84. /**@brief Function for registering for events from the peer database.
  85. *
  86. * @note This function will initialize the module if it is not already initialized.
  87. *
  88. * @param[in] evt_handler Event handler to register.
  89. *
  90. * @retval NRF_SUCCESS Registration successful.
  91. * @retval NRF_ERROR_NO_MEM No more event handlers can be registered.
  92. * @retval NRF_ERROR_NULL evt_handler was NULL.
  93. * @retval NRF_ERROR_INTERNAL An unexpected error happened.
  94. */
  95. ret_code_t pdb_register(pdb_evt_handler_t evt_handler);
  96. /**@brief Function for allocating persistent bond storage for a peer.
  97. *
  98. * @return The ID of the newly allocated storage.
  99. * @retval PM_PEER_ID_INVALID If no peer ID is available.
  100. */
  101. pm_peer_id_t pdb_peer_allocate(void);
  102. /**@brief Function for freeing a peer's persistent bond storage.
  103. *
  104. * @note This function will call @ref pdb_write_buf_release on the data for this peer.
  105. *
  106. * @param[in] peer_id ID to be freed.
  107. *
  108. * @retval NRF_SUCCESS Peer ID was released and clear operation was initiated successfully.
  109. * @retval NRF_ERROR_INVALID_PARAM Peer ID was invalid.
  110. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  111. */
  112. ret_code_t pdb_peer_free(pm_peer_id_t peer_id);
  113. /**@brief Function for retrieving pointers to read-only peer data.
  114. *
  115. * @note Reading this pointer is not safe in the strictest sense. If a safe read is required:
  116. * - Disable interrupts
  117. * - Call this function. If the return code is @ref NRF_SUCCESS, the following read is safe.
  118. * - Read memory.
  119. * - Enable interrupts.
  120. * @note This buffer does not need to be released. It is a pointer directly to flash.
  121. *
  122. * @param[in] peer_id ID of peer to retrieve data for.
  123. * @param[in] data_id Which piece of data to get.
  124. * @param[out] p_peer_data Pointer to immutable peer data.
  125. * @param[out] p_token Token that can be used to lock data in flash and check data validity.
  126. *
  127. * @retval NRF_SUCCESS Data retrieved successfully.
  128. * @retval NRF_ERROR_INVALID_PARAM Data ID or Peer ID was invalid or unallocated.
  129. * @retval NRF_ERROR_NULL p_peer_data was NULL.
  130. * @retval NRF_ERROR_NOT_FOUND This data was not found for this peer ID.
  131. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  132. */
  133. ret_code_t pdb_read_buf_get(pm_peer_id_t peer_id,
  134. pm_peer_data_id_t data_id,
  135. pm_peer_data_flash_t * p_peer_data,
  136. pm_store_token_t * p_token);
  137. /**@brief Function for retrieving pointers to a write buffer for peer data.
  138. *
  139. * @details This function will provide pointers to a buffer of the data. The data buffer will not be
  140. * written to persistent storage until @ref pdb_write_buf_store is called. The buffer is
  141. * released by calling either @ref pdb_write_buf_release, @ref pdb_write_buf_store, or
  142. * @ref pdb_peer_free.
  143. *
  144. * When the data_id refers to a variable length data type, the available size is written
  145. * to the data, both the top-level, and any internal length fields.
  146. *
  147. * @note Calling this function on a peer_id/data_id pair that already has a buffer created will
  148. * give the same buffer, not create a new one. If n_bufs was increased since last time, the
  149. * buffer might be relocated to be able to provide additional room. In this case, the data
  150. * will be copied. If n_bufs was increased since last time, this function might return @ref
  151. * NRF_ERROR_BUSY. In that case, the buffer is automatically released.
  152. *
  153. * @param[in] peer_id ID of peer to get a write buffer for.
  154. * @param[in] data_id Which piece of data to get.
  155. * @param[in] n_bufs The number of contiguous buffers needed.
  156. * @param[out] p_peer_data Pointers to mutable peer data.
  157. *
  158. * @retval NRF_SUCCESS Data retrieved successfully.
  159. * @retval NRF_ERROR_INVALID_PARAM Data ID or Peer ID was invalid or unallocated, or n_bufs was 0
  160. * or more than the total available buffers.
  161. * @retval NRF_ERROR_NULL p_peer_data was NULL.
  162. * @retval NRF_ERROR_BUSY Not enough buffer(s) available.
  163. * @retval NRF_ERROR_INTERNAL Unexpected internal error.
  164. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  165. */
  166. ret_code_t pdb_write_buf_get(pm_peer_id_t peer_id,
  167. pm_peer_data_id_t data_id,
  168. uint32_t n_bufs,
  169. pm_peer_data_t * p_peer_data);
  170. /**@brief Function for freeing a write buffer allocated with @ref pdb_write_buf_get.
  171. *
  172. * @note This function will not write peer data to persistent memory. Data in released buffer will
  173. * be lost.
  174. *
  175. * @note This function will undo any previous call to @ref pdb_write_buf_store_prepare for this
  176. * piece of data.
  177. *
  178. * @param[in] peer_id ID of peer to release buffer for.
  179. * @param[in] data_id Which piece of data to release buffer for.
  180. *
  181. * @retval NRF_SUCCESS Successfully released buffer.
  182. * @retval NRF_ERROR_NOT_FOUND No buffer was allocated for this peer ID/data ID pair.
  183. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  184. * @retval NRF_ERROR_INTERNAL Unexpected internal error.
  185. */
  186. ret_code_t pdb_write_buf_release(pm_peer_id_t peer_id, pm_peer_data_id_t data_id);
  187. /**@brief Function for reserving space in persistent storage for data in a buffer.
  188. *
  189. * @note This function only works for data which has a write buffer allocated. If the write buffer
  190. * is released, this prepare is undone.
  191. *
  192. * @note If space has already been reserved for this data, nothing is done.
  193. *
  194. * @param[in] peer_id The peer whose data to reserve space for.
  195. * @param[in] data_id The type of data to reserve space for.
  196. *
  197. * @retval NRF_SUCCESS Successfully reserved space in persistent storage.
  198. * @retval NRF_ERROR_NO_MEM Not enough room in persistent storage.
  199. * @retval NRF_ERROR_BUSY Could not process request at this time. Reattempt later.
  200. * @retval NRF_ERROR_NOT_FOUND No buffer has been allocated for this peer ID/data ID pair.
  201. * @retval NRF_ERROR_INVALID_PARAM Data ID or Peer ID was invalid or unallocated.
  202. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  203. */
  204. ret_code_t pdb_write_buf_store_prepare(pm_peer_id_t peer_id, pm_peer_data_id_t data_id);
  205. /**@brief Function for writing data into persistent storage. Writing happens asynchronously.
  206. *
  207. * @note This will unlock the data after it has been written.
  208. *
  209. * @param[in] peer_id ID of peer to store data for.
  210. * @param[in] data_id Which piece of data to store.
  211. *
  212. * @retval NRF_SUCCESS Data storing was successfully started.
  213. * @retval NRF_ERROR_NO_MEM No space available in persistent storage. Please clear some
  214. * space, the operation will be reattempted after the next compress
  215. * procedure. This error will not happen if
  216. * @ref pdb_write_buf_store_prepare is called beforehand.
  217. * @retval NRF_ERROR_INVALID_PARAM Data ID was invalid.
  218. * @retval NRF_ERROR_NOT_FOUND No buffer has been allocated for this peer ID/data ID pair.
  219. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  220. * @retval NRF_ERROR_INTERNAL Unexpected internal error.
  221. */
  222. ret_code_t pdb_write_buf_store(pm_peer_id_t peer_id,
  223. pm_peer_data_id_t data_id);
  224. /**@brief Function for clearing data from persistent storage.
  225. *
  226. * @param[in] peer_id ID of peer to clear data for.
  227. * @param[in] data_id Which piece of data to clear.
  228. *
  229. * @retval NRF_SUCCESS The clear was initiated successfully.
  230. * @retval NRF_ERROR_INVALID_PARAM Data ID or peer ID was invalid.
  231. * @retval NRF_ERROR_NOT_FOUND Nothing to clear for this peer ID/data ID combination.
  232. * @retval NRF_ERROR_BUSY Underlying modules are busy and can't take any more requests at
  233. * this moment.
  234. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  235. * @retval NRF_ERROR_INTERNAL Internal error.
  236. */
  237. ret_code_t pdb_clear(pm_peer_id_t peer_id, pm_peer_data_id_t data_id);
  238. /**@brief Function for querying the number of valid peer IDs available. I.E the number of peers
  239. * in persistent storage.
  240. *
  241. * @return The number of valid peer IDs.
  242. */
  243. uint32_t pdb_n_peers(void);
  244. /**@brief Function for getting the next peer ID in the sequence of all used peer IDs. Can be
  245. * used to loop through all used peer IDs.
  246. *
  247. * @note @ref PM_PEER_ID_INVALID is considered to be before the first and after the last ordinary
  248. * peer ID.
  249. *
  250. * @param[in] prev_peer_id The previous peer ID.
  251. *
  252. * @return The next peer ID.
  253. * @return The first ordinary peer ID if prev_peer_id was @ref PM_PEER_ID_INVALID.
  254. * @retval PM_PEER_ID_INVALID if prev_peer_id was the last ordinary peer ID.
  255. */
  256. pm_peer_id_t pdb_next_peer_id_get(pm_peer_id_t prev_peer_id);
  257. /**@brief Function for updating currently stored peer data to a new version
  258. *
  259. * @details Updating happens asynchronously.
  260. * Expect a @ref PDS_EVT_STORED or @ref PDS_EVT_ERROR_STORE for the store token
  261. * and a @ref PDS_EVT_ERROR_CLEAR or @ref PDS_EVT_ERROR_CLEAR for the old token
  262. *
  263. * @param[in] peer_data New data
  264. * @param[in] old_token Store token for the old data
  265. * @param[out] p_store_token Store token for the new data
  266. *
  267. * @retval NRF_SUCESS The update was initiated successfully
  268. * @retval NRF_ERROR_NOT_FOUND The old store token was invalid.
  269. * @retval NRF_ERROR_NULL Data contained a NULL pointer.
  270. * @retval NRF_ERROR_NO_MEM No space available in persistent storage.
  271. * @retval NRF_ERROR_BUSY FDS or underlying modules are busy and can't take any
  272. * more requests
  273. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  274. */
  275. ret_code_t pdb_peer_data_update(pm_peer_data_const_t peer_data,
  276. pm_store_token_t old_token,
  277. pm_store_token_t * p_store_token);
  278. /**@brief Function for reading data directly from persistent storage to external memory.
  279. *
  280. * @param[in] peer_id ID of peer to read data for.
  281. * @param[in] data_id Which piece of data to read.
  282. * @param[inout] p_peer_data Where to store the data. If the data to be read has variable length,
  283. * the appropriate length field needs to reflect the available buffer
  284. * space. On a successful read, the length field is updated to match the
  285. * length of the read data.
  286. *
  287. * @retval NRF_SUCCESS Data successfully read.
  288. * @retval NRF_ERROR_INVALID_PARAM Data ID or Peer ID was invalid or unallocated.
  289. * @retval NRF_ERROR_NULL p_peer_data contained a NULL pointer.
  290. * @retval NRF_ERROR_NOT_FOUND This data was not found for this peer ID.
  291. * @retval NRF_ERROR_DATA_SIZE The provided buffer was not large enough.
  292. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  293. */
  294. ret_code_t pdb_raw_read(pm_peer_id_t peer_id,
  295. pm_peer_data_id_t data_id,
  296. pm_peer_data_t * p_peer_data);
  297. /**@brief Function for writing data directly to persistent storage from external memory.
  298. *
  299. * @param[in] peer_id ID of peer to write data for.
  300. * @param[in] p_peer_data Data to store.
  301. * @param[out] p_store_token A token identifying this particular store operation. The token can be
  302. * used to identify events pertaining to this operation.
  303. *
  304. * @retval NRF_SUCCESS Data successfully written.
  305. * @retval NRF_ERROR_INVALID_PARAM Data ID or Peer ID was invalid or unallocated.
  306. * @retval NRF_ERROR_NULL p_peer_data contained a NULL pointer.
  307. * @retval NRF_ERROR_NO_MEM No space available in persistent storage.
  308. * @retval NRF_ERROR_INVALID_LENGTH Data length above the maximum allowed.
  309. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  310. * @retval NRF_ERROR_BUSY Unable to perform operation at this time.
  311. */
  312. ret_code_t pdb_raw_store(pm_peer_id_t peer_id,
  313. pm_peer_data_const_t * p_peer_data,
  314. pm_store_token_t * p_store_token);
  315. /** @}
  316. * @endcond
  317. */
  318. #endif /* PEER_DATABASE_H__ */