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