1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-24 14:12:24 +01:00

Batch 3 services

This commit is contained in:
J. Nick Koston
2025-07-30 14:33:11 -10:00
parent 4b7f3355ea
commit a4ebcc691a
5 changed files with 113 additions and 93 deletions

View File

@@ -1505,7 +1505,7 @@ message BluetoothGATTGetServicesResponse {
option (ifdef) = "USE_BLUETOOTH_PROXY"; option (ifdef) = "USE_BLUETOOTH_PROXY";
uint64 address = 1; uint64 address = 1;
repeated BluetoothGATTService services = 2 [(fixed_array_size) = 1]; repeated BluetoothGATTService services = 2;
} }
message BluetoothGATTGetServicesDoneResponse { message BluetoothGATTGetServicesDoneResponse {

View File

@@ -1929,11 +1929,13 @@ void BluetoothGATTService::calculate_size(ProtoSize &size) const {
} }
void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const { void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint64(1, this->address); buffer.encode_uint64(1, this->address);
buffer.encode_message(2, this->services[0], true); for (auto &it : this->services) {
buffer.encode_message(2, it, true);
}
} }
void BluetoothGATTGetServicesResponse::calculate_size(ProtoSize &size) const { void BluetoothGATTGetServicesResponse::calculate_size(ProtoSize &size) const {
size.add_uint64(1, this->address); size.add_uint64(1, this->address);
size.add_message_object_force(1, this->services[0]); size.add_repeated_message(1, this->services);
} }
void BluetoothGATTGetServicesDoneResponse::encode(ProtoWriteBuffer buffer) const { void BluetoothGATTGetServicesDoneResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint64(1, this->address); buffer.encode_uint64(1, this->address);

View File

@@ -1895,12 +1895,12 @@ class BluetoothGATTService : public ProtoMessage {
class BluetoothGATTGetServicesResponse : public ProtoMessage { class BluetoothGATTGetServicesResponse : public ProtoMessage {
public: public:
static constexpr uint8_t MESSAGE_TYPE = 71; static constexpr uint8_t MESSAGE_TYPE = 71;
static constexpr uint8_t ESTIMATED_SIZE = 21; static constexpr uint8_t ESTIMATED_SIZE = 38;
#ifdef HAS_PROTO_MESSAGE_DUMP #ifdef HAS_PROTO_MESSAGE_DUMP
const char *message_name() const override { return "bluetooth_gatt_get_services_response"; } const char *message_name() const override { return "bluetooth_gatt_get_services_response"; }
#endif #endif
uint64_t address{0}; uint64_t address{0};
std::array<BluetoothGATTService, 1> services{}; std::vector<BluetoothGATTService> services{};
void encode(ProtoWriteBuffer buffer) const override; void encode(ProtoWriteBuffer buffer) const override;
void calculate_size(ProtoSize &size) const override; void calculate_size(ProtoSize &size) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP #ifdef HAS_PROTO_MESSAGE_DUMP

View File

@@ -57,7 +57,7 @@ void BluetoothConnection::reset_connection_(esp_err_t reason) {
} }
void BluetoothConnection::send_service_for_discovery_() { void BluetoothConnection::send_service_for_discovery_() {
if (this->send_service_ == this->service_count_) { if (this->send_service_ >= this->service_count_) {
this->send_service_ = DONE_SENDING_SERVICES; this->send_service_ = DONE_SENDING_SERVICES;
this->proxy_->send_gatt_services_done(this->address_); this->proxy_->send_gatt_services_done(this->address_);
if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE || if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE ||
@@ -73,23 +73,35 @@ void BluetoothConnection::send_service_for_discovery_() {
return; return;
} }
// Send next service // Prepare response for up to 3 services
api::BluetoothGATTGetServicesResponse resp;
resp.address = this->address_;
// Process up to 3 services in this iteration
static constexpr int MAX_SERVICES_PER_BATCH = 3;
int services_to_process = std::min(MAX_SERVICES_PER_BATCH, this->service_count_ - this->send_service_);
resp.services.reserve(services_to_process);
for (int service_idx = 0; service_idx < services_to_process; service_idx++) {
esp_gattc_service_elem_t service_result; esp_gattc_service_elem_t service_result;
uint16_t service_count = 1; uint16_t service_count = 1;
esp_gatt_status_t service_status = esp_ble_gattc_get_service(this->gattc_if_, this->conn_id_, nullptr, esp_gatt_status_t service_status = esp_ble_gattc_get_service(this->gattc_if_, this->conn_id_, nullptr,
&service_result, &service_count, this->send_service_); &service_result, &service_count, this->send_service_);
this->send_service_++;
if (service_status != ESP_GATT_OK || service_count == 0) { if (service_status != ESP_GATT_OK || service_count == 0) {
ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_service %s, status=%d, service_count=%d, offset=%d", ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_service %s, status=%d, service_count=%d, offset=%d",
this->connection_index_, this->address_str().c_str(), service_status != ESP_GATT_OK ? "error" : "missing", this->connection_index_, this->address_str().c_str(),
service_status, service_count, this->send_service_ - 1); service_status != ESP_GATT_OK ? "error" : "missing", service_status, service_count, this->send_service_);
// If first service fails, return. If second fails, send what we have.
if (services_processed == 0) {
return; return;
} }
break;
}
api::BluetoothGATTGetServicesResponse resp; this->send_service_++;
resp.address = this->address_; resp.services.emplace_back();
auto &service_resp = resp.services[0]; auto &service_resp = resp.services.back();
fill_128bit_uuid_array(service_resp.uuid, service_result.uuid); fill_128bit_uuid_array(service_resp.uuid, service_result.uuid);
service_resp.handle = service_result.start_handle; service_resp.handle = service_result.start_handle;
@@ -106,9 +118,9 @@ void BluetoothConnection::send_service_for_discovery_() {
} }
if (total_char_count == 0) { if (total_char_count == 0) {
// No characteristics, just send the service response // No characteristics, continue to next service
api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE); services_processed++;
return; continue;
} }
// Reserve space and process characteristics // Reserve space and process characteristics
@@ -182,9 +194,14 @@ void BluetoothConnection::send_service_for_discovery_() {
} }
} }
// Send the message (we already checked api_conn is not null at the beginning) services_processed++;
}
// Send the message with 1-3 services
if (services_processed > 0) {
api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE); api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE);
} }
}
bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
esp_ble_gattc_cb_param_t *param) { esp_ble_gattc_cb_param_t *param) {

View File

@@ -22,6 +22,7 @@ namespace esphome::bluetooth_proxy {
static const esp_err_t ESP_GATT_NOT_CONNECTED = -1; static const esp_err_t ESP_GATT_NOT_CONNECTED = -1;
static const int DONE_SENDING_SERVICES = -2; static const int DONE_SENDING_SERVICES = -2;
static const uint8_t MAX_SERVICES_PER_BATCH = 3;
using namespace esp32_ble_client; using namespace esp32_ble_client;