ble_bas.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 Battery Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Battery Service module.
  46. *
  47. * @details This module implements the Battery Service with the Battery Level characteristic.
  48. * During initialization it adds the Battery Service and Battery Level characteristic
  49. * to the BLE stack database. Optionally it can also add a Report Reference descriptor
  50. * to the Battery Level characteristic (used when including the Battery Service in
  51. * the HID service).
  52. *
  53. * If specified, the module will support notification of the Battery Level characteristic
  54. * through the ble_bas_battery_level_update() function.
  55. * If an event handler is supplied by the application, the Battery Service will
  56. * generate Battery Service events to the application.
  57. *
  58. * @note The application must register this module as BLE event observer using the
  59. * NRF_SDH_BLE_OBSERVER macro. Example:
  60. * @code
  61. * ble_bas_t instance;
  62. * NRF_SDH_BLE_OBSERVER(anything, BLE_BAS_BLE_OBSERVER_PRIO,
  63. * ble_bas_on_ble_evt, &instance);
  64. * @endcode
  65. *
  66. * @note Attention!
  67. * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile
  68. * qualification listings, this section of source code must not be modified.
  69. */
  70. #ifndef BLE_BAS_H__
  71. #define BLE_BAS_H__
  72. #include <stdint.h>
  73. #include <stdbool.h>
  74. #include "ble.h"
  75. #include "ble_srv_common.h"
  76. #include "nrf_sdh_ble.h"
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. /**@brief Macro for defining a ble_bas instance.
  81. *
  82. * @param _name Name of the instance.
  83. * @hideinitializer
  84. */
  85. #define BLE_BAS_DEF(_name) \
  86. static ble_bas_t _name; \
  87. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  88. BLE_BAS_BLE_OBSERVER_PRIO, \
  89. ble_bas_on_ble_evt, \
  90. &_name)
  91. /**@brief Battery Service event type. */
  92. typedef enum
  93. {
  94. BLE_BAS_EVT_NOTIFICATION_ENABLED, /**< Battery value notification enabled event. */
  95. BLE_BAS_EVT_NOTIFICATION_DISABLED /**< Battery value notification disabled event. */
  96. } ble_bas_evt_type_t;
  97. /**@brief Battery Service event. */
  98. typedef struct
  99. {
  100. ble_bas_evt_type_t evt_type; /**< Type of event. */
  101. uint16_t conn_handle; /**< Connection handle. */
  102. } ble_bas_evt_t;
  103. // Forward declaration of the ble_bas_t type.
  104. typedef struct ble_bas_s ble_bas_t;
  105. /**@brief Battery Service event handler type. */
  106. typedef void (* ble_bas_evt_handler_t) (ble_bas_t * p_bas, ble_bas_evt_t * p_evt);
  107. /**@brief Battery Service init structure. This contains all options and data needed for
  108. * initialization of the service.*/
  109. typedef struct
  110. {
  111. ble_bas_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Battery Service. */
  112. bool support_notification; /**< TRUE if notification of Battery Level measurement is supported. */
  113. ble_srv_report_ref_t * p_report_ref; /**< If not NULL, a Report Reference descriptor with the specified value will be added to the Battery Level characteristic */
  114. uint8_t initial_batt_level; /**< Initial battery level */
  115. security_req_t bl_rd_sec; /**< Security requirement for reading the BL characteristic value. */
  116. security_req_t bl_cccd_wr_sec; /**< Security requirement for writing the BL characteristic CCCD. */
  117. security_req_t bl_report_rd_sec; /**< Security requirement for reading the BL characteristic descriptor. */
  118. } ble_bas_init_t;
  119. /**@brief Battery Service structure. This contains various status information for the service. */
  120. struct ble_bas_s
  121. {
  122. ble_bas_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Battery Service. */
  123. uint16_t service_handle; /**< Handle of Battery Service (as provided by the BLE stack). */
  124. ble_gatts_char_handles_t battery_level_handles; /**< Handles related to the Battery Level characteristic. */
  125. uint16_t report_ref_handle; /**< Handle of the Report Reference descriptor. */
  126. uint8_t battery_level_last; /**< Last Battery Level measurement passed to the Battery Service. */
  127. bool is_notification_supported; /**< TRUE if notification of Battery Level is supported. */
  128. };
  129. /**@brief Function for initializing the Battery Service.
  130. *
  131. * @param[out] p_bas Battery Service structure. This structure will have to be supplied by
  132. * the application. It will be initialized by this function, and will later
  133. * be used to identify this particular service instance.
  134. * @param[in] p_bas_init Information needed to initialize the service.
  135. *
  136. * @return NRF_SUCCESS on successful initialization of service, otherwise an error code.
  137. */
  138. ret_code_t ble_bas_init(ble_bas_t * p_bas, const ble_bas_init_t * p_bas_init);
  139. /**@brief Function for handling the Application's BLE Stack events.
  140. *
  141. * @details Handles all events from the BLE stack of interest to the Battery Service.
  142. *
  143. * @note For the requirements in the BAS specification to be fulfilled,
  144. * ble_bas_battery_level_update() must be called upon reconnection if the
  145. * battery level has changed while the service has been disconnected from a bonded
  146. * client.
  147. *
  148. * @param[in] p_ble_evt Event received from the BLE stack.
  149. * @param[in] p_context Battery Service structure.
  150. */
  151. void ble_bas_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  152. /**@brief Function for updating the battery level.
  153. *
  154. * @details The application calls this function after having performed a battery measurement.
  155. * The battery level characteristic will only be sent to the clients which have
  156. * enabled notifications. \ref BLE_CONN_HANDLE_ALL can be used as a connection handle
  157. * to send notifications to all connected devices.
  158. *
  159. * @param[in] p_bas Battery Service structure.
  160. * @param[in] battery_level New battery measurement value (in percent of full capacity).
  161. * @param[in] conn_handle Connection handle.
  162. *
  163. * @return NRF_SUCCESS on success, otherwise an error code.
  164. */
  165. ret_code_t ble_bas_battery_level_update(ble_bas_t * p_bas,
  166. uint8_t battery_level,
  167. uint16_t conn_handle);
  168. /**@brief Function for sending the last battery level when bonded client reconnects.
  169. *
  170. * @details The application calls this function, in the case of a reconnection of
  171. * a bonded client if the value of the battery has changed since
  172. * its disconnection.
  173. *
  174. * @note For the requirements in the BAS specification to be fulfilled,
  175. * this function must be called upon reconnection if the battery level has changed
  176. * while the service has been disconnected from a bonded client.
  177. *
  178. * @param[in] p_bas Battery Service structure.
  179. * @param[in] conn_handle Connection handle.
  180. *
  181. * @return NRF_SUCCESS on success,
  182. * NRF_ERROR_INVALID_STATE when notification is not supported,
  183. * otherwise an error code returned by @ref sd_ble_gatts_hvx.
  184. */
  185. ret_code_t ble_bas_battery_lvl_on_reconnection_update(ble_bas_t * p_bas,
  186. uint16_t conn_handle);
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. #endif // BLE_BAS_H__
  191. /** @} */