adaptation for bkap

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Vincent Coubard
Date:
Thu Sep 15 10:51:27 2016 +0100
Branch:
sync_with_github
Revision:
265:46333d70cf81
Parent:
254:f4d217cadbde
Child:
276:a20f4cad1d61
Sync with a3160cd9f64b84d00a671330ffb5e4137a1e4f7b

2016-07-11 13:21:11+02:00: Andrea Palmieri
Update Address declaration types

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