ESPHome  2024.11.1
improv_serial_component.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/defines.h"
7 #include "esphome/core/helpers.h"
8 #ifdef USE_WIFI
9 #include <improv.h>
10 #include <vector>
11 
12 #ifdef USE_ARDUINO
13 #include <HardwareSerial.h>
14 #endif
15 #ifdef USE_ESP_IDF
16 #include <driver/uart.h>
17 #if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) || \
18  defined(USE_ESP32_VARIANT_ESP32H2)
19 #include <driver/usb_serial_jtag.h>
20 #include <hal/usb_serial_jtag_ll.h>
21 #endif
22 #if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
23 #include <esp_private/usb_console.h>
24 #endif
25 #endif
26 
27 namespace esphome {
28 namespace improv_serial {
29 
30 enum ImprovSerialType : uint8_t {
33  TYPE_RPC = 0x03,
35 };
36 
37 static const uint16_t IMPROV_SERIAL_TIMEOUT = 100;
38 static const uint8_t IMPROV_SERIAL_VERSION = 1;
39 
41  public:
42  void setup() override;
43  void loop() override;
44  void dump_config() override;
45 
46  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
47 
48  protected:
49  bool parse_improv_serial_byte_(uint8_t byte);
50  bool parse_improv_payload_(improv::ImprovCommand &command);
51 
53  void set_error_(improv::Error error);
54  void send_response_(std::vector<uint8_t> &response);
56 
57  std::vector<uint8_t> build_rpc_settings_response_(improv::Command command);
58  std::vector<uint8_t> build_version_info_();
59 
61  void write_data_(std::vector<uint8_t> &data);
62 
63 #ifdef USE_ARDUINO
64  Stream *hw_serial_{nullptr};
65 #endif
66 #ifdef USE_ESP_IDF
67  uart_port_t uart_num_;
68 #endif
69 
70  std::vector<uint8_t> rx_buffer_;
71  uint32_t last_read_byte_{0};
73  improv::State state_{improv::STATE_AUTHORIZED};
74 };
75 
77  *global_improv_serial_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
78 
79 } // namespace improv_serial
80 } // namespace esphome
81 #endif
void write_data_(std::vector< uint8_t > &data)
bool parse_improv_payload_(improv::ImprovCommand &command)
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:26
std::vector< uint8_t > build_rpc_settings_response_(improv::Command command)
void send_response_(std::vector< uint8_t > &response)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< uint8_t > build_version_info_()
bool state
Definition: fan.h:34
ImprovSerialComponent * global_improv_serial_component