ESPHome  2024.3.2
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 
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 #endif
21 #if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
22 #include <esp_private/usb_console.h>
23 #endif
24 #endif
25 
26 namespace esphome {
27 namespace improv_serial {
28 
29 enum ImprovSerialType : uint8_t {
32  TYPE_RPC = 0x03,
34 };
35 
36 static const uint16_t IMPROV_SERIAL_TIMEOUT = 100;
37 static const uint8_t IMPROV_SERIAL_VERSION = 1;
38 
40  public:
41  void setup() override;
42  void loop() override;
43  void dump_config() override;
44 
45  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
46 
47  protected:
48  bool parse_improv_serial_byte_(uint8_t byte);
49  bool parse_improv_payload_(improv::ImprovCommand &command);
50 
52  void set_error_(improv::Error error);
53  void send_response_(std::vector<uint8_t> &response);
55 
56  std::vector<uint8_t> build_rpc_settings_response_(improv::Command command);
57  std::vector<uint8_t> build_version_info_();
58 
60  void write_data_(std::vector<uint8_t> &data);
61 
62 #ifdef USE_ARDUINO
63  Stream *hw_serial_{nullptr};
64 #endif
65 #ifdef USE_ESP_IDF
66  uart_port_t uart_num_;
67 #endif
68 
69  std::vector<uint8_t> rx_buffer_;
70  uint32_t last_read_byte_{0};
72  improv::State state_{improv::STATE_AUTHORIZED};
73 };
74 
76  *global_improv_serial_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
77 
78 } // namespace improv_serial
79 } // namespace esphome
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)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
std::vector< uint8_t > build_version_info_()
bool state
Definition: fan.h:34
ImprovSerialComponent * global_improv_serial_component