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