ESPHome  2024.11.2
esp32_ble_tracker.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/defines.h"
6 #include "esphome/core/helpers.h"
7 
8 #include <array>
9 #include <string>
10 #include <vector>
11 
12 #ifdef USE_ESP32
13 
14 #include <esp_bt_defs.h>
15 #include <esp_gap_ble_api.h>
16 #include <esp_gattc_api.h>
17 
18 #include <freertos/FreeRTOS.h>
19 #include <freertos/semphr.h>
20 
23 
24 namespace esphome {
25 namespace esp32_ble_tracker {
26 
27 using namespace esp32_ble;
28 
29 using adv_data_t = std::vector<uint8_t>;
30 
34 };
35 
36 struct ServiceData {
39 };
40 
42  public:
43  ESPBLEiBeacon() { memset(&this->beacon_data_, 0, sizeof(this->beacon_data_)); }
44  ESPBLEiBeacon(const uint8_t *data);
45  static optional<ESPBLEiBeacon> from_manufacturer_data(const ServiceData &data);
46 
47  uint16_t get_major() { return byteswap(this->beacon_data_.major); }
48  uint16_t get_minor() { return byteswap(this->beacon_data_.minor); }
49  int8_t get_signal_power() { return this->beacon_data_.signal_power; }
50  ESPBTUUID get_uuid() { return ESPBTUUID::from_raw_reversed(this->beacon_data_.proximity_uuid); }
51 
52  protected:
53  struct {
54  uint8_t sub_type;
55  uint8_t length;
56  uint8_t proximity_uuid[16];
57  uint16_t major;
58  uint16_t minor;
59  int8_t signal_power;
60  } PACKED beacon_data_;
61 };
62 
63 class ESPBTDevice {
64  public:
65  void parse_scan_rst(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &param);
66 
67  std::string address_str() const;
68 
69  uint64_t address_uint64() const;
70 
71  const uint8_t *address() const { return address_; }
72 
73  esp_ble_addr_type_t get_address_type() const { return this->address_type_; }
74  int get_rssi() const { return rssi_; }
75  const std::string &get_name() const { return this->name_; }
76 
77  const std::vector<int8_t> &get_tx_powers() const { return tx_powers_; }
78 
79  const optional<uint16_t> &get_appearance() const { return appearance_; }
80  const optional<uint8_t> &get_ad_flag() const { return ad_flag_; }
81  const std::vector<ESPBTUUID> &get_service_uuids() const { return service_uuids_; }
82 
83  const std::vector<ServiceData> &get_manufacturer_datas() const { return manufacturer_datas_; }
84 
85  const std::vector<ServiceData> &get_service_datas() const { return service_datas_; }
86 
87  const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &get_scan_result() const { return scan_result_; }
88 
89  bool resolve_irk(const uint8_t *irk) const;
90 
92  for (auto &it : this->manufacturer_datas_) {
94  if (res.has_value())
95  return *res;
96  }
97  return {};
98  }
99 
100  protected:
101  void parse_adv_(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &param);
102 
103  esp_bd_addr_t address_{
104  0,
105  };
106  esp_ble_addr_type_t address_type_{BLE_ADDR_TYPE_PUBLIC};
107  int rssi_{0};
108  std::string name_{};
109  std::vector<int8_t> tx_powers_{};
110  optional<uint16_t> appearance_{};
111  optional<uint8_t> ad_flag_{};
112  std::vector<ESPBTUUID> service_uuids_{};
113  std::vector<ServiceData> manufacturer_datas_{};
114  std::vector<ServiceData> service_datas_{};
115  esp_ble_gap_cb_param_t::ble_scan_result_evt_param scan_result_{};
116 };
117 
118 class ESP32BLETracker;
119 
121  public:
122  virtual void on_scan_end() {}
123  virtual bool parse_device(const ESPBTDevice &device) = 0;
124  virtual bool parse_devices(esp_ble_gap_cb_param_t::ble_scan_result_evt_param *advertisements, size_t count) {
125  return false;
126  };
129  };
130  void set_parent(ESP32BLETracker *parent) { parent_ = parent; }
131 
132  protected:
133  ESP32BLETracker *parent_{nullptr};
134 };
135 
136 enum class ClientState {
137  // Connection is allocated
138  INIT,
139  // Client is disconnecting
141  // Connection is idle, no device detected.
142  IDLE,
143  // Searching for device.
144  SEARCHING,
145  // Device advertisement found.
146  DISCOVERED,
147  // Device is discovered and the scanner is stopped
149  // Connection in progress.
150  CONNECTING,
151  // Initial connection established.
152  CONNECTED,
153  // The client and sub-clients have completed setup.
154  ESTABLISHED,
155 };
156 
157 enum class ConnectionType {
158  // The default connection type, we hold all the services in ram
159  // for the duration of the connection.
160  V1,
161  // The client has a cache of the services and mtu so we should not
162  // fetch them again
164  // The client does not need the services and mtu once we send them
165  // so we should wipe them from memory as soon as we send them
167 };
168 
170  public:
171  virtual bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
172  esp_ble_gattc_cb_param_t *param) = 0;
173  virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
174  virtual void connect() = 0;
175  virtual void disconnect() = 0;
176  virtual void set_state(ClientState st) { this->state_ = st; }
177  ClientState state() const { return state_; }
178  int app_id;
179 
180  protected:
182 };
183 
184 class ESP32BLETracker : public Component,
185  public GAPEventHandler,
186  public GATTcEventHandler,
187  public BLEStatusEventHandler,
188  public Parented<ESP32BLE> {
189  public:
190  void set_scan_duration(uint32_t scan_duration) { scan_duration_ = scan_duration; }
191  void set_scan_interval(uint32_t scan_interval) { scan_interval_ = scan_interval; }
192  void set_scan_window(uint32_t scan_window) { scan_window_ = scan_window; }
193  void set_scan_active(bool scan_active) { scan_active_ = scan_active; }
194  void set_scan_continuous(bool scan_continuous) { scan_continuous_ = scan_continuous; }
195 
197  void setup() override;
198  void dump_config() override;
199  float get_setup_priority() const override;
200 
201  void loop() override;
202 
203  void register_listener(ESPBTDeviceListener *listener);
204  void register_client(ESPBTClient *client);
205  void recalculate_advertisement_parser_types();
206 
207  void print_bt_device_info(const ESPBTDevice &device);
208 
209  void start_scan();
210  void stop_scan();
211 
212  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
213  esp_ble_gattc_cb_param_t *param) override;
214  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
215  void ble_before_disabled_event_handler() override;
216 
217  protected:
218  void stop_scan_();
220  void start_scan_(bool first);
222  void end_of_scan_();
224  void gap_scan_result_(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &param);
226  void gap_scan_set_param_complete_(const esp_ble_gap_cb_param_t::ble_scan_param_cmpl_evt_param &param);
228  void gap_scan_start_complete_(const esp_ble_gap_cb_param_t::ble_scan_start_cmpl_evt_param &param);
230  void gap_scan_stop_complete_(const esp_ble_gap_cb_param_t::ble_scan_stop_cmpl_evt_param &param);
231 
232  int app_id_;
233 
235  std::vector<uint64_t> already_discovered_;
236  std::vector<ESPBTDeviceListener *> listeners_;
238  std::vector<ESPBTClient *> clients_;
240  esp_ble_scan_params_t scan_params_;
242  uint32_t scan_duration_;
243  uint32_t scan_interval_;
244  uint32_t scan_window_;
249  bool ble_was_disabled_{true};
250  bool raw_advertisements_{false};
251  bool parse_advertisements_{false};
252  SemaphoreHandle_t scan_result_lock_;
253  SemaphoreHandle_t scan_end_lock_;
254  size_t scan_result_index_{0};
255 #ifdef USE_PSRAM
256  const static u_int8_t SCAN_RESULT_BUFFER_SIZE = 32;
257 #else
258  const static u_int8_t SCAN_RESULT_BUFFER_SIZE = 16;
259 #endif // USE_PSRAM
260  esp_ble_gap_cb_param_t::ble_scan_result_evt_param *scan_result_buffer_;
261  esp_bt_status_t scan_start_failed_{ESP_BT_STATUS_SUCCESS};
262  esp_bt_status_t scan_set_param_failed_{ESP_BT_STATUS_SUCCESS};
263 };
264 
265 // NOLINTNEXTLINE
267 
268 } // namespace esp32_ble_tracker
269 } // namespace esphome
270 
271 #endif
void setup()
optional< ESPBLEiBeacon > get_ibeacon() const
void loop()
struct esphome::sen5x::Sen5xBaselines PACKED
const std::vector< int8_t > & get_tx_powers() const
const optional< uint16_t > & get_appearance() const
const std::vector< ServiceData > & get_manufacturer_datas() const
const std::vector< ESPBTUUID > & get_service_uuids() const
esp_ble_scan_params_t scan_params_
A structure holding the ESP BLE scan parameters.
ESP32BLETracker * global_esp32_ble_tracker
virtual void set_state(ClientState st)
esp_ble_gap_cb_param_t::ble_scan_result_evt_param * scan_result_buffer_
const std::vector< ServiceData > & get_service_datas() const
esp_ble_addr_type_t get_address_type() const
const esp_ble_gap_cb_param_t::ble_scan_result_evt_param & get_scan_result() const
virtual AdvertisementParserType get_advertisement_parser_type()
uint32_t scan_duration_
The interval in seconds to perform scans.
std::vector< uint8_t > adv_data_t
constexpr14 T byteswap(T n)
Definition: helpers.h:130
std::vector< uint64_t > already_discovered_
Vector of addresses that have already been printed in print_bt_device_info.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual bool parse_devices(esp_ble_gap_cb_param_t::ble_scan_result_evt_param *advertisements, size_t count)
void set_scan_duration(uint32_t scan_duration)
const std::string & get_name() const
void set_scan_interval(uint32_t scan_interval)
static ESPBTUUID from_raw_reversed(const uint8_t *data)
Definition: ble_uuid.cpp:34
static optional< ESPBLEiBeacon > from_manufacturer_data(const ServiceData &data)
void set_scan_continuous(bool scan_continuous)
std::vector< ESPBTDeviceListener * > listeners_
std::vector< ESPBTClient * > clients_
Client parameters.
Helper class to easily give an object a parent of type T.
Definition: helpers.h:522
const optional< uint8_t > & get_ad_flag() const