Skip to content

Commit

Permalink
[OTA] Initalize OTA apps using Linux platform functions
Browse files Browse the repository at this point in the history
- Add in an optional param to allow for app-specific custom command line options
  • Loading branch information
carol-apple committed Feb 9, 2022
1 parent 68b451e commit 8f19b27
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 125 deletions.
85 changes: 23 additions & 62 deletions examples/ota-provider-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,17 @@
* limitations under the License.
*/

#include <platform/CHIPDeviceLayer.h>
#include <platform/PlatformManager.h>

#include <app/clusters/ota-provider/ota-provider-delegate.h>
#include <app/clusters/ota-provider/ota-provider.h>
#include <app/server/Server.h>
#include <app/util/util.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <json/json.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPArgParser.hpp>
#include <lib/support/CHIPMem.h>
#include <lib/support/logging/CHIPLogging.h>
#include <ota-provider-common/BdxOtaSender.h>
#include <ota-provider-common/DefaultUserConsentProvider.h>
#include <ota-provider-common/OTAProviderExample.h>

#include "AppMain.h"

#include <fstream>
#include <iostream>
#include <unistd.h>
Expand Down Expand Up @@ -62,6 +55,8 @@ constexpr uint16_t kOptionUserConsentNeeded = 'c';

static constexpr uint16_t kMaximumDiscriminatorValue = 0xFFF;

OTAProviderExample gOtaProvider;

// Global variables used for passing the CLI arguments to the OTAProviderExample object
static OTAProviderExample::QueryImageBehaviorType gQueryImageBehavior = OTAProviderExample::kRespondWithUnknown;
static uint32_t gDelayedActionTimeSec = 0;
Expand Down Expand Up @@ -308,85 +303,48 @@ HelpOptions helpOptions("ota-provider-app", "Usage: ota-provider-app [options]",

OptionSet * allOptions[] = { &cmdLineOptions, &helpOptions, nullptr };

int main(int argc, char * argv[])
void ApplicationInit()
{
CHIP_ERROR err = CHIP_NO_ERROR;
OTAProviderExample otaProvider;
chip::ota::DefaultUserConsentProvider userConsentProvider;

if (chip::Platform::MemoryInit() != CHIP_NO_ERROR)
{
fprintf(stderr, "FAILED to initialize memory\n");
return 1;
}

if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR)
{
fprintf(stderr, "FAILED to initialize chip stack\n");
return 1;
}

if (!chip::ArgParser::ParseArgs(argv[0], argc, argv, allOptions))
{
return 1;
}

chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig();

if (gSetupDiscriminator.HasValue())
{
// Set discriminator to user specified value
ChipLogProgress(SoftwareUpdate, "Setting discriminator to: %" PRIu16, gSetupDiscriminator.Value());
err = chip::DeviceLayer::ConfigurationMgr().StoreSetupDiscriminator(gSetupDiscriminator.Value());
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Setup discriminator setting failed with code: %" CHIP_ERROR_FORMAT, err.Format());
return 1;
}
}

chip::Server::GetInstance().Init();

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider());

BdxOtaSender * bdxOtaSender = otaProvider.GetBdxOtaSender();
VerifyOrReturnError(bdxOtaSender != nullptr, 1);
BdxOtaSender * bdxOtaSender = gOtaProvider.GetBdxOtaSender();
VerifyOrReturn(bdxOtaSender != nullptr);
err = chip::Server::GetInstance().GetExchangeManager().RegisterUnsolicitedMessageHandlerForProtocol(chip::Protocols::BDX::Id,
bdxOtaSender);
if (err != CHIP_NO_ERROR)
{
ChipLogDetail(SoftwareUpdate, "RegisterUnsolicitedMessageHandler failed: %s", chip::ErrorStr(err));
return 1;
return;
}

ChipLogDetail(SoftwareUpdate, "using OTA file: %s", gOtaFilepath ? gOtaFilepath : "(none)");
ChipLogDetail(SoftwareUpdate, "Using OTA file: %s", gOtaFilepath ? gOtaFilepath : "(none)");

if (gOtaFilepath != nullptr)
{
otaProvider.SetOTAFilePath(gOtaFilepath);
gOtaProvider.SetOTAFilePath(gOtaFilepath);
}

otaProvider.SetQueryImageBehavior(gQueryImageBehavior);
otaProvider.SetDelayedActionTimeSec(gDelayedActionTimeSec);
gOtaProvider.SetQueryImageBehavior(gQueryImageBehavior);
gOtaProvider.SetDelayedActionTimeSec(gDelayedActionTimeSec);
if (gSoftwareVersion.HasValue())
{
otaProvider.SetSoftwareVersion(gSoftwareVersion.Value());
gOtaProvider.SetSoftwareVersion(gSoftwareVersion.Value());
}
if (gSoftwareVersionString)
{
otaProvider.SetSoftwareVersionString(gSoftwareVersionString);
gOtaProvider.SetSoftwareVersionString(gSoftwareVersionString);
}

if (gUserConsentState != chip::ota::UserConsentState::kUnknown)
{
userConsentProvider.SetGlobalUserConsentState(gUserConsentState);
otaProvider.SetUserConsentDelegate(&userConsentProvider);
gOtaProvider.SetUserConsentDelegate(&userConsentProvider);
}

if (gUserConsentNeeded)
{
otaProvider.SetUserConsentNeeded(true);
gOtaProvider.SetUserConsentNeeded(true);
}

ChipLogDetail(SoftwareUpdate, "Using ImageList file: %s", gOtaImageListFilepath ? gOtaImageListFilepath : "(none)");
Expand All @@ -396,12 +354,15 @@ int main(int argc, char * argv[])
// Parse JSON file and load the ota candidates
std::vector<OTAProviderExample::DeviceSoftwareVersionModel> candidates;
ParseJsonFileAndPopulateCandidates(gOtaImageListFilepath, candidates);
otaProvider.SetOTACandidates(candidates);
gOtaProvider.SetOTACandidates(candidates);
}

chip::app::Clusters::OTAProvider::SetDelegate(kOtaProviderEndpoint, &otaProvider);

chip::DeviceLayer::PlatformMgr().RunEventLoop();
chip::app::Clusters::OTAProvider::SetDelegate(kOtaProviderEndpoint, &gOtaProvider);
}

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv, &cmdLineOptions) == 0);
ChipLinuxAppMainLoop();
return 0;
}
8 changes: 8 additions & 0 deletions examples/ota-requestor-app/linux/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import("//build_overrides/chip.gni")

import("${chip_root}/config/standalone/args.gni")

chip_device_project_config_include = "<CHIPProjectAppConfig.h>"
chip_project_config_include = "<CHIPProjectAppConfig.h>"
chip_system_project_config_include = "<SystemProjectConfig.h>"

chip_project_config_include_dirs =
[ "${chip_root}/examples/ota-requestor-app/linux/include" ]
chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ]

declare_args() {
chip_enable_ota_requestor = true
}
34 changes: 34 additions & 0 deletions examples/ota-requestor-app/linux/include/CHIPProjectAppConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* Copyright (c) 2020 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.
*/

/**
* @file
* Example project configuration file for CHIP.
*
* This is a place to put application or project-specific overrides
* to the default configuration values for general CHIP features.
*
*/

#pragma once

// include the CHIPProjectConfig from config/standalone
#include <CHIPProjectConfig.h>

// Allows app options (ports) to be configured on launch of app
#define CHIP_DEVICE_ENABLE_PORT_PARAMS 1
53 changes: 7 additions & 46 deletions examples/ota-requestor-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
* limitations under the License.
*/

#include <app/server/Server.h>
#include <controller/ExampleOperationalCredentialsIssuer.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <lib/support/CHIPArgParser.hpp>
#include <platform/CHIPDeviceLayer.h>

#include "AppMain.h"
#include "app/clusters/ota-requestor/BDXDownloader.h"
#include "app/clusters/ota-requestor/OTARequestor.h"
#include "platform/GenericOTARequestorDriver.h"
Expand Down Expand Up @@ -172,46 +167,9 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier,
return (retval);
}

int main(int argc, char * argv[])
void ApplicationInit()
{
CHIP_ERROR err = CHIP_NO_ERROR;

if (chip::Platform::MemoryInit() != CHIP_NO_ERROR)
{

ChipLogError(SoftwareUpdate, "FAILED to initialize memory");
return 1;
}

if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "FAILED to initialize chip stack");
return 1;
}

if (!chip::ArgParser::ParseArgs(argv[0], argc, argv, allOptions))
{
return 1;
}

chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig();

// Set discriminator to user specified value
ChipLogProgress(SoftwareUpdate, "Setting discriminator to: %d", setupDiscriminator);
err = chip::DeviceLayer::ConfigurationMgr().StoreSetupDiscriminator(setupDiscriminator);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Setup discriminator setting failed with code: %" CHIP_ERROR_FORMAT, err.Format());
return 1;
}

// Init Data Model and CHIP App Server with user specified UDP port
Server::GetInstance().Init(nullptr, requestorSecurePort);
chip::Dnssd::Resolver::Instance().Init(chip::DeviceLayer::UDPEndPointManager());
ChipLogProgress(SoftwareUpdate, "Initializing the Application Server. Listening on UDP port %d", requestorSecurePort);

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider());

// Initialize all OTA download components
InitOTARequestor();
Expand All @@ -225,9 +183,12 @@ int main(int argc, char * argv[])
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(delayQueryTimeInSec * 1000),
OnStartDelayTimerHandler, nullptr);
}
}

chip::DeviceLayer::PlatformMgr().RunEventLoop();

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv, &cmdLineOptions) == 0);
ChipLinuxAppMainLoop();
return 0;
}

Expand Down
24 changes: 12 additions & 12 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
#endif

#include "AppMain.h"
#include "Options.h"

using namespace chip;
using namespace chip::ArgParser;
using namespace chip::Credentials;
using namespace chip::DeviceLayer;
using namespace chip::Inet;
Expand Down Expand Up @@ -106,7 +106,7 @@ static bool EnsureWiFiIsStarted()
}
#endif

int ChipLinuxAppInit(int argc, char ** argv)
int ChipLinuxAppInit(int argc, char ** argv, OptionSet * customOptions)
{
CHIP_ERROR err = CHIP_NO_ERROR;
#if CONFIG_NETWORK_LAYER_BLE
Expand All @@ -122,6 +122,15 @@ int ChipLinuxAppInit(int argc, char ** argv)
err = Platform::MemoryInit();
SuccessOrExit(err);

err = DeviceLayer::PlatformMgr().InitChipStack();
SuccessOrExit(err);

err = GetSetupPayload(LinuxDeviceOptions::GetInstance().payload, rendezvousFlags);
SuccessOrExit(err);

err = ParseArguments(argc, argv, customOptions);
SuccessOrExit(err);

#ifdef CHIP_CONFIG_KVS_PATH
if (LinuxDeviceOptions::GetInstance().KVS == nullptr)
{
Expand All @@ -134,15 +143,6 @@ int ChipLinuxAppInit(int argc, char ** argv)
SuccessOrExit(err);
#endif

err = DeviceLayer::PlatformMgr().InitChipStack();
SuccessOrExit(err);

err = GetSetupPayload(LinuxDeviceOptions::GetInstance().payload, rendezvousFlags);
SuccessOrExit(err);

err = ParseArguments(argc, argv);
SuccessOrExit(err);

ConfigurationMgr().LogDeviceConfig();

PrintOnboardingCodes(LinuxDeviceOptions::GetInstance().payload);
Expand Down Expand Up @@ -182,7 +182,7 @@ int ChipLinuxAppInit(int argc, char ** argv)
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogProgress(NotSpecified, "Failed to run Linux Lighting App: %s ", ErrorStr(err));
ChipLogProgress(NotSpecified, "Failed to init Linux App: %s ", ErrorStr(err));
// End the program with non zero error code to indicate a error.
return 1;
}
Expand Down
4 changes: 3 additions & 1 deletion examples/platform/linux/AppMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <platform/PlatformManager.h>
#include <transport/TransportMgr.h>

int ChipLinuxAppInit(int argc, char ** argv);
#include "Options.h"

int ChipLinuxAppInit(int argc, char ** argv, chip::ArgParser::OptionSet * customOptions = nullptr);
void ChipLinuxAppMainLoop();

#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
Expand Down
13 changes: 10 additions & 3 deletions examples/platform/linux/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <platform/CHIPDeviceLayer.h>

#include <lib/core/CHIPError.h>
#include <lib/support/CHIPArgParser.hpp>

using namespace chip;
using namespace chip::ArgParser;
Expand Down Expand Up @@ -236,13 +235,21 @@ OptionSet sDeviceOptions = { HandleOption, sDeviceOptionDefs, "GENERAL OPTIONS",
OptionSet * sLinuxDeviceOptionSets[] = { &sDeviceOptions, nullptr, nullptr };
} // namespace

CHIP_ERROR ParseArguments(int argc, char * argv[])
CHIP_ERROR ParseArguments(int argc, char * argv[], OptionSet * customOptions)
{
// Index 0 is for the general Linux options
uint8_t optionSetIndex = 1;
if (customOptions != nullptr)
{
// If there are custom options, include it during arg parsing
sLinuxDeviceOptionSets[optionSetIndex++] = customOptions;
}

char usage[kAppUsageLength];
snprintf(usage, kAppUsageLength, "Usage: %s [options]", argv[0]);

HelpOptions helpOptions(argv[0], usage, "1.0");
sLinuxDeviceOptionSets[1] = &helpOptions;
sLinuxDeviceOptionSets[optionSetIndex] = &helpOptions;

if (!ParseArgs(argv[0], argc, argv, sLinuxDeviceOptionSets))
{
Expand Down
3 changes: 2 additions & 1 deletion examples/platform/linux/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <cstdint>

#include <lib/core/CHIPError.h>
#include <lib/support/CHIPArgParser.hpp>
#include <setup_payload/SetupPayload.h>

struct LinuxDeviceOptions
Expand All @@ -45,4 +46,4 @@ struct LinuxDeviceOptions
static LinuxDeviceOptions & GetInstance();
};

CHIP_ERROR ParseArguments(int argc, char * argv[]);
CHIP_ERROR ParseArguments(int argc, char * argv[], chip::ArgParser::OptionSet * customOptions = nullptr);

0 comments on commit 8f19b27

Please sign in to comment.