ESPHome 2026.9.0-dev
Loading...
Searching...
No Matches
serial_proxy.h
Go to the documentation of this file.
1#pragma once
2
3// WARNING: This component is EXPERIMENTAL. The API may change at any time
4// without following the normal breaking changes policy. Use at your own risk.
5// Once the API is considered stable, this warning will be removed.
6
8
9#ifdef USE_SERIAL_PROXY
10
12#include "esphome/core/hal.h"
14
15// Include api_pb2.h only when the API is enabled. The full include is needed
16// to hold SerialProxyDataReceived by value as a pre-allocated member.
17// Guarding prevents pulling conflicting Zephyr logging macro names into
18// translation units that include this header without USE_API defined.
19#ifdef USE_API
21#endif
22
23// Forward-declare types needed outside the USE_API guard.
24namespace esphome::api {
25class APIConnection;
26namespace enums {
29} // namespace enums
30} // namespace esphome::api
31
32namespace esphome::serial_proxy {
33
40
51
53inline constexpr size_t SERIAL_PROXY_MAX_READ_SIZE = 256;
54
55class SerialProxy final : public uart::UARTDevice, public Component {
56 public:
57 void setup() override;
58 void loop() override;
59 void dump_config() override;
60 float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
61
64
66 void set_instance_index(uint32_t index) { this->instance_index_ = index; }
67
69 void set_name(const char *name) { this->name_ = name; }
70
72 const char *get_name() const { return this->name_; }
73
75 void set_port_type(api::enums::SerialProxyPortType port_type) { this->port_type_ = port_type; }
76
79
87 SerialProxyResult configure(api::APIConnection *api_connection, uint32_t baudrate, bool flow_control, uint8_t parity,
88 uint8_t stop_bits, uint8_t data_size);
89
92
95
100 void write_from_client(api::APIConnection *api_connection, const uint8_t *data, size_t len);
101
103 SerialProxyResult set_modem_pins(api::APIConnection *api_connection, uint32_t line_states);
104
106 uint32_t get_modem_pins() const;
107
110 return (this->rts_pin_ != nullptr ? static_cast<uint32_t>(SERIAL_PROXY_LINE_STATE_FLAG_RTS) : 0u) |
111 (this->dtr_pin_ != nullptr ? static_cast<uint32_t>(SERIAL_PROXY_LINE_STATE_FLAG_DTR) : 0u);
112 }
113
117
119 void set_rts_pin(GPIOPin *pin) { this->rts_pin_ = pin; }
120
122 void set_dtr_pin(GPIOPin *pin) { this->dtr_pin_ = pin; }
123
124 protected:
125#ifdef USE_API
128
130 bool port_claimed_by_other_(api::APIConnection *api_connection) const;
131#endif
132
135
138
139#ifdef USE_API
142#endif
143
145 const char *name_{nullptr};
146
149
151 GPIOPin *rts_pin_{nullptr};
152 GPIOPin *dtr_pin_{nullptr};
153
155 bool rts_state_{false};
156 bool dtr_state_{false};
157};
158
159} // namespace esphome::serial_proxy
160
161#endif // USE_SERIAL_PROXY
float get_setup_priority() const override
void set_dtr_pin(GPIOPin *pin)
Set the DTR GPIO pin (from YAML configuration)
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.
void read_and_send_(size_t available)
Read from UART and send to API client (slow path with 256-byte stack buffer)
SerialProxyResult serial_proxy_request(api::APIConnection *api_connection, api::enums::SerialProxyRequestType type)
Handle a subscribe/unsubscribe request from an API client.
uint32_t get_modem_pins() const
Get current modem pin states as a bitmask of SerialProxyLineStateFlag values.
uint32_t instance_index_
Instance index for identifying this proxy in API messages.
bool port_claimed_by_other_(api::APIConnection *api_connection) const
True when a live subscriber other than the given connection holds the port.
void set_port_type(api::enums::SerialProxyPortType port_type)
Set the port type (from YAML configuration)
void set_instance_index(uint32_t index)
Set the instance index (called by Application::register_serial_proxy)
uint32_t get_instance_index() const
Get the instance index (position in Application's serial_proxies_ vector)
bool rts_state_
Current modem pin states.
void set_name(const char *name)
Set the human-readable port name (from YAML configuration)
SerialProxyResult flush_port(api::APIConnection *api_connection)
Flush the serial port (block until all TX data is sent)
uint32_t get_configured_modem_pins() const
Get the modem pins this instance can drive as a bitmask of SerialProxyLineStateFlag values.
api::SerialProxyDataReceived outgoing_msg_
Pre-allocated outgoing message; instance field is set once in setup()
const char * name_
Human-readable port name (points to a string literal in flash)
api::enums::SerialProxyPortType port_type_
Port type.
GPIOPin * rts_pin_
Optional GPIO pins for modem control.
void set_rts_pin(GPIOPin *pin)
Set the RTS GPIO pin (from YAML configuration)
SerialProxyResult set_modem_pins(api::APIConnection *api_connection, uint32_t line_states)
Set modem pin states from a bitmask of SerialProxyLineStateFlag values.
api::APIConnection * get_api_connection()
Get the currently subscribed API connection (nullptr if none)
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.
api::enums::SerialProxyPortType get_port_type() const
Get the port type.
api::APIConnection * api_connection_
Subscribed API client (only one allowed at a time)
const char * get_name() const
Get the human-readable port name.
uint16_t type
SerialProxyLineStateFlag
Bit flags for the line_states field exchanged with API clients.
@ SERIAL_PROXY_LINE_STATE_FLAG_RTS
RTS (Request To Send)
@ SERIAL_PROXY_LINE_STATE_FLAG_DTR
DTR (Data Terminal Ready)
SerialProxyResult
Result of a client-initiated operation; mapped to api::enums::SerialProxyStatus by the API layer.
@ SERIAL_PROXY_RESULT_TIMEOUT
Timed out before TX completed.
@ SERIAL_PROXY_RESULT_ERROR
Driver or hardware error.
@ SERIAL_PROXY_RESULT_NOT_SUPPORTED
Requested feature is not available on this instance.
@ SERIAL_PROXY_RESULT_PORT_IN_USE
Denied: another live client holds the port.
@ SERIAL_PROXY_RESULT_OK
Operation completed or request accepted.
@ SERIAL_PROXY_RESULT_INVALID_ARGUMENT
A parameter value is out of range.
@ SERIAL_PROXY_RESULT_ASSUMED_SUCCESS
Platform cannot confirm TX drain; success assumed.
constexpr size_t SERIAL_PROXY_MAX_READ_SIZE
Maximum bytes to read from UART in a single loop iteration.
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:57
const void size_t len
Definition hal.h:64
static void uint32_t