Nordic stack and drivers for the mbed BLE API

Fork of nRF51822 by Nordic Semiconductor

Committer:
amithy
Date:
Fri Nov 10 01:00:06 2017 +0000
Revision:
639:5aeed2c29513
Parent:
638:c90ae1400bf2
for testing export

Who changed what in which revision?

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