Matter SDK Coverage Report
Current view: top level - app - CommandHandlerImpl.cpp (source / functions) Coverage Total Hit
Test: SHA:b879ecb8e99e175eea0a293a888bda853da2b19c Lines: 72.7 % 477 347
Test Date: 2025-01-17 19:00:11 Functions: 84.8 % 46 39

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020 Project CHIP Authors
       4              :  *    All rights reserved.
       5              :  *
       6              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       7              :  *    you may not use this file except in compliance with the License.
       8              :  *    You may obtain a copy of the License at
       9              :  *
      10              :  *        http://www.apache.org/licenses/LICENSE-2.0
      11              :  *
      12              :  *    Unless required by applicable law or agreed to in writing, software
      13              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      14              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15              :  *    See the License for the specific language governing permissions and
      16              :  *    limitations under the License.
      17              :  */
      18              : #include <app/CommandHandlerImpl.h>
      19              : 
      20              : #include <access/AccessControl.h>
      21              : #include <access/SubjectDescriptor.h>
      22              : #include <app-common/zap-generated/cluster-objects.h>
      23              : #include <app/MessageDef/StatusIB.h>
      24              : #include <app/RequiredPrivilege.h>
      25              : #include <app/StatusResponse.h>
      26              : #include <app/data-model-provider/OperationTypes.h>
      27              : #include <app/util/MatterCallbacks.h>
      28              : #include <credentials/GroupDataProvider.h>
      29              : #include <lib/core/CHIPConfig.h>
      30              : #include <lib/core/TLVData.h>
      31              : #include <lib/core/TLVUtilities.h>
      32              : #include <lib/support/IntrusiveList.h>
      33              : #include <lib/support/TypeTraits.h>
      34              : #include <messaging/ExchangeContext.h>
      35              : #include <platform/LockTracker.h>
      36              : #include <protocols/interaction_model/StatusCode.h>
      37              : #include <protocols/secure_channel/Constants.h>
      38              : 
      39              : namespace chip {
      40              : namespace app {
      41              : using Status = Protocols::InteractionModel::Status;
      42              : 
      43           57 : CommandHandlerImpl::CommandHandlerImpl(Callback * apCallback) : mpCallback(apCallback), mSuppressResponse(false) {}
      44              : 
      45            8 : CommandHandlerImpl::CommandHandlerImpl(TestOnlyOverrides & aTestOverride, Callback * apCallback) : CommandHandlerImpl(apCallback)
      46              : {
      47            8 :     if (aTestOverride.commandPathRegistry)
      48              :     {
      49            8 :         mMaxPathsPerInvoke   = aTestOverride.commandPathRegistry->MaxSize();
      50            8 :         mCommandPathRegistry = aTestOverride.commandPathRegistry;
      51              :     }
      52            8 :     if (aTestOverride.commandResponder)
      53              :     {
      54            8 :         SetExchangeInterface(aTestOverride.commandResponder);
      55              :     }
      56            8 : }
      57              : 
      58           57 : CommandHandlerImpl::~CommandHandlerImpl()
      59              : {
      60           57 :     InvalidateHandles();
      61           57 : }
      62              : 
      63           70 : CHIP_ERROR CommandHandlerImpl::AllocateBuffer()
      64              : {
      65              :     // We should only allocate a buffer if we will be sending out a response.
      66           70 :     VerifyOrReturnError(ResponsesAccepted(), CHIP_ERROR_INCORRECT_STATE);
      67              : 
      68           70 :     if (!mBufferAllocated)
      69              :     {
      70           48 :         mCommandMessageWriter.Reset();
      71              : 
      72           48 :         const size_t commandBufferMaxSize = mpResponder->GetCommandResponseMaxBufferSize();
      73           48 :         auto commandPacket                = System::PacketBufferHandle::New(commandBufferMaxSize);
      74              : 
      75           48 :         VerifyOrReturnError(!commandPacket.IsNull(), CHIP_ERROR_NO_MEMORY);
      76              : 
      77           48 :         mCommandMessageWriter.Init(std::move(commandPacket));
      78           48 :         ReturnErrorOnFailure(mInvokeResponseBuilder.InitWithEndBufferReserved(&mCommandMessageWriter));
      79              : 
      80           48 :         if (mReserveSpaceForMoreChunkMessages)
      81              :         {
      82           10 :             ReturnErrorOnFailure(mInvokeResponseBuilder.ReserveSpaceForMoreChunkedMessages());
      83              :         }
      84              : 
      85              :         // Sending an InvokeResponse to an InvokeResponse is going to be removed from the spec soon.
      86              :         // It was never implemented in the SDK, and there are no command responses that expect a
      87              :         // command response. This means we will never receive an InvokeResponse Message in response
      88              :         // to an InvokeResponse Message that we are sending. This means that the only response
      89              :         // we are expecting to receive in response to an InvokeResponse Message that we are
      90              :         // sending-out is a status when we are chunking multiple responses. As a result, to satisfy the
      91              :         // condition that we don't set SuppressResponse to true while also setting
      92              :         // MoreChunkedMessages to true, we are hardcoding the value to false here.
      93           48 :         mInvokeResponseBuilder.SuppressResponse(/* aSuppressResponse = */ false);
      94           48 :         ReturnErrorOnFailure(mInvokeResponseBuilder.GetError());
      95              : 
      96           48 :         mInvokeResponseBuilder.CreateInvokeResponses(/* aReserveEndBuffer = */ true);
      97           48 :         ReturnErrorOnFailure(mInvokeResponseBuilder.GetError());
      98              : 
      99           48 :         mBufferAllocated = true;
     100           48 :         MoveToState(State::NewResponseMessage);
     101           48 :     }
     102              : 
     103           70 :     return CHIP_NO_ERROR;
     104              : }
     105              : 
     106           39 : Status CommandHandlerImpl::OnInvokeCommandRequest(CommandHandlerExchangeInterface & commandResponder,
     107              :                                                   System::PacketBufferHandle && payload, bool isTimedInvoke)
     108              : {
     109           39 :     VerifyOrDieWithMsg(mState == State::Idle, DataManagement, "state should be Idle");
     110              : 
     111           39 :     SetExchangeInterface(&commandResponder);
     112              : 
     113              :     // Using RAII here: if this is the only handle remaining, DecrementHoldOff will
     114              :     // call the CommandHandlerImpl::OnDone callback when this function returns.
     115           39 :     Handle workHandle(this);
     116              : 
     117           39 :     Status status = ProcessInvokeRequest(std::move(payload), isTimedInvoke);
     118           39 :     mGoneAsync    = true;
     119           78 :     return status;
     120           39 : }
     121              : 
     122           20 : CHIP_ERROR CommandHandlerImpl::TryAddResponseData(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
     123              :                                                   const DataModel::EncodableToTLV & aEncodable)
     124              : {
     125           20 :     ConcreteCommandPath responseCommandPath = { aRequestCommandPath.mEndpointId, aRequestCommandPath.mClusterId,
     126           20 :                                                 aResponseCommandId };
     127              : 
     128           20 :     InvokeResponseParameters prepareParams(aRequestCommandPath);
     129           20 :     prepareParams.SetStartOrEndDataStruct(false);
     130              : 
     131              :     {
     132           20 :         ScopedChange<bool> internalCallToAddResponse(mInternalCallToAddResponseData, true);
     133           20 :         ReturnErrorOnFailure(PrepareInvokeResponseCommand(responseCommandPath, prepareParams));
     134           20 :     }
     135              : 
     136           18 :     TLV::TLVWriter * writer = GetCommandDataIBTLVWriter();
     137           18 :     VerifyOrReturnError(writer != nullptr, CHIP_ERROR_INCORRECT_STATE);
     138           18 :     ReturnErrorOnFailure(aEncodable.EncodeTo(*writer, TLV::ContextTag(CommandDataIB::Tag::kFields)));
     139           16 :     return FinishCommand(/* aEndDataStruct = */ false);
     140              : }
     141              : 
     142           19 : CHIP_ERROR CommandHandlerImpl::AddResponseData(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
     143              :                                                const DataModel::EncodableToTLV & aEncodable)
     144              : {
     145              :     // Return early when response should not be sent out.
     146           19 :     VerifyOrReturnValue(ResponsesAccepted(), CHIP_NO_ERROR);
     147           36 :     return TryAddingResponse(
     148           38 :         [&]() -> CHIP_ERROR { return TryAddResponseData(aRequestCommandPath, aResponseCommandId, aEncodable); });
     149              : }
     150              : 
     151           38 : CHIP_ERROR CommandHandlerImpl::ValidateInvokeRequestMessageAndBuildRegistry(InvokeRequestMessage::Parser & invokeRequestMessage)
     152              : {
     153           38 :     CHIP_ERROR err          = CHIP_NO_ERROR;
     154           38 :     size_t commandCount     = 0;
     155           38 :     bool commandRefExpected = false;
     156           38 :     InvokeRequests::Parser invokeRequests;
     157              : 
     158           38 :     ReturnErrorOnFailure(invokeRequestMessage.GetInvokeRequests(&invokeRequests));
     159           38 :     TLV::TLVReader invokeRequestsReader;
     160           38 :     invokeRequests.GetReader(&invokeRequestsReader);
     161              : 
     162           38 :     ReturnErrorOnFailure(TLV::Utilities::Count(invokeRequestsReader, commandCount, false /* recurse */));
     163              : 
     164              :     // If this is a GroupRequest the only thing to check is that there is only one
     165              :     // CommandDataIB.
     166           38 :     if (IsGroupRequest())
     167              :     {
     168            0 :         VerifyOrReturnError(commandCount == 1, CHIP_ERROR_INVALID_ARGUMENT);
     169            0 :         return CHIP_NO_ERROR;
     170              :     }
     171              :     // While technically any commandCount == 1 should already be unique and does not need
     172              :     // any further validation, we do need to read and populate the registry to help
     173              :     // in building the InvokeResponse.
     174              : 
     175           38 :     VerifyOrReturnError(commandCount <= MaxPathsPerInvoke(), CHIP_ERROR_INVALID_ARGUMENT);
     176              : 
     177              :     // If there is more than one CommandDataIB, spec states that CommandRef must be provided.
     178           36 :     commandRefExpected = commandCount > 1;
     179              : 
     180           72 :     while (CHIP_NO_ERROR == (err = invokeRequestsReader.Next()))
     181              :     {
     182           40 :         VerifyOrReturnError(TLV::AnonymousTag() == invokeRequestsReader.GetTag(), CHIP_ERROR_INVALID_ARGUMENT);
     183           38 :         CommandDataIB::Parser commandData;
     184           38 :         ReturnErrorOnFailure(commandData.Init(invokeRequestsReader));
     185              : 
     186              :         // First validate that we can get a ConcreteCommandPath.
     187           38 :         CommandPathIB::Parser commandPath;
     188           38 :         ConcreteCommandPath concretePath(0, 0, 0);
     189           38 :         ReturnErrorOnFailure(commandData.GetPath(&commandPath));
     190           38 :         ReturnErrorOnFailure(commandPath.GetConcreteCommandPath(concretePath));
     191              : 
     192              :         // Grab the CommandRef if there is one, and validate that it's there when it
     193              :         // has to be.
     194           37 :         std::optional<uint16_t> commandRef;
     195              :         uint16_t ref;
     196           37 :         err = commandData.GetRef(&ref);
     197           37 :         VerifyOrReturnError(err == CHIP_NO_ERROR || err == CHIP_END_OF_TLV, err);
     198           37 :         if (err == CHIP_END_OF_TLV && commandRefExpected)
     199              :         {
     200            0 :             return CHIP_ERROR_INVALID_ARGUMENT;
     201              :         }
     202           37 :         if (err == CHIP_NO_ERROR)
     203              :         {
     204            5 :             commandRef.emplace(ref);
     205              :         }
     206              : 
     207              :         // Adding can fail if concretePath is not unique, or if commandRef is a value
     208              :         // and is not unique, or if we have already added more paths than we support.
     209           37 :         ReturnErrorOnFailure(GetCommandPathRegistry().Add(concretePath, commandRef));
     210              :     }
     211              : 
     212              :     // It's OK/expected to have reached the end of the container without failure.
     213           34 :     if (CHIP_END_OF_TLV == err)
     214              :     {
     215           34 :         err = CHIP_NO_ERROR;
     216              :     }
     217           34 :     ReturnErrorOnFailure(err);
     218           34 :     return invokeRequestMessage.ExitContainer();
     219              : }
     220              : 
     221           41 : Status CommandHandlerImpl::ProcessInvokeRequest(System::PacketBufferHandle && payload, bool isTimedInvoke)
     222              : {
     223           41 :     CHIP_ERROR err = CHIP_NO_ERROR;
     224           41 :     System::PacketBufferTLVReader reader;
     225           41 :     InvokeRequestMessage::Parser invokeRequestMessage;
     226           41 :     InvokeRequests::Parser invokeRequests;
     227           41 :     reader.Init(std::move(payload));
     228           41 :     VerifyOrReturnError(invokeRequestMessage.Init(reader) == CHIP_NO_ERROR, Status::InvalidAction);
     229              : #if CHIP_CONFIG_IM_PRETTY_PRINT
     230           40 :     invokeRequestMessage.PrettyPrint();
     231              : #endif
     232           40 :     VerifyOrDie(mpResponder);
     233           40 :     if (mpResponder->GetGroupId().HasValue())
     234              :     {
     235            0 :         SetGroupRequest(true);
     236              :     }
     237              : 
     238              :     // When updating this code, please remember to make corresponding changes to TestOnlyInvokeCommandRequestWithFaultsInjected.
     239           40 :     VerifyOrReturnError(invokeRequestMessage.GetSuppressResponse(&mSuppressResponse) == CHIP_NO_ERROR, Status::InvalidAction);
     240           40 :     VerifyOrReturnError(invokeRequestMessage.GetTimedRequest(&mTimedRequest) == CHIP_NO_ERROR, Status::InvalidAction);
     241           40 :     VerifyOrReturnError(invokeRequestMessage.GetInvokeRequests(&invokeRequests) == CHIP_NO_ERROR, Status::InvalidAction);
     242           40 :     VerifyOrReturnError(mTimedRequest == isTimedInvoke, Status::TimedRequestMismatch);
     243              : 
     244              :     {
     245           38 :         InvokeRequestMessage::Parser validationInvokeRequestMessage = invokeRequestMessage;
     246           38 :         VerifyOrReturnError(ValidateInvokeRequestMessageAndBuildRegistry(validationInvokeRequestMessage) == CHIP_NO_ERROR,
     247              :                             Status::InvalidAction);
     248              :     }
     249              : 
     250           33 :     TLV::TLVReader invokeRequestsReader;
     251           33 :     invokeRequests.GetReader(&invokeRequestsReader);
     252              : 
     253           33 :     size_t commandCount = 0;
     254           33 :     VerifyOrReturnError(TLV::Utilities::Count(invokeRequestsReader, commandCount, false /* recurse */) == CHIP_NO_ERROR,
     255              :                         Status::InvalidAction);
     256           33 :     if (commandCount > 1)
     257              :     {
     258            1 :         mReserveSpaceForMoreChunkMessages = true;
     259              :     }
     260              : 
     261           67 :     while (CHIP_NO_ERROR == (err = invokeRequestsReader.Next()))
     262              :     {
     263           34 :         VerifyOrReturnError(TLV::AnonymousTag() == invokeRequestsReader.GetTag(), Status::InvalidAction);
     264           34 :         CommandDataIB::Parser commandData;
     265           34 :         VerifyOrReturnError(commandData.Init(invokeRequestsReader) == CHIP_NO_ERROR, Status::InvalidAction);
     266           34 :         Status status = Status::Success;
     267           34 :         if (IsGroupRequest())
     268              :         {
     269            0 :             status = ProcessGroupCommandDataIB(commandData);
     270              :         }
     271              :         else
     272              :         {
     273           34 :             status = ProcessCommandDataIB(commandData);
     274              :         }
     275           34 :         if (status != Status::Success)
     276              :         {
     277            0 :             return status;
     278              :         }
     279              :     }
     280              : 
     281              :     // if we have exhausted this container
     282           33 :     if (CHIP_END_OF_TLV == err)
     283              :     {
     284           33 :         err = CHIP_NO_ERROR;
     285              :     }
     286           33 :     VerifyOrReturnError(err == CHIP_NO_ERROR, Status::InvalidAction);
     287           33 :     VerifyOrReturnError(invokeRequestMessage.ExitContainer() == CHIP_NO_ERROR, Status::InvalidAction);
     288           33 :     return Status::Success;
     289           41 : }
     290              : 
     291           46 : void CommandHandlerImpl::Close()
     292              : {
     293           46 :     mSuppressResponse = false;
     294           46 :     mpResponder       = nullptr;
     295           46 :     MoveToState(State::AwaitingDestruction);
     296              : 
     297              :     // We must finish all async work before we can shut down a CommandHandlerImpl. The actual CommandHandlerImpl MUST finish their
     298              :     // work in reasonable time or there is a bug. The only case for releasing CommandHandlerImpl without CommandHandler::Handle
     299              :     // releasing its reference is the stack shutting down, in which case Close() is not called. So the below check should always
     300              :     // pass.
     301           46 :     VerifyOrDieWithMsg(mPendingWork == 0, DataManagement, "CommandHandlerImpl::Close() called with %u unfinished async work items",
     302              :                        static_cast<unsigned int>(mPendingWork));
     303           46 :     InvalidateHandles();
     304              : 
     305           46 :     if (mpCallback)
     306              :     {
     307           43 :         mpCallback->OnDone(*this);
     308              :     }
     309           46 : }
     310              : 
     311           87 : void CommandHandlerImpl::AddToHandleList(Handle * apHandle)
     312              : {
     313           87 :     mpHandleList.PushBack(apHandle);
     314           87 : }
     315              : 
     316           87 : void CommandHandlerImpl::RemoveFromHandleList(Handle * apHandle)
     317              : {
     318           87 :     VerifyOrDie(mpHandleList.Contains(apHandle));
     319           87 :     mpHandleList.Remove(apHandle);
     320           87 : }
     321              : 
     322          103 : void CommandHandlerImpl::InvalidateHandles()
     323              : {
     324          103 :     for (auto handle = mpHandleList.begin(); handle != mpHandleList.end(); ++handle)
     325              :     {
     326            0 :         handle->Invalidate();
     327              :     }
     328          103 :     mpHandleList.Clear();
     329          103 : }
     330              : 
     331           87 : void CommandHandlerImpl::IncrementHoldOff(Handle * apHandle)
     332              : {
     333           87 :     mPendingWork++;
     334           87 :     AddToHandleList(apHandle);
     335           87 : }
     336              : 
     337           87 : void CommandHandlerImpl::DecrementHoldOff(Handle * apHandle)
     338              : {
     339              : 
     340           87 :     mPendingWork--;
     341           87 :     ChipLogDetail(DataManagement, "Decreasing reference count for CommandHandlerImpl, remaining %u",
     342              :                   static_cast<unsigned int>(mPendingWork));
     343              : 
     344           87 :     RemoveFromHandleList(apHandle);
     345              : 
     346           87 :     if (mPendingWork != 0)
     347              :     {
     348           41 :         return;
     349              :     }
     350              : 
     351           46 :     if (mpResponder == nullptr)
     352              :     {
     353            0 :         ChipLogProgress(DataManagement, "Skipping command response: response sender is null");
     354              :     }
     355           46 :     else if (!IsGroupRequest())
     356              :     {
     357           46 :         CHIP_ERROR err = FinalizeLastInvokeResponseMessage();
     358           46 :         if (err != CHIP_NO_ERROR)
     359              :         {
     360            9 :             ChipLogError(DataManagement, "Failed to finalize command response: %" CHIP_ERROR_FORMAT, err.Format());
     361              :         }
     362              :     }
     363              : 
     364           46 :     Close();
     365              : }
     366              : 
     367              : namespace {
     368              : // We use this when the sender did not actually provide a CommandFields struct,
     369              : // to avoid downstream consumers having to worry about cases when there is or is
     370              : // not a struct available.  We use an empty struct with anonymous tag, since we
     371              : // can't use a context tag at top level, and consumers should not care about the
     372              : // tag here).
     373              : constexpr uint8_t sNoFields[] = {
     374              :     CHIP_TLV_STRUCTURE(CHIP_TLV_TAG_ANONYMOUS),
     375              :     CHIP_TLV_END_OF_CONTAINER,
     376              : };
     377              : } // anonymous namespace
     378              : 
     379           34 : Status CommandHandlerImpl::ProcessCommandDataIB(CommandDataIB::Parser & aCommandElement)
     380              : {
     381           34 :     CHIP_ERROR err = CHIP_NO_ERROR;
     382           34 :     CommandPathIB::Parser commandPath;
     383           34 :     ConcreteCommandPath concretePath(0, 0, 0);
     384           34 :     TLV::TLVReader commandDataReader;
     385              : 
     386              :     // NOTE: errors may occur before the concrete command path is even fully decoded.
     387              : 
     388           34 :     err = aCommandElement.GetPath(&commandPath);
     389           34 :     VerifyOrReturnError(err == CHIP_NO_ERROR, Status::InvalidAction);
     390              : 
     391           34 :     err = commandPath.GetConcreteCommandPath(concretePath);
     392           34 :     VerifyOrReturnError(err == CHIP_NO_ERROR, Status::InvalidAction);
     393              : 
     394              :     {
     395           34 :         Access::SubjectDescriptor subjectDescriptor = GetSubjectDescriptor();
     396           34 :         DataModel::InvokeRequest request;
     397              : 
     398           34 :         request.path              = concretePath;
     399           34 :         request.subjectDescriptor = &subjectDescriptor;
     400           34 :         request.invokeFlags.Set(DataModel::InvokeFlags::kTimed, IsTimedInvoke());
     401              : 
     402           34 :         Status preCheckStatus = mpCallback->ValidateCommandCanBeDispatched(request);
     403           34 :         if (preCheckStatus != Status::Success)
     404              :         {
     405            7 :             return FallibleAddStatus(concretePath, preCheckStatus) != CHIP_NO_ERROR ? Status::Failure : Status::Success;
     406              :         }
     407              :     }
     408              : 
     409           27 :     err = aCommandElement.GetFields(&commandDataReader);
     410           27 :     if (CHIP_END_OF_TLV == err)
     411              :     {
     412            2 :         ChipLogDetail(DataManagement,
     413              :                       "Received command without data for Endpoint=%u Cluster=" ChipLogFormatMEI " Command=" ChipLogFormatMEI,
     414              :                       concretePath.mEndpointId, ChipLogValueMEI(concretePath.mClusterId), ChipLogValueMEI(concretePath.mCommandId));
     415            2 :         commandDataReader.Init(sNoFields);
     416            2 :         err = commandDataReader.Next();
     417              :     }
     418           27 :     if (CHIP_NO_ERROR == err)
     419              :     {
     420           27 :         ChipLogDetail(DataManagement, "Received command for Endpoint=%u Cluster=" ChipLogFormatMEI " Command=" ChipLogFormatMEI,
     421              :                       concretePath.mEndpointId, ChipLogValueMEI(concretePath.mClusterId), ChipLogValueMEI(concretePath.mCommandId));
     422           27 :         SuccessOrExit(err = DataModelCallbacks::GetInstance()->PreCommandReceived(concretePath, GetSubjectDescriptor()));
     423           27 :         mpCallback->DispatchCommand(*this, concretePath, commandDataReader);
     424           27 :         DataModelCallbacks::GetInstance()->PostCommandReceived(concretePath, GetSubjectDescriptor());
     425              :     }
     426              : 
     427            0 : exit:
     428           27 :     if (err != CHIP_NO_ERROR)
     429              :     {
     430            0 :         return FallibleAddStatus(concretePath, Status::InvalidCommand) != CHIP_NO_ERROR ? Status::Failure : Status::Success;
     431              :     }
     432              : 
     433              :     // We have handled the error status above and put the error status in response, now return success status so we can process
     434              :     // other commands in the invoke request.
     435           27 :     return Status::Success;
     436              : }
     437              : 
     438            0 : Status CommandHandlerImpl::ProcessGroupCommandDataIB(CommandDataIB::Parser & aCommandElement)
     439              : {
     440            0 :     CHIP_ERROR err = CHIP_NO_ERROR;
     441            0 :     CommandPathIB::Parser commandPath;
     442            0 :     TLV::TLVReader commandDataReader;
     443              :     ClusterId clusterId;
     444              :     CommandId commandId;
     445              :     GroupId groupId;
     446              :     FabricIndex fabric;
     447              : 
     448            0 :     Credentials::GroupDataProvider::GroupEndpoint mapping;
     449            0 :     Credentials::GroupDataProvider * groupDataProvider = Credentials::GetGroupDataProvider();
     450              :     Credentials::GroupDataProvider::EndpointIterator * iterator;
     451              : 
     452            0 :     err = aCommandElement.GetPath(&commandPath);
     453            0 :     VerifyOrReturnError(err == CHIP_NO_ERROR, Status::InvalidAction);
     454              : 
     455            0 :     err = commandPath.GetGroupCommandPath(&clusterId, &commandId);
     456            0 :     VerifyOrReturnError(err == CHIP_NO_ERROR, Status::InvalidAction);
     457              : 
     458            0 :     VerifyOrDie(mpResponder);
     459              :     // The optionalGroupId must have a value, otherwise we wouldn't have reached this code path.
     460            0 :     groupId = mpResponder->GetGroupId().Value();
     461            0 :     fabric  = GetAccessingFabricIndex();
     462              : 
     463            0 :     ChipLogDetail(DataManagement, "Received group command for Group=%u Cluster=" ChipLogFormatMEI " Command=" ChipLogFormatMEI,
     464              :                   groupId, ChipLogValueMEI(clusterId), ChipLogValueMEI(commandId));
     465              : 
     466            0 :     err = aCommandElement.GetFields(&commandDataReader);
     467            0 :     if (CHIP_END_OF_TLV == err)
     468              :     {
     469            0 :         ChipLogDetail(DataManagement,
     470              :                       "Received command without data for Group=%u Cluster=" ChipLogFormatMEI " Command=" ChipLogFormatMEI, groupId,
     471              :                       ChipLogValueMEI(clusterId), ChipLogValueMEI(commandId));
     472            0 :         commandDataReader.Init(sNoFields);
     473            0 :         err = commandDataReader.Next();
     474            0 :         VerifyOrReturnError(err == CHIP_NO_ERROR, Status::InvalidAction);
     475              :     }
     476            0 :     VerifyOrReturnError(err == CHIP_NO_ERROR, Status::Failure);
     477              : 
     478              :     // No check for `CommandIsFabricScoped` unlike in `ProcessCommandDataIB()` since group commands
     479              :     // always have an accessing fabric, by definition.
     480              : 
     481              :     // Find which endpoints can process the command, and dispatch to them.
     482            0 :     iterator = groupDataProvider->IterateEndpoints(fabric);
     483            0 :     VerifyOrReturnError(iterator != nullptr, Status::Failure);
     484              : 
     485            0 :     while (iterator->Next(mapping))
     486              :     {
     487            0 :         if (groupId != mapping.group_id)
     488              :         {
     489            0 :             continue;
     490              :         }
     491              : 
     492            0 :         ChipLogDetail(DataManagement,
     493              :                       "Processing group command for Endpoint=%u Cluster=" ChipLogFormatMEI " Command=" ChipLogFormatMEI,
     494              :                       mapping.endpoint_id, ChipLogValueMEI(clusterId), ChipLogValueMEI(commandId));
     495              : 
     496            0 :         const ConcreteCommandPath concretePath(mapping.endpoint_id, clusterId, commandId);
     497              : 
     498              :         {
     499            0 :             Access::SubjectDescriptor subjectDescriptor = GetSubjectDescriptor();
     500            0 :             DataModel::InvokeRequest request;
     501              : 
     502            0 :             request.path              = concretePath;
     503            0 :             request.subjectDescriptor = &subjectDescriptor;
     504            0 :             request.invokeFlags.Set(DataModel::InvokeFlags::kTimed, IsTimedInvoke());
     505              : 
     506            0 :             Status preCheckStatus = mpCallback->ValidateCommandCanBeDispatched(request);
     507            0 :             if (preCheckStatus != Status::Success)
     508              :             {
     509              :                 // Command failed for a specific path, but keep trying the rest of the paths.
     510            0 :                 continue;
     511              :             }
     512              :         }
     513              : 
     514            0 :         if ((err = DataModelCallbacks::GetInstance()->PreCommandReceived(concretePath, GetSubjectDescriptor())) == CHIP_NO_ERROR)
     515              :         {
     516            0 :             TLV::TLVReader dataReader(commandDataReader);
     517            0 :             mpCallback->DispatchCommand(*this, concretePath, dataReader);
     518            0 :             DataModelCallbacks::GetInstance()->PostCommandReceived(concretePath, GetSubjectDescriptor());
     519              :         }
     520              :         else
     521              :         {
     522            0 :             ChipLogError(DataManagement,
     523              :                          "Error when calling PreCommandReceived for Endpoint=%u Cluster=" ChipLogFormatMEI
     524              :                          " Command=" ChipLogFormatMEI " : %" CHIP_ERROR_FORMAT,
     525              :                          mapping.endpoint_id, ChipLogValueMEI(clusterId), ChipLogValueMEI(commandId), err.Format());
     526            0 :             continue;
     527              :         }
     528              :     }
     529            0 :     iterator->Release();
     530            0 :     return Status::Success;
     531              : }
     532              : 
     533           40 : CHIP_ERROR CommandHandlerImpl::TryAddStatusInternal(const ConcreteCommandPath & aCommandPath, const StatusIB & aStatus)
     534              : {
     535              :     // Return early when response should not be sent out.
     536           40 :     VerifyOrReturnValue(ResponsesAccepted(), CHIP_NO_ERROR);
     537              : 
     538           39 :     ReturnErrorOnFailure(PrepareStatus(aCommandPath));
     539           38 :     CommandStatusIB::Builder & commandStatus = mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().GetStatus();
     540           38 :     StatusIB::Builder & statusIBBuilder      = commandStatus.CreateErrorStatus();
     541           38 :     ReturnErrorOnFailure(commandStatus.GetError());
     542           38 :     statusIBBuilder.EncodeStatusIB(aStatus);
     543           38 :     ReturnErrorOnFailure(statusIBBuilder.GetError());
     544           38 :     return FinishStatus();
     545              : }
     546              : 
     547           39 : CHIP_ERROR CommandHandlerImpl::AddStatusInternal(const ConcreteCommandPath & aCommandPath, const StatusIB & aStatus)
     548              : {
     549           79 :     return TryAddingResponse([&]() -> CHIP_ERROR { return TryAddStatusInternal(aCommandPath, aStatus); });
     550              : }
     551              : 
     552           25 : void CommandHandlerImpl::AddStatus(const ConcreteCommandPath & aCommandPath,
     553              :                                    const Protocols::InteractionModel::ClusterStatusCode & status, const char * context)
     554              : {
     555              : 
     556           25 :     CHIP_ERROR error = FallibleAddStatus(aCommandPath, status, context);
     557              : 
     558           25 :     if (error != CHIP_NO_ERROR)
     559              :     {
     560            0 :         ChipLogError(DataManagement, "Failed to add command status: %" CHIP_ERROR_FORMAT, error.Format());
     561              :         // TODO(#30453) we could call mpResponder->ResponseDropped() if err == CHIP_ERROR_NO_MEMORY. This should
     562              :         // be done as a follow up so that change can be evaluated as a standalone PR.
     563              : 
     564              :         // Do not crash if the status has not been added due to running out of packet buffers or other resources.
     565              :         // It is better to drop a single response than to go offline and lose all sessions and subscriptions.
     566            0 :         VerifyOrDie(error == CHIP_ERROR_NO_MEMORY);
     567              :     }
     568           25 : }
     569              : 
     570           39 : CHIP_ERROR CommandHandlerImpl::FallibleAddStatus(const ConcreteCommandPath & path,
     571              :                                                  const Protocols::InteractionModel::ClusterStatusCode & status,
     572              :                                                  const char * context)
     573              : {
     574           39 :     if (!status.IsSuccess())
     575              :     {
     576           17 :         if (context == nullptr)
     577              :         {
     578           17 :             context = "no additional context";
     579              :         }
     580              : 
     581           17 :         if (status.HasClusterSpecificCode())
     582              :         {
     583            1 :             ChipLogError(DataManagement,
     584              :                          "Endpoint=%u Cluster=" ChipLogFormatMEI " Command=" ChipLogFormatMEI " status " ChipLogFormatIMStatus
     585              :                          " ClusterSpecificCode=%u (%s)",
     586              :                          path.mEndpointId, ChipLogValueMEI(path.mClusterId), ChipLogValueMEI(path.mCommandId),
     587              :                          ChipLogValueIMStatus(status.GetStatus()), static_cast<unsigned>(status.GetClusterSpecificCode().Value()),
     588              :                          context);
     589              :         }
     590              :         else
     591              :         {
     592           16 :             ChipLogError(DataManagement,
     593              :                          "Endpoint=%u Cluster=" ChipLogFormatMEI " Command=" ChipLogFormatMEI " status " ChipLogFormatIMStatus
     594              :                          " (%s)",
     595              :                          path.mEndpointId, ChipLogValueMEI(path.mClusterId), ChipLogValueMEI(path.mCommandId),
     596              :                          ChipLogValueIMStatus(status.GetStatus()), context);
     597              :         }
     598              :     }
     599              : 
     600           39 :     return AddStatusInternal(path, StatusIB{ status });
     601              : }
     602              : 
     603           23 : CHIP_ERROR CommandHandlerImpl::PrepareInvokeResponseCommand(const ConcreteCommandPath & aResponseCommandPath,
     604              :                                                             const CommandHandlerImpl::InvokeResponseParameters & aPrepareParameters)
     605              : {
     606           23 :     auto commandPathRegistryEntry = GetCommandPathRegistry().Find(aPrepareParameters.mRequestCommandPath);
     607           23 :     VerifyOrReturnValue(commandPathRegistryEntry.has_value(), CHIP_ERROR_INCORRECT_STATE);
     608              : 
     609           22 :     return PrepareInvokeResponseCommand(*commandPathRegistryEntry, aResponseCommandPath, aPrepareParameters.mStartOrEndDataStruct);
     610              : }
     611              : 
     612           22 : CHIP_ERROR CommandHandlerImpl::PrepareInvokeResponseCommand(const CommandPathRegistryEntry & apCommandPathRegistryEntry,
     613              :                                                             const ConcreteCommandPath & aCommandPath, bool aStartDataStruct)
     614              : {
     615              :     // Intentionally omitting the ResponsesAccepted early exit. Direct use of PrepareInvokeResponseCommand
     616              :     // is discouraged, as it often indicates incorrect usage patterns (see GitHub issue #32486).
     617              :     // If you're encountering CHIP_ERROR_INCORRECT_STATE, refactoring to use AddResponse is recommended.
     618           22 :     ReturnErrorOnFailure(AllocateBuffer());
     619              : 
     620           22 :     if (!mInternalCallToAddResponseData && mState == State::AddedCommand)
     621              :     {
     622              :         // An attempt is being made to add CommandData InvokeResponse using primitive
     623              :         // CommandHandlerImpl APIs. While not recommended, as this potentially leaves the
     624              :         // CommandHandlerImpl in an incorrect state upon failure, this approach is permitted
     625              :         // for legacy reasons. To maximize the likelihood of success, particularly when
     626              :         // handling large amounts of data, we try to obtain a new, completely empty
     627              :         // InvokeResponseMessage, as the existing one already has space occupied.
     628            0 :         ReturnErrorOnFailure(FinalizeInvokeResponseMessageAndPrepareNext());
     629              :     }
     630              : 
     631           22 :     CreateBackupForResponseRollback();
     632              :     //
     633              :     // We must not be in the middle of preparing a command, or having prepared or sent one.
     634              :     //
     635           22 :     VerifyOrReturnError(mState == State::NewResponseMessage || mState == State::AddedCommand, CHIP_ERROR_INCORRECT_STATE);
     636              : 
     637              :     // TODO(#30453): See if we can pass this back up the stack so caller can provide this instead of taking up
     638              :     // space in CommandHanlder.
     639           22 :     mRefForResponse = apCommandPathRegistryEntry.ref;
     640              : 
     641           22 :     MoveToState(State::Preparing);
     642           22 :     InvokeResponseIBs::Builder & invokeResponses = mInvokeResponseBuilder.GetInvokeResponses();
     643           22 :     InvokeResponseIB::Builder & invokeResponse   = invokeResponses.CreateInvokeResponse();
     644           22 :     ReturnErrorOnFailure(invokeResponses.GetError());
     645              : 
     646           20 :     CommandDataIB::Builder & commandData = invokeResponse.CreateCommand();
     647           20 :     ReturnErrorOnFailure(commandData.GetError());
     648           20 :     CommandPathIB::Builder & path = commandData.CreatePath();
     649           20 :     ReturnErrorOnFailure(commandData.GetError());
     650           20 :     ReturnErrorOnFailure(path.Encode(aCommandPath));
     651           20 :     if (aStartDataStruct)
     652              :     {
     653            2 :         ReturnErrorOnFailure(commandData.GetWriter()->StartContainer(TLV::ContextTag(CommandDataIB::Tag::kFields),
     654              :                                                                      TLV::kTLVType_Structure, mDataElementContainerType));
     655              :     }
     656           20 :     MoveToState(State::AddingCommand);
     657           20 :     return CHIP_NO_ERROR;
     658              : }
     659              : 
     660           18 : CHIP_ERROR CommandHandlerImpl::FinishCommand(bool aStartDataStruct)
     661              : {
     662              :     // Intentionally omitting the ResponsesAccepted early exit. Direct use of FinishCommand
     663              :     // is discouraged, as it often indicates incorrect usage patterns (see GitHub issue #32486).
     664              :     // If you're encountering CHIP_ERROR_INCORRECT_STATE, refactoring to use AddResponse is recommended.
     665           18 :     VerifyOrReturnError(mState == State::AddingCommand, CHIP_ERROR_INCORRECT_STATE);
     666           17 :     CommandDataIB::Builder & commandData = mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().GetCommand();
     667           17 :     if (aStartDataStruct)
     668              :     {
     669            1 :         ReturnErrorOnFailure(commandData.GetWriter()->EndContainer(mDataElementContainerType));
     670              :     }
     671              : 
     672           17 :     if (mRefForResponse.has_value())
     673              :     {
     674            9 :         ReturnErrorOnFailure(commandData.Ref(*mRefForResponse));
     675              :     }
     676              : 
     677           17 :     ReturnErrorOnFailure(commandData.EndOfCommandDataIB());
     678           17 :     ReturnErrorOnFailure(mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().EndOfInvokeResponseIB());
     679           17 :     MoveToState(State::AddedCommand);
     680           17 :     return CHIP_NO_ERROR;
     681              : }
     682              : 
     683           39 : CHIP_ERROR CommandHandlerImpl::PrepareStatus(const ConcreteCommandPath & aCommandPath)
     684              : {
     685           39 :     ReturnErrorOnFailure(AllocateBuffer());
     686              :     //
     687              :     // We must not be in the middle of preparing a command, or having prepared or sent one.
     688              :     //
     689           39 :     VerifyOrReturnError(mState == State::NewResponseMessage || mState == State::AddedCommand, CHIP_ERROR_INCORRECT_STATE);
     690           39 :     if (mState == State::AddedCommand)
     691              :     {
     692            8 :         CreateBackupForResponseRollback();
     693              :     }
     694              : 
     695           39 :     auto commandPathRegistryEntry = GetCommandPathRegistry().Find(aCommandPath);
     696           39 :     VerifyOrReturnError(commandPathRegistryEntry.has_value(), CHIP_ERROR_INCORRECT_STATE);
     697           39 :     mRefForResponse = commandPathRegistryEntry->ref;
     698              : 
     699           39 :     MoveToState(State::Preparing);
     700           39 :     InvokeResponseIBs::Builder & invokeResponses = mInvokeResponseBuilder.GetInvokeResponses();
     701           39 :     InvokeResponseIB::Builder & invokeResponse   = invokeResponses.CreateInvokeResponse();
     702           39 :     ReturnErrorOnFailure(invokeResponses.GetError());
     703           38 :     CommandStatusIB::Builder & commandStatus = invokeResponse.CreateStatus();
     704           38 :     ReturnErrorOnFailure(commandStatus.GetError());
     705           38 :     CommandPathIB::Builder & path = commandStatus.CreatePath();
     706           38 :     ReturnErrorOnFailure(commandStatus.GetError());
     707           38 :     ReturnErrorOnFailure(path.Encode(aCommandPath));
     708           38 :     MoveToState(State::AddingCommand);
     709           38 :     return CHIP_NO_ERROR;
     710              : }
     711              : 
     712           38 : CHIP_ERROR CommandHandlerImpl::FinishStatus()
     713              : {
     714           38 :     VerifyOrReturnError(mState == State::AddingCommand, CHIP_ERROR_INCORRECT_STATE);
     715              : 
     716           38 :     CommandStatusIB::Builder & commandStatus = mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().GetStatus();
     717           38 :     if (mRefForResponse.has_value())
     718              :     {
     719            3 :         ReturnErrorOnFailure(commandStatus.Ref(*mRefForResponse));
     720              :     }
     721              : 
     722           38 :     ReturnErrorOnFailure(mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().GetStatus().EndOfCommandStatusIB());
     723           38 :     ReturnErrorOnFailure(mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().EndOfInvokeResponseIB());
     724           38 :     MoveToState(State::AddedCommand);
     725           38 :     return CHIP_NO_ERROR;
     726              : }
     727              : 
     728           30 : void CommandHandlerImpl::CreateBackupForResponseRollback()
     729              : {
     730           30 :     VerifyOrReturn(mState == State::NewResponseMessage || mState == State::AddedCommand);
     731           30 :     VerifyOrReturn(mInvokeResponseBuilder.GetInvokeResponses().GetError() == CHIP_NO_ERROR);
     732           30 :     VerifyOrReturn(mInvokeResponseBuilder.GetError() == CHIP_NO_ERROR);
     733           30 :     mInvokeResponseBuilder.Checkpoint(mBackupWriter);
     734           30 :     mBackupState         = mState;
     735           30 :     mRollbackBackupValid = true;
     736              : }
     737              : 
     738            5 : CHIP_ERROR CommandHandlerImpl::RollbackResponse()
     739              : {
     740            5 :     VerifyOrReturnError(mRollbackBackupValid, CHIP_ERROR_INCORRECT_STATE);
     741            5 :     VerifyOrReturnError(mState == State::Preparing || mState == State::AddingCommand, CHIP_ERROR_INCORRECT_STATE);
     742            5 :     ChipLogDetail(DataManagement, "Rolling back response");
     743              :     // TODO(#30453): Rollback of mInvokeResponseBuilder should handle resetting
     744              :     // InvokeResponses.
     745            5 :     mInvokeResponseBuilder.GetInvokeResponses().ResetError();
     746            5 :     mInvokeResponseBuilder.Rollback(mBackupWriter);
     747            5 :     MoveToState(mBackupState);
     748            5 :     mRollbackBackupValid = false;
     749            5 :     return CHIP_NO_ERROR;
     750              : }
     751              : 
     752           19 : TLV::TLVWriter * CommandHandlerImpl::GetCommandDataIBTLVWriter()
     753              : {
     754           19 :     if (mState != State::AddingCommand)
     755              :     {
     756            1 :         return nullptr;
     757              :     }
     758              : 
     759           18 :     return mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().GetCommand().GetWriter();
     760              : }
     761              : 
     762            0 : FabricIndex CommandHandlerImpl::GetAccessingFabricIndex() const
     763              : {
     764            0 :     VerifyOrDie(!mGoneAsync);
     765            0 :     VerifyOrDie(mpResponder);
     766            0 :     return mpResponder->GetAccessingFabricIndex();
     767              : }
     768              : 
     769            3 : CHIP_ERROR CommandHandlerImpl::FinalizeInvokeResponseMessageAndPrepareNext()
     770              : {
     771            3 :     ReturnErrorOnFailure(FinalizeInvokeResponseMessage(/* aHasMoreChunks = */ true));
     772              :     // After successfully finalizing InvokeResponseMessage, no buffer should remain
     773              :     // allocated.
     774            3 :     VerifyOrDie(!mBufferAllocated);
     775            3 :     CHIP_ERROR err = AllocateBuffer();
     776            3 :     if (err != CHIP_NO_ERROR)
     777              :     {
     778              :         // TODO(#30453): Improve ResponseDropped calls to occur only when dropping is
     779              :         // definitively guaranteed.
     780              :         // Response dropping is not yet definitive as a subsequent call
     781              :         // to AllocateBuffer might succeed.
     782            0 :         VerifyOrDie(mpResponder);
     783            0 :         mpResponder->ResponseDropped();
     784              :     }
     785            3 :     return err;
     786              : }
     787              : 
     788           49 : CHIP_ERROR CommandHandlerImpl::FinalizeInvokeResponseMessage(bool aHasMoreChunks)
     789              : {
     790           49 :     System::PacketBufferHandle packet;
     791              : 
     792           49 :     VerifyOrReturnError(mState == State::AddedCommand, CHIP_ERROR_INCORRECT_STATE);
     793           40 :     ReturnErrorOnFailure(mInvokeResponseBuilder.GetInvokeResponses().EndOfInvokeResponses());
     794           40 :     if (aHasMoreChunks)
     795              :     {
     796              :         // Unreserving space previously reserved for MoreChunkedMessages is done
     797              :         // in the call to mInvokeResponseBuilder.MoreChunkedMessages.
     798            3 :         mInvokeResponseBuilder.MoreChunkedMessages(aHasMoreChunks);
     799            3 :         ReturnErrorOnFailure(mInvokeResponseBuilder.GetError());
     800              :     }
     801           40 :     ReturnErrorOnFailure(mInvokeResponseBuilder.EndOfInvokeResponseMessage());
     802           40 :     ReturnErrorOnFailure(mCommandMessageWriter.Finalize(&packet));
     803           40 :     VerifyOrDie(mpResponder);
     804           40 :     mpResponder->AddInvokeResponseToSend(std::move(packet));
     805           40 :     mBufferAllocated     = false;
     806           40 :     mRollbackBackupValid = false;
     807           40 :     return CHIP_NO_ERROR;
     808           49 : }
     809              : 
     810           54 : void CommandHandlerImpl::SetExchangeInterface(CommandHandlerExchangeInterface * commandResponder)
     811              : {
     812           54 :     VerifyOrDieWithMsg(mState == State::Idle, DataManagement, "CommandResponseSender can only be set in idle state");
     813           54 :     mpResponder = commandResponder;
     814           54 : }
     815              : 
     816          273 : const char * CommandHandlerImpl::GetStateStr() const
     817              : {
     818              : #if CHIP_DETAIL_LOGGING
     819          273 :     switch (mState)
     820              :     {
     821            0 :     case State::Idle:
     822            0 :         return "Idle";
     823              : 
     824           50 :     case State::NewResponseMessage:
     825           50 :         return "NewResponseMessage";
     826              : 
     827           61 :     case State::Preparing:
     828           61 :         return "Preparing";
     829              : 
     830           58 :     case State::AddingCommand:
     831           58 :         return "AddingCommand";
     832              : 
     833           58 :     case State::AddedCommand:
     834           58 :         return "AddedCommand";
     835              : 
     836            0 :     case State::DispatchResponses:
     837            0 :         return "DispatchResponses";
     838              : 
     839           46 :     case State::AwaitingDestruction:
     840           46 :         return "AwaitingDestruction";
     841              :     }
     842              : #endif // CHIP_DETAIL_LOGGING
     843            0 :     return "N/A";
     844              : }
     845              : 
     846          273 : void CommandHandlerImpl::MoveToState(const State aTargetState)
     847              : {
     848          273 :     mState = aTargetState;
     849          273 :     ChipLogDetail(DataManagement, "Command handler moving to [%10.10s]", GetStateStr());
     850          273 : }
     851              : 
     852            0 : void CommandHandlerImpl::FlushAcksRightAwayOnSlowCommand()
     853              : {
     854            0 :     if (mpResponder)
     855              :     {
     856            0 :         mpResponder->HandlingSlowCommand();
     857              :     }
     858            0 : }
     859              : 
     860          111 : Access::SubjectDescriptor CommandHandlerImpl::GetSubjectDescriptor() const
     861              : {
     862          111 :     VerifyOrDie(!mGoneAsync);
     863          111 :     VerifyOrDie(mpResponder);
     864          111 :     return mpResponder->GetSubjectDescriptor();
     865              : }
     866              : 
     867           57 : bool CommandHandlerImpl::IsTimedInvoke() const
     868              : {
     869           57 :     return mTimedRequest;
     870              : }
     871              : 
     872            8 : void CommandHandlerImpl::AddResponse(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
     873              :                                      const DataModel::EncodableToTLV & aEncodable)
     874              : {
     875            8 :     CHIP_ERROR err = AddResponseData(aRequestCommandPath, aResponseCommandId, aEncodable);
     876            8 :     if (err != CHIP_NO_ERROR)
     877              :     {
     878            1 :         ChipLogError(DataManagement, "Adding response failed: %" CHIP_ERROR_FORMAT ". Returning failure instead.", err.Format());
     879            1 :         AddStatus(aRequestCommandPath, Protocols::InteractionModel::Status::Failure);
     880              :     }
     881            8 : }
     882              : 
     883            2 : Messaging::ExchangeContext * CommandHandlerImpl::GetExchangeContext() const
     884              : {
     885            2 :     VerifyOrDie(mpResponder);
     886            2 :     return mpResponder->GetExchangeContext();
     887              : }
     888              : 
     889              : #if CHIP_WITH_NLFAULTINJECTION
     890              : 
     891              : namespace {
     892              : 
     893            0 : CHIP_ERROR TestOnlyExtractCommandPathFromNextInvokeRequest(TLV::TLVReader & invokeRequestsReader,
     894              :                                                            ConcreteCommandPath & concretePath)
     895              : {
     896            0 :     ReturnErrorOnFailure(invokeRequestsReader.Next(TLV::AnonymousTag()));
     897            0 :     CommandDataIB::Parser commandData;
     898            0 :     ReturnErrorOnFailure(commandData.Init(invokeRequestsReader));
     899            0 :     CommandPathIB::Parser commandPath;
     900            0 :     ReturnErrorOnFailure(commandData.GetPath(&commandPath));
     901            0 :     return commandPath.GetConcreteCommandPath(concretePath);
     902              : }
     903              : 
     904            0 : [[maybe_unused]] const char * GetFaultInjectionTypeStr(CommandHandlerImpl::NlFaultInjectionType faultType)
     905              : {
     906            0 :     switch (faultType)
     907              :     {
     908            0 :     case CommandHandlerImpl::NlFaultInjectionType::SeparateResponseMessages:
     909              :         return "Each response will be sent in a separate InvokeResponseMessage. The order of responses will be the same as the "
     910            0 :                "original request.";
     911            0 :     case CommandHandlerImpl::NlFaultInjectionType::SeparateResponseMessagesAndInvertedResponseOrder:
     912              :         return "Each response will be sent in a separate InvokeResponseMessage. The order of responses will be reversed from the "
     913            0 :                "original request.";
     914            0 :     case CommandHandlerImpl::NlFaultInjectionType::SkipSecondResponse:
     915            0 :         return "Single InvokeResponseMessages. Dropping response to second request";
     916              :     }
     917            0 :     ChipLogError(DataManagement, "TH Failure: Unexpected fault type");
     918            0 :     chipAbort();
     919              : }
     920              : 
     921              : } // anonymous namespace
     922              : 
     923              : // This method intentionally duplicates code from other sections. While code consolidation
     924              : // is generally preferred, here we prioritize generating a clear crash message to aid in
     925              : // troubleshooting test failures.
     926            0 : void CommandHandlerImpl::TestOnlyInvokeCommandRequestWithFaultsInjected(CommandHandlerExchangeInterface & commandResponder,
     927              :                                                                         System::PacketBufferHandle && payload, bool isTimedInvoke,
     928              :                                                                         NlFaultInjectionType faultType)
     929              : {
     930            0 :     VerifyOrDieWithMsg(mState == State::Idle, DataManagement, "TH Failure: state should be Idle, issue with TH");
     931            0 :     SetExchangeInterface(&commandResponder);
     932              : 
     933            0 :     ChipLogProgress(DataManagement, "Response to InvokeRequestMessage overridden by fault injection");
     934            0 :     ChipLogProgress(DataManagement, "   Injecting the following response:%s", GetFaultInjectionTypeStr(faultType));
     935              : 
     936            0 :     Handle workHandle(this);
     937            0 :     VerifyOrDieWithMsg(!commandResponder.GetGroupId().HasValue(), DataManagement, "DUT Failure: Unexpected Group Command");
     938              : 
     939            0 :     System::PacketBufferTLVReader reader;
     940            0 :     InvokeRequestMessage::Parser invokeRequestMessage;
     941            0 :     InvokeRequests::Parser invokeRequests;
     942            0 :     reader.Init(std::move(payload));
     943            0 :     VerifyOrDieWithMsg(invokeRequestMessage.Init(reader) == CHIP_NO_ERROR, DataManagement,
     944              :                        "TH Failure: Failed 'invokeRequestMessage.Init(reader)'");
     945              : #if CHIP_CONFIG_IM_PRETTY_PRINT
     946            0 :     invokeRequestMessage.PrettyPrint();
     947              : #endif
     948              : 
     949            0 :     VerifyOrDieWithMsg(invokeRequestMessage.GetSuppressResponse(&mSuppressResponse) == CHIP_NO_ERROR, DataManagement,
     950              :                        "DUT Failure: Mandatory SuppressResponse field missing");
     951            0 :     VerifyOrDieWithMsg(invokeRequestMessage.GetTimedRequest(&mTimedRequest) == CHIP_NO_ERROR, DataManagement,
     952              :                        "DUT Failure: Mandatory TimedRequest field missing");
     953            0 :     VerifyOrDieWithMsg(invokeRequestMessage.GetInvokeRequests(&invokeRequests) == CHIP_NO_ERROR, DataManagement,
     954              :                        "DUT Failure: Mandatory InvokeRequests field missing");
     955            0 :     VerifyOrDieWithMsg(mTimedRequest == isTimedInvoke, DataManagement,
     956              :                        "DUT Failure: TimedRequest value in message mismatches action");
     957              : 
     958              :     {
     959            0 :         InvokeRequestMessage::Parser validationInvokeRequestMessage = invokeRequestMessage;
     960            0 :         VerifyOrDieWithMsg(ValidateInvokeRequestMessageAndBuildRegistry(validationInvokeRequestMessage) == CHIP_NO_ERROR,
     961              :                            DataManagement, "DUT Failure: InvokeRequestMessage contents were invalid");
     962              :     }
     963              : 
     964            0 :     TLV::TLVReader invokeRequestsReader;
     965            0 :     invokeRequests.GetReader(&invokeRequestsReader);
     966              : 
     967            0 :     size_t commandCount = 0;
     968            0 :     VerifyOrDieWithMsg(TLV::Utilities::Count(invokeRequestsReader, commandCount, false /* recurse */) == CHIP_NO_ERROR,
     969              :                        DataManagement,
     970              :                        "TH Failure: Failed to get the length of InvokeRequests after InvokeRequestMessage validation");
     971              : 
     972              :     // The command count check (specifically for a count of 2) is tied to IDM_1_3. This may need adjustment for
     973              :     // compatibility with future test plans.
     974            0 :     VerifyOrDieWithMsg(commandCount == 2, DataManagement, "DUT failure: We were strictly expecting exactly 2 InvokeRequests");
     975            0 :     mReserveSpaceForMoreChunkMessages = true;
     976              : 
     977              :     {
     978              :         // Response path is the same as request path since we are replying with a failure message.
     979            0 :         ConcreteCommandPath concreteResponsePath1;
     980            0 :         ConcreteCommandPath concreteResponsePath2;
     981            0 :         VerifyOrDieWithMsg(
     982              :             TestOnlyExtractCommandPathFromNextInvokeRequest(invokeRequestsReader, concreteResponsePath1) == CHIP_NO_ERROR,
     983              :             DataManagement, "DUT Failure: Issues encountered while extracting the ConcreteCommandPath from the first request");
     984            0 :         VerifyOrDieWithMsg(
     985              :             TestOnlyExtractCommandPathFromNextInvokeRequest(invokeRequestsReader, concreteResponsePath2) == CHIP_NO_ERROR,
     986              :             DataManagement, "DUT Failure: Issues encountered while extracting the ConcreteCommandPath from the second request");
     987              : 
     988            0 :         if (faultType == NlFaultInjectionType::SeparateResponseMessagesAndInvertedResponseOrder)
     989              :         {
     990            0 :             ConcreteCommandPath temp(concreteResponsePath1);
     991            0 :             concreteResponsePath1 = concreteResponsePath2;
     992            0 :             concreteResponsePath2 = temp;
     993              :         }
     994              : 
     995            0 :         VerifyOrDieWithMsg(FallibleAddStatus(concreteResponsePath1, Status::Failure) == CHIP_NO_ERROR, DataManagement,
     996              :                            "TH Failure: Error adding the first InvokeResponse");
     997            0 :         if (faultType == NlFaultInjectionType::SeparateResponseMessages ||
     998              :             faultType == NlFaultInjectionType::SeparateResponseMessagesAndInvertedResponseOrder)
     999              :         {
    1000            0 :             VerifyOrDieWithMsg(FinalizeInvokeResponseMessageAndPrepareNext() == CHIP_NO_ERROR, DataManagement,
    1001              :                                "TH Failure: Failed to create second InvokeResponseMessage");
    1002              :         }
    1003            0 :         if (faultType != NlFaultInjectionType::SkipSecondResponse)
    1004              :         {
    1005            0 :             VerifyOrDieWithMsg(FallibleAddStatus(concreteResponsePath2, Status::Failure) == CHIP_NO_ERROR, DataManagement,
    1006              :                                "TH Failure: Error adding the second InvokeResponse");
    1007              :         }
    1008              :     }
    1009              : 
    1010            0 :     VerifyOrDieWithMsg(invokeRequestsReader.Next() == CHIP_END_OF_TLV, DataManagement,
    1011              :                        "DUT Failure: Unexpected TLV ending of InvokeRequests");
    1012            0 :     VerifyOrDieWithMsg(invokeRequestMessage.ExitContainer() == CHIP_NO_ERROR, DataManagement,
    1013              :                        "DUT Failure: InvokeRequestMessage TLV is not properly terminated");
    1014            0 : }
    1015              : #endif // CHIP_WITH_NLFAULTINJECTION
    1016              : 
    1017              : } // namespace app
    1018              : } // namespace chip
        

Generated by: LCOV version 2.0-1