fdsf
Fork of nRF51822 by
source/nRF5xGap.cpp@578:78f69f1be114, 2016-01-11 (annotated)
- Committer:
- vcoubard
- Date:
- Mon Jan 11 10:19:25 2016 +0000
- Revision:
- 578:78f69f1be114
- Parent:
- 577:d38f01a3e701
- Child:
- 581:1dd90ec7527b
Synchronized with git rev 90de915f
Author: Andres Amaya Garcia
Replace deprecated inclusions of mbed.h
Replace all deprecated inclusions of mbed.h for mbed-drivers/mbed.h when using
mbed OS. Applications should compile for mbed classic.
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 | 541:884f95bf5351 | 171 | /* Start Advertising */ |
vcoubard | 541:884f95bf5351 | 172 | ble_gap_adv_params_t adv_para = {0}; |
vcoubard | 541:884f95bf5351 | 173 | |
vcoubard | 541:884f95bf5351 | 174 | adv_para.type = params.getAdvertisingType(); |
vcoubard | 541:884f95bf5351 | 175 | adv_para.p_peer_addr = NULL; // Undirected advertisement |
vcoubard | 541:884f95bf5351 | 176 | adv_para.fp = BLE_GAP_ADV_FP_ANY; |
vcoubard | 541:884f95bf5351 | 177 | adv_para.p_whitelist = NULL; |
vcoubard | 541:884f95bf5351 | 178 | adv_para.interval = params.getIntervalInADVUnits(); // advertising interval (in units of 0.625 ms) |
vcoubard | 541:884f95bf5351 | 179 | adv_para.timeout = params.getTimeout(); |
vcoubard | 541:884f95bf5351 | 180 | |
vcoubard | 541:884f95bf5351 | 181 | ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE); |
vcoubard | 541:884f95bf5351 | 182 | |
vcoubard | 541:884f95bf5351 | 183 | state.advertising = 1; |
vcoubard | 541:884f95bf5351 | 184 | |
vcoubard | 541:884f95bf5351 | 185 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 186 | } |
vcoubard | 541:884f95bf5351 | 187 | |
vcoubard | 541:884f95bf5351 | 188 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 189 | /*! |
vcoubard | 541:884f95bf5351 | 190 | @brief Stops the BLE HW and disconnects from any devices |
vcoubard | 541:884f95bf5351 | 191 | |
vcoubard | 541:884f95bf5351 | 192 | @returns ble_error_t |
vcoubard | 541:884f95bf5351 | 193 | |
vcoubard | 541:884f95bf5351 | 194 | @retval BLE_ERROR_NONE |
vcoubard | 541:884f95bf5351 | 195 | Everything executed properly |
vcoubard | 541:884f95bf5351 | 196 | |
vcoubard | 541:884f95bf5351 | 197 | @section EXAMPLE |
vcoubard | 541:884f95bf5351 | 198 | |
vcoubard | 541:884f95bf5351 | 199 | @code |
vcoubard | 541:884f95bf5351 | 200 | |
vcoubard | 541:884f95bf5351 | 201 | @endcode |
vcoubard | 541:884f95bf5351 | 202 | */ |
vcoubard | 541:884f95bf5351 | 203 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 204 | ble_error_t nRF5xGap::stopAdvertising(void) |
vcoubard | 541:884f95bf5351 | 205 | { |
vcoubard | 541:884f95bf5351 | 206 | /* Stop Advertising */ |
vcoubard | 541:884f95bf5351 | 207 | ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE); |
vcoubard | 541:884f95bf5351 | 208 | |
vcoubard | 541:884f95bf5351 | 209 | state.advertising = 0; |
vcoubard | 541:884f95bf5351 | 210 | |
vcoubard | 541:884f95bf5351 | 211 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 212 | } |
vcoubard | 541:884f95bf5351 | 213 | |
vcoubard | 541:884f95bf5351 | 214 | ble_error_t nRF5xGap::connect(const Address_t peerAddr, |
vcoubard | 541:884f95bf5351 | 215 | Gap::AddressType_t peerAddrType, |
vcoubard | 541:884f95bf5351 | 216 | const ConnectionParams_t *connectionParams, |
vcoubard | 541:884f95bf5351 | 217 | const GapScanningParams *scanParamsIn) |
vcoubard | 541:884f95bf5351 | 218 | { |
vcoubard | 541:884f95bf5351 | 219 | ble_gap_addr_t addr; |
vcoubard | 541:884f95bf5351 | 220 | addr.addr_type = peerAddrType; |
vcoubard | 541:884f95bf5351 | 221 | memcpy(addr.addr, peerAddr, Gap::ADDR_LEN); |
vcoubard | 541:884f95bf5351 | 222 | |
vcoubard | 541:884f95bf5351 | 223 | ble_gap_conn_params_t connParams; |
vcoubard | 541:884f95bf5351 | 224 | if (connectionParams != NULL) { |
vcoubard | 541:884f95bf5351 | 225 | connParams.min_conn_interval = connectionParams->minConnectionInterval; |
vcoubard | 541:884f95bf5351 | 226 | connParams.max_conn_interval = connectionParams->maxConnectionInterval; |
vcoubard | 541:884f95bf5351 | 227 | connParams.slave_latency = connectionParams->slaveLatency; |
vcoubard | 541:884f95bf5351 | 228 | connParams.conn_sup_timeout = connectionParams->connectionSupervisionTimeout; |
vcoubard | 541:884f95bf5351 | 229 | } else { |
vcoubard | 541:884f95bf5351 | 230 | connParams.min_conn_interval = 50; |
vcoubard | 541:884f95bf5351 | 231 | connParams.max_conn_interval = 100; |
vcoubard | 541:884f95bf5351 | 232 | connParams.slave_latency = 0; |
vcoubard | 541:884f95bf5351 | 233 | connParams.conn_sup_timeout = 600; |
vcoubard | 541:884f95bf5351 | 234 | } |
vcoubard | 541:884f95bf5351 | 235 | |
vcoubard | 541:884f95bf5351 | 236 | ble_gap_scan_params_t scanParams; |
vcoubard | 541:884f95bf5351 | 237 | scanParams.selective = 0; /**< If 1, ignore unknown devices (non whitelisted). */ |
vcoubard | 541:884f95bf5351 | 238 | scanParams.p_whitelist = NULL; /**< Pointer to whitelist, NULL if none is given. */ |
vcoubard | 541:884f95bf5351 | 239 | if (scanParamsIn != NULL) { |
vcoubard | 541:884f95bf5351 | 240 | scanParams.active = scanParamsIn->getActiveScanning(); /**< If 1, perform active scanning (scan requests). */ |
vcoubard | 541:884f95bf5351 | 241 | scanParams.interval = scanParamsIn->getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */ |
vcoubard | 541:884f95bf5351 | 242 | scanParams.window = scanParamsIn->getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */ |
vcoubard | 541:884f95bf5351 | 243 | scanParams.timeout = scanParamsIn->getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */ |
vcoubard | 541:884f95bf5351 | 244 | } else { |
vcoubard | 541:884f95bf5351 | 245 | scanParams.active = _scanningParams.getActiveScanning(); /**< If 1, perform active scanning (scan requests). */ |
vcoubard | 541:884f95bf5351 | 246 | scanParams.interval = _scanningParams.getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */ |
vcoubard | 541:884f95bf5351 | 247 | scanParams.window = _scanningParams.getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */ |
vcoubard | 541:884f95bf5351 | 248 | scanParams.timeout = _scanningParams.getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */ |
vcoubard | 541:884f95bf5351 | 249 | } |
vcoubard | 541:884f95bf5351 | 250 | |
vcoubard | 541:884f95bf5351 | 251 | uint32_t rc = sd_ble_gap_connect(&addr, &scanParams, &connParams); |
vcoubard | 541:884f95bf5351 | 252 | if (rc == NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 253 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 254 | } |
vcoubard | 541:884f95bf5351 | 255 | switch (rc) { |
vcoubard | 541:884f95bf5351 | 256 | case NRF_ERROR_INVALID_ADDR: |
vcoubard | 541:884f95bf5351 | 257 | return BLE_ERROR_INVALID_PARAM; |
vcoubard | 541:884f95bf5351 | 258 | case NRF_ERROR_INVALID_PARAM: |
vcoubard | 541:884f95bf5351 | 259 | return BLE_ERROR_INVALID_PARAM; |
vcoubard | 541:884f95bf5351 | 260 | case NRF_ERROR_INVALID_STATE: |
vcoubard | 541:884f95bf5351 | 261 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 541:884f95bf5351 | 262 | case BLE_ERROR_GAP_INVALID_BLE_ADDR: |
vcoubard | 541:884f95bf5351 | 263 | return BLE_ERROR_INVALID_PARAM; |
vcoubard | 541:884f95bf5351 | 264 | case NRF_ERROR_NO_MEM: |
vcoubard | 541:884f95bf5351 | 265 | return BLE_ERROR_NO_MEM; |
vcoubard | 541:884f95bf5351 | 266 | case NRF_ERROR_BUSY: |
vcoubard | 541:884f95bf5351 | 267 | return BLE_STACK_BUSY; |
vcoubard | 541:884f95bf5351 | 268 | default: |
vcoubard | 541:884f95bf5351 | 269 | case BLE_ERROR_GAP_WHITELIST_IN_USE: |
vcoubard | 541:884f95bf5351 | 270 | return BLE_ERROR_UNSPECIFIED; |
vcoubard | 541:884f95bf5351 | 271 | } |
vcoubard | 541:884f95bf5351 | 272 | } |
vcoubard | 541:884f95bf5351 | 273 | |
vcoubard | 541:884f95bf5351 | 274 | ble_error_t nRF5xGap::disconnect(Handle_t connectionHandle, DisconnectionReason_t reason) |
vcoubard | 541:884f95bf5351 | 275 | { |
vcoubard | 541:884f95bf5351 | 276 | state.advertising = 0; |
vcoubard | 541:884f95bf5351 | 277 | state.connected = 0; |
vcoubard | 541:884f95bf5351 | 278 | |
vcoubard | 541:884f95bf5351 | 279 | uint8_t code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION; |
vcoubard | 541:884f95bf5351 | 280 | switch (reason) { |
vcoubard | 541:884f95bf5351 | 281 | case REMOTE_USER_TERMINATED_CONNECTION: |
vcoubard | 541:884f95bf5351 | 282 | code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION; |
vcoubard | 541:884f95bf5351 | 283 | break; |
vcoubard | 541:884f95bf5351 | 284 | case CONN_INTERVAL_UNACCEPTABLE: |
vcoubard | 541:884f95bf5351 | 285 | code = BLE_HCI_CONN_INTERVAL_UNACCEPTABLE; |
vcoubard | 541:884f95bf5351 | 286 | break; |
vcoubard | 541:884f95bf5351 | 287 | default: |
vcoubard | 541:884f95bf5351 | 288 | break; |
vcoubard | 541:884f95bf5351 | 289 | } |
vcoubard | 541:884f95bf5351 | 290 | |
vcoubard | 541:884f95bf5351 | 291 | /* Disconnect if we are connected to a central device */ |
vcoubard | 541:884f95bf5351 | 292 | ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(connectionHandle, code), BLE_ERROR_PARAM_OUT_OF_RANGE); |
vcoubard | 541:884f95bf5351 | 293 | |
vcoubard | 541:884f95bf5351 | 294 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 295 | } |
vcoubard | 541:884f95bf5351 | 296 | |
vcoubard | 541:884f95bf5351 | 297 | /*! |
vcoubard | 541:884f95bf5351 | 298 | @brief Disconnects if we are connected to a central device |
vcoubard | 541:884f95bf5351 | 299 | |
vcoubard | 541:884f95bf5351 | 300 | @returns ble_error_t |
vcoubard | 541:884f95bf5351 | 301 | |
vcoubard | 541:884f95bf5351 | 302 | @retval BLE_ERROR_NONE |
vcoubard | 541:884f95bf5351 | 303 | Everything executed properly |
vcoubard | 541:884f95bf5351 | 304 | */ |
vcoubard | 541:884f95bf5351 | 305 | ble_error_t nRF5xGap::disconnect(DisconnectionReason_t reason) |
vcoubard | 541:884f95bf5351 | 306 | { |
vcoubard | 541:884f95bf5351 | 307 | return disconnect(m_connectionHandle, reason); |
vcoubard | 541:884f95bf5351 | 308 | } |
vcoubard | 541:884f95bf5351 | 309 | |
vcoubard | 541:884f95bf5351 | 310 | ble_error_t nRF5xGap::getPreferredConnectionParams(ConnectionParams_t *params) |
vcoubard | 541:884f95bf5351 | 311 | { |
vcoubard | 541:884f95bf5351 | 312 | ASSERT_INT(NRF_SUCCESS, |
vcoubard | 541:884f95bf5351 | 313 | sd_ble_gap_ppcp_get(reinterpret_cast<ble_gap_conn_params_t *>(params)), |
vcoubard | 541:884f95bf5351 | 314 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
vcoubard | 541:884f95bf5351 | 315 | |
vcoubard | 541:884f95bf5351 | 316 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 317 | } |
vcoubard | 541:884f95bf5351 | 318 | |
vcoubard | 541:884f95bf5351 | 319 | ble_error_t nRF5xGap::setPreferredConnectionParams(const ConnectionParams_t *params) |
vcoubard | 541:884f95bf5351 | 320 | { |
vcoubard | 541:884f95bf5351 | 321 | ASSERT_INT(NRF_SUCCESS, |
vcoubard | 541:884f95bf5351 | 322 | sd_ble_gap_ppcp_set(reinterpret_cast<const ble_gap_conn_params_t *>(params)), |
vcoubard | 541:884f95bf5351 | 323 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
vcoubard | 541:884f95bf5351 | 324 | |
vcoubard | 541:884f95bf5351 | 325 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 326 | } |
vcoubard | 541:884f95bf5351 | 327 | |
vcoubard | 541:884f95bf5351 | 328 | ble_error_t nRF5xGap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *newParams) |
vcoubard | 541:884f95bf5351 | 329 | { |
vcoubard | 541:884f95bf5351 | 330 | uint32_t rc; |
vcoubard | 541:884f95bf5351 | 331 | |
vcoubard | 541:884f95bf5351 | 332 | rc = sd_ble_gap_conn_param_update(handle, reinterpret_cast<ble_gap_conn_params_t *>(const_cast<ConnectionParams_t*>(newParams))); |
vcoubard | 541:884f95bf5351 | 333 | if (rc == NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 334 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 335 | } else { |
vcoubard | 541:884f95bf5351 | 336 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
vcoubard | 541:884f95bf5351 | 337 | } |
vcoubard | 541:884f95bf5351 | 338 | } |
vcoubard | 541:884f95bf5351 | 339 | |
vcoubard | 541:884f95bf5351 | 340 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 341 | /*! |
vcoubard | 575:7023a8204a1b | 342 | @brief Clear nRF5xGap's state. |
vcoubard | 575:7023a8204a1b | 343 | |
vcoubard | 575:7023a8204a1b | 344 | @returns ble_error_t |
vcoubard | 575:7023a8204a1b | 345 | |
vcoubard | 575:7023a8204a1b | 346 | @retval BLE_ERROR_NONE |
vcoubard | 575:7023a8204a1b | 347 | Everything executed properly |
vcoubard | 575:7023a8204a1b | 348 | */ |
vcoubard | 575:7023a8204a1b | 349 | /**************************************************************************/ |
vcoubard | 575:7023a8204a1b | 350 | ble_error_t nRF5xGap::reset(void) |
vcoubard | 575:7023a8204a1b | 351 | { |
vcoubard | 575:7023a8204a1b | 352 | /* Clear all state that is from the parent, including private members */ |
vcoubard | 575:7023a8204a1b | 353 | if (Gap::reset() != BLE_ERROR_NONE) { |
vcoubard | 575:7023a8204a1b | 354 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 575:7023a8204a1b | 355 | } |
vcoubard | 575:7023a8204a1b | 356 | |
vcoubard | 575:7023a8204a1b | 357 | /* Clear derived class members */ |
vcoubard | 575:7023a8204a1b | 358 | m_connectionHandle = BLE_CONN_HANDLE_INVALID; |
vcoubard | 575:7023a8204a1b | 359 | |
vcoubard | 575:7023a8204a1b | 360 | return BLE_ERROR_NONE; |
vcoubard | 575:7023a8204a1b | 361 | } |
vcoubard | 575:7023a8204a1b | 362 | |
vcoubard | 575:7023a8204a1b | 363 | /**************************************************************************/ |
vcoubard | 575:7023a8204a1b | 364 | /*! |
vcoubard | 541:884f95bf5351 | 365 | @brief Sets the 16-bit connection handle |
vcoubard | 541:884f95bf5351 | 366 | */ |
vcoubard | 541:884f95bf5351 | 367 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 368 | void nRF5xGap::setConnectionHandle(uint16_t con_handle) |
vcoubard | 541:884f95bf5351 | 369 | { |
vcoubard | 541:884f95bf5351 | 370 | m_connectionHandle = con_handle; |
vcoubard | 541:884f95bf5351 | 371 | } |
vcoubard | 541:884f95bf5351 | 372 | |
vcoubard | 541:884f95bf5351 | 373 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 374 | /*! |
vcoubard | 541:884f95bf5351 | 375 | @brief Gets the 16-bit connection handle |
vcoubard | 541:884f95bf5351 | 376 | */ |
vcoubard | 541:884f95bf5351 | 377 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 378 | uint16_t nRF5xGap::getConnectionHandle(void) |
vcoubard | 541:884f95bf5351 | 379 | { |
vcoubard | 541:884f95bf5351 | 380 | return m_connectionHandle; |
vcoubard | 541:884f95bf5351 | 381 | } |
vcoubard | 541:884f95bf5351 | 382 | |
vcoubard | 562:0d32ae12429e | 383 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 384 | /*! |
vcoubard | 541:884f95bf5351 | 385 | @brief Sets the BLE device address |
vcoubard | 541:884f95bf5351 | 386 | |
vcoubard | 541:884f95bf5351 | 387 | @returns ble_error_t |
vcoubard | 541:884f95bf5351 | 388 | |
vcoubard | 541:884f95bf5351 | 389 | @section EXAMPLE |
vcoubard | 541:884f95bf5351 | 390 | |
vcoubard | 541:884f95bf5351 | 391 | @code |
vcoubard | 541:884f95bf5351 | 392 | |
vcoubard | 541:884f95bf5351 | 393 | uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 }; |
vcoubard | 577:d38f01a3e701 | 394 | nrf.getGap().setAddress(Gap::BLEProtocol::AddressType::RANDOM_STATIC, device_address); |
vcoubard | 541:884f95bf5351 | 395 | |
vcoubard | 541:884f95bf5351 | 396 | @endcode |
vcoubard | 541:884f95bf5351 | 397 | */ |
vcoubard | 541:884f95bf5351 | 398 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 399 | ble_error_t nRF5xGap::setAddress(AddressType_t type, const Address_t address) |
vcoubard | 541:884f95bf5351 | 400 | { |
vcoubard | 552:20b282c26f96 | 401 | uint8_t cycle_mode; |
vcoubard | 552:20b282c26f96 | 402 | ble_gap_addr_t dev_addr; |
vcoubard | 552:20b282c26f96 | 403 | |
vcoubard | 552:20b282c26f96 | 404 | /* When using Public or Static addresses, the cycle mode must be None. |
vcoubard | 552:20b282c26f96 | 405 | When using Random Private addresses, the cycle mode must be Auto. |
vcoubard | 552:20b282c26f96 | 406 | In auto mode, the given address is ignored. |
vcoubard | 552:20b282c26f96 | 407 | */ |
vcoubard | 577:d38f01a3e701 | 408 | if ((type == BLEProtocol::AddressType::PUBLIC) || (type == BLEProtocol::AddressType::RANDOM_STATIC)) |
vcoubard | 552:20b282c26f96 | 409 | { |
vcoubard | 552:20b282c26f96 | 410 | cycle_mode = BLE_GAP_ADDR_CYCLE_MODE_NONE; |
vcoubard | 552:20b282c26f96 | 411 | memcpy(dev_addr.addr, address, ADDR_LEN); |
vcoubard | 552:20b282c26f96 | 412 | } |
vcoubard | 577:d38f01a3e701 | 413 | else if ((type == BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE) || (type == BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE)) |
vcoubard | 552:20b282c26f96 | 414 | { |
vcoubard | 552:20b282c26f96 | 415 | cycle_mode = BLE_GAP_ADDR_CYCLE_MODE_AUTO; |
vcoubard | 552:20b282c26f96 | 416 | // address is ignored when in auto mode |
vcoubard | 552:20b282c26f96 | 417 | } |
vcoubard | 552:20b282c26f96 | 418 | else |
vcoubard | 552:20b282c26f96 | 419 | { |
vcoubard | 541:884f95bf5351 | 420 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
vcoubard | 541:884f95bf5351 | 421 | } |
vcoubard | 541:884f95bf5351 | 422 | |
vcoubard | 541:884f95bf5351 | 423 | dev_addr.addr_type = type; |
vcoubard | 552:20b282c26f96 | 424 | ASSERT_INT(ERROR_NONE, sd_ble_gap_address_set(cycle_mode, &dev_addr), BLE_ERROR_PARAM_OUT_OF_RANGE); |
vcoubard | 541:884f95bf5351 | 425 | |
vcoubard | 541:884f95bf5351 | 426 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 427 | } |
vcoubard | 541:884f95bf5351 | 428 | |
vcoubard | 541:884f95bf5351 | 429 | ble_error_t nRF5xGap::getAddress(AddressType_t *typeP, Address_t address) |
vcoubard | 541:884f95bf5351 | 430 | { |
vcoubard | 541:884f95bf5351 | 431 | ble_gap_addr_t dev_addr; |
vcoubard | 541:884f95bf5351 | 432 | if (sd_ble_gap_address_get(&dev_addr) != NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 433 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
vcoubard | 541:884f95bf5351 | 434 | } |
vcoubard | 541:884f95bf5351 | 435 | |
vcoubard | 541:884f95bf5351 | 436 | if (typeP != NULL) { |
vcoubard | 541:884f95bf5351 | 437 | *typeP = static_cast<AddressType_t>(dev_addr.addr_type); |
vcoubard | 541:884f95bf5351 | 438 | } |
vcoubard | 541:884f95bf5351 | 439 | if (address != NULL) { |
vcoubard | 541:884f95bf5351 | 440 | memcpy(address, dev_addr.addr, ADDR_LEN); |
vcoubard | 541:884f95bf5351 | 441 | } |
vcoubard | 541:884f95bf5351 | 442 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 443 | } |
vcoubard | 541:884f95bf5351 | 444 | |
vcoubard | 541:884f95bf5351 | 445 | ble_error_t nRF5xGap::setDeviceName(const uint8_t *deviceName) |
vcoubard | 541:884f95bf5351 | 446 | { |
vcoubard | 541:884f95bf5351 | 447 | ble_gap_conn_sec_mode_t sec_mode; |
vcoubard | 541:884f95bf5351 | 448 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed |
vcoubard | 541:884f95bf5351 | 449 | |
vcoubard | 541:884f95bf5351 | 450 | if (sd_ble_gap_device_name_set(&sec_mode, deviceName, strlen((const char *)deviceName)) == NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 451 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 452 | } else { |
vcoubard | 541:884f95bf5351 | 453 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
vcoubard | 541:884f95bf5351 | 454 | } |
vcoubard | 541:884f95bf5351 | 455 | } |
vcoubard | 541:884f95bf5351 | 456 | |
vcoubard | 541:884f95bf5351 | 457 | ble_error_t nRF5xGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP) |
vcoubard | 541:884f95bf5351 | 458 | { |
vcoubard | 541:884f95bf5351 | 459 | if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 460 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 461 | } else { |
vcoubard | 541:884f95bf5351 | 462 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
vcoubard | 541:884f95bf5351 | 463 | } |
vcoubard | 541:884f95bf5351 | 464 | } |
vcoubard | 541:884f95bf5351 | 465 | |
vcoubard | 541:884f95bf5351 | 466 | ble_error_t nRF5xGap::setAppearance(GapAdvertisingData::Appearance appearance) |
vcoubard | 541:884f95bf5351 | 467 | { |
vcoubard | 541:884f95bf5351 | 468 | if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 469 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 470 | } else { |
vcoubard | 541:884f95bf5351 | 471 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
vcoubard | 541:884f95bf5351 | 472 | } |
vcoubard | 541:884f95bf5351 | 473 | } |
vcoubard | 541:884f95bf5351 | 474 | |
vcoubard | 541:884f95bf5351 | 475 | ble_error_t nRF5xGap::getAppearance(GapAdvertisingData::Appearance *appearanceP) |
vcoubard | 541:884f95bf5351 | 476 | { |
vcoubard | 541:884f95bf5351 | 477 | if ((sd_ble_gap_appearance_get(reinterpret_cast<uint16_t *>(appearanceP)) == NRF_SUCCESS)) { |
vcoubard | 541:884f95bf5351 | 478 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 479 | } else { |
vcoubard | 541:884f95bf5351 | 480 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
vcoubard | 541:884f95bf5351 | 481 | } |
vcoubard | 541:884f95bf5351 | 482 | } |
vcoubard | 541:884f95bf5351 | 483 | |
vcoubard | 541:884f95bf5351 | 484 | /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */ |
vcoubard | 541:884f95bf5351 | 485 | ble_error_t nRF5xGap::setTxPower(int8_t txPower) |
vcoubard | 541:884f95bf5351 | 486 | { |
vcoubard | 541:884f95bf5351 | 487 | unsigned rc; |
vcoubard | 541:884f95bf5351 | 488 | if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 489 | switch (rc) { |
vcoubard | 541:884f95bf5351 | 490 | case NRF_ERROR_BUSY: |
vcoubard | 541:884f95bf5351 | 491 | return BLE_STACK_BUSY; |
vcoubard | 541:884f95bf5351 | 492 | case NRF_ERROR_INVALID_PARAM: |
vcoubard | 541:884f95bf5351 | 493 | default: |
vcoubard | 541:884f95bf5351 | 494 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
vcoubard | 541:884f95bf5351 | 495 | } |
vcoubard | 541:884f95bf5351 | 496 | } |
vcoubard | 541:884f95bf5351 | 497 | |
vcoubard | 541:884f95bf5351 | 498 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 499 | } |
vcoubard | 541:884f95bf5351 | 500 | |
vcoubard | 541:884f95bf5351 | 501 | void nRF5xGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP) |
vcoubard | 541:884f95bf5351 | 502 | { |
vcoubard | 541:884f95bf5351 | 503 | static const int8_t permittedTxValues[] = { |
vcoubard | 541:884f95bf5351 | 504 | -40, -30, -20, -16, -12, -8, -4, 0, 4 |
vcoubard | 541:884f95bf5351 | 505 | }; |
vcoubard | 541:884f95bf5351 | 506 | |
vcoubard | 541:884f95bf5351 | 507 | *valueArrayPP = permittedTxValues; |
vcoubard | 541:884f95bf5351 | 508 | *countP = sizeof(permittedTxValues) / sizeof(int8_t); |
rgrover1 | 388:db85a09c27ef | 509 | } |