From abf852b52ac9fd9ff58e803afda17b4f8664c0e5 Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Sun, 16 Jan 2022 19:57:43 +0530 Subject: [PATCH] [ESP32] Schedule the restart once OTA image apply is successful Also, Apply suggestion from #13484 in lighting-app --- examples/lighting-app/esp32/README.md | 17 ++++++++++++----- examples/lighting-app/esp32/main/main.cpp | 4 +--- examples/ota-requestor-app/esp32/README.md | 5 +++-- src/platform/ESP32/OTAImageProcessorImpl.cpp | 11 +++++++++++ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/examples/lighting-app/esp32/README.md b/examples/lighting-app/esp32/README.md index 534e6ec3df0c16..193559246e9640 100644 --- a/examples/lighting-app/esp32/README.md +++ b/examples/lighting-app/esp32/README.md @@ -142,10 +142,10 @@ scripts/examples/gn_build_example.sh examples/ota-provider-app/linux out/debug c hello-world.bin can be obtained from compiling the hello-world ESP-IDF example. -- Provision the Linux OTA Provider using chip-tool +- Commission the Linux OTA Provider using chip-tool ``` -./out/debug/chip-tool pairing onnetwork 12345 20202021 +./out/debug/chip-tool pairing onnetwork 12346 20202021 ``` ## Query for an OTA Image @@ -154,10 +154,17 @@ After commissioning is successful, press Enter in requestor device console and type below query. ``` ->matter ota query 1 12345 0 +>matter ota query 1 12346 0 +``` + +Alternate way to query an OTA image using chip-tool + +``` +./out/debug/chip-tool otasoftwareupdaterequestor announce-ota-provider 12346 0 0 0 12345 0 ``` ## Apply update -Once transfer is complete, reboot the device manually to boot from upgraded OTA -image. +Once the transfer is complete OTA requestor sends ApplyUpdateRequest command to +OTA provider for applying the image. Device will restart on successful +application of OTA image. diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index b26a2fa6afd2e6..c3114e04c2b363 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -57,12 +57,10 @@ static void InitOTARequestor(void) { #if CONFIG_ENABLE_OTA_REQUESTOR SetRequestorInstance(&gRequestorCore); - gRequestorCore.SetServerInstance(&Server::GetInstance()); - gRequestorCore.SetOtaRequestorDriver(&gRequestorUser); + gRequestorCore.Init(&Server::GetInstance(), &gRequestorUser, &gDownloader); gImageProcessor.SetOTADownloader(&gDownloader); gDownloader.SetImageProcessorDelegate(&gImageProcessor); gRequestorUser.Init(&gRequestorCore, &gImageProcessor); - gRequestorCore.SetBDXDownloader(&gDownloader); #endif } diff --git a/examples/ota-requestor-app/esp32/README.md b/examples/ota-requestor-app/esp32/README.md index 20534f43393f8c..3b5ba687b56bc9 100644 --- a/examples/ota-requestor-app/esp32/README.md +++ b/examples/ota-requestor-app/esp32/README.md @@ -45,8 +45,9 @@ chip-tool. On receiving this command OTA requestor will query for OTA image. ## Apply update -Once transfer is complete, reboot the device manually to boot from upgraded OTA -image. +Once the transfer is complete OTA requestor sends ApplyUpdateRequest command to +OTA provider for applying the image. Device will restart on successful +application of OTA image. ## ESP32 OTA Requestor with Linux OTA Provider diff --git a/src/platform/ESP32/OTAImageProcessorImpl.cpp b/src/platform/ESP32/OTAImageProcessorImpl.cpp index 67d873b2abc569..c539b89c073932 100644 --- a/src/platform/ESP32/OTAImageProcessorImpl.cpp +++ b/src/platform/ESP32/OTAImageProcessorImpl.cpp @@ -26,9 +26,17 @@ #include "lib/core/CHIPError.h" #define TAG "OTAImageProcessor" +using namespace chip::System; using namespace ::chip::DeviceLayer::Internal; namespace chip { +namespace { + +void HandleRestart(Layer * systemLayer, void * appState) +{ + esp_restart(); +} +} // namespace CHIP_ERROR OTAImageProcessorImpl::PrepareDownload() { @@ -168,6 +176,9 @@ void OTAImageProcessorImpl::HandleApply(intptr_t context) return; } ESP_LOGI(TAG, "Applying, Boot partition set offset:0x%x", imageProcessor->mOTAUpdatePartition->address); + + // HandleApply is called after delayed action time seconds are elapsed, so it would be safe to schedule the restart + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(2 * 1000), HandleRestart, nullptr); } CHIP_ERROR OTAImageProcessorImpl::SetBlock(ByteSpan & block)