ble_ias.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 Immediate Alert Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Immediate Alert Service module.
  46. *
  47. * @details This module implements the Immediate Alert Service with the Alert Level characteristic.
  48. * During initialization it adds the Immediate Alert Service and Alert Level characteristic
  49. * to the BLE stack database.
  50. *
  51. * The application must supply an event handler for receiving Immediate Alert Service
  52. * events. Using this handler, the service will notify the application when the
  53. * Alert Level characteristic value changes.
  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_ias_t instance;
  62. * NRF_SDH_BLE_OBSERVER(anything, BLE_IAS_BLE_OBSERVER_PRIO,
  63. * ble_ias_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_IAS_H__
  71. #define BLE_IAS_H__
  72. #include <stdint.h>
  73. #include "ble.h"
  74. #include "nrf_sdh_ble.h"
  75. #include "ble_link_ctx_manager.h"
  76. #include "ble_srv_common.h"
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. /**@brief Macro for defining a ble_ias instance.
  81. *
  82. * @param _name Name of the instance.
  83. * @param[in] _ias_max_clients Maximum number of IAS clients connected at a time.
  84. * @hideinitializer
  85. */
  86. #define BLE_IAS_DEF(_name, _ias_max_clients) \
  87. BLE_LINK_CTX_MANAGER_DEF(CONCAT_2(_name, _link_ctx_storage), \
  88. (_ias_max_clients), \
  89. sizeof(ble_ias_client_context_t)); \
  90. static ble_ias_t _name = \
  91. { \
  92. .p_link_ctx_storage = &CONCAT_2(_name, _link_ctx_storage) \
  93. }; \
  94. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  95. BLE_IAS_BLE_OBSERVER_PRIO, \
  96. ble_ias_on_ble_evt, \
  97. &_name)
  98. /**@brief Immediate Alert Service event type. */
  99. typedef enum
  100. {
  101. BLE_IAS_EVT_ALERT_LEVEL_UPDATED /**< Alert Level Updated event. */
  102. } ble_ias_evt_type_t;
  103. /**@brief Immediate Alert Service client context structure.
  104. *
  105. * @details This structure contains state context related to hosts.
  106. */
  107. typedef struct
  108. {
  109. uint8_t alert_level; /**< New Alert Level value. */
  110. } ble_ias_client_context_t;
  111. /**@brief Immediate Alert Service event. */
  112. typedef struct
  113. {
  114. ble_ias_evt_type_t evt_type; /**< Type of event. */
  115. uint16_t conn_handle; /**< Connection handle. */
  116. ble_ias_client_context_t * p_link_ctx; /**< A pointer to the link context. */
  117. } ble_ias_evt_t;
  118. // Forward declaration of the ble_ias_t type.
  119. typedef struct ble_ias_s ble_ias_t;
  120. /**@brief Immediate Alert Service event handler type. */
  121. typedef void (*ble_ias_evt_handler_t) (ble_ias_t * p_ias, ble_ias_evt_t * p_evt);
  122. /**@brief Immediate Alert Service init structure. This contains all options and data needed for
  123. * initialization of the service. */
  124. typedef struct
  125. {
  126. ble_ias_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Immediate Alert Service. */
  127. security_req_t alert_wr_sec; /**< Security requirement for writing Alert Level characteristic. */
  128. } ble_ias_init_t;
  129. /**@brief Immediate Alert Service structure. This contains various status information for the
  130. * service. */
  131. struct ble_ias_s
  132. {
  133. ble_ias_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Immediate Alert Service. */
  134. uint16_t service_handle; /**< Handle of Immediate Alert Service (as provided by the BLE stack). */
  135. ble_gatts_char_handles_t alert_level_handles; /**< Handles related to the Alert Level characteristic. */
  136. blcm_link_ctx_storage_t * const p_link_ctx_storage; /**< Pointer to link context storage with handles of all current connections and its context. */
  137. };
  138. /**@brief Function for initializing the Immediate Alert Service.
  139. *
  140. * @param[out] p_ias Immediate Alert Service structure. This structure will have to be
  141. * supplied by the application. It will be initialized by this function,
  142. * and will later be used to identify this particular service instance.
  143. * @param[in] p_ias_init Information needed to initialize the service.
  144. *
  145. * @return NRF_SUCCESS on successful initialization of service, otherwise an error code.
  146. */
  147. uint32_t ble_ias_init(ble_ias_t * p_ias, const ble_ias_init_t * p_ias_init);
  148. /**@brief Function for handling the Application's BLE Stack events.
  149. *
  150. * @details Handles all events from the BLE stack of interest to the Immediate Alert Service.
  151. *
  152. * @param[in] p_ble_evt Event received from the BLE stack.
  153. * @param[in] p_context Immediate Alert Service structure.
  154. */
  155. void ble_ias_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  156. /**@brief Function for getting value of the Alert Level characteristic.
  157. *
  158. * @param[in] p_ias Immediate Alert Service structure.
  159. * @param[in] conn_handle Connection handle of the destination client.
  160. * @param[out] p_alert_level Alert Level value which has been set by the specific client.
  161. */
  162. uint32_t ble_ias_alert_level_get(ble_ias_t * p_ias, uint16_t conn_handle, uint8_t * p_alert_level);
  163. #ifdef __cplusplus
  164. }
  165. #endif
  166. #endif // BLE_IAS_H__
  167. /** @} */