Skip to content

Commit 9503928

Browse files
committed
Merge branch 'esp_matter/component_optimization' into 'main'
Added Singly Linked List with Common Utility Functions See merge request app-frameworks/esp-matter!853
2 parents b5ed1c3 + b322f94 commit 9503928

File tree

2 files changed

+138
-142
lines changed

2 files changed

+138
-142
lines changed

components/esp_matter/esp_matter_core.cpp

+13-142
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <esp_matter_providers.h>
3838

3939
#include <esp_matter_nvs.h>
40+
#include <singly_linked_list.h>
4041

4142
using chip::CommandId;
4243
using chip::DataVersion;
@@ -273,18 +274,6 @@ static esp_err_t read_min_unused_endpoint_id()
273274
#endif // defined(CONFIG_ESP_MATTER_ENABLE_MATTER_SERVER) && defined(CONFIG_ESP_MATTER_ENABLE_DATA_MODEL)
274275
} /* node */
275276

276-
namespace cluster {
277-
static int get_count(_cluster_t *current)
278-
{
279-
int count = 0;
280-
while (current) {
281-
current = current->next;
282-
count++;
283-
}
284-
return count;
285-
}
286-
} /* cluster */
287-
288277
namespace command {
289278
#if defined(CONFIG_ESP_MATTER_ENABLE_MATTER_SERVER) && defined(CONFIG_ESP_MATTER_ENABLE_DATA_MODEL)
290279
command_entry_t *get_cluster_accepted_command_list(uint32_t cluster_id);
@@ -297,32 +286,8 @@ size_t get_cluster_accepted_command_count(uint32_t cluster_id) { return 0; }
297286
command_entry_t *get_cluster_generated_command_list(uint32_t cluster_id) { return nullptr; }
298287
size_t get_cluster_generated_command_count(uint32_t cluster_id) {return 0; }
299288
#endif // defined(CONFIG_ESP_MATTER_ENABLE_MATTER_SERVER) && defined(CONFIG_ESP_MATTER_ENABLE_DATA_MODEL)
300-
301-
static int get_count(_command_t *current, int command_flag)
302-
{
303-
int count = 0;
304-
while (current) {
305-
if (current->flags & command_flag) {
306-
count++;
307-
}
308-
current = current->next;
309-
}
310-
return count;
311-
}
312289
} /* command */
313290

314-
namespace event {
315-
static int get_count(_event_t *current)
316-
{
317-
int count = 0;
318-
while (current) {
319-
count++;
320-
current = current->next;
321-
}
322-
return count;
323-
}
324-
}
325-
326291
namespace attribute {
327292

328293
esp_err_t get_data_from_attr_val(esp_matter_attr_val_t *val, EmberAfAttributeType *attribute_type,
@@ -331,16 +296,6 @@ esp_err_t get_attr_val_from_data(esp_matter_attr_val_t *val, EmberAfAttributeTyp
331296
uint16_t attribute_size, uint8_t *value,
332297
const EmberAfAttributeMetadata * attribute_metadata);
333298

334-
static int get_count(_attribute_base_t *current)
335-
{
336-
int count = 0;
337-
while (current) {
338-
current = current->next;
339-
count++;
340-
}
341-
return count;
342-
}
343-
344299
static EmberAfAttributeMetadata *get_external_attribute_metadata(_attribute_t * attribute)
345300
{
346301
if (NULL == attribute || (attribute->flags & ATTRIBUTE_FLAG_MANAGED_INTERNALLY)) {
@@ -511,7 +466,7 @@ esp_err_t enable(endpoint_t *endpoint)
511466

512467
/* Clusters */
513468
_cluster_t *cluster = current_endpoint->cluster_list;
514-
int cluster_count = cluster::get_count(cluster);
469+
int cluster_count = SinglyLinkedList<_cluster_t>::count(cluster);
515470
int cluster_index = 0;
516471

517472
DataVersion *data_versions_ptr = (DataVersion *)esp_matter_mem_calloc(1, cluster_count * sizeof(DataVersion));
@@ -560,7 +515,7 @@ esp_err_t enable(endpoint_t *endpoint)
560515
/* Client Generated Commands */
561516
command_flag = COMMAND_FLAG_ACCEPTED;
562517
command = cluster->command_list;
563-
command_count = command::get_count(command, command_flag);
518+
command_count = SinglyLinkedList<_command_t>::count_with_flag(command, command_flag);
564519
command_count += command::get_cluster_accepted_command_count(cluster_id);
565520
if (command_count > 0) {
566521
command_index = 0;
@@ -588,7 +543,7 @@ esp_err_t enable(endpoint_t *endpoint)
588543
/* Server Generated Commands */
589544
command_flag = COMMAND_FLAG_GENERATED;
590545
command = cluster->command_list;
591-
command_count = command::get_count(command, command_flag);
546+
command_count = SinglyLinkedList<_command_t>::count_with_flag(command, command_flag);
592547
command_count += command::get_cluster_generated_command_count(cluster_id);
593548
if (command_count > 0) {
594549
command_index = 0;
@@ -615,7 +570,7 @@ esp_err_t enable(endpoint_t *endpoint)
615570

616571
/* Event */
617572
event = cluster->event_list;
618-
event_count = event::get_count(event);
573+
event_count = SinglyLinkedList<_event_t>::count(event);
619574
if (event_count > 0) {
620575
event_index = 0;
621576
event_ids = (EventId *)esp_matter_mem_calloc(1, (event_count + 1) * sizeof(EventId));
@@ -1044,18 +999,7 @@ attribute_t *create(cluster_t *cluster, uint32_t attribute_id, uint16_t flags, e
1044999
}
10451000

10461001
/* Add */
1047-
_attribute_base_t *previous_attribute = NULL;
1048-
_attribute_base_t *current_attribute = current_cluster->attribute_list;
1049-
while (current_attribute) {
1050-
previous_attribute = current_attribute;
1051-
current_attribute = current_attribute->next;
1052-
}
1053-
if (previous_attribute == NULL) {
1054-
current_cluster->attribute_list = attribute;
1055-
} else {
1056-
previous_attribute->next = attribute;
1057-
}
1058-
1002+
SinglyLinkedList<_attribute_base_t>::append(&current_cluster->attribute_list, attribute);
10591003
return (attribute_t *)attribute;
10601004
}
10611005

@@ -1356,31 +1300,10 @@ command_t *create(cluster_t *cluster, uint32_t command_id, uint8_t flags, callba
13561300
command->user_callback = NULL;
13571301

13581302
/* Add */
1359-
_command_t *previous_command = NULL;
1360-
_command_t *current_command = current_cluster->command_list;
1361-
while (current_command) {
1362-
previous_command = current_command;
1363-
current_command = current_command->next;
1364-
}
1365-
if (previous_command == NULL) {
1366-
current_cluster->command_list = command;
1367-
} else {
1368-
previous_command->next = command;
1369-
}
1370-
1303+
SinglyLinkedList<_command_t>::append(&current_cluster->command_list, command);
13711304
return (command_t *)command;
13721305
}
13731306

1374-
static esp_err_t destroy(command_t *command)
1375-
{
1376-
VerifyOrReturnError(command, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Command cannot be NULL"));
1377-
_command_t *current_command = (_command_t *)command;
1378-
1379-
/* Free */
1380-
esp_matter_mem_free(current_command);
1381-
return ESP_OK;
1382-
}
1383-
13841307
command_t *get(cluster_t *cluster, uint32_t command_id, uint16_t flags)
13851308
{
13861309
VerifyOrReturnValue(cluster, NULL, ESP_LOGE(TAG, "Cluster cannot be NULL."));
@@ -1470,31 +1393,10 @@ event_t *create(cluster_t *cluster, uint32_t event_id)
14701393
event->event_id = event_id;
14711394

14721395
/* Add */
1473-
_event_t *previous_event = NULL;
1474-
_event_t *current_event = current_cluster->event_list;
1475-
while (current_event) {
1476-
previous_event = current_event;
1477-
current_event = current_event->next;
1478-
}
1479-
if (previous_event == NULL) {
1480-
current_cluster->event_list = event;
1481-
} else {
1482-
previous_event->next = event;
1483-
}
1484-
1396+
SinglyLinkedList<_event_t>::append(&current_cluster->event_list, event);
14851397
return (event_t *)event;
14861398
}
14871399

1488-
static esp_err_t destroy(event_t *event)
1489-
{
1490-
VerifyOrReturnError(event, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Event cannot be NULL"));
1491-
_event_t *current_event = (_event_t *)event;
1492-
1493-
/* Free */
1494-
esp_matter_mem_free(current_event);
1495-
return ESP_OK;
1496-
}
1497-
14981400
event_t *get(cluster_t *cluster, uint32_t event_id)
14991401
{
15001402
VerifyOrReturnValue(cluster, NULL, ESP_LOGE(TAG, "Cluster cannot be NULL."));
@@ -1600,18 +1502,7 @@ cluster_t *create(endpoint_t *endpoint, uint32_t cluster_id, uint8_t flags)
16001502
matter_cluster->functions = NULL;
16011503

16021504
/* Add */
1603-
_cluster_t *previous_cluster = NULL;
1604-
_cluster_t *current_cluster = current_endpoint->cluster_list;
1605-
while (current_cluster) {
1606-
previous_cluster = current_cluster;
1607-
current_cluster = current_cluster->next;
1608-
}
1609-
if (previous_cluster == NULL) {
1610-
current_endpoint->cluster_list = cluster;
1611-
} else {
1612-
previous_cluster->next = cluster;
1613-
}
1614-
1505+
SinglyLinkedList<_cluster_t>::append(&current_endpoint->cluster_list, cluster);
16151506
return (cluster_t *)cluster;
16161507
}
16171508

@@ -1621,12 +1512,7 @@ static esp_err_t destroy(cluster_t *cluster)
16211512
_cluster_t *current_cluster = (_cluster_t *)cluster;
16221513

16231514
/* Parse and delete all commands */
1624-
_command_t *command = current_cluster->command_list;
1625-
while (command) {
1626-
_command_t *next_command = command->next;
1627-
command::destroy((command_t *)command);
1628-
command = next_command;
1629-
}
1515+
SinglyLinkedList<_command_t>::delete_list(&current_cluster->command_list);
16301516

16311517
/* Parse and delete all attributes */
16321518
_attribute_base_t *attribute = current_cluster->attribute_list;
@@ -1637,12 +1523,7 @@ static esp_err_t destroy(cluster_t *cluster)
16371523
}
16381524

16391525
/* Parse and delete all events */
1640-
_event_t *event = current_cluster->event_list;
1641-
while (event) {
1642-
_event_t *next_event = event->next;
1643-
event::destroy((event_t *)event);
1644-
event = next_event;
1645-
}
1526+
SinglyLinkedList<_event_t>::delete_list(&current_cluster->event_list);
16461527

16471528
/* Free */
16481529
esp_matter_mem_free(current_cluster);
@@ -1808,18 +1689,7 @@ endpoint_t *create(node_t *node, uint8_t flags, void *priv_data)
18081689
#endif // defined(CONFIG_ESP_MATTER_ENABLE_MATTER_SERVER) && defined(CONFIG_ESP_MATTER_ENABLE_DATA_MODEL)
18091690

18101691
/* Add */
1811-
_endpoint_t *previous_endpoint = NULL;
1812-
_endpoint_t *current_endpoint = current_node->endpoint_list;
1813-
while (current_endpoint) {
1814-
previous_endpoint = current_endpoint;
1815-
current_endpoint = current_endpoint->next;
1816-
}
1817-
if (previous_endpoint == NULL) {
1818-
current_node->endpoint_list = endpoint;
1819-
} else {
1820-
previous_endpoint->next = endpoint;
1821-
}
1822-
1692+
SinglyLinkedList<_endpoint_t>::append(&current_node->endpoint_list, endpoint);
18231693
return (endpoint_t *)endpoint;
18241694
}
18251695

@@ -1998,6 +1868,7 @@ uint16_t get_count(node_t *node)
19981868
endpoint = get_next(endpoint);
19991869
}
20001870
return count;
1871+
20011872
}
20021873

20031874
uint16_t get_id(endpoint_t *endpoint)

0 commit comments

Comments
 (0)