Devchannel Team / X_NUCLEO_IDB0XA1

Dependents:   Hello_BLE F446RE-BLE

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
hemddabral
Date:
Mon Aug 04 08:48:24 2014 +0000
Revision:
10:814262eb0746
Parent:
6:08cfc94b5f49
Child:
11:87ddcfa4fa48
added support for parsing device name in GAP

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