Nano RGB LED mesh

Dependencies:   nrf51-sdk

Dependents:   BLENano_Mesh

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Fri Apr 08 11:05:56 2016 +0100
Revision:
622:5b9728d28fdc
Parent:
616:69ea3a4b7243
Synchronized with git rev 3427ed2d
Author: Vincent Coubard
Merge pull request #113 from ARMmbed/master

Sync develop against master

Who changed what in which revision?

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