Skip to content

Commit 40b9d14

Browse files
committed
Merge branch 'feat/add-destroy-methods' into 'main'
Add Destroy API Support for Commands, Attributes, and Events See merge request app-frameworks/esp-matter!1336
2 parents 8b526dc + 0d77177 commit 40b9d14

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

components/esp_matter/data_model/esp_matter_data_model.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ attribute_t *create(cluster_t *cluster, uint32_t attribute_id, uint16_t flags, e
555555
return (attribute_t *)attribute;
556556
}
557557

558-
static esp_err_t destroy(attribute_t *attribute)
558+
static esp_err_t free_attribute(attribute_t *attribute)
559559
{
560560
VerifyOrReturnError(attribute, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Attribute cannot be NULL"));
561561
_attribute_t *current_attribute = (_attribute_t *)attribute;
@@ -587,6 +587,22 @@ static esp_err_t destroy(attribute_t *attribute)
587587
return ESP_OK;
588588
}
589589

590+
esp_err_t destroy(cluster_t *cluster, attribute_t *attribute)
591+
{
592+
VerifyOrReturnError(cluster && attribute, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Cluster or attribute cannot be NULL"));
593+
_cluster_t *current_cluster = (_cluster_t *)cluster;
594+
_attribute_base_t *target_attribute = (_attribute_base_t *)attribute;
595+
596+
_attribute_base_t **current_attribute = &current_cluster->attribute_list;
597+
while (*current_attribute && *current_attribute != target_attribute) {
598+
current_attribute = &(*current_attribute)->next;
599+
}
600+
601+
VerifyOrReturnError(*current_attribute, ESP_ERR_NOT_FOUND, ESP_LOGE(TAG, "Attribute not found in the cluster"));
602+
*current_attribute = target_attribute->next;
603+
return free_attribute(attribute);
604+
}
605+
590606
attribute_t *get(cluster_t *cluster, uint32_t attribute_id)
591607
{
592608
VerifyOrReturnValue(cluster, NULL, ESP_LOGE(TAG, "Cluster cannot be NULL."));
@@ -1179,6 +1195,15 @@ command_t *create(cluster_t *cluster, uint32_t command_id, uint8_t flags, callba
11791195
return (command_t *)command;
11801196
}
11811197

1198+
esp_err_t destroy(cluster_t *cluster, command_t *command)
1199+
{
1200+
VerifyOrReturnError(cluster && command, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Cluster or command cannot be NULL"));
1201+
_cluster_t *current_cluster = (_cluster_t *)cluster;
1202+
_command_t *current_command = (_command_t *)command;
1203+
SinglyLinkedList<_command_t>::remove(&current_cluster->command_list, current_command);
1204+
return ESP_OK;
1205+
}
1206+
11821207
command_t *get(uint16_t endpoint_id, uint32_t cluster_id, uint32_t command_id)
11831208
{
11841209
_cluster_t *current_cluster = (_cluster_t *)cluster::get(endpoint_id, cluster_id);
@@ -1287,6 +1312,15 @@ event_t *create(cluster_t *cluster, uint32_t event_id)
12871312
return (event_t *)event;
12881313
}
12891314

1315+
esp_err_t destroy(cluster_t *cluster, event_t *event)
1316+
{
1317+
VerifyOrReturnError(cluster && event, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Cluster or event cannot be NULL"));
1318+
_cluster_t *current_cluster = (_cluster_t *)cluster;
1319+
_event_t *current_event = (_event_t *)event;
1320+
SinglyLinkedList<_event_t>::remove(&current_cluster->event_list, current_event);
1321+
return ESP_OK;
1322+
}
1323+
12901324
event_t *get(cluster_t *cluster, uint32_t event_id)
12911325
{
12921326
VerifyOrReturnValue(cluster, NULL, ESP_LOGE(TAG, "Cluster cannot be NULL."));
@@ -1377,7 +1411,7 @@ esp_err_t destroy(cluster_t *cluster)
13771411
_attribute_base_t *attribute = current_cluster->attribute_list;
13781412
while (attribute) {
13791413
_attribute_base_t *next_attribute = attribute->next;
1380-
attribute::destroy((attribute_t *)attribute);
1414+
attribute::free_attribute((attribute_t *)attribute);
13811415
attribute = next_attribute;
13821416
}
13831417

components/esp_matter/data_model/esp_matter_feature.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,15 @@ esp_err_t add(cluster_t *cluster)
517517
VerifyOrReturnError(cluster, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Cluster cannot be NULL"));
518518
update_feature_map(cluster, get_id());
519519

520+
command_t *on_command = esp_matter::command::get(cluster, OnOff::Commands::On::Id, COMMAND_FLAG_ACCEPTED);
521+
if (on_command) {
522+
esp_matter::command::destroy(cluster, on_command);
523+
}
524+
command_t *toggle_command = esp_matter::command::get(cluster, OnOff::Commands::Toggle::Id, COMMAND_FLAG_ACCEPTED);
525+
if (toggle_command) {
526+
esp_matter::command::destroy(cluster, toggle_command);
527+
}
528+
520529
return ESP_OK;
521530
}
522531

@@ -4259,6 +4268,14 @@ esp_err_t add(cluster_t *cluster)
42594268
{
42604269
VerifyOrReturnError(cluster, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Cluster cannot be NULL"));
42614270
update_feature_map(cluster, get_id());
4271+
command_t *stop_command = esp_matter::command::get(cluster, ClosureControl::Commands::Stop::Id, COMMAND_FLAG_ACCEPTED);
4272+
if (stop_command) {
4273+
esp_matter::command::destroy(cluster, stop_command);
4274+
}
4275+
event_t *movement_completed = esp_matter::event::get(cluster, ClosureControl::Events::MovementCompleted::Id);
4276+
if (movement_completed) {
4277+
esp_matter::event::destroy(cluster, movement_completed);
4278+
}
42624279
return ESP_OK;
42634280
}
42644281

components/esp_matter/data_model/private/esp_matter_data_model_priv.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,54 @@ esp_err_t get_val_internal(attribute_t *attribute, esp_matter_attr_val_t *val);
7272
*/
7373
esp_err_t set_val_internal(attribute_t *attribute, esp_matter_attr_val_t *val, bool call_callbacks = true);
7474

75+
/** Destroy attribute
76+
*
77+
* This function destroys an attribute that was created and added to a cluster.
78+
* It removes the attribute from the cluster's attribute list, frees any
79+
* allocated memory for the attribute value (e.g., string, array), and deletes
80+
* the attribute from NVS if it was stored there.
81+
*
82+
* @param[in] cluster Cluster handle.
83+
* @param[in] attribute Attribute handle.
84+
*
85+
* @return ESP_OK on success.
86+
* @return error in case of failure.
87+
*/
88+
esp_err_t destroy(cluster_t *cluster, attribute_t *attribute);
89+
7590
} // namespace attribute
7691

92+
namespace command {
93+
94+
/** Destroy command
95+
*
96+
* This function destroys a command that was created and added to a cluster.
97+
* It removes the command from the cluster's command list.
98+
*
99+
* @param[in] cluster Cluster handle.
100+
* @param[in] command Command handle.
101+
*
102+
* @return ESP_OK on success.
103+
* @return error in case of failure.
104+
*/
105+
esp_err_t destroy(cluster_t *cluster, command_t *command);
106+
107+
} // namespace command
108+
109+
namespace event {
110+
111+
/** Destroy event
112+
*
113+
* This function destroys an event that was created and added to a cluster.
114+
* It removes the event from the cluster's event list.
115+
*
116+
* @param[in] cluster Cluster handle.
117+
* @param[in] event Event handle.
118+
*
119+
* @return ESP_OK on success.
120+
* @return error in case of failure.
121+
*/
122+
esp_err_t destroy(cluster_t *cluster, event_t *event);
123+
124+
} // namespace event
77125
} // namespace esp_matter

0 commit comments

Comments
 (0)