feature: add attribute report API

This commit is contained in:
InfiniteYuan
2023-04-04 16:15:02 +08:00
parent 3b376bec96
commit 6744184244
2 changed files with 54 additions and 0 deletions
@@ -21,6 +21,7 @@
#include <string.h>
#include <app/util/attribute-storage.h>
#include <app/reporting/reporting.h>
#include <protocols/interaction_model/Constants.h>
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 */
@@ -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.