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