Skip to content

Commit

Permalink
Move socket watch APIs into System::Layer (#9086)
Browse files Browse the repository at this point in the history
* Move socket watch APIs into System::Layer

#### Problem

Transitionally, `System::Layer` operations are provided by a
configuration-specific `WatchableEventManager`, but they are not yet
sufficiently aligned to convert to abstract and implementation classes.

Part of #7715 Virtualize System and Inet interfaces

#### Change overview

- Move socket event watch APIs into `System::Layer`.
- Remove `WatchableSocket` and its include-file hack that allowed
  implementation-specific state to be contained directly in EndPoints.
- Revise the select() implementation to use an internal pool for watch
  state in place of the EndPoint-embedded WatchableSocket.
- Revise the libevent sample implementation to use libevent timers
  directly instead of the System::Timer pool. The libevent
  implementation uses STL containers with heap-allocated state for the
  sake of being different from the select()-based implementation.

#### Testing

CI; no change to functionality should is expected.

* restyle

* fix GetCommandExitStatus race

* Enforce use of API via System::Layer

* also include dispatch case

* add LwIP case

* restyle
  • Loading branch information
kpschoedel authored Aug 23, 2021
1 parent 9f64655 commit 82cf1b1
Show file tree
Hide file tree
Showing 60 changed files with 1,181 additions and 1,262 deletions.
14 changes: 9 additions & 5 deletions examples/chip-tool/commands/common/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ int Commands::Run(int argc, char ** argv)
chip::DeviceLayer::PlatformMgr().RunEventLoop();
#endif // !CONFIG_USE_SEPARATE_EVENTLOOP

if (command)
{
err = command->GetCommandExitStatus();
}

exit:
if (err != CHIP_NO_ERROR)
{
Expand All @@ -135,6 +130,15 @@ int Commands::Run(int argc, char ** argv)
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
#endif // CONFIG_USE_SEPARATE_EVENTLOOP

if (command)
{
err = command->GetCommandExitStatus();
if (err != CHIP_NO_ERROR)
{
ChipLogError(chipTool, "Run command failure: %s", chip::ErrorStr(err));
}
}

if (command)
{
command->Shutdown();
Expand Down
1 change: 0 additions & 1 deletion examples/minimal-mdns/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <support/CHIPArgParser.hpp>
#include <support/CHIPMem.h>
#include <system/SystemPacketBuffer.h>
#include <system/SystemTimer.h>

#include "AllInterfaceListener.h"
#include "PacketReporter.h"
Expand Down
1 change: 0 additions & 1 deletion examples/minimal-mdns/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <support/CHIPArgParser.hpp>
#include <support/CHIPMem.h>
#include <system/SystemPacketBuffer.h>
#include <system/SystemTimer.h>

#include "AllInterfaceListener.h"
#include "PacketReporter.h"
Expand Down
5 changes: 2 additions & 3 deletions examples/platform/nrfconnect/util/test/TestInetCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include <support/CHIPMem.h>
#include <support/ErrorStr.h>
#include <support/ScopedBuffer.h>
#include <system/SystemTimer.h>

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#include <arpa/inet.h>
Expand Down Expand Up @@ -114,7 +113,7 @@ void ServiceEvents(struct ::timeval & aSleepTime)
FD_ZERO(&exceptFDs);

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (gSystemLayer.State() == System::kLayerState_Initialized)
if (gSystemLayer.State() == System::LayerState::kInitialized)
gSystemLayer.PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, aSleepTime);
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

Expand All @@ -131,7 +130,7 @@ void ServiceEvents(struct ::timeval & aSleepTime)
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

if (gSystemLayer.State() == System::kLayerState_Initialized)
if (gSystemLayer.State() == System::LayerState::kInitialized)
{

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Expand Down
1 change: 0 additions & 1 deletion src/app/EventManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>
#include <support/logging/CHIPLogging.h>
#include <system/SystemTimer.h>

using namespace chip::TLV;

Expand Down
1 change: 0 additions & 1 deletion src/app/tests/TestReadInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include <support/ErrorStr.h>
#include <support/UnitTestRegistration.h>
#include <system/SystemPacketBuffer.h>
#include <system/SystemTimer.h>
#include <system/TLVPacketBufferBackingStore.h>
#include <transport/SecureSessionMgr.h>
#include <transport/raw/UDP.h>
Expand Down
1 change: 0 additions & 1 deletion src/app/tests/integration/MockEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <protocols/secure_channel/PASESession.h>
#include <support/ErrorStr.h>
#include <system/SystemPacketBuffer.h>
#include <system/SystemTimer.h>
#include <transport/SecureSessionMgr.h>

static uint64_t kLivenessDeviceStatus = chip::TLV::ContextTag(1);
Expand Down
1 change: 0 additions & 1 deletion src/app/util/af-event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <app/util/attribute-storage.h>

#include <platform/CHIPDeviceLayer.h>
#include <system/SystemTimer.h>

#define EMBER_MAX_EVENT_CONTROL_DELAY_MS (UINT32_MAX / 2)
#define EMBER_MAX_EVENT_CONTROL_DELAY_QS (EMBER_MAX_EVENT_CONTROL_DELAY_MS >> 8)
Expand Down
13 changes: 0 additions & 13 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,19 +468,6 @@ CHIP_ERROR DeviceController::ServiceEvents()
return CHIP_NO_ERROR;
}

CHIP_ERROR DeviceController::ServiceEventSignal()
{
VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE);

#if CONFIG_DEVICE_LAYER && (CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK)
DeviceLayer::SystemLayer.WatchableEventsManager().Signal();
#else
ReturnErrorOnFailure(CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE);
#endif // CONFIG_DEVICE_LAYER && (CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK)

return CHIP_NO_ERROR;
}

CHIP_ERROR DeviceController::GetFabricId(uint64_t & fabricId)
{
Transport::FabricInfo * fabric = mFabrics.FindFabricWithIndex(mFabricIndex);
Expand Down
8 changes: 0 additions & 8 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,6 @@ class DLL_EXPORT DeviceController : public Messaging::ExchangeDelegate,
*/
CHIP_ERROR ServiceEvents();

/**
* @brief
* Allow the CHIP Stack to process any pending events
* This can be called in an event handler loop to trigger callbacks within the CHIP stack
* @return CHIP_ERROR The return status
*/
CHIP_ERROR ServiceEventSignal();

/**
* @brief Get the Fabric ID assigned to the device.
*
Expand Down
6 changes: 6 additions & 0 deletions src/include/platform/PlatformManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include <platform/CHIPDeviceEvent.h>
#include <system/SystemLayer.h>

#if CHIP_SYSTEM_CONFIG_USE_LWIP
#include <system/LwIPEventSupport.h>
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

namespace chip {

namespace DeviceLayer {
Expand Down Expand Up @@ -176,7 +180,9 @@ class PlatformManager
friend class Internal::GenericThreadStackManagerImpl_OpenThread_LwIP;
template <class>
friend class Internal::GenericConfigurationManagerImpl;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
friend class System::PlatformEventing;
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

/*
* PostEvent can be called safely on any thread without locking the stack.
Expand Down
2 changes: 1 addition & 1 deletion src/inet/EndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState)
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
(void) mSocket.Init(aInetLayer.SystemLayer()->WatchableEventsManager());
mSocket = INET_INVALID_SOCKET_FD;
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
}

Expand Down
7 changes: 4 additions & 3 deletions src/inet/EndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <support/DLLUtil.h>

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#include <system/WatchableSocket.h>
#include <system/SocketEvents.h>
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
Expand Down Expand Up @@ -97,8 +97,9 @@ class DLL_EXPORT EndPointBasis : public InetLayerBasis
#endif

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
System::WatchableSocket mSocket; /**< Encapsulated socket descriptor. */
int mSocket; /**< Encapsulated socket descriptor. */
IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */
System::SocketWatchToken mWatch; /**< Socket event watcher */
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

#if CHIP_SYSTEM_CONFIG_USE_LWIP
Expand Down Expand Up @@ -145,7 +146,7 @@ inline bool EndPointBasis::IsNetworkFrameworkEndPoint(void) const
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
inline bool EndPointBasis::IsSocketsEndPoint() const
{
return mSocket.HasFD();
return mSocket != INET_INVALID_SOCKET_FD;
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

Expand Down
Loading

0 comments on commit 82cf1b1

Please sign in to comment.