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