Skip to content

Allow advertisement to contain multiple types of data #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/BLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
#include "BLERemoteCharacteristic.h"
#include "BLERemoteService.h"

struct BLEEirData
{
unsigned char length;
unsigned char type;
unsigned char data[BLE_EIR_DATA_MAX_VALUE_LENGTH];
};

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this!

What do you think about renaming it to BLEEirData or something else, and also using it for the scan response arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that sounds like a good idea. I can add it to the PR.

class BLEDevice;

class BLEDeviceEventListener
Expand Down Expand Up @@ -49,12 +56,10 @@ class BLEDevice
void setConnectable(bool connectable);
void setBondStore(BLEBondStore& bondStore);

virtual void begin(unsigned char /*advertisementDataType*/,
unsigned char /*advertisementDataLength*/,
const unsigned char* /*advertisementData*/,
unsigned char /*scanDataType*/,
unsigned char /*scanDataLength*/,
const unsigned char* /*scanData*/,
virtual void begin(unsigned char /*advertisementDataSize*/,
BLEEirData * /*advertisementData*/,
unsigned char /*scanDataSize*/,
BLEEirData * /*scanData*/,
BLELocalAttribute** /*localAttributes*/,
unsigned char /*numLocalAttributes*/,
BLERemoteAttribute** /*remoteAttributes*/,
Expand Down
2 changes: 2 additions & 0 deletions src/BLEDeviceLimits.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@

#define BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH 26
#define BLE_SCAN_DATA_MAX_VALUE_LENGTH 29
#define BLE_EIR_DATA_MAX_VALUE_LENGTH 29
#define BLE_ATTRIBUTE_MAX_VALUE_LENGTH 20
#define BLE_REMOTE_ATTRIBUTE_MAX_VALUE_LENGTH 22

#else

#define BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH 20
#define BLE_SCAN_DATA_MAX_VALUE_LENGTH 20
#define BLE_EIR_DATA_MAX_VALUE_LENGTH 20
#define BLE_ATTRIBUTE_MAX_VALUE_LENGTH 20
#define BLE_REMOTE_ATTRIBUTE_MAX_VALUE_LENGTH 22

Expand Down
68 changes: 40 additions & 28 deletions src/BLEPeripheral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,52 +69,64 @@ BLEPeripheral::~BLEPeripheral() {
}

void BLEPeripheral::begin() {
unsigned char advertisementDataType = 0;
unsigned char scanDataType = 0;
unsigned char advertisementDataSize = 0;

unsigned char advertisementDataLength = 0;
unsigned char scanDataLength = 0;

unsigned char advertisementData[BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH];
unsigned char scanData[BLE_SCAN_DATA_MAX_VALUE_LENGTH];
BLEEirData advertisementData[3];
BLEEirData scanData = { 0 };

unsigned char remainingAdvertisementDataLength = BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH + 2;
if (this->_serviceSolicitationUuid){
BLEUuid serviceSolicitationUuid = BLEUuid(this->_serviceSolicitationUuid);

advertisementDataLength = serviceSolicitationUuid.length();
advertisementDataType = (advertisementDataLength > 2) ? 0x15 : 0x14;
unsigned char uuidLength = serviceSolicitationUuid.length();
advertisementData[advertisementDataSize].length = uuidLength;
advertisementData[advertisementDataSize].type = (uuidLength > 2) ? 0x15 : 0x14;

memcpy(advertisementData, serviceSolicitationUuid.data(), advertisementDataLength);
} else if (this->_advertisedServiceUuid){
memcpy(advertisementData[advertisementDataSize].data, serviceSolicitationUuid.data(), uuidLength);
advertisementDataSize += 1;
remainingAdvertisementDataLength -= uuidLength + 2;
}
if (this->_advertisedServiceUuid){
BLEUuid advertisedServiceUuid = BLEUuid(this->_advertisedServiceUuid);

advertisementDataLength = advertisedServiceUuid.length();
advertisementDataType = (advertisementDataLength > 2) ? 0x06 : 0x02;

memcpy(advertisementData, advertisedServiceUuid.data(), advertisementDataLength);
} else if (this->_manufacturerData && this->_manufacturerDataLength > 0) {
advertisementDataLength = this->_manufacturerDataLength;
unsigned char uuidLength = advertisedServiceUuid.length();
if (uuidLength + 2 <= remainingAdvertisementDataLength) {
advertisementData[advertisementDataSize].length = uuidLength;
advertisementData[advertisementDataSize].type = (uuidLength > 2) ? 0x06 : 0x02;

if (advertisementDataLength > sizeof(advertisementData)) {
advertisementDataLength = sizeof(advertisementData);
memcpy(advertisementData[advertisementDataSize].data, advertisedServiceUuid.data(), uuidLength);
advertisementDataSize += 1;
remainingAdvertisementDataLength -= uuidLength + 2;
}
}
if (this->_manufacturerData && this->_manufacturerDataLength > 0) {
if (remainingAdvertisementDataLength >= 3) {
unsigned char dataLength = this->_manufacturerDataLength;

if (dataLength + 2 > remainingAdvertisementDataLength) {
dataLength = remainingAdvertisementDataLength - 2;
}

advertisementDataType = 0xff;
advertisementData[advertisementDataSize].length = dataLength;
advertisementData[advertisementDataSize].type = 0xff;

memcpy(advertisementData, this->_manufacturerData, advertisementDataLength);
memcpy(advertisementData[advertisementDataSize].data, this->_manufacturerData, dataLength);
advertisementDataSize += 1;
remainingAdvertisementDataLength -= dataLength + 2;
}
}

if (this->_localName){
unsigned char localNameLength = strlen(this->_localName);
scanDataLength = localNameLength;
scanData.length = localNameLength;

if (scanDataLength > sizeof(scanData)) {
scanDataLength = sizeof(scanData);
if (scanData.length > BLE_SCAN_DATA_MAX_VALUE_LENGTH) {
scanData.length = BLE_SCAN_DATA_MAX_VALUE_LENGTH;
}

scanDataType = (localNameLength > scanDataLength) ? 0x08 : 0x09;
scanData.type = (localNameLength > scanData.length) ? 0x08 : 0x09;

memcpy(scanData, this->_localName, scanDataLength);
memcpy(scanData.data, this->_localName, scanData.length);
}

if (this->_localAttributes == NULL) {
Expand Down Expand Up @@ -144,8 +156,8 @@ void BLEPeripheral::begin() {
this->addRemoteAttribute(this->_remoteServicesChangedCharacteristic);
}

this->_device->begin(advertisementDataType, advertisementDataLength, advertisementData,
scanDataType, scanDataLength, scanData,
this->_device->begin(advertisementDataSize, advertisementData,
scanData.length > 0 ? 1 : 0, &scanData,
this->_localAttributes, this->_numLocalAttributes,
this->_remoteAttributes, this->_numRemoteAttributes);

Expand Down
38 changes: 21 additions & 17 deletions src/nRF51822.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,10 @@ nRF51822::~nRF51822() {
this->end();
}

void nRF51822::begin(unsigned char advertisementDataType,
unsigned char advertisementDataLength,
const unsigned char* advertisementData,
unsigned char scanDataType,
unsigned char scanDataLength,
const unsigned char* scanData,
void nRF51822::begin(unsigned char advertisementDataSize,
BLEEirData *advertisementData,
unsigned char scanDataSize,
BLEEirData *scanData,
BLELocalAttribute** localAttributes,
unsigned char numLocalAttributes,
BLERemoteAttribute** remoteAttributes,
Expand Down Expand Up @@ -196,22 +194,28 @@ void nRF51822::begin(unsigned char advertisementDataType,

this->_advDataLen += 3;

if (advertisementDataType && advertisementDataLength && advertisementData) {
this->_advData[this->_advDataLen + 0] = advertisementDataLength + 1;
this->_advData[this->_advDataLen + 1] = advertisementDataType;
this->_advDataLen += 2;
if (advertisementDataSize && advertisementData) {
for (int i = 0; i < advertisementDataSize; i++) {
this->_advData[this->_advDataLen + 0] = advertisementData[i].length + 1;
this->_advData[this->_advDataLen + 1] = advertisementData[i].type;
this->_advDataLen += 2;

memcpy(&this->_advData[this->_advDataLen], advertisementData, advertisementDataLength);
memcpy(&this->_advData[this->_advDataLen], advertisementData[i].data, advertisementData[i].length);

this->_advDataLen += advertisementDataLength;
this->_advDataLen += advertisementData[i].length;
}
}

if (scanDataType && scanDataLength && scanData) {
srData[0] = scanDataLength + 1;
srData[1] = scanDataType;
memcpy(&srData[2], scanData, scanDataLength);
if (scanDataSize && scanData) {
for (int i = 0; i < scanDataSize; i++) {
srData[srDataLen + 0] = scanData[i].length + 1;
srData[srDataLen + 1] = scanData[i].type;
srDataLen += 2;

memcpy(&srData[srDataLen], scanData[i].data, scanData[i].length);

srDataLen = 2 + scanDataLength;
srDataLen += scanData[i].length;
}
}

sd_ble_gap_adv_data_set(this->_advData, this->_advDataLen, srData, srDataLen);
Expand Down
10 changes: 4 additions & 6 deletions src/nRF51822.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ class nRF51822 : public BLEDevice

virtual ~nRF51822();

virtual void begin(unsigned char advertisementDataType,
unsigned char advertisementDataLength,
const unsigned char* advertisementData,
unsigned char scanDataType,
unsigned char scanDataLength,
const unsigned char* scanData,
virtual void begin(unsigned char advertisementDataSize,
BLEEirData *advertisementData,
unsigned char scanDataSize,
BLEEirData *scanData,
BLELocalAttribute** localAttributes,
unsigned char numLocalAttributes,
BLERemoteAttribute** remoteAttributes,
Expand Down
26 changes: 12 additions & 14 deletions src/nRF8001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,10 @@ nRF8001::~nRF8001() {
this->end();
}

void nRF8001::begin(unsigned char advertisementDataType,
unsigned char advertisementDataLength,
const unsigned char* advertisementData,
unsigned char scanDataType,
unsigned char scanDataLength,
const unsigned char* scanData,
void nRF8001::begin(unsigned char advertisementDataSize,
BLEEirData *advertisementData,
unsigned char scanDataSize,
BLEEirData *scanData,
BLELocalAttribute** localAttributes,
unsigned char numLocalAttributes,
BLERemoteAttribute** remoteAttributes,
Expand Down Expand Up @@ -261,8 +259,8 @@ void nRF8001::begin(unsigned char advertisementDataType,

setupMsg.status_byte = 0;

bool hasAdvertisementData = advertisementDataType && advertisementDataLength && advertisementData;
bool hasScanData = scanDataType && scanDataLength && scanData;
bool hasAdvertisementData = advertisementDataSize && advertisementData;
bool hasScanData = scanDataSize && scanData;

for (int i = 0; i < NB_BASE_SETUP_MESSAGES; i++) {
int setupMsgSize = pgm_read_byte_near(&baseSetupMsgs[i].buffer[0]) + 2;
Expand Down Expand Up @@ -305,13 +303,13 @@ void nRF8001::begin(unsigned char advertisementDataType,
setupMsgData->data[0] |= 0x01;
}
} else if (i == 5 && hasAdvertisementData) {
setupMsgData->data[0] = advertisementDataType;
setupMsgData->data[1] = advertisementDataLength;
memcpy(&setupMsgData->data[2], advertisementData, advertisementDataLength);
setupMsgData->data[0] = advertisementData[0].type;
setupMsgData->data[1] = advertisementData[0].length;
memcpy(&setupMsgData->data[2], advertisementData[0].data, advertisementData[0].length);
} else if (i == 6 && hasScanData) {
setupMsgData->data[0] = scanDataType;
setupMsgData->data[1] = scanDataLength;
memcpy(&setupMsgData->data[2], scanData, scanDataLength);
setupMsgData->data[0] = scanData[0].type;
setupMsgData->data[1] = scanData[0].length;
memcpy(&setupMsgData->data[2], scanData[0].data, scanData[0].length);
}

this->sendSetupMessage(&setupMsg);
Expand Down
10 changes: 4 additions & 6 deletions src/nRF8001.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ class nRF8001 : protected BLEDevice

virtual ~nRF8001();

virtual void begin(unsigned char advertisementDataType,
unsigned char advertisementDataLength,
const unsigned char* advertisementData,
unsigned char scanDataType,
unsigned char scanDataLength,
const unsigned char* scanData,
virtual void begin(unsigned char advertisementDataSize,
BLEEirData *advertisementData,
unsigned char scanDataSize,
BLEEirData *scanData,
BLELocalAttribute** localAttributes,
unsigned char numLocalAttributes,
BLERemoteAttribute** remoteAttributes,
Expand Down