Skip to content

Commit

Permalink
[ESP32] Added some OTA events for the device (project-chip#21953)
Browse files Browse the repository at this point in the history
* [ESP32] Added some OTA events for the device

* Addressed review comments

* fix the cut pasted log prints
  • Loading branch information
shubhamdp authored and isiu-apple committed Sep 16, 2022
1 parent 6f08c23 commit 2a447e7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
36 changes: 36 additions & 0 deletions examples/ota-requestor-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@ DeviceLayer::ESP32DeviceInfoProvider gExampleDeviceInfoProvider;
#else
DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
#endif // CONFIG_ENABLE_ESP32_DEVICE_INFO_PROVIDER

void OTAEventsHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
{
if (event->Type == DeviceLayer::DeviceEventType::kOtaStateChanged)
{
switch (event->OtaStateChanged.newState)
{
case DeviceLayer::kOtaDownloadInProgress:
ChipLogProgress(DeviceLayer, "OTA image download in progress");
break;
case DeviceLayer::kOtaDownloadComplete:
ChipLogProgress(DeviceLayer, "OTA image download complete");
break;
case DeviceLayer::kOtaDownloadFailed:
ChipLogProgress(DeviceLayer, "OTA image download failed");
break;
case DeviceLayer::kOtaDownloadAborted:
ChipLogProgress(DeviceLayer, "OTA image download aborted");
break;
case DeviceLayer::kOtaApplyInProgress:
ChipLogProgress(DeviceLayer, "OTA image apply in progress");
break;
case DeviceLayer::kOtaApplyComplete:
ChipLogProgress(DeviceLayer, "OTA image apply complete");
break;
case DeviceLayer::kOtaApplyFailed:
ChipLogProgress(DeviceLayer, "OTA image apply failed");
break;
default:
break;
}
}
}

} // namespace

extern "C" void app_main()
Expand Down Expand Up @@ -136,4 +170,6 @@ extern "C" void app_main()
#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER

chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast<intptr_t>(nullptr));

chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(OTAEventsHandler, reinterpret_cast<intptr_t>(nullptr));
}
28 changes: 28 additions & 0 deletions src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,34 @@ enum ActivityChange
enum OtaState
{
kOtaSpaceAvailable = 0,
/**
* This state indicates that Node is currently downloading a software update.
*/
kOtaDownloadInProgress,
/**
* This state indicates that Node has successfully downloaded a software update.
*/
kOtaDownloadComplete,
/**
* This state indicates that Node has failed to download a software update.
*/
kOtaDownloadFailed,
/**
* This state indicates that Node has aborted the download of a software update.
*/
kOtaDownloadAborted,
/**
* This state indicate that Node is currently in the process of verifying and applying a software update.
*/
kOtaApplyInProgress,
/**
* This state indicates that Node has successfully applied a software update.
*/
kOtaApplyComplete,
/**
* This state indicates that Node has failed to apply a software update.
*/
kOtaApplyFailed,
};

inline ConnectivityChange GetConnectivityChange(bool prevState, bool newState)
Expand Down
35 changes: 30 additions & 5 deletions src/platform/ESP32/OTAImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <app/clusters/ota-requestor/OTADownloader.h>
#include <app/clusters/ota-requestor/OTARequestorInterface.h>
#include <platform/CHIPDeviceEvent.h>
#include <platform/ESP32/ESP32Utils.h>

#include "OTAImageProcessorImpl.h"
Expand All @@ -37,11 +38,25 @@ void HandleRestart(Layer * systemLayer, void * appState)
{
esp_restart();
}

void PostOTAStateChangeEvent(DeviceLayer::OtaState newState)
{
DeviceLayer::ChipDeviceEvent otaChange;
otaChange.Type = DeviceLayer::DeviceEventType::kOtaStateChanged;
otaChange.OtaStateChanged.newState = newState;
CHIP_ERROR error = DeviceLayer::PlatformMgr().PostEvent(&otaChange);

if (error != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Error while posting OtaChange event %" CHIP_ERROR_FORMAT, error.Format());
}
}

} // namespace

bool OTAImageProcessorImpl::IsFirstImageRun()
{
OTARequestorInterface * requestor = chip::GetRequestorInstance();
OTARequestorInterface * requestor = GetRequestorInstance();
if (requestor == nullptr)
{
return false;
Expand All @@ -52,7 +67,7 @@ bool OTAImageProcessorImpl::IsFirstImageRun()

CHIP_ERROR OTAImageProcessorImpl::ConfirmCurrentImage()
{
OTARequestorInterface * requestor = chip::GetRequestorInstance();
OTARequestorInterface * requestor = GetRequestorInstance();
if (requestor == nullptr)
{
return CHIP_ERROR_INTERNAL;
Expand Down Expand Up @@ -132,6 +147,7 @@ void OTAImageProcessorImpl::HandlePrepareDownload(intptr_t context)
}
imageProcessor->mHeaderParser.Init();
imageProcessor->mDownloader->OnPreparedForDownload(CHIP_NO_ERROR);
PostOTAStateChangeEvent(DeviceLayer::kOtaDownloadInProgress);
}

void OTAImageProcessorImpl::HandleFinalize(intptr_t context)
Expand All @@ -153,10 +169,12 @@ void OTAImageProcessorImpl::HandleFinalize(intptr_t context)
{
ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
}
PostOTAStateChangeEvent(DeviceLayer::kOtaDownloadFailed);
return;
}
imageProcessor->ReleaseBlock();
ChipLogProgress(SoftwareUpdate, "OTA image downloaded to offset 0x%x", imageProcessor->mOTAUpdatePartition->address);
PostOTAStateChangeEvent(DeviceLayer::kOtaDownloadComplete);
}

void OTAImageProcessorImpl::HandleAbort(intptr_t context)
Expand All @@ -172,6 +190,7 @@ void OTAImageProcessorImpl::HandleAbort(intptr_t context)
ESP_LOGE(TAG, "ESP OTA abort failed");
}
imageProcessor->ReleaseBlock();
PostOTAStateChangeEvent(DeviceLayer::kOtaDownloadAborted);
}

void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context)
Expand All @@ -195,6 +214,7 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context)
{
ESP_LOGE(TAG, "Failed to process OTA image header");
imageProcessor->mDownloader->EndDownload(error);
PostOTAStateChangeEvent(DeviceLayer::kOtaDownloadFailed);
return;
}

Expand All @@ -203,6 +223,7 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context)
{
ESP_LOGE(TAG, "esp_ota_write failed (%s)", esp_err_to_name(err));
imageProcessor->mDownloader->EndDownload(CHIP_ERROR_WRITE_FAILED);
PostOTAStateChangeEvent(DeviceLayer::kOtaDownloadFailed);
return;
}
imageProcessor->mParams.downloadedBytes += block.size();
Expand All @@ -211,17 +232,21 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context)

void OTAImageProcessorImpl::HandleApply(intptr_t context)
{
PostOTAStateChangeEvent(DeviceLayer::kOtaApplyInProgress);
auto * imageProcessor = reinterpret_cast<OTAImageProcessorImpl *>(context);
esp_err_t err = esp_ota_set_boot_partition(imageProcessor->mOTAUpdatePartition);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
PostOTAStateChangeEvent(DeviceLayer::kOtaApplyFailed);
return;
}
ESP_LOGI(TAG, "Applying, Boot partition set offset:0x%x", imageProcessor->mOTAUpdatePartition->address);

PostOTAStateChangeEvent(DeviceLayer::kOtaApplyComplete);

// 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);
DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(2 * 1000), HandleRestart, nullptr);
}

CHIP_ERROR OTAImageProcessorImpl::SetBlock(ByteSpan & block)
Expand All @@ -237,7 +262,7 @@ CHIP_ERROR OTAImageProcessorImpl::SetBlock(ByteSpan & block)
{
ReleaseBlock();
}
uint8_t * mBlock_ptr = static_cast<uint8_t *>(chip::Platform::MemoryAlloc(block.size()));
uint8_t * mBlock_ptr = static_cast<uint8_t *>(Platform::MemoryAlloc(block.size()));
if (mBlock_ptr == nullptr)
{
return CHIP_ERROR_NO_MEMORY;
Expand All @@ -257,7 +282,7 @@ CHIP_ERROR OTAImageProcessorImpl::ReleaseBlock()
{
if (mBlock.data() != nullptr)
{
chip::Platform::MemoryFree(mBlock.data());
Platform::MemoryFree(mBlock.data());
}
mBlock = MutableByteSpan();
return CHIP_NO_ERROR;
Expand Down

0 comments on commit 2a447e7

Please sign in to comment.