adaptation for bkap

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Andrea Palmieri
Date:
Thu Jun 09 17:16:36 2016 +0200
Revision:
236:2a73f3a97452
Parent:
234:3f6bba52b307
Child:
237:64c2ddbd9329
Fix issue on ADV payload de-allocation

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