Added support for obtaining BLE device name by parsing advertising data.

Fork of BLE_BlueNRG by Mridupawan Das

Committer:
hemddabral
Date:
Thu Jul 31 11:09:28 2014 +0000
Revision:
7:e293b0f43dc7
Parent:
6:08cfc94b5f49
Added support for parsing BLE device name from advertising data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mridup 2:a2b623661316 1 /* mbed Microcontroller Library
mridup 2:a2b623661316 2 * Copyright (c) 2006-2013 ARM Limited
mridup 2:a2b623661316 3 *
mridup 2:a2b623661316 4 * Licensed under the Apache License, Version 2.0 (the "License");
mridup 2:a2b623661316 5 * you may not use this file except in compliance with the License.
mridup 2:a2b623661316 6 * You may obtain a copy of the License at
mridup 2:a2b623661316 7 *
mridup 2:a2b623661316 8 * http://www.apache.org/licenses/LICENSE-2.0
mridup 2:a2b623661316 9 *
mridup 2:a2b623661316 10 * Unless required by applicable law or agreed to in writing, software
mridup 2:a2b623661316 11 * distributed under the License is distributed on an "AS IS" BASIS,
mridup 2:a2b623661316 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mridup 2:a2b623661316 13 * See the License for the specific language governing permissions and
mridup 2:a2b623661316 14 * limitations under the License.
mridup 2:a2b623661316 15 */
mridup 2:a2b623661316 16
mridup 2:a2b623661316 17 #include "BlueNRGDevice.h"
mridup 2:a2b623661316 18 #include "mbed.h"
hemddabral 7:e293b0f43dc7 19 #include "Payload.h"
mridup 2:a2b623661316 20
mridup 3:9c4c13795643 21 //Local Variables
mridup 3:9c4c13795643 22 const uint8_t *device_name;
mridup 2:a2b623661316 23
hemddabral 7:e293b0f43dc7 24 Serial pc1(USBTX, USBRX); // tx, rx
hemddabral 7:e293b0f43dc7 25
mridup 2:a2b623661316 26 /**************************************************************************/
mridup 2:a2b623661316 27 /*!
mridup 2:a2b623661316 28 @brief Sets the advertising parameters and payload for the device
mridup 2:a2b623661316 29
mridup 2:a2b623661316 30 @param[in] params
mridup 2:a2b623661316 31 Basic advertising details, including the advertising
mridup 2:a2b623661316 32 delay, timeout and how the device should be advertised
mridup 2:a2b623661316 33 @params[in] advData
mridup 2:a2b623661316 34 The primary advertising data payload
mridup 2:a2b623661316 35 @params[in] scanResponse
mridup 2:a2b623661316 36 The optional Scan Response payload if the advertising
mridup 2:a2b623661316 37 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
mridup 2:a2b623661316 38 in \ref GapAdveritinngParams
mridup 2:a2b623661316 39
mridup 2:a2b623661316 40 @returns \ref ble_error_t
mridup 2:a2b623661316 41
mridup 2:a2b623661316 42 @retval BLE_ERROR_NONE
mridup 2:a2b623661316 43 Everything executed properly
mridup 2:a2b623661316 44
mridup 2:a2b623661316 45 @retval BLE_ERROR_BUFFER_OVERFLOW
mridup 2:a2b623661316 46 The proposed action would cause a buffer overflow. All
mridup 2:a2b623661316 47 advertising payloads must be <= 31 bytes, for example.
mridup 2:a2b623661316 48
mridup 2:a2b623661316 49 @retval BLE_ERROR_NOT_IMPLEMENTED
mridup 2:a2b623661316 50 A feature was requested that is not yet supported in the
mridup 2:a2b623661316 51 nRF51 firmware or hardware.
mridup 2:a2b623661316 52
mridup 2:a2b623661316 53 @retval BLE_ERROR_PARAM_OUT_OF_RANGE
mridup 2:a2b623661316 54 One of the proposed values is outside the valid range.
mridup 2:a2b623661316 55
mridup 2:a2b623661316 56 @section EXAMPLE
mridup 2:a2b623661316 57
mridup 2:a2b623661316 58 @code
mridup 2:a2b623661316 59
mridup 2:a2b623661316 60 @endcode
mridup 2:a2b623661316 61 */
mridup 2:a2b623661316 62 /**************************************************************************/
mridup 2:a2b623661316 63 ble_error_t BlueNRGGap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse)
mridup 2:a2b623661316 64 {
mridup 2:a2b623661316 65 /* Make sure we don't exceed the advertising payload length */
mridup 2:a2b623661316 66 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
mridup 2:a2b623661316 67 return BLE_ERROR_BUFFER_OVERFLOW;
mridup 2:a2b623661316 68 }
mridup 2:a2b623661316 69
mridup 2:a2b623661316 70 /* Make sure we have a payload! */
hemddabral 7:e293b0f43dc7 71 if (advData.getPayloadLen() <= 0) {
mridup 2:a2b623661316 72 return BLE_ERROR_PARAM_OUT_OF_RANGE;
hemddabral 7:e293b0f43dc7 73 } else { //set the advData here in some local variable so that startAdvertising can use it.
hemddabral 7:e293b0f43dc7 74 Payload load(advData.getPayload(), advData.getPayloadLen());
hemddabral 7:e293b0f43dc7 75
hemddabral 7:e293b0f43dc7 76 for(uint8_t index=0; index<load.getPayloadUnitCount(); index++) {
hemddabral 7:e293b0f43dc7 77 //UnitPayload unitLoad = load.getPayLoadAtIndex(index);
hemddabral 7:e293b0f43dc7 78 switch(load.getIDAtIndex(index)) {
hemddabral 7:e293b0f43dc7 79 case GapAdvertisingData::FLAGS: /* ref *Flags */
hemddabral 7:e293b0f43dc7 80 break;
hemddabral 7:e293b0f43dc7 81 case GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS: /**< Incomplete list of 16-bit Service IDs */
hemddabral 7:e293b0f43dc7 82 break;
hemddabral 7:e293b0f43dc7 83 case GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS: /**< Complete list of 16-bit Service IDs */
hemddabral 7:e293b0f43dc7 84 break;
hemddabral 7:e293b0f43dc7 85 case GapAdvertisingData::INCOMPLETE_LIST_32BIT_SERVICE_IDS: /**< Incomplete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */
hemddabral 7:e293b0f43dc7 86 break;
hemddabral 7:e293b0f43dc7 87 case GapAdvertisingData::COMPLETE_LIST_32BIT_SERVICE_IDS: /**< Complete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */
hemddabral 7:e293b0f43dc7 88 break;
hemddabral 7:e293b0f43dc7 89 case GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS: /**< Incomplete list of 128-bit Service IDs */
hemddabral 7:e293b0f43dc7 90 break;
hemddabral 7:e293b0f43dc7 91 case GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS: /**< Complete list of 128-bit Service IDs */
hemddabral 7:e293b0f43dc7 92 break;
hemddabral 7:e293b0f43dc7 93 case GapAdvertisingData::SHORTENED_LOCAL_NAME: /**< Shortened Local Name */
hemddabral 7:e293b0f43dc7 94 break;
hemddabral 7:e293b0f43dc7 95 case GapAdvertisingData::COMPLETE_LOCAL_NAME: /**< Complete Local Name */
hemddabral 7:e293b0f43dc7 96 device_name = load.getDataAtIndex(index);
hemddabral 7:e293b0f43dc7 97 break;
hemddabral 7:e293b0f43dc7 98 case GapAdvertisingData::TX_POWER_LEVEL: /**< TX Power Level (in dBm) */
hemddabral 7:e293b0f43dc7 99 break;
hemddabral 7:e293b0f43dc7 100 case GapAdvertisingData::DEVICE_ID: /**< Device ID */
hemddabral 7:e293b0f43dc7 101 break;
hemddabral 7:e293b0f43dc7 102 case GapAdvertisingData::SLAVE_CONNECTION_INTERVAL_RANGE: /**< Slave :Connection Interval Range */
hemddabral 7:e293b0f43dc7 103 break;
hemddabral 7:e293b0f43dc7 104 case GapAdvertisingData::SERVICE_DATA: /**< Service Data */
hemddabral 7:e293b0f43dc7 105 break;
hemddabral 7:e293b0f43dc7 106 case GapAdvertisingData::APPEARANCE: /**< \ref Appearance */
hemddabral 7:e293b0f43dc7 107 break;
hemddabral 7:e293b0f43dc7 108 case GapAdvertisingData::ADVERTISING_INTERVAL: /**< Advertising Interval */
hemddabral 7:e293b0f43dc7 109 break;
hemddabral 7:e293b0f43dc7 110 case GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA: /**< Manufacturer Specific Data */
hemddabral 7:e293b0f43dc7 111 break;
hemddabral 7:e293b0f43dc7 112
hemddabral 7:e293b0f43dc7 113 }
hemddabral 7:e293b0f43dc7 114 }
hemddabral 7:e293b0f43dc7 115 //const uint8_t *payload = advData.getPayload();
hemddabral 7:e293b0f43dc7 116
mridup 2:a2b623661316 117 }
mridup 3:9c4c13795643 118
mridup 2:a2b623661316 119 return BLE_ERROR_NONE;
mridup 2:a2b623661316 120 }
mridup 2:a2b623661316 121
mridup 2:a2b623661316 122 /**************************************************************************/
mridup 2:a2b623661316 123 /*!
mridup 2:a2b623661316 124 @brief Starts the BLE HW, initialising any services that were
mridup 2:a2b623661316 125 added before this function was called.
mridup 2:a2b623661316 126
mridup 2:a2b623661316 127 @note All services must be added before calling this function!
mridup 2:a2b623661316 128
mridup 2:a2b623661316 129 @returns ble_error_t
mridup 2:a2b623661316 130
mridup 2:a2b623661316 131 @retval BLE_ERROR_NONE
mridup 2:a2b623661316 132 Everything executed properly
mridup 2:a2b623661316 133
mridup 2:a2b623661316 134 @section EXAMPLE
mridup 2:a2b623661316 135
mridup 2:a2b623661316 136 @code
mridup 2:a2b623661316 137
mridup 2:a2b623661316 138 @endcode
mridup 2:a2b623661316 139 */
mridup 2:a2b623661316 140 /**************************************************************************/
mridup 2:a2b623661316 141 ble_error_t BlueNRGGap::startAdvertising(const GapAdvertisingParams &params)
mridup 2:a2b623661316 142 {
mridup 2:a2b623661316 143 /* Make sure we support the advertising type */
mridup 2:a2b623661316 144 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) {
mridup 2:a2b623661316 145 /* ToDo: This requires a propery security implementation, etc. */
mridup 2:a2b623661316 146 return BLE_ERROR_NOT_IMPLEMENTED;
mridup 2:a2b623661316 147 }
mridup 2:a2b623661316 148
mridup 2:a2b623661316 149 /* Check interval range */
mridup 2:a2b623661316 150 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) {
mridup 2:a2b623661316 151 /* Min delay is slightly longer for unconnectable devices */
mridup 2:a2b623661316 152 if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
mridup 2:a2b623661316 153 (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) {
mridup 2:a2b623661316 154 return BLE_ERROR_PARAM_OUT_OF_RANGE;
mridup 2:a2b623661316 155 }
mridup 2:a2b623661316 156 } else {
mridup 2:a2b623661316 157 if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) ||
mridup 2:a2b623661316 158 (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) {
mridup 2:a2b623661316 159 return BLE_ERROR_PARAM_OUT_OF_RANGE;
mridup 2:a2b623661316 160 }
mridup 2:a2b623661316 161 }
mridup 2:a2b623661316 162
mridup 2:a2b623661316 163 /* Check timeout is zero for Connectable Directed */
mridup 2:a2b623661316 164 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) {
mridup 2:a2b623661316 165 /* Timeout must be 0 with this type, although we'll never get here */
mridup 2:a2b623661316 166 /* since this isn't implemented yet anyway */
mridup 2:a2b623661316 167 return BLE_ERROR_PARAM_OUT_OF_RANGE;
mridup 2:a2b623661316 168 }
mridup 2:a2b623661316 169
mridup 2:a2b623661316 170 /* Check timeout for other advertising types */
mridup 2:a2b623661316 171 if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
mridup 2:a2b623661316 172 (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX)) {
mridup 2:a2b623661316 173 return BLE_ERROR_PARAM_OUT_OF_RANGE;
mridup 2:a2b623661316 174 }
mridup 2:a2b623661316 175
mridup 2:a2b623661316 176 tBleStatus ret;
mridup 2:a2b623661316 177
mridup 3:9c4c13795643 178 //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G'};
mridup 5:31dedfa19a12 179 //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[27],device_name[28],device_name[29],device_name[30], device_name[31],
mridup 5:31dedfa19a12 180 // device_name[32], device_name[33], device_name[34], device_name[35], device_name[36]};
mridup 2:a2b623661316 181
hemddabral 7:e293b0f43dc7 182 #if 0
hemddabral 7:e293b0f43dc7 183 pc1.printf("Hello Teraterm\n");
hemddabral 7:e293b0f43dc7 184 /*const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[13],device_name[14],device_name[15],device_name[16], device_name[17],
hemddabral 7:e293b0f43dc7 185 device_name[18], device_name[19], device_name[20], device_name[21], device_name[22]};*/
hemddabral 7:e293b0f43dc7 186 const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'h', 'd', 'd'};
hemddabral 7:e293b0f43dc7 187 #else
hemddabral 7:e293b0f43dc7 188
hemddabral 7:e293b0f43dc7 189 char *namePtr = new char[1+sizeof(device_name)];
hemddabral 7:e293b0f43dc7 190 namePtr[0] = AD_TYPE_COMPLETE_LOCAL_NAME;
hemddabral 7:e293b0f43dc7 191 pc1.printf("now setting name to: %s...\n", device_name);
hemddabral 7:e293b0f43dc7 192 pc1.printf("device_name length=%d", sizeof(namePtr));
hemddabral 7:e293b0f43dc7 193 int i=0;
hemddabral 7:e293b0f43dc7 194 while(device_name[i]!=0) {
hemddabral 7:e293b0f43dc7 195 namePtr[i+1] = device_name[i];
hemddabral 7:e293b0f43dc7 196 pc1.printf("%c\n", namePtr[i+1]);
hemddabral 7:e293b0f43dc7 197 i++;
hemddabral 7:e293b0f43dc7 198 }
hemddabral 7:e293b0f43dc7 199
hemddabral 7:e293b0f43dc7 200 const char* local_name = (const char*)namePtr;
hemddabral 7:e293b0f43dc7 201
hemddabral 7:e293b0f43dc7 202 #endif
mridup 5:31dedfa19a12 203
mridup 5:31dedfa19a12 204 //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[27],device_name[28]};
mridup 5:31dedfa19a12 205 const LongUUID_t HRM_SERVICE_UUID_128 = {0x18, 0x0D};
mridup 2:a2b623661316 206 /* disable scan response */
mridup 2:a2b623661316 207 hci_le_set_scan_resp_data(0,NULL);
mridup 2:a2b623661316 208
mridup 2:a2b623661316 209 /*aci_gap_set_discoverable(Advertising_Event_Type, Adv_min_intvl, Adv_Max_Intvl, Addr_Type, Adv_Filter_Policy,
mridup 2:a2b623661316 210 Local_Name_Length, local_name, service_uuid_length, service_uuid_list, Slave_conn_intvl_min, Slave_conn_intvl_max);*/
mridup 2:a2b623661316 211 /*LINK_LAYER.H DESCRIBES THE ADVERTISING TYPES*/
mridup 2:a2b623661316 212
mridup 2:a2b623661316 213 ret = aci_gap_set_discoverable(params.getAdvertisingType(), params.getInterval(), 0, PUBLIC_ADDR, NO_WHITE_LIST_USE,
mridup 4:fa5b5693c1b5 214 11 /*Length of the local_name[] array*/, local_name, 0, NULL, 0, 0);
mridup 4:fa5b5693c1b5 215 state.advertising = 1;
mridup 2:a2b623661316 216
mridup 2:a2b623661316 217 return BLE_ERROR_NONE;
mridup 2:a2b623661316 218 }
mridup 2:a2b623661316 219
mridup 2:a2b623661316 220 /**************************************************************************/
mridup 2:a2b623661316 221 /*!
mridup 2:a2b623661316 222 @brief Stops the BLE HW and disconnects from any devices
mridup 2:a2b623661316 223
mridup 2:a2b623661316 224 @returns ble_error_t
mridup 2:a2b623661316 225
mridup 2:a2b623661316 226 @retval BLE_ERROR_NONE
mridup 2:a2b623661316 227 Everything executed properly
mridup 2:a2b623661316 228
mridup 2:a2b623661316 229 @section EXAMPLE
mridup 2:a2b623661316 230
mridup 2:a2b623661316 231 @code
mridup 2:a2b623661316 232
mridup 2:a2b623661316 233 @endcode
mridup 2:a2b623661316 234 */
mridup 2:a2b623661316 235 /**************************************************************************/
mridup 2:a2b623661316 236 ble_error_t BlueNRGGap::stopAdvertising(void)
mridup 2:a2b623661316 237 {
mridup 2:a2b623661316 238
mridup 2:a2b623661316 239
mridup 2:a2b623661316 240 return BLE_ERROR_NONE;
mridup 2:a2b623661316 241 }
mridup 2:a2b623661316 242
mridup 2:a2b623661316 243 /**************************************************************************/
mridup 2:a2b623661316 244 /*!
mridup 2:a2b623661316 245 @brief Disconnects if we are connected to a central device
mridup 2:a2b623661316 246
mridup 2:a2b623661316 247 @returns ble_error_t
mridup 2:a2b623661316 248
mridup 2:a2b623661316 249 @retval BLE_ERROR_NONE
mridup 2:a2b623661316 250 Everything executed properly
mridup 2:a2b623661316 251
mridup 2:a2b623661316 252 @section EXAMPLE
mridup 2:a2b623661316 253
mridup 2:a2b623661316 254 @code
mridup 2:a2b623661316 255
mridup 2:a2b623661316 256 @endcode
mridup 2:a2b623661316 257 */
mridup 2:a2b623661316 258 /**************************************************************************/
mridup 2:a2b623661316 259 ble_error_t BlueNRGGap::disconnect(void)
mridup 2:a2b623661316 260 {
mridup 2:a2b623661316 261
mridup 2:a2b623661316 262 return BLE_ERROR_NONE;
mridup 2:a2b623661316 263 }
mridup 2:a2b623661316 264
mridup 2:a2b623661316 265 /**************************************************************************/
mridup 2:a2b623661316 266 /*!
mridup 2:a2b623661316 267 @brief Sets the 16-bit connection handle
mridup 2:a2b623661316 268 */
mridup 2:a2b623661316 269 /**************************************************************************/
mridup 2:a2b623661316 270 void BlueNRGGap::setConnectionHandle(uint16_t con_handle)
mridup 2:a2b623661316 271 {
mridup 2:a2b623661316 272 m_connectionHandle = con_handle;
mridup 2:a2b623661316 273 }
mridup 2:a2b623661316 274
mridup 2:a2b623661316 275 /**************************************************************************/
mridup 2:a2b623661316 276 /*!
mridup 2:a2b623661316 277 @brief Gets the 16-bit connection handle
mridup 2:a2b623661316 278 */
mridup 2:a2b623661316 279 /**************************************************************************/
mridup 2:a2b623661316 280 uint16_t BlueNRGGap::getConnectionHandle(void)
mridup 2:a2b623661316 281 {
mridup 2:a2b623661316 282 return m_connectionHandle;
mridup 2:a2b623661316 283 }
mridup 2:a2b623661316 284
mridup 2:a2b623661316 285 /**************************************************************************/
mridup 2:a2b623661316 286 /*!
mridup 2:a2b623661316 287 @brief Sets the BLE device address
mridup 2:a2b623661316 288
mridup 2:a2b623661316 289 @returns ble_error_t
mridup 2:a2b623661316 290
mridup 2:a2b623661316 291 @section EXAMPLE
mridup 2:a2b623661316 292
mridup 2:a2b623661316 293 @code
mridup 2:a2b623661316 294
mridup 2:a2b623661316 295 @endcode
mridup 2:a2b623661316 296 */
mridup 2:a2b623661316 297 /**************************************************************************/
mridup 2:a2b623661316 298 ble_error_t BlueNRGGap::setAddress(addr_type_t type, const uint8_t address[6])
mridup 2:a2b623661316 299 {
mridup 4:fa5b5693c1b5 300 tBleStatus ret;
mridup 4:fa5b5693c1b5 301
mridup 2:a2b623661316 302 if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) {
mridup 2:a2b623661316 303 return BLE_ERROR_PARAM_OUT_OF_RANGE;
mridup 2:a2b623661316 304 }
mridup 4:fa5b5693c1b5 305
mridup 4:fa5b5693c1b5 306 ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, (const tHalUint8*)address);
mridup 4:fa5b5693c1b5 307
mridup 4:fa5b5693c1b5 308 //if (ret==BLE_STATUS_SUCCESS)
mridup 2:a2b623661316 309 return BLE_ERROR_NONE;
mridup 4:fa5b5693c1b5 310
mridup 4:fa5b5693c1b5 311 //return BLE_ERROR_PARAM_OUT_OF_RANGE;
mridup 2:a2b623661316 312 }