High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Jun 19 15:53:06 2015 +0100
Revision:
706:bb924c6a3dd2
Parent:
704:7bafc89799c7
Child:
710:b2e1a2660ec2
Synchronized with git rev 9183583f
Author: Rohit Grover
disconnect() should take a connection handle.

Who changed what in which revision?

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