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