test

Dependencies:   nrf51-sdk

Dependents:   microbit-dal

Fork of nRF51822 by Lancaster University

Committer:
LancasterUniversity
Date:
Wed Apr 06 22:38:43 2016 +0100
Revision:
615:65ea2acfc6a2
Parent:
614:cf574a4ebbcc
Child:
616:a8f9b022d8fd
Synchronized with git rev 60a7c0c0
Author: Rohit Grover
bring in the latest changes for BLE::init() where we allow <object, member> tuples for init callback.

Who changed what in which revision?

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