diff --git a/components/esp_matter/esp_matter_core.cpp b/components/esp_matter/esp_matter_core.cpp index c31ea1277..4be8c64f8 100644 --- a/components/esp_matter/esp_matter_core.cpp +++ b/components/esp_matter/esp_matter_core.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -782,6 +783,20 @@ static void esp_matter_chip_init_task(intptr_t context) if (GetDiagnosticDataProvider().GetBootReason(bootReason) == CHIP_NO_ERROR) { chip::app::Clusters::GeneralDiagnosticsServer::Instance().OnDeviceReboot(bootReason); } + +#if CHIP_CONFIG_ENABLE_ICD_SERVER + if (!icd::get_icd_server_enabled()) { + // ICD server has been initialized in chip::Server::GetInstance().Init(). disable it here if + // icd_server_enabled is set to false. + chip::app::InteractionModelEngine::GetInstance()->SetICDManager(nullptr); + chip::app::DnssdServer::Instance().SetICDManager(nullptr); + chip::TestEventTriggerDelegate *test_event_trigger = chip::Server::GetInstance().GetTestEventTriggerDelegate(); + if (test_event_trigger) { + test_event_trigger->RemoveHandler(&chip::Server::GetInstance().GetICDManager()); + } + chip::Server::GetInstance().GetICDManager().Shutdown(); + } +#endif // CHIP_CONFIG_ENABLE_ICD_SERVER PlatformMgr().ScheduleWork(deinit_ble_if_commissioned, reinterpret_cast(nullptr)); xTaskNotifyGive(task_to_notify); } @@ -827,8 +842,13 @@ static esp_err_t init_thread_stack_and_start_thread_task() VerifyOrReturnError(ThreadStackMgr().InitThreadStack() == CHIP_NO_ERROR, ESP_FAIL, ESP_LOGE(TAG, "Failed to initialize Thread stack")); #if CHIP_CONFIG_ENABLE_ICD_SERVER - VerifyOrReturnError(ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice) == CHIP_NO_ERROR, - ESP_FAIL, ESP_LOGE(TAG, "Failed to set the Thread device type")); + if (icd::get_icd_server_enabled()) { + VerifyOrReturnError(ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice) == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to set the Thread device type")); + } else { + VerifyOrReturnError(ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice) == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to set the Thread device type")); + } #elif CHIP_DEVICE_CONFIG_THREAD_FTD VerifyOrReturnError(ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router) == CHIP_NO_ERROR, diff --git a/components/esp_matter/esp_matter_endpoint.cpp b/components/esp_matter/esp_matter_endpoint.cpp index 6643cf1f0..5dacfa7e2 100644 --- a/components/esp_matter/esp_matter_endpoint.cpp +++ b/components/esp_matter/esp_matter_endpoint.cpp @@ -15,6 +15,7 @@ #include #include #include +#include static const char *TAG = "esp_matter_endpoint"; @@ -76,17 +77,19 @@ esp_err_t add(endpoint_t *endpoint, config_t *config) group_key_management::create(endpoint, CLUSTER_FLAG_SERVER); #if CHIP_CONFIG_ENABLE_ICD_SERVER - icd_management::create(endpoint, &(config->icd_management), CLUSTER_FLAG_SERVER, + if (icd::get_icd_server_enabled()) { + icd_management::create(endpoint, &(config->icd_management), CLUSTER_FLAG_SERVER, #if CHIP_CONFIG_ENABLE_ICD_LIT - icd_management::feature::long_idle_time_support::get_id() | + icd_management::feature::long_idle_time_support::get_id() | #if CHIP_CONFIG_ENABLE_ICD_CIP - icd_management::feature::check_in_protocol_support::get_id() | + icd_management::feature::check_in_protocol_support::get_id() | #endif // CHIP_CONFIG_ENABLE_ICD_CIP #if CHIP_CONFIG_ENABLE_ICD_UAT - icd_management::feature::user_active_mode_trigger::get_id() | + icd_management::feature::user_active_mode_trigger::get_id() | #endif // CHIP_CONFIG_ENABLE_ICD_UAT #endif // CHIP_CONFIG_ENABLE_ICD_LIT 0); + } #endif // CHIP_CONFIG_ENABLE_ICD_SERVER return ESP_OK; } diff --git a/components/esp_matter/esp_matter_icd_configuration.cpp b/components/esp_matter/esp_matter_icd_configuration.cpp index 60a869412..7001a3b05 100644 --- a/components/esp_matter/esp_matter_icd_configuration.cpp +++ b/components/esp_matter/esp_matter_icd_configuration.cpp @@ -107,17 +107,31 @@ static esp_err_t set_active_threshold(uint32_t active_threshold_ms) return chip::Test::ICDConfigurationDataTestAccess::SetActiveThreshold(Milliseconds32(active_threshold_ms)); } +static bool s_enable_icd_server = true; + +bool get_icd_server_enabled() +{ + return s_enable_icd_server; +} + esp_err_t set_configuration_data(config_t *config) { + ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "config cannot be NULL"); + if (!config->enable_icd_server) { + ESP_RETURN_ON_FALSE(!node::get(), ESP_ERR_INVALID_STATE, TAG, + "Could not disable ICD server after data model is created"); + } + s_enable_icd_server = config->enable_icd_server; ESP_RETURN_ON_FALSE(!is_started(), ESP_ERR_INVALID_STATE, TAG, "Could not change ICD configuration data after Matter is started"); - ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "config cannot be NULL"); ESP_RETURN_ON_ERROR(set_polling_intervals(config->fast_interval_ms, config->slow_interval_ms), TAG, "Failed to set polling intervals"); ESP_RETURN_ON_ERROR(set_mode_durations(config->active_mode_duration_ms, config->idle_mode_duration_s), TAG, "Failed to set mode durations"); - ESP_RETURN_ON_ERROR(set_active_threshold(config->active_threshold_ms.value()), TAG, - "Failed to set active threshold"); + if (config->active_threshold_ms.has_value()) { + ESP_RETURN_ON_ERROR(set_active_threshold(config->active_threshold_ms.value()), TAG, + "Failed to set active threshold"); + } return ESP_OK; } diff --git a/components/esp_matter/esp_matter_icd_configuration.h b/components/esp_matter/esp_matter_icd_configuration.h index 59f98efd1..60a131484 100644 --- a/components/esp_matter/esp_matter_icd_configuration.h +++ b/components/esp_matter/esp_matter_icd_configuration.h @@ -19,6 +19,7 @@ namespace esp_matter { namespace icd { typedef struct config { + bool enable_icd_server; std::optional fast_interval_ms; std::optional slow_interval_ms; std::optional active_mode_duration_ms; @@ -26,6 +27,14 @@ typedef struct config { std::optional active_threshold_ms; } config_t; +/** Get whether ICD server is enabled for Matter end-device + */ +bool get_icd_server_enabled(); + +/** Set ICD configuration data + * + * This function allows the user to enable or disable the ICD server and configure its settings at runtime, before creating the Matter data model. + */ esp_err_t set_configuration_data(config_t *config); } // namespace icd