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:
Thu Jul 02 09:06:11 2015 +0100
Revision:
714:a6130aaa0fd9
Synchronized with git rev 7e8977d8
Author: Rohit Grover
Release 0.3.8
=============

This is a minor set of enhancements before we yotta-ize BLE_API.

Enhancements
~~~~~~~~~~~~

* Minor rework for class UUID; added a default and copy constructor; and a != operator.

* Added copy constructor and accessors for GapAdvertisingParams.

* GapScanningParams:: remove unnecessary checks for SCAN_TIMEOUT_MAX.

* Add a comment header block to explain why BLEDevice::init() may not be safe
to call from global static context.

* Introduce GattAttribute::INVALID_HANDLE.

* Replace some deprecated uses of Gap::address_t with Gap::Address_t.

Bugfixes
~~~~~~~~

* None.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 714:a6130aaa0fd9 1 /* mbed Microcontroller Library
rgrover1 714:a6130aaa0fd9 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 714:a6130aaa0fd9 3 *
rgrover1 714:a6130aaa0fd9 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 714:a6130aaa0fd9 5 * you may not use this file except in compliance with the License.
rgrover1 714:a6130aaa0fd9 6 * You may obtain a copy of the License at
rgrover1 714:a6130aaa0fd9 7 *
rgrover1 714:a6130aaa0fd9 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 714:a6130aaa0fd9 9 *
rgrover1 714:a6130aaa0fd9 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 714:a6130aaa0fd9 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 714:a6130aaa0fd9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 714:a6130aaa0fd9 13 * See the License for the specific language governing permissions and
rgrover1 714:a6130aaa0fd9 14 * limitations under the License.
rgrover1 714:a6130aaa0fd9 15 */
rgrover1 714:a6130aaa0fd9 16
rgrover1 714:a6130aaa0fd9 17 #ifndef __GAP_H__
rgrover1 714:a6130aaa0fd9 18 #define __GAP_H__
rgrover1 714:a6130aaa0fd9 19
rgrover1 714:a6130aaa0fd9 20 #include "GapAdvertisingData.h"
rgrover1 714:a6130aaa0fd9 21 #include "GapAdvertisingParams.h"
rgrover1 714:a6130aaa0fd9 22 #include "GapEvents.h"
rgrover1 714:a6130aaa0fd9 23 #include "CallChain.h"
rgrover1 714:a6130aaa0fd9 24 #include "FunctionPointerWithContext.h"
rgrover1 714:a6130aaa0fd9 25
rgrover1 714:a6130aaa0fd9 26 using namespace mbed;
rgrover1 714:a6130aaa0fd9 27
rgrover1 714:a6130aaa0fd9 28 class GapScanningParams; /* forward declaration */
rgrover1 714:a6130aaa0fd9 29
rgrover1 714:a6130aaa0fd9 30 class Gap {
rgrover1 714:a6130aaa0fd9 31 public:
rgrover1 714:a6130aaa0fd9 32 enum AddressType_t {
rgrover1 714:a6130aaa0fd9 33 ADDR_TYPE_PUBLIC = 0,
rgrover1 714:a6130aaa0fd9 34 ADDR_TYPE_RANDOM_STATIC,
rgrover1 714:a6130aaa0fd9 35 ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE,
rgrover1 714:a6130aaa0fd9 36 ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE
rgrover1 714:a6130aaa0fd9 37 };
rgrover1 714:a6130aaa0fd9 38 typedef enum AddressType_t addr_type_t; /* @Note: deprecated. Use AddressType_t instead. */
rgrover1 714:a6130aaa0fd9 39
rgrover1 714:a6130aaa0fd9 40 static const unsigned ADDR_LEN = 6;
rgrover1 714:a6130aaa0fd9 41 typedef uint8_t Address_t[ADDR_LEN]; /* 48-bit address, LSB format. */
rgrover1 714:a6130aaa0fd9 42 typedef Address_t address_t; /* @Note: deprecated. Use Address_t instead. */
rgrover1 714:a6130aaa0fd9 43
rgrover1 714:a6130aaa0fd9 44 enum AdvertisementType_t {
rgrover1 714:a6130aaa0fd9 45 ADV_IND = 0x00, /**< Connectable undirected. */
rgrover1 714:a6130aaa0fd9 46 ADV_DIRECT_IND = 0x01, /**< Connectable directed. */
rgrover1 714:a6130aaa0fd9 47 ADV_SCAN_IND = 0x02, /**< Scannable undirected. */
rgrover1 714:a6130aaa0fd9 48 ADV_NONCONN_IND = 0x03, /**< Non connectable undirected. */
rgrover1 714:a6130aaa0fd9 49 };
rgrover1 714:a6130aaa0fd9 50
rgrover1 714:a6130aaa0fd9 51 /**
rgrover1 714:a6130aaa0fd9 52 * Enumeration for disconnection reasons. The values for these reasons are
rgrover1 714:a6130aaa0fd9 53 * derived from Nordic's implementation; but the reasons are meant to be
rgrover1 714:a6130aaa0fd9 54 * independent of the transport. If you are returned a reason which is not
rgrover1 714:a6130aaa0fd9 55 * covered by this enumeration, then please refer to the underlying
rgrover1 714:a6130aaa0fd9 56 * transport library.
rgrover1 714:a6130aaa0fd9 57 */
rgrover1 714:a6130aaa0fd9 58 enum DisconnectionReason_t {
rgrover1 714:a6130aaa0fd9 59 REMOTE_USER_TERMINATED_CONNECTION = 0x13,
rgrover1 714:a6130aaa0fd9 60 LOCAL_HOST_TERMINATED_CONNECTION = 0x16,
rgrover1 714:a6130aaa0fd9 61 CONN_INTERVAL_UNACCEPTABLE = 0x3B,
rgrover1 714:a6130aaa0fd9 62 };
rgrover1 714:a6130aaa0fd9 63
rgrover1 714:a6130aaa0fd9 64 /* Describes the current state of the device (more than one bit can be set) */
rgrover1 714:a6130aaa0fd9 65 struct GapState_t {
rgrover1 714:a6130aaa0fd9 66 unsigned advertising : 1; /**< peripheral is currently advertising */
rgrover1 714:a6130aaa0fd9 67 unsigned connected : 1; /**< peripheral is connected to a central */
rgrover1 714:a6130aaa0fd9 68 };
rgrover1 714:a6130aaa0fd9 69
rgrover1 714:a6130aaa0fd9 70 typedef uint16_t Handle_t;
rgrover1 714:a6130aaa0fd9 71
rgrover1 714:a6130aaa0fd9 72 typedef struct {
rgrover1 714:a6130aaa0fd9 73 uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
rgrover1 714:a6130aaa0fd9 74 uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
rgrover1 714:a6130aaa0fd9 75 uint16_t slaveLatency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/
rgrover1 714:a6130aaa0fd9 76 uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
rgrover1 714:a6130aaa0fd9 77 } ConnectionParams_t;
rgrover1 714:a6130aaa0fd9 78
rgrover1 714:a6130aaa0fd9 79 enum SecurityMode_t {
rgrover1 714:a6130aaa0fd9 80 SECURITY_MODE_NO_ACCESS,
rgrover1 714:a6130aaa0fd9 81 SECURITY_MODE_ENCRYPTION_OPEN_LINK, /**< require no protection, open link. */
rgrover1 714:a6130aaa0fd9 82 SECURITY_MODE_ENCRYPTION_NO_MITM, /**< require encryption, but no MITM protection. */
rgrover1 714:a6130aaa0fd9 83 SECURITY_MODE_ENCRYPTION_WITH_MITM, /**< require encryption and MITM protection. */
rgrover1 714:a6130aaa0fd9 84 SECURITY_MODE_SIGNED_NO_MITM, /**< require signing or encryption, but no MITM protection. */
rgrover1 714:a6130aaa0fd9 85 SECURITY_MODE_SIGNED_WITH_MITM, /**< require signing or encryption, and MITM protection. */
rgrover1 714:a6130aaa0fd9 86 };
rgrover1 714:a6130aaa0fd9 87
rgrover1 714:a6130aaa0fd9 88 /**
rgrover1 714:a6130aaa0fd9 89 * @brief Defines possible security status/states.
rgrover1 714:a6130aaa0fd9 90 *
rgrover1 714:a6130aaa0fd9 91 * @details Defines possible security status/states of a link when requested by getLinkSecurity().
rgrover1 714:a6130aaa0fd9 92 */
rgrover1 714:a6130aaa0fd9 93 enum LinkSecurityStatus_t {
rgrover1 714:a6130aaa0fd9 94 NOT_ENCRYPTED, /**< The link is not secured. */
rgrover1 714:a6130aaa0fd9 95 ENCRYPTION_IN_PROGRESS, /**< Link security is being established.*/
rgrover1 714:a6130aaa0fd9 96 ENCRYPTED /**< The link is secure.*/
rgrover1 714:a6130aaa0fd9 97 };
rgrover1 714:a6130aaa0fd9 98
rgrover1 714:a6130aaa0fd9 99 enum SecurityIOCapabilities_t {
rgrover1 714:a6130aaa0fd9 100 IO_CAPS_DISPLAY_ONLY = 0x00, /**< Display Only. */
rgrover1 714:a6130aaa0fd9 101 IO_CAPS_DISPLAY_YESNO = 0x01, /**< Display and Yes/No entry. */
rgrover1 714:a6130aaa0fd9 102 IO_CAPS_KEYBOARD_ONLY = 0x02, /**< Keyboard Only. */
rgrover1 714:a6130aaa0fd9 103 IO_CAPS_NONE = 0x03, /**< No I/O capabilities. */
rgrover1 714:a6130aaa0fd9 104 IO_CAPS_KEYBOARD_DISPLAY = 0x04, /**< Keyboard and Display. */
rgrover1 714:a6130aaa0fd9 105 };
rgrover1 714:a6130aaa0fd9 106
rgrover1 714:a6130aaa0fd9 107 enum SecurityCompletionStatus_t {
rgrover1 714:a6130aaa0fd9 108 SEC_STATUS_SUCCESS = 0x00, /**< Procedure completed with success. */
rgrover1 714:a6130aaa0fd9 109 SEC_STATUS_TIMEOUT = 0x01, /**< Procedure timed out. */
rgrover1 714:a6130aaa0fd9 110 SEC_STATUS_PDU_INVALID = 0x02, /**< Invalid PDU received. */
rgrover1 714:a6130aaa0fd9 111 SEC_STATUS_PASSKEY_ENTRY_FAILED = 0x81, /**< Passkey entry failed (user canceled or other). */
rgrover1 714:a6130aaa0fd9 112 SEC_STATUS_OOB_NOT_AVAILABLE = 0x82, /**< Out of Band Key not available. */
rgrover1 714:a6130aaa0fd9 113 SEC_STATUS_AUTH_REQ = 0x83, /**< Authentication requirements not met. */
rgrover1 714:a6130aaa0fd9 114 SEC_STATUS_CONFIRM_VALUE = 0x84, /**< Confirm value failed. */
rgrover1 714:a6130aaa0fd9 115 SEC_STATUS_PAIRING_NOT_SUPP = 0x85, /**< Pairing not supported. */
rgrover1 714:a6130aaa0fd9 116 SEC_STATUS_ENC_KEY_SIZE = 0x86, /**< Encryption key size. */
rgrover1 714:a6130aaa0fd9 117 SEC_STATUS_SMP_CMD_UNSUPPORTED = 0x87, /**< Unsupported SMP command. */
rgrover1 714:a6130aaa0fd9 118 SEC_STATUS_UNSPECIFIED = 0x88, /**< Unspecified reason. */
rgrover1 714:a6130aaa0fd9 119 SEC_STATUS_REPEATED_ATTEMPTS = 0x89, /**< Too little time elapsed since last attempt. */
rgrover1 714:a6130aaa0fd9 120 SEC_STATUS_INVALID_PARAMS = 0x8A, /**< Invalid parameters. */
rgrover1 714:a6130aaa0fd9 121 };
rgrover1 714:a6130aaa0fd9 122
rgrover1 714:a6130aaa0fd9 123 /**
rgrover1 714:a6130aaa0fd9 124 * Declaration of type containing a passkey to be used during pairing. This
rgrover1 714:a6130aaa0fd9 125 * is passed into initializeSecurity() to specify a pre-programmed passkey
rgrover1 714:a6130aaa0fd9 126 * for authentication instead of generating a random one.
rgrover1 714:a6130aaa0fd9 127 */
rgrover1 714:a6130aaa0fd9 128 static const unsigned PASSKEY_LEN = 6;
rgrover1 714:a6130aaa0fd9 129 typedef uint8_t Passkey_t[PASSKEY_LEN]; /**< 6-digit passkey in ASCII ('0'-'9' digits only). */
rgrover1 714:a6130aaa0fd9 130
rgrover1 714:a6130aaa0fd9 131 static const uint16_t UNIT_1_25_MS = 1250; /**< Number of microseconds in 1.25 milliseconds. */
rgrover1 714:a6130aaa0fd9 132 static const uint16_t UNIT_0_625_MS = 625; /**< Number of microseconds in 0.625 milliseconds. */
rgrover1 714:a6130aaa0fd9 133 static uint16_t MSEC_TO_GAP_DURATION_UNITS(uint32_t durationInMillis) {
rgrover1 714:a6130aaa0fd9 134 return (durationInMillis * 1000) / UNIT_1_25_MS;
rgrover1 714:a6130aaa0fd9 135 }
rgrover1 714:a6130aaa0fd9 136 static uint16_t MSEC_TO_ADVERTISEMENT_DURATION_UNITS(uint32_t durationInMillis) {
rgrover1 714:a6130aaa0fd9 137 return (durationInMillis * 1000) / UNIT_0_625_MS;
rgrover1 714:a6130aaa0fd9 138 }
rgrover1 714:a6130aaa0fd9 139 static uint16_t ADVERTISEMENT_DURATION_UNITS_TO_MS(uint16_t gapUnits) {
rgrover1 714:a6130aaa0fd9 140 return (gapUnits * UNIT_0_625_MS) / 1000;
rgrover1 714:a6130aaa0fd9 141 }
rgrover1 714:a6130aaa0fd9 142
rgrover1 714:a6130aaa0fd9 143 typedef void (*EventCallback_t)(void);
rgrover1 714:a6130aaa0fd9 144 typedef void (*ConnectionEventCallback_t)(Handle_t,
rgrover1 714:a6130aaa0fd9 145 AddressType_t peerAddrType, const Address_t peerAddr,
rgrover1 714:a6130aaa0fd9 146 AddressType_t ownAddrType, const Address_t ownAddr,
rgrover1 714:a6130aaa0fd9 147 const ConnectionParams_t *);
rgrover1 714:a6130aaa0fd9 148 typedef void (*HandleSpecificEvent_t)(Handle_t handle);
rgrover1 714:a6130aaa0fd9 149 typedef void (*DisconnectionEventCallback_t)(Handle_t, DisconnectionReason_t);
rgrover1 714:a6130aaa0fd9 150 typedef void (*RadioNotificationEventCallback_t) (bool radio_active); /* gets passed true for ACTIVE; false for INACTIVE. */
rgrover1 714:a6130aaa0fd9 151 typedef void (*SecuritySetupInitiatedCallback_t)(Handle_t, bool allowBonding, bool requireMITM, SecurityIOCapabilities_t iocaps);
rgrover1 714:a6130aaa0fd9 152 typedef void (*SecuritySetupCompletedCallback_t)(Handle_t, SecurityCompletionStatus_t status);
rgrover1 714:a6130aaa0fd9 153 typedef void (*LinkSecuredCallback_t)(Handle_t handle, SecurityMode_t securityMode);
rgrover1 714:a6130aaa0fd9 154 typedef void (*PasskeyDisplayCallback_t)(Handle_t handle, const Passkey_t passkey);
rgrover1 714:a6130aaa0fd9 155
rgrover1 714:a6130aaa0fd9 156 struct AdvertisementCallbackParams_t {
rgrover1 714:a6130aaa0fd9 157 Address_t peerAddr;
rgrover1 714:a6130aaa0fd9 158 int8_t rssi;
rgrover1 714:a6130aaa0fd9 159 bool isScanResponse;
rgrover1 714:a6130aaa0fd9 160 AdvertisementType_t type;
rgrover1 714:a6130aaa0fd9 161 uint8_t advertisingDataLen;
rgrover1 714:a6130aaa0fd9 162 const uint8_t *advertisingData;
rgrover1 714:a6130aaa0fd9 163 };
rgrover1 714:a6130aaa0fd9 164 typedef FunctionPointerWithContext<const AdvertisementCallbackParams_t *> AdvertisementReportCallback_t;
rgrover1 714:a6130aaa0fd9 165
rgrover1 714:a6130aaa0fd9 166 friend class BLEDevice;
rgrover1 714:a6130aaa0fd9 167
rgrover1 714:a6130aaa0fd9 168 private:
rgrover1 714:a6130aaa0fd9 169 /* These functions must be defined in the sub-class */
rgrover1 714:a6130aaa0fd9 170 virtual ble_error_t setAddress(AddressType_t type, const Address_t address) = 0;
rgrover1 714:a6130aaa0fd9 171 virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address) = 0;
rgrover1 714:a6130aaa0fd9 172 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0;
rgrover1 714:a6130aaa0fd9 173 virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0;
rgrover1 714:a6130aaa0fd9 174 virtual ble_error_t stopAdvertising(void) = 0;
rgrover1 714:a6130aaa0fd9 175 virtual ble_error_t stopScan() = 0;
rgrover1 714:a6130aaa0fd9 176 virtual uint16_t getMinAdvertisingInterval(void) const = 0;
rgrover1 714:a6130aaa0fd9 177 virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const = 0;
rgrover1 714:a6130aaa0fd9 178 virtual uint16_t getMaxAdvertisingInterval(void) const = 0;
rgrover1 714:a6130aaa0fd9 179 virtual ble_error_t disconnect(DisconnectionReason_t reason) = 0;
rgrover1 714:a6130aaa0fd9 180 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0;
rgrover1 714:a6130aaa0fd9 181 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0;
rgrover1 714:a6130aaa0fd9 182 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0;
rgrover1 714:a6130aaa0fd9 183
rgrover1 714:a6130aaa0fd9 184 virtual ble_error_t purgeAllBondingState(void) = 0;
rgrover1 714:a6130aaa0fd9 185 virtual ble_error_t getLinkSecurity(Handle_t connectionHandle, LinkSecurityStatus_t *securityStatusP) = 0;
rgrover1 714:a6130aaa0fd9 186
rgrover1 714:a6130aaa0fd9 187 virtual ble_error_t setDeviceName(const uint8_t *deviceName) = 0;
rgrover1 714:a6130aaa0fd9 188 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) = 0;
rgrover1 714:a6130aaa0fd9 189 virtual ble_error_t setAppearance(uint16_t appearance) = 0;
rgrover1 714:a6130aaa0fd9 190 virtual ble_error_t getAppearance(uint16_t *appearanceP) = 0;
rgrover1 714:a6130aaa0fd9 191
rgrover1 714:a6130aaa0fd9 192 ble_error_t startScan(const GapScanningParams &scanningParams, void (*callback)(const AdvertisementCallbackParams_t *params)) {
rgrover1 714:a6130aaa0fd9 193 ble_error_t err = BLE_ERROR_NONE;
rgrover1 714:a6130aaa0fd9 194 if (callback) {
rgrover1 714:a6130aaa0fd9 195 if ((err = startRadioScan(scanningParams)) == BLE_ERROR_NONE) {
rgrover1 714:a6130aaa0fd9 196 onAdvertisementReport.attach(callback);
rgrover1 714:a6130aaa0fd9 197 }
rgrover1 714:a6130aaa0fd9 198 }
rgrover1 714:a6130aaa0fd9 199
rgrover1 714:a6130aaa0fd9 200 return err;
rgrover1 714:a6130aaa0fd9 201 }
rgrover1 714:a6130aaa0fd9 202
rgrover1 714:a6130aaa0fd9 203 template<typename T>
rgrover1 714:a6130aaa0fd9 204 ble_error_t startScan(const GapScanningParams &scanningParams, T *object, void (T::*callbackMember)(const AdvertisementCallbackParams_t *params)) {
rgrover1 714:a6130aaa0fd9 205 ble_error_t err = BLE_ERROR_NONE;
rgrover1 714:a6130aaa0fd9 206 if (object && callbackMember) {
rgrover1 714:a6130aaa0fd9 207 if ((err = startRadioScan(scanningParams)) == BLE_ERROR_NONE) {
rgrover1 714:a6130aaa0fd9 208 onAdvertisementReport.attach(object, callbackMember);
rgrover1 714:a6130aaa0fd9 209 }
rgrover1 714:a6130aaa0fd9 210 }
rgrover1 714:a6130aaa0fd9 211
rgrover1 714:a6130aaa0fd9 212 return err;
rgrover1 714:a6130aaa0fd9 213 }
rgrover1 714:a6130aaa0fd9 214
rgrover1 714:a6130aaa0fd9 215 protected:
rgrover1 714:a6130aaa0fd9 216 virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) = 0;
rgrover1 714:a6130aaa0fd9 217
rgrover1 714:a6130aaa0fd9 218 /* Event callback handlers */
rgrover1 714:a6130aaa0fd9 219 void setOnTimeout(EventCallback_t callback) {onTimeout = callback;}
rgrover1 714:a6130aaa0fd9 220 void setOnConnection(ConnectionEventCallback_t callback) {onConnection = callback;}
rgrover1 714:a6130aaa0fd9 221
rgrover1 714:a6130aaa0fd9 222 /**
rgrover1 714:a6130aaa0fd9 223 * Set the application callback for disconnection events.
rgrover1 714:a6130aaa0fd9 224 * @param callback
rgrover1 714:a6130aaa0fd9 225 * Pointer to the unique callback.
rgrover1 714:a6130aaa0fd9 226 */
rgrover1 714:a6130aaa0fd9 227 void setOnDisconnection(DisconnectionEventCallback_t callback) {onDisconnection = callback;}
rgrover1 714:a6130aaa0fd9 228
rgrover1 714:a6130aaa0fd9 229 /**
rgrover1 714:a6130aaa0fd9 230 * Set the application callback for radio-notification events.
rgrover1 714:a6130aaa0fd9 231 * @param callback
rgrover1 714:a6130aaa0fd9 232 * Handler to be executed in response to a radio notification event.
rgrover1 714:a6130aaa0fd9 233 */
rgrover1 714:a6130aaa0fd9 234 virtual void setOnRadioNotification(RadioNotificationEventCallback_t callback) {onRadioNotification = callback;}
rgrover1 714:a6130aaa0fd9 235
rgrover1 714:a6130aaa0fd9 236 /**
rgrover1 714:a6130aaa0fd9 237 * To indicate that security procedure for link has started.
rgrover1 714:a6130aaa0fd9 238 */
rgrover1 714:a6130aaa0fd9 239 virtual void setOnSecuritySetupInitiated(SecuritySetupInitiatedCallback_t callback) {onSecuritySetupInitiated = callback;}
rgrover1 714:a6130aaa0fd9 240
rgrover1 714:a6130aaa0fd9 241 /**
rgrover1 714:a6130aaa0fd9 242 * To indicate that security procedure for link has completed.
rgrover1 714:a6130aaa0fd9 243 */
rgrover1 714:a6130aaa0fd9 244 virtual void setOnSecuritySetupCompleted(SecuritySetupCompletedCallback_t callback) {onSecuritySetupCompleted = callback;}
rgrover1 714:a6130aaa0fd9 245
rgrover1 714:a6130aaa0fd9 246 /**
rgrover1 714:a6130aaa0fd9 247 * To indicate that link with the peer is secured. For bonded devices,
rgrover1 714:a6130aaa0fd9 248 * subsequent re-connections with bonded peer will result only in this callback
rgrover1 714:a6130aaa0fd9 249 * when the link is secured and setup procedures will not occur unless the
rgrover1 714:a6130aaa0fd9 250 * bonding information is either lost or deleted on either or both sides.
rgrover1 714:a6130aaa0fd9 251 */
rgrover1 714:a6130aaa0fd9 252 virtual void setOnLinkSecured(LinkSecuredCallback_t callback) {onLinkSecured = callback;}
rgrover1 714:a6130aaa0fd9 253
rgrover1 714:a6130aaa0fd9 254 /**
rgrover1 714:a6130aaa0fd9 255 * To indicate that device context is stored persistently.
rgrover1 714:a6130aaa0fd9 256 */
rgrover1 714:a6130aaa0fd9 257 virtual void setOnSecurityContextStored(HandleSpecificEvent_t callback) {onSecurityContextStored = callback;}
rgrover1 714:a6130aaa0fd9 258
rgrover1 714:a6130aaa0fd9 259 /**
rgrover1 714:a6130aaa0fd9 260 * To set the callback for when the passkey needs to be displayed on a peripheral with DISPLAY capability.
rgrover1 714:a6130aaa0fd9 261 */
rgrover1 714:a6130aaa0fd9 262 virtual void setOnPasskeyDisplay(PasskeyDisplayCallback_t callback) {onPasskeyDisplay = callback;}
rgrover1 714:a6130aaa0fd9 263
rgrover1 714:a6130aaa0fd9 264 /**
rgrover1 714:a6130aaa0fd9 265 * Append to a chain of callbacks to be invoked upon disconnection; these
rgrover1 714:a6130aaa0fd9 266 * callbacks receive no context and are therefore different from the
rgrover1 714:a6130aaa0fd9 267 * onDisconnection callback.
rgrover1 714:a6130aaa0fd9 268 * @param callback
rgrover1 714:a6130aaa0fd9 269 * function pointer to be invoked upon disconnection; receives no context.
rgrover1 714:a6130aaa0fd9 270 *
rgrover1 714:a6130aaa0fd9 271 * @note the disconnection CallChain should have been merged with
rgrover1 714:a6130aaa0fd9 272 * onDisconnctionCallback; but this was not possible because
rgrover1 714:a6130aaa0fd9 273 * FunctionPointer (which is a building block for CallChain) doesn't
rgrover1 714:a6130aaa0fd9 274 * accept variadic templates.
rgrover1 714:a6130aaa0fd9 275 */
rgrover1 714:a6130aaa0fd9 276 template<typename T>
rgrover1 714:a6130aaa0fd9 277 void addToDisconnectionCallChain(T *tptr, void (T::*mptr)(void)) {disconnectionCallChain.add(tptr, mptr);}
rgrover1 714:a6130aaa0fd9 278
rgrover1 714:a6130aaa0fd9 279 private:
rgrover1 714:a6130aaa0fd9 280 GapState_t getState(void) const {
rgrover1 714:a6130aaa0fd9 281 return state;
rgrover1 714:a6130aaa0fd9 282 }
rgrover1 714:a6130aaa0fd9 283
rgrover1 714:a6130aaa0fd9 284 protected:
rgrover1 714:a6130aaa0fd9 285 Gap() :
rgrover1 714:a6130aaa0fd9 286 state(),
rgrover1 714:a6130aaa0fd9 287 onTimeout(NULL),
rgrover1 714:a6130aaa0fd9 288 onConnection(NULL),
rgrover1 714:a6130aaa0fd9 289 onDisconnection(NULL),
rgrover1 714:a6130aaa0fd9 290 onRadioNotification(),
rgrover1 714:a6130aaa0fd9 291 onSecuritySetupInitiated(),
rgrover1 714:a6130aaa0fd9 292 onSecuritySetupCompleted(),
rgrover1 714:a6130aaa0fd9 293 onLinkSecured(),
rgrover1 714:a6130aaa0fd9 294 onSecurityContextStored(),
rgrover1 714:a6130aaa0fd9 295 onPasskeyDisplay(),
rgrover1 714:a6130aaa0fd9 296 onAdvertisementReport(),
rgrover1 714:a6130aaa0fd9 297 disconnectionCallChain() {
rgrover1 714:a6130aaa0fd9 298 /* empty */
rgrover1 714:a6130aaa0fd9 299 }
rgrover1 714:a6130aaa0fd9 300
rgrover1 714:a6130aaa0fd9 301 public:
rgrover1 714:a6130aaa0fd9 302 void processConnectionEvent(Handle_t handle, AddressType_t peerAddrType, const Address_t peerAddr, AddressType_t ownAddrType, const Address_t ownAddr, const ConnectionParams_t *params) {
rgrover1 714:a6130aaa0fd9 303 state.connected = 1;
rgrover1 714:a6130aaa0fd9 304 if (onConnection) {
rgrover1 714:a6130aaa0fd9 305 onConnection(handle, peerAddrType, peerAddr, ownAddrType, ownAddr, params);
rgrover1 714:a6130aaa0fd9 306 }
rgrover1 714:a6130aaa0fd9 307 }
rgrover1 714:a6130aaa0fd9 308
rgrover1 714:a6130aaa0fd9 309 void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) {
rgrover1 714:a6130aaa0fd9 310 state.connected = 0;
rgrover1 714:a6130aaa0fd9 311 if (onDisconnection) {
rgrover1 714:a6130aaa0fd9 312 onDisconnection(handle, reason);
rgrover1 714:a6130aaa0fd9 313 }
rgrover1 714:a6130aaa0fd9 314 disconnectionCallChain.call();
rgrover1 714:a6130aaa0fd9 315 }
rgrover1 714:a6130aaa0fd9 316
rgrover1 714:a6130aaa0fd9 317 void processSecuritySetupInitiatedEvent(Handle_t handle, bool allowBonding, bool requireMITM, SecurityIOCapabilities_t iocaps) {
rgrover1 714:a6130aaa0fd9 318 if (onSecuritySetupInitiated) {
rgrover1 714:a6130aaa0fd9 319 onSecuritySetupInitiated(handle, allowBonding, requireMITM, iocaps);
rgrover1 714:a6130aaa0fd9 320 }
rgrover1 714:a6130aaa0fd9 321 }
rgrover1 714:a6130aaa0fd9 322
rgrover1 714:a6130aaa0fd9 323 void processSecuritySetupCompletedEvent(Handle_t handle, SecurityCompletionStatus_t status) {
rgrover1 714:a6130aaa0fd9 324 if (onSecuritySetupCompleted) {
rgrover1 714:a6130aaa0fd9 325 onSecuritySetupCompleted(handle, status);
rgrover1 714:a6130aaa0fd9 326 }
rgrover1 714:a6130aaa0fd9 327 }
rgrover1 714:a6130aaa0fd9 328
rgrover1 714:a6130aaa0fd9 329 void processLinkSecuredEvent(Handle_t handle, SecurityMode_t securityMode) {
rgrover1 714:a6130aaa0fd9 330 if (onLinkSecured) {
rgrover1 714:a6130aaa0fd9 331 onLinkSecured(handle, securityMode);
rgrover1 714:a6130aaa0fd9 332 }
rgrover1 714:a6130aaa0fd9 333 }
rgrover1 714:a6130aaa0fd9 334
rgrover1 714:a6130aaa0fd9 335 void processSecurityContextStoredEvent(Handle_t handle) {
rgrover1 714:a6130aaa0fd9 336 if (onSecurityContextStored) {
rgrover1 714:a6130aaa0fd9 337 onSecurityContextStored(handle);
rgrover1 714:a6130aaa0fd9 338 }
rgrover1 714:a6130aaa0fd9 339 }
rgrover1 714:a6130aaa0fd9 340
rgrover1 714:a6130aaa0fd9 341 void processPasskeyDisplayEvent(Handle_t handle, const Passkey_t passkey) {
rgrover1 714:a6130aaa0fd9 342 if (onPasskeyDisplay) {
rgrover1 714:a6130aaa0fd9 343 onPasskeyDisplay(handle, passkey);
rgrover1 714:a6130aaa0fd9 344 }
rgrover1 714:a6130aaa0fd9 345 }
rgrover1 714:a6130aaa0fd9 346
rgrover1 714:a6130aaa0fd9 347 void processAdvertisementReport(const Address_t peerAddr,
rgrover1 714:a6130aaa0fd9 348 int8_t rssi,
rgrover1 714:a6130aaa0fd9 349 bool isScanResponse,
rgrover1 714:a6130aaa0fd9 350 AdvertisementType_t type,
rgrover1 714:a6130aaa0fd9 351 uint8_t advertisingDataLen,
rgrover1 714:a6130aaa0fd9 352 const uint8_t *advertisingData) {
rgrover1 714:a6130aaa0fd9 353 AdvertisementCallbackParams_t params;
rgrover1 714:a6130aaa0fd9 354 memcpy(params.peerAddr, peerAddr, ADDR_LEN);
rgrover1 714:a6130aaa0fd9 355 params.rssi = rssi;
rgrover1 714:a6130aaa0fd9 356 params.isScanResponse = isScanResponse;
rgrover1 714:a6130aaa0fd9 357 params.type = type;
rgrover1 714:a6130aaa0fd9 358 params.advertisingDataLen = advertisingDataLen;
rgrover1 714:a6130aaa0fd9 359 params.advertisingData = advertisingData;
rgrover1 714:a6130aaa0fd9 360 onAdvertisementReport.call(&params);
rgrover1 714:a6130aaa0fd9 361 }
rgrover1 714:a6130aaa0fd9 362
rgrover1 714:a6130aaa0fd9 363 void processEvent(GapEvents::gapEvent_e type) {
rgrover1 714:a6130aaa0fd9 364 switch (type) {
rgrover1 714:a6130aaa0fd9 365 case GapEvents::GAP_EVENT_TIMEOUT:
rgrover1 714:a6130aaa0fd9 366 state.advertising = 0;
rgrover1 714:a6130aaa0fd9 367 if (onTimeout) {
rgrover1 714:a6130aaa0fd9 368 onTimeout();
rgrover1 714:a6130aaa0fd9 369 }
rgrover1 714:a6130aaa0fd9 370 break;
rgrover1 714:a6130aaa0fd9 371 default:
rgrover1 714:a6130aaa0fd9 372 break;
rgrover1 714:a6130aaa0fd9 373 }
rgrover1 714:a6130aaa0fd9 374 }
rgrover1 714:a6130aaa0fd9 375
rgrover1 714:a6130aaa0fd9 376 protected:
rgrover1 714:a6130aaa0fd9 377 GapState_t state;
rgrover1 714:a6130aaa0fd9 378
rgrover1 714:a6130aaa0fd9 379 protected:
rgrover1 714:a6130aaa0fd9 380 EventCallback_t onTimeout;
rgrover1 714:a6130aaa0fd9 381 ConnectionEventCallback_t onConnection;
rgrover1 714:a6130aaa0fd9 382 DisconnectionEventCallback_t onDisconnection;
rgrover1 714:a6130aaa0fd9 383 RadioNotificationEventCallback_t onRadioNotification;
rgrover1 714:a6130aaa0fd9 384 SecuritySetupInitiatedCallback_t onSecuritySetupInitiated;
rgrover1 714:a6130aaa0fd9 385 SecuritySetupCompletedCallback_t onSecuritySetupCompleted;
rgrover1 714:a6130aaa0fd9 386 LinkSecuredCallback_t onLinkSecured;
rgrover1 714:a6130aaa0fd9 387 HandleSpecificEvent_t onSecurityContextStored;
rgrover1 714:a6130aaa0fd9 388 PasskeyDisplayCallback_t onPasskeyDisplay;
rgrover1 714:a6130aaa0fd9 389 AdvertisementReportCallback_t onAdvertisementReport;
rgrover1 714:a6130aaa0fd9 390 CallChain disconnectionCallChain;
rgrover1 714:a6130aaa0fd9 391
rgrover1 714:a6130aaa0fd9 392 private:
rgrover1 714:a6130aaa0fd9 393 /* disallow copy and assignment */
rgrover1 714:a6130aaa0fd9 394 Gap(const Gap &);
rgrover1 714:a6130aaa0fd9 395 Gap& operator=(const Gap &);
rgrover1 714:a6130aaa0fd9 396 };
rgrover1 714:a6130aaa0fd9 397
rgrover1 714:a6130aaa0fd9 398 #endif // ifndef __GAP_H__