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:04 2015 +0100
Revision:
689:08b9c561c62f
Parent:
688:10d28b67a7d5
Child:
700:99872678bbf9
Synchronized with git rev ebd6bbce
Author: Rohit Grover
introduce SecurityManager

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 567:e4b38e43de7c 77 typedef uint16_t Handle_t;
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 567:e4b38e43de7c 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 680:dabce2969adf 147 * These functions must be defined 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 680:dabce2969adf 156 virtual ble_error_t setAddress(AddressType_t type, const Address_t address) = 0;
rgrover1 680:dabce2969adf 157
rgrover1 680:dabce2969adf 158 /**
rgrover1 680:dabce2969adf 159 * Fetch the BTLE MAC address and type.
rgrover1 680:dabce2969adf 160 *
rgrover1 680:dabce2969adf 161 * @return BLE_ERROR_NONE on success.
rgrover1 680:dabce2969adf 162 */
rgrover1 680:dabce2969adf 163 virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address) = 0;
rgrover1 680:dabce2969adf 164
rgrover1 680:dabce2969adf 165 /**
rgrover1 680:dabce2969adf 166 * @return Minimum Advertising interval in milliseconds.
rgrover1 680:dabce2969adf 167 */
rgrover1 680:dabce2969adf 168 virtual uint16_t getMinAdvertisingInterval(void) const = 0;
rgrover1 680:dabce2969adf 169
rgrover1 680:dabce2969adf 170 /**
rgrover1 680:dabce2969adf 171 * @return Minimum Advertising interval in milliseconds for non-connectible mode.
rgrover1 680:dabce2969adf 172 */
rgrover1 680:dabce2969adf 173 virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const = 0;
rgrover1 680:dabce2969adf 174
rgrover1 680:dabce2969adf 175 /**
rgrover1 680:dabce2969adf 176 * @return Maximum Advertising interval in milliseconds.
rgrover1 680:dabce2969adf 177 */
rgrover1 680:dabce2969adf 178 virtual uint16_t getMaxAdvertisingInterval(void) const = 0;
rgrover1 680:dabce2969adf 179
rgrover1 680:dabce2969adf 180 virtual ble_error_t stopAdvertising(void) = 0;
rgrover1 680:dabce2969adf 181
rgrover1 680:dabce2969adf 182 /**
rgrover1 680:dabce2969adf 183 * Stop scanning. The current scanning parameters remain in effect.
rgrover1 680:dabce2969adf 184 *
rgrover1 680:dabce2969adf 185 * @retval BLE_ERROR_NONE if successfully stopped scanning procedure.
rgrover1 680:dabce2969adf 186 */
rgrover1 680:dabce2969adf 187 virtual ble_error_t stopScan() = 0;
rgrover1 680:dabce2969adf 188
rgrover1 680:dabce2969adf 189 /**
rgrover1 680:dabce2969adf 190 * Create a connection (GAP Link Establishment).
rgrover1 680:dabce2969adf 191 *
rgrover1 680:dabce2969adf 192 * @param peerAddr
rgrover1 680:dabce2969adf 193 * 48-bit address, LSB format.
rgrover1 680:dabce2969adf 194 * @param peerAddrType
rgrover1 680:dabce2969adf 195 * Address type of the peer.
rgrover1 680:dabce2969adf 196 * @param connectionParams
rgrover1 680:dabce2969adf 197 * Connection parameters.
rgrover1 680:dabce2969adf 198 * @param scanParams
rgrover1 680:dabce2969adf 199 * Paramters to be used while scanning for the peer.
rgrover1 680:dabce2969adf 200 * @return BLE_ERROR_NONE if connection establishment procedure is started
rgrover1 686:1962fb48d9ef 201 * successfully. The connectionCallback (if set) will be invoked upon
rgrover1 680:dabce2969adf 202 * a connection event.
rgrover1 680:dabce2969adf 203 */
rgrover1 670:5e4aecd9af5b 204 virtual ble_error_t connect(const Address_t peerAddr,
rgrover1 670:5e4aecd9af5b 205 Gap::AddressType_t peerAddrType,
rgrover1 670:5e4aecd9af5b 206 const ConnectionParams_t *connectionParams,
rgrover1 670:5e4aecd9af5b 207 const GapScanningParams *scanParams) = 0;
rgrover1 680:dabce2969adf 208
rgrover1 680:dabce2969adf 209 /**
rgrover1 680:dabce2969adf 210 * This call initiates the disconnection procedure, and its completion will
rgrover1 680:dabce2969adf 211 * be communicated to the application with an invocation of the
rgrover1 687:85b0a8f457a1 212 * disconnectionCallback.
rgrover1 680:dabce2969adf 213 *
rgrover1 680:dabce2969adf 214 * @param reason
rgrover1 680:dabce2969adf 215 * The reason for disconnection to be sent back to the peer.
rgrover1 680:dabce2969adf 216 */
rgrover1 680:dabce2969adf 217 virtual ble_error_t disconnect(DisconnectionReason_t reason) = 0;
rgrover1 537:00d5affbb2b2 218
rgrover1 680:dabce2969adf 219 /**
rgrover1 680:dabce2969adf 220 * Get the GAP peripheral preferred connection parameters. These are the
rgrover1 680:dabce2969adf 221 * defaults that the peripheral would like to have in a connection. The
rgrover1 680:dabce2969adf 222 * choice of the connection parameters is eventually up to the central.
rgrover1 680:dabce2969adf 223 *
rgrover1 680:dabce2969adf 224 * @param[out] params
rgrover1 680:dabce2969adf 225 * The structure where the parameters will be stored. Memory
rgrover1 680:dabce2969adf 226 * for this is owned by the caller.
rgrover1 680:dabce2969adf 227 *
rgrover1 680:dabce2969adf 228 * @return BLE_ERROR_NONE if the parameters were successfully filled into
rgrover1 680:dabce2969adf 229 * the given structure pointed to by params.
rgrover1 680:dabce2969adf 230 */
rgrover1 680:dabce2969adf 231 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0;
rgrover1 680:dabce2969adf 232
rgrover1 680:dabce2969adf 233 /**
rgrover1 680:dabce2969adf 234 * Set the GAP peripheral preferred connection parameters. These are the
rgrover1 680:dabce2969adf 235 * defaults that the peripheral would like to have in a connection. The
rgrover1 680:dabce2969adf 236 * choice of the connection parameters is eventually up to the central.
rgrover1 680:dabce2969adf 237 *
rgrover1 680:dabce2969adf 238 * @param[in] params
rgrover1 680:dabce2969adf 239 * The structure containing the desired parameters.
rgrover1 680:dabce2969adf 240 */
rgrover1 680:dabce2969adf 241 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0;
rgrover1 680:dabce2969adf 242
rgrover1 680:dabce2969adf 243 /**
rgrover1 680:dabce2969adf 244 * Update connection parameters while in the peripheral role.
rgrover1 680:dabce2969adf 245 * @details In the peripheral role, this will send the corresponding L2CAP request to the connected peer and wait for
rgrover1 680:dabce2969adf 246 * the central to perform the procedure.
rgrover1 680:dabce2969adf 247 * @param[in] handle
rgrover1 680:dabce2969adf 248 * Connection Handle
rgrover1 680:dabce2969adf 249 * @param[in] params
rgrover1 680:dabce2969adf 250 * Pointer to desired connection parameters. If NULL is provided on a peripheral role,
rgrover1 680:dabce2969adf 251 * the parameters in the PPCP characteristic of the GAP service will be used instead.
rgrover1 680:dabce2969adf 252 */
rgrover1 680:dabce2969adf 253 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0;
rgrover1 680:dabce2969adf 254
rgrover1 680:dabce2969adf 255 /**
rgrover1 680:dabce2969adf 256 * Set the device name characteristic in the GAP service.
rgrover1 680:dabce2969adf 257 * @param[in] deviceName
rgrover1 680:dabce2969adf 258 * The new value for the device-name. This is a UTF-8 encoded, <b>NULL-terminated</b> string.
rgrover1 680:dabce2969adf 259 */
rgrover1 680:dabce2969adf 260 virtual ble_error_t setDeviceName(const uint8_t *deviceName) = 0;
rgrover1 680:dabce2969adf 261
rgrover1 680:dabce2969adf 262 /**
rgrover1 680:dabce2969adf 263 * Get the value of the device name characteristic in the GAP service.
rgrover1 680:dabce2969adf 264 * @param[out] deviceName
rgrover1 680:dabce2969adf 265 * Pointer to an empty buffer where the UTF-8 *non NULL-
rgrover1 680:dabce2969adf 266 * terminated* string will be placed. Set this
rgrover1 680:dabce2969adf 267 * value to NULL in order to obtain the deviceName-length
rgrover1 680:dabce2969adf 268 * from the 'length' parameter.
rgrover1 680:dabce2969adf 269 *
rgrover1 680:dabce2969adf 270 * @param[in/out] lengthP
rgrover1 680:dabce2969adf 271 * (on input) Length of the buffer pointed to by deviceName;
rgrover1 680:dabce2969adf 272 * (on output) the complete device name length (without the
rgrover1 680:dabce2969adf 273 * null terminator).
rgrover1 680:dabce2969adf 274 *
rgrover1 680:dabce2969adf 275 * @note If the device name is longer than the size of the supplied buffer,
rgrover1 680:dabce2969adf 276 * length will return the complete device name length, and not the
rgrover1 680:dabce2969adf 277 * number of bytes actually returned in deviceName. The application may
rgrover1 680:dabce2969adf 278 * use this information to retry with a suitable buffer size.
rgrover1 680:dabce2969adf 279 */
rgrover1 567:e4b38e43de7c 280 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) = 0;
rgrover1 680:dabce2969adf 281
rgrover1 680:dabce2969adf 282 /**
rgrover1 680:dabce2969adf 283 * Set the appearance characteristic in the GAP service.
rgrover1 680:dabce2969adf 284 * @param[in] appearance
rgrover1 680:dabce2969adf 285 * The new value for the device-appearance.
rgrover1 680:dabce2969adf 286 */
rgrover1 680:dabce2969adf 287 virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance) = 0;
rgrover1 680:dabce2969adf 288
rgrover1 680:dabce2969adf 289 /**
rgrover1 680:dabce2969adf 290 * Get the appearance characteristic in the GAP service.
rgrover1 680:dabce2969adf 291 * @param[out] appearance
rgrover1 680:dabce2969adf 292 * The new value for the device-appearance.
rgrover1 680:dabce2969adf 293 */
rgrover1 680:dabce2969adf 294 virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP) = 0;
rgrover1 534:754131484cf1 295
rgrover1 680:dabce2969adf 296 /**
rgrover1 680:dabce2969adf 297 * Set the radio's transmit power.
rgrover1 680:dabce2969adf 298 * @param[in] txPower Radio transmit power in dBm.
rgrover1 680:dabce2969adf 299 */
rgrover1 680:dabce2969adf 300 virtual ble_error_t setTxPower(int8_t txPower) = 0;
rgrover1 680:dabce2969adf 301
rgrover1 680:dabce2969adf 302 /**
rgrover1 680:dabce2969adf 303 * Query the underlying stack for permitted arguments for setTxPower().
rgrover1 680:dabce2969adf 304 *
rgrover1 680:dabce2969adf 305 * @param[out] valueArrayPP
rgrover1 680:dabce2969adf 306 * Out parameter to receive the immutable array of Tx values.
rgrover1 680:dabce2969adf 307 * @param[out] countP
rgrover1 680:dabce2969adf 308 * Out parameter to receive the array's size.
rgrover1 680:dabce2969adf 309 */
rgrover1 680:dabce2969adf 310 virtual void getPermittedTxPowerValues(const int8_t **, size_t *) = 0;
rgrover1 669:7179b4a5aa7d 311
rgrover1 685:676b116f7be2 312 public:
rgrover1 685:676b116f7be2 313 virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) = 0;
rgrover1 685:676b116f7be2 314
rgrover1 677:09070e7285e5 315 /*
rgrover1 680:dabce2969adf 316 * APIs with non-virtual implementations.
rgrover1 677:09070e7285e5 317 */
rgrover1 677:09070e7285e5 318 public:
rgrover1 680:dabce2969adf 319 /**
rgrover1 680:dabce2969adf 320 * Returns the current GAP state of the device using a bitmask which
rgrover1 680:dabce2969adf 321 * describes whether the device is advertising and/or connected.
rgrover1 680:dabce2969adf 322 */
rgrover1 680:dabce2969adf 323 GapState_t getState(void) const {
rgrover1 680:dabce2969adf 324 return state;
rgrover1 680:dabce2969adf 325 }
rgrover1 680:dabce2969adf 326
rgrover1 680:dabce2969adf 327 /**
rgrover1 680:dabce2969adf 328 * Set the GAP advertising mode to use for this device.
rgrover1 680:dabce2969adf 329 */
rgrover1 680:dabce2969adf 330 void setAdvertisingType(GapAdvertisingParams::AdvertisingType_t advType) {
rgrover1 677:09070e7285e5 331 _advParams.setAdvertisingType(advType);
rgrover1 677:09070e7285e5 332 }
rgrover1 677:09070e7285e5 333
rgrover1 680:dabce2969adf 334 /**
rgrover1 680:dabce2969adf 335 * @param[in] interval
rgrover1 680:dabce2969adf 336 * Advertising interval in units of milliseconds. Advertising
rgrover1 680:dabce2969adf 337 * is disabled if interval is 0. If interval is smaller than
rgrover1 680:dabce2969adf 338 * the minimum supported value, then the minimum supported
rgrover1 680:dabce2969adf 339 * value is used instead. This minimum value can be discovered
rgrover1 680:dabce2969adf 340 * using getMinAdvertisingInterval().
rgrover1 680:dabce2969adf 341 *
rgrover1 680:dabce2969adf 342 * This field must be set to 0 if connectionMode is equal
rgrover1 680:dabce2969adf 343 * to ADV_CONNECTABLE_DIRECTED.
rgrover1 680:dabce2969adf 344 *
rgrover1 680:dabce2969adf 345 * @note: Decreasing this value will allow central devices to detect a
rgrover1 680:dabce2969adf 346 * peripheral faster at the expense of more power being used by the radio
rgrover1 680:dabce2969adf 347 * due to the higher data transmit rate.
rgrover1 680:dabce2969adf 348 *
rgrover1 680:dabce2969adf 349 * @note: This API is now *deprecated* and will be dropped in the future.
rgrover1 680:dabce2969adf 350 * You should use the parallel API from Gap directly. A former call to
rgrover1 680:dabce2969adf 351 * ble.setAdvertisingInterval(...) should now be achieved using
rgrover1 680:dabce2969adf 352 * ble.gap().setAdvertisingInterval(...).
rgrover1 680:dabce2969adf 353 *
rgrover1 680:dabce2969adf 354 * @Note: [WARNING] This API previously used 0.625ms as the unit for its
rgrover1 680:dabce2969adf 355 * 'interval' argument. That required an explicit conversion from
rgrover1 680:dabce2969adf 356 * milliseconds using Gap::MSEC_TO_GAP_DURATION_UNITS(). This conversion is
rgrover1 680:dabce2969adf 357 * no longer required as the new units are milliseconds. Any application
rgrover1 680:dabce2969adf 358 * code depending on the old semantics would need to be updated accordingly.
rgrover1 680:dabce2969adf 359 */
rgrover1 677:09070e7285e5 360 void setAdvertisingInterval(uint16_t interval) {
rgrover1 677:09070e7285e5 361 if (interval == 0) {
rgrover1 677:09070e7285e5 362 stopAdvertising();
rgrover1 677:09070e7285e5 363 } else if (interval < getMinAdvertisingInterval()) {
rgrover1 677:09070e7285e5 364 interval = getMinAdvertisingInterval();
rgrover1 677:09070e7285e5 365 }
rgrover1 677:09070e7285e5 366 _advParams.setInterval(MSEC_TO_ADVERTISEMENT_DURATION_UNITS(interval));
rgrover1 677:09070e7285e5 367 }
rgrover1 677:09070e7285e5 368
rgrover1 680:dabce2969adf 369 /**
rgrover1 680:dabce2969adf 370 * @param[in] timeout
rgrover1 680:dabce2969adf 371 * Advertising timeout (in seconds) between 0x1 and 0x3FFF (1
rgrover1 680:dabce2969adf 372 * and 16383). Use 0 to disable the advertising timeout.
rgrover1 680:dabce2969adf 373 */
rgrover1 680:dabce2969adf 374 void setAdvertisingTimeout(uint16_t timeout) {
rgrover1 680:dabce2969adf 375 _advParams.setTimeout(timeout);
rgrover1 680:dabce2969adf 376 }
rgrover1 680:dabce2969adf 377
rgrover1 680:dabce2969adf 378 /**
rgrover1 680:dabce2969adf 379 * Start advertising.
rgrover1 680:dabce2969adf 380 */
rgrover1 674:923c47bd1547 381 ble_error_t startAdvertising(void) {
rgrover1 677:09070e7285e5 382 setAdvertisingData(); /* update the underlying stack */
rgrover1 675:30aab0f789f6 383 return startAdvertising(_advParams);
rgrover1 674:923c47bd1547 384 }
rgrover1 674:923c47bd1547 385
rgrover1 677:09070e7285e5 386 /**
rgrover1 677:09070e7285e5 387 * Reset any advertising payload prepared from prior calls to
rgrover1 680:dabce2969adf 388 * accumulateAdvertisingPayload(). This automatically propagates the re-
rgrover1 680:dabce2969adf 389 * initialized adv payload to the underlying stack.
rgrover1 677:09070e7285e5 390 *
rgrover1 677:09070e7285e5 391 * Note: This should be followed by a call to setAdvertisingPayload() or
rgrover1 677:09070e7285e5 392 * startAdvertising() before the update takes effect.
rgrover1 677:09070e7285e5 393 */
rgrover1 677:09070e7285e5 394 void clearAdvertisingPayload(void) {
rgrover1 677:09070e7285e5 395 _advPayload.clear();
rgrover1 680:dabce2969adf 396 setAdvertisingData();
rgrover1 677:09070e7285e5 397 }
rgrover1 677:09070e7285e5 398
rgrover1 677:09070e7285e5 399 /**
rgrover1 677:09070e7285e5 400 * Accumulate an AD structure in the advertising payload. Please note that
rgrover1 677:09070e7285e5 401 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
rgrover1 677:09070e7285e5 402 * as an additional 31 bytes if the advertising payload proves to be too
rgrover1 677:09070e7285e5 403 * small.
rgrover1 677:09070e7285e5 404 *
rgrover1 680:dabce2969adf 405 * @param[in] flags
rgrover1 680:dabce2969adf 406 * The flags to be added. Please refer to
rgrover1 680:dabce2969adf 407 * GapAdvertisingData::Flags for valid flags. Multiple
rgrover1 680:dabce2969adf 408 * flags may be specified in combination.
rgrover1 677:09070e7285e5 409 */
rgrover1 677:09070e7285e5 410 ble_error_t accumulateAdvertisingPayload(uint8_t flags) {
rgrover1 680:dabce2969adf 411 ble_error_t rc;
rgrover1 680:dabce2969adf 412 if ((rc = _advPayload.addFlags(flags)) != BLE_ERROR_NONE) {
rgrover1 680:dabce2969adf 413 return rc;
rgrover1 680:dabce2969adf 414 }
rgrover1 680:dabce2969adf 415
rgrover1 680:dabce2969adf 416 return setAdvertisingData();
rgrover1 677:09070e7285e5 417 }
rgrover1 677:09070e7285e5 418
rgrover1 677:09070e7285e5 419 /**
rgrover1 677:09070e7285e5 420 * Accumulate an AD structure in the advertising payload. Please note that
rgrover1 677:09070e7285e5 421 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
rgrover1 677:09070e7285e5 422 * as an additional 31 bytes if the advertising payload proves to be too
rgrover1 677:09070e7285e5 423 * small.
rgrover1 677:09070e7285e5 424 *
rgrover1 677:09070e7285e5 425 * @param app
rgrover1 677:09070e7285e5 426 * The appearance of the peripheral.
rgrover1 677:09070e7285e5 427 */
rgrover1 677:09070e7285e5 428 ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::Appearance app) {
rgrover1 677:09070e7285e5 429 setAppearance(app);
rgrover1 680:dabce2969adf 430
rgrover1 680:dabce2969adf 431 ble_error_t rc;
rgrover1 680:dabce2969adf 432 if ((rc = _advPayload.addAppearance(app)) != BLE_ERROR_NONE) {
rgrover1 680:dabce2969adf 433 return rc;
rgrover1 680:dabce2969adf 434 }
rgrover1 680:dabce2969adf 435
rgrover1 680:dabce2969adf 436 return setAdvertisingData();
rgrover1 677:09070e7285e5 437 }
rgrover1 677:09070e7285e5 438
rgrover1 677:09070e7285e5 439 /**
rgrover1 677:09070e7285e5 440 * Accumulate an AD structure in the advertising payload. Please note that
rgrover1 677:09070e7285e5 441 * the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
rgrover1 677:09070e7285e5 442 * as an additional 31 bytes if the advertising payload proves to be too
rgrover1 677:09070e7285e5 443 * small.
rgrover1 677:09070e7285e5 444 *
rgrover1 677:09070e7285e5 445 * @param app
rgrover1 677:09070e7285e5 446 * The max transmit power to be used by the controller. This is
rgrover1 677:09070e7285e5 447 * only a hint.
rgrover1 677:09070e7285e5 448 */
rgrover1 677:09070e7285e5 449 ble_error_t accumulateAdvertisingPayloadTxPower(int8_t power) {
rgrover1 680:dabce2969adf 450 ble_error_t rc;
rgrover1 680:dabce2969adf 451 if ((rc = _advPayload.addTxPower(power)) != BLE_ERROR_NONE) {
rgrover1 680:dabce2969adf 452 return rc;
rgrover1 680:dabce2969adf 453 }
rgrover1 680:dabce2969adf 454
rgrover1 680:dabce2969adf 455 return setAdvertisingData();
rgrover1 677:09070e7285e5 456 }
rgrover1 677:09070e7285e5 457
rgrover1 677:09070e7285e5 458 /**
rgrover1 677:09070e7285e5 459 * Accumulate a variable length byte-stream as an AD structure in the
rgrover1 677:09070e7285e5 460 * advertising payload. Please note that the payload is limited to 31 bytes.
rgrover1 677:09070e7285e5 461 * The SCAN_RESPONSE message may be used as an additional 31 bytes if the
rgrover1 677:09070e7285e5 462 * advertising payload proves to be too small.
rgrover1 677:09070e7285e5 463 *
rgrover1 677:09070e7285e5 464 * @param type The type which describes the variable length data.
rgrover1 677:09070e7285e5 465 * @param data data bytes.
rgrover1 677:09070e7285e5 466 * @param len length of data.
rgrover1 677:09070e7285e5 467 */
rgrover1 677:09070e7285e5 468 ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
rgrover1 677:09070e7285e5 469 if (type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
rgrover1 677:09070e7285e5 470 setDeviceName(data);
rgrover1 677:09070e7285e5 471 }
rgrover1 680:dabce2969adf 472
rgrover1 680:dabce2969adf 473 ble_error_t rc;
rgrover1 680:dabce2969adf 474 if ((rc = _advPayload.addData(type, data, len)) != BLE_ERROR_NONE) {
rgrover1 680:dabce2969adf 475 return rc;
rgrover1 680:dabce2969adf 476 }
rgrover1 680:dabce2969adf 477
rgrover1 680:dabce2969adf 478 return setAdvertisingData();
rgrover1 680:dabce2969adf 479 }
rgrover1 680:dabce2969adf 480
rgrover1 680:dabce2969adf 481 /**
rgrover1 680:dabce2969adf 482 * Setup a particular, user-constructed advertisement payload for the
rgrover1 680:dabce2969adf 483 * underlying stack. It would be uncommon for this API to be used directly;
rgrover1 680:dabce2969adf 484 * there are other APIs to build an advertisement payload (see above).
rgrover1 680:dabce2969adf 485 */
rgrover1 680:dabce2969adf 486 ble_error_t setAdvertisingPayload(const GapAdvertisingData &payload) {
rgrover1 680:dabce2969adf 487 _advPayload = payload;
rgrover1 680:dabce2969adf 488 return setAdvertisingData();
rgrover1 680:dabce2969adf 489 }
rgrover1 680:dabce2969adf 490
rgrover1 680:dabce2969adf 491 /**
rgrover1 680:dabce2969adf 492 * @return Read back advertising data. Useful for storing and
rgrover1 680:dabce2969adf 493 * restoring payload.
rgrover1 680:dabce2969adf 494 */
rgrover1 680:dabce2969adf 495 const GapAdvertisingData &getAdvertisingPayload(void) const {
rgrover1 680:dabce2969adf 496 return _advPayload;
rgrover1 677:09070e7285e5 497 }
rgrover1 677:09070e7285e5 498
rgrover1 677:09070e7285e5 499 /**
rgrover1 677:09070e7285e5 500 * Accumulate a variable length byte-stream as an AD structure in the
rgrover1 677:09070e7285e5 501 * scanResponse payload.
rgrover1 677:09070e7285e5 502 *
rgrover1 680:dabce2969adf 503 * @param[in] type The type which describes the variable length data.
rgrover1 680:dabce2969adf 504 * @param[in] data data bytes.
rgrover1 680:dabce2969adf 505 * @param[in] len length of data.
rgrover1 677:09070e7285e5 506 */
rgrover1 677:09070e7285e5 507 ble_error_t accumulateScanResponse(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
rgrover1 680:dabce2969adf 508 ble_error_t rc;
rgrover1 680:dabce2969adf 509 if ((rc = _scanResponse.addData(type, data, len)) != BLE_ERROR_NONE) {
rgrover1 680:dabce2969adf 510 return rc;
rgrover1 680:dabce2969adf 511 }
rgrover1 680:dabce2969adf 512
rgrover1 680:dabce2969adf 513 return setAdvertisingData();
rgrover1 677:09070e7285e5 514 }
rgrover1 677:09070e7285e5 515
rgrover1 677:09070e7285e5 516 /**
rgrover1 677:09070e7285e5 517 * Reset any scan response prepared from prior calls to
rgrover1 677:09070e7285e5 518 * accumulateScanResponse().
rgrover1 677:09070e7285e5 519 *
rgrover1 677:09070e7285e5 520 * Note: This should be followed by a call to setAdvertisingPayload() or
rgrover1 677:09070e7285e5 521 * startAdvertising() before the update takes effect.
rgrover1 677:09070e7285e5 522 */
rgrover1 677:09070e7285e5 523 void clearScanResponse(void) {
rgrover1 677:09070e7285e5 524 _scanResponse.clear();
rgrover1 680:dabce2969adf 525 setAdvertisingData();
rgrover1 680:dabce2969adf 526 }
rgrover1 680:dabce2969adf 527
rgrover1 680:dabce2969adf 528 /**
rgrover1 680:dabce2969adf 529 * Setup parameters for GAP scanning--i.e. observer mode.
rgrover1 680:dabce2969adf 530 * @param[in] interval
rgrover1 680:dabce2969adf 531 * Scan interval (in milliseconds) [valid values lie between 2.5ms and 10.24s].
rgrover1 680:dabce2969adf 532 * @param[in] window
rgrover1 680:dabce2969adf 533 * Scan Window (in milliseconds) [valid values lie between 2.5ms and 10.24s].
rgrover1 680:dabce2969adf 534 * @param[in] timeout
rgrover1 680:dabce2969adf 535 * Scan timeout (in seconds) between 0x0001 and 0xFFFF, 0x0000 disables timeout.
rgrover1 680:dabce2969adf 536 * @param[in] activeScanning
rgrover1 680:dabce2969adf 537 * Set to True if active-scanning is required. This is used to fetch the
rgrover1 680:dabce2969adf 538 * scan response from a peer if possible.
rgrover1 680:dabce2969adf 539 *
rgrover1 680:dabce2969adf 540 * The scanning window divided by the interval determines the duty cycle for
rgrover1 680:dabce2969adf 541 * scanning. For example, if the interval is 100ms and the window is 10ms,
rgrover1 680:dabce2969adf 542 * then the controller will scan for 10 percent of the time. It is possible
rgrover1 680:dabce2969adf 543 * to have the interval and window set to the same value. In this case,
rgrover1 680:dabce2969adf 544 * scanning is continuous, with a change of scanning frequency once every
rgrover1 680:dabce2969adf 545 * interval.
rgrover1 680:dabce2969adf 546 *
rgrover1 680:dabce2969adf 547 * Once the scanning parameters have been configured, scanning can be
rgrover1 680:dabce2969adf 548 * enabled by using startScan().
rgrover1 680:dabce2969adf 549 *
rgrover1 680:dabce2969adf 550 * @Note: The scan interval and window are recommendations to the BLE stack.
rgrover1 680:dabce2969adf 551 */
rgrover1 680:dabce2969adf 552 ble_error_t setScanParams(uint16_t interval = GapScanningParams::SCAN_INTERVAL_MAX,
rgrover1 680:dabce2969adf 553 uint16_t window = GapScanningParams::SCAN_WINDOW_MAX,
rgrover1 680:dabce2969adf 554 uint16_t timeout = 0,
rgrover1 680:dabce2969adf 555 bool activeScanning = false) {
rgrover1 680:dabce2969adf 556 ble_error_t rc;
rgrover1 680:dabce2969adf 557 if (((rc = _scanningParams.setInterval(interval)) == BLE_ERROR_NONE) &&
rgrover1 680:dabce2969adf 558 ((rc = _scanningParams.setWindow(window)) == BLE_ERROR_NONE) &&
rgrover1 680:dabce2969adf 559 ((rc = _scanningParams.setTimeout(timeout)) == BLE_ERROR_NONE)) {
rgrover1 680:dabce2969adf 560 _scanningParams.setActiveScanning(activeScanning);
rgrover1 680:dabce2969adf 561 return BLE_ERROR_NONE;
rgrover1 680:dabce2969adf 562 }
rgrover1 680:dabce2969adf 563
rgrover1 680:dabce2969adf 564 return rc;
rgrover1 677:09070e7285e5 565 }
rgrover1 677:09070e7285e5 566
rgrover1 680:dabce2969adf 567 /**
rgrover1 680:dabce2969adf 568 * Setup the scanInterval parameter for GAP scanning--i.e. observer mode.
rgrover1 680:dabce2969adf 569 * @param[in] interval
rgrover1 680:dabce2969adf 570 * Scan interval (in milliseconds) [valid values lie between 2.5ms and 10.24s].
rgrover1 680:dabce2969adf 571 *
rgrover1 680:dabce2969adf 572 * The scanning window divided by the interval determines the duty cycle for
rgrover1 680:dabce2969adf 573 * scanning. For example, if the interval is 100ms and the window is 10ms,
rgrover1 680:dabce2969adf 574 * then the controller will scan for 10 percent of the time. It is possible
rgrover1 680:dabce2969adf 575 * to have the interval and window set to the same value. In this case,
rgrover1 680:dabce2969adf 576 * scanning is continuous, with a change of scanning frequency once every
rgrover1 680:dabce2969adf 577 * interval.
rgrover1 680:dabce2969adf 578 *
rgrover1 680:dabce2969adf 579 * Once the scanning parameters have been configured, scanning can be
rgrover1 680:dabce2969adf 580 * enabled by using startScan().
rgrover1 680:dabce2969adf 581 */
rgrover1 680:dabce2969adf 582 ble_error_t setScanInterval(uint16_t interval) {
rgrover1 680:dabce2969adf 583 return _scanningParams.setInterval(interval);
rgrover1 680:dabce2969adf 584 }
rgrover1 674:923c47bd1547 585
rgrover1 680:dabce2969adf 586 /**
rgrover1 680:dabce2969adf 587 * Setup the scanWindow parameter for GAP scanning--i.e. observer mode.
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 *
rgrover1 680:dabce2969adf 591 * The scanning window divided by the interval determines the duty cycle for
rgrover1 680:dabce2969adf 592 * scanning. For example, if the interval is 100ms and the window is 10ms,
rgrover1 680:dabce2969adf 593 * then the controller will scan for 10 percent of the time. It is possible
rgrover1 680:dabce2969adf 594 * to have the interval and window set to the same value. In this case,
rgrover1 680:dabce2969adf 595 * scanning is continuous, with a change of scanning frequency once every
rgrover1 680:dabce2969adf 596 * interval.
rgrover1 680:dabce2969adf 597 *
rgrover1 680:dabce2969adf 598 * Once the scanning parameters have been configured, scanning can be
rgrover1 680:dabce2969adf 599 * enabled by using startScan().
rgrover1 680:dabce2969adf 600 */
rgrover1 680:dabce2969adf 601 ble_error_t setScanWindow(uint16_t window) {
rgrover1 680:dabce2969adf 602 return _scanningParams.setWindow(window);
rgrover1 680:dabce2969adf 603 }
rgrover1 680:dabce2969adf 604
rgrover1 680:dabce2969adf 605 /**
rgrover1 680:dabce2969adf 606 * Setup parameters for GAP scanning--i.e. observer mode.
rgrover1 680:dabce2969adf 607 * @param[in] timeout
rgrover1 680:dabce2969adf 608 * Scan timeout (in seconds) between 0x0001 and 0xFFFF, 0x0000 disables timeout.
rgrover1 680:dabce2969adf 609 *
rgrover1 680:dabce2969adf 610 * Once the scanning parameters have been configured, scanning can be
rgrover1 680:dabce2969adf 611 * enabled by using startScan().
rgrover1 680:dabce2969adf 612 */
rgrover1 680:dabce2969adf 613 ble_error_t setScanTimeout(uint16_t timeout) {
rgrover1 680:dabce2969adf 614 return _scanningParams.setTimeout(timeout);
rgrover1 680:dabce2969adf 615 }
rgrover1 680:dabce2969adf 616
rgrover1 680:dabce2969adf 617 /**
rgrover1 680:dabce2969adf 618 * Setup parameters for GAP scanning--i.e. observer mode.
rgrover1 680:dabce2969adf 619 * @param[in] activeScanning
rgrover1 680:dabce2969adf 620 * Set to True if active-scanning is required. This is used to fetch the
rgrover1 680:dabce2969adf 621 * scan response from a peer if possible.
rgrover1 680:dabce2969adf 622 *
rgrover1 680:dabce2969adf 623 * Once the scanning parameters have been configured, scanning can be
rgrover1 680:dabce2969adf 624 * enabled by using startScan().
rgrover1 680:dabce2969adf 625 */
rgrover1 680:dabce2969adf 626 void setActiveScanning(bool activeScanning) {
rgrover1 680:dabce2969adf 627 _scanningParams.setActiveScanning(activeScanning);
rgrover1 680:dabce2969adf 628 }
rgrover1 680:dabce2969adf 629
rgrover1 680:dabce2969adf 630 /**
rgrover1 680:dabce2969adf 631 * Start scanning (Observer Procedure) based on the parameters currently in
rgrover1 680:dabce2969adf 632 * effect.
rgrover1 680:dabce2969adf 633 *
rgrover1 680:dabce2969adf 634 * @param[in] callback
rgrover1 680:dabce2969adf 635 * The application specific callback to be invoked upon
rgrover1 680:dabce2969adf 636 * receiving every advertisement report. This can be passed in
rgrover1 680:dabce2969adf 637 * as NULL, in which case scanning may not be enabled at all.
rgrover1 680:dabce2969adf 638 */
rgrover1 674:923c47bd1547 639 ble_error_t startScan(void (*callback)(const AdvertisementCallbackParams_t *params)) {
rgrover1 407:ca6b956b33d1 640 ble_error_t err = BLE_ERROR_NONE;
rgrover1 407:ca6b956b33d1 641 if (callback) {
rgrover1 675:30aab0f789f6 642 if ((err = startRadioScan(_scanningParams)) == BLE_ERROR_NONE) {
rgrover1 407:ca6b956b33d1 643 onAdvertisementReport.attach(callback);
rgrover1 407:ca6b956b33d1 644 }
rgrover1 407:ca6b956b33d1 645 }
rgrover1 407:ca6b956b33d1 646
rgrover1 407:ca6b956b33d1 647 return err;
rgrover1 407:ca6b956b33d1 648 }
rgrover1 407:ca6b956b33d1 649
rgrover1 680:dabce2969adf 650 /**
rgrover1 680:dabce2969adf 651 * Same as above, but this takes an (object, method) pair for a callback.
rgrover1 680:dabce2969adf 652 */
rgrover1 407:ca6b956b33d1 653 template<typename T>
rgrover1 674:923c47bd1547 654 ble_error_t startScan(T *object, void (T::*callbackMember)(const AdvertisementCallbackParams_t *params)) {
rgrover1 407:ca6b956b33d1 655 ble_error_t err = BLE_ERROR_NONE;
rgrover1 407:ca6b956b33d1 656 if (object && callbackMember) {
rgrover1 675:30aab0f789f6 657 if ((err = startRadioScan(_scanningParams)) == BLE_ERROR_NONE) {
rgrover1 407:ca6b956b33d1 658 onAdvertisementReport.attach(object, callbackMember);
rgrover1 407:ca6b956b33d1 659 }
rgrover1 407:ca6b956b33d1 660 }
rgrover1 407:ca6b956b33d1 661
rgrover1 407:ca6b956b33d1 662 return err;
rgrover1 407:ca6b956b33d1 663 }
rgrover1 407:ca6b956b33d1 664
rgrover1 680:dabce2969adf 665 private:
rgrover1 680:dabce2969adf 666 ble_error_t setAdvertisingData(void) {
rgrover1 680:dabce2969adf 667 return setAdvertisingData(_advPayload, _scanResponse);
rgrover1 680:dabce2969adf 668 }
rgrover1 680:dabce2969adf 669
rgrover1 680:dabce2969adf 670 private:
rgrover1 680:dabce2969adf 671 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0;
rgrover1 680:dabce2969adf 672 virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0;
rgrover1 680:dabce2969adf 673
rgrover1 680:dabce2969adf 674 public:
rgrover1 680:dabce2969adf 675 /**
rgrover1 680:dabce2969adf 676 * Accessors to read back currently active advertising params.
rgrover1 680:dabce2969adf 677 */
rgrover1 680:dabce2969adf 678 GapAdvertisingParams &getAdvertisingParams(void) {
rgrover1 680:dabce2969adf 679 return _advParams;
rgrover1 680:dabce2969adf 680 }
rgrover1 680:dabce2969adf 681 const GapAdvertisingParams &getAdvertisingParams(void) const {
rgrover1 680:dabce2969adf 682 return _advParams;
rgrover1 680:dabce2969adf 683 }
rgrover1 680:dabce2969adf 684
rgrover1 680:dabce2969adf 685 /**
rgrover1 680:dabce2969adf 686 * Setup a particular, user-constructed set of advertisement parameters for
rgrover1 680:dabce2969adf 687 * the underlying stack. It would be uncommon for this API to be used
rgrover1 680:dabce2969adf 688 * directly; there are other APIs to tweak advertisement parameters
rgrover1 680:dabce2969adf 689 * individually.
rgrover1 680:dabce2969adf 690 */
rgrover1 680:dabce2969adf 691 void setAdvertisingParams(const GapAdvertisingParams &newParams) {
rgrover1 680:dabce2969adf 692 _advParams = newParams;
rgrover1 680:dabce2969adf 693 }
rgrover1 680:dabce2969adf 694
rgrover1 684:448cdd97e926 695 /* Event callback handlers */
rgrover1 669:7179b4a5aa7d 696 public:
rgrover1 684:448cdd97e926 697 /**
rgrover1 684:448cdd97e926 698 * Setup a callback for timeout events. Refer to TimeoutSource_t for
rgrover1 684:448cdd97e926 699 * possible event types.
rgrover1 684:448cdd97e926 700 */
rgrover1 684:448cdd97e926 701 void onTimeout(TimeoutEventCallback_t callback) {timeoutCallback = callback;}
rgrover1 684:448cdd97e926 702
rgrover1 686:1962fb48d9ef 703 /**
rgrover1 686:1962fb48d9ef 704 * Setup a callback for connection events. Refer to ConnectionEventCallback_t.
rgrover1 686:1962fb48d9ef 705 */
rgrover1 686:1962fb48d9ef 706 void onConnection(ConnectionEventCallback_t callback) {connectionCallback = callback;}
rgrover1 260:ea7f9f14cc15 707
rgrover1 260:ea7f9f14cc15 708 /**
rgrover1 260:ea7f9f14cc15 709 * Set the application callback for disconnection events.
rgrover1 260:ea7f9f14cc15 710 * @param callback
rgrover1 260:ea7f9f14cc15 711 * Pointer to the unique callback.
rgrover1 260:ea7f9f14cc15 712 */
rgrover1 687:85b0a8f457a1 713 void onDisconnection(DisconnectionEventCallback_t callback) {disconnectionCallback = callback;}
rgrover1 687:85b0a8f457a1 714
rgrover1 687:85b0a8f457a1 715 /**
rgrover1 687:85b0a8f457a1 716 * Append to a chain of callbacks to be invoked upon disconnection; these
rgrover1 687:85b0a8f457a1 717 * callbacks receive no context and are therefore different from the
rgrover1 687:85b0a8f457a1 718 * disconnectionCallback callback.
rgrover1 687:85b0a8f457a1 719 * @param callback
rgrover1 687:85b0a8f457a1 720 * function pointer to be invoked upon disconnection; receives no context.
rgrover1 687:85b0a8f457a1 721 */
rgrover1 687:85b0a8f457a1 722 template<typename T>
rgrover1 687:85b0a8f457a1 723 void addToDisconnectionCallChain(T *tptr, void (T::*mptr)(void)) {disconnectionCallChain.add(tptr, mptr);}
rgrover1 567:e4b38e43de7c 724
rgrover1 567:e4b38e43de7c 725 /**
rgrover1 567:e4b38e43de7c 726 * Set the application callback for radio-notification events.
rgrover1 688:10d28b67a7d5 727 *
rgrover1 688:10d28b67a7d5 728 * Radio Notification is a feature that enables ACTIVE and INACTIVE
rgrover1 688:10d28b67a7d5 729 * (nACTIVE) signals from the stack that notify the application when the
rgrover1 688:10d28b67a7d5 730 * radio is in use. The signal is sent using software interrupt.
rgrover1 688:10d28b67a7d5 731 *
rgrover1 688:10d28b67a7d5 732 * The ACTIVE signal is sent before the Radio Event starts. The nACTIVE
rgrover1 688:10d28b67a7d5 733 * signal is sent at the end of the Radio Event. These signals can be used
rgrover1 688:10d28b67a7d5 734 * by the application programmer to synchronize application logic with radio
rgrover1 688:10d28b67a7d5 735 * activity. For example, the ACTIVE signal can be used to shut off external
rgrover1 688:10d28b67a7d5 736 * devices to manage peak current drawn during periods when the radio is on,
rgrover1 688:10d28b67a7d5 737 * or to trigger sensor data collection for transmission in the Radio Event.
rgrover1 688:10d28b67a7d5 738 *
rgrover1 567:e4b38e43de7c 739 * @param callback
rgrover1 688:10d28b67a7d5 740 * The application handler to be invoked in response to a radio
rgrover1 688:10d28b67a7d5 741 * ACTIVE/INACTIVE event.
rgrover1 567:e4b38e43de7c 742 */
rgrover1 688:10d28b67a7d5 743 virtual void onRadioNotification(RadioNotificationEventCallback_t callback) {radioNotificationCallback = callback;}
rgrover1 567:e4b38e43de7c 744
rgrover1 260:ea7f9f14cc15 745 protected:
rgrover1 349:b8b2b3973c47 746 Gap() :
rgrover1 675:30aab0f789f6 747 _advParams(),
rgrover1 675:30aab0f789f6 748 _advPayload(),
rgrover1 675:30aab0f789f6 749 _scanningParams(),
rgrover1 675:30aab0f789f6 750 _scanResponse(),
rgrover1 349:b8b2b3973c47 751 state(),
rgrover1 684:448cdd97e926 752 timeoutCallback(NULL),
rgrover1 686:1962fb48d9ef 753 connectionCallback(NULL),
rgrover1 687:85b0a8f457a1 754 disconnectionCallback(NULL),
rgrover1 688:10d28b67a7d5 755 radioNotificationCallback(),
rgrover1 382:dcd0428dadb0 756 onAdvertisementReport(),
rgrover1 349:b8b2b3973c47 757 disconnectionCallChain() {
rgrover1 675:30aab0f789f6 758 _advPayload.clear();
rgrover1 675:30aab0f789f6 759 _scanResponse.clear();
rgrover1 260:ea7f9f14cc15 760 }
rgrover1 260:ea7f9f14cc15 761
rgrover1 260:ea7f9f14cc15 762 public:
rgrover1 670:5e4aecd9af5b 763 void processConnectionEvent(Handle_t handle,
rgrover1 670:5e4aecd9af5b 764 Role_t role,
rgrover1 670:5e4aecd9af5b 765 AddressType_t peerAddrType,
rgrover1 670:5e4aecd9af5b 766 const Address_t peerAddr,
rgrover1 670:5e4aecd9af5b 767 AddressType_t ownAddrType,
rgrover1 670:5e4aecd9af5b 768 const Address_t ownAddr,
rgrover1 670:5e4aecd9af5b 769 const ConnectionParams_t *connectionParams) {
rgrover1 260:ea7f9f14cc15 770 state.connected = 1;
rgrover1 686:1962fb48d9ef 771 if (connectionCallback) {
rgrover1 670:5e4aecd9af5b 772 ConnectionCallbackParams_t callbackParams(handle, role, peerAddrType, peerAddr, ownAddrType, ownAddr, connectionParams);
rgrover1 686:1962fb48d9ef 773 connectionCallback(&callbackParams);
rgrover1 260:ea7f9f14cc15 774 }
rgrover1 260:ea7f9f14cc15 775 }
rgrover1 260:ea7f9f14cc15 776
rgrover1 260:ea7f9f14cc15 777 void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) {
rgrover1 260:ea7f9f14cc15 778 state.connected = 0;
rgrover1 687:85b0a8f457a1 779 if (disconnectionCallback) {
rgrover1 687:85b0a8f457a1 780 disconnectionCallback(handle, reason);
rgrover1 260:ea7f9f14cc15 781 }
rgrover1 260:ea7f9f14cc15 782 disconnectionCallChain.call();
rgrover1 260:ea7f9f14cc15 783 }
rgrover1 260:ea7f9f14cc15 784
rgrover1 680:dabce2969adf 785 void processAdvertisementReport(const Address_t peerAddr,
rgrover1 680:dabce2969adf 786 int8_t rssi,
rgrover1 680:dabce2969adf 787 bool isScanResponse,
rgrover1 680:dabce2969adf 788 GapAdvertisingParams::AdvertisingType_t type,
rgrover1 680:dabce2969adf 789 uint8_t advertisingDataLen,
rgrover1 680:dabce2969adf 790 const uint8_t *advertisingData) {
rgrover1 406:cec6778acc66 791 AdvertisementCallbackParams_t params;
rgrover1 406:cec6778acc66 792 memcpy(params.peerAddr, peerAddr, ADDR_LEN);
rgrover1 406:cec6778acc66 793 params.rssi = rssi;
rgrover1 406:cec6778acc66 794 params.isScanResponse = isScanResponse;
rgrover1 406:cec6778acc66 795 params.type = type;
rgrover1 406:cec6778acc66 796 params.advertisingDataLen = advertisingDataLen;
rgrover1 406:cec6778acc66 797 params.advertisingData = advertisingData;
rgrover1 406:cec6778acc66 798 onAdvertisementReport.call(&params);
rgrover1 382:dcd0428dadb0 799 }
rgrover1 382:dcd0428dadb0 800
rgrover1 684:448cdd97e926 801 void processTimeoutEvent(TimeoutSource_t source) {
rgrover1 684:448cdd97e926 802 if (timeoutCallback) {
rgrover1 684:448cdd97e926 803 timeoutCallback(source);
rgrover1 260:ea7f9f14cc15 804 }
rgrover1 260:ea7f9f14cc15 805 }
rgrover1 260:ea7f9f14cc15 806
rgrover1 260:ea7f9f14cc15 807 protected:
rgrover1 675:30aab0f789f6 808 GapAdvertisingParams _advParams;
rgrover1 675:30aab0f789f6 809 GapAdvertisingData _advPayload;
rgrover1 675:30aab0f789f6 810 GapScanningParams _scanningParams;
rgrover1 675:30aab0f789f6 811 GapAdvertisingData _scanResponse;
rgrover1 674:923c47bd1547 812
rgrover1 363:a3d59a6e5da0 813 GapState_t state;
rgrover1 260:ea7f9f14cc15 814
rgrover1 337:e7c2eb38f5cc 815 protected:
rgrover1 684:448cdd97e926 816 TimeoutEventCallback_t timeoutCallback;
rgrover1 686:1962fb48d9ef 817 ConnectionEventCallback_t connectionCallback;
rgrover1 687:85b0a8f457a1 818 DisconnectionEventCallback_t disconnectionCallback;
rgrover1 688:10d28b67a7d5 819 RadioNotificationEventCallback_t radioNotificationCallback;
rgrover1 385:6e66d1c6de00 820 AdvertisementReportCallback_t onAdvertisementReport;
rgrover1 363:a3d59a6e5da0 821 CallChain disconnectionCallChain;
rgrover1 260:ea7f9f14cc15 822
rgrover1 260:ea7f9f14cc15 823 private:
rgrover1 260:ea7f9f14cc15 824 /* disallow copy and assignment */
rgrover1 260:ea7f9f14cc15 825 Gap(const Gap &);
rgrover1 260:ea7f9f14cc15 826 Gap& operator=(const Gap &);
rgrover1 260:ea7f9f14cc15 827 };
rgrover1 260:ea7f9f14cc15 828
rgrover1 260:ea7f9f14cc15 829 #endif // ifndef __GAP_H__