From 8ceb1b9d60debe6b573aed8e3ae11b1a40f54a85 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 5 Aug 2025 19:49:20 -1000 Subject: [PATCH] [bluetooth_proxy] Reduce flash usage by consolidating duplicate logging (#10094) --- .../bluetooth_proxy/bluetooth_connection.cpp | 73 ++++++++++--------- .../bluetooth_proxy/bluetooth_connection.h | 4 + 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp index 01c2aa3d22..23b73127d9 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp @@ -185,8 +185,7 @@ void BluetoothConnection::send_service_for_discovery_() { service_result.start_handle, service_result.end_handle, 0, &total_char_count); if (char_count_status != ESP_GATT_OK) { - ESP_LOGE(TAG, "[%d] [%s] Error getting characteristic count, status=%d", this->connection_index_, - this->address_str().c_str(), char_count_status); + this->log_connection_error_("esp_ble_gattc_get_attr_count", char_count_status); this->send_service_ = DONE_SENDING_SERVICES; return; } @@ -220,8 +219,7 @@ void BluetoothConnection::send_service_for_discovery_() { break; } if (char_status != ESP_GATT_OK) { - ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->connection_index_, - this->address_str().c_str(), char_status); + this->log_connection_error_("esp_ble_gattc_get_all_char", char_status); this->send_service_ = DONE_SENDING_SERVICES; return; } @@ -244,8 +242,7 @@ void BluetoothConnection::send_service_for_discovery_() { this->gattc_if_, this->conn_id_, ESP_GATT_DB_DESCRIPTOR, 0, 0, char_result.char_handle, &total_desc_count); if (desc_count_status != ESP_GATT_OK) { - ESP_LOGE(TAG, "[%d] [%s] Error getting descriptor count for char handle %d, status=%d", - this->connection_index_, this->address_str().c_str(), char_result.char_handle, desc_count_status); + this->log_connection_error_("esp_ble_gattc_get_attr_count", desc_count_status); this->send_service_ = DONE_SENDING_SERVICES; return; } @@ -266,8 +263,7 @@ void BluetoothConnection::send_service_for_discovery_() { break; } if (desc_status != ESP_GATT_OK) { - ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_descr error, status=%d", this->connection_index_, - this->address_str().c_str(), desc_status); + this->log_connection_error_("esp_ble_gattc_get_all_descr", desc_status); this->send_service_ = DONE_SENDING_SERVICES; return; } @@ -321,6 +317,25 @@ void BluetoothConnection::send_service_for_discovery_() { api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE); } +void BluetoothConnection::log_connection_error_(const char *operation, esp_gatt_status_t status) { + ESP_LOGE(TAG, "[%d] [%s] %s error, status=%d", this->connection_index_, this->address_str().c_str(), operation, + status); +} + +void BluetoothConnection::log_connection_warning_(const char *operation, esp_err_t err) { + ESP_LOGW(TAG, "[%d] [%s] %s failed, err=%d", this->connection_index_, this->address_str().c_str(), operation, err); +} + +void BluetoothConnection::log_gatt_not_connected_(const char *action, const char *type) { + ESP_LOGW(TAG, "[%d] [%s] Cannot %s GATT %s, not connected.", this->connection_index_, this->address_str().c_str(), + action, type); +} + +void BluetoothConnection::log_gatt_operation_error_(const char *operation, uint16_t handle, esp_gatt_status_t status) { + ESP_LOGW(TAG, "[%d] [%s] Error %s for handle 0x%2X, status=%d", this->connection_index_, this->address_str().c_str(), + operation, handle, status); +} + bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) { if (!BLEClientBase::gattc_event_handler(event, gattc_if, param)) @@ -361,8 +376,7 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga case ESP_GATTC_READ_DESCR_EVT: case ESP_GATTC_READ_CHAR_EVT: { if (param->read.status != ESP_GATT_OK) { - ESP_LOGW(TAG, "[%d] [%s] Error reading char/descriptor at handle 0x%2X, status=%d", this->connection_index_, - this->address_str_.c_str(), param->read.handle, param->read.status); + this->log_gatt_operation_error_("reading char/descriptor", param->read.handle, param->read.status); this->proxy_->send_gatt_error(this->address_, param->read.handle, param->read.status); break; } @@ -376,8 +390,7 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga case ESP_GATTC_WRITE_CHAR_EVT: case ESP_GATTC_WRITE_DESCR_EVT: { if (param->write.status != ESP_GATT_OK) { - ESP_LOGW(TAG, "[%d] [%s] Error writing char/descriptor at handle 0x%2X, status=%d", this->connection_index_, - this->address_str_.c_str(), param->write.handle, param->write.status); + this->log_gatt_operation_error_("writing char/descriptor", param->write.handle, param->write.status); this->proxy_->send_gatt_error(this->address_, param->write.handle, param->write.status); break; } @@ -389,9 +402,8 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga } case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: { if (param->unreg_for_notify.status != ESP_GATT_OK) { - ESP_LOGW(TAG, "[%d] [%s] Error unregistering notifications for handle 0x%2X, status=%d", - this->connection_index_, this->address_str_.c_str(), param->unreg_for_notify.handle, - param->unreg_for_notify.status); + this->log_gatt_operation_error_("unregistering notifications", param->unreg_for_notify.handle, + param->unreg_for_notify.status); this->proxy_->send_gatt_error(this->address_, param->unreg_for_notify.handle, param->unreg_for_notify.status); break; } @@ -403,8 +415,8 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga } case ESP_GATTC_REG_FOR_NOTIFY_EVT: { if (param->reg_for_notify.status != ESP_GATT_OK) { - ESP_LOGW(TAG, "[%d] [%s] Error registering notifications for handle 0x%2X, status=%d", this->connection_index_, - this->address_str_.c_str(), param->reg_for_notify.handle, param->reg_for_notify.status); + this->log_gatt_operation_error_("registering notifications", param->reg_for_notify.handle, + param->reg_for_notify.status); this->proxy_->send_gatt_error(this->address_, param->reg_for_notify.handle, param->reg_for_notify.status); break; } @@ -450,8 +462,7 @@ void BluetoothConnection::gap_event_handler(esp_gap_ble_cb_event_t event, esp_bl esp_err_t BluetoothConnection::read_characteristic(uint16_t handle) { if (!this->connected()) { - ESP_LOGW(TAG, "[%d] [%s] Cannot read GATT characteristic, not connected.", this->connection_index_, - this->address_str_.c_str()); + this->log_gatt_not_connected_("read", "characteristic"); return ESP_GATT_NOT_CONNECTED; } @@ -469,8 +480,7 @@ esp_err_t BluetoothConnection::read_characteristic(uint16_t handle) { esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const std::string &data, bool response) { if (!this->connected()) { - ESP_LOGW(TAG, "[%d] [%s] Cannot write GATT characteristic, not connected.", this->connection_index_, - this->address_str_.c_str()); + this->log_gatt_not_connected_("write", "characteristic"); return ESP_GATT_NOT_CONNECTED; } ESP_LOGV(TAG, "[%d] [%s] Writing GATT characteristic handle %d", this->connection_index_, this->address_str_.c_str(), @@ -480,8 +490,7 @@ esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const std:: esp_ble_gattc_write_char(this->gattc_if_, this->conn_id_, handle, data.size(), (uint8_t *) data.data(), response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE); if (err != ERR_OK) { - ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_write_char error, err=%d", this->connection_index_, - this->address_str_.c_str(), err); + this->log_connection_warning_("esp_ble_gattc_write_char", err); return err; } return ESP_OK; @@ -489,8 +498,7 @@ esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const std:: esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) { if (!this->connected()) { - ESP_LOGW(TAG, "[%d] [%s] Cannot read GATT descriptor, not connected.", this->connection_index_, - this->address_str_.c_str()); + this->log_gatt_not_connected_("read", "descriptor"); return ESP_GATT_NOT_CONNECTED; } ESP_LOGV(TAG, "[%d] [%s] Reading GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(), @@ -498,8 +506,7 @@ esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) { esp_err_t err = esp_ble_gattc_read_char_descr(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE); if (err != ERR_OK) { - ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_read_char_descr error, err=%d", this->connection_index_, - this->address_str_.c_str(), err); + this->log_connection_warning_("esp_ble_gattc_read_char_descr", err); return err; } return ESP_OK; @@ -507,8 +514,7 @@ esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) { esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::string &data, bool response) { if (!this->connected()) { - ESP_LOGW(TAG, "[%d] [%s] Cannot write GATT descriptor, not connected.", this->connection_index_, - this->address_str_.c_str()); + this->log_gatt_not_connected_("write", "descriptor"); return ESP_GATT_NOT_CONNECTED; } ESP_LOGV(TAG, "[%d] [%s] Writing GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(), @@ -527,8 +533,7 @@ esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::stri esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enable) { if (!this->connected()) { - ESP_LOGW(TAG, "[%d] [%s] Cannot notify GATT characteristic, not connected.", this->connection_index_, - this->address_str_.c_str()); + this->log_gatt_not_connected_("notify", "characteristic"); return ESP_GATT_NOT_CONNECTED; } @@ -537,8 +542,7 @@ esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enabl this->address_str_.c_str(), handle); esp_err_t err = esp_ble_gattc_register_for_notify(this->gattc_if_, this->remote_bda_, handle); if (err != ESP_OK) { - ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_register_for_notify failed, err=%d", this->connection_index_, - this->address_str_.c_str(), err); + this->log_connection_warning_("esp_ble_gattc_register_for_notify", err); return err; } } else { @@ -546,8 +550,7 @@ esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enabl this->address_str_.c_str(), handle); esp_err_t err = esp_ble_gattc_unregister_for_notify(this->gattc_if_, this->remote_bda_, handle); if (err != ESP_OK) { - ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_unregister_for_notify failed, err=%d", this->connection_index_, - this->address_str_.c_str(), err); + this->log_connection_warning_("esp_ble_gattc_unregister_for_notify", err); return err; } } diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.h b/esphome/components/bluetooth_proxy/bluetooth_connection.h index 042868e7a4..92c9172e83 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.h +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.h @@ -33,6 +33,10 @@ class BluetoothConnection : public esp32_ble_client::BLEClientBase { void send_service_for_discovery_(); void reset_connection_(esp_err_t reason); void update_allocated_slot_(uint64_t find_value, uint64_t set_value); + void log_connection_error_(const char *operation, esp_gatt_status_t status); + void log_connection_warning_(const char *operation, esp_err_t err); + void log_gatt_not_connected_(const char *action, const char *type); + void log_gatt_operation_error_(const char *operation, uint16_t handle, esp_gatt_status_t status); // Memory optimized layout for 32-bit systems // Group 1: Pointers (4 bytes each, naturally aligned)