Skip to content

Commit

Permalink
Enable System timers with the NW backend (project-chip#1434)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored Jul 4, 2020
1 parent 042cf32 commit 8fed11f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
13 changes: 11 additions & 2 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ CHIP_ERROR ChipDeviceController::GetLayers(Layer ** systemLayer, InetLayer ** in

void ChipDeviceController::ServiceEvents()
{
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
if (mState != kState_Initialized)
{
return;
}

// Set the select timeout to 100ms
struct timeval aSleepTime;
aSleepTime.tv_sec = 0;
Expand All @@ -299,10 +299,16 @@ void ChipDeviceController::ServiceEvents()
FD_ZERO(&exceptFDs);

if (mSystemLayer->State() == System::kLayerState_Initialized)
{
mSystemLayer->PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, aSleepTime);
}

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (mInetLayer->State == Inet::InetLayer::kState_Initialized)
{
mInetLayer->PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, aSleepTime);
}
#endif

int selectRes = select(numFDs, &readFDs, &writeFDs, &exceptFDs, &aSleepTime);
if (selectRes < 0)
Expand All @@ -316,11 +322,14 @@ void ChipDeviceController::ServiceEvents()
mSystemLayer->HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
}

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (mInetLayer->State == Inet::InetLayer::kState_Initialized)
{
mInetLayer->HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
}
#endif

#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
}

void ChipDeviceController::ClearRequestState()
Expand Down
22 changes: 11 additions & 11 deletions src/system/SystemLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
// Include system and language headers
#include <stddef.h>

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
Expand Down Expand Up @@ -97,23 +97,23 @@ Layer::Layer() : mLayerState(kLayerState_NotInitialized), mContext(NULL), mPlatf
this->mTimerComplete = false;
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
this->mWakePipeIn = 0;
this->mWakePipeOut = 0;

#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING
this->mHandleSelectThread = PTHREAD_NULL;
#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
}

Error Layer::Init(void * aContext)
{
Error lReturn;
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
int lPipeFDs[2];
int lOSReturn, lFlags;
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

RegisterLayerErrorFormatter();
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Expand All @@ -133,7 +133,7 @@ Error Layer::Init(void * aContext)
this->AddEventHandlerDelegate(sSystemEventHandlerDelegate);
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
// Create a Unix pipe to allow an arbitrary thread to wake the thread in the select loop.
lOSReturn = ::pipe(lPipeFDs);
VerifyOrExit(lOSReturn == 0, lReturn = chip::System::MapErrorPOSIX(errno));
Expand All @@ -149,7 +149,7 @@ Error Layer::Init(void * aContext)
lFlags = ::fcntl(this->mWakePipeOut, F_GETFL, 0);
lOSReturn = ::fcntl(this->mWakePipeOut, F_SETFL, lFlags | O_NONBLOCK);
VerifyOrExit(lOSReturn == 0, lReturn = chip::System::MapErrorPOSIX(errno));
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

this->mLayerState = kLayerState_Initialized;
this->mContext = aContext;
Expand All @@ -171,14 +171,14 @@ Error Layer::Shutdown()
lReturn = Platform::Layer::WillShutdown(*this, lContext);
SuccessOrExit(lReturn);

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
if (this->mWakePipeOut != -1)
{
::close(this->mWakePipeOut);
this->mWakePipeOut = -1;
this->mWakePipeIn = -1;
}
#endif
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

for (size_t i = 0; i < Timer::sPool.Size(); ++i)
{
Expand Down Expand Up @@ -598,7 +598,7 @@ void Layer::DispatchTimerCallbacks(const uint64_t kCurrentEpoch)
}
}

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

/**
* Prepare the sets of file descriptors for @p select() to work with.
Expand Down Expand Up @@ -753,7 +753,7 @@ void Layer::WakeSelect()
static_cast<void>(kIOResult);
}

#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

#if CHIP_SYSTEM_CONFIG_USE_LWIP
LwIPEventHandlerDelegate Layer::sSystemEventHandlerDelegate;
Expand Down
8 changes: 4 additions & 4 deletions src/system/SystemLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ class DLL_EXPORT Layer

Error ScheduleWork(TimerCompleteFunct aComplete, void * aAppState);

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
void PrepareSelect(int & aSetSize, fd_set * aReadSet, fd_set * aWriteSet, fd_set * aExceptionSet, struct timeval & aSleepTime);
void HandleSelectResult(int aSetSize, fd_set * aReadSet, fd_set * aWriteSet, fd_set * aExceptionSet);
void WakeSelect(void);
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

#if CHIP_SYSTEM_CONFIG_USE_LWIP
typedef Error (*EventHandler)(Object & aTarget, EventType aEventType, uintptr_t aArgument);
Expand Down Expand Up @@ -187,14 +187,14 @@ class DLL_EXPORT Layer
bool mTimerComplete;
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
int mWakePipeIn;
int mWakePipeOut;

#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING
pthread_t mHandleSelectThread;
#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

#if CHIP_SYSTEM_CONFIG_USE_LWIP
static Error HandleSystemLayerEvent(Object & aTarget, EventType aEventType, uintptr_t aArgument);
Expand Down

0 comments on commit 8fed11f

Please sign in to comment.