テスト用

Fork of nRF51822 by Nordic Semiconductor

Committer:
Rohit Grover
Date:
Mon Jul 07 13:43:31 2014 +0100
Revision:
37:c29c330d942c
Parent:
22:c6ee8136847e
Child:
40:1cb3707fa798
changes required to upgrade to V7 of the soft-device

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 22:c6ee8136847e 171 ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para),
Rohit Grover 22:c6ee8136847e 172 BLE_ERROR_PARAM_OUT_OF_RANGE);
Rohit Grover 22:c6ee8136847e 173
Rohit Grover 22:c6ee8136847e 174 state.advertising = 1;
Rohit Grover 22:c6ee8136847e 175
Rohit Grover 22:c6ee8136847e 176 return BLE_ERROR_NONE;
Rohit Grover 22:c6ee8136847e 177 }
Rohit Grover 22:c6ee8136847e 178
Rohit Grover 22:c6ee8136847e 179 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 180 /*!
Rohit Grover 22:c6ee8136847e 181 @brief Stops the BLE HW and disconnects from any devices
Rohit Grover 22:c6ee8136847e 182
Rohit Grover 22:c6ee8136847e 183 @returns ble_error_t
Rohit Grover 22:c6ee8136847e 184
Rohit Grover 22:c6ee8136847e 185 @retval BLE_ERROR_NONE
Rohit Grover 22:c6ee8136847e 186 Everything executed properly
Rohit Grover 22:c6ee8136847e 187
Rohit Grover 22:c6ee8136847e 188 @section EXAMPLE
Rohit Grover 22:c6ee8136847e 189
Rohit Grover 22:c6ee8136847e 190 @code
Rohit Grover 22:c6ee8136847e 191
Rohit Grover 22:c6ee8136847e 192 @endcode
Rohit Grover 22:c6ee8136847e 193 */
Rohit Grover 22:c6ee8136847e 194 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 195 ble_error_t nRF51Gap::stopAdvertising(void)
Rohit Grover 22:c6ee8136847e 196 {
Rohit Grover 22:c6ee8136847e 197 /* Stop Advertising */
Rohit Grover 22:c6ee8136847e 198 ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE);
Rohit Grover 22:c6ee8136847e 199
Rohit Grover 22:c6ee8136847e 200 state.advertising = 0;
Rohit Grover 22:c6ee8136847e 201
Rohit Grover 22:c6ee8136847e 202 return BLE_ERROR_NONE;
Rohit Grover 22:c6ee8136847e 203 }
Rohit Grover 22:c6ee8136847e 204
Rohit Grover 22:c6ee8136847e 205 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 206 /*!
Rohit Grover 22:c6ee8136847e 207 @brief Disconnects if we are connected to a central device
Rohit Grover 22:c6ee8136847e 208
Rohit Grover 22:c6ee8136847e 209 @returns ble_error_t
Rohit Grover 22:c6ee8136847e 210
Rohit Grover 22:c6ee8136847e 211 @retval BLE_ERROR_NONE
Rohit Grover 22:c6ee8136847e 212 Everything executed properly
Rohit Grover 22:c6ee8136847e 213
Rohit Grover 22:c6ee8136847e 214 @section EXAMPLE
Rohit Grover 22:c6ee8136847e 215
Rohit Grover 22:c6ee8136847e 216 @code
Rohit Grover 22:c6ee8136847e 217
Rohit Grover 22:c6ee8136847e 218 @endcode
Rohit Grover 22:c6ee8136847e 219 */
Rohit Grover 22:c6ee8136847e 220 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 221 ble_error_t nRF51Gap::disconnect(void)
Rohit Grover 22:c6ee8136847e 222 {
Rohit Grover 22:c6ee8136847e 223 state.advertising = 0;
Rohit Grover 22:c6ee8136847e 224 state.connected = 0;
Rohit Grover 22:c6ee8136847e 225
Rohit Grover 22:c6ee8136847e 226 /* Disconnect if we are connected to a central device */
Rohit Grover 22:c6ee8136847e 227 ASSERT_INT(ERROR_NONE,
Rohit Grover 22:c6ee8136847e 228 sd_ble_gap_disconnect(m_connectionHandle,
Rohit Grover 22:c6ee8136847e 229 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION),
Rohit Grover 22:c6ee8136847e 230 BLE_ERROR_PARAM_OUT_OF_RANGE);
Rohit Grover 22:c6ee8136847e 231
Rohit Grover 22:c6ee8136847e 232 return BLE_ERROR_NONE;
Rohit Grover 22:c6ee8136847e 233 }
Rohit Grover 22:c6ee8136847e 234
Rohit Grover 22:c6ee8136847e 235 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 236 /*!
Rohit Grover 22:c6ee8136847e 237 @brief Sets the 16-bit connection handle
Rohit Grover 22:c6ee8136847e 238 */
Rohit Grover 22:c6ee8136847e 239 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 240 void nRF51Gap::setConnectionHandle(uint16_t con_handle)
Rohit Grover 22:c6ee8136847e 241 {
Rohit Grover 22:c6ee8136847e 242 m_connectionHandle = con_handle;
Rohit Grover 22:c6ee8136847e 243 }
Rohit Grover 22:c6ee8136847e 244
Rohit Grover 22:c6ee8136847e 245 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 246 /*!
Rohit Grover 22:c6ee8136847e 247 @brief Gets the 16-bit connection handle
Rohit Grover 22:c6ee8136847e 248 */
Rohit Grover 22:c6ee8136847e 249 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 250 uint16_t nRF51Gap::getConnectionHandle(void)
Rohit Grover 22:c6ee8136847e 251 {
Rohit Grover 22:c6ee8136847e 252 return m_connectionHandle;
Rohit Grover 22:c6ee8136847e 253 }
Rohit Grover 22:c6ee8136847e 254
Rohit Grover 22:c6ee8136847e 255 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 256 /*!
Rohit Grover 22:c6ee8136847e 257 @brief Sets the BLE device address
Rohit Grover 22:c6ee8136847e 258
Rohit Grover 22:c6ee8136847e 259 @returns ble_error_t
Rohit Grover 22:c6ee8136847e 260
Rohit Grover 22:c6ee8136847e 261 @section EXAMPLE
Rohit Grover 22:c6ee8136847e 262
Rohit Grover 22:c6ee8136847e 263 @code
Rohit Grover 22:c6ee8136847e 264
Rohit Grover 22:c6ee8136847e 265 uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 };
Rohit Grover 22:c6ee8136847e 266 nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address);
Rohit Grover 22:c6ee8136847e 267
Rohit Grover 22:c6ee8136847e 268 @endcode
Rohit Grover 22:c6ee8136847e 269 */
Rohit Grover 22:c6ee8136847e 270 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 271 ble_error_t nRF51Gap::setAddress(addr_type_t type, const uint8_t address[6])
Rohit Grover 22:c6ee8136847e 272 {
Rohit Grover 22:c6ee8136847e 273 if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) {
Rohit Grover 22:c6ee8136847e 274 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Rohit Grover 22:c6ee8136847e 275 }
Rohit Grover 22:c6ee8136847e 276
Rohit Grover 22:c6ee8136847e 277 ble_gap_addr_t dev_addr;
Rohit Grover 22:c6ee8136847e 278 dev_addr.addr_type = type;
Rohit Grover 22:c6ee8136847e 279 memcpy(dev_addr.addr, address, 6);
Rohit Grover 22:c6ee8136847e 280
Rohit Grover 22:c6ee8136847e 281 ASSERT_INT(ERROR_NONE,
Rohit Grover 37:c29c330d942c 282 sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &dev_addr),
Rohit Grover 22:c6ee8136847e 283 BLE_ERROR_PARAM_OUT_OF_RANGE);
Rohit Grover 22:c6ee8136847e 284
Rohit Grover 37:c29c330d942c 285
Rohit Grover 22:c6ee8136847e 286 return BLE_ERROR_NONE;
Rohit Grover 22:c6ee8136847e 287 }