ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
ble_characteristic.h
Go to the documentation of this file.
1#pragma once
2
3#include "ble_descriptor.h"
7
8#include <vector>
9
10#ifdef USE_ESP32
11
12#include <esp_gap_ble_api.h>
13#include <esp_gatt_defs.h>
14#include <esp_gattc_api.h>
15#include <esp_gatts_api.h>
16#include <esp_bt_defs.h>
17#include <freertos/FreeRTOS.h>
18#include <freertos/semphr.h>
19
20namespace esphome {
21namespace esp32_ble_server {
22
23using namespace esp32_ble;
24using namespace bytebuffer;
25using namespace event_emitter;
26
27class BLEService;
28
29namespace BLECharacteristicEvt {
33
36};
37} // namespace BLECharacteristicEvt
38
39class BLECharacteristic : public EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>, uint16_t>,
40 public EventEmitter<BLECharacteristicEvt::EmptyEvt, uint16_t> {
41 public:
42 BLECharacteristic(ESPBTUUID uuid, uint32_t properties);
44
45 void set_value(ByteBuffer buffer);
46 void set_value(const std::vector<uint8_t> &buffer);
47 void set_value(const std::string &buffer);
48
49 void set_broadcast_property(bool value);
50 void set_indicate_property(bool value);
51 void set_notify_property(bool value);
52 void set_read_property(bool value);
53 void set_write_property(bool value);
54 void set_write_no_response_property(bool value);
55
56 void notify();
57
58 void do_create(BLEService *service);
59 void do_delete() { this->clients_to_notify_.clear(); }
60 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
61
62 void add_descriptor(BLEDescriptor *descriptor);
63 void remove_descriptor(BLEDescriptor *descriptor);
64
65 BLEService *get_service() { return this->service_; }
66 ESPBTUUID get_uuid() { return this->uuid_; }
67 std::vector<uint8_t> &get_value() { return this->value_; }
68
69 static const uint32_t PROPERTY_READ = 1 << 0;
70 static const uint32_t PROPERTY_WRITE = 1 << 1;
71 static const uint32_t PROPERTY_NOTIFY = 1 << 2;
72 static const uint32_t PROPERTY_BROADCAST = 1 << 3;
73 static const uint32_t PROPERTY_INDICATE = 1 << 4;
74 static const uint32_t PROPERTY_WRITE_NR = 1 << 5;
75
76 bool is_created();
77 bool is_failed();
78
79 protected:
80 bool write_event_{false};
83 esp_gatt_char_prop_t properties_;
84 uint16_t handle_{0xFFFF};
85
86 uint16_t value_read_offset_{0};
87 std::vector<uint8_t> value_;
88 SemaphoreHandle_t set_value_lock_;
89
90 std::vector<BLEDescriptor *> descriptors_;
91
93 uint16_t conn_id;
94 bool indicate; // true = indicate, false = notify
95 };
96 std::vector<ClientNotificationEntry> clients_to_notify_;
97
98 void remove_client_from_notify_list_(uint16_t conn_id);
100
101 esp_gatt_perm_t permissions_ = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
102
110};
111
112} // namespace esp32_ble_server
113} // namespace esphome
114
115#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:38
ClientNotificationEntry * find_client_in_notify_list_(uint16_t conn_id)
void remove_descriptor(BLEDescriptor *descriptor)
enum esphome::esp32_ble_server::BLECharacteristic::State INIT
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_
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
bool indicate
uint16_t conn_id