mirror of
https://github.com/esphome/esphome.git
synced 2026-01-10 12:10:48 -07:00
[ethernet] Eliminate heap allocations in dump_config logging (#12665)
This commit is contained in:
@@ -644,6 +644,12 @@ void EthernetComponent::dump_connect_params_() {
|
|||||||
dns_ip2 = dns_getserver(1);
|
dns_ip2 = dns_getserver(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use stack buffers for IP address formatting to avoid heap allocations
|
||||||
|
char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
|
||||||
|
char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
|
||||||
|
char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
|
||||||
|
char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
|
||||||
|
char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG,
|
||||||
" IP Address: %s\n"
|
" IP Address: %s\n"
|
||||||
" Hostname: '%s'\n"
|
" Hostname: '%s'\n"
|
||||||
@@ -651,9 +657,9 @@ void EthernetComponent::dump_connect_params_() {
|
|||||||
" Gateway: %s\n"
|
" Gateway: %s\n"
|
||||||
" DNS1: %s\n"
|
" DNS1: %s\n"
|
||||||
" DNS2: %s",
|
" DNS2: %s",
|
||||||
network::IPAddress(&ip.ip).str().c_str(), App.get_name().c_str(),
|
network::IPAddress(&ip.ip).str_to(ip_buf), App.get_name().c_str(),
|
||||||
network::IPAddress(&ip.netmask).str().c_str(), network::IPAddress(&ip.gw).str().c_str(),
|
network::IPAddress(&ip.netmask).str_to(subnet_buf), network::IPAddress(&ip.gw).str_to(gateway_buf),
|
||||||
network::IPAddress(dns_ip1).str().c_str(), network::IPAddress(dns_ip2).str().c_str());
|
network::IPAddress(dns_ip1).str_to(dns1_buf), network::IPAddress(dns_ip2).str_to(dns2_buf));
|
||||||
|
|
||||||
#if USE_NETWORK_IPV6
|
#if USE_NETWORK_IPV6
|
||||||
struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
|
struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
|
||||||
@@ -665,12 +671,13 @@ void EthernetComponent::dump_connect_params_() {
|
|||||||
}
|
}
|
||||||
#endif /* USE_NETWORK_IPV6 */
|
#endif /* USE_NETWORK_IPV6 */
|
||||||
|
|
||||||
|
char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG,
|
||||||
" MAC Address: %s\n"
|
" MAC Address: %s\n"
|
||||||
" Is Full Duplex: %s\n"
|
" Is Full Duplex: %s\n"
|
||||||
" Link Speed: %u",
|
" Link Speed: %u",
|
||||||
this->get_eth_mac_address_pretty().c_str(), YESNO(this->get_duplex_mode() == ETH_DUPLEX_FULL),
|
this->get_eth_mac_address_pretty_into_buffer(mac_buf),
|
||||||
this->get_link_speed() == ETH_SPEED_100M ? 100 : 10);
|
YESNO(this->get_duplex_mode() == ETH_DUPLEX_FULL), this->get_link_speed() == ETH_SPEED_100M ? 100 : 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_ETHERNET_SPI
|
#ifdef USE_ETHERNET_SPI
|
||||||
@@ -711,11 +718,16 @@ void EthernetComponent::get_eth_mac_address_raw(uint8_t *mac) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string EthernetComponent::get_eth_mac_address_pretty() {
|
std::string EthernetComponent::get_eth_mac_address_pretty() {
|
||||||
|
char buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
|
||||||
|
return std::string(this->get_eth_mac_address_pretty_into_buffer(buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *EthernetComponent::get_eth_mac_address_pretty_into_buffer(
|
||||||
|
std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf) {
|
||||||
uint8_t mac[6];
|
uint8_t mac[6];
|
||||||
get_eth_mac_address_raw(mac);
|
get_eth_mac_address_raw(mac);
|
||||||
char buf[18];
|
format_mac_addr_upper(mac, buf.data());
|
||||||
format_mac_addr_upper(mac, buf);
|
return buf.data();
|
||||||
return std::string(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eth_duplex_t EthernetComponent::get_duplex_mode() {
|
eth_duplex_t EthernetComponent::get_duplex_mode() {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/defines.h"
|
#include "esphome/core/defines.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/components/network/ip_address.h"
|
#include "esphome/components/network/ip_address.h"
|
||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
@@ -93,6 +94,7 @@ class EthernetComponent : public Component {
|
|||||||
void set_use_address(const char *use_address);
|
void set_use_address(const char *use_address);
|
||||||
void get_eth_mac_address_raw(uint8_t *mac);
|
void get_eth_mac_address_raw(uint8_t *mac);
|
||||||
std::string get_eth_mac_address_pretty();
|
std::string get_eth_mac_address_pretty();
|
||||||
|
const char *get_eth_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf);
|
||||||
eth_duplex_t get_duplex_mode();
|
eth_duplex_t get_duplex_mode();
|
||||||
eth_speed_t get_link_speed();
|
eth_speed_t get_link_speed();
|
||||||
bool powerdown();
|
bool powerdown();
|
||||||
|
|||||||
Reference in New Issue
Block a user