2021-06-08 11:56:21 +12:00
|
|
|
#pragma once
|
|
|
|
|
2021-06-12 08:31:15 +12:00
|
|
|
#include "ble_advertising.h"
|
2023-11-06 03:54:39 +01:00
|
|
|
#include "ble_uuid.h"
|
2021-06-12 08:31:15 +12:00
|
|
|
|
2024-07-22 06:20:09 +02:00
|
|
|
#include <functional>
|
|
|
|
|
2023-11-06 03:54:39 +01:00
|
|
|
#include "esphome/core/automation.h"
|
2021-06-08 11:56:21 +12:00
|
|
|
#include "esphome/core/component.h"
|
2021-06-12 08:31:15 +12:00
|
|
|
#include "esphome/core/defines.h"
|
2021-06-08 11:56:21 +12:00
|
|
|
#include "esphome/core/helpers.h"
|
|
|
|
|
2023-01-25 14:36:30 +13:00
|
|
|
#include "ble_event.h"
|
2023-11-06 03:54:39 +01:00
|
|
|
#include "queue.h"
|
2021-06-12 08:31:15 +12:00
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ESP32
|
2021-06-08 11:56:21 +12:00
|
|
|
|
|
|
|
#include <esp_gap_ble_api.h>
|
|
|
|
#include <esp_gattc_api.h>
|
2023-11-06 03:54:39 +01:00
|
|
|
#include <esp_gatts_api.h>
|
2023-01-25 14:36:30 +13:00
|
|
|
|
2021-06-08 11:56:21 +12:00
|
|
|
namespace esphome {
|
|
|
|
namespace esp32_ble {
|
|
|
|
|
2023-06-09 07:41:09 +12:00
|
|
|
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address);
|
|
|
|
|
2021-09-24 18:02:28 +02:00
|
|
|
// NOLINTNEXTLINE(modernize-use-using)
|
2021-06-08 11:56:21 +12:00
|
|
|
typedef struct {
|
|
|
|
void *peer_device;
|
|
|
|
bool connected;
|
|
|
|
uint16_t mtu;
|
|
|
|
} conn_status_t;
|
|
|
|
|
2023-05-01 21:25:10 +00:00
|
|
|
enum IoCapability {
|
|
|
|
IO_CAP_OUT = ESP_IO_CAP_OUT,
|
|
|
|
IO_CAP_IO = ESP_IO_CAP_IO,
|
|
|
|
IO_CAP_IN = ESP_IO_CAP_IN,
|
|
|
|
IO_CAP_NONE = ESP_IO_CAP_NONE,
|
|
|
|
IO_CAP_KBDISP = ESP_IO_CAP_KBDISP,
|
|
|
|
};
|
|
|
|
|
2023-11-06 03:54:39 +01:00
|
|
|
enum BLEComponentState {
|
|
|
|
/** Nothing has been initialized yet. */
|
|
|
|
BLE_COMPONENT_STATE_OFF = 0,
|
|
|
|
/** BLE should be disabled on next loop. */
|
|
|
|
BLE_COMPONENT_STATE_DISABLE,
|
|
|
|
/** BLE is disabled. */
|
|
|
|
BLE_COMPONENT_STATE_DISABLED,
|
|
|
|
/** BLE should be enabled on next loop. */
|
|
|
|
BLE_COMPONENT_STATE_ENABLE,
|
|
|
|
/** BLE is active. */
|
|
|
|
BLE_COMPONENT_STATE_ACTIVE,
|
|
|
|
};
|
|
|
|
|
2023-01-25 14:36:30 +13:00
|
|
|
class GAPEventHandler {
|
|
|
|
public:
|
|
|
|
virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GATTcEventHandler {
|
|
|
|
public:
|
|
|
|
virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
|
|
|
|
esp_ble_gattc_cb_param_t *param) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GATTsEventHandler {
|
|
|
|
public:
|
|
|
|
virtual void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
|
|
|
|
esp_ble_gatts_cb_param_t *param) = 0;
|
|
|
|
};
|
|
|
|
|
2023-11-06 03:54:39 +01:00
|
|
|
class BLEStatusEventHandler {
|
|
|
|
public:
|
|
|
|
virtual void ble_before_disabled_event_handler() = 0;
|
|
|
|
};
|
|
|
|
|
2021-06-08 11:56:21 +12:00
|
|
|
class ESP32BLE : public Component {
|
|
|
|
public:
|
2023-05-01 21:25:10 +00:00
|
|
|
void set_io_capability(IoCapability io_capability) { this->io_cap_ = (esp_ble_io_cap_t) io_capability; }
|
|
|
|
|
2024-07-22 06:20:09 +02:00
|
|
|
void set_advertising_cycle_time(uint32_t advertising_cycle_time) {
|
|
|
|
this->advertising_cycle_time_ = advertising_cycle_time;
|
|
|
|
}
|
|
|
|
uint32_t get_advertising_cycle_time() const { return this->advertising_cycle_time_; }
|
|
|
|
|
2023-11-06 03:54:39 +01:00
|
|
|
void enable();
|
|
|
|
void disable();
|
|
|
|
bool is_active();
|
2021-06-08 11:56:21 +12:00
|
|
|
void setup() override;
|
2021-06-10 14:04:39 +12:00
|
|
|
void loop() override;
|
2021-06-08 11:56:21 +12:00
|
|
|
void dump_config() override;
|
|
|
|
float get_setup_priority() const override;
|
|
|
|
|
2023-11-06 03:54:39 +01:00
|
|
|
void advertising_start();
|
|
|
|
void advertising_set_service_data(const std::vector<uint8_t> &data);
|
|
|
|
void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
|
|
|
|
void advertising_add_service_uuid(ESPBTUUID uuid);
|
|
|
|
void advertising_remove_service_uuid(ESPBTUUID uuid);
|
2024-07-22 06:20:09 +02:00
|
|
|
void advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback);
|
2021-06-08 11:56:21 +12:00
|
|
|
|
2023-01-25 14:36:30 +13:00
|
|
|
void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
|
|
|
|
void register_gattc_event_handler(GATTcEventHandler *handler) { this->gattc_event_handlers_.push_back(handler); }
|
|
|
|
void register_gatts_event_handler(GATTsEventHandler *handler) { this->gatts_event_handlers_.push_back(handler); }
|
2023-11-06 03:54:39 +01:00
|
|
|
void register_ble_status_event_handler(BLEStatusEventHandler *handler) {
|
|
|
|
this->ble_status_event_handlers_.push_back(handler);
|
|
|
|
}
|
|
|
|
void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
|
2023-01-25 14:36:30 +13:00
|
|
|
|
2021-06-08 11:56:21 +12:00
|
|
|
protected:
|
|
|
|
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
|
|
|
|
static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
|
|
|
|
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
|
|
|
|
|
|
|
|
void real_gatts_event_handler_(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
|
|
|
|
void real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
|
|
|
|
void real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
|
|
|
|
|
2021-06-10 14:04:39 +12:00
|
|
|
bool ble_setup_();
|
2023-11-06 03:54:39 +01:00
|
|
|
bool ble_dismantle_();
|
|
|
|
bool ble_pre_setup_();
|
|
|
|
void advertising_init_();
|
2021-06-08 11:56:21 +12:00
|
|
|
|
2023-01-25 14:36:30 +13:00
|
|
|
std::vector<GAPEventHandler *> gap_event_handlers_;
|
|
|
|
std::vector<GATTcEventHandler *> gattc_event_handlers_;
|
|
|
|
std::vector<GATTsEventHandler *> gatts_event_handlers_;
|
2023-11-06 03:54:39 +01:00
|
|
|
std::vector<BLEStatusEventHandler *> ble_status_event_handlers_;
|
|
|
|
BLEComponentState state_{BLE_COMPONENT_STATE_OFF};
|
2023-01-25 14:36:30 +13:00
|
|
|
|
2021-06-08 11:56:21 +12:00
|
|
|
Queue<BLEEvent> ble_events_;
|
2021-06-12 08:31:15 +12:00
|
|
|
BLEAdvertising *advertising_;
|
2023-05-01 21:25:10 +00:00
|
|
|
esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
|
2024-07-22 06:20:09 +02:00
|
|
|
uint32_t advertising_cycle_time_;
|
2023-11-06 03:54:39 +01:00
|
|
|
bool enable_on_boot_;
|
2021-06-08 11:56:21 +12:00
|
|
|
};
|
|
|
|
|
2021-09-24 18:02:28 +02:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
2021-06-08 11:56:21 +12:00
|
|
|
extern ESP32BLE *global_ble;
|
|
|
|
|
2023-11-06 03:54:39 +01:00
|
|
|
template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
|
|
|
|
public:
|
|
|
|
bool check(Ts... x) override { return global_ble->is_active(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Ts> class BLEEnableAction : public Action<Ts...> {
|
|
|
|
public:
|
|
|
|
void play(Ts... x) override { global_ble->enable(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Ts> class BLEDisableAction : public Action<Ts...> {
|
|
|
|
public:
|
|
|
|
void play(Ts... x) override { global_ble->disable(); }
|
|
|
|
};
|
|
|
|
|
2021-06-08 11:56:21 +12:00
|
|
|
} // namespace esp32_ble
|
|
|
|
} // namespace esphome
|
|
|
|
|
|
|
|
#endif
|