Nordic stack and drivers for the mbed BLE API

Fork of nRF51822 by Nordic Semiconductor

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
638:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

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