peer_data_storage.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 PEER_DATA_STORAGE_H__
  41. #define PEER_DATA_STORAGE_H__
  42. #include <stdint.h>
  43. #include "sdk_errors.h"
  44. #include "ble_gap.h"
  45. #include "peer_manager_types.h"
  46. #include "peer_manager_internal.h"
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /**
  51. * @cond NO_DOXYGEN
  52. * @defgroup peer_data_storage Peer Data Storage
  53. * @ingroup peer_manager
  54. * @{
  55. * @brief An internal module of @ref peer_manager. This module provides a Peer Manager-specific API
  56. * to the persistent storage.
  57. *
  58. * @details This module uses Flash Data Storage (FDS) to interface with persistent storage.
  59. */
  60. #define PDS_FIRST_RESERVED_FILE_ID (0xC000) /**< The beginning of the range of file IDs reserved for Peer Manager. */
  61. #define PDS_LAST_RESERVED_FILE_ID (0xFFFE) /**< The end of the range of file IDs reserved for Peer Manager. */
  62. #define PDS_FIRST_RESERVED_RECORD_KEY (0xC000) /**< The beginning of the range of record keys reserved for Peer Manager. */
  63. #define PDS_LAST_RESERVED_RECORD_KEY (0xFFFE) /**< The end of the range of record keys reserved for Peer Manager. */
  64. #define PEER_ID_TO_FILE_ID ( PDS_FIRST_RESERVED_FILE_ID) //!< Macro for converting a @ref pm_peer_id_t to an FDS file ID.
  65. #define FILE_ID_TO_PEER_ID (-PDS_FIRST_RESERVED_FILE_ID) //!< Macro for converting an FDS file ID to a @ref pm_peer_id_t.
  66. #define DATA_ID_TO_RECORD_KEY ( PDS_FIRST_RESERVED_RECORD_KEY) //!< Macro for converting a @ref pm_peer_data_id_t to an FDS record ID.
  67. #define RECORD_KEY_TO_DATA_ID (-PDS_FIRST_RESERVED_RECORD_KEY) //!< Macro for converting an FDS record ID to a @ref pm_peer_data_id_t.
  68. /**@brief Function for initializing the module.
  69. *
  70. * @retval NRF_SUCCESS If initialization was successful.
  71. * @retval NRF_ERROR_STORAGE_FULL If no flash pages were available for use.
  72. * @retval NRF_ERROR_INTERNAL If the module couldn't register with the flash filesystem.
  73. */
  74. ret_code_t pds_init(void);
  75. /**@brief Function for reading peer data in flash.
  76. *
  77. * @param[in] peer_id The peer the data belongs to.
  78. * @param[in] data_id The data to retrieve.
  79. * @param[out] p_data The peer data. May not be @c NULL. p_data.length_words and p_data.data_id
  80. * are ignored. p_data.p_all_data is ignored if @p p_buf_len is @c NULL.
  81. * @param[in] p_buf_len Length of the provided buffer, in bytes. Pass @c NULL to only copy
  82. * a pointer to the data in flash.
  83. *
  84. * @retval NRF_SUCCESS If the operation was successful.
  85. * @retval NRF_ERROR_INVALID_PARAM If @p peer_id or @p data_id are invalid.
  86. * @retval NRF_ERROR_NOT_FOUND If the data was not found in flash.
  87. * @retval NRF_ERROR_DATA_SIZE If the provided buffer is too small. The data is still copied,
  88. * filling the provided buffer.
  89. */
  90. ret_code_t pds_peer_data_read(pm_peer_id_t peer_id,
  91. pm_peer_data_id_t data_id,
  92. pm_peer_data_t * const p_data,
  93. uint32_t const * const p_buf_len);
  94. /**@brief Function to prepare iterating over peer data in flash using @ref pds_peer_data_iterate.
  95. * Call this function once each time before iterating using @ref pds_peer_data_iterate.
  96. */
  97. void pds_peer_data_iterate_prepare(void);
  98. /**@brief Function for iterating peers' data in flash.
  99. * Always call @ref pds_peer_data_iterate_prepare before starting iterating.
  100. *
  101. * @param[in] data_id The peer data to iterate over.
  102. * @param[out] p_peer_id The peer the data belongs to.
  103. * @param[out] p_data The peer data in flash.
  104. *
  105. * @retval true If the operation was successful.
  106. * @retval false If the data was not found in flash, or another error occurred.
  107. */
  108. bool pds_peer_data_iterate(pm_peer_data_id_t data_id,
  109. pm_peer_id_t * const p_peer_id,
  110. pm_peer_data_flash_t * const p_data);
  111. /**@brief Function for storing peer data in flash. If the same piece of data already exists for the
  112. * given peer, it will be updated. This operation is asynchronous.
  113. * Expect a @ref PM_EVT_PEER_DATA_UPDATE_SUCCEEDED or @ref PM_EVT_PEER_DATA_UPDATE_FAILED
  114. * event.
  115. *
  116. * @param[in] peer_id The peer the data belongs to.
  117. * @param[in] p_peer_data The peer data. May not be @c NULL.
  118. * @param[out] p_store_token A token identifying this particular store operation. The token can be
  119. * used to identify events pertaining to this operation. Pass @p NULL
  120. * if not used.
  121. *
  122. * @retval NRF_SUCCESS If the operation was initiated successfully.
  123. * @retval NRF_ERROR_INVALID_PARAM If @p peer_id or the data ID in @p_peer_data are invalid.
  124. * @retval NRF_ERROR_INVALID_ADDR If @p p_peer_data is not word-aligned.
  125. * @retval NRF_ERROR_STORAGE_FULL If no space is available in flash.
  126. * @retval NRF_ERROR_BUSY If the flash filesystem was busy.
  127. * @retval NRF_ERROR_INTERNAL If an unexpected error occurred.
  128. */
  129. ret_code_t pds_peer_data_store(pm_peer_id_t peer_id,
  130. pm_peer_data_const_t const * p_peer_data,
  131. pm_store_token_t * p_store_token);
  132. /**@brief Function for deleting peer data in flash. This operation is asynchronous.
  133. * Expect a @ref PM_EVT_PEER_DATA_UPDATE_SUCCEEDED or @ref PM_EVT_PEER_DATA_UPDATE_FAILED
  134. * event.
  135. *
  136. * @param[in] peer_id The peer the data belongs to
  137. * @param[in] data_id The data to delete.
  138. *
  139. * @retval NRF_SUCCESS If the operation was initiated successfully.
  140. * @retval NRF_ERROR_INVALID_PARAM If @p peer_id or @p data_id are invalid.
  141. * @retval NRF_ERROR_NOT_FOUND If data was not found in flash.
  142. * @retval NRF_ERROR_BUSY If the flash filesystem was busy.
  143. * @retval NRF_ERROR_INTERNAL If an unexpected error occurred.
  144. */
  145. ret_code_t pds_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id);
  146. /**@brief Function for claiming an unused peer ID.
  147. *
  148. * @retval PM_PEER_ID_INVALID If no peer ID was available.
  149. */
  150. pm_peer_id_t pds_peer_id_allocate(void);
  151. /**@brief Function for freeing a peer ID and deleting all data associated with it in flash.
  152. *
  153. * @param[in] peer_id The ID of the peer to free.
  154. *
  155. * @retval NRF_SUCCESS The operation was initiated successfully.
  156. * @retval NRF_ERROR_INVALID_PARAM If @p peer_id is invalid.
  157. */
  158. ret_code_t pds_peer_id_free(pm_peer_id_t peer_id);
  159. /**@brief Function for finding out whether a peer ID is in use.
  160. *
  161. * @param[in] peer_id The peer ID to inquire about.
  162. *
  163. * @retval true @p peer_id is in use.
  164. * @retval false @p peer_id is free.
  165. */
  166. bool pds_peer_id_is_allocated(pm_peer_id_t peer_id);
  167. /**@brief Function for finding out whether a peer ID is marked for deletion.
  168. *
  169. * @param[in] peer_id The peer ID to inquire about.
  170. *
  171. * @retval true @p peer_id is marked for deletion.
  172. * @retval false @p peer_id is not marked for deletion.
  173. */
  174. bool pds_peer_id_is_deleted(pm_peer_id_t peer_id);
  175. /**@brief Function for getting the next peer ID in the sequence of all used peer IDs. Can be
  176. * used to loop through all used peer IDs.
  177. *
  178. * @note @ref PM_PEER_ID_INVALID is considered to be before the first and after the last ordinary
  179. * peer ID.
  180. *
  181. * @param[in] prev_peer_id The previous peer ID.
  182. *
  183. * @return The first ordinary peer ID If @p prev_peer_id is @ref PM_PEER_ID_INVALID.
  184. * @retval PM_PEER_ID_INVALID If @p prev_peer_id is the last ordinary peer ID or the module
  185. * is not initialized.
  186. */
  187. pm_peer_id_t pds_next_peer_id_get(pm_peer_id_t prev_peer_id);
  188. /**@brief Function for getting the next peer ID in the sequence of all peer IDs pending deletion.
  189. * Can be used to loop through all used peer IDs.
  190. *
  191. * @note @ref PM_PEER_ID_INVALID is considered to be before the first and after the last ordinary
  192. * peer ID.
  193. *
  194. * @param[in] prev_peer_id The previous peer ID.
  195. *
  196. * @return The next peer ID pending deletion.
  197. * @return The first ordinary peer ID if prev_peer_id was @ref PM_PEER_ID_INVALID.
  198. * @retval PM_PEER_ID_INVALID if prev_peer_id was the last ordinary peer ID or the module
  199. * is not initialized.
  200. */
  201. pm_peer_id_t pds_next_deleted_peer_id_get(pm_peer_id_t prev_peer_id);
  202. /**@brief Function for querying the number of valid peer IDs available. I.E the number of peers
  203. * in persistent storage.
  204. *
  205. * @return The number of valid peer IDs.
  206. */
  207. uint32_t pds_peer_count_get(void);
  208. /** @}
  209. * @endcond
  210. */
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214. #endif /* PEER_DATA_STORAGE_H__ */