adaptation for bkap

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Andrea Palmieri
Date:
Tue Apr 05 11:23:33 2016 +0200
Revision:
220:e02936f0d4c7
Parent:
217:23870c55b07e
Child:
222:37a7e289ee3d
Fix setting of ADV and CONN intervals

- Introduce STACK MODE 3 allowing a central to advertize while a connection is ongoing
- Cosmetics

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 130:770ce14d3d15 1 /* mbed Microcontroller Library
Wolfgang Betz 130:770ce14d3d15 2 * Copyright (c) 2006-2013 ARM Limited
Wolfgang Betz 130:770ce14d3d15 3 *
Wolfgang Betz 130:770ce14d3d15 4 * Licensed under the Apache License, Version 2.0 (the "License");
Wolfgang Betz 130:770ce14d3d15 5 * you may not use this file except in compliance with the License.
Wolfgang Betz 130:770ce14d3d15 6 * You may obtain a copy of the License at
Wolfgang Betz 130:770ce14d3d15 7 *
Wolfgang Betz 130:770ce14d3d15 8 * http://www.apache.org/licenses/LICENSE-2.0
Wolfgang Betz 130:770ce14d3d15 9 *
Wolfgang Betz 130:770ce14d3d15 10 * Unless required by applicable law or agreed to in writing, software
Wolfgang Betz 130:770ce14d3d15 11 * distributed under the License is distributed on an "AS IS" BASIS,
Wolfgang Betz 130:770ce14d3d15 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Wolfgang Betz 130:770ce14d3d15 13 * See the License for the specific language governing permissions and
Wolfgang Betz 130:770ce14d3d15 14 * limitations under the License.
Wolfgang Betz 130:770ce14d3d15 15 */
Wolfgang Betz 130:770ce14d3d15 16
Wolfgang Betz 130:770ce14d3d15 17
Wolfgang Betz 130:770ce14d3d15 18 /**
Wolfgang Betz 130:770ce14d3d15 19 ******************************************************************************
Wolfgang Betz 130:770ce14d3d15 20 * @file BlueNRGGap.cpp
Wolfgang Betz 130:770ce14d3d15 21 * @author STMicroelectronics
Wolfgang Betz 130:770ce14d3d15 22 * @brief Implementation of BLE_API Gap Class
Wolfgang Betz 130:770ce14d3d15 23 ******************************************************************************
Wolfgang Betz 130:770ce14d3d15 24 * @copy
Wolfgang Betz 130:770ce14d3d15 25 *
Wolfgang Betz 130:770ce14d3d15 26 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
Wolfgang Betz 130:770ce14d3d15 27 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
Wolfgang Betz 130:770ce14d3d15 28 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
Wolfgang Betz 130:770ce14d3d15 29 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
Wolfgang Betz 130:770ce14d3d15 30 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
Wolfgang Betz 130:770ce14d3d15 31 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
Wolfgang Betz 130:770ce14d3d15 32 *
Wolfgang Betz 130:770ce14d3d15 33 * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
Wolfgang Betz 130:770ce14d3d15 34 */
Wolfgang Betz 130:770ce14d3d15 35
Wolfgang Betz 130:770ce14d3d15 36 // ANDREA: Changed some types (e.g., tHalUint8 --> uint8_t)
Wolfgang Betz 130:770ce14d3d15 37
Wolfgang Betz 130:770ce14d3d15 38 /** @defgroup BlueNRGGap
Wolfgang Betz 130:770ce14d3d15 39 * @brief BlueNRG BLE_API GAP Adaptation
Wolfgang Betz 130:770ce14d3d15 40 * @{
Wolfgang Betz 130:770ce14d3d15 41 */
Wolfgang Betz 130:770ce14d3d15 42
Wolfgang Betz 130:770ce14d3d15 43 #include "BlueNRGDevice.h"
Wolfgang Betz 144:bdf5e8432131 44 #include "mbed-drivers/mbed.h"
Wolfgang Betz 130:770ce14d3d15 45 #include "Payload.h"
Wolfgang Betz 130:770ce14d3d15 46 #include "Utils.h"
Wolfgang Betz 132:51056160fa4a 47 #include "debug.h"
Wolfgang Betz 130:770ce14d3d15 48
Wolfgang Betz 130:770ce14d3d15 49 //Local Variables
Wolfgang Betz 130:770ce14d3d15 50 //const char *local_name = NULL;
Wolfgang Betz 130:770ce14d3d15 51 //uint8_t local_name_length = 0;
Wolfgang Betz 130:770ce14d3d15 52 const uint8_t *scan_response_payload = NULL;
Wolfgang Betz 130:770ce14d3d15 53 uint8_t scan_rsp_length = 0;
Wolfgang Betz 130:770ce14d3d15 54
Wolfgang Betz 130:770ce14d3d15 55 uint32_t advtInterval = BLUENRG_GAP_ADV_INTERVAL_MAX;
Wolfgang Betz 130:770ce14d3d15 56
Wolfgang Betz 130:770ce14d3d15 57 /*
Wolfgang Betz 130:770ce14d3d15 58 * Utility to process GAP specific events (e.g., Advertising timeout)
Wolfgang Betz 130:770ce14d3d15 59 */
Wolfgang Betz 130:770ce14d3d15 60 void BlueNRGGap::Process(void)
Wolfgang Betz 130:770ce14d3d15 61 {
Wolfgang Betz 130:770ce14d3d15 62 if(AdvToFlag) {
Wolfgang Betz 130:770ce14d3d15 63 stopAdvertising();
Wolfgang Betz 130:770ce14d3d15 64 }
Wolfgang Betz 130:770ce14d3d15 65
Wolfgang Betz 130:770ce14d3d15 66 }
Wolfgang Betz 130:770ce14d3d15 67
Wolfgang Betz 130:770ce14d3d15 68 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 69 /*!
Wolfgang Betz 130:770ce14d3d15 70 @brief Sets the advertising parameters and payload for the device.
Wolfgang Betz 130:770ce14d3d15 71 Note: Some data types give error when their adv data is updated using aci_gap_update_adv_data() API
Wolfgang Betz 130:770ce14d3d15 72
Wolfgang Betz 130:770ce14d3d15 73 @params[in] advData
Wolfgang Betz 130:770ce14d3d15 74 The primary advertising data payload
Wolfgang Betz 130:770ce14d3d15 75 @params[in] scanResponse
Wolfgang Betz 130:770ce14d3d15 76 The optional Scan Response payload if the advertising
Wolfgang Betz 130:770ce14d3d15 77 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
Wolfgang Betz 130:770ce14d3d15 78 in \ref GapAdveritinngParams
Wolfgang Betz 130:770ce14d3d15 79
Wolfgang Betz 130:770ce14d3d15 80 @returns \ref ble_error_t
Wolfgang Betz 130:770ce14d3d15 81
Wolfgang Betz 130:770ce14d3d15 82 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 83 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 84
Wolfgang Betz 130:770ce14d3d15 85 @retval BLE_ERROR_BUFFER_OVERFLOW
Wolfgang Betz 130:770ce14d3d15 86 The proposed action would cause a buffer overflow. All
Wolfgang Betz 130:770ce14d3d15 87 advertising payloads must be <= 31 bytes, for example.
Wolfgang Betz 130:770ce14d3d15 88
Wolfgang Betz 130:770ce14d3d15 89 @retval BLE_ERROR_NOT_IMPLEMENTED
Wolfgang Betz 130:770ce14d3d15 90 A feature was requested that is not yet supported in the
Wolfgang Betz 130:770ce14d3d15 91 nRF51 firmware or hardware.
Wolfgang Betz 130:770ce14d3d15 92
Wolfgang Betz 130:770ce14d3d15 93 @retval BLE_ERROR_PARAM_OUT_OF_RANGE
Wolfgang Betz 130:770ce14d3d15 94 One of the proposed values is outside the valid range.
Wolfgang Betz 130:770ce14d3d15 95
Wolfgang Betz 130:770ce14d3d15 96 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 97
Wolfgang Betz 130:770ce14d3d15 98 @code
Wolfgang Betz 130:770ce14d3d15 99
Wolfgang Betz 130:770ce14d3d15 100 @endcode
Wolfgang Betz 130:770ce14d3d15 101 */
Wolfgang Betz 130:770ce14d3d15 102 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 103 ble_error_t BlueNRGGap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse)
Wolfgang Betz 130:770ce14d3d15 104 {
Wolfgang Betz 132:51056160fa4a 105 PRINTF("BlueNRGGap::setAdvertisingData\n\r");
Wolfgang Betz 130:770ce14d3d15 106 /* Make sure we don't exceed the advertising payload length */
Wolfgang Betz 130:770ce14d3d15 107 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
Wolfgang Betz 130:770ce14d3d15 108 return BLE_ERROR_BUFFER_OVERFLOW;
Wolfgang Betz 130:770ce14d3d15 109 }
Wolfgang Betz 130:770ce14d3d15 110
Wolfgang Betz 130:770ce14d3d15 111 /* Make sure we have a payload! */
Wolfgang Betz 130:770ce14d3d15 112 if (advData.getPayloadLen() <= 0) {
Wolfgang Betz 130:770ce14d3d15 113 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 114 } else {
Wolfgang Betz 130:770ce14d3d15 115 PayloadPtr loadPtr(advData.getPayload(), advData.getPayloadLen());
Wolfgang Betz 130:770ce14d3d15 116 for(uint8_t index=0; index<loadPtr.getPayloadUnitCount(); index++) {
Wolfgang Betz 130:770ce14d3d15 117 loadPtr.getUnitAtIndex(index);
Wolfgang Betz 130:770ce14d3d15 118
Wolfgang Betz 132:51056160fa4a 119 PRINTF("adData[%d].length=%d\n\r", index,(uint8_t)(*loadPtr.getUnitAtIndex(index).getLenPtr()));
Wolfgang Betz 132:51056160fa4a 120 PRINTF("adData[%d].AdType=0x%x\n\r", index,(uint8_t)(*loadPtr.getUnitAtIndex(index).getAdTypePtr()));
Wolfgang Betz 130:770ce14d3d15 121
Wolfgang Betz 130:770ce14d3d15 122 switch(*loadPtr.getUnitAtIndex(index).getAdTypePtr()) {
Wolfgang Betz 130:770ce14d3d15 123 case GapAdvertisingData::FLAGS: /* ref *Flags */
Wolfgang Betz 130:770ce14d3d15 124 {
Wolfgang Betz 132:51056160fa4a 125 PRINTF("Advertising type: FLAGS\n\r");
Wolfgang Betz 130:770ce14d3d15 126 //Check if Flags are OK. BlueNRG only supports LE Mode.
Wolfgang Betz 130:770ce14d3d15 127 uint8_t *flags = loadPtr.getUnitAtIndex(index).getDataPtr();
Wolfgang Betz 130:770ce14d3d15 128 if((*flags & GapAdvertisingData::BREDR_NOT_SUPPORTED) != GapAdvertisingData::BREDR_NOT_SUPPORTED) {
Wolfgang Betz 132:51056160fa4a 129 PRINTF("BlueNRG does not support BR/EDR Mode");
Wolfgang Betz 130:770ce14d3d15 130 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 131 }
Wolfgang Betz 130:770ce14d3d15 132
Wolfgang Betz 130:770ce14d3d15 133 break;
Wolfgang Betz 130:770ce14d3d15 134 }
Wolfgang Betz 130:770ce14d3d15 135 case GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS: /**< Incomplete list of 16-bit Service IDs */
Wolfgang Betz 130:770ce14d3d15 136 case GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS: /**< Complete list of 16-bit Service IDs */
Andrea Palmieri 189:30fa96fa8ec4 137 case GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS: /**< Incomplete list of 128-bit Service IDs */
Andrea Palmieri 189:30fa96fa8ec4 138 case GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS: /**< Complete list of 128-bit Service IDs */
Wolfgang Betz 130:770ce14d3d15 139 {
Andrea Palmieri 189:30fa96fa8ec4 140 PRINTF("Advertising type: INCOMPLETE_LIST SERVICE_IDS/COMPLETE_LIST SERVICE_IDS\n\r");
Wolfgang Betz 130:770ce14d3d15 141
Wolfgang Betz 130:770ce14d3d15 142 uint8_t buffSize = *loadPtr.getUnitAtIndex(index).getLenPtr()-1;
Wolfgang Betz 130:770ce14d3d15 143 // The total lenght should include the Data Type Value
Wolfgang Betz 130:770ce14d3d15 144 if(buffSize>UUID_BUFFER_SIZE-1) {
Andrea Palmieri 189:30fa96fa8ec4 145 return BLE_ERROR_INVALID_PARAM;
Wolfgang Betz 130:770ce14d3d15 146 }
Wolfgang Betz 130:770ce14d3d15 147
Wolfgang Betz 130:770ce14d3d15 148 servUuidlength = buffSize+1; // +1 to include the Data Type Value
Wolfgang Betz 130:770ce14d3d15 149 servUuidData[0] = (uint8_t)(*loadPtr.getUnitAtIndex(index).getAdTypePtr()); //Data Type Value
Wolfgang Betz 130:770ce14d3d15 150
Wolfgang Betz 132:51056160fa4a 151 PRINTF("servUuidlength=%d servUuidData[0]=%d buffSize=%d\n\r", servUuidlength, servUuidData[0], buffSize);
Wolfgang Betz 130:770ce14d3d15 152 // Save the Service UUID list just after the Data Type Value field
Wolfgang Betz 130:770ce14d3d15 153 memcpy(servUuidData+1, loadPtr.getUnitAtIndex(index).getDataPtr(), buffSize);
Andrea Palmieri 189:30fa96fa8ec4 154 #ifdef DEBUG
Wolfgang Betz 130:770ce14d3d15 155 for(unsigned i=0; i<servUuidlength; i++) {
Wolfgang Betz 132:51056160fa4a 156 PRINTF("servUuidData[%d] = 0x%x\n\r", i, servUuidData[i]);
Wolfgang Betz 130:770ce14d3d15 157 }
Wolfgang Betz 130:770ce14d3d15 158
Wolfgang Betz 130:770ce14d3d15 159 for(unsigned i=0; i<buffSize; i++) {
Wolfgang Betz 132:51056160fa4a 160 PRINTF("loadPtr.getUnitAtIndex(index).getDataPtr()[%d] = 0x%x\n\r", i, loadPtr.getUnitAtIndex(index).getDataPtr()[i]);
Wolfgang Betz 130:770ce14d3d15 161 }
Andrea Palmieri 189:30fa96fa8ec4 162 #endif /* DEBUG */
Wolfgang Betz 130:770ce14d3d15 163 break;
Wolfgang Betz 130:770ce14d3d15 164 }
Wolfgang Betz 130:770ce14d3d15 165 case GapAdvertisingData::INCOMPLETE_LIST_32BIT_SERVICE_IDS: /**< Incomplete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */
Wolfgang Betz 130:770ce14d3d15 166 {
Wolfgang Betz 132:51056160fa4a 167 PRINTF("Advertising type: INCOMPLETE_LIST_32BIT_SERVICE_IDS\n\r");
Wolfgang Betz 130:770ce14d3d15 168 return BLE_ERROR_NOT_IMPLEMENTED;
Wolfgang Betz 130:770ce14d3d15 169 }
Wolfgang Betz 130:770ce14d3d15 170 case GapAdvertisingData::COMPLETE_LIST_32BIT_SERVICE_IDS: /**< Complete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */
Wolfgang Betz 130:770ce14d3d15 171 {
Wolfgang Betz 132:51056160fa4a 172 PRINTF("Advertising type: COMPLETE_LIST_32BIT_SERVICE_IDS\n\r");
Wolfgang Betz 130:770ce14d3d15 173 return BLE_ERROR_NOT_IMPLEMENTED;
Wolfgang Betz 130:770ce14d3d15 174 }
Wolfgang Betz 130:770ce14d3d15 175 case GapAdvertisingData::SHORTENED_LOCAL_NAME: /**< Shortened Local Name */
Wolfgang Betz 130:770ce14d3d15 176 {
Wolfgang Betz 130:770ce14d3d15 177 break;
Wolfgang Betz 130:770ce14d3d15 178 }
Wolfgang Betz 130:770ce14d3d15 179 case GapAdvertisingData::COMPLETE_LOCAL_NAME: /**< Complete Local Name */
Wolfgang Betz 130:770ce14d3d15 180 {
Wolfgang Betz 132:51056160fa4a 181 PRINTF("Advertising type: COMPLETE_LOCAL_NAME\n\r");
Wolfgang Betz 130:770ce14d3d15 182 loadPtr.getUnitAtIndex(index).printDataAsString();
Wolfgang Betz 130:770ce14d3d15 183 local_name_length = *loadPtr.getUnitAtIndex(index).getLenPtr()-1;
Wolfgang Betz 130:770ce14d3d15 184 local_name = (uint8_t*)loadPtr.getUnitAtIndex(index).getAdTypePtr();
Wolfgang Betz 132:51056160fa4a 185 PRINTF("Advertising type: COMPLETE_LOCAL_NAME local_name=%s\n\r", local_name);
Wolfgang Betz 130:770ce14d3d15 186 //COMPLETE_LOCAL_NAME is only advertising device name. Gatt Device Name is not the same.(Must be set right after GAP/GATT init?)
Wolfgang Betz 130:770ce14d3d15 187
Wolfgang Betz 132:51056160fa4a 188 PRINTF("device_name length=%d\n\r", local_name_length);
Wolfgang Betz 130:770ce14d3d15 189 break;
Wolfgang Betz 130:770ce14d3d15 190 }
Wolfgang Betz 130:770ce14d3d15 191 case GapAdvertisingData::TX_POWER_LEVEL: /**< TX Power Level (in dBm) */
Wolfgang Betz 130:770ce14d3d15 192 {
Wolfgang Betz 132:51056160fa4a 193 PRINTF("Advertising type: TX_POWER_LEVEL\n\r");
Wolfgang Betz 130:770ce14d3d15 194 int8_t enHighPower = 0;
Wolfgang Betz 130:770ce14d3d15 195 int8_t paLevel = 0;
Wolfgang Betz 132:51056160fa4a 196 #ifdef DEBUG
Wolfgang Betz 130:770ce14d3d15 197 int8_t dbm = *loadPtr.getUnitAtIndex(index).getDataPtr();
Wolfgang Betz 130:770ce14d3d15 198 int8_t dbmActuallySet = getHighPowerAndPALevelValue(dbm, enHighPower, paLevel);
Wolfgang Betz 130:770ce14d3d15 199 #endif
Wolfgang Betz 132:51056160fa4a 200 PRINTF("dbm=%d, dbmActuallySet=%d\n\r", dbm, dbmActuallySet);
Wolfgang Betz 132:51056160fa4a 201 PRINTF("enHighPower=%d, paLevel=%d\n\r", enHighPower, paLevel);
Wolfgang Betz 130:770ce14d3d15 202 aci_hal_set_tx_power_level(enHighPower, paLevel);
Wolfgang Betz 130:770ce14d3d15 203 break;
Wolfgang Betz 130:770ce14d3d15 204 }
Wolfgang Betz 130:770ce14d3d15 205 case GapAdvertisingData::DEVICE_ID: /**< Device ID */
Wolfgang Betz 130:770ce14d3d15 206 {
Wolfgang Betz 130:770ce14d3d15 207 break;
Wolfgang Betz 130:770ce14d3d15 208 }
Wolfgang Betz 130:770ce14d3d15 209 case GapAdvertisingData::SLAVE_CONNECTION_INTERVAL_RANGE: /**< Slave :Connection Interval Range */
Wolfgang Betz 130:770ce14d3d15 210 {
Wolfgang Betz 130:770ce14d3d15 211 break;
Wolfgang Betz 130:770ce14d3d15 212 }
Wolfgang Betz 130:770ce14d3d15 213 case GapAdvertisingData::SERVICE_DATA: /**< Service Data */
Wolfgang Betz 130:770ce14d3d15 214 {
Wolfgang Betz 132:51056160fa4a 215 PRINTF("Advertising type: SERVICE_DATA\n\r");
Wolfgang Betz 130:770ce14d3d15 216 uint8_t buffSize = *loadPtr.getUnitAtIndex(index).getLenPtr()-1;
Wolfgang Betz 132:51056160fa4a 217 PRINTF("Advertising type: SERVICE_DATA (buffSize=%d)\n\r", buffSize);
Wolfgang Betz 130:770ce14d3d15 218 // 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 219 if(buffSize>ADV_DATA_MAX_SIZE-2) {
Wolfgang Betz 130:770ce14d3d15 220 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 221 }
Wolfgang Betz 130:770ce14d3d15 222 for(int i=0; i<buffSize+1; i++) {
Wolfgang Betz 132:51056160fa4a 223 PRINTF("Advertising type: SERVICE_DATA loadPtr.getUnitAtIndex(index).getDataPtr()[%d] = 0x%x\n\r", i, loadPtr.getUnitAtIndex(index).getDataPtr()[i]);
Wolfgang Betz 130:770ce14d3d15 224 }
Wolfgang Betz 130:770ce14d3d15 225 AdvLen = buffSize+2; // the total ADV DATA LEN should include two more bytes: the buffer size byte; and the Service Data Type Value byte
Wolfgang Betz 130:770ce14d3d15 226 AdvData[0] = buffSize+1; // the fisrt byte is the data buffer size (type+data)
Wolfgang Betz 130:770ce14d3d15 227 AdvData[1] = AD_TYPE_SERVICE_DATA;
Wolfgang Betz 130:770ce14d3d15 228 memcpy(AdvData+2, loadPtr.getUnitAtIndex(index).getDataPtr(), buffSize);
Wolfgang Betz 130:770ce14d3d15 229 break;
Wolfgang Betz 130:770ce14d3d15 230 }
Wolfgang Betz 130:770ce14d3d15 231 case GapAdvertisingData::APPEARANCE:
Wolfgang Betz 130:770ce14d3d15 232 {
Wolfgang Betz 130:770ce14d3d15 233 /*
Wolfgang Betz 130:770ce14d3d15 234 Tested with GapAdvertisingData::GENERIC_PHONE.
Wolfgang Betz 130:770ce14d3d15 235 for other appearances BLE Scanner android app is not behaving properly
Wolfgang Betz 130:770ce14d3d15 236 */
Wolfgang Betz 132:51056160fa4a 237 PRINTF("Advertising type: APPEARANCE\n\r");
Wolfgang Betz 130:770ce14d3d15 238 const char *deviceAppearance = NULL;
Wolfgang Betz 130:770ce14d3d15 239 deviceAppearance = (const char*)loadPtr.getUnitAtIndex(index).getDataPtr(); // to be set later when startAdvertising() is called
Wolfgang Betz 130:770ce14d3d15 240
Wolfgang Betz 132:51056160fa4a 241 #ifdef DEBUG
Wolfgang Betz 130:770ce14d3d15 242 uint8_t Appearance[2] = {0, 0};
Wolfgang Betz 130:770ce14d3d15 243 uint16_t devP = (uint16_t)*deviceAppearance;
Wolfgang Betz 130:770ce14d3d15 244 STORE_LE_16(Appearance, (uint16_t)devP);
Wolfgang Betz 130:770ce14d3d15 245 #endif
Wolfgang Betz 130:770ce14d3d15 246
Wolfgang Betz 132:51056160fa4a 247 PRINTF("input: deviceAppearance= 0x%x 0x%x..., strlen(deviceAppearance)=%d\n\r", Appearance[1], Appearance[0], (uint8_t)*loadPtr.getUnitAtIndex(index).getLenPtr()-1); /**< \ref Appearance */
Wolfgang Betz 130:770ce14d3d15 248
Wolfgang Betz 130:770ce14d3d15 249 aci_gatt_update_char_value(g_gap_service_handle, g_appearance_char_handle, 0, 2, (uint8_t *)deviceAppearance);//not using array Appearance[2]
Wolfgang Betz 130:770ce14d3d15 250 break;
Wolfgang Betz 130:770ce14d3d15 251 }
Wolfgang Betz 130:770ce14d3d15 252 case GapAdvertisingData::ADVERTISING_INTERVAL: /**< Advertising Interval */
Wolfgang Betz 130:770ce14d3d15 253 {
Wolfgang Betz 132:51056160fa4a 254 PRINTF("Advertising type: ADVERTISING_INTERVAL\n\r");
Wolfgang Betz 130:770ce14d3d15 255 advtInterval = (uint16_t)(*loadPtr.getUnitAtIndex(index).getDataPtr());
Wolfgang Betz 135:f0b6cf8ac4fc 256 PRINTF("advtInterval=%d\n\r", (int)advtInterval);
Wolfgang Betz 130:770ce14d3d15 257 break;
Wolfgang Betz 130:770ce14d3d15 258 }
Andrea Palmieri 182:e7cff2f17ef9 259 case GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA: /**< Manufacturer Specific Data */
Wolfgang Betz 130:770ce14d3d15 260 {
Andrea Palmieri 182:e7cff2f17ef9 261 PRINTF("Advertising type: MANUFACTURER_SPECIFIC_DATA\n\r");
Andrea Palmieri 182:e7cff2f17ef9 262 uint8_t buffSize = *loadPtr.getUnitAtIndex(index).getLenPtr()-1;
Andrea Palmieri 182:e7cff2f17ef9 263 PRINTF("Advertising type: MANUFACTURER_SPECIFIC_DATA (buffSize=%d)\n\r", buffSize);
Andrea Palmieri 182:e7cff2f17ef9 264 // the total ADV DATA LEN should include two more bytes:
Andrea Palmieri 182:e7cff2f17ef9 265 // the buffer size byte;
Andrea Palmieri 182:e7cff2f17ef9 266 // and the Manufacturer Specific Data Type Value byte
Andrea Palmieri 182:e7cff2f17ef9 267 if(buffSize>ADV_DATA_MAX_SIZE-2) {
Andrea Palmieri 182:e7cff2f17ef9 268 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Andrea Palmieri 182:e7cff2f17ef9 269 }
Andrea Palmieri 182:e7cff2f17ef9 270 for(int i=0; i<buffSize+1; i++) {
Andrea Palmieri 182:e7cff2f17ef9 271 PRINTF("Advertising type: MANUFACTURER_SPECIFIC_DATA loadPtr.getUnitAtIndex(index).getDataPtr()[%d] = 0x%x\n\r",
Andrea Palmieri 182:e7cff2f17ef9 272 i, loadPtr.getUnitAtIndex(index).getDataPtr()[i]);
Andrea Palmieri 182:e7cff2f17ef9 273 }
Andrea Palmieri 182:e7cff2f17ef9 274 AdvLen = buffSize+2; // the total ADV DATA LEN should include two more bytes: the buffer size byte; and the Manufacturer Specific Data Type Value byte
Andrea Palmieri 182:e7cff2f17ef9 275 AdvData[0] = buffSize+1; // the fisrt byte is the data buffer size (type+data)
Andrea Palmieri 182:e7cff2f17ef9 276 AdvData[1] = AD_TYPE_MANUFACTURER_SPECIFIC_DATA;
Andrea Palmieri 182:e7cff2f17ef9 277 memcpy(AdvData+2, loadPtr.getUnitAtIndex(index).getDataPtr(), buffSize);
Wolfgang Betz 130:770ce14d3d15 278 break;
Wolfgang Betz 130:770ce14d3d15 279 }
Wolfgang Betz 130:770ce14d3d15 280
Wolfgang Betz 130:770ce14d3d15 281 }
Wolfgang Betz 130:770ce14d3d15 282 }
Wolfgang Betz 130:770ce14d3d15 283 //Set the SCAN_RSP Payload
Wolfgang Betz 130:770ce14d3d15 284 scan_response_payload = scanResponse.getPayload();
Wolfgang Betz 130:770ce14d3d15 285 scan_rsp_length = scanResponse.getPayloadLen();
Andrea Palmieri 213:781abfbf2d1c 286
Andrea Palmieri 213:781abfbf2d1c 287 // Update the ADV data if we are already in ADV mode
Andrea Palmieri 213:781abfbf2d1c 288 if(AdvLen > 0 && state.advertising == 1) {
Andrea Palmieri 213:781abfbf2d1c 289
Andrea Palmieri 213:781abfbf2d1c 290 tBleStatus ret = aci_gap_update_adv_data(AdvLen, AdvData);
Andrea Palmieri 213:781abfbf2d1c 291 if(BLE_STATUS_SUCCESS!=ret) {
Andrea Palmieri 213:781abfbf2d1c 292 PRINTF("error occurred while adding adv data (ret=0x%x)\n", ret);
Andrea Palmieri 213:781abfbf2d1c 293 switch (ret) {
Andrea Palmieri 213:781abfbf2d1c 294 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 213:781abfbf2d1c 295 return BLE_STACK_BUSY;
Andrea Palmieri 213:781abfbf2d1c 296 default:
Andrea Palmieri 213:781abfbf2d1c 297 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 213:781abfbf2d1c 298 }
Andrea Palmieri 213:781abfbf2d1c 299 }
Andrea Palmieri 213:781abfbf2d1c 300 }
Wolfgang Betz 130:770ce14d3d15 301 }
Wolfgang Betz 130:770ce14d3d15 302 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 303 }
Wolfgang Betz 130:770ce14d3d15 304
Wolfgang Betz 130:770ce14d3d15 305 /*
Wolfgang Betz 130:770ce14d3d15 306 * Utility to set ADV timeout flag
Wolfgang Betz 130:770ce14d3d15 307 */
Wolfgang Betz 130:770ce14d3d15 308 void BlueNRGGap::setAdvToFlag(void) {
Wolfgang Betz 130:770ce14d3d15 309 AdvToFlag = true;
Wolfgang Betz 130:770ce14d3d15 310 }
Wolfgang Betz 130:770ce14d3d15 311
Wolfgang Betz 130:770ce14d3d15 312 /*
Wolfgang Betz 130:770ce14d3d15 313 * ADV timeout callback
Wolfgang Betz 130:770ce14d3d15 314 */
Wolfgang Betz 132:51056160fa4a 315 // ANDREA: mbedOS
Wolfgang Betz 138:32e3c4ca7a45 316 #ifdef AST_FOR_MBED_OS
Wolfgang Betz 132:51056160fa4a 317 static void advTimeoutCB(void)
Wolfgang Betz 132:51056160fa4a 318 {
Wolfgang Betz 132:51056160fa4a 319 Gap::GapState_t state;
Wolfgang Betz 132:51056160fa4a 320
Wolfgang Betz 132:51056160fa4a 321 state = BlueNRGGap::getInstance().getState();
Wolfgang Betz 132:51056160fa4a 322 if (state.advertising == 1) {
Wolfgang Betz 132:51056160fa4a 323
Wolfgang Betz 132:51056160fa4a 324 BlueNRGGap::getInstance().stopAdvertising();
Wolfgang Betz 132:51056160fa4a 325
Wolfgang Betz 132:51056160fa4a 326 }
Wolfgang Betz 132:51056160fa4a 327 }
Wolfgang Betz 132:51056160fa4a 328 #else
Wolfgang Betz 130:770ce14d3d15 329 static void advTimeoutCB(void)
Wolfgang Betz 130:770ce14d3d15 330 {
Wolfgang Betz 130:770ce14d3d15 331 Gap::GapState_t state;
Wolfgang Betz 130:770ce14d3d15 332
Wolfgang Betz 130:770ce14d3d15 333 state = BlueNRGGap::getInstance().getState();
Wolfgang Betz 130:770ce14d3d15 334 if (state.advertising == 1) {
Wolfgang Betz 130:770ce14d3d15 335
Wolfgang Betz 130:770ce14d3d15 336 BlueNRGGap::getInstance().setAdvToFlag();
Wolfgang Betz 130:770ce14d3d15 337
Wolfgang Betz 130:770ce14d3d15 338 Timeout t = BlueNRGGap::getInstance().getAdvTimeout();
Wolfgang Betz 130:770ce14d3d15 339 t.detach(); /* disable the callback from the timeout */
Wolfgang Betz 130:770ce14d3d15 340
Wolfgang Betz 130:770ce14d3d15 341 }
Wolfgang Betz 130:770ce14d3d15 342 }
Wolfgang Betz 138:32e3c4ca7a45 343 #endif /* AST_FOR_MBED_OS */
Wolfgang Betz 130:770ce14d3d15 344
Wolfgang Betz 130:770ce14d3d15 345 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 346 /*!
Wolfgang Betz 130:770ce14d3d15 347 @brief Starts the BLE HW, initialising any services that were
Wolfgang Betz 130:770ce14d3d15 348 added before this function was called.
Wolfgang Betz 130:770ce14d3d15 349
Wolfgang Betz 130:770ce14d3d15 350 @param[in] params
Wolfgang Betz 130:770ce14d3d15 351 Basic advertising details, including the advertising
Wolfgang Betz 130:770ce14d3d15 352 delay, timeout and how the device should be advertised
Wolfgang Betz 130:770ce14d3d15 353
Wolfgang Betz 130:770ce14d3d15 354 @note All services must be added before calling this function!
Wolfgang Betz 130:770ce14d3d15 355
Wolfgang Betz 130:770ce14d3d15 356 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 357
Wolfgang Betz 130:770ce14d3d15 358 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 359 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 360
Wolfgang Betz 130:770ce14d3d15 361 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 362
Wolfgang Betz 130:770ce14d3d15 363 @code
Wolfgang Betz 130:770ce14d3d15 364
Wolfgang Betz 130:770ce14d3d15 365 @endcode
Wolfgang Betz 130:770ce14d3d15 366 */
Wolfgang Betz 130:770ce14d3d15 367 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 368
Wolfgang Betz 130:770ce14d3d15 369 ble_error_t BlueNRGGap::startAdvertising(const GapAdvertisingParams &params)
Andrea Palmieri 187:2e517d3ddd71 370 {
Andrea Palmieri 187:2e517d3ddd71 371 tBleStatus ret;
Andrea Palmieri 187:2e517d3ddd71 372
Wolfgang Betz 130:770ce14d3d15 373 /* Make sure we support the advertising type */
Wolfgang Betz 130:770ce14d3d15 374 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) {
Andrea Palmieri 220:e02936f0d4c7 375 /* ToDo: This requires a proper security implementation, etc. */
Wolfgang Betz 130:770ce14d3d15 376 return BLE_ERROR_NOT_IMPLEMENTED;
Wolfgang Betz 130:770ce14d3d15 377 }
Wolfgang Betz 130:770ce14d3d15 378
Wolfgang Betz 130:770ce14d3d15 379 /* Check interval range */
Wolfgang Betz 130:770ce14d3d15 380 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) {
Wolfgang Betz 130:770ce14d3d15 381 /* Min delay is slightly longer for unconnectable devices */
Wolfgang Betz 130:770ce14d3d15 382 if ((params.getIntervalInADVUnits() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
Wolfgang Betz 130:770ce14d3d15 383 (params.getIntervalInADVUnits() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) {
Wolfgang Betz 130:770ce14d3d15 384 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 385 }
Wolfgang Betz 130:770ce14d3d15 386 } else {
Wolfgang Betz 130:770ce14d3d15 387 if ((params.getIntervalInADVUnits() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN) ||
Wolfgang Betz 130:770ce14d3d15 388 (params.getIntervalInADVUnits() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) {
Wolfgang Betz 130:770ce14d3d15 389 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 390 }
Wolfgang Betz 130:770ce14d3d15 391 }
Wolfgang Betz 130:770ce14d3d15 392
Wolfgang Betz 130:770ce14d3d15 393 /* Check timeout is zero for Connectable Directed */
Wolfgang Betz 130:770ce14d3d15 394 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) {
Wolfgang Betz 130:770ce14d3d15 395 /* Timeout must be 0 with this type, although we'll never get here */
Wolfgang Betz 130:770ce14d3d15 396 /* since this isn't implemented yet anyway */
Wolfgang Betz 130:770ce14d3d15 397 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 398 }
Wolfgang Betz 130:770ce14d3d15 399
Wolfgang Betz 130:770ce14d3d15 400 /* Check timeout for other advertising types */
Wolfgang Betz 130:770ce14d3d15 401 if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
Wolfgang Betz 130:770ce14d3d15 402 (params.getTimeout() > GapAdvertisingParams::GAP_ADV_PARAMS_TIMEOUT_MAX)) {
Wolfgang Betz 130:770ce14d3d15 403 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 404 }
Wolfgang Betz 130:770ce14d3d15 405
Wolfgang Betz 130:770ce14d3d15 406 /* set scan response data */
Andrea Palmieri 187:2e517d3ddd71 407 ret = hci_le_set_scan_resp_data(scan_rsp_length, scan_response_payload);
Andrea Palmieri 187:2e517d3ddd71 408 if(BLE_STATUS_SUCCESS!=ret) {
Andrea Palmieri 187:2e517d3ddd71 409 PRINTF(" error while setting scan response data (ret=0x%x)\n", ret);
Andrea Palmieri 187:2e517d3ddd71 410 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 411 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 412 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 413 default:
Andrea Palmieri 187:2e517d3ddd71 414 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 415 }
Andrea Palmieri 187:2e517d3ddd71 416 }
Wolfgang Betz 130:770ce14d3d15 417
Andrea Palmieri 220:e02936f0d4c7 418 advtInterval = params.getIntervalInADVUnits();
Andrea Palmieri 220:e02936f0d4c7 419 PRINTF("advtInterval=%ld\n\r", advtInterval);
Wolfgang Betz 130:770ce14d3d15 420
Andrea Palmieri 220:e02936f0d4c7 421 ret = aci_gap_set_discoverable(params.getAdvertisingType(), // AdvType
Andrea Palmieri 220:e02936f0d4c7 422 advtInterval, // AdvIntervMin
Andrea Palmieri 220:e02936f0d4c7 423 advtInterval, // AdvIntervMax
Andrea Palmieri 220:e02936f0d4c7 424 PUBLIC_ADDR, // OwnAddrType
Andrea Palmieri 220:e02936f0d4c7 425 NO_WHITE_LIST_USE, // AdvFilterPolicy
Andrea Palmieri 220:e02936f0d4c7 426 local_name_length, // LocalNameLen
Andrea Palmieri 220:e02936f0d4c7 427 (const char*)local_name, // LocalName
Andrea Palmieri 220:e02936f0d4c7 428 servUuidlength, // ServiceUUIDLen
Andrea Palmieri 220:e02936f0d4c7 429 servUuidData, // ServiceUUIDList
Andrea Palmieri 220:e02936f0d4c7 430 0, // SlaveConnIntervMin
Andrea Palmieri 220:e02936f0d4c7 431 0); // SlaveConnIntervMax
Wolfgang Betz 130:770ce14d3d15 432
Wolfgang Betz 130:770ce14d3d15 433
Andrea Palmieri 220:e02936f0d4c7 434 PRINTF("!!!setting discoverable (servUuidlength=0x%x)\n\r", servUuidlength);
Wolfgang Betz 130:770ce14d3d15 435 if(BLE_STATUS_SUCCESS!=ret) {
Andrea Palmieri 220:e02936f0d4c7 436 PRINTF("error occurred while setting discoverable (ret=0x%x)\n\r", ret);
Andrea Palmieri 187:2e517d3ddd71 437 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 438 case BLE_STATUS_INVALID_PARAMS:
Andrea Palmieri 187:2e517d3ddd71 439 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 187:2e517d3ddd71 440 case ERR_COMMAND_DISALLOWED:
Andrea Palmieri 187:2e517d3ddd71 441 return BLE_ERROR_OPERATION_NOT_PERMITTED;
Andrea Palmieri 187:2e517d3ddd71 442 case ERR_UNSUPPORTED_FEATURE:
Andrea Palmieri 187:2e517d3ddd71 443 return BLE_ERROR_NOT_IMPLEMENTED;
Andrea Palmieri 187:2e517d3ddd71 444 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 445 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 446 default:
Andrea Palmieri 187:2e517d3ddd71 447 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 448 }
Wolfgang Betz 130:770ce14d3d15 449 }
Wolfgang Betz 130:770ce14d3d15 450
Wolfgang Betz 130:770ce14d3d15 451 // Before updating the ADV data, delete COMPLETE_LOCAL_NAME and TX_POWER_LEVEL fields (if present)
Wolfgang Betz 130:770ce14d3d15 452 if(AdvLen>0) {
Andrea Palmieri 220:e02936f0d4c7 453 if(local_name!=NULL) {
Wolfgang Betz 132:51056160fa4a 454 PRINTF("!!!calling aci_gap_delete_ad_type AD_TYPE_COMPLETE_LOCAL_NAME!!!\n");
Wolfgang Betz 130:770ce14d3d15 455 ret = aci_gap_delete_ad_type(AD_TYPE_COMPLETE_LOCAL_NAME);
Andrea Palmieri 187:2e517d3ddd71 456 if (BLE_STATUS_SUCCESS!=ret){
Wolfgang Betz 132:51056160fa4a 457 PRINTF("aci_gap_delete_ad_type failed return=%d\n", ret);
Andrea Palmieri 187:2e517d3ddd71 458 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 459 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 460 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 461 default:
Andrea Palmieri 187:2e517d3ddd71 462 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 463 }
Wolfgang Betz 130:770ce14d3d15 464 }
Wolfgang Betz 130:770ce14d3d15 465 }
Wolfgang Betz 130:770ce14d3d15 466
Andrea Palmieri 201:a9bc2e8b01d7 467 // If ADV Data Type is SERVICE DATA or MANUFACTURER SPECIFIC DATA,
Andrea Palmieri 201:a9bc2e8b01d7 468 // we need to delete it to make the needed room in ADV payload
Andrea Palmieri 201:a9bc2e8b01d7 469 if(AdvData[1]==AD_TYPE_SERVICE_DATA || AdvData[1]==AD_TYPE_MANUFACTURER_SPECIFIC_DATA) {
Wolfgang Betz 132:51056160fa4a 470 PRINTF("!!!calling aci_gap_delete_ad_type(AD_TYPE_TX_POWER_LEVEL)!!!\n");
Wolfgang Betz 130:770ce14d3d15 471 ret = aci_gap_delete_ad_type(AD_TYPE_TX_POWER_LEVEL);
Andrea Palmieri 187:2e517d3ddd71 472 if (BLE_STATUS_SUCCESS!=ret){
Wolfgang Betz 132:51056160fa4a 473 PRINTF("aci_gap_delete_ad_type failed return=%d\n", ret);
Andrea Palmieri 187:2e517d3ddd71 474 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 475 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 476 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 477 default:
Andrea Palmieri 187:2e517d3ddd71 478 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 479 }
Wolfgang Betz 130:770ce14d3d15 480 }
Wolfgang Betz 130:770ce14d3d15 481 }
Wolfgang Betz 130:770ce14d3d15 482
Wolfgang Betz 130:770ce14d3d15 483 ret = aci_gap_update_adv_data(AdvLen, AdvData);
Wolfgang Betz 130:770ce14d3d15 484 if(BLE_STATUS_SUCCESS!=ret) {
Wolfgang Betz 132:51056160fa4a 485 PRINTF("error occurred while adding adv data (ret=0x%x)\n", ret);
Andrea Palmieri 187:2e517d3ddd71 486 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 487 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 488 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 489 default:
Andrea Palmieri 187:2e517d3ddd71 490 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 491 }
Wolfgang Betz 130:770ce14d3d15 492 }
Wolfgang Betz 130:770ce14d3d15 493
Andrea Palmieri 187:2e517d3ddd71 494 } // AdvLen>0
Wolfgang Betz 130:770ce14d3d15 495
Wolfgang Betz 130:770ce14d3d15 496 state.advertising = 1;
Wolfgang Betz 130:770ce14d3d15 497
Wolfgang Betz 130:770ce14d3d15 498 AdvToFlag = false;
Wolfgang Betz 130:770ce14d3d15 499 if(params.getTimeout() != 0) {
Wolfgang Betz 132:51056160fa4a 500 PRINTF("!!! attaching to!!!\n");
Wolfgang Betz 132:51056160fa4a 501 // ANDREA: mbedOS
Wolfgang Betz 138:32e3c4ca7a45 502 #ifdef AST_FOR_MBED_OS
Wolfgang Betz 132:51056160fa4a 503 minar::Scheduler::postCallback(advTimeoutCB).delay(minar::milliseconds(params.getTimeout()));
Wolfgang Betz 132:51056160fa4a 504 #else
Wolfgang Betz 130:770ce14d3d15 505 advTimeout.attach(advTimeoutCB, params.getTimeout());
Wolfgang Betz 132:51056160fa4a 506 #endif
Wolfgang Betz 130:770ce14d3d15 507 }
Wolfgang Betz 130:770ce14d3d15 508
Wolfgang Betz 130:770ce14d3d15 509 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 510 }
Wolfgang Betz 130:770ce14d3d15 511
Wolfgang Betz 130:770ce14d3d15 512 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 513 /*!
Wolfgang Betz 130:770ce14d3d15 514 @brief Stops the BLE HW and disconnects from any devices
Wolfgang Betz 130:770ce14d3d15 515
Wolfgang Betz 130:770ce14d3d15 516 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 517
Wolfgang Betz 130:770ce14d3d15 518 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 519 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 520
Wolfgang Betz 130:770ce14d3d15 521 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 522
Wolfgang Betz 130:770ce14d3d15 523 @code
Wolfgang Betz 130:770ce14d3d15 524
Wolfgang Betz 130:770ce14d3d15 525 @endcode
Wolfgang Betz 130:770ce14d3d15 526 */
Wolfgang Betz 130:770ce14d3d15 527 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 528 ble_error_t BlueNRGGap::stopAdvertising(void)
Wolfgang Betz 130:770ce14d3d15 529 {
Wolfgang Betz 130:770ce14d3d15 530 tBleStatus ret;
Wolfgang Betz 130:770ce14d3d15 531
Wolfgang Betz 130:770ce14d3d15 532 if(state.advertising == 1) {
Wolfgang Betz 130:770ce14d3d15 533 //Set non-discoverable to stop advertising
Wolfgang Betz 130:770ce14d3d15 534 ret = aci_gap_set_non_discoverable();
Wolfgang Betz 130:770ce14d3d15 535
Andrea Palmieri 187:2e517d3ddd71 536 if (BLE_STATUS_SUCCESS!=ret){
Wolfgang Betz 132:51056160fa4a 537 PRINTF("Error in stopping advertisement (ret=0x%x)!!\n\r", ret) ;
Andrea Palmieri 187:2e517d3ddd71 538 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 539 case ERR_COMMAND_DISALLOWED:
Andrea Palmieri 187:2e517d3ddd71 540 return BLE_ERROR_OPERATION_NOT_PERMITTED;
Andrea Palmieri 187:2e517d3ddd71 541 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 542 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 543 default:
Andrea Palmieri 187:2e517d3ddd71 544 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 545 }
Wolfgang Betz 130:770ce14d3d15 546 }
Wolfgang Betz 132:51056160fa4a 547 PRINTF("Advertisement stopped!!\n\r") ;
Wolfgang Betz 130:770ce14d3d15 548 //Set GapState_t::advertising state
Wolfgang Betz 130:770ce14d3d15 549 state.advertising = 0;
Wolfgang Betz 130:770ce14d3d15 550 }
Wolfgang Betz 130:770ce14d3d15 551
Wolfgang Betz 130:770ce14d3d15 552 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 553 }
Wolfgang Betz 130:770ce14d3d15 554
Wolfgang Betz 130:770ce14d3d15 555 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 556 /*!
Wolfgang Betz 130:770ce14d3d15 557 @brief Disconnects if we are connected to a central device
Wolfgang Betz 130:770ce14d3d15 558
Wolfgang Betz 130:770ce14d3d15 559 @param[in] reason
Wolfgang Betz 130:770ce14d3d15 560 Disconnection Reason
Wolfgang Betz 130:770ce14d3d15 561
Wolfgang Betz 130:770ce14d3d15 562 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 563
Wolfgang Betz 130:770ce14d3d15 564 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 565 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 566
Wolfgang Betz 130:770ce14d3d15 567 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 568
Wolfgang Betz 130:770ce14d3d15 569 @code
Wolfgang Betz 130:770ce14d3d15 570
Wolfgang Betz 130:770ce14d3d15 571 @endcode
Wolfgang Betz 130:770ce14d3d15 572 */
Wolfgang Betz 130:770ce14d3d15 573 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 574 ble_error_t BlueNRGGap::disconnect(Gap::DisconnectionReason_t reason)
Wolfgang Betz 130:770ce14d3d15 575 {
Andrea Palmieri 142:adf1567d3900 576 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 577 (void)reason;
Andrea Palmieri 142:adf1567d3900 578
Wolfgang Betz 130:770ce14d3d15 579 tBleStatus ret;
Wolfgang Betz 130:770ce14d3d15 580 //For Reason codes check BlueTooth HCI Spec
Wolfgang Betz 130:770ce14d3d15 581
Wolfgang Betz 130:770ce14d3d15 582 if(m_connectionHandle != BLE_CONN_HANDLE_INVALID) {
Wolfgang Betz 130:770ce14d3d15 583 ret = aci_gap_terminate(m_connectionHandle, 0x16);//0x16 Connection Terminated by Local Host.
Wolfgang Betz 130:770ce14d3d15 584
Andrea Palmieri 187:2e517d3ddd71 585 if (BLE_STATUS_SUCCESS != ret){
Andrea Palmieri 187:2e517d3ddd71 586 PRINTF("Error in GAP termination (ret=0x%x)!!\n\r", ret) ;
Andrea Palmieri 187:2e517d3ddd71 587 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 588 case ERR_COMMAND_DISALLOWED:
Andrea Palmieri 187:2e517d3ddd71 589 return BLE_ERROR_OPERATION_NOT_PERMITTED;
Andrea Palmieri 187:2e517d3ddd71 590 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 591 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 592 default:
Andrea Palmieri 187:2e517d3ddd71 593 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 594 }
Wolfgang Betz 130:770ce14d3d15 595 }
Wolfgang Betz 130:770ce14d3d15 596
Wolfgang Betz 132:51056160fa4a 597 //PRINTF("Disconnected from localhost!!\n\r") ;
Wolfgang Betz 130:770ce14d3d15 598 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
Wolfgang Betz 130:770ce14d3d15 599 }
Wolfgang Betz 130:770ce14d3d15 600
Wolfgang Betz 130:770ce14d3d15 601 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 602 }
Wolfgang Betz 130:770ce14d3d15 603
Wolfgang Betz 130:770ce14d3d15 604 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 605 /*!
Wolfgang Betz 130:770ce14d3d15 606 @brief Disconnects if we are connected to a central device
Wolfgang Betz 130:770ce14d3d15 607
Wolfgang Betz 130:770ce14d3d15 608 @param[in] reason
Wolfgang Betz 130:770ce14d3d15 609 Disconnection Reason
Wolfgang Betz 130:770ce14d3d15 610
Wolfgang Betz 130:770ce14d3d15 611 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 612
Wolfgang Betz 130:770ce14d3d15 613 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 614 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 615
Wolfgang Betz 130:770ce14d3d15 616 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 617
Wolfgang Betz 130:770ce14d3d15 618 @code
Wolfgang Betz 130:770ce14d3d15 619
Wolfgang Betz 130:770ce14d3d15 620 @endcode
Wolfgang Betz 130:770ce14d3d15 621 */
Wolfgang Betz 130:770ce14d3d15 622 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 623 ble_error_t BlueNRGGap::disconnect(Handle_t connectionHandle, Gap::DisconnectionReason_t reason)
Wolfgang Betz 130:770ce14d3d15 624 {
Andrea Palmieri 142:adf1567d3900 625 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 626 (void)reason;
Andrea Palmieri 142:adf1567d3900 627
Wolfgang Betz 130:770ce14d3d15 628 tBleStatus ret;
Wolfgang Betz 130:770ce14d3d15 629 //For Reason codes check BlueTooth HCI Spec
Wolfgang Betz 130:770ce14d3d15 630
Wolfgang Betz 130:770ce14d3d15 631 if(connectionHandle != BLE_CONN_HANDLE_INVALID) {
Wolfgang Betz 130:770ce14d3d15 632 ret = aci_gap_terminate(connectionHandle, 0x16);//0x16 Connection Terminated by Local Host.
Wolfgang Betz 130:770ce14d3d15 633
Andrea Palmieri 187:2e517d3ddd71 634 if (BLE_STATUS_SUCCESS != ret){
Andrea Palmieri 187:2e517d3ddd71 635 PRINTF("Error in GAP termination (ret=0x%x)!!\n\r", ret) ;
Andrea Palmieri 187:2e517d3ddd71 636 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 637 case ERR_COMMAND_DISALLOWED:
Andrea Palmieri 187:2e517d3ddd71 638 return BLE_ERROR_OPERATION_NOT_PERMITTED;
Andrea Palmieri 187:2e517d3ddd71 639 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 640 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 641 default:
Andrea Palmieri 187:2e517d3ddd71 642 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 643 }
Wolfgang Betz 130:770ce14d3d15 644 }
Wolfgang Betz 130:770ce14d3d15 645
Wolfgang Betz 132:51056160fa4a 646 //PRINTF("Disconnected from localhost!!\n\r") ;
Wolfgang Betz 130:770ce14d3d15 647 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
Wolfgang Betz 130:770ce14d3d15 648 }
Wolfgang Betz 130:770ce14d3d15 649
Wolfgang Betz 130:770ce14d3d15 650 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 651 }
Wolfgang Betz 130:770ce14d3d15 652
Wolfgang Betz 130:770ce14d3d15 653 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 654 /*!
Wolfgang Betz 130:770ce14d3d15 655 @brief Sets the 16-bit connection handle
Wolfgang Betz 130:770ce14d3d15 656
Wolfgang Betz 130:770ce14d3d15 657 @param[in] con_handle
Wolfgang Betz 130:770ce14d3d15 658 Connection Handle which is set in the Gap Instance
Wolfgang Betz 130:770ce14d3d15 659
Wolfgang Betz 130:770ce14d3d15 660 @returns void
Wolfgang Betz 130:770ce14d3d15 661 */
Wolfgang Betz 130:770ce14d3d15 662 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 663 void BlueNRGGap::setConnectionHandle(uint16_t con_handle)
Wolfgang Betz 130:770ce14d3d15 664 {
Wolfgang Betz 130:770ce14d3d15 665 m_connectionHandle = con_handle;
Wolfgang Betz 130:770ce14d3d15 666 }
Wolfgang Betz 130:770ce14d3d15 667
Wolfgang Betz 130:770ce14d3d15 668 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 669 /*!
Wolfgang Betz 130:770ce14d3d15 670 @brief Gets the 16-bit connection handle
Wolfgang Betz 130:770ce14d3d15 671
Wolfgang Betz 130:770ce14d3d15 672 @param[in] void
Wolfgang Betz 130:770ce14d3d15 673
Wolfgang Betz 130:770ce14d3d15 674 @returns uint16_t
Wolfgang Betz 130:770ce14d3d15 675 Connection Handle of the Gap Instance
Wolfgang Betz 130:770ce14d3d15 676 */
Wolfgang Betz 130:770ce14d3d15 677 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 678 uint16_t BlueNRGGap::getConnectionHandle(void)
Wolfgang Betz 130:770ce14d3d15 679 {
Wolfgang Betz 130:770ce14d3d15 680 return m_connectionHandle;
Wolfgang Betz 130:770ce14d3d15 681 }
Wolfgang Betz 130:770ce14d3d15 682
Wolfgang Betz 130:770ce14d3d15 683 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 684 /*!
Wolfgang Betz 130:770ce14d3d15 685 @brief Sets the BLE device address. SetAddress will reset the BLE
Wolfgang Betz 130:770ce14d3d15 686 device and re-initialize BTLE. Will not start advertising.
Wolfgang Betz 130:770ce14d3d15 687
Wolfgang Betz 130:770ce14d3d15 688 @param[in] type
Wolfgang Betz 130:770ce14d3d15 689 Type of Address
Wolfgang Betz 130:770ce14d3d15 690
Wolfgang Betz 130:770ce14d3d15 691 @param[in] address[6]
Wolfgang Betz 130:770ce14d3d15 692 Value of the Address to be set
Wolfgang Betz 130:770ce14d3d15 693
Wolfgang Betz 130:770ce14d3d15 694 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 695
Wolfgang Betz 130:770ce14d3d15 696 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 697
Wolfgang Betz 130:770ce14d3d15 698 @code
Wolfgang Betz 130:770ce14d3d15 699
Wolfgang Betz 130:770ce14d3d15 700 @endcode
Wolfgang Betz 130:770ce14d3d15 701 */
Wolfgang Betz 130:770ce14d3d15 702 /**************************************************************************/
Wolfgang Betz 132:51056160fa4a 703 ble_error_t BlueNRGGap::setAddress(AddressType_t type, const Address_t address)
Wolfgang Betz 130:770ce14d3d15 704 {
Andrea Palmieri 210:08c4c85ca679 705 if (type > BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE) {
Wolfgang Betz 130:770ce14d3d15 706 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 707 }
Wolfgang Betz 130:770ce14d3d15 708
Wolfgang Betz 132:51056160fa4a 709 addr_type = type;
Wolfgang Betz 130:770ce14d3d15 710 //copy address to bdAddr[6]
Wolfgang Betz 130:770ce14d3d15 711 for(int i=0; i<BDADDR_SIZE; i++) {
Wolfgang Betz 130:770ce14d3d15 712 bdaddr[i] = address[i];
Wolfgang Betz 132:51056160fa4a 713 //PRINTF("i[%d]:0x%x\n\r",i,bdaddr[i]);
Wolfgang Betz 130:770ce14d3d15 714 }
Wolfgang Betz 130:770ce14d3d15 715
Wolfgang Betz 130:770ce14d3d15 716 if(!isSetAddress) isSetAddress = true;
Wolfgang Betz 130:770ce14d3d15 717
Wolfgang Betz 130:770ce14d3d15 718 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 719 }
Wolfgang Betz 130:770ce14d3d15 720
Wolfgang Betz 130:770ce14d3d15 721 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 722 /*!
Wolfgang Betz 130:770ce14d3d15 723 @brief Returns boolean if the address of the device has been set
Wolfgang Betz 130:770ce14d3d15 724 or not
Wolfgang Betz 130:770ce14d3d15 725
Wolfgang Betz 130:770ce14d3d15 726 @returns bool
Wolfgang Betz 130:770ce14d3d15 727
Wolfgang Betz 130:770ce14d3d15 728 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 729
Wolfgang Betz 130:770ce14d3d15 730 @code
Wolfgang Betz 130:770ce14d3d15 731
Wolfgang Betz 130:770ce14d3d15 732 @endcode
Wolfgang Betz 130:770ce14d3d15 733 */
Wolfgang Betz 130:770ce14d3d15 734 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 735 bool BlueNRGGap::getIsSetAddress()
Wolfgang Betz 130:770ce14d3d15 736 {
Wolfgang Betz 130:770ce14d3d15 737 return isSetAddress;
Wolfgang Betz 130:770ce14d3d15 738 }
Wolfgang Betz 130:770ce14d3d15 739
Wolfgang Betz 130:770ce14d3d15 740 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 741 /*!
Wolfgang Betz 130:770ce14d3d15 742 @brief Returns the address of the device if set
Wolfgang Betz 130:770ce14d3d15 743
Wolfgang Betz 130:770ce14d3d15 744 @returns Pointer to the address if Address is set else NULL
Wolfgang Betz 130:770ce14d3d15 745
Wolfgang Betz 130:770ce14d3d15 746 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 747
Wolfgang Betz 130:770ce14d3d15 748 @code
Wolfgang Betz 130:770ce14d3d15 749
Wolfgang Betz 130:770ce14d3d15 750 @endcode
Wolfgang Betz 130:770ce14d3d15 751 */
Wolfgang Betz 130:770ce14d3d15 752 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 753 ble_error_t BlueNRGGap::getAddress(AddressType_t *typeP, Address_t address)
Wolfgang Betz 130:770ce14d3d15 754 {
Wolfgang Betz 132:51056160fa4a 755 *typeP = addr_type;//Gap::ADDR_TYPE_PUBLIC;
Wolfgang Betz 130:770ce14d3d15 756
Wolfgang Betz 130:770ce14d3d15 757 if(isSetAddress)
Wolfgang Betz 130:770ce14d3d15 758 {
Wolfgang Betz 130:770ce14d3d15 759 for(int i=0; i<BDADDR_SIZE; i++) {
Wolfgang Betz 130:770ce14d3d15 760 address[i] = bdaddr[i];
Wolfgang Betz 132:51056160fa4a 761 //PRINTF("i[%d]:0x%x\n\r",i,bdaddr[i]);
Wolfgang Betz 130:770ce14d3d15 762 }
Wolfgang Betz 130:770ce14d3d15 763 }
Wolfgang Betz 130:770ce14d3d15 764
Wolfgang Betz 130:770ce14d3d15 765 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 766 }
Wolfgang Betz 130:770ce14d3d15 767
Wolfgang Betz 130:770ce14d3d15 768 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 769 /*!
Wolfgang Betz 130:770ce14d3d15 770 @brief obtains preferred connection params
Wolfgang Betz 130:770ce14d3d15 771
Wolfgang Betz 130:770ce14d3d15 772 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 773
Wolfgang Betz 130:770ce14d3d15 774 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 775
Wolfgang Betz 130:770ce14d3d15 776 @code
Wolfgang Betz 130:770ce14d3d15 777
Wolfgang Betz 130:770ce14d3d15 778 @endcode
Wolfgang Betz 130:770ce14d3d15 779 */
Wolfgang Betz 130:770ce14d3d15 780 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 781 ble_error_t BlueNRGGap::getPreferredConnectionParams(ConnectionParams_t *params)
Wolfgang Betz 130:770ce14d3d15 782 {
Andrea Palmieri 142:adf1567d3900 783 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 784 (void)params;
Andrea Palmieri 142:adf1567d3900 785
Wolfgang Betz 130:770ce14d3d15 786 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 787 }
Wolfgang Betz 130:770ce14d3d15 788
Wolfgang Betz 130:770ce14d3d15 789
Wolfgang Betz 130:770ce14d3d15 790 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 791 /*!
Wolfgang Betz 130:770ce14d3d15 792 @brief sets preferred connection params
Wolfgang Betz 130:770ce14d3d15 793
Wolfgang Betz 130:770ce14d3d15 794 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 795
Wolfgang Betz 130:770ce14d3d15 796 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 797
Wolfgang Betz 130:770ce14d3d15 798 @code
Wolfgang Betz 130:770ce14d3d15 799
Wolfgang Betz 130:770ce14d3d15 800 @endcode
Wolfgang Betz 130:770ce14d3d15 801 */
Wolfgang Betz 130:770ce14d3d15 802 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 803 ble_error_t BlueNRGGap::setPreferredConnectionParams(const ConnectionParams_t *params)
Wolfgang Betz 130:770ce14d3d15 804 {
Andrea Palmieri 142:adf1567d3900 805 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 806 (void)params;
Andrea Palmieri 142:adf1567d3900 807
Wolfgang Betz 130:770ce14d3d15 808 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 809 }
Wolfgang Betz 130:770ce14d3d15 810
Wolfgang Betz 130:770ce14d3d15 811 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 812 /*!
Wolfgang Betz 130:770ce14d3d15 813 @brief updates preferred connection params
Wolfgang Betz 130:770ce14d3d15 814
Wolfgang Betz 130:770ce14d3d15 815 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 816
Wolfgang Betz 130:770ce14d3d15 817 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 818
Wolfgang Betz 130:770ce14d3d15 819 @code
Wolfgang Betz 130:770ce14d3d15 820
Wolfgang Betz 130:770ce14d3d15 821 @endcode
Wolfgang Betz 130:770ce14d3d15 822 */
Wolfgang Betz 130:770ce14d3d15 823 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 824 ble_error_t BlueNRGGap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *params)
Wolfgang Betz 130:770ce14d3d15 825 {
Andrea Palmieri 142:adf1567d3900 826 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 827 (void) handle;
Andrea Palmieri 142:adf1567d3900 828 (void)params;
Andrea Palmieri 142:adf1567d3900 829
Wolfgang Betz 130:770ce14d3d15 830 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 831 }
Wolfgang Betz 130:770ce14d3d15 832
Wolfgang Betz 130:770ce14d3d15 833 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 834 /*!
Wolfgang Betz 130:770ce14d3d15 835 @brief Sets the Device Name Characteristic
Wolfgang Betz 130:770ce14d3d15 836
Wolfgang Betz 130:770ce14d3d15 837 @param[in] deviceName
Wolfgang Betz 130:770ce14d3d15 838 pointer to device name to be set
Wolfgang Betz 130:770ce14d3d15 839
Wolfgang Betz 130:770ce14d3d15 840 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 841
Wolfgang Betz 130:770ce14d3d15 842 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 843 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 844
Wolfgang Betz 130:770ce14d3d15 845 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 846
Wolfgang Betz 130:770ce14d3d15 847 @code
Wolfgang Betz 130:770ce14d3d15 848
Wolfgang Betz 130:770ce14d3d15 849 @endcode
Wolfgang Betz 130:770ce14d3d15 850 */
Wolfgang Betz 130:770ce14d3d15 851 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 852 ble_error_t BlueNRGGap::setDeviceName(const uint8_t *deviceName)
Wolfgang Betz 130:770ce14d3d15 853 {
Andrea Palmieri 187:2e517d3ddd71 854 tBleStatus ret;
Wolfgang Betz 130:770ce14d3d15 855 uint8_t nameLen = 0;
Wolfgang Betz 130:770ce14d3d15 856
Wolfgang Betz 130:770ce14d3d15 857 DeviceName = (uint8_t *)deviceName;
Wolfgang Betz 132:51056160fa4a 858 //PRINTF("SetDeviceName=%s\n\r", DeviceName);
Wolfgang Betz 130:770ce14d3d15 859
Wolfgang Betz 130:770ce14d3d15 860 nameLen = strlen((const char*)DeviceName);
Wolfgang Betz 132:51056160fa4a 861 //PRINTF("DeviceName Size=%d\n\r", nameLen);
Wolfgang Betz 130:770ce14d3d15 862
Wolfgang Betz 130:770ce14d3d15 863 ret = aci_gatt_update_char_value(g_gap_service_handle,
Wolfgang Betz 130:770ce14d3d15 864 g_device_name_char_handle,
Wolfgang Betz 130:770ce14d3d15 865 0,
Wolfgang Betz 130:770ce14d3d15 866 nameLen,
Wolfgang Betz 130:770ce14d3d15 867 (uint8_t *)DeviceName);
Andrea Palmieri 187:2e517d3ddd71 868
Andrea Palmieri 187:2e517d3ddd71 869 if (BLE_STATUS_SUCCESS != ret){
Andrea Palmieri 187:2e517d3ddd71 870 PRINTF("device set name failed (ret=0x%x)!!\n\r", ret) ;
Andrea Palmieri 187:2e517d3ddd71 871 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 872 case BLE_STATUS_INVALID_HANDLE:
Andrea Palmieri 187:2e517d3ddd71 873 case BLE_STATUS_INVALID_PARAMETER:
Andrea Palmieri 187:2e517d3ddd71 874 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 187:2e517d3ddd71 875 case BLE_STATUS_INSUFFICIENT_RESOURCES:
Andrea Palmieri 187:2e517d3ddd71 876 return BLE_ERROR_NO_MEM;
Andrea Palmieri 187:2e517d3ddd71 877 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 878 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 879 default:
Andrea Palmieri 187:2e517d3ddd71 880 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 881 }
Wolfgang Betz 130:770ce14d3d15 882 }
Wolfgang Betz 130:770ce14d3d15 883
Wolfgang Betz 130:770ce14d3d15 884 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 885 }
Wolfgang Betz 130:770ce14d3d15 886
Wolfgang Betz 130:770ce14d3d15 887 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 888 /*!
Wolfgang Betz 130:770ce14d3d15 889 @brief Gets the Device Name Characteristic
Wolfgang Betz 130:770ce14d3d15 890
Wolfgang Betz 130:770ce14d3d15 891 @param[in] deviceName
Wolfgang Betz 130:770ce14d3d15 892 pointer to device name
Wolfgang Betz 130:770ce14d3d15 893
Wolfgang Betz 130:770ce14d3d15 894 @param[in] lengthP
Wolfgang Betz 130:770ce14d3d15 895 pointer to device name length
Wolfgang Betz 130:770ce14d3d15 896
Wolfgang Betz 130:770ce14d3d15 897 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 898
Wolfgang Betz 130:770ce14d3d15 899 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 900 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 901
Wolfgang Betz 130:770ce14d3d15 902 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 903
Wolfgang Betz 130:770ce14d3d15 904 @code
Wolfgang Betz 130:770ce14d3d15 905
Wolfgang Betz 130:770ce14d3d15 906 @endcode
Wolfgang Betz 130:770ce14d3d15 907 */
Wolfgang Betz 130:770ce14d3d15 908 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 909 ble_error_t BlueNRGGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP)
Wolfgang Betz 130:770ce14d3d15 910 {
Wolfgang Betz 130:770ce14d3d15 911 if(DeviceName==NULL)
Andrea Palmieri 187:2e517d3ddd71 912 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 130:770ce14d3d15 913
Wolfgang Betz 130:770ce14d3d15 914 strcpy((char*)deviceName, (const char*)DeviceName);
Wolfgang Betz 132:51056160fa4a 915 //PRINTF("GetDeviceName=%s\n\r", deviceName);
Wolfgang Betz 130:770ce14d3d15 916
Wolfgang Betz 130:770ce14d3d15 917 *lengthP = strlen((const char*)DeviceName);
Wolfgang Betz 132:51056160fa4a 918 //PRINTF("DeviceName Size=%d\n\r", *lengthP);
Wolfgang Betz 130:770ce14d3d15 919
Wolfgang Betz 130:770ce14d3d15 920 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 921 }
Wolfgang Betz 130:770ce14d3d15 922
Wolfgang Betz 130:770ce14d3d15 923 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 924 /*!
Wolfgang Betz 130:770ce14d3d15 925 @brief Sets the Device Appearance Characteristic
Wolfgang Betz 130:770ce14d3d15 926
Wolfgang Betz 130:770ce14d3d15 927 @param[in] appearance
Wolfgang Betz 130:770ce14d3d15 928 device appearance
Wolfgang Betz 130:770ce14d3d15 929
Wolfgang Betz 130:770ce14d3d15 930 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 931
Wolfgang Betz 130:770ce14d3d15 932 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 933 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 934
Wolfgang Betz 130:770ce14d3d15 935 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 936
Wolfgang Betz 130:770ce14d3d15 937 @code
Wolfgang Betz 130:770ce14d3d15 938
Wolfgang Betz 130:770ce14d3d15 939 @endcode
Wolfgang Betz 130:770ce14d3d15 940 */
Wolfgang Betz 130:770ce14d3d15 941 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 942 ble_error_t BlueNRGGap::setAppearance(GapAdvertisingData::Appearance appearance)
Wolfgang Betz 130:770ce14d3d15 943 {
Andrea Palmieri 187:2e517d3ddd71 944 tBleStatus ret;
Andrea Palmieri 187:2e517d3ddd71 945
Wolfgang Betz 130:770ce14d3d15 946 /*
Wolfgang Betz 130:770ce14d3d15 947 Tested with GapAdvertisingData::GENERIC_PHONE.
Wolfgang Betz 130:770ce14d3d15 948 for other appearances BLE Scanner android app is not behaving properly
Wolfgang Betz 130:770ce14d3d15 949 */
Wolfgang Betz 130:770ce14d3d15 950 //char deviceAppearance[2];
Wolfgang Betz 130:770ce14d3d15 951 STORE_LE_16(deviceAppearance, appearance);
Wolfgang Betz 132:51056160fa4a 952 PRINTF("input: incoming = %d deviceAppearance= 0x%x 0x%x\n\r", appearance, deviceAppearance[1], deviceAppearance[0]);
Wolfgang Betz 130:770ce14d3d15 953
Andrea Palmieri 187:2e517d3ddd71 954 ret = aci_gatt_update_char_value(g_gap_service_handle, g_appearance_char_handle, 0, 2, (uint8_t *)deviceAppearance);
Andrea Palmieri 187:2e517d3ddd71 955 if (BLE_STATUS_SUCCESS == ret){
Andrea Palmieri 187:2e517d3ddd71 956 return BLE_ERROR_NONE;
Andrea Palmieri 187:2e517d3ddd71 957 }
Andrea Palmieri 187:2e517d3ddd71 958
Andrea Palmieri 217:23870c55b07e 959 PRINTF("setAppearance failed (ret=0x%x)!!\n\r", ret);
Andrea Palmieri 187:2e517d3ddd71 960 switch (ret) {
Andrea Palmieri 187:2e517d3ddd71 961 case BLE_STATUS_INVALID_HANDLE:
Andrea Palmieri 187:2e517d3ddd71 962 case BLE_STATUS_INVALID_PARAMETER:
Andrea Palmieri 187:2e517d3ddd71 963 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 187:2e517d3ddd71 964 case BLE_STATUS_INSUFFICIENT_RESOURCES:
Andrea Palmieri 187:2e517d3ddd71 965 return BLE_ERROR_NO_MEM;
Andrea Palmieri 187:2e517d3ddd71 966 case BLE_STATUS_TIMEOUT:
Andrea Palmieri 187:2e517d3ddd71 967 return BLE_STACK_BUSY;
Andrea Palmieri 187:2e517d3ddd71 968 default:
Andrea Palmieri 187:2e517d3ddd71 969 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 187:2e517d3ddd71 970 }
Wolfgang Betz 130:770ce14d3d15 971 }
Wolfgang Betz 130:770ce14d3d15 972
Wolfgang Betz 130:770ce14d3d15 973 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 974 /*!
Wolfgang Betz 130:770ce14d3d15 975 @brief Gets the Device Appearance Characteristic
Wolfgang Betz 130:770ce14d3d15 976
Wolfgang Betz 130:770ce14d3d15 977 @param[in] appearance
Wolfgang Betz 130:770ce14d3d15 978 pointer to device appearance value
Wolfgang Betz 130:770ce14d3d15 979
Wolfgang Betz 130:770ce14d3d15 980 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 981
Wolfgang Betz 130:770ce14d3d15 982 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 983 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 984
Wolfgang Betz 130:770ce14d3d15 985 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 986
Wolfgang Betz 130:770ce14d3d15 987 @code
Wolfgang Betz 130:770ce14d3d15 988
Wolfgang Betz 130:770ce14d3d15 989 @endcode
Wolfgang Betz 130:770ce14d3d15 990 */
Wolfgang Betz 130:770ce14d3d15 991 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 992 ble_error_t BlueNRGGap::getAppearance(GapAdvertisingData::Appearance *appearanceP)
Wolfgang Betz 130:770ce14d3d15 993 {
Wolfgang Betz 130:770ce14d3d15 994 uint16_t devP;
Wolfgang Betz 130:770ce14d3d15 995 if(!appearanceP) return BLE_ERROR_PARAM_OUT_OF_RANGE;
Wolfgang Betz 130:770ce14d3d15 996 devP = ((uint16_t)(0x0000|deviceAppearance[0])) | (((uint16_t)(0x0000|deviceAppearance[1]))<<8);
Wolfgang Betz 130:770ce14d3d15 997 strcpy((char*)appearanceP, (const char*)&devP);
Wolfgang Betz 130:770ce14d3d15 998
Wolfgang Betz 130:770ce14d3d15 999 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 1000 }
Wolfgang Betz 130:770ce14d3d15 1001
Wolfgang Betz 130:770ce14d3d15 1002 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1003 /*!
Wolfgang Betz 130:770ce14d3d15 1004 @brief Gets the value of maximum advertising interval in ms
Wolfgang Betz 130:770ce14d3d15 1005
Wolfgang Betz 130:770ce14d3d15 1006 @returns uint16_t
Wolfgang Betz 130:770ce14d3d15 1007
Wolfgang Betz 130:770ce14d3d15 1008 @retval value of maximum advertising interval in ms
Wolfgang Betz 130:770ce14d3d15 1009
Wolfgang Betz 130:770ce14d3d15 1010 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 1011
Wolfgang Betz 130:770ce14d3d15 1012 @code
Wolfgang Betz 130:770ce14d3d15 1013
Wolfgang Betz 130:770ce14d3d15 1014 @endcode
Wolfgang Betz 130:770ce14d3d15 1015 */
Wolfgang Betz 130:770ce14d3d15 1016 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1017 uint16_t BlueNRGGap::getMaxAdvertisingInterval(void) const {
Wolfgang Betz 130:770ce14d3d15 1018 return advtInterval;
Wolfgang Betz 130:770ce14d3d15 1019 }
Wolfgang Betz 130:770ce14d3d15 1020
Wolfgang Betz 130:770ce14d3d15 1021
Wolfgang Betz 130:770ce14d3d15 1022 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1023 /*!
Wolfgang Betz 130:770ce14d3d15 1024 @brief Gets the value of minimum advertising interval in ms
Wolfgang Betz 130:770ce14d3d15 1025
Wolfgang Betz 130:770ce14d3d15 1026 @returns uint16_t
Wolfgang Betz 130:770ce14d3d15 1027
Wolfgang Betz 130:770ce14d3d15 1028 @retval value of minimum advertising interval in ms
Wolfgang Betz 130:770ce14d3d15 1029
Wolfgang Betz 130:770ce14d3d15 1030 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 1031
Wolfgang Betz 130:770ce14d3d15 1032 @code
Wolfgang Betz 130:770ce14d3d15 1033
Wolfgang Betz 130:770ce14d3d15 1034 @endcode
Wolfgang Betz 130:770ce14d3d15 1035 */
Wolfgang Betz 130:770ce14d3d15 1036 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1037 uint16_t BlueNRGGap::getMinAdvertisingInterval(void) const {
Wolfgang Betz 130:770ce14d3d15 1038 return 0; // minimum Advertising interval is 0
Wolfgang Betz 130:770ce14d3d15 1039 }
Wolfgang Betz 130:770ce14d3d15 1040
Wolfgang Betz 130:770ce14d3d15 1041 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1042 /*!
Wolfgang Betz 130:770ce14d3d15 1043 @brief Gets the value of minimum non connectable advertising interval in ms
Wolfgang Betz 130:770ce14d3d15 1044
Wolfgang Betz 130:770ce14d3d15 1045 @returns uint16_t
Wolfgang Betz 130:770ce14d3d15 1046
Wolfgang Betz 130:770ce14d3d15 1047 @retval value of minimum non connectable advertising interval in ms
Wolfgang Betz 130:770ce14d3d15 1048
Wolfgang Betz 130:770ce14d3d15 1049 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 1050
Wolfgang Betz 130:770ce14d3d15 1051 @code
Wolfgang Betz 130:770ce14d3d15 1052
Wolfgang Betz 130:770ce14d3d15 1053 @endcode
Wolfgang Betz 130:770ce14d3d15 1054 */
Wolfgang Betz 130:770ce14d3d15 1055 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1056 uint16_t BlueNRGGap::getMinNonConnectableAdvertisingInterval(void) const {
Wolfgang Betz 130:770ce14d3d15 1057 return BLE_GAP_ADV_NONCON_INTERVAL_MIN;
Wolfgang Betz 130:770ce14d3d15 1058 }
Wolfgang Betz 130:770ce14d3d15 1059
Andrea Palmieri 160:174387d53809 1060 GapScanningParams* BlueNRGGap::getScanningParams(void)
Andrea Palmieri 160:174387d53809 1061 {
Andrea Palmieri 160:174387d53809 1062 return &_scanningParams;
Andrea Palmieri 160:174387d53809 1063 }
Andrea Palmieri 160:174387d53809 1064
Andrea Palmieri 181:749071129d2e 1065 static void radioScanning(void)
Andrea Palmieri 160:174387d53809 1066 {
Andrea Palmieri 160:174387d53809 1067 GapScanningParams* scanningParams = BlueNRGGap::getInstance().getScanningParams();
Andrea Palmieri 160:174387d53809 1068
Andrea Palmieri 187:2e517d3ddd71 1069 BlueNRGGap::getInstance().startRadioScan(*scanningParams);
Andrea Palmieri 160:174387d53809 1070 }
Andrea Palmieri 160:174387d53809 1071
Wolfgang Betz 165:3576598c0889 1072 static void makeConnection(void)
Wolfgang Betz 162:667f97a4db7f 1073 {
Wolfgang Betz 165:3576598c0889 1074 BlueNRGGap::getInstance().createConnection();
Wolfgang Betz 162:667f97a4db7f 1075 }
Wolfgang Betz 162:667f97a4db7f 1076
Wolfgang Betz 130:770ce14d3d15 1077 // ANDREA
Wolfgang Betz 132:51056160fa4a 1078 void BlueNRGGap::Discovery_CB(Reason_t reason,
Wolfgang Betz 132:51056160fa4a 1079 uint8_t adv_type,
Wolfgang Betz 132:51056160fa4a 1080 uint8_t *addr_type,
Wolfgang Betz 132:51056160fa4a 1081 uint8_t *addr,
Wolfgang Betz 132:51056160fa4a 1082 uint8_t *data_length,
Wolfgang Betz 132:51056160fa4a 1083 uint8_t *data,
Wolfgang Betz 132:51056160fa4a 1084 uint8_t *RSSI)
Wolfgang Betz 132:51056160fa4a 1085 {
Andrea Palmieri 142:adf1567d3900 1086 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 1087 (void)addr_type;
Wolfgang Betz 132:51056160fa4a 1088
Wolfgang Betz 132:51056160fa4a 1089 switch (reason) {
Wolfgang Betz 132:51056160fa4a 1090 case DEVICE_FOUND:
Wolfgang Betz 132:51056160fa4a 1091 {
Wolfgang Betz 132:51056160fa4a 1092 GapAdvertisingParams::AdvertisingType_t type;
Wolfgang Betz 132:51056160fa4a 1093 bool isScanResponse = false;
Wolfgang Betz 132:51056160fa4a 1094 switch(adv_type) {
Wolfgang Betz 132:51056160fa4a 1095 case ADV_IND:
Wolfgang Betz 132:51056160fa4a 1096 type = GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED;
Wolfgang Betz 132:51056160fa4a 1097 break;
Wolfgang Betz 132:51056160fa4a 1098 case ADV_DIRECT_IND:
Wolfgang Betz 132:51056160fa4a 1099 type = GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED;
Wolfgang Betz 132:51056160fa4a 1100 break;
Wolfgang Betz 132:51056160fa4a 1101 case ADV_SCAN_IND:
Wolfgang Betz 132:51056160fa4a 1102 case SCAN_RSP:
Wolfgang Betz 132:51056160fa4a 1103 type = GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED;
Wolfgang Betz 132:51056160fa4a 1104 isScanResponse = true;
Wolfgang Betz 132:51056160fa4a 1105 break;
Wolfgang Betz 132:51056160fa4a 1106 case ADV_NONCONN_IND:
Wolfgang Betz 132:51056160fa4a 1107 type = GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED;
Andrea Palmieri 142:adf1567d3900 1108 break;
Andrea Palmieri 142:adf1567d3900 1109 default:
Andrea Palmieri 142:adf1567d3900 1110 type = GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED;
Wolfgang Betz 132:51056160fa4a 1111 }
Wolfgang Betz 132:51056160fa4a 1112
Andrea Palmieri 217:23870c55b07e 1113 PRINTF("data_length=%d adv peerAddr[%02x %02x %02x %02x %02x %02x] \r\n",
Andrea Palmieri 217:23870c55b07e 1114 *data_length, addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
Wolfgang Betz 132:51056160fa4a 1115 processAdvertisementReport(addr, *RSSI, isScanResponse, type, *data_length, data);
Andrea Palmieri 160:174387d53809 1116 PRINTF("!!!After processAdvertisementReport\n\r");
Wolfgang Betz 132:51056160fa4a 1117 }
Wolfgang Betz 132:51056160fa4a 1118 break;
Wolfgang Betz 132:51056160fa4a 1119
Wolfgang Betz 132:51056160fa4a 1120 case DISCOVERY_COMPLETE:
Wolfgang Betz 132:51056160fa4a 1121 // The discovery is complete. If this is due to a stop scanning (i.e., the device
Wolfgang Betz 132:51056160fa4a 1122 // we are interested in has been found) and a connection has been requested
Wolfgang Betz 132:51056160fa4a 1123 // then we start the device connection. Otherwise, we restart the scanning.
Andrea Palmieri 145:024a552e4715 1124 PRINTF("DISCOVERY_COMPLETE\n\r");
Wolfgang Betz 132:51056160fa4a 1125 _scanning = false;
Wolfgang Betz 132:51056160fa4a 1126
Wolfgang Betz 165:3576598c0889 1127 // Since the DISCOVERY_COMPLETE event can be received during the scanning interval,
Wolfgang Betz 165:3576598c0889 1128 // we need to delay the starting of connection or re-scanning procedures
Wolfgang Betz 165:3576598c0889 1129 uint16_t delay = 2*(_scanningParams.getInterval());
Wolfgang Betz 165:3576598c0889 1130
Andrea Palmieri 217:23870c55b07e 1131 #ifdef AST_FOR_MBED_OS
Wolfgang Betz 162:667f97a4db7f 1132 if(_connecting) {
Wolfgang Betz 165:3576598c0889 1133 minar::Scheduler::postCallback(makeConnection).delay(minar::milliseconds(delay));
Wolfgang Betz 132:51056160fa4a 1134 } else {
Wolfgang Betz 165:3576598c0889 1135 minar::Scheduler::postCallback(radioScanning).delay(minar::milliseconds(delay));
Wolfgang Betz 132:51056160fa4a 1136 }
Andrea Palmieri 217:23870c55b07e 1137 #else
Andrea Palmieri 217:23870c55b07e 1138 Clock_Wait(delay);
Andrea Palmieri 217:23870c55b07e 1139 if(_connecting) {
Andrea Palmieri 217:23870c55b07e 1140 makeConnection();
Andrea Palmieri 217:23870c55b07e 1141 } else {
Andrea Palmieri 217:23870c55b07e 1142 radioScanning();
Andrea Palmieri 217:23870c55b07e 1143 }
Andrea Palmieri 217:23870c55b07e 1144 #endif /* AST_FOR_MBED_OS */
Wolfgang Betz 132:51056160fa4a 1145
Wolfgang Betz 132:51056160fa4a 1146 break;
Wolfgang Betz 132:51056160fa4a 1147 }
Wolfgang Betz 132:51056160fa4a 1148 }
Wolfgang Betz 132:51056160fa4a 1149
Wolfgang Betz 132:51056160fa4a 1150 ble_error_t BlueNRGGap::startRadioScan(const GapScanningParams &scanningParams)
Wolfgang Betz 132:51056160fa4a 1151 {
Wolfgang Betz 132:51056160fa4a 1152
Wolfgang Betz 165:3576598c0889 1153 tBleStatus ret = BLE_STATUS_SUCCESS;
Wolfgang Betz 163:4c008b1089e9 1154
Andrea Palmieri 152:4afc5c2fc4f3 1155 PRINTF("Scanning...\n\r");
Andrea Palmieri 217:23870c55b07e 1156
Andrea Palmieri 217:23870c55b07e 1157 ret = btleStartRadioScan(scanningParams.getActiveScanning(),
Andrea Palmieri 217:23870c55b07e 1158 scanningParams.getInterval(),
Andrea Palmieri 217:23870c55b07e 1159 scanningParams.getWindow(),
Andrea Palmieri 217:23870c55b07e 1160 addr_type);
Andrea Palmieri 217:23870c55b07e 1161
Andrea Palmieri 217:23870c55b07e 1162
Andrea Palmieri 217:23870c55b07e 1163 if (BLE_STATUS_SUCCESS == ret){
Andrea Palmieri 217:23870c55b07e 1164 PRINTF("Observation Procedure Started\n");
Andrea Palmieri 217:23870c55b07e 1165 _scanning = true;
Andrea Palmieri 217:23870c55b07e 1166 return BLE_ERROR_NONE;
Andrea Palmieri 145:024a552e4715 1167 }
Andrea Palmieri 217:23870c55b07e 1168
Andrea Palmieri 217:23870c55b07e 1169 // Observer role is not supported by X-NUCLEO-IDB04A1, return BLE_ERROR_NOT_IMPLEMENTED
Andrea Palmieri 217:23870c55b07e 1170 switch (ret) {
Andrea Palmieri 217:23870c55b07e 1171 case BLE_STATUS_INVALID_CID:
Andrea Palmieri 217:23870c55b07e 1172 printf("Observation Procedure not implemented!!!\n\r");
Andrea Palmieri 217:23870c55b07e 1173 return BLE_ERROR_NOT_IMPLEMENTED;
Andrea Palmieri 217:23870c55b07e 1174 default:
Andrea Palmieri 217:23870c55b07e 1175 printf("Observation Procedure failed (0x%02X)\n\r", ret);
Andrea Palmieri 217:23870c55b07e 1176 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 132:51056160fa4a 1177 }
Andrea Palmieri 217:23870c55b07e 1178
Wolfgang Betz 130:770ce14d3d15 1179 }
Wolfgang Betz 130:770ce14d3d15 1180
Wolfgang Betz 130:770ce14d3d15 1181 ble_error_t BlueNRGGap::stopScan() {
Wolfgang Betz 165:3576598c0889 1182 tBleStatus ret = BLE_STATUS_SUCCESS;
Wolfgang Betz 132:51056160fa4a 1183
Andrea Palmieri 217:23870c55b07e 1184 ret = aci_gap_terminate_gap_procedure(GAP_OBSERVATION_PROC);
Wolfgang Betz 132:51056160fa4a 1185
Wolfgang Betz 132:51056160fa4a 1186 if (ret != BLE_STATUS_SUCCESS) {
Wolfgang Betz 132:51056160fa4a 1187 printf("GAP Terminate Gap Procedure failed\n");
Wolfgang Betz 132:51056160fa4a 1188 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 132:51056160fa4a 1189 } else {
Wolfgang Betz 132:51056160fa4a 1190 PRINTF("Discovery Procedure Terminated\n");
Wolfgang Betz 130:770ce14d3d15 1191 return BLE_ERROR_NONE;
Wolfgang Betz 132:51056160fa4a 1192 }
Wolfgang Betz 130:770ce14d3d15 1193 }
Wolfgang Betz 130:770ce14d3d15 1194
Wolfgang Betz 130:770ce14d3d15 1195 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1196 /*!
Wolfgang Betz 130:770ce14d3d15 1197 @brief set Tx power level
Wolfgang Betz 130:770ce14d3d15 1198 @param[in] txPower Transmission Power level
Wolfgang Betz 130:770ce14d3d15 1199 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 1200 */
Wolfgang Betz 130:770ce14d3d15 1201 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1202 ble_error_t BlueNRGGap::setTxPower(int8_t txPower)
Wolfgang Betz 130:770ce14d3d15 1203 {
Wolfgang Betz 130:770ce14d3d15 1204 tBleStatus ret;
Wolfgang Betz 130:770ce14d3d15 1205
Wolfgang Betz 130:770ce14d3d15 1206 int8_t enHighPower = 0;
Andrea Palmieri 201:a9bc2e8b01d7 1207 int8_t paLevel = 0;
Andrea Palmieri 201:a9bc2e8b01d7 1208
Wolfgang Betz 130:770ce14d3d15 1209 int8_t dbmActuallySet = getHighPowerAndPALevelValue(txPower, enHighPower, paLevel);
Andrea Palmieri 201:a9bc2e8b01d7 1210
Andrea Palmieri 201:a9bc2e8b01d7 1211 #ifndef DEBUG
Andrea Palmieri 145:024a552e4715 1212 /* avoid compiler warnings about unused variables */
Andrea Palmieri 201:a9bc2e8b01d7 1213 (void)dbmActuallySet;
Wolfgang Betz 130:770ce14d3d15 1214 #endif
Wolfgang Betz 130:770ce14d3d15 1215
Wolfgang Betz 132:51056160fa4a 1216 PRINTF("txPower=%d, dbmActuallySet=%d\n\r", txPower, dbmActuallySet);
Wolfgang Betz 132:51056160fa4a 1217 PRINTF("enHighPower=%d, paLevel=%d\n\r", enHighPower, paLevel);
Wolfgang Betz 130:770ce14d3d15 1218 ret = aci_hal_set_tx_power_level(enHighPower, paLevel);
Wolfgang Betz 130:770ce14d3d15 1219 if(ret!=BLE_STATUS_SUCCESS) {
Wolfgang Betz 130:770ce14d3d15 1220 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 130:770ce14d3d15 1221 }
Andrea Palmieri 201:a9bc2e8b01d7 1222
Wolfgang Betz 130:770ce14d3d15 1223 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 1224 }
Wolfgang Betz 130:770ce14d3d15 1225
Wolfgang Betz 130:770ce14d3d15 1226 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1227 /*!
Wolfgang Betz 130:770ce14d3d15 1228 @brief get permitted Tx power values
Wolfgang Betz 130:770ce14d3d15 1229 @param[in] values pointer to pointer to permitted power values
Wolfgang Betz 130:770ce14d3d15 1230 @param[in] num number of values
Wolfgang Betz 130:770ce14d3d15 1231 */
Wolfgang Betz 130:770ce14d3d15 1232 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 1233 void BlueNRGGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP) {
Wolfgang Betz 130:770ce14d3d15 1234 static const int8_t permittedTxValues[] = {
Wolfgang Betz 130:770ce14d3d15 1235 -18, -14, -11, -8, -4, -1, 1, 5, -15, -11, -8, -5, -2, 1, 4, 8
Wolfgang Betz 130:770ce14d3d15 1236 };
Wolfgang Betz 130:770ce14d3d15 1237
Wolfgang Betz 130:770ce14d3d15 1238 *valueArrayPP = permittedTxValues;
Wolfgang Betz 130:770ce14d3d15 1239 *countP = sizeof(permittedTxValues) / sizeof(int8_t);
Wolfgang Betz 130:770ce14d3d15 1240 }
Wolfgang Betz 132:51056160fa4a 1241
Andrea Palmieri 181:749071129d2e 1242 ble_error_t BlueNRGGap::createConnection ()
Wolfgang Betz 132:51056160fa4a 1243 {
Wolfgang Betz 132:51056160fa4a 1244 tBleStatus ret;
Wolfgang Betz 132:51056160fa4a 1245
Wolfgang Betz 132:51056160fa4a 1246 /*
Wolfgang Betz 163:4c008b1089e9 1247 Scan_Interval, Scan_Window, Peer_Address_Type, Peer_Address, Own_Address_Type, Conn_Interval_Min,
Wolfgang Betz 163:4c008b1089e9 1248 Conn_Interval_Max, Conn_Latency, Supervision_Timeout, Conn_Len_Min, Conn_Len_Max
Wolfgang Betz 132:51056160fa4a 1249 */
Andrea Palmieri 220:e02936f0d4c7 1250 ret = aci_gap_create_connection(_scanningParams.getInterval(),
Andrea Palmieri 220:e02936f0d4c7 1251 _scanningParams.getWindow(),
Wolfgang Betz 163:4c008b1089e9 1252 PUBLIC_ADDR,
Wolfgang Betz 163:4c008b1089e9 1253 (unsigned char*)_peerAddr,
Wolfgang Betz 163:4c008b1089e9 1254 PUBLIC_ADDR,
Wolfgang Betz 163:4c008b1089e9 1255 CONN_P1, CONN_P2, 0,
Andrea Palmieri 220:e02936f0d4c7 1256 SUPERV_TIMEOUT, CONN_L1 , CONN_L1);
Wolfgang Betz 165:3576598c0889 1257
Wolfgang Betz 165:3576598c0889 1258 _connecting = false;
Wolfgang Betz 163:4c008b1089e9 1259
Wolfgang Betz 163:4c008b1089e9 1260 if (ret != BLE_STATUS_SUCCESS) {
Wolfgang Betz 132:51056160fa4a 1261 printf("Error while starting connection (ret=0x%02X).\n\r", ret);
Wolfgang Betz 132:51056160fa4a 1262 return BLE_ERROR_UNSPECIFIED;
Wolfgang Betz 132:51056160fa4a 1263 } else {
Wolfgang Betz 132:51056160fa4a 1264 PRINTF("Connection started.\n");
Wolfgang Betz 132:51056160fa4a 1265 return BLE_ERROR_NONE;
Wolfgang Betz 132:51056160fa4a 1266 }
Wolfgang Betz 132:51056160fa4a 1267 }
Wolfgang Betz 132:51056160fa4a 1268
Wolfgang Betz 132:51056160fa4a 1269 ble_error_t BlueNRGGap::connect (const Gap::Address_t peerAddr,
Wolfgang Betz 132:51056160fa4a 1270 Gap::AddressType_t peerAddrType,
Wolfgang Betz 132:51056160fa4a 1271 const ConnectionParams_t *connectionParams,
Wolfgang Betz 132:51056160fa4a 1272 const GapScanningParams *scanParams)
Wolfgang Betz 132:51056160fa4a 1273 {
Andrea Palmieri 142:adf1567d3900 1274 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 1275 (void)peerAddrType;
Andrea Palmieri 142:adf1567d3900 1276 (void)connectionParams;
Andrea Palmieri 142:adf1567d3900 1277 (void)scanParams;
Andrea Palmieri 142:adf1567d3900 1278
Andrea Palmieri 217:23870c55b07e 1279 // Save the peer address
Wolfgang Betz 132:51056160fa4a 1280 for(int i=0; i<BDADDR_SIZE; i++) {
Wolfgang Betz 132:51056160fa4a 1281 _peerAddr[i] = peerAddr[i];
Wolfgang Betz 132:51056160fa4a 1282 }
Wolfgang Betz 132:51056160fa4a 1283
Andrea Palmieri 181:749071129d2e 1284 _connecting = true;
Wolfgang Betz 132:51056160fa4a 1285
Wolfgang Betz 132:51056160fa4a 1286 if(_scanning) {
Wolfgang Betz 132:51056160fa4a 1287 stopScan();
Wolfgang Betz 132:51056160fa4a 1288 } else {
Wolfgang Betz 165:3576598c0889 1289 PRINTF("Calling createConnection from connect()\n\r");
Wolfgang Betz 165:3576598c0889 1290 return createConnection();
Wolfgang Betz 132:51056160fa4a 1291 }
Wolfgang Betz 132:51056160fa4a 1292
Wolfgang Betz 132:51056160fa4a 1293 return BLE_ERROR_NONE;
Wolfgang Betz 132:51056160fa4a 1294 }