From d15b7ca504b2d3233f9c7807ba8ad8ed470a90c7 Mon Sep 17 00:00:00 2001 From: mahesh Date: Thu, 22 May 2025 13:05:37 +0530 Subject: [PATCH] components/esp_matter: Add delegate callbacks for clusters - Actions - DiagnosticLogs - OTAProvider - Thermostat --- components/esp_matter/esp_matter_cluster.cpp | 16 +++++++++ components/esp_matter/esp_matter_cluster.h | 18 +++++++--- .../esp_matter_delegate_callbacks.cpp | 35 +++++++++++++++++++ .../esp_matter_delegate_callbacks.h | 4 +++ examples/ota_provider/main/app_main.cpp | 4 +-- 5 files changed, 71 insertions(+), 6 deletions(-) diff --git a/components/esp_matter/esp_matter_cluster.cpp b/components/esp_matter/esp_matter_cluster.cpp index be0958ff6..be6faaf92 100644 --- a/components/esp_matter/esp_matter_cluster.cpp +++ b/components/esp_matter/esp_matter_cluster.cpp @@ -159,6 +159,10 @@ cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags) VerifyOrReturnValue(cluster, NULL, ESP_LOGE(TAG, "Could not create cluster. cluster_id: 0x%08" PRIX32, Actions::Id)); if (flags & CLUSTER_FLAG_SERVER) { + if (config -> delegate != nullptr) { + static const auto delegate_init_cb = ActionsDelegateInitCB; + set_delegate_and_init_callback(cluster, delegate_init_cb, config->delegate); + } set_plugin_server_init_callback(cluster, NULL); add_function_list(cluster, function_list, function_flags); @@ -308,6 +312,10 @@ cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags) VerifyOrReturnValue(cluster, NULL, ESP_LOGE(TAG, "Could not create cluster. cluster_id: 0x%08" PRIX32, OtaSoftwareUpdateProvider::Id)); if (flags & CLUSTER_FLAG_SERVER) { + if (config -> delegate != nullptr) { + static const auto delegate_init_cb = OtaProviderDelegateInitCB; + set_delegate_and_init_callback(cluster, delegate_init_cb, config->delegate); + } static const auto plugin_server_init_cb = CALL_ONCE(MatterOtaSoftwareUpdateProviderPluginServerInitCallback); set_plugin_server_init_callback(cluster, plugin_server_init_cb); add_function_list(cluster, function_list, function_flags); @@ -1385,6 +1393,10 @@ cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags, uint32_ VerifyOrReturnValue(cluster, NULL, ESP_LOGE(TAG, "Could not create cluster. cluster_id: 0x%08" PRIX32, Thermostat::Id)); if (flags & CLUSTER_FLAG_SERVER) { + if (config -> delegate != nullptr) { + static const auto delegate_init_cb = ThermostatDelegateInitCB; + set_delegate_and_init_callback(cluster, delegate_init_cb, config->delegate); + } static const auto plugin_server_init_cb = CALL_ONCE(MatterThermostatPluginServerInitCallback); set_plugin_server_init_callback(cluster, plugin_server_init_cb); set_add_bounds_callback(cluster, thermostat::add_bounds_cb); @@ -3010,6 +3022,10 @@ cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags) cluster_t *cluster = cluster::create(endpoint, DiagnosticLogs::Id, flags); if (flags & CLUSTER_FLAG_SERVER) { + if (config -> delegate != nullptr) { + static const auto delegate_init_cb = DiagnosticLogsDelegateInitCB; + set_delegate_and_init_callback(cluster, delegate_init_cb, config->delegate); + } static const auto plugin_server_init_cb = CALL_ONCE(MatterDiagnosticLogsPluginServerInitCallback); set_plugin_server_init_callback(cluster, plugin_server_init_cb); add_function_list(cluster, function_list, function_flags); diff --git a/components/esp_matter/esp_matter_cluster.h b/components/esp_matter/esp_matter_cluster.h index fb6e681c2..bd548ca5d 100644 --- a/components/esp_matter/esp_matter_cluster.h +++ b/components/esp_matter/esp_matter_cluster.h @@ -79,7 +79,10 @@ cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags); } /* descriptor */ namespace actions { -using config_t = common::config_t; +typedef struct config { + void *delegate; + config() : delegate(nullptr) {} +} config_t; cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags); } /* actions */ @@ -106,7 +109,10 @@ cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags); } /* binding */ namespace ota_provider { -using config_t = common::config_t; +typedef struct config { + void *delegate; + config() : delegate(nullptr) {} +} config_t; cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags); } /* ota_provider */ @@ -150,7 +156,10 @@ cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags); } /* network_commissioning */ namespace diagnostic_logs { -using config_t = common::config_t; +typedef struct config { + void *delegate; + config() : delegate(nullptr) {} +} config_t; cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags); } /* diagnostic_logs */ @@ -392,6 +401,7 @@ typedef struct config { nullable local_temperature; uint8_t control_sequence_of_operation; uint8_t system_mode; + void *delegate; struct { feature::heating::config_t heating; feature::cooling::config_t cooling; @@ -402,7 +412,7 @@ typedef struct config { feature::matter_schedule_configuration::config_t matter_schedule_configuration; } features; uint32_t feature_flags; - config() : local_temperature(), control_sequence_of_operation(4), system_mode(1), feature_flags(0) {} + config() : local_temperature(), control_sequence_of_operation(4), system_mode(1), delegate(nullptr), feature_flags(0) {} } config_t; cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags, uint32_t features); diff --git a/components/esp_matter/esp_matter_delegate_callbacks.cpp b/components/esp_matter/esp_matter_delegate_callbacks.cpp index c15332ef6..459196f6c 100644 --- a/components/esp_matter/esp_matter_delegate_callbacks.cpp +++ b/components/esp_matter/esp_matter_delegate_callbacks.cpp @@ -41,6 +41,11 @@ #include #include #include +#include +#include +#include +#include +#include using namespace chip::app::Clusters; namespace esp_matter { @@ -354,6 +359,36 @@ void CommissionerControlDelegateInitCB(void *delegate, uint16_t endpoint_id) commissioner_control_instance->Init(); } +void ActionsDelegateInitCB(void *delegate, uint16_t endpoint_id) +{ + VerifyOrReturn(delegate != nullptr); + static Actions::ActionsServer *actionsServer = nullptr; + Actions::Delegate *actions_delegate = static_cast(delegate); + actionsServer = new Actions::ActionsServer(endpoint_id, *actions_delegate); + actionsServer->Init(); +} + + +void ThermostatDelegateInitCB(void *delegate, uint16_t endpoint_id) +{ + VerifyOrReturn(delegate != nullptr); + Thermostat::Delegate *thermostat_delegate = static_cast(delegate); + Thermostat::SetDefaultDelegate(endpoint_id, thermostat_delegate); +} + +void OtaProviderDelegateInitCB(void *delegate, uint16_t endpoint_id) +{ + VerifyOrReturn(delegate != nullptr); + chip::app::Clusters::OTAProvider::SetDelegate(endpoint_id, static_cast(delegate)); +} + +void DiagnosticLogsDelegateInitCB(void *delegate, uint16_t endpoint_id) +{ + VerifyOrReturn(delegate != nullptr); + DiagnosticLogs::DiagnosticLogsProviderDelegate *diagnostic_logs_delegate = static_cast(delegate); + DiagnosticLogs::DiagnosticLogsServer::Instance().SetDiagnosticLogsProviderDelegate(endpoint_id, diagnostic_logs_delegate); +} + } // namespace delegate_cb } // namespace cluster diff --git a/components/esp_matter/esp_matter_delegate_callbacks.h b/components/esp_matter/esp_matter_delegate_callbacks.h index fd26d56ef..8378b4cb3 100644 --- a/components/esp_matter/esp_matter_delegate_callbacks.h +++ b/components/esp_matter/esp_matter_delegate_callbacks.h @@ -51,6 +51,10 @@ void ServiceAreaDelegateInitCB(void *delegate, uint16_t endpoint_id); void WaterHeaterManagementDelegateInitCB(void *delegate, uint16_t endpoint_id); void EnergyPreferenceDelegateInitCB(void *delegate, uint16_t endpoint_id); void CommissionerControlDelegateInitCB(void *delegate, uint16_t endpoint_id); +void ActionsDelegateInitCB(void *delegate, uint16_t endpoint_id); +void ThermostatDelegateInitCB(void *delegate, uint16_t endpoint_id); +void OtaProviderDelegateInitCB(void *delegate, uint16_t endpoint_id); +void DiagnosticLogsDelegateInitCB(void *delegate, uint16_t endpoint_id); } // namespace delegate_cb } // namespace cluster diff --git a/examples/ota_provider/main/app_main.cpp b/examples/ota_provider/main/app_main.cpp index d1f79ce67..83525b640 100644 --- a/examples/ota_provider/main/app_main.cpp +++ b/examples/ota_provider/main/app_main.cpp @@ -54,6 +54,8 @@ extern "C" void app_main() node_t *node = node::create(&node_config, NULL, NULL); endpoint_t *root_node_endpoint = endpoint::get(node, 0); cluster::ota_provider::config_t config; + EspOtaProvider::GetInstance().Init(true); + config.delegate = &EspOtaProvider::GetInstance(); cluster_t *ota_provider_cluster = cluster::ota_provider::create(root_node_endpoint, &config, CLUSTER_FLAG_SERVER); if (!node || !root_node_endpoint || !ota_provider_cluster) { ESP_LOGE(TAG, "Failed to create data model"); @@ -64,8 +66,6 @@ extern "C" void app_main() if (err != ESP_OK) { ESP_LOGE(TAG, "Matter start failed: %d", err); } - EspOtaProvider::GetInstance().Init(true); - OTAProvider::SetDelegate(0, reinterpret_cast(&EspOtaProvider::GetInstance())); #if CONFIG_ENABLE_CHIP_SHELL esp_matter::console::diagnostics_register_commands(); esp_matter::console::wifi_register_commands();