test code 123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

Committer:
youkee
Date:
Thu Sep 01 05:14:03 2016 +0000
Revision:
0:1ad0e04b1bc5
change internal time from 1s to 200ms

Who changed what in which revision?

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