diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 3031ac58f7..61fff064b2 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1495,9 +1495,7 @@ void APIConnection::execute_service(const ExecuteServiceRequest &msg) { NoiseEncryptionSetKeyResponse APIConnection::noise_encryption_set_key(const NoiseEncryptionSetKeyRequest &msg) { psk_t psk{}; NoiseEncryptionSetKeyResponse resp; - // Create temporary string from pointer/length for base64_decode - std::string key_str(reinterpret_cast(msg.key_ptr_), msg.key_len_); - if (base64_decode(key_str, psk.data(), key_str.size()) != psk.size()) { + if (base64_decode(msg.key, psk.data(), msg.key.size()) != psk.size()) { ESP_LOGW(TAG, "Invalid encryption key length"); resp.success = false; return resp; diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp index 8e08d62504..b3b271a027 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp @@ -333,8 +333,7 @@ esp_err_t BluetoothConnection::read_characteristic(uint16_t handle) { return ESP_OK; } -esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const uint8_t *data, size_t data_len, - bool response) { +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()); @@ -344,7 +343,7 @@ esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const uint8 handle); esp_err_t err = - esp_ble_gattc_write_char(this->gattc_if_, this->conn_id_, handle, data_len, const_cast(data), + 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_, @@ -372,7 +371,7 @@ esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) { return ESP_OK; } -esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const uint8_t *data, size_t data_len, bool response) { +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()); @@ -382,7 +381,7 @@ esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const uint8_t * handle); esp_err_t err = esp_ble_gattc_write_char_descr( - this->gattc_if_, this->conn_id_, handle, data_len, const_cast(data), + 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_descr error, err=%d", this->connection_index_, diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.h b/esphome/components/bluetooth_proxy/bluetooth_connection.h index c9c4b7c901..2673238fba 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.h +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.h @@ -19,9 +19,9 @@ class BluetoothConnection : public esp32_ble_client::BLEClientBase { esp32_ble_tracker::AdvertisementParserType get_advertisement_parser_type() override; esp_err_t read_characteristic(uint16_t handle); - esp_err_t write_characteristic(uint16_t handle, const uint8_t *data, size_t data_len, bool response); + esp_err_t write_characteristic(uint16_t handle, const std::string &data, bool response); esp_err_t read_descriptor(uint16_t handle); - esp_err_t write_descriptor(uint16_t handle, const uint8_t *data, size_t data_len, bool response); + esp_err_t write_descriptor(uint16_t handle, const std::string &data, bool response); esp_err_t notify_characteristic(uint16_t handle, bool enable); diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp index e5bc626d1b..8a1a2bff6a 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp @@ -344,8 +344,7 @@ void BluetoothProxy::bluetooth_gatt_write(const api::BluetoothGATTWriteRequest & return; } - auto err = connection->write_characteristic(msg.handle, reinterpret_cast(msg.data.data()), - msg.data.size(), msg.response); + auto err = connection->write_characteristic(msg.handle, msg.data, msg.response); if (err != ESP_OK) { this->send_gatt_error(msg.address, msg.handle, err); } @@ -373,8 +372,7 @@ void BluetoothProxy::bluetooth_gatt_write_descriptor(const api::BluetoothGATTWri return; } - auto err = connection->write_descriptor(msg.handle, reinterpret_cast(msg.data.data()), - msg.data.size(), true); + auto err = connection->write_descriptor(msg.handle, msg.data, true); if (err != ESP_OK) { this->send_gatt_error(msg.address, msg.handle, err); } diff --git a/esphome/components/voice_assistant/voice_assistant.cpp b/esphome/components/voice_assistant/voice_assistant.cpp index 01e93dec5e..c35a0814b8 100644 --- a/esphome/components/voice_assistant/voice_assistant.cpp +++ b/esphome/components/voice_assistant/voice_assistant.cpp @@ -839,12 +839,12 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) { void VoiceAssistant::on_audio(const api::VoiceAssistantAudio &msg) { #ifdef USE_SPEAKER // We should never get to this function if there is no speaker anyway if ((this->speaker_ != nullptr) && (this->speaker_buffer_ != nullptr)) { - if (this->speaker_buffer_index_ + msg.data_len_ < SPEAKER_BUFFER_SIZE) { - memcpy(this->speaker_buffer_ + this->speaker_buffer_index_, msg.data_ptr_, msg.data_len_); - this->speaker_buffer_index_ += msg.data_len_; - this->speaker_buffer_size_ += msg.data_len_; - this->speaker_bytes_received_ += msg.data_len_; - ESP_LOGV(TAG, "Received audio: %u bytes from API", msg.data_len_); + if (this->speaker_buffer_index_ + msg.data.size() < SPEAKER_BUFFER_SIZE) { + memcpy(this->speaker_buffer_ + this->speaker_buffer_index_, msg.data.data(), msg.data.size()); + this->speaker_buffer_index_ += msg.data.size(); + this->speaker_buffer_size_ += msg.data.size(); + this->speaker_bytes_received_ += msg.data.size(); + ESP_LOGV(TAG, "Received audio: %u bytes from API", msg.data.size()); } else { ESP_LOGE(TAG, "Cannot receive audio, buffer is full"); }