Skip to content

Commit

Permalink
Update Mbed OTA requestor app - implement common OTARequestor and BDX…
Browse files Browse the repository at this point in the history
…Downloader

Fix Mbed shell app build
  • Loading branch information
ATmobica committed Dec 16, 2021
1 parent 712b44f commit d9fe34e
Show file tree
Hide file tree
Showing 19 changed files with 423 additions and 909 deletions.
5 changes: 5 additions & 0 deletions config/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,17 @@ if (CONFIG_CHIP_OTA_REQUESTOR)
${CHIP_ROOT}/zzz_generated/ota-requestor-app
${CHIP_ROOT}/src/app/clusters/ota-requestor
${CHIP_ROOT}/src/platform/mbed
${CHIP_ROOT}/src/include/platform
)

target_sources(${APP_TARGET} PRIVATE
${CHIP_ROOT}/zzz_generated/ota-requestor-app/zap-generated/callback-stub.cpp
${CHIP_ROOT}/zzz_generated/ota-requestor-app/zap-generated/CHIPClusters.cpp
${CHIP_ROOT}/zzz_generated/ota-requestor-app/zap-generated/IMClusterCommandHandler.cpp

${CHIP_ROOT}/src/app/clusters/ota-requestor/OTARequestor.cpp
${CHIP_ROOT}/src/app/clusters/ota-requestor/BDXDownloader.cpp
${CHIP_ROOT}/src/app/clusters/ota-requestor/ClusterInterface.cpp
)

list(APPEND CHIP_DEFINES
Expand Down
5 changes: 5 additions & 0 deletions config/mbed/chip-gn/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ group("mbed") {
if (chip_build_pw_rpc_lib) {
deps += [ "//lib/pw_rpc" ]
}

if (chip_enable_ota_requestor) {
deps += [ "${chip_root}/examples/ota-requestor-app/ota-requestor-common" ]

}
}

group("default") {
Expand Down
2 changes: 1 addition & 1 deletion examples/ota-requestor-app/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ target_sources(${APP_TARGET} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/main/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/main/ZclCallbacks.cpp
${CMAKE_CURRENT_SOURCE_DIR}/main/AppTask.cpp
${CMAKE_CURRENT_SOURCE_DIR}/main/MbedOTARequestorDriver.cpp
${CMAKE_CURRENT_SOURCE_DIR}/main/OTARequestorDriverImpl.cpp
${MBED_COMMON}/util/LEDWidget.cpp
)

Expand Down
197 changes: 112 additions & 85 deletions examples/ota-requestor-app/mbed/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
#include "events/EventQueue.h"

#ifdef CHIP_OTA_REQUESTOR
#include "MbedOTARequestorDriver.h"
#include <MbedOTADownloader.h>
#include <MbedOTAImageProcessor.h>
#include <MbedOTARequestor.h>
#include "OTARequestorDriverImpl.h"
#include <BDXDownloader.h>
#include <OTAImageProcessorImpl.h>
#include <OTARequestor.h>
#endif // CHIP_OTA_REQUESTOR

static bool sIsWiFiStationProvisioned = false;
Expand Down Expand Up @@ -92,30 +92,6 @@ int AppTask::Init()
}
}

#ifdef CHIP_OTA_REQUESTOR
// Initialize and interconnect the Requestor and Image Processor objects -- START
// Initialize the instance of the main Requestor Class
MbedOTARequestor * requestorCore = new MbedOTARequestor(OnAnnounceProviderCallback, OnProviderResponseCallback);
SetRequestorInstance(requestorCore);

// Initialize an instance of the Requestor Driver
MbedOTARequestorDriver * requestorUser = new MbedOTARequestorDriver;

// Connect the Requestor and Requestor Driver objects
requestorCore->SetOtaRequestorDriver(requestorUser);

// Initialize the Downloader object
MbedOTADownloader * downloaderCore = new MbedOTADownloader(OnDownloadCompletedCallback);
SetDownloaderInstance(downloaderCore);

// Initialize the Image Processor object
MbedOTAImageProcessor * downloaderUser = new MbedOTAImageProcessor;

// Connect the Downloader and Image Processor objects
downloaderCore->SetImageProcessorDelegate(downloaderUser);
// Initialize and interconnect the Requestor and Image Processor objects -- END
#endif // CHIP_OTA_REQUESTOR

ConnectivityMgrImpl().StartWiFiManagement();

// Init ZCL Data Model and start server
Expand All @@ -132,6 +108,57 @@ int AppTask::Init()
// QR code will be used with CHIP Tool
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));

#ifdef CHIP_OTA_REQUESTOR
// Initialize the instance of the main Requestor Class
OTARequestor * requestor = new OTARequestor();
if (requestor == nullptr)
{
ChipLogError(NotSpecified, "Create OTA Requestor core failed");
return EXIT_FAILURE;
}
SetRequestorInstance(requestor);

Server * server = &(Server::GetInstance());
if (server == nullptr)
{
ChipLogError(NotSpecified, "Get server instance failed");
return EXIT_FAILURE;
}
requestor->SetServerInstance(server);

// Initialize an instance of the Requestor Driver
OTARequestorDriverImpl * requestorDriver = new OTARequestorDriverImpl;
if (requestorDriver == nullptr)
{
ChipLogError(NotSpecified, "Create OTA Requestor driver failed");
return EXIT_FAILURE;
}

// Connect the Requestor and Requestor Driver objects
requestor->SetOtaRequestorDriver(requestorDriver);

// Initialize the Downloader object
BDXDownloader * downloader = new BDXDownloader();
if (downloader == nullptr)
{
ChipLogError(NotSpecified, "Create OTA Downloader failed");
return EXIT_FAILURE;
}

// Initialize the Image Processor object
OTAImageProcessorImpl * imageProcessor = new OTAImageProcessorImpl;
if (imageProcessor == nullptr)
{
ChipLogError(NotSpecified, "Create OTA Image Processor failed");
return EXIT_FAILURE;
}

imageProcessor->SetOTADownloader(downloader);
downloader->SetImageProcessorDelegate(imageProcessor);

requestor->SetBDXDownloader(downloader);
#endif // CHIP_OTA_REQUESTOR

return 0;
}

Expand Down Expand Up @@ -216,60 +243,60 @@ void AppTask::DispatchEvent(const AppEvent * aEvent)
}
}

#ifdef CHIP_OTA_REQUESTOR
void AppTask::OnOtaEventHandler(AppEvent * aEvent)
{
switch (aEvent->Type)
{
case AppEvent::kEventType_ota_provider_announce: {
ChipLogProgress(NotSpecified, "OTA provider announce event");
MbedOTARequestor * requestor = static_cast<MbedOTARequestor *>(GetRequestorInstance());
requestor->ConnectProvider();
break;
}

case AppEvent::kEventType_ota_provider_response: {
ChipLogProgress(NotSpecified, "OTA provider response event");
MbedOTADownloader * downloader = static_cast<MbedOTADownloader *>(GetDownloaderInstance());
downloader->SetDownloadImageInfo(aEvent->OTAProviderResponseEvent.imageDatails->updateFileName);
downloader->BeginPrepareDownload();
break;
}
case AppEvent::kEventType_ota_download_completed:
ChipLogProgress(NotSpecified, "OTA download completed event");
ChipLogProgress(NotSpecified, "Download %.*s image size %ukB",
static_cast<int>(aEvent->OTADownloadCompletedEvent.imageInfo->imageName.size()),
aEvent->OTADownloadCompletedEvent.imageInfo->imageName.data(),
(static_cast<unsigned>(aEvent->OTADownloadCompletedEvent.imageInfo->imageSize) / 1024u));
break;
default:
ChipLogError(NotSpecified, "OTA event unknown");
}
}

void AppTask::OnAnnounceProviderCallback()
{
AppEvent ota_announce_provider_event;
ota_announce_provider_event.Type = AppEvent::kEventType_ota_provider_announce;
ota_announce_provider_event.Handler = OnOtaEventHandler;
sAppTask.PostEvent(&ota_announce_provider_event);
}

void AppTask::OnProviderResponseCallback(MbedOTARequestor::OTAUpdateDetails * updateDetails)
{
AppEvent ota_provider_response_event;
ota_provider_response_event.Type = AppEvent::kEventType_ota_provider_response;
ota_provider_response_event.Handler = OnOtaEventHandler;
ota_provider_response_event.OTAProviderResponseEvent.imageDatails = updateDetails;
sAppTask.PostEvent(&ota_provider_response_event);
}

void AppTask::OnDownloadCompletedCallback(MbedOTADownloader::ImageInfo * imageInfo)
{
AppEvent ota_download_completed_event;
ota_download_completed_event.Type = AppEvent::kEventType_ota_download_completed;
ota_download_completed_event.Handler = OnOtaEventHandler;
ota_download_completed_event.OTADownloadCompletedEvent.imageInfo = imageInfo;
sAppTask.PostEvent(&ota_download_completed_event);
}
#endif
// #ifdef CHIP_OTA_REQUESTOR
// void AppTask::OnOtaEventHandler(AppEvent * aEvent)
// {
// switch (aEvent->Type)
// {
// case AppEvent::kEventType_ota_provider_announce: {
// ChipLogProgress(NotSpecified, "OTA provider announce event");
// MbedOTARequestor * requestor = static_cast<MbedOTARequestor *>(GetRequestorInstance());
// requestor->ConnectProvider();
// break;
// }

// case AppEvent::kEventType_ota_provider_response: {
// ChipLogProgress(NotSpecified, "OTA provider response event");
// MbedOTADownloader * downloader = static_cast<MbedOTADownloader *>(GetDownloaderInstance());
// downloader->SetDownloadImageInfo(aEvent->OTAProviderResponseEvent.imageDatails->updateFileName);
// downloader->BeginPrepareDownload();
// break;
// }
// case AppEvent::kEventType_ota_download_completed:
// ChipLogProgress(NotSpecified, "OTA download completed event");
// ChipLogProgress(NotSpecified, "Download %.*s image size %ukB",
// static_cast<int>(aEvent->OTADownloadCompletedEvent.imageInfo->imageName.size()),
// aEvent->OTADownloadCompletedEvent.imageInfo->imageName.data(),
// (static_cast<unsigned>(aEvent->OTADownloadCompletedEvent.imageInfo->imageSize) / 1024u));
// break;
// default:
// ChipLogError(NotSpecified, "OTA event unknown");
// }
// }

// void AppTask::OnAnnounceProviderCallback()
// {
// AppEvent ota_announce_provider_event;
// ota_announce_provider_event.Type = AppEvent::kEventType_ota_provider_announce;
// ota_announce_provider_event.Handler = OnOtaEventHandler;
// sAppTask.PostEvent(&ota_announce_provider_event);
// }

// void AppTask::OnProviderResponseCallback(MbedOTARequestor::OTAUpdateDetails * updateDetails)
// {
// AppEvent ota_provider_response_event;
// ota_provider_response_event.Type = AppEvent::kEventType_ota_provider_response;
// ota_provider_response_event.Handler = OnOtaEventHandler;
// ota_provider_response_event.OTAProviderResponseEvent.imageDatails = updateDetails;
// sAppTask.PostEvent(&ota_provider_response_event);
// }

// void AppTask::OnDownloadCompletedCallback(MbedOTADownloader::ImageInfo * imageInfo)
// {
// AppEvent ota_download_completed_event;
// ota_download_completed_event.Type = AppEvent::kEventType_ota_download_completed;
// ota_download_completed_event.Handler = OnOtaEventHandler;
// ota_download_completed_event.OTADownloadCompletedEvent.imageInfo = imageInfo;
// sAppTask.PostEvent(&ota_download_completed_event);
// }
// #endif
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@
* limitations under the License.
*/

#include <MbedOTARequestorDriver.h>
#include "OTARequestorDriverImpl.h"

bool MbedOTARequestorDriver::CheckImageDownloadAllowed()
namespace chip {

bool OTARequestorDriverImpl::CheckImageDownloadAllowed()
{
return true;
}

void MbedOTARequestorDriver::ImageDownloadComplete() {}
UserConsentAction OTARequestorDriverImpl::RequestUserConsent()
{
return ImmediateYes;
}

void OTARequestorDriverImpl::ImageDownloadComplete() {}

void OTARequestorDriverImpl::NotifyUpdateStateChange(chip::UpdateStateEnum state) {}

} // namespace chip
20 changes: 0 additions & 20 deletions examples/ota-requestor-app/mbed/main/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@

#include <cstdint>

#ifdef CHIP_OTA_REQUESTOR
#include <platform/mbed/MbedOTADownloader.h>
#include <platform/mbed/MbedOTARequestor.h>
#endif // CHIP_OTA_REQUESTOR

struct AppEvent;
typedef void (*EventHandler)(AppEvent *);

Expand All @@ -42,20 +37,5 @@ struct AppEvent

uint16_t Type;

#ifdef CHIP_OTA_REQUESTOR
union
{
struct
{
MbedOTARequestor::OTAUpdateDetails * imageDatails;
} OTAProviderResponseEvent;

struct
{
chip::MbedOTADownloader::ImageInfo * imageInfo;
} OTADownloadCompletedEvent;
};
#endif // CHIP_OTA_REQUESTOR

EventHandler Handler;
};
17 changes: 6 additions & 11 deletions examples/ota-requestor-app/mbed/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@

#include "AppEvent.h"

#ifdef CHIP_OTA_REQUESTOR
#include <MbedOTADownloader.h>
#include <MbedOTARequestor.h>
#endif

class AppTask
{
public:
Expand All @@ -39,13 +34,13 @@ class AppTask
void PostEvent(AppEvent * aEvent);
void DispatchEvent(const AppEvent * event);

#ifdef CHIP_OTA_REQUESTOR
static void OnOtaEventHandler(AppEvent * aEvent);
// #ifdef CHIP_OTA_REQUESTOR
// static void OnOtaEventHandler(AppEvent * aEvent);

static void OnAnnounceProviderCallback();
static void OnProviderResponseCallback(MbedOTARequestor::OTAUpdateDetails * updateDetails);
static void OnDownloadCompletedCallback(chip::MbedOTADownloader::ImageInfo * imageInfo);
#endif
// static void OnAnnounceProviderCallback();
// static void OnProviderResponseCallback(MbedOTARequestor::OTAUpdateDetails * updateDetails);
// static void OnDownloadCompletedCallback(chip::MbedOTADownloader::ImageInfo * imageInfo);
// #endif

static AppTask sAppTask;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,27 @@

#pragma once

#include "app/clusters/ota-requestor/OTARequestorDriver.h"
#include <OTARequestorDriver.h>

class MbedOTARequestorDriver : public OTARequestorDriver
namespace chip {

class OTARequestorDriverImpl : public OTARequestorDriver
{
public:
// A call into the application logic to give it a chance to allow or stop the Requestor
// from proceeding with actual image download. Returning TRUE will allow the download
// to proceed, returning FALSE will abort the download process.
bool CheckImageDownloadAllowed();

// Application is directed to complete user consent: either return ImmediateYes/ImmediateNo
// without blocking or return Requested and call OTARequestor::OnUserConsent() later.
UserConsentAction RequestUserConsent();

// Notify the application that the download is complete and the image can be applied
void ImageDownloadComplete();

// Notify application of a change in the UpdateState attribute
void NotifyUpdateStateChange(chip::UpdateStateEnum state);
};

} // namespace chip
3 changes: 1 addition & 2 deletions examples/ota-requestor-app/mbed/mbed_app.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"MXCRYPTO_DISABLED",
"NL_ASSERT_LOG=NL_ASSERT_LOG_DEFAULT",
"NL_ASSERT_EXPECT_FLAGS=NL_ASSERT_FLAG_LOG",
"WHD_PRINT_DISABLE",
"MBED_CONF_LWIP_NETBUF_RECVINFO_ENABLED"
"WHD_PRINT_DISABLE"
]
}
},
Expand Down
Loading

0 comments on commit d9fe34e

Please sign in to comment.