CSSE4011_BLE_IMU IMU Seeed Tiny Ble
Dependencies: BLE_API_Tiny_BLE MPU6050-DMP-Seeed-Tiny-BLE mbed
nRF51822/nRF51Gap.cpp@2:44bc61abdf33, 2015-06-10 (annotated)
- Committer:
- flywind
- Date:
- Wed Jun 10 12:56:48 2015 +0000
- Revision:
- 2:44bc61abdf33
- Parent:
- 0:f90c3452d779
milestone working one , yaw pitch roll update in BLE sucessfull
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
flywind | 0:f90c3452d779 | 1 | /* mbed Microcontroller Library |
flywind | 0:f90c3452d779 | 2 | * Copyright (c) 2006-2013 ARM Limited |
flywind | 0:f90c3452d779 | 3 | * |
flywind | 0:f90c3452d779 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
flywind | 0:f90c3452d779 | 5 | * you may not use this file except in compliance with the License. |
flywind | 0:f90c3452d779 | 6 | * You may obtain a copy of the License at |
flywind | 0:f90c3452d779 | 7 | * |
flywind | 0:f90c3452d779 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
flywind | 0:f90c3452d779 | 9 | * |
flywind | 0:f90c3452d779 | 10 | * Unless required by applicable law or agreed to in writing, software |
flywind | 0:f90c3452d779 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
flywind | 0:f90c3452d779 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
flywind | 0:f90c3452d779 | 13 | * See the License for the specific language governing permissions and |
flywind | 0:f90c3452d779 | 14 | * limitations under the License. |
flywind | 0:f90c3452d779 | 15 | */ |
flywind | 0:f90c3452d779 | 16 | |
flywind | 0:f90c3452d779 | 17 | #include "nRF51Gap.h" |
flywind | 0:f90c3452d779 | 18 | #include "mbed.h" |
flywind | 0:f90c3452d779 | 19 | |
flywind | 0:f90c3452d779 | 20 | #include "common/common.h" |
flywind | 0:f90c3452d779 | 21 | #include "ble_advdata.h" |
flywind | 0:f90c3452d779 | 22 | #include "ble_hci.h" |
flywind | 0:f90c3452d779 | 23 | |
flywind | 0:f90c3452d779 | 24 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 25 | /*! |
flywind | 0:f90c3452d779 | 26 | @brief Sets the advertising parameters and payload for the device |
flywind | 0:f90c3452d779 | 27 | |
flywind | 0:f90c3452d779 | 28 | @param[in] params |
flywind | 0:f90c3452d779 | 29 | Basic advertising details, including the advertising |
flywind | 0:f90c3452d779 | 30 | delay, timeout and how the device should be advertised |
flywind | 0:f90c3452d779 | 31 | @params[in] advData |
flywind | 0:f90c3452d779 | 32 | The primary advertising data payload |
flywind | 0:f90c3452d779 | 33 | @params[in] scanResponse |
flywind | 0:f90c3452d779 | 34 | The optional Scan Response payload if the advertising |
flywind | 0:f90c3452d779 | 35 | type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED |
flywind | 0:f90c3452d779 | 36 | in \ref GapAdveritinngParams |
flywind | 0:f90c3452d779 | 37 | |
flywind | 0:f90c3452d779 | 38 | @returns \ref ble_error_t |
flywind | 0:f90c3452d779 | 39 | |
flywind | 0:f90c3452d779 | 40 | @retval BLE_ERROR_NONE |
flywind | 0:f90c3452d779 | 41 | Everything executed properly |
flywind | 0:f90c3452d779 | 42 | |
flywind | 0:f90c3452d779 | 43 | @retval BLE_ERROR_BUFFER_OVERFLOW |
flywind | 0:f90c3452d779 | 44 | The proposed action would cause a buffer overflow. All |
flywind | 0:f90c3452d779 | 45 | advertising payloads must be <= 31 bytes, for example. |
flywind | 0:f90c3452d779 | 46 | |
flywind | 0:f90c3452d779 | 47 | @retval BLE_ERROR_NOT_IMPLEMENTED |
flywind | 0:f90c3452d779 | 48 | A feature was requested that is not yet supported in the |
flywind | 0:f90c3452d779 | 49 | nRF51 firmware or hardware. |
flywind | 0:f90c3452d779 | 50 | |
flywind | 0:f90c3452d779 | 51 | @retval BLE_ERROR_PARAM_OUT_OF_RANGE |
flywind | 0:f90c3452d779 | 52 | One of the proposed values is outside the valid range. |
flywind | 0:f90c3452d779 | 53 | |
flywind | 0:f90c3452d779 | 54 | @section EXAMPLE |
flywind | 0:f90c3452d779 | 55 | |
flywind | 0:f90c3452d779 | 56 | @code |
flywind | 0:f90c3452d779 | 57 | |
flywind | 0:f90c3452d779 | 58 | @endcode |
flywind | 0:f90c3452d779 | 59 | */ |
flywind | 0:f90c3452d779 | 60 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 61 | ble_error_t nRF51Gap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse) |
flywind | 0:f90c3452d779 | 62 | { |
flywind | 0:f90c3452d779 | 63 | /* Make sure we don't exceed the advertising payload length */ |
flywind | 0:f90c3452d779 | 64 | if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) { |
flywind | 0:f90c3452d779 | 65 | return BLE_ERROR_BUFFER_OVERFLOW; |
flywind | 0:f90c3452d779 | 66 | } |
flywind | 0:f90c3452d779 | 67 | |
flywind | 0:f90c3452d779 | 68 | /* Make sure we have a payload! */ |
flywind | 0:f90c3452d779 | 69 | if (advData.getPayloadLen() == 0) { |
flywind | 0:f90c3452d779 | 70 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 71 | } |
flywind | 0:f90c3452d779 | 72 | |
flywind | 0:f90c3452d779 | 73 | /* Check the scan response payload limits */ |
flywind | 0:f90c3452d779 | 74 | //if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED)) |
flywind | 0:f90c3452d779 | 75 | //{ |
flywind | 0:f90c3452d779 | 76 | // /* Check if we're within the upper limit */ |
flywind | 0:f90c3452d779 | 77 | // if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) |
flywind | 0:f90c3452d779 | 78 | // { |
flywind | 0:f90c3452d779 | 79 | // return BLE_ERROR_BUFFER_OVERFLOW; |
flywind | 0:f90c3452d779 | 80 | // } |
flywind | 0:f90c3452d779 | 81 | // /* Make sure we have a payload! */ |
flywind | 0:f90c3452d779 | 82 | // if (advData.getPayloadLen() == 0) |
flywind | 0:f90c3452d779 | 83 | // { |
flywind | 0:f90c3452d779 | 84 | // return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 85 | // } |
flywind | 0:f90c3452d779 | 86 | //} |
flywind | 0:f90c3452d779 | 87 | |
flywind | 0:f90c3452d779 | 88 | /* Send advertising data! */ |
flywind | 0:f90c3452d779 | 89 | ASSERT(ERROR_NONE == |
flywind | 0:f90c3452d779 | 90 | sd_ble_gap_adv_data_set(advData.getPayload(), |
flywind | 0:f90c3452d779 | 91 | advData.getPayloadLen(), |
flywind | 0:f90c3452d779 | 92 | scanResponse.getPayload(), |
flywind | 0:f90c3452d779 | 93 | scanResponse.getPayloadLen()), |
flywind | 0:f90c3452d779 | 94 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
flywind | 0:f90c3452d779 | 95 | |
flywind | 0:f90c3452d779 | 96 | /* Make sure the GAP Service appearance value is aligned with the |
flywind | 0:f90c3452d779 | 97 | *appearance from GapAdvertisingData */ |
flywind | 0:f90c3452d779 | 98 | ASSERT(ERROR_NONE == sd_ble_gap_appearance_set(advData.getAppearance()), |
flywind | 0:f90c3452d779 | 99 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
flywind | 0:f90c3452d779 | 100 | |
flywind | 0:f90c3452d779 | 101 | /* ToDo: Perform some checks on the payload, for example the Scan Response can't */ |
flywind | 0:f90c3452d779 | 102 | /* contains a flags AD type, etc. */ |
flywind | 0:f90c3452d779 | 103 | |
flywind | 0:f90c3452d779 | 104 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 105 | } |
flywind | 0:f90c3452d779 | 106 | |
flywind | 0:f90c3452d779 | 107 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 108 | /*! |
flywind | 0:f90c3452d779 | 109 | @brief Starts the BLE HW, initialising any services that were |
flywind | 0:f90c3452d779 | 110 | added before this function was called. |
flywind | 0:f90c3452d779 | 111 | |
flywind | 0:f90c3452d779 | 112 | @note All services must be added before calling this function! |
flywind | 0:f90c3452d779 | 113 | |
flywind | 0:f90c3452d779 | 114 | @returns ble_error_t |
flywind | 0:f90c3452d779 | 115 | |
flywind | 0:f90c3452d779 | 116 | @retval BLE_ERROR_NONE |
flywind | 0:f90c3452d779 | 117 | Everything executed properly |
flywind | 0:f90c3452d779 | 118 | |
flywind | 0:f90c3452d779 | 119 | @section EXAMPLE |
flywind | 0:f90c3452d779 | 120 | |
flywind | 0:f90c3452d779 | 121 | @code |
flywind | 0:f90c3452d779 | 122 | |
flywind | 0:f90c3452d779 | 123 | @endcode |
flywind | 0:f90c3452d779 | 124 | */ |
flywind | 0:f90c3452d779 | 125 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 126 | ble_error_t nRF51Gap::startAdvertising(const GapAdvertisingParams ¶ms) |
flywind | 0:f90c3452d779 | 127 | { |
flywind | 0:f90c3452d779 | 128 | /* Make sure we support the advertising type */ |
flywind | 0:f90c3452d779 | 129 | if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) { |
flywind | 0:f90c3452d779 | 130 | /* ToDo: This requires a propery security implementation, etc. */ |
flywind | 0:f90c3452d779 | 131 | return BLE_ERROR_NOT_IMPLEMENTED; |
flywind | 0:f90c3452d779 | 132 | } |
flywind | 0:f90c3452d779 | 133 | |
flywind | 0:f90c3452d779 | 134 | /* Check interval range */ |
flywind | 0:f90c3452d779 | 135 | if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) { |
flywind | 0:f90c3452d779 | 136 | /* Min delay is slightly longer for unconnectable devices */ |
flywind | 0:f90c3452d779 | 137 | if ((params.getInterval() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) || |
flywind | 0:f90c3452d779 | 138 | (params.getInterval() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) { |
flywind | 0:f90c3452d779 | 139 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 140 | } |
flywind | 0:f90c3452d779 | 141 | } else { |
flywind | 0:f90c3452d779 | 142 | if ((params.getInterval() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN) || |
flywind | 0:f90c3452d779 | 143 | (params.getInterval() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) { |
flywind | 0:f90c3452d779 | 144 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 145 | } |
flywind | 0:f90c3452d779 | 146 | } |
flywind | 0:f90c3452d779 | 147 | |
flywind | 0:f90c3452d779 | 148 | /* Check timeout is zero for Connectable Directed */ |
flywind | 0:f90c3452d779 | 149 | if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) { |
flywind | 0:f90c3452d779 | 150 | /* Timeout must be 0 with this type, although we'll never get here */ |
flywind | 0:f90c3452d779 | 151 | /* since this isn't implemented yet anyway */ |
flywind | 0:f90c3452d779 | 152 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 153 | } |
flywind | 0:f90c3452d779 | 154 | |
flywind | 0:f90c3452d779 | 155 | /* Check timeout for other advertising types */ |
flywind | 0:f90c3452d779 | 156 | if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && |
flywind | 0:f90c3452d779 | 157 | (params.getTimeout() > GapAdvertisingParams::GAP_ADV_PARAMS_TIMEOUT_MAX)) { |
flywind | 0:f90c3452d779 | 158 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 159 | } |
flywind | 0:f90c3452d779 | 160 | |
flywind | 0:f90c3452d779 | 161 | /* Start Advertising */ |
flywind | 0:f90c3452d779 | 162 | ble_gap_adv_params_t adv_para = {0}; |
flywind | 0:f90c3452d779 | 163 | |
flywind | 0:f90c3452d779 | 164 | adv_para.type = params.getAdvertisingType(); |
flywind | 0:f90c3452d779 | 165 | adv_para.p_peer_addr = NULL; // Undirected advertisement |
flywind | 0:f90c3452d779 | 166 | adv_para.fp = BLE_GAP_ADV_FP_ANY; |
flywind | 0:f90c3452d779 | 167 | adv_para.p_whitelist = NULL; |
flywind | 0:f90c3452d779 | 168 | adv_para.interval = params.getInterval(); // advertising interval (in units of 0.625 ms) |
flywind | 0:f90c3452d779 | 169 | adv_para.timeout = params.getTimeout(); |
flywind | 0:f90c3452d779 | 170 | |
flywind | 0:f90c3452d779 | 171 | ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE); |
flywind | 0:f90c3452d779 | 172 | |
flywind | 0:f90c3452d779 | 173 | state.advertising = 1; |
flywind | 0:f90c3452d779 | 174 | |
flywind | 0:f90c3452d779 | 175 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 176 | } |
flywind | 0:f90c3452d779 | 177 | |
flywind | 0:f90c3452d779 | 178 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 179 | /*! |
flywind | 0:f90c3452d779 | 180 | @brief Stops the BLE HW and disconnects from any devices |
flywind | 0:f90c3452d779 | 181 | |
flywind | 0:f90c3452d779 | 182 | @returns ble_error_t |
flywind | 0:f90c3452d779 | 183 | |
flywind | 0:f90c3452d779 | 184 | @retval BLE_ERROR_NONE |
flywind | 0:f90c3452d779 | 185 | Everything executed properly |
flywind | 0:f90c3452d779 | 186 | |
flywind | 0:f90c3452d779 | 187 | @section EXAMPLE |
flywind | 0:f90c3452d779 | 188 | |
flywind | 0:f90c3452d779 | 189 | @code |
flywind | 0:f90c3452d779 | 190 | |
flywind | 0:f90c3452d779 | 191 | @endcode |
flywind | 0:f90c3452d779 | 192 | */ |
flywind | 0:f90c3452d779 | 193 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 194 | ble_error_t nRF51Gap::stopAdvertising(void) |
flywind | 0:f90c3452d779 | 195 | { |
flywind | 0:f90c3452d779 | 196 | /* Stop Advertising */ |
flywind | 0:f90c3452d779 | 197 | ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE); |
flywind | 0:f90c3452d779 | 198 | |
flywind | 0:f90c3452d779 | 199 | state.advertising = 0; |
flywind | 0:f90c3452d779 | 200 | |
flywind | 0:f90c3452d779 | 201 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 202 | } |
flywind | 0:f90c3452d779 | 203 | |
flywind | 0:f90c3452d779 | 204 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 205 | /*! |
flywind | 0:f90c3452d779 | 206 | @brief Disconnects if we are connected to a central device |
flywind | 0:f90c3452d779 | 207 | |
flywind | 0:f90c3452d779 | 208 | @returns ble_error_t |
flywind | 0:f90c3452d779 | 209 | |
flywind | 0:f90c3452d779 | 210 | @retval BLE_ERROR_NONE |
flywind | 0:f90c3452d779 | 211 | Everything executed properly |
flywind | 0:f90c3452d779 | 212 | |
flywind | 0:f90c3452d779 | 213 | @section EXAMPLE |
flywind | 0:f90c3452d779 | 214 | |
flywind | 0:f90c3452d779 | 215 | @code |
flywind | 0:f90c3452d779 | 216 | |
flywind | 0:f90c3452d779 | 217 | @endcode |
flywind | 0:f90c3452d779 | 218 | */ |
flywind | 0:f90c3452d779 | 219 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 220 | ble_error_t nRF51Gap::disconnect(DisconnectionReason_t reason) |
flywind | 0:f90c3452d779 | 221 | { |
flywind | 0:f90c3452d779 | 222 | state.advertising = 0; |
flywind | 0:f90c3452d779 | 223 | state.connected = 0; |
flywind | 0:f90c3452d779 | 224 | |
flywind | 0:f90c3452d779 | 225 | uint8_t code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION; |
flywind | 0:f90c3452d779 | 226 | switch (reason) { |
flywind | 0:f90c3452d779 | 227 | case REMOTE_USER_TERMINATED_CONNECTION: |
flywind | 0:f90c3452d779 | 228 | code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION; |
flywind | 0:f90c3452d779 | 229 | break; |
flywind | 0:f90c3452d779 | 230 | case CONN_INTERVAL_UNACCEPTABLE: |
flywind | 0:f90c3452d779 | 231 | code = BLE_HCI_CONN_INTERVAL_UNACCEPTABLE; |
flywind | 0:f90c3452d779 | 232 | break; |
flywind | 0:f90c3452d779 | 233 | } |
flywind | 0:f90c3452d779 | 234 | |
flywind | 0:f90c3452d779 | 235 | /* Disconnect if we are connected to a central device */ |
flywind | 0:f90c3452d779 | 236 | ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(m_connectionHandle, code), BLE_ERROR_PARAM_OUT_OF_RANGE); |
flywind | 0:f90c3452d779 | 237 | |
flywind | 0:f90c3452d779 | 238 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 239 | } |
flywind | 0:f90c3452d779 | 240 | |
flywind | 0:f90c3452d779 | 241 | ble_error_t nRF51Gap::getPreferredConnectionParams(ConnectionParams_t *params) |
flywind | 0:f90c3452d779 | 242 | { |
flywind | 0:f90c3452d779 | 243 | ASSERT_INT(NRF_SUCCESS, |
flywind | 0:f90c3452d779 | 244 | sd_ble_gap_ppcp_get(reinterpret_cast<ble_gap_conn_params_t *>(params)), |
flywind | 0:f90c3452d779 | 245 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
flywind | 0:f90c3452d779 | 246 | |
flywind | 0:f90c3452d779 | 247 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 248 | } |
flywind | 0:f90c3452d779 | 249 | |
flywind | 0:f90c3452d779 | 250 | ble_error_t nRF51Gap::setPreferredConnectionParams(const ConnectionParams_t *params) |
flywind | 0:f90c3452d779 | 251 | { |
flywind | 0:f90c3452d779 | 252 | ASSERT_INT(NRF_SUCCESS, |
flywind | 0:f90c3452d779 | 253 | sd_ble_gap_ppcp_set(reinterpret_cast<const ble_gap_conn_params_t *>(params)), |
flywind | 0:f90c3452d779 | 254 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
flywind | 0:f90c3452d779 | 255 | |
flywind | 0:f90c3452d779 | 256 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 257 | } |
flywind | 0:f90c3452d779 | 258 | |
flywind | 0:f90c3452d779 | 259 | ble_error_t nRF51Gap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *newParams) |
flywind | 0:f90c3452d779 | 260 | { |
flywind | 0:f90c3452d779 | 261 | uint32_t rc; |
flywind | 0:f90c3452d779 | 262 | |
flywind | 0:f90c3452d779 | 263 | rc = sd_ble_gap_conn_param_update(handle, reinterpret_cast<ble_gap_conn_params_t *>(const_cast<ConnectionParams_t*>(newParams))); |
flywind | 0:f90c3452d779 | 264 | if (rc == NRF_SUCCESS) { |
flywind | 0:f90c3452d779 | 265 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 266 | } else { |
flywind | 0:f90c3452d779 | 267 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 268 | } |
flywind | 0:f90c3452d779 | 269 | } |
flywind | 0:f90c3452d779 | 270 | |
flywind | 0:f90c3452d779 | 271 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 272 | /*! |
flywind | 0:f90c3452d779 | 273 | @brief Sets the 16-bit connection handle |
flywind | 0:f90c3452d779 | 274 | */ |
flywind | 0:f90c3452d779 | 275 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 276 | void nRF51Gap::setConnectionHandle(uint16_t con_handle) |
flywind | 0:f90c3452d779 | 277 | { |
flywind | 0:f90c3452d779 | 278 | m_connectionHandle = con_handle; |
flywind | 0:f90c3452d779 | 279 | } |
flywind | 0:f90c3452d779 | 280 | |
flywind | 0:f90c3452d779 | 281 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 282 | /*! |
flywind | 0:f90c3452d779 | 283 | @brief Gets the 16-bit connection handle |
flywind | 0:f90c3452d779 | 284 | */ |
flywind | 0:f90c3452d779 | 285 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 286 | uint16_t nRF51Gap::getConnectionHandle(void) |
flywind | 0:f90c3452d779 | 287 | { |
flywind | 0:f90c3452d779 | 288 | return m_connectionHandle; |
flywind | 0:f90c3452d779 | 289 | } |
flywind | 0:f90c3452d779 | 290 | |
flywind | 0:f90c3452d779 | 291 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 292 | /*! |
flywind | 0:f90c3452d779 | 293 | @brief Sets the BLE device address |
flywind | 0:f90c3452d779 | 294 | |
flywind | 0:f90c3452d779 | 295 | @returns ble_error_t |
flywind | 0:f90c3452d779 | 296 | |
flywind | 0:f90c3452d779 | 297 | @section EXAMPLE |
flywind | 0:f90c3452d779 | 298 | |
flywind | 0:f90c3452d779 | 299 | @code |
flywind | 0:f90c3452d779 | 300 | |
flywind | 0:f90c3452d779 | 301 | uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 }; |
flywind | 0:f90c3452d779 | 302 | nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address); |
flywind | 0:f90c3452d779 | 303 | |
flywind | 0:f90c3452d779 | 304 | @endcode |
flywind | 0:f90c3452d779 | 305 | */ |
flywind | 0:f90c3452d779 | 306 | /**************************************************************************/ |
flywind | 0:f90c3452d779 | 307 | ble_error_t nRF51Gap::setAddress(addr_type_t type, const address_t address) |
flywind | 0:f90c3452d779 | 308 | { |
flywind | 0:f90c3452d779 | 309 | if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) { |
flywind | 0:f90c3452d779 | 310 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 311 | } |
flywind | 0:f90c3452d779 | 312 | |
flywind | 0:f90c3452d779 | 313 | ble_gap_addr_t dev_addr; |
flywind | 0:f90c3452d779 | 314 | dev_addr.addr_type = type; |
flywind | 0:f90c3452d779 | 315 | memcpy(dev_addr.addr, address, ADDR_LEN); |
flywind | 0:f90c3452d779 | 316 | |
flywind | 0:f90c3452d779 | 317 | ASSERT_INT(ERROR_NONE, sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &dev_addr), BLE_ERROR_PARAM_OUT_OF_RANGE); |
flywind | 0:f90c3452d779 | 318 | |
flywind | 0:f90c3452d779 | 319 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 320 | } |
flywind | 0:f90c3452d779 | 321 | |
flywind | 0:f90c3452d779 | 322 | ble_error_t nRF51Gap::getAddress(addr_type_t *typeP, address_t address) |
flywind | 0:f90c3452d779 | 323 | { |
flywind | 0:f90c3452d779 | 324 | ble_gap_addr_t dev_addr; |
flywind | 0:f90c3452d779 | 325 | if (sd_ble_gap_address_get(&dev_addr) != NRF_SUCCESS) { |
flywind | 0:f90c3452d779 | 326 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 327 | } |
flywind | 0:f90c3452d779 | 328 | |
flywind | 0:f90c3452d779 | 329 | if (typeP != NULL) { |
flywind | 0:f90c3452d779 | 330 | *typeP = static_cast<addr_type_t>(dev_addr.addr_type); |
flywind | 0:f90c3452d779 | 331 | } |
flywind | 0:f90c3452d779 | 332 | if (address != NULL) { |
flywind | 0:f90c3452d779 | 333 | memcpy(address, dev_addr.addr, ADDR_LEN); |
flywind | 0:f90c3452d779 | 334 | } |
flywind | 0:f90c3452d779 | 335 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 336 | } |
flywind | 0:f90c3452d779 | 337 | |
flywind | 0:f90c3452d779 | 338 | ble_error_t nRF51Gap::setDeviceName(const uint8_t *deviceName) |
flywind | 0:f90c3452d779 | 339 | { |
flywind | 0:f90c3452d779 | 340 | ble_gap_conn_sec_mode_t sec_mode; |
flywind | 0:f90c3452d779 | 341 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed |
flywind | 0:f90c3452d779 | 342 | |
flywind | 0:f90c3452d779 | 343 | if (sd_ble_gap_device_name_set(&sec_mode, deviceName, strlen((const char *)deviceName)) == NRF_SUCCESS) { |
flywind | 0:f90c3452d779 | 344 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 345 | } else { |
flywind | 0:f90c3452d779 | 346 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 347 | } |
flywind | 0:f90c3452d779 | 348 | } |
flywind | 0:f90c3452d779 | 349 | |
flywind | 0:f90c3452d779 | 350 | ble_error_t nRF51Gap::getDeviceName(uint8_t *deviceName, unsigned *lengthP) |
flywind | 0:f90c3452d779 | 351 | { |
flywind | 0:f90c3452d779 | 352 | if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) { |
flywind | 0:f90c3452d779 | 353 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 354 | } else { |
flywind | 0:f90c3452d779 | 355 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 356 | } |
flywind | 0:f90c3452d779 | 357 | } |
flywind | 0:f90c3452d779 | 358 | |
flywind | 0:f90c3452d779 | 359 | ble_error_t nRF51Gap::setAppearance(uint16_t appearance) |
flywind | 0:f90c3452d779 | 360 | { |
flywind | 0:f90c3452d779 | 361 | if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) { |
flywind | 0:f90c3452d779 | 362 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 363 | } else { |
flywind | 0:f90c3452d779 | 364 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 365 | } |
flywind | 0:f90c3452d779 | 366 | } |
flywind | 0:f90c3452d779 | 367 | |
flywind | 0:f90c3452d779 | 368 | ble_error_t nRF51Gap::getAppearance(uint16_t *appearanceP) |
flywind | 0:f90c3452d779 | 369 | { |
flywind | 0:f90c3452d779 | 370 | if (sd_ble_gap_appearance_get(appearanceP)) { |
flywind | 0:f90c3452d779 | 371 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 372 | } else { |
flywind | 0:f90c3452d779 | 373 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 374 | } |
flywind | 0:f90c3452d779 | 375 | } |