Skip to content

Commit 99f80de

Browse files
[nrf fromlist] Fix handling command handler status for group messages
The command handler status is added for the response purposes even in case of invoking commands targeted to the group. In case of using more than one endpoint on a single Matter node that was added to the same group, it leads to the application crash. The direct reason is a state check, that succeeds only for the first endpoint and fails for the subsequent calls. Added a new state flag, which informs if the command that is currently handled origins from groupcast communication or not. In case of targeting the group, application returns prematurely and do not try to add status. Fixes: #30472
1 parent 599f334 commit 99f80de

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/app/CommandHandler.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ Status CommandHandler::ProcessCommandDataIB(CommandDataIB::Parser & aCommandElem
251251
ConcreteCommandPath concretePath(0, 0, 0);
252252
TLV::TLVReader commandDataReader;
253253

254+
SetGroupRequest(false);
255+
254256
// NOTE: errors may occur before the concrete command path is even fully decoded.
255257

256258
err = aCommandElement.GetPath(&commandPath);
@@ -352,6 +354,7 @@ Status CommandHandler::ProcessGroupCommandDataIB(CommandDataIB::Parser & aComman
352354
Credentials::GroupDataProvider::GroupEndpoint mapping;
353355
Credentials::GroupDataProvider * groupDataProvider = Credentials::GetGroupDataProvider();
354356
Credentials::GroupDataProvider::EndpointIterator * iterator;
357+
SetGroupRequest(true);
355358

356359
err = aCommandElement.GetPath(&commandPath);
357360
VerifyOrReturnError(err == CHIP_NO_ERROR, Status::InvalidAction);
@@ -462,7 +465,8 @@ CHIP_ERROR CommandHandler::AddStatusInternal(const ConcreteCommandPath & aComman
462465
void CommandHandler::AddStatus(const ConcreteCommandPath & aCommandPath, const Protocols::InteractionModel::Status aStatus,
463466
const char * context)
464467
{
465-
468+
// Return prematurely in case of requests targeted to a group that should not add the status for response purposes.
469+
VerifyOrReturn(!IsGroupRequest());
466470
VerifyOrDie(FallibleAddStatus(aCommandPath, aStatus, context) == CHIP_NO_ERROR);
467471
}
468472

src/app/CommandHandler.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,16 @@ class CommandHandler : public Messaging::ExchangeDelegate
406406
return FinishCommand(/* aEndDataStruct = */ false);
407407
}
408408

409+
/**
410+
* Check whether the InvokeRequest we are handling is targeted to a group.
411+
*/
412+
bool IsGroupRequest() { return mGroupRequest; }
413+
414+
/**
415+
* Sets the state flag to keep the information that request we are handling is targeted to a group.
416+
*/
417+
void SetGroupRequest(bool isGroupRequest) { mGroupRequest = isGroupRequest; }
418+
409419
Messaging::ExchangeHolder mExchangeCtx;
410420
Callback * mpCallback = nullptr;
411421
InvokeResponseMessage::Builder mInvokeResponseBuilder;
@@ -416,7 +426,8 @@ class CommandHandler : public Messaging::ExchangeDelegate
416426

417427
bool mSentStatusResponse = false;
418428

419-
State mState = State::Idle;
429+
State mState = State::Idle;
430+
bool mGroupRequest = false;
420431
chip::System::PacketBufferTLVWriter mCommandMessageWriter;
421432
TLV::TLVWriter mBackupWriter;
422433
bool mBufferAllocated = false;

0 commit comments

Comments
 (0)