For scoop

Dependents:   GonioTrainer

Fork of nRF51822 by Nordic Semiconductor

Committer:
dkester
Date:
Thu Jun 11 20:58:18 2015 +0000
Revision:
187:02500109ccb8
Parent:
181:ce044982b915
change to goniometer;

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