nrf_ble_gatt.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * Copyright (c) 2016 - 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. /** @file
  41. *
  42. * @defgroup nrf_ble_gatt GATT module
  43. * @{
  44. * @ingroup ble_sdk_lib
  45. * @brief Module for negotiating and keeping track of GATT connection parameters and updating the data length.
  46. */
  47. #ifndef NRF_BLE_GATT_H__
  48. #define NRF_BLE_GATT_H__
  49. #include <stdint.h>
  50. #include <stdbool.h>
  51. #include <string.h>
  52. #include "ble.h"
  53. #include "ble_gatt.h"
  54. #include "sdk_config.h"
  55. #include "sdk_errors.h"
  56. #include "app_util.h"
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. /**@brief Macro for defining a nrf_ble_gatt instance.
  61. *
  62. * @param _name Name of the instance.
  63. * @hideinitializer
  64. */
  65. #define NRF_BLE_GATT_DEF(_name) \
  66. static nrf_ble_gatt_t _name; \
  67. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  68. NRF_BLE_GATT_BLE_OBSERVER_PRIO, \
  69. nrf_ble_gatt_on_ble_evt, &_name)
  70. /**@brief The maximum number of peripheral and central connections combined.
  71. * This value is based on what is configured in the SoftDevice handler sdk_config.
  72. */
  73. #define NRF_BLE_GATT_LINK_COUNT (NRF_SDH_BLE_PERIPHERAL_LINK_COUNT + NRF_SDH_BLE_CENTRAL_LINK_COUNT)
  74. /**@brief GATT module event types. */
  75. typedef enum
  76. {
  77. NRF_BLE_GATT_EVT_ATT_MTU_UPDATED = 0xA77, //!< The ATT_MTU size was updated.
  78. NRF_BLE_GATT_EVT_DATA_LENGTH_UPDATED = 0xDA7A, //!< The data length was updated.
  79. } nrf_ble_gatt_evt_id_t;
  80. /**@brief GATT module event. */
  81. typedef struct
  82. {
  83. nrf_ble_gatt_evt_id_t evt_id; //!< Event ID.
  84. uint16_t conn_handle; //!< Connection handle on which the event happened.
  85. union
  86. {
  87. uint16_t att_mtu_effective; //!< Effective ATT_MTU.
  88. #if !defined (S112) && !defined(S312)
  89. uint8_t data_length; //!< Data length value.
  90. #endif // !defined (S112) && !defined(S312)
  91. } params;
  92. } nrf_ble_gatt_evt_t;
  93. // Forward declaration of the nrf_ble_gatt_t type.
  94. typedef struct nrf_ble_gatt_s nrf_ble_gatt_t;
  95. /**@brief GATT module event handler type.
  96. *
  97. * The GATT module calls a function of this type when a parameter value is changed.
  98. */
  99. typedef void (*nrf_ble_gatt_evt_handler_t) (nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt);
  100. /**@brief GATT information for each connection. */
  101. typedef struct
  102. {
  103. uint16_t att_mtu_desired; //!< Requested ATT_MTU size (in bytes).
  104. uint16_t att_mtu_effective; //!< Effective ATT_MTU size (in bytes).
  105. bool att_mtu_exchange_pending; //!< Indicates that an ATT_MTU exchange request is pending (the call to @ref sd_ble_gattc_exchange_mtu_request returned @ref NRF_ERROR_BUSY).
  106. bool att_mtu_exchange_requested; //!< Indicates that an ATT_MTU exchange request was made.
  107. #if !defined (S112) && !defined(S312)
  108. uint8_t data_length_desired; //!< Desired data length (in bytes).
  109. uint8_t data_length_effective; //!< Requested data length (in bytes).
  110. #endif // !defined (S112) && !defined(S312)
  111. } nrf_ble_gatt_link_t;
  112. /**@brief GATT structure that contains status information for the GATT module. */
  113. struct nrf_ble_gatt_s
  114. {
  115. uint16_t att_mtu_desired_periph; //!< Requested ATT_MTU size for the next peripheral connection that is established.
  116. uint16_t att_mtu_desired_central; //!< Requested ATT_MTU size for the next central connection that is established.
  117. uint8_t data_length; //!< Data length to use for the next connection that is established.
  118. nrf_ble_gatt_link_t links[NRF_BLE_GATT_LINK_COUNT]; //!< GATT related information for all active connections.
  119. nrf_ble_gatt_evt_handler_t evt_handler; //!< GATT event handler.
  120. };
  121. /**@brief Function for initializing the GATT module.
  122. *
  123. * @param[in] evt_handler Event handler.
  124. * @param[out] p_gatt Pointer to the GATT structure.
  125. *
  126. * @retval NRF_SUCCESS If the operation was successful.
  127. * @retval NRF_ERROR_NULL If @p p_gatt is NULL.
  128. */
  129. ret_code_t nrf_ble_gatt_init(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_handler_t evt_handler);
  130. /**@brief Function for setting the ATT_MTU size for the next connection that is established as peripheral.
  131. *
  132. * @param[in] p_gatt Pointer to the GATT structure.
  133. * @param[in] desired_mtu Requested ATT_MTU size.
  134. *
  135. * @retval NRF_SUCCESS If the operation was successful.
  136. * @retval NRF_ERROR_NULL If @p p_gatt is NULL.
  137. * @retval NRF_ERROR_INVALID_PARAM If the size of @p desired_mtu is bigger than
  138. * @ref NRF_SDH_BLE_GATT_MAX_MTU_SIZE or smaller than
  139. * @ref BLE_GATT_ATT_MTU_DEFAULT.
  140. */
  141. ret_code_t nrf_ble_gatt_att_mtu_periph_set(nrf_ble_gatt_t * p_gatt, uint16_t desired_mtu);
  142. /**@brief Function for setting the ATT_MTU size for the next connection that is established as central.
  143. *
  144. * @param[in,out] p_gatt Pointer to the GATT structure.
  145. * @param[in] desired_mtu Requested ATT_MTU size.
  146. *
  147. * @retval NRF_SUCCESS If the operation was successful.
  148. * @retval NRF_ERROR_NULL If @p p_gatt is NULL.
  149. * @retval NRF_ERROR_INVALID_PARAM If the size of @p desired_mtu is bigger than
  150. * @ref NRF_SDH_BLE_GATT_MAX_MTU_SIZE or smaller
  151. * than @ref BLE_GATT_ATT_MTU_DEFAULT.
  152. */
  153. ret_code_t nrf_ble_gatt_att_mtu_central_set(nrf_ble_gatt_t * p_gatt, uint16_t desired_mtu);
  154. /**@brief Function for setting the data length for a connection.
  155. *
  156. * @details If @p conn_handle is a handle to an existing connection, a data length update
  157. * request is sent on that connection.
  158. * If @p conn_handle is @ref BLE_CONN_HANDLE_INVALID, a data length update request
  159. * is sent on the next connection that is established after the ATT_MTU
  160. * exchange has completed. If no ATT_MTU exchange procedure is carried
  161. * out (for example, if a default ATT_MTU size is used), the data length
  162. * is not changed.
  163. */
  164. #if !defined (S112) && !defined(S312)
  165. ret_code_t nrf_ble_gatt_data_length_set(nrf_ble_gatt_t * p_gatt,
  166. uint16_t conn_handle,
  167. uint8_t data_length);
  168. #endif // !defined (S112) && !defined(S312)
  169. /**@brief Function for retrieving the data length of a connection.
  170. *
  171. * @details If @p conn_handle is @ref BLE_CONN_HANDLE_INVALID, the function retrieves the data
  172. * length that will be requested for the next connection.
  173. * If @p conn_handle is a handle to an existing connection, the function retrieves
  174. * the effective data length that was negotiated for that connection.
  175. *
  176. * @param[in,out] p_gatt Pointer to the GATT structure.
  177. * @param[in] conn_handle The connection for which to retrieve the data length, or
  178. * @ref BLE_CONN_HANDLE_INVALID to retrieve the requested data length
  179. * for the next connection.
  180. * @param[out] p_data_length The connection data length.
  181. *
  182. * @retval NRF_SUCCESS If the operation was successful.
  183. * @retval NRF_ERROR_NULL If @p p_gatt or @p p_data_length is NULL.
  184. * @retval NRF_ERROR_INVALID_PARAM If @p conn_handle is larger than @ref NRF_BLE_GATT_LINK_COUNT.
  185. */
  186. #if !defined (S112) && !defined(S312)
  187. ret_code_t nrf_ble_gatt_data_length_get(nrf_ble_gatt_t const * p_gatt,
  188. uint16_t conn_handle,
  189. uint8_t * p_data_length);
  190. #endif // !defined (S112) && !defined(S312)
  191. /**@brief Function for handling BLE stack events.
  192. *
  193. * @details This function handles events from the BLE stack that are of interest to the module.
  194. *
  195. * @param[in] p_ble_evt Event received from the BLE stack.
  196. * @param[in] p_context Pointer to the GATT structure.
  197. */
  198. void nrf_ble_gatt_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  199. /**@brief Function for getting the current ATT_MTU size for a given connection.
  200. *
  201. * @param[in] p_gatt Pointer to the GATT structure.
  202. * @param[in] conn_handle Connection handle of the connection.
  203. *
  204. * @return ATT_MTU size for the given connection.
  205. * @retval 0 If @p p_gatt is NULL or if @p conn_handle is larger than
  206. * the supported maximum number of connections.
  207. */
  208. uint16_t nrf_ble_gatt_eff_mtu_get(nrf_ble_gatt_t const * p_gatt, uint16_t conn_handle);
  209. #ifdef __cplusplus
  210. }
  211. #endif
  212. #endif // NRF_BLE_GATT_H__
  213. /** @} */