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