Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OTA] Initalize OTA apps using Linux platform functions #14931

Merged
merged 2 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/ota-provider-app/linux/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@
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-provider-app/linux/include" ]
chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ]
34 changes: 34 additions & 0 deletions examples/ota-provider-app/linux/include/CHIPProjectAppConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* Copyright (c) 2022 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
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) 2022 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
19 changes: 13 additions & 6 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,9 @@ int ChipLinuxAppInit(int argc, char ** argv)
err = Platform::MemoryInit();
SuccessOrExit(err);

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

#ifdef CHIP_CONFIG_KVS_PATH
if (LinuxDeviceOptions::GetInstance().KVS == nullptr)
{
Expand All @@ -137,10 +140,14 @@ int ChipLinuxAppInit(int argc, char ** argv)
err = DeviceLayer::PlatformMgr().InitChipStack();
SuccessOrExit(err);

err = GetSetupPayload(LinuxDeviceOptions::GetInstance().payload, rendezvousFlags);
SuccessOrExit(err);
if (0 != LinuxDeviceOptions::GetInstance().payload.discriminator)
{
uint16_t discriminator = LinuxDeviceOptions::GetInstance().payload.discriminator;
err = DeviceLayer::ConfigurationMgr().StoreSetupDiscriminator(discriminator);
SuccessOrExit(err);
}

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

ConfigurationMgr().LogDeviceConfig();
Expand Down Expand Up @@ -182,7 +189,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
Loading