ESPHome  2024.4.1
ble_characteristic.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ble_descriptor.h"
5 
6 #include <vector>
7 
8 #ifdef USE_ESP32
9 
10 #include <esp_gap_ble_api.h>
11 #include <esp_gatt_defs.h>
12 #include <esp_gattc_api.h>
13 #include <esp_gatts_api.h>
14 #include <esp_bt_defs.h>
15 #include <freertos/FreeRTOS.h>
16 #include <freertos/semphr.h>
17 
18 namespace esphome {
19 namespace esp32_ble_server {
20 
21 using namespace esp32_ble;
22 
23 class BLEService;
24 
26  public:
27  BLECharacteristic(ESPBTUUID uuid, uint32_t properties);
29 
30  void set_value(const uint8_t *data, size_t length);
31  void set_value(std::vector<uint8_t> value);
32  void set_value(const std::string &value);
33  void set_value(uint8_t &data);
34  void set_value(uint16_t &data);
35  void set_value(uint32_t &data);
36  void set_value(int &data);
37  void set_value(float &data);
38  void set_value(double &data);
39  void set_value(bool &data);
40 
41  void set_broadcast_property(bool value);
42  void set_indicate_property(bool value);
43  void set_notify_property(bool value);
44  void set_read_property(bool value);
45  void set_write_property(bool value);
46  void set_write_no_response_property(bool value);
47 
48  void notify(bool notification = true);
49 
50  void do_create(BLEService *service);
51  void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
52 
53  void on_write(const std::function<void(const std::vector<uint8_t> &)> &&func) { this->on_write_ = func; }
54 
55  void add_descriptor(BLEDescriptor *descriptor);
56  void remove_descriptor(BLEDescriptor *descriptor);
57 
58  BLEService *get_service() { return this->service_; }
59  ESPBTUUID get_uuid() { return this->uuid_; }
60  std::vector<uint8_t> &get_value() { return this->value_; }
61 
62  static const uint32_t PROPERTY_READ = 1 << 0;
63  static const uint32_t PROPERTY_WRITE = 1 << 1;
64  static const uint32_t PROPERTY_NOTIFY = 1 << 2;
65  static const uint32_t PROPERTY_BROADCAST = 1 << 3;
66  static const uint32_t PROPERTY_INDICATE = 1 << 4;
67  static const uint32_t PROPERTY_WRITE_NR = 1 << 5;
68 
69  bool is_created();
70  bool is_failed();
71 
72  protected:
73  bool write_event_{false};
76  esp_gatt_char_prop_t properties_;
77  uint16_t handle_{0xFFFF};
78 
79  uint16_t value_read_offset_{0};
80  std::vector<uint8_t> value_;
81  SemaphoreHandle_t set_value_lock_;
82 
83  std::vector<BLEDescriptor *> descriptors_;
84 
85  std::function<void(const std::vector<uint8_t> &)> on_write_;
86 
87  esp_gatt_perm_t permissions_ = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
88 
89  enum State : uint8_t {
90  FAILED = 0x00,
95  } state_{INIT};
96 };
97 
98 } // namespace esp32_ble_server
99 } // namespace esphome
100 
101 #endif
std::vector< BLEDescriptor * > descriptors_
void on_write(const std::function< void(const std::vector< uint8_t > &)> &&func)
uint16_t length
Definition: tt21100.cpp:12
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7