diff --git a/examples/ota-provider-app/linux/main.cpp b/examples/ota-provider-app/linux/main.cpp index 3fc2e425d8db82..5ccaaad9dde711 100644 --- a/examples/ota-provider-app/linux/main.cpp +++ b/examples/ota-provider-app/linux/main.cpp @@ -16,24 +16,17 @@ * limitations under the License. */ -#include -#include - #include #include #include #include -#include -#include #include -#include -#include -#include -#include #include #include #include +#include "AppMain.h" + #include #include #include @@ -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; @@ -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)"); @@ -396,12 +354,15 @@ int main(int argc, char * argv[]) // Parse JSON file and load the ota candidates std::vector 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; } diff --git a/examples/ota-requestor-app/linux/args.gni b/examples/ota-requestor-app/linux/args.gni index bbf4dd22e611c7..511f14b2cbbe7b 100644 --- a/examples/ota-requestor-app/linux/args.gni +++ b/examples/ota-requestor-app/linux/args.gni @@ -16,6 +16,14 @@ import("//build_overrides/chip.gni") import("${chip_root}/config/standalone/args.gni") +chip_device_project_config_include = "" +chip_project_config_include = "" +chip_system_project_config_include = "" + +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 } diff --git a/examples/ota-requestor-app/linux/include/CHIPProjectAppConfig.h b/examples/ota-requestor-app/linux/include/CHIPProjectAppConfig.h new file mode 100644 index 00000000000000..0ee799cc541d67 --- /dev/null +++ b/examples/ota-requestor-app/linux/include/CHIPProjectAppConfig.h @@ -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 + +// Allows app options (ports) to be configured on launch of app +#define CHIP_DEVICE_ENABLE_PORT_PARAMS 1 diff --git a/examples/ota-requestor-app/linux/main.cpp b/examples/ota-requestor-app/linux/main.cpp index 12bbe74294cd59..560061fb40057c 100644 --- a/examples/ota-requestor-app/linux/main.cpp +++ b/examples/ota-requestor-app/linux/main.cpp @@ -16,12 +16,7 @@ * limitations under the License. */ -#include -#include -#include -#include -#include - +#include "AppMain.h" #include "app/clusters/ota-requestor/BDXDownloader.h" #include "app/clusters/ota-requestor/OTARequestor.h" #include "platform/GenericOTARequestorDriver.h" @@ -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(); @@ -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; } diff --git a/examples/platform/linux/AppMain.cpp b/examples/platform/linux/AppMain.cpp index 556adf14cbcdec..a3ecd18f1bdd4c 100644 --- a/examples/platform/linux/AppMain.cpp +++ b/examples/platform/linux/AppMain.cpp @@ -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; @@ -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 @@ -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) { @@ -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); @@ -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; } diff --git a/examples/platform/linux/AppMain.h b/examples/platform/linux/AppMain.h index ca2e9472e19e29..584da5d099041f 100644 --- a/examples/platform/linux/AppMain.h +++ b/examples/platform/linux/AppMain.h @@ -25,7 +25,9 @@ #include #include -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 diff --git a/examples/platform/linux/Options.cpp b/examples/platform/linux/Options.cpp index 920757541b0935..99f80814ce695f 100644 --- a/examples/platform/linux/Options.cpp +++ b/examples/platform/linux/Options.cpp @@ -22,7 +22,6 @@ #include #include -#include using namespace chip; using namespace chip::ArgParser; @@ -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)) { diff --git a/examples/platform/linux/Options.h b/examples/platform/linux/Options.h index 5be6667ce52e2a..c4258187a8a246 100644 --- a/examples/platform/linux/Options.h +++ b/examples/platform/linux/Options.h @@ -27,6 +27,7 @@ #include #include +#include #include struct LinuxDeviceOptions @@ -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);