Added support for obtaining BLE device name by parsing advertising data.
Fork of BLE_BlueNRG by
Revision 7:e293b0f43dc7, committed 2014-07-31
- Comitter:
- hemddabral
- Date:
- Thu Jul 31 11:09:28 2014 +0000
- Parent:
- 6:08cfc94b5f49
- Commit message:
- Added support for parsing BLE device name from advertising data
Changed in this revision
--- a/BlueNRGGap.cpp Fri Jul 25 09:09:13 2014 +0000 +++ b/BlueNRGGap.cpp Thu Jul 31 11:09:28 2014 +0000 @@ -16,10 +16,13 @@ #include "BlueNRGDevice.h" #include "mbed.h" +#include "Payload.h" //Local Variables const uint8_t *device_name; +Serial pc1(USBTX, USBRX); // tx, rx + /**************************************************************************/ /*! @brief Sets the advertising parameters and payload for the device @@ -65,16 +68,53 @@ } /* Make sure we have a payload! */ - if (advData.getPayloadLen() == 0) { + if (advData.getPayloadLen() <= 0) { return BLE_ERROR_PARAM_OUT_OF_RANGE; + } else { //set the advData here in some local variable so that startAdvertising can use it. + Payload load(advData.getPayload(), advData.getPayloadLen()); + + for(uint8_t index=0; index<load.getPayloadUnitCount(); index++) { + //UnitPayload unitLoad = load.getPayLoadAtIndex(index); + switch(load.getIDAtIndex(index)) { + case GapAdvertisingData::FLAGS: /* ref *Flags */ + break; + case GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS: /**< Incomplete list of 16-bit Service IDs */ + break; + case GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS: /**< Complete list of 16-bit Service IDs */ + break; + case GapAdvertisingData::INCOMPLETE_LIST_32BIT_SERVICE_IDS: /**< Incomplete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */ + break; + case GapAdvertisingData::COMPLETE_LIST_32BIT_SERVICE_IDS: /**< Complete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */ + break; + case GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS: /**< Incomplete list of 128-bit Service IDs */ + break; + case GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS: /**< Complete list of 128-bit Service IDs */ + break; + case GapAdvertisingData::SHORTENED_LOCAL_NAME: /**< Shortened Local Name */ + break; + case GapAdvertisingData::COMPLETE_LOCAL_NAME: /**< Complete Local Name */ + device_name = load.getDataAtIndex(index); + break; + case GapAdvertisingData::TX_POWER_LEVEL: /**< TX Power Level (in dBm) */ + break; + case GapAdvertisingData::DEVICE_ID: /**< Device ID */ + break; + case GapAdvertisingData::SLAVE_CONNECTION_INTERVAL_RANGE: /**< Slave :Connection Interval Range */ + break; + case GapAdvertisingData::SERVICE_DATA: /**< Service Data */ + break; + case GapAdvertisingData::APPEARANCE: /**< \ref Appearance */ + break; + case GapAdvertisingData::ADVERTISING_INTERVAL: /**< Advertising Interval */ + break; + case GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA: /**< Manufacturer Specific Data */ + break; + + } + } + //const uint8_t *payload = advData.getPayload(); + } - - //set the advData here in some local variable so that startAdvertising can use it. - if (advData.getPayloadLen() > 0) { - const uint8_t *payload = advData.getPayload(); - device_name = advData.getPayload(); - } - return BLE_ERROR_NONE; } @@ -139,8 +179,27 @@ //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[27],device_name[28],device_name[29],device_name[30], device_name[31], // device_name[32], device_name[33], device_name[34], device_name[35], device_name[36]}; - const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[13],device_name[14],device_name[15],device_name[16], device_name[17], - device_name[18], device_name[19], device_name[20], device_name[21], device_name[22]}; + #if 0 + pc1.printf("Hello Teraterm\n"); + /*const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[13],device_name[14],device_name[15],device_name[16], device_name[17], + device_name[18], device_name[19], device_name[20], device_name[21], device_name[22]};*/ + const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'h', 'd', 'd'}; + #else + + char *namePtr = new char[1+sizeof(device_name)]; + namePtr[0] = AD_TYPE_COMPLETE_LOCAL_NAME; + pc1.printf("now setting name to: %s...\n", device_name); + pc1.printf("device_name length=%d", sizeof(namePtr)); + int i=0; + while(device_name[i]!=0) { + namePtr[i+1] = device_name[i]; + pc1.printf("%c\n", namePtr[i+1]); + i++; + } + + const char* local_name = (const char*)namePtr; + + #endif //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[27],device_name[28]}; const LongUUID_t HRM_SERVICE_UUID_128 = {0x18, 0x0D};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Payload.cpp Thu Jul 31 11:09:28 2014 +0000 @@ -0,0 +1,60 @@ +#include <Payload.h> + + Payload::Payload() { + stringLength = 0; + payloadUnitCount = 0; + payload = NULL; + } + + Payload::Payload(const uint8_t *tokenString, uint8_t string_ength) { + // initialize private data members + stringLength = string_ength; + payloadUnitCount = 0; + payload = NULL; + + int index = 0; + while( index!=stringLength) { + int len=tokenString[index]; + index=index+1+len; + payloadUnitCount++; + } + + UnitPayload *obj = new UnitPayload[payloadUnitCount]; + int i=0; + int c=0; + int j,k; + + while(i<payloadUnitCount) + { + obj[i].length=tokenString[c]; + obj[i].id=tokenString[c+1]; + + obj[i].data = new uint8_t[obj[i].length]; + for(j=c+2,k=0;(j<(c+obj[i].length+1))&&(k<obj[i].length-1);j++,k++) + { + obj[i].data[k]=tokenString[j]; + + } + + c=c+obj[i].length+1; + i++; + + } + payload = obj; + } + +uint8_t Payload::getPayloadUnitCount() { + return payloadUnitCount; +} + +uint8_t Payload::getIDAtIndex(int index) { + return payload[index].get_id(); +} + +uint8_t Payload::getLengthAtIndex(int index) { + return payload[index].get_length(); +} + +uint8_t* Payload::getDataAtIndex(int index) { + return payload[index].get_data(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Payload.h Thu Jul 31 11:09:28 2014 +0000 @@ -0,0 +1,57 @@ +#include "mbed.h" + +#ifndef __PAYLOAD_H__ +#define __PAYLOAD_H__ + +class UnitPayload +{ + public: + uint8_t length; + uint8_t id; + uint8_t *data; + + void set_length(uint8_t l) { + length=l; + } + + void set_id(uint8_t i) { + id=i; + } + + void set_data(uint8_t* data1) { + for(int j=0;j<length;j++) + { + data[j]=data1[j]; + } + } + + uint8_t get_length() { + return length; + } + + uint8_t get_id() { + return id; + } + + uint8_t* get_data() { + return data; + } + +}; + +class Payload { + UnitPayload *payload; + int stringLength; + int payloadUnitCount; + +public: + Payload(const uint8_t *tokenString, uint8_t string_ength); + Payload::Payload(); + uint8_t Payload::getPayloadUnitCount(); + + uint8_t Payload::getIDAtIndex(int index); + uint8_t Payload::getLengthAtIndex(int index); + uint8_t* Payload::getDataAtIndex(int index); +}; + +#endif // __PAYLOAD_H__ \ No newline at end of file
--- a/btle_test.cpp Fri Jul 25 09:09:13 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* -#ifdef __cplusplus -extern "C" { -#endif*/ -#if 0 -#include "btle.h" - -#include "hw/Gap.h" -#include "hw/GattServer.h" -#include "hw/BLEDevice.h" - -/* -#ifdef __cplusplus - } -#endif*/ -#include "mbed.h" -#include "blecommon.h" - -#include "blueNRGGap.h" -#include "blueNRGGattServer.h" - - -/************************************************************************* -! - \brief - - -*************************************************************************/ -void test_function(void); - -void test_function(void) -{ - //btle_init(); - - } - -class BTLE_Test //: public BLEDeviceInstanceBase -{ - public: - BTLE_Test(void); - virtual ~BTLE_Test(void); - void Init(void); - - /*virtual Gap& getGap() { return blueNRGGap::getInstance(); }; - virtual GattServer& getGattServer() { return blueNRGGattServer::getInstance(); }; - virtual ble_error_t init(void); - virtual ble_error_t reset(void); - virtual void waitForEvent(void); */ - }; - -void BTLE_Test::Init(void) { - //btle_init(); - } - -#endif \ No newline at end of file