BLE test

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Andrea Palmieri
Date:
Mon Jun 27 15:51:20 2016 +0200
Revision:
252:0c2cb16a7166
Parent:
242:058b2e731adc
Parent:
251:86df2c289e7c
Child:
253:9665a6f8bbdb
Merge branch 'master' into mbed_classic

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