No changes

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 10:19:37 2016 +0000
Revision:
601:5f4199aae50f
Parent:
600:0978b5626451
Child:
603:b1616eaf8206
Synchronized with git rev b72f9aa2
Author: Andres Amaya Garcia
Add whitelisting documentation to nRF51xGap

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 598:814c1ce92947 171 /* Add missing IRKs to whitelist from the bond table held by the SoftDevice */
vcoubard 598:814c1ce92947 172 if (advertisingPolicyMode != Gap::ADV_POLICY_IGNORE_WHITELIST) {
vcoubard 598:814c1ce92947 173 ble_error_t error = generateStackWhitelist();
vcoubard 598:814c1ce92947 174 if (error != BLE_ERROR_NONE) {
vcoubard 598:814c1ce92947 175 return error;
vcoubard 598:814c1ce92947 176 }
vcoubard 598:814c1ce92947 177 } else {
vcoubard 598:814c1ce92947 178 /* Reset the whitelist table to avoid any errors */
vcoubard 598:814c1ce92947 179 whitelist.addr_count = 0;
vcoubard 598:814c1ce92947 180 whitelist.irk_count = 0;
vcoubard 598:814c1ce92947 181 }
vcoubard 598:814c1ce92947 182
vcoubard 541:884f95bf5351 183 /* Start Advertising */
vcoubard 541:884f95bf5351 184 ble_gap_adv_params_t adv_para = {0};
vcoubard 541:884f95bf5351 185
vcoubard 541:884f95bf5351 186 adv_para.type = params.getAdvertisingType();
vcoubard 541:884f95bf5351 187 adv_para.p_peer_addr = NULL; // Undirected advertisement
vcoubard 596:b66851544182 188 adv_para.fp = advertisingPolicyMode;
vcoubard 596:b66851544182 189 adv_para.p_whitelist = &whitelist;
vcoubard 541:884f95bf5351 190 adv_para.interval = params.getIntervalInADVUnits(); // advertising interval (in units of 0.625 ms)
vcoubard 541:884f95bf5351 191 adv_para.timeout = params.getTimeout();
vcoubard 541:884f95bf5351 192
vcoubard 541:884f95bf5351 193 ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 194
vcoubard 541:884f95bf5351 195 state.advertising = 1;
vcoubard 541:884f95bf5351 196
vcoubard 541:884f95bf5351 197 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 198 }
vcoubard 541:884f95bf5351 199
vcoubard 598:814c1ce92947 200 /* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */
vcoubard 598:814c1ce92947 201 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
vcoubard 598:814c1ce92947 202 ble_error_t nRF5xGap::startRadioScan(const GapScanningParams &scanningParams) {
vcoubard 598:814c1ce92947 203
vcoubard 598:814c1ce92947 204 /* Add missing IRKs to whitelist from the bond table held by the SoftDevice */
vcoubard 598:814c1ce92947 205 if (scanningPolicyMode != Gap::SCAN_POLICY_IGNORE_WHITELIST) {
vcoubard 598:814c1ce92947 206 ble_error_t error = generateStackWhitelist();
vcoubard 598:814c1ce92947 207 if (error != BLE_ERROR_NONE) {
vcoubard 598:814c1ce92947 208 return error;
vcoubard 598:814c1ce92947 209 }
vcoubard 598:814c1ce92947 210 } else {
vcoubard 598:814c1ce92947 211 /* Reset the whitelist table to avoid any errors */
vcoubard 598:814c1ce92947 212 whitelist.addr_count = 0;
vcoubard 598:814c1ce92947 213 whitelist.irk_count = 0;
vcoubard 598:814c1ce92947 214 }
vcoubard 598:814c1ce92947 215
vcoubard 598:814c1ce92947 216 ble_gap_scan_params_t scanParams = {
vcoubard 598:814c1ce92947 217 .active = scanningParams.getActiveScanning(), /**< If 1, perform active scanning (scan requests). */
vcoubard 598:814c1ce92947 218 .selective = scanningPolicyMode, /**< If 1, ignore unknown devices (non whitelisted). */
vcoubard 598:814c1ce92947 219 .p_whitelist = &whitelist, /**< Pointer to whitelist, NULL if none is given. */
vcoubard 598:814c1ce92947 220 .interval = scanningParams.getInterval(), /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 598:814c1ce92947 221 .window = scanningParams.getWindow(), /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 598:814c1ce92947 222 .timeout = scanningParams.getTimeout(), /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
vcoubard 598:814c1ce92947 223 };
vcoubard 598:814c1ce92947 224
vcoubard 598:814c1ce92947 225 if (sd_ble_gap_scan_start(&scanParams) != NRF_SUCCESS) {
vcoubard 598:814c1ce92947 226 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 598:814c1ce92947 227 }
vcoubard 598:814c1ce92947 228
vcoubard 598:814c1ce92947 229 return BLE_ERROR_NONE;
vcoubard 598:814c1ce92947 230 }
vcoubard 598:814c1ce92947 231
vcoubard 598:814c1ce92947 232 ble_error_t nRF5xGap::stopScan(void) {
vcoubard 598:814c1ce92947 233 if (sd_ble_gap_scan_stop() == NRF_SUCCESS) {
vcoubard 598:814c1ce92947 234 return BLE_ERROR_NONE;
vcoubard 598:814c1ce92947 235 }
vcoubard 598:814c1ce92947 236
vcoubard 598:814c1ce92947 237 return BLE_STACK_BUSY;
vcoubard 598:814c1ce92947 238 }
vcoubard 598:814c1ce92947 239 #endif
vcoubard 598:814c1ce92947 240
vcoubard 541:884f95bf5351 241 /**************************************************************************/
vcoubard 541:884f95bf5351 242 /*!
vcoubard 541:884f95bf5351 243 @brief Stops the BLE HW and disconnects from any devices
vcoubard 541:884f95bf5351 244
vcoubard 541:884f95bf5351 245 @returns ble_error_t
vcoubard 541:884f95bf5351 246
vcoubard 541:884f95bf5351 247 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 248 Everything executed properly
vcoubard 541:884f95bf5351 249
vcoubard 541:884f95bf5351 250 @section EXAMPLE
vcoubard 541:884f95bf5351 251
vcoubard 541:884f95bf5351 252 @code
vcoubard 541:884f95bf5351 253
vcoubard 541:884f95bf5351 254 @endcode
vcoubard 541:884f95bf5351 255 */
vcoubard 541:884f95bf5351 256 /**************************************************************************/
vcoubard 541:884f95bf5351 257 ble_error_t nRF5xGap::stopAdvertising(void)
vcoubard 541:884f95bf5351 258 {
vcoubard 541:884f95bf5351 259 /* Stop Advertising */
vcoubard 541:884f95bf5351 260 ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 261
vcoubard 541:884f95bf5351 262 state.advertising = 0;
vcoubard 541:884f95bf5351 263
vcoubard 541:884f95bf5351 264 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 265 }
vcoubard 541:884f95bf5351 266
vcoubard 581:1dd90ec7527b 267 ble_error_t nRF5xGap::connect(const Address_t peerAddr,
vcoubard 581:1dd90ec7527b 268 BLEProtocol::AddressType_t peerAddrType,
vcoubard 581:1dd90ec7527b 269 const ConnectionParams_t *connectionParams,
vcoubard 581:1dd90ec7527b 270 const GapScanningParams *scanParamsIn)
vcoubard 541:884f95bf5351 271 {
vcoubard 541:884f95bf5351 272 ble_gap_addr_t addr;
vcoubard 541:884f95bf5351 273 addr.addr_type = peerAddrType;
vcoubard 541:884f95bf5351 274 memcpy(addr.addr, peerAddr, Gap::ADDR_LEN);
vcoubard 541:884f95bf5351 275
vcoubard 541:884f95bf5351 276 ble_gap_conn_params_t connParams;
vcoubard 541:884f95bf5351 277 if (connectionParams != NULL) {
vcoubard 541:884f95bf5351 278 connParams.min_conn_interval = connectionParams->minConnectionInterval;
vcoubard 541:884f95bf5351 279 connParams.max_conn_interval = connectionParams->maxConnectionInterval;
vcoubard 541:884f95bf5351 280 connParams.slave_latency = connectionParams->slaveLatency;
vcoubard 541:884f95bf5351 281 connParams.conn_sup_timeout = connectionParams->connectionSupervisionTimeout;
vcoubard 541:884f95bf5351 282 } else {
vcoubard 541:884f95bf5351 283 connParams.min_conn_interval = 50;
vcoubard 541:884f95bf5351 284 connParams.max_conn_interval = 100;
vcoubard 541:884f95bf5351 285 connParams.slave_latency = 0;
vcoubard 541:884f95bf5351 286 connParams.conn_sup_timeout = 600;
vcoubard 541:884f95bf5351 287 }
vcoubard 541:884f95bf5351 288
vcoubard 598:814c1ce92947 289 /* Add missing IRKs to whitelist from the bond table held by the SoftDevice */
vcoubard 598:814c1ce92947 290 if (scanningPolicyMode != Gap::SCAN_POLICY_IGNORE_WHITELIST) {
vcoubard 598:814c1ce92947 291 ble_error_t error = generateStackWhitelist();
vcoubard 598:814c1ce92947 292 if (error != BLE_ERROR_NONE) {
vcoubard 598:814c1ce92947 293 return error;
vcoubard 598:814c1ce92947 294 }
vcoubard 598:814c1ce92947 295 } else {
vcoubard 598:814c1ce92947 296 /* Reset the whitelist table to avoid any errors */
vcoubard 598:814c1ce92947 297 whitelist.addr_count = 0;
vcoubard 598:814c1ce92947 298 whitelist.irk_count = 0;
vcoubard 598:814c1ce92947 299 }
vcoubard 598:814c1ce92947 300
vcoubard 541:884f95bf5351 301 ble_gap_scan_params_t scanParams;
vcoubard 596:b66851544182 302 scanParams.selective = scanningPolicyMode; /**< If 1, ignore unknown devices (non whitelisted). */
vcoubard 596:b66851544182 303 scanParams.p_whitelist = &whitelist; /**< Pointer to whitelist, NULL if none is given. */
vcoubard 541:884f95bf5351 304 if (scanParamsIn != NULL) {
vcoubard 541:884f95bf5351 305 scanParams.active = scanParamsIn->getActiveScanning(); /**< If 1, perform active scanning (scan requests). */
vcoubard 541:884f95bf5351 306 scanParams.interval = scanParamsIn->getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 307 scanParams.window = scanParamsIn->getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 308 scanParams.timeout = scanParamsIn->getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
vcoubard 541:884f95bf5351 309 } else {
vcoubard 541:884f95bf5351 310 scanParams.active = _scanningParams.getActiveScanning(); /**< If 1, perform active scanning (scan requests). */
vcoubard 541:884f95bf5351 311 scanParams.interval = _scanningParams.getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 312 scanParams.window = _scanningParams.getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 313 scanParams.timeout = _scanningParams.getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
vcoubard 541:884f95bf5351 314 }
vcoubard 541:884f95bf5351 315
vcoubard 541:884f95bf5351 316 uint32_t rc = sd_ble_gap_connect(&addr, &scanParams, &connParams);
vcoubard 541:884f95bf5351 317 if (rc == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 318 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 319 }
vcoubard 541:884f95bf5351 320 switch (rc) {
vcoubard 541:884f95bf5351 321 case NRF_ERROR_INVALID_ADDR:
vcoubard 541:884f95bf5351 322 return BLE_ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 323 case NRF_ERROR_INVALID_PARAM:
vcoubard 541:884f95bf5351 324 return BLE_ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 325 case NRF_ERROR_INVALID_STATE:
vcoubard 541:884f95bf5351 326 return BLE_ERROR_INVALID_STATE;
vcoubard 541:884f95bf5351 327 case BLE_ERROR_GAP_INVALID_BLE_ADDR:
vcoubard 541:884f95bf5351 328 return BLE_ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 329 case NRF_ERROR_NO_MEM:
vcoubard 541:884f95bf5351 330 return BLE_ERROR_NO_MEM;
vcoubard 541:884f95bf5351 331 case NRF_ERROR_BUSY:
vcoubard 541:884f95bf5351 332 return BLE_STACK_BUSY;
vcoubard 541:884f95bf5351 333 default:
vcoubard 541:884f95bf5351 334 case BLE_ERROR_GAP_WHITELIST_IN_USE:
vcoubard 541:884f95bf5351 335 return BLE_ERROR_UNSPECIFIED;
vcoubard 541:884f95bf5351 336 }
vcoubard 541:884f95bf5351 337 }
vcoubard 541:884f95bf5351 338
vcoubard 541:884f95bf5351 339 ble_error_t nRF5xGap::disconnect(Handle_t connectionHandle, DisconnectionReason_t reason)
vcoubard 541:884f95bf5351 340 {
vcoubard 541:884f95bf5351 341 state.advertising = 0;
vcoubard 541:884f95bf5351 342 state.connected = 0;
vcoubard 541:884f95bf5351 343
vcoubard 541:884f95bf5351 344 uint8_t code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION;
vcoubard 541:884f95bf5351 345 switch (reason) {
vcoubard 541:884f95bf5351 346 case REMOTE_USER_TERMINATED_CONNECTION:
vcoubard 541:884f95bf5351 347 code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION;
vcoubard 541:884f95bf5351 348 break;
vcoubard 541:884f95bf5351 349 case CONN_INTERVAL_UNACCEPTABLE:
vcoubard 541:884f95bf5351 350 code = BLE_HCI_CONN_INTERVAL_UNACCEPTABLE;
vcoubard 541:884f95bf5351 351 break;
vcoubard 541:884f95bf5351 352 default:
vcoubard 541:884f95bf5351 353 break;
vcoubard 541:884f95bf5351 354 }
vcoubard 541:884f95bf5351 355
vcoubard 541:884f95bf5351 356 /* Disconnect if we are connected to a central device */
vcoubard 541:884f95bf5351 357 ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(connectionHandle, code), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 358
vcoubard 541:884f95bf5351 359 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 360 }
vcoubard 541:884f95bf5351 361
vcoubard 541:884f95bf5351 362 /*!
vcoubard 541:884f95bf5351 363 @brief Disconnects if we are connected to a central device
vcoubard 541:884f95bf5351 364
vcoubard 541:884f95bf5351 365 @returns ble_error_t
vcoubard 541:884f95bf5351 366
vcoubard 541:884f95bf5351 367 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 368 Everything executed properly
vcoubard 541:884f95bf5351 369 */
vcoubard 541:884f95bf5351 370 ble_error_t nRF5xGap::disconnect(DisconnectionReason_t reason)
vcoubard 541:884f95bf5351 371 {
vcoubard 541:884f95bf5351 372 return disconnect(m_connectionHandle, reason);
vcoubard 541:884f95bf5351 373 }
vcoubard 541:884f95bf5351 374
vcoubard 541:884f95bf5351 375 ble_error_t nRF5xGap::getPreferredConnectionParams(ConnectionParams_t *params)
vcoubard 541:884f95bf5351 376 {
vcoubard 541:884f95bf5351 377 ASSERT_INT(NRF_SUCCESS,
vcoubard 541:884f95bf5351 378 sd_ble_gap_ppcp_get(reinterpret_cast<ble_gap_conn_params_t *>(params)),
vcoubard 541:884f95bf5351 379 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 380
vcoubard 541:884f95bf5351 381 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 382 }
vcoubard 541:884f95bf5351 383
vcoubard 541:884f95bf5351 384 ble_error_t nRF5xGap::setPreferredConnectionParams(const ConnectionParams_t *params)
vcoubard 541:884f95bf5351 385 {
vcoubard 541:884f95bf5351 386 ASSERT_INT(NRF_SUCCESS,
vcoubard 541:884f95bf5351 387 sd_ble_gap_ppcp_set(reinterpret_cast<const ble_gap_conn_params_t *>(params)),
vcoubard 541:884f95bf5351 388 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 389
vcoubard 541:884f95bf5351 390 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 391 }
vcoubard 541:884f95bf5351 392
vcoubard 541:884f95bf5351 393 ble_error_t nRF5xGap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *newParams)
vcoubard 541:884f95bf5351 394 {
vcoubard 541:884f95bf5351 395 uint32_t rc;
vcoubard 541:884f95bf5351 396
vcoubard 541:884f95bf5351 397 rc = sd_ble_gap_conn_param_update(handle, reinterpret_cast<ble_gap_conn_params_t *>(const_cast<ConnectionParams_t*>(newParams)));
vcoubard 541:884f95bf5351 398 if (rc == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 399 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 400 } else {
vcoubard 541:884f95bf5351 401 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 402 }
vcoubard 541:884f95bf5351 403 }
vcoubard 541:884f95bf5351 404
vcoubard 541:884f95bf5351 405 /**************************************************************************/
vcoubard 541:884f95bf5351 406 /*!
vcoubard 575:7023a8204a1b 407 @brief Clear nRF5xGap's state.
vcoubard 575:7023a8204a1b 408
vcoubard 575:7023a8204a1b 409 @returns ble_error_t
vcoubard 575:7023a8204a1b 410
vcoubard 575:7023a8204a1b 411 @retval BLE_ERROR_NONE
vcoubard 575:7023a8204a1b 412 Everything executed properly
vcoubard 575:7023a8204a1b 413 */
vcoubard 575:7023a8204a1b 414 /**************************************************************************/
vcoubard 575:7023a8204a1b 415 ble_error_t nRF5xGap::reset(void)
vcoubard 575:7023a8204a1b 416 {
vcoubard 575:7023a8204a1b 417 /* Clear all state that is from the parent, including private members */
vcoubard 575:7023a8204a1b 418 if (Gap::reset() != BLE_ERROR_NONE) {
vcoubard 575:7023a8204a1b 419 return BLE_ERROR_INVALID_STATE;
vcoubard 575:7023a8204a1b 420 }
vcoubard 575:7023a8204a1b 421
vcoubard 575:7023a8204a1b 422 /* Clear derived class members */
vcoubard 575:7023a8204a1b 423 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
vcoubard 575:7023a8204a1b 424
vcoubard 575:7023a8204a1b 425 return BLE_ERROR_NONE;
vcoubard 575:7023a8204a1b 426 }
vcoubard 575:7023a8204a1b 427
vcoubard 575:7023a8204a1b 428 /**************************************************************************/
vcoubard 575:7023a8204a1b 429 /*!
vcoubard 541:884f95bf5351 430 @brief Sets the 16-bit connection handle
vcoubard 541:884f95bf5351 431 */
vcoubard 541:884f95bf5351 432 /**************************************************************************/
vcoubard 541:884f95bf5351 433 void nRF5xGap::setConnectionHandle(uint16_t con_handle)
vcoubard 541:884f95bf5351 434 {
vcoubard 541:884f95bf5351 435 m_connectionHandle = con_handle;
vcoubard 541:884f95bf5351 436 }
vcoubard 541:884f95bf5351 437
vcoubard 541:884f95bf5351 438 /**************************************************************************/
vcoubard 541:884f95bf5351 439 /*!
vcoubard 541:884f95bf5351 440 @brief Gets the 16-bit connection handle
vcoubard 541:884f95bf5351 441 */
vcoubard 541:884f95bf5351 442 /**************************************************************************/
vcoubard 541:884f95bf5351 443 uint16_t nRF5xGap::getConnectionHandle(void)
vcoubard 541:884f95bf5351 444 {
vcoubard 541:884f95bf5351 445 return m_connectionHandle;
vcoubard 541:884f95bf5351 446 }
vcoubard 541:884f95bf5351 447
vcoubard 562:0d32ae12429e 448 /**************************************************************************/
vcoubard 541:884f95bf5351 449 /*!
vcoubard 541:884f95bf5351 450 @brief Sets the BLE device address
vcoubard 541:884f95bf5351 451
vcoubard 541:884f95bf5351 452 @returns ble_error_t
vcoubard 541:884f95bf5351 453
vcoubard 541:884f95bf5351 454 @section EXAMPLE
vcoubard 541:884f95bf5351 455
vcoubard 541:884f95bf5351 456 @code
vcoubard 541:884f95bf5351 457
vcoubard 541:884f95bf5351 458 uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 };
vcoubard 577:d38f01a3e701 459 nrf.getGap().setAddress(Gap::BLEProtocol::AddressType::RANDOM_STATIC, device_address);
vcoubard 541:884f95bf5351 460
vcoubard 541:884f95bf5351 461 @endcode
vcoubard 541:884f95bf5351 462 */
vcoubard 541:884f95bf5351 463 /**************************************************************************/
vcoubard 541:884f95bf5351 464 ble_error_t nRF5xGap::setAddress(AddressType_t type, const Address_t address)
vcoubard 541:884f95bf5351 465 {
vcoubard 552:20b282c26f96 466 uint8_t cycle_mode;
vcoubard 552:20b282c26f96 467 ble_gap_addr_t dev_addr;
vcoubard 552:20b282c26f96 468
vcoubard 552:20b282c26f96 469 /* When using Public or Static addresses, the cycle mode must be None.
vcoubard 552:20b282c26f96 470 When using Random Private addresses, the cycle mode must be Auto.
vcoubard 552:20b282c26f96 471 In auto mode, the given address is ignored.
vcoubard 552:20b282c26f96 472 */
vcoubard 577:d38f01a3e701 473 if ((type == BLEProtocol::AddressType::PUBLIC) || (type == BLEProtocol::AddressType::RANDOM_STATIC))
vcoubard 552:20b282c26f96 474 {
vcoubard 552:20b282c26f96 475 cycle_mode = BLE_GAP_ADDR_CYCLE_MODE_NONE;
vcoubard 552:20b282c26f96 476 memcpy(dev_addr.addr, address, ADDR_LEN);
vcoubard 552:20b282c26f96 477 }
vcoubard 577:d38f01a3e701 478 else if ((type == BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE) || (type == BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE))
vcoubard 552:20b282c26f96 479 {
vcoubard 552:20b282c26f96 480 cycle_mode = BLE_GAP_ADDR_CYCLE_MODE_AUTO;
vcoubard 552:20b282c26f96 481 // address is ignored when in auto mode
vcoubard 552:20b282c26f96 482 }
vcoubard 552:20b282c26f96 483 else
vcoubard 552:20b282c26f96 484 {
vcoubard 541:884f95bf5351 485 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 486 }
vcoubard 541:884f95bf5351 487
vcoubard 541:884f95bf5351 488 dev_addr.addr_type = type;
vcoubard 552:20b282c26f96 489 ASSERT_INT(ERROR_NONE, sd_ble_gap_address_set(cycle_mode, &dev_addr), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 490
vcoubard 541:884f95bf5351 491 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 492 }
vcoubard 541:884f95bf5351 493
vcoubard 541:884f95bf5351 494 ble_error_t nRF5xGap::getAddress(AddressType_t *typeP, Address_t address)
vcoubard 541:884f95bf5351 495 {
vcoubard 541:884f95bf5351 496 ble_gap_addr_t dev_addr;
vcoubard 541:884f95bf5351 497 if (sd_ble_gap_address_get(&dev_addr) != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 498 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 499 }
vcoubard 541:884f95bf5351 500
vcoubard 541:884f95bf5351 501 if (typeP != NULL) {
vcoubard 541:884f95bf5351 502 *typeP = static_cast<AddressType_t>(dev_addr.addr_type);
vcoubard 541:884f95bf5351 503 }
vcoubard 541:884f95bf5351 504 if (address != NULL) {
vcoubard 541:884f95bf5351 505 memcpy(address, dev_addr.addr, ADDR_LEN);
vcoubard 541:884f95bf5351 506 }
vcoubard 541:884f95bf5351 507 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 508 }
vcoubard 541:884f95bf5351 509
vcoubard 541:884f95bf5351 510 ble_error_t nRF5xGap::setDeviceName(const uint8_t *deviceName)
vcoubard 541:884f95bf5351 511 {
vcoubard 541:884f95bf5351 512 ble_gap_conn_sec_mode_t sec_mode;
vcoubard 541:884f95bf5351 513 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed
vcoubard 541:884f95bf5351 514
vcoubard 541:884f95bf5351 515 if (sd_ble_gap_device_name_set(&sec_mode, deviceName, strlen((const char *)deviceName)) == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 516 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 517 } else {
vcoubard 541:884f95bf5351 518 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 519 }
vcoubard 541:884f95bf5351 520 }
vcoubard 541:884f95bf5351 521
vcoubard 541:884f95bf5351 522 ble_error_t nRF5xGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP)
vcoubard 541:884f95bf5351 523 {
vcoubard 541:884f95bf5351 524 if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 525 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 526 } else {
vcoubard 541:884f95bf5351 527 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 528 }
vcoubard 541:884f95bf5351 529 }
vcoubard 541:884f95bf5351 530
vcoubard 541:884f95bf5351 531 ble_error_t nRF5xGap::setAppearance(GapAdvertisingData::Appearance appearance)
vcoubard 541:884f95bf5351 532 {
vcoubard 541:884f95bf5351 533 if (sd_ble_gap_appearance_set(appearance) == 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::getAppearance(GapAdvertisingData::Appearance *appearanceP)
vcoubard 541:884f95bf5351 541 {
vcoubard 541:884f95bf5351 542 if ((sd_ble_gap_appearance_get(reinterpret_cast<uint16_t *>(appearanceP)) == 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 /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */
vcoubard 541:884f95bf5351 550 ble_error_t nRF5xGap::setTxPower(int8_t txPower)
vcoubard 541:884f95bf5351 551 {
vcoubard 541:884f95bf5351 552 unsigned rc;
vcoubard 541:884f95bf5351 553 if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 554 switch (rc) {
vcoubard 541:884f95bf5351 555 case NRF_ERROR_BUSY:
vcoubard 541:884f95bf5351 556 return BLE_STACK_BUSY;
vcoubard 541:884f95bf5351 557 case NRF_ERROR_INVALID_PARAM:
vcoubard 541:884f95bf5351 558 default:
vcoubard 541:884f95bf5351 559 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 560 }
vcoubard 541:884f95bf5351 561 }
vcoubard 541:884f95bf5351 562
vcoubard 541:884f95bf5351 563 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 564 }
vcoubard 541:884f95bf5351 565
vcoubard 541:884f95bf5351 566 void nRF5xGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP)
vcoubard 541:884f95bf5351 567 {
vcoubard 541:884f95bf5351 568 static const int8_t permittedTxValues[] = {
vcoubard 541:884f95bf5351 569 -40, -30, -20, -16, -12, -8, -4, 0, 4
vcoubard 541:884f95bf5351 570 };
vcoubard 541:884f95bf5351 571
vcoubard 541:884f95bf5351 572 *valueArrayPP = permittedTxValues;
vcoubard 541:884f95bf5351 573 *countP = sizeof(permittedTxValues) / sizeof(int8_t);
vcoubard 596:b66851544182 574 }
vcoubard 596:b66851544182 575
vcoubard 601:5f4199aae50f 576 /**************************************************************************/
vcoubard 601:5f4199aae50f 577 /*!
vcoubard 601:5f4199aae50f 578 @brief Get the capacity of the internal whitelist maintained by this
vcoubard 601:5f4199aae50f 579 implementation.
vcoubard 601:5f4199aae50f 580
vcoubard 601:5f4199aae50f 581 @returns The capacity of the internal whitelist.
vcoubard 601:5f4199aae50f 582
vcoubard 601:5f4199aae50f 583 @section EXAMPLE
vcoubard 601:5f4199aae50f 584
vcoubard 601:5f4199aae50f 585 @code
vcoubard 601:5f4199aae50f 586
vcoubard 601:5f4199aae50f 587 @endcode
vcoubard 601:5f4199aae50f 588 */
vcoubard 601:5f4199aae50f 589 /**************************************************************************/
vcoubard 598:814c1ce92947 590 uint8_t nRF5xGap::getMaxWhitelistSize(void) const
vcoubard 596:b66851544182 591 {
vcoubard 596:b66851544182 592 return YOTTA_CFG_WHITELIST_MAX_SIZE;
vcoubard 596:b66851544182 593 }
vcoubard 596:b66851544182 594
vcoubard 601:5f4199aae50f 595 /**************************************************************************/
vcoubard 601:5f4199aae50f 596 /*!
vcoubard 601:5f4199aae50f 597 @brief Get a copy of the implementation's internal whitelist.
vcoubard 601:5f4199aae50f 598
vcoubard 601:5f4199aae50f 599 @param[out] whitelistOut
vcoubard 601:5f4199aae50f 600 A \ref Gap::Whitelist_t structure containing a copy of the
vcoubard 601:5f4199aae50f 601 addresses in the implemenetation's internal whitelist.
vcoubard 601:5f4199aae50f 602
vcoubard 601:5f4199aae50f 603 @returns \ref ble_errror_t
vcoubard 601:5f4199aae50f 604
vcoubard 601:5f4199aae50f 605 @retval BLE_ERROR_NONE
vcoubard 601:5f4199aae50f 606 Everything executed properly.
vcoubard 601:5f4199aae50f 607
vcoubard 601:5f4199aae50f 608 @section EXAMPLE
vcoubard 601:5f4199aae50f 609
vcoubard 601:5f4199aae50f 610 @code
vcoubard 601:5f4199aae50f 611
vcoubard 601:5f4199aae50f 612 @endcode
vcoubard 601:5f4199aae50f 613 */
vcoubard 601:5f4199aae50f 614 /**************************************************************************/
vcoubard 598:814c1ce92947 615 ble_error_t nRF5xGap::getWhitelist(Gap::Whitelist_t &whitelistOut) const
vcoubard 596:b66851544182 616 {
vcoubard 598:814c1ce92947 617 uint8_t i;
vcoubard 598:814c1ce92947 618 for (i = 0; i < whitelistAddressesSize && i < whitelistOut.capacity; ++i) {
vcoubard 598:814c1ce92947 619 memcpy(&whitelistOut.addresses[i], &whitelistAddresses[i], sizeof(BLEProtocol::Address_t));
vcoubard 596:b66851544182 620 }
vcoubard 598:814c1ce92947 621 whitelistOut.size = i;
vcoubard 598:814c1ce92947 622
vcoubard 596:b66851544182 623 return BLE_ERROR_NONE;
vcoubard 596:b66851544182 624 }
vcoubard 596:b66851544182 625
vcoubard 601:5f4199aae50f 626 /**************************************************************************/
vcoubard 601:5f4199aae50f 627 /*!
vcoubard 601:5f4199aae50f 628 @brief Set the whitelist that will be used in the next call to
vcoubard 601:5f4199aae50f 629 startAdvertising().
vcoubard 601:5f4199aae50f 630
vcoubard 601:5f4199aae50f 631 @param[in] whitelistIn
vcoubard 601:5f4199aae50f 632 A reference to a \ref Gap::Whitelist_t structure
vcoubard 601:5f4199aae50f 633 representing a whitelist containing all the white listed
vcoubard 601:5f4199aae50f 634 BLE addresses.
vcoubard 601:5f4199aae50f 635
vcoubard 601:5f4199aae50f 636 @returns \ref ble_errror_t
vcoubard 601:5f4199aae50f 637
vcoubard 601:5f4199aae50f 638 @retval BLE_ERROR_NONE
vcoubard 601:5f4199aae50f 639 Everything executed properly.
vcoubard 601:5f4199aae50f 640
vcoubard 601:5f4199aae50f 641 BLE_ERROR_INVALID_PARAM
vcoubard 601:5f4199aae50f 642 The supplied whitelist contains a private non-resolvable
vcoubard 601:5f4199aae50f 643 address
vcoubard 601:5f4199aae50f 644
vcoubard 601:5f4199aae50f 645 BLE_ERROR_PARAM_OUT_OF_RANGE
vcoubard 601:5f4199aae50f 646 The size of the supplied whitelist exceeds the maximum
vcoubard 601:5f4199aae50f 647 capacity of the implementation's internal whitelist.
vcoubard 601:5f4199aae50f 648
vcoubard 601:5f4199aae50f 649 @section EXAMPLE
vcoubard 601:5f4199aae50f 650
vcoubard 601:5f4199aae50f 651 @code
vcoubard 601:5f4199aae50f 652
vcoubard 601:5f4199aae50f 653 @endcode
vcoubard 601:5f4199aae50f 654 */
vcoubard 601:5f4199aae50f 655 /**************************************************************************/
vcoubard 598:814c1ce92947 656 ble_error_t nRF5xGap::setWhitelist(const Gap::Whitelist_t &whitelistIn)
vcoubard 596:b66851544182 657 {
vcoubard 598:814c1ce92947 658 if (whitelistIn.size > getMaxWhitelistSize()) {
vcoubard 598:814c1ce92947 659 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 598:814c1ce92947 660 }
vcoubard 598:814c1ce92947 661
vcoubard 596:b66851544182 662 whitelistAddressesSize = 0;
vcoubard 598:814c1ce92947 663 for (uint8_t i = 0; i < whitelistIn.size; ++i) {
vcoubard 598:814c1ce92947 664 if (whitelistIn.addresses[i].type == BLEProtocol::AddressType_t::RANDOM_PRIVATE_NON_RESOLVABLE) {
vcoubard 598:814c1ce92947 665 /* This is not allowed because it is completely meaningless */
vcoubard 598:814c1ce92947 666 return BLE_ERROR_INVALID_PARAM;
vcoubard 598:814c1ce92947 667 }
vcoubard 598:814c1ce92947 668 memcpy(&whitelistAddresses[whitelistAddressesSize], &whitelistIn.addresses[i], sizeof(BLEProtocol::Address_t));
vcoubard 596:b66851544182 669 whitelistAddressesSize++;
vcoubard 596:b66851544182 670 }
vcoubard 598:814c1ce92947 671
vcoubard 596:b66851544182 672 return BLE_ERROR_NONE;
vcoubard 596:b66851544182 673 }
vcoubard 596:b66851544182 674
vcoubard 601:5f4199aae50f 675 /**************************************************************************/
vcoubard 601:5f4199aae50f 676 /*!
vcoubard 601:5f4199aae50f 677 @brief Set the advertising policy filter mode that will be used in
vcoubard 601:5f4199aae50f 678 the next call to startAdvertising().
vcoubard 601:5f4199aae50f 679
vcoubard 601:5f4199aae50f 680 @returns \ref ble_errror_t
vcoubard 601:5f4199aae50f 681
vcoubard 601:5f4199aae50f 682 @retval BLE_ERROR_NONE
vcoubard 601:5f4199aae50f 683 Everything executed properly.
vcoubard 601:5f4199aae50f 684
vcoubard 601:5f4199aae50f 685 BLE_ERROR_NOT_IMPLEMENTED
vcoubard 601:5f4199aae50f 686 This feature is currently note implemented.
vcoubard 601:5f4199aae50f 687
vcoubard 601:5f4199aae50f 688 @section EXAMPLE
vcoubard 601:5f4199aae50f 689
vcoubard 601:5f4199aae50f 690 @code
vcoubard 601:5f4199aae50f 691
vcoubard 601:5f4199aae50f 692 @endcode
vcoubard 601:5f4199aae50f 693 */
vcoubard 601:5f4199aae50f 694 /**************************************************************************/
vcoubard 598:814c1ce92947 695 ble_error_t nRF5xGap::setAdvertisingPolicyMode(Gap::AdvertisingPolicyMode_t mode)
vcoubard 596:b66851544182 696 {
vcoubard 596:b66851544182 697 advertisingPolicyMode = mode;
vcoubard 598:814c1ce92947 698
vcoubard 598:814c1ce92947 699 return BLE_ERROR_NONE;
vcoubard 596:b66851544182 700 }
vcoubard 596:b66851544182 701
vcoubard 601:5f4199aae50f 702 /**************************************************************************/
vcoubard 601:5f4199aae50f 703 /*!
vcoubard 601:5f4199aae50f 704 @brief Set the scanning policy filter mode that will be used in
vcoubard 601:5f4199aae50f 705 the next call to startAdvertising().
vcoubard 601:5f4199aae50f 706
vcoubard 601:5f4199aae50f 707 @returns \ref ble_errror_t
vcoubard 601:5f4199aae50f 708
vcoubard 601:5f4199aae50f 709 @retval BLE_ERROR_NONE
vcoubard 601:5f4199aae50f 710 Everything executed properly.
vcoubard 601:5f4199aae50f 711
vcoubard 601:5f4199aae50f 712 BLE_ERROR_NOT_IMPLEMENTED
vcoubard 601:5f4199aae50f 713 This feature is currently note implemented.
vcoubard 601:5f4199aae50f 714
vcoubard 601:5f4199aae50f 715 @section EXAMPLE
vcoubard 601:5f4199aae50f 716
vcoubard 601:5f4199aae50f 717 @code
vcoubard 601:5f4199aae50f 718
vcoubard 601:5f4199aae50f 719 @endcode
vcoubard 601:5f4199aae50f 720 */
vcoubard 601:5f4199aae50f 721 /**************************************************************************/
vcoubard 598:814c1ce92947 722 ble_error_t nRF5xGap::setScanningPolicyMode(Gap::ScanningPolicyMode_t mode)
vcoubard 596:b66851544182 723 {
vcoubard 596:b66851544182 724 scanningPolicyMode = mode;
vcoubard 598:814c1ce92947 725
vcoubard 598:814c1ce92947 726 return BLE_ERROR_NONE;
vcoubard 596:b66851544182 727 }
vcoubard 596:b66851544182 728
vcoubard 601:5f4199aae50f 729 /**************************************************************************/
vcoubard 601:5f4199aae50f 730 /*!
vcoubard 601:5f4199aae50f 731 @brief Set the initiator policy filter mode that will be used in
vcoubard 601:5f4199aae50f 732 the next call to startAdvertising()
vcoubard 601:5f4199aae50f 733
vcoubard 601:5f4199aae50f 734 @returns \ref ble_errror_t
vcoubard 601:5f4199aae50f 735
vcoubard 601:5f4199aae50f 736 @retval BLE_ERROR_NONE
vcoubard 601:5f4199aae50f 737 Everything executed properly.
vcoubard 601:5f4199aae50f 738
vcoubard 601:5f4199aae50f 739 BLE_ERROR_NOT_IMPLEMENTED
vcoubard 601:5f4199aae50f 740 This feature is currently note implemented.
vcoubard 601:5f4199aae50f 741
vcoubard 601:5f4199aae50f 742 @section EXAMPLE
vcoubard 601:5f4199aae50f 743
vcoubard 601:5f4199aae50f 744 @code
vcoubard 601:5f4199aae50f 745
vcoubard 601:5f4199aae50f 746 @endcode
vcoubard 601:5f4199aae50f 747 */
vcoubard 601:5f4199aae50f 748 /**************************************************************************/
vcoubard 598:814c1ce92947 749 ble_error_t nRF5xGap::setInitiatorPolicyMode(Gap::InitiatorPolicyMode_t mode)
vcoubard 596:b66851544182 750 {
vcoubard 598:814c1ce92947 751 return BLE_ERROR_NOT_IMPLEMENTED;
vcoubard 596:b66851544182 752 }
vcoubard 596:b66851544182 753
vcoubard 601:5f4199aae50f 754 /**************************************************************************/
vcoubard 601:5f4199aae50f 755 /*!
vcoubard 601:5f4199aae50f 756 @brief Get the current advertising policy filter mode.
vcoubard 601:5f4199aae50f 757
vcoubard 601:5f4199aae50f 758 @returns The advertising policy filter mode.
vcoubard 601:5f4199aae50f 759
vcoubard 601:5f4199aae50f 760 @section EXAMPLE
vcoubard 601:5f4199aae50f 761
vcoubard 601:5f4199aae50f 762 @code
vcoubard 601:5f4199aae50f 763
vcoubard 601:5f4199aae50f 764 @endcode
vcoubard 601:5f4199aae50f 765 */
vcoubard 601:5f4199aae50f 766 /**************************************************************************/
vcoubard 596:b66851544182 767 Gap::AdvertisingPolicyMode_t nRF5xGap::getAdvertisingPolicyMode(void) const
vcoubard 596:b66851544182 768 {
vcoubard 596:b66851544182 769 return advertisingPolicyMode;
vcoubard 596:b66851544182 770 }
vcoubard 596:b66851544182 771
vcoubard 601:5f4199aae50f 772 /**************************************************************************/
vcoubard 601:5f4199aae50f 773 /*!
vcoubard 601:5f4199aae50f 774 @brief Get the current scanning policy filter mode.
vcoubard 601:5f4199aae50f 775
vcoubard 601:5f4199aae50f 776 @returns The scanning policy filter mode.
vcoubard 601:5f4199aae50f 777
vcoubard 601:5f4199aae50f 778 @section EXAMPLE
vcoubard 601:5f4199aae50f 779
vcoubard 601:5f4199aae50f 780 @code
vcoubard 601:5f4199aae50f 781
vcoubard 601:5f4199aae50f 782 @endcode
vcoubard 601:5f4199aae50f 783 */
vcoubard 601:5f4199aae50f 784 /**************************************************************************/
vcoubard 596:b66851544182 785 Gap::ScanningPolicyMode_t nRF5xGap::getScanningPolicyMode(void) const
vcoubard 596:b66851544182 786 {
vcoubard 596:b66851544182 787 return scanningPolicyMode;
vcoubard 596:b66851544182 788 }
vcoubard 596:b66851544182 789
vcoubard 601:5f4199aae50f 790 /**************************************************************************/
vcoubard 601:5f4199aae50f 791 /*!
vcoubard 601:5f4199aae50f 792 @brief Get the current initiator policy filter mode.
vcoubard 601:5f4199aae50f 793
vcoubard 601:5f4199aae50f 794 @returns The initiator policy filter mode.
vcoubard 601:5f4199aae50f 795
vcoubard 601:5f4199aae50f 796 @note Currently initiator filtering using the whitelist is not
vcoubard 601:5f4199aae50f 797 implemented in this module.
vcoubard 601:5f4199aae50f 798
vcoubard 601:5f4199aae50f 799 @section EXAMPLE
vcoubard 601:5f4199aae50f 800
vcoubard 601:5f4199aae50f 801 @code
vcoubard 601:5f4199aae50f 802
vcoubard 601:5f4199aae50f 803 @endcode
vcoubard 601:5f4199aae50f 804 */
vcoubard 601:5f4199aae50f 805 /**************************************************************************/
vcoubard 596:b66851544182 806 Gap::InitiatorPolicyMode_t nRF5xGap::getInitiatorPolicyMode(void) const
vcoubard 596:b66851544182 807 {
vcoubard 598:814c1ce92947 808 return Gap::INIT_POLICY_IGNORE_WHITELIST;
vcoubard 596:b66851544182 809 }
vcoubard 598:814c1ce92947 810
vcoubard 600:0978b5626451 811 /**************************************************************************/
vcoubard 600:0978b5626451 812 /*!
vcoubard 600:0978b5626451 813 @brief Helper function used to populate the ble_gap_whitelist_t that
vcoubard 600:0978b5626451 814 will be used by the SoftDevice for filtering requests.
vcoubard 600:0978b5626451 815
vcoubard 600:0978b5626451 816 @returns \ref ble_error_t
vcoubard 600:0978b5626451 817
vcoubard 600:0978b5626451 818 @retval BLE_ERROR_NONE
vcoubard 600:0978b5626451 819 Everything executed properly
vcoubard 600:0978b5626451 820
vcoubard 601:5f4199aae50f 821 @retval BLE_ERROR_INVALID_STATE
vcoubard 601:5f4199aae50f 822 The internal stack was not initialized correctly.
vcoubard 600:0978b5626451 823
vcoubard 601:5f4199aae50f 824 @note Both the SecurityManager and Gap must initialize correctly for
vcoubard 601:5f4199aae50f 825 this function to succeed.
vcoubard 600:0978b5626451 826
vcoubard 600:0978b5626451 827 @note This function is needed because for the BLE API the whitelist
vcoubard 600:0978b5626451 828 is just a collection of keys, but for the stack it also includes
vcoubard 600:0978b5626451 829 the IRK table.
vcoubard 600:0978b5626451 830
vcoubard 600:0978b5626451 831 @section EXAMPLE
vcoubard 600:0978b5626451 832
vcoubard 600:0978b5626451 833 @code
vcoubard 600:0978b5626451 834
vcoubard 600:0978b5626451 835 @endcode
vcoubard 600:0978b5626451 836 */
vcoubard 600:0978b5626451 837 /**************************************************************************/
vcoubard 598:814c1ce92947 838 ble_error_t nRF5xGap::generateStackWhitelist(void)
vcoubard 598:814c1ce92947 839 {
vcoubard 598:814c1ce92947 840 ble_gap_whitelist_t whitelistFromBondTable;
vcoubard 598:814c1ce92947 841 ble_gap_addr_t *addressPtr[1];
vcoubard 598:814c1ce92947 842 ble_gap_irk_t *irkPtr[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
vcoubard 598:814c1ce92947 843
vcoubard 598:814c1ce92947 844 nRF5xSecurityManager& securityManager = (nRF5xSecurityManager&) nRF5xn::Instance(0).getSecurityManager();
vcoubard 598:814c1ce92947 845
vcoubard 598:814c1ce92947 846 if (securityManager.hasInitialized()) {
vcoubard 598:814c1ce92947 847 /* We do not care about the addresses, set the count to 0 */
vcoubard 598:814c1ce92947 848 whitelistFromBondTable.addr_count = 0;
vcoubard 598:814c1ce92947 849 /* The Nordic SDK will return a failure if we set pp_addr to NULL */
vcoubard 598:814c1ce92947 850 whitelistFromBondTable.pp_addrs = addressPtr;
vcoubard 598:814c1ce92947 851 /* We want all the IRKs we can get because we do not know which ones match the addresses */
vcoubard 598:814c1ce92947 852 whitelistFromBondTable.irk_count = YOTTA_CFG_IRK_TABLE_MAX_SIZE;
vcoubard 598:814c1ce92947 853 whitelistFromBondTable.pp_irks = irkPtr;
vcoubard 598:814c1ce92947 854
vcoubard 598:814c1ce92947 855 /* Use the security manager to get the IRKs from the bond table */
vcoubard 598:814c1ce92947 856 ble_error_t error = securityManager.createWhitelistFromBondTable(whitelistFromBondTable);
vcoubard 598:814c1ce92947 857 if (error != BLE_ERROR_NONE) {
vcoubard 598:814c1ce92947 858 return error;
vcoubard 598:814c1ce92947 859 }
vcoubard 598:814c1ce92947 860 } else {
vcoubard 598:814c1ce92947 861 /**
vcoubard 598:814c1ce92947 862 * If there is no security manager then we cannot access the bond table,
vcoubard 598:814c1ce92947 863 * so disable IRK matching
vcoubard 598:814c1ce92947 864 */
vcoubard 598:814c1ce92947 865 whitelistFromBondTable.addr_count = 0;
vcoubard 598:814c1ce92947 866 whitelistFromBondTable.irk_count = 0;
vcoubard 598:814c1ce92947 867 }
vcoubard 598:814c1ce92947 868
vcoubard 598:814c1ce92947 869 /**
vcoubard 598:814c1ce92947 870 * For every private resolvable address in the local whitelist check if
vcoubard 598:814c1ce92947 871 * there is an IRK for said address in the bond table and add it to the
vcoubard 598:814c1ce92947 872 * local IRK list.
vcoubard 598:814c1ce92947 873 */
vcoubard 598:814c1ce92947 874 whitelist.irk_count = 0;
vcoubard 598:814c1ce92947 875 whitelist.addr_count = 0;
vcoubard 598:814c1ce92947 876 for (uint8_t i = 0; i < whitelistAddressesSize; ++i) {
vcoubard 598:814c1ce92947 877 if (whitelistAddresses[i].addr_type == BLEProtocol::AddressType_t::RANDOM_PRIVATE_RESOLVABLE) {
vcoubard 598:814c1ce92947 878 /* Test if there is a matching IRK for this private resolvable address */
vcoubard 598:814c1ce92947 879 for (uint8_t j = 0; j < whitelistFromBondTable.irk_count; ++j) {
vcoubard 598:814c1ce92947 880 if (securityManager.matchAddressAndIrk(&whitelistAddresses[i], whitelistFromBondTable.pp_irks[j])) {
vcoubard 598:814c1ce92947 881 /* Found the corresponding IRK, add it to our local whitelist */
vcoubard 598:814c1ce92947 882 whitelist.pp_irks[whitelist.irk_count] = whitelistFromBondTable.pp_irks[j];
vcoubard 598:814c1ce92947 883 whitelist.irk_count++;
vcoubard 598:814c1ce92947 884 /* Make sure we do not look at this IRK again */
vcoubard 598:814c1ce92947 885 if (j != whitelistFromBondTable.irk_count - 1) {
vcoubard 598:814c1ce92947 886 /**
vcoubard 598:814c1ce92947 887 * This is not the last IRK, so replace the pointer
vcoubard 598:814c1ce92947 888 * with the last pointer in the array
vcoubard 598:814c1ce92947 889 */
vcoubard 598:814c1ce92947 890 whitelistFromBondTable.pp_irks[j] =
vcoubard 598:814c1ce92947 891 whitelistFromBondTable.pp_irks[whitelistFromBondTable.irk_count - 1];
vcoubard 598:814c1ce92947 892 }
vcoubard 598:814c1ce92947 893 /**
vcoubard 598:814c1ce92947 894 * If the IRK is the last pointer in the array simply
vcoubard 598:814c1ce92947 895 * decrement the total IRK count
vcoubard 598:814c1ce92947 896 */
vcoubard 598:814c1ce92947 897 whitelistFromBondTable.irk_count--;
vcoubard 598:814c1ce92947 898 break;
vcoubard 598:814c1ce92947 899 }
vcoubard 598:814c1ce92947 900 }
vcoubard 598:814c1ce92947 901 } else {
vcoubard 598:814c1ce92947 902 /* Include the address into the whitelist */
vcoubard 598:814c1ce92947 903 whitelist.pp_addrs[whitelist.addr_count] = &whitelistAddresses[i];
vcoubard 598:814c1ce92947 904 whitelist.addr_count++;
vcoubard 598:814c1ce92947 905 }
vcoubard 598:814c1ce92947 906 }
vcoubard 598:814c1ce92947 907
vcoubard 598:814c1ce92947 908 return BLE_ERROR_NONE;
vcoubard 598:814c1ce92947 909 }