mirror of
https://github.com/esphome/esphome.git
synced 2025-07-29 22:56:37 +00:00
[api] Optimize protobuf empty message handling to reduce flash and runtime overhead (#9908)
This commit is contained in:
parent
189d20a822
commit
68f388f78e
@ -378,7 +378,7 @@ class ConnectResponse : public ProtoMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
class DisconnectRequest : public ProtoDecodableMessage {
|
class DisconnectRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 5;
|
static constexpr uint8_t MESSAGE_TYPE = 5;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -391,7 +391,7 @@ class DisconnectRequest : public ProtoDecodableMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
class DisconnectResponse : public ProtoDecodableMessage {
|
class DisconnectResponse : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 6;
|
static constexpr uint8_t MESSAGE_TYPE = 6;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -404,7 +404,7 @@ class DisconnectResponse : public ProtoDecodableMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
class PingRequest : public ProtoDecodableMessage {
|
class PingRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 7;
|
static constexpr uint8_t MESSAGE_TYPE = 7;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -417,7 +417,7 @@ class PingRequest : public ProtoDecodableMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
class PingResponse : public ProtoDecodableMessage {
|
class PingResponse : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 8;
|
static constexpr uint8_t MESSAGE_TYPE = 8;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -430,7 +430,7 @@ class PingResponse : public ProtoDecodableMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
class DeviceInfoRequest : public ProtoDecodableMessage {
|
class DeviceInfoRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 9;
|
static constexpr uint8_t MESSAGE_TYPE = 9;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -546,7 +546,7 @@ class DeviceInfoResponse : public ProtoMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
class ListEntitiesRequest : public ProtoDecodableMessage {
|
class ListEntitiesRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 11;
|
static constexpr uint8_t MESSAGE_TYPE = 11;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -572,7 +572,7 @@ class ListEntitiesDoneResponse : public ProtoMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
class SubscribeStatesRequest : public ProtoDecodableMessage {
|
class SubscribeStatesRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 20;
|
static constexpr uint8_t MESSAGE_TYPE = 20;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -1045,7 +1045,7 @@ class NoiseEncryptionSetKeyResponse : public ProtoMessage {
|
|||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_API_HOMEASSISTANT_SERVICES
|
#ifdef USE_API_HOMEASSISTANT_SERVICES
|
||||||
class SubscribeHomeassistantServicesRequest : public ProtoDecodableMessage {
|
class SubscribeHomeassistantServicesRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 34;
|
static constexpr uint8_t MESSAGE_TYPE = 34;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -1094,7 +1094,7 @@ class HomeassistantServiceResponse : public ProtoMessage {
|
|||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_API_HOMEASSISTANT_STATES
|
#ifdef USE_API_HOMEASSISTANT_STATES
|
||||||
class SubscribeHomeAssistantStatesRequest : public ProtoDecodableMessage {
|
class SubscribeHomeAssistantStatesRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 38;
|
static constexpr uint8_t MESSAGE_TYPE = 38;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -1145,7 +1145,7 @@ class HomeAssistantStateResponse : public ProtoDecodableMessage {
|
|||||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
class GetTimeRequest : public ProtoDecodableMessage {
|
class GetTimeRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 36;
|
static constexpr uint8_t MESSAGE_TYPE = 36;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -2043,7 +2043,7 @@ class BluetoothGATTNotifyDataResponse : public ProtoMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
class SubscribeBluetoothConnectionsFreeRequest : public ProtoDecodableMessage {
|
class SubscribeBluetoothConnectionsFreeRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 80;
|
static constexpr uint8_t MESSAGE_TYPE = 80;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -2162,7 +2162,7 @@ class BluetoothDeviceUnpairingResponse : public ProtoMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
class UnsubscribeBluetoothLEAdvertisementsRequest : public ProtoDecodableMessage {
|
class UnsubscribeBluetoothLEAdvertisementsRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 87;
|
static constexpr uint8_t MESSAGE_TYPE = 87;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
@ -2418,7 +2418,7 @@ class VoiceAssistantWakeWord : public ProtoMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
class VoiceAssistantConfigurationRequest : public ProtoDecodableMessage {
|
class VoiceAssistantConfigurationRequest : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
static constexpr uint8_t MESSAGE_TYPE = 121;
|
static constexpr uint8_t MESSAGE_TYPE = 121;
|
||||||
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
static constexpr uint8_t ESTIMATED_SIZE = 0;
|
||||||
|
@ -35,7 +35,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
}
|
}
|
||||||
case DisconnectRequest::MESSAGE_TYPE: {
|
case DisconnectRequest::MESSAGE_TYPE: {
|
||||||
DisconnectRequest msg;
|
DisconnectRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_disconnect_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_disconnect_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -44,7 +44,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
}
|
}
|
||||||
case DisconnectResponse::MESSAGE_TYPE: {
|
case DisconnectResponse::MESSAGE_TYPE: {
|
||||||
DisconnectResponse msg;
|
DisconnectResponse msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_disconnect_response: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_disconnect_response: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -53,7 +53,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
}
|
}
|
||||||
case PingRequest::MESSAGE_TYPE: {
|
case PingRequest::MESSAGE_TYPE: {
|
||||||
PingRequest msg;
|
PingRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_ping_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_ping_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -62,7 +62,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
}
|
}
|
||||||
case PingResponse::MESSAGE_TYPE: {
|
case PingResponse::MESSAGE_TYPE: {
|
||||||
PingResponse msg;
|
PingResponse msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_ping_response: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_ping_response: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -71,7 +71,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
}
|
}
|
||||||
case DeviceInfoRequest::MESSAGE_TYPE: {
|
case DeviceInfoRequest::MESSAGE_TYPE: {
|
||||||
DeviceInfoRequest msg;
|
DeviceInfoRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_device_info_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_device_info_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -80,7 +80,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
}
|
}
|
||||||
case ListEntitiesRequest::MESSAGE_TYPE: {
|
case ListEntitiesRequest::MESSAGE_TYPE: {
|
||||||
ListEntitiesRequest msg;
|
ListEntitiesRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_list_entities_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_list_entities_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -89,7 +89,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
}
|
}
|
||||||
case SubscribeStatesRequest::MESSAGE_TYPE: {
|
case SubscribeStatesRequest::MESSAGE_TYPE: {
|
||||||
SubscribeStatesRequest msg;
|
SubscribeStatesRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_subscribe_states_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_subscribe_states_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -152,7 +152,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
#ifdef USE_API_HOMEASSISTANT_SERVICES
|
#ifdef USE_API_HOMEASSISTANT_SERVICES
|
||||||
case SubscribeHomeassistantServicesRequest::MESSAGE_TYPE: {
|
case SubscribeHomeassistantServicesRequest::MESSAGE_TYPE: {
|
||||||
SubscribeHomeassistantServicesRequest msg;
|
SubscribeHomeassistantServicesRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_subscribe_homeassistant_services_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_subscribe_homeassistant_services_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -162,7 +162,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
#endif
|
#endif
|
||||||
case GetTimeRequest::MESSAGE_TYPE: {
|
case GetTimeRequest::MESSAGE_TYPE: {
|
||||||
GetTimeRequest msg;
|
GetTimeRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_get_time_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_get_time_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -181,7 +181,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
#ifdef USE_API_HOMEASSISTANT_STATES
|
#ifdef USE_API_HOMEASSISTANT_STATES
|
||||||
case SubscribeHomeAssistantStatesRequest::MESSAGE_TYPE: {
|
case SubscribeHomeAssistantStatesRequest::MESSAGE_TYPE: {
|
||||||
SubscribeHomeAssistantStatesRequest msg;
|
SubscribeHomeAssistantStatesRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_subscribe_home_assistant_states_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_subscribe_home_assistant_states_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -390,7 +390,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
case SubscribeBluetoothConnectionsFreeRequest::MESSAGE_TYPE: {
|
case SubscribeBluetoothConnectionsFreeRequest::MESSAGE_TYPE: {
|
||||||
SubscribeBluetoothConnectionsFreeRequest msg;
|
SubscribeBluetoothConnectionsFreeRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_subscribe_bluetooth_connections_free_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_subscribe_bluetooth_connections_free_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -401,7 +401,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
case UnsubscribeBluetoothLEAdvertisementsRequest::MESSAGE_TYPE: {
|
case UnsubscribeBluetoothLEAdvertisementsRequest::MESSAGE_TYPE: {
|
||||||
UnsubscribeBluetoothLEAdvertisementsRequest msg;
|
UnsubscribeBluetoothLEAdvertisementsRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_unsubscribe_bluetooth_le_advertisements_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_unsubscribe_bluetooth_le_advertisements_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
@ -555,7 +555,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
|||||||
#ifdef USE_VOICE_ASSISTANT
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
case VoiceAssistantConfigurationRequest::MESSAGE_TYPE: {
|
case VoiceAssistantConfigurationRequest::MESSAGE_TYPE: {
|
||||||
VoiceAssistantConfigurationRequest msg;
|
VoiceAssistantConfigurationRequest msg;
|
||||||
msg.decode(msg_data, msg_size);
|
// Empty message: no decode needed
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
ESP_LOGVV(TAG, "on_voice_assistant_configuration_request: %s", msg.dump().c_str());
|
ESP_LOGVV(TAG, "on_voice_assistant_configuration_request: %s", msg.dump().c_str());
|
||||||
#endif
|
#endif
|
||||||
|
@ -1772,8 +1772,13 @@ def build_message_type(
|
|||||||
if base_class:
|
if base_class:
|
||||||
out = f"class {desc.name} : public {base_class} {{\n"
|
out = f"class {desc.name} : public {base_class} {{\n"
|
||||||
else:
|
else:
|
||||||
# Determine inheritance based on whether the message needs decoding
|
# Check if message has any non-deprecated fields
|
||||||
base_class = "ProtoDecodableMessage" if needs_decode else "ProtoMessage"
|
has_fields = any(not field.options.deprecated for field in desc.field)
|
||||||
|
# Determine inheritance based on whether the message needs decoding and has fields
|
||||||
|
if needs_decode and has_fields:
|
||||||
|
base_class = "ProtoDecodableMessage"
|
||||||
|
else:
|
||||||
|
base_class = "ProtoMessage"
|
||||||
out = f"class {desc.name} : public {base_class} {{\n"
|
out = f"class {desc.name} : public {base_class} {{\n"
|
||||||
out += " public:\n"
|
out += " public:\n"
|
||||||
out += indent("\n".join(public_content)) + "\n"
|
out += indent("\n".join(public_content)) + "\n"
|
||||||
@ -2039,7 +2044,14 @@ def build_service_message_type(
|
|||||||
hout += f"virtual void {func}(const {mt.name} &value){{}};\n"
|
hout += f"virtual void {func}(const {mt.name} &value){{}};\n"
|
||||||
case = ""
|
case = ""
|
||||||
case += f"{mt.name} msg;\n"
|
case += f"{mt.name} msg;\n"
|
||||||
case += "msg.decode(msg_data, msg_size);\n"
|
# Check if this message has any fields (excluding deprecated ones)
|
||||||
|
has_fields = any(not field.options.deprecated for field in mt.field)
|
||||||
|
if has_fields:
|
||||||
|
# Normal case: decode the message
|
||||||
|
case += "msg.decode(msg_data, msg_size);\n"
|
||||||
|
else:
|
||||||
|
# Empty message optimization: skip decode since there are no fields
|
||||||
|
case += "// Empty message: no decode needed\n"
|
||||||
if log:
|
if log:
|
||||||
case += "#ifdef HAS_PROTO_MESSAGE_DUMP\n"
|
case += "#ifdef HAS_PROTO_MESSAGE_DUMP\n"
|
||||||
case += f'ESP_LOGVV(TAG, "{func}: %s", msg.dump().c_str());\n'
|
case += f'ESP_LOGVV(TAG, "{func}: %s", msg.dump().c_str());\n'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user