FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
STM32duinoBLE/src/BLEAdvertisingData.cpp at main · stm32duino/STM32duinoBLE · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
stm32duino
/
STM32duinoBLE
Public
forked from
arduino-libraries/ArduinoBLE
Notifications
You must be signed in to change notification settings
Fork
23
Star
21
Code
Issues
3
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
STM32duinoBLE
/
src
/
BLEAdvertisingData.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
352 lines (317 loc) · 10.7 KB
Breadcrumbs
STM32duinoBLE
/
src
/
BLEAdvertisingData.cpp
Copy path
File metadata and controls
352 lines (317 loc) · 10.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
This file is part of the ArduinoBLE library.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#
include
"
BLEAdvertisingData.h
"
#
define
AD_FIELD_OVERHEAD
(
2
)
BLEAdvertisingData::BLEAdvertisingData
() :
_dataLength(
0
),
_remainingLength(
MAX_AD_DATA_LENGTH
),
_rawData(
NULL
),
_rawDataLength(
0
),
_flags(
0
),
_hasFlags(
false
),
_localName(
NULL
),
_manufacturerData(
NULL
),
_manufacturerDataLength(
0
),
_manufacturerCompanyId(
0
),
_hasManufacturerCompanyId(
false
),
_advertisedServiceUuid(
NULL
),
_advertisedServiceUuidLength(
0
),
_serviceData(
NULL
),
_serviceDataLength(
0
)
{
}
BLEAdvertisingData::~BLEAdvertisingData
()
{
}
inline
bool
BLEAdvertisingData::updateRemainingLength
(
int
oldFieldLength,
int
newFieldLength)
{
int
updatedRemaining = _remainingLength + (oldFieldLength - newFieldLength);
if
(updatedRemaining >=
0
) {
_remainingLength = updatedRemaining;
return
true
;
}
return
false
;
}
int
BLEAdvertisingData::remainingLength
()
const
{
return
_remainingLength;
}
int
BLEAdvertisingData::availableForWrite
()
{
int
available = (_remainingLength -
AD_FIELD_OVERHEAD
);
if
(available <
0
) available =
0
;
return
available;
}
void
BLEAdvertisingData::clear
()
{
_remainingLength =
MAX_AD_DATA_LENGTH
;
_rawData =
NULL
;
_rawDataLength =
0
;
_hasFlags =
false
;
_localName =
NULL
;
_manufacturerData =
NULL
;
_manufacturerDataLength =
0
;
_hasManufacturerCompanyId =
false
;
_advertisedServiceUuid =
NULL
;
_advertisedServiceUuidLength =
0
;
_serviceData =
NULL
;
_serviceDataLength =
0
;
}
void
BLEAdvertisingData::copy
(
const
BLEAdvertisingData& adv)
{
_remainingLength = adv.
_remainingLength
;
_rawData = adv.
_rawData
;
_rawDataLength = adv.
_rawDataLength
;
_flags = adv.
_flags
;
_hasFlags = adv.
_hasFlags
;
_localName = adv.
_localName
;
_manufacturerData = adv.
_manufacturerData
;
_manufacturerDataLength = adv.
_manufacturerDataLength
;
_manufacturerCompanyId = adv.
_manufacturerCompanyId
;
_hasManufacturerCompanyId = adv.
_hasManufacturerCompanyId
;
_advertisedServiceUuid = adv.
_advertisedServiceUuid
;
_advertisedServiceUuidLength = adv.
_advertisedServiceUuidLength
;
_serviceDataUuid = adv.
_serviceDataUuid
;
_serviceData = adv.
_serviceData
;
_serviceDataLength = adv.
_serviceDataLength
;
}
BLEAdvertisingData& BLEAdvertisingData::
operator
=(
const
BLEAdvertisingData &other)
{
copy
(other);
return
*
this
;
}
bool
BLEAdvertisingData::setAdvertisedServiceUuid
(
const
char
* advertisedServiceUuid)
{
BLEUuid
uuid
(advertisedServiceUuid);
int
previousLength = (_advertisedServiceUuidLength >
0
) ? (_advertisedServiceUuidLength +
AD_FIELD_OVERHEAD
) :
0
;
bool
success =
updateRemainingLength
(previousLength, (uuid.
length
() +
AD_FIELD_OVERHEAD
));
if
(success) {
_advertisedServiceUuid = advertisedServiceUuid;
_advertisedServiceUuidLength = uuid.
length
();
}
return
success;
}
bool
BLEAdvertisingData::setAdvertisedService
(
const
BLEService& service)
{
return
setAdvertisedServiceUuid
(service.
uuid
());
}
bool
BLEAdvertisingData::setManufacturerData
(
const
uint8_t
manufacturerData[],
int
manufacturerDataLength)
{
int
previousLength =
0
;
if
(_manufacturerDataLength) {
previousLength = _manufacturerDataLength +
AD_FIELD_OVERHEAD
;
if
(_hasManufacturerCompanyId) {
previousLength +=
sizeof
(_manufacturerCompanyId);
}
}
bool
success =
updateRemainingLength
(previousLength, (manufacturerDataLength +
AD_FIELD_OVERHEAD
));
if
(success) {
_manufacturerData = manufacturerData;
_manufacturerDataLength = manufacturerDataLength;
_hasManufacturerCompanyId =
false
;
}
return
success;
}
bool
BLEAdvertisingData::setManufacturerData
(
const
uint16_t
companyId,
const
uint8_t
manufacturerData[],
int
manufacturerDataLength)
{
int
previousLength =
0
;
if
(_manufacturerDataLength) {
previousLength = _manufacturerDataLength +
AD_FIELD_OVERHEAD
;
if
(_hasManufacturerCompanyId) {
previousLength +=
sizeof
(_manufacturerCompanyId);
}
}
bool
success =
updateRemainingLength
(previousLength, (manufacturerDataLength +
sizeof
(companyId) +
AD_FIELD_OVERHEAD
));
if
(success) {
_manufacturerData = manufacturerData;
_manufacturerDataLength = manufacturerDataLength;
_manufacturerCompanyId = companyId;
_hasManufacturerCompanyId =
true
;
}
return
success;
}
bool
BLEAdvertisingData::setAdvertisedServiceData
(
uint16_t
uuid,
const
uint8_t
data[],
int
length)
{
int
previousLength = (_serviceDataLength >
0
) ? (_serviceDataLength +
sizeof
(uuid) +
AD_FIELD_OVERHEAD
) :
0
;
bool
success =
updateRemainingLength
(previousLength, (length +
sizeof
(uuid) +
AD_FIELD_OVERHEAD
));
if
(success) {
_serviceDataUuid = uuid;
_serviceData = data;
_serviceDataLength = length;
}
return
success;
}
bool
BLEAdvertisingData::setLocalName
(
const
char
*localName)
{
int
previousLength = (_localName &&
strlen
(_localName) >
0
) ? (
strlen
(_localName) +
AD_FIELD_OVERHEAD
) :
0
;
bool
success =
updateRemainingLength
(previousLength, (
strlen
(localName) +
AD_FIELD_OVERHEAD
));
if
(success) {
_localName = localName;
}
return
success;
}
bool
BLEAdvertisingData::setRawData
(
const
uint8_t
* data,
int
length)
{
if
(length >
MAX_AD_DATA_LENGTH
) {
return
false
;
}
_rawData = data;
_rawDataLength = length;
return
true
;
}
bool
BLEAdvertisingData::setRawData
(
const
BLEAdvertisingRawData& rawData)
{
if
(rawData.
length
>
MAX_AD_DATA_LENGTH
) {
return
false
;
}
_rawData = rawData.
data
;
_rawDataLength = rawData.
length
;
return
true
;
}
bool
BLEAdvertisingData::setFlags
(
uint8_t
flags)
{
int
previousLength = (_hasFlags) ? (
sizeof
(_flags) +
AD_FIELD_OVERHEAD
) :
0
;
bool
success =
updateRemainingLength
(previousLength, (
sizeof
(flags) +
AD_FIELD_OVERHEAD
));
if
(success) {
_hasFlags =
true
;
_flags = flags;
}
return
success;
}
bool
BLEAdvertisingData::updateData
()
{
//
Success indicates whether all the fields have been inserted
bool
success =
true
;
//
Reset data
_dataLength =
0
;
//
If rawData is present, then only rawData is inserted in the advertising packet
if
(_rawData && _rawDataLength) {
return
addRawData
(_rawData, _rawDataLength);
}
//
Try to add flags into the current advertising packet
if
(_hasFlags) {
success &=
addFlags
(_flags);
}
//
Try to add Advertised service uuid into the current advertising packet
if
(_advertisedServiceUuid) {
success &=
addAdvertisedServiceUuid
(_advertisedServiceUuid);
}
//
Try to add Manufacturer data into the current advertising packet
if
(_manufacturerData && _manufacturerDataLength) {
if
(_hasManufacturerCompanyId) {
success &=
addManufacturerData
(_manufacturerCompanyId, _manufacturerData, _manufacturerDataLength);
}
else
{
success &=
addManufacturerData
(_manufacturerData, _manufacturerDataLength);
}
}
//
Try to add Service data into the current advertising packet
if
(_serviceData && _serviceDataLength) {
success &=
addAdvertisedServiceData
(_serviceDataUuid, _serviceData, _serviceDataLength);
}
//
Try to add Local name into the current advertising packet
if
(_localName) {
success &=
addLocalName
(_localName);
}
return
success;
}
uint8_t
*
BLEAdvertisingData::data
()
{
return
_data;
}
int
BLEAdvertisingData::dataLength
()
const
{
return
_dataLength;
}
bool
BLEAdvertisingData::hasFlags
()
const
{
return
_hasFlags;
}
bool
BLEAdvertisingData::addLocalName
(
const
char
*localName)
{
bool
success =
false
;
if
(
strlen
(localName) > (
MAX_AD_DATA_LENGTH
-
AD_FIELD_OVERHEAD
)) {
success =
addField
(BLEFieldShortLocalName, (
uint8_t
*)localName, (
MAX_AD_DATA_LENGTH
-
AD_FIELD_OVERHEAD
));
}
else
{
success =
addField
(BLEFieldCompleteLocalName, localName);
}
return
success;
}
bool
BLEAdvertisingData::addAdvertisedServiceUuid
(
const
char
* advertisedServiceUuid)
{
BLEUuid
uuid
(advertisedServiceUuid);
int
uuidLen = uuid.
length
();
BLEAdField advField;
if
(uuidLen >
2
) {
advField = BLEFieldIncompleteAdvertisedService128;
}
else
{
advField = BLEFieldIncompleteAdvertisedService16;
}
return
addField
(advField, uuid.
data
(), uuidLen);
}
bool
BLEAdvertisingData::addManufacturerData
(
const
uint8_t
manufacturerData[],
int
manufacturerDataLength)
{
return
addField
(BLEFieldManufacturerData, manufacturerData, manufacturerDataLength);
}
bool
BLEAdvertisingData::addManufacturerData
(
const
uint16_t
companyId,
const
uint8_t
manufacturerData[],
int
manufacturerDataLength)
{
int
tempDataLength = manufacturerDataLength +
sizeof
(companyId);
uint8_t
tempData[
MAX_AD_DATA_LENGTH
];
memcpy
(tempData, &companyId,
sizeof
(companyId));
memcpy
(&tempData[
sizeof
(companyId)], manufacturerData, manufacturerDataLength);
return
addField
(BLEFieldManufacturerData, tempData, tempDataLength);
}
bool
BLEAdvertisingData::addAdvertisedServiceData
(
uint16_t
uuid,
const
uint8_t
data[],
int
length)
{
int
tempDataLength = length +
sizeof
(uuid);
uint8_t
tempData[
MAX_AD_DATA_LENGTH
];
memcpy
(tempData, &uuid,
sizeof
(uuid));
memcpy
(&tempData[
sizeof
(uuid)], data, length);
return
addField
(BLEFieldServiceData, tempData, tempDataLength);
}
bool
BLEAdvertisingData::addRawData
(
const
uint8_t
* data,
int
length)
{
//
Bypass addField to add the integral raw data
if
(length > (
MAX_AD_DATA_LENGTH
- _dataLength)) {
//
Not enough space
return
false
;
}
memcpy
(&_data[_dataLength], data, length);
_dataLength += length;
return
true
;
}
bool
BLEAdvertisingData::addFlags
(
uint8_t
flags)
{
return
addField
(BLEFieldFlags, &flags,
sizeof
(flags));
}
bool
BLEAdvertisingData::addField
(BLEAdField field,
const
char
* data)
{
int
dataLength =
strlen
(data);
return
addField
(field, (
uint8_t
*)data, dataLength);
}
bool
BLEAdvertisingData::addField
(BLEAdField field,
const
uint8_t
* data,
int
length)
{
int
fieldLength = length +
AD_FIELD_OVERHEAD
;
//
Considering data TYPE and LENGTH fields
if
(fieldLength > (
MAX_AD_DATA_LENGTH
- _dataLength)) {
//
Not enough space for storing this field
return
false
;
}
//
Insert field into advertising data of the instance
_data[_dataLength++] = length +
1
;
_data[_dataLength++] = field;
memcpy
(&_data[_dataLength], data, length);
_dataLength += length;
return
true
;
}
Back
|
FazBrowse Home
|
New Git URL