Skip to content

Commit

Permalink
[examples] Extract common Linux platform app start up code (#6222)
Browse files Browse the repository at this point in the history
* [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 <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Jun 23, 2021
1 parent 7272eee commit 1446842
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 173 deletions.
1 change: 1 addition & 0 deletions examples/all-clusters-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
24 changes: 3 additions & 21 deletions examples/all-clusters-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <support/CHIPMem.h>
#include <support/RandUtils.h>

#include "Server.h"
#include "AppMain.h"

#include <cassert>
#include <iostream>
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion examples/lighting-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
4 changes: 2 additions & 2 deletions examples/lighting-app/linux/include/Rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

#pragma once

#include <CommonRpc.h>

namespace chip {
namespace rpc {

class LightingService;

int Init();

} // namespace rpc
} // namespace chip
132 changes: 4 additions & 128 deletions examples/lighting-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,17 @@
* limitations under the License.
*/

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

#include "af.h"
#include "gen/attribute-id.h"
#include "gen/cluster-id.h"
#include <app/chip-zcl-zpro-codec.h>
#include <app/util/af-types.h>
#include <app/util/attribute-storage.h>
#include <app/util/util.h>
#include <core/CHIPError.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#include <support/CHIPMem.h>
#include <support/RandUtils.h>

#include "LightingManager.h"
#include "Options.h"
#include "Server.h"

#include <AppMain.h>

#if defined(PW_RPC_ENABLED)
#include "Rpc.h"
Expand All @@ -44,8 +36,6 @@
#include <iostream>

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,
Expand Down Expand Up @@ -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;
}
117 changes: 117 additions & 0 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <thread>

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

#include <app/server/OnboardingCodesUtil.h>
#include <app/server/Server.h>
#include <core/CHIPError.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#include <support/CHIPMem.h>
#include <support/RandUtils.h>

#if defined(PW_RPC_ENABLED)
#include <CommonRpc.h>
#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();
}
22 changes: 22 additions & 0 deletions examples/platform/linux/AppMain.h
Original file line number Diff line number Diff line change
@@ -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();
Loading

0 comments on commit 1446842

Please sign in to comment.