17static const char *
const TAG =
"serial_proxy";
48 ESP_LOGW(TAG,
"Subscriber disconnected");
67 size_t to_read = std::min(
available,
sizeof(buffer));
79 "Serial Proxy [%" PRIu32
"]:\n"
88 this->
rts_pin_ !=
nullptr ?
"configured" :
"not configured",
89 this->
dtr_pin_ !=
nullptr ?
"configured" :
"not configured");
95 "Configuring serial proxy [%" PRIu32
"]: baud=%" PRIu32
", flow_ctrl=%s, parity=%" PRIu8
", stop=%" PRIu8
97 this->
instance_index_, baudrate, YESNO(flow_control), parity, stop_bits, data_size);
99 auto *uart_comp = this->
parent_;
100 if (uart_comp ==
nullptr) {
101 ESP_LOGE(TAG,
"UART component not available");
107 ESP_LOGW(TAG,
"Invalid baud rate: 0");
110 if (stop_bits < 1 || stop_bits > 2) {
111 ESP_LOGW(TAG,
"Invalid stop bits: %u (must be 1 or 2)", stop_bits);
114 if (data_size < 5 || data_size > 8) {
115 ESP_LOGW(TAG,
"Invalid data bits: %u (must be 5-8)", data_size);
119 ESP_LOGW(TAG,
"Invalid parity: %u (must be 0-2)", parity);
125 uart_comp->set_stop_bits(stop_bits);
126 uart_comp->set_data_bits(data_size);
134 uart_comp->set_parity(PARITY_MAP[parity]);
137#if defined(USE_ESP8266) || defined(USE_ESP32)
138 uart_comp->load_settings(
true);
142 ESP_LOGW(TAG,
"Hardware flow control requested but is not yet supported");
147 if (data ==
nullptr ||
len == 0)
155 ESP_LOGV(TAG,
"Setting modem pins [%" PRIu32
"]: RTS=%s, DTR=%s", this->
instance_index_, ONOFF(rts), ONOFF(dtr));
173 ESP_LOGV(TAG,
"Flushing serial proxy [%" PRIu32
"]", this->
instance_index_);
174 return this->
flush();
182 ESP_LOGE(TAG,
"Only one API subscription is allowed at a time");
187 ESP_LOGV(TAG,
"API connection subscribed to serial proxy [%" PRIu32
"]", this->
instance_index_);
191 ESP_LOGV(TAG,
"API connection is not subscribed to serial proxy [%" PRIu32
"]", this->
instance_index_);
196 ESP_LOGV(TAG,
"API connection unsubscribed from serial proxy [%" PRIu32
"]", this->
instance_index_);
199 ESP_LOGW(TAG,
"Unknown serial proxy request type: %" PRIu32,
static_cast<uint32_t>(
type));
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
virtual void digital_write(bool value)=0
void send_serial_proxy_data(const SerialProxyDataReceived &msg)
bool is_marked_for_removal() const
void set_data(const uint8_t *data, size_t len)
void read_and_send_(size_t available)
Read from UART and send to API client (slow path with 256-byte stack buffer)
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 rts_state_
Current modem pin states.
void configure(uint32_t baudrate, bool flow_control, uint8_t parity, uint8_t stop_bits, uint8_t data_size)
Configure UART parameters and apply them.
void serial_proxy_request(api::APIConnection *api_connection, api::enums::SerialProxyRequestType type)
Handle a subscribe/unsubscribe request from an API client.
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.
uart::UARTFlushResult flush_port()
Flush the serial port (block until all TX data is sent)
void set_modem_pins(uint32_t line_states)
Set modem pin states from a bitmask of SerialProxyLineStateFlag values.
void write_from_client(const uint8_t *data, size_t len)
Write data received from an API client to the serial device.
void dump_config() override
api::APIConnection * api_connection_
Subscribed API client (only one allowed at a time)
void set_baud_rate(uint32_t baud_rate)
optional< std::array< uint8_t, N > > read_array()
void write_array(const uint8_t *data, size_t len)
struct @65::@66 __attribute__
@ SERIAL_PROXY_PORT_TYPE_RS232
@ SERIAL_PROXY_PORT_TYPE_RS485
@ SERIAL_PROXY_REQUEST_TYPE_UNSUBSCRIBE
@ SERIAL_PROXY_REQUEST_TYPE_SUBSCRIBE
@ SERIAL_PROXY_LINE_STATE_FLAG_RTS
RTS (Request To Send)
@ SERIAL_PROXY_LINE_STATE_FLAG_DTR
DTR (Data Terminal Ready)
constexpr size_t SERIAL_PROXY_MAX_READ_SIZE
Maximum bytes to read from UART in a single loop iteration.
@ UART_CONFIG_PARITY_EVEN
@ UART_CONFIG_PARITY_NONE
UARTFlushResult
Result of a flush() call.
bool api_is_connected()
Return whether the node has at least one client connected to the native API.