Skip to content

Commit 80b5ff3

Browse files
committed
Refactoring and various optimizations
1 parent f6c837e commit 80b5ff3

File tree

10 files changed

+121
-108
lines changed

10 files changed

+121
-108
lines changed

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BLE Gateway component will allow you to forward BLE Advertising data packets for
3333
If the heart of your Home Automation system is Home Assistant or another similar system and you use [ESPHome](https://esphome.io) devices to extend BLE coverage and process data from BLE sensors, you can dramatically decrease system complexity by remove all BLE data processing from ESPHome devices and forward raw BLE Advertising data to external components like [Passive BLE Monitor](https://github.com/custom-components/ble_monitor).
3434

3535
**Important note:** Currently in order to run BLE Gateway you need to make [some changes](https://github.com/esphome/esphome/pull/2854) in ESPHome `esp32_ble_tracker` component, I make PR and hopefully it will be accepted.
36-
[Passive BLE Monitor](https://github.com/custom-components/ble_monitor) integration already has required support, thanks to [@Ernst79](https://github.com/Ernst79), please update it to version 6.2 or latest.
36+
[Passive BLE Monitor](https://github.com/custom-components/ble_monitor) integration already has required support, thanks to [@Ernst79](https://github.com/Ernst79), please update it to version 6.2 or later.
3737

3838
#### ESPHome configuration example
3939
Note: This example use [event](https://esphome.io/components/api.html#homeassistant-event-action), you can use direct `ble_monitor.parse_data` [service call](https://esphome.io/components/api.html#homeassistant-service-action)
@@ -44,10 +44,10 @@ ble_gateway:
4444
- mac_address: !secret lywsd03mmc_mac
4545
on_ble_advertise:
4646
then:
47-
- homeassistant.event:
48-
event: esphome.on_ble_advertise
49-
data:
50-
packet: !lambda return packet;
47+
homeassistant.event:
48+
event: esphome.on_ble_advertise
49+
data:
50+
packet: !lambda return packet;
5151
```
5252

5353
#### Home Assistant Passive BLE Monitor configuration example
@@ -86,10 +86,10 @@ ble_gateway:
8686
id: blegateway
8787
on_ble_advertise:
8888
then:
89-
- homeassistant.event:
90-
event: esphome.on_ble_advertise
91-
data:
92-
packet: !lambda return packet;
89+
homeassistant.event:
90+
event: esphome.on_ble_advertise
91+
data:
92+
packet: !lambda return packet;
9393
9494
text_sensor:
9595
- platform: homeassistant
@@ -98,7 +98,7 @@ text_sensor:
9898
attribute: devices
9999
on_value:
100100
then:
101-
- lambda: id(blegateway).set_devices(x);
101+
lambda: id(blegateway).set_devices(x);
102102
103103
# Home Assistant
104104
input_boolean:

components/ble_gateway/ble_gateway.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#ifdef USE_ESP32
2+
13
#include "ble_gateway.h"
24
#include "esphome/core/log.h"
35

4-
#ifdef USE_ESP32
5-
66
namespace esphome {
77
namespace ble_gateway {
88

components/ble_gateway/ble_gateway.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#pragma once
22

3-
#include "esphome/core/component.h"
4-
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
5-
63
#ifdef USE_ESP32
74

5+
#include "esphome/core/component.h"
6+
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
87
#include "esphome/core/automation.h"
98

109
namespace esphome {
@@ -23,7 +22,7 @@ class BLEGateway : public Component, public esp32_ble_tracker::ESPBTDeviceListen
2322
void set_devices(std::string devices);
2423
protected:
2524
std::vector<uint64_t> devices_{};
26-
CallbackManager<void(const esp32_ble_tracker::ESPBTDevice &, std::string)> callback_;
25+
CallbackManager<void(const esp32_ble_tracker::ESPBTDevice &, std::string)> callback_{};
2726
};
2827

2928
class BLEGatewayBLEAdvertiseTrigger : public Trigger<const esp32_ble_tracker::ESPBTDevice &, std::string> {

components/myhomeiot_ble_client/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
MyHomeIOT_BLEClient = myhomeiot_ble_client_ns.class_(
2222
"MyHomeIOT_BLEClient", cg.Component
2323
)
24+
MyHomeIOT_BLEClientConstRef = MyHomeIOT_BLEClient.operator("ref").operator("const")
2425

2526
# Triggers
2627
MyHomeIOT_BLEClientValueTrigger = myhomeiot_ble_client_ns.class_(
@@ -76,4 +77,4 @@ async def to_code(config):
7677

7778
for conf in config.get(CONF_ON_VALUE, []):
7879
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
79-
await automation.build_automation(trigger, [(cg.std_vector.template(cg.uint8), "x")], conf)
80+
await automation.build_automation(trigger, [(cg.std_vector.template(cg.uint8), "x"), (MyHomeIOT_BLEClientConstRef, "xthis")], conf)

components/myhomeiot_ble_client/automation.h

-21
This file was deleted.

components/myhomeiot_ble_client/myhomeiot_ble_client.cpp

+49-50
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#include "myhomeiot_ble_client.h"
2-
#include "esphome/core/log.h"
3-
41
#ifdef ARDUINO_ARCH_ESP32
52

63
#include <esp_gap_ble_api.h>
4+
#include "esphome/core/log.h"
5+
#include "myhomeiot_ble_client.h"
76

87
namespace esphome {
98
namespace myhomeiot_ble_client {
@@ -16,7 +15,7 @@ void MyHomeIOT_BLEClient::setup() {
1615

1716
void MyHomeIOT_BLEClient::dump_config() {
1817
ESP_LOGCONFIG(TAG, "MyHomeIOT BLE Client");
19-
ESP_LOGCONFIG(TAG, " MAC address: %s", to_string(this->address).c_str());
18+
ESP_LOGCONFIG(TAG, " MAC address: %s", to_string(this->address_).c_str());
2019
ESP_LOGCONFIG(TAG, " Service UUID: %s", this->service_uuid_.to_string().c_str());
2120
ESP_LOGCONFIG(TAG, " Characteristic UUID: %s", this->char_uuid_.to_string().c_str());
2221
LOG_UPDATE_INTERVAL(this);
@@ -30,31 +29,31 @@ void MyHomeIOT_BLEClient::loop() {
3029
}
3130

3231
void MyHomeIOT_BLEClient::connect() {
33-
ESP_LOGI(TAG, "[%s] Connecting", to_string(this->address).c_str());
32+
ESP_LOGI(TAG, "[%s] Connecting", to_string(this->address_).c_str());
3433
this->state_ = MYHOMEIOT_CONNECTING;
35-
if (auto status = esp_ble_gattc_open(ble_host_->gattc_if, this->remote_bda, BLE_ADDR_TYPE_PUBLIC, true))
34+
if (auto status = esp_ble_gattc_open(ble_host_->gattc_if, this->remote_bda_, BLE_ADDR_TYPE_PUBLIC, true))
3635
{
37-
ESP_LOGW(TAG, "[%s] open error, status (%d)", to_string(this->address).c_str(), status);
36+
ESP_LOGW(TAG, "[%s] open error, status (%d)", to_string(this->address_).c_str(), status);
3837
report_error(MYHOMEIOT_IDLE);
3938
}
4039
}
4140

4241
void MyHomeIOT_BLEClient::disconnect() {
43-
ESP_LOGI(TAG, "[%s] Disconnecting", to_string(this->address).c_str());
42+
ESP_LOGI(TAG, "[%s] Disconnecting", to_string(this->address_).c_str());
4443
this->state_ = MYHOMEIOT_IDLE;
45-
if (auto status = esp_ble_gattc_close(ble_host_->gattc_if, this->conn_id))
46-
ESP_LOGW(TAG, "[%s] close error, status (%d)", to_string(this->address).c_str(), status);
44+
if (auto status = esp_ble_gattc_close(ble_host_->gattc_if, this->conn_id_))
45+
ESP_LOGW(TAG, "[%s] close error, status (%d)", to_string(this->address_).c_str(), status);
4746
}
4847

4948
void MyHomeIOT_BLEClient::update() {
50-
this->is_update_requested = true;
49+
this->is_update_requested_ = true;
5150
}
5251

5352
void MyHomeIOT_BLEClient::report_results(uint8_t *data, uint16_t len) {
5453
this->status_clear_warning();
55-
std::vector<uint8_t> ret(data, data + len);
56-
this->callback_.call(ret);
57-
this->is_update_requested = false;
54+
std::vector<uint8_t> value(data, data + len);
55+
this->callback_.call(value, *this);
56+
this->is_update_requested_ = false;
5857
}
5958

6059
void MyHomeIOT_BLEClient::report_error(esp32_ble_tracker::ClientState state) {
@@ -63,12 +62,12 @@ void MyHomeIOT_BLEClient::report_error(esp32_ble_tracker::ClientState state) {
6362
}
6463

6564
bool MyHomeIOT_BLEClient::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
66-
if (!this->is_update_requested || this->state_ != MYHOMEIOT_IDLE
67-
|| device.address_uint64() != this->address)
65+
if (!this->is_update_requested_ || this->state_ != MYHOMEIOT_IDLE
66+
|| device.address_uint64() != this->address_)
6867
return false;
6968

7069
ESP_LOGD(TAG, "[%s] Found device", device.address_str().c_str());
71-
memcpy(this->remote_bda, device.address(), sizeof(this->remote_bda));
70+
memcpy(this->remote_bda_, device.address(), sizeof(this->remote_bda_));
7271
this->state_ = MYHOMEIOT_DISCOVERED;
7372
return true;
7473
}
@@ -77,71 +76,71 @@ void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
7776
esp_ble_gattc_cb_param_t *param) {
7877
switch (event) {
7978
case ESP_GATTC_OPEN_EVT: {
80-
if (memcmp(param->open.remote_bda, this->remote_bda, sizeof(this->remote_bda)) != 0)
79+
if (memcmp(param->open.remote_bda, this->remote_bda_, sizeof(this->remote_bda_)) != 0)
8180
break;
8281
if (param->open.status != ESP_GATT_OK) {
83-
ESP_LOGW(TAG, "[%s] OPEN_EVT failed, status (%d), app_id (%d)", to_string(this->address).c_str(),
82+
ESP_LOGW(TAG, "[%s] OPEN_EVT failed, status (%d), app_id (%d)", to_string(this->address_).c_str(),
8483
param->open.status, ble_host_->app_id);
8584
report_error(MYHOMEIOT_IDLE);
8685
break;
8786
}
88-
ESP_LOGI(TAG, "[%s] Connected successfully, app_id (%d)", to_string(this->address).c_str(), ble_host_->app_id);
89-
this->conn_id = param->open.conn_id;
87+
ESP_LOGI(TAG, "[%s] Connected successfully, app_id (%d)", to_string(this->address_).c_str(), ble_host_->app_id);
88+
this->conn_id_ = param->open.conn_id;
9089
if (auto status = esp_ble_gattc_send_mtu_req(ble_host_->gattc_if, param->open.conn_id))
9190
{
92-
ESP_LOGW(TAG, "[%s] send_mtu_req failed, status (%d)", to_string(this->address).c_str(), status);
91+
ESP_LOGW(TAG, "[%s] send_mtu_req failed, status (%d)", to_string(this->address_).c_str(), status);
9392
report_error();
9493
break;
9594
}
96-
this->start_handle = this->end_handle = this->char_handle = ESP_GATT_ILLEGAL_HANDLE;
95+
this->start_handle_ = this->end_handle_ = this->char_handle_ = ESP_GATT_ILLEGAL_HANDLE;
9796
this->state_ = MYHOMEIOT_CONNECTED;
9897
break;
9998
}
10099
case ESP_GATTC_CFG_MTU_EVT: {
101-
if (param->cfg_mtu.conn_id != this->conn_id)
100+
if (param->cfg_mtu.conn_id != this->conn_id_)
102101
break;
103102
if (param->cfg_mtu.status != ESP_GATT_OK) {
104-
ESP_LOGW(TAG, "[%s] CFG_MTU_EVT failed, status (%d)", to_string(this->address).c_str(),
103+
ESP_LOGW(TAG, "[%s] CFG_MTU_EVT failed, status (%d)", to_string(this->address_).c_str(),
105104
param->cfg_mtu.status);
106105
report_error();
107106
break;
108107
}
109-
ESP_LOGV(TAG, "[%s] CFG_MTU_EVT, MTU (%d)", to_string(this->address).c_str(), param->cfg_mtu.mtu);
108+
ESP_LOGV(TAG, "[%s] CFG_MTU_EVT, MTU (%d)", to_string(this->address_).c_str(), param->cfg_mtu.mtu);
110109
if (auto status = esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id, nullptr)) {
111-
ESP_LOGW(TAG, "[%s] search_service failed, status (%d)", to_string(this->address).c_str(), status);
110+
ESP_LOGW(TAG, "[%s] search_service failed, status (%d)", to_string(this->address_).c_str(), status);
112111
report_error();
113112
}
114113
break;
115114
}
116115
case ESP_GATTC_DISCONNECT_EVT: {
117-
if (memcmp(param->disconnect.remote_bda, this->remote_bda, sizeof(this->remote_bda)) != 0)
116+
if (memcmp(param->disconnect.remote_bda, this->remote_bda_, sizeof(this->remote_bda_)) != 0)
118117
break;
119-
ESP_LOGD(TAG, "[%s] DISCONNECT_EVT", to_string(this->address).c_str());
118+
ESP_LOGD(TAG, "[%s] DISCONNECT_EVT", to_string(this->address_).c_str());
120119
this->state_ = MYHOMEIOT_IDLE;
121120
break;
122121
}
123122
case ESP_GATTC_SEARCH_RES_EVT: {
124-
if (param->search_res.conn_id != this->conn_id)
123+
if (param->search_res.conn_id != this->conn_id_)
125124
break;
126125
esp32_ble_tracker::ESPBTUUID uuid = param->search_res.srvc_id.uuid.len == ESP_UUID_LEN_16 ? esp32_ble_tracker::ESPBTUUID::from_uint16(param->search_res.srvc_id.uuid.uuid.uuid16)
127126
: param->search_res.srvc_id.uuid.len == ESP_UUID_LEN_32 ? esp32_ble_tracker::ESPBTUUID::from_uint32(param->search_res.srvc_id.uuid.uuid.uuid32)
128127
: esp32_ble_tracker::ESPBTUUID::from_raw(param->search_res.srvc_id.uuid.uuid.uuid128);
129128
if (uuid == this->service_uuid_) {
130-
ESP_LOGD(TAG, "[%s] SEARCH_RES_EVT service (%s) found", to_string(this->address).c_str(),
129+
ESP_LOGD(TAG, "[%s] SEARCH_RES_EVT service (%s) found", to_string(this->address_).c_str(),
131130
this->service_uuid_.to_string().c_str());
132-
start_handle = param->search_res.start_handle;
133-
end_handle = param->search_res.end_handle;
131+
this->start_handle_ = param->search_res.start_handle;
132+
this->end_handle_ = param->search_res.end_handle;
134133
}
135134
break;
136135
}
137136
case ESP_GATTC_SEARCH_CMPL_EVT: {
138-
if (param->search_cmpl.conn_id != this->conn_id)
137+
if (param->search_cmpl.conn_id != this->conn_id_)
139138
break;
140-
ESP_LOGV(TAG, "[%s] SEARCH_CMPL_EVT", to_string(this->address).c_str());
139+
ESP_LOGV(TAG, "[%s] SEARCH_CMPL_EVT", to_string(this->address_).c_str());
141140

142-
if (this->start_handle == ESP_GATT_ILLEGAL_HANDLE)
141+
if (this->start_handle_ == ESP_GATT_ILLEGAL_HANDLE)
143142
{
144-
ESP_LOGE(TAG, "[%s] SEARCH_CMPL_EVT service (%s) not found", to_string(this->address).c_str(),
143+
ESP_LOGE(TAG, "[%s] SEARCH_CMPL_EVT service (%s) not found", to_string(this->address_).c_str(),
145144
this->service_uuid_.to_string().c_str());
146145
report_error();
147146
break;
@@ -151,47 +150,47 @@ void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
151150
esp_gattc_char_elem_t result;
152151
while (true) {
153152
uint16_t count = 1;
154-
auto status = esp_ble_gattc_get_all_char(ble_host_->gattc_if, this->conn_id,
155-
this->start_handle, this->end_handle, &result, &count, offset);
153+
auto status = esp_ble_gattc_get_all_char(ble_host_->gattc_if, this->conn_id_,
154+
this->start_handle_, this->end_handle_, &result, &count, offset);
156155
if (status != ESP_GATT_OK) {
157156
if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND)
158157
break;
159-
ESP_LOGW(TAG, "[%s] get_all_char error, status (%d)", to_string(this->address).c_str(), status);
158+
ESP_LOGW(TAG, "[%s] get_all_char error, status (%d)", to_string(this->address_).c_str(), status);
160159
report_error();
161160
break;
162161
}
163162
if (count == 0)
164163
break;
165164

166165
if (this->char_uuid_ == esp32_ble_tracker::ESPBTUUID::from_uuid(result.uuid)) {
167-
ESP_LOGD(TAG, "[%s] SEARCH_CMPL_EVT char (%s) found", to_string(this->address).c_str(),
166+
ESP_LOGD(TAG, "[%s] SEARCH_CMPL_EVT char (%s) found", to_string(this->address_).c_str(),
168167
this->char_uuid_.to_string().c_str());
169-
this->char_handle = result.char_handle;
168+
this->char_handle_ = result.char_handle;
170169

171-
if (auto status = esp_ble_gattc_read_char(ble_host_->gattc_if, this->conn_id,
172-
this->char_handle, ESP_GATT_AUTH_REQ_NONE) != ESP_GATT_OK) {
170+
if (auto status = esp_ble_gattc_read_char(ble_host_->gattc_if, this->conn_id_,
171+
this->char_handle_, ESP_GATT_AUTH_REQ_NONE) != ESP_GATT_OK) {
173172
ESP_LOGW(TAG, "[%s] read_char error sending read request, status (%d)",
174-
to_string(this->address).c_str(), status);
175-
this->char_handle = ESP_GATT_ILLEGAL_HANDLE;
173+
to_string(this->address_).c_str(), status);
174+
this->char_handle_ = ESP_GATT_ILLEGAL_HANDLE;
176175
}
177176
break;
178177
}
179178
offset++;
180179
}
181-
if (this->char_handle == ESP_GATT_ILLEGAL_HANDLE)
180+
if (this->char_handle_ == ESP_GATT_ILLEGAL_HANDLE)
182181
{
183-
ESP_LOGE(TAG, "[%s] SEARCH_CMPL_EVT char (%s) not found", to_string(this->address).c_str(),
182+
ESP_LOGE(TAG, "[%s] SEARCH_CMPL_EVT char (%s) not found", to_string(this->address_).c_str(),
184183
this->char_uuid_.to_string().c_str());
185184
report_error();
186185
}
187186
break;
188187
}
189188
case ESP_GATTC_READ_CHAR_EVT: {
190-
if (param->read.conn_id != this->conn_id || param->read.handle != this->char_handle)
189+
if (param->read.conn_id != this->conn_id_ || param->read.handle != this->char_handle_)
191190
break;
192191
if (param->read.status != ESP_GATT_OK)
193192
{
194-
ESP_LOGW(TAG, "[%s] READ_CHAR_EVT error reading char at handle (%d), status (%d)", to_string(this->address).c_str(),
193+
ESP_LOGW(TAG, "[%s] READ_CHAR_EVT error reading char at handle (%d), status (%d)", to_string(this->address_).c_str(),
195194
param->read.handle, param->read.status);
196195
report_error();
197196
break;

0 commit comments

Comments
 (0)