ESPHome  2024.3.1
bluetooth_proxy.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
5 #include <map>
6 #include <vector>
7 
13 #include "esphome/core/component.h"
14 #include "esphome/core/defines.h"
15 
16 #include "bluetooth_connection.h"
17 
18 namespace esphome {
19 namespace bluetooth_proxy {
20 
21 static const esp_err_t ESP_GATT_NOT_CONNECTED = -1;
22 
23 using namespace esp32_ble_client;
24 
25 // Legacy versions:
26 // Version 1: Initial version without active connections
27 // Version 2: Support for active connections
28 // Version 3: New connection API
29 // Version 4: Pairing support
30 // Version 5: Cache clear support
31 static const uint32_t LEGACY_ACTIVE_CONNECTIONS_VERSION = 5;
32 static const uint32_t LEGACY_PASSIVE_ONLY_VERSION = 1;
33 
34 enum BluetoothProxyFeature : uint32_t {
38  FEATURE_PAIRING = 1 << 3,
41 };
42 
45 };
46 
48  public:
50  bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
51  bool parse_devices(esp_ble_gap_cb_param_t::ble_scan_result_evt_param *advertisements, size_t count) override;
52  void dump_config() override;
53  void loop() override;
54  esp32_ble_tracker::AdvertisementParserType get_advertisement_parser_type() override;
55 
57  this->connections_.push_back(connection);
58  connection->proxy_ = this;
59  }
60 
61  void bluetooth_device_request(const api::BluetoothDeviceRequest &msg);
62  void bluetooth_gatt_read(const api::BluetoothGATTReadRequest &msg);
63  void bluetooth_gatt_write(const api::BluetoothGATTWriteRequest &msg);
64  void bluetooth_gatt_read_descriptor(const api::BluetoothGATTReadDescriptorRequest &msg);
65  void bluetooth_gatt_write_descriptor(const api::BluetoothGATTWriteDescriptorRequest &msg);
66  void bluetooth_gatt_send_services(const api::BluetoothGATTGetServicesRequest &msg);
67  void bluetooth_gatt_notify(const api::BluetoothGATTNotifyRequest &msg);
68 
69  int get_bluetooth_connections_free();
70  int get_bluetooth_connections_limit() { return this->connections_.size(); }
71 
72  void subscribe_api_connection(api::APIConnection *api_connection, uint32_t flags);
73  void unsubscribe_api_connection(api::APIConnection *api_connection);
74  api::APIConnection *get_api_connection() { return this->api_connection_; }
75 
76  void send_device_connection(uint64_t address, bool connected, uint16_t mtu = 0, esp_err_t error = ESP_OK);
77  void send_connections_free();
78  void send_gatt_services_done(uint64_t address);
79  void send_gatt_error(uint64_t address, uint16_t handle, esp_err_t error);
80  void send_device_pairing(uint64_t address, bool paired, esp_err_t error = ESP_OK);
81  void send_device_unpairing(uint64_t address, bool success, esp_err_t error = ESP_OK);
82  void send_device_clear_cache(uint64_t address, bool success, esp_err_t error = ESP_OK);
83 
84  static void uint64_to_bd_addr(uint64_t address, esp_bd_addr_t bd_addr) {
85  bd_addr[0] = (address >> 40) & 0xff;
86  bd_addr[1] = (address >> 32) & 0xff;
87  bd_addr[2] = (address >> 24) & 0xff;
88  bd_addr[3] = (address >> 16) & 0xff;
89  bd_addr[4] = (address >> 8) & 0xff;
90  bd_addr[5] = (address >> 0) & 0xff;
91  }
92 
93  void set_active(bool active) { this->active_ = active; }
94  bool has_active() { return this->active_; }
95 
96  uint32_t get_legacy_version() const {
97  if (this->active_) {
98  return LEGACY_ACTIVE_CONNECTIONS_VERSION;
99  }
100  return LEGACY_PASSIVE_ONLY_VERSION;
101  }
102 
103  uint32_t get_feature_flags() const {
104  uint32_t flags = 0;
107  if (this->active_) {
112  }
113 
114  return flags;
115  }
116 
117  protected:
118  void send_api_packet_(const esp32_ble_tracker::ESPBTDevice &device);
119 
120  BluetoothConnection *get_connection_(uint64_t address, bool reserve);
121 
122  bool active_;
123 
124  std::vector<BluetoothConnection *> connections_{};
125  api::APIConnection *api_connection_{nullptr};
126  bool raw_advertisements_{false};
127 };
128 
129 extern BluetoothProxy *global_bluetooth_proxy; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
130 
131 } // namespace bluetooth_proxy
132 } // namespace esphome
133 
134 #endif // USE_ESP32
void loop()
BluetoothProxy * global_bluetooth_proxy
static void uint64_to_bd_addr(uint64_t address, esp_bd_addr_t bd_addr)
const uint32_t flags
Definition: stm32flash.h:85
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void register_connection(BluetoothConnection *connection)