peer_database.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_DATABASE_H__
  41. #define PEER_DATABASE_H__
  42. #include <stdint.h>
  43. #include "peer_manager_types.h"
  44. #include "peer_manager_internal.h"
  45. #include "sdk_errors.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /**
  50. * @cond NO_DOXYGEN
  51. * @defgroup peer_database Peer Database
  52. * @ingroup peer_manager
  53. * @{
  54. * @brief An internal module of @ref peer_manager. A module for simple management of reading and
  55. * writing of peer data into persistent storage.
  56. *
  57. */
  58. #define PDB_WRITE_BUF_SIZE (sizeof(pm_peer_data_bonding_t)) //!< The size (in bytes) of each block in the internal buffer accessible via @ref pdb_write_buf_get.
  59. /**@brief Macro for creating a peer ID value from a connection handle.
  60. *
  61. * Use this macro with pdb_write_buf_get() or pdb_write_buf_store(). It allows to create a write buffer
  62. * even if you do not yet know the proper peer ID the data will be stored for.
  63. *
  64. * @return @p conn_handle + @ref PM_PEER_ID_N_AVAILABLE_IDS.
  65. */
  66. #define PDB_TEMP_PEER_ID(conn_handle) (PM_PEER_ID_N_AVAILABLE_IDS + conn_handle)
  67. /**@brief Function for initializing the module.
  68. *
  69. * @retval NRF_SUCCESS If initialization was successful.
  70. * @retval NRF_ERROR_INTERNAL An unexpected error happened.
  71. */
  72. ret_code_t pdb_init(void);
  73. /**@brief Function for freeing a peer's persistent bond storage.
  74. *
  75. * @note This function will call @ref pdb_write_buf_release on the data for this peer.
  76. *
  77. * @param[in] peer_id ID to be freed.
  78. *
  79. * @retval NRF_SUCCESS Peer ID was released and clear operation was initiated successfully.
  80. * @retval NRF_ERROR_INVALID_PARAM Peer ID was invalid.
  81. */
  82. ret_code_t pdb_peer_free(pm_peer_id_t peer_id);
  83. /**@brief Function for retrieving a pointer to peer data in flash (read-only).
  84. *
  85. * @note Dereferencing this pointer is not the safest thing to do if interrupts are enabled,
  86. * because Flash Data Storage garbage collection might move the data around. Either disable
  87. * interrupts while using the data, or use @ref pdb_peer_data_load.
  88. *
  89. * @param[in] peer_id The peer the data belongs to.
  90. * @param[in] data_id The data to read.
  91. * @param[out] p_peer_data The peer data, read-only.
  92. *
  93. * @retval NRF_SUCCESS If the pointer to the data was retrieved successfully.
  94. * @retval NRF_ERROR_INVALID_PARAM If either @p peer_id or @p data_id are invalid.
  95. * @retval NRF_ERROR_NOT_FOUND If data was not found in flash.
  96. */
  97. ret_code_t pdb_peer_data_ptr_get(pm_peer_id_t peer_id,
  98. pm_peer_data_id_t data_id,
  99. pm_peer_data_flash_t * const p_peer_data);
  100. /**@brief Function for retrieving pointers to a write buffer for peer data.
  101. *
  102. * @details This function will provide pointers to a buffer of the data. The data buffer will not be
  103. * written to persistent storage until @ref pdb_write_buf_store is called. The buffer is
  104. * released by calling either @ref pdb_write_buf_release, @ref pdb_write_buf_store, or
  105. * @ref pdb_peer_free.
  106. *
  107. * When the data_id refers to a variable length data type, the available size is written
  108. * to the data, both the top-level, and any internal length fields.
  109. *
  110. * @note Calling this function on a peer_id/data_id pair that already has a buffer created will
  111. * give the same buffer, not create a new one. If n_bufs was increased since last time, the
  112. * buffer might be relocated to be able to provide additional room. In this case, the data
  113. * will be copied. If n_bufs was increased since last time, this function might return @ref
  114. * NRF_ERROR_BUSY. In that case, the buffer is automatically released.
  115. *
  116. * @param[in] peer_id ID of the peer to get a write buffer for. If @p peer_id is larger than
  117. * @ref PM_PEER_ID_N_AVAILABLE_IDS, it is interpreted as pertaining to
  118. * the connection with connection handle peer_id - PM_PEER_ID_N_AVAILABLE_IDS.
  119. * See @ref PDB_TEMP_PEER_ID.
  120. * @param[in] data_id The piece of data to get.
  121. * @param[in] n_bufs Number of contiguous buffers needed.
  122. * @param[out] p_peer_data Pointers to mutable peer data.
  123. *
  124. * @retval NRF_SUCCESS Data retrieved successfully.
  125. * @retval NRF_ERROR_INVALID_PARAM @p data_id was invalid, or @p n_bufs was 0 or more than the total
  126. * available buffers.
  127. * @retval NRF_ERROR_FORBIDDEN n_bufs was higher or lower than the existing buffer. If needed,
  128. * release the existing buffer with @ref pdb_write_buf_release, and
  129. * call this function again.
  130. * @retval NRF_ERROR_NULL p_peer_data was NULL.
  131. * @retval NRF_ERROR_BUSY Not enough buffer(s) available.
  132. * @retval NRF_ERROR_INTERNAL Unexpected internal error.
  133. */
  134. ret_code_t pdb_write_buf_get(pm_peer_id_t peer_id,
  135. pm_peer_data_id_t data_id,
  136. uint32_t n_bufs,
  137. pm_peer_data_t * p_peer_data);
  138. /**@brief Function for freeing a write buffer allocated with @ref pdb_write_buf_get.
  139. *
  140. * @note This function will not write peer data to persistent memory. Data in released buffer will
  141. * be lost.
  142. *
  143. * @param[in] peer_id ID of peer to release buffer for.
  144. * @param[in] data_id Which piece of data to release buffer for.
  145. *
  146. * @retval NRF_SUCCESS Successfully released buffer.
  147. * @retval NRF_ERROR_NOT_FOUND No buffer was allocated for this peer ID/data ID pair.
  148. */
  149. ret_code_t pdb_write_buf_release(pm_peer_id_t peer_id, pm_peer_data_id_t data_id);
  150. /**@brief Function for writing data into persistent storage. Writing happens asynchronously.
  151. *
  152. * @note This will unlock the data after it has been written.
  153. *
  154. * @param[in] peer_id The ID used to address the write buffer.
  155. * @param[in] data_id The piece of data to store.
  156. * @param[in] new_peer_id The ID to put in flash. This is usually the same as peer_id, but
  157. * must be valid, i.e. allocated (and smaller than @ref PM_PEER_ID_N_AVAILABLE_IDS).
  158. *
  159. * @retval NRF_SUCCESS Data storing was successfully started.
  160. * @retval NRF_ERROR_STORAGE_FULL No space available in persistent storage. Please clear some
  161. * space, the operation will be reattempted after the next compress
  162. * procedure.
  163. * @retval NRF_ERROR_INVALID_PARAM @p data_id or @p new_peer_id was invalid.
  164. * @retval NRF_ERROR_NOT_FOUND No buffer has been allocated for this peer ID/data ID pair.
  165. * @retval NRF_ERROR_INTERNAL Unexpected internal error.
  166. */
  167. ret_code_t pdb_write_buf_store(pm_peer_id_t peer_id,
  168. pm_peer_data_id_t data_id,
  169. pm_peer_id_t new_peer_id);
  170. /** @}
  171. * @endcond
  172. */
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. #endif /* PEER_DATABASE_H__ */