ESPHome  2022.9.1
esp32_ble_tracker.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
6 #include "queue.h"
7 
8 #ifdef USE_ESP32
9 
10 #include <string>
11 #include <array>
12 #include <esp_gap_ble_api.h>
13 #include <esp_gattc_api.h>
14 #include <esp_bt_defs.h>
15 
16 namespace esphome {
17 namespace esp32_ble_tracker {
18 
19 class ESPBTUUID {
20  public:
21  ESPBTUUID();
22 
23  static ESPBTUUID from_uint16(uint16_t uuid);
24 
25  static ESPBTUUID from_uint32(uint32_t uuid);
26 
27  static ESPBTUUID from_raw(const uint8_t *data);
28 
29  static ESPBTUUID from_raw(const std::string &data);
30 
31  static ESPBTUUID from_uuid(esp_bt_uuid_t uuid);
32 
33  ESPBTUUID as_128bit() const;
34 
35  bool contains(uint8_t data1, uint8_t data2) const;
36 
37  bool operator==(const ESPBTUUID &uuid) const;
38  bool operator!=(const ESPBTUUID &uuid) const { return !(*this == uuid); }
39 
40  esp_bt_uuid_t get_uuid() const;
41 
42  std::string to_string() const;
43 
44  protected:
45  esp_bt_uuid_t uuid_;
46 };
47 
48 using adv_data_t = std::vector<uint8_t>;
49 
50 struct ServiceData {
53 };
54 
56  public:
57  ESPBLEiBeacon() { memset(&this->beacon_data_, 0, sizeof(this->beacon_data_)); }
58  ESPBLEiBeacon(const uint8_t *data);
59  static optional<ESPBLEiBeacon> from_manufacturer_data(const ServiceData &data);
60 
61  uint16_t get_major() { return ((this->beacon_data_.major & 0xFF) << 8) | (this->beacon_data_.major >> 8); }
62  uint16_t get_minor() { return ((this->beacon_data_.minor & 0xFF) << 8) | (this->beacon_data_.minor >> 8); }
63  int8_t get_signal_power() { return this->beacon_data_.signal_power; }
64  ESPBTUUID get_uuid() { return ESPBTUUID::from_raw(this->beacon_data_.proximity_uuid); }
65 
66  protected:
67  struct {
68  uint8_t sub_type;
69  uint8_t length;
70  uint8_t proximity_uuid[16];
71  uint16_t major;
72  uint16_t minor;
73  int8_t signal_power;
74  } PACKED beacon_data_;
75 };
76 
77 class ESPBTDevice {
78  public:
79  void parse_scan_rst(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &param);
80 
81  std::string address_str() const;
82 
83  uint64_t address_uint64() const;
84 
85  const uint8_t *address() const { return address_; }
86 
87  esp_ble_addr_type_t get_address_type() const { return this->address_type_; }
88  int get_rssi() const { return rssi_; }
89  const std::string &get_name() const { return this->name_; }
90 
91  const std::vector<int8_t> &get_tx_powers() const { return tx_powers_; }
92 
93  const optional<uint16_t> &get_appearance() const { return appearance_; }
94  const optional<uint8_t> &get_ad_flag() const { return ad_flag_; }
95  const std::vector<ESPBTUUID> &get_service_uuids() const { return service_uuids_; }
96 
97  const std::vector<ServiceData> &get_manufacturer_datas() const { return manufacturer_datas_; }
98 
99  const std::vector<ServiceData> &get_service_datas() const { return service_datas_; }
100 
101  const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &get_scan_result() const { return scan_result_; }
102 
104  for (auto &it : this->manufacturer_datas_) {
106  if (res.has_value())
107  return *res;
108  }
109  return {};
110  }
111 
112  protected:
113  void parse_adv_(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &param);
114 
115  esp_bd_addr_t address_{
116  0,
117  };
118  esp_ble_addr_type_t address_type_{BLE_ADDR_TYPE_PUBLIC};
119  int rssi_{0};
120  std::string name_{};
121  std::vector<int8_t> tx_powers_{};
122  optional<uint16_t> appearance_{};
123  optional<uint8_t> ad_flag_{};
124  std::vector<ESPBTUUID> service_uuids_;
125  std::vector<ServiceData> manufacturer_datas_{};
126  std::vector<ServiceData> service_datas_{};
127  esp_ble_gap_cb_param_t::ble_scan_result_evt_param scan_result_{};
128 };
129 
130 class ESP32BLETracker;
131 
133  public:
134  virtual void on_scan_end() {}
135  virtual bool parse_device(const ESPBTDevice &device) = 0;
136  void set_parent(ESP32BLETracker *parent) { parent_ = parent; }
137 
138  protected:
139  ESP32BLETracker *parent_{nullptr};
140 };
141 
142 enum class ClientState {
143  // Connection is idle, no device detected.
144  IDLE,
145  // Device advertisement found.
146  DISCOVERED,
147  // Connection in progress.
148  CONNECTING,
149  // Initial connection established.
150  CONNECTED,
151  // The client and sub-clients have completed setup.
152  ESTABLISHED,
153 };
154 
156  public:
157  virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
158  esp_ble_gattc_cb_param_t *param) = 0;
159  virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
160  virtual void connect() = 0;
161  void set_state(ClientState st) { this->state_ = st; }
162  ClientState state() const { return state_; }
163  int app_id;
164 
165  protected:
167 };
168 
169 class ESP32BLETracker : public Component {
170  public:
171  void set_scan_duration(uint32_t scan_duration) { scan_duration_ = scan_duration; }
172  void set_scan_interval(uint32_t scan_interval) { scan_interval_ = scan_interval; }
173  void set_scan_window(uint32_t scan_window) { scan_window_ = scan_window; }
174  void set_scan_active(bool scan_active) { scan_active_ = scan_active; }
175  void set_scan_continuous(bool scan_continuous) { scan_continuous_ = scan_continuous; }
176 
178  void setup() override;
179  void dump_config() override;
180  float get_setup_priority() const override;
181 
182  void loop() override;
183 
185  listener->set_parent(this);
186  this->listeners_.push_back(listener);
187  }
188 
189  void register_client(ESPBTClient *client);
190 
191  void print_bt_device_info(const ESPBTDevice &device);
192 
193  void start_scan();
194  void stop_scan();
195 
196  protected:
198  static bool ble_setup();
200  void start_scan_(bool first);
202  void end_of_scan_();
204  static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
205  void real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
207  void gap_scan_result_(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &param);
209  void gap_scan_set_param_complete_(const esp_ble_gap_cb_param_t::ble_scan_param_cmpl_evt_param &param);
211  void gap_scan_start_complete_(const esp_ble_gap_cb_param_t::ble_scan_start_cmpl_evt_param &param);
213  void gap_scan_stop_complete_(const esp_ble_gap_cb_param_t::ble_scan_stop_cmpl_evt_param &param);
214 
215  int app_id_;
217  static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
218  void real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
219 
221  std::vector<uint64_t> already_discovered_;
222  std::vector<ESPBTDeviceListener *> listeners_;
224  std::vector<ESPBTClient *> clients_;
226  esp_ble_scan_params_t scan_params_;
228  uint32_t scan_duration_;
229  uint32_t scan_interval_;
230  uint32_t scan_window_;
234  SemaphoreHandle_t scan_result_lock_;
235  SemaphoreHandle_t scan_end_lock_;
236  size_t scan_result_index_{0};
237  esp_ble_gap_cb_param_t::ble_scan_result_evt_param scan_result_buffer_[16];
238  esp_bt_status_t scan_start_failed_{ESP_BT_STATUS_SUCCESS};
239  esp_bt_status_t scan_set_param_failed_{ESP_BT_STATUS_SUCCESS};
240 
242 };
243 
244 // NOLINTNEXTLINE
246 
247 } // namespace esp32_ble_tracker
248 } // namespace esphome
249 
250 #endif
void setup()
optional< ESPBLEiBeacon > get_ibeacon() const
void loop()
struct esphome::sen5x::Sen5xBaselines PACKED
void register_listener(ESPBTDeviceListener *listener)
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.
static ESPBTUUID from_raw(const uint8_t *data)
ESP32BLETracker * global_esp32_ble_tracker
bool operator!=(const ESPBTUUID &uuid) const
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
bool contains(uint8_t data1, uint8_t data2) const
uint32_t scan_duration_
The interval in seconds to perform scans.
std::vector< uint8_t > adv_data_t
std::vector< uint64_t > already_discovered_
Vector of addresses that have already been printed in print_bt_device_info.
static ESPBTUUID from_uint16(uint16_t uuid)
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: a4988.cpp:4
void set_scan_duration(uint32_t scan_duration)
const std::string & get_name() const
void set_scan_interval(uint32_t scan_interval)
bool operator==(const ESPBTUUID &uuid) const
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.
const optional< uint8_t > & get_ad_flag() const
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)