gatts_cache_manager.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 GATTS_CACHE_MANAGER_H__
  13. #define GATTS_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 gatts_cache_manager GATT Server 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 pertaining to the GATT server role of the local device.
  26. */
  27. /**@brief Events that can come from the GATT Server Cache Manager module.
  28. */
  29. typedef enum
  30. {
  31. GSCM_EVT_LOCAL_DB_CACHE_STORED, /**< The persistent cache for the local database has been updated with provided values, for one peer. */
  32. GSCM_EVT_LOCAL_DB_CACHE_UPDATED, /**< The persistent cache for the local database has been updated with values from the SoftDevice, for one peer. */
  33. GSCM_EVT_SC_STATE_STORED, /**< The service changed pending flag in persistent storage has been updated, for one peer. */
  34. } gscm_evt_id_t;
  35. /**@brief Structure containing an event from the GSCM module.
  36. */
  37. typedef struct
  38. {
  39. gscm_evt_id_t evt_id; /**< The type of event this is. */
  40. pm_peer_id_t peer_id; /**< The peer ID this event pertains to. */
  41. union
  42. {
  43. struct
  44. {
  45. uint16_t conn_handle; /**< The connection this event pertains to. */
  46. } local_db_cache_updated;
  47. struct
  48. {
  49. bool state; /**< The newly stored state of the Service Changed pending flag. */
  50. } sc_state_stored;
  51. } params; /**< Event parameters specific to certain event types. */
  52. } gscm_evt_t;
  53. /**@brief Event handler for events from the GATT Server Cache Manager module.
  54. *
  55. * @param[in] event The event that has happened.
  56. * @param[in] peer The id of the peer the event pertains to.
  57. * @param[in] flags The data the event pertains to.
  58. */
  59. typedef void (*gscm_evt_handler_t)(gscm_evt_t const * p_event);
  60. /**@brief Function for initializing the GATT Server Cache Manager module.
  61. *
  62. * @param[in] evt_handler Callback for events from the GATT Server Cache Manager module.
  63. *
  64. * @retval NRF_SUCCESS Initialization was successful.
  65. * @retval NRF_ERROR_NULL evt_handler was NULL.
  66. */
  67. ret_code_t gscm_init(gscm_evt_handler_t evt_handler);
  68. /**@brief Function for triggering local GATT database data to be stored persistently. Values are
  69. * retrieved from the SoftDevice and written to persistent storage.
  70. *
  71. * @param[in] conn_handle Connection handle to perform update on.
  72. *
  73. * @retval NRF_SUCCESS Store operation started.
  74. * @retval BLE_ERROR_INVALID_CONN_HANDLE conn_handle does not refer to an active connection with a
  75. * bonded peer.
  76. * @retval NRF_ERROR_BUSY Unable to perform operation at this time. Reattempt later.
  77. * @retval NRF_ERROR_DATA_SIZE Write buffer not large enough. Call will never work with
  78. * this GATT database.
  79. * @retval NRF_ERROR_NO_MEM No room in persistent_storage. Free up space; the
  80. * operation will be automatically reattempted after the
  81. * next compression procedure
  82. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  83. */
  84. ret_code_t gscm_local_db_cache_update(uint16_t conn_handle);
  85. /**@brief Function for applying stored local GATT database data to the SoftDevice. Values are
  86. * retrieved from persistent storage and given to the SoftDevice.
  87. *
  88. * @param[in] conn_handle Connection handle to apply values to.
  89. *
  90. * @retval NRF_SUCCESS Store operation started.
  91. * @retval BLE_ERROR_INVALID_CONN_HANDLE conn_handle does not refer to an active connection with a
  92. * bonded peer.
  93. * @retval NRF_ERROR_INVALID_DATA The stored data was rejected by the SoftDevice, which
  94. * probably means that the local database has changed. The
  95. * system part of the sys_attributes was attempted applied,
  96. * so service changed indications can be sent to subscribers.
  97. * @retval NRF_ERROR_BUSY Unable to perform operation at this time. Reattempt later.
  98. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  99. * @return An unexpected return value from an internal function call.
  100. */
  101. ret_code_t gscm_local_db_cache_apply(uint16_t conn_handle);
  102. /**@brief Function for setting new values in the local database cache.
  103. *
  104. * @note If the peer is connected, the values will also be applied immediately to the connection.
  105. * @note The data in the pointer must be available until the GSCM_EVT_LOCAL_DB_STORED event is
  106. * received.
  107. *
  108. * @param[in] peer_id Peer to set values for.
  109. * @param[in] p_local_db Database values to apply. If NULL, the values will instead be cleared.
  110. *
  111. * @retval NRF_SUCCESS Operation started, and values were applied (if connected).
  112. * @retval NRF_ERROR_NOT_FOUND The peer ID was invalid or unallocated.
  113. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  114. * @return An unexpected return value from an internal function call.
  115. */
  116. ret_code_t gscm_local_db_cache_set(pm_peer_id_t peer_id, pm_peer_data_local_gatt_db_t * p_local_db);
  117. /**@brief Function for retrieving values in the local database cache.
  118. *
  119. * @param[in] peer_id Peer to get values for.
  120. * @param[inout] p_local_db Where to store the data. The length field needs to reflect the
  121. * available buffer space. On a successful read, the length field is
  122. * updated to match the length of the read data.
  123. *
  124. * @retval NRF_SUCCESS Values retrieved successfully.
  125. * @retval NRF_ERROR_NOT_FOUND The peer ID was invalid or unallocated.
  126. * @retval NRF_ERROR_NULL p_local_db was NULL.
  127. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  128. */
  129. ret_code_t gscm_local_db_cache_get(pm_peer_id_t peer_id, pm_peer_data_local_gatt_db_t * p_local_db);
  130. /**@brief Function for storing the fact that the local database has changed, for all currently
  131. * bonded peers.
  132. *
  133. * @note This will cause a later call to @ref gscm_service_changed_ind_needed to return true for
  134. * a connection with a currently bonded peer.
  135. */
  136. void gscm_local_database_has_changed(void);
  137. /**@brief Function for checking if a service changed indication should be sent.
  138. *
  139. * @param[in] conn_handle The connection to check.
  140. *
  141. * @return true if a service changed indication should be sent, false if not.
  142. */
  143. bool gscm_service_changed_ind_needed(uint16_t conn_handle);
  144. /**@brief Function for sending a service changed indication to a connected peer.
  145. *
  146. * @param[in] conn_handle The connection to send the indication on.
  147. *
  148. * @retval NRF_SUCCESS Indication sent or not needed.
  149. * @retval BLE_ERROR_INVALID_CONN_HANDLE conn_handle does not refer to an active connection.
  150. * @retval NRF_ERROR_BUSY Unable to send indication at this time. Reattempt later.
  151. * @retval BLE_ERROR_GATTS_SYS_ATTR_MISSING Information missing. Apply local cache, then reattempt.
  152. * @retval NRF_ERROR_INVALID_PARAM From @ref sd_ble_gatts_service_changed. Unexpected.
  153. * @retval NRF_ERROR_NOT_SUPPORTED Service changed characteristic is not present.
  154. * @retval NRF_ERROR_INVALID_STATE Service changed cannot be indicated to this peer
  155. * because the peer has not subscribed to it.
  156. */
  157. ret_code_t gscm_service_changed_ind_send(uint16_t conn_handle);
  158. /**@brief Function for specifying that a peer has been made aware of the latest local database
  159. * change.
  160. *
  161. * @note After calling this, a later call to @ref gscm_service_changed_ind_needed will to return
  162. * false for this peer unless @ref gscm_local_database_has_changed is called again.
  163. *
  164. * @param[in] peer_id The connection to send the indication on.
  165. */
  166. void gscm_db_change_notification_done(pm_peer_id_t peer_id);
  167. /** @}
  168. * @endcond
  169. */
  170. #endif /* GATTS_CACHE_MANAGER_H__ */