[api] Replace check_data_state_ with debug assert in write methods

Callers already guarantee DATA state before calling write_protobuf_packet
(via try_to_clear_buffer) and write_protobuf_messages (via loop processing).
The runtime check is unreachable — replaced with ESPHOME_DEBUG_API assert.
This commit is contained in:
J. Nick Koston
2026-03-29 11:49:52 -10:00
parent 3bfc9b7f1f
commit c970c38e8b
3 changed files with 11 additions and 14 deletions
+3 -2
View File
@@ -161,10 +161,11 @@ class APIFrameHelper {
this->nodelay_counter_ = 0;
}
}
// Write a single protobuf message - the hot path (87-100% of all writes)
// Write a single protobuf message - the hot path (87-100% of all writes).
// Caller must ensure state is DATA before calling.
virtual APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) = 0;
// Write multiple protobuf messages in a single batched operation.
// messages must not be empty — caller is responsible for checking.
// Caller must ensure state is DATA and messages is not empty.
// messages contains (message_type, offset, length) for each message in the buffer.
// The buffer contains all messages with appropriate padding before each.
virtual APIError write_protobuf_messages(ProtoWriteBuffer buffer, std::span<const MessageInfo> messages) = 0;
@@ -489,9 +489,9 @@ APIError APINoiseFrameHelper::encrypt_noise_message_(uint8_t *buf_start, const M
}
APIError APINoiseFrameHelper::write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) {
APIError aerr = this->check_data_state_();
if (aerr != APIError::OK)
return aerr;
#ifdef ESPHOME_DEBUG_API
assert(this->state_ == State::DATA);
#endif
// Resize buffer to include footer space for Noise MAC
if (frame_footer_size_)
@@ -508,10 +508,8 @@ APIError APINoiseFrameHelper::write_protobuf_packet(uint8_t type, ProtoWriteBuff
}
APIError APINoiseFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffer, std::span<const MessageInfo> messages) {
APIError aerr = this->check_data_state_();
if (aerr != APIError::OK)
return aerr;
#ifdef ESPHOME_DEBUG_API
assert(this->state_ == State::DATA);
assert(!messages.empty());
#endif
@@ -285,9 +285,9 @@ ESPHOME_ALWAYS_INLINE static inline uint8_t *write_plaintext_header(uint8_t *buf
}
APIError APIPlaintextFrameHelper::write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) {
APIError aerr = this->check_data_state_();
if (aerr != APIError::OK)
return aerr;
#ifdef ESPHOME_DEBUG_API
assert(this->state_ == State::DATA);
#endif
MessageInfo msg{type, 0, static_cast<uint16_t>(buffer.get_buffer()->size() - frame_header_padding_)};
uint8_t *buffer_data = buffer.get_buffer()->data();
@@ -299,10 +299,8 @@ APIError APIPlaintextFrameHelper::write_protobuf_packet(uint8_t type, ProtoWrite
APIError APIPlaintextFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffer,
std::span<const MessageInfo> messages) {
APIError aerr = this->check_data_state_();
if (aerr != APIError::OK)
return aerr;
#ifdef ESPHOME_DEBUG_API
assert(this->state_ == State::DATA);
assert(!messages.empty());
#endif