Openwear requires RC oscillator to be used

Fork of nRF51822 by Nordic Semiconductor

Committer:
janekm
Date:
Thu Sep 04 21:18:28 2014 +0000
Revision:
62:4dfe75dd5769
Parent:
56:a1071b629aa3
?

Who changed what in which revision?

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