Skip to content

Commit da21712

Browse files
committed
Merge pull request #89 from asharov/master
Allow advertisement to contain multiple types of data
2 parents 8944955 + 5dd2e6b commit da21712

File tree

7 files changed

+94
-77
lines changed

7 files changed

+94
-77
lines changed

src/BLEDevice.h

+11-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
#include "BLERemoteCharacteristic.h"
1212
#include "BLERemoteService.h"
1313

14+
struct BLEEirData
15+
{
16+
unsigned char length;
17+
unsigned char type;
18+
unsigned char data[BLE_EIR_DATA_MAX_VALUE_LENGTH];
19+
};
20+
1421
class BLEDevice;
1522

1623
class BLEDeviceEventListener
@@ -49,12 +56,10 @@ class BLEDevice
4956
void setConnectable(bool connectable);
5057
void setBondStore(BLEBondStore& bondStore);
5158

52-
virtual void begin(unsigned char /*advertisementDataType*/,
53-
unsigned char /*advertisementDataLength*/,
54-
const unsigned char* /*advertisementData*/,
55-
unsigned char /*scanDataType*/,
56-
unsigned char /*scanDataLength*/,
57-
const unsigned char* /*scanData*/,
59+
virtual void begin(unsigned char /*advertisementDataSize*/,
60+
BLEEirData * /*advertisementData*/,
61+
unsigned char /*scanDataSize*/,
62+
BLEEirData * /*scanData*/,
5863
BLELocalAttribute** /*localAttributes*/,
5964
unsigned char /*numLocalAttributes*/,
6065
BLERemoteAttribute** /*remoteAttributes*/,

src/BLEDeviceLimits.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
#define BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH 26
2424
#define BLE_SCAN_DATA_MAX_VALUE_LENGTH 29
25+
#define BLE_EIR_DATA_MAX_VALUE_LENGTH 29
2526
#define BLE_ATTRIBUTE_MAX_VALUE_LENGTH 20
2627
#define BLE_REMOTE_ATTRIBUTE_MAX_VALUE_LENGTH 22
2728

2829
#else
2930

3031
#define BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH 20
3132
#define BLE_SCAN_DATA_MAX_VALUE_LENGTH 20
33+
#define BLE_EIR_DATA_MAX_VALUE_LENGTH 20
3234
#define BLE_ATTRIBUTE_MAX_VALUE_LENGTH 20
3335
#define BLE_REMOTE_ATTRIBUTE_MAX_VALUE_LENGTH 22
3436

src/BLEPeripheral.cpp

+40-28
Original file line numberDiff line numberDiff line change
@@ -69,52 +69,64 @@ BLEPeripheral::~BLEPeripheral() {
6969
}
7070

7171
void BLEPeripheral::begin() {
72-
unsigned char advertisementDataType = 0;
73-
unsigned char scanDataType = 0;
72+
unsigned char advertisementDataSize = 0;
7473

75-
unsigned char advertisementDataLength = 0;
76-
unsigned char scanDataLength = 0;
77-
78-
unsigned char advertisementData[BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH];
79-
unsigned char scanData[BLE_SCAN_DATA_MAX_VALUE_LENGTH];
74+
BLEEirData advertisementData[3];
75+
BLEEirData scanData = { 0 };
8076

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

84-
advertisementDataLength = serviceSolicitationUuid.length();
85-
advertisementDataType = (advertisementDataLength > 2) ? 0x15 : 0x14;
81+
unsigned char uuidLength = serviceSolicitationUuid.length();
82+
advertisementData[advertisementDataSize].length = uuidLength;
83+
advertisementData[advertisementDataSize].type = (uuidLength > 2) ? 0x15 : 0x14;
8684

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

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

98-
if (advertisementDataLength > sizeof(advertisementData)) {
99-
advertisementDataLength = sizeof(advertisementData);
97+
memcpy(advertisementData[advertisementDataSize].data, advertisedServiceUuid.data(), uuidLength);
98+
advertisementDataSize += 1;
99+
remainingAdvertisementDataLength -= uuidLength + 2;
100100
}
101+
}
102+
if (this->_manufacturerData && this->_manufacturerDataLength > 0) {
103+
if (remainingAdvertisementDataLength >= 3) {
104+
unsigned char dataLength = this->_manufacturerDataLength;
105+
106+
if (dataLength + 2 > remainingAdvertisementDataLength) {
107+
dataLength = remainingAdvertisementDataLength - 2;
108+
}
101109

102-
advertisementDataType = 0xff;
110+
advertisementData[advertisementDataSize].length = dataLength;
111+
advertisementData[advertisementDataSize].type = 0xff;
103112

104-
memcpy(advertisementData, this->_manufacturerData, advertisementDataLength);
113+
memcpy(advertisementData[advertisementDataSize].data, this->_manufacturerData, dataLength);
114+
advertisementDataSize += 1;
115+
remainingAdvertisementDataLength -= dataLength + 2;
116+
}
105117
}
106118

107119
if (this->_localName){
108120
unsigned char localNameLength = strlen(this->_localName);
109-
scanDataLength = localNameLength;
121+
scanData.length = localNameLength;
110122

111-
if (scanDataLength > sizeof(scanData)) {
112-
scanDataLength = sizeof(scanData);
123+
if (scanData.length > BLE_SCAN_DATA_MAX_VALUE_LENGTH) {
124+
scanData.length = BLE_SCAN_DATA_MAX_VALUE_LENGTH;
113125
}
114126

115-
scanDataType = (localNameLength > scanDataLength) ? 0x08 : 0x09;
127+
scanData.type = (localNameLength > scanData.length) ? 0x08 : 0x09;
116128

117-
memcpy(scanData, this->_localName, scanDataLength);
129+
memcpy(scanData.data, this->_localName, scanData.length);
118130
}
119131

120132
if (this->_localAttributes == NULL) {
@@ -144,8 +156,8 @@ void BLEPeripheral::begin() {
144156
this->addRemoteAttribute(this->_remoteServicesChangedCharacteristic);
145157
}
146158

147-
this->_device->begin(advertisementDataType, advertisementDataLength, advertisementData,
148-
scanDataType, scanDataLength, scanData,
159+
this->_device->begin(advertisementDataSize, advertisementData,
160+
scanData.length > 0 ? 1 : 0, &scanData,
149161
this->_localAttributes, this->_numLocalAttributes,
150162
this->_remoteAttributes, this->_numRemoteAttributes);
151163

src/nRF51822.cpp

+21-17
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,10 @@ nRF51822::~nRF51822() {
7676
this->end();
7777
}
7878

79-
void nRF51822::begin(unsigned char advertisementDataType,
80-
unsigned char advertisementDataLength,
81-
const unsigned char* advertisementData,
82-
unsigned char scanDataType,
83-
unsigned char scanDataLength,
84-
const unsigned char* scanData,
79+
void nRF51822::begin(unsigned char advertisementDataSize,
80+
BLEEirData *advertisementData,
81+
unsigned char scanDataSize,
82+
BLEEirData *scanData,
8583
BLELocalAttribute** localAttributes,
8684
unsigned char numLocalAttributes,
8785
BLERemoteAttribute** remoteAttributes,
@@ -196,22 +194,28 @@ void nRF51822::begin(unsigned char advertisementDataType,
196194

197195
this->_advDataLen += 3;
198196

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

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

206-
this->_advDataLen += advertisementDataLength;
205+
this->_advDataLen += advertisementData[i].length;
206+
}
207207
}
208208

209-
if (scanDataType && scanDataLength && scanData) {
210-
srData[0] = scanDataLength + 1;
211-
srData[1] = scanDataType;
212-
memcpy(&srData[2], scanData, scanDataLength);
209+
if (scanDataSize && scanData) {
210+
for (int i = 0; i < scanDataSize; i++) {
211+
srData[srDataLen + 0] = scanData[i].length + 1;
212+
srData[srDataLen + 1] = scanData[i].type;
213+
srDataLen += 2;
214+
215+
memcpy(&srData[srDataLen], scanData[i].data, scanData[i].length);
213216

214-
srDataLen = 2 + scanDataLength;
217+
srDataLen += scanData[i].length;
218+
}
215219
}
216220

217221
sd_ble_gap_adv_data_set(this->_advData, this->_advDataLen, srData, srDataLen);

src/nRF51822.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,10 @@ class nRF51822 : public BLEDevice
5252

5353
virtual ~nRF51822();
5454

55-
virtual void begin(unsigned char advertisementDataType,
56-
unsigned char advertisementDataLength,
57-
const unsigned char* advertisementData,
58-
unsigned char scanDataType,
59-
unsigned char scanDataLength,
60-
const unsigned char* scanData,
55+
virtual void begin(unsigned char advertisementDataSize,
56+
BLEEirData *advertisementData,
57+
unsigned char scanDataSize,
58+
BLEEirData *scanData,
6159
BLELocalAttribute** localAttributes,
6260
unsigned char numLocalAttributes,
6361
BLERemoteAttribute** remoteAttributes,

src/nRF8001.cpp

+12-14
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,10 @@ nRF8001::~nRF8001() {
143143
this->end();
144144
}
145145

146-
void nRF8001::begin(unsigned char advertisementDataType,
147-
unsigned char advertisementDataLength,
148-
const unsigned char* advertisementData,
149-
unsigned char scanDataType,
150-
unsigned char scanDataLength,
151-
const unsigned char* scanData,
146+
void nRF8001::begin(unsigned char advertisementDataSize,
147+
BLEEirData *advertisementData,
148+
unsigned char scanDataSize,
149+
BLEEirData *scanData,
152150
BLELocalAttribute** localAttributes,
153151
unsigned char numLocalAttributes,
154152
BLERemoteAttribute** remoteAttributes,
@@ -261,8 +259,8 @@ void nRF8001::begin(unsigned char advertisementDataType,
261259

262260
setupMsg.status_byte = 0;
263261

264-
bool hasAdvertisementData = advertisementDataType && advertisementDataLength && advertisementData;
265-
bool hasScanData = scanDataType && scanDataLength && scanData;
262+
bool hasAdvertisementData = advertisementDataSize && advertisementData;
263+
bool hasScanData = scanDataSize && scanData;
266264

267265
for (int i = 0; i < NB_BASE_SETUP_MESSAGES; i++) {
268266
int setupMsgSize = pgm_read_byte_near(&baseSetupMsgs[i].buffer[0]) + 2;
@@ -305,13 +303,13 @@ void nRF8001::begin(unsigned char advertisementDataType,
305303
setupMsgData->data[0] |= 0x01;
306304
}
307305
} else if (i == 5 && hasAdvertisementData) {
308-
setupMsgData->data[0] = advertisementDataType;
309-
setupMsgData->data[1] = advertisementDataLength;
310-
memcpy(&setupMsgData->data[2], advertisementData, advertisementDataLength);
306+
setupMsgData->data[0] = advertisementData[0].type;
307+
setupMsgData->data[1] = advertisementData[0].length;
308+
memcpy(&setupMsgData->data[2], advertisementData[0].data, advertisementData[0].length);
311309
} else if (i == 6 && hasScanData) {
312-
setupMsgData->data[0] = scanDataType;
313-
setupMsgData->data[1] = scanDataLength;
314-
memcpy(&setupMsgData->data[2], scanData, scanDataLength);
310+
setupMsgData->data[0] = scanData[0].type;
311+
setupMsgData->data[1] = scanData[0].length;
312+
memcpy(&setupMsgData->data[2], scanData[0].data, scanData[0].length);
315313
}
316314

317315
this->sendSetupMessage(&setupMsg);

src/nRF8001.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ class nRF8001 : protected BLEDevice
4646

4747
virtual ~nRF8001();
4848

49-
virtual void begin(unsigned char advertisementDataType,
50-
unsigned char advertisementDataLength,
51-
const unsigned char* advertisementData,
52-
unsigned char scanDataType,
53-
unsigned char scanDataLength,
54-
const unsigned char* scanData,
49+
virtual void begin(unsigned char advertisementDataSize,
50+
BLEEirData *advertisementData,
51+
unsigned char scanDataSize,
52+
BLEEirData *scanData,
5553
BLELocalAttribute** localAttributes,
5654
unsigned char numLocalAttributes,
5755
BLERemoteAttribute** remoteAttributes,

0 commit comments

Comments
 (0)