nrf52-dk

Committer:
mamont090671
Date:
Fri Jun 05 10:49:10 2020 +0000
Revision:
0:70ce6da7a141
nrf52840

Who changed what in which revision?

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