diff --git a/components/esp_matter/esp_matter_core.cpp b/components/esp_matter/esp_matter_core.cpp index 1e56eba03..f6e4dd6f6 100644 --- a/components/esp_matter/esp_matter_core.cpp +++ b/components/esp_matter/esp_matter_core.cpp @@ -67,6 +67,7 @@ using chip::DeviceLayer::ThreadStackMgr; static const char *TAG = "esp_matter_core"; static bool esp_matter_started = false; +static chip::CommonCaseDeviceServerInitParams *s_server_init_params = nullptr; #ifndef CONFIG_ESP_MATTER_ENABLE_MATTER_SERVER // If Matter Server is disabled, these functions are required by InteractionModelEngine but not linked @@ -212,12 +213,21 @@ static void deinit_ble_if_commissioned(intptr_t unused) static void esp_matter_chip_init_task(intptr_t context) { TaskHandle_t task_to_notify = reinterpret_cast(context); - static chip::CommonCaseDeviceServerInitParams initParams; + static chip::CommonCaseDeviceServerInitParams defaultInitParams; + + chip::CommonCaseDeviceServerInitParams &initParams = + s_server_init_params ? *s_server_init_params : defaultInitParams; initParams.InitializeStaticResourcesBeforeServerInit(); - initParams.appDelegate = &s_app_delegate; - initParams.testEventTriggerDelegate = test_event_trigger::get_delegate(); - initParams.dataModelProvider = chip::app::CodegenDataModelProviderInstance(initParams.persistentStorageDelegate); + if (!initParams.appDelegate) { + initParams.appDelegate = &s_app_delegate; + } + if (!initParams.testEventTriggerDelegate) { + initParams.testEventTriggerDelegate = test_event_trigger::get_delegate(); + } + if (!initParams.dataModelProvider) { + initParams.dataModelProvider = chip::app::CodegenDataModelProviderInstance(initParams.persistentStorageDelegate); + } CHIP_ERROR ret = chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&s_fabric_delegate); if (ret != CHIP_NO_ERROR) @@ -359,6 +369,13 @@ bool is_started() return esp_matter_started; } +esp_err_t set_server_init_params(chip::CommonCaseDeviceServerInitParams *server_init_params) +{ + VerifyOrReturnError(!esp_matter_started, ESP_ERR_INVALID_STATE, ESP_LOGE(TAG, "esp_matter has started")); + s_server_init_params = server_init_params; + return ESP_OK; +} + esp_err_t start(event_callback_t callback, intptr_t callback_arg) { VerifyOrReturnError(!esp_matter_started, ESP_ERR_INVALID_STATE, ESP_LOGE(TAG, "esp_matter has started")); diff --git a/components/esp_matter/esp_matter_core.h b/components/esp_matter/esp_matter_core.h index ec03d7b39..490a17a62 100644 --- a/components/esp_matter/esp_matter_core.h +++ b/components/esp_matter/esp_matter_core.h @@ -23,6 +23,7 @@ #include #include #include +#include using chip::app::ConcreteCommandPath; using chip::DeviceLayer::ChipDeviceEvent; @@ -45,6 +46,30 @@ typedef void (*event_callback_t)(const ChipDeviceEvent *event, intptr_t arg); */ bool is_started(); +/** + * @brief Set the server initialization parameters for Matter. + * + * This function must be called before Matter is started. `esp_matter::start()` + * Calling it after Matter has started will have no effect and will return an error. + * + * @note Starting Matter without valid initialization parameters may lead to undefined behavior or startup failure. + * + * @note The provided pointer is stored internally and used later during Matter initialization. + * It is not copied, so the caller must ensure that the memory pointed to by `server_init_params` + * remains valid and unmodified until Matter is started. + * + * @note If this function is called with a `nullptr`, it will still return ESP_OK. The system + * will proceed with default initialization behavior. + * + * @param[in] server_init_params Pointer to the server initialization parameters. May be nullptr. + * + * @return ESP_OK Successfully set the server initialization parameters. + * @return ESP_ERR_INVALID_STATE If called after Matter has already started. + * @return error in case of failure. + */ + +esp_err_t set_server_init_params(chip::CommonCaseDeviceServerInitParams *server_init_params); + /** ESP Matter Start * * Initialize and start the matter thread.