ble_advertising.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. */
  11. /**@file
  12. *
  13. * @defgroup ble_sdk_lib_advertising Advertising Module
  14. * @{
  15. * @ingroup ble_sdk_lib
  16. * @brief Module for handling connectable BLE advertising.
  17. *
  18. * @details The Advertising Module handles connectable advertising for your application. It can
  19. * be configured with advertising modes to suit most typical use cases.
  20. * Your main application can react to changes in advertising modes
  21. * if an event handler is provided.
  22. *
  23. * @note The Advertising Module supports only applications with a single peripheral link.
  24. *
  25. * The application must propagate BLE stack events to this module by calling
  26. * @ref ble_advertising_on_ble_evt() and system events by calling
  27. * @ref ble_advertising_on_sys_evt().
  28. *
  29. */
  30. #ifndef BLE_ADVERTISING_H__
  31. #define BLE_ADVERTISING_H__
  32. #include <stdint.h>
  33. #include "ble_gattc.h"
  34. #include "ble.h"
  35. #include "nrf_error.h"
  36. #include "ble_advdata.h"
  37. /**@brief Advertising modes.
  38. */
  39. typedef enum
  40. {
  41. BLE_ADV_MODE_IDLE, /**< Idle; no connectable advertising is ongoing.*/
  42. BLE_ADV_MODE_DIRECTED, /**< Directed advertising attempts to connect to the most recently disconnected peer. */
  43. BLE_ADV_MODE_DIRECTED_SLOW, /**< Directed advertising (low duty cycle) attempts to connect to the most recently disconnected peer. */
  44. BLE_ADV_MODE_FAST, /**< Fast advertising will connect to any peer device, or filter with a whitelist if one exists. */
  45. BLE_ADV_MODE_SLOW, /**< Slow advertising is similar to fast advertising. By default, it uses a longer advertising interval and time-out than fast advertising. However, these options are defined by the user. */
  46. } ble_adv_mode_t;
  47. /**@brief Advertising events.
  48. *
  49. * @details These events are propagated to the main application if a handler was provided during
  50. * initialization of the Advertising Module. Events for modes that are not used can be
  51. * ignored. Similarly, BLE_ADV_EVT_WHITELIST_REQUEST and BLE_ADV_EVT_PEER_ADDR_REQUEST
  52. * can be ignored if whitelist and direct advertising is not used.
  53. */
  54. typedef enum
  55. {
  56. BLE_ADV_EVT_IDLE, /**< Idle; no connectable advertising is ongoing.*/
  57. BLE_ADV_EVT_DIRECTED, /**< Direct advertising mode has started. */
  58. BLE_ADV_EVT_DIRECTED_SLOW, /**< Directed advertising (low duty cycle) has started. */
  59. BLE_ADV_EVT_FAST, /**< Fast advertising mode has started. */
  60. BLE_ADV_EVT_SLOW, /**< Slow advertising mode has started.*/
  61. BLE_ADV_EVT_FAST_WHITELIST, /**< Fast advertising mode using the whitelist has started. */
  62. BLE_ADV_EVT_SLOW_WHITELIST, /**< Slow advertising mode using the whitelist has started.*/
  63. BLE_ADV_EVT_WHITELIST_REQUEST, /**< Request a whitelist from the main application. For whitelist advertising to work, the whitelist must be set when this event occurs. */
  64. BLE_ADV_EVT_PEER_ADDR_REQUEST /**< Request a peer address from the main application. For directed advertising to work, the peer address must be set when this event occurs. */
  65. } ble_adv_evt_t;
  66. /**@brief Options for the different advertisement modes.
  67. *
  68. * @details This structure is used to enable or disable advertising modes and to configure time-out
  69. * periods and advertising intervals.
  70. */
  71. typedef struct
  72. {
  73. bool ble_adv_whitelist_enabled; /**< Enable or disable use of the whitelist. */
  74. bool ble_adv_directed_enabled; /**< Enable or disable direct advertising mode. */
  75. bool ble_adv_directed_slow_enabled; /**< Enable or disable direct advertising mode. */
  76. uint32_t ble_adv_directed_slow_interval; /**< Advertising interval for directed advertising. */
  77. uint32_t ble_adv_directed_slow_timeout; /**< Time-out (number of tries) for direct advertising. */
  78. bool ble_adv_fast_enabled; /**< Enable or disable fast advertising mode. */
  79. uint32_t ble_adv_fast_interval; /**< Advertising interval for fast advertising. */
  80. uint32_t ble_adv_fast_timeout; /**< Time-out (in seconds) for fast advertising. */
  81. bool ble_adv_slow_enabled; /**< Enable or disable slow advertising mode. */
  82. uint32_t ble_adv_slow_interval; /**< Advertising interval for slow advertising. */
  83. uint32_t ble_adv_slow_timeout; /**< Time-out (in seconds) for slow advertising. */
  84. }ble_adv_modes_config_t;
  85. /**@brief BLE advertising event handler type. */
  86. typedef void (*ble_advertising_evt_handler_t) (ble_adv_evt_t const adv_evt);
  87. /**@brief BLE advertising error handler type. */
  88. typedef void (*ble_advertising_error_handler_t) (uint32_t nrf_error);
  89. /**@brief Initialization parameters for the Advertising Module.
  90. * @details This structure is used to pass advertising options, advertising data, and an event handler to the Advertising Module during initialization. */
  91. typedef struct
  92. {
  93. ble_adv_modes_config_t options; /**< Parameters for advertising modes.*/
  94. ble_advdata_t advdata; /**< Advertising data. */
  95. ble_advertising_evt_handler_t evt_handler; /**< Event handler. */
  96. }ble_adv_init_t;
  97. /* Defines to make the mode options easier to set during advertising init.*/
  98. #define BLE_ADV_DIRECTED_ENABLED true
  99. #define BLE_ADV_DIRECTED_DISABLED false
  100. #define BLE_ADV_DIRECTED_SLOW_ENABLED true
  101. #define BLE_ADV_DIRECTED_SLOW_DISABLED false
  102. #define BLE_ADV_FAST_ENABLED true
  103. #define BLE_ADV_FAST_DISABLED false
  104. #define BLE_ADV_SLOW_ENABLED true
  105. #define BLE_ADV_SLOW_DISABLED false
  106. #define BLE_ADV_WHITELIST_ENABLED true
  107. #define BLE_ADV_WHITELIST_DISABLED false
  108. /**@brief Function for handling BLE events.
  109. *
  110. * @details This function must be called from the BLE stack event dispatcher for
  111. * the module to handle BLE events that are relevant for the Advertising Module.
  112. *
  113. * @param[in] p_ble_evt BLE stack event.
  114. */
  115. void ble_advertising_on_ble_evt(const ble_evt_t * const p_ble_evt);
  116. /**@brief Function for handling system events.
  117. *
  118. * @details This function must be called to handle system events that are relevant
  119. * for the Advertising Module. Specifically, the advertising module can not use the
  120. * softdevice as long as there are pending writes to the flash memory. This
  121. * event handler is designed to delay advertising until there is no flash operation.
  122. *
  123. * @param[in] sys_evt System event.
  124. */
  125. void ble_advertising_on_sys_evt(uint32_t sys_evt);
  126. /**@brief Function for initializing the Advertising Module.
  127. *
  128. * @details Encodes the required advertising data and passes it to the stack.
  129. * Also builds a structure to be passed to the stack when starting advertising.
  130. * The supplied advertising data is copied to a local structure and is manipulated
  131. * depending on what advertising modes are started in @ref ble_advertising_start.
  132. *
  133. * @param[in] p_advdata Advertising data: name, appearance, discovery flags, and more.
  134. * @param[in] p_srdata Scan response data: Supplement to advertising data.
  135. * @param[in] p_config Select which advertising modes and intervals will be utilized.
  136. * @param[in] evt_handler Event handler that will be called upon advertising events.
  137. * @param[in] error_handler Error handler that will propogate internal errors to the main applications.
  138. *
  139. * @retval NRF_SUCCESS If initialization was successful. Otherwise, an error code is returned.
  140. */
  141. uint32_t ble_advertising_init(ble_advdata_t const * p_advdata,
  142. ble_advdata_t const * p_srdata,
  143. ble_adv_modes_config_t const * p_config,
  144. ble_advertising_evt_handler_t const evt_handler,
  145. ble_advertising_error_handler_t const error_handler);
  146. /**@brief Function for starting advertising.
  147. *
  148. * @details You can start advertising in any of the advertising modes that you enabled
  149. * during initialization.
  150. *
  151. * @param[in] advertising_mode Advertising mode.
  152. *
  153. * @retval @ref NRF_SUCCESS On success, else an error code indicating reason for failure.
  154. * @retval @ref NRF_ERROR_INVALID_STATE
  155. */
  156. uint32_t ble_advertising_start(ble_adv_mode_t advertising_mode);
  157. /**@brief Function for setting the peer address.
  158. *
  159. * @details The peer address must be set by the application upon receiving a
  160. * @ref BLE_ADV_EVT_PEER_ADDR_REQUEST event. Without the peer address, the directed
  161. * advertising mode will not be run.
  162. *
  163. * @param[in] p_peer_addr Pointer to a peer address.
  164. *
  165. * @retval @ref NRF_SUCCESS Successfully stored the peer address pointer in the advertising module.
  166. * @retval @ref NRF_ERROR_INVALID_STATE If a reply was not expected.
  167. */
  168. uint32_t ble_advertising_peer_addr_reply(ble_gap_addr_t * p_peer_addr);
  169. /**@brief Function for setting a whitelist.
  170. *
  171. * @details The whitelist must be set by the application upon receiving a
  172. * @ref BLE_ADV_EVT_WHITELIST_REQUEST event. Without the whitelist, the whitelist
  173. * advertising for fast and slow modes will not be run.
  174. *
  175. * @param[in] p_whitelist Pointer to a whitelist.
  176. *
  177. * @retval @ref NRF_SUCCESS Successfully stored pointers to the whitelist into the advertising module.
  178. * @retval @ref NRF_ERROR_INVALID_STATE If a reply was not expected.
  179. */
  180. uint32_t ble_advertising_whitelist_reply(ble_gap_whitelist_t * p_whitelist);
  181. /**@brief Function for disabling whitelist advertising.
  182. *
  183. * @details This function temporarily disables whitelist advertising.
  184. * Calling this function resets the current time-out countdown.
  185. *
  186. * @retval @ref NRF_SUCCESS On success, else an error message propogated from the Softdevice.
  187. */
  188. uint32_t ble_advertising_restart_without_whitelist(void);
  189. /** @} */
  190. #endif // BLE_ADVERTISING_H__
  191. /** @} */