テスト用

Fork of nRF51822 by Nordic Semiconductor

Committer:
mbed_official
Date:
Thu Apr 03 01:45:38 2014 +0100
Revision:
1:f84abedbf4fb
Parent:
0:eff01767de02
Child:
3:791d672cbbec
Synchronized with git revision 348e2de6c8b2badc1fd1fe7ae044c24c25244a11

Full URL: https://github.com/mbedmicro/mbed/commit/348e2de6c8b2badc1fd1fe7ae044c24c25244a11/

Who changed what in which revision?

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