prova

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Wed Apr 06 19:13:52 2016 +0100
Revision:
1134:d540a48f650d
Parent:
1132:6362b7c2fdff
Child:
1135:22aada733dbd
Synchronized with git rev d363d7ee
Author: Rohit Grover
Merge branch 'develop'

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 1131:692ddf04fc42 1 /* mbed Microcontroller Library
vcoubard 1131:692ddf04fc42 2 * Copyright (c) 2006-2013 ARM Limited
vcoubard 1131:692ddf04fc42 3 *
vcoubard 1131:692ddf04fc42 4 * Licensed under the Apache License, Version 2.0 (the "License");
vcoubard 1131:692ddf04fc42 5 * you may not use this file except in compliance with the License.
vcoubard 1131:692ddf04fc42 6 * You may obtain a copy of the License at
vcoubard 1131:692ddf04fc42 7 *
vcoubard 1131:692ddf04fc42 8 * http://www.apache.org/licenses/LICENSE-2.0
vcoubard 1131:692ddf04fc42 9 *
vcoubard 1131:692ddf04fc42 10 * Unless required by applicable law or agreed to in writing, software
vcoubard 1131:692ddf04fc42 11 * distributed under the License is distributed on an "AS IS" BASIS,
vcoubard 1131:692ddf04fc42 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vcoubard 1131:692ddf04fc42 13 * See the License for the specific language governing permissions and
vcoubard 1131:692ddf04fc42 14 * limitations under the License.
vcoubard 1131:692ddf04fc42 15 */
vcoubard 1131:692ddf04fc42 16
vcoubard 1131:692ddf04fc42 17 #ifndef __GAP_H__
vcoubard 1131:692ddf04fc42 18 #define __GAP_H__
vcoubard 1131:692ddf04fc42 19
vcoubard 1131:692ddf04fc42 20 #include "GapAdvertisingData.h"
vcoubard 1131:692ddf04fc42 21 #include "GapAdvertisingParams.h"
vcoubard 1131:692ddf04fc42 22 #include "GapScanningParams.h"
vcoubard 1131:692ddf04fc42 23 #include "GapEvents.h"
vcoubard 1131:692ddf04fc42 24 #include "CallChainOfFunctionPointersWithContext.h"
vcoubard 1131:692ddf04fc42 25 #include "FunctionPointerWithContext.h"
vcoubard 1131:692ddf04fc42 26
vcoubard 1131:692ddf04fc42 27 /* Forward declarations for classes that will only be used for pointers or references in the following. */
vcoubard 1131:692ddf04fc42 28 class GapAdvertisingParams;
vcoubard 1131:692ddf04fc42 29 class GapScanningParams;
vcoubard 1131:692ddf04fc42 30 class GapAdvertisingData;
vcoubard 1131:692ddf04fc42 31
vcoubard 1131:692ddf04fc42 32 class Gap {
vcoubard 1131:692ddf04fc42 33 public:
vcoubard 1131:692ddf04fc42 34 enum AddressType_t {
vcoubard 1131:692ddf04fc42 35 ADDR_TYPE_PUBLIC = 0,
vcoubard 1131:692ddf04fc42 36 ADDR_TYPE_RANDOM_STATIC,
vcoubard 1131:692ddf04fc42 37 ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE,
vcoubard 1131:692ddf04fc42 38 ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE
vcoubard 1131:692ddf04fc42 39 };
vcoubard 1131:692ddf04fc42 40 typedef enum AddressType_t addr_type_t; /* @Note: Deprecated. Use AddressType_t instead. */
vcoubard 1131:692ddf04fc42 41
vcoubard 1131:692ddf04fc42 42 static const unsigned ADDR_LEN = 6;
vcoubard 1131:692ddf04fc42 43 typedef uint8_t Address_t[ADDR_LEN]; /* 48-bit address, LSB format. */
vcoubard 1131:692ddf04fc42 44 typedef Address_t address_t; /* @Note: Deprecated. Use Address_t instead. */
vcoubard 1131:692ddf04fc42 45
vcoubard 1131:692ddf04fc42 46 enum TimeoutSource_t {
vcoubard 1131:692ddf04fc42 47 TIMEOUT_SRC_ADVERTISING = 0x00, /**< Advertising timeout. */
vcoubard 1131:692ddf04fc42 48 TIMEOUT_SRC_SECURITY_REQUEST = 0x01, /**< Security request timeout. */
vcoubard 1131:692ddf04fc42 49 TIMEOUT_SRC_SCAN = 0x02, /**< Scanning timeout. */
vcoubard 1131:692ddf04fc42 50 TIMEOUT_SRC_CONN = 0x03, /**< Connection timeout. */
vcoubard 1131:692ddf04fc42 51 };
vcoubard 1131:692ddf04fc42 52
vcoubard 1131:692ddf04fc42 53 /**
vcoubard 1131:692ddf04fc42 54 * Enumeration for disconnection reasons. The values for these reasons are
vcoubard 1131:692ddf04fc42 55 * derived from Nordic's implementation, but the reasons are meant to be
vcoubard 1131:692ddf04fc42 56 * independent of the transport. If you are returned a reason that is not
vcoubard 1131:692ddf04fc42 57 * covered by this enumeration, please refer to the underlying
vcoubard 1131:692ddf04fc42 58 * transport library.
vcoubard 1131:692ddf04fc42 59 */
vcoubard 1131:692ddf04fc42 60 enum DisconnectionReason_t {
vcoubard 1131:692ddf04fc42 61 CONNECTION_TIMEOUT = 0x08,
vcoubard 1131:692ddf04fc42 62 REMOTE_USER_TERMINATED_CONNECTION = 0x13,
vcoubard 1131:692ddf04fc42 63 REMOTE_DEV_TERMINATION_DUE_TO_LOW_RESOURCES = 0x14, /**< Remote device terminated connection due to low resources.*/
vcoubard 1131:692ddf04fc42 64 REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF = 0x15, /**< Remote device terminated connection due to power off. */
vcoubard 1131:692ddf04fc42 65 LOCAL_HOST_TERMINATED_CONNECTION = 0x16,
vcoubard 1131:692ddf04fc42 66 CONN_INTERVAL_UNACCEPTABLE = 0x3B,
vcoubard 1131:692ddf04fc42 67 };
vcoubard 1131:692ddf04fc42 68
vcoubard 1131:692ddf04fc42 69 /* Describes the current state of the device (more than one bit can be set). */
vcoubard 1131:692ddf04fc42 70 struct GapState_t {
vcoubard 1131:692ddf04fc42 71 unsigned advertising : 1; /**< Peripheral is currently advertising. */
vcoubard 1131:692ddf04fc42 72 unsigned connected : 1; /**< Peripheral is connected to a central. */
vcoubard 1131:692ddf04fc42 73 };
vcoubard 1131:692ddf04fc42 74
vcoubard 1131:692ddf04fc42 75 typedef uint16_t Handle_t; /* Type for connection handle. */
vcoubard 1131:692ddf04fc42 76
vcoubard 1131:692ddf04fc42 77 typedef struct {
vcoubard 1131:692ddf04fc42 78 uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
vcoubard 1131:692ddf04fc42 79 uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
vcoubard 1131:692ddf04fc42 80 uint16_t slaveLatency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/
vcoubard 1131:692ddf04fc42 81 uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
vcoubard 1131:692ddf04fc42 82 } ConnectionParams_t;
vcoubard 1131:692ddf04fc42 83
vcoubard 1131:692ddf04fc42 84 enum Role_t {
vcoubard 1131:692ddf04fc42 85 PERIPHERAL = 0x1, /**< Peripheral Role. */
vcoubard 1131:692ddf04fc42 86 CENTRAL = 0x2, /**< Central Role. */
vcoubard 1131:692ddf04fc42 87 };
vcoubard 1131:692ddf04fc42 88
vcoubard 1131:692ddf04fc42 89 struct AdvertisementCallbackParams_t {
vcoubard 1131:692ddf04fc42 90 Address_t peerAddr;
vcoubard 1131:692ddf04fc42 91 int8_t rssi;
vcoubard 1131:692ddf04fc42 92 bool isScanResponse;
vcoubard 1131:692ddf04fc42 93 GapAdvertisingParams::AdvertisingType_t type;
vcoubard 1131:692ddf04fc42 94 uint8_t advertisingDataLen;
vcoubard 1131:692ddf04fc42 95 const uint8_t *advertisingData;
vcoubard 1131:692ddf04fc42 96 };
vcoubard 1131:692ddf04fc42 97 typedef FunctionPointerWithContext<const AdvertisementCallbackParams_t *> AdvertisementReportCallback_t;
vcoubard 1131:692ddf04fc42 98
vcoubard 1131:692ddf04fc42 99 struct ConnectionCallbackParams_t {
vcoubard 1131:692ddf04fc42 100 Handle_t handle;
vcoubard 1131:692ddf04fc42 101 Role_t role;
vcoubard 1131:692ddf04fc42 102 AddressType_t peerAddrType;
vcoubard 1131:692ddf04fc42 103 Address_t peerAddr;
vcoubard 1131:692ddf04fc42 104 AddressType_t ownAddrType;
vcoubard 1131:692ddf04fc42 105 Address_t ownAddr;
vcoubard 1131:692ddf04fc42 106 const ConnectionParams_t *connectionParams;
vcoubard 1131:692ddf04fc42 107
vcoubard 1131:692ddf04fc42 108 ConnectionCallbackParams_t(Handle_t handleIn,
vcoubard 1131:692ddf04fc42 109 Role_t roleIn,
vcoubard 1131:692ddf04fc42 110 AddressType_t peerAddrTypeIn,
vcoubard 1131:692ddf04fc42 111 const uint8_t *peerAddrIn,
vcoubard 1131:692ddf04fc42 112 AddressType_t ownAddrTypeIn,
vcoubard 1131:692ddf04fc42 113 const uint8_t *ownAddrIn,
vcoubard 1131:692ddf04fc42 114 const ConnectionParams_t *connectionParamsIn) :
vcoubard 1131:692ddf04fc42 115 handle(handleIn),
vcoubard 1131:692ddf04fc42 116 role(roleIn),
vcoubard 1131:692ddf04fc42 117 peerAddrType(peerAddrTypeIn),
vcoubard 1131:692ddf04fc42 118 peerAddr(),
vcoubard 1131:692ddf04fc42 119 ownAddrType(ownAddrTypeIn),
vcoubard 1131:692ddf04fc42 120 ownAddr(),
vcoubard 1131:692ddf04fc42 121 connectionParams(connectionParamsIn) {
vcoubard 1131:692ddf04fc42 122 memcpy(peerAddr, peerAddrIn, ADDR_LEN);
vcoubard 1131:692ddf04fc42 123 memcpy(ownAddr, ownAddrIn, ADDR_LEN);
vcoubard 1131:692ddf04fc42 124 }
vcoubard 1131:692ddf04fc42 125 };
vcoubard 1131:692ddf04fc42 126
vcoubard 1131:692ddf04fc42 127 struct DisconnectionCallbackParams_t {
vcoubard 1131:692ddf04fc42 128 Handle_t handle;
vcoubard 1131:692ddf04fc42 129 DisconnectionReason_t reason;
vcoubard 1131:692ddf04fc42 130
vcoubard 1131:692ddf04fc42 131 DisconnectionCallbackParams_t(Handle_t handleIn,
vcoubard 1131:692ddf04fc42 132 DisconnectionReason_t reasonIn) :
vcoubard 1131:692ddf04fc42 133 handle(handleIn),
vcoubard 1131:692ddf04fc42 134 reason(reasonIn)
vcoubard 1131:692ddf04fc42 135 {}
vcoubard 1131:692ddf04fc42 136 };
vcoubard 1131:692ddf04fc42 137
vcoubard 1131:692ddf04fc42 138 static const uint16_t UNIT_1_25_MS = 1250; /**< Number of microseconds in 1.25 milliseconds. */
vcoubard 1131:692ddf04fc42 139 static uint16_t MSEC_TO_GAP_DURATION_UNITS(uint32_t durationInMillis) {
vcoubard 1131:692ddf04fc42 140 return (durationInMillis * 1000) / UNIT_1_25_MS;
vcoubard 1131:692ddf04fc42 141 }
vcoubard 1131:692ddf04fc42 142
vcoubard 1131:692ddf04fc42 143 typedef FunctionPointerWithContext<TimeoutSource_t> TimeoutEventCallback_t;
vcoubard 1131:692ddf04fc42 144 typedef CallChainOfFunctionPointersWithContext<TimeoutSource_t> TimeoutEventCallbackChain_t;
vcoubard 1131:692ddf04fc42 145
vcoubard 1131:692ddf04fc42 146 typedef FunctionPointerWithContext<const ConnectionCallbackParams_t *> ConnectionEventCallback_t;
vcoubard 1131:692ddf04fc42 147 typedef CallChainOfFunctionPointersWithContext<const ConnectionCallbackParams_t *> ConnectionEventCallbackChain_t;
vcoubard 1131:692ddf04fc42 148
vcoubard 1131:692ddf04fc42 149 typedef FunctionPointerWithContext<const DisconnectionCallbackParams_t*> DisconnectionEventCallback_t;
vcoubard 1132:6362b7c2fdff 150 typedef CallChainOfFunctionPointersWithContext<const DisconnectionCallbackParams_t*> DisconnectionEventCallbackChain_t;
vcoubard 1131:692ddf04fc42 151
vcoubard 1131:692ddf04fc42 152 typedef FunctionPointerWithContext<bool> RadioNotificationEventCallback_t;
vcoubard 1131:692ddf04fc42 153
vcoubard 1131:692ddf04fc42 154 /*
vcoubard 1131:692ddf04fc42 155 * The following functions are meant to be overridden in the platform-specific sub-class.
vcoubard 1131:692ddf04fc42 156 */
vcoubard 1131:692ddf04fc42 157 public:
vcoubard 1131:692ddf04fc42 158 /**
vcoubard 1131:692ddf04fc42 159 * Set the BTLE MAC address and type. Please note that the address format is
vcoubard 1131:692ddf04fc42 160 * least significant byte first (LSB). Please refer to Address_t.
vcoubard 1131:692ddf04fc42 161 *
vcoubard 1131:692ddf04fc42 162 * @return BLE_ERROR_NONE on success.
vcoubard 1131:692ddf04fc42 163 */
vcoubard 1131:692ddf04fc42 164 virtual ble_error_t setAddress(AddressType_t type, const Address_t address) {
vcoubard 1131:692ddf04fc42 165 /* avoid compiler warnings about unused variables */
vcoubard 1131:692ddf04fc42 166 (void)type;
vcoubard 1131:692ddf04fc42 167 (void)address;
vcoubard 1131:692ddf04fc42 168
vcoubard 1131:692ddf04fc42 169 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 170 }
vcoubard 1131:692ddf04fc42 171
vcoubard 1131:692ddf04fc42 172 /**
vcoubard 1131:692ddf04fc42 173 * Fetch the BTLE MAC address and type.
vcoubard 1131:692ddf04fc42 174 *
vcoubard 1131:692ddf04fc42 175 * @return BLE_ERROR_NONE on success.
vcoubard 1131:692ddf04fc42 176 */
vcoubard 1131:692ddf04fc42 177 virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address) {
vcoubard 1131:692ddf04fc42 178 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 179 (void)typeP;
vcoubard 1131:692ddf04fc42 180 (void)address;
vcoubard 1131:692ddf04fc42 181
vcoubard 1131:692ddf04fc42 182 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 183 }
vcoubard 1131:692ddf04fc42 184
vcoubard 1131:692ddf04fc42 185 /**
vcoubard 1134:d540a48f650d 186 * @return Minimum Advertising interval in milliseconds for connectable
vcoubard 1134:d540a48f650d 187 * undirected and connectable directed event types.
vcoubard 1131:692ddf04fc42 188 */
vcoubard 1131:692ddf04fc42 189 virtual uint16_t getMinAdvertisingInterval(void) const {
vcoubard 1131:692ddf04fc42 190 return 0; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 191 }
vcoubard 1131:692ddf04fc42 192
vcoubard 1131:692ddf04fc42 193 /**
vcoubard 1134:d540a48f650d 194 * @return Minimum Advertising interval in milliseconds for scannable
vcoubard 1134:d540a48f650d 195 * undirected and non-connectable undirected event types.
vcoubard 1131:692ddf04fc42 196 */
vcoubard 1131:692ddf04fc42 197 virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {
vcoubard 1131:692ddf04fc42 198 return 0; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 199 }
vcoubard 1131:692ddf04fc42 200
vcoubard 1131:692ddf04fc42 201 /**
vcoubard 1131:692ddf04fc42 202 * @return Maximum Advertising interval in milliseconds.
vcoubard 1131:692ddf04fc42 203 */
vcoubard 1131:692ddf04fc42 204 virtual uint16_t getMaxAdvertisingInterval(void) const {
vcoubard 1131:692ddf04fc42 205 return 0xFFFF; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 206 }
vcoubard 1131:692ddf04fc42 207
vcoubard 1131:692ddf04fc42 208 virtual ble_error_t stopAdvertising(void) {
vcoubard 1131:692ddf04fc42 209 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 210 }
vcoubard 1131:692ddf04fc42 211
vcoubard 1131:692ddf04fc42 212 /**
vcoubard 1131:692ddf04fc42 213 * Stop scanning. The current scanning parameters remain in effect.
vcoubard 1131:692ddf04fc42 214 *
vcoubard 1131:692ddf04fc42 215 * @retval BLE_ERROR_NONE if successfully stopped scanning procedure.
vcoubard 1131:692ddf04fc42 216 */
vcoubard 1131:692ddf04fc42 217 virtual ble_error_t stopScan() {
vcoubard 1131:692ddf04fc42 218 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 219 }
vcoubard 1131:692ddf04fc42 220
vcoubard 1131:692ddf04fc42 221 /**
vcoubard 1131:692ddf04fc42 222 * Create a connection (GAP Link Establishment).
vcoubard 1131:692ddf04fc42 223 *
vcoubard 1131:692ddf04fc42 224 * @param peerAddr
vcoubard 1131:692ddf04fc42 225 * 48-bit address, LSB format.
vcoubard 1131:692ddf04fc42 226 * @param peerAddrType
vcoubard 1131:692ddf04fc42 227 * Address type of the peer.
vcoubard 1131:692ddf04fc42 228 * @param connectionParams
vcoubard 1131:692ddf04fc42 229 * Connection parameters.
vcoubard 1131:692ddf04fc42 230 * @param scanParams
vcoubard 1131:692ddf04fc42 231 * Paramters to be used while scanning for the peer.
vcoubard 1131:692ddf04fc42 232 * @return BLE_ERROR_NONE if connection establishment procedure is started
vcoubard 1131:692ddf04fc42 233 * successfully. The connectionCallChain (if set) will be invoked upon
vcoubard 1131:692ddf04fc42 234 * a connection event.
vcoubard 1131:692ddf04fc42 235 */
vcoubard 1131:692ddf04fc42 236 virtual ble_error_t connect(const Address_t peerAddr,
vcoubard 1131:692ddf04fc42 237 Gap::AddressType_t peerAddrType,
vcoubard 1131:692ddf04fc42 238 const ConnectionParams_t *connectionParams,
vcoubard 1131:692ddf04fc42 239 const GapScanningParams *scanParams) {
vcoubard 1131:692ddf04fc42 240 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 241 (void)peerAddr;
vcoubard 1131:692ddf04fc42 242 (void)peerAddrType;
vcoubard 1131:692ddf04fc42 243 (void)connectionParams;
vcoubard 1131:692ddf04fc42 244 (void)scanParams;
vcoubard 1131:692ddf04fc42 245
vcoubard 1131:692ddf04fc42 246 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 247 }
vcoubard 1131:692ddf04fc42 248
vcoubard 1131:692ddf04fc42 249 /**
vcoubard 1131:692ddf04fc42 250 * This call initiates the disconnection procedure, and its completion will
vcoubard 1131:692ddf04fc42 251 * be communicated to the application with an invocation of the
vcoubard 1131:692ddf04fc42 252 * disconnectionCallback.
vcoubard 1131:692ddf04fc42 253 *
vcoubard 1131:692ddf04fc42 254 * @param reason
vcoubard 1131:692ddf04fc42 255 * The reason for disconnection; to be sent back to the peer.
vcoubard 1131:692ddf04fc42 256 */
vcoubard 1131:692ddf04fc42 257 virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason) {
vcoubard 1131:692ddf04fc42 258 /* avoid compiler warnings about unused variables */
vcoubard 1131:692ddf04fc42 259 (void)connectionHandle;
vcoubard 1131:692ddf04fc42 260 (void)reason;
vcoubard 1131:692ddf04fc42 261
vcoubard 1131:692ddf04fc42 262 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 263 }
vcoubard 1131:692ddf04fc42 264
vcoubard 1131:692ddf04fc42 265 /**
vcoubard 1131:692ddf04fc42 266 * This call initiates the disconnection procedure, and its completion will
vcoubard 1131:692ddf04fc42 267 * be communicated to the application with an invocation of the
vcoubard 1131:692ddf04fc42 268 * disconnectionCallback.
vcoubard 1131:692ddf04fc42 269 *
vcoubard 1131:692ddf04fc42 270 * @param reason
vcoubard 1131:692ddf04fc42 271 * The reason for disconnection; to be sent back to the peer.
vcoubard 1131:692ddf04fc42 272 *
vcoubard 1131:692ddf04fc42 273 * @note: This version of disconnect() doesn't take a connection handle. It
vcoubard 1131:692ddf04fc42 274 * works reliably only for stacks that are limited to a single
vcoubard 1131:692ddf04fc42 275 * connection. This API should be considered *deprecated* in favour of the
vcoubard 1131:692ddf04fc42 276 * alternative, which takes a connection handle. It will be dropped in the future.
vcoubard 1131:692ddf04fc42 277 */
vcoubard 1131:692ddf04fc42 278 virtual ble_error_t disconnect(DisconnectionReason_t reason) {
vcoubard 1131:692ddf04fc42 279 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 280 (void)reason;
vcoubard 1131:692ddf04fc42 281
vcoubard 1131:692ddf04fc42 282 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 283 }
vcoubard 1131:692ddf04fc42 284
vcoubard 1131:692ddf04fc42 285 /**
vcoubard 1131:692ddf04fc42 286 * Get the GAP peripheral preferred connection parameters. These are the
vcoubard 1131:692ddf04fc42 287 * defaults that the peripheral would like to have in a connection. The
vcoubard 1131:692ddf04fc42 288 * choice of the connection parameters is eventually up to the central.
vcoubard 1131:692ddf04fc42 289 *
vcoubard 1131:692ddf04fc42 290 * @param[out] params
vcoubard 1131:692ddf04fc42 291 * The structure where the parameters will be stored. Memory
vcoubard 1131:692ddf04fc42 292 * for this is owned by the caller.
vcoubard 1131:692ddf04fc42 293 *
vcoubard 1131:692ddf04fc42 294 * @return BLE_ERROR_NONE if the parameters were successfully filled into
vcoubard 1131:692ddf04fc42 295 * the given structure pointed to by params.
vcoubard 1131:692ddf04fc42 296 */
vcoubard 1131:692ddf04fc42 297 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) {
vcoubard 1131:692ddf04fc42 298 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 299 (void)params;
vcoubard 1131:692ddf04fc42 300
vcoubard 1131:692ddf04fc42 301 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 302 }
vcoubard 1131:692ddf04fc42 303
vcoubard 1131:692ddf04fc42 304 /**
vcoubard 1131:692ddf04fc42 305 * Set the GAP peripheral preferred connection parameters. These are the
vcoubard 1131:692ddf04fc42 306 * defaults that the peripheral would like to have in a connection. The
vcoubard 1131:692ddf04fc42 307 * choice of the connection parameters is eventually up to the central.
vcoubard 1131:692ddf04fc42 308 *
vcoubard 1131:692ddf04fc42 309 * @param[in] params
vcoubard 1131:692ddf04fc42 310 * The structure containing the desired parameters.
vcoubard 1131:692ddf04fc42 311 */
vcoubard 1131:692ddf04fc42 312 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) {
vcoubard 1131:692ddf04fc42 313 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 314 (void)params;
vcoubard 1131:692ddf04fc42 315
vcoubard 1131:692ddf04fc42 316 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 317 }
vcoubard 1131:692ddf04fc42 318
vcoubard 1131:692ddf04fc42 319 /**
vcoubard 1131:692ddf04fc42 320 * Update connection parameters.
vcoubard 1131:692ddf04fc42 321 * In the central role this will initiate a Link Layer connection parameter update procedure.
vcoubard 1131:692ddf04fc42 322 * In the peripheral role, this will send the corresponding L2CAP request and wait for
vcoubard 1131:692ddf04fc42 323 * the central to perform the procedure.
vcoubard 1131:692ddf04fc42 324 *
vcoubard 1131:692ddf04fc42 325 * @param[in] handle
vcoubard 1131:692ddf04fc42 326 * Connection Handle.
vcoubard 1131:692ddf04fc42 327 * @param[in] params
vcoubard 1131:692ddf04fc42 328 * Pointer to desired connection parameters. If NULL is provided on a peripheral role,
vcoubard 1131:692ddf04fc42 329 * the parameters in the PPCP characteristic of the GAP service will be used instead.
vcoubard 1131:692ddf04fc42 330 */
vcoubard 1131:692ddf04fc42 331 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) {
vcoubard 1131:692ddf04fc42 332 /* avoid compiler warnings about unused variables */
vcoubard 1131:692ddf04fc42 333 (void)handle;
vcoubard 1131:692ddf04fc42 334 (void)params;
vcoubard 1131:692ddf04fc42 335
vcoubard 1131:692ddf04fc42 336 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 337 }
vcoubard 1131:692ddf04fc42 338
vcoubard 1131:692ddf04fc42 339 /**
vcoubard 1131:692ddf04fc42 340 * Set the device name characteristic in the GAP service.
vcoubard 1131:692ddf04fc42 341 * @param[in] deviceName
vcoubard 1131:692ddf04fc42 342 * The new value for the device-name. This is a UTF-8 encoded, <b>NULL-terminated</b> string.
vcoubard 1131:692ddf04fc42 343 */
vcoubard 1131:692ddf04fc42 344 virtual ble_error_t setDeviceName(const uint8_t *deviceName) {
vcoubard 1131:692ddf04fc42 345 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 346 (void)deviceName;
vcoubard 1131:692ddf04fc42 347
vcoubard 1131:692ddf04fc42 348 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 349 }
vcoubard 1131:692ddf04fc42 350
vcoubard 1131:692ddf04fc42 351 /**
vcoubard 1131:692ddf04fc42 352 * Get the value of the device name characteristic in the GAP service.
vcoubard 1131:692ddf04fc42 353 * @param[out] deviceName
vcoubard 1131:692ddf04fc42 354 * Pointer to an empty buffer where the UTF-8 *non NULL-
vcoubard 1131:692ddf04fc42 355 * terminated* string will be placed. Set this
vcoubard 1131:692ddf04fc42 356 * value to NULL in order to obtain the deviceName-length
vcoubard 1131:692ddf04fc42 357 * from the 'length' parameter.
vcoubard 1131:692ddf04fc42 358 *
vcoubard 1131:692ddf04fc42 359 * @param[in/out] lengthP
vcoubard 1131:692ddf04fc42 360 * (on input) Length of the buffer pointed to by deviceName;
vcoubard 1131:692ddf04fc42 361 * (on output) the complete device name length (without the
vcoubard 1131:692ddf04fc42 362 * null terminator).
vcoubard 1131:692ddf04fc42 363 *
vcoubard 1131:692ddf04fc42 364 * @note If the device name is longer than the size of the supplied buffer,
vcoubard 1131:692ddf04fc42 365 * length will return the complete device name length, and not the
vcoubard 1131:692ddf04fc42 366 * number of bytes actually returned in deviceName. The application may
vcoubard 1131:692ddf04fc42 367 * use this information to retry with a suitable buffer size.
vcoubard 1131:692ddf04fc42 368 */
vcoubard 1131:692ddf04fc42 369 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) {
vcoubard 1131:692ddf04fc42 370 /* avoid compiler warnings about unused variables */
vcoubard 1131:692ddf04fc42 371 (void)deviceName;
vcoubard 1131:692ddf04fc42 372 (void)lengthP;
vcoubard 1131:692ddf04fc42 373
vcoubard 1131:692ddf04fc42 374 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 375 }
vcoubard 1131:692ddf04fc42 376
vcoubard 1131:692ddf04fc42 377 /**
vcoubard 1131:692ddf04fc42 378 * Set the appearance characteristic in the GAP service.
vcoubard 1131:692ddf04fc42 379 * @param[in] appearance
vcoubard 1131:692ddf04fc42 380 * The new value for the device-appearance.
vcoubard 1131:692ddf04fc42 381 */
vcoubard 1131:692ddf04fc42 382 virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance) {
vcoubard 1131:692ddf04fc42 383 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 384 (void)appearance;
vcoubard 1131:692ddf04fc42 385
vcoubard 1131:692ddf04fc42 386 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 387 }
vcoubard 1131:692ddf04fc42 388
vcoubard 1131:692ddf04fc42 389 /**
vcoubard 1131:692ddf04fc42 390 * Get the appearance characteristic in the GAP service.
vcoubard 1131:692ddf04fc42 391 * @param[out] appearance
vcoubard 1131:692ddf04fc42 392 * The new value for the device-appearance.
vcoubard 1131:692ddf04fc42 393 */
vcoubard 1131:692ddf04fc42 394 virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP) {
vcoubard 1131:692ddf04fc42 395 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 396 (void)appearanceP;
vcoubard 1131:692ddf04fc42 397
vcoubard 1131:692ddf04fc42 398 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 399 }
vcoubard 1131:692ddf04fc42 400
vcoubard 1131:692ddf04fc42 401 /**
vcoubard 1131:692ddf04fc42 402 * Set the radio's transmit power.
vcoubard 1131:692ddf04fc42 403 * @param[in] txPower Radio transmit power in dBm.
vcoubard 1131:692ddf04fc42 404 */
vcoubard 1131:692ddf04fc42 405 virtual ble_error_t setTxPower(int8_t txPower) {
vcoubard 1131:692ddf04fc42 406 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 407 (void)txPower;
vcoubard 1131:692ddf04fc42 408
vcoubard 1131:692ddf04fc42 409 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 410 }
vcoubard 1131:692ddf04fc42 411
vcoubard 1131:692ddf04fc42 412 /**
vcoubard 1131:692ddf04fc42 413 * Query the underlying stack for permitted arguments for setTxPower().
vcoubard 1131:692ddf04fc42 414 *
vcoubard 1131:692ddf04fc42 415 * @param[out] valueArrayPP
vcoubard 1131:692ddf04fc42 416 * Out parameter to receive the immutable array of Tx values.
vcoubard 1131:692ddf04fc42 417 * @param[out] countP
vcoubard 1131:692ddf04fc42 418 * Out parameter to receive the array's size.
vcoubard 1131:692ddf04fc42 419 */
vcoubard 1131:692ddf04fc42 420 virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP) {
vcoubard 1131:692ddf04fc42 421 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 422 (void)valueArrayPP;
vcoubard 1131:692ddf04fc42 423 (void)countP;
vcoubard 1131:692ddf04fc42 424
vcoubard 1131:692ddf04fc42 425 *countP = 0; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 426 }
vcoubard 1131:692ddf04fc42 427
vcoubard 1131:692ddf04fc42 428 protected:
vcoubard 1131:692ddf04fc42 429 /* Override the following in the underlying adaptation layer to provide the functionality of scanning. */
vcoubard 1131:692ddf04fc42 430 virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) {
vcoubard 1131:692ddf04fc42 431 (void)scanningParams;
vcoubard 1131:692ddf04fc42 432 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 433 }
vcoubard 1131:692ddf04fc42 434
vcoubard 1131:692ddf04fc42 435 /*
vcoubard 1131:692ddf04fc42 436 * APIs with non-virtual implementations.
vcoubard 1131:692ddf04fc42 437 */
vcoubard 1131:692ddf04fc42 438 public:
vcoubard 1131:692ddf04fc42 439 /**
vcoubard 1131:692ddf04fc42 440 * Returns the current GAP state of the device using a bitmask that
vcoubard 1131:692ddf04fc42 441 * describes whether the device is advertising or connected.
vcoubard 1131:692ddf04fc42 442 */
vcoubard 1131:692ddf04fc42 443 GapState_t getState(void) const {
vcoubard 1131:692ddf04fc42 444 return state;
vcoubard 1131:692ddf04fc42 445 }
vcoubard 1131:692ddf04fc42 446
vcoubard 1131:692ddf04fc42 447 /**
vcoubard 1131:692ddf04fc42 448 * Set the GAP advertising mode to use for this device.
vcoubard 1131:692ddf04fc42 449 */
vcoubard 1131:692ddf04fc42 450 void setAdvertisingType(GapAdvertisingParams::AdvertisingType_t advType) {
vcoubard 1131:692ddf04fc42 451 _advParams.setAdvertisingType(advType);
vcoubard 1131:692ddf04fc42 452 }
vcoubard 1131:692ddf04fc42 453
vcoubard 1131:692ddf04fc42 454 /**
vcoubard 1131:692ddf04fc42 455 * @param[in] interval
vcoubard 1131:692ddf04fc42 456 * Advertising interval in units of milliseconds. Advertising
vcoubard 1131:692ddf04fc42 457 * is disabled if interval is 0. If interval is smaller than
vcoubard 1131:692ddf04fc42 458 * the minimum supported value, then the minimum supported
vcoubard 1131:692ddf04fc42 459 * value is used instead. This minimum value can be discovered
vcoubard 1131:692ddf04fc42 460 * using getMinAdvertisingInterval().
vcoubard 1131:692ddf04fc42 461 *
vcoubard 1131:692ddf04fc42 462 * This field must be set to 0 if connectionMode is equal
vcoubard 1131:692ddf04fc42 463 * to ADV_CONNECTABLE_DIRECTED.
vcoubard 1131:692ddf04fc42 464 *
vcoubard 1131:692ddf04fc42 465 * @note: Decreasing this value will allow central devices to detect a
vcoubard 1131:692ddf04fc42 466 * peripheral faster, at the expense of more power being used by the radio
vcoubard 1131:692ddf04fc42 467 * due to the higher data transmit rate.
vcoubard 1131:692ddf04fc42 468 *
vcoubard 1131:692ddf04fc42 469 * @Note: [WARNING] This API previously used 0.625ms as the unit for its
vcoubard 1131:692ddf04fc42 470 * 'interval' argument. That required an explicit conversion from
vcoubard 1131:692ddf04fc42 471 * milliseconds using Gap::MSEC_TO_GAP_DURATION_UNITS(). This conversion is
vcoubard 1131:692ddf04fc42 472 * no longer required as the new units are milliseconds. Any application
vcoubard 1131:692ddf04fc42 473 * code depending on the old semantics needs to be updated accordingly.
vcoubard 1131:692ddf04fc42 474 */
vcoubard 1131:692ddf04fc42 475 void setAdvertisingInterval(uint16_t interval) {
vcoubard 1131:692ddf04fc42 476 if (interval == 0) {
vcoubard 1131:692ddf04fc42 477 stopAdvertising();
vcoubard 1131:692ddf04fc42 478 } else if (interval < getMinAdvertisingInterval()) {
vcoubard 1131:692ddf04fc42 479 interval = getMinAdvertisingInterval();
vcoubard 1131:692ddf04fc42 480 }
vcoubard 1131:692ddf04fc42 481 _advParams.setInterval(interval);
vcoubard 1131:692ddf04fc42 482 }
vcoubard 1131:692ddf04fc42 483
vcoubard 1131:692ddf04fc42 484 /**
vcoubard 1131:692ddf04fc42 485 * @param[in] timeout
vcoubard 1131:692ddf04fc42 486 * Advertising timeout (in seconds) between 0x1 and 0x3FFF (1
vcoubard 1131:692ddf04fc42 487 * and 16383). Use 0 to disable the advertising timeout.
vcoubard 1131:692ddf04fc42 488 */
vcoubard 1131:692ddf04fc42 489 void setAdvertisingTimeout(uint16_t timeout) {
vcoubard 1131:692ddf04fc42 490 _advParams.setTimeout(timeout);
vcoubard 1131:692ddf04fc42 491 }
vcoubard 1131:692ddf04fc42 492
vcoubard 1131:692ddf04fc42 493 /**
vcoubard 1131:692ddf04fc42 494 * Start advertising.
vcoubard 1131:692ddf04fc42 495 */
vcoubard 1131:692ddf04fc42 496 ble_error_t startAdvertising(void) {
vcoubard 1131:692ddf04fc42 497 setAdvertisingData(); /* Update the underlying stack. */
vcoubard 1131:692ddf04fc42 498 return startAdvertising(_advParams);
vcoubard 1131:692ddf04fc42 499 }
vcoubard 1131:692ddf04fc42 500
vcoubard 1131:692ddf04fc42 501 /**
vcoubard 1131:692ddf04fc42 502 * Reset any advertising payload prepared from prior calls to
vcoubard 1131:692ddf04fc42 503 * accumulateAdvertisingPayload(). This automatically propagates the re-
vcoubard 1131:692ddf04fc42 504 * initialized advertising payload to the underlying stack.
vcoubard 1131:692ddf04fc42 505 */
vcoubard 1131:692ddf04fc42 506 void clearAdvertisingPayload(void) {
vcoubard 1131:692ddf04fc42 507 _advPayload.clear();
vcoubard 1131:692ddf04fc42 508 setAdvertisingData();
vcoubard 1131:692ddf04fc42 509 }
vcoubard 1131:692ddf04fc42 510
vcoubard 1131:692ddf04fc42 511 /**
vcoubard 1131:692ddf04fc42 512 * Accumulate an AD structure in the advertising payload. Please note that
vcoubard 1131:692ddf04fc42 513 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
vcoubard 1131:692ddf04fc42 514 * as an additional 31 bytes if the advertising payload is too
vcoubard 1131:692ddf04fc42 515 * small.
vcoubard 1131:692ddf04fc42 516 *
vcoubard 1131:692ddf04fc42 517 * @param[in] flags
vcoubard 1131:692ddf04fc42 518 * The flags to be added. Please refer to
vcoubard 1131:692ddf04fc42 519 * GapAdvertisingData::Flags for valid flags. Multiple
vcoubard 1131:692ddf04fc42 520 * flags may be specified in combination.
vcoubard 1131:692ddf04fc42 521 */
vcoubard 1131:692ddf04fc42 522 ble_error_t accumulateAdvertisingPayload(uint8_t flags) {
vcoubard 1131:692ddf04fc42 523 ble_error_t rc;
vcoubard 1131:692ddf04fc42 524 if ((rc = _advPayload.addFlags(flags)) != BLE_ERROR_NONE) {
vcoubard 1131:692ddf04fc42 525 return rc;
vcoubard 1131:692ddf04fc42 526 }
vcoubard 1131:692ddf04fc42 527
vcoubard 1131:692ddf04fc42 528 return setAdvertisingData();
vcoubard 1131:692ddf04fc42 529 }
vcoubard 1131:692ddf04fc42 530
vcoubard 1131:692ddf04fc42 531 /**
vcoubard 1131:692ddf04fc42 532 * Accumulate an AD structure in the advertising payload. Please note that
vcoubard 1131:692ddf04fc42 533 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
vcoubard 1131:692ddf04fc42 534 * as an additional 31 bytes if the advertising payload is too
vcoubard 1131:692ddf04fc42 535 * small.
vcoubard 1131:692ddf04fc42 536 *
vcoubard 1131:692ddf04fc42 537 * @param app
vcoubard 1131:692ddf04fc42 538 * The appearance of the peripheral.
vcoubard 1131:692ddf04fc42 539 */
vcoubard 1131:692ddf04fc42 540 ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::Appearance app) {
vcoubard 1131:692ddf04fc42 541 setAppearance(app);
vcoubard 1131:692ddf04fc42 542
vcoubard 1131:692ddf04fc42 543 ble_error_t rc;
vcoubard 1131:692ddf04fc42 544 if ((rc = _advPayload.addAppearance(app)) != BLE_ERROR_NONE) {
vcoubard 1131:692ddf04fc42 545 return rc;
vcoubard 1131:692ddf04fc42 546 }
vcoubard 1131:692ddf04fc42 547
vcoubard 1131:692ddf04fc42 548 return setAdvertisingData();
vcoubard 1131:692ddf04fc42 549 }
vcoubard 1131:692ddf04fc42 550
vcoubard 1131:692ddf04fc42 551 /**
vcoubard 1131:692ddf04fc42 552 * Accumulate an AD structure in the advertising payload. Please note that
vcoubard 1131:692ddf04fc42 553 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
vcoubard 1131:692ddf04fc42 554 * as an additional 31 bytes if the advertising payload is too
vcoubard 1131:692ddf04fc42 555 * small.
vcoubard 1131:692ddf04fc42 556 *
vcoubard 1131:692ddf04fc42 557 * @param power
vcoubard 1131:692ddf04fc42 558 * The max transmit power to be used by the controller (in dBm).
vcoubard 1131:692ddf04fc42 559 */
vcoubard 1131:692ddf04fc42 560 ble_error_t accumulateAdvertisingPayloadTxPower(int8_t power) {
vcoubard 1131:692ddf04fc42 561 ble_error_t rc;
vcoubard 1131:692ddf04fc42 562 if ((rc = _advPayload.addTxPower(power)) != BLE_ERROR_NONE) {
vcoubard 1131:692ddf04fc42 563 return rc;
vcoubard 1131:692ddf04fc42 564 }
vcoubard 1131:692ddf04fc42 565
vcoubard 1131:692ddf04fc42 566 return setAdvertisingData();
vcoubard 1131:692ddf04fc42 567 }
vcoubard 1131:692ddf04fc42 568
vcoubard 1131:692ddf04fc42 569 /**
vcoubard 1131:692ddf04fc42 570 * Accumulate a variable length byte-stream as an AD structure in the
vcoubard 1131:692ddf04fc42 571 * advertising payload. Please note that the payload is limited to 31 bytes.
vcoubard 1131:692ddf04fc42 572 * The SCAN_RESPONSE message may be used as an additional 31 bytes if the
vcoubard 1131:692ddf04fc42 573 * advertising payload is too small.
vcoubard 1131:692ddf04fc42 574 *
vcoubard 1131:692ddf04fc42 575 * @param type The type describing the variable length data.
vcoubard 1131:692ddf04fc42 576 * @param data Data bytes.
vcoubard 1131:692ddf04fc42 577 * @param len Length of data.
vcoubard 1131:692ddf04fc42 578 */
vcoubard 1131:692ddf04fc42 579 ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
vcoubard 1131:692ddf04fc42 580 if (type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
vcoubard 1131:692ddf04fc42 581 setDeviceName(data);
vcoubard 1131:692ddf04fc42 582 }
vcoubard 1131:692ddf04fc42 583
vcoubard 1131:692ddf04fc42 584 ble_error_t rc;
vcoubard 1131:692ddf04fc42 585 if ((rc = _advPayload.addData(type, data, len)) != BLE_ERROR_NONE) {
vcoubard 1131:692ddf04fc42 586 return rc;
vcoubard 1131:692ddf04fc42 587 }
vcoubard 1131:692ddf04fc42 588
vcoubard 1131:692ddf04fc42 589 return setAdvertisingData();
vcoubard 1131:692ddf04fc42 590 }
vcoubard 1131:692ddf04fc42 591
vcoubard 1131:692ddf04fc42 592 /**
vcoubard 1131:692ddf04fc42 593 * Update a particular ADV field in the advertising payload (based on
vcoubard 1131:692ddf04fc42 594 * matching type and length). Note: the length of the new data must be the
vcoubard 1131:692ddf04fc42 595 * same as the old one.
vcoubard 1131:692ddf04fc42 596 *
vcoubard 1131:692ddf04fc42 597 * @param[in] type The ADV type field describing the variable length data.
vcoubard 1131:692ddf04fc42 598 * @param[in] data Data bytes.
vcoubard 1131:692ddf04fc42 599 * @param[in] len Length of data.
vcoubard 1131:692ddf04fc42 600 *
vcoubard 1131:692ddf04fc42 601 * @note: If advertisements are enabled, then the update will take effect immediately.
vcoubard 1131:692ddf04fc42 602 *
vcoubard 1131:692ddf04fc42 603 * @return BLE_ERROR_NONE if the advertisement payload was updated based on
vcoubard 1131:692ddf04fc42 604 * a <type, len> match; otherwise, an appropriate error.
vcoubard 1131:692ddf04fc42 605 */
vcoubard 1131:692ddf04fc42 606 ble_error_t updateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
vcoubard 1131:692ddf04fc42 607 if (type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
vcoubard 1131:692ddf04fc42 608 setDeviceName(data);
vcoubard 1131:692ddf04fc42 609 }
vcoubard 1131:692ddf04fc42 610
vcoubard 1131:692ddf04fc42 611 ble_error_t rc;
vcoubard 1131:692ddf04fc42 612 if ((rc = _advPayload.updateData(type, data, len)) != BLE_ERROR_NONE) {
vcoubard 1131:692ddf04fc42 613 return rc;
vcoubard 1131:692ddf04fc42 614 }
vcoubard 1131:692ddf04fc42 615
vcoubard 1131:692ddf04fc42 616 return setAdvertisingData();
vcoubard 1131:692ddf04fc42 617 }
vcoubard 1131:692ddf04fc42 618
vcoubard 1131:692ddf04fc42 619 /**
vcoubard 1131:692ddf04fc42 620 * Set up a particular, user-constructed advertisement payload for the
vcoubard 1131:692ddf04fc42 621 * underlying stack. It would be uncommon for this API to be used directly;
vcoubard 1131:692ddf04fc42 622 * there are other APIs to build an advertisement payload (see above).
vcoubard 1131:692ddf04fc42 623 */
vcoubard 1131:692ddf04fc42 624 ble_error_t setAdvertisingPayload(const GapAdvertisingData &payload) {
vcoubard 1131:692ddf04fc42 625 _advPayload = payload;
vcoubard 1131:692ddf04fc42 626 return setAdvertisingData();
vcoubard 1131:692ddf04fc42 627 }
vcoubard 1131:692ddf04fc42 628
vcoubard 1131:692ddf04fc42 629 /**
vcoubard 1131:692ddf04fc42 630 * @return Read back advertising data. Useful for storing and
vcoubard 1131:692ddf04fc42 631 * restoring payload.
vcoubard 1131:692ddf04fc42 632 */
vcoubard 1131:692ddf04fc42 633 const GapAdvertisingData &getAdvertisingPayload(void) const {
vcoubard 1131:692ddf04fc42 634 return _advPayload;
vcoubard 1131:692ddf04fc42 635 }
vcoubard 1131:692ddf04fc42 636
vcoubard 1131:692ddf04fc42 637 /**
vcoubard 1131:692ddf04fc42 638 * Accumulate a variable length byte-stream as an AD structure in the
vcoubard 1131:692ddf04fc42 639 * scanResponse payload.
vcoubard 1131:692ddf04fc42 640 *
vcoubard 1131:692ddf04fc42 641 * @param[in] type The type describing the variable length data.
vcoubard 1131:692ddf04fc42 642 * @param[in] data Data bytes.
vcoubard 1131:692ddf04fc42 643 * @param[in] len Length of data.
vcoubard 1131:692ddf04fc42 644 */
vcoubard 1131:692ddf04fc42 645 ble_error_t accumulateScanResponse(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
vcoubard 1131:692ddf04fc42 646 ble_error_t rc;
vcoubard 1131:692ddf04fc42 647 if ((rc = _scanResponse.addData(type, data, len)) != BLE_ERROR_NONE) {
vcoubard 1131:692ddf04fc42 648 return rc;
vcoubard 1131:692ddf04fc42 649 }
vcoubard 1131:692ddf04fc42 650
vcoubard 1131:692ddf04fc42 651 return setAdvertisingData();
vcoubard 1131:692ddf04fc42 652 }
vcoubard 1131:692ddf04fc42 653
vcoubard 1131:692ddf04fc42 654 /**
vcoubard 1131:692ddf04fc42 655 * Reset any scan response prepared from prior calls to
vcoubard 1131:692ddf04fc42 656 * accumulateScanResponse().
vcoubard 1131:692ddf04fc42 657 *
vcoubard 1131:692ddf04fc42 658 * Note: This should be followed by a call to setAdvertisingPayload() or
vcoubard 1131:692ddf04fc42 659 * startAdvertising() before the update takes effect.
vcoubard 1131:692ddf04fc42 660 */
vcoubard 1131:692ddf04fc42 661 void clearScanResponse(void) {
vcoubard 1131:692ddf04fc42 662 _scanResponse.clear();
vcoubard 1131:692ddf04fc42 663 setAdvertisingData();
vcoubard 1131:692ddf04fc42 664 }
vcoubard 1131:692ddf04fc42 665
vcoubard 1131:692ddf04fc42 666 /**
vcoubard 1131:692ddf04fc42 667 * Set up parameters for GAP scanning (observer mode).
vcoubard 1131:692ddf04fc42 668 * @param[in] interval
vcoubard 1131:692ddf04fc42 669 * Scan interval (in milliseconds) [valid values lie between 2.5ms and 10.24s].
vcoubard 1131:692ddf04fc42 670 * @param[in] window
vcoubard 1131:692ddf04fc42 671 * Scan Window (in milliseconds) [valid values lie between 2.5ms and 10.24s].
vcoubard 1131:692ddf04fc42 672 * @param[in] timeout
vcoubard 1131:692ddf04fc42 673 * Scan timeout (in seconds) between 0x0001 and 0xFFFF; 0x0000 disables the timeout.
vcoubard 1131:692ddf04fc42 674 * @param[in] activeScanning
vcoubard 1131:692ddf04fc42 675 * Set to True if active-scanning is required. This is used to fetch the
vcoubard 1131:692ddf04fc42 676 * scan response from a peer if possible.
vcoubard 1131:692ddf04fc42 677 *
vcoubard 1131:692ddf04fc42 678 * The scanning window divided by the interval determines the duty cycle for
vcoubard 1131:692ddf04fc42 679 * scanning. For example, if the interval is 100ms and the window is 10ms,
vcoubard 1131:692ddf04fc42 680 * then the controller will scan for 10 percent of the time. It is possible
vcoubard 1131:692ddf04fc42 681 * to have the interval and window set to the same value. In this case,
vcoubard 1131:692ddf04fc42 682 * scanning is continuous, with a change of scanning frequency once every
vcoubard 1131:692ddf04fc42 683 * interval.
vcoubard 1131:692ddf04fc42 684 *
vcoubard 1131:692ddf04fc42 685 * Once the scanning parameters have been configured, scanning can be
vcoubard 1131:692ddf04fc42 686 * enabled by using startScan().
vcoubard 1131:692ddf04fc42 687 *
vcoubard 1131:692ddf04fc42 688 * @Note: The scan interval and window are recommendations to the BLE stack.
vcoubard 1131:692ddf04fc42 689 */
vcoubard 1131:692ddf04fc42 690 ble_error_t setScanParams(uint16_t interval = GapScanningParams::SCAN_INTERVAL_MAX,
vcoubard 1131:692ddf04fc42 691 uint16_t window = GapScanningParams::SCAN_WINDOW_MAX,
vcoubard 1131:692ddf04fc42 692 uint16_t timeout = 0,
vcoubard 1131:692ddf04fc42 693 bool activeScanning = false) {
vcoubard 1131:692ddf04fc42 694 ble_error_t rc;
vcoubard 1131:692ddf04fc42 695 if (((rc = _scanningParams.setInterval(interval)) == BLE_ERROR_NONE) &&
vcoubard 1131:692ddf04fc42 696 ((rc = _scanningParams.setWindow(window)) == BLE_ERROR_NONE) &&
vcoubard 1131:692ddf04fc42 697 ((rc = _scanningParams.setTimeout(timeout)) == BLE_ERROR_NONE)) {
vcoubard 1131:692ddf04fc42 698 _scanningParams.setActiveScanning(activeScanning);
vcoubard 1131:692ddf04fc42 699 return BLE_ERROR_NONE;
vcoubard 1131:692ddf04fc42 700 }
vcoubard 1131:692ddf04fc42 701
vcoubard 1131:692ddf04fc42 702 return rc;
vcoubard 1131:692ddf04fc42 703 }
vcoubard 1131:692ddf04fc42 704
vcoubard 1131:692ddf04fc42 705 /**
vcoubard 1131:692ddf04fc42 706 * Set up the scanInterval parameter for GAP scanning (observer mode).
vcoubard 1131:692ddf04fc42 707 * @param[in] interval
vcoubard 1131:692ddf04fc42 708 * Scan interval (in milliseconds) [valid values lie between 2.5ms and 10.24s].
vcoubard 1131:692ddf04fc42 709 *
vcoubard 1131:692ddf04fc42 710 * The scanning window divided by the interval determines the duty cycle for
vcoubard 1131:692ddf04fc42 711 * scanning. For example, if the interval is 100ms and the window is 10ms,
vcoubard 1131:692ddf04fc42 712 * then the controller will scan for 10 percent of the time. It is possible
vcoubard 1131:692ddf04fc42 713 * to have the interval and window set to the same value. In this case,
vcoubard 1131:692ddf04fc42 714 * scanning is continuous, with a change of scanning frequency once every
vcoubard 1131:692ddf04fc42 715 * interval.
vcoubard 1131:692ddf04fc42 716 *
vcoubard 1131:692ddf04fc42 717 * Once the scanning parameters have been configured, scanning can be
vcoubard 1131:692ddf04fc42 718 * enabled by using startScan().
vcoubard 1131:692ddf04fc42 719 */
vcoubard 1131:692ddf04fc42 720 ble_error_t setScanInterval(uint16_t interval) {
vcoubard 1131:692ddf04fc42 721 return _scanningParams.setInterval(interval);
vcoubard 1131:692ddf04fc42 722 }
vcoubard 1131:692ddf04fc42 723
vcoubard 1131:692ddf04fc42 724 /**
vcoubard 1131:692ddf04fc42 725 * Set up the scanWindow parameter for GAP scanning (observer mode).
vcoubard 1131:692ddf04fc42 726 * @param[in] window
vcoubard 1131:692ddf04fc42 727 * Scan Window (in milliseconds) [valid values lie between 2.5ms and 10.24s].
vcoubard 1131:692ddf04fc42 728 *
vcoubard 1131:692ddf04fc42 729 * The scanning window divided by the interval determines the duty cycle for
vcoubard 1131:692ddf04fc42 730 * scanning. For example, if the interval is 100ms and the window is 10ms,
vcoubard 1131:692ddf04fc42 731 * then the controller will scan for 10 percent of the time. It is possible
vcoubard 1131:692ddf04fc42 732 * to have the interval and window set to the same value. In this case,
vcoubard 1131:692ddf04fc42 733 * scanning is continuous, with a change of scanning frequency once every
vcoubard 1131:692ddf04fc42 734 * interval.
vcoubard 1131:692ddf04fc42 735 *
vcoubard 1131:692ddf04fc42 736 * Once the scanning parameters have been configured, scanning can be
vcoubard 1131:692ddf04fc42 737 * enabled by using startScan().
vcoubard 1131:692ddf04fc42 738 *
vcoubard 1131:692ddf04fc42 739 * If scanning is already active, the updated value of scanWindow will be
vcoubard 1131:692ddf04fc42 740 * propagated to the underlying BLE stack.
vcoubard 1131:692ddf04fc42 741 */
vcoubard 1131:692ddf04fc42 742 ble_error_t setScanWindow(uint16_t window) {
vcoubard 1131:692ddf04fc42 743 ble_error_t rc;
vcoubard 1131:692ddf04fc42 744 if ((rc = _scanningParams.setWindow(window)) != BLE_ERROR_NONE) {
vcoubard 1131:692ddf04fc42 745 return rc;
vcoubard 1131:692ddf04fc42 746 }
vcoubard 1131:692ddf04fc42 747
vcoubard 1131:692ddf04fc42 748 /* If scanning is already active, propagate the new setting to the stack. */
vcoubard 1131:692ddf04fc42 749 if (scanningActive) {
vcoubard 1131:692ddf04fc42 750 return startRadioScan(_scanningParams);
vcoubard 1131:692ddf04fc42 751 }
vcoubard 1131:692ddf04fc42 752
vcoubard 1131:692ddf04fc42 753 return BLE_ERROR_NONE;
vcoubard 1131:692ddf04fc42 754 }
vcoubard 1131:692ddf04fc42 755
vcoubard 1131:692ddf04fc42 756 /**
vcoubard 1131:692ddf04fc42 757 * Set up parameters for GAP scanning (observer mode).
vcoubard 1131:692ddf04fc42 758 * @param[in] timeout
vcoubard 1131:692ddf04fc42 759 * Scan timeout (in seconds) between 0x0001 and 0xFFFF; 0x0000 disables the timeout.
vcoubard 1131:692ddf04fc42 760 *
vcoubard 1131:692ddf04fc42 761 * Once the scanning parameters have been configured, scanning can be
vcoubard 1131:692ddf04fc42 762 * enabled by using startScan().
vcoubard 1131:692ddf04fc42 763 *
vcoubard 1131:692ddf04fc42 764 * If scanning is already active, the updated value of scanTimeout will be
vcoubard 1131:692ddf04fc42 765 * propagated to the underlying BLE stack.
vcoubard 1131:692ddf04fc42 766 */
vcoubard 1131:692ddf04fc42 767 ble_error_t setScanTimeout(uint16_t timeout) {
vcoubard 1131:692ddf04fc42 768 ble_error_t rc;
vcoubard 1131:692ddf04fc42 769 if ((rc = _scanningParams.setTimeout(timeout)) != BLE_ERROR_NONE) {
vcoubard 1131:692ddf04fc42 770 return rc;
vcoubard 1131:692ddf04fc42 771 }
vcoubard 1131:692ddf04fc42 772
vcoubard 1131:692ddf04fc42 773 /* If scanning is already active, propagate the new settings to the stack. */
vcoubard 1131:692ddf04fc42 774 if (scanningActive) {
vcoubard 1131:692ddf04fc42 775 return startRadioScan(_scanningParams);
vcoubard 1131:692ddf04fc42 776 }
vcoubard 1131:692ddf04fc42 777
vcoubard 1131:692ddf04fc42 778 return BLE_ERROR_NONE;
vcoubard 1131:692ddf04fc42 779 }
vcoubard 1131:692ddf04fc42 780
vcoubard 1131:692ddf04fc42 781 /**
vcoubard 1131:692ddf04fc42 782 * Set up parameters for GAP scanning (observer mode).
vcoubard 1131:692ddf04fc42 783 * @param[in] activeScanning
vcoubard 1131:692ddf04fc42 784 * Set to True if active-scanning is required. This is used to fetch the
vcoubard 1131:692ddf04fc42 785 * scan response from a peer if possible.
vcoubard 1131:692ddf04fc42 786 *
vcoubard 1131:692ddf04fc42 787 * Once the scanning parameters have been configured, scanning can be
vcoubard 1131:692ddf04fc42 788 * enabled by using startScan().
vcoubard 1131:692ddf04fc42 789 *
vcoubard 1131:692ddf04fc42 790 * If scanning is already in progress, then active-scanning will be enabled
vcoubard 1131:692ddf04fc42 791 * for the underlying BLE stack.
vcoubard 1131:692ddf04fc42 792 */
vcoubard 1131:692ddf04fc42 793 ble_error_t setActiveScanning(bool activeScanning) {
vcoubard 1131:692ddf04fc42 794 _scanningParams.setActiveScanning(activeScanning);
vcoubard 1131:692ddf04fc42 795
vcoubard 1131:692ddf04fc42 796 /* If scanning is already active, propagate the new settings to the stack. */
vcoubard 1131:692ddf04fc42 797 if (scanningActive) {
vcoubard 1131:692ddf04fc42 798 return startRadioScan(_scanningParams);
vcoubard 1131:692ddf04fc42 799 }
vcoubard 1131:692ddf04fc42 800
vcoubard 1131:692ddf04fc42 801 return BLE_ERROR_NONE;
vcoubard 1131:692ddf04fc42 802 }
vcoubard 1131:692ddf04fc42 803
vcoubard 1131:692ddf04fc42 804 /**
vcoubard 1131:692ddf04fc42 805 * Start scanning (Observer Procedure) based on the parameters currently in
vcoubard 1131:692ddf04fc42 806 * effect.
vcoubard 1131:692ddf04fc42 807 *
vcoubard 1131:692ddf04fc42 808 * @param[in] callback
vcoubard 1131:692ddf04fc42 809 * The application-specific callback to be invoked upon
vcoubard 1131:692ddf04fc42 810 * receiving every advertisement report. This can be passed in
vcoubard 1131:692ddf04fc42 811 * as NULL, in which case scanning may not be enabled at all.
vcoubard 1131:692ddf04fc42 812 */
vcoubard 1131:692ddf04fc42 813 ble_error_t startScan(void (*callback)(const AdvertisementCallbackParams_t *params)) {
vcoubard 1131:692ddf04fc42 814 ble_error_t err = BLE_ERROR_NONE;
vcoubard 1131:692ddf04fc42 815 if (callback) {
vcoubard 1131:692ddf04fc42 816 if ((err = startRadioScan(_scanningParams)) == BLE_ERROR_NONE) {
vcoubard 1131:692ddf04fc42 817 scanningActive = true;
vcoubard 1131:692ddf04fc42 818 onAdvertisementReport.attach(callback);
vcoubard 1131:692ddf04fc42 819 }
vcoubard 1131:692ddf04fc42 820 }
vcoubard 1131:692ddf04fc42 821
vcoubard 1131:692ddf04fc42 822 return err;
vcoubard 1131:692ddf04fc42 823 }
vcoubard 1131:692ddf04fc42 824
vcoubard 1131:692ddf04fc42 825 /**
vcoubard 1131:692ddf04fc42 826 * Same as above, but this takes an (object, method) pair for a callback.
vcoubard 1131:692ddf04fc42 827 */
vcoubard 1131:692ddf04fc42 828 template<typename T>
vcoubard 1131:692ddf04fc42 829 ble_error_t startScan(T *object, void (T::*callbackMember)(const AdvertisementCallbackParams_t *params)) {
vcoubard 1131:692ddf04fc42 830 ble_error_t err = BLE_ERROR_NONE;
vcoubard 1131:692ddf04fc42 831 if (object && callbackMember) {
vcoubard 1131:692ddf04fc42 832 if ((err = startRadioScan(_scanningParams)) == BLE_ERROR_NONE) {
vcoubard 1131:692ddf04fc42 833 scanningActive = true;
vcoubard 1131:692ddf04fc42 834 onAdvertisementReport.attach(object, callbackMember);
vcoubard 1131:692ddf04fc42 835 }
vcoubard 1131:692ddf04fc42 836 }
vcoubard 1131:692ddf04fc42 837
vcoubard 1131:692ddf04fc42 838 return err;
vcoubard 1131:692ddf04fc42 839 }
vcoubard 1131:692ddf04fc42 840
vcoubard 1131:692ddf04fc42 841 /**
vcoubard 1131:692ddf04fc42 842 * Initialize radio-notification events to be generated from the stack.
vcoubard 1131:692ddf04fc42 843 * This API doesn't need to be called directly.
vcoubard 1131:692ddf04fc42 844 *
vcoubard 1131:692ddf04fc42 845 * Radio Notification is a feature that enables ACTIVE and INACTIVE
vcoubard 1131:692ddf04fc42 846 * (nACTIVE) signals from the stack that notify the application when the
vcoubard 1131:692ddf04fc42 847 * radio is in use.
vcoubard 1131:692ddf04fc42 848 *
vcoubard 1131:692ddf04fc42 849 * The ACTIVE signal is sent before the radio event starts. The nACTIVE
vcoubard 1131:692ddf04fc42 850 * signal is sent at the end of the radio event. These signals can be used
vcoubard 1131:692ddf04fc42 851 * by the application programmer to synchronize application logic with radio
vcoubard 1131:692ddf04fc42 852 * activity. For example, the ACTIVE signal can be used to shut off external
vcoubard 1131:692ddf04fc42 853 * devices, to manage peak current drawn during periods when the radio is on,
vcoubard 1131:692ddf04fc42 854 * or to trigger sensor data collection for transmission in the Radio Event.
vcoubard 1131:692ddf04fc42 855 *
vcoubard 1131:692ddf04fc42 856 * @return BLE_ERROR_NONE on successful initialization, otherwise an error code.
vcoubard 1131:692ddf04fc42 857 */
vcoubard 1131:692ddf04fc42 858 virtual ble_error_t initRadioNotification(void) {
vcoubard 1131:692ddf04fc42 859 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 860 }
vcoubard 1131:692ddf04fc42 861
vcoubard 1131:692ddf04fc42 862 private:
vcoubard 1131:692ddf04fc42 863 ble_error_t setAdvertisingData(void) {
vcoubard 1131:692ddf04fc42 864 return setAdvertisingData(_advPayload, _scanResponse);
vcoubard 1131:692ddf04fc42 865 }
vcoubard 1131:692ddf04fc42 866
vcoubard 1131:692ddf04fc42 867 private:
vcoubard 1131:692ddf04fc42 868 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse) = 0;
vcoubard 1131:692ddf04fc42 869 virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0;
vcoubard 1131:692ddf04fc42 870
vcoubard 1131:692ddf04fc42 871 public:
vcoubard 1131:692ddf04fc42 872 /**
vcoubard 1131:692ddf04fc42 873 * Accessors to read back currently active advertising params.
vcoubard 1131:692ddf04fc42 874 */
vcoubard 1131:692ddf04fc42 875 GapAdvertisingParams &getAdvertisingParams(void) {
vcoubard 1131:692ddf04fc42 876 return _advParams;
vcoubard 1131:692ddf04fc42 877 }
vcoubard 1131:692ddf04fc42 878 const GapAdvertisingParams &getAdvertisingParams(void) const {
vcoubard 1131:692ddf04fc42 879 return _advParams;
vcoubard 1131:692ddf04fc42 880 }
vcoubard 1131:692ddf04fc42 881
vcoubard 1131:692ddf04fc42 882 /**
vcoubard 1131:692ddf04fc42 883 * Set up a particular, user-constructed set of advertisement parameters for
vcoubard 1131:692ddf04fc42 884 * the underlying stack. It would be uncommon for this API to be used
vcoubard 1131:692ddf04fc42 885 * directly; there are other APIs to tweak advertisement parameters
vcoubard 1131:692ddf04fc42 886 * individually.
vcoubard 1131:692ddf04fc42 887 */
vcoubard 1131:692ddf04fc42 888 void setAdvertisingParams(const GapAdvertisingParams &newParams) {
vcoubard 1131:692ddf04fc42 889 _advParams = newParams;
vcoubard 1131:692ddf04fc42 890 }
vcoubard 1131:692ddf04fc42 891
vcoubard 1131:692ddf04fc42 892 /* Event callback handlers. */
vcoubard 1131:692ddf04fc42 893 public:
vcoubard 1131:692ddf04fc42 894 /**
vcoubard 1131:692ddf04fc42 895 * Set up a callback for timeout events. Refer to TimeoutSource_t for
vcoubard 1131:692ddf04fc42 896 * possible event types.
vcoubard 1131:692ddf04fc42 897 * @note It is possible to unregister callbacks using onTimeout().detach(callback)
vcoubard 1131:692ddf04fc42 898 */
vcoubard 1131:692ddf04fc42 899 void onTimeout(TimeoutEventCallback_t callback) {
vcoubard 1131:692ddf04fc42 900 timeoutCallbackChain.add(callback);
vcoubard 1131:692ddf04fc42 901 }
vcoubard 1131:692ddf04fc42 902
vcoubard 1131:692ddf04fc42 903 /**
vcoubard 1131:692ddf04fc42 904 * @brief provide access to the callchain of timeout event callbacks
vcoubard 1131:692ddf04fc42 905 * It is possible to register callbacks using onTimeout().add(callback);
vcoubard 1132:6362b7c2fdff 906 * It is possible to unregister callbacks using onTimeout().detach(callback)
vcoubard 1131:692ddf04fc42 907 * @return The timeout event callbacks chain
vcoubard 1131:692ddf04fc42 908 */
vcoubard 1131:692ddf04fc42 909 TimeoutEventCallbackChain_t& onTimeout() {
vcoubard 1131:692ddf04fc42 910 return timeoutCallbackChain;
vcoubard 1131:692ddf04fc42 911 }
vcoubard 1131:692ddf04fc42 912
vcoubard 1131:692ddf04fc42 913 /**
vcoubard 1131:692ddf04fc42 914 * Append to a chain of callbacks to be invoked upon GAP connection.
vcoubard 1131:692ddf04fc42 915 * @note It is possible to unregister callbacks using onConnection().detach(callback)
vcoubard 1131:692ddf04fc42 916 */
vcoubard 1131:692ddf04fc42 917 void onConnection(ConnectionEventCallback_t callback) {connectionCallChain.add(callback);}
vcoubard 1131:692ddf04fc42 918
vcoubard 1131:692ddf04fc42 919 template<typename T>
vcoubard 1131:692ddf04fc42 920 void onConnection(T *tptr, void (T::*mptr)(const ConnectionCallbackParams_t*)) {connectionCallChain.add(tptr, mptr);}
vcoubard 1131:692ddf04fc42 921
vcoubard 1131:692ddf04fc42 922 /**
vcoubard 1131:692ddf04fc42 923 * @brief provide access to the callchain of connection event callbacks
vcoubard 1131:692ddf04fc42 924 * It is possible to register callbacks using onConnection().add(callback);
vcoubard 1132:6362b7c2fdff 925 * It is possible to unregister callbacks using onConnection().detach(callback)
vcoubard 1131:692ddf04fc42 926 * @return The connection event callbacks chain
vcoubard 1131:692ddf04fc42 927 */
vcoubard 1132:6362b7c2fdff 928 ConnectionEventCallbackChain_t& onConnection() {
vcoubard 1131:692ddf04fc42 929 return connectionCallChain;
vcoubard 1131:692ddf04fc42 930 }
vcoubard 1131:692ddf04fc42 931
vcoubard 1131:692ddf04fc42 932 /**
vcoubard 1131:692ddf04fc42 933 * Append to a chain of callbacks to be invoked upon GAP disconnection.
vcoubard 1131:692ddf04fc42 934 * @note It is possible to unregister callbacks using onDisconnection().detach(callback)
vcoubard 1131:692ddf04fc42 935 */
vcoubard 1131:692ddf04fc42 936 void onDisconnection(DisconnectionEventCallback_t callback) {disconnectionCallChain.add(callback);}
vcoubard 1131:692ddf04fc42 937
vcoubard 1131:692ddf04fc42 938 template<typename T>
vcoubard 1131:692ddf04fc42 939 void onDisconnection(T *tptr, void (T::*mptr)(const DisconnectionCallbackParams_t*)) {disconnectionCallChain.add(tptr, mptr);}
vcoubard 1131:692ddf04fc42 940
vcoubard 1131:692ddf04fc42 941 /**
vcoubard 1131:692ddf04fc42 942 * @brief provide access to the callchain of disconnection event callbacks
vcoubard 1131:692ddf04fc42 943 * It is possible to register callbacks using onDisconnection().add(callback);
vcoubard 1132:6362b7c2fdff 944 * It is possible to unregister callbacks using onDisconnection().detach(callback)
vcoubard 1131:692ddf04fc42 945 * @return The disconnection event callbacks chain
vcoubard 1131:692ddf04fc42 946 */
vcoubard 1131:692ddf04fc42 947 DisconnectionEventCallbackChain_t& onDisconnection() {
vcoubard 1131:692ddf04fc42 948 return disconnectionCallChain;
vcoubard 1131:692ddf04fc42 949 }
vcoubard 1131:692ddf04fc42 950
vcoubard 1131:692ddf04fc42 951 /**
vcoubard 1131:692ddf04fc42 952 * Set the application callback for radio-notification events.
vcoubard 1131:692ddf04fc42 953 *
vcoubard 1131:692ddf04fc42 954 * Radio Notification is a feature that enables ACTIVE and INACTIVE
vcoubard 1131:692ddf04fc42 955 * (nACTIVE) signals from the stack that notify the application when the
vcoubard 1131:692ddf04fc42 956 * radio is in use.
vcoubard 1131:692ddf04fc42 957 *
vcoubard 1131:692ddf04fc42 958 * The ACTIVE signal is sent before the radio event starts. The nACTIVE
vcoubard 1131:692ddf04fc42 959 * signal is sent at the end of the radio event. These signals can be used
vcoubard 1131:692ddf04fc42 960 * by the application programmer to synchronize application logic with radio
vcoubard 1131:692ddf04fc42 961 * activity. For example, the ACTIVE signal can be used to shut off external
vcoubard 1131:692ddf04fc42 962 * devices, to manage peak current drawn during periods when the radio is on,
vcoubard 1131:692ddf04fc42 963 * or to trigger sensor data collection for transmission in the Radio Event.
vcoubard 1131:692ddf04fc42 964 *
vcoubard 1131:692ddf04fc42 965 * @param callback
vcoubard 1131:692ddf04fc42 966 * The application handler to be invoked in response to a radio
vcoubard 1131:692ddf04fc42 967 * ACTIVE/INACTIVE event.
vcoubard 1131:692ddf04fc42 968 *
vcoubard 1131:692ddf04fc42 969 * Or in the other version:
vcoubard 1131:692ddf04fc42 970 *
vcoubard 1131:692ddf04fc42 971 * @param tptr
vcoubard 1131:692ddf04fc42 972 * Pointer to the object of a class defining the member callback
vcoubard 1131:692ddf04fc42 973 * function (mptr).
vcoubard 1131:692ddf04fc42 974 * @param mptr
vcoubard 1131:692ddf04fc42 975 * The member callback (within the context of an object) to be
vcoubard 1131:692ddf04fc42 976 * invoked in response to a radio ACTIVE/INACTIVE event.
vcoubard 1131:692ddf04fc42 977 */
vcoubard 1131:692ddf04fc42 978 void onRadioNotification(void (*callback)(bool param)) {
vcoubard 1131:692ddf04fc42 979 radioNotificationCallback.attach(callback);
vcoubard 1131:692ddf04fc42 980 }
vcoubard 1131:692ddf04fc42 981 template <typename T>
vcoubard 1131:692ddf04fc42 982 void onRadioNotification(T *tptr, void (T::*mptr)(bool)) {
vcoubard 1131:692ddf04fc42 983 radioNotificationCallback.attach(tptr, mptr);
vcoubard 1131:692ddf04fc42 984 }
vcoubard 1131:692ddf04fc42 985
vcoubard 1131:692ddf04fc42 986 protected:
vcoubard 1131:692ddf04fc42 987 Gap() :
vcoubard 1131:692ddf04fc42 988 _advParams(),
vcoubard 1131:692ddf04fc42 989 _advPayload(),
vcoubard 1131:692ddf04fc42 990 _scanningParams(),
vcoubard 1131:692ddf04fc42 991 _scanResponse(),
vcoubard 1131:692ddf04fc42 992 state(),
vcoubard 1131:692ddf04fc42 993 scanningActive(false),
vcoubard 1131:692ddf04fc42 994 timeoutCallbackChain(),
vcoubard 1131:692ddf04fc42 995 radioNotificationCallback(),
vcoubard 1131:692ddf04fc42 996 onAdvertisementReport(),
vcoubard 1131:692ddf04fc42 997 connectionCallChain(),
vcoubard 1131:692ddf04fc42 998 disconnectionCallChain() {
vcoubard 1131:692ddf04fc42 999 _advPayload.clear();
vcoubard 1131:692ddf04fc42 1000 _scanResponse.clear();
vcoubard 1131:692ddf04fc42 1001 }
vcoubard 1131:692ddf04fc42 1002
vcoubard 1131:692ddf04fc42 1003 /* Entry points for the underlying stack to report events back to the user. */
vcoubard 1131:692ddf04fc42 1004 public:
vcoubard 1131:692ddf04fc42 1005 void processConnectionEvent(Handle_t handle,
vcoubard 1131:692ddf04fc42 1006 Role_t role,
vcoubard 1131:692ddf04fc42 1007 AddressType_t peerAddrType,
vcoubard 1131:692ddf04fc42 1008 const Address_t peerAddr,
vcoubard 1131:692ddf04fc42 1009 AddressType_t ownAddrType,
vcoubard 1131:692ddf04fc42 1010 const Address_t ownAddr,
vcoubard 1131:692ddf04fc42 1011 const ConnectionParams_t *connectionParams) {
vcoubard 1131:692ddf04fc42 1012 state.connected = 1;
vcoubard 1131:692ddf04fc42 1013 ConnectionCallbackParams_t callbackParams(handle, role, peerAddrType, peerAddr, ownAddrType, ownAddr, connectionParams);
vcoubard 1131:692ddf04fc42 1014 connectionCallChain.call(&callbackParams);
vcoubard 1131:692ddf04fc42 1015 }
vcoubard 1131:692ddf04fc42 1016
vcoubard 1131:692ddf04fc42 1017 void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) {
vcoubard 1131:692ddf04fc42 1018 state.connected = 0;
vcoubard 1131:692ddf04fc42 1019 DisconnectionCallbackParams_t callbackParams(handle, reason);
vcoubard 1131:692ddf04fc42 1020 disconnectionCallChain.call(&callbackParams);
vcoubard 1131:692ddf04fc42 1021 }
vcoubard 1131:692ddf04fc42 1022
vcoubard 1131:692ddf04fc42 1023 void processAdvertisementReport(const Address_t peerAddr,
vcoubard 1131:692ddf04fc42 1024 int8_t rssi,
vcoubard 1131:692ddf04fc42 1025 bool isScanResponse,
vcoubard 1131:692ddf04fc42 1026 GapAdvertisingParams::AdvertisingType_t type,
vcoubard 1131:692ddf04fc42 1027 uint8_t advertisingDataLen,
vcoubard 1131:692ddf04fc42 1028 const uint8_t *advertisingData) {
vcoubard 1131:692ddf04fc42 1029 AdvertisementCallbackParams_t params;
vcoubard 1131:692ddf04fc42 1030 memcpy(params.peerAddr, peerAddr, ADDR_LEN);
vcoubard 1131:692ddf04fc42 1031 params.rssi = rssi;
vcoubard 1131:692ddf04fc42 1032 params.isScanResponse = isScanResponse;
vcoubard 1131:692ddf04fc42 1033 params.type = type;
vcoubard 1131:692ddf04fc42 1034 params.advertisingDataLen = advertisingDataLen;
vcoubard 1131:692ddf04fc42 1035 params.advertisingData = advertisingData;
vcoubard 1131:692ddf04fc42 1036 onAdvertisementReport.call(&params);
vcoubard 1131:692ddf04fc42 1037 }
vcoubard 1131:692ddf04fc42 1038
vcoubard 1131:692ddf04fc42 1039 void processTimeoutEvent(TimeoutSource_t source) {
vcoubard 1131:692ddf04fc42 1040 if (timeoutCallbackChain) {
vcoubard 1131:692ddf04fc42 1041 timeoutCallbackChain(source);
vcoubard 1131:692ddf04fc42 1042 }
vcoubard 1131:692ddf04fc42 1043 }
vcoubard 1131:692ddf04fc42 1044
vcoubard 1131:692ddf04fc42 1045 protected:
vcoubard 1131:692ddf04fc42 1046 GapAdvertisingParams _advParams;
vcoubard 1131:692ddf04fc42 1047 GapAdvertisingData _advPayload;
vcoubard 1131:692ddf04fc42 1048 GapScanningParams _scanningParams;
vcoubard 1131:692ddf04fc42 1049 GapAdvertisingData _scanResponse;
vcoubard 1131:692ddf04fc42 1050
vcoubard 1131:692ddf04fc42 1051 GapState_t state;
vcoubard 1131:692ddf04fc42 1052 bool scanningActive;
vcoubard 1131:692ddf04fc42 1053
vcoubard 1131:692ddf04fc42 1054 protected:
vcoubard 1131:692ddf04fc42 1055 TimeoutEventCallbackChain_t timeoutCallbackChain;
vcoubard 1131:692ddf04fc42 1056 RadioNotificationEventCallback_t radioNotificationCallback;
vcoubard 1131:692ddf04fc42 1057 AdvertisementReportCallback_t onAdvertisementReport;
vcoubard 1131:692ddf04fc42 1058 ConnectionEventCallbackChain_t connectionCallChain;
vcoubard 1131:692ddf04fc42 1059 DisconnectionEventCallbackChain_t disconnectionCallChain;
vcoubard 1131:692ddf04fc42 1060
vcoubard 1131:692ddf04fc42 1061 private:
vcoubard 1131:692ddf04fc42 1062 /* Disallow copy and assignment. */
vcoubard 1131:692ddf04fc42 1063 Gap(const Gap &);
vcoubard 1131:692ddf04fc42 1064 Gap& operator=(const Gap &);
vcoubard 1131:692ddf04fc42 1065 };
vcoubard 1131:692ddf04fc42 1066
rgrover1 710:b2e1a2660ec2 1067 #endif // ifndef __GAP_H__