[openthread] Refactor to optimize and match code rules (#14156)

This commit is contained in:
schrob
2026-02-23 04:43:42 +01:00
committed by GitHub
parent 6801604533
commit ee94bc4715
3 changed files with 23 additions and 20 deletions

View File

@@ -35,9 +35,9 @@ void OpenThreadComponent::dump_config() {
#elif CONFIG_OPENTHREAD_MTD
ESP_LOGCONFIG(TAG, " Device Type: MTD");
// TBD: Synchronized Sleepy End Device
if (this->poll_period > 0) {
if (this->poll_period_ > 0) {
ESP_LOGCONFIG(TAG, " Device is configured as Sleepy End Device (SED)");
uint32_t duration = this->poll_period / 1000;
uint32_t duration = this->poll_period_ / 1000;
ESP_LOGCONFIG(TAG, " Poll Period: %" PRIu32 "s", duration);
} else {
ESP_LOGCONFIG(TAG, " Device is configured as Minimal End Device (MED)");

View File

@@ -36,22 +36,22 @@ class OpenThreadComponent : public Component {
const char *get_use_address() const;
void set_use_address(const char *use_address);
#if CONFIG_OPENTHREAD_MTD
void set_poll_period(uint32_t poll_period) { this->poll_period = poll_period; }
void set_poll_period(uint32_t poll_period) { this->poll_period_ = poll_period; }
#endif
protected:
std::optional<otIp6Address> get_omr_address_(InstanceLock &lock);
std::function<void()> factory_reset_external_callback_;
#if CONFIG_OPENTHREAD_MTD
uint32_t poll_period_{0};
#endif
bool teardown_started_{false};
bool teardown_complete_{false};
std::function<void()> factory_reset_external_callback_;
private:
// Stores a pointer to a string literal (static storage duration).
// ONLY set from Python-generated code with string literals - never dynamic strings.
const char *use_address_{""};
#if CONFIG_OPENTHREAD_MTD
uint32_t poll_period{0};
#endif
};
extern OpenThreadComponent *global_openthread_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

View File

@@ -81,9 +81,11 @@ void OpenThreadComponent::ot_main() {
// Initialize the OpenThread stack
// otLoggingSetLevel(OT_LOG_LEVEL_DEBG);
ESP_ERROR_CHECK(esp_openthread_init(&config));
// Fetch OT instance once to avoid repeated call into OT stack
otInstance *instance = esp_openthread_get_instance();
#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
ESP_ERROR_CHECK(esp_openthread_state_indicator_init(esp_openthread_get_instance()));
ESP_ERROR_CHECK(esp_openthread_state_indicator_init(instance));
#endif
#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
@@ -112,28 +114,29 @@ void OpenThreadComponent::ot_main() {
link_mode_config.mDeviceType = true;
link_mode_config.mNetworkData = true;
#elif CONFIG_OPENTHREAD_MTD
if (this->poll_period > 0) {
if (otLinkSetPollPeriod(esp_openthread_get_instance(), this->poll_period) != OT_ERROR_NONE) {
ESP_LOGE(TAG, "Failed to set OpenThread pollperiod.");
if (this->poll_period_ > 0) {
if (otLinkSetPollPeriod(instance, this->poll_period_) != OT_ERROR_NONE) {
ESP_LOGE(TAG, "Failed to set pollperiod");
}
uint32_t link_polling_period = otLinkGetPollPeriod(esp_openthread_get_instance());
ESP_LOGD(TAG, "Link Polling Period: %" PRIu32, link_polling_period);
ESP_LOGD(TAG, "Link Polling Period: %" PRIu32, otLinkGetPollPeriod(instance));
}
link_mode_config.mRxOnWhenIdle = this->poll_period == 0;
link_mode_config.mRxOnWhenIdle = this->poll_period_ == 0;
link_mode_config.mDeviceType = false;
link_mode_config.mNetworkData = false;
#endif
if (otThreadSetLinkMode(esp_openthread_get_instance(), link_mode_config) != OT_ERROR_NONE) {
ESP_LOGE(TAG, "Failed to set OpenThread linkmode.");
if (otThreadSetLinkMode(instance, link_mode_config) != OT_ERROR_NONE) {
ESP_LOGE(TAG, "Failed to set linkmode");
}
link_mode_config = otThreadGetLinkMode(esp_openthread_get_instance());
#ifdef ESPHOME_LOG_HAS_DEBUG // Fetch link mode from OT only when DEBUG
link_mode_config = otThreadGetLinkMode(instance);
ESP_LOGD(TAG,
"Link Mode Device Type: %s\n"
"Link Mode Network Data: %s\n"
"Link Mode RX On When Idle: %s",
link_mode_config.mDeviceType ? "true" : "false", link_mode_config.mNetworkData ? "true" : "false",
link_mode_config.mRxOnWhenIdle ? "true" : "false");
TRUEFALSE(link_mode_config.mDeviceType), TRUEFALSE(link_mode_config.mNetworkData),
TRUEFALSE(link_mode_config.mRxOnWhenIdle));
#endif
// Run the main loop
#if CONFIG_OPENTHREAD_CLI
@@ -144,7 +147,7 @@ void OpenThreadComponent::ot_main() {
#ifndef USE_OPENTHREAD_FORCE_DATASET
// Check if openthread has a valid dataset from a previous execution
otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
otError error = otDatasetGetActiveTlvs(instance, &dataset);
if (error != OT_ERROR_NONE) {
// Make sure the length is 0 so we fallback to the configuration
dataset.mLength = 0;