Skip to content

Commit

Permalink
implement ota-requestor command handlers and delegate class
Browse files Browse the repository at this point in the history
  • Loading branch information
holbrookt committed Oct 12, 2021
1 parent 75f3895 commit 51a9052
Show file tree
Hide file tree
Showing 14 changed files with 1,098 additions and 16 deletions.
6 changes: 6 additions & 0 deletions examples/ota-requestor-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
#include <zap-generated/CHIPClientCallbacks.h>
#include <zap-generated/CHIPClusters.h>

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

#include "BDXDownloader.h"
#include "ExampleRequestorDelegate.h"
#include "ExampleSelfCommissioning.h"
#include "PersistentStorage.h"

Expand Down Expand Up @@ -223,6 +226,9 @@ int main(int argc, char * argv[])
return 1;
}

ExampleRequestorDelegate delegate;
chip::app::clusters::OTARequestor::SetDelegate(&delegate);

chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig();
err = mStorage.Init();
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init Storage failure: %s", chip::ErrorStr(err)));
Expand Down
4 changes: 4 additions & 0 deletions examples/ota-requestor-app/ota-requestor-common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ chip_data_model("ota-requestor-common") {
sources = [
"BDXDownloader.cpp",
"BDXDownloader.h",
"ExampleRequestorDelegate.cpp",
"ExampleRequestorDelegate.h",
]

public_configs = [ ":config" ]

is_server = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <ExampleRequestorDelegate.h>

#include <app-common/zap-generated/enums.h>
#include <app/util/af-enums.h>
#include <controller/CHIPDevice.h>
#include <lib/support/BufferReader.h>
#include <lib/support/Span.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>

constexpr uint32_t kImmediateStartDelayMs = 1;

ExampleRequestorDelegate::ExampleRequestorDelegate()
{
mOtaStartDelayMs = 0;
mProviderFabricIndex.SetValue(0);
}

void ExampleRequestorDelegate::Init(chip::Controller::ControllerDeviceInitParams connectParams, uint32_t startDelayMs)
{
mConnectParams = connectParams;
mOtaStartDelayMs = startDelayMs;
}

void ExampleRequestorDelegate::ConnectAndBeginOTA()
{
VerifyOrReturn(mProviderId.HasValue(), ChipLogError(SoftwareUpdate, "%s: missing Provider ID", __FUNCTION__));
VerifyOrReturn(mProviderFabricIndex.HasValue(), ChipLogError(SoftwareUpdate, "%s: missing Provider FabricIndex", __FUNCTION__));

ChipLogProgress(SoftwareUpdate, "When #7976 is fixed, this will attempt to connect to 0x" ChipLogFormatX64 " on FabricId %u",
ChipLogValueX64(mProviderId.Value()), mProviderFabricIndex.Value());

// TODO: uncomment and fill in after #7976 is fixed
// mProviderDevice.Init(mConnectParams, mProviderId.Value(), address, mProviderFabricIndex.Value());
// mProviderDevice.EstablishConnectivity();
}

EmberAfStatus ExampleRequestorDelegate::HandleAnnounceOTAProvider(chip::app::CommandHandler * commandObj,
chip::NodeId providerLocation, uint16_t vendorId,
uint8_t announcementReason, chip::ByteSpan metadataForNode)
{
mProviderId.SetValue(providerLocation);

ChipLogDetail(SoftwareUpdate, "notified of Provider at NodeID: 0x" ChipLogFormatX64, ChipLogValueX64(mProviderId.Value()));

// If reason is URGENT_UPDATE_AVAILABLE, we start OTA immediately. Otherwise, respect the timer value set in mOtaStartDelayMs.
// This is done to exemplify what a real-world OTA Requestor might do while also being configurable enough to use as a test app.
uint32_t msToStart = 0;
switch (announcementReason)
{
case static_cast<uint8_t>(EMBER_ZCL_OTA_ANNOUNCEMENT_REASON_SIMPLE_ANNOUNCEMENT):
case static_cast<uint8_t>(EMBER_ZCL_OTA_ANNOUNCEMENT_REASON_UPDATE_AVAILABLE):
msToStart = mOtaStartDelayMs;
break;
case static_cast<uint8_t>(EMBER_ZCL_OTA_ANNOUNCEMENT_REASON_URGENT_UPDATE_AVAILABLE):
msToStart = kImmediateStartDelayMs;
break;
default:
break;
}

chip::DeviceLayer::SystemLayer().StartTimer(msToStart, StartDelayTimerHandler, this);

return EMBER_ZCL_STATUS_SUCCESS;
}

void ExampleRequestorDelegate::StartDelayTimerHandler(chip::System::Layer * systemLayer, void * appState)
{
VerifyOrReturn(appState != nullptr);
static_cast<ExampleRequestorDelegate *>(appState)->ConnectAndBeginOTA();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <app/Command.h>
#include <app/clusters/ota-requestor/ota-requestor-delegate.h>
#include <app/util/basic-types.h>
#include <controller/CHIPDevice.h>
#include <lib/core/NodeId.h>
#include <lib/core/Optional.h>

using chip::app::clusters::OTARequestorDelegate;

// An example implementation for how an application might handle receiving an AnnounceOTAProvider command. In this case, the
// AnnounceOTAProvider command will be used as a trigger to send a QueryImage command and begin the OTA process. This class also
// contains other application-specific logic related to OTA Software Update.
class ExampleRequestorDelegate : public OTARequestorDelegate
{
public:
ExampleRequestorDelegate();

void Init(chip::Controller::ControllerDeviceInitParams connectParams, uint32_t startDelayMs);

// Inherited from OTAProviderDelegate
EmberAfStatus HandleAnnounceOTAProvider(chip::app::CommandHandler * commandObj, chip::NodeId providerLocation,
uint16_t vendorId, uint8_t announcementReason, chip::ByteSpan metadataForNode) override;

private:
void ConnectAndBeginOTA();
static void StartDelayTimerHandler(chip::System::Layer * systemLayer, void * appState);

chip::Controller::Device mProviderDevice;
chip::Controller::ControllerDeviceInitParams mConnectParams;
chip::Optional<chip::NodeId> mProviderId;
chip::Optional<chip::FabricIndex> mProviderFabricIndex;
uint32_t mOtaStartDelayMs;
};
Loading

0 comments on commit 51a9052

Please sign in to comment.