ble_ias_c.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * Copyright (c) 2012 - 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 ble_ias_c Immediate Alert Service Client
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Immediate Alert Service Client module
  46. *
  47. * @details This module implements the Immediate Alert Service client - the locator role of the Find Me
  48. * profile. On @ref BLE_GAP_EVT_CONNECTED event, this module starts discovery of the
  49. * Immediate Alert Service with Alert Level characteristic at the peer. This module will
  50. * inform the application about the successful service and characteristic discovery with
  51. * the @ref BLE_IAS_C_EVT_DISCOVERY_COMPLETE event. The application can use @ref
  52. * ble_ias_c_send_alert_level function to signal alerts to the peer.
  53. *
  54. * @note The application must register this module as the BLE event observer by using the
  55. * NRF_SDH_BLE_OBSERVER macro. Example:
  56. * @code
  57. * ble_ias_c_t instance;
  58. * NRF_SDH_BLE_OBSERVER(anything, BLE_IAS_C_BLE_OBSERVER_PRIO,
  59. * ble_ias_c_on_ble_evt, &instance);
  60. * @endcode
  61. */
  62. #ifndef BLE_IAS_C_H__
  63. #define BLE_IAS_C_H__
  64. #include <stdint.h>
  65. #include "ble_srv_common.h"
  66. #include "ble_gattc.h"
  67. #include "ble.h"
  68. #include "ble_db_discovery.h"
  69. #include "nrf_ble_gq.h"
  70. #include "nrf_sdh_ble.h"
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. /**@brief Macro for defining a ble_ias_c instance.
  75. *
  76. * @param _name Name of the instance.
  77. * @hideinitializer
  78. */
  79. #define BLE_IAS_C_DEF(_name) \
  80. static ble_ias_c_t _name; \
  81. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  82. BLE_IAS_C_BLE_OBSERVER_PRIO, \
  83. ble_ias_c_on_ble_evt, &_name)
  84. /** @brief Macro for defining multiple ble_ias_c instances.
  85. *
  86. * @param _name Name of the array of instances.
  87. * @param _cnt Number of instances to define.
  88. * @hideinitializer
  89. */
  90. #define BLE_IAS_C_ARRAY_DEF(_name, _cnt) \
  91. static ble_ias_c_t _name[_cnt]; \
  92. NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
  93. BLE_IAS_C_BLE_OBSERVER_PRIO, \
  94. ble_ias_c_on_ble_evt, &_name, _cnt)
  95. // Forward declaration of the ble_ias_c_t type.
  96. typedef struct ble_ias_c_s ble_ias_c_t;
  97. /**@brief Immediate Alert Service client event type. */
  98. typedef enum
  99. {
  100. BLE_IAS_C_EVT_DISCOVERY_COMPLETE, /**< Event indicating that the Immediate Alert Service is found at the peer. */
  101. BLE_IAS_C_EVT_DISCOVERY_FAILED, /**< Event indicating that the Immediate Alert Service is not found at the peer. */
  102. BLE_IAS_C_EVT_DISCONN_COMPLETE /**< Event indicating that the Immediate Alert Service Client module completed the processing of BLE_GAP_EVT_DISCONNECTED event. This event is triggered only if a valid instance of IAS was found at the peer during the discovery phase. The application can use this event to do a cleanup related to the IAS Client.*/
  103. } ble_ias_c_evt_type_t;
  104. /**@brief Immediate Alert Service client event. */
  105. typedef struct
  106. {
  107. ble_ias_c_evt_type_t evt_type; /**< Type of event. */
  108. uint16_t conn_handle; /**< Connection handle on which the IAS service was discovered on the peer device. This is filled if the evt_type is @ref BLE_IAS_C_EVT_DISCOVERY_COMPLETE.*/
  109. ble_gattc_char_t alert_level; /**< Information on the discovered Alert Level characteristic discovered. This is filled if the evt_type is @ref BLE_IAS_C_EVT_DISCOVERY_COMPLETE.*/
  110. } ble_ias_c_evt_t;
  111. /**@brief Immediate Alert Service client event handler type. */
  112. typedef void (*ble_ias_c_evt_handler_t) (ble_ias_c_t * p_ias_c, ble_ias_c_evt_t * p_evt);
  113. /**@brief IAS Client structure. Contains various status information for the client. */
  114. struct ble_ias_c_s
  115. {
  116. ble_ias_c_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Immediate Alert Service client. */
  117. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  118. uint16_t conn_handle; /**< Handle of the current connection. Set with @ref ble_ias_c_handles_assign when connected. */
  119. ble_uuid_t service_uuid; /**< The GATT Service holding the discovered Immediate Service. */
  120. ble_gattc_char_t alert_level_char; /**< IAS Alert Level Characteristic. Stores data about the alert characteristic found on the peer. */
  121. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to the BLE GATT Queue instance. */
  122. };
  123. /**@brief IAS Client init structure. Contains all options and data needed for the initialization of
  124. * the client.*/
  125. typedef struct
  126. {
  127. ble_ias_c_evt_handler_t evt_handler; /**< Event handler to be called for handling events from the Immediate Alert Service client. */
  128. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  129. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to the BLE GATT Queue instance. */
  130. } ble_ias_c_init_t;
  131. /**@brief Function for initializing the Immediate Alert Service client.
  132. *
  133. * @details This call allows the application to initialize the Immediate Alert Service client.
  134. *
  135. * @param[out] p_ias_c Immediate Alert Service client structure. This structure must
  136. * be supplied by the application. It is initialized by this
  137. * function, and is later used to identify this particular client
  138. * instance.
  139. * @param[in] p_ias_c_init Information needed to initialize the Immediate Alert Service client.
  140. *
  141. * @return NRF_SUCCESS on successful initialization of service.
  142. */
  143. uint32_t ble_ias_c_init(ble_ias_c_t * p_ias_c, ble_ias_c_init_t const * p_ias_c_init);
  144. /**@brief Function for sending alert level to the peer.
  145. *
  146. * @details This function allows the application to send an alert to the peer.
  147. *
  148. * @param[in] p_ias_c Immediate Alert Service client structure.
  149. * @param[in] alert_level Required alert level to be sent to the peer.
  150. *
  151. * @retval NRF_SUCCESS On success.
  152. * @retval err_code Otherwise, this API propagates the error code returned by function
  153. * @ref nrf_ble_gq_item_add.
  154. */
  155. uint32_t ble_ias_c_send_alert_level(ble_ias_c_t const * p_ias_c, uint8_t alert_level);
  156. /**@brief Function for handling the Application's BLE Stack events for Immediate Alert Service client.
  157. *
  158. * @details Handles all events from the BLE stack of interest to the Immediate Alert Service client.
  159. *
  160. * @param[in] p_ble_evt Event received from the BLE stack.
  161. * @param[in] p_context Immediate Alert Service client structure.
  162. */
  163. void ble_ias_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  164. /**@brief Function for checking whether the peer's Immediate Alert Service instance and the Alert Level
  165. * characteristic have been discovered.
  166. *
  167. * @param[in] p_ias_c Immediate Alert Service client structure.
  168. *
  169. * @return True, if a handle is assigned to alert_level_handle, meaning it must have been discovered.
  170. * @return False, if the handle is invalid.
  171. */
  172. static __INLINE bool ble_ias_c_is_discovered(ble_ias_c_t const * p_ias_c)
  173. {
  174. return (p_ias_c->alert_level_char.handle_value != BLE_GATT_HANDLE_INVALID);
  175. }
  176. /**@brief Function for handling events from the Database Discovery module.
  177. *
  178. * @details Call this function when you get a callback event from the Database Discovery module.
  179. * This function handles an event from the Database Discovery module, and determines
  180. * whether it relates to the discovery of Immediate Alert Service at the peer. If it does, the function
  181. * calls the application's event handler to indicate that the Immediate Alert Service was
  182. * discovered at the peer. The function also populates the event with service-related
  183. * information before providing it to the application.
  184. *
  185. * @param[in] p_ias_c Pointer to the Immediate Alert client structure instance that will handle
  186. * the discovery.
  187. * @param[in] p_evt Pointer to the event received from the Database Discovery module.
  188. *
  189. */
  190. void ble_ias_c_on_db_disc_evt(ble_ias_c_t * p_ias_c, ble_db_discovery_evt_t const * p_evt);
  191. /**@brief Function for assigning handles to an instance of ias_c.
  192. *
  193. * @details Call this function when a link has been established with a peer to
  194. * associate the link to this instance of the module. This makes it
  195. * possible to handle several links and associate each link to a particular
  196. * instance of this module. The connection handle and attribute handles are
  197. * provided from the discovery event @ref BLE_IAS_C_EVT_DISCOVERY_COMPLETE.
  198. *
  199. * @param[in] p_ias_c Pointer to the IAS client structure instance for associating the link.
  200. * @param[in] conn_handle Connection handle to associated with the given IAS instance.
  201. * @param[in] alert_level_handle Attribute handle on the IAS server that you want this IAS_C client to
  202. * interact with.
  203. *
  204. * @retval NRF_SUCCESS If the operation was successful.
  205. * @retval NRF_ERROR_NULL If a p_ias_c was a NULL pointer.
  206. * @retval err_code Otherwise, this API propagates the error code returned by function
  207. * @ref nrf_ble_gq_conn_handle_register.
  208. */
  209. uint32_t ble_ias_c_handles_assign(ble_ias_c_t * p_ias_c,
  210. uint16_t conn_handle,
  211. uint16_t alert_level_handle);
  212. #ifdef __cplusplus
  213. }
  214. #endif
  215. #endif // BLE_IAS_C_H__
  216. /** @} */