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