Nordic stack and drivers for the mbed BLE API

Fork of nRF51822 by Nordic Semiconductor

Committer:
cordonn2
Date:
Tue Mar 28 19:09:24 2017 +0000
Revision:
639:823cb91436a8
Parent:
638:c90ae1400bf2
Added ability to filter advertising channel programmatically

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