5#ifndef ELEC_C7222_BLE_GAP_ADVERTISEMENT_DATA_H_
6#define ELEC_C7222_BLE_GAP_ADVERTISEMENT_DATA_H_
66 Build(type, data.data(), data.size());
83 static_assert(!std::is_pointer<T>::value,
"object_ptr must point to a concrete type");
85 Build(type,
reinterpret_cast<const std::uint8_t*
>(object_ptr),
sizeof(T));
100 static_assert(!std::is_pointer<T>::value,
"object_ptr must point to a concrete type");
102 Build(type,
reinterpret_cast<const std::uint8_t*
>(object_ptr),
sizeof(T) * elem_count);
117 template <
typename T>
119 static_assert(!std::is_pointer<T>::value,
120 "object_ref must refer to a concrete type, not a pointer type");
121 if(std::is_object<T>::value)
122 Build(type,
reinterpret_cast<const std::uint8_t*
>(&object_ref),
sizeof(T));
125 Build(type,
reinterpret_cast<const std::uint8_t*
>(&obj),
sizeof(T));
158 assert(data_.size() >= 2 &&
"AdvertisementData: data size too small to contain type");
166 assert(data_.size() >= 1 &&
"AdvertisementData: data size too small to contain length");
185 return data_ == other.data_;
201 std::vector<uint8_t>::iterator begin() {
202 return data_.begin();
207 std::vector<uint8_t>::const_iterator begin()
const {
208 return data_.begin();
213 std::vector<uint8_t>::const_iterator cbegin()
const {
214 return data_.cbegin();
219 std::vector<uint8_t>::iterator end() {
225 std::vector<uint8_t>::const_iterator end()
const {
231 std::vector<uint8_t>::const_iterator cend()
const {
286 std::vector<uint8_t> data_;
326 bool Set(
const std::list<AdvertisementData>& ads);
372 bool Add(
const std::list<AdvertisementData>& ads);
385 const std::vector<uint8_t>&
data()
const;
457 return advertisements_;
469 size_t adv_data_size);
480 std::list<AdvertisementData> advertisements_;
484 std::vector<uint8_t> data_;
495 return static_cast<uint8_t
>(lhs) |
static_cast<uint8_t
>(rhs);
498 uint8_t ret = lhs |
static_cast<uint8_t
>(rhs);
503 uint8_t ret = lhs |
static_cast<uint8_t
>(rhs);
508 uint8_t ret =
static_cast<uint8_t
>(lhs) | rhs;
515 return static_cast<uint8_t
>(lhs) &
static_cast<uint8_t
>(rhs);
519 uint8_t ret = lhs &
static_cast<uint8_t
>(rhs);
525 uint8_t ret = lhs &
static_cast<uint8_t
>(rhs);
531 uint8_t ret =
static_cast<uint8_t
>(lhs) & rhs;
537 uint8_t ret = lhs ^
static_cast<uint8_t
>(rhs);
542 uint8_t ret = lhs ^
static_cast<uint8_t
>(rhs);
547 uint8_t ret =
static_cast<uint8_t
>(lhs) & rhs;
constexpr uint8_t operator^=(uint8_t lhs, c7222::AdvertisementData::Flags rhs)
Definition advertisement_data.hpp:541
constexpr uint8_t operator|=(uint8_t lhs, c7222::AdvertisementData::Flags rhs)
Definition advertisement_data.hpp:502
constexpr uint8_t operator|(c7222::AdvertisementData::Flags lhs, c7222::AdvertisementData::Flags rhs)
Definition advertisement_data.hpp:493
constexpr uint8_t operator&(c7222::AdvertisementData::Flags lhs, c7222::AdvertisementData::Flags rhs)
Definition advertisement_data.hpp:513
constexpr uint8_t operator^(uint8_t lhs, c7222::AdvertisementData::Flags rhs)
Definition advertisement_data.hpp:536
constexpr uint8_t operator&=(uint8_t lhs, c7222::AdvertisementData::Flags rhs)
Definition advertisement_data.hpp:524
Builder for assembling a complete advertising payload.
Definition advertisement_data.hpp:304
void ReplaceOrAdd(AdvertisementData &&ad)
ReplaceOrAdd the AD structure with the same type (move).
AdvertisementDataBuilder & operator+=(const AdvertisementData &ad)
Add an AD structure and enforce uniqueness.
bool Push(const AdvertisementData &ad)
Add an AD structure to the payload.
bool Pop()
Remove the last AD structure from the payload.
bool Validate() const
Validate the assembled payload.
bool Build()
Build the raw payload from stored AD structures.
AdvertisementDataBuilder()
Create an empty builder.
size_t size() const
Return the total payload size in bytes.
const uint8_t * bytes() const
Return a raw pointer to the advertising payload bytes.
friend std::ostream & operator<<(std::ostream &os, const AdvertisementDataBuilder &adb)
Stream the builder payload for logging/debugging.
static std::list< AdvertisementData > DecodeBufferToAdvertisementDataList(const uint8_t *adv_data, size_t adv_data_size)
Decode a raw advertising payload buffer into a list of AD structures.
AdvertisementDataBuilder operator+(const AdvertisementData &ad)
Return a builder with an additional AD structure.
bool Set(const std::list< AdvertisementData > &ads)
ReplaceOrAdd the payload with a list of AD structures.
bool operator==(const AdvertisementDataBuilder &other) const
Compare two builders by payload or list contents.
void ReplaceOrAdd(const AdvertisementData &ad)
ReplaceOrAdd the AD structure with the same type.
AdvertisementDataBuilder operator+(const AdvertisementDataBuilder &adb)
Return a builder that is the merge of two builders.
const std::vector< uint8_t > & data() const
Return the raw advertising payload bytes.
void Clear()
Clear all stored AD structures.
AdvertisementDataBuilder & operator+=(const AdvertisementDataBuilder &adb)
Merge another builder into this one, enforcing uniqueness.
AdvertisementDataBuilder(const std::list< AdvertisementData > &ads)
Create a builder from a list of AD structures.
bool Add(const AdvertisementData &ad)
Add an AD structure to the payload.
const std::list< AdvertisementData > & advertisement_data_list() const
Decode the payload into a list of AD structures.
Definition advertisement_data.hpp:456
bool Add(const std::list< AdvertisementData > &ads)
Add a list of AD structures to the payload.
bool Set(const uint8_t *data, size_t size)
ReplaceOrAdd the payload from a raw advertising buffer.
Generic advertisement data structure builder.
Definition advertisement_data.hpp:46
friend std::ostream & operator<<(std::ostream &os, const AdvertisementData &ad)
Stream an AD structure for logging/debugging.
bool operator==(const AdvertisementData &other) const
Compare AD structure bytes for equality.
Definition advertisement_data.hpp:184
Flags
Definition advertisement_data.hpp:48
@ kSimultaneousLeAndBrEdrController
@ kSimultaneousLeAndBrEdrHost
@ kLeGeneralDiscoverableMode
@ kLeLimitedDiscoverableMode
static bool ValidateBuffer(const std::vector< uint8_t > &adv_data)
Validate a raw advertising payload stored in a vector.
Definition advertisement_data.hpp:266
AdvertisementData(AdvertisementDataType type, const std::vector< uint8_t > &data)
Build an AD structure from a byte vector.
Definition advertisement_data.hpp:65
static bool ValidateBuffer(const uint8_t *adv_data, size_t adv_data_size)
Validate a raw advertising payload buffer.
uint8_t GetLength() const
Return the length field (type + value bytes).
Definition advertisement_data.hpp:165
friend std::ostream & operator<<(std::ostream &os, const AdvertisementData::Flags &flag)
Stream AD Flags for logging/debugging.
AdvertisementData(AdvertisementDataType type, const T *object_ptr)
Build an AD structure from a typed object pointer.
Definition advertisement_data.hpp:82
const std::vector< uint8_t > & GetData() const
Return the full AD structure bytes.
Definition advertisement_data.hpp:134
size_t GetSize() const
Return the total size of the AD structure (length + type + value).
Definition advertisement_data.hpp:150
AdvertisementDataType GetType() const
Return the AD structure type.
Definition advertisement_data.hpp:157
AdvertisementData(AdvertisementDataType type, const T &object_ref)
Build an AD structure from a typed object reference.
Definition advertisement_data.hpp:118
std::vector< uint8_t > operator+=(const AdvertisementData &other) const
Concatenate two AD structures into a raw byte vector.
AdvertisementData(AdvertisementDataType type, const T *object_ptr, size_t elem_count)
Build an AD structure from an array of typed objects.
Definition advertisement_data.hpp:99
static bool ValidateLength(AdvertisementDataType type, size_t length)
Validate the AD length field for a given type.
const uint8_t * GetBytes() const
Return a raw pointer to the AD structure bytes.
Definition advertisement_data.hpp:143
C7222 course abstractions namespace.
Definition ble.hpp:20
std::vector< uint8_t > operator+(const AdvertisementData &lhs, const AdvertisementData &rhs)
Concatenate two AD structures into a raw byte vector.
constexpr size_t kAdvertisementDataLegacyMaxSize
Maximum length for legacy advertising data (length + type + value).
Definition advertisement_data.hpp:21
AdvertisementDataType
BLE GAP advertisement data types.
Definition advertisement_data.hpp:29
@ kSlaveConnectionIntervalRange
@ kIncompleteList16BitUuid
constexpr size_t kAdvertisementDataStructHeaderOverhead
Overhead bytes for an AD structure (length + type).
Definition advertisement_data.hpp:25