konashi/SBBLEのテスト

Dependencies:   BLE_API mbed

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
robo8080
Date:
Sun Aug 17 00:53:23 2014 +0000
Revision:
8:a62b8f7d5dcf
Parent:
5:61109bce11fe
DeviceName??

Who changed what in which revision?

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