ble_lls.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_lls Link Loss Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Link Loss Service module.
  46. *
  47. * @details This module implements the Link Loss Service with the Alert Level characteristic.
  48. * During initialization it adds the Link Loss Service and Alert Level characteristic
  49. * to the BLE stack database.
  50. *
  51. * The application must supply an event handler for receiving Link Loss Service
  52. * events. Using this handler, the service will notify the application when the
  53. * link has been lost, and which Alert Level has been set.
  54. *
  55. * The service also provides a function for letting the application poll the current
  56. * value of the Alert Level characteristic.
  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_lls_t instance;
  62. * NRF_SDH_BLE_OBSERVER(anything, BLE_LLS_BLE_OBSERVER_PRIO,
  63. * ble_lls_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_LLS_H__
  71. #define BLE_LLS_H__
  72. #include <stdint.h>
  73. #include "ble.h"
  74. #include "ble_srv_common.h"
  75. #include "nrf_sdh_ble.h"
  76. #ifdef __cplusplus
  77. extern "C" {
  78. #endif
  79. /**@brief Macro for defining a ble_lls instance.
  80. *
  81. * @param _name Name of the instance.
  82. * @hideinitializer
  83. */
  84. #define BLE_LLS_DEF(_name) \
  85. static ble_lls_t _name; \
  86. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  87. BLE_LLS_BLE_OBSERVER_PRIO, \
  88. ble_lls_on_ble_evt, &_name)
  89. /**@brief Link Loss Service event type. */
  90. typedef enum
  91. {
  92. BLE_LLS_EVT_LINK_LOSS_ALERT /**< Alert Level Updated event. */
  93. } ble_lls_evt_type_t;
  94. /**@brief Link Loss Service event. */
  95. typedef struct
  96. {
  97. ble_lls_evt_type_t evt_type; /**< Type of event. */
  98. union
  99. {
  100. uint8_t alert_level; /**< New Alert Level value. */
  101. } params;
  102. } ble_lls_evt_t;
  103. // Forward declaration of the ble_lls_t type.
  104. typedef struct ble_lls_s ble_lls_t;
  105. /**@brief Link Loss Service event handler type. */
  106. typedef void (*ble_lls_evt_handler_t) (ble_lls_t * p_lls, ble_lls_evt_t * p_evt);
  107. /**@brief Link Loss Service init structure. This contains all options and data needed for initialization of the service. */
  108. typedef struct
  109. {
  110. ble_lls_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Link Loss Service. */
  111. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  112. uint8_t initial_alert_level; /**< Initial value of the Alert Level characteristic. */
  113. security_req_t alert_level_rd_sec; /**< Security requirement for reading Alert Level characteristic. */
  114. security_req_t alert_level_wr_sec; /**< Security requirement for writing Alert Level characteristic. */
  115. } ble_lls_init_t;
  116. /**@brief Link Loss Service structure. This contains various status information for the service. */
  117. struct ble_lls_s
  118. {
  119. ble_lls_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Link Loss Service. */
  120. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  121. uint16_t service_handle; /**< Handle of Link Loss Service (as provided by the BLE stack). */
  122. ble_gatts_char_handles_t alert_level_handles; /**< Handles related to the Alert Level characteristic. */
  123. uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */
  124. };
  125. /**@brief Function for initializing the Link Loss Service.
  126. *
  127. * @param[out] p_lls Link Loss Service structure. This structure will have to be supplied by
  128. * the application. It will be initialized by this function, and will later
  129. * be used to identify this particular service instance.
  130. * @param[in] p_lls_init Information needed to initialize the service.
  131. *
  132. * @return NRF_SUCCESS on successful initialization of service, otherwise an error code.
  133. */
  134. uint32_t ble_lls_init(ble_lls_t * p_lls, const ble_lls_init_t * p_lls_init);
  135. /**@brief Function for handling the Application's BLE Stack events.
  136. *
  137. * @details Handles all events from the BLE stack of interest to the Link Loss Service.
  138. *
  139. * @param[in] p_ble_evt Event received from the BLE stack.
  140. * @param[in] p_context Link Loss Service structure.
  141. */
  142. void ble_lls_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  143. /**@brief Function for getting current value of the Alert Level characteristic.
  144. *
  145. * @param[in] p_lls Link Loss Service structure.
  146. * @param[out] p_alert_level Current Alert Level value.
  147. *
  148. * @return NRF_SUCCESS on success, otherwise an error code.
  149. */
  150. uint32_t ble_lls_alert_level_get(ble_lls_t * p_lls, uint8_t * p_alert_level);
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. #endif // BLE_LLS_H__
  155. /** @} */