gatt_cache_manager.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 GATT_CACHE_MANAGER_H__
  13. #define GATT_CACHE_MANAGER_H__
  14. #include <stdint.h>
  15. #include "sdk_errors.h"
  16. #include "ble.h"
  17. #include "ble_gap.h"
  18. #include "peer_manager_types.h"
  19. /**
  20. * @cond NO_DOXYGEN
  21. * @defgroup gatt_cache_manager GATT Cache Manager
  22. * @ingroup peer_manager
  23. * @{
  24. * @brief An internal module of @ref peer_manager. A module for managing persistent storing of GATT
  25. * attributes.
  26. */
  27. /**@brief Events that can come from the GATT Cache Manager module.
  28. */
  29. typedef enum
  30. {
  31. GCM_EVT_LOCAL_DB_CACHE_STORED, /**< The persistent cache for the local database has been updated with provided values, for one peer. */
  32. GCM_EVT_LOCAL_DB_CACHE_UPDATED, /**< The persistent cache for the local database has been updated with values from the SoftDevice, for one peer. */
  33. GCM_EVT_LOCAL_DB_CACHE_APPLIED, /**< The SoftDevice has been given local database values from the persistent cache, for one peer. */
  34. GCM_EVT_ERROR_LOCAL_DB_CACHE_APPLY, /**< The stored local database values for a peer were rejected by the SoftDevice, which means the database has changed. */
  35. GCM_EVT_REMOTE_DB_CACHE_UPDATED, /**< The persistent cache for the remote database has been updated with provided values, for one peer. */
  36. GCM_EVT_SERVICE_CHANGED_IND_SENT, /**< A service changed indication has been sent to a peer. */
  37. GCM_EVT_SERVICE_CHANGED_IND_CONFIRMED, /**< A sent service changed indication has been confirmed by a peer. */
  38. GCM_EVT_ERROR_DATA_SIZE, /**< An operation failed because the write buffer of the Peer Database module was not large enough. This is a fatal error. */
  39. GCM_EVT_ERROR_STORAGE_FULL, /**< An operation failed because there was no available storage room in persistent storage. Please free up room, and the operation will automatically continue. */
  40. GCM_EVT_ERROR_UNEXPECTED, /**< An operation failed with an unexpected error. The error is provided. This is possibly a fatal error. */
  41. } gcm_evt_id_t;
  42. /**@brief A structure meant to be used for event parameters for multiple event types.
  43. */
  44. typedef struct
  45. {
  46. uint16_t conn_handle; /**< The connection handle. Likely the connection handle an event pertains to. */
  47. } gcm_evt_param_conn_handle_t;
  48. /**@brief Structure containing an event from the GCM module.
  49. */
  50. typedef struct
  51. {
  52. gcm_evt_id_t evt_id; /**< The type of event this is. */
  53. pm_peer_id_t peer_id; /**< The peer ID this event pertains to. */
  54. union
  55. {
  56. gcm_evt_param_conn_handle_t local_db_cache_updated;
  57. gcm_evt_param_conn_handle_t local_db_cache_applied;
  58. gcm_evt_param_conn_handle_t error_local_db_cache_apply;
  59. gcm_evt_param_conn_handle_t service_changed_ind_sent;
  60. gcm_evt_param_conn_handle_t service_changed_ind_confirmed;
  61. gcm_evt_param_conn_handle_t error_data_size;
  62. gcm_evt_param_conn_handle_t error_no_mem;
  63. struct
  64. {
  65. uint16_t conn_handle; /**< The handle of the connection the event pertains to. */
  66. ret_code_t error; /**< The unexpected error that occurred. */
  67. } error_unexpected;
  68. } params; /**< Event specific parameters. Chosen based on evt_id. */
  69. } gcm_evt_t;
  70. /**@brief Event handler for events from the GATT Cache Manager module.
  71. *
  72. * @param[in] event The event that has happened.
  73. * @param[in] peer The id of the peer the event pertains to.
  74. * @param[in] flags The data the event pertains to.
  75. */
  76. typedef void (*gcm_evt_handler_t)(gcm_evt_t const * p_event);
  77. /**@brief Function for initializing the GATT Cache Manager module.
  78. *
  79. * @param[in] evt_handler Callback for events from the GATT Cache Manager module.
  80. *
  81. * @retval NRF_SUCCESS Initialization was successful.
  82. * @retval NRF_ERROR_NULL evt_handler was NULL.
  83. */
  84. ret_code_t gcm_init(gcm_evt_handler_t evt_handler);
  85. /**@brief Function for dispatching SoftDevice events to the GATT Cache Manager module.
  86. *
  87. * @param[in] p_ble_evt The SoftDevice event.
  88. */
  89. void gcm_ble_evt_handler(ble_evt_t * p_ble_evt);
  90. /**@brief Function for storing a discovered remote database persistently.
  91. *
  92. * @param[in] peer_id Peer to store the database for.
  93. * @param[in] p_remote_db Database values to store as an array. Can be NULL if n_services is 0.
  94. * @param[in] n_services Number of services in p_remote_db array. If 0, values are cleared.
  95. *
  96. * @retval NRF_SUCCESS Store procedure successfully started.
  97. * @retval NRF_ERROR_NOT_FOUND The peer id is invalid or unallocated.
  98. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  99. */
  100. ret_code_t gcm_remote_db_store(pm_peer_id_t peer_id,
  101. ble_gatt_db_srv_t * p_remote_db,
  102. uint32_t n_services);
  103. /**@brief Function for retrieving a persistently stored remote database.
  104. *
  105. * @param[in] peer_id Peer to retrieve data for.
  106. * @param[out] p_remote_db If p_n_services was large enough: Copied database values.
  107. * @param[inout] p_n_services In: Size of provided p_remote_db array. Out: Size of data in flash.
  108. *
  109. * @note p_n_services is always updated with the size of the data to be retrieved. The data is only
  110. * copied if p_remote_db is large enough (p_n_services is large enough initially).
  111. *
  112. * @retval NRF_SUCCESS Data retrieved successfully.
  113. * @retval NRF_ERROR_NOT_FOUND The peer ID is invalid or unallocated.
  114. * @retval NRF_ERROR_NULL p_remote_db is NULL.
  115. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  116. */
  117. ret_code_t gcm_remote_db_retrieve(pm_peer_id_t peer_id,
  118. ble_gatt_db_srv_t * p_remote_db,
  119. uint32_t * p_n_services);
  120. /**@brief Function for triggering local GATT database data to be stored persistently. Values are
  121. * retrieved from SoftDevice and written to persistent storage.
  122. *
  123. * @note This function is only needed when you want to override the regular functionality of the
  124. * module, e.g. to immediately store to flash instead of waiting for the native logic to
  125. * perform the update.
  126. *
  127. * @param[in] conn_handle Connection handle to perform update on.
  128. *
  129. * @retval NRF_SUCCESS Store operation started.
  130. * @retval BLE_ERROR_INVALID_CONN_HANDLE conn_handle does not refer to an active, bonded connection.
  131. * @retval NRF_ERROR_DATA_SIZE Write buffer not large enough. Call will never work with
  132. * this GATT database.
  133. * @retval NRF_ERROR_NO_MEM No room in persistent_storage. Free up space; the
  134. * operation will be automatically reattempted after the
  135. * next compression procedure
  136. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  137. */
  138. ret_code_t gcm_local_db_cache_update(uint16_t conn_handle);
  139. /**@brief Function for setting new values in the local database cache.
  140. *
  141. * @note If the peer is connected, the values will also be applied immediately to the connection.
  142. * @note This function is only needed when you want to override the regular functionality of the
  143. * module.
  144. * @note The data in the pointer must be available until the GCM_EVT_LOCAL_DB_CACHE_SET event is
  145. * received.
  146. *
  147. * @param[in] peer_id Peer to set values for.
  148. * @param[in] p_local_db Database values to apply. If NULL, the values will instead be cleared.
  149. *
  150. * @retval NRF_SUCCESS Operation started, and values were applied (if connected).
  151. * @retval NRF_ERROR_NOT_FOUND The peer ID was invalid or unallocated.
  152. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  153. */
  154. ret_code_t gcm_local_db_cache_set(pm_peer_id_t peer_id, pm_peer_data_local_gatt_db_t * p_local_db);
  155. /**@brief Function for retrieving values in the local database cache.
  156. *
  157. * @note This function is not needed for regular operation of the module.
  158. *
  159. * @param[in] peer_id Peer to get values for.
  160. * @param[out] p_local_db Database values.
  161. *
  162. * @retval NRF_SUCCESS Values retrieved successfully.
  163. * @retval NRF_ERROR_NOT_FOUND The peer ID was invalid or unallocated.
  164. * @retval NRF_ERROR_NULL p_local_db was NULL.
  165. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  166. */
  167. ret_code_t gcm_local_db_cache_get(pm_peer_id_t peer_id, pm_peer_data_local_gatt_db_t * p_local_db);
  168. /**@brief Function for manually informing that the local database has changed.
  169. *
  170. * @details This causes a service changed notification to be sent to all bonded peers that
  171. * subscribe to it.
  172. */
  173. void gcm_local_database_has_changed(void);
  174. /** @}
  175. * @endcond
  176. */
  177. #endif /* GATT_CACHE_MANAGER_H__ */