ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
adv_payload.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <cstring>
8#include <span>
9
12
14
18 public:
19 static constexpr size_t MAX_LEN = 31;
20
21 const uint8_t *data() const { return this->buf_.data(); }
22 size_t size() const { return this->len_; }
23
24 void add_flags(uint8_t flags) {
25 if (flags != 0 && this->left_() >= 3)
26 this->add_(0x01, &flags, 1);
27 }
28 void add_appearance(uint16_t appearance) {
29 if (appearance == 0 || this->left_() <= 3)
30 return;
31 uint8_t value[2];
32 put_le16(value, appearance);
33 this->add_(0x19, value, sizeof(value));
34 }
36 void add_name(const char *name, size_t len) {
37 if (this->left_() <= 2)
38 return;
39 const size_t fit = std::min(len, this->left_() - 2);
40 this->add_(fit < len ? 0x08 : 0x09, reinterpret_cast<const uint8_t *>(name), fit);
41 }
43 void add_manufacturer_data(std::span<const uint8_t> data) {
44 if (!data.empty() && this->left_() > 2)
45 this->add_(0xFF, data.data(), std::min(data.size(), this->left_() - 2));
46 }
47 void add_tx_power(int8_t dbm) {
48 if (this->left_() > 2)
49 this->add_(0x0A, reinterpret_cast<const uint8_t *>(&dbm), 1);
50 }
52 void add_uuids(std::span<const ESPBTUUID> uuids, const ESPBTUUID &skip = {}) {
53 this->add_short_uuids_(uuids, skip, 2, 0x02);
54 this->add_short_uuids_(uuids, skip, 4, 0x04);
55 if (this->left_() < 18)
56 return;
57 for (const auto &uuid : uuids) {
58 if (uuid != skip && uuid_size(uuid) == 16) {
59 this->add_(0x07, uuid.uuid128(), 16);
60 return;
61 }
62 }
63 }
65 void add_service_data(std::span<const uint8_t> data) {
66 if (!data.empty() && this->left_() >= data.size() + 2)
67 this->add_(0x16, data.data(), data.size());
68 }
69
71 static size_t uuid_size(const ESPBTUUID &uuid, uint32_t *value = nullptr) {
72 static constexpr uint8_t BASE[12] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00};
73 uint32_t short_value;
74 if (uuid.type() == ESPBTUUID::Type::UUID16) {
75 short_value = uuid.uuid16();
76 } else if (uuid.type() == ESPBTUUID::Type::UUID32) {
77 short_value = uuid.uuid32();
78 } else {
79 const uint8_t *raw = uuid.uuid128();
80 if (memcmp(raw, BASE, sizeof(BASE)) != 0)
81 return 16;
82 short_value = encode_value_le<uint32_t>(raw + 12);
83 }
84 if (value != nullptr)
85 *value = short_value;
86 return short_value <= 0xFFFF ? 2 : 4;
87 }
88
89 protected:
90 size_t left_() const { return MAX_LEN - this->len_; }
91 void add_(uint8_t type, const uint8_t *value, size_t len) {
92 this->buf_[this->len_++] = len + 1;
93 this->buf_[this->len_++] = type;
94 memcpy(this->buf_.data() + this->len_, value, len);
95 this->len_ += len;
96 }
97 void add_short_uuids_(std::span<const ESPBTUUID> uuids, const ESPBTUUID &skip, size_t size, uint8_t partial_type) {
98 const size_t count =
99 std::count_if(uuids.begin(), uuids.end(), [&](const auto &u) { return u != skip && uuid_size(u) == size; });
100 if (count == 0 || this->left_() < 2 + size)
101 return;
102 size_t fit = std::min(count, (this->left_() - 2) / size);
103 this->buf_[this->len_++] = 1 + fit * size;
104 this->buf_[this->len_++] = fit < count ? partial_type : partial_type + 1;
105 for (const auto &uuid : uuids) {
106 if (fit == 0)
107 break;
109 if (uuid == skip || uuid_size(uuid, &value) != size)
110 continue;
111 if (size == 2) {
112 put_le16(this->buf_.data() + this->len_, value);
113 } else {
114 put_le32(this->buf_.data() + this->len_, value);
115 }
116 this->len_ += size;
117 fit--;
118 }
119 }
120
121 std::array<uint8_t, MAX_LEN> buf_{};
122 uint8_t len_{0};
123};
124
127 std::span<const ESPBTUUID> uuids;
129 std::span<const uint8_t> manufacturer_data;
130 std::span<const uint8_t> service_data;
131 const char *name;
132 size_t name_len;
133 uint16_t appearance;
136};
137
140inline void build_adv_packets(const AdvContent &content, AdvPayload &adv, AdvPayload &rsp) {
141 const bool priority = content.priority_uuid.is_set();
142 AdvPayload &extras = priority ? rsp : adv;
143 adv.add_flags(0x06); // LE general discoverable, BR/EDR not supported
144 extras.add_appearance(content.appearance);
145 if (content.name_in_adv)
146 adv.add_name(content.name, content.name_len);
147 rsp.add_name(content.name, content.name_len);
149 rsp.add_tx_power(content.tx_power_dbm);
150 if (priority) {
151 adv.add_uuids({&content.priority_uuid, 1});
152 } else {
153 adv.add_uuids(content.uuids);
154 }
155 rsp.add_uuids(content.uuids, content.priority_uuid);
156 adv.add_service_data(content.service_data);
157 if (!priority)
158 rsp.add_service_data(content.service_data);
159}
160
161} // namespace esphome::ble_device_base
uint8_t raw[35]
Definition bl0939.h:0
A 31 byte advertising or scan response payload, packed in the order and with the fit rules of Bluedro...
Definition adv_payload.h:17
std::array< uint8_t, MAX_LEN > buf_
void add_(uint8_t type, const uint8_t *value, size_t len)
Definition adv_payload.h:91
static size_t uuid_size(const ESPBTUUID &uuid, uint32_t *value=nullptr)
Bluedroid's width for uuid: 2 or 4 (and its value) when it has a 16 or 32-bit form,...
Definition adv_payload.h:71
void add_uuids(std::span< const ESPBTUUID > uuids, const ESPBTUUID &skip={})
16 and 32-bit lists (partial when they do not all fit), then the first 128-bit UUID; skip is left out...
Definition adv_payload.h:52
static constexpr size_t MAX_LEN
Definition adv_payload.h:19
void add_manufacturer_data(std::span< const uint8_t > data)
Truncated to what fits.
Definition adv_payload.h:43
void add_appearance(uint16_t appearance)
Definition adv_payload.h:28
void add_short_uuids_(std::span< const ESPBTUUID > uuids, const ESPBTUUID &skip, size_t size, uint8_t partial_type)
Definition adv_payload.h:97
const uint8_t * data() const
Definition adv_payload.h:21
void add_name(const char *name, size_t len)
Complete name, or the shortened name type with what fits.
Definition adv_payload.h:36
void add_service_data(std::span< const uint8_t > data)
Raw service data element (its first bytes are the UUID); dropped whole when it does not fit.
Definition adv_payload.h:65
bool is_set() const
True if a UUID has been configured (not default-constructed).
Definition ble_device.h:99
uint16_t flags
uint8_t priority
uint8_t type
unsigned count
Definition gif.cpp:51
void build_adv_packets(const AdvContent &content, AdvPayload &adv, AdvPayload &rsp)
Split content between the advertising packet and the scan response.
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084
constexpr uint8_t * put_le32(uint8_t *dst, uint32_t value)
Definition helpers.h:1135
constexpr uint8_t * put_le16(uint8_t *dst, uint16_t value)
Definition helpers.h:1130
static void uint32_t
What the services advertising carries. A set priority_uuid (also in uuids) gets the advertising packe...
std::span< const uint8_t > service_data
std::span< const uint8_t > manufacturer_data
std::span< const ESPBTUUID > uuids