BLE test

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Vincent Coubard
Date:
Thu Sep 15 10:51:18 2016 +0100
Branch:
a8af514bf03daac81073325025db2c98e2151a34
Revision:
256:146bb175ce80
Parent:
255:97adb68decd4
Child:
259:323f588e5f57
Sync with a8af514bf03daac81073325025db2c98e2151a34

2016-07-07 15:39:28+01:00: Vincent Coubard
Fix start advertising while in connection.
Fix connection parameters reported to the user.

Highlighted by gapState_test_04 and GapState_test_07

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