19
19
20
20
#include " BLEAdvertisingData.h"
21
21
22
+ #define AD_FIELD_OVERHEAD (2 )
23
+
22
24
BLEAdvertisingData::BLEAdvertisingData () :
23
25
_dataLength(0 ),
24
26
_rawDataLength(0 ),
@@ -31,56 +33,112 @@ BLEAdvertisingData::BLEAdvertisingData() :
31
33
_serviceData(NULL ),
32
34
_serviceDataLength(0 ),
33
35
_flags(0 ),
34
- _hasFlags(false )
36
+ _hasFlags(false ),
37
+ _remainingLength(MAX_AD_DATA_LENGTH)
35
38
{
36
39
}
37
40
38
41
BLEAdvertisingData::~BLEAdvertisingData ()
39
42
{
40
43
}
41
44
42
- void BLEAdvertisingData::setAdvertisedServiceUuid ( const char * advertisedServiceUuid )
45
+ inline bool BLEAdvertisingData::updateRemainingLength ( int fieldLength )
43
46
{
44
- _advertisedServiceUuid = advertisedServiceUuid;
47
+ if (fieldLength <= _remainingLength) {
48
+ _remainingLength -= fieldLength;
49
+ return true ;
50
+ }
51
+ return false ;
45
52
}
46
53
47
- void BLEAdvertisingData::setManufacturerData ( const uint8_t manufacturerData[], int manufacturerDataLength)
54
+ int BLEAdvertisingData::remainingLength () const
48
55
{
49
- _manufacturerData = manufacturerData;
50
- _manufacturerDataLength = manufacturerDataLength;
51
- _hasManufacturerCompanyId = false ;
56
+ return _remainingLength;
52
57
}
53
58
54
- void BLEAdvertisingData::setManufacturerData ( const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength )
59
+ int BLEAdvertisingData::availableForWrite ( )
55
60
{
56
- _manufacturerData = manufacturerData;
57
- _manufacturerDataLength = manufacturerDataLength;
58
- _manufacturerCompanyId = companyId;
59
- _hasManufacturerCompanyId = true ;
61
+ int available = (_remainingLength - AD_FIELD_OVERHEAD);
62
+ if (available < 0 ) available = 0 ;
63
+ return available;
60
64
}
61
65
62
- void BLEAdvertisingData::setAdvertisedServiceData ( uint16_t uuid, const uint8_t data[], int length )
66
+ bool BLEAdvertisingData::setAdvertisedServiceUuid ( const char * advertisedServiceUuid )
63
67
{
64
- _serviceDataUuid = uuid;
65
- _serviceData = data;
66
- _serviceDataLength = length;
68
+ BLEUuid uuid (advertisedServiceUuid);
69
+ bool success = updateRemainingLength (uuid.length () + AD_FIELD_OVERHEAD);
70
+ if (success) {
71
+ _advertisedServiceUuid = advertisedServiceUuid;
72
+ }
73
+ return success;
67
74
}
68
75
69
- void BLEAdvertisingData::setLocalName (const char *localName )
76
+ bool BLEAdvertisingData::setAdvertisedService (const BLEService& service )
70
77
{
71
- _localName = localName ;
78
+ return setAdvertisedServiceUuid (service. uuid ()) ;
72
79
}
73
80
74
- void BLEAdvertisingData::setRawData (const uint8_t * data, uint8_t length )
81
+ bool BLEAdvertisingData::setManufacturerData (const uint8_t manufacturerData[], int manufacturerDataLength )
75
82
{
76
- _rawData = data;
77
- _rawDataLength = length;
83
+ bool success = updateRemainingLength (manufacturerDataLength + AD_FIELD_OVERHEAD);
84
+ if (success) {
85
+ _manufacturerData = manufacturerData;
86
+ _manufacturerDataLength = manufacturerDataLength;
87
+ _hasManufacturerCompanyId = false ;
88
+ }
89
+ return success;
78
90
}
79
91
80
- void BLEAdvertisingData::setFlags ( uint8_t flags )
92
+ bool BLEAdvertisingData::setManufacturerData ( const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength )
81
93
{
82
- _hasFlags = true ;
83
- _flags = flags;
94
+ bool success = updateRemainingLength (manufacturerDataLength + sizeof (companyId) + AD_FIELD_OVERHEAD);
95
+ if (success) {
96
+ _manufacturerData = manufacturerData;
97
+ _manufacturerDataLength = manufacturerDataLength;
98
+ _manufacturerCompanyId = companyId;
99
+ _hasManufacturerCompanyId = true ;
100
+ }
101
+ return success;
102
+ }
103
+
104
+ bool BLEAdvertisingData::setAdvertisedServiceData (uint16_t uuid, const uint8_t data[], int length)
105
+ {
106
+ bool success = updateRemainingLength (length + sizeof (uuid) + AD_FIELD_OVERHEAD);
107
+ if (success) {
108
+ _serviceDataUuid = uuid;
109
+ _serviceData = data;
110
+ _serviceDataLength = length;
111
+ }
112
+ return success;
113
+ }
114
+
115
+ bool BLEAdvertisingData::setLocalName (const char *localName)
116
+ {
117
+ bool success = updateRemainingLength (strlen (localName) + AD_FIELD_OVERHEAD);
118
+ if (success) {
119
+ _localName = localName;
120
+ }
121
+ return success;
122
+ }
123
+
124
+ bool BLEAdvertisingData::setRawData (const uint8_t * data, int length)
125
+ {
126
+ bool success = updateRemainingLength (length);
127
+ if (success) {
128
+ _rawData = data;
129
+ _rawDataLength = length;
130
+ }
131
+ return success;
132
+ }
133
+
134
+ bool BLEAdvertisingData::setFlags (uint8_t flags)
135
+ {
136
+ bool success = updateRemainingLength (sizeof (flags) + AD_FIELD_OVERHEAD);
137
+ if (success) {
138
+ _hasFlags = true ;
139
+ _flags = flags;
140
+ }
141
+ return success;
84
142
}
85
143
86
144
bool BLEAdvertisingData::updateData ()
@@ -133,8 +191,8 @@ int BLEAdvertisingData::dataLength() const
133
191
bool BLEAdvertisingData::addLocalName (const char *localName)
134
192
{
135
193
bool success = false ;
136
- if (strlen (localName) > (MAX_AD_DATA_LENGTH - 2 )) {
137
- success = addField (BLEFieldShortLocalName, (uint8_t *)localName, (MAX_AD_DATA_LENGTH - 2 ));
194
+ if (strlen (localName) > (MAX_AD_DATA_LENGTH - AD_FIELD_OVERHEAD )) {
195
+ success = addField (BLEFieldShortLocalName, (uint8_t *)localName, (MAX_AD_DATA_LENGTH - AD_FIELD_OVERHEAD ));
138
196
} else {
139
197
success = addField (BLEFieldCompleteLocalName, localName);
140
198
}
@@ -161,7 +219,7 @@ bool BLEAdvertisingData::addManufacturerData(const uint8_t manufacturerData[], i
161
219
162
220
bool BLEAdvertisingData::addManufacturerData (const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength)
163
221
{
164
- uint8_t tempDataLength = manufacturerDataLength + sizeof (companyId);
222
+ int tempDataLength = manufacturerDataLength + sizeof (companyId);
165
223
uint8_t tempData[MAX_AD_DATA_LENGTH];
166
224
memcpy (tempData, &companyId, sizeof (companyId));
167
225
memcpy (&tempData[sizeof (companyId)], manufacturerData, manufacturerDataLength);
@@ -170,14 +228,14 @@ bool BLEAdvertisingData::addManufacturerData(const uint16_t companyId, const uin
170
228
171
229
bool BLEAdvertisingData::addAdvertisedServiceData (uint16_t uuid, const uint8_t data[], int length)
172
230
{
173
- uint8_t tempDataLength = length + sizeof (uuid);
231
+ int tempDataLength = length + sizeof (uuid);
174
232
uint8_t tempData[MAX_AD_DATA_LENGTH];
175
233
memcpy (tempData, &uuid, sizeof (uuid));
176
234
memcpy (&tempData[sizeof (uuid)], data, length);
177
235
return addField (BLEFieldServiceData, tempData, tempDataLength);
178
236
}
179
237
180
- bool BLEAdvertisingData::addRawData (const uint8_t * data, uint8_t length)
238
+ bool BLEAdvertisingData::addRawData (const uint8_t * data, int length)
181
239
{
182
240
// Bypass addField to add the integral raw data
183
241
if (length > (MAX_AD_DATA_LENGTH - _dataLength)) {
@@ -191,18 +249,18 @@ bool BLEAdvertisingData::addRawData(const uint8_t* data, uint8_t length)
191
249
192
250
bool BLEAdvertisingData::addFlags (uint8_t flags)
193
251
{
194
- return addField (BLEFieldFlags, &flags, 1 );
252
+ return addField (BLEFieldFlags, &flags, sizeof (flags) );
195
253
}
196
254
197
255
bool BLEAdvertisingData::addField (BLEAdField field, const char * data)
198
256
{
199
- uint8_t dataLength = strlen (data);
257
+ int dataLength = strlen (data);
200
258
return addField (field, (uint8_t *)data, dataLength);
201
259
}
202
260
203
- bool BLEAdvertisingData::addField (BLEAdField field, const uint8_t * data, uint8_t length)
261
+ bool BLEAdvertisingData::addField (BLEAdField field, const uint8_t * data, int length)
204
262
{
205
- uint8_t fieldLength = length + 2 ; // Considering data TYPE and LENGTH fields
263
+ int fieldLength = length + AD_FIELD_OVERHEAD ; // Considering data TYPE and LENGTH fields
206
264
if (fieldLength > (MAX_AD_DATA_LENGTH - _dataLength)) {
207
265
// Not enough space for storing this field
208
266
return false ;
0 commit comments