/* * * 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 "AppMain.h" #include #include #include #include #include #include using chip::BDXDownloader; using chip::ByteSpan; using chip::CharSpan; using chip::EndpointId; using chip::FabricIndex; using chip::GetRequestorInstance; using chip::NodeId; using chip::OnDeviceConnected; using chip::OnDeviceConnectionFailure; using chip::OTADownloader; using chip::OTAImageProcessorImpl; using chip::PeerId; using chip::Server; using chip::VendorId; using chip::app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum; using chip::Callback::Callback; using chip::System::Layer; using chip::Transport::PeerAddress; using namespace chip; using namespace chip::app; using namespace chip::ArgParser; using namespace chip::DeviceLayer; using namespace chip::Messaging; using namespace chip::app::Clusters::OtaSoftwareUpdateProvider::Commands; class CustomOTARequestorDriver : public DeviceLayer::ExtendedOTARequestorDriver { public: bool CanConsent() override; void UpdateDownloaded() override; void UpdateConfirmed(System::Clock::Seconds32 delay) override; static void AppliedNotifyUpdateTimer(System::Layer * systemLayer, void * appState); void SendNotifyUpdateApplied(); }; DefaultOTARequestor gRequestorCore; DefaultOTARequestorStorage gRequestorStorage; CustomOTARequestorDriver gRequestorUser; BDXDownloader gDownloader; OTAImageProcessorImpl gImageProcessor; chip::ota::DefaultOTARequestorUserConsent gUserConsentProvider; static chip::ota::UserConsentState gUserConsentState = chip::ota::UserConsentState::kUnknown; bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue); constexpr uint16_t kOptionAutoApplyImage = 'a'; constexpr uint16_t kOptionRequestorCanConsent = 'c'; constexpr uint16_t kOptionDisableNotify = 'd'; constexpr uint16_t kOptionOtaDownloadPath = 'f'; constexpr uint16_t kOptionPeriodicQueryTimeout = 'p'; constexpr uint16_t kOptionUserConsentState = 'u'; constexpr uint16_t kOptionWatchdogTimeout = 'w'; constexpr uint16_t kSkipExecImageFile = 's'; constexpr size_t kMaxFilePathSize = 256; uint32_t gPeriodicQueryTimeoutSec = 0; uint32_t gWatchdogTimeoutSec = 0; chip::Optional gRequestorCanConsent; static char gOtaDownloadPath[kMaxFilePathSize] = "/tmp/test.bin"; bool gAutoApplyImage = false; bool gSendNotifyUpdateApplied = true; bool gSkipExecImageFile = false; OptionDef cmdLineOptionsDef[] = { { "autoApplyImage", chip::ArgParser::kNoArgument, kOptionAutoApplyImage }, { "requestorCanConsent", chip::ArgParser::kArgumentRequired, kOptionRequestorCanConsent }, { "disableNotifyUpdateApplied", chip::ArgParser::kNoArgument, kOptionDisableNotify }, { "otaDownloadPath", chip::ArgParser::kArgumentRequired, kOptionOtaDownloadPath }, { "periodicQueryTimeout", chip::ArgParser::kArgumentRequired, kOptionPeriodicQueryTimeout }, { "userConsentState", chip::ArgParser::kArgumentRequired, kOptionUserConsentState }, { "watchdogTimeout", chip::ArgParser::kArgumentRequired, kOptionWatchdogTimeout }, { "skipExecImageFile", chip::ArgParser::kNoArgument, kSkipExecImageFile }, {}, }; // Options for various test scenarios OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS", " -a, --autoApplyImage\n" " If supplied, apply the image immediately after download.\n" " Otherwise, the OTA update is complete after image download.\n" " -c, --requestorCanConsent \n" " Value for the RequestorCanConsent field in the QueryImage command.\n" " If not supplied, the value is determined by the driver.\n" " -d, --disableNotifyUpdateApplied\n" " If supplied, disable sending of the NotifyUpdateApplied command.\n" " Otherwise, after successfully loading into the updated image, send the NotifyUpdateApplied command.\n" " -f, --otaDownloadPath \n" " If supplied, the OTA image is downloaded to the given fully-qualified file-path.\n" " Otherwise, the default location for the downloaded image is at /tmp/test.bin\n" " -p, --periodicQueryTimeout