BLE test

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Vincent Coubard
Date:
Thu Sep 15 10:51:32 2016 +0100
Branch:
9bb31e3d89ab51aca24fe8b1162853fbc70fe791
Revision:
270:ca649990a830
Parent:
268:c0a1e03c5736
Child:
272:4639da7705e1
Sync with 9bb31e3d89ab51aca24fe8b1162853fbc70fe791

2016-07-13 15:02:10+02:00: Andrea Palmieri
Clean implementation of setAdvertisingData() and startAdvertising()

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 {
Andrea Palmieri 142:adf1567d3900 768 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 769 (void) handle;
Andrea Palmieri 142:adf1567d3900 770 (void)params;
Andrea Palmieri 142:adf1567d3900 771
Wolfgang Betz 130:770ce14d3d15 772 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 773 }
Wolfgang Betz 130:770ce14d3d15 774
Wolfgang Betz 130:770ce14d3d15 775 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 776 /*!
Vincent Coubard 255:97adb68decd4 777 @brief Sets the Device Name Characteristic
Wolfgang Betz 130:770ce14d3d15 778
Wolfgang Betz 130:770ce14d3d15 779 @param[in] deviceName
Wolfgang Betz 130:770ce14d3d15 780 pointer to device name to be set
Wolfgang Betz 130:770ce14d3d15 781
Wolfgang Betz 130:770ce14d3d15 782 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 783
Wolfgang Betz 130:770ce14d3d15 784 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 785 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 786
Wolfgang Betz 130:770ce14d3d15 787 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 788
Wolfgang Betz 130:770ce14d3d15 789 @code
Wolfgang Betz 130:770ce14d3d15 790
Wolfgang Betz 130:770ce14d3d15 791 @endcode
Wolfgang Betz 130:770ce14d3d15 792 */
Wolfgang Betz 130:770ce14d3d15 793 /**************************************************************************/
Vincent Coubard 255:97adb68decd4 794 ble_error_t BlueNRGGap::setDeviceName(const uint8_t *deviceName)
Wolfgang Betz 130:770ce14d3d15 795 {
Andrea Palmieri 187:2e517d3ddd71 796 tBleStatus ret;
Vincent Coubard 255:97adb68decd4 797 uint8_t nameLen = 0;
Vincent Coubard 255:97adb68decd4 798
Andrea Palmieri 228:25368f053411 799 nameLen = strlen((const char*)deviceName);
Andrea Palmieri 228:25368f053411 800 PRINTF("DeviceName Size=%d\n\r", nameLen);
Andrea Palmieri 228:25368f053411 801
Andrea Palmieri 222:37a7e289ee3d 802 ret = aci_gatt_update_char_value(g_gap_service_handle,
Andrea Palmieri 222:37a7e289ee3d 803 g_device_name_char_handle,
Andrea Palmieri 222:37a7e289ee3d 804 0,
Andrea Palmieri 222:37a7e289ee3d 805 nameLen,
Andrea Palmieri 228:25368f053411 806 deviceName);
Andrea Palmieri 187:2e517d3ddd71 807
Andrea Palmieri 187:2e517d3ddd71 808 if (BLE_STATUS_SUCCESS != ret){
Andrea Palmieri 187:2e517d3ddd71 809 PRINTF("device set name failed (ret=0x%x)!!\n\r", ret) ;
Andrea Palmieri 187:2e517d3ddd71 810 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 811 case BLE_STATUS_INVALID_HANDLE:
Andrea Palmieri 187:2e517d3ddd71 812 case BLE_STATUS_INVALID_PARAMETER:
Andrea Palmieri 187:2e517d3ddd71 813 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 187:2e517d3ddd71 814 case BLE_STATUS_INSUFFICIENT_RESOURCES:
Andrea Palmieri 187:2e517d3ddd71 815 return BLE_ERROR_NO_MEM;
Andrea Palmieri 187:2e517d3ddd71 816 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 817 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 818 default:
Andrea Palmieri 187:2e517d3ddd71 819 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 820 }
Wolfgang Betz 130:770ce14d3d15 821 }
Wolfgang Betz 130:770ce14d3d15 822
Wolfgang Betz 130:770ce14d3d15 823 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 824 }
Wolfgang Betz 130:770ce14d3d15 825
Wolfgang Betz 130:770ce14d3d15 826 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 827 /*!
Vincent Coubard 255:97adb68decd4 828 @brief Gets the Device Name Characteristic
Wolfgang Betz 130:770ce14d3d15 829
Wolfgang Betz 130:770ce14d3d15 830 @param[in] deviceName
Vincent Coubard 255:97adb68decd4 831 pointer to device name
Wolfgang Betz 130:770ce14d3d15 832
Wolfgang Betz 130:770ce14d3d15 833 @param[in] lengthP
Vincent Coubard 255:97adb68decd4 834 pointer to device name length
Wolfgang Betz 130:770ce14d3d15 835
Wolfgang Betz 130:770ce14d3d15 836 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 837
Wolfgang Betz 130:770ce14d3d15 838 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 839 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 840
Wolfgang Betz 130:770ce14d3d15 841 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 842
Wolfgang Betz 130:770ce14d3d15 843 @code
Wolfgang Betz 130:770ce14d3d15 844
Wolfgang Betz 130:770ce14d3d15 845 @endcode
Wolfgang Betz 130:770ce14d3d15 846 */
Wolfgang Betz 130:770ce14d3d15 847 /**************************************************************************/
Andrea Palmieri 228:25368f053411 848 ble_error_t BlueNRGGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP)
Andrea Palmieri 228:25368f053411 849 {
Andrea Palmieri 228:25368f053411 850 tBleStatus ret;
Andrea Palmieri 228:25368f053411 851
Andrea Palmieri 229:9981f62cdb1a 852 ret = aci_gatt_read_handle_value(g_device_name_char_handle+BlueNRGGattServer::CHAR_VALUE_HANDLE,
Andrea Palmieri 228:25368f053411 853 *lengthP,
Andrea Palmieri 228:25368f053411 854 (uint16_t *)lengthP,
Andrea Palmieri 228:25368f053411 855 deviceName);
Andrea Palmieri 228:25368f053411 856 PRINTF("getDeviceName ret=0x%02x (lengthP=%d)\n\r", ret, *lengthP);
Andrea Palmieri 228:25368f053411 857 if (ret == BLE_STATUS_SUCCESS) {
Andrea Palmieri 228:25368f053411 858 return BLE_ERROR_NONE;
Andrea Palmieri 228:25368f053411 859 } else {
Andrea Palmieri 228:25368f053411 860 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Andrea Palmieri 228:25368f053411 861 }
Wolfgang Betz 130:770ce14d3d15 862 }
Wolfgang Betz 130:770ce14d3d15 863
Wolfgang Betz 130:770ce14d3d15 864 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 865 /*!
Vincent Coubard 255:97adb68decd4 866 @brief Sets the Device Appearance Characteristic
Wolfgang Betz 130:770ce14d3d15 867
Wolfgang Betz 130:770ce14d3d15 868 @param[in] appearance
Vincent Coubard 255:97adb68decd4 869 device appearance
Wolfgang Betz 130:770ce14d3d15 870
Wolfgang Betz 130:770ce14d3d15 871 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 872
Wolfgang Betz 130:770ce14d3d15 873 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 874 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 875
Wolfgang Betz 130:770ce14d3d15 876 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 877
Wolfgang Betz 130:770ce14d3d15 878 @code
Wolfgang Betz 130:770ce14d3d15 879
Wolfgang Betz 130:770ce14d3d15 880 @endcode
Wolfgang Betz 130:770ce14d3d15 881 */
Wolfgang Betz 130:770ce14d3d15 882 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 883 ble_error_t BlueNRGGap::setAppearance(GapAdvertisingData::Appearance appearance)
Wolfgang Betz 130:770ce14d3d15 884 {
Andrea Palmieri 187:2e517d3ddd71 885 tBleStatus ret;
Andrea Palmieri 228:25368f053411 886 uint8_t deviceAppearance[2];
Andrea Palmieri 187:2e517d3ddd71 887
Vincent Coubard 255:97adb68decd4 888 STORE_LE_16(deviceAppearance, appearance);
Vincent Coubard 253:9665a6f8bbdb 889 PRINTF("setAppearance= 0x%x 0x%x\n\r", deviceAppearance[1], deviceAppearance[0]);
Vincent Coubard 255:97adb68decd4 890
Andrea Palmieri 228:25368f053411 891 ret = aci_gatt_update_char_value(g_gap_service_handle,
Andrea Palmieri 228:25368f053411 892 g_appearance_char_handle,
Andrea Palmieri 228:25368f053411 893 0, 2, (uint8_t *)deviceAppearance);
Andrea Palmieri 187:2e517d3ddd71 894 if (BLE_STATUS_SUCCESS == ret){
Andrea Palmieri 187:2e517d3ddd71 895 return BLE_ERROR_NONE;
Andrea Palmieri 187:2e517d3ddd71 896 }
Andrea Palmieri 187:2e517d3ddd71 897
Andrea Palmieri 217:23870c55b07e 898 PRINTF("setAppearance failed (ret=0x%x)!!\n\r", ret);
Andrea Palmieri 187:2e517d3ddd71 899 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 900 case BLE_STATUS_INVALID_HANDLE:
Andrea Palmieri 187:2e517d3ddd71 901 case BLE_STATUS_INVALID_PARAMETER:
Andrea Palmieri 187:2e517d3ddd71 902 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 187:2e517d3ddd71 903 case BLE_STATUS_INSUFFICIENT_RESOURCES:
Andrea Palmieri 187:2e517d3ddd71 904 return BLE_ERROR_NO_MEM;
Andrea Palmieri 187:2e517d3ddd71 905 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 906 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 907 default:
Andrea Palmieri 187:2e517d3ddd71 908 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 909 }
Wolfgang Betz 130:770ce14d3d15 910 }
Wolfgang Betz 130:770ce14d3d15 911
Wolfgang Betz 130:770ce14d3d15 912 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 913 /*!
Wolfgang Betz 130:770ce14d3d15 914 @brief Gets the Device Appearance Characteristic
Wolfgang Betz 130:770ce14d3d15 915
Wolfgang Betz 130:770ce14d3d15 916 @param[in] appearance
Vincent Coubard 255:97adb68decd4 917 pointer to device appearance value
Wolfgang Betz 130:770ce14d3d15 918
Wolfgang Betz 130:770ce14d3d15 919 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 920
Wolfgang Betz 130:770ce14d3d15 921 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 922 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 923
Wolfgang Betz 130:770ce14d3d15 924 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 925
Wolfgang Betz 130:770ce14d3d15 926 @code
Wolfgang Betz 130:770ce14d3d15 927
Wolfgang Betz 130:770ce14d3d15 928 @endcode
Wolfgang Betz 130:770ce14d3d15 929 */
Wolfgang Betz 130:770ce14d3d15 930 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 931 ble_error_t BlueNRGGap::getAppearance(GapAdvertisingData::Appearance *appearanceP)
Wolfgang Betz 130:770ce14d3d15 932 {
Andrea Palmieri 228:25368f053411 933 tBleStatus ret;
Andrea Palmieri 228:25368f053411 934 uint16_t lengthP = 2;
Andrea Palmieri 228:25368f053411 935
Andrea Palmieri 229:9981f62cdb1a 936 ret = aci_gatt_read_handle_value(g_appearance_char_handle+BlueNRGGattServer::CHAR_VALUE_HANDLE,
Andrea Palmieri 228:25368f053411 937 lengthP,
Andrea Palmieri 228:25368f053411 938 &lengthP,
Andrea Palmieri 228:25368f053411 939 (uint8_t*)appearanceP);
Andrea Palmieri 228:25368f053411 940 PRINTF("getAppearance ret=0x%02x (lengthP=%d)\n\r", ret, lengthP);
Andrea Palmieri 228:25368f053411 941 if (ret == BLE_STATUS_SUCCESS) {
Andrea Palmieri 228:25368f053411 942 return BLE_ERROR_NONE;
Andrea Palmieri 228:25368f053411 943 } else {
Andrea Palmieri 228:25368f053411 944 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Andrea Palmieri 228:25368f053411 945 }
Andrea Palmieri 228:25368f053411 946
Wolfgang Betz 130:770ce14d3d15 947 }
Wolfgang Betz 130:770ce14d3d15 948
Andrea Palmieri 160:174387d53809 949 GapScanningParams* BlueNRGGap::getScanningParams(void)
Andrea Palmieri 160:174387d53809 950 {
Andrea Palmieri 160:174387d53809 951 return &_scanningParams;
Andrea Palmieri 160:174387d53809 952 }
Andrea Palmieri 160:174387d53809 953
Wolfgang Betz 165:3576598c0889 954 static void makeConnection(void)
Wolfgang Betz 162:667f97a4db7f 955 {
Wolfgang Betz 165:3576598c0889 956 BlueNRGGap::getInstance().createConnection();
Wolfgang Betz 162:667f97a4db7f 957 }
Wolfgang Betz 162:667f97a4db7f 958
Wolfgang Betz 132:51056160fa4a 959 void BlueNRGGap::Discovery_CB(Reason_t reason,
Wolfgang Betz 132:51056160fa4a 960 uint8_t adv_type,
Andrea Palmieri 229:9981f62cdb1a 961 uint8_t addr_type,
Wolfgang Betz 132:51056160fa4a 962 uint8_t *addr,
Wolfgang Betz 132:51056160fa4a 963 uint8_t *data_length,
Wolfgang Betz 132:51056160fa4a 964 uint8_t *data,
Wolfgang Betz 132:51056160fa4a 965 uint8_t *RSSI)
Wolfgang Betz 132:51056160fa4a 966 {
Wolfgang Betz 132:51056160fa4a 967 switch (reason) {
Wolfgang Betz 132:51056160fa4a 968 case DEVICE_FOUND:
Wolfgang Betz 132:51056160fa4a 969 {
Wolfgang Betz 132:51056160fa4a 970 GapAdvertisingParams::AdvertisingType_t type;
Wolfgang Betz 132:51056160fa4a 971 bool isScanResponse = false;
Andrea Palmieri 229:9981f62cdb1a 972
Andrea Palmieri 229:9981f62cdb1a 973 /*
Andrea Palmieri 229:9981f62cdb1a 974 * Whitelisting (scan policy):
Andrea Palmieri 229:9981f62cdb1a 975 * SCAN_POLICY_FILTER_ALL_ADV (ADV packets only from devs in the White List) &&
Andrea Palmieri 229:9981f62cdb1a 976 * Private Random Address
Andrea Palmieri 229:9981f62cdb1a 977 * => scan_results = FALSE
Andrea Palmieri 229:9981f62cdb1a 978 * FIXME: the Security Manager should be implemented
Andrea Palmieri 229:9981f62cdb1a 979 */
Andrea Palmieri 229:9981f62cdb1a 980 ScanningPolicyMode_t mode = getScanningPolicyMode();
Andrea Palmieri 229:9981f62cdb1a 981 PRINTF("mode=%u addr_type=%u\n\r", mode, addr_type);
Andrea Palmieri 229:9981f62cdb1a 982 if(mode == Gap::SCAN_POLICY_FILTER_ALL_ADV ||
Andrea Palmieri 229:9981f62cdb1a 983 (addr_type == RESOLVABLE_PRIVATE_ADDR ||
Andrea Palmieri 229:9981f62cdb1a 984 addr_type == NON_RESOLVABLE_PRIVATE_ADDR)) {
Andrea Palmieri 229:9981f62cdb1a 985 return;
Andrea Palmieri 229:9981f62cdb1a 986 }
Andrea Palmieri 229:9981f62cdb1a 987
Wolfgang Betz 132:51056160fa4a 988 switch(adv_type) {
Wolfgang Betz 132:51056160fa4a 989 case ADV_IND:
Wolfgang Betz 132:51056160fa4a 990 type = GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED;
Wolfgang Betz 132:51056160fa4a 991 break;
Wolfgang Betz 132:51056160fa4a 992 case ADV_DIRECT_IND:
Wolfgang Betz 132:51056160fa4a 993 type = GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED;
Wolfgang Betz 132:51056160fa4a 994 break;
Wolfgang Betz 132:51056160fa4a 995 case ADV_SCAN_IND:
Wolfgang Betz 132:51056160fa4a 996 case SCAN_RSP:
Wolfgang Betz 132:51056160fa4a 997 type = GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED;
Wolfgang Betz 132:51056160fa4a 998 isScanResponse = true;
Wolfgang Betz 132:51056160fa4a 999 break;
Wolfgang Betz 132:51056160fa4a 1000 case ADV_NONCONN_IND:
Wolfgang Betz 132:51056160fa4a 1001 type = GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED;
Andrea Palmieri 142:adf1567d3900 1002 break;
Andrea Palmieri 142:adf1567d3900 1003 default:
Andrea Palmieri 142:adf1567d3900 1004 type = GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED;
Wolfgang Betz 132:51056160fa4a 1005 }
Vincent Coubard 255:97adb68decd4 1006
Andrea Palmieri 217:23870c55b07e 1007 PRINTF("data_length=%d adv peerAddr[%02x %02x %02x %02x %02x %02x] \r\n",
Andrea Palmieri 217:23870c55b07e 1008 *data_length, addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
Andrea Palmieri 238:b6454c839f42 1009 if(!_connecting) {
Andrea Palmieri 238:b6454c839f42 1010 processAdvertisementReport(addr, *RSSI, isScanResponse, type, *data_length, data);
Andrea Palmieri 238:b6454c839f42 1011 }
Andrea Palmieri 160:174387d53809 1012 PRINTF("!!!After processAdvertisementReport\n\r");
Wolfgang Betz 132:51056160fa4a 1013 }
Wolfgang Betz 132:51056160fa4a 1014 break;
Vincent Coubard 255:97adb68decd4 1015
Wolfgang Betz 132:51056160fa4a 1016 case DISCOVERY_COMPLETE:
Wolfgang Betz 132:51056160fa4a 1017 // The discovery is complete. If this is due to a stop scanning (i.e., the device
Wolfgang Betz 132:51056160fa4a 1018 // we are interested in has been found) and a connection has been requested
Andrea Palmieri 229:9981f62cdb1a 1019 // then we start the device connection.
Andrea Palmieri 145:024a552e4715 1020 PRINTF("DISCOVERY_COMPLETE\n\r");
Wolfgang Betz 132:51056160fa4a 1021 _scanning = false;
Wolfgang Betz 132:51056160fa4a 1022
Wolfgang Betz 165:3576598c0889 1023 // Since the DISCOVERY_COMPLETE event can be received during the scanning interval,
Andrea Palmieri 229:9981f62cdb1a 1024 // we need to delay the starting of connection
Wolfgang Betz 165:3576598c0889 1025 uint16_t delay = 2*(_scanningParams.getInterval());
Wolfgang Betz 165:3576598c0889 1026
Andrea Palmieri 217:23870c55b07e 1027 #ifdef AST_FOR_MBED_OS
Wolfgang Betz 162:667f97a4db7f 1028 if(_connecting) {
Wolfgang Betz 165:3576598c0889 1029 minar::Scheduler::postCallback(makeConnection).delay(minar::milliseconds(delay));
Wolfgang Betz 132:51056160fa4a 1030 }
Andrea Palmieri 217:23870c55b07e 1031 #else
Andrea Palmieri 217:23870c55b07e 1032 Clock_Wait(delay);
Andrea Palmieri 217:23870c55b07e 1033 if(_connecting) {
Andrea Palmieri 217:23870c55b07e 1034 makeConnection();
Andrea Palmieri 217:23870c55b07e 1035 }
Andrea Palmieri 217:23870c55b07e 1036 #endif /* AST_FOR_MBED_OS */
Wolfgang Betz 132:51056160fa4a 1037
Wolfgang Betz 132:51056160fa4a 1038 break;
Wolfgang Betz 132:51056160fa4a 1039 }
Wolfgang Betz 132:51056160fa4a 1040 }
Wolfgang Betz 132:51056160fa4a 1041
Wolfgang Betz 132:51056160fa4a 1042 ble_error_t BlueNRGGap::startRadioScan(const GapScanningParams &scanningParams)
Wolfgang Betz 132:51056160fa4a 1043 {
Vincent Coubard 255:97adb68decd4 1044
Wolfgang Betz 165:3576598c0889 1045 tBleStatus ret = BLE_STATUS_SUCCESS;
Andrea Palmieri 222:37a7e289ee3d 1046
Andrea Palmieri 222:37a7e289ee3d 1047 // Stop ADV before scanning
Andrea Palmieri 223:36df860f7ffc 1048 /*
Andrea Palmieri 222:37a7e289ee3d 1049 if (state.advertising == 1) {
Andrea Palmieri 222:37a7e289ee3d 1050 stopAdvertising();
Andrea Palmieri 222:37a7e289ee3d 1051 }
Andrea Palmieri 223:36df860f7ffc 1052 */
Andrea Palmieri 222:37a7e289ee3d 1053
Andrea Palmieri 229:9981f62cdb1a 1054 /*
Andrea Palmieri 229:9981f62cdb1a 1055 * Whitelisting (scan policy):
Andrea Palmieri 229:9981f62cdb1a 1056 * SCAN_POLICY_FILTER_ALL_ADV (ADV packets only from devs in the White List) &&
Andrea Palmieri 229:9981f62cdb1a 1057 * White List is empty
Andrea Palmieri 229:9981f62cdb1a 1058 * => scan operation = FAILURE
Andrea Palmieri 229:9981f62cdb1a 1059 * FIXME: the Security Manager should be implemented
Andrea Palmieri 229:9981f62cdb1a 1060 */
Andrea Palmieri 229:9981f62cdb1a 1061 ScanningPolicyMode_t mode = getScanningPolicyMode();
Andrea Palmieri 229:9981f62cdb1a 1062 uint8_t whiteListSize = whitelistAddresses.size;
Andrea Palmieri 229:9981f62cdb1a 1063 if(whiteListSize == 0 && mode == Gap::SCAN_POLICY_FILTER_ALL_ADV) {
Andrea Palmieri 229:9981f62cdb1a 1064 return BLE_ERROR_OPERATION_NOT_PERMITTED;
Andrea Palmieri 229:9981f62cdb1a 1065 }
Andrea Palmieri 229:9981f62cdb1a 1066
Andrea Palmieri 217:23870c55b07e 1067 ret = btleStartRadioScan(scanningParams.getActiveScanning(),
Andrea Palmieri 217:23870c55b07e 1068 scanningParams.getInterval(),
Andrea Palmieri 217:23870c55b07e 1069 scanningParams.getWindow(),
Andrea Palmieri 217:23870c55b07e 1070 addr_type);
Andrea Palmieri 217:23870c55b07e 1071
Andrea Palmieri 229:9981f62cdb1a 1072 PRINTF("Scanning...\n\r");
Andrea Palmieri 229:9981f62cdb1a 1073 PRINTF("scanningParams.getInterval()=%u[msec]\r\n",(scanningParams.getInterval()*625)/1000);
Andrea Palmieri 229:9981f62cdb1a 1074 PRINTF("scanningParams.getWindow()=%u[msec]\r\n",(scanningParams.getWindow()*625)/1000);
Andrea Palmieri 229:9981f62cdb1a 1075 //PRINTF("_advParams.getInterval()=%u\r\n",_advParams.getInterval());
Andrea Palmieri 229:9981f62cdb1a 1076 //PRINTF("CONN_P1=%u\r\n",(unsigned)CONN_P1);
Andrea Palmieri 229:9981f62cdb1a 1077 //PRINTF("CONN_P2=%u\r\n",(unsigned)CONN_P2);
Andrea Palmieri 217:23870c55b07e 1078 if (BLE_STATUS_SUCCESS == ret){
Andrea Palmieri 217:23870c55b07e 1079 PRINTF("Observation Procedure Started\n");
Andrea Palmieri 217:23870c55b07e 1080 _scanning = true;
Andrea Palmieri 217:23870c55b07e 1081 return BLE_ERROR_NONE;
Andrea Palmieri 145:024a552e4715 1082 }
Andrea Palmieri 217:23870c55b07e 1083
Andrea Palmieri 217:23870c55b07e 1084 // Observer role is not supported by X-NUCLEO-IDB04A1, return BLE_ERROR_NOT_IMPLEMENTED
Andrea Palmieri 217:23870c55b07e 1085 switch (ret) {
Andrea Palmieri 217:23870c55b07e 1086 case BLE_STATUS_INVALID_CID:
Andrea Palmieri 229:9981f62cdb1a 1087 PRINTF("Observation Procedure not implemented!!!\n\r");
Andrea Palmieri 217:23870c55b07e 1088 return BLE_ERROR_NOT_IMPLEMENTED;
Andrea Palmieri 217:23870c55b07e 1089 default:
Andrea Palmieri 229:9981f62cdb1a 1090 PRINTF("Observation Procedure failed (0x%02X)\n\r", ret);
Andrea Palmieri 217:23870c55b07e 1091 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 132:51056160fa4a 1092 }
Andrea Palmieri 217:23870c55b07e 1093
Wolfgang Betz 130:770ce14d3d15 1094 }
Wolfgang Betz 130:770ce14d3d15 1095
Wolfgang Betz 130:770ce14d3d15 1096 ble_error_t BlueNRGGap::stopScan() {
Wolfgang Betz 165:3576598c0889 1097 tBleStatus ret = BLE_STATUS_SUCCESS;
Andrea Palmieri 229:9981f62cdb1a 1098
Andrea Palmieri 229:9981f62cdb1a 1099 PRINTF("stopScan\n\r");
Andrea Palmieri 217:23870c55b07e 1100 ret = aci_gap_terminate_gap_procedure(GAP_OBSERVATION_PROC);
Vincent Coubard 255:97adb68decd4 1101
Wolfgang Betz 132:51056160fa4a 1102 if (ret != BLE_STATUS_SUCCESS) {
Andrea Palmieri 238:b6454c839f42 1103 PRINTF("GAP Terminate Gap Procedure failed(ret=0x%x)\n", ret);
Vincent Coubard 255:97adb68decd4 1104 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 132:51056160fa4a 1105 } else {
Wolfgang Betz 132:51056160fa4a 1106 PRINTF("Discovery Procedure Terminated\n");
Vincent Coubard 255:97adb68decd4 1107 return BLE_ERROR_NONE;
Wolfgang Betz 132:51056160fa4a 1108 }
Wolfgang Betz 130:770ce14d3d15 1109 }
Wolfgang Betz 130:770ce14d3d15 1110
Wolfgang Betz 130:770ce14d3d15 1111 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1112 /*!
Wolfgang Betz 130:770ce14d3d15 1113 @brief set Tx power level
Wolfgang Betz 130:770ce14d3d15 1114 @param[in] txPower Transmission Power level
Wolfgang Betz 130:770ce14d3d15 1115 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 1116 */
Wolfgang Betz 130:770ce14d3d15 1117 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1118 ble_error_t BlueNRGGap::setTxPower(int8_t txPower)
Wolfgang Betz 130:770ce14d3d15 1119 {
Wolfgang Betz 130:770ce14d3d15 1120 tBleStatus ret;
Vincent Coubard 255:97adb68decd4 1121
Wolfgang Betz 130:770ce14d3d15 1122 int8_t enHighPower = 0;
Andrea Palmieri 201:a9bc2e8b01d7 1123 int8_t paLevel = 0;
Andrea Palmieri 201:a9bc2e8b01d7 1124
Andrea Palmieri 228:25368f053411 1125 ret = getHighPowerAndPALevelValue(txPower, enHighPower, paLevel);
Andrea Palmieri 228:25368f053411 1126 if(ret!=BLE_STATUS_SUCCESS) {
Andrea Palmieri 228:25368f053411 1127 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Andrea Palmieri 228:25368f053411 1128 }
Andrea Palmieri 201:a9bc2e8b01d7 1129
Vincent Coubard 255:97adb68decd4 1130 PRINTF("enHighPower=%d, paLevel=%d\n\r", enHighPower, paLevel);
Wolfgang Betz 130:770ce14d3d15 1131 ret = aci_hal_set_tx_power_level(enHighPower, paLevel);
Wolfgang Betz 130:770ce14d3d15 1132 if(ret!=BLE_STATUS_SUCCESS) {
Andrea Palmieri 228:25368f053411 1133 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 1134 }
Andrea Palmieri 201:a9bc2e8b01d7 1135
Wolfgang Betz 130:770ce14d3d15 1136 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 1137 }
Wolfgang Betz 130:770ce14d3d15 1138
Wolfgang Betz 130:770ce14d3d15 1139 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1140 /*!
Wolfgang Betz 130:770ce14d3d15 1141 @brief get permitted Tx power values
Wolfgang Betz 130:770ce14d3d15 1142 @param[in] values pointer to pointer to permitted power values
Vincent Coubard 255:97adb68decd4 1143 @param[in] num number of values
Wolfgang Betz 130:770ce14d3d15 1144 */
Wolfgang Betz 130:770ce14d3d15 1145 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1146 void BlueNRGGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP) {
Wolfgang Betz 130:770ce14d3d15 1147 static const int8_t permittedTxValues[] = {
Andrea Palmieri 229:9981f62cdb1a 1148 -18, -15, -14, -12, -11, -9, -8, -6, -5, -2, 0, 2, 4, 5, 8
Wolfgang Betz 130:770ce14d3d15 1149 };
Wolfgang Betz 130:770ce14d3d15 1150
Wolfgang Betz 130:770ce14d3d15 1151 *valueArrayPP = permittedTxValues;
Wolfgang Betz 130:770ce14d3d15 1152 *countP = sizeof(permittedTxValues) / sizeof(int8_t);
Wolfgang Betz 130:770ce14d3d15 1153 }
Wolfgang Betz 132:51056160fa4a 1154
Andrea Palmieri 229:9981f62cdb1a 1155 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1156 /*!
Andrea Palmieri 232:aead10288880 1157 @brief Set advertising parameters according to the current state
Andrea Palmieri 232:aead10288880 1158 Parameters value is set taking into account guidelines of the BlueNRG
Andrea Palmieri 232:aead10288880 1159 time slots allocation
Andrea Palmieri 232:aead10288880 1160 */
Andrea Palmieri 232:aead10288880 1161 /**************************************************************************/
Andrea Palmieri 232:aead10288880 1162 void BlueNRGGap::setAdvParameters(void)
Andrea Palmieri 232:aead10288880 1163 {
Andrea Palmieri 232:aead10288880 1164 uint32_t advIntMS;
Andrea Palmieri 232:aead10288880 1165
Andrea Palmieri 232:aead10288880 1166 if(state.connected == 1) {
Andrea Palmieri 232:aead10288880 1167 advIntMS = (conn_min_interval*1.25)-GUARD_INT;
Andrea Palmieri 232:aead10288880 1168 advInterval = _advParams.MSEC_TO_ADVERTISEMENT_DURATION_UNITS(advIntMS);
Vincent Coubard 256:146bb175ce80 1169
Vincent Coubard 259:323f588e5f57 1170 PRINTF("conn_min_interval is equal to %u\r\n", conn_min_interval);
Andrea Palmieri 232:aead10288880 1171 } else {
Andrea Palmieri 232:aead10288880 1172 advInterval = _advParams.getIntervalInADVUnits();
Andrea Palmieri 232:aead10288880 1173 }
Andrea Palmieri 232:aead10288880 1174 }
Andrea Palmieri 232:aead10288880 1175
Andrea Palmieri 232:aead10288880 1176 /**************************************************************************/
Andrea Palmieri 232:aead10288880 1177 /*!
Andrea Palmieri 229:9981f62cdb1a 1178 @brief Set connection parameters according to the current state (ADV and/or SCAN)
Andrea Palmieri 229:9981f62cdb1a 1179 Parameters value is set taking into account guidelines of the BlueNRG
Andrea Palmieri 229:9981f62cdb1a 1180 time slots allocation
Andrea Palmieri 229:9981f62cdb1a 1181 */
Andrea Palmieri 229:9981f62cdb1a 1182 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1183 void BlueNRGGap::setConnectionParameters(void)
Andrea Palmieri 229:9981f62cdb1a 1184 {
Andrea Palmieri 229:9981f62cdb1a 1185 if (state.advertising == 1) {
Andrea Palmieri 229:9981f62cdb1a 1186
Andrea Palmieri 232:aead10288880 1187 if (_scanningParams.getInterval() < advInterval) {
Andrea Palmieri 232:aead10288880 1188 PRINTF("state.adv=1 scanInterval<advInterval\r\n");
Andrea Palmieri 232:aead10288880 1189 scanInterval = advInterval;
Andrea Palmieri 232:aead10288880 1190 scanWindow = advInterval;
Andrea Palmieri 229:9981f62cdb1a 1191 } else {
Andrea Palmieri 232:aead10288880 1192 PRINTF("state.adv=1 scanInterval>=advInterval\r\n");
Andrea Palmieri 229:9981f62cdb1a 1193 scanInterval = _scanningParams.getInterval();
Andrea Palmieri 229:9981f62cdb1a 1194 scanWindow = _scanningParams.getWindow();
Andrea Palmieri 229:9981f62cdb1a 1195 }
Andrea Palmieri 234:3f6bba52b307 1196
Andrea Palmieri 234:3f6bba52b307 1197 if(advInterval>(MAX_INT_CONN-(GUARD_INT/1.25))) { //(4000-GUARD_INT)ms
Andrea Palmieri 234:3f6bba52b307 1198 conn_min_interval = MAX_INT_CONN;
Andrea Palmieri 234:3f6bba52b307 1199 conn_max_interval = MAX_INT_CONN;
Andrea Palmieri 234:3f6bba52b307 1200 } else {
Andrea Palmieri 234:3f6bba52b307 1201 conn_min_interval = (_advParams.ADVERTISEMENT_DURATION_UNITS_TO_MS(advInterval)+GUARD_INT)/1.25;
Andrea Palmieri 234:3f6bba52b307 1202 conn_max_interval = (_advParams.ADVERTISEMENT_DURATION_UNITS_TO_MS(advInterval)+GUARD_INT)/1.25;
Andrea Palmieri 234:3f6bba52b307 1203 }
Andrea Palmieri 229:9981f62cdb1a 1204
Andrea Palmieri 229:9981f62cdb1a 1205 } else {
Andrea Palmieri 229:9981f62cdb1a 1206
Andrea Palmieri 232:aead10288880 1207 PRINTF("state.adv = 0\r\n");
Andrea Palmieri 232:aead10288880 1208
Andrea Palmieri 229:9981f62cdb1a 1209 scanInterval = _scanningParams.getInterval();
Andrea Palmieri 229:9981f62cdb1a 1210 scanWindow = _scanningParams.getWindow();
Andrea Palmieri 234:3f6bba52b307 1211 if(SCAN_DURATION_UNITS_TO_MSEC(scanInterval)>(MAX_INT_CONN*1.25) ||
Andrea Palmieri 234:3f6bba52b307 1212 SCAN_DURATION_UNITS_TO_MSEC(scanInterval)<(MIN_INT_CONN*1.25)) { //(4000)ms || (7.5)ms
Andrea Palmieri 234:3f6bba52b307 1213 conn_min_interval = DEF_INT_CONN;
Andrea Palmieri 234:3f6bba52b307 1214 conn_max_interval = DEF_INT_CONN;
Andrea Palmieri 234:3f6bba52b307 1215 } else {
Andrea Palmieri 234:3f6bba52b307 1216 conn_min_interval = SCAN_DURATION_UNITS_TO_MSEC(scanInterval)/1.25;
Andrea Palmieri 234:3f6bba52b307 1217 conn_max_interval = SCAN_DURATION_UNITS_TO_MSEC(scanInterval)/1.25;
Andrea Palmieri 234:3f6bba52b307 1218 }
Andrea Palmieri 229:9981f62cdb1a 1219 }
Andrea Palmieri 229:9981f62cdb1a 1220 PRINTF("scanInterval=%u[msec]\r\n",SCAN_DURATION_UNITS_TO_MSEC(scanInterval));
Andrea Palmieri 229:9981f62cdb1a 1221 PRINTF("scanWindow()=%u[msec]\r\n",SCAN_DURATION_UNITS_TO_MSEC(scanWindow));
Andrea Palmieri 234:3f6bba52b307 1222 PRINTF("conn_min_interval=%u[msec]\r\n",(unsigned)(conn_min_interval*1.25));
Andrea Palmieri 234:3f6bba52b307 1223 PRINTF("conn_max_interval=%u[msec]\r\n",(unsigned)(conn_max_interval*1.25));
Andrea Palmieri 229:9981f62cdb1a 1224
Andrea Palmieri 229:9981f62cdb1a 1225 }
Andrea Palmieri 229:9981f62cdb1a 1226
Andrea Palmieri 181:749071129d2e 1227 ble_error_t BlueNRGGap::createConnection ()
Wolfgang Betz 132:51056160fa4a 1228 {
Wolfgang Betz 132:51056160fa4a 1229 tBleStatus ret;
Andrea Palmieri 229:9981f62cdb1a 1230
Andrea Palmieri 229:9981f62cdb1a 1231 /*
Andrea Palmieri 229:9981f62cdb1a 1232 Before creating connection, set parameters according
Andrea Palmieri 229:9981f62cdb1a 1233 to previous or current procedure (ADV and/or SCAN)
Andrea Palmieri 229:9981f62cdb1a 1234 */
Andrea Palmieri 229:9981f62cdb1a 1235 setConnectionParameters();
Andrea Palmieri 229:9981f62cdb1a 1236
Wolfgang Betz 132:51056160fa4a 1237 /*
Vincent Coubard 255:97adb68decd4 1238 Scan_Interval, Scan_Window, Peer_Address_Type, Peer_Address, Own_Address_Type, Conn_Interval_Min,
Vincent Coubard 255:97adb68decd4 1239 Conn_Interval_Max, Conn_Latency, Supervision_Timeout, Conn_Len_Min, Conn_Len_Max
Wolfgang Betz 132:51056160fa4a 1240 */
Andrea Palmieri 229:9981f62cdb1a 1241 ret = aci_gap_create_connection(scanInterval,
Andrea Palmieri 229:9981f62cdb1a 1242 scanWindow,
Andrea Palmieri 229:9981f62cdb1a 1243 _peerAddrType,
Wolfgang Betz 163:4c008b1089e9 1244 (unsigned char*)_peerAddr,
Andrea Palmieri 229:9981f62cdb1a 1245 addr_type,
Andrea Palmieri 229:9981f62cdb1a 1246 conn_min_interval, conn_max_interval, 0,
Andrea Palmieri 238:b6454c839f42 1247 SUPERV_TIMEOUT, CONN_L1, CONN_L1);
Wolfgang Betz 165:3576598c0889 1248
Andrea Palmieri 238:b6454c839f42 1249 //_connecting = false;
Vincent Coubard 255:97adb68decd4 1250
Wolfgang Betz 163:4c008b1089e9 1251 if (ret != BLE_STATUS_SUCCESS) {
Andrea Palmieri 229:9981f62cdb1a 1252 PRINTF("Error while starting connection (ret=0x%02X).\n\r", ret);
Wolfgang Betz 132:51056160fa4a 1253 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 132:51056160fa4a 1254 } else {
Wolfgang Betz 132:51056160fa4a 1255 PRINTF("Connection started.\n");
Andrea Palmieri 238:b6454c839f42 1256 _connecting = false;
Wolfgang Betz 132:51056160fa4a 1257 return BLE_ERROR_NONE;
Wolfgang Betz 132:51056160fa4a 1258 }
Wolfgang Betz 132:51056160fa4a 1259 }
Wolfgang Betz 132:51056160fa4a 1260
Wolfgang Betz 132:51056160fa4a 1261 ble_error_t BlueNRGGap::connect (const Gap::Address_t peerAddr,
Wolfgang Betz 132:51056160fa4a 1262 Gap::AddressType_t peerAddrType,
Wolfgang Betz 132:51056160fa4a 1263 const ConnectionParams_t *connectionParams,
Wolfgang Betz 132:51056160fa4a 1264 const GapScanningParams *scanParams)
Wolfgang Betz 132:51056160fa4a 1265 {
Andrea Palmieri 142:adf1567d3900 1266 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 1267 (void)connectionParams;
Andrea Palmieri 142:adf1567d3900 1268 (void)scanParams;
Andrea Palmieri 142:adf1567d3900 1269
Andrea Palmieri 217:23870c55b07e 1270 // Save the peer address
Wolfgang Betz 132:51056160fa4a 1271 for(int i=0; i<BDADDR_SIZE; i++) {
Wolfgang Betz 132:51056160fa4a 1272 _peerAddr[i] = peerAddr[i];
Wolfgang Betz 132:51056160fa4a 1273 }
Andrea Palmieri 229:9981f62cdb1a 1274 _peerAddrType = peerAddrType;
Wolfgang Betz 132:51056160fa4a 1275
Andrea Palmieri 181:749071129d2e 1276 _connecting = true;
Wolfgang Betz 132:51056160fa4a 1277
Wolfgang Betz 132:51056160fa4a 1278 if(_scanning) {
Wolfgang Betz 132:51056160fa4a 1279 stopScan();
Wolfgang Betz 132:51056160fa4a 1280 } else {
Wolfgang Betz 165:3576598c0889 1281 PRINTF("Calling createConnection from connect()\n\r");
Wolfgang Betz 165:3576598c0889 1282 return createConnection();
Wolfgang Betz 132:51056160fa4a 1283 }
Vincent Coubard 255:97adb68decd4 1284
Wolfgang Betz 132:51056160fa4a 1285 return BLE_ERROR_NONE;
Wolfgang Betz 132:51056160fa4a 1286 }
Andrea Palmieri 229:9981f62cdb1a 1287
Andrea Palmieri 229:9981f62cdb1a 1288 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1289 /*!
Andrea Palmieri 229:9981f62cdb1a 1290 @brief Set the advertising policy filter mode that will be used in
Andrea Palmieri 229:9981f62cdb1a 1291 the next call to startAdvertising().
Andrea Palmieri 229:9981f62cdb1a 1292
Andrea Palmieri 229:9981f62cdb1a 1293 @returns \ref ble_errror_t
Andrea Palmieri 229:9981f62cdb1a 1294
Andrea Palmieri 229:9981f62cdb1a 1295 @retval BLE_ERROR_NONE
Andrea Palmieri 229:9981f62cdb1a 1296 Everything executed properly.
Andrea Palmieri 229:9981f62cdb1a 1297
Andrea Palmieri 229:9981f62cdb1a 1298 BLE_ERROR_NOT_IMPLEMENTED
Andrea Palmieri 229:9981f62cdb1a 1299 This feature is currently note implemented.
Andrea Palmieri 229:9981f62cdb1a 1300 */
Andrea Palmieri 229:9981f62cdb1a 1301 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1302 ble_error_t BlueNRGGap::setAdvertisingPolicyMode(Gap::AdvertisingPolicyMode_t mode)
Andrea Palmieri 229:9981f62cdb1a 1303 {
Andrea Palmieri 229:9981f62cdb1a 1304 advertisingPolicyMode = mode;
Andrea Palmieri 229:9981f62cdb1a 1305
Andrea Palmieri 229:9981f62cdb1a 1306 return BLE_ERROR_NONE;
Andrea Palmieri 229:9981f62cdb1a 1307 }
Andrea Palmieri 229:9981f62cdb1a 1308
Andrea Palmieri 229:9981f62cdb1a 1309 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1310 /*!
Andrea Palmieri 229:9981f62cdb1a 1311 @brief Set the scanning policy filter mode that will be used in
Andrea Palmieri 229:9981f62cdb1a 1312 the next call to startAdvertising().
Andrea Palmieri 229:9981f62cdb1a 1313
Andrea Palmieri 229:9981f62cdb1a 1314 @returns \ref ble_errror_t
Andrea Palmieri 229:9981f62cdb1a 1315
Andrea Palmieri 229:9981f62cdb1a 1316 @retval BLE_ERROR_NONE
Andrea Palmieri 229:9981f62cdb1a 1317 Everything executed properly.
Andrea Palmieri 229:9981f62cdb1a 1318
Andrea Palmieri 229:9981f62cdb1a 1319 BLE_ERROR_NOT_IMPLEMENTED
Andrea Palmieri 229:9981f62cdb1a 1320 This feature is currently note implemented.
Andrea Palmieri 229:9981f62cdb1a 1321 */
Andrea Palmieri 229:9981f62cdb1a 1322 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1323 ble_error_t BlueNRGGap::setScanningPolicyMode(Gap::ScanningPolicyMode_t mode)
Andrea Palmieri 229:9981f62cdb1a 1324 {
Andrea Palmieri 229:9981f62cdb1a 1325 scanningPolicyMode = mode;
Andrea Palmieri 229:9981f62cdb1a 1326
Andrea Palmieri 229:9981f62cdb1a 1327 return BLE_ERROR_NONE;
Andrea Palmieri 229:9981f62cdb1a 1328 }
Andrea Palmieri 229:9981f62cdb1a 1329
Andrea Palmieri 229:9981f62cdb1a 1330 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1331 /*!
Andrea Palmieri 229:9981f62cdb1a 1332 @brief Get the current advertising policy filter mode.
Andrea Palmieri 229:9981f62cdb1a 1333
Andrea Palmieri 229:9981f62cdb1a 1334 @returns The advertising policy filter mode.
Andrea Palmieri 229:9981f62cdb1a 1335 */
Andrea Palmieri 229:9981f62cdb1a 1336 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1337 Gap::AdvertisingPolicyMode_t BlueNRGGap::getAdvertisingPolicyMode(void) const
Andrea Palmieri 229:9981f62cdb1a 1338 {
Andrea Palmieri 229:9981f62cdb1a 1339 return advertisingPolicyMode;
Andrea Palmieri 229:9981f62cdb1a 1340 }
Andrea Palmieri 229:9981f62cdb1a 1341
Andrea Palmieri 229:9981f62cdb1a 1342 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1343 /*!
Andrea Palmieri 229:9981f62cdb1a 1344 @brief Get the current scanning policy filter mode.
Andrea Palmieri 229:9981f62cdb1a 1345
Andrea Palmieri 229:9981f62cdb1a 1346 @returns The scanning policy filter mode.
Andrea Palmieri 229:9981f62cdb1a 1347
Andrea Palmieri 229:9981f62cdb1a 1348 */
Andrea Palmieri 229:9981f62cdb1a 1349 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1350 Gap::ScanningPolicyMode_t BlueNRGGap::getScanningPolicyMode(void) const
Andrea Palmieri 229:9981f62cdb1a 1351 {
Andrea Palmieri 229:9981f62cdb1a 1352 return scanningPolicyMode;
Andrea Palmieri 229:9981f62cdb1a 1353 }
Andrea Palmieri 229:9981f62cdb1a 1354
Andrea Palmieri 229:9981f62cdb1a 1355 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1356 /*!
Andrea Palmieri 229:9981f62cdb1a 1357 @brief Clear BlueNRGGap's state.
Andrea Palmieri 229:9981f62cdb1a 1358
Andrea Palmieri 229:9981f62cdb1a 1359 @returns ble_error_t
Andrea Palmieri 229:9981f62cdb1a 1360
Andrea Palmieri 229:9981f62cdb1a 1361 @retval BLE_ERROR_NONE
Andrea Palmieri 229:9981f62cdb1a 1362 Everything executed properly
Andrea Palmieri 229:9981f62cdb1a 1363 */
Andrea Palmieri 229:9981f62cdb1a 1364 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1365 ble_error_t BlueNRGGap::reset(void)
Andrea Palmieri 229:9981f62cdb1a 1366 {
Andrea Palmieri 229:9981f62cdb1a 1367 /* Clear all state that is from the parent, including private members */
Andrea Palmieri 229:9981f62cdb1a 1368 if (Gap::reset() != BLE_ERROR_NONE) {
Andrea Palmieri 229:9981f62cdb1a 1369 return BLE_ERROR_INVALID_STATE;
Andrea Palmieri 229:9981f62cdb1a 1370 }
Andrea Palmieri 229:9981f62cdb1a 1371
Andrea Palmieri 229:9981f62cdb1a 1372 /* Clear derived class members */
Andrea Palmieri 229:9981f62cdb1a 1373 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
Andrea Palmieri 229:9981f62cdb1a 1374
Andrea Palmieri 229:9981f62cdb1a 1375 /* Set the whitelist policy filter modes to IGNORE_WHITELIST */
Andrea Palmieri 229:9981f62cdb1a 1376 advertisingPolicyMode = Gap::ADV_POLICY_IGNORE_WHITELIST;
Andrea Palmieri 229:9981f62cdb1a 1377 scanningPolicyMode = Gap::SCAN_POLICY_IGNORE_WHITELIST;
Andrea Palmieri 229:9981f62cdb1a 1378
Andrea Palmieri 229:9981f62cdb1a 1379 return BLE_ERROR_NONE;
Vincent Coubard 256:146bb175ce80 1380 }
Vincent Coubard 256:146bb175ce80 1381
Vincent Coubard 256:146bb175ce80 1382 void BlueNRGGap::setConnectionInterval(uint16_t interval) {
Vincent Coubard 256:146bb175ce80 1383 conn_min_interval = interval;
Vincent Coubard 256:146bb175ce80 1384 conn_max_interval = interval;
Vincent Coubard 255:97adb68decd4 1385 }