fork

Fork of nRF51822 by Nordic Semiconductor

Committer:
elevatorguy
Date:
Thu Aug 13 02:33:38 2015 +0000
Revision:
386:52cd618a90b4
Parent:
381:c997e9717fb7
changes

Who changed what in which revision?

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