Skip to content

Commit

Permalink
BLE Host and BLE Client: Added support for ESPHome 2022.11.x and older
Browse files Browse the repository at this point in the history
  • Loading branch information
myhomeiot committed Nov 20, 2022
1 parent a453a0e commit e42db60
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
7 changes: 4 additions & 3 deletions components/myhomeiot_ble_client/myhomeiot_ble_client.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef ARDUINO_ARCH_ESP32
#ifdef USE_ESP32

#include <esp_gap_ble_api.h>
#include "esphome/core/log.h"
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions components/myhomeiot_ble_client/myhomeiot_ble_client.h
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -18,7 +18,7 @@ class MyHomeIOT_BLEClient : public PollingComponent, public myhomeiot_ble_host::

void add_on_state_callback(std::function<void(std::vector<uint8_t>, 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; }

Expand Down
26 changes: 20 additions & 6 deletions components/myhomeiot_ble_host/myhomeiot_ble_host.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef ARDUINO_ARCH_ESP32
#ifdef USE_ESP32

#include <esp_gap_ble_api.h>
#include "esphome/core/log.h"
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions components/myhomeiot_ble_host/myhomeiot_ble_host.h
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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_; }
Expand All @@ -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 {}

Expand Down

0 comments on commit e42db60

Please sign in to comment.