From e42db6015ab923f4190fd31ba7b131909aa62ee3 Mon Sep 17 00:00:00 2001 From: myhomeiot Date: Sun, 20 Nov 2022 14:55:46 +0200 Subject: [PATCH] BLE Host and BLE Client: Added support for ESPHome 2022.11.x and older --- .../myhomeiot_ble_client.cpp | 7 ++--- .../myhomeiot_ble_client.h | 4 +-- .../myhomeiot_ble_host/myhomeiot_ble_host.cpp | 26 ++++++++++++++----- .../myhomeiot_ble_host/myhomeiot_ble_host.h | 11 ++++++-- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/components/myhomeiot_ble_client/myhomeiot_ble_client.cpp b/components/myhomeiot_ble_client/myhomeiot_ble_client.cpp index 6229bcc..e69d469 100644 --- a/components/myhomeiot_ble_client/myhomeiot_ble_client.cpp +++ b/components/myhomeiot_ble_client/myhomeiot_ble_client.cpp @@ -1,4 +1,4 @@ -#ifdef ARDUINO_ARCH_ESP32 +#ifdef USE_ESP32 #include #include "esphome/core/log.h" @@ -72,7 +72,7 @@ bool MyHomeIOT_BLEClient::parse_device(const esp32_ble_tracker::ESPBTDevice &dev return true; } -void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if, +bool MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if, esp_ble_gattc_cb_param_t *param) { switch (event) { case ESP_GATTC_OPEN_EVT: { @@ -114,7 +114,7 @@ void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga } case ESP_GATTC_DISCONNECT_EVT: { if (memcmp(param->disconnect.remote_bda, this->remote_bda_, sizeof(this->remote_bda_)) != 0) - break; + return false; ESP_LOGD(TAG, "[%s] DISCONNECT_EVT", to_string(this->address_).c_str()); this->state_ = MYHOMEIOT_IDLE; break; @@ -203,6 +203,7 @@ void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga default: break; } + return true; } std::string MyHomeIOT_BLEClient::to_string(uint64_t address) const { diff --git a/components/myhomeiot_ble_client/myhomeiot_ble_client.h b/components/myhomeiot_ble_client/myhomeiot_ble_client.h index 59edc6c..b3b811a 100644 --- a/components/myhomeiot_ble_client/myhomeiot_ble_client.h +++ b/components/myhomeiot_ble_client/myhomeiot_ble_client.h @@ -1,6 +1,6 @@ #pragma once -#ifdef ARDUINO_ARCH_ESP32 +#ifdef USE_ESP32 #include "esphome/core/component.h" #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" @@ -18,7 +18,7 @@ class MyHomeIOT_BLEClient : public PollingComponent, public myhomeiot_ble_host:: void add_on_state_callback(std::function, const MyHomeIOT_BLEClient &)> &&callback) { this->callback_.add(std::move(callback)); } bool parse_device(const esp32_ble_tracker::ESPBTDevice &device); - void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param); + bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param); void set_address(uint64_t address) { this->address_ = address; } diff --git a/components/myhomeiot_ble_host/myhomeiot_ble_host.cpp b/components/myhomeiot_ble_host/myhomeiot_ble_host.cpp index 3354cd8..8381922 100644 --- a/components/myhomeiot_ble_host/myhomeiot_ble_host.cpp +++ b/components/myhomeiot_ble_host/myhomeiot_ble_host.cpp @@ -1,4 +1,4 @@ -#ifdef ARDUINO_ARCH_ESP32 +#ifdef USE_ESP32 #include #include "esphome/core/log.h" @@ -46,13 +46,26 @@ bool MyHomeIOT_BLEHost::parse_device(const esp32_ble_tracker::ESPBTDevice &devic return false; } -void MyHomeIOT_BLEHost::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if, +#if ESPHOME_VERSION_CODE >= VERSION_CODE(2022, 11, 0) +bool MyHomeIOT_BLEHost::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, + esp_ble_gattc_cb_param_t *param) { + return gattc_event_handler_internal(event, gattc_if, param); +} +#else +void MyHomeIOT_BLEHost::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, + esp_ble_gattc_cb_param_t *param) { + gattc_event_handler_internal(event, gattc_if, param); +} +#endif + +bool MyHomeIOT_BLEHost::gattc_event_handler_internal(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if, esp_ble_gattc_cb_param_t *param) { if (event == ESP_GATTC_REG_EVT && this->app_id != param->reg.app_id) - return; - if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && gattc_if != this->gattc_if) - return; + return false; + if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->gattc_if) + return false; + bool result = true; switch (event) { case ESP_GATTC_REG_EVT: { if (param->reg.status == ESP_GATT_OK) { @@ -66,11 +79,12 @@ void MyHomeIOT_BLEHost::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt default: if (current) { - current->gattc_event_handler(event, esp_gattc_if, param); + result = current->gattc_event_handler(event, esp_gattc_if, param); this->set_state(current->state()); } break; } + return result; } } // namespace myhomeiot_ble_host diff --git a/components/myhomeiot_ble_host/myhomeiot_ble_host.h b/components/myhomeiot_ble_host/myhomeiot_ble_host.h index 0ae8cb0..f0ad6c8 100644 --- a/components/myhomeiot_ble_host/myhomeiot_ble_host.h +++ b/components/myhomeiot_ble_host/myhomeiot_ble_host.h @@ -1,6 +1,6 @@ #pragma once -#ifdef ARDUINO_ARCH_ESP32 +#ifdef USE_ESP32 #include "esphome/core/component.h" #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" @@ -33,7 +33,7 @@ class MyHomeIOT_BLEHost; class MyHomeIOT_BLEClientNode { public: virtual bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) = 0; - 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; + virtual bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) = 0; virtual void loop() = 0; MyHomeIOT_BLEHost *host() { return this->ble_host_; } @@ -59,8 +59,15 @@ class MyHomeIOT_BLEHost : public Component, public esp32_ble_tracker::ESPBTClien int gattc_if; bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override; +#if ESPHOME_VERSION_CODE >= VERSION_CODE(2022, 11, 0) + bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, + esp_ble_gattc_cb_param_t *param) override; +#else void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override; +#endif + bool gattc_event_handler_internal(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, + esp_ble_gattc_cb_param_t *param); virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {} void on_scan_end() override {}