From ec72e3209d76ad85368f84b8231083a762d2b587 Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Mon, 15 Jan 2024 18:04:31 +0530 Subject: [PATCH] examples: added ABORT_APP_ON_FAILURE macro to abort if APIs return error --- .../all_device_types_app/main/CMakeLists.txt | 2 +- .../all_device_types_app/main/app_main.cpp | 22 ++++---------- examples/blemesh_bridge/main/CMakeLists.txt | 3 +- examples/blemesh_bridge/main/app_main.cpp | 20 ++++--------- examples/common/utils/common_macros.h | 29 +++++++++++++++++++ examples/controller/main/CMakeLists.txt | 3 +- examples/controller/main/app_main.cpp | 17 ++++------- examples/demo/badge/main/CMakeLists.txt | 2 +- examples/demo/badge/main/app_main.cpp | 17 ++++------- .../esp-now_bridge_light/main/CMakeLists.txt | 2 +- .../esp-now_bridge_light/main/app_main.cpp | 28 +++++++----------- examples/generic_switch/main/CMakeLists.txt | 2 +- examples/generic_switch/main/app_main.cpp | 10 ++++--- examples/light/main/CMakeLists.txt | 2 +- examples/light/main/app_main.cpp | 20 ++++++------- examples/light_switch/main/CMakeLists.txt | 2 +- examples/light_switch/main/app_main.cpp | 12 +++----- examples/refrigerator/main/CMakeLists.txt | 2 +- examples/refrigerator/main/app_main.cpp | 17 +++++------ .../room_air_conditioner/main/CMakeLists.txt | 2 +- .../room_air_conditioner/main/app_main.cpp | 12 +++----- examples/sleepy_device/main/CMakeLists.txt | 2 +- examples/sleepy_device/main/app_main.cpp | 13 ++++----- examples/zap_light/main/CMakeLists.txt | 2 +- examples/zap_light/main/app_main.cpp | 5 ++-- examples/zigbee_bridge/main/CMakeLists.txt | 2 +- examples/zigbee_bridge/main/app_main.cpp | 23 +++++---------- 27 files changed, 121 insertions(+), 152 deletions(-) create mode 100644 examples/common/utils/common_macros.h diff --git a/examples/all_device_types_app/main/CMakeLists.txt b/examples/all_device_types_app/main/CMakeLists.txt index 30caf463c..1fa311502 100644 --- a/examples/all_device_types_app/main/CMakeLists.txt +++ b/examples/all_device_types_app/main/CMakeLists.txt @@ -1,7 +1,7 @@ set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console app_reset console fatfs hal) idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS "." + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils" PRIV_REQUIRES ${PRIV_REQUIRES_LIST}) set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) diff --git a/examples/all_device_types_app/main/app_main.cpp b/examples/all_device_types_app/main/app_main.cpp index 30a83f93f..1ff726254 100644 --- a/examples/all_device_types_app/main/app_main.cpp +++ b/examples/all_device_types_app/main/app_main.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include "esp_console.h" @@ -101,20 +102,14 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; + // node handle can be used to add/modify other endpoints. node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); - - /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ - if (!node) { - ESP_LOGE(TAG, "Matter node creation failed"); - } + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); uint8_t device_type_index; if(esp_matter::nvs_helpers::get_device_type_from_nvs(&device_type_index) != ESP_OK) { semaphoreHandle = xSemaphoreCreateBinary(); - if (semaphoreHandle == NULL) - { - ESP_LOGE(TAG, "Failed to create semaphore %d"); - } + ABORT_APP_ON_FAILURE(semaphoreHandle != nullptr, ESP_LOGE(TAG, "Failed to create semaphore")); ESP_LOGI(TAG, "\r\n\r\nEnter command: create --device_type to get started"); example::console::init(); @@ -124,9 +119,7 @@ extern "C" void app_main() semaphoreHandle = NULL; example::console::deinit(); - } - else - { + } else { esp_matter::data_model::create(device_type_index); } @@ -142,10 +135,7 @@ extern "C" void app_main() /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } - + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); #if CONFIG_ENABLE_CHIP_SHELL esp_matter::console::diagnostics_register_commands(); diff --git a/examples/blemesh_bridge/main/CMakeLists.txt b/examples/blemesh_bridge/main/CMakeLists.txt index 2c2211557..4564ca7c1 100644 --- a/examples/blemesh_bridge/main/CMakeLists.txt +++ b/examples/blemesh_bridge/main/CMakeLists.txt @@ -1,6 +1,7 @@ idf_component_register(SRC_DIRS "." PRIV_INCLUDE_DIRS "." - "${ESP_MATTER_PATH}/examples/common/blemesh_platform") + "${ESP_MATTER_PATH}/examples/common/blemesh_platform" + "${ESP_MATTER_PATH}/examples/common/utils") set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/blemesh_bridge/main/app_main.cpp b/examples/blemesh_bridge/main/app_main.cpp index 9c7446d7e..0bde1554f 100644 --- a/examples/blemesh_bridge/main/app_main.cpp +++ b/examples/blemesh_bridge/main/app_main.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include "blemesh_bridge.h" @@ -123,31 +124,22 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; + // node handle can be used to add/modify other endpoints. node_t *node = node::create(&node_config, app_attribute_update_cb, NULL); - - /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ - if (!node) { - ESP_LOGE(TAG, "Matter node creation failed"); - } + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); aggregator::config_t aggregator_config; endpoint_t *aggregator = endpoint::aggregator::create(node, &aggregator_config, ENDPOINT_FLAG_NONE, NULL); - if (!aggregator) { - ESP_LOGE(TAG, "Matter aggregator endpoint creation failed"); - } + ABORT_APP_ON_FAILURE(aggregator != nullptr, ESP_LOGE(TAG, "Failed to create aggregator endpoint")); aggregator_endpoint_id = endpoint::get_id(aggregator); /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); err = app_bridge_initialize(node, create_bridge_devices); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err)); #if CONFIG_ENABLE_CHIP_SHELL esp_matter::console::diagnostics_register_commands(); diff --git a/examples/common/utils/common_macros.h b/examples/common/utils/common_macros.h new file mode 100644 index 000000000..04497e304 --- /dev/null +++ b/examples/common/utils/common_macros.h @@ -0,0 +1,29 @@ +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include +#include + +#define ABORT_APP_ON_FAILURE(x, ...) do { \ + if (!(unlikely(x))) { \ + __VA_ARGS__; \ + vTaskDelay(5000 / portTICK_PERIOD_MS); \ + abort(); \ + } \ + } while (0) + diff --git a/examples/controller/main/CMakeLists.txt b/examples/controller/main/CMakeLists.txt index 5d45edd60..75413ea06 100644 --- a/examples/controller/main/CMakeLists.txt +++ b/examples/controller/main/CMakeLists.txt @@ -1,5 +1,6 @@ idf_component_register(SRC_DIRS "." - INCLUDE_DIRS ".") + INCLUDE_DIRS "." + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") if(CONFIG_SPIFFS_ATTESTATION_TRUST_STORE) spiffs_create_partition_image(paa_cert ${CMAKE_SOURCE_DIR}/paa_cert FLASH_IN_PROJECT) diff --git a/examples/controller/main/app_main.cpp b/examples/controller/main/app_main.cpp index 1dd1ebfae..eeece08f1 100644 --- a/examples/controller/main/app_main.cpp +++ b/examples/controller/main/app_main.cpp @@ -25,6 +25,7 @@ #include #include #endif // CONFIG_OPENTHREAD_BORDER_ROUTER +#include #include #include @@ -90,22 +91,14 @@ extern "C" void app_main() // to a specific fabric. node::config_t node_config; node_t *node = node::create(&node_config, NULL, NULL); - if (!node) { - ESP_LOGE(TAG, "Failed to create esp_matter node"); - return; - } - endpoint_t *root_endpoint = endpoint::get(node, 0); - if (!root_endpoint) { - ESP_LOGE(TAG, "Failed to create root_node endpoint"); - return; - } + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); + #endif // !CONFIG_ESP_MATTER_COMMISSIONER_ENABLE /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); + #if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE esp_matter::lock::chip_stack_lock(portMAX_DELAY); esp_matter::commissioner::init(5580); diff --git a/examples/demo/badge/main/CMakeLists.txt b/examples/demo/badge/main/CMakeLists.txt index f48a36750..43626a795 100644 --- a/examples/demo/badge/main/CMakeLists.txt +++ b/examples/demo/badge/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS ".") + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/demo/badge/main/app_main.cpp b/examples/demo/badge/main/app_main.cpp index 8884ba342..f83cf15bf 100644 --- a/examples/demo/badge/main/app_main.cpp +++ b/examples/demo/badge/main/app_main.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include #if CHIP_DEVICE_CONFIG_ENABLE_THREAD @@ -302,16 +303,13 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); on_off_light::config_t light_config; light_config.on_off.on_off = DEFAULT_POWER; light_config.on_off.lighting.start_up_on_off = nullptr; endpoint_t *endpoint = on_off_light::create(node, &light_config, ENDPOINT_FLAG_NONE, NULL); - - /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ - if (!node || !endpoint) { - ESP_LOGE(TAG, "Matter node creation failed"); - } + ABORT_APP_ON_FAILURE(endpoint != nullptr, ESP_LOGE(TAG, "Failed to create on off light endpoint")); light_endpoint_id = endpoint::get_id(endpoint); ESP_LOGI(TAG, "Light created with endpoint_id %d", light_endpoint_id); @@ -319,6 +317,7 @@ extern "C" void app_main() /* Create custom badge cluster in basic-information endpoint */ uint32_t custom_cluster_id = BADGE_CLUSTER_ID; cluster_t *badge_cluster = cluster::create(endpoint::get(node, 0x0), custom_cluster_id, CLUSTER_FLAG_SERVER); + ABORT_APP_ON_FAILURE(badge_cluster != nullptr, ESP_LOGE(TAG, "Failed to create badge cluster")); /* Create custom attributes in badge cluster for retrieving the vcard properties */ attribute::create(badge_cluster, NAME_ATTRIBUTE_ID, ATTRIBUTE_FLAG_WRITABLE | ATTRIBUTE_FLAG_NONVOLATILE, esp_matter_long_char_str("", MAX_ATTR_SIZE)); @@ -341,9 +340,7 @@ extern "C" void app_main() /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); /* Starting driver with default values */ if (chip::Server::GetInstance().GetFabricTable().FabricCount()) @@ -351,9 +348,7 @@ extern "C" void app_main() #if CONFIG_ENABLE_ENCRYPTED_OTA err = esp_matter_ota_requestor_encrypted_init(s_decryption_key, s_decryption_key_len); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Failed to initialized the encrypted OTA, err: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to initialized the encrypted OTA, err: %d", err)); #endif // CONFIG_ENABLE_ENCRYPTED_OTA #if CONFIG_ENABLE_CHIP_SHELL diff --git a/examples/esp-now_bridge_light/main/CMakeLists.txt b/examples/esp-now_bridge_light/main/CMakeLists.txt index f48a36750..43626a795 100644 --- a/examples/esp-now_bridge_light/main/CMakeLists.txt +++ b/examples/esp-now_bridge_light/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS ".") + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/esp-now_bridge_light/main/app_main.cpp b/examples/esp-now_bridge_light/main/app_main.cpp index 5d114527a..1703ef29e 100644 --- a/examples/esp-now_bridge_light/main/app_main.cpp +++ b/examples/esp-now_bridge_light/main/app_main.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -141,12 +142,9 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; + // node handle can be used to add/modify other endpoints. node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); - - /* This node handle can be used to create/add other endpoints and clusters. */ - if (!node) { - ESP_LOGE(TAG, "Matter node creation failed"); - } + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); color_temperature_light::config_t light_config; light_config.on_off.on_off = DEFAULT_POWER; @@ -156,22 +154,20 @@ extern "C" void app_main() light_config.color_control.color_mode = (uint8_t)ColorControl::ColorMode::kColorTemperature; light_config.color_control.enhanced_color_mode = (uint8_t)ColorControl::ColorMode::kColorTemperature; light_config.color_control.color_temperature.startup_color_temperature_mireds = nullptr; + + // endpoint handles can be used to add/modify clusters. endpoint_t *endpoint = color_temperature_light::create(node, &light_config, ENDPOINT_FLAG_NONE, light_handle); - if (!endpoint) { - ESP_LOGE(TAG, "Matter color temperature light endpoint creation failed"); - } + ABORT_APP_ON_FAILURE(endpoint != nullptr, ESP_LOGE(TAG, "Failed to create color temperature light endpoint")); aggregator::config_t aggregator_config; endpoint_t *aggregator = endpoint::aggregator::create(node, &aggregator_config, ENDPOINT_FLAG_NONE, NULL); - if (!aggregator) { - ESP_LOGE(TAG, "Matter aggregator endpoint creation failed"); - } + ABORT_APP_ON_FAILURE(aggregator != nullptr, ESP_LOGE(TAG, "Failed to create aggregator endpoint")); light_endpoint_id = endpoint::get_id(endpoint); ESP_LOGI(TAG, "Light created with endpoint_id %d", light_endpoint_id); aggregator_endpoint_id = endpoint::get_id(aggregator); - ESP_LOGI(TAG, "Switch created with endpoint id %d", aggregator_endpoint_id); + ESP_LOGI(TAG, "Aggregator created with endpoint id %d", aggregator_endpoint_id); /* Add additional features to the node */ cluster_t *cluster = cluster::get(endpoint, ColorControl::Id); @@ -182,14 +178,10 @@ extern "C" void app_main() /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); err = app_bridge_initialize(node, create_bridge_devices); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err)); app_espnow_init(); diff --git a/examples/generic_switch/main/CMakeLists.txt b/examples/generic_switch/main/CMakeLists.txt index f48a36750..43626a795 100644 --- a/examples/generic_switch/main/CMakeLists.txt +++ b/examples/generic_switch/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS ".") + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/generic_switch/main/app_main.cpp b/examples/generic_switch/main/app_main.cpp index ebb54c947..a090c49ac 100644 --- a/examples/generic_switch/main/app_main.cpp +++ b/examples/generic_switch/main/app_main.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -202,9 +203,11 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); /* Call for Boot button */ err = create_button(NULL, node); + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to create generic switch button")); /* Use the code snippet commented below to create more physical buttons. */ @@ -226,16 +229,15 @@ extern "C" void app_main() /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); SetTagList(0, chip::Span(gEp0TagList)); SetTagList(1, chip::Span(gEp1TagList)); - nvs_handle_t handle; nvs_open_from_partition(CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL, "chip-factory", NVS_READWRITE, &handle); + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to open namespace:chip-factory from partition:" + CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL ", err:%d", err)); int32_t out_value = 0; if (nvs_get_i32(handle, "fl-sz/1", &out_value) == ESP_ERR_NVS_NOT_FOUND) diff --git a/examples/light/main/CMakeLists.txt b/examples/light/main/CMakeLists.txt index f48a36750..43626a795 100644 --- a/examples/light/main/CMakeLists.txt +++ b/examples/light/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS ".") + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/light/main/app_main.cpp b/examples/light/main/app_main.cpp index f4fbdc373..1d61acb2b 100644 --- a/examples/light/main/app_main.cpp +++ b/examples/light/main/app_main.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #if CHIP_DEVICE_CONFIG_ENABLE_THREAD @@ -156,7 +157,10 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; + + // node handle can be used to add/modify other endpoints. node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); extended_color_light::config_t light_config; light_config.on_off.on_off = DEFAULT_POWER; @@ -166,12 +170,10 @@ extern "C" void app_main() light_config.color_control.color_mode = (uint8_t)ColorControl::ColorMode::kColorTemperature; light_config.color_control.enhanced_color_mode = (uint8_t)ColorControl::ColorMode::kColorTemperature; light_config.color_control.color_temperature.startup_color_temperature_mireds = nullptr; - endpoint_t *endpoint = extended_color_light::create(node, &light_config, ENDPOINT_FLAG_NONE, light_handle); - /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ - if (!node || !endpoint) { - ESP_LOGE(TAG, "Matter node creation failed"); - } + // endpoint handles can be used to add/modify clusters. + endpoint_t *endpoint = extended_color_light::create(node, &light_config, ENDPOINT_FLAG_NONE, light_handle); + ABORT_APP_ON_FAILURE(endpoint != nullptr, ESP_LOGE(TAG, "Failed to create extended color light endpoint")); light_endpoint_id = endpoint::get_id(endpoint); ESP_LOGI(TAG, "Light created with endpoint_id %d", light_endpoint_id); @@ -201,18 +203,14 @@ extern "C" void app_main() /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); /* Starting driver with default values */ app_driver_light_set_defaults(light_endpoint_id); #if CONFIG_ENABLE_ENCRYPTED_OTA err = esp_matter_ota_requestor_encrypted_init(s_decryption_key, s_decryption_key_len); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Failed to initialized the encrypted OTA, err: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to initialized the encrypted OTA, err: %d", err)); #endif // CONFIG_ENABLE_ENCRYPTED_OTA #if CONFIG_ENABLE_CHIP_SHELL diff --git a/examples/light_switch/main/CMakeLists.txt b/examples/light_switch/main/CMakeLists.txt index 76c4fe44b..ebb991411 100644 --- a/examples/light_switch/main/CMakeLists.txt +++ b/examples/light_switch/main/CMakeLists.txt @@ -5,7 +5,7 @@ if (CONFIG_DYNAMIC_PASSCODE_COMMISSIONABLE_DATA_PROVIDER) endif() idf_component_register(SRC_DIRS ${SRC_DIRS_LIST} - PRIV_INCLUDE_DIRS ".") + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/light_switch/main/app_main.cpp b/examples/light_switch/main/app_main.cpp index cb97da585..377973ea3 100644 --- a/examples/light_switch/main/app_main.cpp +++ b/examples/light_switch/main/app_main.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include #if CHIP_DEVICE_CONFIG_ENABLE_THREAD @@ -107,14 +108,11 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); on_off_switch::config_t switch_config; endpoint_t *endpoint = on_off_switch::create(node, &switch_config, ENDPOINT_FLAG_NONE, switch_handle); - - /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ - if (!node || !endpoint) { - ESP_LOGE(TAG, "Matter node creation failed"); - } + ABORT_APP_ON_FAILURE(endpoint != nullptr, ESP_LOGE(TAG, "Failed to create on off switch endpoint")); /* Add group cluster to the switch endpoint */ cluster::groups::config_t groups_config; @@ -140,9 +138,7 @@ extern "C" void app_main() /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); #if CONFIG_ENABLE_CHIP_SHELL esp_matter::console::diagnostics_register_commands(); diff --git a/examples/refrigerator/main/CMakeLists.txt b/examples/refrigerator/main/CMakeLists.txt index 84bff0916..4de6b9521 100644 --- a/examples/refrigerator/main/CMakeLists.txt +++ b/examples/refrigerator/main/CMakeLists.txt @@ -18,7 +18,7 @@ set(exclude_srcs_list "${MATTER_SDK_PATH}/examples/all-clusters-app/all-cluste idf_component_register(SRC_DIRS ${SRC_DIRS_LIST} EXCLUDE_SRCS ${exclude_srcs_list} INCLUDE_DIRS ${INCLUDE_DIRS_LIST} - PRIV_INCLUDE_DIRS ".") + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/refrigerator/main/app_main.cpp b/examples/refrigerator/main/app_main.cpp index 3f8c4e460..47bf3f172 100644 --- a/examples/refrigerator/main/app_main.cpp +++ b/examples/refrigerator/main/app_main.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -101,19 +102,18 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); // "Identify", "Groups", "Scenes", "Refrigerator Mode Select" and "Refrigerator Alarm" are optional cluster for refrigerator device type so we are not adding them by default. refrigerator::config_t refrigerator_config; endpoint_t *endpoint = refrigerator::create(node, &refrigerator_config, ENDPOINT_FLAG_NONE, NULL); + ABORT_APP_ON_FAILURE(endpoint != nullptr, ESP_LOGE(TAG, "Failed to create refrigerator endpoint")); // "Temperature Measurement", "Refrigerator and Temperature Controlled Cabinet Mode Select" are optional cluster for temperature_controlled_cabinet device type so we are not adding them by default. temperature_controlled_cabinet::config_t temperature_controlled_cabinet_config; endpoint_t *endpoint1 = temperature_controlled_cabinet::create(node, &temperature_controlled_cabinet_config, ENDPOINT_FLAG_NONE, NULL); + ABORT_APP_ON_FAILURE(endpoint1 != nullptr, ESP_LOGE(TAG, "Failed to create temperature controlled cabinet endpoint")); - /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ - if (!node || !endpoint || !endpoint1) { - ESP_LOGE(TAG, "Matter node creation failed"); - } esp_matter::cluster_t *cluster = esp_matter::cluster::get(endpoint1, chip::app::Clusters::TemperatureControl::Id); // Atlest one of temperature_number and temperature_level feature is mandatory. @@ -127,9 +127,7 @@ extern "C" void app_main() ESP_LOGI(TAG, "Temperature controlled cabinet created with endpoint_id %d", temp_ctrl_endpoint_id); err = set_parent_endpoint(endpoint1, endpoint); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Failed to set parent %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to set parent endpoint, err:%d", err)); #if CHIP_DEVICE_CONFIG_ENABLE_THREAD /* Set OpenThread platform config */ @@ -143,9 +141,8 @@ extern "C" void app_main() /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); + chip::app::Clusters::TemperatureControl::SetInstance(&sAppSupportedTemperatureLevelsDelegate); #if CONFIG_ENABLE_CHIP_SHELL diff --git a/examples/room_air_conditioner/main/CMakeLists.txt b/examples/room_air_conditioner/main/CMakeLists.txt index f48a36750..43626a795 100644 --- a/examples/room_air_conditioner/main/CMakeLists.txt +++ b/examples/room_air_conditioner/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS ".") + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/room_air_conditioner/main/app_main.cpp b/examples/room_air_conditioner/main/app_main.cpp index b30c07d6c..b5376af94 100644 --- a/examples/room_air_conditioner/main/app_main.cpp +++ b/examples/room_air_conditioner/main/app_main.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #if CHIP_DEVICE_CONFIG_ENABLE_THREAD @@ -144,15 +145,12 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); room_air_conditioner::config_t room_air_conditioner_config; room_air_conditioner_config.on_off.on_off = DEFAULT_POWER; endpoint_t *endpoint = room_air_conditioner::create(node, &room_air_conditioner_config, ENDPOINT_FLAG_NONE, room_air_conditioner_handle); - - /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ - if (!node || !endpoint) { - ESP_LOGE(TAG, "Matter node creation failed"); - } + ABORT_APP_ON_FAILURE(endpoint != nullptr, ESP_LOGE(TAG, "Failed to create room air conditioner endpoint")); room_air_conditioner_endpoint_id = endpoint::get_id(endpoint); ESP_LOGI(TAG, "Room Air Conditioner created with endpoint_id %d", room_air_conditioner_endpoint_id); @@ -169,9 +167,7 @@ extern "C" void app_main() /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); /* Starting driver with default values */ app_driver_room_air_conditioner_set_defaults(room_air_conditioner_endpoint_id); diff --git a/examples/sleepy_device/main/CMakeLists.txt b/examples/sleepy_device/main/CMakeLists.txt index f48a36750..43626a795 100644 --- a/examples/sleepy_device/main/CMakeLists.txt +++ b/examples/sleepy_device/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS ".") + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/sleepy_device/main/app_main.cpp b/examples/sleepy_device/main/app_main.cpp index 25554b3f6..6cf326325 100644 --- a/examples/sleepy_device/main/app_main.cpp +++ b/examples/sleepy_device/main/app_main.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #if CHIP_DEVICE_CONFIG_ENABLE_THREAD #include @@ -142,13 +143,11 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); + endpoint::on_off_light::config_t endpoint_config; endpoint_t *app_endpoint = endpoint::on_off_light::create(node, &endpoint_config, ENDPOINT_FLAG_NONE, NULL); - - /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ - if (!node || !app_endpoint) { - ESP_LOGE(TAG, "Matter node creation failed"); - } + ABORT_APP_ON_FAILURE(app_endpoint != nullptr, ESP_LOGE(TAG, "Failed to create on off light endpoint")); #if CHIP_DEVICE_CONFIG_ENABLE_THREAD /* Set OpenThread platform config */ @@ -162,7 +161,5 @@ extern "C" void app_main() /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); } diff --git a/examples/zap_light/main/CMakeLists.txt b/examples/zap_light/main/CMakeLists.txt index 50cb2f7c0..5ad541895 100644 --- a/examples/zap_light/main/CMakeLists.txt +++ b/examples/zap_light/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS ".") + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") # We must set CHIP_ROOT to include esp32_codegen.cmake get_filename_component(CHIP_ROOT "${MATTER_SDK_PATH}" REALPATH) diff --git a/examples/zap_light/main/app_main.cpp b/examples/zap_light/main/app_main.cpp index 3bdcd7361..45eb56e4a 100644 --- a/examples/zap_light/main/app_main.cpp +++ b/examples/zap_light/main/app_main.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #if CHIP_DEVICE_CONFIG_ENABLE_THREAD @@ -107,9 +108,7 @@ extern "C" void app_main() /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); /* Starting driver with default values */ app_driver_light_set_defaults(light_endpoint_id); diff --git a/examples/zigbee_bridge/main/CMakeLists.txt b/examples/zigbee_bridge/main/CMakeLists.txt index f48a36750..43626a795 100644 --- a/examples/zigbee_bridge/main/CMakeLists.txt +++ b/examples/zigbee_bridge/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS ".") + PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils") set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/zigbee_bridge/main/app_main.cpp b/examples/zigbee_bridge/main/app_main.cpp index cdb5fc747..de45e04c7 100644 --- a/examples/zigbee_bridge/main/app_main.cpp +++ b/examples/zigbee_bridge/main/app_main.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -124,30 +125,20 @@ extern "C" void app_main() /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; node_t *node = node::create(&node_config, app_attribute_update_cb, NULL); - - /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ - if (!node) { - ESP_LOGE(TAG, "Matter node creation failed"); - } + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); aggregator::config_t aggregator_config; endpoint_t *aggregator = endpoint::aggregator::create(node, &aggregator_config, ENDPOINT_FLAG_NONE, NULL); - if (!aggregator) { - ESP_LOGE(TAG, "Matter aggregator endpoint creation failed"); - } else { - aggregator_endpoint_id = endpoint::get_id(aggregator); - } + ABORT_APP_ON_FAILURE(aggregator != nullptr, ESP_LOGE(TAG, "Failed to create aggregator endpoint")); + + aggregator_endpoint_id = endpoint::get_id(aggregator); /* Matter start */ err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Matter start failed: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); err = app_bridge_initialize(node, create_bridge_devices); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err); - } + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err)); #if CONFIG_ENABLE_CHIP_SHELL esp_matter::console::diagnostics_register_commands();