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