ESPHome 2025.11.4
Loading...
Searching...
No Matches
ble_characteristic.cpp
Go to the documentation of this file.
2#include "ble_server.h"
3#include "ble_service.h"
4
6#include "esphome/core/log.h"
7
8#ifdef USE_ESP32
9
10namespace esphome {
11namespace esp32_ble_server {
12
13static const char *const TAG = "esp32_ble_server.characteristic";
14
16 for (auto *descriptor : this->descriptors_) {
17 delete descriptor; // NOLINT(cppcoreguidelines-owning-memory)
18 }
19 vSemaphoreDelete(this->set_value_lock_);
20}
21
22BLECharacteristic::BLECharacteristic(const ESPBTUUID uuid, uint32_t properties) : uuid_(uuid) {
23 this->set_value_lock_ = xSemaphoreCreateBinary();
24 xSemaphoreGive(this->set_value_lock_);
25
26 this->properties_ = (esp_gatt_char_prop_t) 0;
27
28 this->set_broadcast_property((properties & PROPERTY_BROADCAST) != 0);
29 this->set_indicate_property((properties & PROPERTY_INDICATE) != 0);
30 this->set_notify_property((properties & PROPERTY_NOTIFY) != 0);
31 this->set_read_property((properties & PROPERTY_READ) != 0);
32 this->set_write_property((properties & PROPERTY_WRITE) != 0);
33 this->set_write_no_response_property((properties & PROPERTY_WRITE_NR) != 0);
34}
35
37
38void BLECharacteristic::set_value(std::vector<uint8_t> &&buffer) {
39 xSemaphoreTake(this->set_value_lock_, 0L);
40 this->value_ = std::move(buffer);
41 xSemaphoreGive(this->set_value_lock_);
42}
43
44void BLECharacteristic::set_value(std::initializer_list<uint8_t> data) {
45 this->set_value(std::vector<uint8_t>(data)); // Delegate to move overload
46}
47
48void BLECharacteristic::set_value(const std::string &buffer) {
49 this->set_value(std::vector<uint8_t>(buffer.begin(), buffer.end())); // Delegate to move overload
50}
51
53 if (this->service_ == nullptr || this->service_->get_server() == nullptr ||
54 this->service_->get_server()->get_connected_client_count() == 0)
55 return;
56
57 const uint16_t *clients = this->service_->get_server()->get_clients();
58 uint8_t client_count = this->service_->get_server()->get_client_count();
59
60 for (uint8_t i = 0; i < client_count; i++) {
61 uint16_t client = clients[i];
62 size_t length = this->value_.size();
63 // Find the client in the list of clients to notify
64 auto *entry = this->find_client_in_notify_list_(client);
65 if (entry == nullptr)
66 continue;
67 bool require_ack = entry->indicate;
68 // TODO: Remove this block when INDICATE acknowledgment is supported
69 if (require_ack) {
70 ESP_LOGW(TAG, "INDICATE acknowledgment is not yet supported (i.e. it works as a NOTIFY)");
71 require_ack = false;
72 }
73 esp_err_t err = esp_ble_gatts_send_indicate(this->service_->get_server()->get_gatts_if(), client, this->handle_,
74 length, this->value_.data(), require_ack);
75 if (err != ESP_OK) {
76 ESP_LOGE(TAG, "esp_ble_gatts_send_indicate failed %d", err);
77 return;
78 }
79 }
80}
81
83 // If the descriptor is the CCCD descriptor, listen to its write event to know if the client wants to be notified
84 if (descriptor->get_uuid() == ESPBTUUID::from_uint16(ESP_GATT_UUID_CHAR_CLIENT_CONFIG)) {
85 descriptor->on_write([this](std::span<const uint8_t> value, uint16_t conn_id) {
86 if (value.size() != 2)
87 return;
88 uint16_t cccd = encode_uint16(value[1], value[0]);
89 bool notify = (cccd & 1) != 0;
90 bool indicate = (cccd & 2) != 0;
91 // Remove existing entry if present
93 // Add new entry if needed
94 if (notify || indicate) {
95 this->clients_to_notify_.push_back({conn_id, indicate});
96 }
97 });
98 }
99 this->descriptors_.push_back(descriptor);
100}
101
103 this->descriptors_.erase(std::remove(this->descriptors_.begin(), this->descriptors_.end(), descriptor),
104 this->descriptors_.end());
105}
106
108 this->service_ = service;
109 esp_attr_control_t control;
110 control.auto_rsp = ESP_GATT_RSP_BY_APP;
111
112 ESP_LOGV(TAG, "Creating characteristic - %s", this->uuid_.to_string().c_str());
113
114 esp_bt_uuid_t uuid = this->uuid_.get_uuid();
115 esp_err_t err = esp_ble_gatts_add_char(service->get_handle(), &uuid, static_cast<esp_gatt_perm_t>(this->permissions_),
116 this->properties_, nullptr, &control);
117
118 if (err != ESP_OK) {
119 ESP_LOGE(TAG, "esp_ble_gatts_add_char failed: %d", err);
120 return;
121 }
122
123 this->state_ = CREATING;
124}
125
127 if (this->state_ == CREATED)
128 return true;
129
130 if (this->state_ != CREATING_DEPENDENTS)
131 return false;
132
133 for (auto *descriptor : this->descriptors_) {
134 if (!descriptor->is_created())
135 return false;
136 }
137 // All descriptors are created if we reach here
138 this->state_ = CREATED;
139 return true;
140}
141
143 if (this->state_ == FAILED)
144 return true;
145
146 for (auto *descriptor : this->descriptors_) {
147 if (descriptor->is_failed()) {
148 this->state_ = FAILED;
149 return true;
150 }
151 }
152 return false;
153}
154
155void BLECharacteristic::set_property_bit_(esp_gatt_char_prop_t bit, bool value) {
156 if (value) {
157 this->properties_ = (esp_gatt_char_prop_t) (this->properties_ | bit);
158 } else {
159 this->properties_ = (esp_gatt_char_prop_t) (this->properties_ & ~bit);
160 }
161}
162
164 this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_BROADCAST, value);
165}
167 this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_INDICATE, value);
168}
170 this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_NOTIFY, value);
171}
172void BLECharacteristic::set_read_property(bool value) { this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_READ, value); }
173void BLECharacteristic::set_write_property(bool value) { this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_WRITE, value); }
175 this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_WRITE_NR, value);
176}
177
178void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
179 esp_ble_gatts_cb_param_t *param) {
180 switch (event) {
181 case ESP_GATTS_ADD_CHAR_EVT: {
182 if (this->uuid_ == ESPBTUUID::from_uuid(param->add_char.char_uuid)) {
183 this->handle_ = param->add_char.attr_handle;
184
185 for (auto *descriptor : this->descriptors_) {
186 descriptor->do_create(this);
187 }
188
189 this->state_ = CREATING_DEPENDENTS;
190 }
191 break;
192 }
193 case ESP_GATTS_READ_EVT: {
194 if (param->read.handle != this->handle_)
195 break; // Not this characteristic
196
197 if (!param->read.need_rsp)
198 break; // For some reason you can request a read but not want a response
199
200 if (this->on_read_callback_) {
201 (*this->on_read_callback_)(param->read.conn_id);
202 }
203
204 uint16_t max_offset = 22;
205
206 esp_gatt_rsp_t response;
207 if (param->read.is_long) {
208 if (this->value_.size() - this->value_read_offset_ < max_offset) {
209 // Last message in the chain
210 response.attr_value.len = this->value_.size() - this->value_read_offset_;
211 response.attr_value.offset = this->value_read_offset_;
212 memcpy(response.attr_value.value, this->value_.data() + response.attr_value.offset, response.attr_value.len);
213 this->value_read_offset_ = 0;
214 } else {
215 response.attr_value.len = max_offset;
216 response.attr_value.offset = this->value_read_offset_;
217 memcpy(response.attr_value.value, this->value_.data() + response.attr_value.offset, response.attr_value.len);
218 this->value_read_offset_ += max_offset;
219 }
220 } else {
221 response.attr_value.offset = 0;
222 if (this->value_.size() + 1 > max_offset) {
223 response.attr_value.len = max_offset;
224 this->value_read_offset_ = max_offset;
225 } else {
226 response.attr_value.len = this->value_.size();
227 }
228 memcpy(response.attr_value.value, this->value_.data(), response.attr_value.len);
229 }
230
231 response.attr_value.handle = this->handle_;
232 response.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
233
234 esp_err_t err =
235 esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id, ESP_GATT_OK, &response);
236 if (err != ESP_OK) {
237 ESP_LOGE(TAG, "esp_ble_gatts_send_response failed: %d", err);
238 }
239 break;
240 }
241 case ESP_GATTS_WRITE_EVT: {
242 if (this->handle_ != param->write.handle)
243 break;
244
245 if (param->write.is_prep) {
246 this->value_.insert(this->value_.end(), param->write.value, param->write.value + param->write.len);
247 this->write_event_ = true;
248 } else {
249 this->set_value(ByteBuffer::wrap(param->write.value, param->write.len));
250 }
251
252 if (param->write.need_rsp) {
253 esp_gatt_rsp_t response;
254
255 response.attr_value.len = param->write.len;
256 response.attr_value.handle = this->handle_;
257 response.attr_value.offset = param->write.offset;
258 response.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
259 memcpy(response.attr_value.value, param->write.value, param->write.len);
260
261 esp_err_t err =
262 esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, &response);
263
264 if (err != ESP_OK) {
265 ESP_LOGE(TAG, "esp_ble_gatts_send_response failed: %d", err);
266 }
267 }
268
269 if (!param->write.is_prep) {
270 if (this->on_write_callback_) {
271 (*this->on_write_callback_)(this->value_, param->write.conn_id);
272 }
273 }
274
275 break;
276 }
277
278 case ESP_GATTS_EXEC_WRITE_EVT: {
279 if (!this->write_event_)
280 break;
281 this->write_event_ = false;
282 if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
283 if (this->on_write_callback_) {
284 (*this->on_write_callback_)(this->value_, param->exec_write.conn_id);
285 }
286 }
287 esp_err_t err =
288 esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, nullptr);
289 if (err != ESP_OK) {
290 ESP_LOGE(TAG, "esp_ble_gatts_send_response failed: %d", err);
291 }
292 break;
293 }
294 default:
295 break;
296 }
297
298 for (auto *descriptor : this->descriptors_) {
299 descriptor->gatts_event_handler(event, gatts_if, param);
300 }
301}
302
304 // Since we typically have very few clients (often just 1), we can optimize
305 // for the common case by swapping with the last element and popping
306 for (size_t i = 0; i < this->clients_to_notify_.size(); i++) {
307 if (this->clients_to_notify_[i].conn_id == conn_id) {
308 // Swap with last element and pop (safe even when i is the last element)
309 this->clients_to_notify_[i] = this->clients_to_notify_.back();
310 this->clients_to_notify_.pop_back();
311 return;
312 }
313 }
314}
315
317 for (auto &entry : this->clients_to_notify_) {
318 if (entry.conn_id == conn_id) {
319 return &entry;
320 }
321 }
322 return nullptr;
323}
324
325} // namespace esp32_ble_server
326} // namespace esphome
327
328#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:38
std::vector< uint8_t > get_data()
Definition bytebuffer.h:300
static ByteBuffer wrap(T value, Endian endianness=LITTLE)
Definition bytebuffer.h:156
std::string to_string() const
Definition ble_uuid.cpp:146
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Definition ble_uuid.cpp:84
static ESPBTUUID from_uint16(uint16_t uuid)
Definition ble_uuid.cpp:17
esp_bt_uuid_t get_uuid() const
Definition ble_uuid.cpp:145
ClientNotificationEntry * find_client_in_notify_list_(uint16_t conn_id)
void remove_descriptor(BLEDescriptor *descriptor)
std::unique_ptr< std::function< void(std::span< const uint8_t >, uint16_t)> > on_write_callback_
void set_property_bit_(esp_gatt_char_prop_t bit, bool value)
BLECharacteristic(ESPBTUUID uuid, uint32_t properties)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
std::vector< BLEDescriptor * > descriptors_
void add_descriptor(BLEDescriptor *descriptor)
std::vector< ClientNotificationEntry > clients_to_notify_
std::unique_ptr< std::function< void(uint16_t)> > on_read_callback_
void on_write(std::function< void(std::span< const uint8_t >, uint16_t)> &&callback)
const uint16_t * get_clients() const
Definition ble_server.h:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:380
uint16_t length
Definition tt21100.cpp:0