diff --git a/examples/light/main/app_matter.cpp b/examples/light/main/app_matter.cpp index cca81cead..b3500da7e 100644 --- a/examples/light/main/app_matter.cpp +++ b/examples/light/main/app_matter.cpp @@ -22,11 +22,12 @@ #include "app/server/Server.h" #include "app/util/af.h" #include "app/util/basic-types.h" -#include "platform/CHIPDeviceLayer.h" #include "core/CHIPError.h" +#include "freertos/FreeRTOS.h" #include "lib/shell/Engine.h" #include "lib/support/CHIPMem.h" -#include +#include "platform/CHIPDeviceLayer.h" +#include "platform/PlatformManager.h" using chip::AttributeId; using chip::ClusterId; @@ -236,6 +237,16 @@ static void on_device_event(const ChipDeviceEvent *event, intptr_t arg) ESP_LOGI(TAG, "Current free heap: %zu", heap_caps_get_free_size(MALLOC_CAP_8BIT)); } +static void matter_init_task(intptr_t context) +{ + xTaskHandle task_to_notify = reinterpret_cast(context); + chip::Server::GetInstance().Init(); +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + chip::app::MdnsServer::Instance().StartServer(); +#endif + xTaskNotifyGive(task_to_notify); +} + esp_err_t app_matter_init() { if (PlatformMgr().InitChipStack() != CHIP_NO_ERROR) { @@ -264,10 +275,9 @@ esp_err_t app_matter_init() return ESP_FAIL; } #endif - chip::Server::GetInstance().Init(); -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - chip::app::MdnsServer::Instance().StartServer(); -#endif + PlatformMgr().ScheduleWork(matter_init_task, reinterpret_cast(xTaskGetCurrentTaskHandle())); + // Wait for the matter stack to be initialized + xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); esp_matter_attribute_callback_add(APP_MATTER_NAME, app_matter_attribute_update, NULL); return ESP_OK; diff --git a/examples/light/sdkconfig.defaults b/examples/light/sdkconfig.defaults index 23c2c7d7a..522a6fa78 100644 --- a/examples/light/sdkconfig.defaults +++ b/examples/light/sdkconfig.defaults @@ -19,7 +19,3 @@ CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" # Enable chip shell CONFIG_ENABLE_CHIP_SHELL=y - -# Main task needs a bit more stack than the default -# default is 3584, bump this up to 5k. -CONFIG_ESP_MAIN_TASK_STACK_SIZE=5120 diff --git a/examples/rainmaker_light/main/app_matter.cpp b/examples/rainmaker_light/main/app_matter.cpp index 950fa2015..46dc76241 100644 --- a/examples/rainmaker_light/main/app_matter.cpp +++ b/examples/rainmaker_light/main/app_matter.cpp @@ -6,10 +6,10 @@ CONDITIONS OF ANY KIND, either express or implied. */ -#include "esp_matter.h" -#include "esp_matter_standard.h" #include "app_matter.h" #include "app_constants.h" +#include "esp_matter.h" +#include "esp_matter_standard.h" #include "esp_heap_caps.h" #include "esp_log.h" @@ -22,19 +22,18 @@ #include "app/server/Server.h" #include "app/util/af.h" #include "app/util/basic-types.h" -#include "platform/CHIPDeviceLayer.h" #include "core/CHIPError.h" #include "lib/shell/Engine.h" #include "lib/support/CHIPMem.h" -#include +#include "platform/CHIPDeviceLayer.h" using chip::AttributeId; using chip::ClusterId; using chip::EndpointId; using chip::DeviceLayer::ChipDeviceEvent; using chip::DeviceLayer::ConnectivityMgr; -using chip::DeviceLayer::DeviceEventType::PublicEventTypes; using chip::DeviceLayer::PlatformMgr; +using chip::DeviceLayer::DeviceEventType::PublicEventTypes; typedef enum { REMAP_MATTER_TO_STANDARD, @@ -186,7 +185,8 @@ static esp_matter_attr_val_t app_matter_get_attribute_val(char *attribute, int v return esp_matter_int(value); } -static esp_err_t app_matter_attribute_update(const char *endpoint, const char *attribute, esp_matter_attr_val_t val, void *priv_data) +static esp_err_t app_matter_attribute_update(const char *endpoint, const char *attribute, esp_matter_attr_val_t val, + void *priv_data) { EndpointId endpoint_id = app_matter_get_endpoint_id(endpoint); ClusterId cluster_id = app_matter_get_cluster_id(attribute); @@ -196,7 +196,8 @@ static esp_err_t app_matter_attribute_update(const char *endpoint, const char *a uint8_t value_remap = (uint8_t)app_matter_remap((char *)attribute, value, REMAP_STANDARD_TO_MATTER); ESP_LOGD(TAG, "Changing %s from standard: %d, to matter: %d\n", attribute, value, value_remap); - EmberAfStatus status = emberAfWriteAttribute(endpoint_id, cluster_id, attribute_id, CLUSTER_MASK_SERVER, (uint8_t *)&value_remap, attribute_type); + EmberAfStatus status = emberAfWriteAttribute(endpoint_id, cluster_id, attribute_id, CLUSTER_MASK_SERVER, + (uint8_t *)&value_remap, attribute_type); if (status != EMBER_ZCL_STATUS_SUCCESS) { ESP_LOGE(TAG, "Error updating attribute to matter"); return ESP_FAIL; @@ -204,7 +205,8 @@ static esp_err_t app_matter_attribute_update(const char *endpoint, const char *a return ESP_OK; } -void emberAfPostAttributeChangeCallback(EndpointId endpoint, ClusterId cluster, AttributeId attribute, uint8_t mask, uint16_t manufacturer, uint8_t type, uint16_t size, uint8_t *value) +void emberAfPostAttributeChangeCallback(EndpointId endpoint, ClusterId cluster, AttributeId attribute, uint8_t mask, + uint16_t manufacturer, uint8_t type, uint16_t size, uint8_t *value) { char *endpoint_name = (char *)app_matter_get_endpoint_name(endpoint); char *attribute_name = (char *)app_matter_get_attribute_name(cluster, attribute); @@ -233,6 +235,13 @@ static void on_device_event(const ChipDeviceEvent *event, intptr_t arg) ESP_LOGI(TAG, "Current free heap: %zu", heap_caps_get_free_size(MALLOC_CAP_8BIT)); } +static void matter_init_task(intptr_t context) +{ + xTaskHandle task_to_notify = reinterpret_cast(context); + chip::Server::GetInstance().Init(); + xTaskNotifyGive(task_to_notify); +} + esp_err_t app_matter_init() { if (PlatformMgr().InitChipStack() != CHIP_NO_ERROR) { @@ -251,7 +260,9 @@ esp_err_t app_matter_init() } PlatformMgr().AddEventHandler(on_device_event, static_cast(NULL)); - chip::Server::GetInstance().Init(); + PlatformMgr().ScheduleWork(matter_init_task, reinterpret_cast(xTaskGetCurrentTaskHandle())); + // Wait for the matter stack to be initialized + xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); esp_matter_attribute_callback_add(APP_MATTER_NAME, app_matter_attribute_update, NULL); return ESP_OK; diff --git a/examples/rainmaker_light/sdkconfig.defaults b/examples/rainmaker_light/sdkconfig.defaults index 3c7753d7b..59362f807 100644 --- a/examples/rainmaker_light/sdkconfig.defaults +++ b/examples/rainmaker_light/sdkconfig.defaults @@ -29,7 +29,3 @@ CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA=y # Temporary Fix for Timer Overflows CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=3120 - -# Main task needs a bit more stack than the default -# default is 3584, bump this up to 5k. -CONFIG_ESP_MAIN_TASK_STACK_SIZE=5120