The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Thu Nov 08 11:45:42 2018 +0000
Revision:
171:3a7713b1edbc
Parent:
TARGET_NRF51_DK/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_11/ble/ble_advertising/ble_advertising.h@169:a7c7b631e539
mbed library. Release version 164

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 143:86740a56073b 1 /*
AnnaBridge 143:86740a56073b 2 * Copyright (c) 2015 Nordic Semiconductor ASA
AnnaBridge 143:86740a56073b 3 * All rights reserved.
AnnaBridge 143:86740a56073b 4 *
AnnaBridge 143:86740a56073b 5 * Redistribution and use in source and binary forms, with or without modification,
AnnaBridge 143:86740a56073b 6 * are permitted provided that the following conditions are met:
AnnaBridge 143:86740a56073b 7 *
AnnaBridge 143:86740a56073b 8 * 1. Redistributions of source code must retain the above copyright notice, this list
AnnaBridge 143:86740a56073b 9 * of conditions and the following disclaimer.
AnnaBridge 143:86740a56073b 10 *
AnnaBridge 143:86740a56073b 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
AnnaBridge 143:86740a56073b 12 * integrated circuit in a product or a software update for such product, must reproduce
AnnaBridge 143:86740a56073b 13 * the above copyright notice, this list of conditions and the following disclaimer in
AnnaBridge 143:86740a56073b 14 * the documentation and/or other materials provided with the distribution.
AnnaBridge 143:86740a56073b 15 *
AnnaBridge 143:86740a56073b 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
AnnaBridge 143:86740a56073b 17 * used to endorse or promote products derived from this software without specific prior
AnnaBridge 143:86740a56073b 18 * written permission.
AnnaBridge 143:86740a56073b 19 *
AnnaBridge 143:86740a56073b 20 * 4. This software, with or without modification, must only be used with a
AnnaBridge 143:86740a56073b 21 * Nordic Semiconductor ASA integrated circuit.
AnnaBridge 143:86740a56073b 22 *
AnnaBridge 143:86740a56073b 23 * 5. Any software provided in binary or object form under this license must not be reverse
AnnaBridge 143:86740a56073b 24 * engineered, decompiled, modified and/or disassembled.
AnnaBridge 143:86740a56073b 25 *
AnnaBridge 143:86740a56073b 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
AnnaBridge 143:86740a56073b 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
AnnaBridge 143:86740a56073b 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
AnnaBridge 143:86740a56073b 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
AnnaBridge 143:86740a56073b 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
AnnaBridge 143:86740a56073b 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
AnnaBridge 143:86740a56073b 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
AnnaBridge 143:86740a56073b 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
AnnaBridge 143:86740a56073b 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
AnnaBridge 143:86740a56073b 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
AnnaBridge 143:86740a56073b 36 *
AnnaBridge 143:86740a56073b 37 */
AnnaBridge 143:86740a56073b 38
AnnaBridge 143:86740a56073b 39
AnnaBridge 143:86740a56073b 40
AnnaBridge 143:86740a56073b 41 /**@file
AnnaBridge 143:86740a56073b 42 *
AnnaBridge 143:86740a56073b 43 * @defgroup ble_sdk_lib_advertising Advertising Module
AnnaBridge 143:86740a56073b 44 * @{
AnnaBridge 143:86740a56073b 45 * @ingroup ble_sdk_lib
AnnaBridge 143:86740a56073b 46 * @brief Module for handling connectable BLE advertising.
AnnaBridge 143:86740a56073b 47 *
AnnaBridge 143:86740a56073b 48 * @details The Advertising Module handles connectable advertising for your application. It can
AnnaBridge 143:86740a56073b 49 * be configured with advertising modes to suit most typical use cases.
AnnaBridge 143:86740a56073b 50 * Your main application can react to changes in advertising modes
AnnaBridge 143:86740a56073b 51 * if an event handler is provided.
AnnaBridge 143:86740a56073b 52 *
AnnaBridge 143:86740a56073b 53 * @note The Advertising Module supports only applications with a single peripheral link.
AnnaBridge 143:86740a56073b 54 *
AnnaBridge 143:86740a56073b 55 * The application must propagate BLE stack events to this module by calling
AnnaBridge 143:86740a56073b 56 * @ref ble_advertising_on_ble_evt() and system events by calling
AnnaBridge 143:86740a56073b 57 * @ref ble_advertising_on_sys_evt().
AnnaBridge 143:86740a56073b 58 *
AnnaBridge 143:86740a56073b 59 */
AnnaBridge 143:86740a56073b 60
AnnaBridge 143:86740a56073b 61 #ifndef BLE_ADVERTISING_H__
AnnaBridge 143:86740a56073b 62 #define BLE_ADVERTISING_H__
AnnaBridge 143:86740a56073b 63
AnnaBridge 143:86740a56073b 64 #include <stdint.h>
AnnaBridge 143:86740a56073b 65 #include "nrf_ble_gattc.h"
AnnaBridge 143:86740a56073b 66 #include "nrf_ble.h"
AnnaBridge 143:86740a56073b 67 #include "nrf_error.h"
AnnaBridge 143:86740a56073b 68 #include "ble_advdata.h"
AnnaBridge 143:86740a56073b 69
AnnaBridge 143:86740a56073b 70 /**@brief Advertising modes.
AnnaBridge 143:86740a56073b 71 */
AnnaBridge 143:86740a56073b 72 typedef enum
AnnaBridge 143:86740a56073b 73 {
AnnaBridge 143:86740a56073b 74 BLE_ADV_MODE_IDLE, /**< Idle; no connectable advertising is ongoing.*/
AnnaBridge 143:86740a56073b 75 BLE_ADV_MODE_DIRECTED, /**< Directed advertising attempts to connect to the most recently disconnected peer. */
AnnaBridge 143:86740a56073b 76 BLE_ADV_MODE_DIRECTED_SLOW, /**< Directed advertising (low duty cycle) attempts to connect to the most recently disconnected peer. */
AnnaBridge 143:86740a56073b 77 BLE_ADV_MODE_FAST, /**< Fast advertising will connect to any peer device, or filter with a whitelist if one exists. */
AnnaBridge 143:86740a56073b 78 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. */
AnnaBridge 143:86740a56073b 79 } ble_adv_mode_t;
AnnaBridge 143:86740a56073b 80
AnnaBridge 143:86740a56073b 81 /**@brief Advertising events.
AnnaBridge 143:86740a56073b 82 *
AnnaBridge 143:86740a56073b 83 * @details These events are propagated to the main application if a handler was provided during
AnnaBridge 143:86740a56073b 84 * initialization of the Advertising Module. Events for modes that are not used can be
AnnaBridge 143:86740a56073b 85 * ignored. Similarly, BLE_ADV_EVT_WHITELIST_REQUEST and BLE_ADV_EVT_PEER_ADDR_REQUEST
AnnaBridge 143:86740a56073b 86 * can be ignored if whitelist and direct advertising is not used.
AnnaBridge 143:86740a56073b 87 */
AnnaBridge 143:86740a56073b 88 typedef enum
AnnaBridge 143:86740a56073b 89 {
AnnaBridge 143:86740a56073b 90 BLE_ADV_EVT_IDLE, /**< Idle; no connectable advertising is ongoing.*/
AnnaBridge 143:86740a56073b 91 BLE_ADV_EVT_DIRECTED, /**< Direct advertising mode has started. */
AnnaBridge 143:86740a56073b 92 BLE_ADV_EVT_DIRECTED_SLOW, /**< Directed advertising (low duty cycle) has started. */
AnnaBridge 143:86740a56073b 93 BLE_ADV_EVT_FAST, /**< Fast advertising mode has started. */
AnnaBridge 143:86740a56073b 94 BLE_ADV_EVT_SLOW, /**< Slow advertising mode has started.*/
AnnaBridge 143:86740a56073b 95 BLE_ADV_EVT_FAST_WHITELIST, /**< Fast advertising mode using the whitelist has started. */
AnnaBridge 143:86740a56073b 96 BLE_ADV_EVT_SLOW_WHITELIST, /**< Slow advertising mode using the whitelist has started.*/
AnnaBridge 143:86740a56073b 97 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. */
AnnaBridge 143:86740a56073b 98 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. */
AnnaBridge 143:86740a56073b 99 } ble_adv_evt_t;
AnnaBridge 143:86740a56073b 100
AnnaBridge 143:86740a56073b 101 /**@brief Options for the different advertisement modes.
AnnaBridge 143:86740a56073b 102 *
AnnaBridge 143:86740a56073b 103 * @details This structure is used to enable or disable advertising modes and to configure time-out
AnnaBridge 143:86740a56073b 104 * periods and advertising intervals.
AnnaBridge 143:86740a56073b 105 */
AnnaBridge 143:86740a56073b 106 typedef struct
AnnaBridge 143:86740a56073b 107 {
AnnaBridge 143:86740a56073b 108 bool ble_adv_whitelist_enabled; /**< Enable or disable use of the whitelist. */
AnnaBridge 143:86740a56073b 109 bool ble_adv_directed_enabled; /**< Enable or disable direct advertising mode. */
AnnaBridge 143:86740a56073b 110 bool ble_adv_directed_slow_enabled; /**< Enable or disable direct advertising mode. */
AnnaBridge 143:86740a56073b 111 uint32_t ble_adv_directed_slow_interval; /**< Advertising interval for directed advertising. */
AnnaBridge 143:86740a56073b 112 uint32_t ble_adv_directed_slow_timeout; /**< Time-out (number of tries) for direct advertising. */
AnnaBridge 143:86740a56073b 113 bool ble_adv_fast_enabled; /**< Enable or disable fast advertising mode. */
AnnaBridge 143:86740a56073b 114 uint32_t ble_adv_fast_interval; /**< Advertising interval for fast advertising. */
AnnaBridge 143:86740a56073b 115 uint32_t ble_adv_fast_timeout; /**< Time-out (in seconds) for fast advertising. */
AnnaBridge 143:86740a56073b 116 bool ble_adv_slow_enabled; /**< Enable or disable slow advertising mode. */
AnnaBridge 143:86740a56073b 117 uint32_t ble_adv_slow_interval; /**< Advertising interval for slow advertising. */
AnnaBridge 143:86740a56073b 118 uint32_t ble_adv_slow_timeout; /**< Time-out (in seconds) for slow advertising. */
AnnaBridge 143:86740a56073b 119 }ble_adv_modes_config_t;
AnnaBridge 143:86740a56073b 120
AnnaBridge 143:86740a56073b 121
AnnaBridge 143:86740a56073b 122 /**@brief BLE advertising event handler type. */
AnnaBridge 143:86740a56073b 123 typedef void (*ble_advertising_evt_handler_t) (ble_adv_evt_t const adv_evt);
AnnaBridge 143:86740a56073b 124
AnnaBridge 143:86740a56073b 125 /**@brief BLE advertising error handler type. */
AnnaBridge 143:86740a56073b 126 typedef void (*ble_advertising_error_handler_t) (uint32_t nrf_error);
AnnaBridge 143:86740a56073b 127
AnnaBridge 143:86740a56073b 128 /**@brief Initialization parameters for the Advertising Module.
AnnaBridge 143:86740a56073b 129 * @details This structure is used to pass advertising options, advertising data, and an event handler to the Advertising Module during initialization. */
AnnaBridge 143:86740a56073b 130 typedef struct
AnnaBridge 143:86740a56073b 131 {
AnnaBridge 143:86740a56073b 132 ble_adv_modes_config_t options; /**< Parameters for advertising modes.*/
AnnaBridge 143:86740a56073b 133 ble_advdata_t advdata; /**< Advertising data. */
AnnaBridge 143:86740a56073b 134 ble_advertising_evt_handler_t evt_handler; /**< Event handler. */
AnnaBridge 143:86740a56073b 135 }ble_adv_init_t;
AnnaBridge 143:86740a56073b 136
AnnaBridge 143:86740a56073b 137
AnnaBridge 143:86740a56073b 138 /* Defines to make the mode options easier to set during advertising init.*/
AnnaBridge 143:86740a56073b 139 #define BLE_ADV_DIRECTED_ENABLED true
AnnaBridge 143:86740a56073b 140 #define BLE_ADV_DIRECTED_DISABLED false
AnnaBridge 143:86740a56073b 141
AnnaBridge 143:86740a56073b 142 #define BLE_ADV_DIRECTED_SLOW_ENABLED true
AnnaBridge 143:86740a56073b 143 #define BLE_ADV_DIRECTED_SLOW_DISABLED false
AnnaBridge 143:86740a56073b 144
AnnaBridge 143:86740a56073b 145 #define BLE_ADV_FAST_ENABLED true
AnnaBridge 143:86740a56073b 146 #define BLE_ADV_FAST_DISABLED false
AnnaBridge 143:86740a56073b 147
AnnaBridge 143:86740a56073b 148 #define BLE_ADV_SLOW_ENABLED true
AnnaBridge 143:86740a56073b 149 #define BLE_ADV_SLOW_DISABLED false
AnnaBridge 143:86740a56073b 150
AnnaBridge 143:86740a56073b 151 #define BLE_ADV_WHITELIST_ENABLED true
AnnaBridge 143:86740a56073b 152 #define BLE_ADV_WHITELIST_DISABLED false
AnnaBridge 143:86740a56073b 153
AnnaBridge 143:86740a56073b 154
AnnaBridge 143:86740a56073b 155 /**@brief Function for handling BLE events.
AnnaBridge 143:86740a56073b 156 *
AnnaBridge 143:86740a56073b 157 * @details This function must be called from the BLE stack event dispatcher for
AnnaBridge 143:86740a56073b 158 * the module to handle BLE events that are relevant for the Advertising Module.
AnnaBridge 143:86740a56073b 159 *
AnnaBridge 143:86740a56073b 160 * @param[in] p_ble_evt BLE stack event.
AnnaBridge 143:86740a56073b 161 */
AnnaBridge 143:86740a56073b 162 void ble_advertising_on_ble_evt(const ble_evt_t * const p_ble_evt);
AnnaBridge 143:86740a56073b 163
AnnaBridge 143:86740a56073b 164
AnnaBridge 143:86740a56073b 165 /**@brief Function for handling system events.
AnnaBridge 143:86740a56073b 166 *
AnnaBridge 143:86740a56073b 167 * @details This function must be called to handle system events that are relevant
AnnaBridge 143:86740a56073b 168 * for the Advertising Module. Specifically, the advertising module can not use the
AnnaBridge 143:86740a56073b 169 * softdevice as long as there are pending writes to the flash memory. This
AnnaBridge 143:86740a56073b 170 * event handler is designed to delay advertising until there is no flash operation.
AnnaBridge 143:86740a56073b 171 *
AnnaBridge 143:86740a56073b 172 * @param[in] sys_evt System event.
AnnaBridge 143:86740a56073b 173 */
AnnaBridge 143:86740a56073b 174 void ble_advertising_on_sys_evt(uint32_t sys_evt);
AnnaBridge 143:86740a56073b 175
AnnaBridge 143:86740a56073b 176
AnnaBridge 143:86740a56073b 177 /**@brief Function for initializing the Advertising Module.
AnnaBridge 143:86740a56073b 178 *
AnnaBridge 143:86740a56073b 179 * @details Encodes the required advertising data and passes it to the stack.
AnnaBridge 143:86740a56073b 180 * Also builds a structure to be passed to the stack when starting advertising.
AnnaBridge 143:86740a56073b 181 * The supplied advertising data is copied to a local structure and is manipulated
AnnaBridge 143:86740a56073b 182 * depending on what advertising modes are started in @ref ble_advertising_start.
AnnaBridge 143:86740a56073b 183 *
AnnaBridge 143:86740a56073b 184 * @param[in] p_advdata Advertising data: name, appearance, discovery flags, and more.
AnnaBridge 143:86740a56073b 185 * @param[in] p_srdata Scan response data: Supplement to advertising data.
AnnaBridge 143:86740a56073b 186 * @param[in] p_config Select which advertising modes and intervals will be utilized.
AnnaBridge 143:86740a56073b 187 * @param[in] evt_handler Event handler that will be called upon advertising events.
AnnaBridge 143:86740a56073b 188 * @param[in] error_handler Error handler that will propogate internal errors to the main applications.
AnnaBridge 143:86740a56073b 189 *
AnnaBridge 143:86740a56073b 190 * @retval NRF_SUCCESS If initialization was successful. Otherwise, an error code is returned.
AnnaBridge 143:86740a56073b 191 */
AnnaBridge 143:86740a56073b 192 uint32_t ble_advertising_init(ble_advdata_t const * p_advdata,
AnnaBridge 143:86740a56073b 193 ble_advdata_t const * p_srdata,
AnnaBridge 143:86740a56073b 194 ble_adv_modes_config_t const * p_config,
AnnaBridge 143:86740a56073b 195 ble_advertising_evt_handler_t const evt_handler,
AnnaBridge 143:86740a56073b 196 ble_advertising_error_handler_t const error_handler);
AnnaBridge 143:86740a56073b 197
AnnaBridge 143:86740a56073b 198
AnnaBridge 143:86740a56073b 199 /**@brief Function for starting advertising.
AnnaBridge 143:86740a56073b 200 *
AnnaBridge 143:86740a56073b 201 * @details You can start advertising in any of the advertising modes that you enabled
AnnaBridge 143:86740a56073b 202 * during initialization.
AnnaBridge 143:86740a56073b 203 *
AnnaBridge 143:86740a56073b 204 * @param[in] advertising_mode Advertising mode.
AnnaBridge 143:86740a56073b 205 *
AnnaBridge 143:86740a56073b 206 * @retval @ref NRF_SUCCESS On success, else an error code indicating reason for failure.
AnnaBridge 143:86740a56073b 207 * @retval @ref NRF_ERROR_INVALID_STATE
AnnaBridge 143:86740a56073b 208 */
AnnaBridge 143:86740a56073b 209 uint32_t ble_advertising_start(ble_adv_mode_t advertising_mode);
AnnaBridge 143:86740a56073b 210
AnnaBridge 143:86740a56073b 211 /**@brief Function for setting the peer address.
AnnaBridge 143:86740a56073b 212 *
AnnaBridge 143:86740a56073b 213 * @details The peer address must be set by the application upon receiving a
AnnaBridge 143:86740a56073b 214 * @ref BLE_ADV_EVT_PEER_ADDR_REQUEST event. Without the peer address, the directed
AnnaBridge 143:86740a56073b 215 * advertising mode will not be run.
AnnaBridge 143:86740a56073b 216 *
AnnaBridge 143:86740a56073b 217 * @param[in] p_peer_addr Pointer to a peer address.
AnnaBridge 143:86740a56073b 218 *
AnnaBridge 143:86740a56073b 219 * @retval @ref NRF_SUCCESS Successfully stored the peer address pointer in the advertising module.
AnnaBridge 143:86740a56073b 220 * @retval @ref NRF_ERROR_INVALID_STATE If a reply was not expected.
AnnaBridge 143:86740a56073b 221 */
AnnaBridge 143:86740a56073b 222 uint32_t ble_advertising_peer_addr_reply(ble_gap_addr_t * p_peer_addr);
AnnaBridge 143:86740a56073b 223
AnnaBridge 143:86740a56073b 224
AnnaBridge 143:86740a56073b 225 /**@brief Function for setting a whitelist.
AnnaBridge 143:86740a56073b 226 *
AnnaBridge 143:86740a56073b 227 * @details The whitelist must be set by the application upon receiving a
AnnaBridge 143:86740a56073b 228 * @ref BLE_ADV_EVT_WHITELIST_REQUEST event. Without the whitelist, the whitelist
AnnaBridge 143:86740a56073b 229 * advertising for fast and slow modes will not be run.
AnnaBridge 143:86740a56073b 230 *
AnnaBridge 143:86740a56073b 231 * @param[in] p_whitelist Pointer to a whitelist.
AnnaBridge 143:86740a56073b 232 *
AnnaBridge 143:86740a56073b 233 * @retval @ref NRF_SUCCESS Successfully stored pointers to the whitelist into the advertising module.
AnnaBridge 143:86740a56073b 234 * @retval @ref NRF_ERROR_INVALID_STATE If a reply was not expected.
AnnaBridge 143:86740a56073b 235 */
AnnaBridge 143:86740a56073b 236 uint32_t ble_advertising_whitelist_reply(ble_gap_whitelist_t * p_whitelist);
AnnaBridge 143:86740a56073b 237
AnnaBridge 143:86740a56073b 238
AnnaBridge 143:86740a56073b 239 /**@brief Function for disabling whitelist advertising.
AnnaBridge 143:86740a56073b 240 *
AnnaBridge 143:86740a56073b 241 * @details This function temporarily disables whitelist advertising.
AnnaBridge 143:86740a56073b 242 * Calling this function resets the current time-out countdown.
AnnaBridge 143:86740a56073b 243 *
AnnaBridge 143:86740a56073b 244 * @retval @ref NRF_SUCCESS On success, else an error message propogated from the Softdevice.
AnnaBridge 143:86740a56073b 245 */
AnnaBridge 143:86740a56073b 246 uint32_t ble_advertising_restart_without_whitelist(void);
AnnaBridge 143:86740a56073b 247 /** @} */
AnnaBridge 143:86740a56073b 248
AnnaBridge 143:86740a56073b 249 #endif // BLE_ADVERTISING_H__
AnnaBridge 143:86740a56073b 250
AnnaBridge 143:86740a56073b 251 /** @} */