Nano RGB LED mesh

Dependencies:   nrf51-sdk

Dependents:   BLENano_Mesh

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 10:19:24 2016 +0000
Revision:
575:7023a8204a1b
Parent:
571:bbf6410b6a89
Child:
577:d38f01a3e701
Synchronized with git rev 252ef3cf
Author: Andres Amaya Garcia
Make _gapInstance non-static in nRF5xn

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