My fork of X_NUCLEO_IDB0XA1

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Vincent Coubard
Date:
Thu Sep 15 10:51:31 2016 +0100
Branch:
5d71c4ee51a741fa3aabe0b7ad4b814d7bf27aee
Revision:
268:c0a1e03c5736
Parent:
266:b49e28134d83
Child:
270:ca649990a830
Sync with 5d71c4ee51a741fa3aabe0b7ad4b814d7bf27aee

2016-07-12 15:18:12+01:00: Vincent Coubard
Fix set address function

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 259:323f588e5f57 504 PRINTF("impossible to set advertising parameters\n\r");
Vincent Coubard 259:323f588e5f57 505 PRINTF("advInterval min: %u, advInterval max: %u\n\r", advInterval, advInterval + 1);
Vincent Coubard 259:323f588e5f57 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 210:08c4c85ca679 814 if (type > BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE) {
Wolfgang Betz 130:770ce14d3d15 815 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 816 }
Vincent Coubard 255:97adb68decd4 817
Vincent Coubard 268:c0a1e03c5736 818 if(type == BLEProtocol::AddressType::PUBLIC){
Vincent Coubard 268:c0a1e03c5736 819 tBleStatus ret = aci_hal_write_config_data(
Vincent Coubard 268:c0a1e03c5736 820 CONFIG_DATA_PUBADDR_OFFSET,
Vincent Coubard 268:c0a1e03c5736 821 CONFIG_DATA_PUBADDR_LEN,
Vincent Coubard 268:c0a1e03c5736 822 address
Vincent Coubard 268:c0a1e03c5736 823 );
Andrea Palmieri 229:9981f62cdb1a 824 if(ret != BLE_STATUS_SUCCESS) {
Andrea Palmieri 239:8e93d6e26c55 825 return BLE_ERROR_OPERATION_NOT_PERMITTED;
Andrea Palmieri 229:9981f62cdb1a 826 }
Vincent Coubard 268:c0a1e03c5736 827 } else if (type == BLEProtocol::AddressType::RANDOM_STATIC) {
Vincent Coubard 268:c0a1e03c5736 828 // ensure that the random static address is well formed
Vincent Coubard 268:c0a1e03c5736 829 if ((address[5] & 0xC0) != 0xC0) {
Vincent Coubard 268:c0a1e03c5736 830 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Vincent Coubard 268:c0a1e03c5736 831 }
Vincent Coubard 268:c0a1e03c5736 832
Vincent Coubard 268:c0a1e03c5736 833 // thanks to const correctness of the API ...
Vincent Coubard 268:c0a1e03c5736 834 tBDAddr random_address = { 0 };
Vincent Coubard 268:c0a1e03c5736 835 memcpy(random_address, address, sizeof(random_address));
Vincent Coubard 268:c0a1e03c5736 836 int err = hci_le_set_random_address(random_address);
Vincent Coubard 268:c0a1e03c5736 837 if (err) {
Vincent Coubard 268:c0a1e03c5736 838 return BLE_ERROR_OPERATION_NOT_PERMITTED;
Vincent Coubard 268:c0a1e03c5736 839 }
Vincent Coubard 268:c0a1e03c5736 840
Vincent Coubard 268:c0a1e03c5736 841 // It is not possible to get the bluetooth address when it is set
Vincent Coubard 268:c0a1e03c5736 842 // store it locally in class data member
Vincent Coubard 268:c0a1e03c5736 843 memcpy(bdaddr, address, sizeof(bdaddr));
Andrea Palmieri 239:8e93d6e26c55 844 } else {
Vincent Coubard 268:c0a1e03c5736 845 // FIXME random addresses are not supported yet
Vincent Coubard 268:c0a1e03c5736 846 // BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE
Vincent Coubard 268:c0a1e03c5736 847 // BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE
Vincent Coubard 268:c0a1e03c5736 848 return BLE_ERROR_NOT_IMPLEMENTED;
Wolfgang Betz 130:770ce14d3d15 849 }
Vincent Coubard 255:97adb68decd4 850
Vincent Coubard 268:c0a1e03c5736 851 // if we're here then the address was correctly set
Vincent Coubard 268:c0a1e03c5736 852 // commit it inside the addr_type
Vincent Coubard 268:c0a1e03c5736 853 addr_type = type;
Vincent Coubard 268:c0a1e03c5736 854 isSetAddress = true;
Wolfgang Betz 130:770ce14d3d15 855 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 856 }
Andrea Palmieri 229:9981f62cdb1a 857
Wolfgang Betz 130:770ce14d3d15 858 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 859 /*!
Wolfgang Betz 130:770ce14d3d15 860 @brief Returns boolean if the address of the device has been set
Wolfgang Betz 130:770ce14d3d15 861 or not
Vincent Coubard 255:97adb68decd4 862
Wolfgang Betz 130:770ce14d3d15 863 @returns bool
Wolfgang Betz 130:770ce14d3d15 864
Wolfgang Betz 130:770ce14d3d15 865 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 866
Wolfgang Betz 130:770ce14d3d15 867 @code
Wolfgang Betz 130:770ce14d3d15 868
Wolfgang Betz 130:770ce14d3d15 869 @endcode
Wolfgang Betz 130:770ce14d3d15 870 */
Wolfgang Betz 130:770ce14d3d15 871 /**************************************************************************/
Vincent Coubard 255:97adb68decd4 872 bool BlueNRGGap::getIsSetAddress()
Wolfgang Betz 130:770ce14d3d15 873 {
Vincent Coubard 255:97adb68decd4 874 return isSetAddress;
Wolfgang Betz 130:770ce14d3d15 875 }
Wolfgang Betz 130:770ce14d3d15 876
Wolfgang Betz 130:770ce14d3d15 877 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 878 /*!
Wolfgang Betz 130:770ce14d3d15 879 @brief Returns the address of the device if set
Wolfgang Betz 130:770ce14d3d15 880
Wolfgang Betz 130:770ce14d3d15 881 @returns Pointer to the address if Address is set else NULL
Wolfgang Betz 130:770ce14d3d15 882
Wolfgang Betz 130:770ce14d3d15 883 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 884
Wolfgang Betz 130:770ce14d3d15 885 @code
Wolfgang Betz 130:770ce14d3d15 886
Wolfgang Betz 130:770ce14d3d15 887 @endcode
Wolfgang Betz 130:770ce14d3d15 888 */
Wolfgang Betz 130:770ce14d3d15 889 /**************************************************************************/
Vincent Coubard 255:97adb68decd4 890 ble_error_t BlueNRGGap::getAddress(AddressType_t *typeP, Address_t address)
Wolfgang Betz 130:770ce14d3d15 891 {
Andrea Palmieri 229:9981f62cdb1a 892 uint8_t bdaddr[BDADDR_SIZE];
Andrea Palmieri 229:9981f62cdb1a 893 uint8_t data_len_out;
Andrea Palmieri 229:9981f62cdb1a 894
Vincent Coubard 268:c0a1e03c5736 895 // precondition, check that pointers in input are valid
Vincent Coubard 268:c0a1e03c5736 896 if (typeP == NULL || address == NULL) {
Vincent Coubard 268:c0a1e03c5736 897 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 228:25368f053411 898 }
Andrea Palmieri 228:25368f053411 899
Vincent Coubard 268:c0a1e03c5736 900 if (addr_type == BLEProtocol::AddressType::PUBLIC) {
Vincent Coubard 268:c0a1e03c5736 901 tBleStatus ret = aci_hal_read_config_data(CONFIG_DATA_PUBADDR_OFFSET, BDADDR_SIZE, &data_len_out, bdaddr);
Vincent Coubard 268:c0a1e03c5736 902 if(ret != BLE_STATUS_SUCCESS || data_len_out != BDADDR_SIZE) {
Vincent Coubard 268:c0a1e03c5736 903 return BLE_ERROR_UNSPECIFIED;
Vincent Coubard 268:c0a1e03c5736 904 }
Vincent Coubard 268:c0a1e03c5736 905 } else if (addr_type == BLEProtocol::AddressType::RANDOM_STATIC) {
Vincent Coubard 268:c0a1e03c5736 906 // FIXME hci_read_bd_addr and
Vincent Coubard 268:c0a1e03c5736 907 // aci_hal_read_config_data CONFIG_DATA_RANDOM_ADDRESS_IDB05A1
Vincent Coubard 268:c0a1e03c5736 908 // does not work, use the address stored in class data member
Vincent Coubard 268:c0a1e03c5736 909 memcpy(bdaddr, this->bdaddr, sizeof(bdaddr));
Vincent Coubard 268:c0a1e03c5736 910 } else {
Vincent Coubard 268:c0a1e03c5736 911 // FIXME: should be implemented with privacy features
Vincent Coubard 268:c0a1e03c5736 912 // BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE
Vincent Coubard 268:c0a1e03c5736 913 // BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE
Vincent Coubard 268:c0a1e03c5736 914 return BLE_ERROR_NOT_IMPLEMENTED;
Andrea Palmieri 229:9981f62cdb1a 915 }
Andrea Palmieri 229:9981f62cdb1a 916
Vincent Coubard 268:c0a1e03c5736 917 *typeP = addr_type;
Vincent Coubard 268:c0a1e03c5736 918 memcpy(address, bdaddr, BDADDR_SIZE);
Vincent Coubard 255:97adb68decd4 919
Wolfgang Betz 130:770ce14d3d15 920 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 921 }
Wolfgang Betz 130:770ce14d3d15 922
Wolfgang Betz 130:770ce14d3d15 923 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 924 /*!
Wolfgang Betz 130:770ce14d3d15 925 @brief obtains preferred connection params
Wolfgang Betz 130:770ce14d3d15 926
Wolfgang Betz 130:770ce14d3d15 927 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 928
Wolfgang Betz 130:770ce14d3d15 929 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 930
Wolfgang Betz 130:770ce14d3d15 931 @code
Wolfgang Betz 130:770ce14d3d15 932
Wolfgang Betz 130:770ce14d3d15 933 @endcode
Wolfgang Betz 130:770ce14d3d15 934 */
Wolfgang Betz 130:770ce14d3d15 935 /**************************************************************************/
Vincent Coubard 255:97adb68decd4 936 ble_error_t BlueNRGGap::getPreferredConnectionParams(ConnectionParams_t *params)
Wolfgang Betz 130:770ce14d3d15 937 {
Vincent Coubard 266:b49e28134d83 938 static const size_t parameter_size = 2;
Vincent Coubard 266:b49e28134d83 939
Vincent Coubard 266:b49e28134d83 940 if (!g_preferred_connection_parameters_char_handle) {
Vincent Coubard 266:b49e28134d83 941 return BLE_ERROR_OPERATION_NOT_PERMITTED;
Vincent Coubard 266:b49e28134d83 942 }
Vincent Coubard 266:b49e28134d83 943
Vincent Coubard 266:b49e28134d83 944 // Peripheral preferred connection parameters are an array of 4 uint16_t
Vincent Coubard 266:b49e28134d83 945 uint8_t parameters_packed[parameter_size * 4];
Vincent Coubard 266:b49e28134d83 946 uint16_t bytes_read = 0;
Andrea Palmieri 142:adf1567d3900 947
Vincent Coubard 266:b49e28134d83 948 tBleStatus err = aci_gatt_read_handle_value(
Vincent Coubard 266:b49e28134d83 949 g_preferred_connection_parameters_char_handle + BlueNRGGattServer::CHAR_VALUE_HANDLE,
Vincent Coubard 266:b49e28134d83 950 sizeof(parameters_packed),
Vincent Coubard 266:b49e28134d83 951 &bytes_read,
Vincent Coubard 266:b49e28134d83 952 parameters_packed
Vincent Coubard 266:b49e28134d83 953 );
Vincent Coubard 266:b49e28134d83 954
Vincent Coubard 266:b49e28134d83 955 PRINTF("getPreferredConnectionParams err=0x%02x (bytes_read=%u)\n\r", err, bytes_read);
Vincent Coubard 266:b49e28134d83 956
Vincent Coubard 266:b49e28134d83 957 // check that the read succeed and the result have the expected length
Vincent Coubard 266:b49e28134d83 958 if (err || bytes_read != sizeof(parameters_packed)) {
Vincent Coubard 266:b49e28134d83 959 return BLE_ERROR_UNSPECIFIED;
Vincent Coubard 266:b49e28134d83 960 }
Vincent Coubard 266:b49e28134d83 961
Vincent Coubard 266:b49e28134d83 962 // memcpy field by field
Vincent Coubard 266:b49e28134d83 963 memcpy(&params->minConnectionInterval, parameters_packed, parameter_size);
Vincent Coubard 266:b49e28134d83 964 memcpy(&params->maxConnectionInterval, &parameters_packed[parameter_size], parameter_size);
Vincent Coubard 266:b49e28134d83 965 memcpy(&params->slaveLatency, &parameters_packed[2 * parameter_size], parameter_size);
Vincent Coubard 266:b49e28134d83 966 memcpy(&params->connectionSupervisionTimeout, &parameters_packed[3 * parameter_size], parameter_size);
Vincent Coubard 266:b49e28134d83 967
Vincent Coubard 266:b49e28134d83 968 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 969 }
Wolfgang Betz 130:770ce14d3d15 970
Wolfgang Betz 130:770ce14d3d15 971
Wolfgang Betz 130:770ce14d3d15 972 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 973 /*!
Wolfgang Betz 130:770ce14d3d15 974 @brief sets preferred connection params
Wolfgang Betz 130:770ce14d3d15 975
Wolfgang Betz 130:770ce14d3d15 976 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 977
Wolfgang Betz 130:770ce14d3d15 978 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 979
Wolfgang Betz 130:770ce14d3d15 980 @code
Wolfgang Betz 130:770ce14d3d15 981
Wolfgang Betz 130:770ce14d3d15 982 @endcode
Wolfgang Betz 130:770ce14d3d15 983 */
Wolfgang Betz 130:770ce14d3d15 984 /**************************************************************************/
Vincent Coubard 255:97adb68decd4 985 ble_error_t BlueNRGGap::setPreferredConnectionParams(const ConnectionParams_t *params)
Wolfgang Betz 130:770ce14d3d15 986 {
Vincent Coubard 266:b49e28134d83 987 static const size_t parameter_size = 2;
Vincent Coubard 266:b49e28134d83 988 uint8_t parameters_packed[parameter_size * 4];
Vincent Coubard 266:b49e28134d83 989
Vincent Coubard 266:b49e28134d83 990 // ensure that parameters are correct
Vincent Coubard 266:b49e28134d83 991 // see BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part C]
Vincent Coubard 266:b49e28134d83 992 // section 12.3 PERIPHERAL PREFERRED CONNECTION PARAMETERS CHARACTERISTIC
Vincent Coubard 266:b49e28134d83 993 if (((0x0006 > params->minConnectionInterval) || (params->minConnectionInterval > 0x0C80)) &&
Vincent Coubard 266:b49e28134d83 994 params->minConnectionInterval != 0xFFFF) {
Vincent Coubard 266:b49e28134d83 995 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Vincent Coubard 266:b49e28134d83 996 }
Vincent Coubard 266:b49e28134d83 997
Vincent Coubard 266:b49e28134d83 998 if (((params->minConnectionInterval > params->maxConnectionInterval) || (params->maxConnectionInterval > 0x0C80)) &&
Vincent Coubard 266:b49e28134d83 999 params->maxConnectionInterval != 0xFFFF) {
Vincent Coubard 266:b49e28134d83 1000 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Vincent Coubard 266:b49e28134d83 1001 }
Vincent Coubard 266:b49e28134d83 1002
Vincent Coubard 266:b49e28134d83 1003 if (params->slaveLatency > 0x01F3) {
Vincent Coubard 266:b49e28134d83 1004 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Vincent Coubard 266:b49e28134d83 1005 }
Vincent Coubard 266:b49e28134d83 1006
Vincent Coubard 266:b49e28134d83 1007 if (((0x000A > params->connectionSupervisionTimeout) || (params->connectionSupervisionTimeout > 0x0C80)) &&
Vincent Coubard 266:b49e28134d83 1008 params->connectionSupervisionTimeout != 0xFFFF) {
Vincent Coubard 266:b49e28134d83 1009 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Vincent Coubard 266:b49e28134d83 1010 }
Andrea Palmieri 142:adf1567d3900 1011
Vincent Coubard 266:b49e28134d83 1012 // copy the parameters inside the byte array
Vincent Coubard 266:b49e28134d83 1013 memcpy(parameters_packed, &params->minConnectionInterval, parameter_size);
Vincent Coubard 266:b49e28134d83 1014 memcpy(&parameters_packed[parameter_size], &params->maxConnectionInterval, parameter_size);
Vincent Coubard 266:b49e28134d83 1015 memcpy(&parameters_packed[2 * parameter_size], &params->slaveLatency, parameter_size);
Vincent Coubard 266:b49e28134d83 1016 memcpy(&parameters_packed[3 * parameter_size], &params->connectionSupervisionTimeout, parameter_size);
Vincent Coubard 266:b49e28134d83 1017
Vincent Coubard 266:b49e28134d83 1018 tBleStatus err = aci_gatt_update_char_value(
Vincent Coubard 266:b49e28134d83 1019 g_gap_service_handle,
Vincent Coubard 266:b49e28134d83 1020 g_preferred_connection_parameters_char_handle,
Vincent Coubard 266:b49e28134d83 1021 /* offset */ 0,
Vincent Coubard 266:b49e28134d83 1022 sizeof(parameters_packed),
Vincent Coubard 266:b49e28134d83 1023 parameters_packed
Vincent Coubard 266:b49e28134d83 1024 );
Vincent Coubard 266:b49e28134d83 1025
Vincent Coubard 266:b49e28134d83 1026 if (err) {
Vincent Coubard 266:b49e28134d83 1027 PRINTF("setPreferredConnectionParams failed (err=0x%x)!!\n\r", err) ;
Vincent Coubard 266:b49e28134d83 1028 switch (err) {
Vincent Coubard 266:b49e28134d83 1029 case BLE_STATUS_INVALID_HANDLE:
Vincent Coubard 266:b49e28134d83 1030 case BLE_STATUS_INVALID_PARAMETER:
Vincent Coubard 266:b49e28134d83 1031 return BLE_ERROR_INVALID_PARAM;
Vincent Coubard 266:b49e28134d83 1032 case BLE_STATUS_INSUFFICIENT_RESOURCES:
Vincent Coubard 266:b49e28134d83 1033 return BLE_ERROR_NO_MEM;
Vincent Coubard 266:b49e28134d83 1034 case BLE_STATUS_TIMEOUT:
Vincent Coubard 266:b49e28134d83 1035 return BLE_STACK_BUSY;
Vincent Coubard 266:b49e28134d83 1036 default:
Vincent Coubard 266:b49e28134d83 1037 return BLE_ERROR_UNSPECIFIED;
Vincent Coubard 266:b49e28134d83 1038 }
Vincent Coubard 266:b49e28134d83 1039 }
Vincent Coubard 266:b49e28134d83 1040
Vincent Coubard 266:b49e28134d83 1041 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 1042 }
Wolfgang Betz 130:770ce14d3d15 1043
Wolfgang Betz 130:770ce14d3d15 1044 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1045 /*!
Wolfgang Betz 130:770ce14d3d15 1046 @brief updates preferred connection params
Wolfgang Betz 130:770ce14d3d15 1047
Wolfgang Betz 130:770ce14d3d15 1048 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 1049
Wolfgang Betz 130:770ce14d3d15 1050 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 1051
Wolfgang Betz 130:770ce14d3d15 1052 @code
Wolfgang Betz 130:770ce14d3d15 1053
Wolfgang Betz 130:770ce14d3d15 1054 @endcode
Wolfgang Betz 130:770ce14d3d15 1055 */
Wolfgang Betz 130:770ce14d3d15 1056 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1057 ble_error_t BlueNRGGap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *params)
Wolfgang Betz 130:770ce14d3d15 1058 {
Andrea Palmieri 142:adf1567d3900 1059 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 1060 (void) handle;
Andrea Palmieri 142:adf1567d3900 1061 (void)params;
Andrea Palmieri 142:adf1567d3900 1062
Wolfgang Betz 130:770ce14d3d15 1063 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 1064 }
Wolfgang Betz 130:770ce14d3d15 1065
Wolfgang Betz 130:770ce14d3d15 1066 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1067 /*!
Vincent Coubard 255:97adb68decd4 1068 @brief Sets the Device Name Characteristic
Wolfgang Betz 130:770ce14d3d15 1069
Wolfgang Betz 130:770ce14d3d15 1070 @param[in] deviceName
Wolfgang Betz 130:770ce14d3d15 1071 pointer to device name to be set
Wolfgang Betz 130:770ce14d3d15 1072
Wolfgang Betz 130:770ce14d3d15 1073 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 1074
Wolfgang Betz 130:770ce14d3d15 1075 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 1076 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 1077
Wolfgang Betz 130:770ce14d3d15 1078 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 1079
Wolfgang Betz 130:770ce14d3d15 1080 @code
Wolfgang Betz 130:770ce14d3d15 1081
Wolfgang Betz 130:770ce14d3d15 1082 @endcode
Wolfgang Betz 130:770ce14d3d15 1083 */
Wolfgang Betz 130:770ce14d3d15 1084 /**************************************************************************/
Vincent Coubard 255:97adb68decd4 1085 ble_error_t BlueNRGGap::setDeviceName(const uint8_t *deviceName)
Wolfgang Betz 130:770ce14d3d15 1086 {
Andrea Palmieri 187:2e517d3ddd71 1087 tBleStatus ret;
Vincent Coubard 255:97adb68decd4 1088 uint8_t nameLen = 0;
Vincent Coubard 255:97adb68decd4 1089
Andrea Palmieri 228:25368f053411 1090 nameLen = strlen((const char*)deviceName);
Andrea Palmieri 228:25368f053411 1091 PRINTF("DeviceName Size=%d\n\r", nameLen);
Andrea Palmieri 228:25368f053411 1092
Andrea Palmieri 222:37a7e289ee3d 1093 ret = aci_gatt_update_char_value(g_gap_service_handle,
Andrea Palmieri 222:37a7e289ee3d 1094 g_device_name_char_handle,
Andrea Palmieri 222:37a7e289ee3d 1095 0,
Andrea Palmieri 222:37a7e289ee3d 1096 nameLen,
Andrea Palmieri 228:25368f053411 1097 deviceName);
Andrea Palmieri 187:2e517d3ddd71 1098
Andrea Palmieri 187:2e517d3ddd71 1099 if (BLE_STATUS_SUCCESS != ret){
Andrea Palmieri 187:2e517d3ddd71 1100 PRINTF("device set name failed (ret=0x%x)!!\n\r", ret) ;
Andrea Palmieri 187:2e517d3ddd71 1101 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 1102 case BLE_STATUS_INVALID_HANDLE:
Andrea Palmieri 187:2e517d3ddd71 1103 case BLE_STATUS_INVALID_PARAMETER:
Andrea Palmieri 187:2e517d3ddd71 1104 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 187:2e517d3ddd71 1105 case BLE_STATUS_INSUFFICIENT_RESOURCES:
Andrea Palmieri 187:2e517d3ddd71 1106 return BLE_ERROR_NO_MEM;
Andrea Palmieri 187:2e517d3ddd71 1107 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 1108 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 1109 default:
Andrea Palmieri 187:2e517d3ddd71 1110 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 1111 }
Wolfgang Betz 130:770ce14d3d15 1112 }
Wolfgang Betz 130:770ce14d3d15 1113
Wolfgang Betz 130:770ce14d3d15 1114 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 1115 }
Wolfgang Betz 130:770ce14d3d15 1116
Wolfgang Betz 130:770ce14d3d15 1117 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1118 /*!
Vincent Coubard 255:97adb68decd4 1119 @brief Gets the Device Name Characteristic
Wolfgang Betz 130:770ce14d3d15 1120
Wolfgang Betz 130:770ce14d3d15 1121 @param[in] deviceName
Vincent Coubard 255:97adb68decd4 1122 pointer to device name
Wolfgang Betz 130:770ce14d3d15 1123
Wolfgang Betz 130:770ce14d3d15 1124 @param[in] lengthP
Vincent Coubard 255:97adb68decd4 1125 pointer to device name length
Wolfgang Betz 130:770ce14d3d15 1126
Wolfgang Betz 130:770ce14d3d15 1127 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 1128
Wolfgang Betz 130:770ce14d3d15 1129 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 1130 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 1131
Wolfgang Betz 130:770ce14d3d15 1132 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 1133
Wolfgang Betz 130:770ce14d3d15 1134 @code
Wolfgang Betz 130:770ce14d3d15 1135
Wolfgang Betz 130:770ce14d3d15 1136 @endcode
Wolfgang Betz 130:770ce14d3d15 1137 */
Wolfgang Betz 130:770ce14d3d15 1138 /**************************************************************************/
Andrea Palmieri 228:25368f053411 1139 ble_error_t BlueNRGGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP)
Andrea Palmieri 228:25368f053411 1140 {
Andrea Palmieri 228:25368f053411 1141 tBleStatus ret;
Andrea Palmieri 228:25368f053411 1142
Andrea Palmieri 229:9981f62cdb1a 1143 ret = aci_gatt_read_handle_value(g_device_name_char_handle+BlueNRGGattServer::CHAR_VALUE_HANDLE,
Andrea Palmieri 228:25368f053411 1144 *lengthP,
Andrea Palmieri 228:25368f053411 1145 (uint16_t *)lengthP,
Andrea Palmieri 228:25368f053411 1146 deviceName);
Andrea Palmieri 228:25368f053411 1147 PRINTF("getDeviceName ret=0x%02x (lengthP=%d)\n\r", ret, *lengthP);
Andrea Palmieri 228:25368f053411 1148 if (ret == BLE_STATUS_SUCCESS) {
Andrea Palmieri 228:25368f053411 1149 return BLE_ERROR_NONE;
Andrea Palmieri 228:25368f053411 1150 } else {
Andrea Palmieri 228:25368f053411 1151 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Andrea Palmieri 228:25368f053411 1152 }
Wolfgang Betz 130:770ce14d3d15 1153 }
Wolfgang Betz 130:770ce14d3d15 1154
Wolfgang Betz 130:770ce14d3d15 1155 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1156 /*!
Vincent Coubard 255:97adb68decd4 1157 @brief Sets the Device Appearance Characteristic
Wolfgang Betz 130:770ce14d3d15 1158
Wolfgang Betz 130:770ce14d3d15 1159 @param[in] appearance
Vincent Coubard 255:97adb68decd4 1160 device appearance
Wolfgang Betz 130:770ce14d3d15 1161
Wolfgang Betz 130:770ce14d3d15 1162 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 1163
Wolfgang Betz 130:770ce14d3d15 1164 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 1165 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 1166
Wolfgang Betz 130:770ce14d3d15 1167 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 1168
Wolfgang Betz 130:770ce14d3d15 1169 @code
Wolfgang Betz 130:770ce14d3d15 1170
Wolfgang Betz 130:770ce14d3d15 1171 @endcode
Wolfgang Betz 130:770ce14d3d15 1172 */
Wolfgang Betz 130:770ce14d3d15 1173 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1174 ble_error_t BlueNRGGap::setAppearance(GapAdvertisingData::Appearance appearance)
Wolfgang Betz 130:770ce14d3d15 1175 {
Andrea Palmieri 187:2e517d3ddd71 1176 tBleStatus ret;
Andrea Palmieri 228:25368f053411 1177 uint8_t deviceAppearance[2];
Andrea Palmieri 187:2e517d3ddd71 1178
Vincent Coubard 255:97adb68decd4 1179 STORE_LE_16(deviceAppearance, appearance);
Vincent Coubard 253:9665a6f8bbdb 1180 PRINTF("setAppearance= 0x%x 0x%x\n\r", deviceAppearance[1], deviceAppearance[0]);
Vincent Coubard 255:97adb68decd4 1181
Andrea Palmieri 228:25368f053411 1182 ret = aci_gatt_update_char_value(g_gap_service_handle,
Andrea Palmieri 228:25368f053411 1183 g_appearance_char_handle,
Andrea Palmieri 228:25368f053411 1184 0, 2, (uint8_t *)deviceAppearance);
Andrea Palmieri 187:2e517d3ddd71 1185 if (BLE_STATUS_SUCCESS == ret){
Andrea Palmieri 187:2e517d3ddd71 1186 return BLE_ERROR_NONE;
Andrea Palmieri 187:2e517d3ddd71 1187 }
Andrea Palmieri 187:2e517d3ddd71 1188
Andrea Palmieri 217:23870c55b07e 1189 PRINTF("setAppearance failed (ret=0x%x)!!\n\r", ret);
Andrea Palmieri 187:2e517d3ddd71 1190 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 1191 case BLE_STATUS_INVALID_HANDLE:
Andrea Palmieri 187:2e517d3ddd71 1192 case BLE_STATUS_INVALID_PARAMETER:
Andrea Palmieri 187:2e517d3ddd71 1193 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 187:2e517d3ddd71 1194 case BLE_STATUS_INSUFFICIENT_RESOURCES:
Andrea Palmieri 187:2e517d3ddd71 1195 return BLE_ERROR_NO_MEM;
Andrea Palmieri 187:2e517d3ddd71 1196 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 1197 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 1198 default:
Andrea Palmieri 187:2e517d3ddd71 1199 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 1200 }
Wolfgang Betz 130:770ce14d3d15 1201 }
Wolfgang Betz 130:770ce14d3d15 1202
Wolfgang Betz 130:770ce14d3d15 1203 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1204 /*!
Wolfgang Betz 130:770ce14d3d15 1205 @brief Gets the Device Appearance Characteristic
Wolfgang Betz 130:770ce14d3d15 1206
Wolfgang Betz 130:770ce14d3d15 1207 @param[in] appearance
Vincent Coubard 255:97adb68decd4 1208 pointer to device appearance value
Wolfgang Betz 130:770ce14d3d15 1209
Wolfgang Betz 130:770ce14d3d15 1210 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 1211
Wolfgang Betz 130:770ce14d3d15 1212 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 1213 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 1214
Wolfgang Betz 130:770ce14d3d15 1215 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 1216
Wolfgang Betz 130:770ce14d3d15 1217 @code
Wolfgang Betz 130:770ce14d3d15 1218
Wolfgang Betz 130:770ce14d3d15 1219 @endcode
Wolfgang Betz 130:770ce14d3d15 1220 */
Wolfgang Betz 130:770ce14d3d15 1221 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1222 ble_error_t BlueNRGGap::getAppearance(GapAdvertisingData::Appearance *appearanceP)
Wolfgang Betz 130:770ce14d3d15 1223 {
Andrea Palmieri 228:25368f053411 1224 tBleStatus ret;
Andrea Palmieri 228:25368f053411 1225 uint16_t lengthP = 2;
Andrea Palmieri 228:25368f053411 1226
Andrea Palmieri 229:9981f62cdb1a 1227 ret = aci_gatt_read_handle_value(g_appearance_char_handle+BlueNRGGattServer::CHAR_VALUE_HANDLE,
Andrea Palmieri 228:25368f053411 1228 lengthP,
Andrea Palmieri 228:25368f053411 1229 &lengthP,
Andrea Palmieri 228:25368f053411 1230 (uint8_t*)appearanceP);
Andrea Palmieri 228:25368f053411 1231 PRINTF("getAppearance ret=0x%02x (lengthP=%d)\n\r", ret, lengthP);
Andrea Palmieri 228:25368f053411 1232 if (ret == BLE_STATUS_SUCCESS) {
Andrea Palmieri 228:25368f053411 1233 return BLE_ERROR_NONE;
Andrea Palmieri 228:25368f053411 1234 } else {
Andrea Palmieri 228:25368f053411 1235 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Andrea Palmieri 228:25368f053411 1236 }
Andrea Palmieri 228:25368f053411 1237
Wolfgang Betz 130:770ce14d3d15 1238 }
Wolfgang Betz 130:770ce14d3d15 1239
Andrea Palmieri 160:174387d53809 1240 GapScanningParams* BlueNRGGap::getScanningParams(void)
Andrea Palmieri 160:174387d53809 1241 {
Andrea Palmieri 160:174387d53809 1242 return &_scanningParams;
Andrea Palmieri 160:174387d53809 1243 }
Andrea Palmieri 160:174387d53809 1244
Wolfgang Betz 165:3576598c0889 1245 static void makeConnection(void)
Wolfgang Betz 162:667f97a4db7f 1246 {
Wolfgang Betz 165:3576598c0889 1247 BlueNRGGap::getInstance().createConnection();
Wolfgang Betz 162:667f97a4db7f 1248 }
Wolfgang Betz 162:667f97a4db7f 1249
Wolfgang Betz 132:51056160fa4a 1250 void BlueNRGGap::Discovery_CB(Reason_t reason,
Wolfgang Betz 132:51056160fa4a 1251 uint8_t adv_type,
Andrea Palmieri 229:9981f62cdb1a 1252 uint8_t addr_type,
Wolfgang Betz 132:51056160fa4a 1253 uint8_t *addr,
Wolfgang Betz 132:51056160fa4a 1254 uint8_t *data_length,
Wolfgang Betz 132:51056160fa4a 1255 uint8_t *data,
Wolfgang Betz 132:51056160fa4a 1256 uint8_t *RSSI)
Wolfgang Betz 132:51056160fa4a 1257 {
Wolfgang Betz 132:51056160fa4a 1258 switch (reason) {
Wolfgang Betz 132:51056160fa4a 1259 case DEVICE_FOUND:
Wolfgang Betz 132:51056160fa4a 1260 {
Wolfgang Betz 132:51056160fa4a 1261 GapAdvertisingParams::AdvertisingType_t type;
Wolfgang Betz 132:51056160fa4a 1262 bool isScanResponse = false;
Andrea Palmieri 229:9981f62cdb1a 1263
Andrea Palmieri 229:9981f62cdb1a 1264 /*
Andrea Palmieri 229:9981f62cdb1a 1265 * Whitelisting (scan policy):
Andrea Palmieri 229:9981f62cdb1a 1266 * SCAN_POLICY_FILTER_ALL_ADV (ADV packets only from devs in the White List) &&
Andrea Palmieri 229:9981f62cdb1a 1267 * Private Random Address
Andrea Palmieri 229:9981f62cdb1a 1268 * => scan_results = FALSE
Andrea Palmieri 229:9981f62cdb1a 1269 * FIXME: the Security Manager should be implemented
Andrea Palmieri 229:9981f62cdb1a 1270 */
Andrea Palmieri 229:9981f62cdb1a 1271 ScanningPolicyMode_t mode = getScanningPolicyMode();
Andrea Palmieri 229:9981f62cdb1a 1272 PRINTF("mode=%u addr_type=%u\n\r", mode, addr_type);
Andrea Palmieri 229:9981f62cdb1a 1273 if(mode == Gap::SCAN_POLICY_FILTER_ALL_ADV ||
Andrea Palmieri 229:9981f62cdb1a 1274 (addr_type == RESOLVABLE_PRIVATE_ADDR ||
Andrea Palmieri 229:9981f62cdb1a 1275 addr_type == NON_RESOLVABLE_PRIVATE_ADDR)) {
Andrea Palmieri 229:9981f62cdb1a 1276 return;
Andrea Palmieri 229:9981f62cdb1a 1277 }
Andrea Palmieri 229:9981f62cdb1a 1278
Wolfgang Betz 132:51056160fa4a 1279 switch(adv_type) {
Wolfgang Betz 132:51056160fa4a 1280 case ADV_IND:
Wolfgang Betz 132:51056160fa4a 1281 type = GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED;
Wolfgang Betz 132:51056160fa4a 1282 break;
Wolfgang Betz 132:51056160fa4a 1283 case ADV_DIRECT_IND:
Wolfgang Betz 132:51056160fa4a 1284 type = GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED;
Wolfgang Betz 132:51056160fa4a 1285 break;
Wolfgang Betz 132:51056160fa4a 1286 case ADV_SCAN_IND:
Wolfgang Betz 132:51056160fa4a 1287 case SCAN_RSP:
Wolfgang Betz 132:51056160fa4a 1288 type = GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED;
Wolfgang Betz 132:51056160fa4a 1289 isScanResponse = true;
Wolfgang Betz 132:51056160fa4a 1290 break;
Wolfgang Betz 132:51056160fa4a 1291 case ADV_NONCONN_IND:
Wolfgang Betz 132:51056160fa4a 1292 type = GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED;
Andrea Palmieri 142:adf1567d3900 1293 break;
Andrea Palmieri 142:adf1567d3900 1294 default:
Andrea Palmieri 142:adf1567d3900 1295 type = GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED;
Wolfgang Betz 132:51056160fa4a 1296 }
Vincent Coubard 255:97adb68decd4 1297
Andrea Palmieri 217:23870c55b07e 1298 PRINTF("data_length=%d adv peerAddr[%02x %02x %02x %02x %02x %02x] \r\n",
Andrea Palmieri 217:23870c55b07e 1299 *data_length, addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
Andrea Palmieri 238:b6454c839f42 1300 if(!_connecting) {
Andrea Palmieri 238:b6454c839f42 1301 processAdvertisementReport(addr, *RSSI, isScanResponse, type, *data_length, data);
Andrea Palmieri 238:b6454c839f42 1302 }
Andrea Palmieri 160:174387d53809 1303 PRINTF("!!!After processAdvertisementReport\n\r");
Wolfgang Betz 132:51056160fa4a 1304 }
Wolfgang Betz 132:51056160fa4a 1305 break;
Vincent Coubard 255:97adb68decd4 1306
Wolfgang Betz 132:51056160fa4a 1307 case DISCOVERY_COMPLETE:
Wolfgang Betz 132:51056160fa4a 1308 // The discovery is complete. If this is due to a stop scanning (i.e., the device
Wolfgang Betz 132:51056160fa4a 1309 // we are interested in has been found) and a connection has been requested
Andrea Palmieri 229:9981f62cdb1a 1310 // then we start the device connection.
Andrea Palmieri 145:024a552e4715 1311 PRINTF("DISCOVERY_COMPLETE\n\r");
Wolfgang Betz 132:51056160fa4a 1312 _scanning = false;
Wolfgang Betz 132:51056160fa4a 1313
Wolfgang Betz 165:3576598c0889 1314 // Since the DISCOVERY_COMPLETE event can be received during the scanning interval,
Andrea Palmieri 229:9981f62cdb1a 1315 // we need to delay the starting of connection
Wolfgang Betz 165:3576598c0889 1316 uint16_t delay = 2*(_scanningParams.getInterval());
Wolfgang Betz 165:3576598c0889 1317
Andrea Palmieri 217:23870c55b07e 1318 #ifdef AST_FOR_MBED_OS
Wolfgang Betz 162:667f97a4db7f 1319 if(_connecting) {
Wolfgang Betz 165:3576598c0889 1320 minar::Scheduler::postCallback(makeConnection).delay(minar::milliseconds(delay));
Wolfgang Betz 132:51056160fa4a 1321 }
Andrea Palmieri 217:23870c55b07e 1322 #else
Andrea Palmieri 217:23870c55b07e 1323 Clock_Wait(delay);
Andrea Palmieri 217:23870c55b07e 1324 if(_connecting) {
Andrea Palmieri 217:23870c55b07e 1325 makeConnection();
Andrea Palmieri 217:23870c55b07e 1326 }
Andrea Palmieri 217:23870c55b07e 1327 #endif /* AST_FOR_MBED_OS */
Wolfgang Betz 132:51056160fa4a 1328
Wolfgang Betz 132:51056160fa4a 1329 break;
Wolfgang Betz 132:51056160fa4a 1330 }
Wolfgang Betz 132:51056160fa4a 1331 }
Wolfgang Betz 132:51056160fa4a 1332
Wolfgang Betz 132:51056160fa4a 1333 ble_error_t BlueNRGGap::startRadioScan(const GapScanningParams &scanningParams)
Wolfgang Betz 132:51056160fa4a 1334 {
Vincent Coubard 255:97adb68decd4 1335
Wolfgang Betz 165:3576598c0889 1336 tBleStatus ret = BLE_STATUS_SUCCESS;
Andrea Palmieri 222:37a7e289ee3d 1337
Andrea Palmieri 222:37a7e289ee3d 1338 // Stop ADV before scanning
Andrea Palmieri 223:36df860f7ffc 1339 /*
Andrea Palmieri 222:37a7e289ee3d 1340 if (state.advertising == 1) {
Andrea Palmieri 222:37a7e289ee3d 1341 stopAdvertising();
Andrea Palmieri 222:37a7e289ee3d 1342 }
Andrea Palmieri 223:36df860f7ffc 1343 */
Andrea Palmieri 222:37a7e289ee3d 1344
Andrea Palmieri 229:9981f62cdb1a 1345 /*
Andrea Palmieri 229:9981f62cdb1a 1346 * Whitelisting (scan policy):
Andrea Palmieri 229:9981f62cdb1a 1347 * SCAN_POLICY_FILTER_ALL_ADV (ADV packets only from devs in the White List) &&
Andrea Palmieri 229:9981f62cdb1a 1348 * White List is empty
Andrea Palmieri 229:9981f62cdb1a 1349 * => scan operation = FAILURE
Andrea Palmieri 229:9981f62cdb1a 1350 * FIXME: the Security Manager should be implemented
Andrea Palmieri 229:9981f62cdb1a 1351 */
Andrea Palmieri 229:9981f62cdb1a 1352 ScanningPolicyMode_t mode = getScanningPolicyMode();
Andrea Palmieri 229:9981f62cdb1a 1353 uint8_t whiteListSize = whitelistAddresses.size;
Andrea Palmieri 229:9981f62cdb1a 1354 if(whiteListSize == 0 && mode == Gap::SCAN_POLICY_FILTER_ALL_ADV) {
Andrea Palmieri 229:9981f62cdb1a 1355 return BLE_ERROR_OPERATION_NOT_PERMITTED;
Andrea Palmieri 229:9981f62cdb1a 1356 }
Andrea Palmieri 229:9981f62cdb1a 1357
Andrea Palmieri 217:23870c55b07e 1358 ret = btleStartRadioScan(scanningParams.getActiveScanning(),
Andrea Palmieri 217:23870c55b07e 1359 scanningParams.getInterval(),
Andrea Palmieri 217:23870c55b07e 1360 scanningParams.getWindow(),
Andrea Palmieri 217:23870c55b07e 1361 addr_type);
Andrea Palmieri 217:23870c55b07e 1362
Andrea Palmieri 229:9981f62cdb1a 1363 PRINTF("Scanning...\n\r");
Andrea Palmieri 229:9981f62cdb1a 1364 PRINTF("scanningParams.getInterval()=%u[msec]\r\n",(scanningParams.getInterval()*625)/1000);
Andrea Palmieri 229:9981f62cdb1a 1365 PRINTF("scanningParams.getWindow()=%u[msec]\r\n",(scanningParams.getWindow()*625)/1000);
Andrea Palmieri 229:9981f62cdb1a 1366 //PRINTF("_advParams.getInterval()=%u\r\n",_advParams.getInterval());
Andrea Palmieri 229:9981f62cdb1a 1367 //PRINTF("CONN_P1=%u\r\n",(unsigned)CONN_P1);
Andrea Palmieri 229:9981f62cdb1a 1368 //PRINTF("CONN_P2=%u\r\n",(unsigned)CONN_P2);
Andrea Palmieri 217:23870c55b07e 1369 if (BLE_STATUS_SUCCESS == ret){
Andrea Palmieri 217:23870c55b07e 1370 PRINTF("Observation Procedure Started\n");
Andrea Palmieri 217:23870c55b07e 1371 _scanning = true;
Andrea Palmieri 217:23870c55b07e 1372 return BLE_ERROR_NONE;
Andrea Palmieri 145:024a552e4715 1373 }
Andrea Palmieri 217:23870c55b07e 1374
Andrea Palmieri 217:23870c55b07e 1375 // Observer role is not supported by X-NUCLEO-IDB04A1, return BLE_ERROR_NOT_IMPLEMENTED
Andrea Palmieri 217:23870c55b07e 1376 switch (ret) {
Andrea Palmieri 217:23870c55b07e 1377 case BLE_STATUS_INVALID_CID:
Andrea Palmieri 229:9981f62cdb1a 1378 PRINTF("Observation Procedure not implemented!!!\n\r");
Andrea Palmieri 217:23870c55b07e 1379 return BLE_ERROR_NOT_IMPLEMENTED;
Andrea Palmieri 217:23870c55b07e 1380 default:
Andrea Palmieri 229:9981f62cdb1a 1381 PRINTF("Observation Procedure failed (0x%02X)\n\r", ret);
Andrea Palmieri 217:23870c55b07e 1382 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 132:51056160fa4a 1383 }
Andrea Palmieri 217:23870c55b07e 1384
Wolfgang Betz 130:770ce14d3d15 1385 }
Wolfgang Betz 130:770ce14d3d15 1386
Wolfgang Betz 130:770ce14d3d15 1387 ble_error_t BlueNRGGap::stopScan() {
Wolfgang Betz 165:3576598c0889 1388 tBleStatus ret = BLE_STATUS_SUCCESS;
Andrea Palmieri 229:9981f62cdb1a 1389
Andrea Palmieri 229:9981f62cdb1a 1390 PRINTF("stopScan\n\r");
Andrea Palmieri 217:23870c55b07e 1391 ret = aci_gap_terminate_gap_procedure(GAP_OBSERVATION_PROC);
Vincent Coubard 255:97adb68decd4 1392
Wolfgang Betz 132:51056160fa4a 1393 if (ret != BLE_STATUS_SUCCESS) {
Andrea Palmieri 238:b6454c839f42 1394 PRINTF("GAP Terminate Gap Procedure failed(ret=0x%x)\n", ret);
Vincent Coubard 255:97adb68decd4 1395 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 132:51056160fa4a 1396 } else {
Wolfgang Betz 132:51056160fa4a 1397 PRINTF("Discovery Procedure Terminated\n");
Vincent Coubard 255:97adb68decd4 1398 return BLE_ERROR_NONE;
Wolfgang Betz 132:51056160fa4a 1399 }
Wolfgang Betz 130:770ce14d3d15 1400 }
Wolfgang Betz 130:770ce14d3d15 1401
Wolfgang Betz 130:770ce14d3d15 1402 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1403 /*!
Wolfgang Betz 130:770ce14d3d15 1404 @brief set Tx power level
Wolfgang Betz 130:770ce14d3d15 1405 @param[in] txPower Transmission Power level
Wolfgang Betz 130:770ce14d3d15 1406 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 1407 */
Wolfgang Betz 130:770ce14d3d15 1408 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1409 ble_error_t BlueNRGGap::setTxPower(int8_t txPower)
Wolfgang Betz 130:770ce14d3d15 1410 {
Wolfgang Betz 130:770ce14d3d15 1411 tBleStatus ret;
Vincent Coubard 255:97adb68decd4 1412
Wolfgang Betz 130:770ce14d3d15 1413 int8_t enHighPower = 0;
Andrea Palmieri 201:a9bc2e8b01d7 1414 int8_t paLevel = 0;
Andrea Palmieri 201:a9bc2e8b01d7 1415
Andrea Palmieri 228:25368f053411 1416 ret = getHighPowerAndPALevelValue(txPower, enHighPower, paLevel);
Andrea Palmieri 228:25368f053411 1417 if(ret!=BLE_STATUS_SUCCESS) {
Andrea Palmieri 228:25368f053411 1418 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Andrea Palmieri 228:25368f053411 1419 }
Andrea Palmieri 201:a9bc2e8b01d7 1420
Vincent Coubard 255:97adb68decd4 1421 PRINTF("enHighPower=%d, paLevel=%d\n\r", enHighPower, paLevel);
Wolfgang Betz 130:770ce14d3d15 1422 ret = aci_hal_set_tx_power_level(enHighPower, paLevel);
Wolfgang Betz 130:770ce14d3d15 1423 if(ret!=BLE_STATUS_SUCCESS) {
Andrea Palmieri 228:25368f053411 1424 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 1425 }
Andrea Palmieri 201:a9bc2e8b01d7 1426
Wolfgang Betz 130:770ce14d3d15 1427 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 1428 }
Wolfgang Betz 130:770ce14d3d15 1429
Wolfgang Betz 130:770ce14d3d15 1430 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1431 /*!
Wolfgang Betz 130:770ce14d3d15 1432 @brief get permitted Tx power values
Wolfgang Betz 130:770ce14d3d15 1433 @param[in] values pointer to pointer to permitted power values
Vincent Coubard 255:97adb68decd4 1434 @param[in] num number of values
Wolfgang Betz 130:770ce14d3d15 1435 */
Wolfgang Betz 130:770ce14d3d15 1436 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1437 void BlueNRGGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP) {
Wolfgang Betz 130:770ce14d3d15 1438 static const int8_t permittedTxValues[] = {
Andrea Palmieri 229:9981f62cdb1a 1439 -18, -15, -14, -12, -11, -9, -8, -6, -5, -2, 0, 2, 4, 5, 8
Wolfgang Betz 130:770ce14d3d15 1440 };
Wolfgang Betz 130:770ce14d3d15 1441
Wolfgang Betz 130:770ce14d3d15 1442 *valueArrayPP = permittedTxValues;
Wolfgang Betz 130:770ce14d3d15 1443 *countP = sizeof(permittedTxValues) / sizeof(int8_t);
Wolfgang Betz 130:770ce14d3d15 1444 }
Wolfgang Betz 132:51056160fa4a 1445
Andrea Palmieri 229:9981f62cdb1a 1446 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1447 /*!
Andrea Palmieri 232:aead10288880 1448 @brief Set advertising parameters according to the current state
Andrea Palmieri 232:aead10288880 1449 Parameters value is set taking into account guidelines of the BlueNRG
Andrea Palmieri 232:aead10288880 1450 time slots allocation
Andrea Palmieri 232:aead10288880 1451 */
Andrea Palmieri 232:aead10288880 1452 /**************************************************************************/
Andrea Palmieri 232:aead10288880 1453 void BlueNRGGap::setAdvParameters(void)
Andrea Palmieri 232:aead10288880 1454 {
Andrea Palmieri 232:aead10288880 1455 uint32_t advIntMS;
Andrea Palmieri 232:aead10288880 1456
Andrea Palmieri 232:aead10288880 1457 if(state.connected == 1) {
Andrea Palmieri 232:aead10288880 1458 advIntMS = (conn_min_interval*1.25)-GUARD_INT;
Andrea Palmieri 232:aead10288880 1459 advInterval = _advParams.MSEC_TO_ADVERTISEMENT_DURATION_UNITS(advIntMS);
Vincent Coubard 256:146bb175ce80 1460
Vincent Coubard 259:323f588e5f57 1461 PRINTF("conn_min_interval is equal to %u\r\n", conn_min_interval);
Andrea Palmieri 232:aead10288880 1462 } else {
Andrea Palmieri 232:aead10288880 1463 advInterval = _advParams.getIntervalInADVUnits();
Andrea Palmieri 232:aead10288880 1464 }
Andrea Palmieri 232:aead10288880 1465 }
Andrea Palmieri 232:aead10288880 1466
Andrea Palmieri 232:aead10288880 1467 /**************************************************************************/
Andrea Palmieri 232:aead10288880 1468 /*!
Andrea Palmieri 229:9981f62cdb1a 1469 @brief Set connection parameters according to the current state (ADV and/or SCAN)
Andrea Palmieri 229:9981f62cdb1a 1470 Parameters value is set taking into account guidelines of the BlueNRG
Andrea Palmieri 229:9981f62cdb1a 1471 time slots allocation
Andrea Palmieri 229:9981f62cdb1a 1472 */
Andrea Palmieri 229:9981f62cdb1a 1473 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1474 void BlueNRGGap::setConnectionParameters(void)
Andrea Palmieri 229:9981f62cdb1a 1475 {
Andrea Palmieri 229:9981f62cdb1a 1476 if (state.advertising == 1) {
Andrea Palmieri 229:9981f62cdb1a 1477
Andrea Palmieri 232:aead10288880 1478 if (_scanningParams.getInterval() < advInterval) {
Andrea Palmieri 232:aead10288880 1479 PRINTF("state.adv=1 scanInterval<advInterval\r\n");
Andrea Palmieri 232:aead10288880 1480 scanInterval = advInterval;
Andrea Palmieri 232:aead10288880 1481 scanWindow = advInterval;
Andrea Palmieri 229:9981f62cdb1a 1482 } else {
Andrea Palmieri 232:aead10288880 1483 PRINTF("state.adv=1 scanInterval>=advInterval\r\n");
Andrea Palmieri 229:9981f62cdb1a 1484 scanInterval = _scanningParams.getInterval();
Andrea Palmieri 229:9981f62cdb1a 1485 scanWindow = _scanningParams.getWindow();
Andrea Palmieri 229:9981f62cdb1a 1486 }
Andrea Palmieri 234:3f6bba52b307 1487
Andrea Palmieri 234:3f6bba52b307 1488 if(advInterval>(MAX_INT_CONN-(GUARD_INT/1.25))) { //(4000-GUARD_INT)ms
Andrea Palmieri 234:3f6bba52b307 1489 conn_min_interval = MAX_INT_CONN;
Andrea Palmieri 234:3f6bba52b307 1490 conn_max_interval = MAX_INT_CONN;
Andrea Palmieri 234:3f6bba52b307 1491 } else {
Andrea Palmieri 234:3f6bba52b307 1492 conn_min_interval = (_advParams.ADVERTISEMENT_DURATION_UNITS_TO_MS(advInterval)+GUARD_INT)/1.25;
Andrea Palmieri 234:3f6bba52b307 1493 conn_max_interval = (_advParams.ADVERTISEMENT_DURATION_UNITS_TO_MS(advInterval)+GUARD_INT)/1.25;
Andrea Palmieri 234:3f6bba52b307 1494 }
Andrea Palmieri 229:9981f62cdb1a 1495
Andrea Palmieri 229:9981f62cdb1a 1496 } else {
Andrea Palmieri 229:9981f62cdb1a 1497
Andrea Palmieri 232:aead10288880 1498 PRINTF("state.adv = 0\r\n");
Andrea Palmieri 232:aead10288880 1499
Andrea Palmieri 229:9981f62cdb1a 1500 scanInterval = _scanningParams.getInterval();
Andrea Palmieri 229:9981f62cdb1a 1501 scanWindow = _scanningParams.getWindow();
Andrea Palmieri 234:3f6bba52b307 1502 if(SCAN_DURATION_UNITS_TO_MSEC(scanInterval)>(MAX_INT_CONN*1.25) ||
Andrea Palmieri 234:3f6bba52b307 1503 SCAN_DURATION_UNITS_TO_MSEC(scanInterval)<(MIN_INT_CONN*1.25)) { //(4000)ms || (7.5)ms
Andrea Palmieri 234:3f6bba52b307 1504 conn_min_interval = DEF_INT_CONN;
Andrea Palmieri 234:3f6bba52b307 1505 conn_max_interval = DEF_INT_CONN;
Andrea Palmieri 234:3f6bba52b307 1506 } else {
Andrea Palmieri 234:3f6bba52b307 1507 conn_min_interval = SCAN_DURATION_UNITS_TO_MSEC(scanInterval)/1.25;
Andrea Palmieri 234:3f6bba52b307 1508 conn_max_interval = SCAN_DURATION_UNITS_TO_MSEC(scanInterval)/1.25;
Andrea Palmieri 234:3f6bba52b307 1509 }
Andrea Palmieri 229:9981f62cdb1a 1510 }
Andrea Palmieri 229:9981f62cdb1a 1511 PRINTF("scanInterval=%u[msec]\r\n",SCAN_DURATION_UNITS_TO_MSEC(scanInterval));
Andrea Palmieri 229:9981f62cdb1a 1512 PRINTF("scanWindow()=%u[msec]\r\n",SCAN_DURATION_UNITS_TO_MSEC(scanWindow));
Andrea Palmieri 234:3f6bba52b307 1513 PRINTF("conn_min_interval=%u[msec]\r\n",(unsigned)(conn_min_interval*1.25));
Andrea Palmieri 234:3f6bba52b307 1514 PRINTF("conn_max_interval=%u[msec]\r\n",(unsigned)(conn_max_interval*1.25));
Andrea Palmieri 229:9981f62cdb1a 1515
Andrea Palmieri 229:9981f62cdb1a 1516 }
Andrea Palmieri 229:9981f62cdb1a 1517
Andrea Palmieri 181:749071129d2e 1518 ble_error_t BlueNRGGap::createConnection ()
Wolfgang Betz 132:51056160fa4a 1519 {
Wolfgang Betz 132:51056160fa4a 1520 tBleStatus ret;
Andrea Palmieri 229:9981f62cdb1a 1521
Andrea Palmieri 229:9981f62cdb1a 1522 /*
Andrea Palmieri 229:9981f62cdb1a 1523 Before creating connection, set parameters according
Andrea Palmieri 229:9981f62cdb1a 1524 to previous or current procedure (ADV and/or SCAN)
Andrea Palmieri 229:9981f62cdb1a 1525 */
Andrea Palmieri 229:9981f62cdb1a 1526 setConnectionParameters();
Andrea Palmieri 229:9981f62cdb1a 1527
Wolfgang Betz 132:51056160fa4a 1528 /*
Vincent Coubard 255:97adb68decd4 1529 Scan_Interval, Scan_Window, Peer_Address_Type, Peer_Address, Own_Address_Type, Conn_Interval_Min,
Vincent Coubard 255:97adb68decd4 1530 Conn_Interval_Max, Conn_Latency, Supervision_Timeout, Conn_Len_Min, Conn_Len_Max
Wolfgang Betz 132:51056160fa4a 1531 */
Andrea Palmieri 229:9981f62cdb1a 1532 ret = aci_gap_create_connection(scanInterval,
Andrea Palmieri 229:9981f62cdb1a 1533 scanWindow,
Andrea Palmieri 229:9981f62cdb1a 1534 _peerAddrType,
Wolfgang Betz 163:4c008b1089e9 1535 (unsigned char*)_peerAddr,
Andrea Palmieri 229:9981f62cdb1a 1536 addr_type,
Andrea Palmieri 229:9981f62cdb1a 1537 conn_min_interval, conn_max_interval, 0,
Andrea Palmieri 238:b6454c839f42 1538 SUPERV_TIMEOUT, CONN_L1, CONN_L1);
Wolfgang Betz 165:3576598c0889 1539
Andrea Palmieri 238:b6454c839f42 1540 //_connecting = false;
Vincent Coubard 255:97adb68decd4 1541
Wolfgang Betz 163:4c008b1089e9 1542 if (ret != BLE_STATUS_SUCCESS) {
Andrea Palmieri 229:9981f62cdb1a 1543 PRINTF("Error while starting connection (ret=0x%02X).\n\r", ret);
Wolfgang Betz 132:51056160fa4a 1544 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 132:51056160fa4a 1545 } else {
Wolfgang Betz 132:51056160fa4a 1546 PRINTF("Connection started.\n");
Andrea Palmieri 238:b6454c839f42 1547 _connecting = false;
Wolfgang Betz 132:51056160fa4a 1548 return BLE_ERROR_NONE;
Wolfgang Betz 132:51056160fa4a 1549 }
Wolfgang Betz 132:51056160fa4a 1550 }
Wolfgang Betz 132:51056160fa4a 1551
Wolfgang Betz 132:51056160fa4a 1552 ble_error_t BlueNRGGap::connect (const Gap::Address_t peerAddr,
Wolfgang Betz 132:51056160fa4a 1553 Gap::AddressType_t peerAddrType,
Wolfgang Betz 132:51056160fa4a 1554 const ConnectionParams_t *connectionParams,
Wolfgang Betz 132:51056160fa4a 1555 const GapScanningParams *scanParams)
Wolfgang Betz 132:51056160fa4a 1556 {
Andrea Palmieri 142:adf1567d3900 1557 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 1558 (void)connectionParams;
Andrea Palmieri 142:adf1567d3900 1559 (void)scanParams;
Andrea Palmieri 142:adf1567d3900 1560
Andrea Palmieri 217:23870c55b07e 1561 // Save the peer address
Wolfgang Betz 132:51056160fa4a 1562 for(int i=0; i<BDADDR_SIZE; i++) {
Wolfgang Betz 132:51056160fa4a 1563 _peerAddr[i] = peerAddr[i];
Wolfgang Betz 132:51056160fa4a 1564 }
Andrea Palmieri 229:9981f62cdb1a 1565 _peerAddrType = peerAddrType;
Wolfgang Betz 132:51056160fa4a 1566
Andrea Palmieri 181:749071129d2e 1567 _connecting = true;
Wolfgang Betz 132:51056160fa4a 1568
Wolfgang Betz 132:51056160fa4a 1569 if(_scanning) {
Wolfgang Betz 132:51056160fa4a 1570 stopScan();
Wolfgang Betz 132:51056160fa4a 1571 } else {
Wolfgang Betz 165:3576598c0889 1572 PRINTF("Calling createConnection from connect()\n\r");
Wolfgang Betz 165:3576598c0889 1573 return createConnection();
Wolfgang Betz 132:51056160fa4a 1574 }
Vincent Coubard 255:97adb68decd4 1575
Wolfgang Betz 132:51056160fa4a 1576 return BLE_ERROR_NONE;
Wolfgang Betz 132:51056160fa4a 1577 }
Andrea Palmieri 229:9981f62cdb1a 1578
Andrea Palmieri 229:9981f62cdb1a 1579 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1580 /*!
Andrea Palmieri 229:9981f62cdb1a 1581 @brief Set the advertising policy filter mode that will be used in
Andrea Palmieri 229:9981f62cdb1a 1582 the next call to startAdvertising().
Andrea Palmieri 229:9981f62cdb1a 1583
Andrea Palmieri 229:9981f62cdb1a 1584 @returns \ref ble_errror_t
Andrea Palmieri 229:9981f62cdb1a 1585
Andrea Palmieri 229:9981f62cdb1a 1586 @retval BLE_ERROR_NONE
Andrea Palmieri 229:9981f62cdb1a 1587 Everything executed properly.
Andrea Palmieri 229:9981f62cdb1a 1588
Andrea Palmieri 229:9981f62cdb1a 1589 BLE_ERROR_NOT_IMPLEMENTED
Andrea Palmieri 229:9981f62cdb1a 1590 This feature is currently note implemented.
Andrea Palmieri 229:9981f62cdb1a 1591 */
Andrea Palmieri 229:9981f62cdb1a 1592 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1593 ble_error_t BlueNRGGap::setAdvertisingPolicyMode(Gap::AdvertisingPolicyMode_t mode)
Andrea Palmieri 229:9981f62cdb1a 1594 {
Andrea Palmieri 229:9981f62cdb1a 1595 advertisingPolicyMode = mode;
Andrea Palmieri 229:9981f62cdb1a 1596
Andrea Palmieri 229:9981f62cdb1a 1597 return BLE_ERROR_NONE;
Andrea Palmieri 229:9981f62cdb1a 1598 }
Andrea Palmieri 229:9981f62cdb1a 1599
Andrea Palmieri 229:9981f62cdb1a 1600 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1601 /*!
Andrea Palmieri 229:9981f62cdb1a 1602 @brief Set the scanning policy filter mode that will be used in
Andrea Palmieri 229:9981f62cdb1a 1603 the next call to startAdvertising().
Andrea Palmieri 229:9981f62cdb1a 1604
Andrea Palmieri 229:9981f62cdb1a 1605 @returns \ref ble_errror_t
Andrea Palmieri 229:9981f62cdb1a 1606
Andrea Palmieri 229:9981f62cdb1a 1607 @retval BLE_ERROR_NONE
Andrea Palmieri 229:9981f62cdb1a 1608 Everything executed properly.
Andrea Palmieri 229:9981f62cdb1a 1609
Andrea Palmieri 229:9981f62cdb1a 1610 BLE_ERROR_NOT_IMPLEMENTED
Andrea Palmieri 229:9981f62cdb1a 1611 This feature is currently note implemented.
Andrea Palmieri 229:9981f62cdb1a 1612 */
Andrea Palmieri 229:9981f62cdb1a 1613 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1614 ble_error_t BlueNRGGap::setScanningPolicyMode(Gap::ScanningPolicyMode_t mode)
Andrea Palmieri 229:9981f62cdb1a 1615 {
Andrea Palmieri 229:9981f62cdb1a 1616 scanningPolicyMode = mode;
Andrea Palmieri 229:9981f62cdb1a 1617
Andrea Palmieri 229:9981f62cdb1a 1618 return BLE_ERROR_NONE;
Andrea Palmieri 229:9981f62cdb1a 1619 }
Andrea Palmieri 229:9981f62cdb1a 1620
Andrea Palmieri 229:9981f62cdb1a 1621 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1622 /*!
Andrea Palmieri 229:9981f62cdb1a 1623 @brief Get the current advertising policy filter mode.
Andrea Palmieri 229:9981f62cdb1a 1624
Andrea Palmieri 229:9981f62cdb1a 1625 @returns The advertising policy filter mode.
Andrea Palmieri 229:9981f62cdb1a 1626 */
Andrea Palmieri 229:9981f62cdb1a 1627 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1628 Gap::AdvertisingPolicyMode_t BlueNRGGap::getAdvertisingPolicyMode(void) const
Andrea Palmieri 229:9981f62cdb1a 1629 {
Andrea Palmieri 229:9981f62cdb1a 1630 return advertisingPolicyMode;
Andrea Palmieri 229:9981f62cdb1a 1631 }
Andrea Palmieri 229:9981f62cdb1a 1632
Andrea Palmieri 229:9981f62cdb1a 1633 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1634 /*!
Andrea Palmieri 229:9981f62cdb1a 1635 @brief Get the current scanning policy filter mode.
Andrea Palmieri 229:9981f62cdb1a 1636
Andrea Palmieri 229:9981f62cdb1a 1637 @returns The scanning policy filter mode.
Andrea Palmieri 229:9981f62cdb1a 1638
Andrea Palmieri 229:9981f62cdb1a 1639 */
Andrea Palmieri 229:9981f62cdb1a 1640 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1641 Gap::ScanningPolicyMode_t BlueNRGGap::getScanningPolicyMode(void) const
Andrea Palmieri 229:9981f62cdb1a 1642 {
Andrea Palmieri 229:9981f62cdb1a 1643 return scanningPolicyMode;
Andrea Palmieri 229:9981f62cdb1a 1644 }
Andrea Palmieri 229:9981f62cdb1a 1645
Andrea Palmieri 229:9981f62cdb1a 1646 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1647 /*!
Andrea Palmieri 229:9981f62cdb1a 1648 @brief Clear BlueNRGGap's state.
Andrea Palmieri 229:9981f62cdb1a 1649
Andrea Palmieri 229:9981f62cdb1a 1650 @returns ble_error_t
Andrea Palmieri 229:9981f62cdb1a 1651
Andrea Palmieri 229:9981f62cdb1a 1652 @retval BLE_ERROR_NONE
Andrea Palmieri 229:9981f62cdb1a 1653 Everything executed properly
Andrea Palmieri 229:9981f62cdb1a 1654 */
Andrea Palmieri 229:9981f62cdb1a 1655 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 1656 ble_error_t BlueNRGGap::reset(void)
Andrea Palmieri 229:9981f62cdb1a 1657 {
Andrea Palmieri 229:9981f62cdb1a 1658 /* Clear all state that is from the parent, including private members */
Andrea Palmieri 229:9981f62cdb1a 1659 if (Gap::reset() != BLE_ERROR_NONE) {
Andrea Palmieri 229:9981f62cdb1a 1660 return BLE_ERROR_INVALID_STATE;
Andrea Palmieri 229:9981f62cdb1a 1661 }
Andrea Palmieri 229:9981f62cdb1a 1662
Andrea Palmieri 229:9981f62cdb1a 1663 /* Clear derived class members */
Andrea Palmieri 229:9981f62cdb1a 1664 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
Andrea Palmieri 229:9981f62cdb1a 1665
Andrea Palmieri 229:9981f62cdb1a 1666 memset(deviceAppearance, 0, sizeof(deviceAppearance));
Andrea Palmieri 229:9981f62cdb1a 1667 memset(local_name, 0, LOCAL_NAME_MAX_SIZE);
Andrea Palmieri 229:9981f62cdb1a 1668 memset(local_name, 0, UUID_BUFFER_SIZE);
Andrea Palmieri 229:9981f62cdb1a 1669 memset(AdvData, 0, ADV_DATA_MAX_SIZE);
Andrea Palmieri 229:9981f62cdb1a 1670
Andrea Palmieri 229:9981f62cdb1a 1671 /* Set the whitelist policy filter modes to IGNORE_WHITELIST */
Andrea Palmieri 229:9981f62cdb1a 1672 advertisingPolicyMode = Gap::ADV_POLICY_IGNORE_WHITELIST;
Andrea Palmieri 229:9981f62cdb1a 1673 scanningPolicyMode = Gap::SCAN_POLICY_IGNORE_WHITELIST;
Andrea Palmieri 229:9981f62cdb1a 1674
Andrea Palmieri 229:9981f62cdb1a 1675 return BLE_ERROR_NONE;
Vincent Coubard 256:146bb175ce80 1676 }
Vincent Coubard 256:146bb175ce80 1677
Vincent Coubard 256:146bb175ce80 1678 void BlueNRGGap::setConnectionInterval(uint16_t interval) {
Vincent Coubard 256:146bb175ce80 1679 conn_min_interval = interval;
Vincent Coubard 256:146bb175ce80 1680 conn_max_interval = interval;
Vincent Coubard 255:97adb68decd4 1681 }