Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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