ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
improv_base.cpp
Go to the documentation of this file.
1#include "improv_base.h"
2
6
7namespace esphome {
8namespace improv_base {
9
10#if defined(USE_ESP32_IMPROV_NEXT_URL) || defined(USE_IMPROV_SERIAL_NEXT_URL)
11static constexpr const char DEVICE_NAME_PLACEHOLDER[] = "{{device_name}}";
12static constexpr size_t DEVICE_NAME_PLACEHOLDER_LEN = sizeof(DEVICE_NAME_PLACEHOLDER) - 1;
13static constexpr const char IP_ADDRESS_PLACEHOLDER[] = "{{ip_address}}";
14static constexpr size_t IP_ADDRESS_PLACEHOLDER_LEN = sizeof(IP_ADDRESS_PLACEHOLDER) - 1;
15
16static void replace_all_in_place(std::string &str, const char *placeholder, size_t placeholder_len,
17 const std::string &replacement) {
18 size_t pos = 0;
19 const size_t replacement_len = replacement.length();
20 while ((pos = str.find(placeholder, pos)) != std::string::npos) {
21 str.replace(pos, placeholder_len, replacement);
22 pos += replacement_len;
23 }
24}
25
27 if (this->next_url_.empty()) {
28 return "";
29 }
30
31 std::string formatted_url = this->next_url_;
32
33 // Replace all occurrences of {{device_name}}
34 replace_all_in_place(formatted_url, DEVICE_NAME_PLACEHOLDER, DEVICE_NAME_PLACEHOLDER_LEN, App.get_name());
35
36 // Replace all occurrences of {{ip_address}}
37 for (auto &ip : network::get_ip_addresses()) {
38 if (ip.is_ip4()) {
39 replace_all_in_place(formatted_url, IP_ADDRESS_PLACEHOLDER, IP_ADDRESS_PLACEHOLDER_LEN, ip.str());
40 break;
41 }
42 }
43
44 // Note: {{esphome_version}} is replaced at code generation time in Python
45
46 return formatted_url;
47}
48#endif
49
50} // namespace improv_base
51} // namespace esphome
const std::string & get_name() const
Get the name of this Application set by pre_setup().
network::IPAddresses get_ip_addresses()
Definition util.cpp:66
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Application App
Global storage of Application pointer - only one Application can exist.