From 695c803fc9b2c74b7733eba97605653f4ff81dc7 Mon Sep 17 00:00:00 2001 From: Song Guo Date: Fri, 23 Apr 2021 21:23:48 +0800 Subject: [PATCH] [examples] Extract common Linux platform app start up code (#6222) * [examples] Extract common Linux platform app start up code * Restyled by gn * Use LogDeviceConfig + PrintOnboardingCodes * Move PW_RPC_ENABLED back to program * Add PW_RPC_ENABLED * Fix Linux build Co-authored-by: Restyled.io --- examples/all-clusters-app/linux/BUILD.gn | 1 + examples/all-clusters-app/linux/main.cpp | 24 +--- examples/lighting-app/linux/BUILD.gn | 2 +- examples/lighting-app/linux/include/Rpc.h | 4 +- examples/lighting-app/linux/main.cpp | 132 +----------------- examples/platform/linux/AppMain.cpp | 117 ++++++++++++++++ examples/platform/linux/AppMain.h | 22 +++ examples/platform/linux/BUILD.gn | 37 +++++ examples/platform/linux/CommonRpc.h | 27 ++++ .../linux/Options.cpp | 0 .../include => platform/linux}/Options.h | 0 examples/tv-app/linux/BUILD.gn | 1 + examples/tv-app/linux/main.cpp | 24 +--- 13 files changed, 218 insertions(+), 173 deletions(-) create mode 100644 examples/platform/linux/AppMain.cpp create mode 100644 examples/platform/linux/AppMain.h create mode 100644 examples/platform/linux/BUILD.gn create mode 100644 examples/platform/linux/CommonRpc.h rename examples/{lighting-app => platform}/linux/Options.cpp (100%) rename examples/{lighting-app/linux/include => platform/linux}/Options.h (100%) diff --git a/examples/all-clusters-app/linux/BUILD.gn b/examples/all-clusters-app/linux/BUILD.gn index 73bc2884372c7d..b9fdd4d48d4869 100644 --- a/examples/all-clusters-app/linux/BUILD.gn +++ b/examples/all-clusters-app/linux/BUILD.gn @@ -20,6 +20,7 @@ executable("chip-all-clusters-app") { deps = [ "${chip_root}/examples/all-clusters-app/all-clusters-common", + "${chip_root}/examples/platform/linux:app-main", "${chip_root}/src/lib", ] diff --git a/examples/all-clusters-app/linux/main.cpp b/examples/all-clusters-app/linux/main.cpp index 1db5f5aba6daeb..4acf729a7156b1 100644 --- a/examples/all-clusters-app/linux/main.cpp +++ b/examples/all-clusters-app/linux/main.cpp @@ -32,7 +32,7 @@ #include #include -#include "Server.h" +#include "AppMain.h" #include #include @@ -54,25 +54,7 @@ bool emberAfBasicClusterMfgSpecificPingCallback(chip::app::Command * commandObj) int main(int argc, char * argv[]) { - CHIP_ERROR err = CHIP_NO_ERROR; - - err = chip::Platform::MemoryInit(); - SuccessOrExit(err); - - err = chip::DeviceLayer::PlatformMgr().InitChipStack(); - SuccessOrExit(err); - - // Init ZCL Data Model and CHIP App Server - InitServer(); - - chip::DeviceLayer::PlatformMgr().RunEventLoop(); - -exit: - if (err != CHIP_NO_ERROR) - { - std::cerr << "Failed to run All Clusters App: " << ErrorStr(err) << std::endl; - // End the program with non zero error code to indicate a error. - return 1; - } + VerifyOrDie(ChipLinuxAppInit(argc, argv) == 0); + ChipLinuxAppMainLoop(); return 0; } diff --git a/examples/lighting-app/linux/BUILD.gn b/examples/lighting-app/linux/BUILD.gn index 3c93a77f008f5d..31e7c165e94aef 100644 --- a/examples/lighting-app/linux/BUILD.gn +++ b/examples/lighting-app/linux/BUILD.gn @@ -31,13 +31,13 @@ config("includes") { executable("chip-lighting-app") { sources = [ "LightingManager.cpp", - "Options.cpp", "include/LightingManager.h", "main.cpp", ] deps = [ "${chip_root}/examples/lighting-app/lighting-common", + "${chip_root}/examples/platform/linux:app-main", "${chip_root}/src/lib", ] diff --git a/examples/lighting-app/linux/include/Rpc.h b/examples/lighting-app/linux/include/Rpc.h index d4fc12d4861e0f..a86ec6d72da3bb 100644 --- a/examples/lighting-app/linux/include/Rpc.h +++ b/examples/lighting-app/linux/include/Rpc.h @@ -18,12 +18,12 @@ #pragma once +#include + namespace chip { namespace rpc { class LightingService; -int Init(); - } // namespace rpc } // namespace chip diff --git a/examples/lighting-app/linux/main.cpp b/examples/lighting-app/linux/main.cpp index e40b443b3a0458..a04f1093583be7 100644 --- a/examples/lighting-app/linux/main.cpp +++ b/examples/lighting-app/linux/main.cpp @@ -16,9 +16,6 @@ * limitations under the License. */ -#include -#include - #include "af.h" #include "gen/attribute-id.h" #include "gen/cluster-id.h" @@ -26,15 +23,10 @@ #include #include #include -#include -#include -#include -#include -#include #include "LightingManager.h" -#include "Options.h" -#include "Server.h" + +#include #if defined(PW_RPC_ENABLED) #include "Rpc.h" @@ -44,8 +36,6 @@ #include using namespace chip; -using namespace chip::Inet; -using namespace chip::Transport; using namespace chip::DeviceLayer; void emberAfPostAttributeChangeCallback(EndpointId endpoint, ClusterId clusterId, AttributeId attributeId, uint8_t mask, @@ -93,124 +83,10 @@ void emberAfOnOffClusterInitCallback(EndpointId endpoint) // TODO: implement any additional Cluster Server init actions } -namespace { -void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg) -{ - (void) arg; - if (event->Type == chip::DeviceLayer::DeviceEventType::kCHIPoBLEConnectionEstablished) - { - ChipLogProgress(DeviceLayer, "Receive kCHIPoBLEConnectionEstablished"); - } -} - -CHIP_ERROR PrintQRCodeContent() -{ - CHIP_ERROR err = CHIP_NO_ERROR; - // If we do not have a discriminator, generate one - chip::SetupPayload payload; - uint32_t setUpPINCode; - uint16_t setUpDiscriminator; - uint16_t vendorId; - uint16_t productId; - std::string result; - - err = ConfigurationMgr().GetSetupPinCode(setUpPINCode); - SuccessOrExit(err); - - err = ConfigurationMgr().GetSetupDiscriminator(setUpDiscriminator); - SuccessOrExit(err); - - err = ConfigurationMgr().GetVendorId(vendorId); - SuccessOrExit(err); - - err = ConfigurationMgr().GetProductId(productId); - SuccessOrExit(err); - - payload.version = 0; - payload.vendorID = vendorId; - payload.productID = productId; - payload.setUpPINCode = setUpPINCode; - payload.discriminator = setUpDiscriminator; - - // Wrap it so SuccessOrExit can work - { - chip::QRCodeSetupPayloadGenerator generator(payload); - err = generator.payloadBase41Representation(result); - SuccessOrExit(err); - } - - std::cout << "SetupPINCode: [" << setUpPINCode << "]" << std::endl; - // There might be whitespace in setup QRCode, add brackets to make it clearer. - std::cout << "SetupQRCode: [" << result << "]" << std::endl; - -exit: - if (err != CHIP_NO_ERROR) - { - std::cerr << "Failed to generate QR Code: " << ErrorStr(err) << std::endl; - } - return err; -} -} // namespace - int main(int argc, char * argv[]) { - CHIP_ERROR err = CHIP_NO_ERROR; - - err = chip::Platform::MemoryInit(); - SuccessOrExit(err); - - err = ParseArguments(argc, argv); - SuccessOrExit(err); - - err = chip::DeviceLayer::PlatformMgr().InitChipStack(); - SuccessOrExit(err); - - err = PrintQRCodeContent(); - SuccessOrExit(err); - -#if defined(PW_RPC_ENABLED) - chip::rpc::Init(); - std::cerr << "PW_RPC initialized." << std::endl; -#endif // defined(PW_RPC_ENABLED) - - chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(EventHandler, 0); - - chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName(nullptr); // Use default device name (CHIP-XXXX) - -#if CONFIG_NETWORK_LAYER_BLE - chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(LinuxDeviceOptions::GetInstance().mBleDevice, false); -#endif - - chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(true); - + VerifyOrDie(ChipLinuxAppInit(argc, argv) == 0); LightingMgr().Init(); - - // Init ZCL Data Model and CHIP App Server - InitServer(); - -#if CHIP_DEVICE_CONFIG_ENABLE_WPA - if (LinuxDeviceOptions::GetInstance().mWiFi) - { - chip::DeviceLayer::ConnectivityMgrImpl().StartWiFiManagement(); - } -#endif // CHIP_DEVICE_CONFIG_ENABLE_WPA - -#if CHIP_ENABLE_OPENTHREAD - if (LinuxDeviceOptions::GetInstance().mThread) - { - SuccessOrExit(err = chip::DeviceLayer::ThreadStackMgrImpl().InitThreadStack()); - std::cerr << "Thread initialized." << std::endl; - } -#endif // CHIP_ENABLE_OPENTHREAD - - chip::DeviceLayer::PlatformMgr().RunEventLoop(); - -exit: - if (err != CHIP_NO_ERROR) - { - std::cerr << "Failed to run Linux Lighting App: " << ErrorStr(err) << std::endl; - // End the program with non zero error code to indicate a error. - return 1; - } + ChipLinuxAppMainLoop(); return 0; } diff --git a/examples/platform/linux/AppMain.cpp b/examples/platform/linux/AppMain.cpp new file mode 100644 index 00000000000000..216c3c0069c9ab --- /dev/null +++ b/examples/platform/linux/AppMain.cpp @@ -0,0 +1,117 @@ +/* + * + * 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 +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#if defined(PW_RPC_ENABLED) +#include +#endif + +#include "Options.h" + +using namespace chip; +using namespace chip::Inet; +using namespace chip::Transport; +using namespace chip::DeviceLayer; + +namespace { +void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg) +{ + (void) arg; + if (event->Type == chip::DeviceLayer::DeviceEventType::kCHIPoBLEConnectionEstablished) + { + ChipLogProgress(DeviceLayer, "Receive kCHIPoBLEConnectionEstablished"); + } +} +} // namespace + +int ChipLinuxAppInit(int argc, char ** argv) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + + err = chip::Platform::MemoryInit(); + SuccessOrExit(err); + + err = ParseArguments(argc, argv); + SuccessOrExit(err); + + err = chip::DeviceLayer::PlatformMgr().InitChipStack(); + SuccessOrExit(err); + + ConfigurationMgr().LogDeviceConfig(); + PrintOnboardingCodes(chip::RendezvousInformationFlag::kBLE); + +#if defined(PW_RPC_ENABLED) + chip::rpc::Init(); + ChipLogProgress(NotSpecified, "PW_RPC initialized."); +#endif // defined(PW_RPC_ENABLED) + + chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(EventHandler, 0); + + chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName(nullptr); // Use default device name (CHIP-XXXX) + +#if CONFIG_NETWORK_LAYER_BLE + chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(LinuxDeviceOptions::GetInstance().mBleDevice, false); +#endif + + chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(true); + +#if CHIP_DEVICE_CONFIG_ENABLE_WPA + if (LinuxDeviceOptions::GetInstance().mWiFi) + { + chip::DeviceLayer::ConnectivityMgrImpl().StartWiFiManagement(); + } +#endif // CHIP_DEVICE_CONFIG_ENABLE_WPA + +#if CHIP_ENABLE_OPENTHREAD + if (LinuxDeviceOptions::GetInstance().mThread) + { + SuccessOrExit(err = chip::DeviceLayer::ThreadStackMgrImpl().InitThreadStack()); + ChipLogProgress(NotSpecified, "Thread initialized."); + } +#endif // CHIP_ENABLE_OPENTHREAD + +exit: + if (err != CHIP_NO_ERROR) + { + ChipLogProgress(NotSpecified, "Failed to run Linux Lighting App: %s ", ErrorStr(err)); + // End the program with non zero error code to indicate a error. + return 1; + } + return 0; +} + +void ChipLinuxAppMainLoop() +{ + // Init ZCL Data Model and CHIP App Server + InitServer(); + + chip::DeviceLayer::PlatformMgr().RunEventLoop(); +} diff --git a/examples/platform/linux/AppMain.h b/examples/platform/linux/AppMain.h new file mode 100644 index 00000000000000..53a5674c161cf1 --- /dev/null +++ b/examples/platform/linux/AppMain.h @@ -0,0 +1,22 @@ +/* + * + * 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 + +int ChipLinuxAppInit(int argc, char ** argv); +void ChipLinuxAppMainLoop(); diff --git a/examples/platform/linux/BUILD.gn b/examples/platform/linux/BUILD.gn new file mode 100644 index 00000000000000..ff8dd7cde5b714 --- /dev/null +++ b/examples/platform/linux/BUILD.gn @@ -0,0 +1,37 @@ +# Copyright (c) 2020 Project CHIP Authors +# +# 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. + +import("//build_overrides/chip.gni") +import("${chip_root}/examples/common/pigweed/pigweed_rpcs.gni") + +config("app-main-config") { + include_dirs = [ "." ] +} + +source_set("app-main") { + sources = [ + "AppMain.cpp", + "AppMain.h", + "Options.cpp", + "Options.h", + ] + + if (chip_enable_pw_rpc) { + defines = [ "PW_RPC_ENABLED" ] + } + + public_deps = [ "${chip_root}/src/lib" ] + + public_configs = [ ":app-main-config" ] +} diff --git a/examples/platform/linux/CommonRpc.h b/examples/platform/linux/CommonRpc.h new file mode 100644 index 00000000000000..85495b2bf8fbaf --- /dev/null +++ b/examples/platform/linux/CommonRpc.h @@ -0,0 +1,27 @@ +/* + * + * 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 + +namespace chip { +namespace rpc { + +int Init(); + +} // namespace rpc +} // namespace chip diff --git a/examples/lighting-app/linux/Options.cpp b/examples/platform/linux/Options.cpp similarity index 100% rename from examples/lighting-app/linux/Options.cpp rename to examples/platform/linux/Options.cpp diff --git a/examples/lighting-app/linux/include/Options.h b/examples/platform/linux/Options.h similarity index 100% rename from examples/lighting-app/linux/include/Options.h rename to examples/platform/linux/Options.h diff --git a/examples/tv-app/linux/BUILD.gn b/examples/tv-app/linux/BUILD.gn index f2b6cb26dcd0e7..4a870a0f262807 100644 --- a/examples/tv-app/linux/BUILD.gn +++ b/examples/tv-app/linux/BUILD.gn @@ -30,6 +30,7 @@ executable("chip-tv-app") { ] deps = [ + "${chip_root}/examples/platform/linux:app-main", "${chip_root}/examples/tv-app/tv-common", "${chip_root}/src/lib", "${chip_root}/third_party/inipp", diff --git a/examples/tv-app/linux/main.cpp b/examples/tv-app/linux/main.cpp index 0ac5d791abfa95..9b9e2ed997c7e4 100644 --- a/examples/tv-app/linux/main.cpp +++ b/examples/tv-app/linux/main.cpp @@ -33,7 +33,7 @@ #include #include -#include "Server.h" +#include "AppMain.h" #include @@ -57,25 +57,7 @@ bool emberAfBasicClusterMfgSpecificPingCallback(chip::app::Command * commandObj) int main(int argc, char * argv[]) { - CHIP_ERROR err = CHIP_NO_ERROR; - - err = chip::Platform::MemoryInit(); - SuccessOrExit(err); - - err = chip::DeviceLayer::PlatformMgr().InitChipStack(); - SuccessOrExit(err); - - // Init ZCL Data Model and CHIP App Server - InitServer(); - - chip::DeviceLayer::PlatformMgr().RunEventLoop(); - -exit: - if (err != CHIP_NO_ERROR) - { - std::cerr << "Failed to run TV App: " << ErrorStr(err) << std::endl; - // End the program with non zero error code to indicate a error. - return 1; - } + VerifyOrDie(ChipLinuxAppInit(argc, argv) == 0); + ChipLinuxAppMainLoop(); return 0; }