BLE test

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Andrea Palmieri
Date:
Fri Apr 08 17:42:38 2016 +0200
Revision:
223:36df860f7ffc
Parent:
222:37a7e289ee3d
Child:
224:d8669b6b3175
Stack Mode 0x04 allows Simultaneous Scanning and Advertisement (SSAdv)

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