From 31f7240204d2194e1fe34f09a6278aea2891f863 Mon Sep 17 00:00:00 2001 From: Rohit Jadhav Date: Fri, 8 Nov 2024 12:02:19 +0530 Subject: [PATCH] components/esp-matter: Add Energy Preference Cluster --- .../esp_matter/esp_matter_attribute.cpp | 35 +++++++++++++ components/esp_matter/esp_matter_attribute.h | 10 ++++ components/esp_matter/esp_matter_cluster.cpp | 43 ++++++++++++++++ components/esp_matter/esp_matter_cluster.h | 11 ++++ .../esp_matter_delegate_callbacks.cpp | 11 ++++ .../esp_matter_delegate_callbacks.h | 1 + components/esp_matter/esp_matter_feature.cpp | 51 +++++++++++++++++++ components/esp_matter/esp_matter_feature.h | 28 +++++++++- .../private/esp_matter_cluster_revisions.h | 4 ++ docs/en/app_guide.rst | 11 ++++ 10 files changed, 203 insertions(+), 2 deletions(-) diff --git a/components/esp_matter/esp_matter_attribute.cpp b/components/esp_matter/esp_matter_attribute.cpp index 9cbea08a3..c141447f4 100644 --- a/components/esp_matter/esp_matter_attribute.cpp +++ b/components/esp_matter/esp_matter_attribute.cpp @@ -4811,5 +4811,40 @@ attribute_t *create_boost_state(cluster_t *cluster, uint8_t value) } /* attribute */ } /* water_heater_management */ +namespace energy_preference { +namespace attribute { +attribute_t *create_energy_balances(cluster_t *cluster, uint8_t *value, uint16_t length, uint16_t count) +{ + return esp_matter::attribute::create(cluster, EnergyPreference::Attributes::EnergyBalances::Id, ATTRIBUTE_FLAG_NONE, + esp_matter_array(value, length, count)); +} + +attribute_t *create_current_energy_balance(cluster_t *cluster, uint8_t value) +{ + return esp_matter::attribute::create(cluster, EnergyPreference::Attributes::CurrentEnergyBalance::Id, + ATTRIBUTE_FLAG_NONVOLATILE | ATTRIBUTE_FLAG_WRITABLE, esp_matter_uint8(value)); +} + +attribute_t *create_energy_priorities(cluster_t *cluster, uint8_t *value, uint16_t length, uint16_t count) +{ + return esp_matter::attribute::create(cluster, EnergyPreference::Attributes::EnergyPriorities::Id, ATTRIBUTE_FLAG_NONE, + esp_matter_array(value, length, count)); +} + +attribute_t *create_low_power_mode_sensitivities(cluster_t *cluster, uint8_t *value, uint16_t length, uint16_t count) +{ + return esp_matter::attribute::create(cluster, EnergyPreference::Attributes::LowPowerModeSensitivities::Id, ATTRIBUTE_FLAG_NONE, + esp_matter_array(value, length, count)); +} + +attribute_t *create_current_low_power_mode_sensitivity(cluster_t *cluster, uint8_t value) +{ + return esp_matter::attribute::create(cluster, EnergyPreference::Attributes::CurrentLowPowerModeSensitivity::Id, + ATTRIBUTE_FLAG_NONVOLATILE | ATTRIBUTE_FLAG_WRITABLE, esp_matter_uint8(value)); +} + +} /* attribute */ +} /* energy_preference */ + } /* cluster */ } /* esp_matter */ diff --git a/components/esp_matter/esp_matter_attribute.h b/components/esp_matter/esp_matter_attribute.h index cd3dea87b..a58bd45cf 100644 --- a/components/esp_matter/esp_matter_attribute.h +++ b/components/esp_matter/esp_matter_attribute.h @@ -1171,5 +1171,15 @@ attribute_t *create_boost_state(cluster_t *cluster, uint8_t value); } /* attribute */ } /* water_heater_management */ +namespace energy_preference { +namespace attribute { +attribute_t *create_energy_balances(cluster_t *cluster, uint8_t *value, uint16_t length, uint16_t count); +attribute_t *create_current_energy_balance(cluster_t *cluster, uint8_t value); +attribute_t *create_energy_priorities(cluster_t *cluster, uint8_t *value, uint16_t length, uint16_t count); +attribute_t *create_low_power_mode_sensitivities(cluster_t *cluster, uint8_t *value, uint16_t length, uint16_t count); +attribute_t *create_current_low_power_mode_sensitivity(cluster_t *cluster, uint8_t value); +} /* attribute */ +} /* energy_preference */ + } /* cluster */ } /* esp_matter */ diff --git a/components/esp_matter/esp_matter_cluster.cpp b/components/esp_matter/esp_matter_cluster.cpp index a0b0af92c..c9aa7e768 100644 --- a/components/esp_matter/esp_matter_cluster.cpp +++ b/components/esp_matter/esp_matter_cluster.cpp @@ -3651,6 +3651,49 @@ cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags) } } /* water_heater_mode */ +namespace energy_preference { +const function_generic_t *function_list = NULL; +const int function_flags = CLUSTER_FLAG_NONE; + +cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags, uint32_t features) +{ + cluster_t *cluster = cluster::create(endpoint, EnergyPreference::Id, flags); + if (!cluster) { + ESP_LOGE(TAG, "Could not create cluster"); + return NULL; + } + if (flags & CLUSTER_FLAG_SERVER) { + if (config -> delegate != nullptr) { + static const auto delegate_init_cb = EnergyPreferenceDelegateInitCB; + set_delegate_and_init_callback(cluster, delegate_init_cb, config->delegate); + } + static const auto plugin_server_init_cb = CALL_ONCE(MatterEnergyPreferencePluginServerInitCallback); + set_plugin_server_init_callback(cluster, plugin_server_init_cb); + add_function_list(cluster, function_list, function_flags); + + /* Attributes managed internally */ + global::attribute::create_feature_map(cluster, 0); + + /** Attributes not managed internally **/ + global::attribute::create_cluster_revision(cluster, cluster_revision); + } + + if (flags & CLUSTER_FLAG_CLIENT) { + create_default_binding_cluster(endpoint); + } + + /* Features */ + if (features & feature::energy_balance::get_id()) { + feature::energy_balance::add(cluster, &(config->energy_balance)); + } + if (features & feature::low_power_mode_sensitivity::get_id()) { + feature::low_power_mode_sensitivity::add(cluster, &(config->low_power_mode_sensitivity)); + } + + return cluster; +} +} /* energy_preference */ + // namespace binary_input_basic { // // ToDo // } /* binary_input_basic */ diff --git a/components/esp_matter/esp_matter_cluster.h b/components/esp_matter/esp_matter_cluster.h index a9a1b5e5a..918ebdc7d 100644 --- a/components/esp_matter/esp_matter_cluster.h +++ b/components/esp_matter/esp_matter_cluster.h @@ -930,5 +930,16 @@ typedef struct config { cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags); } /* water_heater_mode */ +namespace energy_preference { +typedef struct config { + feature::energy_balance::config_t energy_balance; + feature::low_power_mode_sensitivity::config_t low_power_mode_sensitivity; + void *delegate; + config() : delegate(nullptr) {} +} config_t; + +cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags, uint32_t features); +} /* energy_preference */ + } /* cluster */ } /* esp_matter */ diff --git a/components/esp_matter/esp_matter_delegate_callbacks.cpp b/components/esp_matter/esp_matter_delegate_callbacks.cpp index 396c11759..5613ced17 100644 --- a/components/esp_matter/esp_matter_delegate_callbacks.cpp +++ b/components/esp_matter/esp_matter_delegate_callbacks.cpp @@ -39,6 +39,7 @@ #include #include #include +#include using namespace chip::app::Clusters; namespace esp_matter { @@ -399,6 +400,16 @@ void WaterHeaterManagementDelegateInitCB(void *delegate, uint16_t endpoint_id) wHtrInstance->Init(); } +void EnergyPreferenceDelegateInitCB(void *delegate, uint16_t endpoint_id) +{ + if(delegate == nullptr) + { + return; + } + EnergyPreference::Delegate *energy_preference_delegate = static_cast(delegate); + EnergyPreference::SetDelegate(energy_preference_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 ca3487436..5ec83761b 100644 --- a/components/esp_matter/esp_matter_delegate_callbacks.h +++ b/components/esp_matter/esp_matter_delegate_callbacks.h @@ -49,6 +49,7 @@ void ModeSelectDelegateInitCB(void *delegate, uint16_t endpoint_id); void ThreadBorderRouterManagementDelegateInitCB(void *delegate, uint16_t endpoint_id); void ServiceAreaDelegateInitCB(void *delegate, uint16_t endpoint_id); void WaterHeaterManagementDelegateInitCB(void *delegate, uint16_t endpoint_id); +void EnergyPreferenceDelegateInitCB(void *delegate, uint16_t endpoint_id); } // namespace delegate_cb } // namespace cluster diff --git a/components/esp_matter/esp_matter_feature.cpp b/components/esp_matter/esp_matter_feature.cpp index dfc39e878..8d51806c9 100644 --- a/components/esp_matter/esp_matter_feature.cpp +++ b/components/esp_matter/esp_matter_feature.cpp @@ -4902,5 +4902,56 @@ esp_err_t add(cluster_t *cluster, config_t *config) } /* feature */ } /* water_heater_management */ +namespace energy_preference { +namespace feature { + +namespace energy_balance { + +uint32_t get_id() +{ + return (uint32_t)EnergyPreference::Feature::kEnergyBalance; +} + +esp_err_t add(cluster_t *cluster, config_t *config) +{ + if (!cluster) { + ESP_LOGE(TAG, "Cluster cannot be NULL"); + return ESP_ERR_INVALID_ARG; + } + update_feature_map(cluster, get_id()); + + attribute::create_energy_balances(cluster, NULL, 0, 0); + attribute::create_current_energy_balance(cluster, config->current_energy_balance); + attribute::create_energy_priorities(cluster, NULL, 0, 0); + + return ESP_OK; +} +} /* energy_balance */ + +namespace low_power_mode_sensitivity { + +uint32_t get_id() +{ + return (uint32_t)EnergyPreference::Feature::kLowPowerModeSensitivity; +} + +esp_err_t add(cluster_t *cluster, config_t *config) +{ + if (!cluster) { + ESP_LOGE(TAG, "Cluster cannot be NULL"); + return ESP_ERR_INVALID_ARG; + } + update_feature_map(cluster, get_id()); + + attribute::create_low_power_mode_sensitivities(cluster, NULL, 0, 0); + attribute::create_current_low_power_mode_sensitivity(cluster, config->current_low_power_mode_sensitivity); + + return ESP_OK; +} +} /* low_power_mode_sensitivity */ + +} /* feature */ +} /* energy_preference */ + } /* cluster */ } /* esp_matter */ diff --git a/components/esp_matter/esp_matter_feature.h b/components/esp_matter/esp_matter_feature.h index 8fee7006e..4772b7ad6 100644 --- a/components/esp_matter/esp_matter_feature.h +++ b/components/esp_matter/esp_matter_feature.h @@ -2147,11 +2147,35 @@ typedef struct config { uint32_t get_id(); esp_err_t add(cluster_t *cluster, config_t *config); - } /* tank_percent */ - } /* feature */ } /* water_heater_management */ +namespace energy_preference { +namespace feature { + +namespace energy_balance { +typedef struct config { + uint8_t current_energy_balance; + config() : current_energy_balance(0) {} +} config_t; + +uint32_t get_id(); +esp_err_t add(cluster_t *cluster, config_t *config); +} /* energy_balance */ + +namespace low_power_mode_sensitivity { +typedef struct config { + uint8_t current_low_power_mode_sensitivity; + config() : current_low_power_mode_sensitivity(0) {} +} config_t; + +uint32_t get_id(); +esp_err_t add(cluster_t *cluster, config_t *config); +} /* low_power_mode_sensitivity */ + +} /* feature */ +} /* energy_preference */ + } /* cluster */ } /* esp_matter */ diff --git a/components/esp_matter/private/esp_matter_cluster_revisions.h b/components/esp_matter/private/esp_matter_cluster_revisions.h index 77c28807a..b4e3646f3 100644 --- a/components/esp_matter/private/esp_matter_cluster_revisions.h +++ b/components/esp_matter/private/esp_matter_cluster_revisions.h @@ -391,6 +391,10 @@ namespace water_heater_management { constexpr uint16_t cluster_revision = 2; } // namespace water_heater_management +namespace energy_preference { +constexpr uint16_t cluster_revision = 1; +} // namespace energy_preference + } // namespace cluster } // namespace esp_matter diff --git a/docs/en/app_guide.rst b/docs/en/app_guide.rst index 7f918cb7b..d3e540444 100644 --- a/docs/en/app_guide.rst +++ b/docs/en/app_guide.rst @@ -46,6 +46,7 @@ List of clusters with delegate: - Target Navigator Cluster. - Mode Select Cluster. - Water Heater Management Cluster. + - Energy Preference Cluster. 9.1.1 Mode Base Cluster ----------------------- @@ -226,6 +227,14 @@ ModeWaterHeater, ModeRefrigerator, ModeLaundryWasher and ModeMicrowaveOven. `Water Heater Management`_, `Water Heater Management Delegate`_ +9.1.21 Energy Preference Cluster +-------------------------------- + +.. csv-table:: + :header: "Delegate Class", "Reference Implementation" + + `Energy Preference`_, `Energy Preference Delegate`_ + .. note:: Make sure that after implementing delegate class, you set the delegate class pointer at the time of creating cluster. @@ -282,3 +291,5 @@ ModeWaterHeater, ModeRefrigerator, ModeLaundryWasher and ModeMicrowaveOven. .. _`Mode Select Delegate`: https://github.com/espressif/connectedhomeip/blob/ea679d2dc674f576f4d391d1d71af1489010e580/examples/all-clusters-app/all-clusters-common/include/static-supported-modes-manager.h .. _`Water Heater Management`: https://github.com/espressif/connectedhomeip/blob/ea679d2dc674f576f4d391d1d71af1489010e580/src/app/clusters/water-heater-management-server/water-heater-management-server.h .. _`Water Heater Management Delegate`: https://github.com/espressif/connectedhomeip/blob/ea679d2dc674f576f4d391d1d71af1489010e580/examples/energy-management-app/energy-management-common/water-heater/include/WhmDelegate.h +.. _`Energy Preference`: https://github.com/espressif/connectedhomeip/blob/ea679d2dc674f576f4d391d1d71af1489010e580/src/app/clusters/energy-preference-server/energy-preference-server.h +.. _`Energy Preference Delegate`: https://github.com/espressif/connectedhomeip/blob/ea679d2dc674f576f4d391d1d71af1489010e580/examples/all-clusters-app/all-clusters-common/src/energy-preference-delegate.cpp