From 06884fb733f6221f70b91f446ffc592115a8e36e Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Wed, 16 Feb 2022 05:01:04 +0530 Subject: [PATCH] [ESP32]: Fix initialization order of ota-requestor and ota-provider apps (#15179) Also, fixed the wrong API being called in ESP32 OTA image processor implementation --- examples/ota-provider-app/esp32/main/main.cpp | 120 +++++++++--------- .../ota-requestor-app/esp32/main/main.cpp | 12 +- src/platform/ESP32/OTAImageProcessorImpl.cpp | 2 +- 3 files changed, 67 insertions(+), 67 deletions(-) diff --git a/examples/ota-provider-app/esp32/main/main.cpp b/examples/ota-provider-app/esp32/main/main.cpp index 6d836cfa0b709d..546df355fa285c 100644 --- a/examples/ota-provider-app/esp32/main/main.cpp +++ b/examples/ota-provider-app/esp32/main/main.cpp @@ -75,6 +75,66 @@ static void InitServer(intptr_t context) SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); sWiFiNetworkCommissioningInstance.Init(); + + BdxOtaSender * bdxOtaSender = otaProvider.GetBdxOtaSender(); + VerifyOrReturn(bdxOtaSender != nullptr, ESP_LOGE(TAG, "bdxOtaSender is nullptr")); + + // Register handler to handle bdx messages + CHIP_ERROR error = chip::Server::GetInstance().GetExchangeManager().RegisterUnsolicitedMessageHandlerForProtocol( + chip::Protocols::BDX::Id, bdxOtaSender); + if (error != CHIP_NO_ERROR) + { + ESP_LOGE(TAG, "RegisterUnsolicitedMessageHandler failed: %s", chip::ErrorStr(error)); + return; + } + + BdxOtaSenderCallbacks callbacks; + callbacks.onBlockQuery = &onBlockQueryCallback; + callbacks.onTransferComplete = &onTransferCompleteCallback; + callbacks.onTransferFailed = &onTransferFailedCallback; + bdxOtaSender->SetCallbacks(callbacks); + + esp_vfs_spiffs_conf_t spiffs_conf = { + .base_path = "/spiffs", + .partition_label = NULL, + .max_files = 3, + .format_if_mount_failed = false, + }; + + esp_err_t err = esp_vfs_spiffs_register(&spiffs_conf); + if (err != ESP_OK) + { + ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(err)); + return; + } + size_t total = 0, used = 0; + err = esp_spiffs_info(NULL, &total, &used); + ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); + char otaImagePath[kMaxImagePathlen]; + sprintf(otaImagePath, "/spiffs/%s", otaFilename); + otaImageFile = fopen(otaImagePath, "r"); + if (otaImageFile == NULL) + { + ESP_LOGE(TAG, "Failed to open %s", otaFilename); + return; + } + fseek(otaImageFile, 0, SEEK_END); + otaImageLen = ftell(otaImageFile); + rewind(otaImageFile); + ESP_LOGI(TAG, "The OTA image size: %d", otaImageLen); + if (otaImageLen > 0) + { + otaProvider.SetQueryImageBehavior(OTAProviderExample::kRespondWithUpdateAvailable); + otaProvider.SetOTAFilePath(otaFilename); + } + + chip::app::Clusters::OTAProvider::SetDelegate(kOtaProviderEndpoint, &otaProvider); + + // Launch a chip shell and register OTA Provider Commands + chip::LaunchShell(); + OTAProviderCommands & otaProviderCommands = OTAProviderCommands::GetInstance(); + otaProviderCommands.SetExampleOTAProvider(&otaProvider); + otaProviderCommands.Register(); } } // namespace @@ -153,64 +213,4 @@ extern "C" void app_main() } chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast(nullptr)); - - BdxOtaSender * bdxOtaSender = otaProvider.GetBdxOtaSender(); - VerifyOrReturn(bdxOtaSender != nullptr, ESP_LOGE(TAG, "bdxOtaSender is nullptr")); - - // Register handler to handle bdx messages - error = chip::Server::GetInstance().GetExchangeManager().RegisterUnsolicitedMessageHandlerForProtocol(chip::Protocols::BDX::Id, - bdxOtaSender); - if (error != CHIP_NO_ERROR) - { - ESP_LOGE(TAG, "RegisterUnsolicitedMessageHandler failed: %s", chip::ErrorStr(error)); - return; - } - - BdxOtaSenderCallbacks callbacks; - callbacks.onBlockQuery = &onBlockQueryCallback; - callbacks.onTransferComplete = &onTransferCompleteCallback; - callbacks.onTransferFailed = &onTransferFailedCallback; - bdxOtaSender->SetCallbacks(callbacks); - - esp_vfs_spiffs_conf_t spiffs_conf = { - .base_path = "/spiffs", - .partition_label = NULL, - .max_files = 3, - .format_if_mount_failed = false, - }; - - err = esp_vfs_spiffs_register(&spiffs_conf); - if (err != ESP_OK) - { - ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(err)); - return; - } - size_t total = 0, used = 0; - err = esp_spiffs_info(NULL, &total, &used); - ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); - char otaImagePath[kMaxImagePathlen]; - sprintf(otaImagePath, "/spiffs/%s", otaFilename); - otaImageFile = fopen(otaImagePath, "r"); - if (otaImageFile == NULL) - { - ESP_LOGE(TAG, "Failed to open %s", otaFilename); - return; - } - fseek(otaImageFile, 0, SEEK_END); - otaImageLen = ftell(otaImageFile); - rewind(otaImageFile); - ESP_LOGI(TAG, "The OTA image size: %d", otaImageLen); - if (otaImageLen > 0) - { - otaProvider.SetQueryImageBehavior(OTAProviderExample::kRespondWithUpdateAvailable); - otaProvider.SetOTAFilePath(otaFilename); - } - - chip::app::Clusters::OTAProvider::SetDelegate(kOtaProviderEndpoint, &otaProvider); - - // Launch a chip shell and register OTA Provider Commands - chip::LaunchShell(); - OTAProviderCommands & otaProviderCommands = OTAProviderCommands::GetInstance(); - otaProviderCommands.SetExampleOTAProvider(&otaProvider); - otaProviderCommands.Register(); } diff --git a/examples/ota-requestor-app/esp32/main/main.cpp b/examples/ota-requestor-app/esp32/main/main.cpp index 06a791874bbb3f..4b81bf8ee34352 100644 --- a/examples/ota-requestor-app/esp32/main/main.cpp +++ b/examples/ota-requestor-app/esp32/main/main.cpp @@ -69,6 +69,12 @@ static void InitServer(intptr_t context) SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); sWiFiNetworkCommissioningInstance.Init(); + + SetRequestorInstance(&gRequestorCore); + gRequestorCore.Init(&(Server::GetInstance()), &gRequestorUser, &gDownloader); + gImageProcessor.SetOTADownloader(&gDownloader); + gDownloader.SetImageProcessorDelegate(&gImageProcessor); + gRequestorUser.Init(&gRequestorCore, &gImageProcessor); } } // namespace @@ -106,10 +112,4 @@ extern "C" void app_main() } chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast(nullptr)); - - SetRequestorInstance(&gRequestorCore); - gRequestorCore.Init(&(Server::GetInstance()), &gRequestorUser, &gDownloader); - gImageProcessor.SetOTADownloader(&gDownloader); - gDownloader.SetImageProcessorDelegate(&gImageProcessor); - gRequestorUser.Init(&gRequestorCore, &gImageProcessor); } diff --git a/src/platform/ESP32/OTAImageProcessorImpl.cpp b/src/platform/ESP32/OTAImageProcessorImpl.cpp index 41d18eb4038ab5..6f8d5d07dda0d3 100644 --- a/src/platform/ESP32/OTAImageProcessorImpl.cpp +++ b/src/platform/ESP32/OTAImageProcessorImpl.cpp @@ -160,7 +160,7 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context) ByteSpan block = ByteSpan(imageProcessor->mBlock.data(), imageProcessor->mBlock.size()); - CHIP_ERROR error = imageProcessor->ProcessBlock(block); + CHIP_ERROR error = imageProcessor->ProcessHeader(block); if (error != CHIP_NO_ERROR) { ESP_LOGE(TAG, "Failed to process OTA image header");