diff --git a/components/esp_matter/esp_matter_ota.cpp b/components/esp_matter/esp_matter_ota.cpp index 08e026774..e549717c6 100644 --- a/components/esp_matter/esp_matter_ota.cpp +++ b/components/esp_matter/esp_matter_ota.cpp @@ -17,7 +17,7 @@ #include #include -#include +#include #include #include @@ -30,7 +30,7 @@ using chip::DefaultOTARequestor; using chip::DefaultOTARequestorStorage; using chip::OTAImageProcessorImpl; using chip::Server; -using chip::DeviceLayer::DefaultOTARequestorDriver; +using chip::DeviceLayer::ExtendedOTARequestorDriver; using namespace esp_matter; using namespace esp_matter::endpoint; @@ -39,10 +39,15 @@ using namespace esp_matter::cluster; #if CONFIG_ENABLE_OTA_REQUESTOR DefaultOTARequestor gRequestorCore; DefaultOTARequestorStorage gRequestorStorage; -DefaultOTARequestorDriver gRequestorUser; +ExtendedOTARequestorDriver gRequestorUser; BDXDownloader gDownloader; OTAImageProcessorImpl gImageProcessor; -#endif + +static esp_matter_ota_requestor_impl_t s_ota_requestor_impl = { + .driver = &gRequestorUser, + .image_processor = &gImageProcessor, +}; +#endif // CONFIG_ENABLE_OTA_REQUESTOR esp_err_t esp_matter_ota_requestor_init(void) { @@ -61,16 +66,42 @@ esp_err_t esp_matter_ota_requestor_init(void) #endif } +static esp_err_t esp_matter_ota_override_impl(const esp_matter_ota_requestor_impl_t *impl) +{ +#if CONFIG_ENABLE_OTA_REQUESTOR + VerifyOrReturnError(impl != nullptr, ESP_ERR_INVALID_ARG); + + if (impl->driver != nullptr) { + s_ota_requestor_impl.driver = impl->driver; + } + if (impl->image_processor != nullptr) { + s_ota_requestor_impl.image_processor = impl->image_processor; + } + + s_ota_requestor_impl.user_consent = impl->user_consent; + + return ESP_OK; +#else + return ESP_ERR_NOT_SUPPORTED; +#endif // CONFIG_ENABLE_OTA_REQUESTOR +} + void esp_matter_ota_requestor_start(void) { #if CONFIG_ENABLE_OTA_REQUESTOR VerifyOrReturn(chip::GetRequestorInstance() == nullptr); + chip::SetRequestorInstance(&gRequestorCore); gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); - gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); - gImageProcessor.SetOTADownloader(&gDownloader); - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); + + gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, *s_ota_requestor_impl.driver, gDownloader); + + s_ota_requestor_impl.image_processor->SetOTADownloader(&gDownloader); + + gDownloader.SetImageProcessorDelegate(s_ota_requestor_impl.image_processor); + + s_ota_requestor_impl.driver->SetUserConsentDelegate(s_ota_requestor_impl.user_consent); + s_ota_requestor_impl.driver->Init(&gRequestorCore, s_ota_requestor_impl.image_processor); #endif } @@ -93,6 +124,9 @@ esp_err_t esp_matter_ota_requestor_set_config(const esp_matter_ota_config_t & co if (config.watchdog_timeout) { gRequestorUser.SetWatchdogTimeout(config.watchdog_timeout); } + if (config.impl != nullptr) { + esp_matter_ota_override_impl(config.impl); + } return ESP_OK; #else diff --git a/components/esp_matter/esp_matter_ota.h b/components/esp_matter/esp_matter_ota.h index 9a03b1ba8..4a42cd65f 100644 --- a/components/esp_matter/esp_matter_ota.h +++ b/components/esp_matter/esp_matter_ota.h @@ -17,6 +17,19 @@ #include "esp_err.h" #include "sdkconfig.h" +#include +#include +#include + +typedef struct { + // ota requestor driver + chip::DeviceLayer::ExtendedOTARequestorDriver *driver = nullptr; + // user consent + chip::ota::OTARequestorUserConsentDelegate *user_consent = nullptr; + // ota image processor + chip::OTAImageProcessorImpl *image_processor = nullptr; +} esp_matter_ota_requestor_impl_t; + typedef struct { /** * Timeout (in seconds) for querying default OTA Providers @@ -29,16 +42,23 @@ typedef struct { * Default timeout is 300 seconds */ uint32_t watchdog_timeout; + /** + * Options to override the default behavior of the OTA requestor + * Refer to esp_matter_ota_requestor_impl_t for more details. + * If not set, default implementation is used. + */ + const esp_matter_ota_requestor_impl_t *impl = nullptr; } esp_matter_ota_config_t; - -/** Initialize the Matter OTA Requestor +/** + * Initialize the Matter OTA Requestor * + * Adds the OTA requestor server cluster and ota provider client cluster to root node endpoint. */ esp_err_t esp_matter_ota_requestor_init(void); -/**Start the Matter OTA Requestor - * +/** + * Start the Matter OTA Requestor */ void esp_matter_ota_requestor_start(void); @@ -46,7 +66,7 @@ void esp_matter_ota_requestor_start(void); * @brief This API initializes the handling of encrypted OTA image * * @param[in] key null terminated RSA-3072 key in PEM format. - * The key buffer must remain allocated and should not be freed. + * The key buffer must remain allocated and should not be freed. * @param[in] size Size of the key, size shall contain the null terminator as well. * If using `strlen` then please consider adding +1 to the size. *