Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 /* mbed Microcontroller Library
bryantaylor 0:eafc3fd41f75 2 * Copyright (c) 2006-2013 ARM Limited
bryantaylor 0:eafc3fd41f75 3 *
bryantaylor 0:eafc3fd41f75 4 * Licensed under the Apache License, Version 2.0 (the "License");
bryantaylor 0:eafc3fd41f75 5 * you may not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 6 * You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 7 *
bryantaylor 0:eafc3fd41f75 8 * http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 9 *
bryantaylor 0:eafc3fd41f75 10 * Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 11 * distributed under the License is distributed on an "AS IS" BASIS,
bryantaylor 0:eafc3fd41f75 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 13 * See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 14 * limitations under the License.
bryantaylor 0:eafc3fd41f75 15 */
bryantaylor 0:eafc3fd41f75 16
bryantaylor 0:eafc3fd41f75 17 #ifndef __GAP_H__
bryantaylor 0:eafc3fd41f75 18 #define __GAP_H__
bryantaylor 0:eafc3fd41f75 19
bryantaylor 0:eafc3fd41f75 20 #include "ble/BLEProtocol.h"
bryantaylor 0:eafc3fd41f75 21 #include "GapAdvertisingData.h"
bryantaylor 0:eafc3fd41f75 22 #include "GapAdvertisingParams.h"
bryantaylor 0:eafc3fd41f75 23 #include "GapScanningParams.h"
bryantaylor 0:eafc3fd41f75 24 #include "GapEvents.h"
bryantaylor 0:eafc3fd41f75 25 #include "CallChainOfFunctionPointersWithContext.h"
bryantaylor 0:eafc3fd41f75 26 #include "FunctionPointerWithContext.h"
bryantaylor 0:eafc3fd41f75 27 #include "deprecate.h"
bryantaylor 0:eafc3fd41f75 28
bryantaylor 0:eafc3fd41f75 29 /* Forward declarations for classes that will only be used for pointers or references in the following. */
bryantaylor 0:eafc3fd41f75 30 class GapAdvertisingParams;
bryantaylor 0:eafc3fd41f75 31 class GapScanningParams;
bryantaylor 0:eafc3fd41f75 32 class GapAdvertisingData;
bryantaylor 0:eafc3fd41f75 33
bryantaylor 0:eafc3fd41f75 34 class Gap {
bryantaylor 0:eafc3fd41f75 35 /*
bryantaylor 0:eafc3fd41f75 36 * DEPRECATION ALERT: all of the APIs in this `public` block are deprecated.
bryantaylor 0:eafc3fd41f75 37 * They have been relocated to the class BLEProtocol.
bryantaylor 0:eafc3fd41f75 38 */
bryantaylor 0:eafc3fd41f75 39 public:
bryantaylor 0:eafc3fd41f75 40 /**
bryantaylor 0:eafc3fd41f75 41 * Address-type for BLEProtocol addresses.
bryantaylor 0:eafc3fd41f75 42 *
bryantaylor 0:eafc3fd41f75 43 * @deprecated Use BLEProtocol::AddressType_t instead.
bryantaylor 0:eafc3fd41f75 44 */
bryantaylor 0:eafc3fd41f75 45 typedef BLEProtocol::AddressType_t AddressType_t;
bryantaylor 0:eafc3fd41f75 46
bryantaylor 0:eafc3fd41f75 47 /**
bryantaylor 0:eafc3fd41f75 48 * Address-type for BLEProtocol addresses.
bryantaylor 0:eafc3fd41f75 49 *
bryantaylor 0:eafc3fd41f75 50 * @deprecated Use BLEProtocol::AddressType_t instead.
bryantaylor 0:eafc3fd41f75 51 */
bryantaylor 0:eafc3fd41f75 52 typedef BLEProtocol::AddressType_t addr_type_t;
bryantaylor 0:eafc3fd41f75 53
bryantaylor 0:eafc3fd41f75 54 /**
bryantaylor 0:eafc3fd41f75 55 * Address-type for BLEProtocol addresses.
bryantaylor 0:eafc3fd41f75 56 *
bryantaylor 0:eafc3fd41f75 57 * @deprecated Use BLEProtocol::AddressType_t instead. The following
bryantaylor 0:eafc3fd41f75 58 * constants have been left in their deprecated state to
bryantaylor 0:eafc3fd41f75 59 * transparenly support existing applications which may have
bryantaylor 0:eafc3fd41f75 60 * used Gap::ADDR_TYPE_*.
bryantaylor 0:eafc3fd41f75 61 */
bryantaylor 0:eafc3fd41f75 62 enum DeprecatedAddressType_t {
bryantaylor 0:eafc3fd41f75 63 ADDR_TYPE_PUBLIC = BLEProtocol::AddressType::PUBLIC,
bryantaylor 0:eafc3fd41f75 64 ADDR_TYPE_RANDOM_STATIC = BLEProtocol::AddressType::RANDOM_STATIC,
bryantaylor 0:eafc3fd41f75 65 ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE = BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE,
bryantaylor 0:eafc3fd41f75 66 ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE = BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE
bryantaylor 0:eafc3fd41f75 67 };
bryantaylor 0:eafc3fd41f75 68
bryantaylor 0:eafc3fd41f75 69 /**
bryantaylor 0:eafc3fd41f75 70 * Length (in octets) of the BLE MAC address.
bryantaylor 0:eafc3fd41f75 71 */
bryantaylor 0:eafc3fd41f75 72 static const unsigned ADDR_LEN = BLEProtocol::ADDR_LEN;
bryantaylor 0:eafc3fd41f75 73 /**
bryantaylor 0:eafc3fd41f75 74 * 48-bit address, LSB format.
bryantaylor 0:eafc3fd41f75 75 *
bryantaylor 0:eafc3fd41f75 76 * @deprecated Use BLEProtocol::AddressBytes_t instead.
bryantaylor 0:eafc3fd41f75 77 */
bryantaylor 0:eafc3fd41f75 78 typedef BLEProtocol::AddressBytes_t Address_t;
bryantaylor 0:eafc3fd41f75 79 /**
bryantaylor 0:eafc3fd41f75 80 * 48-bit address, LSB format.
bryantaylor 0:eafc3fd41f75 81 *
bryantaylor 0:eafc3fd41f75 82 * @deprecated Use BLEProtocol::AddressBytes_t instead.
bryantaylor 0:eafc3fd41f75 83 */
bryantaylor 0:eafc3fd41f75 84 typedef BLEProtocol::AddressBytes_t address_t;
bryantaylor 0:eafc3fd41f75 85
bryantaylor 0:eafc3fd41f75 86 public:
bryantaylor 0:eafc3fd41f75 87 /**
bryantaylor 0:eafc3fd41f75 88 * Enumeration for timeout sources.
bryantaylor 0:eafc3fd41f75 89 */
bryantaylor 0:eafc3fd41f75 90 enum TimeoutSource_t {
bryantaylor 0:eafc3fd41f75 91 TIMEOUT_SRC_ADVERTISING = 0x00, /**< Advertising timeout. */
bryantaylor 0:eafc3fd41f75 92 TIMEOUT_SRC_SECURITY_REQUEST = 0x01, /**< Security request timeout. */
bryantaylor 0:eafc3fd41f75 93 TIMEOUT_SRC_SCAN = 0x02, /**< Scanning timeout. */
bryantaylor 0:eafc3fd41f75 94 TIMEOUT_SRC_CONN = 0x03, /**< Connection timeout. */
bryantaylor 0:eafc3fd41f75 95 };
bryantaylor 0:eafc3fd41f75 96
bryantaylor 0:eafc3fd41f75 97 /**
bryantaylor 0:eafc3fd41f75 98 * Enumeration for disconnection reasons. The values for these reasons are
bryantaylor 0:eafc3fd41f75 99 * derived from Nordic's implementation, but the reasons are meant to be
bryantaylor 0:eafc3fd41f75 100 * independent of the transport. If you are returned a reason that is not
bryantaylor 0:eafc3fd41f75 101 * covered by this enumeration, please refer to the underlying
bryantaylor 0:eafc3fd41f75 102 * transport library.
bryantaylor 0:eafc3fd41f75 103 */
bryantaylor 0:eafc3fd41f75 104 enum DisconnectionReason_t {
bryantaylor 0:eafc3fd41f75 105 CONNECTION_TIMEOUT = 0x08,
bryantaylor 0:eafc3fd41f75 106 REMOTE_USER_TERMINATED_CONNECTION = 0x13,
bryantaylor 0:eafc3fd41f75 107 REMOTE_DEV_TERMINATION_DUE_TO_LOW_RESOURCES = 0x14, /**< Remote device terminated connection due to low resources.*/
bryantaylor 0:eafc3fd41f75 108 REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF = 0x15, /**< Remote device terminated connection due to power off. */
bryantaylor 0:eafc3fd41f75 109 LOCAL_HOST_TERMINATED_CONNECTION = 0x16,
bryantaylor 0:eafc3fd41f75 110 CONN_INTERVAL_UNACCEPTABLE = 0x3B,
bryantaylor 0:eafc3fd41f75 111 };
bryantaylor 0:eafc3fd41f75 112
bryantaylor 0:eafc3fd41f75 113 /**
bryantaylor 0:eafc3fd41f75 114 * Enumeration for whitelist advertising policy filter modes. The possible
bryantaylor 0:eafc3fd41f75 115 * filter modes were obtained from the Bluetooth Core Specification
bryantaylor 0:eafc3fd41f75 116 * 4.2 (Vol. 6), Part B, Section 4.3.2.
bryantaylor 0:eafc3fd41f75 117 *
bryantaylor 0:eafc3fd41f75 118 * @experimental
bryantaylor 0:eafc3fd41f75 119 */
bryantaylor 0:eafc3fd41f75 120 enum AdvertisingPolicyMode_t {
bryantaylor 0:eafc3fd41f75 121 ADV_POLICY_IGNORE_WHITELIST = 0,
bryantaylor 0:eafc3fd41f75 122 ADV_POLICY_FILTER_SCAN_REQS = 1,
bryantaylor 0:eafc3fd41f75 123 ADV_POLICY_FILTER_CONN_REQS = 2,
bryantaylor 0:eafc3fd41f75 124 ADV_POLICY_FILTER_ALL_REQS = 3,
bryantaylor 0:eafc3fd41f75 125 };
bryantaylor 0:eafc3fd41f75 126
bryantaylor 0:eafc3fd41f75 127 /**
bryantaylor 0:eafc3fd41f75 128 * Enumeration for whitelist scanning policy filter modes. The possible
bryantaylor 0:eafc3fd41f75 129 * filter modes were obtained from the Bluetooth Core Specification
bryantaylor 0:eafc3fd41f75 130 * 4.2 (Vol. 6), Part B, Section 4.3.3.
bryantaylor 0:eafc3fd41f75 131 *
bryantaylor 0:eafc3fd41f75 132 * @experimental
bryantaylor 0:eafc3fd41f75 133 */
bryantaylor 0:eafc3fd41f75 134 enum ScanningPolicyMode_t {
bryantaylor 0:eafc3fd41f75 135 SCAN_POLICY_IGNORE_WHITELIST = 0,
bryantaylor 0:eafc3fd41f75 136 SCAN_POLICY_FILTER_ALL_ADV = 1,
bryantaylor 0:eafc3fd41f75 137 };
bryantaylor 0:eafc3fd41f75 138
bryantaylor 0:eafc3fd41f75 139 /**
bryantaylor 0:eafc3fd41f75 140 * Enumeration for the whitelist initiator policy fiter modes. The possible
bryantaylor 0:eafc3fd41f75 141 * filter modes were obtained from the Bluetooth Core Specification
bryantaylor 0:eafc3fd41f75 142 * 4.2 (vol. 6), Part B, Section 4.4.4.
bryantaylor 0:eafc3fd41f75 143 *
bryantaylor 0:eafc3fd41f75 144 * @experimental
bryantaylor 0:eafc3fd41f75 145 */
bryantaylor 0:eafc3fd41f75 146 enum InitiatorPolicyMode_t {
bryantaylor 0:eafc3fd41f75 147 INIT_POLICY_IGNORE_WHITELIST = 0,
bryantaylor 0:eafc3fd41f75 148 INIT_POLICY_FILTER_ALL_ADV = 1,
bryantaylor 0:eafc3fd41f75 149 };
bryantaylor 0:eafc3fd41f75 150
bryantaylor 0:eafc3fd41f75 151 /**
bryantaylor 0:eafc3fd41f75 152 * Representation of a Bluetooth Low Enery Whitelist containing addresses.
bryantaylor 0:eafc3fd41f75 153 *
bryantaylor 0:eafc3fd41f75 154 * @experimental
bryantaylor 0:eafc3fd41f75 155 */
bryantaylor 0:eafc3fd41f75 156 struct Whitelist_t {
bryantaylor 0:eafc3fd41f75 157 BLEProtocol::Address_t *addresses; /**< List of BLE addresses in the whitelist. */
bryantaylor 0:eafc3fd41f75 158 uint8_t size; /**< Total number of BLE addresses in this whitelist */
bryantaylor 0:eafc3fd41f75 159 uint8_t capacity; /**< Maximum number of BLE addresses that can be added to this whitelist. */
bryantaylor 0:eafc3fd41f75 160 };
bryantaylor 0:eafc3fd41f75 161
bryantaylor 0:eafc3fd41f75 162
bryantaylor 0:eafc3fd41f75 163 /**
bryantaylor 0:eafc3fd41f75 164 * Describes the current state of the device (more than one bit can be set).
bryantaylor 0:eafc3fd41f75 165 */
bryantaylor 0:eafc3fd41f75 166 struct GapState_t {
bryantaylor 0:eafc3fd41f75 167 unsigned advertising : 1; /**< Peripheral is currently advertising. */
bryantaylor 0:eafc3fd41f75 168 unsigned connected : 1; /**< Peripheral is connected to a central. */
bryantaylor 0:eafc3fd41f75 169 };
bryantaylor 0:eafc3fd41f75 170
bryantaylor 0:eafc3fd41f75 171 /**
bryantaylor 0:eafc3fd41f75 172 * Type for connection handle.
bryantaylor 0:eafc3fd41f75 173 */
bryantaylor 0:eafc3fd41f75 174 typedef uint16_t Handle_t;
bryantaylor 0:eafc3fd41f75 175
bryantaylor 0:eafc3fd41f75 176 /**
bryantaylor 0:eafc3fd41f75 177 * Structure containing GAP connection parameters. When in peripheral role
bryantaylor 0:eafc3fd41f75 178 * the connection parameters are suggestions. The choice of the connection
bryantaylor 0:eafc3fd41f75 179 * parameters is eventually up to the central.
bryantaylor 0:eafc3fd41f75 180 */
bryantaylor 0:eafc3fd41f75 181 typedef struct {
bryantaylor 0:eafc3fd41f75 182 uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/
bryantaylor 0:eafc3fd41f75 183 uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/
bryantaylor 0:eafc3fd41f75 184 uint16_t slaveLatency; /**< Slave Latency in number of connection events, see BLE_GAP_CP_LIMITS.*/
bryantaylor 0:eafc3fd41f75 185 uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see BLE_GAP_CP_LIMITS.*/
bryantaylor 0:eafc3fd41f75 186 } ConnectionParams_t;
bryantaylor 0:eafc3fd41f75 187
bryantaylor 0:eafc3fd41f75 188 /**
bryantaylor 0:eafc3fd41f75 189 * Enumeration for the possible GAP roles of a BLE device.
bryantaylor 0:eafc3fd41f75 190 */
bryantaylor 0:eafc3fd41f75 191 enum Role_t {
bryantaylor 0:eafc3fd41f75 192 PERIPHERAL = 0x1, /**< Peripheral Role. */
bryantaylor 0:eafc3fd41f75 193 CENTRAL = 0x2, /**< Central Role. */
bryantaylor 0:eafc3fd41f75 194 };
bryantaylor 0:eafc3fd41f75 195
bryantaylor 0:eafc3fd41f75 196 /**
bryantaylor 0:eafc3fd41f75 197 * Structure containing data and metadata of a scanned advertising packet.
bryantaylor 0:eafc3fd41f75 198 */
bryantaylor 0:eafc3fd41f75 199 struct AdvertisementCallbackParams_t {
bryantaylor 0:eafc3fd41f75 200 BLEProtocol::AddressBytes_t peerAddr; /**< The peer's BLE address. */
bryantaylor 0:eafc3fd41f75 201 int8_t rssi; /**< The advertisement packet RSSI value. */
bryantaylor 0:eafc3fd41f75 202 bool isScanResponse; /**< Whether this packet is the response to a scan request. */
bryantaylor 0:eafc3fd41f75 203 GapAdvertisingParams::AdvertisingType_t type; /**< The type of advertisement. */
bryantaylor 0:eafc3fd41f75 204 uint8_t advertisingDataLen; /**< Length of the advertisement data. */
bryantaylor 0:eafc3fd41f75 205 const uint8_t *advertisingData; /**< Pointer to the advertisement packet's data. */
bryantaylor 0:eafc3fd41f75 206 };
bryantaylor 0:eafc3fd41f75 207
bryantaylor 0:eafc3fd41f75 208 /**
bryantaylor 0:eafc3fd41f75 209 * Type for the handlers of advertisement callback events. Refer to
bryantaylor 0:eafc3fd41f75 210 * Gap::startScan().
bryantaylor 0:eafc3fd41f75 211 */
bryantaylor 0:eafc3fd41f75 212 typedef FunctionPointerWithContext<const AdvertisementCallbackParams_t *> AdvertisementReportCallback_t;
bryantaylor 0:eafc3fd41f75 213
bryantaylor 0:eafc3fd41f75 214 /**
bryantaylor 0:eafc3fd41f75 215 * Encapsulates the parameters of a connection. This information is passed
bryantaylor 0:eafc3fd41f75 216 * to the registered handler of connection events. Refer to Gap::onConnection().
bryantaylor 0:eafc3fd41f75 217 */
bryantaylor 0:eafc3fd41f75 218 struct ConnectionCallbackParams_t {
bryantaylor 0:eafc3fd41f75 219 Handle_t handle; /**< The ID for this connection */
bryantaylor 0:eafc3fd41f75 220 Role_t role; /**< This device's role in the connection */
bryantaylor 0:eafc3fd41f75 221 BLEProtocol::AddressType_t peerAddrType; /**< The peer's BLE address type */
bryantaylor 0:eafc3fd41f75 222 BLEProtocol::AddressBytes_t peerAddr; /**< The peer's BLE address */
bryantaylor 0:eafc3fd41f75 223 BLEProtocol::AddressType_t ownAddrType; /**< This device's BLE address type */
bryantaylor 0:eafc3fd41f75 224 BLEProtocol::AddressBytes_t ownAddr; /**< This devices's BLE address */
bryantaylor 0:eafc3fd41f75 225 const ConnectionParams_t *connectionParams; /**< The currently configured connection parameters */
bryantaylor 0:eafc3fd41f75 226
bryantaylor 0:eafc3fd41f75 227 /**
bryantaylor 0:eafc3fd41f75 228 * Constructor for ConnectionCallbackParams_t.
bryantaylor 0:eafc3fd41f75 229 *
bryantaylor 0:eafc3fd41f75 230 * @param[in] handleIn
bryantaylor 0:eafc3fd41f75 231 * Value for ConnectionCallbackParams_t::handle
bryantaylor 0:eafc3fd41f75 232 * @param[in] roleIn
bryantaylor 0:eafc3fd41f75 233 * Value for ConnectionCallbackParams_t::role
bryantaylor 0:eafc3fd41f75 234 * @param[in] peerAddrTypeIn
bryantaylor 0:eafc3fd41f75 235 * Value for ConnectionCallbackParams_t::peerAddrType
bryantaylor 0:eafc3fd41f75 236 * @param[in] peerAddrIn
bryantaylor 0:eafc3fd41f75 237 * Value for ConnectionCallbackParams_t::peerAddr
bryantaylor 0:eafc3fd41f75 238 * @param[in] ownAddrTypeIn
bryantaylor 0:eafc3fd41f75 239 * Value for ConnectionCallbackParams_t::ownAddrType
bryantaylor 0:eafc3fd41f75 240 * @param[in] ownAddrIn
bryantaylor 0:eafc3fd41f75 241 * Value for ConnectionCallbackParams_t::ownAddr
bryantaylor 0:eafc3fd41f75 242 * @param[in] connectionParamsIn
bryantaylor 0:eafc3fd41f75 243 * Value for ConnectionCallbackParams_t::connectionParams
bryantaylor 0:eafc3fd41f75 244 */
bryantaylor 0:eafc3fd41f75 245 ConnectionCallbackParams_t(Handle_t handleIn,
bryantaylor 0:eafc3fd41f75 246 Role_t roleIn,
bryantaylor 0:eafc3fd41f75 247 BLEProtocol::AddressType_t peerAddrTypeIn,
bryantaylor 0:eafc3fd41f75 248 const uint8_t *peerAddrIn,
bryantaylor 0:eafc3fd41f75 249 BLEProtocol::AddressType_t ownAddrTypeIn,
bryantaylor 0:eafc3fd41f75 250 const uint8_t *ownAddrIn,
bryantaylor 0:eafc3fd41f75 251 const ConnectionParams_t *connectionParamsIn) :
bryantaylor 0:eafc3fd41f75 252 handle(handleIn),
bryantaylor 0:eafc3fd41f75 253 role(roleIn),
bryantaylor 0:eafc3fd41f75 254 peerAddrType(peerAddrTypeIn),
bryantaylor 0:eafc3fd41f75 255 peerAddr(),
bryantaylor 0:eafc3fd41f75 256 ownAddrType(ownAddrTypeIn),
bryantaylor 0:eafc3fd41f75 257 ownAddr(),
bryantaylor 0:eafc3fd41f75 258 connectionParams(connectionParamsIn) {
bryantaylor 0:eafc3fd41f75 259 memcpy(peerAddr, peerAddrIn, ADDR_LEN);
bryantaylor 0:eafc3fd41f75 260 memcpy(ownAddr, ownAddrIn, ADDR_LEN);
bryantaylor 0:eafc3fd41f75 261 }
bryantaylor 0:eafc3fd41f75 262 };
bryantaylor 0:eafc3fd41f75 263
bryantaylor 0:eafc3fd41f75 264 /**
bryantaylor 0:eafc3fd41f75 265 * Structure that encapsulates information about a disconnection event.
bryantaylor 0:eafc3fd41f75 266 * Refer to Gap::onDisconnection().
bryantaylor 0:eafc3fd41f75 267 */
bryantaylor 0:eafc3fd41f75 268 struct DisconnectionCallbackParams_t {
bryantaylor 0:eafc3fd41f75 269 Handle_t handle; /**< The ID of the connection that caused the disconnection event */
bryantaylor 0:eafc3fd41f75 270 DisconnectionReason_t reason; /**< The reason of the disconnection event */
bryantaylor 0:eafc3fd41f75 271
bryantaylor 0:eafc3fd41f75 272 /**
bryantaylor 0:eafc3fd41f75 273 * Constructor for DisconnectionCallbackParams_t.
bryantaylor 0:eafc3fd41f75 274 *
bryantaylor 0:eafc3fd41f75 275 * @param[in] handleIn
bryantaylor 0:eafc3fd41f75 276 * Value for DisconnectionCallbackParams_t::handle.
bryantaylor 0:eafc3fd41f75 277 * @param[in] reasonIn
bryantaylor 0:eafc3fd41f75 278 * Value for DisconnectionCallbackParams_t::reason.
bryantaylor 0:eafc3fd41f75 279 */
bryantaylor 0:eafc3fd41f75 280 DisconnectionCallbackParams_t(Handle_t handleIn,
bryantaylor 0:eafc3fd41f75 281 DisconnectionReason_t reasonIn) :
bryantaylor 0:eafc3fd41f75 282 handle(handleIn),
bryantaylor 0:eafc3fd41f75 283 reason(reasonIn)
bryantaylor 0:eafc3fd41f75 284 {}
bryantaylor 0:eafc3fd41f75 285 };
bryantaylor 0:eafc3fd41f75 286
bryantaylor 0:eafc3fd41f75 287 static const uint16_t UNIT_1_25_MS = 1250; /**< Number of microseconds in 1.25 milliseconds. */
bryantaylor 0:eafc3fd41f75 288 /**
bryantaylor 0:eafc3fd41f75 289 * Helper function to convert from units of milliseconds to GAP duration
bryantaylor 0:eafc3fd41f75 290 * units.
bryantaylor 0:eafc3fd41f75 291 *
bryantaylor 0:eafc3fd41f75 292 * @param[in] durationInMillis
bryantaylor 0:eafc3fd41f75 293 * The duration in milliseconds.
bryantaylor 0:eafc3fd41f75 294 *
bryantaylor 0:eafc3fd41f75 295 * @return The duration in GAP duration units.
bryantaylor 0:eafc3fd41f75 296 */
bryantaylor 0:eafc3fd41f75 297 static uint16_t MSEC_TO_GAP_DURATION_UNITS(uint32_t durationInMillis) {
bryantaylor 0:eafc3fd41f75 298 return (durationInMillis * 1000) / UNIT_1_25_MS;
bryantaylor 0:eafc3fd41f75 299 }
bryantaylor 0:eafc3fd41f75 300
bryantaylor 0:eafc3fd41f75 301 /**
bryantaylor 0:eafc3fd41f75 302 * Type for the registered callbacks added to the timeout event callchain.
bryantaylor 0:eafc3fd41f75 303 * Refer to Gap::onTimeout().
bryantaylor 0:eafc3fd41f75 304 */
bryantaylor 0:eafc3fd41f75 305 typedef FunctionPointerWithContext<TimeoutSource_t> TimeoutEventCallback_t;
bryantaylor 0:eafc3fd41f75 306 /**
bryantaylor 0:eafc3fd41f75 307 * Type for the timeout event callchain. Refer to Gap::onTimeout().
bryantaylor 0:eafc3fd41f75 308 */
bryantaylor 0:eafc3fd41f75 309 typedef CallChainOfFunctionPointersWithContext<TimeoutSource_t> TimeoutEventCallbackChain_t;
bryantaylor 0:eafc3fd41f75 310
bryantaylor 0:eafc3fd41f75 311 /**
bryantaylor 0:eafc3fd41f75 312 * Type for the registered callbacks added to the connection event
bryantaylor 0:eafc3fd41f75 313 * callchain. Refer to Gap::onConnection().
bryantaylor 0:eafc3fd41f75 314 */
bryantaylor 0:eafc3fd41f75 315 typedef FunctionPointerWithContext<const ConnectionCallbackParams_t *> ConnectionEventCallback_t;
bryantaylor 0:eafc3fd41f75 316 /**
bryantaylor 0:eafc3fd41f75 317 * Type for the connection event callchain. Refer to Gap::onConnection().
bryantaylor 0:eafc3fd41f75 318 */
bryantaylor 0:eafc3fd41f75 319 typedef CallChainOfFunctionPointersWithContext<const ConnectionCallbackParams_t *> ConnectionEventCallbackChain_t;
bryantaylor 0:eafc3fd41f75 320
bryantaylor 0:eafc3fd41f75 321 /**
bryantaylor 0:eafc3fd41f75 322 * Type for the registered callbacks added to the disconnection event
bryantaylor 0:eafc3fd41f75 323 * callchain. Refer to Gap::onDisconnetion().
bryantaylor 0:eafc3fd41f75 324 */
bryantaylor 0:eafc3fd41f75 325 typedef FunctionPointerWithContext<const DisconnectionCallbackParams_t*> DisconnectionEventCallback_t;
bryantaylor 0:eafc3fd41f75 326 /**
bryantaylor 0:eafc3fd41f75 327 * Type for the disconnection event callchain. Refer to Gap::onDisconnection().
bryantaylor 0:eafc3fd41f75 328 */
bryantaylor 0:eafc3fd41f75 329 typedef CallChainOfFunctionPointersWithContext<const DisconnectionCallbackParams_t*> DisconnectionEventCallbackChain_t;
bryantaylor 0:eafc3fd41f75 330
bryantaylor 0:eafc3fd41f75 331 /**
bryantaylor 0:eafc3fd41f75 332 * Type for the handlers of radio notification callback events. Refer to
bryantaylor 0:eafc3fd41f75 333 * Gap::onRadioNotification().
bryantaylor 0:eafc3fd41f75 334 */
bryantaylor 0:eafc3fd41f75 335 typedef FunctionPointerWithContext<bool> RadioNotificationEventCallback_t;
bryantaylor 0:eafc3fd41f75 336
bryantaylor 0:eafc3fd41f75 337 /**
bryantaylor 0:eafc3fd41f75 338 * Type for the handlers of shutdown callback events. Refer to
bryantaylor 0:eafc3fd41f75 339 * Gap::onShutdown().
bryantaylor 0:eafc3fd41f75 340 */
bryantaylor 0:eafc3fd41f75 341 typedef FunctionPointerWithContext<const Gap *> GapShutdownCallback_t;
bryantaylor 0:eafc3fd41f75 342 /**
bryantaylor 0:eafc3fd41f75 343 * Type for the shutdown event callchain. Refer to Gap::onShutdown().
bryantaylor 0:eafc3fd41f75 344 */
bryantaylor 0:eafc3fd41f75 345 typedef CallChainOfFunctionPointersWithContext<const Gap *> GapShutdownCallbackChain_t;
bryantaylor 0:eafc3fd41f75 346
bryantaylor 0:eafc3fd41f75 347 /*
bryantaylor 0:eafc3fd41f75 348 * The following functions are meant to be overridden in the platform-specific sub-class.
bryantaylor 0:eafc3fd41f75 349 */
bryantaylor 0:eafc3fd41f75 350 public:
bryantaylor 0:eafc3fd41f75 351 /**
bryantaylor 0:eafc3fd41f75 352 * Set the BTLE MAC address and type. Please note that the address format is
bryantaylor 0:eafc3fd41f75 353 * least significant byte first (LSB). Please refer to BLEProtocol::AddressBytes_t.
bryantaylor 0:eafc3fd41f75 354 *
bryantaylor 0:eafc3fd41f75 355 * @param[in] type
bryantaylor 0:eafc3fd41f75 356 * The type of the BLE address to set.
bryantaylor 0:eafc3fd41f75 357 * @param[in] address
bryantaylor 0:eafc3fd41f75 358 * The BLE address to set.
bryantaylor 0:eafc3fd41f75 359 *
bryantaylor 0:eafc3fd41f75 360 * @return BLE_ERROR_NONE on success.
bryantaylor 0:eafc3fd41f75 361 */
bryantaylor 0:eafc3fd41f75 362 virtual ble_error_t setAddress(BLEProtocol::AddressType_t type, const BLEProtocol::AddressBytes_t address) {
bryantaylor 0:eafc3fd41f75 363 /* avoid compiler warnings about unused variables */
bryantaylor 0:eafc3fd41f75 364 (void)type;
bryantaylor 0:eafc3fd41f75 365 (void)address;
bryantaylor 0:eafc3fd41f75 366
bryantaylor 0:eafc3fd41f75 367 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 368 }
bryantaylor 0:eafc3fd41f75 369
bryantaylor 0:eafc3fd41f75 370 /**
bryantaylor 0:eafc3fd41f75 371 * Fetch the BTLE MAC address and type.
bryantaylor 0:eafc3fd41f75 372 *
bryantaylor 0:eafc3fd41f75 373 * @param[out] typeP
bryantaylor 0:eafc3fd41f75 374 * The current BLE address type.
bryantaylor 0:eafc3fd41f75 375 * @param[out] address
bryantaylor 0:eafc3fd41f75 376 * The current BLE address.
bryantaylor 0:eafc3fd41f75 377 *
bryantaylor 0:eafc3fd41f75 378 * @return BLE_ERROR_NONE on success.
bryantaylor 0:eafc3fd41f75 379 */
bryantaylor 0:eafc3fd41f75 380 virtual ble_error_t getAddress(BLEProtocol::AddressType_t *typeP, BLEProtocol::AddressBytes_t address) {
bryantaylor 0:eafc3fd41f75 381 /* Avoid compiler warnings about unused variables. */
bryantaylor 0:eafc3fd41f75 382 (void)typeP;
bryantaylor 0:eafc3fd41f75 383 (void)address;
bryantaylor 0:eafc3fd41f75 384
bryantaylor 0:eafc3fd41f75 385 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 386 }
bryantaylor 0:eafc3fd41f75 387
bryantaylor 0:eafc3fd41f75 388 /**
bryantaylor 0:eafc3fd41f75 389 * Get the minimum advertising interval in milliseconds for connectable
bryantaylor 0:eafc3fd41f75 390 * undirected and connectable directed event types supported by the
bryantaylor 0:eafc3fd41f75 391 * underlying BLE stack.
bryantaylor 0:eafc3fd41f75 392 *
bryantaylor 0:eafc3fd41f75 393 * @return Minimum Advertising interval in milliseconds for connectable
bryantaylor 0:eafc3fd41f75 394 * undirected and connectable directed event types.
bryantaylor 0:eafc3fd41f75 395 */
bryantaylor 0:eafc3fd41f75 396 virtual uint16_t getMinAdvertisingInterval(void) const {
bryantaylor 0:eafc3fd41f75 397 return 0; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 398 }
bryantaylor 0:eafc3fd41f75 399
bryantaylor 0:eafc3fd41f75 400 /**
bryantaylor 0:eafc3fd41f75 401 * Get the minimum advertising interval in milliseconds for scannable
bryantaylor 0:eafc3fd41f75 402 * undirected and non-connectable undirected even types supported by the
bryantaylor 0:eafc3fd41f75 403 * underlying BLE stack.
bryantaylor 0:eafc3fd41f75 404 *
bryantaylor 0:eafc3fd41f75 405 * @return Minimum Advertising interval in milliseconds for scannable
bryantaylor 0:eafc3fd41f75 406 * undirected and non-connectable undirected event types.
bryantaylor 0:eafc3fd41f75 407 */
bryantaylor 0:eafc3fd41f75 408 virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {
bryantaylor 0:eafc3fd41f75 409 return 0; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 410 }
bryantaylor 0:eafc3fd41f75 411
bryantaylor 0:eafc3fd41f75 412 /**
bryantaylor 0:eafc3fd41f75 413 * Get the maximum advertising interval in milliseconds for all event types
bryantaylor 0:eafc3fd41f75 414 * supported by the underlying BLE stack.
bryantaylor 0:eafc3fd41f75 415 *
bryantaylor 0:eafc3fd41f75 416 * @return Maximum Advertising interval in milliseconds.
bryantaylor 0:eafc3fd41f75 417 */
bryantaylor 0:eafc3fd41f75 418 virtual uint16_t getMaxAdvertisingInterval(void) const {
bryantaylor 0:eafc3fd41f75 419 return 0xFFFF; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 420 }
bryantaylor 0:eafc3fd41f75 421
bryantaylor 0:eafc3fd41f75 422 /**
bryantaylor 0:eafc3fd41f75 423 * Stop advertising. The current advertising parameters remain in effect.
bryantaylor 0:eafc3fd41f75 424 *
bryantaylor 0:eafc3fd41f75 425 * @retval BLE_ERROR_NONE if successfully stopped advertising procedure.
bryantaylor 0:eafc3fd41f75 426 */
bryantaylor 0:eafc3fd41f75 427 virtual ble_error_t stopAdvertising(void) {
bryantaylor 0:eafc3fd41f75 428 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 429 }
bryantaylor 0:eafc3fd41f75 430
bryantaylor 0:eafc3fd41f75 431 /**
bryantaylor 0:eafc3fd41f75 432 * Stop scanning. The current scanning parameters remain in effect.
bryantaylor 0:eafc3fd41f75 433 *
bryantaylor 0:eafc3fd41f75 434 * @retval BLE_ERROR_NONE if successfully stopped scanning procedure.
bryantaylor 0:eafc3fd41f75 435 */
bryantaylor 0:eafc3fd41f75 436 virtual ble_error_t stopScan() {
bryantaylor 0:eafc3fd41f75 437 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 438 }
bryantaylor 0:eafc3fd41f75 439
bryantaylor 0:eafc3fd41f75 440 /**
bryantaylor 0:eafc3fd41f75 441 * Create a connection (GAP Link Establishment).
bryantaylor 0:eafc3fd41f75 442 *
bryantaylor 0:eafc3fd41f75 443 * @param[in] peerAddr
bryantaylor 0:eafc3fd41f75 444 * 48-bit address, LSB format.
bryantaylor 0:eafc3fd41f75 445 * @param[in] peerAddrType
bryantaylor 0:eafc3fd41f75 446 * Address type of the peer.
bryantaylor 0:eafc3fd41f75 447 * @param[in] connectionParams
bryantaylor 0:eafc3fd41f75 448 * Connection parameters.
bryantaylor 0:eafc3fd41f75 449 * @param[in] scanParams
bryantaylor 0:eafc3fd41f75 450 * Paramters to be used while scanning for the peer.
bryantaylor 0:eafc3fd41f75 451 *
bryantaylor 0:eafc3fd41f75 452 * @return BLE_ERROR_NONE if connection establishment procedure is started
bryantaylor 0:eafc3fd41f75 453 * successfully. The connectionCallChain (if set) will be invoked upon
bryantaylor 0:eafc3fd41f75 454 * a connection event.
bryantaylor 0:eafc3fd41f75 455 */
bryantaylor 0:eafc3fd41f75 456 virtual ble_error_t connect(const BLEProtocol::AddressBytes_t peerAddr,
bryantaylor 0:eafc3fd41f75 457 BLEProtocol::AddressType_t peerAddrType,
bryantaylor 0:eafc3fd41f75 458 const ConnectionParams_t *connectionParams,
bryantaylor 0:eafc3fd41f75 459 const GapScanningParams *scanParams) {
bryantaylor 0:eafc3fd41f75 460 /* Avoid compiler warnings about unused variables. */
bryantaylor 0:eafc3fd41f75 461 (void)peerAddr;
bryantaylor 0:eafc3fd41f75 462 (void)peerAddrType;
bryantaylor 0:eafc3fd41f75 463 (void)connectionParams;
bryantaylor 0:eafc3fd41f75 464 (void)scanParams;
bryantaylor 0:eafc3fd41f75 465
bryantaylor 0:eafc3fd41f75 466 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 467 }
bryantaylor 0:eafc3fd41f75 468
bryantaylor 0:eafc3fd41f75 469 /**
bryantaylor 0:eafc3fd41f75 470 * Create a connection (GAP Link Establishment).
bryantaylor 0:eafc3fd41f75 471 *
bryantaylor 0:eafc3fd41f75 472 * @deprecated This funtion overloads Gap::connect(const BLEProtocol::Address_t peerAddr,
bryantaylor 0:eafc3fd41f75 473 * BLEProtocol::AddressType_t peerAddrType,
bryantaylor 0:eafc3fd41f75 474 * const ConnectionParams_t *connectionParams,
bryantaylor 0:eafc3fd41f75 475 * const GapScanningParams *scanParams)
bryantaylor 0:eafc3fd41f75 476 * to maintain backward compatibility for change from Gap::AddressType_t to
bryantaylor 0:eafc3fd41f75 477 * BLEProtocol::AddressType_t.
bryantaylor 0:eafc3fd41f75 478 */
bryantaylor 0:eafc3fd41f75 479 ble_error_t connect(const BLEProtocol::AddressBytes_t peerAddr,
bryantaylor 0:eafc3fd41f75 480 DeprecatedAddressType_t peerAddrType,
bryantaylor 0:eafc3fd41f75 481 const ConnectionParams_t *connectionParams,
bryantaylor 0:eafc3fd41f75 482 const GapScanningParams *scanParams)
bryantaylor 0:eafc3fd41f75 483 __deprecated_message("Gap::DeprecatedAddressType_t is deprecated, use BLEProtocol::AddressType_t instead") {
bryantaylor 0:eafc3fd41f75 484 return connect(peerAddr, (BLEProtocol::AddressType_t) peerAddrType, connectionParams, scanParams);
bryantaylor 0:eafc3fd41f75 485 }
bryantaylor 0:eafc3fd41f75 486
bryantaylor 0:eafc3fd41f75 487 /**
bryantaylor 0:eafc3fd41f75 488 * This call initiates the disconnection procedure, and its completion will
bryantaylor 0:eafc3fd41f75 489 * be communicated to the application with an invocation of the
bryantaylor 0:eafc3fd41f75 490 * disconnectionCallback.
bryantaylor 0:eafc3fd41f75 491 *
bryantaylor 0:eafc3fd41f75 492 * @param[in] reason
bryantaylor 0:eafc3fd41f75 493 * The reason for disconnection; to be sent back to the peer.
bryantaylor 0:eafc3fd41f75 494 * @param[in] connectionHandle
bryantaylor 0:eafc3fd41f75 495 * The handle of the connection to disconnect from.
bryantaylor 0:eafc3fd41f75 496 *
bryantaylor 0:eafc3fd41f75 497 * @return BLE_ERROR_NONE if disconnection was successful.
bryantaylor 0:eafc3fd41f75 498 */
bryantaylor 0:eafc3fd41f75 499 virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason) {
bryantaylor 0:eafc3fd41f75 500 /* avoid compiler warnings about unused variables */
bryantaylor 0:eafc3fd41f75 501 (void)connectionHandle;
bryantaylor 0:eafc3fd41f75 502 (void)reason;
bryantaylor 0:eafc3fd41f75 503
bryantaylor 0:eafc3fd41f75 504 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 505 }
bryantaylor 0:eafc3fd41f75 506
bryantaylor 0:eafc3fd41f75 507 /**
bryantaylor 0:eafc3fd41f75 508 * This call initiates the disconnection procedure, and its completion will
bryantaylor 0:eafc3fd41f75 509 * be communicated to the application with an invocation of the
bryantaylor 0:eafc3fd41f75 510 * disconnectionCallback.
bryantaylor 0:eafc3fd41f75 511 *
bryantaylor 0:eafc3fd41f75 512 * @param[in] reason
bryantaylor 0:eafc3fd41f75 513 * The reason for disconnection; to be sent back to the peer.
bryantaylor 0:eafc3fd41f75 514 *
bryantaylor 0:eafc3fd41f75 515 * @return BLE_ERROR_NONE if disconnection was successful.
bryantaylor 0:eafc3fd41f75 516 *
bryantaylor 0:eafc3fd41f75 517 * @deprecated This version of disconnect() doesn't take a connection handle. It
bryantaylor 0:eafc3fd41f75 518 * works reliably only for stacks that are limited to a single
bryantaylor 0:eafc3fd41f75 519 * connection. Use instead Gap::disconnect(Handle_t connectionHandle,
bryantaylor 0:eafc3fd41f75 520 * DisconnectionReason_t reason) instead.
bryantaylor 0:eafc3fd41f75 521 */
bryantaylor 0:eafc3fd41f75 522 virtual ble_error_t disconnect(DisconnectionReason_t reason) {
bryantaylor 0:eafc3fd41f75 523 /* Avoid compiler warnings about unused variables. */
bryantaylor 0:eafc3fd41f75 524 (void)reason;
bryantaylor 0:eafc3fd41f75 525
bryantaylor 0:eafc3fd41f75 526 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 527 }
bryantaylor 0:eafc3fd41f75 528
bryantaylor 0:eafc3fd41f75 529 /**
bryantaylor 0:eafc3fd41f75 530 * Get the GAP peripheral preferred connection parameters. These are the
bryantaylor 0:eafc3fd41f75 531 * defaults that the peripheral would like to have in a connection. The
bryantaylor 0:eafc3fd41f75 532 * choice of the connection parameters is eventually up to the central.
bryantaylor 0:eafc3fd41f75 533 *
bryantaylor 0:eafc3fd41f75 534 * @param[out] params
bryantaylor 0:eafc3fd41f75 535 * The structure where the parameters will be stored. Memory
bryantaylor 0:eafc3fd41f75 536 * for this is owned by the caller.
bryantaylor 0:eafc3fd41f75 537 *
bryantaylor 0:eafc3fd41f75 538 * @return BLE_ERROR_NONE if the parameters were successfully filled into
bryantaylor 0:eafc3fd41f75 539 * the given structure pointed to by params.
bryantaylor 0:eafc3fd41f75 540 */
bryantaylor 0:eafc3fd41f75 541 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) {
bryantaylor 0:eafc3fd41f75 542 /* Avoid compiler warnings about unused variables. */
bryantaylor 0:eafc3fd41f75 543 (void)params;
bryantaylor 0:eafc3fd41f75 544
bryantaylor 0:eafc3fd41f75 545 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 546 }
bryantaylor 0:eafc3fd41f75 547
bryantaylor 0:eafc3fd41f75 548 /**
bryantaylor 0:eafc3fd41f75 549 * Set the GAP peripheral preferred connection parameters. These are the
bryantaylor 0:eafc3fd41f75 550 * defaults that the peripheral would like to have in a connection. The
bryantaylor 0:eafc3fd41f75 551 * choice of the connection parameters is eventually up to the central.
bryantaylor 0:eafc3fd41f75 552 *
bryantaylor 0:eafc3fd41f75 553 * @param[in] params
bryantaylor 0:eafc3fd41f75 554 * The structure containing the desired parameters.
bryantaylor 0:eafc3fd41f75 555 *
bryantaylor 0:eafc3fd41f75 556 * @return BLE_ERROR_NONE if the preferred connection params were set
bryantaylor 0:eafc3fd41f75 557 * correctly.
bryantaylor 0:eafc3fd41f75 558 */
bryantaylor 0:eafc3fd41f75 559 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) {
bryantaylor 0:eafc3fd41f75 560 /* Avoid compiler warnings about unused variables. */
bryantaylor 0:eafc3fd41f75 561 (void)params;
bryantaylor 0:eafc3fd41f75 562
bryantaylor 0:eafc3fd41f75 563 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 564 }
bryantaylor 0:eafc3fd41f75 565
bryantaylor 0:eafc3fd41f75 566 /**
bryantaylor 0:eafc3fd41f75 567 * Update connection parameters. In the central role this will initiate a
bryantaylor 0:eafc3fd41f75 568 * Link Layer connection parameter update procedure. In the peripheral role,
bryantaylor 0:eafc3fd41f75 569 * this will send the corresponding L2CAP request and wait for the central
bryantaylor 0:eafc3fd41f75 570 * to perform the procedure.
bryantaylor 0:eafc3fd41f75 571 *
bryantaylor 0:eafc3fd41f75 572 * @param[in] handle
bryantaylor 0:eafc3fd41f75 573 * Connection Handle.
bryantaylor 0:eafc3fd41f75 574 * @param[in] params
bryantaylor 0:eafc3fd41f75 575 * Pointer to desired connection parameters. If NULL is provided on a peripheral role,
bryantaylor 0:eafc3fd41f75 576 * the parameters in the PPCP characteristic of the GAP service will be used instead.
bryantaylor 0:eafc3fd41f75 577 *
bryantaylor 0:eafc3fd41f75 578 * @return BLE_ERROR_NONE if the connection parameters were updated correctly.
bryantaylor 0:eafc3fd41f75 579 */
bryantaylor 0:eafc3fd41f75 580 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) {
bryantaylor 0:eafc3fd41f75 581 /* avoid compiler warnings about unused variables */
bryantaylor 0:eafc3fd41f75 582 (void)handle;
bryantaylor 0:eafc3fd41f75 583 (void)params;
bryantaylor 0:eafc3fd41f75 584
bryantaylor 0:eafc3fd41f75 585 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 586 }
bryantaylor 0:eafc3fd41f75 587
bryantaylor 0:eafc3fd41f75 588 /**
bryantaylor 0:eafc3fd41f75 589 * Set the device name characteristic in the GAP service.
bryantaylor 0:eafc3fd41f75 590 *
bryantaylor 0:eafc3fd41f75 591 * @param[in] deviceName
bryantaylor 0:eafc3fd41f75 592 * The new value for the device-name. This is a UTF-8 encoded, <b>NULL-terminated</b> string.
bryantaylor 0:eafc3fd41f75 593 *
bryantaylor 0:eafc3fd41f75 594 * @return BLE_ERROR_NONE if the device name was set correctly.
bryantaylor 0:eafc3fd41f75 595 */
bryantaylor 0:eafc3fd41f75 596 virtual ble_error_t setDeviceName(const uint8_t *deviceName) {
bryantaylor 0:eafc3fd41f75 597 /* Avoid compiler warnings about unused variables. */
bryantaylor 0:eafc3fd41f75 598 (void)deviceName;
bryantaylor 0:eafc3fd41f75 599
bryantaylor 0:eafc3fd41f75 600 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 601 }
bryantaylor 0:eafc3fd41f75 602
bryantaylor 0:eafc3fd41f75 603 /**
bryantaylor 0:eafc3fd41f75 604 * Get the value of the device name characteristic in the GAP service.
bryantaylor 0:eafc3fd41f75 605 *
bryantaylor 0:eafc3fd41f75 606 * @param[out] deviceName
bryantaylor 0:eafc3fd41f75 607 * Pointer to an empty buffer where the UTF-8 *non NULL-
bryantaylor 0:eafc3fd41f75 608 * terminated* string will be placed. Set this
bryantaylor 0:eafc3fd41f75 609 * value to NULL in order to obtain the deviceName-length
bryantaylor 0:eafc3fd41f75 610 * from the 'length' parameter.
bryantaylor 0:eafc3fd41f75 611 *
bryantaylor 0:eafc3fd41f75 612 * @param[in,out] lengthP
bryantaylor 0:eafc3fd41f75 613 * (on input) Length of the buffer pointed to by deviceName;
bryantaylor 0:eafc3fd41f75 614 * (on output) the complete device name length (without the
bryantaylor 0:eafc3fd41f75 615 * null terminator).
bryantaylor 0:eafc3fd41f75 616 *
bryantaylor 0:eafc3fd41f75 617 * @return BLE_ERROR_NONE if the device name was fetched correctly from the
bryantaylor 0:eafc3fd41f75 618 * underlying BLE stack.
bryantaylor 0:eafc3fd41f75 619 *
bryantaylor 0:eafc3fd41f75 620 * @note If the device name is longer than the size of the supplied buffer,
bryantaylor 0:eafc3fd41f75 621 * length will return the complete device name length, and not the
bryantaylor 0:eafc3fd41f75 622 * number of bytes actually returned in deviceName. The application may
bryantaylor 0:eafc3fd41f75 623 * use this information to retry with a suitable buffer size.
bryantaylor 0:eafc3fd41f75 624 */
bryantaylor 0:eafc3fd41f75 625 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) {
bryantaylor 0:eafc3fd41f75 626 /* avoid compiler warnings about unused variables */
bryantaylor 0:eafc3fd41f75 627 (void)deviceName;
bryantaylor 0:eafc3fd41f75 628 (void)lengthP;
bryantaylor 0:eafc3fd41f75 629
bryantaylor 0:eafc3fd41f75 630 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 631 }
bryantaylor 0:eafc3fd41f75 632
bryantaylor 0:eafc3fd41f75 633 /**
bryantaylor 0:eafc3fd41f75 634 * Set the appearance characteristic in the GAP service.
bryantaylor 0:eafc3fd41f75 635 *
bryantaylor 0:eafc3fd41f75 636 * @param[in] appearance
bryantaylor 0:eafc3fd41f75 637 * The new value for the device-appearance.
bryantaylor 0:eafc3fd41f75 638 *
bryantaylor 0:eafc3fd41f75 639 * @return BLE_ERROR_NONE if the new appearance was set correctly.
bryantaylor 0:eafc3fd41f75 640 */
bryantaylor 0:eafc3fd41f75 641 virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance) {
bryantaylor 0:eafc3fd41f75 642 /* Avoid compiler warnings about unused variables. */
bryantaylor 0:eafc3fd41f75 643 (void)appearance;
bryantaylor 0:eafc3fd41f75 644
bryantaylor 0:eafc3fd41f75 645 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 646 }
bryantaylor 0:eafc3fd41f75 647
bryantaylor 0:eafc3fd41f75 648 /**
bryantaylor 0:eafc3fd41f75 649 * Get the appearance characteristic in the GAP service.
bryantaylor 0:eafc3fd41f75 650 *
bryantaylor 0:eafc3fd41f75 651 * @param[out] appearance
bryantaylor 0:eafc3fd41f75 652 * The current device-appearance value.
bryantaylor 0:eafc3fd41f75 653 *
bryantaylor 0:eafc3fd41f75 654 * @return BLE_ERROR_NONE if the device-appearance was fetched correctly
bryantaylor 0:eafc3fd41f75 655 * from the underlying BLE stack.
bryantaylor 0:eafc3fd41f75 656 */
bryantaylor 0:eafc3fd41f75 657 virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP) {
bryantaylor 0:eafc3fd41f75 658 /* Avoid compiler warnings about unused variables. */
bryantaylor 0:eafc3fd41f75 659 (void)appearanceP;
bryantaylor 0:eafc3fd41f75 660
bryantaylor 0:eafc3fd41f75 661 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 662 }
bryantaylor 0:eafc3fd41f75 663
bryantaylor 0:eafc3fd41f75 664 /**
bryantaylor 0:eafc3fd41f75 665 * Set the radio's transmit power.
bryantaylor 0:eafc3fd41f75 666 *
bryantaylor 0:eafc3fd41f75 667 * @param[in] txPower
bryantaylor 0:eafc3fd41f75 668 * Radio's transmit power in dBm.
bryantaylor 0:eafc3fd41f75 669 *
bryantaylor 0:eafc3fd41f75 670 * @return BLE_ERROR_NONE if the new radio's transmit power was set
bryantaylor 0:eafc3fd41f75 671 * correctly.
bryantaylor 0:eafc3fd41f75 672 */
bryantaylor 0:eafc3fd41f75 673 virtual ble_error_t setTxPower(int8_t txPower) {
bryantaylor 0:eafc3fd41f75 674 /* Avoid compiler warnings about unused variables. */
bryantaylor 0:eafc3fd41f75 675 (void)txPower;
bryantaylor 0:eafc3fd41f75 676
bryantaylor 0:eafc3fd41f75 677 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 678 }
bryantaylor 0:eafc3fd41f75 679
bryantaylor 0:eafc3fd41f75 680 /**
bryantaylor 0:eafc3fd41f75 681 * Query the underlying stack for permitted arguments for setTxPower().
bryantaylor 0:eafc3fd41f75 682 *
bryantaylor 0:eafc3fd41f75 683 * @param[out] valueArrayPP
bryantaylor 0:eafc3fd41f75 684 * Out parameter to receive the immutable array of Tx values.
bryantaylor 0:eafc3fd41f75 685 * @param[out] countP
bryantaylor 0:eafc3fd41f75 686 * Out parameter to receive the array's size.
bryantaylor 0:eafc3fd41f75 687 */
bryantaylor 0:eafc3fd41f75 688 virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP) {
bryantaylor 0:eafc3fd41f75 689 /* Avoid compiler warnings about unused variables. */
bryantaylor 0:eafc3fd41f75 690 (void)valueArrayPP;
bryantaylor 0:eafc3fd41f75 691 (void)countP;
bryantaylor 0:eafc3fd41f75 692
bryantaylor 0:eafc3fd41f75 693 *countP = 0; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 694 }
bryantaylor 0:eafc3fd41f75 695
bryantaylor 0:eafc3fd41f75 696 /**
bryantaylor 0:eafc3fd41f75 697 * Get the maximum size of the whitelist.
bryantaylor 0:eafc3fd41f75 698 *
bryantaylor 0:eafc3fd41f75 699 * @return Maximum size of the whitelist.
bryantaylor 0:eafc3fd41f75 700 *
bryantaylor 0:eafc3fd41f75 701 * @note If using mbed OS the size of the whitelist can be configured by
bryantaylor 0:eafc3fd41f75 702 * setting the YOTTA_CFG_WHITELIST_MAX_SIZE macro in your yotta
bryantaylor 0:eafc3fd41f75 703 * config file.
bryantaylor 0:eafc3fd41f75 704 *
bryantaylor 0:eafc3fd41f75 705 * @experimental
bryantaylor 0:eafc3fd41f75 706 */
bryantaylor 0:eafc3fd41f75 707 virtual uint8_t getMaxWhitelistSize(void) const
bryantaylor 0:eafc3fd41f75 708 {
bryantaylor 0:eafc3fd41f75 709 return 0;
bryantaylor 0:eafc3fd41f75 710 }
bryantaylor 0:eafc3fd41f75 711
bryantaylor 0:eafc3fd41f75 712 /**
bryantaylor 0:eafc3fd41f75 713 * Get the internal whitelist to be used by the Link Layer when scanning,
bryantaylor 0:eafc3fd41f75 714 * advertising or initiating a connection depending on the filter policies.
bryantaylor 0:eafc3fd41f75 715 *
bryantaylor 0:eafc3fd41f75 716 * @param[in,out] whitelist
bryantaylor 0:eafc3fd41f75 717 * (on input) whitelist.capacity contains the maximum number
bryantaylor 0:eafc3fd41f75 718 * of addresses to be returned.
bryantaylor 0:eafc3fd41f75 719 * (on output) The populated whitelist with copies of the
bryantaylor 0:eafc3fd41f75 720 * addresses in the implementation's whitelist.
bryantaylor 0:eafc3fd41f75 721 *
bryantaylor 0:eafc3fd41f75 722 * @return BLE_ERROR_NONE if the implementation's whitelist was successfully
bryantaylor 0:eafc3fd41f75 723 * copied into the supplied reference.
bryantaylor 0:eafc3fd41f75 724 *
bryantaylor 0:eafc3fd41f75 725 * @experimental
bryantaylor 0:eafc3fd41f75 726 */
bryantaylor 0:eafc3fd41f75 727 virtual ble_error_t getWhitelist(Whitelist_t &whitelist) const
bryantaylor 0:eafc3fd41f75 728 {
bryantaylor 0:eafc3fd41f75 729 (void) whitelist;
bryantaylor 0:eafc3fd41f75 730 return BLE_ERROR_NOT_IMPLEMENTED;
bryantaylor 0:eafc3fd41f75 731 }
bryantaylor 0:eafc3fd41f75 732
bryantaylor 0:eafc3fd41f75 733 /**
bryantaylor 0:eafc3fd41f75 734 * Set the internal whitelist to be used by the Link Layer when scanning,
bryantaylor 0:eafc3fd41f75 735 * advertising or initiating a connection depending on the filter policies.
bryantaylor 0:eafc3fd41f75 736 *
bryantaylor 0:eafc3fd41f75 737 * @param[in] whitelist
bryantaylor 0:eafc3fd41f75 738 * A reference to a whitelist containing the addresses to
bryantaylor 0:eafc3fd41f75 739 * be added to the internal whitelist.
bryantaylor 0:eafc3fd41f75 740 *
bryantaylor 0:eafc3fd41f75 741 * @return BLE_ERROR_NONE if the implementation's whitelist was successfully
bryantaylor 0:eafc3fd41f75 742 * populated with the addresses in the given whitelist.
bryantaylor 0:eafc3fd41f75 743 *
bryantaylor 0:eafc3fd41f75 744 * @note The whitelist must not contain addresses of type @ref
bryantaylor 0:eafc3fd41f75 745 * BLEProtocol::AddressType_t::RANDOM_PRIVATE_NON_RESOLVABLE, this
bryantaylor 0:eafc3fd41f75 746 * this will result in a @ref BLE_ERROR_INVALID_PARAM since the
bryantaylor 0:eafc3fd41f75 747 * remote peer might change its private address at any time and it
bryantaylor 0:eafc3fd41f75 748 * is not possible to resolve it.
bryantaylor 0:eafc3fd41f75 749 * @note If the input whitelist is larger than @ref getMaxWhitelistSize()
bryantaylor 0:eafc3fd41f75 750 * the @ref BLE_ERROR_PARAM_OUT_OF_RANGE is returned.
bryantaylor 0:eafc3fd41f75 751 *
bryantaylor 0:eafc3fd41f75 752 * @experimental
bryantaylor 0:eafc3fd41f75 753 */
bryantaylor 0:eafc3fd41f75 754 virtual ble_error_t setWhitelist(const Whitelist_t &whitelist)
bryantaylor 0:eafc3fd41f75 755 {
bryantaylor 0:eafc3fd41f75 756 (void) whitelist;
bryantaylor 0:eafc3fd41f75 757 return BLE_ERROR_NOT_IMPLEMENTED;
bryantaylor 0:eafc3fd41f75 758 }
bryantaylor 0:eafc3fd41f75 759
bryantaylor 0:eafc3fd41f75 760 /**
bryantaylor 0:eafc3fd41f75 761 * Set the advertising policy filter mode to be used in the next call
bryantaylor 0:eafc3fd41f75 762 * to startAdvertising().
bryantaylor 0:eafc3fd41f75 763 *
bryantaylor 0:eafc3fd41f75 764 * @param[in] mode
bryantaylor 0:eafc3fd41f75 765 * The new advertising policy filter mode.
bryantaylor 0:eafc3fd41f75 766 *
bryantaylor 0:eafc3fd41f75 767 * @return BLE_ERROR_NONE if the specified policy filter mode was set
bryantaylor 0:eafc3fd41f75 768 * successfully.
bryantaylor 0:eafc3fd41f75 769 *
bryantaylor 0:eafc3fd41f75 770 * @experimental
bryantaylor 0:eafc3fd41f75 771 */
bryantaylor 0:eafc3fd41f75 772 virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode)
bryantaylor 0:eafc3fd41f75 773 {
bryantaylor 0:eafc3fd41f75 774 (void) mode;
bryantaylor 0:eafc3fd41f75 775 return BLE_ERROR_NOT_IMPLEMENTED;
bryantaylor 0:eafc3fd41f75 776 }
bryantaylor 0:eafc3fd41f75 777
bryantaylor 0:eafc3fd41f75 778 /**
bryantaylor 0:eafc3fd41f75 779 * Set the scan policy filter mode to be used in the next call
bryantaylor 0:eafc3fd41f75 780 * to startScan().
bryantaylor 0:eafc3fd41f75 781 *
bryantaylor 0:eafc3fd41f75 782 * @param[in] mode
bryantaylor 0:eafc3fd41f75 783 * The new scan policy filter mode.
bryantaylor 0:eafc3fd41f75 784 *
bryantaylor 0:eafc3fd41f75 785 * @return BLE_ERROR_NONE if the specified policy filter mode was set
bryantaylor 0:eafc3fd41f75 786 * successfully.
bryantaylor 0:eafc3fd41f75 787 *
bryantaylor 0:eafc3fd41f75 788 * @experimental
bryantaylor 0:eafc3fd41f75 789 */
bryantaylor 0:eafc3fd41f75 790 virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode)
bryantaylor 0:eafc3fd41f75 791 {
bryantaylor 0:eafc3fd41f75 792 (void) mode;
bryantaylor 0:eafc3fd41f75 793 return BLE_ERROR_NOT_IMPLEMENTED;
bryantaylor 0:eafc3fd41f75 794 }
bryantaylor 0:eafc3fd41f75 795
bryantaylor 0:eafc3fd41f75 796 /**
bryantaylor 0:eafc3fd41f75 797 * Set the initiator policy filter mode to be used.
bryantaylor 0:eafc3fd41f75 798 *
bryantaylor 0:eafc3fd41f75 799 * @param[in] mode
bryantaylor 0:eafc3fd41f75 800 * The new initiator policy filter mode.
bryantaylor 0:eafc3fd41f75 801 *
bryantaylor 0:eafc3fd41f75 802 * @return BLE_ERROR_NONE if the specified policy filter mode was set
bryantaylor 0:eafc3fd41f75 803 * successfully.
bryantaylor 0:eafc3fd41f75 804 *
bryantaylor 0:eafc3fd41f75 805 * @experimental
bryantaylor 0:eafc3fd41f75 806 */
bryantaylor 0:eafc3fd41f75 807 virtual ble_error_t setInitiatorPolicyMode(InitiatorPolicyMode_t mode)
bryantaylor 0:eafc3fd41f75 808 {
bryantaylor 0:eafc3fd41f75 809 (void) mode;
bryantaylor 0:eafc3fd41f75 810 return BLE_ERROR_NOT_IMPLEMENTED;
bryantaylor 0:eafc3fd41f75 811 }
bryantaylor 0:eafc3fd41f75 812
bryantaylor 0:eafc3fd41f75 813 /**
bryantaylor 0:eafc3fd41f75 814 * Get the advertising policy filter mode that will be used in the next
bryantaylor 0:eafc3fd41f75 815 * call to startAdvertising().
bryantaylor 0:eafc3fd41f75 816 *
bryantaylor 0:eafc3fd41f75 817 * @return The set advertising policy filter mode.
bryantaylor 0:eafc3fd41f75 818 *
bryantaylor 0:eafc3fd41f75 819 * @experimental
bryantaylor 0:eafc3fd41f75 820 */
bryantaylor 0:eafc3fd41f75 821 virtual AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const
bryantaylor 0:eafc3fd41f75 822 {
bryantaylor 0:eafc3fd41f75 823 return ADV_POLICY_IGNORE_WHITELIST;
bryantaylor 0:eafc3fd41f75 824 }
bryantaylor 0:eafc3fd41f75 825
bryantaylor 0:eafc3fd41f75 826 /**
bryantaylor 0:eafc3fd41f75 827 * Get the scan policy filter mode that will be used in the next
bryantaylor 0:eafc3fd41f75 828 * call to startScan().
bryantaylor 0:eafc3fd41f75 829 *
bryantaylor 0:eafc3fd41f75 830 * @return The set scan policy filter mode.
bryantaylor 0:eafc3fd41f75 831 *
bryantaylor 0:eafc3fd41f75 832 * @experimental
bryantaylor 0:eafc3fd41f75 833 */
bryantaylor 0:eafc3fd41f75 834 virtual ScanningPolicyMode_t getScanningPolicyMode(void) const
bryantaylor 0:eafc3fd41f75 835 {
bryantaylor 0:eafc3fd41f75 836 return SCAN_POLICY_IGNORE_WHITELIST;
bryantaylor 0:eafc3fd41f75 837 }
bryantaylor 0:eafc3fd41f75 838
bryantaylor 0:eafc3fd41f75 839 /**
bryantaylor 0:eafc3fd41f75 840 * Get the initiator policy filter mode that will be used.
bryantaylor 0:eafc3fd41f75 841 *
bryantaylor 0:eafc3fd41f75 842 * @return The set scan policy filter mode.
bryantaylor 0:eafc3fd41f75 843 *
bryantaylor 0:eafc3fd41f75 844 * @experimental
bryantaylor 0:eafc3fd41f75 845 */
bryantaylor 0:eafc3fd41f75 846 virtual InitiatorPolicyMode_t getInitiatorPolicyMode(void) const
bryantaylor 0:eafc3fd41f75 847 {
bryantaylor 0:eafc3fd41f75 848 return INIT_POLICY_IGNORE_WHITELIST;
bryantaylor 0:eafc3fd41f75 849 }
bryantaylor 0:eafc3fd41f75 850
bryantaylor 0:eafc3fd41f75 851 protected:
bryantaylor 0:eafc3fd41f75 852 /* Override the following in the underlying adaptation layer to provide the functionality of scanning. */
bryantaylor 0:eafc3fd41f75 853
bryantaylor 0:eafc3fd41f75 854 /**
bryantaylor 0:eafc3fd41f75 855 * Start scanning procedure in the underlying BLE stack.
bryantaylor 0:eafc3fd41f75 856 *
bryantaylor 0:eafc3fd41f75 857 * @param[in] scanningParams
bryantaylor 0:eafc3fd41f75 858 * The GAP scanning parameters.
bryantaylor 0:eafc3fd41f75 859 *
bryantaylor 0:eafc3fd41f75 860 * @return BLE_ERROR_NONE if the scan procedure started successfully.
bryantaylor 0:eafc3fd41f75 861 */
bryantaylor 0:eafc3fd41f75 862 virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) {
bryantaylor 0:eafc3fd41f75 863 (void)scanningParams;
bryantaylor 0:eafc3fd41f75 864 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 865 }
bryantaylor 0:eafc3fd41f75 866
bryantaylor 0:eafc3fd41f75 867 /*
bryantaylor 0:eafc3fd41f75 868 * APIs with non-virtual implementations.
bryantaylor 0:eafc3fd41f75 869 */
bryantaylor 0:eafc3fd41f75 870 public:
bryantaylor 0:eafc3fd41f75 871 /**
bryantaylor 0:eafc3fd41f75 872 * Get the current GAP state of the device using a bitmask that
bryantaylor 0:eafc3fd41f75 873 * describes whether the device is advertising or connected.
bryantaylor 0:eafc3fd41f75 874 *
bryantaylor 0:eafc3fd41f75 875 * @return The current GAP state of the device.
bryantaylor 0:eafc3fd41f75 876 */
bryantaylor 0:eafc3fd41f75 877 GapState_t getState(void) const {
bryantaylor 0:eafc3fd41f75 878 return state;
bryantaylor 0:eafc3fd41f75 879 }
bryantaylor 0:eafc3fd41f75 880
bryantaylor 0:eafc3fd41f75 881 /**
bryantaylor 0:eafc3fd41f75 882 * Set the GAP advertising mode to use for this device.
bryantaylor 0:eafc3fd41f75 883 *
bryantaylor 0:eafc3fd41f75 884 * @param[in] advType
bryantaylor 0:eafc3fd41f75 885 * The new type of the advertising packets.
bryantaylor 0:eafc3fd41f75 886 */
bryantaylor 0:eafc3fd41f75 887 void setAdvertisingType(GapAdvertisingParams::AdvertisingType_t advType) {
bryantaylor 0:eafc3fd41f75 888 _advParams.setAdvertisingType(advType);
bryantaylor 0:eafc3fd41f75 889 }
bryantaylor 0:eafc3fd41f75 890
bryantaylor 0:eafc3fd41f75 891 /**
bryantaylor 0:eafc3fd41f75 892 * Set the advertising interval.
bryantaylor 0:eafc3fd41f75 893 *
bryantaylor 0:eafc3fd41f75 894 * @param[in] interval
bryantaylor 0:eafc3fd41f75 895 * Advertising interval in units of milliseconds. Advertising
bryantaylor 0:eafc3fd41f75 896 * is disabled if interval is 0. If interval is smaller than
bryantaylor 0:eafc3fd41f75 897 * the minimum supported value, then the minimum supported
bryantaylor 0:eafc3fd41f75 898 * value is used instead. This minimum value can be discovered
bryantaylor 0:eafc3fd41f75 899 * using getMinAdvertisingInterval().
bryantaylor 0:eafc3fd41f75 900 *
bryantaylor 0:eafc3fd41f75 901 * This field must be set to 0 if connectionMode is equal
bryantaylor 0:eafc3fd41f75 902 * to ADV_CONNECTABLE_DIRECTED.
bryantaylor 0:eafc3fd41f75 903 *
bryantaylor 0:eafc3fd41f75 904 * @note Decreasing this value will allow central devices to detect a
bryantaylor 0:eafc3fd41f75 905 * peripheral faster, at the expense of more power being used by the radio
bryantaylor 0:eafc3fd41f75 906 * due to the higher data transmit rate.
bryantaylor 0:eafc3fd41f75 907 *
bryantaylor 0:eafc3fd41f75 908 * @note [WARNING] This API previously used 0.625ms as the unit for its
bryantaylor 0:eafc3fd41f75 909 * @p interval argument. That required an explicit conversion from
bryantaylor 0:eafc3fd41f75 910 * milliseconds using Gap::MSEC_TO_GAP_DURATION_UNITS(). This conversion is
bryantaylor 0:eafc3fd41f75 911 * no longer required as the new units are milliseconds. Any application
bryantaylor 0:eafc3fd41f75 912 * code depending on the old semantics needs to be updated accordingly.
bryantaylor 0:eafc3fd41f75 913 */
bryantaylor 0:eafc3fd41f75 914 void setAdvertisingInterval(uint16_t interval) {
bryantaylor 0:eafc3fd41f75 915 if (interval == 0) {
bryantaylor 0:eafc3fd41f75 916 stopAdvertising();
bryantaylor 0:eafc3fd41f75 917 } else if (interval < getMinAdvertisingInterval()) {
bryantaylor 0:eafc3fd41f75 918 interval = getMinAdvertisingInterval();
bryantaylor 0:eafc3fd41f75 919 }
bryantaylor 0:eafc3fd41f75 920 _advParams.setInterval(interval);
bryantaylor 0:eafc3fd41f75 921 }
bryantaylor 0:eafc3fd41f75 922
bryantaylor 0:eafc3fd41f75 923 /**
bryantaylor 0:eafc3fd41f75 924 * Set the advertising timeout. The length of time to advertise for before
bryantaylor 0:eafc3fd41f75 925 * a timeout event is generated.
bryantaylor 0:eafc3fd41f75 926 *
bryantaylor 0:eafc3fd41f75 927 * @param[in] timeout
bryantaylor 0:eafc3fd41f75 928 * Advertising timeout (in seconds) between 0x1 and 0x3FFF (1
bryantaylor 0:eafc3fd41f75 929 * and 16383). Use 0 to disable the advertising timeout.
bryantaylor 0:eafc3fd41f75 930 */
bryantaylor 0:eafc3fd41f75 931 void setAdvertisingTimeout(uint16_t timeout) {
bryantaylor 0:eafc3fd41f75 932 _advParams.setTimeout(timeout);
bryantaylor 0:eafc3fd41f75 933 }
bryantaylor 0:eafc3fd41f75 934
bryantaylor 0:eafc3fd41f75 935 /**
bryantaylor 0:eafc3fd41f75 936 * Start advertising.
bryantaylor 0:eafc3fd41f75 937 *
bryantaylor 0:eafc3fd41f75 938 * @return BLE_ERROR_NONE if the device started advertising successfully.
bryantaylor 0:eafc3fd41f75 939 */
bryantaylor 0:eafc3fd41f75 940 ble_error_t startAdvertising(void) {
bryantaylor 0:eafc3fd41f75 941 ble_error_t rc;
bryantaylor 0:eafc3fd41f75 942 if ((rc = startAdvertising(_advParams)) == BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 943 state.advertising = 1;
bryantaylor 0:eafc3fd41f75 944 }
bryantaylor 0:eafc3fd41f75 945 return rc;
bryantaylor 0:eafc3fd41f75 946 }
bryantaylor 0:eafc3fd41f75 947
bryantaylor 0:eafc3fd41f75 948 /**
bryantaylor 0:eafc3fd41f75 949 * Reset any advertising payload prepared from prior calls to
bryantaylor 0:eafc3fd41f75 950 * accumulateAdvertisingPayload(). This automatically propagates the re-
bryantaylor 0:eafc3fd41f75 951 * initialized advertising payload to the underlying stack.
bryantaylor 0:eafc3fd41f75 952 */
bryantaylor 0:eafc3fd41f75 953 void clearAdvertisingPayload(void) {
bryantaylor 0:eafc3fd41f75 954 _advPayload.clear();
bryantaylor 0:eafc3fd41f75 955 setAdvertisingData(_advPayload, _scanResponse);
bryantaylor 0:eafc3fd41f75 956 }
bryantaylor 0:eafc3fd41f75 957
bryantaylor 0:eafc3fd41f75 958 /**
bryantaylor 0:eafc3fd41f75 959 * Accumulate an AD structure in the advertising payload. Please note that
bryantaylor 0:eafc3fd41f75 960 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
bryantaylor 0:eafc3fd41f75 961 * as an additional 31 bytes if the advertising payload is too
bryantaylor 0:eafc3fd41f75 962 * small.
bryantaylor 0:eafc3fd41f75 963 *
bryantaylor 0:eafc3fd41f75 964 * @param[in] flags
bryantaylor 0:eafc3fd41f75 965 * The flags to be added. Please refer to
bryantaylor 0:eafc3fd41f75 966 * GapAdvertisingData::Flags for valid flags. Multiple
bryantaylor 0:eafc3fd41f75 967 * flags may be specified in combination.
bryantaylor 0:eafc3fd41f75 968 *
bryantaylor 0:eafc3fd41f75 969 * @return BLE_ERROR_NONE if the data was successfully added to the
bryantaylor 0:eafc3fd41f75 970 * advertising payload.
bryantaylor 0:eafc3fd41f75 971 */
bryantaylor 0:eafc3fd41f75 972 ble_error_t accumulateAdvertisingPayload(uint8_t flags) {
bryantaylor 0:eafc3fd41f75 973 GapAdvertisingData advPayloadCopy = _advPayload;
bryantaylor 0:eafc3fd41f75 974 ble_error_t rc;
bryantaylor 0:eafc3fd41f75 975 if ((rc = advPayloadCopy.addFlags(flags)) != BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 976 return rc;
bryantaylor 0:eafc3fd41f75 977 }
bryantaylor 0:eafc3fd41f75 978
bryantaylor 0:eafc3fd41f75 979 rc = setAdvertisingData(advPayloadCopy, _scanResponse);
bryantaylor 0:eafc3fd41f75 980 if (rc == BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 981 _advPayload = advPayloadCopy;
bryantaylor 0:eafc3fd41f75 982 }
bryantaylor 0:eafc3fd41f75 983
bryantaylor 0:eafc3fd41f75 984 return rc;
bryantaylor 0:eafc3fd41f75 985 }
bryantaylor 0:eafc3fd41f75 986
bryantaylor 0:eafc3fd41f75 987 /**
bryantaylor 0:eafc3fd41f75 988 * Accumulate an AD structure in the advertising payload. Please note that
bryantaylor 0:eafc3fd41f75 989 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
bryantaylor 0:eafc3fd41f75 990 * as an additional 31 bytes if the advertising payload is too
bryantaylor 0:eafc3fd41f75 991 * small.
bryantaylor 0:eafc3fd41f75 992 *
bryantaylor 0:eafc3fd41f75 993 * @param[in] app
bryantaylor 0:eafc3fd41f75 994 * The appearance of the peripheral.
bryantaylor 0:eafc3fd41f75 995 *
bryantaylor 0:eafc3fd41f75 996 * @return BLE_ERROR_NONE if the data was successfully added to the
bryantaylor 0:eafc3fd41f75 997 * advertising payload.
bryantaylor 0:eafc3fd41f75 998 */
bryantaylor 0:eafc3fd41f75 999 ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::Appearance app) {
bryantaylor 0:eafc3fd41f75 1000 GapAdvertisingData advPayloadCopy = _advPayload;
bryantaylor 0:eafc3fd41f75 1001 ble_error_t rc;
bryantaylor 0:eafc3fd41f75 1002 if ((rc = advPayloadCopy.addAppearance(app)) != BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1003 return rc;
bryantaylor 0:eafc3fd41f75 1004 }
bryantaylor 0:eafc3fd41f75 1005
bryantaylor 0:eafc3fd41f75 1006 rc = setAdvertisingData(advPayloadCopy, _scanResponse);
bryantaylor 0:eafc3fd41f75 1007 if (rc == BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1008 _advPayload = advPayloadCopy;
bryantaylor 0:eafc3fd41f75 1009 }
bryantaylor 0:eafc3fd41f75 1010
bryantaylor 0:eafc3fd41f75 1011 return rc;
bryantaylor 0:eafc3fd41f75 1012 }
bryantaylor 0:eafc3fd41f75 1013
bryantaylor 0:eafc3fd41f75 1014 /**
bryantaylor 0:eafc3fd41f75 1015 * Accumulate an AD structure in the advertising payload. Please note that
bryantaylor 0:eafc3fd41f75 1016 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
bryantaylor 0:eafc3fd41f75 1017 * as an additional 31 bytes if the advertising payload is too
bryantaylor 0:eafc3fd41f75 1018 * small.
bryantaylor 0:eafc3fd41f75 1019 *
bryantaylor 0:eafc3fd41f75 1020 * @param[in] power
bryantaylor 0:eafc3fd41f75 1021 * The max transmit power to be used by the controller (in dBm).
bryantaylor 0:eafc3fd41f75 1022 *
bryantaylor 0:eafc3fd41f75 1023 * @return BLE_ERROR_NONE if the data was successfully added to the
bryantaylor 0:eafc3fd41f75 1024 * advertising payload.
bryantaylor 0:eafc3fd41f75 1025 */
bryantaylor 0:eafc3fd41f75 1026 ble_error_t accumulateAdvertisingPayloadTxPower(int8_t power) {
bryantaylor 0:eafc3fd41f75 1027 GapAdvertisingData advPayloadCopy = _advPayload;
bryantaylor 0:eafc3fd41f75 1028 ble_error_t rc;
bryantaylor 0:eafc3fd41f75 1029 if ((rc = advPayloadCopy.addTxPower(power)) != BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1030 return rc;
bryantaylor 0:eafc3fd41f75 1031 }
bryantaylor 0:eafc3fd41f75 1032
bryantaylor 0:eafc3fd41f75 1033 rc = setAdvertisingData(advPayloadCopy, _scanResponse);
bryantaylor 0:eafc3fd41f75 1034 if (rc == BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1035 _advPayload = advPayloadCopy;
bryantaylor 0:eafc3fd41f75 1036 }
bryantaylor 0:eafc3fd41f75 1037
bryantaylor 0:eafc3fd41f75 1038 return rc;
bryantaylor 0:eafc3fd41f75 1039 }
bryantaylor 0:eafc3fd41f75 1040
bryantaylor 0:eafc3fd41f75 1041 /**
bryantaylor 0:eafc3fd41f75 1042 * Accumulate a variable length byte-stream as an AD structure in the
bryantaylor 0:eafc3fd41f75 1043 * advertising payload. Please note that the payload is limited to 31 bytes.
bryantaylor 0:eafc3fd41f75 1044 * The SCAN_RESPONSE message may be used as an additional 31 bytes if the
bryantaylor 0:eafc3fd41f75 1045 * advertising payload is too small.
bryantaylor 0:eafc3fd41f75 1046 *
bryantaylor 0:eafc3fd41f75 1047 * @param[in] type
bryantaylor 0:eafc3fd41f75 1048 * The type describing the variable length data.
bryantaylor 0:eafc3fd41f75 1049 * @param[in] data
bryantaylor 0:eafc3fd41f75 1050 * Data bytes.
bryantaylor 0:eafc3fd41f75 1051 * @param[in] len
bryantaylor 0:eafc3fd41f75 1052 * Length of data.
bryantaylor 0:eafc3fd41f75 1053 *
bryantaylor 0:eafc3fd41f75 1054 * @return BLE_ERROR_NONE if the advertisement payload was updated based on
bryantaylor 0:eafc3fd41f75 1055 * matching AD type; otherwise, an appropriate error.
bryantaylor 0:eafc3fd41f75 1056 *
bryantaylor 0:eafc3fd41f75 1057 * @note When the specified AD type is INCOMPLETE_LIST_16BIT_SERVICE_IDS,
bryantaylor 0:eafc3fd41f75 1058 * COMPLETE_LIST_16BIT_SERVICE_IDS, INCOMPLETE_LIST_32BIT_SERVICE_IDS,
bryantaylor 0:eafc3fd41f75 1059 * COMPLETE_LIST_32BIT_SERVICE_IDS, INCOMPLETE_LIST_128BIT_SERVICE_IDS,
bryantaylor 0:eafc3fd41f75 1060 * COMPLETE_LIST_128BIT_SERVICE_IDS or LIST_128BIT_SOLICITATION_IDS the
bryantaylor 0:eafc3fd41f75 1061 * supplied value is appended to the values previously added to the
bryantaylor 0:eafc3fd41f75 1062 * payload.
bryantaylor 0:eafc3fd41f75 1063 */
bryantaylor 0:eafc3fd41f75 1064 ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
bryantaylor 0:eafc3fd41f75 1065 GapAdvertisingData advPayloadCopy = _advPayload;
bryantaylor 0:eafc3fd41f75 1066 ble_error_t rc;
bryantaylor 0:eafc3fd41f75 1067 if ((rc = advPayloadCopy.addData(type, data, len)) != BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1068 return rc;
bryantaylor 0:eafc3fd41f75 1069 }
bryantaylor 0:eafc3fd41f75 1070
bryantaylor 0:eafc3fd41f75 1071 rc = setAdvertisingData(advPayloadCopy, _scanResponse);
bryantaylor 0:eafc3fd41f75 1072 if (rc == BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1073 _advPayload = advPayloadCopy;
bryantaylor 0:eafc3fd41f75 1074 }
bryantaylor 0:eafc3fd41f75 1075
bryantaylor 0:eafc3fd41f75 1076 return rc;
bryantaylor 0:eafc3fd41f75 1077 }
bryantaylor 0:eafc3fd41f75 1078
bryantaylor 0:eafc3fd41f75 1079 /**
bryantaylor 0:eafc3fd41f75 1080 * Update a particular ADV field in the advertising payload (based on
bryantaylor 0:eafc3fd41f75 1081 * matching type).
bryantaylor 0:eafc3fd41f75 1082 *
bryantaylor 0:eafc3fd41f75 1083 * @param[in] type
bryantaylor 0:eafc3fd41f75 1084 * The ADV type field describing the variable length data.
bryantaylor 0:eafc3fd41f75 1085 * @param[in] data
bryantaylor 0:eafc3fd41f75 1086 * Data bytes.
bryantaylor 0:eafc3fd41f75 1087 * @param[in] len
bryantaylor 0:eafc3fd41f75 1088 * Length of data.
bryantaylor 0:eafc3fd41f75 1089 *
bryantaylor 0:eafc3fd41f75 1090 * @note If advertisements are enabled, then the update will take effect immediately.
bryantaylor 0:eafc3fd41f75 1091 *
bryantaylor 0:eafc3fd41f75 1092 * @return BLE_ERROR_NONE if the advertisement payload was updated based on
bryantaylor 0:eafc3fd41f75 1093 * matching AD type; otherwise, an appropriate error.
bryantaylor 0:eafc3fd41f75 1094 */
bryantaylor 0:eafc3fd41f75 1095 ble_error_t updateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
bryantaylor 0:eafc3fd41f75 1096 GapAdvertisingData advPayloadCopy = _advPayload;
bryantaylor 0:eafc3fd41f75 1097 ble_error_t rc;
bryantaylor 0:eafc3fd41f75 1098 if ((rc = advPayloadCopy.updateData(type, data, len)) != BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1099 return rc;
bryantaylor 0:eafc3fd41f75 1100 }
bryantaylor 0:eafc3fd41f75 1101
bryantaylor 0:eafc3fd41f75 1102 rc = setAdvertisingData(advPayloadCopy, _scanResponse);
bryantaylor 0:eafc3fd41f75 1103 if (rc == BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1104 _advPayload = advPayloadCopy;
bryantaylor 0:eafc3fd41f75 1105 }
bryantaylor 0:eafc3fd41f75 1106
bryantaylor 0:eafc3fd41f75 1107 return rc;
bryantaylor 0:eafc3fd41f75 1108 }
bryantaylor 0:eafc3fd41f75 1109
bryantaylor 0:eafc3fd41f75 1110 /**
bryantaylor 0:eafc3fd41f75 1111 * Set up a particular, user-constructed advertisement payload for the
bryantaylor 0:eafc3fd41f75 1112 * underlying stack. It would be uncommon for this API to be used directly;
bryantaylor 0:eafc3fd41f75 1113 * there are other APIs to build an advertisement payload (refer to
bryantaylor 0:eafc3fd41f75 1114 * Gap::accumulateAdvertisingPayload()).
bryantaylor 0:eafc3fd41f75 1115 *
bryantaylor 0:eafc3fd41f75 1116 * @param[in] payload
bryantaylor 0:eafc3fd41f75 1117 * A reference to a user constructed advertisement
bryantaylor 0:eafc3fd41f75 1118 * payload.
bryantaylor 0:eafc3fd41f75 1119 *
bryantaylor 0:eafc3fd41f75 1120 * @return BLE_ERROR_NONE if the advertisement payload was successfully
bryantaylor 0:eafc3fd41f75 1121 * set.
bryantaylor 0:eafc3fd41f75 1122 */
bryantaylor 0:eafc3fd41f75 1123 ble_error_t setAdvertisingPayload(const GapAdvertisingData &payload) {
bryantaylor 0:eafc3fd41f75 1124 ble_error_t rc = setAdvertisingData(payload, _scanResponse);
bryantaylor 0:eafc3fd41f75 1125 if (rc == BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1126 _advPayload = payload;
bryantaylor 0:eafc3fd41f75 1127 }
bryantaylor 0:eafc3fd41f75 1128
bryantaylor 0:eafc3fd41f75 1129 return rc;
bryantaylor 0:eafc3fd41f75 1130 }
bryantaylor 0:eafc3fd41f75 1131
bryantaylor 0:eafc3fd41f75 1132 /**
bryantaylor 0:eafc3fd41f75 1133 * Get a reference to the advertising payload.
bryantaylor 0:eafc3fd41f75 1134 *
bryantaylor 0:eafc3fd41f75 1135 * @return Read back advertising data.
bryantaylor 0:eafc3fd41f75 1136 *
bryantaylor 0:eafc3fd41f75 1137 * @note Useful for storing and restoring payload.
bryantaylor 0:eafc3fd41f75 1138 */
bryantaylor 0:eafc3fd41f75 1139 const GapAdvertisingData &getAdvertisingPayload(void) const {
bryantaylor 0:eafc3fd41f75 1140 return _advPayload;
bryantaylor 0:eafc3fd41f75 1141 }
bryantaylor 0:eafc3fd41f75 1142
bryantaylor 0:eafc3fd41f75 1143 /**
bryantaylor 0:eafc3fd41f75 1144 * Accumulate a variable length byte-stream as an AD structure in the
bryantaylor 0:eafc3fd41f75 1145 * scanResponse payload.
bryantaylor 0:eafc3fd41f75 1146 *
bryantaylor 0:eafc3fd41f75 1147 * @param[in] type
bryantaylor 0:eafc3fd41f75 1148 * The type describing the variable length data.
bryantaylor 0:eafc3fd41f75 1149 * @param[in] data
bryantaylor 0:eafc3fd41f75 1150 * Data bytes.
bryantaylor 0:eafc3fd41f75 1151 * @param[in] len
bryantaylor 0:eafc3fd41f75 1152 * Length of data.
bryantaylor 0:eafc3fd41f75 1153 *
bryantaylor 0:eafc3fd41f75 1154 * @return BLE_ERROR_NONE if the data was successfully added to the scan
bryantaylor 0:eafc3fd41f75 1155 * response payload.
bryantaylor 0:eafc3fd41f75 1156 */
bryantaylor 0:eafc3fd41f75 1157 ble_error_t accumulateScanResponse(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
bryantaylor 0:eafc3fd41f75 1158 GapAdvertisingData scanResponseCopy = _scanResponse;
bryantaylor 0:eafc3fd41f75 1159 ble_error_t rc;
bryantaylor 0:eafc3fd41f75 1160 if ((rc = scanResponseCopy.addData(type, data, len)) != BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1161 return rc;
bryantaylor 0:eafc3fd41f75 1162 }
bryantaylor 0:eafc3fd41f75 1163
bryantaylor 0:eafc3fd41f75 1164 rc = setAdvertisingData(_advPayload, scanResponseCopy);
bryantaylor 0:eafc3fd41f75 1165 if (rc == BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1166 _scanResponse = scanResponseCopy;
bryantaylor 0:eafc3fd41f75 1167 }
bryantaylor 0:eafc3fd41f75 1168
bryantaylor 0:eafc3fd41f75 1169 return rc;
bryantaylor 0:eafc3fd41f75 1170 }
bryantaylor 0:eafc3fd41f75 1171
bryantaylor 0:eafc3fd41f75 1172 /**
bryantaylor 0:eafc3fd41f75 1173 * Reset any scan response prepared from prior calls to
bryantaylor 0:eafc3fd41f75 1174 * Gap::accumulateScanResponse().
bryantaylor 0:eafc3fd41f75 1175 *
bryantaylor 0:eafc3fd41f75 1176 * @note This should be followed by a call to Gap::setAdvertisingPayload() or
bryantaylor 0:eafc3fd41f75 1177 * Gap::startAdvertising() before the update takes effect.
bryantaylor 0:eafc3fd41f75 1178 */
bryantaylor 0:eafc3fd41f75 1179 void clearScanResponse(void) {
bryantaylor 0:eafc3fd41f75 1180 _scanResponse.clear();
bryantaylor 0:eafc3fd41f75 1181 setAdvertisingData(_advPayload, _scanResponse);
bryantaylor 0:eafc3fd41f75 1182 }
bryantaylor 0:eafc3fd41f75 1183
bryantaylor 0:eafc3fd41f75 1184 /**
bryantaylor 0:eafc3fd41f75 1185 * Set up parameters for GAP scanning (observer mode).
bryantaylor 0:eafc3fd41f75 1186 *
bryantaylor 0:eafc3fd41f75 1187 * @param[in] interval
bryantaylor 0:eafc3fd41f75 1188 * Scan interval (in milliseconds) [valid values lie between 2.5ms and 10.24s].
bryantaylor 0:eafc3fd41f75 1189 * @param[in] window
bryantaylor 0:eafc3fd41f75 1190 * Scan Window (in milliseconds) [valid values lie between 2.5ms and 10.24s].
bryantaylor 0:eafc3fd41f75 1191 * @param[in] timeout
bryantaylor 0:eafc3fd41f75 1192 * Scan timeout (in seconds) between 0x0001 and 0xFFFF; 0x0000 disables the timeout.
bryantaylor 0:eafc3fd41f75 1193 * @param[in] activeScanning
bryantaylor 0:eafc3fd41f75 1194 * Set to True if active-scanning is required. This is used to fetch the
bryantaylor 0:eafc3fd41f75 1195 * scan response from a peer if possible.
bryantaylor 0:eafc3fd41f75 1196 *
bryantaylor 0:eafc3fd41f75 1197 * @return BLE_ERROR_NONE if the scan parameters were correctly set.
bryantaylor 0:eafc3fd41f75 1198 *
bryantaylor 0:eafc3fd41f75 1199 * @note The scanning window divided by the interval determines the duty cycle for
bryantaylor 0:eafc3fd41f75 1200 * scanning. For example, if the interval is 100ms and the window is 10ms,
bryantaylor 0:eafc3fd41f75 1201 * then the controller will scan for 10 percent of the time. It is possible
bryantaylor 0:eafc3fd41f75 1202 * to have the interval and window set to the same value. In this case,
bryantaylor 0:eafc3fd41f75 1203 * scanning is continuous, with a change of scanning frequency once every
bryantaylor 0:eafc3fd41f75 1204 * interval.
bryantaylor 0:eafc3fd41f75 1205 *
bryantaylor 0:eafc3fd41f75 1206 * @note Once the scanning parameters have been configured, scanning can be
bryantaylor 0:eafc3fd41f75 1207 * enabled by using startScan().
bryantaylor 0:eafc3fd41f75 1208 *
bryantaylor 0:eafc3fd41f75 1209 * @note The scan interval and window are recommendations to the BLE stack.
bryantaylor 0:eafc3fd41f75 1210 */
bryantaylor 0:eafc3fd41f75 1211 ble_error_t setScanParams(uint16_t interval = GapScanningParams::SCAN_INTERVAL_MAX,
bryantaylor 0:eafc3fd41f75 1212 uint16_t window = GapScanningParams::SCAN_WINDOW_MAX,
bryantaylor 0:eafc3fd41f75 1213 uint16_t timeout = 0,
bryantaylor 0:eafc3fd41f75 1214 bool activeScanning = false) {
bryantaylor 0:eafc3fd41f75 1215 ble_error_t rc;
bryantaylor 0:eafc3fd41f75 1216 if (((rc = _scanningParams.setInterval(interval)) == BLE_ERROR_NONE) &&
bryantaylor 0:eafc3fd41f75 1217 ((rc = _scanningParams.setWindow(window)) == BLE_ERROR_NONE) &&
bryantaylor 0:eafc3fd41f75 1218 ((rc = _scanningParams.setTimeout(timeout)) == BLE_ERROR_NONE)) {
bryantaylor 0:eafc3fd41f75 1219 _scanningParams.setActiveScanning(activeScanning);
bryantaylor 0:eafc3fd41f75 1220 return BLE_ERROR_NONE;
bryantaylor 0:eafc3fd41f75 1221 }
bryantaylor 0:eafc3fd41f75 1222
bryantaylor 0:eafc3fd41f75 1223 return rc;
bryantaylor 0:eafc3fd41f75 1224 }
bryantaylor 0:eafc3fd41f75 1225
bryantaylor 0:eafc3fd41f75 1226 /**
bryantaylor 0:eafc3fd41f75 1227 * Set up the scanInterval parameter for GAP scanning (observer mode).
bryantaylor 0:eafc3fd41f75 1228 *
bryantaylor 0:eafc3fd41f75 1229 * @param[in] interval
bryantaylor 0:eafc3fd41f75 1230 * Scan interval (in milliseconds) [valid values lie between 2.5ms and 10.24s].
bryantaylor 0:eafc3fd41f75 1231 *
bryantaylor 0:eafc3fd41f75 1232 * @return BLE_ERROR_NONE if the scan interval was correctly set.
bryantaylor 0:eafc3fd41f75 1233 *
bryantaylor 0:eafc3fd41f75 1234 * @note The scanning window divided by the interval determines the duty cycle for
bryantaylor 0:eafc3fd41f75 1235 * scanning. For example, if the interval is 100ms and the window is 10ms,
bryantaylor 0:eafc3fd41f75 1236 * then the controller will scan for 10 percent of the time. It is possible
bryantaylor 0:eafc3fd41f75 1237 * to have the interval and window set to the same value. In this case,
bryantaylor 0:eafc3fd41f75 1238 * scanning is continuous, with a change of scanning frequency once every
bryantaylor 0:eafc3fd41f75 1239 * interval.
bryantaylor 0:eafc3fd41f75 1240 *
bryantaylor 0:eafc3fd41f75 1241 * @note Once the scanning parameters have been configured, scanning can be
bryantaylor 0:eafc3fd41f75 1242 * enabled by using startScan().
bryantaylor 0:eafc3fd41f75 1243 */
bryantaylor 0:eafc3fd41f75 1244 ble_error_t setScanInterval(uint16_t interval) {
bryantaylor 0:eafc3fd41f75 1245 return _scanningParams.setInterval(interval);
bryantaylor 0:eafc3fd41f75 1246 }
bryantaylor 0:eafc3fd41f75 1247
bryantaylor 0:eafc3fd41f75 1248 /**
bryantaylor 0:eafc3fd41f75 1249 * Set up the scanWindow parameter for GAP scanning (observer mode).
bryantaylor 0:eafc3fd41f75 1250 *
bryantaylor 0:eafc3fd41f75 1251 * @param[in] window
bryantaylor 0:eafc3fd41f75 1252 * Scan Window (in milliseconds) [valid values lie between 2.5ms and 10.24s].
bryantaylor 0:eafc3fd41f75 1253 *
bryantaylor 0:eafc3fd41f75 1254 * @return BLE_ERROR_NONE if the scan window was correctly set.
bryantaylor 0:eafc3fd41f75 1255 *
bryantaylor 0:eafc3fd41f75 1256 * @note The scanning window divided by the interval determines the duty cycle for
bryantaylor 0:eafc3fd41f75 1257 * scanning. For example, if the interval is 100ms and the window is 10ms,
bryantaylor 0:eafc3fd41f75 1258 * then the controller will scan for 10 percent of the time. It is possible
bryantaylor 0:eafc3fd41f75 1259 * to have the interval and window set to the same value. In this case,
bryantaylor 0:eafc3fd41f75 1260 * scanning is continuous, with a change of scanning frequency once every
bryantaylor 0:eafc3fd41f75 1261 * interval.
bryantaylor 0:eafc3fd41f75 1262 *
bryantaylor 0:eafc3fd41f75 1263 * @note Once the scanning parameters have been configured, scanning can be
bryantaylor 0:eafc3fd41f75 1264 * enabled by using startScan().
bryantaylor 0:eafc3fd41f75 1265 *
bryantaylor 0:eafc3fd41f75 1266 * @note If scanning is already active, the updated value of scanWindow will be
bryantaylor 0:eafc3fd41f75 1267 * propagated to the underlying BLE stack.
bryantaylor 0:eafc3fd41f75 1268 */
bryantaylor 0:eafc3fd41f75 1269 ble_error_t setScanWindow(uint16_t window) {
bryantaylor 0:eafc3fd41f75 1270 ble_error_t rc;
bryantaylor 0:eafc3fd41f75 1271 if ((rc = _scanningParams.setWindow(window)) != BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1272 return rc;
bryantaylor 0:eafc3fd41f75 1273 }
bryantaylor 0:eafc3fd41f75 1274
bryantaylor 0:eafc3fd41f75 1275 /* If scanning is already active, propagate the new setting to the stack. */
bryantaylor 0:eafc3fd41f75 1276 if (scanningActive) {
bryantaylor 0:eafc3fd41f75 1277 return startRadioScan(_scanningParams);
bryantaylor 0:eafc3fd41f75 1278 }
bryantaylor 0:eafc3fd41f75 1279
bryantaylor 0:eafc3fd41f75 1280 return BLE_ERROR_NONE;
bryantaylor 0:eafc3fd41f75 1281 }
bryantaylor 0:eafc3fd41f75 1282
bryantaylor 0:eafc3fd41f75 1283 /**
bryantaylor 0:eafc3fd41f75 1284 * Set up parameters for GAP scanning (observer mode).
bryantaylor 0:eafc3fd41f75 1285 *
bryantaylor 0:eafc3fd41f75 1286 * @param[in] timeout
bryantaylor 0:eafc3fd41f75 1287 * Scan timeout (in seconds) between 0x0001 and 0xFFFF; 0x0000 disables the timeout.
bryantaylor 0:eafc3fd41f75 1288 *
bryantaylor 0:eafc3fd41f75 1289 * @return BLE_ERROR_NONE if the scan timeout was correctly set.
bryantaylor 0:eafc3fd41f75 1290 *
bryantaylor 0:eafc3fd41f75 1291 * @note Once the scanning parameters have been configured, scanning can be
bryantaylor 0:eafc3fd41f75 1292 * enabled by using startScan().
bryantaylor 0:eafc3fd41f75 1293 *
bryantaylor 0:eafc3fd41f75 1294 * @note If scanning is already active, the updated value of scanTimeout will be
bryantaylor 0:eafc3fd41f75 1295 * propagated to the underlying BLE stack.
bryantaylor 0:eafc3fd41f75 1296 */
bryantaylor 0:eafc3fd41f75 1297 ble_error_t setScanTimeout(uint16_t timeout) {
bryantaylor 0:eafc3fd41f75 1298 ble_error_t rc;
bryantaylor 0:eafc3fd41f75 1299 if ((rc = _scanningParams.setTimeout(timeout)) != BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1300 return rc;
bryantaylor 0:eafc3fd41f75 1301 }
bryantaylor 0:eafc3fd41f75 1302
bryantaylor 0:eafc3fd41f75 1303 /* If scanning is already active, propagate the new settings to the stack. */
bryantaylor 0:eafc3fd41f75 1304 if (scanningActive) {
bryantaylor 0:eafc3fd41f75 1305 return startRadioScan(_scanningParams);
bryantaylor 0:eafc3fd41f75 1306 }
bryantaylor 0:eafc3fd41f75 1307
bryantaylor 0:eafc3fd41f75 1308 return BLE_ERROR_NONE;
bryantaylor 0:eafc3fd41f75 1309 }
bryantaylor 0:eafc3fd41f75 1310
bryantaylor 0:eafc3fd41f75 1311 /**
bryantaylor 0:eafc3fd41f75 1312 * Modify the active scanning parameter for GAP scanning (observer mode).
bryantaylor 0:eafc3fd41f75 1313 * This is used to fetch the scan response from a peer if possible.
bryantaylor 0:eafc3fd41f75 1314 *
bryantaylor 0:eafc3fd41f75 1315 * @param[in] activeScanning
bryantaylor 0:eafc3fd41f75 1316 * Set to True if active-scanning is required.
bryantaylor 0:eafc3fd41f75 1317 *
bryantaylor 0:eafc3fd41f75 1318 * @return BLE_ERROR_NONE if active scanning was successfully set.
bryantaylor 0:eafc3fd41f75 1319 *
bryantaylor 0:eafc3fd41f75 1320 * @note Once the scanning parameters have been configured, scanning can be
bryantaylor 0:eafc3fd41f75 1321 * enabled by using startScan().
bryantaylor 0:eafc3fd41f75 1322 *
bryantaylor 0:eafc3fd41f75 1323 * @note If scanning is already in progress, then active-scanning will be enabled
bryantaylor 0:eafc3fd41f75 1324 * for the underlying BLE stack.
bryantaylor 0:eafc3fd41f75 1325 */
bryantaylor 0:eafc3fd41f75 1326 ble_error_t setActiveScanning(bool activeScanning) {
bryantaylor 0:eafc3fd41f75 1327 _scanningParams.setActiveScanning(activeScanning);
bryantaylor 0:eafc3fd41f75 1328
bryantaylor 0:eafc3fd41f75 1329 /* If scanning is already active, propagate the new settings to the stack. */
bryantaylor 0:eafc3fd41f75 1330 if (scanningActive) {
bryantaylor 0:eafc3fd41f75 1331 return startRadioScan(_scanningParams);
bryantaylor 0:eafc3fd41f75 1332 }
bryantaylor 0:eafc3fd41f75 1333
bryantaylor 0:eafc3fd41f75 1334 return BLE_ERROR_NONE;
bryantaylor 0:eafc3fd41f75 1335 }
bryantaylor 0:eafc3fd41f75 1336
bryantaylor 0:eafc3fd41f75 1337 /**
bryantaylor 0:eafc3fd41f75 1338 * Start scanning (Observer Procedure) based on the parameters currently in
bryantaylor 0:eafc3fd41f75 1339 * effect.
bryantaylor 0:eafc3fd41f75 1340 *
bryantaylor 0:eafc3fd41f75 1341 * @param[in] callback
bryantaylor 0:eafc3fd41f75 1342 * The application-specific callback to be invoked upon
bryantaylor 0:eafc3fd41f75 1343 * receiving every advertisement report. This can be passed in
bryantaylor 0:eafc3fd41f75 1344 * as NULL, in which case scanning may not be enabled at all.
bryantaylor 0:eafc3fd41f75 1345 *
bryantaylor 0:eafc3fd41f75 1346 * @return BLE_ERROR_NONE if the device successfully started the scan
bryantaylor 0:eafc3fd41f75 1347 * procedure.
bryantaylor 0:eafc3fd41f75 1348 */
bryantaylor 0:eafc3fd41f75 1349 ble_error_t startScan(void (*callback)(const AdvertisementCallbackParams_t *params)) {
bryantaylor 0:eafc3fd41f75 1350 ble_error_t err = BLE_ERROR_NONE;
bryantaylor 0:eafc3fd41f75 1351 if (callback) {
bryantaylor 0:eafc3fd41f75 1352 if ((err = startRadioScan(_scanningParams)) == BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1353 scanningActive = true;
bryantaylor 0:eafc3fd41f75 1354 onAdvertisementReport.attach(callback);
bryantaylor 0:eafc3fd41f75 1355 }
bryantaylor 0:eafc3fd41f75 1356 }
bryantaylor 0:eafc3fd41f75 1357
bryantaylor 0:eafc3fd41f75 1358 return err;
bryantaylor 0:eafc3fd41f75 1359 }
bryantaylor 0:eafc3fd41f75 1360
bryantaylor 0:eafc3fd41f75 1361 /**
bryantaylor 0:eafc3fd41f75 1362 * Same as Gap::startScan(), but allows the possibility to add an object
bryantaylor 0:eafc3fd41f75 1363 * reference and member function as handler for advertisement event
bryantaylor 0:eafc3fd41f75 1364 * callbacks.
bryantaylor 0:eafc3fd41f75 1365 *
bryantaylor 0:eafc3fd41f75 1366 * @param[in] object
bryantaylor 0:eafc3fd41f75 1367 * Pointer to the object of a class defining the member callback
bryantaylor 0:eafc3fd41f75 1368 * function (@p callbackMember).
bryantaylor 0:eafc3fd41f75 1369 * @param[in] callbackMember
bryantaylor 0:eafc3fd41f75 1370 * The member callback (within the context of an object) to be
bryantaylor 0:eafc3fd41f75 1371 * invoked.
bryantaylor 0:eafc3fd41f75 1372 *
bryantaylor 0:eafc3fd41f75 1373 * @return BLE_ERROR_NONE if the device successfully started the scan
bryantaylor 0:eafc3fd41f75 1374 * procedure.
bryantaylor 0:eafc3fd41f75 1375 */
bryantaylor 0:eafc3fd41f75 1376 template<typename T>
bryantaylor 0:eafc3fd41f75 1377 ble_error_t startScan(T *object, void (T::*callbackMember)(const AdvertisementCallbackParams_t *params)) {
bryantaylor 0:eafc3fd41f75 1378 ble_error_t err = BLE_ERROR_NONE;
bryantaylor 0:eafc3fd41f75 1379 if (object && callbackMember) {
bryantaylor 0:eafc3fd41f75 1380 if ((err = startRadioScan(_scanningParams)) == BLE_ERROR_NONE) {
bryantaylor 0:eafc3fd41f75 1381 scanningActive = true;
bryantaylor 0:eafc3fd41f75 1382 onAdvertisementReport.attach(object, callbackMember);
bryantaylor 0:eafc3fd41f75 1383 }
bryantaylor 0:eafc3fd41f75 1384 }
bryantaylor 0:eafc3fd41f75 1385
bryantaylor 0:eafc3fd41f75 1386 return err;
bryantaylor 0:eafc3fd41f75 1387 }
bryantaylor 0:eafc3fd41f75 1388
bryantaylor 0:eafc3fd41f75 1389 /**
bryantaylor 0:eafc3fd41f75 1390 * Initialize radio-notification events to be generated from the stack.
bryantaylor 0:eafc3fd41f75 1391 * This API doesn't need to be called directly.
bryantaylor 0:eafc3fd41f75 1392 *
bryantaylor 0:eafc3fd41f75 1393 * Radio Notification is a feature that enables ACTIVE and INACTIVE
bryantaylor 0:eafc3fd41f75 1394 * (nACTIVE) signals from the stack that notify the application when the
bryantaylor 0:eafc3fd41f75 1395 * radio is in use.
bryantaylor 0:eafc3fd41f75 1396 *
bryantaylor 0:eafc3fd41f75 1397 * The ACTIVE signal is sent before the radio event starts. The nACTIVE
bryantaylor 0:eafc3fd41f75 1398 * signal is sent at the end of the radio event. These signals can be used
bryantaylor 0:eafc3fd41f75 1399 * by the application programmer to synchronize application logic with radio
bryantaylor 0:eafc3fd41f75 1400 * activity. For example, the ACTIVE signal can be used to shut off external
bryantaylor 0:eafc3fd41f75 1401 * devices, to manage peak current drawn during periods when the radio is on,
bryantaylor 0:eafc3fd41f75 1402 * or to trigger sensor data collection for transmission in the Radio Event.
bryantaylor 0:eafc3fd41f75 1403 *
bryantaylor 0:eafc3fd41f75 1404 * @return BLE_ERROR_NONE on successful initialization, otherwise an error code.
bryantaylor 0:eafc3fd41f75 1405 */
bryantaylor 0:eafc3fd41f75 1406 virtual ble_error_t initRadioNotification(void) {
bryantaylor 0:eafc3fd41f75 1407 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
bryantaylor 0:eafc3fd41f75 1408 }
bryantaylor 0:eafc3fd41f75 1409
bryantaylor 0:eafc3fd41f75 1410 private:
bryantaylor 0:eafc3fd41f75 1411 /**
bryantaylor 0:eafc3fd41f75 1412 * Functionality that is BLE stack-dependent and must be implemented by the
bryantaylor 0:eafc3fd41f75 1413 * ported. This is a helper function to set the advertising data in the
bryantaylor 0:eafc3fd41f75 1414 * BLE stack.
bryantaylor 0:eafc3fd41f75 1415 *
bryantaylor 0:eafc3fd41f75 1416 * @param[in] advData
bryantaylor 0:eafc3fd41f75 1417 * The new advertising data.
bryantaylor 0:eafc3fd41f75 1418 * @param[in] scanResponse
bryantaylor 0:eafc3fd41f75 1419 * The new scan response data.
bryantaylor 0:eafc3fd41f75 1420 *
bryantaylor 0:eafc3fd41f75 1421 * @return BLE_ERROR_NONE if the advertising data was set successfully.
bryantaylor 0:eafc3fd41f75 1422 */
bryantaylor 0:eafc3fd41f75 1423 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse) = 0;
bryantaylor 0:eafc3fd41f75 1424
bryantaylor 0:eafc3fd41f75 1425 /**
bryantaylor 0:eafc3fd41f75 1426 * Functionality that is BLE stack-dependent and must be implemented by the
bryantaylor 0:eafc3fd41f75 1427 * ported. This is a helper function to start the advertising procedure in
bryantaylor 0:eafc3fd41f75 1428 * the underlying BLE stack.
bryantaylor 0:eafc3fd41f75 1429 *
bryantaylor 0:eafc3fd41f75 1430 * @param[in]
bryantaylor 0:eafc3fd41f75 1431 * The advertising parameters.
bryantaylor 0:eafc3fd41f75 1432 *
bryantaylor 0:eafc3fd41f75 1433 * @return BLE_ERROR_NONE if the advertising procedure was successfully
bryantaylor 0:eafc3fd41f75 1434 * started.
bryantaylor 0:eafc3fd41f75 1435 */
bryantaylor 0:eafc3fd41f75 1436 virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0;
bryantaylor 0:eafc3fd41f75 1437
bryantaylor 0:eafc3fd41f75 1438 public:
bryantaylor 0:eafc3fd41f75 1439 /**
bryantaylor 0:eafc3fd41f75 1440 * Accessor to read back currently active advertising parameters.
bryantaylor 0:eafc3fd41f75 1441 *
bryantaylor 0:eafc3fd41f75 1442 * @return A reference to the current advertising parameters.
bryantaylor 0:eafc3fd41f75 1443 */
bryantaylor 0:eafc3fd41f75 1444 GapAdvertisingParams &getAdvertisingParams(void) {
bryantaylor 0:eafc3fd41f75 1445 return _advParams;
bryantaylor 0:eafc3fd41f75 1446 }
bryantaylor 0:eafc3fd41f75 1447
bryantaylor 0:eafc3fd41f75 1448 /**
bryantaylor 0:eafc3fd41f75 1449 * A const alternative to Gap::getAdvertisingParams().
bryantaylor 0:eafc3fd41f75 1450 *
bryantaylor 0:eafc3fd41f75 1451 * @return A const reference to the current advertising parameters.
bryantaylor 0:eafc3fd41f75 1452 */
bryantaylor 0:eafc3fd41f75 1453 const GapAdvertisingParams &getAdvertisingParams(void) const {
bryantaylor 0:eafc3fd41f75 1454 return _advParams;
bryantaylor 0:eafc3fd41f75 1455 }
bryantaylor 0:eafc3fd41f75 1456
bryantaylor 0:eafc3fd41f75 1457 /**
bryantaylor 0:eafc3fd41f75 1458 * Set up a particular, user-constructed set of advertisement parameters for
bryantaylor 0:eafc3fd41f75 1459 * the underlying stack. It would be uncommon for this API to be used
bryantaylor 0:eafc3fd41f75 1460 * directly; there are other APIs to tweak advertisement parameters
bryantaylor 0:eafc3fd41f75 1461 * individually.
bryantaylor 0:eafc3fd41f75 1462 *
bryantaylor 0:eafc3fd41f75 1463 * @param[in] newParams
bryantaylor 0:eafc3fd41f75 1464 * The new advertising parameters.
bryantaylor 0:eafc3fd41f75 1465 */
bryantaylor 0:eafc3fd41f75 1466 void setAdvertisingParams(const GapAdvertisingParams &newParams) {
bryantaylor 0:eafc3fd41f75 1467 _advParams = newParams;
bryantaylor 0:eafc3fd41f75 1468 }
bryantaylor 0:eafc3fd41f75 1469
bryantaylor 0:eafc3fd41f75 1470 /* Event callback handlers. */
bryantaylor 0:eafc3fd41f75 1471 public:
bryantaylor 0:eafc3fd41f75 1472 /**
bryantaylor 0:eafc3fd41f75 1473 * Set up a callback for timeout events. Refer to TimeoutSource_t for
bryantaylor 0:eafc3fd41f75 1474 * possible event types.
bryantaylor 0:eafc3fd41f75 1475 *
bryantaylor 0:eafc3fd41f75 1476 * @param[in] callback
bryantaylor 0:eafc3fd41f75 1477 * Event handler being registered.
bryantaylor 0:eafc3fd41f75 1478 *
bryantaylor 0:eafc3fd41f75 1479 * @note It is possible to unregister callbacks using onTimeout().detach(callback).
bryantaylor 0:eafc3fd41f75 1480 */
bryantaylor 0:eafc3fd41f75 1481 void onTimeout(TimeoutEventCallback_t callback) {
bryantaylor 0:eafc3fd41f75 1482 timeoutCallbackChain.add(callback);
bryantaylor 0:eafc3fd41f75 1483 }
bryantaylor 0:eafc3fd41f75 1484
bryantaylor 0:eafc3fd41f75 1485 /**
bryantaylor 0:eafc3fd41f75 1486 * @brief Provide access to the callchain of timeout event callbacks.
bryantaylor 0:eafc3fd41f75 1487 *
bryantaylor 0:eafc3fd41f75 1488 * @note It is possible to register callbacks using onTimeout().add(callback).
bryantaylor 0:eafc3fd41f75 1489 *
bryantaylor 0:eafc3fd41f75 1490 * @note It is possible to unregister callbacks using onTimeout().detach(callback).
bryantaylor 0:eafc3fd41f75 1491 *
bryantaylor 0:eafc3fd41f75 1492 * @return A reference to the timeout event callbacks chain.
bryantaylor 0:eafc3fd41f75 1493 */
bryantaylor 0:eafc3fd41f75 1494 TimeoutEventCallbackChain_t& onTimeout() {
bryantaylor 0:eafc3fd41f75 1495 return timeoutCallbackChain;
bryantaylor 0:eafc3fd41f75 1496 }
bryantaylor 0:eafc3fd41f75 1497
bryantaylor 0:eafc3fd41f75 1498 /**
bryantaylor 0:eafc3fd41f75 1499 * Append to a chain of callbacks to be invoked upon GAP connection.
bryantaylor 0:eafc3fd41f75 1500 *
bryantaylor 0:eafc3fd41f75 1501 * @param[in] callback
bryantaylor 0:eafc3fd41f75 1502 * Event handler being registered.
bryantaylor 0:eafc3fd41f75 1503 *
bryantaylor 0:eafc3fd41f75 1504 * @note It is possible to unregister callbacks using onConnection().detach(callback)
bryantaylor 0:eafc3fd41f75 1505 */
bryantaylor 0:eafc3fd41f75 1506 void onConnection(ConnectionEventCallback_t callback) {
bryantaylor 0:eafc3fd41f75 1507 connectionCallChain.add(callback);
bryantaylor 0:eafc3fd41f75 1508 }
bryantaylor 0:eafc3fd41f75 1509
bryantaylor 0:eafc3fd41f75 1510 /**
bryantaylor 0:eafc3fd41f75 1511 * Same as Gap::onConnection(), but allows the possibility to add an object
bryantaylor 0:eafc3fd41f75 1512 * reference and member function as handler for connection event
bryantaylor 0:eafc3fd41f75 1513 * callbacks.
bryantaylor 0:eafc3fd41f75 1514 *
bryantaylor 0:eafc3fd41f75 1515 * @param[in] tptr
bryantaylor 0:eafc3fd41f75 1516 * Pointer to the object of a class defining the member callback
bryantaylor 0:eafc3fd41f75 1517 * function (@p mptr).
bryantaylor 0:eafc3fd41f75 1518 * @param[in] mptr
bryantaylor 0:eafc3fd41f75 1519 * The member callback (within the context of an object) to be
bryantaylor 0:eafc3fd41f75 1520 * invoked.
bryantaylor 0:eafc3fd41f75 1521 */
bryantaylor 0:eafc3fd41f75 1522 template<typename T>
bryantaylor 0:eafc3fd41f75 1523 void onConnection(T *tptr, void (T::*mptr)(const ConnectionCallbackParams_t*)) {
bryantaylor 0:eafc3fd41f75 1524 connectionCallChain.add(tptr, mptr);
bryantaylor 0:eafc3fd41f75 1525 }
bryantaylor 0:eafc3fd41f75 1526
bryantaylor 0:eafc3fd41f75 1527 /**
bryantaylor 0:eafc3fd41f75 1528 * @brief Provide access to the callchain of connection event callbacks.
bryantaylor 0:eafc3fd41f75 1529 *
bryantaylor 0:eafc3fd41f75 1530 * @return A reference to the connection event callbacks chain.
bryantaylor 0:eafc3fd41f75 1531 *
bryantaylor 0:eafc3fd41f75 1532 * @note It is possible to register callbacks using onConnection().add(callback).
bryantaylor 0:eafc3fd41f75 1533 *
bryantaylor 0:eafc3fd41f75 1534 * @note It is possible to unregister callbacks using onConnection().detach(callback).
bryantaylor 0:eafc3fd41f75 1535 */
bryantaylor 0:eafc3fd41f75 1536 ConnectionEventCallbackChain_t& onConnection() {
bryantaylor 0:eafc3fd41f75 1537 return connectionCallChain;
bryantaylor 0:eafc3fd41f75 1538 }
bryantaylor 0:eafc3fd41f75 1539
bryantaylor 0:eafc3fd41f75 1540 /**
bryantaylor 0:eafc3fd41f75 1541 * Append to a chain of callbacks to be invoked upon GAP disconnection.
bryantaylor 0:eafc3fd41f75 1542 *
bryantaylor 0:eafc3fd41f75 1543 * @param[in] callback
bryantaylor 0:eafc3fd41f75 1544 Event handler being registered.
bryantaylor 0:eafc3fd41f75 1545 *
bryantaylor 0:eafc3fd41f75 1546 * @note It is possible to unregister callbacks using onDisconnection().detach(callback).
bryantaylor 0:eafc3fd41f75 1547 */
bryantaylor 0:eafc3fd41f75 1548 void onDisconnection(DisconnectionEventCallback_t callback) {
bryantaylor 0:eafc3fd41f75 1549 disconnectionCallChain.add(callback);
bryantaylor 0:eafc3fd41f75 1550 }
bryantaylor 0:eafc3fd41f75 1551
bryantaylor 0:eafc3fd41f75 1552 /**
bryantaylor 0:eafc3fd41f75 1553 * Same as Gap::onDisconnection(), but allows the possibility to add an object
bryantaylor 0:eafc3fd41f75 1554 * reference and member function as handler for disconnection event
bryantaylor 0:eafc3fd41f75 1555 * callbacks.
bryantaylor 0:eafc3fd41f75 1556 *
bryantaylor 0:eafc3fd41f75 1557 * @param[in] tptr
bryantaylor 0:eafc3fd41f75 1558 * Pointer to the object of a class defining the member callback
bryantaylor 0:eafc3fd41f75 1559 * function (@p mptr).
bryantaylor 0:eafc3fd41f75 1560 * @param[in] mptr
bryantaylor 0:eafc3fd41f75 1561 * The member callback (within the context of an object) to be
bryantaylor 0:eafc3fd41f75 1562 * invoked.
bryantaylor 0:eafc3fd41f75 1563 */
bryantaylor 0:eafc3fd41f75 1564 template<typename T>
bryantaylor 0:eafc3fd41f75 1565 void onDisconnection(T *tptr, void (T::*mptr)(const DisconnectionCallbackParams_t*)) {
bryantaylor 0:eafc3fd41f75 1566 disconnectionCallChain.add(tptr, mptr);
bryantaylor 0:eafc3fd41f75 1567 }
bryantaylor 0:eafc3fd41f75 1568
bryantaylor 0:eafc3fd41f75 1569 /**
bryantaylor 0:eafc3fd41f75 1570 * @brief Provide access to the callchain of disconnection event callbacks.
bryantaylor 0:eafc3fd41f75 1571 *
bryantaylor 0:eafc3fd41f75 1572 * @return A reference to the disconnection event callback chain.
bryantaylor 0:eafc3fd41f75 1573 *
bryantaylor 0:eafc3fd41f75 1574 * @note It is possible to register callbacks using onDisconnection().add(callback).
bryantaylor 0:eafc3fd41f75 1575 *
bryantaylor 0:eafc3fd41f75 1576 * @note It is possible to unregister callbacks using onDisconnection().detach(callback).
bryantaylor 0:eafc3fd41f75 1577 */
bryantaylor 0:eafc3fd41f75 1578 DisconnectionEventCallbackChain_t& onDisconnection() {
bryantaylor 0:eafc3fd41f75 1579 return disconnectionCallChain;
bryantaylor 0:eafc3fd41f75 1580 }
bryantaylor 0:eafc3fd41f75 1581
bryantaylor 0:eafc3fd41f75 1582 /**
bryantaylor 0:eafc3fd41f75 1583 * Set the application callback for radio-notification events.
bryantaylor 0:eafc3fd41f75 1584 *
bryantaylor 0:eafc3fd41f75 1585 * Radio Notification is a feature that enables ACTIVE and INACTIVE
bryantaylor 0:eafc3fd41f75 1586 * (nACTIVE) signals from the stack that notify the application when the
bryantaylor 0:eafc3fd41f75 1587 * radio is in use.
bryantaylor 0:eafc3fd41f75 1588 *
bryantaylor 0:eafc3fd41f75 1589 * The ACTIVE signal is sent before the radio event starts. The nACTIVE
bryantaylor 0:eafc3fd41f75 1590 * signal is sent at the end of the radio event. These signals can be used
bryantaylor 0:eafc3fd41f75 1591 * by the application programmer to synchronize application logic with radio
bryantaylor 0:eafc3fd41f75 1592 * activity. For example, the ACTIVE signal can be used to shut off external
bryantaylor 0:eafc3fd41f75 1593 * devices, to manage peak current drawn during periods when the radio is on,
bryantaylor 0:eafc3fd41f75 1594 * or to trigger sensor data collection for transmission in the Radio Event.
bryantaylor 0:eafc3fd41f75 1595 *
bryantaylor 0:eafc3fd41f75 1596 * @param[in] callback
bryantaylor 0:eafc3fd41f75 1597 * The application handler to be invoked in response to a radio
bryantaylor 0:eafc3fd41f75 1598 * ACTIVE/INACTIVE event.
bryantaylor 0:eafc3fd41f75 1599 */
bryantaylor 0:eafc3fd41f75 1600 void onRadioNotification(void (*callback)(bool param)) {
bryantaylor 0:eafc3fd41f75 1601 radioNotificationCallback.attach(callback);
bryantaylor 0:eafc3fd41f75 1602 }
bryantaylor 0:eafc3fd41f75 1603
bryantaylor 0:eafc3fd41f75 1604 /**
bryantaylor 0:eafc3fd41f75 1605 * Same as Gap::onRadioNotification(), but allows the posibility to
bryantaylor 0:eafc3fd41f75 1606 * register an object reference and member function as handler for radio
bryantaylor 0:eafc3fd41f75 1607 * notification events.
bryantaylor 0:eafc3fd41f75 1608 *
bryantaylor 0:eafc3fd41f75 1609 * @param[in] tptr
bryantaylor 0:eafc3fd41f75 1610 * Pointer to the object of a class defining the member callback
bryantaylor 0:eafc3fd41f75 1611 * function (@p mptr).
bryantaylor 0:eafc3fd41f75 1612 * @param[in] mptr
bryantaylor 0:eafc3fd41f75 1613 * The member callback (within the context of an object) to be
bryantaylor 0:eafc3fd41f75 1614 * invoked in response to a radio ACTIVE/INACTIVE event.
bryantaylor 0:eafc3fd41f75 1615 */
bryantaylor 0:eafc3fd41f75 1616 template <typename T>
bryantaylor 0:eafc3fd41f75 1617 void onRadioNotification(T *tptr, void (T::*mptr)(bool)) {
bryantaylor 0:eafc3fd41f75 1618 radioNotificationCallback.attach(tptr, mptr);
bryantaylor 0:eafc3fd41f75 1619 }
bryantaylor 0:eafc3fd41f75 1620
bryantaylor 0:eafc3fd41f75 1621 /**
bryantaylor 0:eafc3fd41f75 1622 * Setup a callback to be invoked to notify the user application that the
bryantaylor 0:eafc3fd41f75 1623 * Gap instance is about to shutdown (possibly as a result of a call
bryantaylor 0:eafc3fd41f75 1624 * to BLE::shutdown()).
bryantaylor 0:eafc3fd41f75 1625 *
bryantaylor 0:eafc3fd41f75 1626 * @param[in] callback
bryantaylor 0:eafc3fd41f75 1627 * The handler that is being registered to be notified of
bryantaylor 0:eafc3fd41f75 1628 * shutdown events.
bryantaylor 0:eafc3fd41f75 1629 *
bryantaylor 0:eafc3fd41f75 1630 * @note It is possible to chain together multiple onShutdown callbacks
bryantaylor 0:eafc3fd41f75 1631 * (potentially from different modules of an application) to be notified
bryantaylor 0:eafc3fd41f75 1632 * before the Gap instance is shutdown.
bryantaylor 0:eafc3fd41f75 1633 *
bryantaylor 0:eafc3fd41f75 1634 * @note It is also possible to set up a callback into a member function of
bryantaylor 0:eafc3fd41f75 1635 * some object.
bryantaylor 0:eafc3fd41f75 1636 *
bryantaylor 0:eafc3fd41f75 1637 * @note It is possible to unregister a callback using onShutdown().detach(callback)
bryantaylor 0:eafc3fd41f75 1638 */
bryantaylor 0:eafc3fd41f75 1639 void onShutdown(const GapShutdownCallback_t& callback) {
bryantaylor 0:eafc3fd41f75 1640 shutdownCallChain.add(callback);
bryantaylor 0:eafc3fd41f75 1641 }
bryantaylor 0:eafc3fd41f75 1642
bryantaylor 0:eafc3fd41f75 1643 /**
bryantaylor 0:eafc3fd41f75 1644 * Same as Gap::onShutdown(), but allows the posibility to
bryantaylor 0:eafc3fd41f75 1645 * register an object reference and member function as handler for shutdown
bryantaylor 0:eafc3fd41f75 1646 * events.
bryantaylor 0:eafc3fd41f75 1647 *
bryantaylor 0:eafc3fd41f75 1648 * @param[in] objPtr
bryantaylor 0:eafc3fd41f75 1649 * Pointer to the object of a class defining the member callback
bryantaylor 0:eafc3fd41f75 1650 * function (@p memberPtr).
bryantaylor 0:eafc3fd41f75 1651 * @param[in] memberPtr
bryantaylor 0:eafc3fd41f75 1652 * The member callback (within the context of an object) to be
bryantaylor 0:eafc3fd41f75 1653 * invoked in response to a shutdown event.
bryantaylor 0:eafc3fd41f75 1654 */
bryantaylor 0:eafc3fd41f75 1655 template <typename T>
bryantaylor 0:eafc3fd41f75 1656 void onShutdown(T *objPtr, void (T::*memberPtr)(const Gap *)) {
bryantaylor 0:eafc3fd41f75 1657 shutdownCallChain.add(objPtr, memberPtr);
bryantaylor 0:eafc3fd41f75 1658 }
bryantaylor 0:eafc3fd41f75 1659
bryantaylor 0:eafc3fd41f75 1660 /**
bryantaylor 0:eafc3fd41f75 1661 * @brief Provide access to the callchain of shutdown event callbacks.
bryantaylor 0:eafc3fd41f75 1662 *
bryantaylor 0:eafc3fd41f75 1663 * @return A reference to the shutdown event callback chain.
bryantaylor 0:eafc3fd41f75 1664 *
bryantaylor 0:eafc3fd41f75 1665 * @note It is possible to register callbacks using onShutdown().add(callback).
bryantaylor 0:eafc3fd41f75 1666 *
bryantaylor 0:eafc3fd41f75 1667 * @note It is possible to unregister callbacks using onShutdown().detach(callback).
bryantaylor 0:eafc3fd41f75 1668 */
bryantaylor 0:eafc3fd41f75 1669 GapShutdownCallbackChain_t& onShutdown() {
bryantaylor 0:eafc3fd41f75 1670 return shutdownCallChain;
bryantaylor 0:eafc3fd41f75 1671 }
bryantaylor 0:eafc3fd41f75 1672
bryantaylor 0:eafc3fd41f75 1673 public:
bryantaylor 0:eafc3fd41f75 1674 /**
bryantaylor 0:eafc3fd41f75 1675 * Notify all registered onShutdown callbacks that the Gap instance is
bryantaylor 0:eafc3fd41f75 1676 * about to be shutdown and clear all Gap state of the
bryantaylor 0:eafc3fd41f75 1677 * associated object.
bryantaylor 0:eafc3fd41f75 1678 *
bryantaylor 0:eafc3fd41f75 1679 * This function is meant to be overridden in the platform-specific
bryantaylor 0:eafc3fd41f75 1680 * sub-class. Nevertheless, the sub-class is only expected to reset its
bryantaylor 0:eafc3fd41f75 1681 * state and not the data held in Gap members. This shall be achieved by a
bryantaylor 0:eafc3fd41f75 1682 * call to Gap::reset() from the sub-class' reset() implementation.
bryantaylor 0:eafc3fd41f75 1683 *
bryantaylor 0:eafc3fd41f75 1684 * @return BLE_ERROR_NONE on success.
bryantaylor 0:eafc3fd41f75 1685 *
bryantaylor 0:eafc3fd41f75 1686 * @note Currently a call to reset() does not reset the advertising and
bryantaylor 0:eafc3fd41f75 1687 * scan parameters to default values.
bryantaylor 0:eafc3fd41f75 1688 */
bryantaylor 0:eafc3fd41f75 1689 virtual ble_error_t reset(void) {
bryantaylor 0:eafc3fd41f75 1690 /* Notify that the instance is about to shutdown */
bryantaylor 0:eafc3fd41f75 1691 shutdownCallChain.call(this);
bryantaylor 0:eafc3fd41f75 1692 shutdownCallChain.clear();
bryantaylor 0:eafc3fd41f75 1693
bryantaylor 0:eafc3fd41f75 1694 /* Clear Gap state */
bryantaylor 0:eafc3fd41f75 1695 state.advertising = 0;
bryantaylor 0:eafc3fd41f75 1696 state.connected = 0;
bryantaylor 0:eafc3fd41f75 1697 connectionCount = 0;
bryantaylor 0:eafc3fd41f75 1698
bryantaylor 0:eafc3fd41f75 1699 /* Clear scanning state */
bryantaylor 0:eafc3fd41f75 1700 scanningActive = false;
bryantaylor 0:eafc3fd41f75 1701
bryantaylor 0:eafc3fd41f75 1702 /* Clear advertising and scanning data */
bryantaylor 0:eafc3fd41f75 1703 _advPayload.clear();
bryantaylor 0:eafc3fd41f75 1704 _scanResponse.clear();
bryantaylor 0:eafc3fd41f75 1705
bryantaylor 0:eafc3fd41f75 1706 /* Clear callbacks */
bryantaylor 0:eafc3fd41f75 1707 timeoutCallbackChain.clear();
bryantaylor 0:eafc3fd41f75 1708 connectionCallChain.clear();
bryantaylor 0:eafc3fd41f75 1709 disconnectionCallChain.clear();
bryantaylor 0:eafc3fd41f75 1710 radioNotificationCallback = NULL;
bryantaylor 0:eafc3fd41f75 1711 onAdvertisementReport = NULL;
bryantaylor 0:eafc3fd41f75 1712
bryantaylor 0:eafc3fd41f75 1713 return BLE_ERROR_NONE;
bryantaylor 0:eafc3fd41f75 1714 }
bryantaylor 0:eafc3fd41f75 1715
bryantaylor 0:eafc3fd41f75 1716 protected:
bryantaylor 0:eafc3fd41f75 1717 /**
bryantaylor 0:eafc3fd41f75 1718 * Construct a Gap instance.
bryantaylor 0:eafc3fd41f75 1719 */
bryantaylor 0:eafc3fd41f75 1720 Gap() :
bryantaylor 0:eafc3fd41f75 1721 _advParams(),
bryantaylor 0:eafc3fd41f75 1722 _advPayload(),
bryantaylor 0:eafc3fd41f75 1723 _scanningParams(),
bryantaylor 0:eafc3fd41f75 1724 _scanResponse(),
bryantaylor 0:eafc3fd41f75 1725 connectionCount(0),
bryantaylor 0:eafc3fd41f75 1726 state(),
bryantaylor 0:eafc3fd41f75 1727 scanningActive(false),
bryantaylor 0:eafc3fd41f75 1728 timeoutCallbackChain(),
bryantaylor 0:eafc3fd41f75 1729 radioNotificationCallback(),
bryantaylor 0:eafc3fd41f75 1730 onAdvertisementReport(),
bryantaylor 0:eafc3fd41f75 1731 connectionCallChain(),
bryantaylor 0:eafc3fd41f75 1732 disconnectionCallChain() {
bryantaylor 0:eafc3fd41f75 1733 _advPayload.clear();
bryantaylor 0:eafc3fd41f75 1734 _scanResponse.clear();
bryantaylor 0:eafc3fd41f75 1735 }
bryantaylor 0:eafc3fd41f75 1736
bryantaylor 0:eafc3fd41f75 1737 /* Entry points for the underlying stack to report events back to the user. */
bryantaylor 0:eafc3fd41f75 1738 public:
bryantaylor 0:eafc3fd41f75 1739 /**
bryantaylor 0:eafc3fd41f75 1740 * Helper function that notifies all registered handlers of an occurrence
bryantaylor 0:eafc3fd41f75 1741 * of a connection event. This function is meant to be called from the
bryantaylor 0:eafc3fd41f75 1742 * BLE stack specific implementation when a connection event occurs.
bryantaylor 0:eafc3fd41f75 1743 *
bryantaylor 0:eafc3fd41f75 1744 * @param[in] handle
bryantaylor 0:eafc3fd41f75 1745 * The ID of the connection that generated the event.
bryantaylor 0:eafc3fd41f75 1746 * @param[in] role
bryantaylor 0:eafc3fd41f75 1747 * The role of this BLE device in the connection.
bryantaylor 0:eafc3fd41f75 1748 * @param[in] peerAddrType
bryantaylor 0:eafc3fd41f75 1749 * The peer's BLE address type.
bryantaylor 0:eafc3fd41f75 1750 * @param[in] peerAddr
bryantaylor 0:eafc3fd41f75 1751 * The peer's BLE address.
bryantaylor 0:eafc3fd41f75 1752 * @param[in] ownAddrType
bryantaylor 0:eafc3fd41f75 1753 * This device's BLE address type.
bryantaylor 0:eafc3fd41f75 1754 * @param[in] ownAddr
bryantaylor 0:eafc3fd41f75 1755 * This device's BLE address.
bryantaylor 0:eafc3fd41f75 1756 * @param[in] connectionParams
bryantaylor 0:eafc3fd41f75 1757 * The parameters configured for this connection.
bryantaylor 0:eafc3fd41f75 1758 */
bryantaylor 0:eafc3fd41f75 1759 void processConnectionEvent(Handle_t handle,
bryantaylor 0:eafc3fd41f75 1760 Role_t role,
bryantaylor 0:eafc3fd41f75 1761 BLEProtocol::AddressType_t peerAddrType,
bryantaylor 0:eafc3fd41f75 1762 const BLEProtocol::AddressBytes_t peerAddr,
bryantaylor 0:eafc3fd41f75 1763 BLEProtocol::AddressType_t ownAddrType,
bryantaylor 0:eafc3fd41f75 1764 const BLEProtocol::AddressBytes_t ownAddr,
bryantaylor 0:eafc3fd41f75 1765 const ConnectionParams_t *connectionParams) {
bryantaylor 0:eafc3fd41f75 1766 /* Update Gap state */
bryantaylor 0:eafc3fd41f75 1767 state.advertising = 0;
bryantaylor 0:eafc3fd41f75 1768 state.connected = 1;
bryantaylor 0:eafc3fd41f75 1769 ++connectionCount;
bryantaylor 0:eafc3fd41f75 1770
bryantaylor 0:eafc3fd41f75 1771 ConnectionCallbackParams_t callbackParams(handle, role, peerAddrType, peerAddr, ownAddrType, ownAddr, connectionParams);
bryantaylor 0:eafc3fd41f75 1772 connectionCallChain.call(&callbackParams);
bryantaylor 0:eafc3fd41f75 1773 }
bryantaylor 0:eafc3fd41f75 1774
bryantaylor 0:eafc3fd41f75 1775 /**
bryantaylor 0:eafc3fd41f75 1776 * Helper function that notifies all registered handlers of an occurrence
bryantaylor 0:eafc3fd41f75 1777 * of a disconnection event. This function is meant to be called from the
bryantaylor 0:eafc3fd41f75 1778 * BLE stack specific implementation when a disconnection event occurs.
bryantaylor 0:eafc3fd41f75 1779 *
bryantaylor 0:eafc3fd41f75 1780 * @param[in] handle
bryantaylor 0:eafc3fd41f75 1781 * The ID of the connection that generated the event.
bryantaylor 0:eafc3fd41f75 1782 * @param[in] reason
bryantaylor 0:eafc3fd41f75 1783 * The reason for disconnection.
bryantaylor 0:eafc3fd41f75 1784 */
bryantaylor 0:eafc3fd41f75 1785 void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) {
bryantaylor 0:eafc3fd41f75 1786 /* Update Gap state */
bryantaylor 0:eafc3fd41f75 1787 --connectionCount;
bryantaylor 0:eafc3fd41f75 1788 if (!connectionCount) {
bryantaylor 0:eafc3fd41f75 1789 state.connected = 0;
bryantaylor 0:eafc3fd41f75 1790 }
bryantaylor 0:eafc3fd41f75 1791
bryantaylor 0:eafc3fd41f75 1792 DisconnectionCallbackParams_t callbackParams(handle, reason);
bryantaylor 0:eafc3fd41f75 1793 disconnectionCallChain.call(&callbackParams);
bryantaylor 0:eafc3fd41f75 1794 }
bryantaylor 0:eafc3fd41f75 1795
bryantaylor 0:eafc3fd41f75 1796 /**
bryantaylor 0:eafc3fd41f75 1797 * Helper function that notifies the registered handler of a scanned
bryantaylor 0:eafc3fd41f75 1798 * advertisement packet. This function is meant to be called from the
bryantaylor 0:eafc3fd41f75 1799 * BLE stack specific implementation when a such event occurs.
bryantaylor 0:eafc3fd41f75 1800 *
bryantaylor 0:eafc3fd41f75 1801 * @param[in] peerAddr
bryantaylor 0:eafc3fd41f75 1802 * The peer's BLE address.
bryantaylor 0:eafc3fd41f75 1803 * @param[in] rssi
bryantaylor 0:eafc3fd41f75 1804 * The advertisement packet RSSI value.
bryantaylor 0:eafc3fd41f75 1805 * @param[in] isScanReponse
bryantaylor 0:eafc3fd41f75 1806 * Whether this packet is the response to a scan request.
bryantaylor 0:eafc3fd41f75 1807 * @param[in] type
bryantaylor 0:eafc3fd41f75 1808 * The type of advertisement.
bryantaylor 0:eafc3fd41f75 1809 * @param[in] advertisingDataLen
bryantaylor 0:eafc3fd41f75 1810 * Length of the advertisement data.
bryantaylor 0:eafc3fd41f75 1811 * @param[in] advertisingData
bryantaylor 0:eafc3fd41f75 1812 * Pointer to the advertisement packet's data.
bryantaylor 0:eafc3fd41f75 1813 */
bryantaylor 0:eafc3fd41f75 1814 void processAdvertisementReport(const BLEProtocol::AddressBytes_t peerAddr,
bryantaylor 0:eafc3fd41f75 1815 int8_t rssi,
bryantaylor 0:eafc3fd41f75 1816 bool isScanResponse,
bryantaylor 0:eafc3fd41f75 1817 GapAdvertisingParams::AdvertisingType_t type,
bryantaylor 0:eafc3fd41f75 1818 uint8_t advertisingDataLen,
bryantaylor 0:eafc3fd41f75 1819 const uint8_t *advertisingData) {
bryantaylor 0:eafc3fd41f75 1820 AdvertisementCallbackParams_t params;
bryantaylor 0:eafc3fd41f75 1821 memcpy(params.peerAddr, peerAddr, ADDR_LEN);
bryantaylor 0:eafc3fd41f75 1822 params.rssi = rssi;
bryantaylor 0:eafc3fd41f75 1823 params.isScanResponse = isScanResponse;
bryantaylor 0:eafc3fd41f75 1824 params.type = type;
bryantaylor 0:eafc3fd41f75 1825 params.advertisingDataLen = advertisingDataLen;
bryantaylor 0:eafc3fd41f75 1826 params.advertisingData = advertisingData;
bryantaylor 0:eafc3fd41f75 1827 onAdvertisementReport.call(&params);
bryantaylor 0:eafc3fd41f75 1828 }
bryantaylor 0:eafc3fd41f75 1829
bryantaylor 0:eafc3fd41f75 1830 /**
bryantaylor 0:eafc3fd41f75 1831 * Helper function that notifies all registered handlers of an occurrence
bryantaylor 0:eafc3fd41f75 1832 * of a timeout event. This function is meant to be called from the
bryantaylor 0:eafc3fd41f75 1833 * BLE stack specific implementation when a timeout event occurs.
bryantaylor 0:eafc3fd41f75 1834 *
bryantaylor 0:eafc3fd41f75 1835 * @param[in] source
bryantaylor 0:eafc3fd41f75 1836 * The source of the timout event.
bryantaylor 0:eafc3fd41f75 1837 */
bryantaylor 0:eafc3fd41f75 1838 void processTimeoutEvent(TimeoutSource_t source) {
bryantaylor 0:eafc3fd41f75 1839 if (source == TIMEOUT_SRC_ADVERTISING) {
bryantaylor 0:eafc3fd41f75 1840 /* Update gap state if the source is an advertising timeout */
bryantaylor 0:eafc3fd41f75 1841 state.advertising = 0;
bryantaylor 0:eafc3fd41f75 1842 }
bryantaylor 0:eafc3fd41f75 1843 if (timeoutCallbackChain) {
bryantaylor 0:eafc3fd41f75 1844 timeoutCallbackChain(source);
bryantaylor 0:eafc3fd41f75 1845 }
bryantaylor 0:eafc3fd41f75 1846 }
bryantaylor 0:eafc3fd41f75 1847
bryantaylor 0:eafc3fd41f75 1848 protected:
bryantaylor 0:eafc3fd41f75 1849 /**
bryantaylor 0:eafc3fd41f75 1850 * Currently set advertising parameters.
bryantaylor 0:eafc3fd41f75 1851 */
bryantaylor 0:eafc3fd41f75 1852 GapAdvertisingParams _advParams;
bryantaylor 0:eafc3fd41f75 1853 /**
bryantaylor 0:eafc3fd41f75 1854 * Currently set advertising data.
bryantaylor 0:eafc3fd41f75 1855 */
bryantaylor 0:eafc3fd41f75 1856 GapAdvertisingData _advPayload;
bryantaylor 0:eafc3fd41f75 1857 /**
bryantaylor 0:eafc3fd41f75 1858 * Currently set scanning parameters.
bryantaylor 0:eafc3fd41f75 1859 */
bryantaylor 0:eafc3fd41f75 1860 GapScanningParams _scanningParams;
bryantaylor 0:eafc3fd41f75 1861 /**
bryantaylor 0:eafc3fd41f75 1862 * Currently set scan response data.
bryantaylor 0:eafc3fd41f75 1863 */
bryantaylor 0:eafc3fd41f75 1864 GapAdvertisingData _scanResponse;
bryantaylor 0:eafc3fd41f75 1865
bryantaylor 0:eafc3fd41f75 1866 /**
bryantaylor 0:eafc3fd41f75 1867 * Total number of open connections.
bryantaylor 0:eafc3fd41f75 1868 */
bryantaylor 0:eafc3fd41f75 1869 uint8_t connectionCount;
bryantaylor 0:eafc3fd41f75 1870 /**
bryantaylor 0:eafc3fd41f75 1871 * The current GAP state.
bryantaylor 0:eafc3fd41f75 1872 */
bryantaylor 0:eafc3fd41f75 1873 GapState_t state;
bryantaylor 0:eafc3fd41f75 1874 /**
bryantaylor 0:eafc3fd41f75 1875 * Whether active scanning is set. This is used to fetch the scan response
bryantaylor 0:eafc3fd41f75 1876 * from a peer if possible.
bryantaylor 0:eafc3fd41f75 1877 */
bryantaylor 0:eafc3fd41f75 1878 bool scanningActive;
bryantaylor 0:eafc3fd41f75 1879
bryantaylor 0:eafc3fd41f75 1880 protected:
bryantaylor 0:eafc3fd41f75 1881 /**
bryantaylor 0:eafc3fd41f75 1882 * Callchain containing all registered callback handlers for timeout
bryantaylor 0:eafc3fd41f75 1883 * events.
bryantaylor 0:eafc3fd41f75 1884 */
bryantaylor 0:eafc3fd41f75 1885 TimeoutEventCallbackChain_t timeoutCallbackChain;
bryantaylor 0:eafc3fd41f75 1886 /**
bryantaylor 0:eafc3fd41f75 1887 * The registered callback handler for radio notification events.
bryantaylor 0:eafc3fd41f75 1888 */
bryantaylor 0:eafc3fd41f75 1889 RadioNotificationEventCallback_t radioNotificationCallback;
bryantaylor 0:eafc3fd41f75 1890 /**
bryantaylor 0:eafc3fd41f75 1891 * The registered callback handler for scanned advertisement packet
bryantaylor 0:eafc3fd41f75 1892 * notifications.
bryantaylor 0:eafc3fd41f75 1893 */
bryantaylor 0:eafc3fd41f75 1894 AdvertisementReportCallback_t onAdvertisementReport;
bryantaylor 0:eafc3fd41f75 1895 /**
bryantaylor 0:eafc3fd41f75 1896 * Callchain containing all registered callback handlers for connection
bryantaylor 0:eafc3fd41f75 1897 * events.
bryantaylor 0:eafc3fd41f75 1898 */
bryantaylor 0:eafc3fd41f75 1899 ConnectionEventCallbackChain_t connectionCallChain;
bryantaylor 0:eafc3fd41f75 1900 /**
bryantaylor 0:eafc3fd41f75 1901 * Callchain containing all registered callback handlers for disconnection
bryantaylor 0:eafc3fd41f75 1902 * events.
bryantaylor 0:eafc3fd41f75 1903 */
bryantaylor 0:eafc3fd41f75 1904 DisconnectionEventCallbackChain_t disconnectionCallChain;
bryantaylor 0:eafc3fd41f75 1905
bryantaylor 0:eafc3fd41f75 1906 private:
bryantaylor 0:eafc3fd41f75 1907 /**
bryantaylor 0:eafc3fd41f75 1908 * Callchain containing all registered callback handlers for shutdown
bryantaylor 0:eafc3fd41f75 1909 * events.
bryantaylor 0:eafc3fd41f75 1910 */
bryantaylor 0:eafc3fd41f75 1911 GapShutdownCallbackChain_t shutdownCallChain;
bryantaylor 0:eafc3fd41f75 1912
bryantaylor 0:eafc3fd41f75 1913 private:
bryantaylor 0:eafc3fd41f75 1914 /* Disallow copy and assignment. */
bryantaylor 0:eafc3fd41f75 1915 Gap(const Gap &);
bryantaylor 0:eafc3fd41f75 1916 Gap& operator=(const Gap &);
bryantaylor 0:eafc3fd41f75 1917 };
bryantaylor 0:eafc3fd41f75 1918
bryantaylor 0:eafc3fd41f75 1919 #endif // ifndef __GAP_H__