Nano RGB LED mesh

Dependencies:   nrf51-sdk

Dependents:   BLENano_Mesh

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Wed Dec 02 13:14:18 2015 +0000
Revision:
528:7144407893f0
Parent:
524:502e53732c75
Child:
541:884f95bf5351
Synchronized with git rev 27252473
Author: Marcus Chang
Fixed endianness bug in nRF5xServiceDiscovery::processDiscoverUUIDResponse to be consistent with BLE API.

Who changed what in which revision?

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