ESPHome 2025.5.0
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#include <unordered_map>
10
11#ifdef USE_ESP32
12
13#include <esp_gap_ble_api.h>
14#include <esp_gatt_defs.h>
15#include <esp_gattc_api.h>
16#include <esp_gatts_api.h>
17#include <esp_bt_defs.h>
18#include <freertos/FreeRTOS.h>
19#include <freertos/semphr.h>
20
21namespace esphome {
22namespace esp32_ble_server {
23
24using namespace esp32_ble;
25using namespace bytebuffer;
26using namespace event_emitter;
27
28class BLEService;
29
30namespace BLECharacteristicEvt {
34
37};
38} // namespace BLECharacteristicEvt
39
40class BLECharacteristic : public EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>, uint16_t>,
41 public EventEmitter<BLECharacteristicEvt::EmptyEvt, uint16_t> {
42 public:
43 BLECharacteristic(ESPBTUUID uuid, uint32_t properties);
45
46 void set_value(ByteBuffer buffer);
47 void set_value(const std::vector<uint8_t> &buffer);
48 void set_value(const std::string &buffer);
49
50 void set_broadcast_property(bool value);
51 void set_indicate_property(bool value);
52 void set_notify_property(bool value);
53 void set_read_property(bool value);
54 void set_write_property(bool value);
55 void set_write_no_response_property(bool value);
56
57 void notify();
58
59 void do_create(BLEService *service);
60 void do_delete() { this->clients_to_notify_.clear(); }
61 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
62
63 void add_descriptor(BLEDescriptor *descriptor);
64 void remove_descriptor(BLEDescriptor *descriptor);
65
66 BLEService *get_service() { return this->service_; }
67 ESPBTUUID get_uuid() { return this->uuid_; }
68 std::vector<uint8_t> &get_value() { return this->value_; }
69
70 static const uint32_t PROPERTY_READ = 1 << 0;
71 static const uint32_t PROPERTY_WRITE = 1 << 1;
72 static const uint32_t PROPERTY_NOTIFY = 1 << 2;
73 static const uint32_t PROPERTY_BROADCAST = 1 << 3;
74 static const uint32_t PROPERTY_INDICATE = 1 << 4;
75 static const uint32_t PROPERTY_WRITE_NR = 1 << 5;
76
77 bool is_created();
78 bool is_failed();
79
80 protected:
81 bool write_event_{false};
84 esp_gatt_char_prop_t properties_;
85 uint16_t handle_{0xFFFF};
86
87 uint16_t value_read_offset_{0};
88 std::vector<uint8_t> value_;
89 SemaphoreHandle_t set_value_lock_;
90
91 std::vector<BLEDescriptor *> descriptors_;
92 std::unordered_map<uint16_t, bool> clients_to_notify_;
93
94 esp_gatt_perm_t permissions_ = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
95
103};
104
105} // namespace esp32_ble_server
106} // namespace esphome
107
108#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:38
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::unordered_map< uint16_t, bool > clients_to_notify_
std::vector< BLEDescriptor * > descriptors_
void add_descriptor(BLEDescriptor *descriptor)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7