diff --git a/components/esp_matter/esp_matter_attribute_utils.cpp b/components/esp_matter/esp_matter_attribute_utils.cpp index cafe2d5fb..8b60130b3 100644 --- a/components/esp_matter/esp_matter_attribute_utils.cpp +++ b/components/esp_matter/esp_matter_attribute_utils.cpp @@ -21,6 +21,7 @@ #include #include +#include #include using chip::AttributeId; @@ -1705,6 +1706,43 @@ esp_err_t update(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_i return ESP_OK; } +esp_err_t report(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) +{ + /* Take lock if not already taken */ + lock::status_t lock_status = lock::chip_stack_lock(portMAX_DELAY); + if (lock_status == lock::FAILED) { + ESP_LOGE(TAG, "Could not get task context"); + return ESP_FAIL; + } + + /* Get attribute */ + node_t *node = node::get(); + endpoint_t *endpoint = endpoint::get(node, endpoint_id); + cluster_t *cluster = cluster::get(endpoint, cluster_id); + attribute_t *attribute = attribute::get(cluster, attribute_id); + if (!attribute) { + ESP_LOGE(TAG, "Could not find the attribute"); + return ESP_FAIL; + } + + /* Update attribute */ + esp_matter_attr_val_t raw_val = esp_matter_invalid(NULL); + attribute::get_val(attribute, &raw_val); + if (val->type != raw_val.type) { + ESP_LOGE(TAG, "attribute type mismatch"); + return ESP_FAIL; + } + attribute::set_val(attribute, val); + + /* Report attribute */ + MatterReportingAttributeChangeCallback(endpoint_id, cluster_id, attribute_id); + + if (lock_status == lock::SUCCESS) { + lock::chip_stack_unlock(); + } + return ESP_OK; +} + } /* attribute */ } /* esp_matter */ diff --git a/components/esp_matter/esp_matter_attribute_utils.h b/components/esp_matter/esp_matter_attribute_utils.h index ce9af58d0..7cd7e7c19 100644 --- a/components/esp_matter/esp_matter_attribute_utils.h +++ b/components/esp_matter/esp_matter_attribute_utils.h @@ -360,6 +360,22 @@ esp_err_t set_callback(callback_t callback); */ esp_err_t update(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); +/** Attribute report + * + * This API reports the attribute value. + * After this API is called, the application doesn't gets the attribute update callback with `PRE_UPDATE` or `POST_UPDATE`, the + * attribute is updated in the database. + * + * @param[in] endpoint_id Endpoint ID of the attribute. + * @param[in] cluster_id Cluster ID of the attribute. + * @param[in] attribute_id Attribute ID of the attribute. + * @param[in] val Pointer to new value to report, of type `esp_matter_attr_val_t`. Appropriate elements should be used as per the value type. + * + * @return ESP_OK on success. + * @return error in case of failure. + */ +esp_err_t report(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + /** Attribute value print * * This API prints the attribute value according to the type.