Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Advertisement.cxx
Go to the documentation of this file.
1
28#include "ble/Advertisement.hxx"
29
30namespace ble
31{
32
33//
34// Advertisement::concat_service_data_128()
35//
36std::basic_string<uint8_t> Advertisement::concat_service_data_128(
37 const uint8_t uuid[16], const void *buf, size_t size)
38{
39 const uint8_t *data = static_cast<const uint8_t*>(buf);
40 std::basic_string<uint8_t> result(uuid, uuid + 16);
41 result.append(data, size);
42 return result;
43}
44
45//
46// Advertisement::prepend()
47//
49 Field field, Defs::AdvType type, const void *buf, size_t size, bool clip)
50{
51 const uint8_t *data = static_cast<const uint8_t*>(buf);
52 std::basic_string<uint8_t> *d;
53 size_t max;
54
55 switch (field)
56 {
57 default:
58 case Field::DATA:
59 d = &data_;
60 max = extended_ ?
62 break;
65 d = &scanData_;
67 break;
68 }
69
70 size_t space = std::min((size + 2), (max - d->size()));
71 if (space < (size + 2) && clip == false)
72 {
73 // Data doesn't fit and clipping is not allowed.
74 return -1;
75 }
76 d->insert(0, 1, space - 1);
77 d->insert(1, 1, static_cast<uint8_t>(type));
78 d->insert(2, data, space - 2);
79 return space;
80}
81
82//
83// Advertisement::append()
84//
86 Field field, Defs::AdvType type, const void *buf, size_t size, bool clip)
87{
88 const uint8_t *data = static_cast<const uint8_t*>(buf);
89 std::basic_string<uint8_t> *d;
90 size_t max;
91
92 switch (field)
93 {
94 default:
95 case Field::DATA:
96 d = &data_;
97 max = extended_ ?
99 break;
100 case Field::SCAN_DATA:
102 d = &scanData_;
104 break;
105 }
106
107 size_t space = std::min((size + 2), (max - d->size()));
108 if (space < (size + 2) && clip == false)
109 {
110 // Data doesn't fit and clipping is not allowed.
111 return -1;
112 }
113 d->push_back(space - 1);
114 d->push_back(static_cast<uint8_t>(type));
115 d->append(data, space - 2);
116 return space;
117}
118
119//
120// Advertisement::update()
121//
122int Advertisement::update(Field field, Defs::AdvType type, const void *buf,
123 size_t size, unsigned instance, bool exact_size,
124 bool clip)
125{
126 const uint8_t *data = static_cast<const uint8_t*>(buf);
127 std::basic_string<uint8_t> *d;
128 size_t max;
129
130 switch (field)
131 {
132 default:
133 case Field::DATA:
134 d = &data_;
135 max = extended_ ?
137 break;
138 case Field::SCAN_DATA:
140 d = &scanData_;
142 break;
143 }
144
145 uint8_t len;
146 ssize_t pos = Defs::adv_find_data(*d, type, &len, instance);
147 if (pos < 0)
148 {
149 // No matching advertising data found.
150 return -1;
151 }
152 if (exact_size && len != size)
153 {
154 // Found the data, but it is the wrong size.
155 return -1;
156 }
157 size_t space = std::min((size + 2), (max - d->size()) + (len + 2));
158 if (space < size && clip == false)
159 {
160 // Data doesn't fit and clipping is not allowed.
161 return -1;
162 }
163 d->at(pos) = space - 1;
164 d->replace(pos + 2, len, data, space - 2);
165 return space;
166}
167
168} // namespace ble
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.
int append(Field field, Defs::AdvType type, const void *buf, size_t size, bool clip=false)
Add to the end of the advertisement.
static constexpr size_t MAX_SCAN_DATA_PAYLOAD_SIZE
Maximum payload size of scan data.
Field
Data fields that make up an advertisement.
bool extended_
true extended advertisement, else false
static constexpr size_t MAX_DATA_PAYLOAD_SIZE
Maximum payload size of data.
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
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.
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
static ssize_t adv_find_data(std::basic_string< uint8_t > &adv, AdvType type, uint8_t *size, unsigned instance=1)
Find an advertisment data within an advertisement set.
Definition ble/Defs.cxx:59
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138