BLE test

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Vincent Coubard
Date:
Thu Sep 15 10:51:29 2016 +0100
Branch:
ed913d4b8f4131a00dc3184889a36725d080f65f
Revision:
266:b49e28134d83
Parent:
259:323f588e5f57
Child:
268:c0a1e03c5736
Sync with ed913d4b8f4131a00dc3184889a36725d080f65f

2016-07-11 18:33:50+01:00: Vincent Coubard
implement set/get preferered connection parameters

Who changed what in which revision?

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