Skip to content

Commit

Permalink
Refactor network commissioning setup in Linux example apps. (#27861)
Browse files Browse the repository at this point in the history
* Refactor network commissioning setup in Linux example apps.

Linux (and Mac) example apps were inconsistent in terms of how they handled
network commissioning, with most of them being buggy in terms of not setting
things up properly on Mac or not setting things up properly at all.

The changes here move the setup into AppMain, so it's handled consistently
across all example apps.

* Address review comment.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Sep 28, 2023
1 parent dd4b607 commit 8ad0ef6
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 605 deletions.
99 changes: 0 additions & 99 deletions examples/all-clusters-app/linux/main-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <app/CommandHandler.h>
#include <app/att-storage.h>
#include <app/clusters/identify-server/identify-server.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/server/Server.h>
#include <app/util/af.h>
#include <lib/support/CHIPMem.h>
Expand All @@ -36,17 +35,6 @@
#include <transport/SessionManager.h>
#include <transport/raw/PeerAddress.h>

#if CHIP_DEVICE_LAYER_TARGET_DARWIN
#include <platform/Darwin/NetworkCommissioningDriver.h>
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include <platform/Darwin/WiFi/NetworkCommissioningWiFiDriver.h>
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN

#if CHIP_DEVICE_LAYER_TARGET_LINUX
#include <platform/Linux/NetworkCommissioningDriver.h>
#endif // CHIP_DEVICE_LAYER_TARGET_LINUX

#include <Options.h>

using namespace chip;
Expand Down Expand Up @@ -105,42 +93,7 @@ static Identify gIdentify1 = {
OnTriggerEffect,
};

// Network commissioning
namespace {
// This file is being used by platforms other than Linux, so we need this check to disable related features since we only
// implemented them on linux.
constexpr EndpointId kNetworkCommissioningEndpointMain = 0;
constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE;

#if CHIP_DEVICE_LAYER_TARGET_LINUX
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
NetworkCommissioning::LinuxThreadDriver sThreadDriver;
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
NetworkCommissioning::LinuxWiFiDriver sWiFiDriver;
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

NetworkCommissioning::LinuxEthernetDriver sEthernetDriver;
#endif // CHIP_DEVICE_LAYER_TARGET_LINUX

#if CHIP_DEVICE_LAYER_TARGET_DARWIN
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
NetworkCommissioning::DarwinWiFiDriver sWiFiDriver;
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

NetworkCommissioning::DarwinEthernetDriver sEthernetDriver;
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
Clusters::NetworkCommissioning::Instance sThreadNetworkCommissioningInstance(kNetworkCommissioningEndpointMain, &sThreadDriver);
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(kNetworkCommissioningEndpointSecondary, &sWiFiDriver);
#endif

Clusters::NetworkCommissioning::Instance sEthernetNetworkCommissioningInstance(kNetworkCommissioningEndpointMain, &sEthernetDriver);

class ExampleDeviceInstanceInfoProvider : public DeviceInstanceInfoProvider
{
Expand Down Expand Up @@ -200,58 +153,6 @@ ExampleDeviceInstanceInfoProvider gExampleDeviceInstanceInfoProvider;

void ApplicationInit()
{
(void) kNetworkCommissioningEndpointMain;
// Enable secondary endpoint only when we need it, this should be applied to all platforms.
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);

const bool kThreadEnabled = {
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
LinuxDeviceOptions::GetInstance().mThread
#else
false
#endif
};

const bool kWiFiEnabled = {
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
LinuxDeviceOptions::GetInstance().mWiFi
#else
false
#endif
};

if (kThreadEnabled && kWiFiEnabled)
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
sThreadNetworkCommissioningInstance.Init();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
sWiFiNetworkCommissioningInstance.Init();
#endif
// Only enable secondary endpoint for network commissioning cluster when both WiFi and Thread are enabled.
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, true);
}
else if (kThreadEnabled)
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
sThreadNetworkCommissioningInstance.Init();
#endif
}
else if (kWiFiEnabled)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
// If we only enable WiFi on this device, "move" WiFi instance to main NetworkCommissioning cluster endpoint.
sWiFiNetworkCommissioningInstance.~Instance();
new (&sWiFiNetworkCommissioningInstance)
Clusters::NetworkCommissioning::Instance(kNetworkCommissioningEndpointMain, &sWiFiDriver);
sWiFiNetworkCommissioningInstance.Init();
#endif
}
else
{
sEthernetNetworkCommissioningInstance.Init();
}

std::string path = kChipEventFifoPathPrefix + std::to_string(getpid());

if (sChipNamedPipeCommands.Start(path, &sAllClustersCommandDelegate) != CHIP_NO_ERROR)
Expand Down
8 changes: 7 additions & 1 deletion examples/all-clusters-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
#include "binding-handler.h"
#include "main-common.h"

// Network commissioning
namespace {
constexpr chip::EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE;
} // anonymous namespace

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv, AppOptions::GetOptions()) == 0);
VerifyOrDie(
ChipLinuxAppInit(argc, argv, AppOptions::GetOptions(), chip::MakeOptional(kNetworkCommissioningEndpointSecondary)) == 0);
VerifyOrDie(InitBindingHandlers() == CHIP_NO_ERROR);

LinuxDeviceOptions::GetInstance().dacProvider = AppOptions::GetDACProvider();
Expand Down
91 changes: 1 addition & 90 deletions examples/all-clusters-minimal-app/linux/main-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
#include <app/CommandHandler.h>
#include <app/att-storage.h>
#include <app/clusters/identify-server/identify-server.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/server/Server.h>
#include <app/util/af.h>
#include <lib/support/CHIPMem.h>
#include <new>
#include <platform/Linux/NetworkCommissioningDriver.h>
#include <platform/PlatformManager.h>
#include <system/SystemPacketBuffer.h>
#include <transport/SessionManager.h>
Expand Down Expand Up @@ -84,94 +82,7 @@ static Identify gIdentify1 = {
OnTriggerEffect,
};

// Network commissioning
namespace {
// This file is being used by platforms other than Linux, so we need this check to disable related features since we only
// implemented them on linux.
constexpr EndpointId kNetworkCommissioningEndpointMain = 0;
constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE;

#if CHIP_DEVICE_LAYER_TARGET_LINUX
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
NetworkCommissioning::LinuxThreadDriver sLinuxThreadDriver;
Clusters::NetworkCommissioning::Instance sThreadNetworkCommissioningInstance(kNetworkCommissioningEndpointMain,
&sLinuxThreadDriver);
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WPA
NetworkCommissioning::LinuxWiFiDriver sLinuxWiFiDriver;
Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(kNetworkCommissioningEndpointSecondary,
&sLinuxWiFiDriver);
#endif
NetworkCommissioning::LinuxEthernetDriver sLinuxEthernetDriver;
Clusters::NetworkCommissioning::Instance sEthernetNetworkCommissioningInstance(kNetworkCommissioningEndpointMain,
&sLinuxEthernetDriver);
#else // CHIP_DEVICE_LAYER_TARGET_LINUX
Clusters::NetworkCommissioning::NullNetworkDriver sNullNetworkDriver;
Clusters::NetworkCommissioning::Instance sNullNetworkCommissioningInstance(kNetworkCommissioningEndpointMain, &sNullNetworkDriver);
#endif // CHIP_DEVICE_LAYER_TARGET_LINUX
} // namespace

void ApplicationInit()
{
(void) kNetworkCommissioningEndpointMain;
// Enable secondary endpoint only when we need it, this should be applied to all platforms.
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);

#if CHIP_DEVICE_LAYER_TARGET_LINUX
const bool kThreadEnabled = {
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
LinuxDeviceOptions::GetInstance().mThread
#else
false
#endif
};

const bool kWiFiEnabled = {
#if CHIP_DEVICE_CONFIG_ENABLE_WPA
LinuxDeviceOptions::GetInstance().mWiFi
#else
false
#endif
};

if (kThreadEnabled && kWiFiEnabled)
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
sThreadNetworkCommissioningInstance.Init();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WPA
sWiFiNetworkCommissioningInstance.Init();
#endif
// Only enable secondary endpoint for network commissioning cluster when both WiFi and Thread are enabled.
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, true);
}
else if (kThreadEnabled)
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
sThreadNetworkCommissioningInstance.Init();
#endif
}
else if (kWiFiEnabled)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WPA
// If we only enable WiFi on this device, "move" WiFi instance to main NetworkCommissioning cluster endpoint.
sWiFiNetworkCommissioningInstance.~Instance();
new (&sWiFiNetworkCommissioningInstance)
Clusters::NetworkCommissioning::Instance(kNetworkCommissioningEndpointMain, &sLinuxWiFiDriver);
sWiFiNetworkCommissioningInstance.Init();
#endif
}
else
#endif // CHIP_DEVICE_LAYER_TARGET_LINUX
{
#if CHIP_DEVICE_LAYER_TARGET_LINUX
sEthernetNetworkCommissioningInstance.Init();
#else
// Use NullNetworkCommissioningInstance to disable the network commissioning functions.
sNullNetworkCommissioningInstance.Init();
#endif
}
}
void ApplicationInit() {}

void emberAfLowPowerClusterInitCallback(EndpointId endpoint)
{
Expand Down
8 changes: 7 additions & 1 deletion examples/all-clusters-minimal-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@
#include "AppOptions.h"
#include "binding-handler.h"

// Network commissioning
namespace {
constexpr chip::EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE;
} // anonymous namespace

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv, AppOptions::GetOptions()) == 0);
VerifyOrDie(
ChipLinuxAppInit(argc, argv, AppOptions::GetOptions(), chip::MakeOptional(kNetworkCommissioningEndpointSecondary)) == 0);
VerifyOrDie(InitBindingHandlers() == CHIP_NO_ERROR);

LinuxDeviceOptions::GetInstance().dacProvider = AppOptions::GetDACProvider();
Expand Down
85 changes: 1 addition & 84 deletions examples/bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/ConcreteAttributePath.h>
#include <app/EventLogging.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/reporting/reporting.h>
#include <app/util/af-types.h>
#include <app/util/af.h>
Expand All @@ -39,17 +38,6 @@
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>

#if CHIP_DEVICE_LAYER_TARGET_DARWIN
#include <platform/Darwin/NetworkCommissioningDriver.h>
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include <platform/Darwin/WiFi/NetworkCommissioningWiFiDriver.h>
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN

#if CHIP_DEVICE_LAYER_TARGET_LINUX
#include <platform/Linux/NetworkCommissioningDriver.h>
#endif // CHIP_DEVICE_LAYER_TARGET_LINUX

#include <pthread.h>
#include <sys/ioctl.h>

Expand Down Expand Up @@ -82,36 +70,6 @@ Device * gDevices[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT];
std::vector<Room *> gRooms;
std::vector<Action *> gActions;

#if CHIP_DEVICE_LAYER_TARGET_LINUX
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
DeviceLayer::NetworkCommissioning::LinuxThreadDriver sThreadDriver;
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
DeviceLayer::NetworkCommissioning::LinuxWiFiDriver sWiFiDriver;
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

DeviceLayer::NetworkCommissioning::LinuxEthernetDriver sEthernetDriver;
#endif // CHIP_DEVICE_LAYER_TARGET_LINUX

#if CHIP_DEVICE_LAYER_TARGET_DARWIN
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
DeviceLayer::NetworkCommissioning::DarwinWiFiDriver sWiFiDriver;
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

DeviceLayer::NetworkCommissioning::DarwinEthernetDriver sEthernetDriver;
#endif // CHIP_DEVICE_LAYER_TARGET_DARWIN

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(0, &sWiFiDriver);
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
Clusters::NetworkCommissioning::Instance sThreadNetworkCommissioningInstance(0, &sThreadDriver);
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD

Clusters::NetworkCommissioning::Instance sEthernetNetworkCommissioningInstance(0, &sEthernetDriver);

const int16_t minMeasuredValue = -27315;
const int16_t maxMeasuredValue = 32766;
const int16_t initialMeasuredValue = 100;
Expand Down Expand Up @@ -769,48 +727,7 @@ bool emberAfActionsClusterInstantActionCallback(app::CommandHandler * commandObj
return true;
}

void ApplicationInit()
{
const bool kThreadEnabled = {
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
LinuxDeviceOptions::GetInstance().mThread
#else
false
#endif
};

const bool kWiFiEnabled = {
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
LinuxDeviceOptions::GetInstance().mWiFi
#else
false
#endif
};

if (kThreadEnabled && kWiFiEnabled)
{
// Just use the Thread one.
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
sThreadNetworkCommissioningInstance.Init();
#endif
}
else if (kThreadEnabled)
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
sThreadNetworkCommissioningInstance.Init();
#endif
}
else if (kWiFiEnabled)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
sWiFiNetworkCommissioningInstance.Init();
#endif
}
else
{
sEthernetNetworkCommissioningInstance.Init();
}
}
void ApplicationInit() {}

const EmberAfDeviceType gBridgedOnOffDeviceTypes[] = { { DEVICE_TYPE_LO_ON_OFF_LIGHT, DEVICE_VERSION_DEFAULT },
{ DEVICE_TYPE_BRIDGED_NODE, DEVICE_VERSION_DEFAULT } };
Expand Down
Loading

0 comments on commit 8ad0ef6

Please sign in to comment.