test

Dependencies:   nrf51-sdk

Dependents:   microbit-dal

Fork of nRF51822 by Lancaster University

Committer:
rgrover1
Date:
Tue Jul 21 13:23:44 2015 +0100
Revision:
388:db85a09c27ef
Child:
396:e5b0385fc6f1
Synchronized with git rev 0eb58d86
Author: Rohit Grover
rename nRF51... to nRF5x...
This prepares us to support nRF52.

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