Skip to content

Commit

Permalink
InetLayer/SystemLayer use C++11 singleton patter instead of global va…
Browse files Browse the repository at this point in the history
…riable (#11184)

* InetLayer/SystemLayer use C++11 singleton patter instead of global variable

* Add mock for SystemLayer

* Remove NDEBUG guard
  • Loading branch information
kghost authored and pull[bot] committed Feb 9, 2024
1 parent c8b2550 commit 1569644
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 62 deletions.
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class MdnsDebugListModel : public ActionListModel
private:
void DoReinit()
{
CHIP_ERROR err = Dnssd::ServiceAdvertiser::Instance().Init(&DeviceLayer::InetLayer);
CHIP_ERROR err = Dnssd::ServiceAdvertiser::Instance().Init(&DeviceLayer::InetLayer());
if (err != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Error initializing: %s", err.AsString());
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/discover/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Resolve : public DiscoverCommand, public chip::Dnssd::ResolverDelegate
/////////// DiscoverCommand Interface /////////
CHIP_ERROR RunCommand(NodeId remoteId, uint64_t fabricId) override
{
ReturnErrorOnFailure(chip::Dnssd::Resolver::Instance().Init(&chip::DeviceLayer::InetLayer));
ReturnErrorOnFailure(chip::Dnssd::Resolver::Instance().Init(&chip::DeviceLayer::InetLayer()));
chip::Dnssd::Resolver::Instance().SetResolverDelegate(this);
ChipLogProgress(chipTool, "Dnssd: Searching for NodeId: %" PRIx64 " FabricId: %" PRIx64 " ...", remoteId, fabricId);
return chip::Dnssd::Resolver::Instance().ResolveNodeId(chip::PeerId().SetNodeId(remoteId).SetCompressedFabricId(fabricId),
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal-mdns/advertiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ int main(int argc, char ** args)
return 1;
}

if (chip::Dnssd::ServiceAdvertiser::Instance().Init(&DeviceLayer::InetLayer) != CHIP_NO_ERROR)
if (chip::Dnssd::ServiceAdvertiser::Instance().Init(&DeviceLayer::InetLayer()) != CHIP_NO_ERROR)
{
fprintf(stderr, "FAILED to start MDNS advertisement\n");
return 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal-mdns/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ int main(int argc, char ** args)

MdnsExample::AllInterfaces allInterfaces(gOptions.enableIpV4);

err = mdnsServer.Listen(&chip::DeviceLayer::InetLayer, &allInterfaces, gOptions.listenPort);
err = mdnsServer.Listen(&chip::DeviceLayer::InetLayer(), &allInterfaces, gOptions.listenPort);
if (err != CHIP_NO_ERROR)
{
printf("Server failed to listen on all interfaces: %s\n", chip::ErrorStr(err));
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal-mdns/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ int main(int argc, char ** args)
{
MdnsExample::AllInterfaces allInterfaces(gOptions.enableIpV4);

if (mdnsServer.Listen(&DeviceLayer::InetLayer, &allInterfaces, gOptions.listenPort) != CHIP_NO_ERROR)
if (mdnsServer.Listen(&DeviceLayer::InetLayer(), &allInterfaces, gOptions.listenPort) != CHIP_NO_ERROR)
{
printf("Server failed to listen on all interfaces\n");
return 1;
Expand Down
4 changes: 2 additions & 2 deletions examples/shell/shell_common/cmd_ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,13 @@ void StartPinging(streamer_t * stream, char * destination)
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
err = gTCPManager.Init(Transport::TcpListenParameters(&DeviceLayer::InetLayer)
err = gTCPManager.Init(Transport::TcpListenParameters(&DeviceLayer::InetLayer())
.SetAddressType(gDestAddr.Type())
.SetListenPort(gPingArguments.GetEchoPort() + 1));
VerifyOrExit(err == CHIP_NO_ERROR, streamer_printf(stream, "Failed to init TCP manager error: %s\n", ErrorStr(err)));
#endif

err = gUDPManager.Init(Transport::UdpListenParameters(&DeviceLayer::InetLayer)
err = gUDPManager.Init(Transport::UdpListenParameters(&DeviceLayer::InetLayer())
.SetAddressType(gDestAddr.Type())
.SetListenPort(gPingArguments.GetEchoPort() + 1));
VerifyOrExit(err == CHIP_NO_ERROR, streamer_printf(stream, "Failed to init UDP manager error: %s\n", ErrorStr(err)));
Expand Down
4 changes: 2 additions & 2 deletions examples/shell/shell_common/cmd_send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ void ProcessCommand(streamer_t * stream, char * destination)
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
err = gTCPManager.Init(Transport::TcpListenParameters(&DeviceLayer::InetLayer)
err = gTCPManager.Init(Transport::TcpListenParameters(&DeviceLayer::InetLayer())
.SetAddressType(gDestAddr.Type())
.SetListenPort(gSendArguments.GetPort() + 1));
VerifyOrExit(err == CHIP_NO_ERROR, streamer_printf(stream, "Failed to init TCP manager error: %s\n", ErrorStr(err)));
#endif

err = gUDPManager.Init(Transport::UdpListenParameters(&DeviceLayer::InetLayer)
err = gUDPManager.Init(Transport::UdpListenParameters(&DeviceLayer::InetLayer())
.SetAddressType(gDestAddr.Type())
.SetListenPort(gSendArguments.GetPort() + 1));
VerifyOrExit(err == CHIP_NO_ERROR, streamer_printf(stream, "Failed to init UDP manager error: %s\n", ErrorStr(err)));
Expand Down
4 changes: 2 additions & 2 deletions src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ bool DnssdServer::OnExpiration(uint64_t expirationMs)

ChipLogDetail(Discovery, "OnExpiration - valid time out");

CHIP_ERROR err = Dnssd::ServiceAdvertiser::Instance().Init(&chip::DeviceLayer::InetLayer);
CHIP_ERROR err = Dnssd::ServiceAdvertiser::Instance().Init(&chip::DeviceLayer::InetLayer());
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to initialize advertiser: %s", chip::ErrorStr(err));
Expand Down Expand Up @@ -410,7 +410,7 @@ void DnssdServer::StartServer(chip::Dnssd::CommissioningMode mode)

DeviceLayer::PlatformMgr().AddEventHandler(OnPlatformEventWrapper, 0);

CHIP_ERROR err = Dnssd::ServiceAdvertiser::Instance().Init(&chip::DeviceLayer::InetLayer);
CHIP_ERROR err = Dnssd::ServiceAdvertiser::Instance().Init(&chip::DeviceLayer::InetLayer());
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to initialize advertiser: %s", chip::ErrorStr(err));
Expand Down
4 changes: 2 additions & 2 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint

// Init transport before operations with secure session mgr.
err = mTransports.Init(
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(IPAddressType::kIPv6).SetListenPort(mSecuredServicePort)
UdpListenParameters(&DeviceLayer::InetLayer()).SetAddressType(IPAddressType::kIPv6).SetListenPort(mSecuredServicePort)

#if INET_CONFIG_ENABLE_IPV4
,
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(IPAddressType::kIPv4).SetListenPort(mSecuredServicePort)
UdpListenParameters(&DeviceLayer::InetLayer()).SetAddressType(IPAddressType::kIPv4).SetListenPort(mSecuredServicePort)
#endif
#if CONFIG_NETWORK_LAYER_BLE
,
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ int main(int argc, char * argv[])

InitializeChip();

err = gTransportManager.Init(chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer)
err = gTransportManager.Init(chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer())
.SetAddressType(chip::Inet::IPAddressType::kIPv6)
.SetListenPort(IM_CLIENT_PORT));
SuccessOrExit(err);
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/integration/chip_im_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ int main(int argc, char * argv[])
InitializeChip();

err = gTransportManager.Init(
chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer).SetAddressType(chip::Inet::IPAddressType::kIPv6));
chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer()).SetAddressType(chip::Inet::IPAddressType::kIPv6));
SuccessOrExit(err);

err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gTransportManager, &gMessageCounterManager);
Expand Down
2 changes: 1 addition & 1 deletion src/controller/AbstractDnssdDiscoveryController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void AbstractDnssdDiscoveryController::OnNodeDiscoveryComplete(const chip::Dnssd
CHIP_ERROR AbstractDnssdDiscoveryController::SetUpNodeDiscovery()
{
#if CONFIG_DEVICE_LAYER
ReturnErrorOnFailure(mResolver->Init(&DeviceLayer::InetLayer));
ReturnErrorOnFailure(mResolver->Init(&DeviceLayer::InetLayer()));
#endif
mResolver->SetResolverDelegate(this);

Expand Down
2 changes: 1 addition & 1 deletion src/controller/CHIPDeviceControllerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
ReturnErrorOnFailure(DeviceLayer::PlatformMgr().InitChipStack());

stateParams.systemLayer = &DeviceLayer::SystemLayer();
stateParams.inetLayer = &DeviceLayer::InetLayer;
stateParams.inetLayer = &DeviceLayer::InetLayer();
#else
stateParams.systemLayer = params.systemLayer;
stateParams.inetLayer = params.inetLayer;
Expand Down
2 changes: 1 addition & 1 deletion src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ JNI_METHOD(jlong, newDeviceController)(JNIEnv * env, jobject self)
SuccessOrExit(err);

wrapper = AndroidDeviceControllerWrapper::AllocateNew(sJVM, self, kLocalDeviceId, &DeviceLayer::SystemLayer(),
&DeviceLayer::InetLayer, &err);
&DeviceLayer::InetLayer(), &err);
SuccessOrExit(err);

// Create and start the IO thread. Must be called after Controller()->Init
Expand Down
2 changes: 1 addition & 1 deletion src/controller/python/chip/discovery/NodeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ extern "C" ChipError::StorageType pychip_discovery_resolve(uint64_t fabricId, ui
CHIP_ERROR result = CHIP_NO_ERROR;

chip::python::ChipMainThreadScheduleAndWait([&] {
result = Resolver::Instance().Init(&chip::DeviceLayer::InetLayer);
result = Resolver::Instance().Init(&chip::DeviceLayer::InetLayer());
ReturnOnFailure(result);
Resolver::Instance().SetResolverDelegate(&gPythonResolverDelegate);

Expand Down
25 changes: 6 additions & 19 deletions src/include/platform/CHIPDeviceLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#if !CHIP_DEVICE_LAYER_NONE

#include <ble/BleLayer.h>
#include <inet/InetLayer.h>
#include <lib/core/CHIPCore.h>
#include <platform/CHIPDeviceError.h>
#include <platform/ConfigurationManager.h>
Expand All @@ -42,30 +43,16 @@
namespace chip {
namespace DeviceLayer {

namespace Internal {
extern chip::System::Layer * gSystemLayer;
} // namespace Internal
void SetSystemLayerForTesting(System::LayerImpl * layer);

struct ChipDeviceEvent;
extern Inet::InetLayer InetLayer;

inline chip::System::Layer & SystemLayer()
{
return *Internal::gSystemLayer;
}
// These functions are defined in src/platform/Globals.cpp
chip::Inet::InetLayer & InetLayer();
chip::System::Layer & SystemLayer();

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
inline chip::System::LayerSockets & SystemLayerSockets()
{
return *static_cast<chip::System::LayerSockets *>(Internal::gSystemLayer);
}
chip::System::LayerSockets & SystemLayerSockets();
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

inline void SetSystemLayerForTesting(System::Layer * layer)
{
Internal::gSystemLayer = layer;
}

} // namespace DeviceLayer
} // namespace chip

Expand Down
15 changes: 4 additions & 11 deletions src/include/platform/internal/GenericPlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ namespace DeviceLayer {

namespace Internal {

extern chip::System::Layer * gSystemLayer;
extern chip::System::LayerImpl gSystemLayerImpl;

extern CHIP_ERROR InitEntropy();

template <class ImplClass>
Expand Down Expand Up @@ -75,19 +72,15 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack()
SuccessOrExit(err);

// Initialize the CHIP system layer.
if (gSystemLayer == nullptr)
{
gSystemLayer = &gSystemLayerImpl;
}
err = gSystemLayer->Init();
err = SystemLayer().Init();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "SystemLayer initialization failed: %s", ErrorStr(err));
}
SuccessOrExit(err);

// Initialize the CHIP Inet layer.
err = InetLayer.Init(*gSystemLayer, nullptr);
err = InetLayer().Init(SystemLayer(), nullptr);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "InetLayer initialization failed: %s", ErrorStr(err));
Expand Down Expand Up @@ -137,15 +130,15 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_Shutdown()
{
CHIP_ERROR err;
ChipLogError(DeviceLayer, "Inet Layer shutdown");
err = InetLayer.Shutdown();
err = InetLayer().Shutdown();

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
ChipLogError(DeviceLayer, "BLE layer shutdown");
err = BLEMgr().GetBleLayer()->Shutdown();
#endif

ChipLogError(DeviceLayer, "System Layer shutdown");
err = gSystemLayer->Shutdown();
err = SystemLayer().Shutdown();

return err;
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/dnssd/platform/tests/TestPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void TestStub(nlTestSuite * inSuite, void * inContext)
// without an expected event.
ChipLogError(Discovery, "Test platform returns error correctly");
DiscoveryImplPlatform & mdnsPlatform = DiscoveryImplPlatform::GetInstance();
NL_TEST_ASSERT(inSuite, mdnsPlatform.Init(&DeviceLayer::InetLayer) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, mdnsPlatform.Init(&DeviceLayer::InetLayer()) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, mdnsPlatform.RemoveServices() == CHIP_NO_ERROR);
OperationalAdvertisingParameters params;
NL_TEST_ASSERT(inSuite, mdnsPlatform.Advertise(params) == CHIP_ERROR_UNEXPECTED_EVENT);
Expand All @@ -168,7 +168,7 @@ void TestOperational(nlTestSuite * inSuite, void * inContext)
ChipLogError(Discovery, "Test operational");
test::Reset();
DiscoveryImplPlatform & mdnsPlatform = DiscoveryImplPlatform::GetInstance();
NL_TEST_ASSERT(inSuite, mdnsPlatform.Init(&DeviceLayer::InetLayer) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, mdnsPlatform.Init(&DeviceLayer::InetLayer()) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, mdnsPlatform.RemoveServices() == CHIP_NO_ERROR);

operationalCall1.callType = test::CallType::kStart;
Expand All @@ -189,7 +189,7 @@ void TestCommissionableNode(nlTestSuite * inSuite, void * inContext)
ChipLogError(Discovery, "Test commissionable");
test::Reset();
DiscoveryImplPlatform & mdnsPlatform = DiscoveryImplPlatform::GetInstance();
NL_TEST_ASSERT(inSuite, mdnsPlatform.Init(&DeviceLayer::InetLayer) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, mdnsPlatform.Init(&DeviceLayer::InetLayer()) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, mdnsPlatform.RemoveServices() == CHIP_NO_ERROR);

commissionableSmall.callType = test::CallType::kStart;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/shell/commands/Dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ CHIP_ERROR DnsHandler(int argc, char ** argv)
return CHIP_NO_ERROR;
}

Dnssd::Resolver::Instance().Init(&DeviceLayer::InetLayer);
Dnssd::Resolver::Instance().Init(&DeviceLayer::InetLayer());
Dnssd::Resolver::Instance().SetResolverDelegate(&sDnsShellResolverDelegate);

return sShellDnsSubcommands.ExecCommand(argc, argv);
Expand Down
4 changes: 2 additions & 2 deletions src/messaging/tests/echo/echo_requester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ int main(int argc, char * argv[])

if (gUseTCP)
{
err = gTCPManager.Init(chip::Transport::TcpListenParameters(&chip::DeviceLayer::InetLayer)
err = gTCPManager.Init(chip::Transport::TcpListenParameters(&chip::DeviceLayer::InetLayer())
.SetAddressType(chip::Inet::IPAddressType::kIPv6)
.SetListenPort(ECHO_CLIENT_PORT));
SuccessOrExit(err);
Expand All @@ -238,7 +238,7 @@ int main(int argc, char * argv[])
}
else
{
err = gUDPManager.Init(chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer)
err = gUDPManager.Init(chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer())
.SetAddressType(chip::Inet::IPAddressType::kIPv6)
.SetListenPort(ECHO_CLIENT_PORT));
SuccessOrExit(err);
Expand Down
4 changes: 2 additions & 2 deletions src/messaging/tests/echo/echo_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int main(int argc, char * argv[])

if (useTCP)
{
err = gTCPManager.Init(chip::Transport::TcpListenParameters(&chip::DeviceLayer::InetLayer)
err = gTCPManager.Init(chip::Transport::TcpListenParameters(&chip::DeviceLayer::InetLayer())
#if INET_CONFIG_ENABLE_IPV4
.SetAddressType(chip::Inet::IPAddressType::kIPv4)
#else
Expand All @@ -98,7 +98,7 @@ int main(int argc, char * argv[])
else
{
err = gUDPManager.Init(
chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer).SetAddressType(chip::Inet::IPAddressType::kIPv6));
chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer()).SetAddressType(chip::Inet::IPAddressType::kIPv6));
SuccessOrExit(err);

err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gUDPManager, &gMessageCounterManager);
Expand Down
39 changes: 34 additions & 5 deletions src/platform/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,43 @@
namespace chip {
namespace DeviceLayer {

chip::Inet::InetLayer InetLayer;
chip::Inet::InetLayer & InetLayer()
{
static chip::Inet::InetLayer gInetLayer;
return gInetLayer;
}

namespace Internal {
chip::System::LayerImpl * gMockedSystemLayer = nullptr;

chip::System::Layer * gSystemLayer = nullptr;
chip::System::LayerImpl gSystemLayerImpl;
const char * const TAG = "CHIP[DL]";
void SetSystemLayerForTesting(System::LayerImpl * layer)
{
gMockedSystemLayer = layer;
}

chip::System::LayerImpl & SystemLayerImpl()
{
if (gMockedSystemLayer != nullptr)
return *gMockedSystemLayer;

static chip::System::LayerImpl gSystemLayerImpl;
return gSystemLayerImpl;
}

chip::System::Layer & SystemLayer()
{
return SystemLayerImpl();
}

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
chip::System::LayerSockets & SystemLayerSockets()
{
return SystemLayerImpl();
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

namespace Internal {
const char * const TAG = "CHIP[DL]";
} // namespace Internal

} // namespace DeviceLayer
} // namespace chip

0 comments on commit 1569644

Please sign in to comment.