Nano RGB LED mesh

Dependencies:   nrf51-sdk

Dependents:   BLENano_Mesh

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 10:19:38 2016 +0000
Revision:
604:0f5c8725146c
Parent:
603:b1616eaf8206
Child:
605:07229eef5014
Synchronized with git rev bdf392d4
Author: Andres Amaya Garcia
Remove redundant structures from nRF5xGap

Remove the following structures/arrays from the nRF5xGap class:

* ble_gap_whitelist_t whitelist
* ble_gap_addr_t *whitelistAddressePtrs[]
* ble_gap_irk_t *whitelistIrkPtrs[]

These are made redundant because the SoftDevice does not require the user to
keep a copy of the whitelist itself.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 541:884f95bf5351 1 /* mbed Microcontroller Library
vcoubard 541:884f95bf5351 2 * Copyright (c) 2006-2013 ARM Limited
vcoubard 541:884f95bf5351 3 *
vcoubard 541:884f95bf5351 4 * Licensed under the Apache License, Version 2.0 (the "License");
vcoubard 541:884f95bf5351 5 * you may not use this file except in compliance with the License.
vcoubard 541:884f95bf5351 6 * You may obtain a copy of the License at
vcoubard 541:884f95bf5351 7 *
vcoubard 541:884f95bf5351 8 * http://www.apache.org/licenses/LICENSE-2.0
vcoubard 541:884f95bf5351 9 *
vcoubard 541:884f95bf5351 10 * Unless required by applicable law or agreed to in writing, software
vcoubard 541:884f95bf5351 11 * distributed under the License is distributed on an "AS IS" BASIS,
vcoubard 541:884f95bf5351 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vcoubard 541:884f95bf5351 13 * See the License for the specific language governing permissions and
vcoubard 541:884f95bf5351 14 * limitations under the License.
vcoubard 541:884f95bf5351 15 */
vcoubard 541:884f95bf5351 16
vcoubard 575:7023a8204a1b 17 #include "nRF5xn.h"
vcoubard 578:78f69f1be114 18 #ifdef YOTTA_CFG_MBED_OS
vcoubard 578:78f69f1be114 19 #include "mbed-drivers/mbed.h"
vcoubard 578:78f69f1be114 20 #else
vcoubard 578:78f69f1be114 21 #include "mbed.h"
vcoubard 578:78f69f1be114 22 #endif
vcoubard 575:7023a8204a1b 23 #include "ble/BLE.h"
vcoubard 541:884f95bf5351 24
vcoubard 541:884f95bf5351 25 #include "common/common.h"
vcoubard 541:884f95bf5351 26 #include "ble_advdata.h"
vcoubard 541:884f95bf5351 27 #include "ble_hci.h"
vcoubard 541:884f95bf5351 28
vcoubard 541:884f95bf5351 29 void radioNotificationStaticCallback(bool param) {
vcoubard 575:7023a8204a1b 30 nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap();
vcoubard 575:7023a8204a1b 31 gap.processRadioNotificationEvent(param);
vcoubard 541:884f95bf5351 32 }
vcoubard 541:884f95bf5351 33
vcoubard 541:884f95bf5351 34 /**************************************************************************/
vcoubard 541:884f95bf5351 35 /*!
vcoubard 541:884f95bf5351 36 @brief Sets the advertising parameters and payload for the device
vcoubard 541:884f95bf5351 37
vcoubard 541:884f95bf5351 38 @param[in] params
vcoubard 541:884f95bf5351 39 Basic advertising details, including the advertising
vcoubard 541:884f95bf5351 40 delay, timeout and how the device should be advertised
vcoubard 541:884f95bf5351 41 @params[in] advData
vcoubard 541:884f95bf5351 42 The primary advertising data payload
vcoubard 541:884f95bf5351 43 @params[in] scanResponse
vcoubard 541:884f95bf5351 44 The optional Scan Response payload if the advertising
vcoubard 541:884f95bf5351 45 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
vcoubard 541:884f95bf5351 46 in \ref GapAdveritinngParams
vcoubard 541:884f95bf5351 47
vcoubard 541:884f95bf5351 48 @returns \ref ble_error_t
vcoubard 541:884f95bf5351 49
vcoubard 541:884f95bf5351 50 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 51 Everything executed properly
vcoubard 541:884f95bf5351 52
vcoubard 541:884f95bf5351 53 @retval BLE_ERROR_BUFFER_OVERFLOW
vcoubard 541:884f95bf5351 54 The proposed action would cause a buffer overflow. All
vcoubard 541:884f95bf5351 55 advertising payloads must be <= 31 bytes, for example.
vcoubard 541:884f95bf5351 56
vcoubard 541:884f95bf5351 57 @retval BLE_ERROR_NOT_IMPLEMENTED
vcoubard 541:884f95bf5351 58 A feature was requested that is not yet supported in the
vcoubard 541:884f95bf5351 59 nRF51 firmware or hardware.
vcoubard 541:884f95bf5351 60
vcoubard 541:884f95bf5351 61 @retval BLE_ERROR_PARAM_OUT_OF_RANGE
vcoubard 541:884f95bf5351 62 One of the proposed values is outside the valid range.
vcoubard 541:884f95bf5351 63
vcoubard 541:884f95bf5351 64 @section EXAMPLE
vcoubard 541:884f95bf5351 65
vcoubard 541:884f95bf5351 66 @code
vcoubard 541:884f95bf5351 67
vcoubard 541:884f95bf5351 68 @endcode
vcoubard 541:884f95bf5351 69 */
vcoubard 541:884f95bf5351 70 /**************************************************************************/
vcoubard 541:884f95bf5351 71 ble_error_t nRF5xGap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse)
vcoubard 541:884f95bf5351 72 {
vcoubard 541:884f95bf5351 73 /* Make sure we don't exceed the advertising payload length */
vcoubard 541:884f95bf5351 74 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
vcoubard 541:884f95bf5351 75 return BLE_ERROR_BUFFER_OVERFLOW;
vcoubard 541:884f95bf5351 76 }
vcoubard 541:884f95bf5351 77
vcoubard 541:884f95bf5351 78 /* Make sure we have a payload! */
vcoubard 541:884f95bf5351 79 if (advData.getPayloadLen() == 0) {
vcoubard 541:884f95bf5351 80 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 81 }
vcoubard 541:884f95bf5351 82
vcoubard 541:884f95bf5351 83 /* Check the scan response payload limits */
vcoubard 541:884f95bf5351 84 //if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED))
vcoubard 541:884f95bf5351 85 //{
vcoubard 541:884f95bf5351 86 // /* Check if we're within the upper limit */
vcoubard 541:884f95bf5351 87 // if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
vcoubard 541:884f95bf5351 88 // {
vcoubard 541:884f95bf5351 89 // return BLE_ERROR_BUFFER_OVERFLOW;
vcoubard 541:884f95bf5351 90 // }
vcoubard 541:884f95bf5351 91 // /* Make sure we have a payload! */
vcoubard 541:884f95bf5351 92 // if (advData.getPayloadLen() == 0)
vcoubard 541:884f95bf5351 93 // {
vcoubard 541:884f95bf5351 94 // return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 95 // }
vcoubard 541:884f95bf5351 96 //}
vcoubard 541:884f95bf5351 97
vcoubard 541:884f95bf5351 98 /* Send advertising data! */
vcoubard 541:884f95bf5351 99 ASSERT(ERROR_NONE ==
vcoubard 541:884f95bf5351 100 sd_ble_gap_adv_data_set(advData.getPayload(),
vcoubard 541:884f95bf5351 101 advData.getPayloadLen(),
vcoubard 541:884f95bf5351 102 scanResponse.getPayload(),
vcoubard 541:884f95bf5351 103 scanResponse.getPayloadLen()),
vcoubard 541:884f95bf5351 104 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 105
vcoubard 541:884f95bf5351 106 /* Make sure the GAP Service appearance value is aligned with the
vcoubard 541:884f95bf5351 107 *appearance from GapAdvertisingData */
vcoubard 541:884f95bf5351 108 ASSERT(ERROR_NONE == sd_ble_gap_appearance_set(advData.getAppearance()),
vcoubard 541:884f95bf5351 109 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 110
vcoubard 541:884f95bf5351 111 /* ToDo: Perform some checks on the payload, for example the Scan Response can't */
vcoubard 541:884f95bf5351 112 /* contains a flags AD type, etc. */
vcoubard 541:884f95bf5351 113
vcoubard 541:884f95bf5351 114 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 115 }
vcoubard 541:884f95bf5351 116
vcoubard 541:884f95bf5351 117 /**************************************************************************/
vcoubard 541:884f95bf5351 118 /*!
vcoubard 541:884f95bf5351 119 @brief Starts the BLE HW, initialising any services that were
vcoubard 541:884f95bf5351 120 added before this function was called.
vcoubard 541:884f95bf5351 121
vcoubard 541:884f95bf5351 122 @note All services must be added before calling this function!
vcoubard 541:884f95bf5351 123
vcoubard 541:884f95bf5351 124 @returns ble_error_t
vcoubard 541:884f95bf5351 125
vcoubard 541:884f95bf5351 126 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 127 Everything executed properly
vcoubard 541:884f95bf5351 128
vcoubard 541:884f95bf5351 129 @section EXAMPLE
vcoubard 541:884f95bf5351 130
vcoubard 541:884f95bf5351 131 @code
vcoubard 541:884f95bf5351 132
vcoubard 541:884f95bf5351 133 @endcode
vcoubard 541:884f95bf5351 134 */
vcoubard 541:884f95bf5351 135 /**************************************************************************/
vcoubard 541:884f95bf5351 136 ble_error_t nRF5xGap::startAdvertising(const GapAdvertisingParams &params)
vcoubard 541:884f95bf5351 137 {
vcoubard 541:884f95bf5351 138 /* Make sure we support the advertising type */
vcoubard 541:884f95bf5351 139 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) {
vcoubard 541:884f95bf5351 140 /* ToDo: This requires a propery security implementation, etc. */
vcoubard 541:884f95bf5351 141 return BLE_ERROR_NOT_IMPLEMENTED;
vcoubard 541:884f95bf5351 142 }
vcoubard 541:884f95bf5351 143
vcoubard 541:884f95bf5351 144 /* Check interval range */
vcoubard 541:884f95bf5351 145 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) {
vcoubard 541:884f95bf5351 146 /* Min delay is slightly longer for unconnectable devices */
vcoubard 541:884f95bf5351 147 if ((params.getIntervalInADVUnits() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
vcoubard 541:884f95bf5351 148 (params.getIntervalInADVUnits() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) {
vcoubard 541:884f95bf5351 149 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 150 }
vcoubard 541:884f95bf5351 151 } else {
vcoubard 541:884f95bf5351 152 if ((params.getIntervalInADVUnits() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN) ||
vcoubard 541:884f95bf5351 153 (params.getIntervalInADVUnits() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) {
vcoubard 541:884f95bf5351 154 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 155 }
vcoubard 541:884f95bf5351 156 }
vcoubard 541:884f95bf5351 157
vcoubard 541:884f95bf5351 158 /* Check timeout is zero for Connectable Directed */
vcoubard 541:884f95bf5351 159 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) {
vcoubard 541:884f95bf5351 160 /* Timeout must be 0 with this type, although we'll never get here */
vcoubard 541:884f95bf5351 161 /* since this isn't implemented yet anyway */
vcoubard 541:884f95bf5351 162 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 163 }
vcoubard 541:884f95bf5351 164
vcoubard 541:884f95bf5351 165 /* Check timeout for other advertising types */
vcoubard 541:884f95bf5351 166 if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
vcoubard 541:884f95bf5351 167 (params.getTimeout() > GapAdvertisingParams::GAP_ADV_PARAMS_TIMEOUT_MAX)) {
vcoubard 541:884f95bf5351 168 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 169 }
vcoubard 541:884f95bf5351 170
vcoubard 604:0f5c8725146c 171 /* Allocate the stack's whitelist statically */
vcoubard 604:0f5c8725146c 172 ble_gap_whitelist_t whitelist;
vcoubard 604:0f5c8725146c 173 ble_gap_addr_t *whitelistAddressPtrs[YOTTA_CFG_WHITELIST_MAX_SIZE];
vcoubard 604:0f5c8725146c 174 ble_gap_irk_t *whitelistIrkPtrs[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
vcoubard 604:0f5c8725146c 175 /* Initialize the whitelist */
vcoubard 604:0f5c8725146c 176 whitelist.pp_addrs = whitelistAddressPtrs;
vcoubard 604:0f5c8725146c 177 whitelist.pp_irks = whitelistIrkPtrs;
vcoubard 604:0f5c8725146c 178 whitelist.addr_count = 0;
vcoubard 604:0f5c8725146c 179 whitelist.irk_count = 0;
vcoubard 604:0f5c8725146c 180
vcoubard 598:814c1ce92947 181 /* Add missing IRKs to whitelist from the bond table held by the SoftDevice */
vcoubard 598:814c1ce92947 182 if (advertisingPolicyMode != Gap::ADV_POLICY_IGNORE_WHITELIST) {
vcoubard 604:0f5c8725146c 183 ble_error_t error = generateStackWhitelist(whitelist);
vcoubard 598:814c1ce92947 184 if (error != BLE_ERROR_NONE) {
vcoubard 598:814c1ce92947 185 return error;
vcoubard 598:814c1ce92947 186 }
vcoubard 598:814c1ce92947 187 }
vcoubard 598:814c1ce92947 188
vcoubard 541:884f95bf5351 189 /* Start Advertising */
vcoubard 541:884f95bf5351 190 ble_gap_adv_params_t adv_para = {0};
vcoubard 541:884f95bf5351 191
vcoubard 541:884f95bf5351 192 adv_para.type = params.getAdvertisingType();
vcoubard 541:884f95bf5351 193 adv_para.p_peer_addr = NULL; // Undirected advertisement
vcoubard 596:b66851544182 194 adv_para.fp = advertisingPolicyMode;
vcoubard 596:b66851544182 195 adv_para.p_whitelist = &whitelist;
vcoubard 541:884f95bf5351 196 adv_para.interval = params.getIntervalInADVUnits(); // advertising interval (in units of 0.625 ms)
vcoubard 541:884f95bf5351 197 adv_para.timeout = params.getTimeout();
vcoubard 541:884f95bf5351 198
vcoubard 541:884f95bf5351 199 ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 200
vcoubard 541:884f95bf5351 201 state.advertising = 1;
vcoubard 541:884f95bf5351 202
vcoubard 541:884f95bf5351 203 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 204 }
vcoubard 541:884f95bf5351 205
vcoubard 598:814c1ce92947 206 /* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */
vcoubard 598:814c1ce92947 207 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
vcoubard 604:0f5c8725146c 208 ble_error_t nRF5xGap::startRadioScan(const GapScanningParams &scanningParams)
vcoubard 604:0f5c8725146c 209 {
vcoubard 604:0f5c8725146c 210 /* Allocate the stack's whitelist statically */
vcoubard 604:0f5c8725146c 211 ble_gap_whitelist_t whitelist;
vcoubard 604:0f5c8725146c 212 ble_gap_addr_t *whitelistAddressPtrs[YOTTA_CFG_WHITELIST_MAX_SIZE];
vcoubard 604:0f5c8725146c 213 ble_gap_irk_t *whitelistIrkPtrs[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
vcoubard 604:0f5c8725146c 214 /* Initialize the whitelist */
vcoubard 604:0f5c8725146c 215 whitelist.pp_addrs = whitelistAddressPtrs;
vcoubard 604:0f5c8725146c 216 whitelist.pp_irks = whitelistIrkPtrs;
vcoubard 604:0f5c8725146c 217 whitelist.addr_count = 0;
vcoubard 604:0f5c8725146c 218 whitelist.irk_count = 0;
vcoubard 598:814c1ce92947 219
vcoubard 598:814c1ce92947 220 /* Add missing IRKs to whitelist from the bond table held by the SoftDevice */
vcoubard 598:814c1ce92947 221 if (scanningPolicyMode != Gap::SCAN_POLICY_IGNORE_WHITELIST) {
vcoubard 604:0f5c8725146c 222 ble_error_t error = generateStackWhitelist(whitelist);
vcoubard 598:814c1ce92947 223 if (error != BLE_ERROR_NONE) {
vcoubard 598:814c1ce92947 224 return error;
vcoubard 598:814c1ce92947 225 }
vcoubard 598:814c1ce92947 226 }
vcoubard 598:814c1ce92947 227
vcoubard 598:814c1ce92947 228 ble_gap_scan_params_t scanParams = {
vcoubard 598:814c1ce92947 229 .active = scanningParams.getActiveScanning(), /**< If 1, perform active scanning (scan requests). */
vcoubard 598:814c1ce92947 230 .selective = scanningPolicyMode, /**< If 1, ignore unknown devices (non whitelisted). */
vcoubard 598:814c1ce92947 231 .p_whitelist = &whitelist, /**< Pointer to whitelist, NULL if none is given. */
vcoubard 598:814c1ce92947 232 .interval = scanningParams.getInterval(), /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 598:814c1ce92947 233 .window = scanningParams.getWindow(), /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 598:814c1ce92947 234 .timeout = scanningParams.getTimeout(), /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
vcoubard 598:814c1ce92947 235 };
vcoubard 598:814c1ce92947 236
vcoubard 598:814c1ce92947 237 if (sd_ble_gap_scan_start(&scanParams) != NRF_SUCCESS) {
vcoubard 598:814c1ce92947 238 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 598:814c1ce92947 239 }
vcoubard 598:814c1ce92947 240
vcoubard 598:814c1ce92947 241 return BLE_ERROR_NONE;
vcoubard 598:814c1ce92947 242 }
vcoubard 598:814c1ce92947 243
vcoubard 598:814c1ce92947 244 ble_error_t nRF5xGap::stopScan(void) {
vcoubard 598:814c1ce92947 245 if (sd_ble_gap_scan_stop() == NRF_SUCCESS) {
vcoubard 598:814c1ce92947 246 return BLE_ERROR_NONE;
vcoubard 598:814c1ce92947 247 }
vcoubard 598:814c1ce92947 248
vcoubard 598:814c1ce92947 249 return BLE_STACK_BUSY;
vcoubard 598:814c1ce92947 250 }
vcoubard 598:814c1ce92947 251 #endif
vcoubard 598:814c1ce92947 252
vcoubard 541:884f95bf5351 253 /**************************************************************************/
vcoubard 541:884f95bf5351 254 /*!
vcoubard 541:884f95bf5351 255 @brief Stops the BLE HW and disconnects from any devices
vcoubard 541:884f95bf5351 256
vcoubard 541:884f95bf5351 257 @returns ble_error_t
vcoubard 541:884f95bf5351 258
vcoubard 541:884f95bf5351 259 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 260 Everything executed properly
vcoubard 541:884f95bf5351 261
vcoubard 541:884f95bf5351 262 @section EXAMPLE
vcoubard 541:884f95bf5351 263
vcoubard 541:884f95bf5351 264 @code
vcoubard 541:884f95bf5351 265
vcoubard 541:884f95bf5351 266 @endcode
vcoubard 541:884f95bf5351 267 */
vcoubard 541:884f95bf5351 268 /**************************************************************************/
vcoubard 541:884f95bf5351 269 ble_error_t nRF5xGap::stopAdvertising(void)
vcoubard 541:884f95bf5351 270 {
vcoubard 541:884f95bf5351 271 /* Stop Advertising */
vcoubard 541:884f95bf5351 272 ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 273
vcoubard 541:884f95bf5351 274 state.advertising = 0;
vcoubard 541:884f95bf5351 275
vcoubard 541:884f95bf5351 276 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 277 }
vcoubard 541:884f95bf5351 278
vcoubard 581:1dd90ec7527b 279 ble_error_t nRF5xGap::connect(const Address_t peerAddr,
vcoubard 581:1dd90ec7527b 280 BLEProtocol::AddressType_t peerAddrType,
vcoubard 581:1dd90ec7527b 281 const ConnectionParams_t *connectionParams,
vcoubard 581:1dd90ec7527b 282 const GapScanningParams *scanParamsIn)
vcoubard 541:884f95bf5351 283 {
vcoubard 541:884f95bf5351 284 ble_gap_addr_t addr;
vcoubard 541:884f95bf5351 285 addr.addr_type = peerAddrType;
vcoubard 541:884f95bf5351 286 memcpy(addr.addr, peerAddr, Gap::ADDR_LEN);
vcoubard 541:884f95bf5351 287
vcoubard 541:884f95bf5351 288 ble_gap_conn_params_t connParams;
vcoubard 541:884f95bf5351 289 if (connectionParams != NULL) {
vcoubard 541:884f95bf5351 290 connParams.min_conn_interval = connectionParams->minConnectionInterval;
vcoubard 541:884f95bf5351 291 connParams.max_conn_interval = connectionParams->maxConnectionInterval;
vcoubard 541:884f95bf5351 292 connParams.slave_latency = connectionParams->slaveLatency;
vcoubard 541:884f95bf5351 293 connParams.conn_sup_timeout = connectionParams->connectionSupervisionTimeout;
vcoubard 541:884f95bf5351 294 } else {
vcoubard 541:884f95bf5351 295 connParams.min_conn_interval = 50;
vcoubard 541:884f95bf5351 296 connParams.max_conn_interval = 100;
vcoubard 541:884f95bf5351 297 connParams.slave_latency = 0;
vcoubard 541:884f95bf5351 298 connParams.conn_sup_timeout = 600;
vcoubard 541:884f95bf5351 299 }
vcoubard 541:884f95bf5351 300
vcoubard 604:0f5c8725146c 301 /* Allocate the stack's whitelist statically */
vcoubard 604:0f5c8725146c 302 ble_gap_whitelist_t whitelist;
vcoubard 604:0f5c8725146c 303 ble_gap_addr_t *whitelistAddressPtrs[YOTTA_CFG_WHITELIST_MAX_SIZE];
vcoubard 604:0f5c8725146c 304 ble_gap_irk_t *whitelistIrkPtrs[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
vcoubard 604:0f5c8725146c 305 /* Initialize the whitelist */
vcoubard 604:0f5c8725146c 306 whitelist.pp_addrs = whitelistAddressPtrs;
vcoubard 604:0f5c8725146c 307 whitelist.pp_irks = whitelistIrkPtrs;
vcoubard 604:0f5c8725146c 308 whitelist.addr_count = 0;
vcoubard 604:0f5c8725146c 309 whitelist.irk_count = 0;
vcoubard 604:0f5c8725146c 310
vcoubard 598:814c1ce92947 311 /* Add missing IRKs to whitelist from the bond table held by the SoftDevice */
vcoubard 598:814c1ce92947 312 if (scanningPolicyMode != Gap::SCAN_POLICY_IGNORE_WHITELIST) {
vcoubard 604:0f5c8725146c 313 ble_error_t error = generateStackWhitelist(whitelist);
vcoubard 598:814c1ce92947 314 if (error != BLE_ERROR_NONE) {
vcoubard 598:814c1ce92947 315 return error;
vcoubard 598:814c1ce92947 316 }
vcoubard 598:814c1ce92947 317 }
vcoubard 598:814c1ce92947 318
vcoubard 541:884f95bf5351 319 ble_gap_scan_params_t scanParams;
vcoubard 596:b66851544182 320 scanParams.selective = scanningPolicyMode; /**< If 1, ignore unknown devices (non whitelisted). */
vcoubard 596:b66851544182 321 scanParams.p_whitelist = &whitelist; /**< Pointer to whitelist, NULL if none is given. */
vcoubard 541:884f95bf5351 322 if (scanParamsIn != NULL) {
vcoubard 541:884f95bf5351 323 scanParams.active = scanParamsIn->getActiveScanning(); /**< If 1, perform active scanning (scan requests). */
vcoubard 541:884f95bf5351 324 scanParams.interval = scanParamsIn->getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 325 scanParams.window = scanParamsIn->getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 326 scanParams.timeout = scanParamsIn->getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
vcoubard 541:884f95bf5351 327 } else {
vcoubard 541:884f95bf5351 328 scanParams.active = _scanningParams.getActiveScanning(); /**< If 1, perform active scanning (scan requests). */
vcoubard 541:884f95bf5351 329 scanParams.interval = _scanningParams.getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 330 scanParams.window = _scanningParams.getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 331 scanParams.timeout = _scanningParams.getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
vcoubard 541:884f95bf5351 332 }
vcoubard 541:884f95bf5351 333
vcoubard 541:884f95bf5351 334 uint32_t rc = sd_ble_gap_connect(&addr, &scanParams, &connParams);
vcoubard 541:884f95bf5351 335 if (rc == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 336 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 337 }
vcoubard 541:884f95bf5351 338 switch (rc) {
vcoubard 541:884f95bf5351 339 case NRF_ERROR_INVALID_ADDR:
vcoubard 541:884f95bf5351 340 return BLE_ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 341 case NRF_ERROR_INVALID_PARAM:
vcoubard 541:884f95bf5351 342 return BLE_ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 343 case NRF_ERROR_INVALID_STATE:
vcoubard 541:884f95bf5351 344 return BLE_ERROR_INVALID_STATE;
vcoubard 541:884f95bf5351 345 case BLE_ERROR_GAP_INVALID_BLE_ADDR:
vcoubard 541:884f95bf5351 346 return BLE_ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 347 case NRF_ERROR_NO_MEM:
vcoubard 541:884f95bf5351 348 return BLE_ERROR_NO_MEM;
vcoubard 541:884f95bf5351 349 case NRF_ERROR_BUSY:
vcoubard 541:884f95bf5351 350 return BLE_STACK_BUSY;
vcoubard 541:884f95bf5351 351 default:
vcoubard 541:884f95bf5351 352 case BLE_ERROR_GAP_WHITELIST_IN_USE:
vcoubard 541:884f95bf5351 353 return BLE_ERROR_UNSPECIFIED;
vcoubard 541:884f95bf5351 354 }
vcoubard 541:884f95bf5351 355 }
vcoubard 541:884f95bf5351 356
vcoubard 541:884f95bf5351 357 ble_error_t nRF5xGap::disconnect(Handle_t connectionHandle, DisconnectionReason_t reason)
vcoubard 541:884f95bf5351 358 {
vcoubard 541:884f95bf5351 359 state.advertising = 0;
vcoubard 541:884f95bf5351 360 state.connected = 0;
vcoubard 541:884f95bf5351 361
vcoubard 541:884f95bf5351 362 uint8_t code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION;
vcoubard 541:884f95bf5351 363 switch (reason) {
vcoubard 541:884f95bf5351 364 case REMOTE_USER_TERMINATED_CONNECTION:
vcoubard 541:884f95bf5351 365 code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION;
vcoubard 541:884f95bf5351 366 break;
vcoubard 541:884f95bf5351 367 case CONN_INTERVAL_UNACCEPTABLE:
vcoubard 541:884f95bf5351 368 code = BLE_HCI_CONN_INTERVAL_UNACCEPTABLE;
vcoubard 541:884f95bf5351 369 break;
vcoubard 541:884f95bf5351 370 default:
vcoubard 541:884f95bf5351 371 break;
vcoubard 541:884f95bf5351 372 }
vcoubard 541:884f95bf5351 373
vcoubard 541:884f95bf5351 374 /* Disconnect if we are connected to a central device */
vcoubard 541:884f95bf5351 375 ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(connectionHandle, code), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 376
vcoubard 541:884f95bf5351 377 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 378 }
vcoubard 541:884f95bf5351 379
vcoubard 541:884f95bf5351 380 /*!
vcoubard 541:884f95bf5351 381 @brief Disconnects if we are connected to a central device
vcoubard 541:884f95bf5351 382
vcoubard 541:884f95bf5351 383 @returns ble_error_t
vcoubard 541:884f95bf5351 384
vcoubard 541:884f95bf5351 385 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 386 Everything executed properly
vcoubard 541:884f95bf5351 387 */
vcoubard 541:884f95bf5351 388 ble_error_t nRF5xGap::disconnect(DisconnectionReason_t reason)
vcoubard 541:884f95bf5351 389 {
vcoubard 541:884f95bf5351 390 return disconnect(m_connectionHandle, reason);
vcoubard 541:884f95bf5351 391 }
vcoubard 541:884f95bf5351 392
vcoubard 541:884f95bf5351 393 ble_error_t nRF5xGap::getPreferredConnectionParams(ConnectionParams_t *params)
vcoubard 541:884f95bf5351 394 {
vcoubard 541:884f95bf5351 395 ASSERT_INT(NRF_SUCCESS,
vcoubard 541:884f95bf5351 396 sd_ble_gap_ppcp_get(reinterpret_cast<ble_gap_conn_params_t *>(params)),
vcoubard 541:884f95bf5351 397 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 398
vcoubard 541:884f95bf5351 399 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 400 }
vcoubard 541:884f95bf5351 401
vcoubard 541:884f95bf5351 402 ble_error_t nRF5xGap::setPreferredConnectionParams(const ConnectionParams_t *params)
vcoubard 541:884f95bf5351 403 {
vcoubard 541:884f95bf5351 404 ASSERT_INT(NRF_SUCCESS,
vcoubard 541:884f95bf5351 405 sd_ble_gap_ppcp_set(reinterpret_cast<const ble_gap_conn_params_t *>(params)),
vcoubard 541:884f95bf5351 406 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 407
vcoubard 541:884f95bf5351 408 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 409 }
vcoubard 541:884f95bf5351 410
vcoubard 541:884f95bf5351 411 ble_error_t nRF5xGap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *newParams)
vcoubard 541:884f95bf5351 412 {
vcoubard 541:884f95bf5351 413 uint32_t rc;
vcoubard 541:884f95bf5351 414
vcoubard 541:884f95bf5351 415 rc = sd_ble_gap_conn_param_update(handle, reinterpret_cast<ble_gap_conn_params_t *>(const_cast<ConnectionParams_t*>(newParams)));
vcoubard 541:884f95bf5351 416 if (rc == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 417 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 418 } else {
vcoubard 541:884f95bf5351 419 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 420 }
vcoubard 541:884f95bf5351 421 }
vcoubard 541:884f95bf5351 422
vcoubard 541:884f95bf5351 423 /**************************************************************************/
vcoubard 541:884f95bf5351 424 /*!
vcoubard 575:7023a8204a1b 425 @brief Clear nRF5xGap's state.
vcoubard 575:7023a8204a1b 426
vcoubard 575:7023a8204a1b 427 @returns ble_error_t
vcoubard 575:7023a8204a1b 428
vcoubard 575:7023a8204a1b 429 @retval BLE_ERROR_NONE
vcoubard 575:7023a8204a1b 430 Everything executed properly
vcoubard 575:7023a8204a1b 431 */
vcoubard 575:7023a8204a1b 432 /**************************************************************************/
vcoubard 575:7023a8204a1b 433 ble_error_t nRF5xGap::reset(void)
vcoubard 575:7023a8204a1b 434 {
vcoubard 575:7023a8204a1b 435 /* Clear all state that is from the parent, including private members */
vcoubard 575:7023a8204a1b 436 if (Gap::reset() != BLE_ERROR_NONE) {
vcoubard 575:7023a8204a1b 437 return BLE_ERROR_INVALID_STATE;
vcoubard 575:7023a8204a1b 438 }
vcoubard 575:7023a8204a1b 439
vcoubard 575:7023a8204a1b 440 /* Clear derived class members */
vcoubard 575:7023a8204a1b 441 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
vcoubard 575:7023a8204a1b 442
vcoubard 575:7023a8204a1b 443 return BLE_ERROR_NONE;
vcoubard 575:7023a8204a1b 444 }
vcoubard 575:7023a8204a1b 445
vcoubard 575:7023a8204a1b 446 /**************************************************************************/
vcoubard 575:7023a8204a1b 447 /*!
vcoubard 541:884f95bf5351 448 @brief Sets the 16-bit connection handle
vcoubard 541:884f95bf5351 449 */
vcoubard 541:884f95bf5351 450 /**************************************************************************/
vcoubard 541:884f95bf5351 451 void nRF5xGap::setConnectionHandle(uint16_t con_handle)
vcoubard 541:884f95bf5351 452 {
vcoubard 541:884f95bf5351 453 m_connectionHandle = con_handle;
vcoubard 541:884f95bf5351 454 }
vcoubard 541:884f95bf5351 455
vcoubard 541:884f95bf5351 456 /**************************************************************************/
vcoubard 541:884f95bf5351 457 /*!
vcoubard 541:884f95bf5351 458 @brief Gets the 16-bit connection handle
vcoubard 541:884f95bf5351 459 */
vcoubard 541:884f95bf5351 460 /**************************************************************************/
vcoubard 541:884f95bf5351 461 uint16_t nRF5xGap::getConnectionHandle(void)
vcoubard 541:884f95bf5351 462 {
vcoubard 541:884f95bf5351 463 return m_connectionHandle;
vcoubard 541:884f95bf5351 464 }
vcoubard 541:884f95bf5351 465
vcoubard 562:0d32ae12429e 466 /**************************************************************************/
vcoubard 541:884f95bf5351 467 /*!
vcoubard 541:884f95bf5351 468 @brief Sets the BLE device address
vcoubard 541:884f95bf5351 469
vcoubard 541:884f95bf5351 470 @returns ble_error_t
vcoubard 541:884f95bf5351 471
vcoubard 541:884f95bf5351 472 @section EXAMPLE
vcoubard 541:884f95bf5351 473
vcoubard 541:884f95bf5351 474 @code
vcoubard 541:884f95bf5351 475
vcoubard 541:884f95bf5351 476 uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 };
vcoubard 577:d38f01a3e701 477 nrf.getGap().setAddress(Gap::BLEProtocol::AddressType::RANDOM_STATIC, device_address);
vcoubard 541:884f95bf5351 478
vcoubard 541:884f95bf5351 479 @endcode
vcoubard 541:884f95bf5351 480 */
vcoubard 541:884f95bf5351 481 /**************************************************************************/
vcoubard 541:884f95bf5351 482 ble_error_t nRF5xGap::setAddress(AddressType_t type, const Address_t address)
vcoubard 541:884f95bf5351 483 {
vcoubard 552:20b282c26f96 484 uint8_t cycle_mode;
vcoubard 552:20b282c26f96 485 ble_gap_addr_t dev_addr;
vcoubard 552:20b282c26f96 486
vcoubard 552:20b282c26f96 487 /* When using Public or Static addresses, the cycle mode must be None.
vcoubard 552:20b282c26f96 488 When using Random Private addresses, the cycle mode must be Auto.
vcoubard 552:20b282c26f96 489 In auto mode, the given address is ignored.
vcoubard 552:20b282c26f96 490 */
vcoubard 577:d38f01a3e701 491 if ((type == BLEProtocol::AddressType::PUBLIC) || (type == BLEProtocol::AddressType::RANDOM_STATIC))
vcoubard 552:20b282c26f96 492 {
vcoubard 552:20b282c26f96 493 cycle_mode = BLE_GAP_ADDR_CYCLE_MODE_NONE;
vcoubard 552:20b282c26f96 494 memcpy(dev_addr.addr, address, ADDR_LEN);
vcoubard 552:20b282c26f96 495 }
vcoubard 577:d38f01a3e701 496 else if ((type == BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE) || (type == BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE))
vcoubard 552:20b282c26f96 497 {
vcoubard 552:20b282c26f96 498 cycle_mode = BLE_GAP_ADDR_CYCLE_MODE_AUTO;
vcoubard 552:20b282c26f96 499 // address is ignored when in auto mode
vcoubard 552:20b282c26f96 500 }
vcoubard 552:20b282c26f96 501 else
vcoubard 552:20b282c26f96 502 {
vcoubard 541:884f95bf5351 503 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 504 }
vcoubard 541:884f95bf5351 505
vcoubard 541:884f95bf5351 506 dev_addr.addr_type = type;
vcoubard 552:20b282c26f96 507 ASSERT_INT(ERROR_NONE, sd_ble_gap_address_set(cycle_mode, &dev_addr), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 508
vcoubard 541:884f95bf5351 509 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 510 }
vcoubard 541:884f95bf5351 511
vcoubard 541:884f95bf5351 512 ble_error_t nRF5xGap::getAddress(AddressType_t *typeP, Address_t address)
vcoubard 541:884f95bf5351 513 {
vcoubard 541:884f95bf5351 514 ble_gap_addr_t dev_addr;
vcoubard 541:884f95bf5351 515 if (sd_ble_gap_address_get(&dev_addr) != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 516 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 517 }
vcoubard 541:884f95bf5351 518
vcoubard 541:884f95bf5351 519 if (typeP != NULL) {
vcoubard 541:884f95bf5351 520 *typeP = static_cast<AddressType_t>(dev_addr.addr_type);
vcoubard 541:884f95bf5351 521 }
vcoubard 541:884f95bf5351 522 if (address != NULL) {
vcoubard 541:884f95bf5351 523 memcpy(address, dev_addr.addr, ADDR_LEN);
vcoubard 541:884f95bf5351 524 }
vcoubard 541:884f95bf5351 525 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 526 }
vcoubard 541:884f95bf5351 527
vcoubard 541:884f95bf5351 528 ble_error_t nRF5xGap::setDeviceName(const uint8_t *deviceName)
vcoubard 541:884f95bf5351 529 {
vcoubard 541:884f95bf5351 530 ble_gap_conn_sec_mode_t sec_mode;
vcoubard 541:884f95bf5351 531 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed
vcoubard 541:884f95bf5351 532
vcoubard 541:884f95bf5351 533 if (sd_ble_gap_device_name_set(&sec_mode, deviceName, strlen((const char *)deviceName)) == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 534 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 535 } else {
vcoubard 541:884f95bf5351 536 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 537 }
vcoubard 541:884f95bf5351 538 }
vcoubard 541:884f95bf5351 539
vcoubard 541:884f95bf5351 540 ble_error_t nRF5xGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP)
vcoubard 541:884f95bf5351 541 {
vcoubard 541:884f95bf5351 542 if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 543 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 544 } else {
vcoubard 541:884f95bf5351 545 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 546 }
vcoubard 541:884f95bf5351 547 }
vcoubard 541:884f95bf5351 548
vcoubard 541:884f95bf5351 549 ble_error_t nRF5xGap::setAppearance(GapAdvertisingData::Appearance appearance)
vcoubard 541:884f95bf5351 550 {
vcoubard 541:884f95bf5351 551 if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 552 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 553 } else {
vcoubard 541:884f95bf5351 554 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 555 }
vcoubard 541:884f95bf5351 556 }
vcoubard 541:884f95bf5351 557
vcoubard 541:884f95bf5351 558 ble_error_t nRF5xGap::getAppearance(GapAdvertisingData::Appearance *appearanceP)
vcoubard 541:884f95bf5351 559 {
vcoubard 541:884f95bf5351 560 if ((sd_ble_gap_appearance_get(reinterpret_cast<uint16_t *>(appearanceP)) == NRF_SUCCESS)) {
vcoubard 541:884f95bf5351 561 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 562 } else {
vcoubard 541:884f95bf5351 563 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 564 }
vcoubard 541:884f95bf5351 565 }
vcoubard 541:884f95bf5351 566
vcoubard 541:884f95bf5351 567 /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */
vcoubard 541:884f95bf5351 568 ble_error_t nRF5xGap::setTxPower(int8_t txPower)
vcoubard 541:884f95bf5351 569 {
vcoubard 541:884f95bf5351 570 unsigned rc;
vcoubard 541:884f95bf5351 571 if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 572 switch (rc) {
vcoubard 541:884f95bf5351 573 case NRF_ERROR_BUSY:
vcoubard 541:884f95bf5351 574 return BLE_STACK_BUSY;
vcoubard 541:884f95bf5351 575 case NRF_ERROR_INVALID_PARAM:
vcoubard 541:884f95bf5351 576 default:
vcoubard 541:884f95bf5351 577 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 578 }
vcoubard 541:884f95bf5351 579 }
vcoubard 541:884f95bf5351 580
vcoubard 541:884f95bf5351 581 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 582 }
vcoubard 541:884f95bf5351 583
vcoubard 541:884f95bf5351 584 void nRF5xGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP)
vcoubard 541:884f95bf5351 585 {
vcoubard 541:884f95bf5351 586 static const int8_t permittedTxValues[] = {
vcoubard 541:884f95bf5351 587 -40, -30, -20, -16, -12, -8, -4, 0, 4
vcoubard 541:884f95bf5351 588 };
vcoubard 541:884f95bf5351 589
vcoubard 541:884f95bf5351 590 *valueArrayPP = permittedTxValues;
vcoubard 541:884f95bf5351 591 *countP = sizeof(permittedTxValues) / sizeof(int8_t);
vcoubard 596:b66851544182 592 }
vcoubard 596:b66851544182 593
vcoubard 601:5f4199aae50f 594 /**************************************************************************/
vcoubard 601:5f4199aae50f 595 /*!
vcoubard 601:5f4199aae50f 596 @brief Get the capacity of the internal whitelist maintained by this
vcoubard 601:5f4199aae50f 597 implementation.
vcoubard 601:5f4199aae50f 598
vcoubard 601:5f4199aae50f 599 @returns The capacity of the internal whitelist.
vcoubard 601:5f4199aae50f 600
vcoubard 601:5f4199aae50f 601 @section EXAMPLE
vcoubard 601:5f4199aae50f 602
vcoubard 601:5f4199aae50f 603 @code
vcoubard 601:5f4199aae50f 604
vcoubard 601:5f4199aae50f 605 @endcode
vcoubard 601:5f4199aae50f 606 */
vcoubard 601:5f4199aae50f 607 /**************************************************************************/
vcoubard 598:814c1ce92947 608 uint8_t nRF5xGap::getMaxWhitelistSize(void) const
vcoubard 596:b66851544182 609 {
vcoubard 596:b66851544182 610 return YOTTA_CFG_WHITELIST_MAX_SIZE;
vcoubard 596:b66851544182 611 }
vcoubard 596:b66851544182 612
vcoubard 601:5f4199aae50f 613 /**************************************************************************/
vcoubard 601:5f4199aae50f 614 /*!
vcoubard 601:5f4199aae50f 615 @brief Get a copy of the implementation's internal whitelist.
vcoubard 601:5f4199aae50f 616
vcoubard 601:5f4199aae50f 617 @param[out] whitelistOut
vcoubard 601:5f4199aae50f 618 A \ref Gap::Whitelist_t structure containing a copy of the
vcoubard 601:5f4199aae50f 619 addresses in the implemenetation's internal whitelist.
vcoubard 601:5f4199aae50f 620
vcoubard 601:5f4199aae50f 621 @returns \ref ble_errror_t
vcoubard 601:5f4199aae50f 622
vcoubard 601:5f4199aae50f 623 @retval BLE_ERROR_NONE
vcoubard 601:5f4199aae50f 624 Everything executed properly.
vcoubard 601:5f4199aae50f 625
vcoubard 601:5f4199aae50f 626 @section EXAMPLE
vcoubard 601:5f4199aae50f 627
vcoubard 601:5f4199aae50f 628 @code
vcoubard 601:5f4199aae50f 629
vcoubard 601:5f4199aae50f 630 @endcode
vcoubard 601:5f4199aae50f 631 */
vcoubard 601:5f4199aae50f 632 /**************************************************************************/
vcoubard 598:814c1ce92947 633 ble_error_t nRF5xGap::getWhitelist(Gap::Whitelist_t &whitelistOut) const
vcoubard 596:b66851544182 634 {
vcoubard 598:814c1ce92947 635 uint8_t i;
vcoubard 598:814c1ce92947 636 for (i = 0; i < whitelistAddressesSize && i < whitelistOut.capacity; ++i) {
vcoubard 598:814c1ce92947 637 memcpy(&whitelistOut.addresses[i], &whitelistAddresses[i], sizeof(BLEProtocol::Address_t));
vcoubard 596:b66851544182 638 }
vcoubard 598:814c1ce92947 639 whitelistOut.size = i;
vcoubard 598:814c1ce92947 640
vcoubard 596:b66851544182 641 return BLE_ERROR_NONE;
vcoubard 596:b66851544182 642 }
vcoubard 596:b66851544182 643
vcoubard 601:5f4199aae50f 644 /**************************************************************************/
vcoubard 601:5f4199aae50f 645 /*!
vcoubard 601:5f4199aae50f 646 @brief Set the whitelist that will be used in the next call to
vcoubard 601:5f4199aae50f 647 startAdvertising().
vcoubard 601:5f4199aae50f 648
vcoubard 601:5f4199aae50f 649 @param[in] whitelistIn
vcoubard 601:5f4199aae50f 650 A reference to a \ref Gap::Whitelist_t structure
vcoubard 601:5f4199aae50f 651 representing a whitelist containing all the white listed
vcoubard 601:5f4199aae50f 652 BLE addresses.
vcoubard 601:5f4199aae50f 653
vcoubard 601:5f4199aae50f 654 @returns \ref ble_errror_t
vcoubard 601:5f4199aae50f 655
vcoubard 601:5f4199aae50f 656 @retval BLE_ERROR_NONE
vcoubard 601:5f4199aae50f 657 Everything executed properly.
vcoubard 601:5f4199aae50f 658
vcoubard 601:5f4199aae50f 659 BLE_ERROR_INVALID_PARAM
vcoubard 601:5f4199aae50f 660 The supplied whitelist contains a private non-resolvable
vcoubard 601:5f4199aae50f 661 address
vcoubard 601:5f4199aae50f 662
vcoubard 601:5f4199aae50f 663 BLE_ERROR_PARAM_OUT_OF_RANGE
vcoubard 601:5f4199aae50f 664 The size of the supplied whitelist exceeds the maximum
vcoubard 601:5f4199aae50f 665 capacity of the implementation's internal whitelist.
vcoubard 601:5f4199aae50f 666
vcoubard 601:5f4199aae50f 667 @section EXAMPLE
vcoubard 601:5f4199aae50f 668
vcoubard 601:5f4199aae50f 669 @code
vcoubard 601:5f4199aae50f 670
vcoubard 601:5f4199aae50f 671 @endcode
vcoubard 601:5f4199aae50f 672 */
vcoubard 601:5f4199aae50f 673 /**************************************************************************/
vcoubard 598:814c1ce92947 674 ble_error_t nRF5xGap::setWhitelist(const Gap::Whitelist_t &whitelistIn)
vcoubard 596:b66851544182 675 {
vcoubard 598:814c1ce92947 676 if (whitelistIn.size > getMaxWhitelistSize()) {
vcoubard 598:814c1ce92947 677 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 598:814c1ce92947 678 }
vcoubard 598:814c1ce92947 679
vcoubard 603:b1616eaf8206 680 /* Test for invalid parameters before we change the internal state */
vcoubard 598:814c1ce92947 681 for (uint8_t i = 0; i < whitelistIn.size; ++i) {
vcoubard 598:814c1ce92947 682 if (whitelistIn.addresses[i].type == BLEProtocol::AddressType_t::RANDOM_PRIVATE_NON_RESOLVABLE) {
vcoubard 598:814c1ce92947 683 /* This is not allowed because it is completely meaningless */
vcoubard 598:814c1ce92947 684 return BLE_ERROR_INVALID_PARAM;
vcoubard 598:814c1ce92947 685 }
vcoubard 603:b1616eaf8206 686 }
vcoubard 603:b1616eaf8206 687
vcoubard 603:b1616eaf8206 688 whitelistAddressesSize = 0;
vcoubard 603:b1616eaf8206 689 for (uint8_t i = 0; i < whitelistIn.size; ++i) {
vcoubard 598:814c1ce92947 690 memcpy(&whitelistAddresses[whitelistAddressesSize], &whitelistIn.addresses[i], sizeof(BLEProtocol::Address_t));
vcoubard 596:b66851544182 691 whitelistAddressesSize++;
vcoubard 596:b66851544182 692 }
vcoubard 598:814c1ce92947 693
vcoubard 596:b66851544182 694 return BLE_ERROR_NONE;
vcoubard 596:b66851544182 695 }
vcoubard 596:b66851544182 696
vcoubard 601:5f4199aae50f 697 /**************************************************************************/
vcoubard 601:5f4199aae50f 698 /*!
vcoubard 601:5f4199aae50f 699 @brief Set the advertising policy filter mode that will be used in
vcoubard 601:5f4199aae50f 700 the next call to startAdvertising().
vcoubard 601:5f4199aae50f 701
vcoubard 601:5f4199aae50f 702 @returns \ref ble_errror_t
vcoubard 601:5f4199aae50f 703
vcoubard 601:5f4199aae50f 704 @retval BLE_ERROR_NONE
vcoubard 601:5f4199aae50f 705 Everything executed properly.
vcoubard 601:5f4199aae50f 706
vcoubard 601:5f4199aae50f 707 BLE_ERROR_NOT_IMPLEMENTED
vcoubard 601:5f4199aae50f 708 This feature is currently note implemented.
vcoubard 601:5f4199aae50f 709
vcoubard 601:5f4199aae50f 710 @section EXAMPLE
vcoubard 601:5f4199aae50f 711
vcoubard 601:5f4199aae50f 712 @code
vcoubard 601:5f4199aae50f 713
vcoubard 601:5f4199aae50f 714 @endcode
vcoubard 601:5f4199aae50f 715 */
vcoubard 601:5f4199aae50f 716 /**************************************************************************/
vcoubard 598:814c1ce92947 717 ble_error_t nRF5xGap::setAdvertisingPolicyMode(Gap::AdvertisingPolicyMode_t mode)
vcoubard 596:b66851544182 718 {
vcoubard 596:b66851544182 719 advertisingPolicyMode = mode;
vcoubard 598:814c1ce92947 720
vcoubard 598:814c1ce92947 721 return BLE_ERROR_NONE;
vcoubard 596:b66851544182 722 }
vcoubard 596:b66851544182 723
vcoubard 601:5f4199aae50f 724 /**************************************************************************/
vcoubard 601:5f4199aae50f 725 /*!
vcoubard 601:5f4199aae50f 726 @brief Set the scanning policy filter mode that will be used in
vcoubard 601:5f4199aae50f 727 the next call to startAdvertising().
vcoubard 601:5f4199aae50f 728
vcoubard 601:5f4199aae50f 729 @returns \ref ble_errror_t
vcoubard 601:5f4199aae50f 730
vcoubard 601:5f4199aae50f 731 @retval BLE_ERROR_NONE
vcoubard 601:5f4199aae50f 732 Everything executed properly.
vcoubard 601:5f4199aae50f 733
vcoubard 601:5f4199aae50f 734 BLE_ERROR_NOT_IMPLEMENTED
vcoubard 601:5f4199aae50f 735 This feature is currently note implemented.
vcoubard 601:5f4199aae50f 736
vcoubard 601:5f4199aae50f 737 @section EXAMPLE
vcoubard 601:5f4199aae50f 738
vcoubard 601:5f4199aae50f 739 @code
vcoubard 601:5f4199aae50f 740
vcoubard 601:5f4199aae50f 741 @endcode
vcoubard 601:5f4199aae50f 742 */
vcoubard 601:5f4199aae50f 743 /**************************************************************************/
vcoubard 598:814c1ce92947 744 ble_error_t nRF5xGap::setScanningPolicyMode(Gap::ScanningPolicyMode_t mode)
vcoubard 596:b66851544182 745 {
vcoubard 596:b66851544182 746 scanningPolicyMode = mode;
vcoubard 598:814c1ce92947 747
vcoubard 598:814c1ce92947 748 return BLE_ERROR_NONE;
vcoubard 596:b66851544182 749 }
vcoubard 596:b66851544182 750
vcoubard 601:5f4199aae50f 751 /**************************************************************************/
vcoubard 601:5f4199aae50f 752 /*!
vcoubard 601:5f4199aae50f 753 @brief Set the initiator policy filter mode that will be used in
vcoubard 601:5f4199aae50f 754 the next call to startAdvertising()
vcoubard 601:5f4199aae50f 755
vcoubard 601:5f4199aae50f 756 @returns \ref ble_errror_t
vcoubard 601:5f4199aae50f 757
vcoubard 601:5f4199aae50f 758 @retval BLE_ERROR_NONE
vcoubard 601:5f4199aae50f 759 Everything executed properly.
vcoubard 601:5f4199aae50f 760
vcoubard 601:5f4199aae50f 761 BLE_ERROR_NOT_IMPLEMENTED
vcoubard 601:5f4199aae50f 762 This feature is currently note implemented.
vcoubard 601:5f4199aae50f 763
vcoubard 601:5f4199aae50f 764 @section EXAMPLE
vcoubard 601:5f4199aae50f 765
vcoubard 601:5f4199aae50f 766 @code
vcoubard 601:5f4199aae50f 767
vcoubard 601:5f4199aae50f 768 @endcode
vcoubard 601:5f4199aae50f 769 */
vcoubard 601:5f4199aae50f 770 /**************************************************************************/
vcoubard 598:814c1ce92947 771 ble_error_t nRF5xGap::setInitiatorPolicyMode(Gap::InitiatorPolicyMode_t mode)
vcoubard 596:b66851544182 772 {
vcoubard 598:814c1ce92947 773 return BLE_ERROR_NOT_IMPLEMENTED;
vcoubard 596:b66851544182 774 }
vcoubard 596:b66851544182 775
vcoubard 601:5f4199aae50f 776 /**************************************************************************/
vcoubard 601:5f4199aae50f 777 /*!
vcoubard 601:5f4199aae50f 778 @brief Get the current advertising policy filter mode.
vcoubard 601:5f4199aae50f 779
vcoubard 601:5f4199aae50f 780 @returns The advertising policy filter mode.
vcoubard 601:5f4199aae50f 781
vcoubard 601:5f4199aae50f 782 @section EXAMPLE
vcoubard 601:5f4199aae50f 783
vcoubard 601:5f4199aae50f 784 @code
vcoubard 601:5f4199aae50f 785
vcoubard 601:5f4199aae50f 786 @endcode
vcoubard 601:5f4199aae50f 787 */
vcoubard 601:5f4199aae50f 788 /**************************************************************************/
vcoubard 596:b66851544182 789 Gap::AdvertisingPolicyMode_t nRF5xGap::getAdvertisingPolicyMode(void) const
vcoubard 596:b66851544182 790 {
vcoubard 596:b66851544182 791 return advertisingPolicyMode;
vcoubard 596:b66851544182 792 }
vcoubard 596:b66851544182 793
vcoubard 601:5f4199aae50f 794 /**************************************************************************/
vcoubard 601:5f4199aae50f 795 /*!
vcoubard 601:5f4199aae50f 796 @brief Get the current scanning policy filter mode.
vcoubard 601:5f4199aae50f 797
vcoubard 601:5f4199aae50f 798 @returns The scanning policy filter mode.
vcoubard 601:5f4199aae50f 799
vcoubard 601:5f4199aae50f 800 @section EXAMPLE
vcoubard 601:5f4199aae50f 801
vcoubard 601:5f4199aae50f 802 @code
vcoubard 601:5f4199aae50f 803
vcoubard 601:5f4199aae50f 804 @endcode
vcoubard 601:5f4199aae50f 805 */
vcoubard 601:5f4199aae50f 806 /**************************************************************************/
vcoubard 596:b66851544182 807 Gap::ScanningPolicyMode_t nRF5xGap::getScanningPolicyMode(void) const
vcoubard 596:b66851544182 808 {
vcoubard 596:b66851544182 809 return scanningPolicyMode;
vcoubard 596:b66851544182 810 }
vcoubard 596:b66851544182 811
vcoubard 601:5f4199aae50f 812 /**************************************************************************/
vcoubard 601:5f4199aae50f 813 /*!
vcoubard 601:5f4199aae50f 814 @brief Get the current initiator policy filter mode.
vcoubard 601:5f4199aae50f 815
vcoubard 601:5f4199aae50f 816 @returns The initiator policy filter mode.
vcoubard 601:5f4199aae50f 817
vcoubard 601:5f4199aae50f 818 @note Currently initiator filtering using the whitelist is not
vcoubard 601:5f4199aae50f 819 implemented in this module.
vcoubard 601:5f4199aae50f 820
vcoubard 601:5f4199aae50f 821 @section EXAMPLE
vcoubard 601:5f4199aae50f 822
vcoubard 601:5f4199aae50f 823 @code
vcoubard 601:5f4199aae50f 824
vcoubard 601:5f4199aae50f 825 @endcode
vcoubard 601:5f4199aae50f 826 */
vcoubard 601:5f4199aae50f 827 /**************************************************************************/
vcoubard 596:b66851544182 828 Gap::InitiatorPolicyMode_t nRF5xGap::getInitiatorPolicyMode(void) const
vcoubard 596:b66851544182 829 {
vcoubard 598:814c1ce92947 830 return Gap::INIT_POLICY_IGNORE_WHITELIST;
vcoubard 596:b66851544182 831 }
vcoubard 598:814c1ce92947 832
vcoubard 600:0978b5626451 833 /**************************************************************************/
vcoubard 600:0978b5626451 834 /*!
vcoubard 600:0978b5626451 835 @brief Helper function used to populate the ble_gap_whitelist_t that
vcoubard 600:0978b5626451 836 will be used by the SoftDevice for filtering requests.
vcoubard 600:0978b5626451 837
vcoubard 600:0978b5626451 838 @returns \ref ble_error_t
vcoubard 600:0978b5626451 839
vcoubard 600:0978b5626451 840 @retval BLE_ERROR_NONE
vcoubard 600:0978b5626451 841 Everything executed properly
vcoubard 600:0978b5626451 842
vcoubard 601:5f4199aae50f 843 @retval BLE_ERROR_INVALID_STATE
vcoubard 601:5f4199aae50f 844 The internal stack was not initialized correctly.
vcoubard 600:0978b5626451 845
vcoubard 601:5f4199aae50f 846 @note Both the SecurityManager and Gap must initialize correctly for
vcoubard 601:5f4199aae50f 847 this function to succeed.
vcoubard 600:0978b5626451 848
vcoubard 600:0978b5626451 849 @note This function is needed because for the BLE API the whitelist
vcoubard 600:0978b5626451 850 is just a collection of keys, but for the stack it also includes
vcoubard 600:0978b5626451 851 the IRK table.
vcoubard 600:0978b5626451 852
vcoubard 600:0978b5626451 853 @section EXAMPLE
vcoubard 600:0978b5626451 854
vcoubard 600:0978b5626451 855 @code
vcoubard 600:0978b5626451 856
vcoubard 600:0978b5626451 857 @endcode
vcoubard 600:0978b5626451 858 */
vcoubard 600:0978b5626451 859 /**************************************************************************/
vcoubard 604:0f5c8725146c 860 ble_error_t nRF5xGap::generateStackWhitelist(ble_gap_whitelist_t &whitelist)
vcoubard 598:814c1ce92947 861 {
vcoubard 598:814c1ce92947 862 ble_gap_whitelist_t whitelistFromBondTable;
vcoubard 598:814c1ce92947 863 ble_gap_addr_t *addressPtr[1];
vcoubard 598:814c1ce92947 864 ble_gap_irk_t *irkPtr[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
vcoubard 598:814c1ce92947 865
vcoubard 598:814c1ce92947 866 nRF5xSecurityManager& securityManager = (nRF5xSecurityManager&) nRF5xn::Instance(0).getSecurityManager();
vcoubard 598:814c1ce92947 867
vcoubard 598:814c1ce92947 868 if (securityManager.hasInitialized()) {
vcoubard 598:814c1ce92947 869 /* We do not care about the addresses, set the count to 0 */
vcoubard 598:814c1ce92947 870 whitelistFromBondTable.addr_count = 0;
vcoubard 598:814c1ce92947 871 /* The Nordic SDK will return a failure if we set pp_addr to NULL */
vcoubard 598:814c1ce92947 872 whitelistFromBondTable.pp_addrs = addressPtr;
vcoubard 598:814c1ce92947 873 /* We want all the IRKs we can get because we do not know which ones match the addresses */
vcoubard 598:814c1ce92947 874 whitelistFromBondTable.irk_count = YOTTA_CFG_IRK_TABLE_MAX_SIZE;
vcoubard 598:814c1ce92947 875 whitelistFromBondTable.pp_irks = irkPtr;
vcoubard 598:814c1ce92947 876
vcoubard 598:814c1ce92947 877 /* Use the security manager to get the IRKs from the bond table */
vcoubard 598:814c1ce92947 878 ble_error_t error = securityManager.createWhitelistFromBondTable(whitelistFromBondTable);
vcoubard 598:814c1ce92947 879 if (error != BLE_ERROR_NONE) {
vcoubard 598:814c1ce92947 880 return error;
vcoubard 598:814c1ce92947 881 }
vcoubard 598:814c1ce92947 882 } else {
vcoubard 598:814c1ce92947 883 /**
vcoubard 598:814c1ce92947 884 * If there is no security manager then we cannot access the bond table,
vcoubard 598:814c1ce92947 885 * so disable IRK matching
vcoubard 598:814c1ce92947 886 */
vcoubard 598:814c1ce92947 887 whitelistFromBondTable.addr_count = 0;
vcoubard 598:814c1ce92947 888 whitelistFromBondTable.irk_count = 0;
vcoubard 598:814c1ce92947 889 }
vcoubard 598:814c1ce92947 890
vcoubard 598:814c1ce92947 891 /**
vcoubard 598:814c1ce92947 892 * For every private resolvable address in the local whitelist check if
vcoubard 598:814c1ce92947 893 * there is an IRK for said address in the bond table and add it to the
vcoubard 598:814c1ce92947 894 * local IRK list.
vcoubard 598:814c1ce92947 895 */
vcoubard 598:814c1ce92947 896 whitelist.irk_count = 0;
vcoubard 598:814c1ce92947 897 whitelist.addr_count = 0;
vcoubard 598:814c1ce92947 898 for (uint8_t i = 0; i < whitelistAddressesSize; ++i) {
vcoubard 598:814c1ce92947 899 if (whitelistAddresses[i].addr_type == BLEProtocol::AddressType_t::RANDOM_PRIVATE_RESOLVABLE) {
vcoubard 598:814c1ce92947 900 /* Test if there is a matching IRK for this private resolvable address */
vcoubard 598:814c1ce92947 901 for (uint8_t j = 0; j < whitelistFromBondTable.irk_count; ++j) {
vcoubard 598:814c1ce92947 902 if (securityManager.matchAddressAndIrk(&whitelistAddresses[i], whitelistFromBondTable.pp_irks[j])) {
vcoubard 598:814c1ce92947 903 /* Found the corresponding IRK, add it to our local whitelist */
vcoubard 598:814c1ce92947 904 whitelist.pp_irks[whitelist.irk_count] = whitelistFromBondTable.pp_irks[j];
vcoubard 598:814c1ce92947 905 whitelist.irk_count++;
vcoubard 598:814c1ce92947 906 /* Make sure we do not look at this IRK again */
vcoubard 598:814c1ce92947 907 if (j != whitelistFromBondTable.irk_count - 1) {
vcoubard 598:814c1ce92947 908 /**
vcoubard 598:814c1ce92947 909 * This is not the last IRK, so replace the pointer
vcoubard 598:814c1ce92947 910 * with the last pointer in the array
vcoubard 598:814c1ce92947 911 */
vcoubard 598:814c1ce92947 912 whitelistFromBondTable.pp_irks[j] =
vcoubard 598:814c1ce92947 913 whitelistFromBondTable.pp_irks[whitelistFromBondTable.irk_count - 1];
vcoubard 598:814c1ce92947 914 }
vcoubard 598:814c1ce92947 915 /**
vcoubard 598:814c1ce92947 916 * If the IRK is the last pointer in the array simply
vcoubard 598:814c1ce92947 917 * decrement the total IRK count
vcoubard 598:814c1ce92947 918 */
vcoubard 598:814c1ce92947 919 whitelistFromBondTable.irk_count--;
vcoubard 598:814c1ce92947 920 break;
vcoubard 598:814c1ce92947 921 }
vcoubard 598:814c1ce92947 922 }
vcoubard 598:814c1ce92947 923 } else {
vcoubard 598:814c1ce92947 924 /* Include the address into the whitelist */
vcoubard 598:814c1ce92947 925 whitelist.pp_addrs[whitelist.addr_count] = &whitelistAddresses[i];
vcoubard 598:814c1ce92947 926 whitelist.addr_count++;
vcoubard 598:814c1ce92947 927 }
vcoubard 598:814c1ce92947 928 }
vcoubard 598:814c1ce92947 929
vcoubard 598:814c1ce92947 930 return BLE_ERROR_NONE;
vcoubard 598:814c1ce92947 931 }