This commit is contained in:
J. Nick Koston
2026-03-03 18:22:23 -10:00
parent 5d3d983994
commit a7b61b1061
7 changed files with 389 additions and 213 deletions
+2 -6
View File
@@ -350,9 +350,7 @@ uint16_t APIConnection::fill_and_encode_entity_state_(EntityBase *entity, StateR
#ifdef USE_DEVICES
msg.device_id = entity->get_device_id();
#endif
ProtoSize proto_size;
size_fn(&msg, proto_size);
return encode_to_buffer_(proto_size.get_size(), encode_fn, &msg, conn, remaining_size);
return encode_to_buffer_(size_fn(&msg), encode_fn, &msg, conn, remaining_size);
}
uint16_t APIConnection::fill_and_encode_entity_info_(EntityBase *entity, InfoResponseProtoMessage &msg,
@@ -382,9 +380,7 @@ uint16_t APIConnection::fill_and_encode_entity_info_(EntityBase *entity, InfoRes
#ifdef USE_DEVICES
msg.device_id = entity->get_device_id();
#endif
ProtoSize proto_size;
size_fn(&msg, proto_size);
return encode_to_buffer_(proto_size.get_size(), encode_fn, &msg, conn, remaining_size);
return encode_to_buffer_(size_fn(&msg), encode_fn, &msg, conn, remaining_size);
}
uint16_t APIConnection::fill_and_encode_entity_info_with_device_class_(
+5 -5
View File
@@ -261,7 +261,7 @@ class APIConnection final : public APIServerConnectionBase {
// Function pointer type for type-erased message encoding
using MessageEncodeFn = void (*)(const void *, ProtoWriteBuffer &);
// Function pointer type for type-erased size calculation
using CalculateSizeFn = void (*)(const void *, ProtoSize &);
using CalculateSizeFn = uint32_t (*)(const void *);
template<typename T> bool send_message(const T &msg) {
#ifdef HAS_PROTO_MESSAGE_DUMP
@@ -271,7 +271,7 @@ class APIConnection final : public APIServerConnectionBase {
if constexpr (T::ESTIMATED_SIZE == 0) {
return this->send_message_(0, T::MESSAGE_TYPE, &encode_msg_noop_, &msg);
} else {
return this->send_message_(calculated_size_of(msg), T::MESSAGE_TYPE, &encode_msg_<T>, &msg);
return this->send_message_(msg.calculate_size(), T::MESSAGE_TYPE, &encode_msg_<T>, &msg);
}
}
@@ -334,8 +334,8 @@ class APIConnection final : public APIServerConnectionBase {
}
// Size thunk — converts void* back to concrete type for direct calculate_size() call
template<typename T> static void calc_size_(const void *msg, ProtoSize &size) {
static_cast<const T *>(msg)->calculate_size(size);
template<typename T> static uint32_t calc_size_(const void *msg) {
return static_cast<const T *>(msg)->calculate_size();
}
// Shared no-op encode thunk for empty messages (ESTIMATED_SIZE == 0)
@@ -360,7 +360,7 @@ class APIConnection final : public APIServerConnectionBase {
if constexpr (T::ESTIMATED_SIZE == 0) {
return encode_to_buffer_(0, &encode_msg_noop_, &msg, conn, remaining_size);
} else {
return encode_to_buffer_(calculated_size_of(msg), &encode_msg_<T>, &msg, conn, remaining_size);
return encode_to_buffer_(msg.calculate_size(), &encode_msg_<T>, &msg, conn, remaining_size);
}
}
File diff suppressed because it is too large Load Diff
+97 -88
View File
@@ -333,6 +333,9 @@ class InfoResponseProtoMessage : public ProtoMessage {
#ifdef USE_DEVICES
uint32_t device_id{0};
#endif
protected:
~InfoResponseProtoMessage() = default;
};
class StateResponseProtoMessage : public ProtoMessage {
@@ -341,6 +344,9 @@ class StateResponseProtoMessage : public ProtoMessage {
#ifdef USE_DEVICES
uint32_t device_id{0};
#endif
protected:
~StateResponseProtoMessage() = default;
};
class CommandProtoMessage : public ProtoDecodableMessage {
@@ -349,6 +355,9 @@ class CommandProtoMessage : public ProtoDecodableMessage {
#ifdef USE_DEVICES
uint32_t device_id{0};
#endif
protected:
~CommandProtoMessage() = default;
};
class HelloRequest final : public ProtoDecodableMessage {
public:
@@ -380,7 +389,7 @@ class HelloResponse final : public ProtoMessage {
StringRef server_info{};
StringRef name{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -445,7 +454,7 @@ class AreaInfo final : public ProtoMessage {
uint32_t area_id{0};
StringRef name{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -460,7 +469,7 @@ class DeviceInfo final : public ProtoMessage {
StringRef name{};
uint32_t area_id{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -525,7 +534,7 @@ class DeviceInfoResponse final : public ProtoMessage {
uint32_t zwave_home_id{0};
#endif
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -556,7 +565,7 @@ class ListEntitiesBinarySensorResponse final : public InfoResponseProtoMessage {
StringRef device_class{};
bool is_status_binary_sensor{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -573,7 +582,7 @@ class BinarySensorStateResponse final : public StateResponseProtoMessage {
bool state{false};
bool missing_state{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -595,7 +604,7 @@ class ListEntitiesCoverResponse final : public InfoResponseProtoMessage {
StringRef device_class{};
bool supports_stop{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -613,7 +622,7 @@ class CoverStateResponse final : public StateResponseProtoMessage {
float tilt{0.0f};
enums::CoverOperation current_operation{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -655,7 +664,7 @@ class ListEntitiesFanResponse final : public InfoResponseProtoMessage {
int32_t supported_speed_count{0};
const std::vector<const char *> *supported_preset_modes{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -675,7 +684,7 @@ class FanStateResponse final : public StateResponseProtoMessage {
int32_t speed_level{0};
StringRef preset_mode{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -722,7 +731,7 @@ class ListEntitiesLightResponse final : public InfoResponseProtoMessage {
float max_mireds{0.0f};
const FixedVector<const char *> *effects{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -749,7 +758,7 @@ class LightStateResponse final : public StateResponseProtoMessage {
float warm_white{0.0f};
StringRef effect{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -813,7 +822,7 @@ class ListEntitiesSensorResponse final : public InfoResponseProtoMessage {
StringRef device_class{};
enums::SensorStateClass state_class{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -830,7 +839,7 @@ class SensorStateResponse final : public StateResponseProtoMessage {
float state{0.0f};
bool missing_state{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -849,7 +858,7 @@ class ListEntitiesSwitchResponse final : public InfoResponseProtoMessage {
bool assumed_state{false};
StringRef device_class{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -865,7 +874,7 @@ class SwitchStateResponse final : public StateResponseProtoMessage {
#endif
bool state{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -899,7 +908,7 @@ class ListEntitiesTextSensorResponse final : public InfoResponseProtoMessage {
#endif
StringRef device_class{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -916,7 +925,7 @@ class TextSensorStateResponse final : public StateResponseProtoMessage {
StringRef state{};
bool missing_state{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -955,7 +964,7 @@ class SubscribeLogsResponse final : public ProtoMessage {
this->message_len_ = len;
}
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -988,7 +997,7 @@ class NoiseEncryptionSetKeyResponse final : public ProtoMessage {
#endif
bool success{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1002,7 +1011,7 @@ class HomeassistantServiceMap final : public ProtoMessage {
StringRef key{};
StringRef value{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1031,7 +1040,7 @@ class HomeassistantActionRequest final : public ProtoMessage {
StringRef response_template{};
#endif
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1075,7 +1084,7 @@ class SubscribeHomeAssistantStateResponse final : public ProtoMessage {
StringRef attribute{};
bool once{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1166,7 +1175,7 @@ class ListEntitiesServicesArgument final : public ProtoMessage {
StringRef name{};
enums::ServiceArgType type{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1185,7 +1194,7 @@ class ListEntitiesServicesResponse final : public ProtoMessage {
FixedVector<ListEntitiesServicesArgument> args{};
enums::SupportsResponseType supports_response{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1255,7 +1264,7 @@ class ExecuteServiceResponse final : public ProtoMessage {
uint16_t response_data_len{0};
#endif
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1272,7 +1281,7 @@ class ListEntitiesCameraResponse final : public InfoResponseProtoMessage {
const char *message_name() const override { return "list_entities_camera_response"; }
#endif
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1294,7 +1303,7 @@ class CameraImageResponse final : public StateResponseProtoMessage {
}
bool done{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1345,7 +1354,7 @@ class ListEntitiesClimateResponse final : public InfoResponseProtoMessage {
float visual_max_humidity{0.0f};
uint32_t feature_flags{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1373,7 +1382,7 @@ class ClimateStateResponse final : public StateResponseProtoMessage {
float current_humidity{0.0f};
float target_humidity{0.0f};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1431,7 +1440,7 @@ class ListEntitiesWaterHeaterResponse final : public InfoResponseProtoMessage {
const water_heater::WaterHeaterModeMask *supported_modes{};
uint32_t supported_features{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1452,7 +1461,7 @@ class WaterHeaterStateResponse final : public StateResponseProtoMessage {
float target_temperature_low{0.0f};
float target_temperature_high{0.0f};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1496,7 +1505,7 @@ class ListEntitiesNumberResponse final : public InfoResponseProtoMessage {
enums::NumberMode mode{};
StringRef device_class{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1513,7 +1522,7 @@ class NumberStateResponse final : public StateResponseProtoMessage {
float state{0.0f};
bool missing_state{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1547,7 +1556,7 @@ class ListEntitiesSelectResponse final : public InfoResponseProtoMessage {
#endif
const FixedVector<const char *> *options{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1564,7 +1573,7 @@ class SelectStateResponse final : public StateResponseProtoMessage {
StringRef state{};
bool missing_state{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1601,7 +1610,7 @@ class ListEntitiesSirenResponse final : public InfoResponseProtoMessage {
bool supports_duration{false};
bool supports_volume{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1617,7 +1626,7 @@ class SirenStateResponse final : public StateResponseProtoMessage {
#endif
bool state{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1662,7 +1671,7 @@ class ListEntitiesLockResponse final : public InfoResponseProtoMessage {
bool requires_code{false};
StringRef code_format{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1678,7 +1687,7 @@ class LockStateResponse final : public StateResponseProtoMessage {
#endif
enums::LockState state{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1715,7 +1724,7 @@ class ListEntitiesButtonResponse final : public InfoResponseProtoMessage {
#endif
StringRef device_class{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1747,7 +1756,7 @@ class MediaPlayerSupportedFormat final : public ProtoMessage {
enums::MediaPlayerFormatPurpose purpose{};
uint32_t sample_bytes{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1765,7 +1774,7 @@ class ListEntitiesMediaPlayerResponse final : public InfoResponseProtoMessage {
std::vector<MediaPlayerSupportedFormat> supported_formats{};
uint32_t feature_flags{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1783,7 +1792,7 @@ class MediaPlayerStateResponse final : public StateResponseProtoMessage {
float volume{0.0f};
bool muted{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1839,7 +1848,7 @@ class BluetoothLERawAdvertisement final : public ProtoMessage {
uint8_t data[62]{};
uint8_t data_len{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1856,7 +1865,7 @@ class BluetoothLERawAdvertisementsResponse final : public ProtoMessage {
std::array<BluetoothLERawAdvertisement, BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE> advertisements{};
uint16_t advertisements_len{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1893,7 +1902,7 @@ class BluetoothDeviceConnectionResponse final : public ProtoMessage {
uint32_t mtu{0};
int32_t error{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1921,7 +1930,7 @@ class BluetoothGATTDescriptor final : public ProtoMessage {
uint32_t handle{0};
uint32_t short_uuid{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1936,7 +1945,7 @@ class BluetoothGATTCharacteristic final : public ProtoMessage {
FixedVector<BluetoothGATTDescriptor> descriptors{};
uint32_t short_uuid{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1950,7 +1959,7 @@ class BluetoothGATTService final : public ProtoMessage {
FixedVector<BluetoothGATTCharacteristic> characteristics{};
uint32_t short_uuid{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1967,7 +1976,7 @@ class BluetoothGATTGetServicesResponse final : public ProtoMessage {
uint64_t address{0};
std::vector<BluetoothGATTService> services{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -1983,7 +1992,7 @@ class BluetoothGATTGetServicesDoneResponse final : public ProtoMessage {
#endif
uint64_t address{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2022,7 +2031,7 @@ class BluetoothGATTReadResponse final : public ProtoMessage {
this->data_len_ = len;
}
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2117,7 +2126,7 @@ class BluetoothGATTNotifyDataResponse final : public ProtoMessage {
this->data_len_ = len;
}
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2135,7 +2144,7 @@ class BluetoothConnectionsFreeResponse final : public ProtoMessage {
uint32_t limit{0};
std::array<uint64_t, BLUETOOTH_PROXY_MAX_CONNECTIONS> allocated{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2153,7 +2162,7 @@ class BluetoothGATTErrorResponse final : public ProtoMessage {
uint32_t handle{0};
int32_t error{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2170,7 +2179,7 @@ class BluetoothGATTWriteResponse final : public ProtoMessage {
uint64_t address{0};
uint32_t handle{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2187,7 +2196,7 @@ class BluetoothGATTNotifyResponse final : public ProtoMessage {
uint64_t address{0};
uint32_t handle{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2205,7 +2214,7 @@ class BluetoothDevicePairingResponse final : public ProtoMessage {
bool paired{false};
int32_t error{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2223,7 +2232,7 @@ class BluetoothDeviceUnpairingResponse final : public ProtoMessage {
bool success{false};
int32_t error{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2241,7 +2250,7 @@ class BluetoothDeviceClearCacheResponse final : public ProtoMessage {
bool success{false};
int32_t error{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2259,7 +2268,7 @@ class BluetoothScannerStateResponse final : public ProtoMessage {
enums::BluetoothScannerMode mode{};
enums::BluetoothScannerMode configured_mode{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2305,7 +2314,7 @@ class VoiceAssistantAudioSettings final : public ProtoMessage {
uint32_t auto_gain{0};
float volume_multiplier{0.0f};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2325,7 +2334,7 @@ class VoiceAssistantRequest final : public ProtoMessage {
VoiceAssistantAudioSettings audio_settings{};
StringRef wake_word_phrase{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2387,7 +2396,7 @@ class VoiceAssistantAudio final : public ProtoDecodableMessage {
uint16_t data_len{0};
bool end{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2445,7 +2454,7 @@ class VoiceAssistantAnnounceFinished final : public ProtoMessage {
#endif
bool success{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2458,7 +2467,7 @@ class VoiceAssistantWakeWord final : public ProtoMessage {
StringRef wake_word{};
std::vector<std::string> trained_languages{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2508,7 +2517,7 @@ class VoiceAssistantConfigurationResponse final : public ProtoMessage {
const std::vector<std::string> *active_wake_words{};
uint32_t max_active_wake_words{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2543,7 +2552,7 @@ class ListEntitiesAlarmControlPanelResponse final : public InfoResponseProtoMess
bool requires_code{false};
bool requires_code_to_arm{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2559,7 +2568,7 @@ class AlarmControlPanelStateResponse final : public StateResponseProtoMessage {
#endif
enums::AlarmControlPanelState state{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2598,7 +2607,7 @@ class ListEntitiesTextResponse final : public InfoResponseProtoMessage {
StringRef pattern{};
enums::TextMode mode{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2615,7 +2624,7 @@ class TextStateResponse final : public StateResponseProtoMessage {
StringRef state{};
bool missing_state{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2649,7 +2658,7 @@ class ListEntitiesDateResponse final : public InfoResponseProtoMessage {
const char *message_name() const override { return "list_entities_date_response"; }
#endif
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2668,7 +2677,7 @@ class DateStateResponse final : public StateResponseProtoMessage {
uint32_t month{0};
uint32_t day{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2703,7 +2712,7 @@ class ListEntitiesTimeResponse final : public InfoResponseProtoMessage {
const char *message_name() const override { return "list_entities_time_response"; }
#endif
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2722,7 +2731,7 @@ class TimeStateResponse final : public StateResponseProtoMessage {
uint32_t minute{0};
uint32_t second{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2759,7 +2768,7 @@ class ListEntitiesEventResponse final : public InfoResponseProtoMessage {
StringRef device_class{};
const FixedVector<const char *> *event_types{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2775,7 +2784,7 @@ class EventResponse final : public StateResponseProtoMessage {
#endif
StringRef event_type{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2796,7 +2805,7 @@ class ListEntitiesValveResponse final : public InfoResponseProtoMessage {
bool supports_position{false};
bool supports_stop{false};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2813,7 +2822,7 @@ class ValveStateResponse final : public StateResponseProtoMessage {
float position{0.0f};
enums::ValveOperation current_operation{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2848,7 +2857,7 @@ class ListEntitiesDateTimeResponse final : public InfoResponseProtoMessage {
const char *message_name() const override { return "list_entities_date_time_response"; }
#endif
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2865,7 +2874,7 @@ class DateTimeStateResponse final : public StateResponseProtoMessage {
bool missing_state{false};
uint32_t epoch_seconds{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2899,7 +2908,7 @@ class ListEntitiesUpdateResponse final : public InfoResponseProtoMessage {
#endif
StringRef device_class{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2923,7 +2932,7 @@ class UpdateStateResponse final : public StateResponseProtoMessage {
StringRef release_summary{};
StringRef release_url{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2958,7 +2967,7 @@ class ZWaveProxyFrame final : public ProtoDecodableMessage {
const uint8_t *data{nullptr};
uint16_t data_len{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2977,7 +2986,7 @@ class ZWaveProxyRequest final : public ProtoDecodableMessage {
const uint8_t *data{nullptr};
uint16_t data_len{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -2997,7 +3006,7 @@ class ListEntitiesInfraredResponse final : public InfoResponseProtoMessage {
#endif
uint32_t capabilities{0};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
@@ -3044,7 +3053,7 @@ class InfraredRFReceiveEvent final : public ProtoMessage {
uint32_t key{0};
const std::vector<int32_t> *timings{};
void encode(ProtoWriteBuffer &buffer) const;
void calculate_size(ProtoSize &size) const;
uint32_t calculate_size() const;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *dump_to(DumpBuffer &out) const override;
#endif
+5 -9
View File
@@ -462,7 +462,7 @@ class ProtoMessage {
// dispatch is not needed. This eliminates per-message vtable entries for
// encode/calculate_size, saving ~1.3 KB of flash across all message types.
void encode(ProtoWriteBuffer &buffer) const {}
void calculate_size(ProtoSize &size) const {}
uint32_t calculate_size() const { return 0; }
#ifdef HAS_PROTO_MESSAGE_DUMP
virtual const char *dump_to(DumpBuffer &out) const = 0;
virtual const char *message_name() const { return "unknown"; }
@@ -846,11 +846,11 @@ class ProtoSize {
* @param message The nested message object
*/
template<typename T> inline void add_message_object(uint32_t field_id_size, const T &message) {
add_message_field(field_id_size, calculated_size_of(message));
add_message_field(field_id_size, message.calculate_size());
}
template<typename T> inline void add_message_object_force(uint32_t field_id_size, const T &message) {
add_message_field_force(field_id_size, calculated_size_of(message));
add_message_field_force(field_id_size, message.calculate_size());
}
/**
@@ -912,11 +912,7 @@ class ProtoSize {
// Free template to calculate encoded size of any message type.
// Replaces the former virtual ProtoMessage::calculated_size() member.
template<typename T> inline uint32_t calculated_size_of(const T &msg) {
ProtoSize size;
msg.calculate_size(size);
return size.get_size();
}
template<typename T> inline uint32_t calculated_size_of(const T &msg) { return msg.calculate_size(); }
// Implementation of encode_packed_sint32 - must be after ProtoSize is defined
inline void ProtoWriteBuffer::encode_packed_sint32(uint32_t field_id, const std::vector<int32_t> &values) {
@@ -939,7 +935,7 @@ inline void ProtoWriteBuffer::encode_packed_sint32(uint32_t field_id, const std:
// Implementation of encode_message - must be after ProtoMessage is defined
template<typename T> inline void ProtoWriteBuffer::encode_message(uint32_t field_id, const T &value, bool force) {
uint32_t msg_length_bytes = calculated_size_of(value);
uint32_t msg_length_bytes = value.calculate_size();
this->encode_message_(
field_id, msg_length_bytes, &value,
[](const void *msg, ProtoWriteBuffer &buf) { static_cast<const T *>(msg)->encode(buf); }, force);
@@ -183,10 +183,7 @@ void BluetoothConnection::send_service_for_discovery_() {
static constexpr size_t MAX_PACKET_SIZE = 1360;
// Keep running total of actual message size
size_t current_size = 0;
api::ProtoSize size;
resp.calculate_size(size);
current_size = size.get_size();
size_t current_size = resp.calculate_size();
while (this->send_service_ < this->service_count_) {
esp_gattc_service_elem_t service_result;
@@ -302,9 +299,7 @@ void BluetoothConnection::send_service_for_discovery_() {
} // end if (total_char_count > 0)
// Calculate the actual size of just this service
api::ProtoSize service_sizer;
service_resp.calculate_size(service_sizer);
size_t service_size = service_sizer.get_size() + 1; // +1 for field tag
size_t service_size = service_resp.calculate_size() + 1; // +1 for field tag
// Check if adding this service would exceed the limit
if (current_size + service_size > MAX_PACKET_SIZE) {
+6 -10
View File
@@ -2239,17 +2239,13 @@ def build_message_type(
# Add calculate_size method only if this message needs encoding and has fields
if needs_encode and size_calc:
o = f"void {desc.name}::calculate_size(ProtoSize &size) const {{"
# For a single field, just inline it for simplicity
if len(size_calc) == 1 and len(size_calc[0]) + len(o) + 3 < 120:
o += f" {size_calc[0]} }}\n"
else:
# For multiple fields
o += "\n"
o += indent("\n".join(size_calc)) + "\n"
o += "}\n"
o = f"uint32_t {desc.name}::calculate_size() const {{\n"
o += " ProtoSize size;\n"
o += indent("\n".join(size_calc)) + "\n"
o += " return size.get_size();\n"
o += "}\n"
cpp += o
prot = "void calculate_size(ProtoSize &size) const;"
prot = "uint32_t calculate_size() const;"
public_content.append(prot)
# If no fields to calculate size for or message doesn't need encoding, the default implementation in ProtoMessage will be used