|
ESPHome 2026.11.0-dev
|
#include <serial_proxy.h>
Data Structures | |
| struct | IdentityScratch |
Public Member Functions | |
| void | setup () override |
| void | loop () override |
| void | dump_config () override |
| float | get_setup_priority () const override |
| uint32_t | get_instance_index () const |
| Get the instance index (position in Application's serial_proxies_ vector) | |
| void | set_instance_index (uint32_t index) |
| Set the instance index (called by Application::register_serial_proxy) | |
| void | set_name (const char *name) |
| Set the human-readable port name (from YAML configuration) | |
| const char * | get_name () const |
| Get the human-readable port name. | |
| void | set_port_type (api::enums::SerialProxyPortType port_type) |
| Set the port type (from YAML configuration) | |
| api::enums::SerialProxyPortType | get_port_type () const |
| Get the port type. | |
| SerialProxyResult | set_mode_from_client (api::APIConnection *api_connection, api::enums::SerialProxyMode mode) |
| Handle a mode change requested by an API client. | |
| SerialProxyResult | configure (api::APIConnection *api_connection, uint32_t baudrate, bool flow_control, uint8_t parity, uint8_t stop_bits, uint8_t data_size) |
| Configure UART parameters and apply them. | |
| api::APIConnection * | get_api_connection () |
| Get the currently subscribed API connection (nullptr if none) | |
| SerialProxyResult | serial_proxy_request (api::APIConnection *api_connection, api::enums::SerialProxyRequestType type) |
| Handle a subscribe/unsubscribe request from an API client. | |
| void | write_from_client (api::APIConnection *api_connection, const uint8_t *data, size_t len) |
| Write data received from an API client to the serial device. | |
| SerialProxyResult | set_modem_pins (api::APIConnection *api_connection, uint32_t line_states) |
| Set modem pin states from a bitmask of SerialProxyLineStateFlag values. | |
| uint32_t | get_modem_pins () const |
| Get current modem pin states as a bitmask of SerialProxyLineStateFlag values. | |
| uint32_t | get_configured_modem_pins () const |
| Get the modem pins this instance can drive as a bitmask of SerialProxyLineStateFlag values. | |
| SerialProxyResult | flush_port (api::APIConnection *api_connection) |
| Flush the serial port (block until all TX data is sent) | |
| void | set_rts_pin (GPIOPin *pin) |
| Set the RTS GPIO pin (from YAML configuration) | |
| void | set_dtr_pin (GPIOPin *pin) |
| Set the DTR GPIO pin (from YAML configuration) | |
| void | set_usb_channel (usb_uart::USBUartChannel *channel) |
| Attach the USB UART channel behind this port (from code generation) | |
| void | set_host_uart (uart::HostUartComponent *host_uart) |
| Attach the host UART behind this port (from code generation) | |
| void | send_identity (api::APIConnection *api_connection) |
| Send this port's identity to one client. | |
| void | set_tap (SerialProxyTap *tap) |
| Attach a traffic observer. At most one, set once at setup time. | |
| bool | write_from_tap (const uint8_t *data, size_t len) |
| Write bytes originating from the tap rather than from a client. | |
| bool | tap_is_observed () const |
| Whether the tap is currently being served bytes. | |
| void | tap_request_port () |
| Resume reading after a tap's needs change. | |
| bool | is_device_connected () const |
| Whether the underlying device is present. | |
| void | tap_pump () |
| Run one read-and-dispatch cycle immediately. | |
Public Member Functions inherited from esphome::uart::UARTDevice | |
| UARTDevice ()=default | |
| UARTDevice (UARTComponent *parent) | |
| void | set_uart_parent (UARTComponent *parent) |
| void | write_byte (uint8_t data) |
| void | set_rx_full_threshold (size_t rx_full_threshold) |
| void | set_rx_full_threshold_ms (size_t time) |
| size_t | get_rx_full_threshold () |
| void | set_rx_timeout (size_t rx_timeout) |
| size_t | get_rx_timeout () |
| void | write_array (const uint8_t *data, size_t len) |
| void | write_array_progmem (const uint8_t *data, size_t len) |
| void | write_array (const std::vector< uint8_t > &data) |
| template<size_t N> | |
| void | write_array (const std::array< uint8_t, N > &data) |
| void | write_str (const char *str) |
| bool | read_byte (uint8_t *data) |
| bool | peek_byte (uint8_t *data) |
| bool | read_array (uint8_t *data, size_t len) |
| template<size_t N> | |
| optional< std::array< uint8_t, N > > | read_array () |
| size_t | available () |
| UARTFlushResult | flush () |
| int | read () |
| size_t | write (uint8_t data) |
| int | peek () |
| ESPDEPRECATED ("Use uart.final_validate_device_schema() in Python instead. Removed in 2027.3.0", "2026.9.0") void check_uart_settings(uint32_t baud_rate | |
| Check that the configuration of the UART bus matches the provided values and otherwise print a warning. | |
Public Member Functions inherited from esphome::Component | |
| float | get_actual_setup_priority () const |
| void | set_setup_priority (float priority) |
| void | call () |
| virtual void | on_shutdown () |
| virtual void | on_safe_shutdown () |
| virtual bool | teardown () |
| Called during teardown to allow component to gracefully finish operations. | |
| virtual void | on_powerdown () |
| Called after teardown is complete to power down hardware. | |
| uint8_t | get_component_state () const |
| void | reset_to_construction_state () |
| Reset this component back to the construction state to allow setup to run again. | |
| bool | is_in_loop_state () const |
| Check if this component has completed setup and is in the loop state. | |
| bool | is_idle () const |
| Check if this component is idle. | |
| void | mark_failed () |
| Mark this component as failed. | |
| void | mark_failed (const LogString *message) |
| void | disable_loop () |
| Disable this component's loop. | |
| void | enable_loop () |
| Enable this component's loop. | |
| void | enable_loop_soon_any_context () |
| Thread and ISR-safe version of enable_loop() that can be called from any context. | |
| bool | is_failed () const |
| bool | is_ready () const |
| virtual bool | can_proceed () |
| bool | status_has_warning () const |
| bool | status_has_error () const |
| void | status_set_warning () |
| void | status_set_warning (const char *message) |
| void | status_set_warning (const LogString *message) |
| void | status_set_error () |
| void | status_set_error (const LogString *message) |
| void | status_clear_warning () |
| void | status_clear_error () |
| ESPDEPRECATED ("Use status_momentary_warning() without a name. Removed in 2027.5.0", "2026.11.0") void status_momentary_warning(const char *name | |
| void | status_momentary_warning (uint32_t length=5000) |
Set warning status flag and clear it after length ms. | |
| void | status_momentary_warning (const LogString *message, uint32_t length=5000) |
| ESPDEPRECATED ("Use status_momentary_error() without a name. Removed in 2027.5.0", "2026.11.0") void status_momentary_error(const char *name | |
| void | status_momentary_error (uint32_t length=5000) |
Set error status flag and clear it after length ms. | |
| void | status_momentary_error (const LogString *message, uint32_t length=5000) |
| bool | has_overridden_loop () const |
| const LogString * | get_component_log_str () const ESPHOME_ALWAYS_INLINE |
| Get the integration where this component was declared as a LogString for logging. | |
| bool | should_warn_of_blocking (uint32_t blocking_time, uint32_t &threshold_ms_out) |
Protected Types | |
| using | IdentityScratch = usb_host::UsbDeviceInfo |
Protected Member Functions | |
| void | read_and_send_ (size_t available) |
| Read from UART, hand the bytes to any tap, and forward them to a subscriber (slow path with a 256-byte stack buffer) | |
| bool | is_subscriber_ (api::APIConnection *api_connection) const |
| True when the given connection is the live subscriber. | |
| template<typename F > | |
| bool | with_modem_uart_ (F &&f) const |
| Call f with the UART behind this port if that UART drives the modem lines itself. | |
| uint32_t | wire_time_ms_ (size_t bytes) const |
| Time the wire needs for the given number of bytes at the current framing. | |
| void | reset_mode_ () |
| Return the port to RAW when a subscriber goes away, so the mode never outlives it. | |
| void | reset_mode_ () |
| Without a tap, PROTOCOL is refused, so the mode is fixed at RAW and there is nothing to reset. | |
| bool | tap_observing_ () const |
| True when the tap should be shown the traffic passing through this port. | |
| void | fill_identity_ (IdentityScratch &scratch, api::SerialProxyIdentity &msg) const |
| Fill an identity message for this port. | |
| void | on_usb_connection_changed_ (bool connected) |
| The USB device behind this port was attached or removed; report the port's new identity to every subscribed API client. | |
Protected Member Functions inherited from esphome::Component | |
| friend | void::setup () |
| friend | void::original_setup () |
| void | set_component_source_ (uint8_t index) |
| Set where this component was loaded from for some debug messages. | |
| virtual void | call_setup () |
| void | call_dump_config_ () |
| void | enable_loop_slow_path_ () |
| void | set_component_state_ (uint8_t state) |
| Helper to set component state (clears state bits and sets new state) | |
| bool | set_status_flag_ (uint8_t flag) |
| Helper to set a status LED flag on both this component and the app. | |
| bool | set_error_flag_ (const LogString *message) |
| Set the error flag and log it; true when it was not set before. | |
| void | set_interval (const char *name, uint32_t interval, std::function< void()> &&f) |
| Set an interval function with a const char* name. | |
| void | set_interval (uint32_t id, uint32_t interval, std::function< void()> &&f) |
| Set an interval function with a numeric ID (zero heap allocation). | |
| void | set_interval (InternalSchedulerID id, uint32_t interval, std::function< void()> &&f) |
| void | set_interval (uint32_t interval, std::function< void()> &&f) |
| bool | cancel_interval (const char *name) |
| Cancel an interval function. | |
| bool | cancel_interval (uint32_t id) |
| bool | cancel_interval (InternalSchedulerID id) |
| void | set_timeout (const char *name, uint32_t timeout, std::function< void()> &&f) |
| Set a timeout function with a const char* name. | |
| void | set_timeout (uint32_t id, uint32_t timeout, std::function< void()> &&f) |
| Set a timeout function with a numeric ID (zero heap allocation). | |
| void | set_timeout (InternalSchedulerID id, uint32_t timeout, std::function< void()> &&f) |
| void | set_timeout (uint32_t timeout, std::function< void()> &&f) |
| bool | cancel_timeout (const char *name) |
| Cancel a timeout function. | |
| bool | cancel_timeout (uint32_t id) |
| bool | cancel_timeout (InternalSchedulerID id) |
| void | defer (const char *name, std::function< void()> &&f) |
| Defer a callback to the next loop() call with a const char* name. | |
| void | defer (std::function< void()> &&f) |
| Defer a callback to the next loop() call. | |
| void | defer (uint32_t id, std::function< void()> &&f) |
| Defer a callback with a numeric ID (zero heap allocation) | |
| bool | cancel_defer (const char *name) |
| Cancel a defer callback using the specified name, name must not be empty. | |
| bool | cancel_defer (uint32_t id) |
| void | status_clear_warning_slow_path_ () |
| void | status_clear_error_slow_path_ () |
Protected Attributes | |
| uint32_t | instance_index_ {0} |
| Instance index for identifying this proxy in API messages. | |
| api::APIConnection * | api_connection_ {nullptr} |
| Subscribed API client (only one allowed at a time) | |
| api::SerialProxyDataReceived | outgoing_msg_ |
| Pre-allocated outgoing message; instance field is set once in setup() | |
| const char * | name_ {nullptr} |
| Human-readable port name (points to a string literal in flash) | |
| api::enums::SerialProxyPortType | port_type_ {} |
| Port type. | |
| api::enums::SerialProxyMode | mode_ {} |
| How the bytes passing through are treated; zero is SERIAL_PROXY_MODE_RAW. | |
| GPIOPin * | rts_pin_ {nullptr} |
| Optional GPIO pins for modem control. | |
| GPIOPin * | dtr_pin_ {nullptr} |
| bool | rts_state_ {false} |
| Current modem pin states. | |
| bool | dtr_state_ {false} |
| bool | trim_warned_ {false} |
| Set while writes are being trimmed, so a client streaming into a slow port warns once. | |
| SerialProxyTap * | tap_ {nullptr} |
| usb_uart::USBUartChannel * | usb_channel_ {nullptr} |
| The USB UART channel behind this port; nullptr on non-USB ports. | |
| uart::HostUartComponent * | host_uart_ {nullptr} |
| The host UART behind this port; nullptr on other ports. | |
Protected Attributes inherited from esphome::uart::UARTDevice | |
| UARTComponent * | parent_ {nullptr} |
Protected Attributes inherited from esphome::Component | |
| uint8_t | component_source_index_ {0} |
| Index into component source PROGMEM lookup table (0 = not set) | |
| uint8_t | warn_if_blocking_over_ {WARN_IF_BLOCKING_OVER_CS} |
| Warn threshold in centiseconds (max 2550ms) | |
| uint8_t | component_state_ {0x00} |
| State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION, 0x01=SETUP, 0x02=LOOP, 0x03=FAILED, 0x04=LOOP_DONE) Bit 3: STATUS_LED_WARNING Bit 4: STATUS_LED_ERROR Bit 5: Has overridden loop() (set at registration time) Bits 6-7: Unused - reserved for future expansion. | |
| volatile bool | pending_enable_loop_ {false} |
| ISR-safe flag for enable_loop_soon_any_context. | |
| ComponentRuntimeStats | runtime_stats_ |
Static Protected Attributes | |
| static uint32_t | stall_loop_time = 0 |
| Stall spent by writes in the current loop pass, keyed by the pass's cached start time. | |
| static uint32_t | stall_spent_ms = 0 |
Additional Inherited Members | |
Data Fields inherited from esphome::uart::UARTDevice | |
| uint8_t | stop_bits = 1 |
| uint8_t UARTParityOptions | parity = UART_CONFIG_PARITY_NONE |
| uint8_t UARTParityOptions uint8_t | data_bits = 8) |
Data Fields inherited from esphome::Component | |
| uint32_t | length = 5000) |
Definition at line 115 of file serial_proxy.h.
|
protected |
Definition at line 290 of file serial_proxy.h.
| SerialProxyResult esphome::serial_proxy::SerialProxy::configure | ( | api::APIConnection * | api_connection, |
| uint32_t | baudrate, | ||
| bool | flow_control, | ||
| uint8_t | parity, | ||
| uint8_t | stop_bits, | ||
| uint8_t | data_size ) |
Configure UART parameters and apply them.
| api_connection | The API connection requesting the change |
| baudrate | Baud rate in bits per second |
| flow_control | True to enable hardware flow control |
| parity | Parity setting (0=none, 1=even, 2=odd) |
| stop_bits | Number of stop bits (1 or 2) |
| data_size | Number of data bits (5-8) |
Definition at line 186 of file serial_proxy.cpp.
|
overridevirtual |
Reimplemented from esphome::Component.
Definition at line 170 of file serial_proxy.cpp.
|
protected |
Fill an identity message for this port.
The message's strings are views into scratch, so it must outlive the send.
Definition at line 418 of file serial_proxy.cpp.
| SerialProxyResult esphome::serial_proxy::SerialProxy::flush_port | ( | api::APIConnection * | api_connection | ) |
Flush the serial port (block until all TX data is sent)
| api_connection | The API connection requesting the flush |
Definition at line 487 of file serial_proxy.cpp.
|
inline |
Get the currently subscribed API connection (nullptr if none)
Definition at line 154 of file serial_proxy.h.
|
inline |
Get the modem pins this instance can drive as a bitmask of SerialProxyLineStateFlag values.
A line is driven by its GPIO pin when one is configured, otherwise by the UART if it can.
Definition at line 173 of file serial_proxy.h.
|
inline |
Get the instance index (position in Application's serial_proxies_ vector)
Definition at line 123 of file serial_proxy.h.
| uint32_t esphome::serial_proxy::SerialProxy::get_modem_pins | ( | ) | const |
Get current modem pin states as a bitmask of SerialProxyLineStateFlag values.
Definition at line 474 of file serial_proxy.cpp.
|
inline |
Get the human-readable port name.
Definition at line 132 of file serial_proxy.h.
|
inline |
Get the port type.
Definition at line 138 of file serial_proxy.h.
|
inlineoverridevirtual |
Reimplemented from esphome::Component.
Definition at line 120 of file serial_proxy.h.
|
inline |
Whether the underlying device is present.
On a USB UART this tracks enumeration, so a tap can notice the device being unplugged and plugged back in.
Definition at line 232 of file serial_proxy.h.
|
inlineprotected |
True when the given connection is the live subscriber.
Every port operation (write, configure, modem pins, flush, mode) requires this, so an unsubscribed client can never share the wire with the subscriber or an active tap.
Definition at line 250 of file serial_proxy.h.
|
overridevirtual |
Reimplemented from esphome::Component.
Definition at line 80 of file serial_proxy.cpp.
|
protected |
The USB device behind this port was attached or removed; report the port's new identity to every subscribed API client.
Definition at line 451 of file serial_proxy.cpp.
|
protected |
Read from UART, hand the bytes to any tap, and forward them to a subscriber (slow path with a 256-byte stack buffer)
|
protected |
Return the port to RAW when a subscriber goes away, so the mode never outlives it.
Definition at line 66 of file serial_proxy.cpp.
|
inlineprotected |
Without a tap, PROTOCOL is refused, so the mode is fixed at RAW and there is nothing to reset.
Definition at line 280 of file serial_proxy.h.
| void esphome::serial_proxy::SerialProxy::send_identity | ( | api::APIConnection * | api_connection | ) |
Send this port's identity to one client.
Definition at line 411 of file serial_proxy.cpp.
| SerialProxyResult esphome::serial_proxy::SerialProxy::serial_proxy_request | ( | api::APIConnection * | api_connection, |
| api::enums::SerialProxyRequestType | type ) |
Handle a subscribe/unsubscribe request from an API client.
Definition at line 510 of file serial_proxy.cpp.
|
inline |
Set the DTR GPIO pin (from YAML configuration)
Definition at line 187 of file serial_proxy.h.
|
inline |
Attach the host UART behind this port (from code generation)
Definition at line 196 of file serial_proxy.h.
|
inline |
Set the instance index (called by Application::register_serial_proxy)
Definition at line 126 of file serial_proxy.h.
| SerialProxyResult esphome::serial_proxy::SerialProxy::set_mode_from_client | ( | api::APIConnection * | api_connection, |
| api::enums::SerialProxyMode | mode ) |
Handle a mode change requested by an API client.
Definition at line 257 of file serial_proxy.cpp.
| SerialProxyResult esphome::serial_proxy::SerialProxy::set_modem_pins | ( | api::APIConnection * | api_connection, |
| uint32_t | line_states ) |
Set modem pin states from a bitmask of SerialProxyLineStateFlag values.
Definition at line 367 of file serial_proxy.cpp.
|
inline |
Set the human-readable port name (from YAML configuration)
Definition at line 129 of file serial_proxy.h.
|
inline |
Set the port type (from YAML configuration)
Definition at line 135 of file serial_proxy.h.
|
inline |
Set the RTS GPIO pin (from YAML configuration)
Definition at line 184 of file serial_proxy.h.
|
inline |
Attach a traffic observer. At most one, set once at setup time.
Definition at line 206 of file serial_proxy.h.
|
inline |
Attach the USB UART channel behind this port (from code generation)
Definition at line 191 of file serial_proxy.h.
|
overridevirtual |
Reimplemented from esphome::Component.
Definition at line 32 of file serial_proxy.cpp.
|
inline |
Whether the tap is currently being served bytes.
Can flip false with no callback (a subscriber attaching in RAW mode, say), so a tap should check before starting protocol work and when a reply seems overdue.
Definition at line 223 of file serial_proxy.h.
|
protected |
True when the tap should be shown the traffic passing through this port.
Definition at line 140 of file serial_proxy.cpp.
| void esphome::serial_proxy::SerialProxy::tap_pump | ( | ) |
Run one read-and-dispatch cycle immediately.
Lets a tap make progress before the main loop is running – during setup, for instance, while a component is still blocking on can_proceed(). Must not be called from on_device_rx() or on_client_tx(): each nested cycle costs a 256-byte stack frame.
Definition at line 156 of file serial_proxy.cpp.
|
inline |
Resume reading after a tap's needs change.
loop() disables itself when there is neither a subscriber nor a tap that wants the port, so a tap starting fresh work must ask for it back. Must be called from the main loop.
Definition at line 228 of file serial_proxy.h.
|
protected |
Time the wire needs for the given number of bytes at the current framing.
Definition at line 361 of file serial_proxy.cpp.
|
inlineprotected |
Call f with the UART behind this port if that UART drives the modem lines itself.
Returns false when it cannot. Typed pointers, so the calls are direct.
Definition at line 255 of file serial_proxy.h.
| void esphome::serial_proxy::SerialProxy::write_from_client | ( | api::APIConnection * | api_connection, |
| const uint8_t * | data, | ||
| size_t | len ) |
Write data received from an API client to the serial device.
| api_connection | The API connection sending the data |
| data | Pointer to data buffer |
| len | Number of bytes to write |
Definition at line 304 of file serial_proxy.cpp.
|
inline |
Write bytes originating from the tap rather than from a client.
Bypasses the subscriber ownership check, but only while the tap is being served bytes – so a port in RAW mode with a subscriber attached stays inert. Returns false when the bytes were dropped for that reason.
Definition at line 212 of file serial_proxy.h.
|
protected |
Subscribed API client (only one allowed at a time)
Definition at line 315 of file serial_proxy.h.
|
protected |
Definition at line 335 of file serial_proxy.h.
|
protected |
Definition at line 339 of file serial_proxy.h.
|
protected |
The host UART behind this port; nullptr on other ports.
Definition at line 355 of file serial_proxy.h.
|
protected |
Instance index for identifying this proxy in API messages.
Definition at line 306 of file serial_proxy.h.
|
protected |
How the bytes passing through are treated; zero is SERIAL_PROXY_MODE_RAW.
Definition at line 330 of file serial_proxy.h.
|
protected |
Human-readable port name (points to a string literal in flash)
Definition at line 323 of file serial_proxy.h.
|
protected |
Pre-allocated outgoing message; instance field is set once in setup()
Definition at line 319 of file serial_proxy.h.
|
protected |
Port type.
Definition at line 326 of file serial_proxy.h.
|
protected |
Optional GPIO pins for modem control.
Definition at line 334 of file serial_proxy.h.
|
protected |
Current modem pin states.
Definition at line 338 of file serial_proxy.h.
|
staticprotected |
Stall spent by writes in the current loop pass, keyed by the pass's cached start time.
Static on purpose: there is one main loop, and writes to different ports that arrive in the same pass all stall it, so the budget is one per device rather than one per port
Definition at line 311 of file serial_proxy.h.
|
staticprotected |
Definition at line 312 of file serial_proxy.h.
|
protected |
Definition at line 345 of file serial_proxy.h.
|
protected |
Set while writes are being trimmed, so a client streaming into a slow port warns once.
Definition at line 342 of file serial_proxy.h.
|
protected |
The USB UART channel behind this port; nullptr on non-USB ports.
Definition at line 350 of file serial_proxy.h.