diff --git a/esphome/components/api/api_frame_helper_plaintext.cpp b/esphome/components/api/api_frame_helper_plaintext.cpp index 2fd91965793..1335ae5e602 100644 --- a/esphome/components/api/api_frame_helper_plaintext.cpp +++ b/esphome/components/api/api_frame_helper_plaintext.cpp @@ -136,13 +136,12 @@ APIError APIPlaintextFrameHelper::try_read_frame_() { continue; } - if (msg_size_varint.as_uint32() > MAX_MESSAGE_SIZE) { + if (msg_size_varint.value > MAX_MESSAGE_SIZE) { state_ = State::FAILED; - HELPER_LOG("Bad packet: message size %" PRIu32 " exceeds maximum %u", msg_size_varint.as_uint32(), - MAX_MESSAGE_SIZE); + HELPER_LOG("Bad packet: message size %" PRIu32 " exceeds maximum %u", msg_size_varint.value, MAX_MESSAGE_SIZE); return APIError::BAD_DATA_PACKET; } - rx_header_parsed_len_ = msg_size_varint.as_uint16(); + rx_header_parsed_len_ = static_cast(msg_size_varint.value); // Move to next varint position varint_pos += msg_size_varint.consumed; @@ -152,13 +151,13 @@ APIError APIPlaintextFrameHelper::try_read_frame_() { // not enough data there yet continue; } - if (msg_type_varint.as_uint32() > std::numeric_limits::max()) { + if (msg_type_varint.value > std::numeric_limits::max()) { state_ = State::FAILED; - HELPER_LOG("Bad packet: message type %" PRIu32 " exceeds maximum %u", msg_type_varint.as_uint32(), + HELPER_LOG("Bad packet: message type %" PRIu32 " exceeds maximum %u", msg_type_varint.value, std::numeric_limits::max()); return APIError::BAD_DATA_PACKET; } - rx_header_parsed_type_ = msg_type_varint.as_uint16(); + rx_header_parsed_type_ = static_cast(msg_type_varint.value); rx_header_parsed_ = true; } // header reading done diff --git a/esphome/components/api/proto.cpp b/esphome/components/api/proto.cpp index ac13a9a5ab4..e35565dd6db 100644 --- a/esphome/components/api/proto.cpp +++ b/esphome/components/api/proto.cpp @@ -69,7 +69,7 @@ uint32_t ProtoDecodableMessage::count_repeated_field(const uint8_t *buffer, size break; // Invalid data, stop counting } - uint32_t tag = res.as_uint32(); + uint32_t tag = res.value; uint32_t field_type = tag & WIRE_TYPE_MASK; uint32_t field_id = tag >> 3; ptr += res.consumed; @@ -94,7 +94,7 @@ uint32_t ProtoDecodableMessage::count_repeated_field(const uint8_t *buffer, size if (!res.has_value()) { return count; } - uint32_t field_length = res.as_uint32(); + uint32_t field_length = res.value; ptr += res.consumed; if (field_length > static_cast(end - ptr)) { return count; // Out of bounds @@ -215,7 +215,7 @@ void ProtoDecodableMessage::decode(const uint8_t *buffer, size_t length) { return; } - uint32_t tag = res.as_uint32(); + uint32_t tag = res.value; uint32_t field_type = tag & WIRE_TYPE_MASK; uint32_t field_id = tag >> 3; ptr += res.consumed; @@ -228,7 +228,8 @@ void ProtoDecodableMessage::decode(const uint8_t *buffer, size_t length) { return; } if (!this->decode_varint(field_id, res.value)) { - ESP_LOGV(TAG, "Cannot decode VarInt field %" PRIu32 " with value %" PRIu32 "!", field_id, res.as_uint32()); + ESP_LOGV(TAG, "Cannot decode VarInt field %" PRIu32 " with value %" PRIu64 "!", field_id, + static_cast(res.value)); } ptr += res.consumed; break; @@ -239,7 +240,7 @@ void ProtoDecodableMessage::decode(const uint8_t *buffer, size_t length) { ESP_LOGV(TAG, "Invalid Length Delimited at offset %ld", (long) (ptr - buffer)); return; } - uint32_t field_length = res.as_uint32(); + uint32_t field_length = res.value; ptr += res.consumed; if (field_length > static_cast(end - ptr)) { ESP_LOGV(TAG, "Out-of-bounds Length Delimited at offset %ld", (long) (ptr - buffer)); diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 7915b58b57c..7050efb4460 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -115,8 +115,6 @@ struct ProtoVarIntResult { uint32_t consumed; // PROTO_VARINT_PARSE_FAILED = parse failed constexpr bool has_value() const { return this->consumed != PROTO_VARINT_PARSE_FAILED; } - constexpr uint16_t as_uint16() const { return this->value; } - constexpr uint32_t as_uint32() const { return this->value; } }; /// Static varint parsing methods for the protobuf wire format.