BLE

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Thu Nov 26 12:52:33 2015 +0000
Revision:
955:8b33ea639a1f
Parent:
954:f6eb43f524b2
Child:
956:9228741f4c19
Synchronized with git rev b9cde859
Author: Vincent Coubard
Improve management of onConnection callback chain

Who changed what in which revision?

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