adaptation for bkap

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Andrea Palmieri
Date:
Fri Nov 20 12:14:47 2015 +0100
Revision:
187:2e517d3ddd71
Parent:
186:7e3a549b9678
Child:
189:30fa96fa8ec4
Leave default device name "unknown" and other minor fixes

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