37
37
#include < esp_matter_providers.h>
38
38
39
39
#include < esp_matter_nvs.h>
40
+ #include < singly_linked_list.h>
40
41
41
42
using chip::CommandId;
42
43
using chip::DataVersion;
@@ -273,18 +274,6 @@ static esp_err_t read_min_unused_endpoint_id()
273
274
#endif // defined(CONFIG_ESP_MATTER_ENABLE_MATTER_SERVER) && defined(CONFIG_ESP_MATTER_ENABLE_DATA_MODEL)
274
275
} /* node */
275
276
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
-
288
277
namespace command {
289
278
#if defined(CONFIG_ESP_MATTER_ENABLE_MATTER_SERVER) && defined(CONFIG_ESP_MATTER_ENABLE_DATA_MODEL)
290
279
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; }
297
286
command_entry_t *get_cluster_generated_command_list (uint32_t cluster_id) { return nullptr ; }
298
287
size_t get_cluster_generated_command_count (uint32_t cluster_id) {return 0 ; }
299
288
#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
- }
312
289
} /* command */
313
290
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
-
326
291
namespace attribute {
327
292
328
293
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
331
296
uint16_t attribute_size, uint8_t *value,
332
297
const EmberAfAttributeMetadata * attribute_metadata);
333
298
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
-
344
299
static EmberAfAttributeMetadata *get_external_attribute_metadata (_attribute_t * attribute)
345
300
{
346
301
if (NULL == attribute || (attribute->flags & ATTRIBUTE_FLAG_MANAGED_INTERNALLY)) {
@@ -511,7 +466,7 @@ esp_err_t enable(endpoint_t *endpoint)
511
466
512
467
/* Clusters */
513
468
_cluster_t *cluster = current_endpoint->cluster_list ;
514
- int cluster_count = cluster::get_count (cluster);
469
+ int cluster_count = SinglyLinkedList< _cluster_t >:: count (cluster);
515
470
int cluster_index = 0 ;
516
471
517
472
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)
560
515
/* Client Generated Commands */
561
516
command_flag = COMMAND_FLAG_ACCEPTED;
562
517
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);
564
519
command_count += command::get_cluster_accepted_command_count (cluster_id);
565
520
if (command_count > 0 ) {
566
521
command_index = 0 ;
@@ -588,7 +543,7 @@ esp_err_t enable(endpoint_t *endpoint)
588
543
/* Server Generated Commands */
589
544
command_flag = COMMAND_FLAG_GENERATED;
590
545
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);
592
547
command_count += command::get_cluster_generated_command_count (cluster_id);
593
548
if (command_count > 0 ) {
594
549
command_index = 0 ;
@@ -615,7 +570,7 @@ esp_err_t enable(endpoint_t *endpoint)
615
570
616
571
/* Event */
617
572
event = cluster->event_list ;
618
- event_count = event::get_count (event);
573
+ event_count = SinglyLinkedList< _event_t >:: count (event);
619
574
if (event_count > 0 ) {
620
575
event_index = 0 ;
621
576
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
1044
999
}
1045
1000
1046
1001
/* 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 (¤t_cluster->attribute_list , attribute);
1059
1003
return (attribute_t *)attribute;
1060
1004
}
1061
1005
@@ -1356,31 +1300,10 @@ command_t *create(cluster_t *cluster, uint32_t command_id, uint8_t flags, callba
1356
1300
command->user_callback = NULL ;
1357
1301
1358
1302
/* 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 (¤t_cluster->command_list , command);
1371
1304
return (command_t *)command;
1372
1305
}
1373
1306
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
-
1384
1307
command_t *get (cluster_t *cluster, uint32_t command_id, uint16_t flags)
1385
1308
{
1386
1309
VerifyOrReturnValue (cluster, NULL , ESP_LOGE (TAG, " Cluster cannot be NULL." ));
@@ -1470,31 +1393,10 @@ event_t *create(cluster_t *cluster, uint32_t event_id)
1470
1393
event->event_id = event_id;
1471
1394
1472
1395
/* 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 (¤t_cluster->event_list , event);
1485
1397
return (event_t *)event;
1486
1398
}
1487
1399
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
-
1498
1400
event_t *get (cluster_t *cluster, uint32_t event_id)
1499
1401
{
1500
1402
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)
1600
1502
matter_cluster->functions = NULL ;
1601
1503
1602
1504
/* 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 (¤t_endpoint->cluster_list , cluster);
1615
1506
return (cluster_t *)cluster;
1616
1507
}
1617
1508
@@ -1621,12 +1512,7 @@ static esp_err_t destroy(cluster_t *cluster)
1621
1512
_cluster_t *current_cluster = (_cluster_t *)cluster;
1622
1513
1623
1514
/* 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 (¤t_cluster->command_list );
1630
1516
1631
1517
/* Parse and delete all attributes */
1632
1518
_attribute_base_t *attribute = current_cluster->attribute_list ;
@@ -1637,12 +1523,7 @@ static esp_err_t destroy(cluster_t *cluster)
1637
1523
}
1638
1524
1639
1525
/* 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 (¤t_cluster->event_list );
1646
1527
1647
1528
/* Free */
1648
1529
esp_matter_mem_free (current_cluster);
@@ -1808,18 +1689,7 @@ endpoint_t *create(node_t *node, uint8_t flags, void *priv_data)
1808
1689
#endif // defined(CONFIG_ESP_MATTER_ENABLE_MATTER_SERVER) && defined(CONFIG_ESP_MATTER_ENABLE_DATA_MODEL)
1809
1690
1810
1691
/* 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 (¤t_node->endpoint_list , endpoint);
1823
1693
return (endpoint_t *)endpoint;
1824
1694
}
1825
1695
@@ -1998,6 +1868,7 @@ uint16_t get_count(node_t *node)
1998
1868
endpoint = get_next (endpoint);
1999
1869
}
2000
1870
return count;
1871
+
2001
1872
}
2002
1873
2003
1874
uint16_t get_id (endpoint_t *endpoint)
0 commit comments