ver:init

Committer:
iv123
Date:
Sun Jun 18 16:10:28 2017 +0000
Revision:
0:88b85febcb45
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iv123 0:88b85febcb45 1 /* mbed Microcontroller Library
iv123 0:88b85febcb45 2 * Copyright (c) 2006-2013 ARM Limited
iv123 0:88b85febcb45 3 *
iv123 0:88b85febcb45 4 * Licensed under the Apache License, Version 2.0 (the "License");
iv123 0:88b85febcb45 5 * you may not use this file except in compliance with the License.
iv123 0:88b85febcb45 6 * You may obtain a copy of the License at
iv123 0:88b85febcb45 7 *
iv123 0:88b85febcb45 8 * http://www.apache.org/licenses/LICENSE-2.0
iv123 0:88b85febcb45 9 *
iv123 0:88b85febcb45 10 * Unless required by applicable law or agreed to in writing, software
iv123 0:88b85febcb45 11 * distributed under the License is distributed on an "AS IS" BASIS,
iv123 0:88b85febcb45 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
iv123 0:88b85febcb45 13 * See the License for the specific language governing permissions and
iv123 0:88b85febcb45 14 * limitations under the License.
iv123 0:88b85febcb45 15 */
iv123 0:88b85febcb45 16
iv123 0:88b85febcb45 17
iv123 0:88b85febcb45 18 /**
iv123 0:88b85febcb45 19 ******************************************************************************
iv123 0:88b85febcb45 20 * @file BlueNRGGap.cpp
iv123 0:88b85febcb45 21 * @author STMicroelectronics
iv123 0:88b85febcb45 22 * @brief Implementation of BLE_API Gap Class
iv123 0:88b85febcb45 23 ******************************************************************************
iv123 0:88b85febcb45 24 * @copy
iv123 0:88b85febcb45 25 *
iv123 0:88b85febcb45 26 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
iv123 0:88b85febcb45 27 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
iv123 0:88b85febcb45 28 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
iv123 0:88b85febcb45 29 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
iv123 0:88b85febcb45 30 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
iv123 0:88b85febcb45 31 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
iv123 0:88b85febcb45 32 *
iv123 0:88b85febcb45 33 * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
iv123 0:88b85febcb45 34 */
iv123 0:88b85febcb45 35
iv123 0:88b85febcb45 36 /** @defgroup BlueNRGGap
iv123 0:88b85febcb45 37 * @brief BlueNRG BLE_API GAP Adaptation
iv123 0:88b85febcb45 38 * @{
iv123 0:88b85febcb45 39 */
iv123 0:88b85febcb45 40
iv123 0:88b85febcb45 41 #include "BlueNRGDevice.h"
iv123 0:88b85febcb45 42 #ifdef YOTTA_CFG_MBED_OS
iv123 0:88b85febcb45 43 #include "mbed-drivers/mbed.h"
iv123 0:88b85febcb45 44 #else
iv123 0:88b85febcb45 45 #include "mbed.h"
iv123 0:88b85febcb45 46 #endif
iv123 0:88b85febcb45 47 #include "ble_payload.h"
iv123 0:88b85febcb45 48 #include "ble_utils.h"
iv123 0:88b85febcb45 49 #include "ble_debug.h"
iv123 0:88b85febcb45 50
iv123 0:88b85febcb45 51 /*
iv123 0:88b85febcb45 52 * Utility to process GAP specific events (e.g., Advertising timeout)
iv123 0:88b85febcb45 53 */
iv123 0:88b85febcb45 54 void BlueNRGGap::Process(void)
iv123 0:88b85febcb45 55 {
iv123 0:88b85febcb45 56 if(AdvToFlag) {
iv123 0:88b85febcb45 57 stopAdvertising();
iv123 0:88b85febcb45 58 }
iv123 0:88b85febcb45 59
iv123 0:88b85febcb45 60 }
iv123 0:88b85febcb45 61
iv123 0:88b85febcb45 62 /**************************************************************************/
iv123 0:88b85febcb45 63 /*!
iv123 0:88b85febcb45 64 @brief Sets the advertising parameters and payload for the device.
iv123 0:88b85febcb45 65 Note: Some data types give error when their adv data is updated using aci_gap_update_adv_data() API
iv123 0:88b85febcb45 66
iv123 0:88b85febcb45 67 @params[in] advData
iv123 0:88b85febcb45 68 The primary advertising data payload
iv123 0:88b85febcb45 69 @params[in] scanResponse
iv123 0:88b85febcb45 70 The optional Scan Response payload if the advertising
iv123 0:88b85febcb45 71 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
iv123 0:88b85febcb45 72 in \ref GapAdveritinngParams
iv123 0:88b85febcb45 73
iv123 0:88b85febcb45 74 @returns \ref ble_error_t
iv123 0:88b85febcb45 75
iv123 0:88b85febcb45 76 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 77 Everything executed properly
iv123 0:88b85febcb45 78
iv123 0:88b85febcb45 79 @retval BLE_ERROR_BUFFER_OVERFLOW
iv123 0:88b85febcb45 80 The proposed action would cause a buffer overflow. All
iv123 0:88b85febcb45 81 advertising payloads must be <= 31 bytes, for example.
iv123 0:88b85febcb45 82
iv123 0:88b85febcb45 83 @retval BLE_ERROR_NOT_IMPLEMENTED
iv123 0:88b85febcb45 84 A feature was requested that is not yet supported in the
iv123 0:88b85febcb45 85 nRF51 firmware or hardware.
iv123 0:88b85febcb45 86
iv123 0:88b85febcb45 87 @retval BLE_ERROR_PARAM_OUT_OF_RANGE
iv123 0:88b85febcb45 88 One of the proposed values is outside the valid range.
iv123 0:88b85febcb45 89
iv123 0:88b85febcb45 90 @section EXAMPLE
iv123 0:88b85febcb45 91
iv123 0:88b85febcb45 92 @code
iv123 0:88b85febcb45 93
iv123 0:88b85febcb45 94 @endcode
iv123 0:88b85febcb45 95 */
iv123 0:88b85febcb45 96 /**************************************************************************/
iv123 0:88b85febcb45 97 ble_error_t BlueNRGGap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse)
iv123 0:88b85febcb45 98 {
iv123 0:88b85febcb45 99 PRINTF("BlueNRGGap::setAdvertisingData\n\r");
iv123 0:88b85febcb45 100 /* Make sure we don't exceed the advertising payload length */
iv123 0:88b85febcb45 101 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
iv123 0:88b85febcb45 102 PRINTF("Exceeded the advertising payload length\n\r");
iv123 0:88b85febcb45 103 return BLE_ERROR_BUFFER_OVERFLOW;
iv123 0:88b85febcb45 104 }
iv123 0:88b85febcb45 105
iv123 0:88b85febcb45 106 /* Make sure we have a payload! */
iv123 0:88b85febcb45 107 if (advData.getPayloadLen() != 0) {
iv123 0:88b85febcb45 108 PayloadPtr loadPtr(advData.getPayload(), advData.getPayloadLen());
iv123 0:88b85febcb45 109
iv123 0:88b85febcb45 110 /* Align the GAP Service Appearance Char value coherently
iv123 0:88b85febcb45 111 This setting is duplicate (see below GapAdvertisingData::APPEARANCE)
iv123 0:88b85febcb45 112 since BLE API has an overloaded function for appearance
iv123 0:88b85febcb45 113 */
iv123 0:88b85febcb45 114 STORE_LE_16(deviceAppearance, advData.getAppearance());
iv123 0:88b85febcb45 115 setAppearance((GapAdvertisingData::Appearance)(deviceAppearance[1]<<8|deviceAppearance[0]));
iv123 0:88b85febcb45 116
iv123 0:88b85febcb45 117
iv123 0:88b85febcb45 118 for(uint8_t index=0; index<loadPtr.getPayloadUnitCount(); index++) {
iv123 0:88b85febcb45 119 loadPtr.getUnitAtIndex(index);
iv123 0:88b85febcb45 120
iv123 0:88b85febcb45 121 PRINTF("adData[%d].length=%d\n\r", index,(uint8_t)(*loadPtr.getUnitAtIndex(index).getLenPtr()));
iv123 0:88b85febcb45 122 PRINTF("adData[%d].AdType=0x%x\n\r", index,(uint8_t)(*loadPtr.getUnitAtIndex(index).getAdTypePtr()));
iv123 0:88b85febcb45 123
iv123 0:88b85febcb45 124 switch(*loadPtr.getUnitAtIndex(index).getAdTypePtr()) {
iv123 0:88b85febcb45 125 /**< TX Power Level (in dBm) */
iv123 0:88b85febcb45 126 case GapAdvertisingData::TX_POWER_LEVEL:
iv123 0:88b85febcb45 127 {
iv123 0:88b85febcb45 128 PRINTF("Advertising type: TX_POWER_LEVEL\n\r");
iv123 0:88b85febcb45 129 int8_t enHighPower = 0;
iv123 0:88b85febcb45 130 int8_t paLevel = 0;
iv123 0:88b85febcb45 131
iv123 0:88b85febcb45 132 int8_t dbm = *loadPtr.getUnitAtIndex(index).getDataPtr();
iv123 0:88b85febcb45 133 tBleStatus ret = getHighPowerAndPALevelValue(dbm, enHighPower, paLevel);
iv123 0:88b85febcb45 134 #ifdef DEBUG
iv123 0:88b85febcb45 135 PRINTF("dbm=%d, ret=%d\n\r", dbm, ret);
iv123 0:88b85febcb45 136 PRINTF("enHighPower=%d, paLevel=%d\n\r", enHighPower, paLevel);
iv123 0:88b85febcb45 137 #endif
iv123 0:88b85febcb45 138 if(ret == BLE_STATUS_SUCCESS) {
iv123 0:88b85febcb45 139 aci_hal_set_tx_power_level(enHighPower, paLevel);
iv123 0:88b85febcb45 140 }
iv123 0:88b85febcb45 141 break;
iv123 0:88b85febcb45 142 }
iv123 0:88b85febcb45 143 /**< Appearance */
iv123 0:88b85febcb45 144 case GapAdvertisingData::APPEARANCE:
iv123 0:88b85febcb45 145 {
iv123 0:88b85febcb45 146 PRINTF("Advertising type: APPEARANCE\n\r");
iv123 0:88b85febcb45 147
iv123 0:88b85febcb45 148 GapAdvertisingData::Appearance appearanceP;
iv123 0:88b85febcb45 149 memcpy(deviceAppearance, loadPtr.getUnitAtIndex(index).getDataPtr(), 2);
iv123 0:88b85febcb45 150
iv123 0:88b85febcb45 151 PRINTF("input: deviceAppearance= 0x%x 0x%x\n\r", deviceAppearance[1], deviceAppearance[0]);
iv123 0:88b85febcb45 152
iv123 0:88b85febcb45 153 appearanceP = (GapAdvertisingData::Appearance)(deviceAppearance[1]<<8|deviceAppearance[0]);
iv123 0:88b85febcb45 154 /* Align the GAP Service Appearance Char value coherently */
iv123 0:88b85febcb45 155 setAppearance(appearanceP);
iv123 0:88b85febcb45 156 break;
iv123 0:88b85febcb45 157 }
iv123 0:88b85febcb45 158
iv123 0:88b85febcb45 159 } // end switch
iv123 0:88b85febcb45 160
iv123 0:88b85febcb45 161 } //end for
iv123 0:88b85febcb45 162
iv123 0:88b85febcb45 163 }
iv123 0:88b85febcb45 164
iv123 0:88b85febcb45 165 // update the advertising data in the shield if advertising is running
iv123 0:88b85febcb45 166 if (state.advertising == 1) {
iv123 0:88b85febcb45 167 tBleStatus ret = hci_le_set_scan_resp_data(scanResponse.getPayloadLen(), scanResponse.getPayload());
iv123 0:88b85febcb45 168
iv123 0:88b85febcb45 169 if(BLE_STATUS_SUCCESS != ret) {
iv123 0:88b85febcb45 170 PRINTF(" error while setting scan response data (ret=0x%x)\n", ret);
iv123 0:88b85febcb45 171 switch (ret) {
iv123 0:88b85febcb45 172 case BLE_STATUS_TIMEOUT:
iv123 0:88b85febcb45 173 return BLE_STACK_BUSY;
iv123 0:88b85febcb45 174 default:
iv123 0:88b85febcb45 175 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 176 }
iv123 0:88b85febcb45 177 }
iv123 0:88b85febcb45 178
iv123 0:88b85febcb45 179 ret = hci_le_set_advertising_data(advData.getPayloadLen(), advData.getPayload());
iv123 0:88b85febcb45 180 if (ret) {
iv123 0:88b85febcb45 181 PRINTF("error while setting the payload\r\n");
iv123 0:88b85febcb45 182 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 183 }
iv123 0:88b85febcb45 184 }
iv123 0:88b85febcb45 185
iv123 0:88b85febcb45 186 _advData = advData;
iv123 0:88b85febcb45 187 _scanResponse = scanResponse;
iv123 0:88b85febcb45 188
iv123 0:88b85febcb45 189 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 190 }
iv123 0:88b85febcb45 191
iv123 0:88b85febcb45 192 /*
iv123 0:88b85febcb45 193 * Utility to set ADV timeout flag
iv123 0:88b85febcb45 194 */
iv123 0:88b85febcb45 195 void BlueNRGGap::setAdvToFlag(void) {
iv123 0:88b85febcb45 196 AdvToFlag = true;
iv123 0:88b85febcb45 197 }
iv123 0:88b85febcb45 198
iv123 0:88b85febcb45 199 /*
iv123 0:88b85febcb45 200 * ADV timeout callback
iv123 0:88b85febcb45 201 */
iv123 0:88b85febcb45 202 #ifdef AST_FOR_MBED_OS
iv123 0:88b85febcb45 203 static void advTimeoutCB(void)
iv123 0:88b85febcb45 204 {
iv123 0:88b85febcb45 205 Gap::GapState_t state;
iv123 0:88b85febcb45 206
iv123 0:88b85febcb45 207 state = BlueNRGGap::getInstance().getState();
iv123 0:88b85febcb45 208 if (state.advertising == 1) {
iv123 0:88b85febcb45 209
iv123 0:88b85febcb45 210 BlueNRGGap::getInstance().stopAdvertising();
iv123 0:88b85febcb45 211
iv123 0:88b85febcb45 212 }
iv123 0:88b85febcb45 213 }
iv123 0:88b85febcb45 214 #else
iv123 0:88b85febcb45 215 static void advTimeoutCB(void)
iv123 0:88b85febcb45 216 {
iv123 0:88b85febcb45 217 Gap::GapState_t state;
iv123 0:88b85febcb45 218
iv123 0:88b85febcb45 219 state = BlueNRGGap::getInstance().getState();
iv123 0:88b85febcb45 220 if (state.advertising == 1) {
iv123 0:88b85febcb45 221
iv123 0:88b85febcb45 222 BlueNRGGap::getInstance().setAdvToFlag();
iv123 0:88b85febcb45 223
iv123 0:88b85febcb45 224 Timeout t = BlueNRGGap::getInstance().getAdvTimeout();
iv123 0:88b85febcb45 225 t.detach(); /* disable the callback from the timeout */
iv123 0:88b85febcb45 226
iv123 0:88b85febcb45 227 }
iv123 0:88b85febcb45 228 }
iv123 0:88b85febcb45 229 #endif /* AST_FOR_MBED_OS */
iv123 0:88b85febcb45 230
iv123 0:88b85febcb45 231 /**************************************************************************/
iv123 0:88b85febcb45 232 /*!
iv123 0:88b85febcb45 233 @brief Starts the BLE HW, initialising any services that were
iv123 0:88b85febcb45 234 added before this function was called.
iv123 0:88b85febcb45 235
iv123 0:88b85febcb45 236 @param[in] params
iv123 0:88b85febcb45 237 Basic advertising details, including the advertising
iv123 0:88b85febcb45 238 delay, timeout and how the device should be advertised
iv123 0:88b85febcb45 239
iv123 0:88b85febcb45 240 @note All services must be added before calling this function!
iv123 0:88b85febcb45 241
iv123 0:88b85febcb45 242 @returns ble_error_t
iv123 0:88b85febcb45 243
iv123 0:88b85febcb45 244 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 245 Everything executed properly
iv123 0:88b85febcb45 246
iv123 0:88b85febcb45 247 @section EXAMPLE
iv123 0:88b85febcb45 248
iv123 0:88b85febcb45 249 @code
iv123 0:88b85febcb45 250
iv123 0:88b85febcb45 251 @endcode
iv123 0:88b85febcb45 252 */
iv123 0:88b85febcb45 253 /**************************************************************************/
iv123 0:88b85febcb45 254
iv123 0:88b85febcb45 255 ble_error_t BlueNRGGap::startAdvertising(const GapAdvertisingParams &params)
iv123 0:88b85febcb45 256 {
iv123 0:88b85febcb45 257 tBleStatus ret;
iv123 0:88b85febcb45 258 int err;
iv123 0:88b85febcb45 259
iv123 0:88b85febcb45 260 /* Make sure we support the advertising type */
iv123 0:88b85febcb45 261 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) {
iv123 0:88b85febcb45 262 /* ToDo: This requires a proper security implementation, etc. */
iv123 0:88b85febcb45 263 return BLE_ERROR_NOT_IMPLEMENTED;
iv123 0:88b85febcb45 264 }
iv123 0:88b85febcb45 265
iv123 0:88b85febcb45 266 /* Check interval range */
iv123 0:88b85febcb45 267 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) {
iv123 0:88b85febcb45 268 /* Min delay is slightly longer for unconnectable devices */
iv123 0:88b85febcb45 269 if ((params.getIntervalInADVUnits() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
iv123 0:88b85febcb45 270 (params.getIntervalInADVUnits() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) {
iv123 0:88b85febcb45 271 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 272 }
iv123 0:88b85febcb45 273 } else {
iv123 0:88b85febcb45 274 if ((params.getIntervalInADVUnits() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN) ||
iv123 0:88b85febcb45 275 (params.getIntervalInADVUnits() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) {
iv123 0:88b85febcb45 276 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 277 }
iv123 0:88b85febcb45 278 }
iv123 0:88b85febcb45 279
iv123 0:88b85febcb45 280 /* Check timeout is zero for Connectable Directed */
iv123 0:88b85febcb45 281 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) {
iv123 0:88b85febcb45 282 /* Timeout must be 0 with this type, although we'll never get here */
iv123 0:88b85febcb45 283 /* since this isn't implemented yet anyway */
iv123 0:88b85febcb45 284 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 285 }
iv123 0:88b85febcb45 286
iv123 0:88b85febcb45 287 /* Check timeout for other advertising types */
iv123 0:88b85febcb45 288 if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
iv123 0:88b85febcb45 289 (params.getTimeout() > GapAdvertisingParams::GAP_ADV_PARAMS_TIMEOUT_MAX)) {
iv123 0:88b85febcb45 290 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 291 }
iv123 0:88b85febcb45 292
iv123 0:88b85febcb45 293 /*
iv123 0:88b85febcb45 294 * Advertising filter policy setting
iv123 0:88b85febcb45 295 * FIXME: the Security Manager should be implemented
iv123 0:88b85febcb45 296 */
iv123 0:88b85febcb45 297 AdvertisingPolicyMode_t mode = getAdvertisingPolicyMode();
iv123 0:88b85febcb45 298 if(mode != ADV_POLICY_IGNORE_WHITELIST) {
iv123 0:88b85febcb45 299 ret = aci_gap_configure_whitelist();
iv123 0:88b85febcb45 300 if(ret != BLE_STATUS_SUCCESS) {
iv123 0:88b85febcb45 301 PRINTF("aci_gap_configure_whitelist ret=0x%x\n\r", ret);
iv123 0:88b85febcb45 302 return BLE_ERROR_OPERATION_NOT_PERMITTED;
iv123 0:88b85febcb45 303 }
iv123 0:88b85febcb45 304 }
iv123 0:88b85febcb45 305
iv123 0:88b85febcb45 306 uint8_t advFilterPolicy = NO_WHITE_LIST_USE;
iv123 0:88b85febcb45 307 switch(mode) {
iv123 0:88b85febcb45 308 case ADV_POLICY_FILTER_SCAN_REQS:
iv123 0:88b85febcb45 309 advFilterPolicy = WHITE_LIST_FOR_ONLY_SCAN;
iv123 0:88b85febcb45 310 break;
iv123 0:88b85febcb45 311 case ADV_POLICY_FILTER_CONN_REQS:
iv123 0:88b85febcb45 312 advFilterPolicy = WHITE_LIST_FOR_ONLY_CONN;
iv123 0:88b85febcb45 313 break;
iv123 0:88b85febcb45 314 case ADV_POLICY_FILTER_ALL_REQS:
iv123 0:88b85febcb45 315 advFilterPolicy = WHITE_LIST_FOR_ALL;
iv123 0:88b85febcb45 316 break;
iv123 0:88b85febcb45 317 default:
iv123 0:88b85febcb45 318 advFilterPolicy = NO_WHITE_LIST_USE;
iv123 0:88b85febcb45 319 break;
iv123 0:88b85febcb45 320 }
iv123 0:88b85febcb45 321
iv123 0:88b85febcb45 322 /* Check the ADV type before setting scan response data */
iv123 0:88b85febcb45 323 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED ||
iv123 0:88b85febcb45 324 params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED) {
iv123 0:88b85febcb45 325
iv123 0:88b85febcb45 326 /* set scan response data */
iv123 0:88b85febcb45 327 PRINTF(" setting scan response data (_scanResponseLen=%u)\n", _scanResponse.getPayloadLen());
iv123 0:88b85febcb45 328 ret = hci_le_set_scan_resp_data(_scanResponse.getPayloadLen(), _scanResponse.getPayload());
iv123 0:88b85febcb45 329
iv123 0:88b85febcb45 330 if(BLE_STATUS_SUCCESS!=ret) {
iv123 0:88b85febcb45 331 PRINTF(" error while setting scan response data (ret=0x%x)\n", ret);
iv123 0:88b85febcb45 332 switch (ret) {
iv123 0:88b85febcb45 333 case BLE_STATUS_TIMEOUT:
iv123 0:88b85febcb45 334 return BLE_STACK_BUSY;
iv123 0:88b85febcb45 335 default:
iv123 0:88b85febcb45 336 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 337 }
iv123 0:88b85febcb45 338 }
iv123 0:88b85febcb45 339 } else {
iv123 0:88b85febcb45 340 hci_le_set_scan_resp_data(0, NULL);
iv123 0:88b85febcb45 341 }
iv123 0:88b85febcb45 342
iv123 0:88b85febcb45 343 setAdvParameters();
iv123 0:88b85febcb45 344 PRINTF("advInterval=%d advType=%d\n\r", advInterval, params.getAdvertisingType());
iv123 0:88b85febcb45 345
iv123 0:88b85febcb45 346 err = hci_le_set_advertising_data(_advData.getPayloadLen(), _advData.getPayload());
iv123 0:88b85febcb45 347
iv123 0:88b85febcb45 348 if (err) {
iv123 0:88b85febcb45 349 PRINTF("error while setting the payload\r\n");
iv123 0:88b85febcb45 350 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 351 }
iv123 0:88b85febcb45 352
iv123 0:88b85febcb45 353 tBDAddr dummy_addr = { 0 };
iv123 0:88b85febcb45 354 uint16_t advIntervalMin = advInterval == GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX ? advInterval - 1 : advInterval;
iv123 0:88b85febcb45 355 uint16_t advIntervalMax = advIntervalMin + 1;
iv123 0:88b85febcb45 356
iv123 0:88b85febcb45 357 err = hci_le_set_advertising_parameters(
iv123 0:88b85febcb45 358 advIntervalMin,
iv123 0:88b85febcb45 359 advIntervalMax,
iv123 0:88b85febcb45 360 params.getAdvertisingType(),
iv123 0:88b85febcb45 361 addr_type,
iv123 0:88b85febcb45 362 0x00,
iv123 0:88b85febcb45 363 dummy_addr,
iv123 0:88b85febcb45 364 /* all channels */ 7,
iv123 0:88b85febcb45 365 advFilterPolicy
iv123 0:88b85febcb45 366 );
iv123 0:88b85febcb45 367
iv123 0:88b85febcb45 368 if (err) {
iv123 0:88b85febcb45 369 PRINTF("impossible to set advertising parameters\n\r");
iv123 0:88b85febcb45 370 PRINTF("advInterval min: %u, advInterval max: %u\n\r", advInterval, advInterval + 1);
iv123 0:88b85febcb45 371 PRINTF("advType: %u, advFilterPolicy: %u\n\r", params.getAdvertisingType(), advFilterPolicy);
iv123 0:88b85febcb45 372 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 373 }
iv123 0:88b85febcb45 374
iv123 0:88b85febcb45 375 err = hci_le_set_advertise_enable(0x01);
iv123 0:88b85febcb45 376 if (err) {
iv123 0:88b85febcb45 377 PRINTF("impossible to start advertising\n\r");
iv123 0:88b85febcb45 378 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 379 }
iv123 0:88b85febcb45 380
iv123 0:88b85febcb45 381 state.advertising = 1;
iv123 0:88b85febcb45 382
iv123 0:88b85febcb45 383 AdvToFlag = false;
iv123 0:88b85febcb45 384 if(params.getTimeout() != 0) {
iv123 0:88b85febcb45 385 PRINTF("!!! attaching to!!!\n");
iv123 0:88b85febcb45 386 #ifdef AST_FOR_MBED_OS
iv123 0:88b85febcb45 387 minar::Scheduler::postCallback(advTimeoutCB).delay(minar::milliseconds(params.getTimeout() * 1000));
iv123 0:88b85febcb45 388 #else
iv123 0:88b85febcb45 389 advTimeout.attach(advTimeoutCB, params.getTimeout() * 1000);
iv123 0:88b85febcb45 390 #endif
iv123 0:88b85febcb45 391 }
iv123 0:88b85febcb45 392
iv123 0:88b85febcb45 393 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 394
iv123 0:88b85febcb45 395 }
iv123 0:88b85febcb45 396
iv123 0:88b85febcb45 397
iv123 0:88b85febcb45 398 /**************************************************************************/
iv123 0:88b85febcb45 399 /*!
iv123 0:88b85febcb45 400 @brief Stops the BLE HW and disconnects from any devices
iv123 0:88b85febcb45 401
iv123 0:88b85febcb45 402 @returns ble_error_t
iv123 0:88b85febcb45 403
iv123 0:88b85febcb45 404 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 405 Everything executed properly
iv123 0:88b85febcb45 406
iv123 0:88b85febcb45 407 @section EXAMPLE
iv123 0:88b85febcb45 408
iv123 0:88b85febcb45 409 @code
iv123 0:88b85febcb45 410
iv123 0:88b85febcb45 411 @endcode
iv123 0:88b85febcb45 412 */
iv123 0:88b85febcb45 413 /**************************************************************************/
iv123 0:88b85febcb45 414 ble_error_t BlueNRGGap::stopAdvertising(void)
iv123 0:88b85febcb45 415 {
iv123 0:88b85febcb45 416
iv123 0:88b85febcb45 417 if(state.advertising == 1) {
iv123 0:88b85febcb45 418
iv123 0:88b85febcb45 419 int err = hci_le_set_advertise_enable(0);
iv123 0:88b85febcb45 420 if (err) {
iv123 0:88b85febcb45 421 return BLE_ERROR_OPERATION_NOT_PERMITTED;
iv123 0:88b85febcb45 422 }
iv123 0:88b85febcb45 423
iv123 0:88b85febcb45 424 PRINTF("Advertisement stopped!!\n\r") ;
iv123 0:88b85febcb45 425 //Set GapState_t::advertising state
iv123 0:88b85febcb45 426 state.advertising = 0;
iv123 0:88b85febcb45 427 }
iv123 0:88b85febcb45 428
iv123 0:88b85febcb45 429 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 430 }
iv123 0:88b85febcb45 431
iv123 0:88b85febcb45 432 /**************************************************************************/
iv123 0:88b85febcb45 433 /*!
iv123 0:88b85febcb45 434 @brief Disconnects if we are connected to a central device
iv123 0:88b85febcb45 435
iv123 0:88b85febcb45 436 @param[in] reason
iv123 0:88b85febcb45 437 Disconnection Reason
iv123 0:88b85febcb45 438
iv123 0:88b85febcb45 439 @returns ble_error_t
iv123 0:88b85febcb45 440
iv123 0:88b85febcb45 441 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 442 Everything executed properly
iv123 0:88b85febcb45 443
iv123 0:88b85febcb45 444 @section EXAMPLE
iv123 0:88b85febcb45 445
iv123 0:88b85febcb45 446 @code
iv123 0:88b85febcb45 447
iv123 0:88b85febcb45 448 @endcode
iv123 0:88b85febcb45 449 */
iv123 0:88b85febcb45 450 /**************************************************************************/
iv123 0:88b85febcb45 451 ble_error_t BlueNRGGap::disconnect(Handle_t connectionHandle, Gap::DisconnectionReason_t reason)
iv123 0:88b85febcb45 452 {
iv123 0:88b85febcb45 453 tBleStatus ret;
iv123 0:88b85febcb45 454
iv123 0:88b85febcb45 455 ret = aci_gap_terminate(connectionHandle, reason);
iv123 0:88b85febcb45 456
iv123 0:88b85febcb45 457 if (BLE_STATUS_SUCCESS != ret){
iv123 0:88b85febcb45 458 PRINTF("Error in GAP termination (ret=0x%x)!!\n\r", ret) ;
iv123 0:88b85febcb45 459 switch (ret) {
iv123 0:88b85febcb45 460 case ERR_COMMAND_DISALLOWED:
iv123 0:88b85febcb45 461 return BLE_ERROR_OPERATION_NOT_PERMITTED;
iv123 0:88b85febcb45 462 case BLE_STATUS_TIMEOUT:
iv123 0:88b85febcb45 463 return BLE_STACK_BUSY;
iv123 0:88b85febcb45 464 default:
iv123 0:88b85febcb45 465 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 466 }
iv123 0:88b85febcb45 467 }
iv123 0:88b85febcb45 468
iv123 0:88b85febcb45 469 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 470 }
iv123 0:88b85febcb45 471
iv123 0:88b85febcb45 472 /**************************************************************************/
iv123 0:88b85febcb45 473 /*!
iv123 0:88b85febcb45 474 @brief Disconnects if we are connected to a central device
iv123 0:88b85febcb45 475
iv123 0:88b85febcb45 476 @param[in] reason
iv123 0:88b85febcb45 477 Disconnection Reason
iv123 0:88b85febcb45 478
iv123 0:88b85febcb45 479 @returns ble_error_t
iv123 0:88b85febcb45 480
iv123 0:88b85febcb45 481 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 482 Everything executed properly
iv123 0:88b85febcb45 483
iv123 0:88b85febcb45 484 @section EXAMPLE
iv123 0:88b85febcb45 485
iv123 0:88b85febcb45 486 @code
iv123 0:88b85febcb45 487
iv123 0:88b85febcb45 488 @endcode
iv123 0:88b85febcb45 489 */
iv123 0:88b85febcb45 490 /**************************************************************************/
iv123 0:88b85febcb45 491 ble_error_t BlueNRGGap::disconnect(Gap::DisconnectionReason_t reason)
iv123 0:88b85febcb45 492 {
iv123 0:88b85febcb45 493 return disconnect(m_connectionHandle, reason);
iv123 0:88b85febcb45 494 }
iv123 0:88b85febcb45 495
iv123 0:88b85febcb45 496 /**************************************************************************/
iv123 0:88b85febcb45 497 /*!
iv123 0:88b85febcb45 498 @brief Sets the 16-bit connection handle
iv123 0:88b85febcb45 499
iv123 0:88b85febcb45 500 @param[in] conn_handle
iv123 0:88b85febcb45 501 Connection Handle which is set in the Gap Instance
iv123 0:88b85febcb45 502
iv123 0:88b85febcb45 503 @returns void
iv123 0:88b85febcb45 504 */
iv123 0:88b85febcb45 505 /**************************************************************************/
iv123 0:88b85febcb45 506 void BlueNRGGap::setConnectionHandle(uint16_t conn_handle)
iv123 0:88b85febcb45 507 {
iv123 0:88b85febcb45 508 m_connectionHandle = conn_handle;
iv123 0:88b85febcb45 509 }
iv123 0:88b85febcb45 510
iv123 0:88b85febcb45 511 /**************************************************************************/
iv123 0:88b85febcb45 512 /*!
iv123 0:88b85febcb45 513 @brief Gets the 16-bit connection handle
iv123 0:88b85febcb45 514
iv123 0:88b85febcb45 515 @param[in] void
iv123 0:88b85febcb45 516
iv123 0:88b85febcb45 517 @returns uint16_t
iv123 0:88b85febcb45 518 Connection Handle of the Gap Instance
iv123 0:88b85febcb45 519 */
iv123 0:88b85febcb45 520 /**************************************************************************/
iv123 0:88b85febcb45 521 uint16_t BlueNRGGap::getConnectionHandle(void)
iv123 0:88b85febcb45 522 {
iv123 0:88b85febcb45 523 return m_connectionHandle;
iv123 0:88b85febcb45 524 }
iv123 0:88b85febcb45 525
iv123 0:88b85febcb45 526 /**************************************************************************/
iv123 0:88b85febcb45 527 /*!
iv123 0:88b85febcb45 528 @brief Sets the BLE device address. SetAddress will reset the BLE
iv123 0:88b85febcb45 529 device and re-initialize BTLE. Will not start advertising.
iv123 0:88b85febcb45 530
iv123 0:88b85febcb45 531 @param[in] type
iv123 0:88b85febcb45 532 Type of Address
iv123 0:88b85febcb45 533
iv123 0:88b85febcb45 534 @param[in] address[6]
iv123 0:88b85febcb45 535 Value of the Address to be set
iv123 0:88b85febcb45 536
iv123 0:88b85febcb45 537 @returns ble_error_t
iv123 0:88b85febcb45 538
iv123 0:88b85febcb45 539 @section EXAMPLE
iv123 0:88b85febcb45 540
iv123 0:88b85febcb45 541 @code
iv123 0:88b85febcb45 542
iv123 0:88b85febcb45 543 @endcode
iv123 0:88b85febcb45 544 */
iv123 0:88b85febcb45 545 /**************************************************************************/
iv123 0:88b85febcb45 546 ble_error_t BlueNRGGap::setAddress(AddressType_t type, const BLEProtocol::AddressBytes_t address)
iv123 0:88b85febcb45 547 {
iv123 0:88b85febcb45 548 if (type > BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE) {
iv123 0:88b85febcb45 549 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 550 }
iv123 0:88b85febcb45 551
iv123 0:88b85febcb45 552 if(type == BLEProtocol::AddressType::PUBLIC){
iv123 0:88b85febcb45 553 tBleStatus ret = aci_hal_write_config_data(
iv123 0:88b85febcb45 554 CONFIG_DATA_PUBADDR_OFFSET,
iv123 0:88b85febcb45 555 CONFIG_DATA_PUBADDR_LEN,
iv123 0:88b85febcb45 556 address
iv123 0:88b85febcb45 557 );
iv123 0:88b85febcb45 558 if(ret != BLE_STATUS_SUCCESS) {
iv123 0:88b85febcb45 559 return BLE_ERROR_OPERATION_NOT_PERMITTED;
iv123 0:88b85febcb45 560 }
iv123 0:88b85febcb45 561 } else if (type == BLEProtocol::AddressType::RANDOM_STATIC) {
iv123 0:88b85febcb45 562 // ensure that the random static address is well formed
iv123 0:88b85febcb45 563 if ((address[5] & 0xC0) != 0xC0) {
iv123 0:88b85febcb45 564 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 565 }
iv123 0:88b85febcb45 566
iv123 0:88b85febcb45 567 // thanks to const correctness of the API ...
iv123 0:88b85febcb45 568 tBDAddr random_address = { 0 };
iv123 0:88b85febcb45 569 memcpy(random_address, address, sizeof(random_address));
iv123 0:88b85febcb45 570 int err = hci_le_set_random_address(random_address);
iv123 0:88b85febcb45 571 if (err) {
iv123 0:88b85febcb45 572 return BLE_ERROR_OPERATION_NOT_PERMITTED;
iv123 0:88b85febcb45 573 }
iv123 0:88b85febcb45 574
iv123 0:88b85febcb45 575 // It is not possible to get the bluetooth address when it is set
iv123 0:88b85febcb45 576 // store it locally in class data member
iv123 0:88b85febcb45 577 memcpy(bdaddr, address, sizeof(bdaddr));
iv123 0:88b85febcb45 578 } else {
iv123 0:88b85febcb45 579 // FIXME random addresses are not supported yet
iv123 0:88b85febcb45 580 // BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE
iv123 0:88b85febcb45 581 // BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE
iv123 0:88b85febcb45 582 return BLE_ERROR_NOT_IMPLEMENTED;
iv123 0:88b85febcb45 583 }
iv123 0:88b85febcb45 584
iv123 0:88b85febcb45 585 // if we're here then the address was correctly set
iv123 0:88b85febcb45 586 // commit it inside the addr_type
iv123 0:88b85febcb45 587 addr_type = type;
iv123 0:88b85febcb45 588 isSetAddress = true;
iv123 0:88b85febcb45 589 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 590 }
iv123 0:88b85febcb45 591
iv123 0:88b85febcb45 592 /**************************************************************************/
iv123 0:88b85febcb45 593 /*!
iv123 0:88b85febcb45 594 @brief Returns boolean if the address of the device has been set
iv123 0:88b85febcb45 595 or not
iv123 0:88b85febcb45 596
iv123 0:88b85febcb45 597 @returns bool
iv123 0:88b85febcb45 598
iv123 0:88b85febcb45 599 @section EXAMPLE
iv123 0:88b85febcb45 600
iv123 0:88b85febcb45 601 @code
iv123 0:88b85febcb45 602
iv123 0:88b85febcb45 603 @endcode
iv123 0:88b85febcb45 604 */
iv123 0:88b85febcb45 605 /**************************************************************************/
iv123 0:88b85febcb45 606 bool BlueNRGGap::getIsSetAddress()
iv123 0:88b85febcb45 607 {
iv123 0:88b85febcb45 608 return isSetAddress;
iv123 0:88b85febcb45 609 }
iv123 0:88b85febcb45 610
iv123 0:88b85febcb45 611 /**************************************************************************/
iv123 0:88b85febcb45 612 /*!
iv123 0:88b85febcb45 613 @brief Returns the address of the device if set
iv123 0:88b85febcb45 614
iv123 0:88b85febcb45 615 @returns Pointer to the address if Address is set else NULL
iv123 0:88b85febcb45 616
iv123 0:88b85febcb45 617 @section EXAMPLE
iv123 0:88b85febcb45 618
iv123 0:88b85febcb45 619 @code
iv123 0:88b85febcb45 620
iv123 0:88b85febcb45 621 @endcode
iv123 0:88b85febcb45 622 */
iv123 0:88b85febcb45 623 /**************************************************************************/
iv123 0:88b85febcb45 624 ble_error_t BlueNRGGap::getAddress(BLEProtocol::AddressType_t *typeP, BLEProtocol::AddressBytes_t address)
iv123 0:88b85febcb45 625 {
iv123 0:88b85febcb45 626 uint8_t bdaddr[BDADDR_SIZE];
iv123 0:88b85febcb45 627 uint8_t data_len_out;
iv123 0:88b85febcb45 628
iv123 0:88b85febcb45 629 // precondition, check that pointers in input are valid
iv123 0:88b85febcb45 630 if (typeP == NULL || address == NULL) {
iv123 0:88b85febcb45 631 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 632 }
iv123 0:88b85febcb45 633
iv123 0:88b85febcb45 634 if (addr_type == BLEProtocol::AddressType::PUBLIC) {
iv123 0:88b85febcb45 635 tBleStatus ret = aci_hal_read_config_data(CONFIG_DATA_PUBADDR_OFFSET, BDADDR_SIZE, &data_len_out, bdaddr);
iv123 0:88b85febcb45 636 if(ret != BLE_STATUS_SUCCESS || data_len_out != BDADDR_SIZE) {
iv123 0:88b85febcb45 637 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 638 }
iv123 0:88b85febcb45 639 } else if (addr_type == BLEProtocol::AddressType::RANDOM_STATIC) {
iv123 0:88b85febcb45 640 // FIXME hci_read_bd_addr and
iv123 0:88b85febcb45 641 // aci_hal_read_config_data CONFIG_DATA_RANDOM_ADDRESS_IDB05A1
iv123 0:88b85febcb45 642 // does not work, use the address stored in class data member
iv123 0:88b85febcb45 643 memcpy(bdaddr, this->bdaddr, sizeof(bdaddr));
iv123 0:88b85febcb45 644 } else {
iv123 0:88b85febcb45 645 // FIXME: should be implemented with privacy features
iv123 0:88b85febcb45 646 // BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE
iv123 0:88b85febcb45 647 // BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE
iv123 0:88b85febcb45 648 return BLE_ERROR_NOT_IMPLEMENTED;
iv123 0:88b85febcb45 649 }
iv123 0:88b85febcb45 650
iv123 0:88b85febcb45 651 *typeP = addr_type;
iv123 0:88b85febcb45 652 memcpy(address, bdaddr, BDADDR_SIZE);
iv123 0:88b85febcb45 653
iv123 0:88b85febcb45 654 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 655 }
iv123 0:88b85febcb45 656
iv123 0:88b85febcb45 657 /**************************************************************************/
iv123 0:88b85febcb45 658 /*!
iv123 0:88b85febcb45 659 @brief obtains preferred connection params
iv123 0:88b85febcb45 660
iv123 0:88b85febcb45 661 @returns ble_error_t
iv123 0:88b85febcb45 662
iv123 0:88b85febcb45 663 @section EXAMPLE
iv123 0:88b85febcb45 664
iv123 0:88b85febcb45 665 @code
iv123 0:88b85febcb45 666
iv123 0:88b85febcb45 667 @endcode
iv123 0:88b85febcb45 668 */
iv123 0:88b85febcb45 669 /**************************************************************************/
iv123 0:88b85febcb45 670 ble_error_t BlueNRGGap::getPreferredConnectionParams(ConnectionParams_t *params)
iv123 0:88b85febcb45 671 {
iv123 0:88b85febcb45 672 static const size_t parameter_size = 2;
iv123 0:88b85febcb45 673
iv123 0:88b85febcb45 674 if (!g_preferred_connection_parameters_char_handle) {
iv123 0:88b85febcb45 675 return BLE_ERROR_OPERATION_NOT_PERMITTED;
iv123 0:88b85febcb45 676 }
iv123 0:88b85febcb45 677
iv123 0:88b85febcb45 678 // Peripheral preferred connection parameters are an array of 4 uint16_t
iv123 0:88b85febcb45 679 uint8_t parameters_packed[parameter_size * 4];
iv123 0:88b85febcb45 680 uint16_t bytes_read = 0;
iv123 0:88b85febcb45 681
iv123 0:88b85febcb45 682 tBleStatus err = aci_gatt_read_handle_value(
iv123 0:88b85febcb45 683 g_preferred_connection_parameters_char_handle + BlueNRGGattServer::CHAR_VALUE_HANDLE,
iv123 0:88b85febcb45 684 sizeof(parameters_packed),
iv123 0:88b85febcb45 685 &bytes_read,
iv123 0:88b85febcb45 686 parameters_packed
iv123 0:88b85febcb45 687 );
iv123 0:88b85febcb45 688
iv123 0:88b85febcb45 689 PRINTF("getPreferredConnectionParams err=0x%02x (bytes_read=%u)\n\r", err, bytes_read);
iv123 0:88b85febcb45 690
iv123 0:88b85febcb45 691 // check that the read succeed and the result have the expected length
iv123 0:88b85febcb45 692 if (err || bytes_read != sizeof(parameters_packed)) {
iv123 0:88b85febcb45 693 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 694 }
iv123 0:88b85febcb45 695
iv123 0:88b85febcb45 696 // memcpy field by field
iv123 0:88b85febcb45 697 memcpy(&params->minConnectionInterval, parameters_packed, parameter_size);
iv123 0:88b85febcb45 698 memcpy(&params->maxConnectionInterval, &parameters_packed[parameter_size], parameter_size);
iv123 0:88b85febcb45 699 memcpy(&params->slaveLatency, &parameters_packed[2 * parameter_size], parameter_size);
iv123 0:88b85febcb45 700 memcpy(&params->connectionSupervisionTimeout, &parameters_packed[3 * parameter_size], parameter_size);
iv123 0:88b85febcb45 701
iv123 0:88b85febcb45 702 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 703 }
iv123 0:88b85febcb45 704
iv123 0:88b85febcb45 705
iv123 0:88b85febcb45 706 /**************************************************************************/
iv123 0:88b85febcb45 707 /*!
iv123 0:88b85febcb45 708 @brief sets preferred connection params
iv123 0:88b85febcb45 709
iv123 0:88b85febcb45 710 @returns ble_error_t
iv123 0:88b85febcb45 711
iv123 0:88b85febcb45 712 @section EXAMPLE
iv123 0:88b85febcb45 713
iv123 0:88b85febcb45 714 @code
iv123 0:88b85febcb45 715
iv123 0:88b85febcb45 716 @endcode
iv123 0:88b85febcb45 717 */
iv123 0:88b85febcb45 718 /**************************************************************************/
iv123 0:88b85febcb45 719 ble_error_t BlueNRGGap::setPreferredConnectionParams(const ConnectionParams_t *params)
iv123 0:88b85febcb45 720 {
iv123 0:88b85febcb45 721 static const size_t parameter_size = 2;
iv123 0:88b85febcb45 722 uint8_t parameters_packed[parameter_size * 4];
iv123 0:88b85febcb45 723
iv123 0:88b85febcb45 724 // ensure that parameters are correct
iv123 0:88b85febcb45 725 // see BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part C]
iv123 0:88b85febcb45 726 // section 12.3 PERIPHERAL PREFERRED CONNECTION PARAMETERS CHARACTERISTIC
iv123 0:88b85febcb45 727 if (((0x0006 > params->minConnectionInterval) || (params->minConnectionInterval > 0x0C80)) &&
iv123 0:88b85febcb45 728 params->minConnectionInterval != 0xFFFF) {
iv123 0:88b85febcb45 729 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 730 }
iv123 0:88b85febcb45 731
iv123 0:88b85febcb45 732 if (((params->minConnectionInterval > params->maxConnectionInterval) || (params->maxConnectionInterval > 0x0C80)) &&
iv123 0:88b85febcb45 733 params->maxConnectionInterval != 0xFFFF) {
iv123 0:88b85febcb45 734 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 735 }
iv123 0:88b85febcb45 736
iv123 0:88b85febcb45 737 if (params->slaveLatency > 0x01F3) {
iv123 0:88b85febcb45 738 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 739 }
iv123 0:88b85febcb45 740
iv123 0:88b85febcb45 741 if (((0x000A > params->connectionSupervisionTimeout) || (params->connectionSupervisionTimeout > 0x0C80)) &&
iv123 0:88b85febcb45 742 params->connectionSupervisionTimeout != 0xFFFF) {
iv123 0:88b85febcb45 743 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 744 }
iv123 0:88b85febcb45 745
iv123 0:88b85febcb45 746 // copy the parameters inside the byte array
iv123 0:88b85febcb45 747 memcpy(parameters_packed, &params->minConnectionInterval, parameter_size);
iv123 0:88b85febcb45 748 memcpy(&parameters_packed[parameter_size], &params->maxConnectionInterval, parameter_size);
iv123 0:88b85febcb45 749 memcpy(&parameters_packed[2 * parameter_size], &params->slaveLatency, parameter_size);
iv123 0:88b85febcb45 750 memcpy(&parameters_packed[3 * parameter_size], &params->connectionSupervisionTimeout, parameter_size);
iv123 0:88b85febcb45 751
iv123 0:88b85febcb45 752 tBleStatus err = aci_gatt_update_char_value(
iv123 0:88b85febcb45 753 g_gap_service_handle,
iv123 0:88b85febcb45 754 g_preferred_connection_parameters_char_handle,
iv123 0:88b85febcb45 755 /* offset */ 0,
iv123 0:88b85febcb45 756 sizeof(parameters_packed),
iv123 0:88b85febcb45 757 parameters_packed
iv123 0:88b85febcb45 758 );
iv123 0:88b85febcb45 759
iv123 0:88b85febcb45 760 if (err) {
iv123 0:88b85febcb45 761 PRINTF("setPreferredConnectionParams failed (err=0x%x)!!\n\r", err) ;
iv123 0:88b85febcb45 762 switch (err) {
iv123 0:88b85febcb45 763 case BLE_STATUS_INVALID_HANDLE:
iv123 0:88b85febcb45 764 case BLE_STATUS_INVALID_PARAMETER:
iv123 0:88b85febcb45 765 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 766 case BLE_STATUS_INSUFFICIENT_RESOURCES:
iv123 0:88b85febcb45 767 return BLE_ERROR_NO_MEM;
iv123 0:88b85febcb45 768 case BLE_STATUS_TIMEOUT:
iv123 0:88b85febcb45 769 return BLE_STACK_BUSY;
iv123 0:88b85febcb45 770 default:
iv123 0:88b85febcb45 771 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 772 }
iv123 0:88b85febcb45 773 }
iv123 0:88b85febcb45 774
iv123 0:88b85febcb45 775 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 776 }
iv123 0:88b85febcb45 777
iv123 0:88b85febcb45 778 /**************************************************************************/
iv123 0:88b85febcb45 779 /*!
iv123 0:88b85febcb45 780 @brief updates preferred connection params
iv123 0:88b85febcb45 781
iv123 0:88b85febcb45 782 @returns ble_error_t
iv123 0:88b85febcb45 783
iv123 0:88b85febcb45 784 @section EXAMPLE
iv123 0:88b85febcb45 785
iv123 0:88b85febcb45 786 @code
iv123 0:88b85febcb45 787
iv123 0:88b85febcb45 788 @endcode
iv123 0:88b85febcb45 789 */
iv123 0:88b85febcb45 790 /**************************************************************************/
iv123 0:88b85febcb45 791 ble_error_t BlueNRGGap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *params)
iv123 0:88b85febcb45 792 {
iv123 0:88b85febcb45 793 tBleStatus ret = BLE_STATUS_SUCCESS;
iv123 0:88b85febcb45 794
iv123 0:88b85febcb45 795 if(gapRole == Gap::CENTRAL) {
iv123 0:88b85febcb45 796 ret = aci_gap_start_connection_update(handle,
iv123 0:88b85febcb45 797 params->minConnectionInterval,
iv123 0:88b85febcb45 798 params->maxConnectionInterval,
iv123 0:88b85febcb45 799 params->slaveLatency,
iv123 0:88b85febcb45 800 params->connectionSupervisionTimeout,
iv123 0:88b85febcb45 801 CONN_L1, CONN_L2);
iv123 0:88b85febcb45 802 } else {
iv123 0:88b85febcb45 803 ret = aci_l2cap_connection_parameter_update_request(handle,
iv123 0:88b85febcb45 804 params->minConnectionInterval,
iv123 0:88b85febcb45 805 params->maxConnectionInterval,
iv123 0:88b85febcb45 806 params->slaveLatency,
iv123 0:88b85febcb45 807 params->connectionSupervisionTimeout);
iv123 0:88b85febcb45 808 }
iv123 0:88b85febcb45 809
iv123 0:88b85febcb45 810 if (BLE_STATUS_SUCCESS != ret){
iv123 0:88b85febcb45 811 PRINTF("updateConnectionParams failed (ret=0x%x)!!\n\r", ret) ;
iv123 0:88b85febcb45 812 switch (ret) {
iv123 0:88b85febcb45 813 case ERR_INVALID_HCI_CMD_PARAMS:
iv123 0:88b85febcb45 814 case BLE_STATUS_INVALID_PARAMETER:
iv123 0:88b85febcb45 815 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 816 case ERR_COMMAND_DISALLOWED:
iv123 0:88b85febcb45 817 case BLE_STATUS_NOT_ALLOWED:
iv123 0:88b85febcb45 818 return BLE_ERROR_OPERATION_NOT_PERMITTED;
iv123 0:88b85febcb45 819 default:
iv123 0:88b85febcb45 820 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 821 }
iv123 0:88b85febcb45 822 }
iv123 0:88b85febcb45 823
iv123 0:88b85febcb45 824 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 825 }
iv123 0:88b85febcb45 826
iv123 0:88b85febcb45 827 /**************************************************************************/
iv123 0:88b85febcb45 828 /*!
iv123 0:88b85febcb45 829 @brief Sets the Device Name Characteristic
iv123 0:88b85febcb45 830
iv123 0:88b85febcb45 831 @param[in] deviceName
iv123 0:88b85febcb45 832 pointer to device name to be set
iv123 0:88b85febcb45 833
iv123 0:88b85febcb45 834 @returns ble_error_t
iv123 0:88b85febcb45 835
iv123 0:88b85febcb45 836 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 837 Everything executed properly
iv123 0:88b85febcb45 838
iv123 0:88b85febcb45 839 @section EXAMPLE
iv123 0:88b85febcb45 840
iv123 0:88b85febcb45 841 @code
iv123 0:88b85febcb45 842
iv123 0:88b85febcb45 843 @endcode
iv123 0:88b85febcb45 844 */
iv123 0:88b85febcb45 845 /**************************************************************************/
iv123 0:88b85febcb45 846 ble_error_t BlueNRGGap::setDeviceName(const uint8_t *deviceName)
iv123 0:88b85febcb45 847 {
iv123 0:88b85febcb45 848 tBleStatus ret;
iv123 0:88b85febcb45 849 uint8_t nameLen = 0;
iv123 0:88b85febcb45 850
iv123 0:88b85febcb45 851 nameLen = strlen((const char*)deviceName);
iv123 0:88b85febcb45 852 PRINTF("DeviceName Size=%d\n\r", nameLen);
iv123 0:88b85febcb45 853
iv123 0:88b85febcb45 854 ret = aci_gatt_update_char_value(g_gap_service_handle,
iv123 0:88b85febcb45 855 g_device_name_char_handle,
iv123 0:88b85febcb45 856 0,
iv123 0:88b85febcb45 857 nameLen,
iv123 0:88b85febcb45 858 deviceName);
iv123 0:88b85febcb45 859
iv123 0:88b85febcb45 860 if (BLE_STATUS_SUCCESS != ret){
iv123 0:88b85febcb45 861 PRINTF("device set name failed (ret=0x%x)!!\n\r", ret) ;
iv123 0:88b85febcb45 862 switch (ret) {
iv123 0:88b85febcb45 863 case BLE_STATUS_INVALID_HANDLE:
iv123 0:88b85febcb45 864 case BLE_STATUS_INVALID_PARAMETER:
iv123 0:88b85febcb45 865 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 866 case BLE_STATUS_INSUFFICIENT_RESOURCES:
iv123 0:88b85febcb45 867 return BLE_ERROR_NO_MEM;
iv123 0:88b85febcb45 868 case BLE_STATUS_TIMEOUT:
iv123 0:88b85febcb45 869 return BLE_STACK_BUSY;
iv123 0:88b85febcb45 870 default:
iv123 0:88b85febcb45 871 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 872 }
iv123 0:88b85febcb45 873 }
iv123 0:88b85febcb45 874
iv123 0:88b85febcb45 875 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 876 }
iv123 0:88b85febcb45 877
iv123 0:88b85febcb45 878 /**************************************************************************/
iv123 0:88b85febcb45 879 /*!
iv123 0:88b85febcb45 880 @brief Gets the Device Name Characteristic
iv123 0:88b85febcb45 881
iv123 0:88b85febcb45 882 @param[in] deviceName
iv123 0:88b85febcb45 883 pointer to device name
iv123 0:88b85febcb45 884
iv123 0:88b85febcb45 885 @param[in] lengthP
iv123 0:88b85febcb45 886 pointer to device name length
iv123 0:88b85febcb45 887
iv123 0:88b85febcb45 888 @returns ble_error_t
iv123 0:88b85febcb45 889
iv123 0:88b85febcb45 890 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 891 Everything executed properly
iv123 0:88b85febcb45 892
iv123 0:88b85febcb45 893 @section EXAMPLE
iv123 0:88b85febcb45 894
iv123 0:88b85febcb45 895 @code
iv123 0:88b85febcb45 896
iv123 0:88b85febcb45 897 @endcode
iv123 0:88b85febcb45 898 */
iv123 0:88b85febcb45 899 /**************************************************************************/
iv123 0:88b85febcb45 900 ble_error_t BlueNRGGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP)
iv123 0:88b85febcb45 901 {
iv123 0:88b85febcb45 902 tBleStatus ret;
iv123 0:88b85febcb45 903
iv123 0:88b85febcb45 904 ret = aci_gatt_read_handle_value(g_device_name_char_handle+BlueNRGGattServer::CHAR_VALUE_HANDLE,
iv123 0:88b85febcb45 905 *lengthP,
iv123 0:88b85febcb45 906 (uint16_t *)lengthP,
iv123 0:88b85febcb45 907 deviceName);
iv123 0:88b85febcb45 908 PRINTF("getDeviceName ret=0x%02x (lengthP=%d)\n\r", ret, *lengthP);
iv123 0:88b85febcb45 909 if (ret == BLE_STATUS_SUCCESS) {
iv123 0:88b85febcb45 910 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 911 } else {
iv123 0:88b85febcb45 912 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 913 }
iv123 0:88b85febcb45 914 }
iv123 0:88b85febcb45 915
iv123 0:88b85febcb45 916 /**************************************************************************/
iv123 0:88b85febcb45 917 /*!
iv123 0:88b85febcb45 918 @brief Sets the Device Appearance Characteristic
iv123 0:88b85febcb45 919
iv123 0:88b85febcb45 920 @param[in] appearance
iv123 0:88b85febcb45 921 device appearance
iv123 0:88b85febcb45 922
iv123 0:88b85febcb45 923 @returns ble_error_t
iv123 0:88b85febcb45 924
iv123 0:88b85febcb45 925 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 926 Everything executed properly
iv123 0:88b85febcb45 927
iv123 0:88b85febcb45 928 @section EXAMPLE
iv123 0:88b85febcb45 929
iv123 0:88b85febcb45 930 @code
iv123 0:88b85febcb45 931
iv123 0:88b85febcb45 932 @endcode
iv123 0:88b85febcb45 933 */
iv123 0:88b85febcb45 934 /**************************************************************************/
iv123 0:88b85febcb45 935 ble_error_t BlueNRGGap::setAppearance(GapAdvertisingData::Appearance appearance)
iv123 0:88b85febcb45 936 {
iv123 0:88b85febcb45 937 tBleStatus ret;
iv123 0:88b85febcb45 938 uint8_t deviceAppearance[2];
iv123 0:88b85febcb45 939
iv123 0:88b85febcb45 940 STORE_LE_16(deviceAppearance, appearance);
iv123 0:88b85febcb45 941 PRINTF("setAppearance= 0x%x 0x%x\n\r", deviceAppearance[1], deviceAppearance[0]);
iv123 0:88b85febcb45 942
iv123 0:88b85febcb45 943 ret = aci_gatt_update_char_value(g_gap_service_handle,
iv123 0:88b85febcb45 944 g_appearance_char_handle,
iv123 0:88b85febcb45 945 0, 2, (uint8_t *)deviceAppearance);
iv123 0:88b85febcb45 946 if (BLE_STATUS_SUCCESS == ret){
iv123 0:88b85febcb45 947 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 948 }
iv123 0:88b85febcb45 949
iv123 0:88b85febcb45 950 PRINTF("setAppearance failed (ret=0x%x)!!\n\r", ret);
iv123 0:88b85febcb45 951 switch (ret) {
iv123 0:88b85febcb45 952 case BLE_STATUS_INVALID_HANDLE:
iv123 0:88b85febcb45 953 case BLE_STATUS_INVALID_PARAMETER:
iv123 0:88b85febcb45 954 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 955 case BLE_STATUS_INSUFFICIENT_RESOURCES:
iv123 0:88b85febcb45 956 return BLE_ERROR_NO_MEM;
iv123 0:88b85febcb45 957 case BLE_STATUS_TIMEOUT:
iv123 0:88b85febcb45 958 return BLE_STACK_BUSY;
iv123 0:88b85febcb45 959 default:
iv123 0:88b85febcb45 960 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 961 }
iv123 0:88b85febcb45 962 }
iv123 0:88b85febcb45 963
iv123 0:88b85febcb45 964 /**************************************************************************/
iv123 0:88b85febcb45 965 /*!
iv123 0:88b85febcb45 966 @brief Gets the Device Appearance Characteristic
iv123 0:88b85febcb45 967
iv123 0:88b85febcb45 968 @param[in] appearance
iv123 0:88b85febcb45 969 pointer to device appearance value
iv123 0:88b85febcb45 970
iv123 0:88b85febcb45 971 @returns ble_error_t
iv123 0:88b85febcb45 972
iv123 0:88b85febcb45 973 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 974 Everything executed properly
iv123 0:88b85febcb45 975
iv123 0:88b85febcb45 976 @section EXAMPLE
iv123 0:88b85febcb45 977
iv123 0:88b85febcb45 978 @code
iv123 0:88b85febcb45 979
iv123 0:88b85febcb45 980 @endcode
iv123 0:88b85febcb45 981 */
iv123 0:88b85febcb45 982 /**************************************************************************/
iv123 0:88b85febcb45 983 ble_error_t BlueNRGGap::getAppearance(GapAdvertisingData::Appearance *appearanceP)
iv123 0:88b85febcb45 984 {
iv123 0:88b85febcb45 985 tBleStatus ret;
iv123 0:88b85febcb45 986 uint16_t lengthP = 2;
iv123 0:88b85febcb45 987
iv123 0:88b85febcb45 988 ret = aci_gatt_read_handle_value(g_appearance_char_handle+BlueNRGGattServer::CHAR_VALUE_HANDLE,
iv123 0:88b85febcb45 989 lengthP,
iv123 0:88b85febcb45 990 &lengthP,
iv123 0:88b85febcb45 991 (uint8_t*)appearanceP);
iv123 0:88b85febcb45 992 PRINTF("getAppearance ret=0x%02x (lengthP=%d)\n\r", ret, lengthP);
iv123 0:88b85febcb45 993 if (ret == BLE_STATUS_SUCCESS) {
iv123 0:88b85febcb45 994 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 995 } else {
iv123 0:88b85febcb45 996 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 997 }
iv123 0:88b85febcb45 998
iv123 0:88b85febcb45 999 }
iv123 0:88b85febcb45 1000
iv123 0:88b85febcb45 1001 GapScanningParams* BlueNRGGap::getScanningParams(void)
iv123 0:88b85febcb45 1002 {
iv123 0:88b85febcb45 1003 return &_scanningParams;
iv123 0:88b85febcb45 1004 }
iv123 0:88b85febcb45 1005
iv123 0:88b85febcb45 1006 static void makeConnection(void)
iv123 0:88b85febcb45 1007 {
iv123 0:88b85febcb45 1008 BlueNRGGap::getInstance().createConnection();
iv123 0:88b85febcb45 1009 }
iv123 0:88b85febcb45 1010
iv123 0:88b85febcb45 1011 void BlueNRGGap::Discovery_CB(Reason_t reason,
iv123 0:88b85febcb45 1012 uint8_t adv_type,
iv123 0:88b85febcb45 1013 uint8_t addr_type,
iv123 0:88b85febcb45 1014 uint8_t *addr,
iv123 0:88b85febcb45 1015 uint8_t *data_length,
iv123 0:88b85febcb45 1016 uint8_t *data,
iv123 0:88b85febcb45 1017 uint8_t *RSSI)
iv123 0:88b85febcb45 1018 {
iv123 0:88b85febcb45 1019 switch (reason) {
iv123 0:88b85febcb45 1020 case DEVICE_FOUND:
iv123 0:88b85febcb45 1021 {
iv123 0:88b85febcb45 1022 GapAdvertisingParams::AdvertisingType_t type;
iv123 0:88b85febcb45 1023 bool isScanResponse = false;
iv123 0:88b85febcb45 1024
iv123 0:88b85febcb45 1025 /*
iv123 0:88b85febcb45 1026 * Whitelisting (scan policy):
iv123 0:88b85febcb45 1027 * SCAN_POLICY_FILTER_ALL_ADV (ADV packets only from devs in the White List) &&
iv123 0:88b85febcb45 1028 * Private Random Address
iv123 0:88b85febcb45 1029 * => scan_results = FALSE
iv123 0:88b85febcb45 1030 * FIXME: the Security Manager should be implemented
iv123 0:88b85febcb45 1031 */
iv123 0:88b85febcb45 1032 ScanningPolicyMode_t mode = getScanningPolicyMode();
iv123 0:88b85febcb45 1033 PRINTF("mode=%u addr_type=%u\n\r", mode, addr_type);
iv123 0:88b85febcb45 1034 if(mode == Gap::SCAN_POLICY_FILTER_ALL_ADV ||
iv123 0:88b85febcb45 1035 (addr_type == RESOLVABLE_PRIVATE_ADDR ||
iv123 0:88b85febcb45 1036 addr_type == NON_RESOLVABLE_PRIVATE_ADDR)) {
iv123 0:88b85febcb45 1037 return;
iv123 0:88b85febcb45 1038 }
iv123 0:88b85febcb45 1039
iv123 0:88b85febcb45 1040 switch(adv_type) {
iv123 0:88b85febcb45 1041 case ADV_IND:
iv123 0:88b85febcb45 1042 type = GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED;
iv123 0:88b85febcb45 1043 break;
iv123 0:88b85febcb45 1044 case ADV_DIRECT_IND:
iv123 0:88b85febcb45 1045 type = GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED;
iv123 0:88b85febcb45 1046 break;
iv123 0:88b85febcb45 1047 case ADV_SCAN_IND:
iv123 0:88b85febcb45 1048 case SCAN_RSP:
iv123 0:88b85febcb45 1049 type = GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED;
iv123 0:88b85febcb45 1050 isScanResponse = true;
iv123 0:88b85febcb45 1051 break;
iv123 0:88b85febcb45 1052 case ADV_NONCONN_IND:
iv123 0:88b85febcb45 1053 type = GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED;
iv123 0:88b85febcb45 1054 break;
iv123 0:88b85febcb45 1055 default:
iv123 0:88b85febcb45 1056 type = GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED;
iv123 0:88b85febcb45 1057 }
iv123 0:88b85febcb45 1058
iv123 0:88b85febcb45 1059 PRINTF("data_length=%d adv peerAddr[%02x %02x %02x %02x %02x %02x] \r\n",
iv123 0:88b85febcb45 1060 *data_length, addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
iv123 0:88b85febcb45 1061 if(!_connecting) {
iv123 0:88b85febcb45 1062 processAdvertisementReport(addr, *RSSI, isScanResponse, type, *data_length, data);
iv123 0:88b85febcb45 1063 }
iv123 0:88b85febcb45 1064 PRINTF("!!!After processAdvertisementReport\n\r");
iv123 0:88b85febcb45 1065 }
iv123 0:88b85febcb45 1066 break;
iv123 0:88b85febcb45 1067
iv123 0:88b85febcb45 1068 case DISCOVERY_COMPLETE:
iv123 0:88b85febcb45 1069 // The discovery is complete. If this is due to a stop scanning (i.e., the device
iv123 0:88b85febcb45 1070 // we are interested in has been found) and a connection has been requested
iv123 0:88b85febcb45 1071 // then we start the device connection.
iv123 0:88b85febcb45 1072 PRINTF("DISCOVERY_COMPLETE\n\r");
iv123 0:88b85febcb45 1073 _scanning = false;
iv123 0:88b85febcb45 1074
iv123 0:88b85febcb45 1075 // Since the DISCOVERY_COMPLETE event can be received during the scanning interval,
iv123 0:88b85febcb45 1076 // we need to delay the starting of connection
iv123 0:88b85febcb45 1077 uint16_t delay = 2*(_scanningParams.getInterval());
iv123 0:88b85febcb45 1078
iv123 0:88b85febcb45 1079 #ifdef AST_FOR_MBED_OS
iv123 0:88b85febcb45 1080 if(_connecting) {
iv123 0:88b85febcb45 1081 minar::Scheduler::postCallback(makeConnection).delay(minar::milliseconds(delay));
iv123 0:88b85febcb45 1082 }
iv123 0:88b85febcb45 1083 #else
iv123 0:88b85febcb45 1084 Clock_Wait(delay);
iv123 0:88b85febcb45 1085 if(_connecting) {
iv123 0:88b85febcb45 1086 makeConnection();
iv123 0:88b85febcb45 1087 }
iv123 0:88b85febcb45 1088 #endif /* AST_FOR_MBED_OS */
iv123 0:88b85febcb45 1089
iv123 0:88b85febcb45 1090 break;
iv123 0:88b85febcb45 1091 }
iv123 0:88b85febcb45 1092 }
iv123 0:88b85febcb45 1093
iv123 0:88b85febcb45 1094 ble_error_t BlueNRGGap::startRadioScan(const GapScanningParams &scanningParams)
iv123 0:88b85febcb45 1095 {
iv123 0:88b85febcb45 1096
iv123 0:88b85febcb45 1097 tBleStatus ret = BLE_STATUS_SUCCESS;
iv123 0:88b85febcb45 1098
iv123 0:88b85febcb45 1099 // Stop ADV before scanning
iv123 0:88b85febcb45 1100 /*
iv123 0:88b85febcb45 1101 if (state.advertising == 1) {
iv123 0:88b85febcb45 1102 stopAdvertising();
iv123 0:88b85febcb45 1103 }
iv123 0:88b85febcb45 1104 */
iv123 0:88b85febcb45 1105
iv123 0:88b85febcb45 1106 /*
iv123 0:88b85febcb45 1107 * Whitelisting (scan policy):
iv123 0:88b85febcb45 1108 * SCAN_POLICY_FILTER_ALL_ADV (ADV packets only from devs in the White List) &&
iv123 0:88b85febcb45 1109 * White List is empty
iv123 0:88b85febcb45 1110 * => scan operation = FAILURE
iv123 0:88b85febcb45 1111 * FIXME: the Security Manager should be implemented
iv123 0:88b85febcb45 1112 */
iv123 0:88b85febcb45 1113 ScanningPolicyMode_t mode = getScanningPolicyMode();
iv123 0:88b85febcb45 1114 uint8_t whiteListSize = whitelistAddresses.size;
iv123 0:88b85febcb45 1115 if(whiteListSize == 0 && mode == Gap::SCAN_POLICY_FILTER_ALL_ADV) {
iv123 0:88b85febcb45 1116 return BLE_ERROR_OPERATION_NOT_PERMITTED;
iv123 0:88b85febcb45 1117 }
iv123 0:88b85febcb45 1118
iv123 0:88b85febcb45 1119 ret = btleStartRadioScan(scanningParams.getActiveScanning(),
iv123 0:88b85febcb45 1120 scanningParams.getInterval(),
iv123 0:88b85febcb45 1121 scanningParams.getWindow(),
iv123 0:88b85febcb45 1122 addr_type);
iv123 0:88b85febcb45 1123
iv123 0:88b85febcb45 1124 PRINTF("Scanning...\n\r");
iv123 0:88b85febcb45 1125 PRINTF("scanningParams.getInterval()=%u[msec]\r\n",(scanningParams.getInterval()*625)/1000);
iv123 0:88b85febcb45 1126 PRINTF("scanningParams.getWindow()=%u[msec]\r\n",(scanningParams.getWindow()*625)/1000);
iv123 0:88b85febcb45 1127 //PRINTF("_advParams.getInterval()=%u\r\n",_advParams.getInterval());
iv123 0:88b85febcb45 1128 //PRINTF("CONN_P1=%u\r\n",(unsigned)CONN_P1);
iv123 0:88b85febcb45 1129 //PRINTF("CONN_P2=%u\r\n",(unsigned)CONN_P2);
iv123 0:88b85febcb45 1130 if (BLE_STATUS_SUCCESS == ret){
iv123 0:88b85febcb45 1131 PRINTF("Observation Procedure Started\n");
iv123 0:88b85febcb45 1132 _scanning = true;
iv123 0:88b85febcb45 1133 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 1134 }
iv123 0:88b85febcb45 1135
iv123 0:88b85febcb45 1136 // Observer role is not supported by X-NUCLEO-IDB04A1, return BLE_ERROR_NOT_IMPLEMENTED
iv123 0:88b85febcb45 1137 switch (ret) {
iv123 0:88b85febcb45 1138 case BLE_STATUS_INVALID_CID:
iv123 0:88b85febcb45 1139 PRINTF("Observation Procedure not implemented!!!\n\r");
iv123 0:88b85febcb45 1140 return BLE_ERROR_NOT_IMPLEMENTED;
iv123 0:88b85febcb45 1141 default:
iv123 0:88b85febcb45 1142 PRINTF("Observation Procedure failed (0x%02X)\n\r", ret);
iv123 0:88b85febcb45 1143 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 1144 }
iv123 0:88b85febcb45 1145
iv123 0:88b85febcb45 1146 }
iv123 0:88b85febcb45 1147
iv123 0:88b85febcb45 1148 ble_error_t BlueNRGGap::stopScan() {
iv123 0:88b85febcb45 1149 tBleStatus ret = BLE_STATUS_SUCCESS;
iv123 0:88b85febcb45 1150
iv123 0:88b85febcb45 1151 PRINTF("stopScan\n\r");
iv123 0:88b85febcb45 1152 ret = aci_gap_terminate_gap_procedure(GAP_OBSERVATION_PROC);
iv123 0:88b85febcb45 1153
iv123 0:88b85febcb45 1154 if (ret != BLE_STATUS_SUCCESS) {
iv123 0:88b85febcb45 1155 PRINTF("GAP Terminate Gap Procedure failed(ret=0x%x)\n", ret);
iv123 0:88b85febcb45 1156 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 1157 } else {
iv123 0:88b85febcb45 1158 PRINTF("Discovery Procedure Terminated\n");
iv123 0:88b85febcb45 1159 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 1160 }
iv123 0:88b85febcb45 1161 }
iv123 0:88b85febcb45 1162
iv123 0:88b85febcb45 1163 /**************************************************************************/
iv123 0:88b85febcb45 1164 /*!
iv123 0:88b85febcb45 1165 @brief set Tx power level
iv123 0:88b85febcb45 1166 @param[in] txPower Transmission Power level
iv123 0:88b85febcb45 1167 @returns ble_error_t
iv123 0:88b85febcb45 1168 */
iv123 0:88b85febcb45 1169 /**************************************************************************/
iv123 0:88b85febcb45 1170 ble_error_t BlueNRGGap::setTxPower(int8_t txPower)
iv123 0:88b85febcb45 1171 {
iv123 0:88b85febcb45 1172 tBleStatus ret;
iv123 0:88b85febcb45 1173
iv123 0:88b85febcb45 1174 int8_t enHighPower = 0;
iv123 0:88b85febcb45 1175 int8_t paLevel = 0;
iv123 0:88b85febcb45 1176
iv123 0:88b85febcb45 1177 ret = getHighPowerAndPALevelValue(txPower, enHighPower, paLevel);
iv123 0:88b85febcb45 1178 if(ret!=BLE_STATUS_SUCCESS) {
iv123 0:88b85febcb45 1179 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 1180 }
iv123 0:88b85febcb45 1181
iv123 0:88b85febcb45 1182 PRINTF("enHighPower=%d, paLevel=%d\n\r", enHighPower, paLevel);
iv123 0:88b85febcb45 1183 ret = aci_hal_set_tx_power_level(enHighPower, paLevel);
iv123 0:88b85febcb45 1184 if(ret!=BLE_STATUS_SUCCESS) {
iv123 0:88b85febcb45 1185 return BLE_ERROR_PARAM_OUT_OF_RANGE;
iv123 0:88b85febcb45 1186 }
iv123 0:88b85febcb45 1187
iv123 0:88b85febcb45 1188 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 1189 }
iv123 0:88b85febcb45 1190
iv123 0:88b85febcb45 1191 /**************************************************************************/
iv123 0:88b85febcb45 1192 /*!
iv123 0:88b85febcb45 1193 @brief get permitted Tx power values
iv123 0:88b85febcb45 1194 @param[in] values pointer to pointer to permitted power values
iv123 0:88b85febcb45 1195 @param[in] num number of values
iv123 0:88b85febcb45 1196 */
iv123 0:88b85febcb45 1197 /**************************************************************************/
iv123 0:88b85febcb45 1198 void BlueNRGGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP) {
iv123 0:88b85febcb45 1199 static const int8_t permittedTxValues[] = {
iv123 0:88b85febcb45 1200 -18, -15, -14, -12, -11, -9, -8, -6, -5, -2, 0, 2, 4, 5, 8
iv123 0:88b85febcb45 1201 };
iv123 0:88b85febcb45 1202
iv123 0:88b85febcb45 1203 *valueArrayPP = permittedTxValues;
iv123 0:88b85febcb45 1204 *countP = sizeof(permittedTxValues) / sizeof(int8_t);
iv123 0:88b85febcb45 1205 }
iv123 0:88b85febcb45 1206
iv123 0:88b85febcb45 1207 /**************************************************************************/
iv123 0:88b85febcb45 1208 /*!
iv123 0:88b85febcb45 1209 @brief Set advertising parameters according to the current state
iv123 0:88b85febcb45 1210 Parameters value is set taking into account guidelines of the BlueNRG
iv123 0:88b85febcb45 1211 time slots allocation
iv123 0:88b85febcb45 1212 */
iv123 0:88b85febcb45 1213 /**************************************************************************/
iv123 0:88b85febcb45 1214 void BlueNRGGap::setAdvParameters(void)
iv123 0:88b85febcb45 1215 {
iv123 0:88b85febcb45 1216 uint32_t advIntMS;
iv123 0:88b85febcb45 1217
iv123 0:88b85febcb45 1218 if(state.connected == 1) {
iv123 0:88b85febcb45 1219 advIntMS = (conn_min_interval*1.25)-GUARD_INT;
iv123 0:88b85febcb45 1220 advInterval = _advParams.MSEC_TO_ADVERTISEMENT_DURATION_UNITS(advIntMS);
iv123 0:88b85febcb45 1221
iv123 0:88b85febcb45 1222 PRINTF("conn_min_interval is equal to %u\r\n", conn_min_interval);
iv123 0:88b85febcb45 1223 } else {
iv123 0:88b85febcb45 1224 advInterval = _advParams.getIntervalInADVUnits();
iv123 0:88b85febcb45 1225 }
iv123 0:88b85febcb45 1226 }
iv123 0:88b85febcb45 1227
iv123 0:88b85febcb45 1228 /**************************************************************************/
iv123 0:88b85febcb45 1229 /*!
iv123 0:88b85febcb45 1230 @brief Set connection parameters according to the current state (ADV and/or SCAN)
iv123 0:88b85febcb45 1231 Parameters value is set taking into account guidelines of the BlueNRG
iv123 0:88b85febcb45 1232 time slots allocation
iv123 0:88b85febcb45 1233 */
iv123 0:88b85febcb45 1234 /**************************************************************************/
iv123 0:88b85febcb45 1235 void BlueNRGGap::setConnectionParameters(void)
iv123 0:88b85febcb45 1236 {
iv123 0:88b85febcb45 1237 if (state.advertising == 1) {
iv123 0:88b85febcb45 1238
iv123 0:88b85febcb45 1239 if (_scanningParams.getInterval() < advInterval) {
iv123 0:88b85febcb45 1240 PRINTF("state.adv=1 scanInterval<advInterval\r\n");
iv123 0:88b85febcb45 1241 scanInterval = advInterval;
iv123 0:88b85febcb45 1242 scanWindow = advInterval;
iv123 0:88b85febcb45 1243 } else {
iv123 0:88b85febcb45 1244 PRINTF("state.adv=1 scanInterval>=advInterval\r\n");
iv123 0:88b85febcb45 1245 scanInterval = _scanningParams.getInterval();
iv123 0:88b85febcb45 1246 scanWindow = _scanningParams.getWindow();
iv123 0:88b85febcb45 1247 }
iv123 0:88b85febcb45 1248
iv123 0:88b85febcb45 1249 if(advInterval>(MAX_INT_CONN-(GUARD_INT/1.25))) { //(4000-GUARD_INT)ms
iv123 0:88b85febcb45 1250 conn_min_interval = MAX_INT_CONN;
iv123 0:88b85febcb45 1251 conn_max_interval = MAX_INT_CONN;
iv123 0:88b85febcb45 1252 } else {
iv123 0:88b85febcb45 1253 conn_min_interval = (_advParams.ADVERTISEMENT_DURATION_UNITS_TO_MS(advInterval)+GUARD_INT)/1.25;
iv123 0:88b85febcb45 1254 conn_max_interval = (_advParams.ADVERTISEMENT_DURATION_UNITS_TO_MS(advInterval)+GUARD_INT)/1.25;
iv123 0:88b85febcb45 1255 }
iv123 0:88b85febcb45 1256
iv123 0:88b85febcb45 1257 } else {
iv123 0:88b85febcb45 1258
iv123 0:88b85febcb45 1259 PRINTF("state.adv = 0\r\n");
iv123 0:88b85febcb45 1260
iv123 0:88b85febcb45 1261 scanInterval = _scanningParams.getInterval();
iv123 0:88b85febcb45 1262 scanWindow = _scanningParams.getWindow();
iv123 0:88b85febcb45 1263 if(SCAN_DURATION_UNITS_TO_MSEC(scanInterval)>(MAX_INT_CONN*1.25) ||
iv123 0:88b85febcb45 1264 SCAN_DURATION_UNITS_TO_MSEC(scanInterval)<(MIN_INT_CONN*1.25)) { //(4000)ms || (7.5)ms
iv123 0:88b85febcb45 1265 conn_min_interval = DEF_INT_CONN;
iv123 0:88b85febcb45 1266 conn_max_interval = DEF_INT_CONN;
iv123 0:88b85febcb45 1267 } else {
iv123 0:88b85febcb45 1268 conn_min_interval = SCAN_DURATION_UNITS_TO_MSEC(scanInterval)/1.25;
iv123 0:88b85febcb45 1269 conn_max_interval = SCAN_DURATION_UNITS_TO_MSEC(scanInterval)/1.25;
iv123 0:88b85febcb45 1270 }
iv123 0:88b85febcb45 1271 }
iv123 0:88b85febcb45 1272 PRINTF("scanInterval=%u[msec]\r\n",SCAN_DURATION_UNITS_TO_MSEC(scanInterval));
iv123 0:88b85febcb45 1273 PRINTF("scanWindow()=%u[msec]\r\n",SCAN_DURATION_UNITS_TO_MSEC(scanWindow));
iv123 0:88b85febcb45 1274 PRINTF("conn_min_interval=%u[msec]\r\n",(unsigned)(conn_min_interval*1.25));
iv123 0:88b85febcb45 1275 PRINTF("conn_max_interval=%u[msec]\r\n",(unsigned)(conn_max_interval*1.25));
iv123 0:88b85febcb45 1276
iv123 0:88b85febcb45 1277 }
iv123 0:88b85febcb45 1278
iv123 0:88b85febcb45 1279 ble_error_t BlueNRGGap::createConnection ()
iv123 0:88b85febcb45 1280 {
iv123 0:88b85febcb45 1281 tBleStatus ret;
iv123 0:88b85febcb45 1282
iv123 0:88b85febcb45 1283 /*
iv123 0:88b85febcb45 1284 Before creating connection, set parameters according
iv123 0:88b85febcb45 1285 to previous or current procedure (ADV and/or SCAN)
iv123 0:88b85febcb45 1286 */
iv123 0:88b85febcb45 1287 setConnectionParameters();
iv123 0:88b85febcb45 1288
iv123 0:88b85febcb45 1289 /*
iv123 0:88b85febcb45 1290 Scan_Interval, Scan_Window, Peer_Address_Type, Peer_Address, Own_Address_Type, Conn_Interval_Min,
iv123 0:88b85febcb45 1291 Conn_Interval_Max, Conn_Latency, Supervision_Timeout, Conn_Len_Min, Conn_Len_Max
iv123 0:88b85febcb45 1292 */
iv123 0:88b85febcb45 1293 ret = aci_gap_create_connection(scanInterval,
iv123 0:88b85febcb45 1294 scanWindow,
iv123 0:88b85febcb45 1295 _peerAddrType,
iv123 0:88b85febcb45 1296 (unsigned char*)_peerAddr,
iv123 0:88b85febcb45 1297 addr_type,
iv123 0:88b85febcb45 1298 conn_min_interval, conn_max_interval, 0,
iv123 0:88b85febcb45 1299 SUPERV_TIMEOUT, CONN_L1, CONN_L1);
iv123 0:88b85febcb45 1300
iv123 0:88b85febcb45 1301 //_connecting = false;
iv123 0:88b85febcb45 1302
iv123 0:88b85febcb45 1303 if (ret != BLE_STATUS_SUCCESS) {
iv123 0:88b85febcb45 1304 PRINTF("Error while starting connection (ret=0x%02X).\n\r", ret);
iv123 0:88b85febcb45 1305 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 1306 } else {
iv123 0:88b85febcb45 1307 PRINTF("Connection started.\n");
iv123 0:88b85febcb45 1308 _connecting = false;
iv123 0:88b85febcb45 1309 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 1310 }
iv123 0:88b85febcb45 1311 }
iv123 0:88b85febcb45 1312
iv123 0:88b85febcb45 1313 ble_error_t BlueNRGGap::connect (const Gap::Address_t peerAddr,
iv123 0:88b85febcb45 1314 Gap::AddressType_t peerAddrType,
iv123 0:88b85febcb45 1315 const ConnectionParams_t *connectionParams,
iv123 0:88b85febcb45 1316 const GapScanningParams *scanParams)
iv123 0:88b85febcb45 1317 {
iv123 0:88b85febcb45 1318 /* avoid compiler warnings about unused variables */
iv123 0:88b85febcb45 1319 (void)connectionParams;
iv123 0:88b85febcb45 1320
iv123 0:88b85febcb45 1321 setScanParams(scanParams->getInterval(),
iv123 0:88b85febcb45 1322 scanParams->getWindow(),
iv123 0:88b85febcb45 1323 scanParams->getTimeout(),
iv123 0:88b85febcb45 1324 scanParams->getActiveScanning()
iv123 0:88b85febcb45 1325 );
iv123 0:88b85febcb45 1326
iv123 0:88b85febcb45 1327 // Save the peer address
iv123 0:88b85febcb45 1328 for(int i=0; i<BDADDR_SIZE; i++) {
iv123 0:88b85febcb45 1329 _peerAddr[i] = peerAddr[i];
iv123 0:88b85febcb45 1330 }
iv123 0:88b85febcb45 1331 _peerAddrType = peerAddrType;
iv123 0:88b85febcb45 1332
iv123 0:88b85febcb45 1333 _connecting = true;
iv123 0:88b85febcb45 1334
iv123 0:88b85febcb45 1335 if(_scanning) {
iv123 0:88b85febcb45 1336 stopScan();
iv123 0:88b85febcb45 1337 } else {
iv123 0:88b85febcb45 1338 PRINTF("Calling createConnection from connect()\n\r");
iv123 0:88b85febcb45 1339 return createConnection();
iv123 0:88b85febcb45 1340 }
iv123 0:88b85febcb45 1341
iv123 0:88b85febcb45 1342 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 1343 }
iv123 0:88b85febcb45 1344
iv123 0:88b85febcb45 1345 /**************************************************************************/
iv123 0:88b85febcb45 1346 /*!
iv123 0:88b85febcb45 1347 @brief Set the advertising policy filter mode that will be used in
iv123 0:88b85febcb45 1348 the next call to startAdvertising().
iv123 0:88b85febcb45 1349
iv123 0:88b85febcb45 1350 @returns \ref ble_errror_t
iv123 0:88b85febcb45 1351
iv123 0:88b85febcb45 1352 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 1353 Everything executed properly.
iv123 0:88b85febcb45 1354
iv123 0:88b85febcb45 1355 BLE_ERROR_NOT_IMPLEMENTED
iv123 0:88b85febcb45 1356 This feature is currently note implemented.
iv123 0:88b85febcb45 1357 */
iv123 0:88b85febcb45 1358 /**************************************************************************/
iv123 0:88b85febcb45 1359 ble_error_t BlueNRGGap::setAdvertisingPolicyMode(Gap::AdvertisingPolicyMode_t mode)
iv123 0:88b85febcb45 1360 {
iv123 0:88b85febcb45 1361 advertisingPolicyMode = mode;
iv123 0:88b85febcb45 1362
iv123 0:88b85febcb45 1363 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 1364 }
iv123 0:88b85febcb45 1365
iv123 0:88b85febcb45 1366 /**************************************************************************/
iv123 0:88b85febcb45 1367 /*!
iv123 0:88b85febcb45 1368 @brief Set the scanning policy filter mode that will be used in
iv123 0:88b85febcb45 1369 the next call to startAdvertising().
iv123 0:88b85febcb45 1370
iv123 0:88b85febcb45 1371 @returns \ref ble_errror_t
iv123 0:88b85febcb45 1372
iv123 0:88b85febcb45 1373 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 1374 Everything executed properly.
iv123 0:88b85febcb45 1375
iv123 0:88b85febcb45 1376 BLE_ERROR_NOT_IMPLEMENTED
iv123 0:88b85febcb45 1377 This feature is currently note implemented.
iv123 0:88b85febcb45 1378 */
iv123 0:88b85febcb45 1379 /**************************************************************************/
iv123 0:88b85febcb45 1380 ble_error_t BlueNRGGap::setScanningPolicyMode(Gap::ScanningPolicyMode_t mode)
iv123 0:88b85febcb45 1381 {
iv123 0:88b85febcb45 1382 scanningPolicyMode = mode;
iv123 0:88b85febcb45 1383
iv123 0:88b85febcb45 1384 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 1385 }
iv123 0:88b85febcb45 1386
iv123 0:88b85febcb45 1387 /**************************************************************************/
iv123 0:88b85febcb45 1388 /*!
iv123 0:88b85febcb45 1389 @brief Get the current advertising policy filter mode.
iv123 0:88b85febcb45 1390
iv123 0:88b85febcb45 1391 @returns The advertising policy filter mode.
iv123 0:88b85febcb45 1392 */
iv123 0:88b85febcb45 1393 /**************************************************************************/
iv123 0:88b85febcb45 1394 Gap::AdvertisingPolicyMode_t BlueNRGGap::getAdvertisingPolicyMode(void) const
iv123 0:88b85febcb45 1395 {
iv123 0:88b85febcb45 1396 return advertisingPolicyMode;
iv123 0:88b85febcb45 1397 }
iv123 0:88b85febcb45 1398
iv123 0:88b85febcb45 1399 /**************************************************************************/
iv123 0:88b85febcb45 1400 /*!
iv123 0:88b85febcb45 1401 @brief Get the current scanning policy filter mode.
iv123 0:88b85febcb45 1402
iv123 0:88b85febcb45 1403 @returns The scanning policy filter mode.
iv123 0:88b85febcb45 1404
iv123 0:88b85febcb45 1405 */
iv123 0:88b85febcb45 1406 /**************************************************************************/
iv123 0:88b85febcb45 1407 Gap::ScanningPolicyMode_t BlueNRGGap::getScanningPolicyMode(void) const
iv123 0:88b85febcb45 1408 {
iv123 0:88b85febcb45 1409 return scanningPolicyMode;
iv123 0:88b85febcb45 1410 }
iv123 0:88b85febcb45 1411
iv123 0:88b85febcb45 1412 /**************************************************************************/
iv123 0:88b85febcb45 1413 /*!
iv123 0:88b85febcb45 1414 @brief Clear BlueNRGGap's state.
iv123 0:88b85febcb45 1415
iv123 0:88b85febcb45 1416 @returns ble_error_t
iv123 0:88b85febcb45 1417
iv123 0:88b85febcb45 1418 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 1419 Everything executed properly
iv123 0:88b85febcb45 1420 */
iv123 0:88b85febcb45 1421 /**************************************************************************/
iv123 0:88b85febcb45 1422 ble_error_t BlueNRGGap::reset(void)
iv123 0:88b85febcb45 1423 {
iv123 0:88b85febcb45 1424 /* Clear all state that is from the parent, including private members */
iv123 0:88b85febcb45 1425 if (Gap::reset() != BLE_ERROR_NONE) {
iv123 0:88b85febcb45 1426 return BLE_ERROR_INVALID_STATE;
iv123 0:88b85febcb45 1427 }
iv123 0:88b85febcb45 1428
iv123 0:88b85febcb45 1429 /* Clear derived class members */
iv123 0:88b85febcb45 1430 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
iv123 0:88b85febcb45 1431
iv123 0:88b85febcb45 1432 /* Set the whitelist policy filter modes to IGNORE_WHITELIST */
iv123 0:88b85febcb45 1433 advertisingPolicyMode = Gap::ADV_POLICY_IGNORE_WHITELIST;
iv123 0:88b85febcb45 1434 scanningPolicyMode = Gap::SCAN_POLICY_IGNORE_WHITELIST;
iv123 0:88b85febcb45 1435
iv123 0:88b85febcb45 1436 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 1437 }
iv123 0:88b85febcb45 1438
iv123 0:88b85febcb45 1439 void BlueNRGGap::setConnectionInterval(uint16_t interval) {
iv123 0:88b85febcb45 1440 conn_min_interval = interval;
iv123 0:88b85febcb45 1441 conn_max_interval = interval;
iv123 0:88b85febcb45 1442 }
iv123 0:88b85febcb45 1443
iv123 0:88b85febcb45 1444 void BlueNRGGap::setGapRole(Role_t role)
iv123 0:88b85febcb45 1445 {
iv123 0:88b85febcb45 1446 gapRole = role;
iv123 0:88b85febcb45 1447 }