Skip to content

Commit 6090618

Browse files
yyzhong-grestyled-commitsbzbarsky-apple
authored
Remove dependency of InteractionModelEngine in CommandHandler (#31830)
* Remove dependency of InteractionModelEngine in CommandHandler * Address comments and fix tizen test * Fix tizen example * Fix segfault in CommandHandler::Handle::Get() * Change CommandHandler::Callback function name * Add virtual function in CommandHandler::Callback in CommandResponseSender * Add mpCallback null checks * Add weak reference to CommandHandler * Remove magic number from ImEngine * self clean up * Restyled by clang-format * Use IntrusiveList for weak references of the Handles * Update src/app/CommandHandler.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/app/CommandHandler.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/app/CommandHandler.h Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * compile fix after renaming --------- Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent a2792ff commit 6090618

4 files changed

+78
-43
lines changed

src/app/CommandHandler.cpp

+47-16
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <lib/core/CHIPConfig.h>
3636
#include <lib/core/TLVData.h>
3737
#include <lib/core/TLVUtilities.h>
38+
#include <lib/support/IntrusiveList.h>
3839
#include <lib/support/TypeTraits.h>
3940
#include <platform/LockTracker.h>
4041
#include <protocols/secure_channel/Constants.h>
@@ -58,6 +59,11 @@ CommandHandler::CommandHandler(TestOnlyOverrides & aTestOverride, Callback * apC
5859
}
5960
}
6061

62+
CommandHandler::~CommandHandler()
63+
{
64+
InvalidateHandles();
65+
}
66+
6167
CHIP_ERROR CommandHandler::AllocateBuffer()
6268
{
6369
// We should only allocate a buffer if we will be sending out a response.
@@ -266,23 +272,48 @@ void CommandHandler::Close()
266272
// reference is the stack shutting down, in which case Close() is not called. So the below check should always pass.
267273
VerifyOrDieWithMsg(mPendingWork == 0, DataManagement, "CommandHandler::Close() called with %u unfinished async work items",
268274
static_cast<unsigned int>(mPendingWork));
275+
InvalidateHandles();
269276

270277
if (mpCallback)
271278
{
272279
mpCallback->OnDone(*this);
273280
}
274281
}
275282

276-
void CommandHandler::IncrementHoldOff()
283+
void CommandHandler::AddToHandleList(Handle * apHandle)
284+
{
285+
mpHandleList.PushBack(apHandle);
286+
}
287+
288+
void CommandHandler::RemoveFromHandleList(Handle * apHandle)
289+
{
290+
VerifyOrDie(mpHandleList.Contains(apHandle));
291+
mpHandleList.Remove(apHandle);
292+
}
293+
294+
void CommandHandler::InvalidateHandles()
295+
{
296+
for (auto handle = mpHandleList.begin(); handle != mpHandleList.end(); ++handle)
297+
{
298+
handle->Invalidate();
299+
}
300+
}
301+
302+
void CommandHandler::IncrementHoldOff(Handle * apHandle)
277303
{
278304
mPendingWork++;
305+
AddToHandleList(apHandle);
279306
}
280307

281-
void CommandHandler::DecrementHoldOff()
308+
void CommandHandler::DecrementHoldOff(Handle * apHandle)
282309
{
310+
283311
mPendingWork--;
284312
ChipLogDetail(DataManagement, "Decreasing reference count for CommandHandler, remaining %u",
285313
static_cast<unsigned int>(mPendingWork));
314+
315+
RemoveFromHandleList(apHandle);
316+
286317
if (mPendingWork != 0)
287318
{
288319
return;
@@ -771,35 +802,35 @@ FabricIndex CommandHandler::GetAccessingFabricIndex() const
771802
return mpResponder->GetAccessingFabricIndex();
772803
}
773804

805+
void CommandHandler::Handle::Init(CommandHandler * handler)
806+
{
807+
if (handler != nullptr)
808+
{
809+
handler->IncrementHoldOff(this);
810+
mpHandler = handler;
811+
}
812+
}
813+
774814
CommandHandler * CommandHandler::Handle::Get()
775815
{
776816
// Not safe to work with CommandHandler in parallel with other Matter work.
777817
assertChipStackLockedByCurrentThread();
778818

779-
return (mMagic == InteractionModelEngine::GetInstance()->GetMagicNumber()) ? mpHandler : nullptr;
819+
return mpHandler;
780820
}
781821

782822
void CommandHandler::Handle::Release()
783823
{
784824
if (mpHandler != nullptr)
785825
{
786-
if (mMagic == InteractionModelEngine::GetInstance()->GetMagicNumber())
787-
{
788-
mpHandler->DecrementHoldOff();
789-
}
790-
mpHandler = nullptr;
791-
mMagic = 0;
826+
mpHandler->DecrementHoldOff(this);
827+
Invalidate();
792828
}
793829
}
794830

795-
CommandHandler::Handle::Handle(CommandHandler * handle)
831+
CommandHandler::Handle::Handle(CommandHandler * handler)
796832
{
797-
if (handle != nullptr)
798-
{
799-
handle->IncrementHoldOff();
800-
mpHandler = handle;
801-
mMagic = InteractionModelEngine::GetInstance()->GetMagicNumber();
802-
}
833+
Init(handler);
803834
}
804835

805836
CHIP_ERROR CommandHandler::FinalizeInvokeResponseMessageAndPrepareNext()

src/app/CommandHandler.h

+31-14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <lib/support/BitFlags.h>
4242
#include <lib/support/CodeUtils.h>
4343
#include <lib/support/DLLUtil.h>
44+
#include <lib/support/IntrusiveList.h>
4445
#include <lib/support/Scoped.h>
4546
#include <lib/support/logging/CHIPLogging.h>
4647
#include <messaging/ExchangeHolder.h>
@@ -110,29 +111,26 @@ class CommandHandler
110111
* The Invoke Response will not be sent until all outstanding Handles have
111112
* been destroyed or have had Release called.
112113
*/
113-
class Handle
114+
class Handle : public IntrusiveListNodeBase<>
114115
{
115116
public:
116117
Handle() {}
117118
Handle(const Handle & handle) = delete;
118119
Handle(Handle && handle)
119120
{
120-
mpHandler = handle.mpHandler;
121-
mMagic = handle.mMagic;
122-
handle.mpHandler = nullptr;
123-
handle.mMagic = 0;
121+
Init(handle.mpHandler);
122+
handle.Release();
124123
}
125124
Handle(decltype(nullptr)) {}
126-
Handle(CommandHandler * handle);
125+
Handle(CommandHandler * handler);
127126
~Handle() { Release(); }
128127

129128
Handle & operator=(Handle && handle)
130129
{
131130
Release();
132-
mpHandler = handle.mpHandler;
133-
mMagic = handle.mMagic;
134-
handle.mpHandler = nullptr;
135-
handle.mMagic = 0;
131+
Init(handle.mpHandler);
132+
133+
handle.Release();
136134
return *this;
137135
}
138136

@@ -143,16 +141,19 @@ class CommandHandler
143141
}
144142

145143
/**
146-
* Get the CommandHandler object it holds. Get() may return a nullptr if the CommandHandler object is holds is no longer
144+
* Get the CommandHandler object it holds. Get() may return a nullptr if the CommandHandler object it holds is no longer
147145
* valid.
148146
*/
149147
CommandHandler * Get();
150148

151149
void Release();
152150

151+
void Invalidate() { mpHandler = nullptr; }
152+
153153
private:
154+
void Init(CommandHandler * handler);
155+
154156
CommandHandler * mpHandler = nullptr;
155-
uint32_t mMagic = 0;
156157
};
157158

158159
// Previously we kept adding arguments with default values individually as parameters. This is because there
@@ -191,6 +192,14 @@ class CommandHandler
191192
*/
192193
CommandHandler(Callback * apCallback);
193194

195+
/*
196+
* Destructor.
197+
*
198+
* The call will also invalidate all Handles created for this CommandHandler.
199+
*
200+
*/
201+
~CommandHandler();
202+
194203
/*
195204
* Constructor to override the number of supported paths per invoke and command responder.
196205
*
@@ -525,13 +534,13 @@ class CommandHandler
525534
* Users should use CommandHandler::Handle for management the lifespan of the CommandHandler.
526535
* DefRef should be released in reasonable time, and Close() should only be called when the refcount reached 0.
527536
*/
528-
void IncrementHoldOff();
537+
void IncrementHoldOff(Handle * apHandle);
529538

530539
/**
531540
* DecrementHoldOff is used by CommandHandler::Handle for decreasing the refcount of the CommandHandler.
532541
* When refcount reached 0, CommandHandler will send the response to the peer and shutdown.
533542
*/
534-
void DecrementHoldOff();
543+
void DecrementHoldOff(Handle * apHandle);
535544

536545
/*
537546
* Allocates a packet buffer used for encoding an invoke response payload.
@@ -672,12 +681,20 @@ class CommandHandler
672681

673682
size_t MaxPathsPerInvoke() const { return mMaxPathsPerInvoke; }
674683

684+
void AddToHandleList(Handle * handle);
685+
686+
void RemoveFromHandleList(Handle * handle);
687+
688+
void InvalidateHandles();
689+
675690
bool TestOnlyIsInIdleState() const { return mState == State::Idle; }
676691

677692
Callback * mpCallback = nullptr;
678693
InvokeResponseMessage::Builder mInvokeResponseBuilder;
679694
TLV::TLVType mDataElementContainerType = TLV::kTLVType_NotSpecified;
680695
size_t mPendingWork = 0;
696+
/* List to store all currently-outstanding Handles for this Command Handler.*/
697+
IntrusiveList<Handle> mpHandleList;
681698

682699
chip::System::PacketBufferTLVWriter mCommandMessageWriter;
683700
TLV::TLVWriter mBackupWriter;

src/app/InteractionModelEngine.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ CHIP_ERROR InteractionModelEngine::Init(Messaging::ExchangeManager * apExchangeM
8585
ReturnErrorOnFailure(mpExchangeMgr->RegisterUnsolicitedMessageHandlerForProtocol(Protocols::InteractionModel::Id, this));
8686

8787
mReportingEngine.Init();
88-
mMagic++;
8988

9089
StatusIB::RegisterErrorFormatter();
9190

@@ -111,9 +110,6 @@ void InteractionModelEngine::Shutdown()
111110

112111
mCommandHandlerList = nullptr;
113112

114-
// Increase magic number to invalidate all Handle-s.
115-
mMagic++;
116-
117113
mCommandResponderObjs.ReleaseAll();
118114

119115
mTimedHandlers.ForEachActiveObject([this](TimedHandler * obj) -> Loop {

src/app/InteractionModelEngine.h

-9
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,6 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
192192
*/
193193
WriteHandler * ActiveWriteHandlerAt(unsigned int aIndex);
194194

195-
/**
196-
* The Magic number of this InteractionModelEngine, the magic number is set during Init()
197-
*/
198-
uint32_t GetMagicNumber() const { return mMagic; }
199-
200195
reporting::Engine & GetReportingEngine() { return mReportingEngine; }
201196

202197
reporting::ReportScheduler * GetReportScheduler() { return mReportScheduler; }
@@ -706,10 +701,6 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
706701
CASESessionManager * mpCASESessionMgr = nullptr;
707702

708703
SubscriptionResumptionStorage * mpSubscriptionResumptionStorage = nullptr;
709-
710-
// A magic number for tracking values between stack Shutdown()-s and Init()-s.
711-
// An ObjectHandle is valid iff. its magic equals to this one.
712-
uint32_t mMagic = 0;
713704
};
714705

715706
} // namespace app

0 commit comments

Comments
 (0)