12 namespace improv_serial {
14 static const char *
const TAG =
"improv_serial";
26 this->
state_ = improv::STATE_PROVISIONED;
35 optional<uint8_t> byte;
47 #if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6) && \ 48 !defined(USE_ESP32_VARIANT_ESP32S2) && !defined(USE_ESP32_VARIANT_ESP32S3) 50 #endif // !USE_ESP32_VARIANT_ESP32C3 && !USE_ESP32_VARIANT_ESP32S2 && !USE_ESP32_VARIANT_ESP32S3 53 uart_get_buffered_data_len(this->
uart_num_, &available);
55 uart_read_bytes(this->
uart_num_, &data, 1, 0);
60 #if defined(USE_LOGGER_USB_CDC) && defined(CONFIG_ESP_CONSOLE_USB_CDC) 62 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) 63 if (esp_usb_console_available_for_read()) {
65 if (esp_usb_console_read_available()) {
67 esp_usb_console_read_buf((
char *) &data, 1);
71 #endif // USE_LOGGER_USB_CDC 72 #ifdef USE_LOGGER_USB_SERIAL_JTAG 74 if (usb_serial_jtag_read_bytes((
char *) &data, 1, 0)) {
79 #endif // USE_LOGGER_USB_SERIAL_JTAG 90 this->
hw_serial_->write(data.data(), data.size());
96 #if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6) && \ 97 !defined(USE_ESP32_VARIANT_ESP32S2) && !defined(USE_ESP32_VARIANT_ESP32S3) 99 #endif // !USE_ESP32_VARIANT_ESP32C3 && !USE_ESP32_VARIANT_ESP32S2 && !USE_ESP32_VARIANT_ESP32S3 100 uart_write_bytes(this->
uart_num_, data.data(), data.size());
102 #if defined(USE_LOGGER_USB_CDC) && defined(CONFIG_ESP_CONSOLE_USB_CDC) 104 const char *msg = (
char *) data.data();
105 esp_usb_console_write_buf(msg, data.size());
108 #endif // USE_LOGGER_USB_CDC 109 #ifdef USE_LOGGER_USB_SERIAL_JTAG 111 usb_serial_jtag_write_bytes((
char *) data.data(), data.size(), 20 / portTICK_PERIOD_MS);
113 usb_serial_jtag_ll_txfifo_flush();
115 #endif // USE_LOGGER_USB_SERIAL_JTAG 126 ESP_LOGV(TAG,
"Improv Serial timeout");
130 while (byte.has_value()) {
140 if (this->
state_ == improv::STATE_PROVISIONING) {
155 std::vector<std::string> urls;
162 std::string webserver_url =
"http://" + ip.str() +
":" +
to_string(USE_WEBSERVER_PORT);
163 urls.push_back(webserver_url);
168 std::vector<uint8_t> data = improv::build_rpc_response(command, urls,
false);
173 #ifdef ESPHOME_PROJECT_NAME 174 std::vector<std::string> infos = {ESPHOME_PROJECT_NAME, ESPHOME_PROJECT_VERSION, ESPHOME_VARIANT,
App.
get_name()};
176 std::vector<std::string> infos = {
"ESPHome", ESPHOME_VERSION, ESPHOME_VARIANT,
App.
get_name()};
178 std::vector<uint8_t> data = improv::build_rpc_response(improv::GET_DEVICE_INFO, infos,
false);
185 ESP_LOGV(TAG,
"Improv Serial byte: 0x%02X", byte);
188 return improv::parse_improv_serial_byte(
189 at, byte, raw, [
this](improv::ImprovCommand command) ->
bool {
return this->
parse_improv_payload_(command); },
191 ESP_LOGW(TAG,
"Error decoding Improv payload");
197 switch (command.command) {
198 case improv::WIFI_SETTINGS: {
200 sta.set_ssid(command.ssid);
201 sta.set_password(command.password);
207 ESP_LOGD(TAG,
"Received Improv wifi settings ssid=%s, password=" LOG_SECRET(
"%s"), command.ssid.c_str(),
208 command.password.c_str());
211 this->
set_timeout(
"wifi-connect-timeout", 30000, f);
214 case improv::GET_CURRENT_STATE:
216 if (this->
state_ == improv::STATE_PROVISIONED) {
221 case improv::GET_DEVICE_INFO: {
226 case improv::GET_WIFI_NETWORKS: {
227 std::vector<std::string> networks;
229 for (
auto &scan : results) {
230 if (scan.get_is_hidden())
232 const std::string &ssid = scan.get_ssid();
233 if (std::find(networks.begin(), networks.end(), ssid) != networks.end())
236 std::vector<uint8_t> data = improv::build_rpc_response(
237 improv::GET_WIFI_NETWORKS, {ssid,
str_sprintf(
"%d", scan.get_rssi()), YESNO(scan.get_with_auth())},
false);
239 networks.push_back(ssid);
242 std::vector<uint8_t> data =
243 improv::build_rpc_response(improv::GET_WIFI_NETWORKS, std::vector<std::string>{},
false);
248 ESP_LOGW(TAG,
"Unknown Improv payload");
258 std::vector<uint8_t> data = {
'I',
'M',
'P',
'R',
'O',
'V'};
260 data[6] = IMPROV_SERIAL_VERSION;
266 for (uint8_t d : data)
274 std::vector<uint8_t> data = {
'I',
'M',
'P',
'R',
'O',
'V'};
276 data[6] = IMPROV_SERIAL_VERSION;
281 uint8_t checksum = 0x00;
282 for (uint8_t d : data)
289 std::vector<uint8_t> data = {
'I',
'M',
'P',
'R',
'O',
'V'};
291 data[6] = IMPROV_SERIAL_VERSION;
293 data[8] = response.size();
294 data.insert(data.end(), response.begin(), response.end());
296 uint8_t checksum = 0x00;
297 for (uint8_t d : data)
299 data.push_back(checksum);
305 this->
set_error_(improv::ERROR_UNABLE_TO_CONNECT);
307 ESP_LOGW(TAG,
"Timed out trying to connect to given WiFi network");
void write_data_(std::vector< uint8_t > &data)
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
const std::string & get_password() const
bool parse_improv_payload_(improv::ImprovCommand &command)
void dump_config() override
optional< uint8_t > read_byte_()
void save_wifi_sta(const std::string &ssid, const std::string &password)
std::string get_formatted_next_url_()
const std::vector< WiFiScanResult > & get_scan_result() const
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool parse_improv_serial_byte_(uint8_t byte)
void set_state_(improv::State state)
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
uint32_t IRAM_ATTR HOT millis()
wifi::WiFiAP connecting_sta_
void start_connecting(const WiFiAP &ap, bool two)
std::vector< uint8_t > build_rpc_settings_response_(improv::Command command)
void send_response_(std::vector< uint8_t > &response)
std::string str_sprintf(const char *fmt,...)
void set_error_(improv::Error error)
Application App
Global storage of Application pointer - only one Application can exist.
WiFiComponent * global_wifi_component
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Stream * get_hw_serial() const
std::string to_string(int value)
void on_wifi_connect_timeout_()
void set_sta(const WiFiAP &ap)
Implementation of SPI Controller mode.
const std::string & get_ssid() const
std::vector< uint8_t > rx_buffer_
uart_port_t get_uart_num() const
std::vector< uint8_t > build_version_info_()
ImprovSerialComponent * global_improv_serial_component
void IRAM_ATTR HOT delay(uint32_t ms)