ble_bas_c.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_bas_c Battery Service Client
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Battery Service Client module.
  46. *
  47. * @details This module contains APIs to read and interact with the Battery Service of a remote
  48. * device.
  49. *
  50. * @note The application must register this module as the BLE event observer by using the
  51. * NRF_SDH_BLE_OBSERVER macro. Example:
  52. * @code
  53. * ble_bas_c_t instance;
  54. * NRF_SDH_BLE_OBSERVER(anything, BLE_BAS_C_BLE_OBSERVER_PRIO,
  55. * ble_bas_c_on_ble_evt, &instance);
  56. * @endcode
  57. */
  58. #ifndef BLE_BAS_C_H__
  59. #define BLE_BAS_C_H__
  60. #include <stdint.h>
  61. #include "ble.h"
  62. #include "ble_db_discovery.h"
  63. #include "ble_srv_common.h"
  64. #include "nrf_ble_gq.h"
  65. #include "nrf_sdh_ble.h"
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69. /**@brief Macro for defining a ble_bas_c instance.
  70. *
  71. * @param _name Name of the instance.
  72. * @hideinitializer
  73. */
  74. #define BLE_BAS_C_DEF(_name) \
  75. static ble_bas_c_t _name; \
  76. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  77. BLE_BAS_C_BLE_OBSERVER_PRIO, \
  78. ble_bas_c_on_ble_evt, &_name)
  79. /** @brief Macro for defining multiple ble_bas_c instances.
  80. *
  81. * @param _name Name of the array of instances.
  82. * @param _cnt Number of instances to define.
  83. * @hideinitializer
  84. */
  85. #define BLE_BAS_C_ARRAY_DEF(_name, _cnt) \
  86. static ble_bas_c_t _name[_cnt]; \
  87. NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
  88. BLE_BAS_C_BLE_OBSERVER_PRIO, \
  89. ble_bas_c_on_ble_evt, &_name, _cnt)
  90. /**
  91. * @defgroup bas_c_enums Enumerations
  92. * @{
  93. */
  94. /**@brief Battery Service Client event type. */
  95. typedef enum
  96. {
  97. BLE_BAS_C_EVT_DISCOVERY_COMPLETE, /**< Event indicating that the Battery Service was discovered at the peer. */
  98. BLE_BAS_C_EVT_BATT_NOTIFICATION, /**< Event indicating that a notification of the Battery Level characteristic was received from the peer. */
  99. BLE_BAS_C_EVT_BATT_READ_RESP /**< Event indicating that a read response on Battery Level characteristic was received from the peer. */
  100. } ble_bas_c_evt_type_t;
  101. /** @} */
  102. /**
  103. * @defgroup bas_c_structs Structures
  104. * @{
  105. */
  106. /**@brief Structure containing the handles related to the Battery Service found on the peer. */
  107. typedef struct
  108. {
  109. uint16_t bl_cccd_handle; /**< Handle of the CCCD of the Battery Level characteristic. */
  110. uint16_t bl_handle; /**< Handle of the Battery Level characteristic, as provided by the SoftDevice. */
  111. } ble_bas_c_db_t;
  112. /**@brief Battery Service Client Event structure. */
  113. typedef struct
  114. {
  115. ble_bas_c_evt_type_t evt_type; /**< Event Type. */
  116. uint16_t conn_handle; /**< Connection handle relevant to this event.*/
  117. union
  118. {
  119. ble_bas_c_db_t bas_db; /**< Handles related to the Battery Service, found on the peer device. This is filled if the evt_type is @ref BLE_BAS_C_EVT_DISCOVERY_COMPLETE.*/
  120. uint8_t battery_level; /**< Information about the battery level, received from peer. This field is used for the events @ref BLE_BAS_C_EVT_BATT_NOTIFICATION and @ref BLE_BAS_C_EVT_BATT_READ_RESP.*/
  121. } params;
  122. } ble_bas_c_evt_t;
  123. /** @} */
  124. /**
  125. * @defgroup bas_c_types Types
  126. * @{
  127. */
  128. // Forward declaration of the ble_bas_t type.
  129. typedef struct ble_bas_c_s ble_bas_c_t;
  130. /**@brief Event handler type.
  131. *
  132. * @details This is the type of the event handler that is to be provided by the application
  133. * of the BAS module to receive events.
  134. */
  135. typedef void (* ble_bas_c_evt_handler_t) (ble_bas_c_t * p_bas_bas_c, ble_bas_c_evt_t * p_evt);
  136. /** @} */
  137. /**
  138. * @addtogroup bas_c_structs
  139. * @{
  140. */
  141. /**@brief Battery Service Client structure. */
  142. struct ble_bas_c_s
  143. {
  144. uint16_t conn_handle; /**< Connection handle, as provided by the SoftDevice. */
  145. ble_bas_c_db_t peer_bas_db; /**< Handles related to BAS on the peer.*/
  146. ble_bas_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the Battery Service. */
  147. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  148. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to the BLE GATT Queue instance. */
  149. };
  150. /**@brief Battery Service Client initialization structure. */
  151. typedef struct
  152. {
  153. ble_bas_c_evt_handler_t evt_handler; /**< Event handler to be called by the Battery Service Client module when there is an event related to the Battery Service. */
  154. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  155. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to the BLE GATT Queue instance. */
  156. } ble_bas_c_init_t;
  157. /** @} */
  158. /**
  159. * @defgroup bas_c_functions Functions
  160. * @{
  161. */
  162. /**@brief Function for initializing the Battery Service Client module.
  163. *
  164. * @details This function initializes the module and sets up database discovery to discover
  165. * the Battery Service. After calling this function, call @ref ble_db_discovery_start
  166. * to start discovery once a link with a peer has been established.
  167. *
  168. * @param[out] p_ble_bas_c Pointer to the Battery Service Client structure.
  169. * @param[in] p_ble_bas_c_init Pointer to the Battery Service initialization structure that contains
  170. * the initialization information.
  171. *
  172. * @retval NRF_SUCCESS Operation success.
  173. * @retval NRF_ERROR_NULL A parameter is NULL.
  174. * @retval err_code Otherwise, an error code returned by @ref ble_db_discovery_evt_register.
  175. */
  176. uint32_t ble_bas_c_init(ble_bas_c_t * p_ble_bas_c, ble_bas_c_init_t * p_ble_bas_c_init);
  177. /**@brief Function for handling BLE events from the SoftDevice.
  178. *
  179. * @details This function handles the BLE events received from the SoftDevice. If a BLE
  180. * event is relevant to the Battery Service Client module, the function uses the event's data to update
  181. * interval variables and, if necessary, send events to the application.
  182. *
  183. * @note This function must be called by the application.
  184. *
  185. * @param[in] p_ble_evt Pointer to the BLE event.
  186. * @param[in] p_context Pointer to the Battery Service client structure.
  187. */
  188. void ble_bas_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  189. /**@brief Function for enabling notifications on the Battery Level characteristic.
  190. *
  191. * @details This function enables the notification of the Battery Level characteristic at the
  192. * peer by writing to the CCCD of the Battery Level Characteristic.
  193. *
  194. * @param p_ble_bas_c Pointer to the Battery Service client structure.
  195. *
  196. * @retval NRF_SUCCESS If the SoftDevice has been requested to write to the CCCD of the peer.
  197. * @retval NRF_ERROR_NULL Parameter is NULL.
  198. * @retval err_code Otherwise, an error code returned by the SoftDevice API @ref
  199. * sd_ble_gattc_write.
  200. */
  201. uint32_t ble_bas_c_bl_notif_enable(ble_bas_c_t * p_ble_bas_c);
  202. /**@brief Function for reading the Battery Level characteristic.
  203. *
  204. * @param p_ble_bas_c Pointer to the Battery Service client structure.
  205. *
  206. * @retval NRF_SUCCESS If the read request was successfully queued to be sent to peer.
  207. */
  208. uint32_t ble_bas_c_bl_read(ble_bas_c_t * p_ble_bas_c);
  209. /**@brief Function for handling events from the Database Discovery module.
  210. *
  211. * @details Call this function when you get a callback event from the Database Discovery module.
  212. * This function handles an event from the Database Discovery module, and determines
  213. * whether it relates to the discovery of Battery Service at the peer. If it does, this function
  214. * calls the application's event handler to indicate that the Battery Service was
  215. * discovered at the peer. The function also populates the event with service-related
  216. * information before providing it to the application.
  217. *
  218. * @param p_ble_bas_c Pointer to the Battery Service client structure.
  219. * @param[in] p_evt Pointer to the event received from the Database Discovery module.
  220. *
  221. */
  222. void ble_bas_on_db_disc_evt(ble_bas_c_t * p_ble_bas_c, ble_db_discovery_evt_t const * p_evt);
  223. /**@brief Function for assigning handles to this instance of bas_c.
  224. *
  225. * @details Call this function when a link has been established with a peer to
  226. * associate the link to this instance of the module. This makes it
  227. * possible to handle several links and associate each link to a particular
  228. * instance of this module. The connection handle and attribute handles are
  229. * provided from the discovery event @ref BLE_BAS_C_EVT_DISCOVERY_COMPLETE.
  230. *
  231. * @param[in] p_ble_bas_c Pointer to the Battery client structure instance for associating the link.
  232. * @param[in] conn_handle Connection handle associated with the given Battery Client Instance.
  233. * @param[in] p_peer_handles Attribute handles on the BAS server you want this BAS client to
  234. * interact with.
  235. */
  236. uint32_t ble_bas_c_handles_assign(ble_bas_c_t * p_ble_bas_c,
  237. uint16_t conn_handle,
  238. ble_bas_c_db_t * p_peer_handles);
  239. /** @} */ // End tag for Function group.
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #endif // BLE_BAS_C_H__
  244. /** @} */ // End tag for the file.