Changed the device name.

Dependents:   BLE_Health_Thermometer_HeartRateMonitor

Fork of BLE_API_Native_IRC by Yoshihiro TSUBOI

Committer:
ktownsend
Date:
Thu Feb 20 11:12:32 2014 +0000
Revision:
13:1800682b5703
Parent:
0:4c3097c65247
Fixed Appearance in the GAP Service

Who changed what in which revision?

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