Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Advertisement.hxx
Go to the documentation of this file.
1
28#ifndef _BLE_ADVERTISEMENT_HXX_
29#define _BLE_ADVERTISEMENT_HXX_
30
31#include "ble/Defs.hxx"
32#include "utils/macros.h"
33
34namespace ble
35{
36
39{
40public:
42 static constexpr size_t MAX_DATA_PAYLOAD_SIZE = 31;
43
45 static constexpr size_t MAX_SCAN_DATA_PAYLOAD_SIZE = 31;
46
48 static constexpr size_t MAX_EXT_DATA_PAYLOAD_SIZE = 254;
49
51 enum Field
52 {
55 };
56
58 enum class Flags : uint8_t
59 {
64 LE_BR_EDR_HOST = 0x10,
67 };
68
71 Advertisement(bool extended = false)
72 : extended_(extended)
73 {
74 }
75
80 Advertisement(size_t data_reserve, size_t scan_data_reserve)
81 : extended_(false)
82 {
83 if (data_reserve > MAX_DATA_PAYLOAD_SIZE)
84 {
85 data_reserve = MAX_DATA_PAYLOAD_SIZE;
86 }
87 if (scan_data_reserve > MAX_SCAN_DATA_PAYLOAD_SIZE)
88 {
89 scan_data_reserve = MAX_SCAN_DATA_PAYLOAD_SIZE;
90 }
91 data_.reserve(data_reserve);
92 scanData_.reserve(scan_data_reserve);
93 }
94
99 Advertisement(size_t data_reserve, size_t dummy, bool extended)
100 : extended_(extended)
101 {
102 size_t max =
104 if (data_reserve > max)
105 {
106 data_reserve = max;
107 }
108 data_.reserve(data_reserve);
109 }
110
116 std::basic_string<uint8_t> concat_service_data_128(
117 const uint8_t uuid[16], const void *buf, size_t size);
118
123 std::basic_string<uint8_t> concat_service_data_128(
124 const uint8_t uuid[16], std::basic_string<uint8_t> &buf)
125 {
126 return concat_service_data_128(uuid, buf.data(), buf.size());
127 }
128
136 int prepend(Field field, Defs::AdvType type, const void *buf, size_t size,
137 bool clip = false);
138
145 int prepend(Field field, Defs::AdvType type,
146 std::basic_string<uint8_t> &buf, bool clip = false)
147 {
148 return prepend(field, type, buf.data(), buf.size(), clip);
149 }
150
156 int prepend_name(Field field, std::string &name)
157 {
158 int result = prepend(
159 field, Defs::AdvType::NAME_COMPLETE, name.data(), name.size());
160 if (result > 0)
161 {
162 return result;
163 }
164 return prepend(
165 field, Defs::AdvType::NAME_SHORT, name.data(), name.size(), true);
166 }
167
175 int append(Field field, Defs::AdvType type, const void *buf, size_t size,
176 bool clip = false);
177
184 int append(Field field, Defs::AdvType type, std::basic_string<uint8_t> &buf,
185 bool clip = false)
186 {
187 return append(field, type, buf.data(), buf.size(), clip);
188 }
189
195 int append_name(Field field, std::string &name)
196 {
197 int result = append(
198 field, Defs::AdvType::NAME_COMPLETE, name.data(), name.size());
199 if (result > 0)
200 {
201 return result;
202 }
203 return append(
204 field, Defs::AdvType::NAME_SHORT, name.data(), name.size(), true);
205 }
206
216 int update(Field field, Defs::AdvType type, const void *buf, size_t size,
217 unsigned instance = 1, bool exact_size = true,
218 bool clip = false);
219
229 int update(Field field, Defs::AdvType type, std::basic_string<uint8_t> &buf,
230 unsigned instance = 1, bool exact_size = true,
231 bool clip = false)
232 {
233 return update(
234 field, type, buf.data(), buf.size(), instance, exact_size, clip);
235 }
236
240 {
241 return extended_;
242 }
243
246 uint8_t *get_data()
247 {
248 return (uint8_t*)data_.data();
249 }
250
254 {
255 return data_.size();
256 }
257
260 uint8_t *get_scan_data()
261 {
263 return (uint8_t*)scanData_.data();
264 }
265
269 {
270 return extended_ ? 0 : scanData_.size();
271 }
272
273#if defined(GTEST)
274 std::basic_string<uint8_t> &test_get_data()
275 {
276 return data_;
277 }
278
279 std::basic_string<uint8_t> &test_get_scan_data()
280 {
281 return scanData_;
282 }
283#endif
284
285private:
287 std::basic_string<uint8_t> data_;
288
290 std::basic_string<uint8_t> scanData_;
291
293};
294
295} // namespace ble
296
297#endif // _BLE_ADVERTISEMENT_HXX_
Object helper to define an advertisement.
int update(Field field, Defs::AdvType type, const void *buf, size_t size, unsigned instance=1, bool exact_size=true, bool clip=false)
Update existing advertisement.
uint8_t * get_data()
Get the advertisement data.
std::basic_string< uint8_t > concat_service_data_128(const uint8_t uuid[16], std::basic_string< uint8_t > &buf)
Concatenate a 128-bit (16-byte) UUID with provided data.
size_t get_data_size()
Get the advertisement data size in bytes.
int prepend_name(Field field, std::string &name)
Add name to the beginning of the advertisement.
@ LE_ONLY_LIMITED_DISC_MODE
BLE only limited discovery mode.
@ LE_BR_EDR_HOST
BLE + BR/EDR Host.
@ LE_GENERAL_DISC_MODE
BLE general discovery mode.
@ LE_BR_EDR_CONTROLLER
BLE + BR/EDR controller.
@ LE_LIMITED_DISC_MODE
BLE limited discovery mode.
@ BR_EDR_NOT_SUPPORTED
BR/EDR (classic) not supported.
@ LE_ONLY_GENERAL_DISC_MODE
BEE only general discovery mode.
uint8_t * get_scan_data()
Get the advertisement data.
bool is_extended()
Test if extended advertisement or not.
int append(Field field, Defs::AdvType type, const void *buf, size_t size, bool clip=false)
Add to the end of the advertisement.
Advertisement(size_t data_reserve, size_t scan_data_reserve)
Constructor which reserves data and scan data space.
Advertisement(size_t data_reserve, size_t dummy, bool extended)
Constructor which reserves data space.
static constexpr size_t MAX_SCAN_DATA_PAYLOAD_SIZE
Maximum payload size of scan data.
int append(Field field, Defs::AdvType type, std::basic_string< uint8_t > &buf, bool clip=false)
Add to the end of the advertisement.
Field
Data fields that make up an advertisement.
Advertisement(bool extended=false)
Constructor.
int append_name(Field field, std::string &name)
Add name to the end of the advertisement.
bool extended_
true extended advertisement, else false
static constexpr size_t MAX_DATA_PAYLOAD_SIZE
Maximum payload size of data.
int update(Field field, Defs::AdvType type, std::basic_string< uint8_t > &buf, unsigned instance=1, bool exact_size=true, bool clip=false)
Update existing advertisement.
static constexpr size_t MAX_EXT_DATA_PAYLOAD_SIZE
Maximum payload size of extended data.
std::basic_string< uint8_t > data_
advertising data, also used for extended advertising
int prepend(Field field, Defs::AdvType type, std::basic_string< uint8_t > &buf, bool clip=false)
Add to the beginning of the advertisement.
std::basic_string< uint8_t > concat_service_data_128(const uint8_t uuid[16], const void *buf, size_t size)
Concatenate a 128-bit (16-byte) UUID with provided data.
size_t get_scan_data_size()
Get the advertisement data size in bytes.
int prepend(Field field, Defs::AdvType type, const void *buf, size_t size, bool clip=false)
Add to the beginning of the advertisement.
AdvType
Advertising types.
Definition ble/Defs.hxx:106
@ NAME_COMPLETE
complete local name
@ NAME_SHORT
shortened local name
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138