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