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