Skip to content

Commit f21e7b4

Browse files
committed
Merge branch 'fix_dynamic_ep_check' into 'main'
Fix dynamic endpoint check and added abort in examples See merge request app-frameworks/esp-matter!607
2 parents fe4f9f6 + ec72e32 commit f21e7b4

File tree

28 files changed

+127
-152
lines changed

28 files changed

+127
-152
lines changed

components/esp_matter/esp_matter_core.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,12 @@ endpoint_t *create(node_t *node, uint8_t flags, void *priv_data)
18821882
}
18831883
_node_t *current_node = (_node_t *)node;
18841884

1885+
if (get_count(node) >= CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT) {
1886+
ESP_LOGE(TAG, "Dynamic endpoint count cannot be greater than CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT:%u",
1887+
CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT);
1888+
return NULL;
1889+
}
1890+
18851891
/* Allocate */
18861892
_endpoint_t *endpoint = (_endpoint_t *)esp_matter_mem_calloc(1, sizeof(_endpoint_t));
18871893
if (!endpoint) {

examples/all_device_types_app/main/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console app_reset console fatfs hal)
22

33
idf_component_register(SRC_DIRS "."
4-
PRIV_INCLUDE_DIRS "."
4+
PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils"
55
PRIV_REQUIRES ${PRIV_REQUIRES_LIST})
66

77
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)

examples/all_device_types_app/main/app_main.cpp

+6-16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <esp_matter_console.h>
1515
#include <esp_matter_ota.h>
1616

17+
#include <common_macros.h>
1718
#include <app_priv.h>
1819
#include <app_reset.h>
1920
#include "esp_console.h"
@@ -142,20 +143,14 @@ extern "C" void app_main()
142143

143144
/* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */
144145
node::config_t node_config;
146+
// node handle can be used to add/modify other endpoints.
145147
node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb);
146-
147-
/* These node and endpoint handles can be used to create/add other endpoints and clusters. */
148-
if (!node) {
149-
ESP_LOGE(TAG, "Matter node creation failed");
150-
}
148+
ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node"));
151149

152150
uint8_t device_type_index;
153151
if(esp_matter::nvs_helpers::get_device_type_from_nvs(&device_type_index) != ESP_OK) {
154152
semaphoreHandle = xSemaphoreCreateBinary();
155-
if (semaphoreHandle == NULL)
156-
{
157-
ESP_LOGE(TAG, "Failed to create semaphore %d");
158-
}
153+
ABORT_APP_ON_FAILURE(semaphoreHandle != nullptr, ESP_LOGE(TAG, "Failed to create semaphore"));
159154

160155
ESP_LOGI(TAG, "\r\n\r\nEnter command: create --device_type to get started");
161156
example::console::init();
@@ -165,9 +160,7 @@ extern "C" void app_main()
165160
semaphoreHandle = NULL;
166161

167162
example::console::deinit();
168-
}
169-
else
170-
{
163+
} else {
171164
esp_matter::data_model::create(device_type_index);
172165
}
173166

@@ -183,10 +176,7 @@ extern "C" void app_main()
183176

184177
/* Matter start */
185178
err = esp_matter::start(app_event_cb);
186-
if (err != ESP_OK) {
187-
ESP_LOGE(TAG, "Matter start failed: %d", err);
188-
}
189-
179+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err));
190180

191181
#if CONFIG_ENABLE_CHIP_SHELL
192182
esp_matter::console::diagnostics_register_commands();
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
idf_component_register(SRC_DIRS "."
22
PRIV_INCLUDE_DIRS "."
3-
"${ESP_MATTER_PATH}/examples/common/blemesh_platform")
3+
"${ESP_MATTER_PATH}/examples/common/blemesh_platform"
4+
"${ESP_MATTER_PATH}/examples/common/utils")
45

56
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
67
target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H")

examples/blemesh_bridge/main/app_main.cpp

+6-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <esp_matter_console.h>
1515
#include <esp_matter_ota.h>
1616

17+
#include <common_macros.h>
1718
#include <app_bridged_device.h>
1819

1920
#include "blemesh_bridge.h"
@@ -123,31 +124,22 @@ extern "C" void app_main()
123124

124125
/* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */
125126
node::config_t node_config;
127+
// node handle can be used to add/modify other endpoints.
126128
node_t *node = node::create(&node_config, app_attribute_update_cb, NULL);
127-
128-
/* These node and endpoint handles can be used to create/add other endpoints and clusters. */
129-
if (!node) {
130-
ESP_LOGE(TAG, "Matter node creation failed");
131-
}
129+
ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node"));
132130

133131
aggregator::config_t aggregator_config;
134132
endpoint_t *aggregator = endpoint::aggregator::create(node, &aggregator_config, ENDPOINT_FLAG_NONE, NULL);
135-
if (!aggregator) {
136-
ESP_LOGE(TAG, "Matter aggregator endpoint creation failed");
137-
}
133+
ABORT_APP_ON_FAILURE(aggregator != nullptr, ESP_LOGE(TAG, "Failed to create aggregator endpoint"));
138134

139135
aggregator_endpoint_id = endpoint::get_id(aggregator);
140136

141137
/* Matter start */
142138
err = esp_matter::start(app_event_cb);
143-
if (err != ESP_OK) {
144-
ESP_LOGE(TAG, "Matter start failed: %d", err);
145-
}
139+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err));
146140

147141
err = app_bridge_initialize(node, create_bridge_devices);
148-
if (err != ESP_OK) {
149-
ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err);
150-
}
142+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err));
151143

152144
#if CONFIG_ENABLE_CHIP_SHELL
153145
esp_matter::console::diagnostics_register_commands();

examples/common/utils/common_macros.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <stdlib.h>
18+
19+
#include <freertos/FreeRTOS.h>
20+
#include <freertos/task.h>
21+
22+
#define ABORT_APP_ON_FAILURE(x, ...) do { \
23+
if (!(unlikely(x))) { \
24+
__VA_ARGS__; \
25+
vTaskDelay(5000 / portTICK_PERIOD_MS); \
26+
abort(); \
27+
} \
28+
} while (0)
29+

examples/controller/main/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
idf_component_register(SRC_DIRS "."
2-
INCLUDE_DIRS ".")
2+
INCLUDE_DIRS "."
3+
PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils")
34

45
if(CONFIG_SPIFFS_ATTESTATION_TRUST_STORE)
56
spiffs_create_partition_image(paa_cert ${CMAKE_SOURCE_DIR}/paa_cert FLASH_IN_PROJECT)

examples/controller/main/app_main.cpp

+5-12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <esp_matter_thread_br_launcher.h>
2626
#include <esp_ot_config.h>
2727
#endif // CONFIG_OPENTHREAD_BORDER_ROUTER
28+
#include <common_macros.h>
2829
#include <app_reset.h>
2930

3031
#include <app/server/Server.h>
@@ -90,22 +91,14 @@ extern "C" void app_main()
9091
// to a specific fabric.
9192
node::config_t node_config;
9293
node_t *node = node::create(&node_config, NULL, NULL);
93-
if (!node) {
94-
ESP_LOGE(TAG, "Failed to create esp_matter node");
95-
return;
96-
}
97-
endpoint_t *root_endpoint = endpoint::get(node, 0);
98-
if (!root_endpoint) {
99-
ESP_LOGE(TAG, "Failed to create root_node endpoint");
100-
return;
101-
}
94+
ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node"));
95+
10296
#endif // !CONFIG_ESP_MATTER_COMMISSIONER_ENABLE
10397

10498
/* Matter start */
10599
err = esp_matter::start(app_event_cb);
106-
if (err != ESP_OK) {
107-
ESP_LOGE(TAG, "Matter start failed: %d", err);
108-
}
100+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err));
101+
109102
#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE
110103
esp_matter::lock::chip_stack_lock(portMAX_DELAY);
111104
esp_matter::commissioner::init(5580);
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
idf_component_register(SRC_DIRS "."
2-
PRIV_INCLUDE_DIRS ".")
2+
PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils")
33

44
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
55
target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H")

examples/demo/badge/main/app_main.cpp

+6-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <esp_matter_ota.h>
1616
#include <esp_matter_attribute_utils.h>
1717

18+
#include <common_macros.h>
1819
#include <app_priv.h>
1920
#include <app_reset.h>
2021
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
@@ -302,23 +303,21 @@ extern "C" void app_main()
302303
/* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */
303304
node::config_t node_config;
304305
node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb);
306+
ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node"));
305307

306308
on_off_light::config_t light_config;
307309
light_config.on_off.on_off = DEFAULT_POWER;
308310
light_config.on_off.lighting.start_up_on_off = nullptr;
309311
endpoint_t *endpoint = on_off_light::create(node, &light_config, ENDPOINT_FLAG_NONE, NULL);
310-
311-
/* These node and endpoint handles can be used to create/add other endpoints and clusters. */
312-
if (!node || !endpoint) {
313-
ESP_LOGE(TAG, "Matter node creation failed");
314-
}
312+
ABORT_APP_ON_FAILURE(endpoint != nullptr, ESP_LOGE(TAG, "Failed to create on off light endpoint"));
315313

316314
light_endpoint_id = endpoint::get_id(endpoint);
317315
ESP_LOGI(TAG, "Light created with endpoint_id %d", light_endpoint_id);
318316

319317
/* Create custom badge cluster in basic-information endpoint */
320318
uint32_t custom_cluster_id = BADGE_CLUSTER_ID;
321319
cluster_t *badge_cluster = cluster::create(endpoint::get(node, 0x0), custom_cluster_id, CLUSTER_FLAG_SERVER);
320+
ABORT_APP_ON_FAILURE(badge_cluster != nullptr, ESP_LOGE(TAG, "Failed to create badge cluster"));
322321

323322
/* Create custom attributes in badge cluster for retrieving the vcard properties */
324323
attribute::create(badge_cluster, NAME_ATTRIBUTE_ID, ATTRIBUTE_FLAG_WRITABLE | ATTRIBUTE_FLAG_NONVOLATILE, esp_matter_long_char_str("", MAX_ATTR_SIZE));
@@ -341,19 +340,15 @@ extern "C" void app_main()
341340

342341
/* Matter start */
343342
err = esp_matter::start(app_event_cb);
344-
if (err != ESP_OK) {
345-
ESP_LOGE(TAG, "Matter start failed: %d", err);
346-
}
343+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err));
347344

348345
/* Starting driver with default values */
349346
if (chip::Server::GetInstance().GetFabricTable().FabricCount())
350347
app_driver_light_set_defaults(light_endpoint_id);
351348

352349
#if CONFIG_ENABLE_ENCRYPTED_OTA
353350
err = esp_matter_ota_requestor_encrypted_init(s_decryption_key, s_decryption_key_len);
354-
if (err != ESP_OK) {
355-
ESP_LOGE(TAG, "Failed to initialized the encrypted OTA, err: %d", err);
356-
}
351+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to initialized the encrypted OTA, err: %d", err));
357352
#endif // CONFIG_ENABLE_ENCRYPTED_OTA
358353

359354
#if CONFIG_ENABLE_CHIP_SHELL
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
idf_component_register(SRC_DIRS "."
2-
PRIV_INCLUDE_DIRS ".")
2+
PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils")
33

44
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
55
target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H")

examples/esp-now_bridge_light/main/app_main.cpp

+10-18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <esp_matter_console.h>
1515
#include <esp_matter_ota.h>
1616
#include <app_bridged_device.h>
17+
#include <common_macros.h>
1718
#include <app_priv.h>
1819
#include <app_espnow.h>
1920
#include <app_reset.h>
@@ -141,12 +142,9 @@ extern "C" void app_main()
141142

142143
/* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */
143144
node::config_t node_config;
145+
// node handle can be used to add/modify other endpoints.
144146
node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb);
145-
146-
/* This node handle can be used to create/add other endpoints and clusters. */
147-
if (!node) {
148-
ESP_LOGE(TAG, "Matter node creation failed");
149-
}
147+
ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node"));
150148

151149
color_temperature_light::config_t light_config;
152150
light_config.on_off.on_off = DEFAULT_POWER;
@@ -156,22 +154,20 @@ extern "C" void app_main()
156154
light_config.color_control.color_mode = (uint8_t)ColorControl::ColorMode::kColorTemperature;
157155
light_config.color_control.enhanced_color_mode = (uint8_t)ColorControl::ColorMode::kColorTemperature;
158156
light_config.color_control.color_temperature.startup_color_temperature_mireds = nullptr;
157+
158+
// endpoint handles can be used to add/modify clusters.
159159
endpoint_t *endpoint = color_temperature_light::create(node, &light_config, ENDPOINT_FLAG_NONE, light_handle);
160-
if (!endpoint) {
161-
ESP_LOGE(TAG, "Matter color temperature light endpoint creation failed");
162-
}
160+
ABORT_APP_ON_FAILURE(endpoint != nullptr, ESP_LOGE(TAG, "Failed to create color temperature light endpoint"));
163161

164162
aggregator::config_t aggregator_config;
165163
endpoint_t *aggregator = endpoint::aggregator::create(node, &aggregator_config, ENDPOINT_FLAG_NONE, NULL);
166-
if (!aggregator) {
167-
ESP_LOGE(TAG, "Matter aggregator endpoint creation failed");
168-
}
164+
ABORT_APP_ON_FAILURE(aggregator != nullptr, ESP_LOGE(TAG, "Failed to create aggregator endpoint"));
169165

170166
light_endpoint_id = endpoint::get_id(endpoint);
171167
ESP_LOGI(TAG, "Light created with endpoint_id %d", light_endpoint_id);
172168

173169
aggregator_endpoint_id = endpoint::get_id(aggregator);
174-
ESP_LOGI(TAG, "Switch created with endpoint id %d", aggregator_endpoint_id);
170+
ESP_LOGI(TAG, "Aggregator created with endpoint id %d", aggregator_endpoint_id);
175171

176172
/* Add additional features to the node */
177173
cluster_t *cluster = cluster::get(endpoint, ColorControl::Id);
@@ -182,14 +178,10 @@ extern "C" void app_main()
182178

183179
/* Matter start */
184180
err = esp_matter::start(app_event_cb);
185-
if (err != ESP_OK) {
186-
ESP_LOGE(TAG, "Matter start failed: %d", err);
187-
}
181+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err));
188182

189183
err = app_bridge_initialize(node, create_bridge_devices);
190-
if (err != ESP_OK) {
191-
ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err);
192-
}
184+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err));
193185

194186
app_espnow_init();
195187

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
idf_component_register(SRC_DIRS "."
2-
PRIV_INCLUDE_DIRS ".")
2+
PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils")
33

44
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
55
target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H")

examples/generic_switch/main/app_main.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <esp_matter_console.h>
1515
#include <esp_matter_ota.h>
1616

17+
#include <common_macros.h>
1718
#include <app_priv.h>
1819
#include <app_reset.h>
1920
#include <app/util/attribute-storage.h>
@@ -202,9 +203,11 @@ extern "C" void app_main()
202203
/* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */
203204
node::config_t node_config;
204205
node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb);
206+
ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node"));
205207

206208
/* Call for Boot button */
207209
err = create_button(NULL, node);
210+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to create generic switch button"));
208211

209212
/* Use the code snippet commented below to create more physical buttons. */
210213

@@ -226,16 +229,15 @@ extern "C" void app_main()
226229

227230
/* Matter start */
228231
err = esp_matter::start(app_event_cb);
229-
if (err != ESP_OK) {
230-
ESP_LOGE(TAG, "Matter start failed: %d", err);
231-
}
232+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err));
232233

233234
SetTagList(0, chip::Span<const Descriptor::Structs::SemanticTagStruct::Type>(gEp0TagList));
234235
SetTagList(1, chip::Span<const Descriptor::Structs::SemanticTagStruct::Type>(gEp1TagList));
235236

236-
237237
nvs_handle_t handle;
238238
nvs_open_from_partition(CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL, "chip-factory", NVS_READWRITE, &handle);
239+
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to open namespace:chip-factory from partition:"
240+
CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL ", err:%d", err));
239241

240242
int32_t out_value = 0;
241243
if (nvs_get_i32(handle, "fl-sz/1", &out_value) == ESP_ERR_NVS_NOT_FOUND)

examples/light/main/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
idf_component_register(SRC_DIRS "."
2-
PRIV_INCLUDE_DIRS ".")
2+
PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils")
33

44
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
55
target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H")

0 commit comments

Comments
 (0)