[api] Replace empty messages check with debug assert

Callers are responsible for not passing empty spans. Added
ESPHOME_DEBUG_API assert to catch bugs during development.
This commit is contained in:
J. Nick Koston
2026-03-29 11:47:42 -10:00
parent 371070ab78
commit 3bfc9b7f1f
3 changed files with 10 additions and 11 deletions
+4 -3
View File
@@ -163,9 +163,10 @@ class APIFrameHelper {
}
// Write a single protobuf message - the hot path (87-100% of all writes)
virtual APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) = 0;
// Write multiple protobuf messages in a single batched operation
// messages contains (message_type, offset, length) for each message in the buffer
// The buffer contains all messages with appropriate padding before each
// Write multiple protobuf messages in a single batched operation.
// messages must not be empty — caller is responsible for checking.
// 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;
// Get the frame header padding required by this protocol
uint8_t frame_header_padding() const { return frame_header_padding_; }
@@ -511,10 +511,9 @@ APIError APINoiseFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffer, s
APIError aerr = this->check_data_state_();
if (aerr != APIError::OK)
return aerr;
if (messages.empty()) {
return APIError::OK;
}
#ifdef ESPHOME_DEBUG_API
assert(!messages.empty());
#endif
uint8_t *buffer_data = buffer.get_buffer()->data();
StaticVector<struct iovec, MAX_MESSAGES_PER_BATCH> iovs;
@@ -302,10 +302,9 @@ APIError APIPlaintextFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffe
APIError aerr = this->check_data_state_();
if (aerr != APIError::OK)
return aerr;
if (messages.empty()) {
return APIError::OK;
}
#ifdef ESPHOME_DEBUG_API
assert(!messages.empty());
#endif
uint8_t *buffer_data = buffer.get_buffer()->data();
StaticVector<struct iovec, MAX_MESSAGES_PER_BATCH> iovs;