adaptation for bkap

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Andrea Palmieri
Date:
Tue Jun 14 14:45:08 2016 +0200
Revision:
238:b6454c839f42
Parent:
237:64c2ddbd9329
Child:
239:8e93d6e26c55
Filter out ADV report while creating a connection and fix ADV data update

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

Who changed what in which revision?

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