Nordic stack and drivers for the mbed BLE API. Version to work around build bug.

Dependents:   microbit_rubber_ducky microbit_mouse_BLE microbit_mouse_BLE_daybreak_version microbit_presenter

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 10:19:10 2016 +0000
Revision:
552:20b282c26f96
Parent:
541:884f95bf5351
Child:
561:613dbbdeed27
Synchronized with git rev f4f35c8a
Author: Rohit Grover
Release 2.1.1
=============

A minor release to separate the concept of minlen and len in
GattCharacteristic.

Who changed what in which revision?

UserRevisionLine numberNew 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 541:884f95bf5351 17 #include "nRF5xGap.h"
vcoubard 541:884f95bf5351 18 #include "mbed.h"
vcoubard 541:884f95bf5351 19
vcoubard 541:884f95bf5351 20 #include "common/common.h"
vcoubard 541:884f95bf5351 21 #include "ble_advdata.h"
vcoubard 541:884f95bf5351 22 #include "ble_hci.h"
vcoubard 541:884f95bf5351 23
vcoubard 541:884f95bf5351 24 nRF5xGap &nRF5xGap::getInstance() {
vcoubard 541:884f95bf5351 25 static nRF5xGap m_instance;
vcoubard 541:884f95bf5351 26 return m_instance;
vcoubard 541:884f95bf5351 27 }
vcoubard 541:884f95bf5351 28
vcoubard 541:884f95bf5351 29 void radioNotificationStaticCallback(bool param) {
vcoubard 541:884f95bf5351 30 nRF5xGap::getInstance().processRadioNotificationEvent(param);
vcoubard 541:884f95bf5351 31 }
vcoubard 541:884f95bf5351 32
vcoubard 541:884f95bf5351 33 /**************************************************************************/
vcoubard 541:884f95bf5351 34 /*!
vcoubard 541:884f95bf5351 35 @brief Sets the advertising parameters and payload for the device
vcoubard 541:884f95bf5351 36
vcoubard 541:884f95bf5351 37 @param[in] params
vcoubard 541:884f95bf5351 38 Basic advertising details, including the advertising
vcoubard 541:884f95bf5351 39 delay, timeout and how the device should be advertised
vcoubard 541:884f95bf5351 40 @params[in] advData
vcoubard 541:884f95bf5351 41 The primary advertising data payload
vcoubard 541:884f95bf5351 42 @params[in] scanResponse
vcoubard 541:884f95bf5351 43 The optional Scan Response payload if the advertising
vcoubard 541:884f95bf5351 44 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
vcoubard 541:884f95bf5351 45 in \ref GapAdveritinngParams
vcoubard 541:884f95bf5351 46
vcoubard 541:884f95bf5351 47 @returns \ref ble_error_t
vcoubard 541:884f95bf5351 48
vcoubard 541:884f95bf5351 49 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 50 Everything executed properly
vcoubard 541:884f95bf5351 51
vcoubard 541:884f95bf5351 52 @retval BLE_ERROR_BUFFER_OVERFLOW
vcoubard 541:884f95bf5351 53 The proposed action would cause a buffer overflow. All
vcoubard 541:884f95bf5351 54 advertising payloads must be <= 31 bytes, for example.
vcoubard 541:884f95bf5351 55
vcoubard 541:884f95bf5351 56 @retval BLE_ERROR_NOT_IMPLEMENTED
vcoubard 541:884f95bf5351 57 A feature was requested that is not yet supported in the
vcoubard 541:884f95bf5351 58 nRF51 firmware or hardware.
vcoubard 541:884f95bf5351 59
vcoubard 541:884f95bf5351 60 @retval BLE_ERROR_PARAM_OUT_OF_RANGE
vcoubard 541:884f95bf5351 61 One of the proposed values is outside the valid range.
vcoubard 541:884f95bf5351 62
vcoubard 541:884f95bf5351 63 @section EXAMPLE
vcoubard 541:884f95bf5351 64
vcoubard 541:884f95bf5351 65 @code
vcoubard 541:884f95bf5351 66
vcoubard 541:884f95bf5351 67 @endcode
vcoubard 541:884f95bf5351 68 */
vcoubard 541:884f95bf5351 69 /**************************************************************************/
vcoubard 541:884f95bf5351 70 ble_error_t nRF5xGap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse)
vcoubard 541:884f95bf5351 71 {
vcoubard 541:884f95bf5351 72 /* Make sure we don't exceed the advertising payload length */
vcoubard 541:884f95bf5351 73 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
vcoubard 541:884f95bf5351 74 return BLE_ERROR_BUFFER_OVERFLOW;
vcoubard 541:884f95bf5351 75 }
vcoubard 541:884f95bf5351 76
vcoubard 541:884f95bf5351 77 /* Make sure we have a payload! */
vcoubard 541:884f95bf5351 78 if (advData.getPayloadLen() == 0) {
vcoubard 541:884f95bf5351 79 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 80 }
vcoubard 541:884f95bf5351 81
vcoubard 541:884f95bf5351 82 /* Check the scan response payload limits */
vcoubard 541:884f95bf5351 83 //if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED))
vcoubard 541:884f95bf5351 84 //{
vcoubard 541:884f95bf5351 85 // /* Check if we're within the upper limit */
vcoubard 541:884f95bf5351 86 // if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
vcoubard 541:884f95bf5351 87 // {
vcoubard 541:884f95bf5351 88 // return BLE_ERROR_BUFFER_OVERFLOW;
vcoubard 541:884f95bf5351 89 // }
vcoubard 541:884f95bf5351 90 // /* Make sure we have a payload! */
vcoubard 541:884f95bf5351 91 // if (advData.getPayloadLen() == 0)
vcoubard 541:884f95bf5351 92 // {
vcoubard 541:884f95bf5351 93 // return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 94 // }
vcoubard 541:884f95bf5351 95 //}
vcoubard 541:884f95bf5351 96
vcoubard 541:884f95bf5351 97 /* Send advertising data! */
vcoubard 541:884f95bf5351 98 ASSERT(ERROR_NONE ==
vcoubard 541:884f95bf5351 99 sd_ble_gap_adv_data_set(advData.getPayload(),
vcoubard 541:884f95bf5351 100 advData.getPayloadLen(),
vcoubard 541:884f95bf5351 101 scanResponse.getPayload(),
vcoubard 541:884f95bf5351 102 scanResponse.getPayloadLen()),
vcoubard 541:884f95bf5351 103 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 104
vcoubard 541:884f95bf5351 105 /* Make sure the GAP Service appearance value is aligned with the
vcoubard 541:884f95bf5351 106 *appearance from GapAdvertisingData */
vcoubard 541:884f95bf5351 107 ASSERT(ERROR_NONE == sd_ble_gap_appearance_set(advData.getAppearance()),
vcoubard 541:884f95bf5351 108 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 109
vcoubard 541:884f95bf5351 110 /* ToDo: Perform some checks on the payload, for example the Scan Response can't */
vcoubard 541:884f95bf5351 111 /* contains a flags AD type, etc. */
vcoubard 541:884f95bf5351 112
vcoubard 541:884f95bf5351 113 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 114 }
vcoubard 541:884f95bf5351 115
vcoubard 541:884f95bf5351 116 /**************************************************************************/
vcoubard 541:884f95bf5351 117 /*!
vcoubard 541:884f95bf5351 118 @brief Starts the BLE HW, initialising any services that were
vcoubard 541:884f95bf5351 119 added before this function was called.
vcoubard 541:884f95bf5351 120
vcoubard 541:884f95bf5351 121 @note All services must be added before calling this function!
vcoubard 541:884f95bf5351 122
vcoubard 541:884f95bf5351 123 @returns ble_error_t
vcoubard 541:884f95bf5351 124
vcoubard 541:884f95bf5351 125 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 126 Everything executed properly
vcoubard 541:884f95bf5351 127
vcoubard 541:884f95bf5351 128 @section EXAMPLE
vcoubard 541:884f95bf5351 129
vcoubard 541:884f95bf5351 130 @code
vcoubard 541:884f95bf5351 131
vcoubard 541:884f95bf5351 132 @endcode
vcoubard 541:884f95bf5351 133 */
vcoubard 541:884f95bf5351 134 /**************************************************************************/
vcoubard 541:884f95bf5351 135 ble_error_t nRF5xGap::startAdvertising(const GapAdvertisingParams &params)
vcoubard 541:884f95bf5351 136 {
vcoubard 541:884f95bf5351 137 /* Make sure we support the advertising type */
vcoubard 541:884f95bf5351 138 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) {
vcoubard 541:884f95bf5351 139 /* ToDo: This requires a propery security implementation, etc. */
vcoubard 541:884f95bf5351 140 return BLE_ERROR_NOT_IMPLEMENTED;
vcoubard 541:884f95bf5351 141 }
vcoubard 541:884f95bf5351 142
vcoubard 541:884f95bf5351 143 /* Check interval range */
vcoubard 541:884f95bf5351 144 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) {
vcoubard 541:884f95bf5351 145 /* Min delay is slightly longer for unconnectable devices */
vcoubard 541:884f95bf5351 146 if ((params.getIntervalInADVUnits() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
vcoubard 541:884f95bf5351 147 (params.getIntervalInADVUnits() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) {
vcoubard 541:884f95bf5351 148 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 149 }
vcoubard 541:884f95bf5351 150 } else {
vcoubard 541:884f95bf5351 151 if ((params.getIntervalInADVUnits() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN) ||
vcoubard 541:884f95bf5351 152 (params.getIntervalInADVUnits() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) {
vcoubard 541:884f95bf5351 153 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 154 }
vcoubard 541:884f95bf5351 155 }
vcoubard 541:884f95bf5351 156
vcoubard 541:884f95bf5351 157 /* Check timeout is zero for Connectable Directed */
vcoubard 541:884f95bf5351 158 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) {
vcoubard 541:884f95bf5351 159 /* Timeout must be 0 with this type, although we'll never get here */
vcoubard 541:884f95bf5351 160 /* since this isn't implemented yet anyway */
vcoubard 541:884f95bf5351 161 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 162 }
vcoubard 541:884f95bf5351 163
vcoubard 541:884f95bf5351 164 /* Check timeout for other advertising types */
vcoubard 541:884f95bf5351 165 if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
vcoubard 541:884f95bf5351 166 (params.getTimeout() > GapAdvertisingParams::GAP_ADV_PARAMS_TIMEOUT_MAX)) {
vcoubard 541:884f95bf5351 167 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 168 }
vcoubard 541:884f95bf5351 169
vcoubard 541:884f95bf5351 170 /* Start Advertising */
vcoubard 541:884f95bf5351 171 ble_gap_adv_params_t adv_para = {0};
vcoubard 541:884f95bf5351 172
vcoubard 541:884f95bf5351 173 adv_para.type = params.getAdvertisingType();
vcoubard 541:884f95bf5351 174 adv_para.p_peer_addr = NULL; // Undirected advertisement
vcoubard 541:884f95bf5351 175 adv_para.fp = BLE_GAP_ADV_FP_ANY;
vcoubard 541:884f95bf5351 176 adv_para.p_whitelist = NULL;
vcoubard 541:884f95bf5351 177 adv_para.interval = params.getIntervalInADVUnits(); // advertising interval (in units of 0.625 ms)
vcoubard 541:884f95bf5351 178 adv_para.timeout = params.getTimeout();
vcoubard 541:884f95bf5351 179
vcoubard 541:884f95bf5351 180 ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 181
vcoubard 541:884f95bf5351 182 state.advertising = 1;
vcoubard 541:884f95bf5351 183
vcoubard 541:884f95bf5351 184 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 185 }
vcoubard 541:884f95bf5351 186
vcoubard 541:884f95bf5351 187 /**************************************************************************/
vcoubard 541:884f95bf5351 188 /*!
vcoubard 541:884f95bf5351 189 @brief Stops the BLE HW and disconnects from any devices
vcoubard 541:884f95bf5351 190
vcoubard 541:884f95bf5351 191 @returns ble_error_t
vcoubard 541:884f95bf5351 192
vcoubard 541:884f95bf5351 193 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 194 Everything executed properly
vcoubard 541:884f95bf5351 195
vcoubard 541:884f95bf5351 196 @section EXAMPLE
vcoubard 541:884f95bf5351 197
vcoubard 541:884f95bf5351 198 @code
vcoubard 541:884f95bf5351 199
vcoubard 541:884f95bf5351 200 @endcode
vcoubard 541:884f95bf5351 201 */
vcoubard 541:884f95bf5351 202 /**************************************************************************/
vcoubard 541:884f95bf5351 203 ble_error_t nRF5xGap::stopAdvertising(void)
vcoubard 541:884f95bf5351 204 {
vcoubard 541:884f95bf5351 205 /* Stop Advertising */
vcoubard 541:884f95bf5351 206 ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 207
vcoubard 541:884f95bf5351 208 state.advertising = 0;
vcoubard 541:884f95bf5351 209
vcoubard 541:884f95bf5351 210 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 211 }
vcoubard 541:884f95bf5351 212
vcoubard 541:884f95bf5351 213 ble_error_t nRF5xGap::connect(const Address_t peerAddr,
vcoubard 541:884f95bf5351 214 Gap::AddressType_t peerAddrType,
vcoubard 541:884f95bf5351 215 const ConnectionParams_t *connectionParams,
vcoubard 541:884f95bf5351 216 const GapScanningParams *scanParamsIn)
vcoubard 541:884f95bf5351 217 {
vcoubard 541:884f95bf5351 218 ble_gap_addr_t addr;
vcoubard 541:884f95bf5351 219 addr.addr_type = peerAddrType;
vcoubard 541:884f95bf5351 220 memcpy(addr.addr, peerAddr, Gap::ADDR_LEN);
vcoubard 541:884f95bf5351 221
vcoubard 541:884f95bf5351 222 ble_gap_conn_params_t connParams;
vcoubard 541:884f95bf5351 223 if (connectionParams != NULL) {
vcoubard 541:884f95bf5351 224 connParams.min_conn_interval = connectionParams->minConnectionInterval;
vcoubard 541:884f95bf5351 225 connParams.max_conn_interval = connectionParams->maxConnectionInterval;
vcoubard 541:884f95bf5351 226 connParams.slave_latency = connectionParams->slaveLatency;
vcoubard 541:884f95bf5351 227 connParams.conn_sup_timeout = connectionParams->connectionSupervisionTimeout;
vcoubard 541:884f95bf5351 228 } else {
vcoubard 541:884f95bf5351 229 connParams.min_conn_interval = 50;
vcoubard 541:884f95bf5351 230 connParams.max_conn_interval = 100;
vcoubard 541:884f95bf5351 231 connParams.slave_latency = 0;
vcoubard 541:884f95bf5351 232 connParams.conn_sup_timeout = 600;
vcoubard 541:884f95bf5351 233 }
vcoubard 541:884f95bf5351 234
vcoubard 541:884f95bf5351 235 ble_gap_scan_params_t scanParams;
vcoubard 541:884f95bf5351 236 scanParams.selective = 0; /**< If 1, ignore unknown devices (non whitelisted). */
vcoubard 541:884f95bf5351 237 scanParams.p_whitelist = NULL; /**< Pointer to whitelist, NULL if none is given. */
vcoubard 541:884f95bf5351 238 if (scanParamsIn != NULL) {
vcoubard 541:884f95bf5351 239 scanParams.active = scanParamsIn->getActiveScanning(); /**< If 1, perform active scanning (scan requests). */
vcoubard 541:884f95bf5351 240 scanParams.interval = scanParamsIn->getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 241 scanParams.window = scanParamsIn->getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 242 scanParams.timeout = scanParamsIn->getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
vcoubard 541:884f95bf5351 243 } else {
vcoubard 541:884f95bf5351 244 scanParams.active = _scanningParams.getActiveScanning(); /**< If 1, perform active scanning (scan requests). */
vcoubard 541:884f95bf5351 245 scanParams.interval = _scanningParams.getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 246 scanParams.window = _scanningParams.getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
vcoubard 541:884f95bf5351 247 scanParams.timeout = _scanningParams.getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
vcoubard 541:884f95bf5351 248 }
vcoubard 541:884f95bf5351 249
vcoubard 541:884f95bf5351 250 uint32_t rc = sd_ble_gap_connect(&addr, &scanParams, &connParams);
vcoubard 541:884f95bf5351 251 if (rc == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 252 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 253 }
vcoubard 541:884f95bf5351 254 switch (rc) {
vcoubard 541:884f95bf5351 255 case NRF_ERROR_INVALID_ADDR:
vcoubard 541:884f95bf5351 256 return BLE_ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 257 case NRF_ERROR_INVALID_PARAM:
vcoubard 541:884f95bf5351 258 return BLE_ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 259 case NRF_ERROR_INVALID_STATE:
vcoubard 541:884f95bf5351 260 return BLE_ERROR_INVALID_STATE;
vcoubard 541:884f95bf5351 261 case BLE_ERROR_GAP_INVALID_BLE_ADDR:
vcoubard 541:884f95bf5351 262 return BLE_ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 263 case NRF_ERROR_NO_MEM:
vcoubard 541:884f95bf5351 264 return BLE_ERROR_NO_MEM;
vcoubard 541:884f95bf5351 265 case NRF_ERROR_BUSY:
vcoubard 541:884f95bf5351 266 return BLE_STACK_BUSY;
vcoubard 541:884f95bf5351 267 default:
vcoubard 541:884f95bf5351 268 case BLE_ERROR_GAP_WHITELIST_IN_USE:
vcoubard 541:884f95bf5351 269 return BLE_ERROR_UNSPECIFIED;
vcoubard 541:884f95bf5351 270 }
vcoubard 541:884f95bf5351 271 }
vcoubard 541:884f95bf5351 272
vcoubard 541:884f95bf5351 273 ble_error_t nRF5xGap::disconnect(Handle_t connectionHandle, DisconnectionReason_t reason)
vcoubard 541:884f95bf5351 274 {
vcoubard 541:884f95bf5351 275 state.advertising = 0;
vcoubard 541:884f95bf5351 276 state.connected = 0;
vcoubard 541:884f95bf5351 277
vcoubard 541:884f95bf5351 278 uint8_t code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION;
vcoubard 541:884f95bf5351 279 switch (reason) {
vcoubard 541:884f95bf5351 280 case REMOTE_USER_TERMINATED_CONNECTION:
vcoubard 541:884f95bf5351 281 code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION;
vcoubard 541:884f95bf5351 282 break;
vcoubard 541:884f95bf5351 283 case CONN_INTERVAL_UNACCEPTABLE:
vcoubard 541:884f95bf5351 284 code = BLE_HCI_CONN_INTERVAL_UNACCEPTABLE;
vcoubard 541:884f95bf5351 285 break;
vcoubard 541:884f95bf5351 286 default:
vcoubard 541:884f95bf5351 287 break;
vcoubard 541:884f95bf5351 288 }
vcoubard 541:884f95bf5351 289
vcoubard 541:884f95bf5351 290 /* Disconnect if we are connected to a central device */
vcoubard 541:884f95bf5351 291 ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(connectionHandle, code), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 292
vcoubard 541:884f95bf5351 293 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 294 }
vcoubard 541:884f95bf5351 295
vcoubard 541:884f95bf5351 296 /*!
vcoubard 541:884f95bf5351 297 @brief Disconnects if we are connected to a central device
vcoubard 541:884f95bf5351 298
vcoubard 541:884f95bf5351 299 @returns ble_error_t
vcoubard 541:884f95bf5351 300
vcoubard 541:884f95bf5351 301 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 302 Everything executed properly
vcoubard 541:884f95bf5351 303 */
vcoubard 541:884f95bf5351 304 ble_error_t nRF5xGap::disconnect(DisconnectionReason_t reason)
vcoubard 541:884f95bf5351 305 {
vcoubard 541:884f95bf5351 306 return disconnect(m_connectionHandle, reason);
vcoubard 541:884f95bf5351 307 }
vcoubard 541:884f95bf5351 308
vcoubard 541:884f95bf5351 309 ble_error_t nRF5xGap::getPreferredConnectionParams(ConnectionParams_t *params)
vcoubard 541:884f95bf5351 310 {
vcoubard 541:884f95bf5351 311 ASSERT_INT(NRF_SUCCESS,
vcoubard 541:884f95bf5351 312 sd_ble_gap_ppcp_get(reinterpret_cast<ble_gap_conn_params_t *>(params)),
vcoubard 541:884f95bf5351 313 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 314
vcoubard 541:884f95bf5351 315 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 316 }
vcoubard 541:884f95bf5351 317
vcoubard 541:884f95bf5351 318 ble_error_t nRF5xGap::setPreferredConnectionParams(const ConnectionParams_t *params)
vcoubard 541:884f95bf5351 319 {
vcoubard 541:884f95bf5351 320 ASSERT_INT(NRF_SUCCESS,
vcoubard 541:884f95bf5351 321 sd_ble_gap_ppcp_set(reinterpret_cast<const ble_gap_conn_params_t *>(params)),
vcoubard 541:884f95bf5351 322 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 323
vcoubard 541:884f95bf5351 324 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 325 }
vcoubard 541:884f95bf5351 326
vcoubard 541:884f95bf5351 327 ble_error_t nRF5xGap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *newParams)
vcoubard 541:884f95bf5351 328 {
vcoubard 541:884f95bf5351 329 uint32_t rc;
vcoubard 541:884f95bf5351 330
vcoubard 541:884f95bf5351 331 rc = sd_ble_gap_conn_param_update(handle, reinterpret_cast<ble_gap_conn_params_t *>(const_cast<ConnectionParams_t*>(newParams)));
vcoubard 541:884f95bf5351 332 if (rc == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 333 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 334 } else {
vcoubard 541:884f95bf5351 335 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 336 }
vcoubard 541:884f95bf5351 337 }
vcoubard 541:884f95bf5351 338
vcoubard 541:884f95bf5351 339 /**************************************************************************/
vcoubard 541:884f95bf5351 340 /*!
vcoubard 541:884f95bf5351 341 @brief Sets the 16-bit connection handle
vcoubard 541:884f95bf5351 342 */
vcoubard 541:884f95bf5351 343 /**************************************************************************/
vcoubard 541:884f95bf5351 344 void nRF5xGap::setConnectionHandle(uint16_t con_handle)
vcoubard 541:884f95bf5351 345 {
vcoubard 541:884f95bf5351 346 m_connectionHandle = con_handle;
vcoubard 541:884f95bf5351 347 }
vcoubard 541:884f95bf5351 348
vcoubard 541:884f95bf5351 349 /**************************************************************************/
vcoubard 541:884f95bf5351 350 /*!
vcoubard 541:884f95bf5351 351 @brief Gets the 16-bit connection handle
vcoubard 541:884f95bf5351 352 */
vcoubard 541:884f95bf5351 353 /**************************************************************************/
vcoubard 541:884f95bf5351 354 uint16_t nRF5xGap::getConnectionHandle(void)
vcoubard 541:884f95bf5351 355 {
vcoubard 541:884f95bf5351 356 return m_connectionHandle;
vcoubard 541:884f95bf5351 357 }
vcoubard 541:884f95bf5351 358
vcoubard 541:884f95bf5351 359 /**************************************************************************/
vcoubard 541:884f95bf5351 360 /*!
vcoubard 541:884f95bf5351 361 @brief Sets the BLE device address
vcoubard 541:884f95bf5351 362
vcoubard 541:884f95bf5351 363 @returns ble_error_t
vcoubard 541:884f95bf5351 364
vcoubard 541:884f95bf5351 365 @section EXAMPLE
vcoubard 541:884f95bf5351 366
vcoubard 541:884f95bf5351 367 @code
vcoubard 541:884f95bf5351 368
vcoubard 541:884f95bf5351 369 uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 };
vcoubard 541:884f95bf5351 370 nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address);
vcoubard 541:884f95bf5351 371
vcoubard 541:884f95bf5351 372 @endcode
vcoubard 541:884f95bf5351 373 */
vcoubard 541:884f95bf5351 374 /**************************************************************************/
vcoubard 541:884f95bf5351 375 ble_error_t nRF5xGap::setAddress(AddressType_t type, const Address_t address)
vcoubard 541:884f95bf5351 376 {
vcoubard 552:20b282c26f96 377 uint8_t cycle_mode;
vcoubard 552:20b282c26f96 378 ble_gap_addr_t dev_addr;
vcoubard 552:20b282c26f96 379
vcoubard 552:20b282c26f96 380 /* When using Public or Static addresses, the cycle mode must be None.
vcoubard 552:20b282c26f96 381 When using Random Private addresses, the cycle mode must be Auto.
vcoubard 552:20b282c26f96 382 In auto mode, the given address is ignored.
vcoubard 552:20b282c26f96 383 */
vcoubard 552:20b282c26f96 384 if ((type == ADDR_TYPE_PUBLIC) || (type == ADDR_TYPE_RANDOM_STATIC))
vcoubard 552:20b282c26f96 385 {
vcoubard 552:20b282c26f96 386 cycle_mode = BLE_GAP_ADDR_CYCLE_MODE_NONE;
vcoubard 552:20b282c26f96 387 memcpy(dev_addr.addr, address, ADDR_LEN);
vcoubard 552:20b282c26f96 388 }
vcoubard 552:20b282c26f96 389 else if ((type == ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE) || (type == ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE))
vcoubard 552:20b282c26f96 390 {
vcoubard 552:20b282c26f96 391 cycle_mode = BLE_GAP_ADDR_CYCLE_MODE_AUTO;
vcoubard 552:20b282c26f96 392 // address is ignored when in auto mode
vcoubard 552:20b282c26f96 393 }
vcoubard 552:20b282c26f96 394 else
vcoubard 552:20b282c26f96 395 {
vcoubard 541:884f95bf5351 396 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 397 }
vcoubard 541:884f95bf5351 398
vcoubard 541:884f95bf5351 399 dev_addr.addr_type = type;
vcoubard 552:20b282c26f96 400 ASSERT_INT(ERROR_NONE, sd_ble_gap_address_set(cycle_mode, &dev_addr), BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 401
vcoubard 541:884f95bf5351 402 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 403 }
vcoubard 541:884f95bf5351 404
vcoubard 541:884f95bf5351 405 ble_error_t nRF5xGap::getAddress(AddressType_t *typeP, Address_t address)
vcoubard 541:884f95bf5351 406 {
vcoubard 541:884f95bf5351 407 ble_gap_addr_t dev_addr;
vcoubard 541:884f95bf5351 408 if (sd_ble_gap_address_get(&dev_addr) != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 409 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 410 }
vcoubard 541:884f95bf5351 411
vcoubard 541:884f95bf5351 412 if (typeP != NULL) {
vcoubard 541:884f95bf5351 413 *typeP = static_cast<AddressType_t>(dev_addr.addr_type);
vcoubard 541:884f95bf5351 414 }
vcoubard 541:884f95bf5351 415 if (address != NULL) {
vcoubard 541:884f95bf5351 416 memcpy(address, dev_addr.addr, ADDR_LEN);
vcoubard 541:884f95bf5351 417 }
vcoubard 541:884f95bf5351 418 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 419 }
vcoubard 541:884f95bf5351 420
vcoubard 541:884f95bf5351 421 ble_error_t nRF5xGap::setDeviceName(const uint8_t *deviceName)
vcoubard 541:884f95bf5351 422 {
vcoubard 541:884f95bf5351 423 ble_gap_conn_sec_mode_t sec_mode;
vcoubard 541:884f95bf5351 424 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed
vcoubard 541:884f95bf5351 425
vcoubard 541:884f95bf5351 426 if (sd_ble_gap_device_name_set(&sec_mode, deviceName, strlen((const char *)deviceName)) == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 427 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 428 } else {
vcoubard 541:884f95bf5351 429 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 430 }
vcoubard 541:884f95bf5351 431 }
vcoubard 541:884f95bf5351 432
vcoubard 541:884f95bf5351 433 ble_error_t nRF5xGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP)
vcoubard 541:884f95bf5351 434 {
vcoubard 541:884f95bf5351 435 if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 436 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 437 } else {
vcoubard 541:884f95bf5351 438 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 439 }
vcoubard 541:884f95bf5351 440 }
vcoubard 541:884f95bf5351 441
vcoubard 541:884f95bf5351 442 ble_error_t nRF5xGap::setAppearance(GapAdvertisingData::Appearance appearance)
vcoubard 541:884f95bf5351 443 {
vcoubard 541:884f95bf5351 444 if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) {
vcoubard 541:884f95bf5351 445 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 446 } else {
vcoubard 541:884f95bf5351 447 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 448 }
vcoubard 541:884f95bf5351 449 }
vcoubard 541:884f95bf5351 450
vcoubard 541:884f95bf5351 451 ble_error_t nRF5xGap::getAppearance(GapAdvertisingData::Appearance *appearanceP)
vcoubard 541:884f95bf5351 452 {
vcoubard 541:884f95bf5351 453 if ((sd_ble_gap_appearance_get(reinterpret_cast<uint16_t *>(appearanceP)) == NRF_SUCCESS)) {
vcoubard 541:884f95bf5351 454 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 455 } else {
vcoubard 541:884f95bf5351 456 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 457 }
vcoubard 541:884f95bf5351 458 }
vcoubard 541:884f95bf5351 459
vcoubard 541:884f95bf5351 460 /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */
vcoubard 541:884f95bf5351 461 ble_error_t nRF5xGap::setTxPower(int8_t txPower)
vcoubard 541:884f95bf5351 462 {
vcoubard 541:884f95bf5351 463 unsigned rc;
vcoubard 541:884f95bf5351 464 if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) {
vcoubard 541:884f95bf5351 465 switch (rc) {
vcoubard 541:884f95bf5351 466 case NRF_ERROR_BUSY:
vcoubard 541:884f95bf5351 467 return BLE_STACK_BUSY;
vcoubard 541:884f95bf5351 468 case NRF_ERROR_INVALID_PARAM:
vcoubard 541:884f95bf5351 469 default:
vcoubard 541:884f95bf5351 470 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 541:884f95bf5351 471 }
vcoubard 541:884f95bf5351 472 }
vcoubard 541:884f95bf5351 473
vcoubard 541:884f95bf5351 474 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 475 }
vcoubard 541:884f95bf5351 476
vcoubard 541:884f95bf5351 477 void nRF5xGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP)
vcoubard 541:884f95bf5351 478 {
vcoubard 541:884f95bf5351 479 static const int8_t permittedTxValues[] = {
vcoubard 541:884f95bf5351 480 -40, -30, -20, -16, -12, -8, -4, 0, 4
vcoubard 541:884f95bf5351 481 };
vcoubard 541:884f95bf5351 482
vcoubard 541:884f95bf5351 483 *valueArrayPP = permittedTxValues;
vcoubard 541:884f95bf5351 484 *countP = sizeof(permittedTxValues) / sizeof(int8_t);
rgrover1 388:db85a09c27ef 485 }