BLE test

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Vincent Coubard
Date:
Thu Sep 15 10:51:34 2016 +0100
Branch:
83c30f290087a6f5a503812f507492e725a3b717
Revision:
272:4639da7705e1
Parent:
270:ca649990a830
Child:
276:a20f4cad1d61
Sync with 83c30f290087a6f5a503812f507492e725a3b717

2016-07-18 18:14:06+02:00: Andrea Palmieri
Implement updateConnectionParams

Signed-off-by: Andrea Palmieri <andrea.palmieri@st.com>

Who changed what in which revision?

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