[api] Devirtualize API command dispatch (#15044)

This commit is contained in:
J. Nick Koston
2026-03-22 20:57:40 -10:00
committed by GitHub
parent 225330413a
commit f4097d5a95
6 changed files with 164 additions and 167 deletions
+1 -1
View File
@@ -234,7 +234,7 @@ void APIConnection::loop() {
this->last_traffic_ = now; this->last_traffic_ = now;
} }
// read a packet // read a packet
this->read_message(buffer.data_len, buffer.type, buffer.data); this->read_message_(buffer.data_len, buffer.type, buffer.data);
if (this->flags_.remove) if (this->flags_.remove)
return; return;
} }
+87 -69
View File
@@ -49,11 +49,29 @@ class APIConnection final : public APIServerConnectionBase {
friend class APIServer; friend class APIServer;
friend class ListEntitiesIterator; friend class ListEntitiesIterator;
APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent); APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
virtual ~APIConnection(); ~APIConnection();
void start(); void start();
void loop(); void loop();
protected:
// read_message_ is defined here (instead of in APIServerConnectionBase) so the
// compiler can devirtualize and inline on_* handler calls within this final class.
void read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data);
// Auth helpers defined here (not in ProtoService) so the compiler can
// devirtualize is_connection_setup()/on_no_setup_connection() calls
// within this final class.
inline bool check_connection_setup_() {
if (!this->is_connection_setup()) {
this->on_no_setup_connection();
return false;
}
return true;
}
inline bool check_authenticated_() { return this->check_connection_setup_(); }
public:
bool send_list_info_done() { bool send_list_info_done() {
return this->schedule_message_(nullptr, ListEntitiesDoneResponse::MESSAGE_TYPE, return this->schedule_message_(nullptr, ListEntitiesDoneResponse::MESSAGE_TYPE,
ListEntitiesDoneResponse::ESTIMATED_SIZE); ListEntitiesDoneResponse::ESTIMATED_SIZE);
@@ -63,72 +81,72 @@ class APIConnection final : public APIServerConnectionBase {
#endif #endif
#ifdef USE_COVER #ifdef USE_COVER
bool send_cover_state(cover::Cover *cover); bool send_cover_state(cover::Cover *cover);
void on_cover_command_request(const CoverCommandRequest &msg) override; void on_cover_command_request(const CoverCommandRequest &msg);
#endif #endif
#ifdef USE_FAN #ifdef USE_FAN
bool send_fan_state(fan::Fan *fan); bool send_fan_state(fan::Fan *fan);
void on_fan_command_request(const FanCommandRequest &msg) override; void on_fan_command_request(const FanCommandRequest &msg);
#endif #endif
#ifdef USE_LIGHT #ifdef USE_LIGHT
bool send_light_state(light::LightState *light); bool send_light_state(light::LightState *light);
void on_light_command_request(const LightCommandRequest &msg) override; void on_light_command_request(const LightCommandRequest &msg);
#endif #endif
#ifdef USE_SENSOR #ifdef USE_SENSOR
bool send_sensor_state(sensor::Sensor *sensor); bool send_sensor_state(sensor::Sensor *sensor);
#endif #endif
#ifdef USE_SWITCH #ifdef USE_SWITCH
bool send_switch_state(switch_::Switch *a_switch); bool send_switch_state(switch_::Switch *a_switch);
void on_switch_command_request(const SwitchCommandRequest &msg) override; void on_switch_command_request(const SwitchCommandRequest &msg);
#endif #endif
#ifdef USE_TEXT_SENSOR #ifdef USE_TEXT_SENSOR
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor); bool send_text_sensor_state(text_sensor::TextSensor *text_sensor);
#endif #endif
#ifdef USE_CAMERA #ifdef USE_CAMERA
void set_camera_state(std::shared_ptr<camera::CameraImage> image); void set_camera_state(std::shared_ptr<camera::CameraImage> image);
void on_camera_image_request(const CameraImageRequest &msg) override; void on_camera_image_request(const CameraImageRequest &msg);
#endif #endif
#ifdef USE_CLIMATE #ifdef USE_CLIMATE
bool send_climate_state(climate::Climate *climate); bool send_climate_state(climate::Climate *climate);
void on_climate_command_request(const ClimateCommandRequest &msg) override; void on_climate_command_request(const ClimateCommandRequest &msg);
#endif #endif
#ifdef USE_NUMBER #ifdef USE_NUMBER
bool send_number_state(number::Number *number); bool send_number_state(number::Number *number);
void on_number_command_request(const NumberCommandRequest &msg) override; void on_number_command_request(const NumberCommandRequest &msg);
#endif #endif
#ifdef USE_DATETIME_DATE #ifdef USE_DATETIME_DATE
bool send_date_state(datetime::DateEntity *date); bool send_date_state(datetime::DateEntity *date);
void on_date_command_request(const DateCommandRequest &msg) override; void on_date_command_request(const DateCommandRequest &msg);
#endif #endif
#ifdef USE_DATETIME_TIME #ifdef USE_DATETIME_TIME
bool send_time_state(datetime::TimeEntity *time); bool send_time_state(datetime::TimeEntity *time);
void on_time_command_request(const TimeCommandRequest &msg) override; void on_time_command_request(const TimeCommandRequest &msg);
#endif #endif
#ifdef USE_DATETIME_DATETIME #ifdef USE_DATETIME_DATETIME
bool send_datetime_state(datetime::DateTimeEntity *datetime); bool send_datetime_state(datetime::DateTimeEntity *datetime);
void on_date_time_command_request(const DateTimeCommandRequest &msg) override; void on_date_time_command_request(const DateTimeCommandRequest &msg);
#endif #endif
#ifdef USE_TEXT #ifdef USE_TEXT
bool send_text_state(text::Text *text); bool send_text_state(text::Text *text);
void on_text_command_request(const TextCommandRequest &msg) override; void on_text_command_request(const TextCommandRequest &msg);
#endif #endif
#ifdef USE_SELECT #ifdef USE_SELECT
bool send_select_state(select::Select *select); bool send_select_state(select::Select *select);
void on_select_command_request(const SelectCommandRequest &msg) override; void on_select_command_request(const SelectCommandRequest &msg);
#endif #endif
#ifdef USE_BUTTON #ifdef USE_BUTTON
void on_button_command_request(const ButtonCommandRequest &msg) override; void on_button_command_request(const ButtonCommandRequest &msg);
#endif #endif
#ifdef USE_LOCK #ifdef USE_LOCK
bool send_lock_state(lock::Lock *a_lock); bool send_lock_state(lock::Lock *a_lock);
void on_lock_command_request(const LockCommandRequest &msg) override; void on_lock_command_request(const LockCommandRequest &msg);
#endif #endif
#ifdef USE_VALVE #ifdef USE_VALVE
bool send_valve_state(valve::Valve *valve); bool send_valve_state(valve::Valve *valve);
void on_valve_command_request(const ValveCommandRequest &msg) override; void on_valve_command_request(const ValveCommandRequest &msg);
#endif #endif
#ifdef USE_MEDIA_PLAYER #ifdef USE_MEDIA_PLAYER
bool send_media_player_state(media_player::MediaPlayer *media_player); bool send_media_player_state(media_player::MediaPlayer *media_player);
void on_media_player_command_request(const MediaPlayerCommandRequest &msg) override; void on_media_player_command_request(const MediaPlayerCommandRequest &msg);
#endif #endif
bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len); bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len);
#ifdef USE_API_HOMEASSISTANT_SERVICES #ifdef USE_API_HOMEASSISTANT_SERVICES
@@ -138,23 +156,23 @@ class APIConnection final : public APIServerConnectionBase {
this->send_message(call); this->send_message(call);
} }
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
void on_homeassistant_action_response(const HomeassistantActionResponse &msg) override; void on_homeassistant_action_response(const HomeassistantActionResponse &msg);
#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES #endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
#endif // USE_API_HOMEASSISTANT_SERVICES #endif // USE_API_HOMEASSISTANT_SERVICES
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void on_subscribe_bluetooth_le_advertisements_request(const SubscribeBluetoothLEAdvertisementsRequest &msg) override; void on_subscribe_bluetooth_le_advertisements_request(const SubscribeBluetoothLEAdvertisementsRequest &msg);
void on_unsubscribe_bluetooth_le_advertisements_request() override; void on_unsubscribe_bluetooth_le_advertisements_request();
void on_bluetooth_device_request(const BluetoothDeviceRequest &msg) override; void on_bluetooth_device_request(const BluetoothDeviceRequest &msg);
void on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &msg) override; void on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &msg);
void on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &msg) override; void on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &msg);
void on_bluetooth_gatt_read_descriptor_request(const BluetoothGATTReadDescriptorRequest &msg) override; void on_bluetooth_gatt_read_descriptor_request(const BluetoothGATTReadDescriptorRequest &msg);
void on_bluetooth_gatt_write_descriptor_request(const BluetoothGATTWriteDescriptorRequest &msg) override; void on_bluetooth_gatt_write_descriptor_request(const BluetoothGATTWriteDescriptorRequest &msg);
void on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &msg) override; void on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &msg);
void on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &msg) override; void on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &msg);
void on_subscribe_bluetooth_connections_free_request() override; void on_subscribe_bluetooth_connections_free_request();
void on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &msg) override; void on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &msg);
void on_bluetooth_set_connection_params_request(const BluetoothSetConnectionParamsRequest &msg) override; void on_bluetooth_set_connection_params_request(const BluetoothSetConnectionParamsRequest &msg);
#endif #endif
#ifdef USE_HOMEASSISTANT_TIME #ifdef USE_HOMEASSISTANT_TIME
@@ -165,42 +183,42 @@ class APIConnection final : public APIServerConnectionBase {
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
void on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &msg) override; void on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &msg);
void on_voice_assistant_response(const VoiceAssistantResponse &msg) override; void on_voice_assistant_response(const VoiceAssistantResponse &msg);
void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) override; void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg);
void on_voice_assistant_audio(const VoiceAssistantAudio &msg) override; void on_voice_assistant_audio(const VoiceAssistantAudio &msg);
void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg) override; void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg);
void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &msg) override; void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &msg);
void on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &msg) override; void on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &msg);
void on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg) override; void on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg);
#endif #endif
#ifdef USE_ZWAVE_PROXY #ifdef USE_ZWAVE_PROXY
void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg) override; void on_z_wave_proxy_frame(const ZWaveProxyFrame &msg);
void on_z_wave_proxy_request(const ZWaveProxyRequest &msg) override; void on_z_wave_proxy_request(const ZWaveProxyRequest &msg);
#endif #endif
#ifdef USE_ALARM_CONTROL_PANEL #ifdef USE_ALARM_CONTROL_PANEL
bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel); bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel);
void on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &msg) override; void on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &msg);
#endif #endif
#ifdef USE_WATER_HEATER #ifdef USE_WATER_HEATER
bool send_water_heater_state(water_heater::WaterHeater *water_heater); bool send_water_heater_state(water_heater::WaterHeater *water_heater);
void on_water_heater_command_request(const WaterHeaterCommandRequest &msg) override; void on_water_heater_command_request(const WaterHeaterCommandRequest &msg);
#endif #endif
#ifdef USE_IR_RF #ifdef USE_IR_RF
void on_infrared_rf_transmit_raw_timings_request(const InfraredRFTransmitRawTimingsRequest &msg) override; void on_infrared_rf_transmit_raw_timings_request(const InfraredRFTransmitRawTimingsRequest &msg);
void send_infrared_rf_receive_event(const InfraredRFReceiveEvent &msg); void send_infrared_rf_receive_event(const InfraredRFReceiveEvent &msg);
#endif #endif
#ifdef USE_SERIAL_PROXY #ifdef USE_SERIAL_PROXY
void on_serial_proxy_configure_request(const SerialProxyConfigureRequest &msg) override; void on_serial_proxy_configure_request(const SerialProxyConfigureRequest &msg);
void on_serial_proxy_write_request(const SerialProxyWriteRequest &msg) override; void on_serial_proxy_write_request(const SerialProxyWriteRequest &msg);
void on_serial_proxy_set_modem_pins_request(const SerialProxySetModemPinsRequest &msg) override; void on_serial_proxy_set_modem_pins_request(const SerialProxySetModemPinsRequest &msg);
void on_serial_proxy_get_modem_pins_request(const SerialProxyGetModemPinsRequest &msg) override; void on_serial_proxy_get_modem_pins_request(const SerialProxyGetModemPinsRequest &msg);
void on_serial_proxy_request(const SerialProxyRequest &msg) override; void on_serial_proxy_request(const SerialProxyRequest &msg);
void send_serial_proxy_data(const SerialProxyDataReceived &msg); void send_serial_proxy_data(const SerialProxyDataReceived &msg);
#endif #endif
@@ -210,26 +228,26 @@ class APIConnection final : public APIServerConnectionBase {
#ifdef USE_UPDATE #ifdef USE_UPDATE
bool send_update_state(update::UpdateEntity *update); bool send_update_state(update::UpdateEntity *update);
void on_update_command_request(const UpdateCommandRequest &msg) override; void on_update_command_request(const UpdateCommandRequest &msg);
#endif #endif
void on_disconnect_response() override; void on_disconnect_response();
void on_ping_response() override { void on_ping_response() {
// we initiated ping // we initiated ping
this->flags_.sent_ping = false; this->flags_.sent_ping = false;
} }
#ifdef USE_API_HOMEASSISTANT_STATES #ifdef USE_API_HOMEASSISTANT_STATES
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override; void on_home_assistant_state_response(const HomeAssistantStateResponse &msg);
#endif #endif
#ifdef USE_HOMEASSISTANT_TIME #ifdef USE_HOMEASSISTANT_TIME
void on_get_time_response(const GetTimeResponse &value) override; void on_get_time_response(const GetTimeResponse &value);
#endif #endif
void on_hello_request(const HelloRequest &msg) override; void on_hello_request(const HelloRequest &msg);
void on_disconnect_request() override; void on_disconnect_request();
void on_ping_request() override; void on_ping_request();
void on_device_info_request() override; void on_device_info_request();
void on_list_entities_request() override { this->begin_iterator_(ActiveIterator::LIST_ENTITIES); } void on_list_entities_request() { this->begin_iterator_(ActiveIterator::LIST_ENTITIES); }
void on_subscribe_states_request() override { void on_subscribe_states_request() {
this->flags_.state_subscription = true; this->flags_.state_subscription = true;
// Start initial state iterator only if no iterator is active // Start initial state iterator only if no iterator is active
// If list_entities is running, we'll start initial_state when it completes // If list_entities is running, we'll start initial_state when it completes
@@ -237,7 +255,7 @@ class APIConnection final : public APIServerConnectionBase {
this->begin_iterator_(ActiveIterator::INITIAL_STATE); this->begin_iterator_(ActiveIterator::INITIAL_STATE);
} }
} }
void on_subscribe_logs_request(const SubscribeLogsRequest &msg) override { void on_subscribe_logs_request(const SubscribeLogsRequest &msg) {
this->flags_.log_subscription = msg.level; this->flags_.log_subscription = msg.level;
if (msg.dump_config) if (msg.dump_config)
App.schedule_dump_config(); App.schedule_dump_config();
@@ -249,13 +267,13 @@ class APIConnection final : public APIServerConnectionBase {
#endif #endif
} }
#ifdef USE_API_HOMEASSISTANT_SERVICES #ifdef USE_API_HOMEASSISTANT_SERVICES
void on_subscribe_homeassistant_services_request() override { this->flags_.service_call_subscription = true; } void on_subscribe_homeassistant_services_request() { this->flags_.service_call_subscription = true; }
#endif #endif
#ifdef USE_API_HOMEASSISTANT_STATES #ifdef USE_API_HOMEASSISTANT_STATES
void on_subscribe_home_assistant_states_request() override; void on_subscribe_home_assistant_states_request();
#endif #endif
#ifdef USE_API_USER_DEFINED_ACTIONS #ifdef USE_API_USER_DEFINED_ACTIONS
void on_execute_service_request(const ExecuteServiceRequest &msg) override; void on_execute_service_request(const ExecuteServiceRequest &msg);
#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES #ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message); void send_execute_service_response(uint32_t call_id, bool success, StringRef error_message);
#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON #ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
@@ -265,13 +283,13 @@ class APIConnection final : public APIServerConnectionBase {
#endif // USE_API_USER_DEFINED_ACTION_RESPONSES #endif // USE_API_USER_DEFINED_ACTION_RESPONSES
#endif #endif
#ifdef USE_API_NOISE #ifdef USE_API_NOISE
void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) override; void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg);
#endif #endif
bool is_authenticated() override { bool is_authenticated() {
return static_cast<ConnectionState>(this->flags_.connection_state) == ConnectionState::AUTHENTICATED; return static_cast<ConnectionState>(this->flags_.connection_state) == ConnectionState::AUTHENTICATED;
} }
bool is_connection_setup() override { bool is_connection_setup() {
return static_cast<ConnectionState>(this->flags_.connection_state) == ConnectionState::CONNECTED || return static_cast<ConnectionState>(this->flags_.connection_state) == ConnectionState::CONNECTED ||
this->is_authenticated(); this->is_authenticated();
} }
@@ -284,8 +302,8 @@ class APIConnection final : public APIServerConnectionBase {
(this->client_api_version_major_ == major && this->client_api_version_minor_ >= minor); (this->client_api_version_major_ == major && this->client_api_version_minor_ >= minor);
} }
void on_fatal_error() override; void on_fatal_error();
void on_no_setup_connection() override; void on_no_setup_connection();
// Function pointer type for type-erased message encoding // Function pointer type for type-erased message encoding
using MessageEncodeFn = void (*)(const void *, ProtoWriteBuffer &); using MessageEncodeFn = void (*)(const void *, ProtoWriteBuffer &);
@@ -324,7 +342,7 @@ class APIConnection final : public APIServerConnectionBase {
return true; return true;
return this->try_to_clear_buffer_slow_(log_out_of_space); return this->try_to_clear_buffer_slow_(log_out_of_space);
} }
bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override; bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type);
const char *get_name() const { return this->helper_->get_client_name(); } const char *get_name() const { return this->helper_->get_client_name(); }
/// Get peer name (IP address) into caller-provided buffer, returns buf for convenience /// Get peer name (IP address) into caller-provided buffer, returns buf for convenience
+2 -1
View File
@@ -1,6 +1,7 @@
// This file was automatically generated with a tool. // This file was automatically generated with a tool.
// See script/api_protobuf/api_protobuf.py // See script/api_protobuf/api_protobuf.py
#include "api_pb2_service.h" #include "api_pb2_service.h"
#include "api_connection.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome::api { namespace esphome::api {
@@ -20,7 +21,7 @@ void APIServerConnectionBase::log_receive_message_(const LogString *name) {
} }
#endif #endif
void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) { void APIConnection::read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) {
// Check authentication/connection requirements // Check authentication/connection requirements
switch (msg_type) { switch (msg_type) {
case HelloRequest::MESSAGE_TYPE: // No setup required case HelloRequest::MESSAGE_TYPE: // No setup required
+65 -69
View File
@@ -8,7 +8,7 @@
namespace esphome::api { namespace esphome::api {
class APIServerConnectionBase : public ProtoService { class APIServerConnectionBase {
public: public:
#ifdef HAS_PROTO_MESSAGE_DUMP #ifdef HAS_PROTO_MESSAGE_DUMP
protected: protected:
@@ -19,227 +19,223 @@ class APIServerConnectionBase : public ProtoService {
public: public:
#endif #endif
virtual void on_hello_request(const HelloRequest &value){}; void on_hello_request(const HelloRequest &value){};
virtual void on_disconnect_request(){}; void on_disconnect_request(){};
virtual void on_disconnect_response(){}; void on_disconnect_response(){};
virtual void on_ping_request(){}; void on_ping_request(){};
virtual void on_ping_response(){}; void on_ping_response(){};
virtual void on_device_info_request(){}; void on_device_info_request(){};
virtual void on_list_entities_request(){}; void on_list_entities_request(){};
virtual void on_subscribe_states_request(){}; void on_subscribe_states_request(){};
#ifdef USE_COVER #ifdef USE_COVER
virtual void on_cover_command_request(const CoverCommandRequest &value){}; void on_cover_command_request(const CoverCommandRequest &value){};
#endif #endif
#ifdef USE_FAN #ifdef USE_FAN
virtual void on_fan_command_request(const FanCommandRequest &value){}; void on_fan_command_request(const FanCommandRequest &value){};
#endif #endif
#ifdef USE_LIGHT #ifdef USE_LIGHT
virtual void on_light_command_request(const LightCommandRequest &value){}; void on_light_command_request(const LightCommandRequest &value){};
#endif #endif
#ifdef USE_SWITCH #ifdef USE_SWITCH
virtual void on_switch_command_request(const SwitchCommandRequest &value){}; void on_switch_command_request(const SwitchCommandRequest &value){};
#endif #endif
virtual void on_subscribe_logs_request(const SubscribeLogsRequest &value){}; void on_subscribe_logs_request(const SubscribeLogsRequest &value){};
#ifdef USE_API_NOISE #ifdef USE_API_NOISE
virtual void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &value){}; void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &value){};
#endif #endif
#ifdef USE_API_HOMEASSISTANT_SERVICES #ifdef USE_API_HOMEASSISTANT_SERVICES
virtual void on_subscribe_homeassistant_services_request(){}; void on_subscribe_homeassistant_services_request(){};
#endif #endif
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
virtual void on_homeassistant_action_response(const HomeassistantActionResponse &value){}; void on_homeassistant_action_response(const HomeassistantActionResponse &value){};
#endif #endif
#ifdef USE_API_HOMEASSISTANT_STATES #ifdef USE_API_HOMEASSISTANT_STATES
virtual void on_subscribe_home_assistant_states_request(){}; void on_subscribe_home_assistant_states_request(){};
#endif #endif
#ifdef USE_API_HOMEASSISTANT_STATES #ifdef USE_API_HOMEASSISTANT_STATES
virtual void on_home_assistant_state_response(const HomeAssistantStateResponse &value){}; void on_home_assistant_state_response(const HomeAssistantStateResponse &value){};
#endif #endif
virtual void on_get_time_response(const GetTimeResponse &value){}; void on_get_time_response(const GetTimeResponse &value){};
#ifdef USE_API_USER_DEFINED_ACTIONS #ifdef USE_API_USER_DEFINED_ACTIONS
virtual void on_execute_service_request(const ExecuteServiceRequest &value){}; void on_execute_service_request(const ExecuteServiceRequest &value){};
#endif #endif
#ifdef USE_CAMERA #ifdef USE_CAMERA
virtual void on_camera_image_request(const CameraImageRequest &value){}; void on_camera_image_request(const CameraImageRequest &value){};
#endif #endif
#ifdef USE_CLIMATE #ifdef USE_CLIMATE
virtual void on_climate_command_request(const ClimateCommandRequest &value){}; void on_climate_command_request(const ClimateCommandRequest &value){};
#endif #endif
#ifdef USE_WATER_HEATER #ifdef USE_WATER_HEATER
virtual void on_water_heater_command_request(const WaterHeaterCommandRequest &value){}; void on_water_heater_command_request(const WaterHeaterCommandRequest &value){};
#endif #endif
#ifdef USE_NUMBER #ifdef USE_NUMBER
virtual void on_number_command_request(const NumberCommandRequest &value){}; void on_number_command_request(const NumberCommandRequest &value){};
#endif #endif
#ifdef USE_SELECT #ifdef USE_SELECT
virtual void on_select_command_request(const SelectCommandRequest &value){}; void on_select_command_request(const SelectCommandRequest &value){};
#endif #endif
#ifdef USE_SIREN #ifdef USE_SIREN
virtual void on_siren_command_request(const SirenCommandRequest &value){}; void on_siren_command_request(const SirenCommandRequest &value){};
#endif #endif
#ifdef USE_LOCK #ifdef USE_LOCK
virtual void on_lock_command_request(const LockCommandRequest &value){}; void on_lock_command_request(const LockCommandRequest &value){};
#endif #endif
#ifdef USE_BUTTON #ifdef USE_BUTTON
virtual void on_button_command_request(const ButtonCommandRequest &value){}; void on_button_command_request(const ButtonCommandRequest &value){};
#endif #endif
#ifdef USE_MEDIA_PLAYER #ifdef USE_MEDIA_PLAYER
virtual void on_media_player_command_request(const MediaPlayerCommandRequest &value){}; void on_media_player_command_request(const MediaPlayerCommandRequest &value){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_subscribe_bluetooth_le_advertisements_request( void on_subscribe_bluetooth_le_advertisements_request(const SubscribeBluetoothLEAdvertisementsRequest &value){};
const SubscribeBluetoothLEAdvertisementsRequest &value){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_bluetooth_device_request(const BluetoothDeviceRequest &value){}; void on_bluetooth_device_request(const BluetoothDeviceRequest &value){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &value){}; void on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &value){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &value){}; void on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &value){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &value){}; void on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &value){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_bluetooth_gatt_read_descriptor_request(const BluetoothGATTReadDescriptorRequest &value){}; void on_bluetooth_gatt_read_descriptor_request(const BluetoothGATTReadDescriptorRequest &value){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_bluetooth_gatt_write_descriptor_request(const BluetoothGATTWriteDescriptorRequest &value){}; void on_bluetooth_gatt_write_descriptor_request(const BluetoothGATTWriteDescriptorRequest &value){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &value){}; void on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &value){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_subscribe_bluetooth_connections_free_request(){}; void on_subscribe_bluetooth_connections_free_request(){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_unsubscribe_bluetooth_le_advertisements_request(){}; void on_unsubscribe_bluetooth_le_advertisements_request(){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &value){}; void on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &value){};
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
virtual void on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &value){}; void on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &value){};
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
virtual void on_voice_assistant_response(const VoiceAssistantResponse &value){}; void on_voice_assistant_response(const VoiceAssistantResponse &value){};
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
virtual void on_voice_assistant_event_response(const VoiceAssistantEventResponse &value){}; void on_voice_assistant_event_response(const VoiceAssistantEventResponse &value){};
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
virtual void on_voice_assistant_audio(const VoiceAssistantAudio &value){}; void on_voice_assistant_audio(const VoiceAssistantAudio &value){};
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
virtual void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &value){}; void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &value){};
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
virtual void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &value){}; void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &value){};
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
virtual void on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &value){}; void on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &value){};
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
virtual void on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &value){}; void on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &value){};
#endif #endif
#ifdef USE_ALARM_CONTROL_PANEL #ifdef USE_ALARM_CONTROL_PANEL
virtual void on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &value){}; void on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &value){};
#endif #endif
#ifdef USE_TEXT #ifdef USE_TEXT
virtual void on_text_command_request(const TextCommandRequest &value){}; void on_text_command_request(const TextCommandRequest &value){};
#endif #endif
#ifdef USE_DATETIME_DATE #ifdef USE_DATETIME_DATE
virtual void on_date_command_request(const DateCommandRequest &value){}; void on_date_command_request(const DateCommandRequest &value){};
#endif #endif
#ifdef USE_DATETIME_TIME #ifdef USE_DATETIME_TIME
virtual void on_time_command_request(const TimeCommandRequest &value){}; void on_time_command_request(const TimeCommandRequest &value){};
#endif #endif
#ifdef USE_VALVE #ifdef USE_VALVE
virtual void on_valve_command_request(const ValveCommandRequest &value){}; void on_valve_command_request(const ValveCommandRequest &value){};
#endif #endif
#ifdef USE_DATETIME_DATETIME #ifdef USE_DATETIME_DATETIME
virtual void on_date_time_command_request(const DateTimeCommandRequest &value){}; void on_date_time_command_request(const DateTimeCommandRequest &value){};
#endif #endif
#ifdef USE_UPDATE #ifdef USE_UPDATE
virtual void on_update_command_request(const UpdateCommandRequest &value){}; void on_update_command_request(const UpdateCommandRequest &value){};
#endif #endif
#ifdef USE_ZWAVE_PROXY #ifdef USE_ZWAVE_PROXY
virtual void on_z_wave_proxy_frame(const ZWaveProxyFrame &value){}; void on_z_wave_proxy_frame(const ZWaveProxyFrame &value){};
#endif #endif
#ifdef USE_ZWAVE_PROXY #ifdef USE_ZWAVE_PROXY
virtual void on_z_wave_proxy_request(const ZWaveProxyRequest &value){}; void on_z_wave_proxy_request(const ZWaveProxyRequest &value){};
#endif #endif
#ifdef USE_IR_RF #ifdef USE_IR_RF
virtual void on_infrared_rf_transmit_raw_timings_request(const InfraredRFTransmitRawTimingsRequest &value){}; void on_infrared_rf_transmit_raw_timings_request(const InfraredRFTransmitRawTimingsRequest &value){};
#endif #endif
#ifdef USE_SERIAL_PROXY #ifdef USE_SERIAL_PROXY
virtual void on_serial_proxy_configure_request(const SerialProxyConfigureRequest &value){}; void on_serial_proxy_configure_request(const SerialProxyConfigureRequest &value){};
#endif #endif
#ifdef USE_SERIAL_PROXY #ifdef USE_SERIAL_PROXY
virtual void on_serial_proxy_write_request(const SerialProxyWriteRequest &value){}; void on_serial_proxy_write_request(const SerialProxyWriteRequest &value){};
#endif #endif
#ifdef USE_SERIAL_PROXY #ifdef USE_SERIAL_PROXY
virtual void on_serial_proxy_set_modem_pins_request(const SerialProxySetModemPinsRequest &value){}; void on_serial_proxy_set_modem_pins_request(const SerialProxySetModemPinsRequest &value){};
#endif #endif
#ifdef USE_SERIAL_PROXY #ifdef USE_SERIAL_PROXY
virtual void on_serial_proxy_get_modem_pins_request(const SerialProxyGetModemPinsRequest &value){}; void on_serial_proxy_get_modem_pins_request(const SerialProxyGetModemPinsRequest &value){};
#endif #endif
#ifdef USE_SERIAL_PROXY #ifdef USE_SERIAL_PROXY
virtual void on_serial_proxy_request(const SerialProxyRequest &value){}; void on_serial_proxy_request(const SerialProxyRequest &value){};
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
virtual void on_bluetooth_set_connection_params_request(const BluetoothSetConnectionParamsRequest &value){}; void on_bluetooth_set_connection_params_request(const BluetoothSetConnectionParamsRequest &value){};
#endif #endif
protected:
void read_message(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) override;
}; };
} // namespace esphome::api } // namespace esphome::api
+2 -21
View File
@@ -711,26 +711,7 @@ inline void ProtoLengthDelimited::decode_to_message(ProtoDecodableMessage &msg)
template<typename T> const char *proto_enum_to_string(T value); template<typename T> const char *proto_enum_to_string(T value);
class ProtoService { // ProtoService removed — its methods were inlined into APIConnection.
public: // APIConnection is the concrete server-side implementation; the extra virtual layer was unnecessary.
protected:
virtual bool is_authenticated() = 0;
virtual bool is_connection_setup() = 0;
virtual void on_fatal_error() = 0;
virtual void on_no_setup_connection() = 0;
virtual bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) = 0;
virtual void read_message(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) = 0;
// Authentication helper methods
inline bool check_connection_setup_() {
if (!this->is_connection_setup()) {
this->on_no_setup_connection();
return false;
}
return true;
}
inline bool check_authenticated_() { return this->check_connection_setup_(); }
};
} // namespace esphome::api } // namespace esphome::api
+7 -6
View File
@@ -2608,7 +2608,7 @@ def build_service_message_type(
is_empty = not has_fields is_empty = not has_fields
if is_empty: if is_empty:
EMPTY_MESSAGES.add(mt.name) EMPTY_MESSAGES.add(mt.name)
hout += f"virtual void {func}({'' if is_empty else f'const {mt.name} &value'}){{}};\n" hout += f"void {func}({'' if is_empty else f'const {mt.name} &value'}){{}};\n"
case = "" case = ""
if not is_empty: if not is_empty:
case += f"{mt.name} msg;\n" case += f"{mt.name} msg;\n"
@@ -2960,6 +2960,7 @@ namespace esphome::api {
cpp = FILE_HEADER cpp = FILE_HEADER
cpp += """\ cpp += """\
#include "api_pb2_service.h" #include "api_pb2_service.h"
#include "api_connection.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome::api { namespace esphome::api {
@@ -2970,7 +2971,7 @@ static const char *const TAG = "api.service";
class_name = "APIServerConnectionBase" class_name = "APIServerConnectionBase"
hpp += f"class {class_name} : public ProtoService {{\n" hpp += f"class {class_name} {{\n"
hpp += " public:\n" hpp += " public:\n"
# Add logging helper method declarations # Add logging helper method declarations
@@ -3063,11 +3064,11 @@ static const char *const TAG = "api.service";
result += "#endif\n" result += "#endif\n"
return result return result
# Generate read_message with auth check before dispatch # Generate read_message_ as APIConnection method (not base class) so the compiler
hpp += " protected:\n" # can devirtualize and inline the on_* handler calls within the same class.
hpp += " void read_message(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) override;\n" # APIConnection declares this method in api_connection.h.
out = f"void {class_name}::read_message(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) {{\n" out = "void APIConnection::read_message_(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) {\n"
# Auth check block before dispatch switch # Auth check block before dispatch switch
out += " // Check authentication/connection requirements\n" out += " // Check authentication/connection requirements\n"