Skip to content

Commit 88bf0ca

Browse files
committed
Change ownership of local advertising data from GAP to BLELocalDevice
1 parent 6a9e6ae commit 88bf0ca

File tree

7 files changed

+29
-64
lines changed

7 files changed

+29
-64
lines changed

src/BLEAdvertisingData.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ bool BLEAdvertisingData::updateData()
9696
return success;
9797
}
9898

99-
uint8_t* BLEAdvertisingData::getData()
99+
uint8_t* BLEAdvertisingData::data()
100100
{
101101
return _data;
102102
}
103103

104-
int BLEAdvertisingData::getDataLength()
104+
int BLEAdvertisingData::dataLength() const
105105
{
106106
return _dataLength;
107107
}

src/BLEAdvertisingData.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ class BLEAdvertisingData {
4848
void setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length);
4949

5050
bool updateData();
51-
52-
uint8_t* getData();
53-
int getDataLength();
51+
uint8_t* data();
52+
int dataLength() const;
5453

5554
private:
5655
bool addAdvertisedServiceUuid(const char* advertisedServiceUuid);

src/local/BLELocalCharacteristic.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
#include <Arduino.h>
21+
#include "BLELocalDevice.h"
2122

2223
#include "utility/ATT.h"
2324
#include "utility/GAP.h"
@@ -121,10 +122,10 @@ int BLELocalCharacteristic::writeValue(const uint8_t value[], int length)
121122
if (_broadcast) {
122123
uint16_t serviceUuid = GATT.serviceUuidForCharacteristic(this);
123124

124-
GAP.setAdvertisedServiceData(serviceUuid, value, length);
125+
BLE.setAdvertisedServiceData(serviceUuid, value, length);
125126

126127
if (!ATT.connected() && GAP.advertising()) {
127-
GAP.advertise();
128+
BLE.advertise();
128129
}
129130
}
130131

src/local/BLELocalDevice.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,27 +185,32 @@ int BLELocalDevice::rssi()
185185

186186
void BLELocalDevice::setAdvertisedServiceUuid(const char* advertisedServiceUuid)
187187
{
188-
GAP.setAdvertisedServiceUuid(advertisedServiceUuid);
188+
_advertisingData.setAdvertisedServiceUuid(advertisedServiceUuid);
189189
}
190190

191191
void BLELocalDevice::setAdvertisedService(const BLEService& service)
192192
{
193193
setAdvertisedServiceUuid(service.uuid());
194194
}
195195

196+
void BLELocalDevice::setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length)
197+
{
198+
_advertisingData.setAdvertisedServiceData(uuid, data, length);
199+
}
200+
196201
void BLELocalDevice::setManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength)
197202
{
198-
GAP.setManufacturerData(manufacturerData, manufacturerDataLength);
203+
_advertisingData.setManufacturerData(manufacturerData, manufacturerDataLength);
199204
}
200205

201206
void BLELocalDevice::setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength)
202207
{
203-
GAP.setManufacturerData(companyId, manufacturerData, manufacturerDataLength);
208+
_advertisingData.setManufacturerData(companyId, manufacturerData, manufacturerDataLength);
204209
}
205210

206211
void BLELocalDevice::setLocalName(const char *localName)
207212
{
208-
GAP.setLocalName(localName);
213+
_scanResponseData.setLocalName(localName);
209214
}
210215

211216
void BLELocalDevice::setDeviceName(const char* deviceName)
@@ -225,7 +230,10 @@ void BLELocalDevice::addService(BLEService& service)
225230

226231
int BLELocalDevice::advertise()
227232
{
228-
return GAP.advertise();
233+
_advertisingData.updateData();
234+
_scanResponseData.updateData();
235+
return GAP.advertise( _advertisingData.data(), _advertisingData.dataLength(),
236+
_scanResponseData.data(), _scanResponseData.dataLength());
229237
}
230238

231239
void BLELocalDevice::stopAdvertise()

src/local/BLELocalDevice.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "BLEDevice.h"
2424
#include "BLEService.h"
25+
#include "BLEAdvertisingData.h"
2526

2627
class BLELocalDevice {
2728
public:
@@ -43,6 +44,7 @@ class BLELocalDevice {
4344

4445
void setAdvertisedServiceUuid(const char* advertisedServiceUuid);
4546
void setAdvertisedService(const BLEService& service);
47+
void setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length);
4648
void setManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength);
4749
void setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength);
4850
void setLocalName(const char *localName);
@@ -76,6 +78,8 @@ class BLELocalDevice {
7678
void noDebug();
7779

7880
private:
81+
BLEAdvertisingData _advertisingData;
82+
BLEAdvertisingData _scanResponseData;
7983
};
8084

8185
extern BLELocalDevice BLE;

src/utility/GAP.cpp

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,61 +41,28 @@ GAPClass::~GAPClass()
4141
{
4242
}
4343

44-
void GAPClass::setAdvertisedServiceUuid(const char* advertisedServiceUuid)
45-
{
46-
_advertisingData.setAdvertisedServiceUuid(advertisedServiceUuid);
47-
}
48-
49-
void GAPClass::setManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength)
50-
{
51-
_advertisingData.setManufacturerData(manufacturerData, manufacturerDataLength);
52-
}
53-
54-
void GAPClass::setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength)
55-
{
56-
_advertisingData.setManufacturerData(companyId, manufacturerData, manufacturerDataLength);
57-
}
58-
59-
void GAPClass::setLocalName(const char *localName)
60-
{
61-
_scanResponseData.setLocalName(localName);
62-
}
63-
64-
void GAPClass::setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length)
65-
{
66-
_advertisingData.setAdvertisedServiceData(uuid, data, length);
67-
}
68-
6944
bool GAPClass::advertising()
7045
{
7146
return _advertising;
7247
}
7348

74-
int GAPClass::advertise()
49+
int GAPClass::advertise(uint8_t* advData, uint8_t advDataLen, uint8_t* scanData, uint8_t scanDataLen)
7550
{
7651
uint8_t directBdaddr[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
7752

78-
_advertisingData.updateData();
79-
_scanResponseData.updateData();
80-
81-
uint8_t scanLength = _scanResponseData.getDataLength();
82-
uint8_t type = (_connectable) ? GAP_ADV_IND : (scanLength ? GAP_ADV_SCAN_IND : GAP_ADV_NONCONN_IND);
53+
uint8_t type = (_connectable) ? GAP_ADV_IND : (scanDataLen ? GAP_ADV_SCAN_IND : GAP_ADV_NONCONN_IND);
8354

8455
stopAdvertise();
8556

8657
if (HCI.leSetAdvertisingParameters(_advertisingInterval, _advertisingInterval, type, 0x00, 0x00, directBdaddr, 0x07, 0) != 0) {
8758
return 0;
8859
}
8960

90-
uint8_t* advertisingData = _advertisingData.getData();
91-
uint8_t advertisingDataLen = _advertisingData.getDataLength();
92-
if (HCI.leSetAdvertisingData(advertisingDataLen, advertisingData) != 0) {
61+
if (HCI.leSetAdvertisingData(advDataLen, advData) != 0) {
9362
return 0;
9463
}
9564

96-
uint8_t* scanResponseData = _scanResponseData.getData();
97-
uint8_t scanResponseDataLen = _scanResponseData.getDataLength();
98-
if (HCI.leSetScanResponseData(scanResponseDataLen, scanResponseData) != 0) {
65+
if (HCI.leSetScanResponseData(scanDataLen, scanData) != 0) {
9966
return 0;
10067
}
10168

src/utility/GAP.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,15 @@
2222

2323
#include "utility/BLELinkedList.h"
2424

25-
#include "BLEAdvertisingData.h"
2625
#include "BLEDevice.h"
2726

2827
class GAPClass {
2928
public:
3029
GAPClass();
3130
virtual ~GAPClass();
3231

33-
void setAdvertisedServiceUuid(const char* advertisedServiceUuid);
34-
void setManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength);
35-
void setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength);
36-
void setLocalName(const char *localName);
37-
3832
bool advertising();
39-
int advertise();
33+
int advertise(uint8_t* advData, uint8_t advDataLength, uint8_t* scanData, uint8_t scanDataLength);
4034
void stopAdvertise();
4135

4236
int scan(bool withDuplicates);
@@ -51,11 +45,6 @@ class GAPClass {
5145

5246
void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler);
5347

54-
protected:
55-
friend class BLELocalCharacteristic;
56-
57-
void setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length);
58-
5948
protected:
6049
friend class HCIClass;
6150

@@ -78,9 +67,6 @@ class GAPClass {
7867
String _scanNameFilter;
7968
String _scanUuidFilter;
8069
String _scanAddressFilter;
81-
82-
BLEAdvertisingData _advertisingData;
83-
BLEAdvertisingData _scanResponseData;
8470
};
8571

8672
extern GAPClass GAP;

0 commit comments

Comments
 (0)