No changes

Fork of nRF51822 by Nordic Semiconductor

Committer:
galism
Date:
Sun Feb 26 03:15:43 2017 +0000
Revision:
639:abe58b2a2b5d
Parent:
615:1751e2e2637a
Child:
616:69ea3a4b7243
No changes

Who changed what in which revision?

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