1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 08:41:59 +00:00

[openthread_info] Eliminate heap allocations in text sensors (#13036)

This commit is contained in:
J. Nick Koston
2026-01-06 17:53:00 -10:00
committed by GitHub
parent 4391457a96
commit 3a84e4a0b4

View File

@@ -33,13 +33,12 @@ class IPAddressOpenThreadInfo : public PollingComponent, public text_sensor::Tex
return;
}
char address_as_string[40];
otIp6AddressToString(&*address, address_as_string, 40);
std::string ip = address_as_string;
char buf[OT_IP6_ADDRESS_STRING_SIZE];
otIp6AddressToString(&*address, buf, sizeof(buf));
if (this->last_ip_ != ip) {
this->last_ip_ = ip;
this->publish_state(this->last_ip_);
if (this->last_ip_ != buf) {
this->last_ip_ = buf;
this->publish_state(buf);
}
}
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
@@ -89,7 +88,9 @@ class ExtAddrOpenThreadInfo : public OpenThreadInstancePollingComponent, public
const auto *extaddr = otLinkGetExtendedAddress(instance);
if (!std::equal(this->last_extaddr_.begin(), this->last_extaddr_.end(), extaddr->m8)) {
std::copy(extaddr->m8, extaddr->m8 + 8, this->last_extaddr_.begin());
this->publish_state(format_hex(extaddr->m8, 8));
char buf[format_hex_size(8)];
format_hex_to(buf, extaddr->m8, 8);
this->publish_state(buf);
}
}
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
@@ -107,7 +108,9 @@ class Eui64OpenThreadInfo : public OpenThreadInstancePollingComponent, public te
if (!std::equal(this->last_eui64_.begin(), this->last_eui64_.end(), addr.m8)) {
std::copy(addr.m8, addr.m8 + 8, this->last_eui64_.begin());
this->publish_state(format_hex(this->last_eui64_.begin(), 8));
char buf[format_hex_size(8)];
format_hex_to(buf, this->last_eui64_.data(), 8);
this->publish_state(buf);
}
}
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
@@ -123,7 +126,9 @@ class ChannelOpenThreadInfo : public OpenThreadInstancePollingComponent, public
uint8_t channel = otLinkGetChannel(instance);
if (this->last_channel_ != channel) {
this->last_channel_ = channel;
this->publish_state(std::to_string(this->last_channel_));
char buf[4]; // max "255" + null
snprintf(buf, sizeof(buf), "%u", channel);
this->publish_state(buf);
}
}
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
@@ -168,7 +173,9 @@ class NetworkKeyOpenThreadInfo : public DatasetOpenThreadInfo, public text_senso
void update_dataset(otOperationalDataset *dataset) override {
if (!std::equal(this->last_key_.begin(), this->last_key_.end(), dataset->mNetworkKey.m8)) {
std::copy(dataset->mNetworkKey.m8, dataset->mNetworkKey.m8 + 16, this->last_key_.begin());
this->publish_state(format_hex(dataset->mNetworkKey.m8, 16));
char buf[format_hex_size(16)];
format_hex_to(buf, dataset->mNetworkKey.m8, 16);
this->publish_state(buf);
}
}
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
@@ -201,7 +208,9 @@ class ExtPanIdOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor:
void update_dataset(otOperationalDataset *dataset) override {
if (!std::equal(this->last_extpanid_.begin(), this->last_extpanid_.end(), dataset->mExtendedPanId.m8)) {
std::copy(dataset->mExtendedPanId.m8, dataset->mExtendedPanId.m8 + 8, this->last_extpanid_.begin());
this->publish_state(format_hex(this->last_extpanid_.begin(), 8));
char buf[format_hex_size(8)];
format_hex_to(buf, this->last_extpanid_.data(), 8);
this->publish_state(buf);
}
}