forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteHandler.cpp
695 lines (573 loc) · 26.6 KB
/
WriteHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "messaging/ExchangeContext.h"
#include <app/AppConfig.h>
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/InteractionModelEngine.h>
#include <app/MessageDef/EventPathIB.h>
#include <app/MessageDef/StatusIB.h>
#include <app/StatusResponse.h>
#include <app/WriteHandler.h>
#include <app/reporting/Engine.h>
#include <app/util/MatterCallbacks.h>
#include <app/util/ember-compatibility-functions.h>
#include <credentials/GroupDataProvider.h>
#include <lib/support/TypeTraits.h>
#include <protocols/interaction_model/StatusCode.h>
namespace chip {
namespace app {
using namespace Protocols::InteractionModel;
using Status = Protocols::InteractionModel::Status;
constexpr uint8_t kListAttributeType = 0x48;
CHIP_ERROR WriteHandler::Init(WriteHandlerDelegate * apWriteHandlerDelegate)
{
VerifyOrReturnError(!mExchangeCtx, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(apWriteHandlerDelegate, CHIP_ERROR_INVALID_ARGUMENT);
mDelegate = apWriteHandlerDelegate;
MoveToState(State::Initialized);
mACLCheckCache.ClearValue();
mProcessingAttributePath.ClearValue();
return CHIP_NO_ERROR;
}
void WriteHandler::Close()
{
VerifyOrReturn(mState != State::Uninitialized);
// DeliverFinalListWriteEnd will be a no-op if we have called
// DeliverFinalListWriteEnd in success conditions, so passing false for
// wasSuccessful here is safe: if it does anything, we were in fact not
// successful.
DeliverFinalListWriteEnd(false /* wasSuccessful */);
mExchangeCtx.Release();
mSuppressResponse = false;
MoveToState(State::Uninitialized);
}
Status WriteHandler::HandleWriteRequestMessage(Messaging::ExchangeContext * apExchangeContext,
System::PacketBufferHandle && aPayload, bool aIsTimedWrite)
{
System::PacketBufferHandle packet = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
VerifyOrReturnError(!packet.IsNull(), Status::Failure);
System::PacketBufferTLVWriter messageWriter;
messageWriter.Init(std::move(packet));
VerifyOrReturnError(mWriteResponseBuilder.Init(&messageWriter) == CHIP_NO_ERROR, Status::Failure);
mWriteResponseBuilder.CreateWriteResponses();
VerifyOrReturnError(mWriteResponseBuilder.GetError() == CHIP_NO_ERROR, Status::Failure);
Status status = ProcessWriteRequest(std::move(aPayload), aIsTimedWrite);
// Do not send response on Group Write
if (status == Status::Success && !apExchangeContext->IsGroupExchangeContext())
{
CHIP_ERROR err = SendWriteResponse(std::move(messageWriter));
if (err != CHIP_NO_ERROR)
{
status = Status::Failure;
}
}
return status;
}
Status WriteHandler::OnWriteRequest(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle && aPayload,
bool aIsTimedWrite)
{
//
// Let's take over further message processing on this exchange from the IM.
// This is only relevant during chunked requests.
//
mExchangeCtx.Grab(apExchangeContext);
Status status = HandleWriteRequestMessage(apExchangeContext, std::move(aPayload), aIsTimedWrite);
// The write transaction will be alive only when the message was handled successfully and there are more chunks.
if (!(status == Status::Success && mHasMoreChunks))
{
Close();
}
return status;
}
CHIP_ERROR WriteHandler::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrDieWithMsg(apExchangeContext == mExchangeCtx.Get(), DataManagement,
"Incoming exchange context should be same as the initial request.");
VerifyOrDieWithMsg(!apExchangeContext->IsGroupExchangeContext(), DataManagement,
"OnMessageReceived should not be called on GroupExchangeContext");
if (!aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::WriteRequest))
{
if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::StatusResponse))
{
CHIP_ERROR statusError = CHIP_NO_ERROR;
// Parse the status response so we can log it properly.
StatusResponse::ProcessStatusResponse(std::move(aPayload), statusError);
}
ChipLogDetail(DataManagement, "Unexpected message type %d", aPayloadHeader.GetMessageType());
StatusResponse::Send(Status::InvalidAction, apExchangeContext, false /*aExpectResponse*/);
Close();
return CHIP_ERROR_INVALID_MESSAGE_TYPE;
}
Status status =
HandleWriteRequestMessage(apExchangeContext, std::move(aPayload), false /* chunked write should not be timed write */);
if (status == Status::Success)
{
// We have no more chunks, the write response has been sent in HandleWriteRequestMessage, so close directly.
if (!mHasMoreChunks)
{
Close();
}
}
else
{
err = StatusResponse::Send(status, apExchangeContext, false /*aExpectResponse*/);
Close();
}
return CHIP_NO_ERROR;
}
void WriteHandler::OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext)
{
ChipLogError(DataManagement, "Time out! failed to receive status response from Exchange: " ChipLogFormatExchange,
ChipLogValueExchange(apExchangeContext));
Close();
}
CHIP_ERROR WriteHandler::FinalizeMessage(System::PacketBufferTLVWriter && aMessageWriter, System::PacketBufferHandle & packet)
{
VerifyOrReturnError(mState == State::AddStatus, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(mWriteResponseBuilder.GetWriteResponses().EndOfAttributeStatuses());
ReturnErrorOnFailure(mWriteResponseBuilder.EndOfWriteResponseMessage());
ReturnErrorOnFailure(aMessageWriter.Finalize(&packet));
return CHIP_NO_ERROR;
}
CHIP_ERROR WriteHandler::SendWriteResponse(System::PacketBufferTLVWriter && aMessageWriter)
{
CHIP_ERROR err = CHIP_NO_ERROR;
System::PacketBufferHandle packet;
VerifyOrExit(mState == State::AddStatus, err = CHIP_ERROR_INCORRECT_STATE);
err = FinalizeMessage(std::move(aMessageWriter), packet);
SuccessOrExit(err);
VerifyOrExit(mExchangeCtx, err = CHIP_ERROR_INCORRECT_STATE);
mExchangeCtx->UseSuggestedResponseTimeout(app::kExpectedIMProcessingTime);
err = mExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::WriteResponse, std::move(packet),
mHasMoreChunks ? Messaging::SendMessageFlags::kExpectResponse
: Messaging::SendMessageFlags::kNone);
SuccessOrExit(err);
MoveToState(State::Sending);
exit:
return err;
}
void WriteHandler::DeliverListWriteBegin(const ConcreteAttributePath & aPath)
{
if (auto * attrOverride = GetAttributeAccessOverride(aPath.mEndpointId, aPath.mClusterId))
{
attrOverride->OnListWriteBegin(aPath);
}
}
void WriteHandler::DeliverListWriteEnd(const ConcreteAttributePath & aPath, bool writeWasSuccessful)
{
if (auto * attrOverride = GetAttributeAccessOverride(aPath.mEndpointId, aPath.mClusterId))
{
attrOverride->OnListWriteEnd(aPath, writeWasSuccessful);
}
}
void WriteHandler::DeliverFinalListWriteEnd(bool writeWasSuccessful)
{
if (mProcessingAttributePath.HasValue() && mProcessingAttributeIsList)
{
DeliverListWriteEnd(mProcessingAttributePath.Value(), writeWasSuccessful);
}
mProcessingAttributePath.ClearValue();
}
CHIP_ERROR WriteHandler::DeliverFinalListWriteEndForGroupWrite(bool writeWasSuccessful)
{
VerifyOrReturnError(mProcessingAttributePath.HasValue() && mProcessingAttributeIsList, CHIP_NO_ERROR);
Credentials::GroupDataProvider::GroupEndpoint mapping;
Credentials::GroupDataProvider * groupDataProvider = Credentials::GetGroupDataProvider();
Credentials::GroupDataProvider::EndpointIterator * iterator;
GroupId groupId = mExchangeCtx->GetSessionHandle()->AsIncomingGroupSession()->GetGroupId();
FabricIndex fabricIndex = GetAccessingFabricIndex();
auto processingConcreteAttributePath = mProcessingAttributePath.Value();
mProcessingAttributePath.ClearValue();
iterator = groupDataProvider->IterateEndpoints(fabricIndex);
VerifyOrReturnError(iterator != nullptr, CHIP_ERROR_NO_MEMORY);
while (iterator->Next(mapping))
{
if (groupId != mapping.group_id)
{
continue;
}
processingConcreteAttributePath.mEndpointId = mapping.endpoint_id;
VerifyOrReturnError(mDelegate, CHIP_ERROR_INCORRECT_STATE);
if (!mDelegate->HasConflictWriteRequests(this, processingConcreteAttributePath))
{
DeliverListWriteEnd(processingConcreteAttributePath, writeWasSuccessful);
}
}
iterator->Release();
return CHIP_NO_ERROR;
}
namespace {
// To reduce the various use of previousProcessed.HasValue() && previousProcessed.Value() == nextAttribute to save code size.
bool IsSameAttribute(const Optional<ConcreteAttributePath> & previousProcessed, const ConcreteDataAttributePath & nextAttribute)
{
return previousProcessed.HasValue() && previousProcessed.Value() == nextAttribute;
}
bool ShouldReportListWriteEnd(const Optional<ConcreteAttributePath> & previousProcessed, bool previousProcessedAttributeIsList,
const ConcreteDataAttributePath & nextAttribute)
{
return previousProcessedAttributeIsList && !IsSameAttribute(previousProcessed, nextAttribute) && previousProcessed.HasValue();
}
bool ShouldReportListWriteBegin(const Optional<ConcreteAttributePath> & previousProcessed, bool previousProcessedAttributeIsList,
const ConcreteDataAttributePath & nextAttribute)
{
return !IsSameAttribute(previousProcessed, nextAttribute) && nextAttribute.IsListOperation();
}
} // namespace
CHIP_ERROR WriteHandler::ProcessAttributeDataIBs(TLV::TLVReader & aAttributeDataIBsReader)
{
CHIP_ERROR err = CHIP_NO_ERROR;
ReturnErrorCodeIf(!mExchangeCtx, CHIP_ERROR_INTERNAL);
const Access::SubjectDescriptor subjectDescriptor = mExchangeCtx->GetSessionHandle()->GetSubjectDescriptor();
while (CHIP_NO_ERROR == (err = aAttributeDataIBsReader.Next()))
{
chip::TLV::TLVReader dataReader;
AttributeDataIB::Parser element;
AttributePathIB::Parser attributePath;
ConcreteDataAttributePath dataAttributePath;
TLV::TLVReader reader = aAttributeDataIBsReader;
err = element.Init(reader);
SuccessOrExit(err);
err = element.GetPath(&attributePath);
SuccessOrExit(err);
err = attributePath.GetConcreteAttributePath(dataAttributePath);
SuccessOrExit(err);
err = element.GetData(&dataReader);
SuccessOrExit(err);
const auto attributeMetadata = GetAttributeMetadata(dataAttributePath);
bool currentAttributeIsList = (attributeMetadata != nullptr && attributeMetadata->attributeType == kListAttributeType);
if (!dataAttributePath.IsListOperation() && currentAttributeIsList)
{
dataAttributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
}
VerifyOrExit(mDelegate, err = CHIP_ERROR_INCORRECT_STATE);
if (mDelegate->HasConflictWriteRequests(this, dataAttributePath) ||
// Per chunking protocol, we are processing the list entries, but the initial empty list is not processed, so we reject
// it with Busy status code.
(dataAttributePath.IsListItemOperation() && !IsSameAttribute(mProcessingAttributePath, dataAttributePath)))
{
err = AddStatusInternal(dataAttributePath, StatusIB(Status::Busy));
continue;
}
if (ShouldReportListWriteEnd(mProcessingAttributePath, mProcessingAttributeIsList, dataAttributePath))
{
DeliverListWriteEnd(mProcessingAttributePath.Value(), mAttributeWriteSuccessful);
}
if (ShouldReportListWriteBegin(mProcessingAttributePath, mProcessingAttributeIsList, dataAttributePath))
{
DeliverListWriteBegin(dataAttributePath);
mAttributeWriteSuccessful = true;
}
mProcessingAttributeIsList = dataAttributePath.IsListOperation();
mProcessingAttributePath.SetValue(dataAttributePath);
DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
DataModelCallbacks::OperationOrder::Pre, dataAttributePath);
TLV::TLVWriter backup;
DataVersion version = 0;
mWriteResponseBuilder.GetWriteResponses().Checkpoint(backup);
err = element.GetDataVersion(&version);
if (CHIP_NO_ERROR == err)
{
dataAttributePath.mDataVersion.SetValue(version);
}
else if (CHIP_END_OF_TLV == err)
{
err = CHIP_NO_ERROR;
}
SuccessOrExit(err);
err = WriteSingleClusterData(subjectDescriptor, dataAttributePath, dataReader, this);
if (err != CHIP_NO_ERROR)
{
mWriteResponseBuilder.GetWriteResponses().Rollback(backup);
err = AddStatusInternal(dataAttributePath, StatusIB(err));
}
DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
DataModelCallbacks::OperationOrder::Post, dataAttributePath);
SuccessOrExit(err);
}
if (CHIP_END_OF_TLV == err)
{
err = CHIP_NO_ERROR;
}
SuccessOrExit(err);
if (!mHasMoreChunks)
{
DeliverFinalListWriteEnd(mAttributeWriteSuccessful);
}
exit:
return err;
}
CHIP_ERROR WriteHandler::ProcessGroupAttributeDataIBs(TLV::TLVReader & aAttributeDataIBsReader)
{
CHIP_ERROR err = CHIP_NO_ERROR;
ReturnErrorCodeIf(!mExchangeCtx, CHIP_ERROR_INTERNAL);
const Access::SubjectDescriptor subjectDescriptor =
mExchangeCtx->GetSessionHandle()->AsIncomingGroupSession()->GetSubjectDescriptor();
GroupId groupId = mExchangeCtx->GetSessionHandle()->AsIncomingGroupSession()->GetGroupId();
FabricIndex fabric = GetAccessingFabricIndex();
while (CHIP_NO_ERROR == (err = aAttributeDataIBsReader.Next()))
{
chip::TLV::TLVReader dataReader;
AttributeDataIB::Parser element;
AttributePathIB::Parser attributePath;
ConcreteDataAttributePath dataAttributePath;
TLV::TLVReader reader = aAttributeDataIBsReader;
Credentials::GroupDataProvider::GroupEndpoint mapping;
Credentials::GroupDataProvider * groupDataProvider = Credentials::GetGroupDataProvider();
Credentials::GroupDataProvider::EndpointIterator * iterator;
err = element.Init(reader);
SuccessOrExit(err);
err = element.GetPath(&attributePath);
SuccessOrExit(err);
err = attributePath.GetGroupAttributePath(dataAttributePath);
SuccessOrExit(err);
err = element.GetData(&dataReader);
SuccessOrExit(err);
if (!dataAttributePath.IsListOperation() && dataReader.GetType() == TLV::TLVType::kTLVType_Array)
{
dataAttributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
}
ChipLogDetail(DataManagement,
"Received group attribute write for Group=%u Cluster=" ChipLogFormatMEI " attribute=" ChipLogFormatMEI,
groupId, ChipLogValueMEI(dataAttributePath.mClusterId), ChipLogValueMEI(dataAttributePath.mAttributeId));
iterator = groupDataProvider->IterateEndpoints(fabric);
VerifyOrExit(iterator != nullptr, err = CHIP_ERROR_NO_MEMORY);
bool shouldReportListWriteEnd =
ShouldReportListWriteEnd(mProcessingAttributePath, mProcessingAttributeIsList, dataAttributePath);
bool shouldReportListWriteBegin = false; // This will be set below.
const EmberAfAttributeMetadata * attributeMetadata = nullptr;
while (iterator->Next(mapping))
{
if (groupId != mapping.group_id)
{
continue;
}
dataAttributePath.mEndpointId = mapping.endpoint_id;
// Try to get the metadata from for the attribute from one of the expanded endpoints (it doesn't really matter which
// endpoint we pick, as long as it's valid) and update the path info according to it and recheck if we need to report
// list write begin.
if (attributeMetadata == nullptr)
{
attributeMetadata = GetAttributeMetadata(dataAttributePath);
bool currentAttributeIsList =
(attributeMetadata != nullptr && attributeMetadata->attributeType == kListAttributeType);
if (!dataAttributePath.IsListOperation() && currentAttributeIsList)
{
dataAttributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
}
ConcreteDataAttributePath pathForCheckingListWriteBegin(kInvalidEndpointId, dataAttributePath.mClusterId,
dataAttributePath.mEndpointId, dataAttributePath.mListOp,
dataAttributePath.mListIndex);
shouldReportListWriteBegin =
ShouldReportListWriteBegin(mProcessingAttributePath, mProcessingAttributeIsList, pathForCheckingListWriteBegin);
}
if (shouldReportListWriteEnd)
{
auto processingConcreteAttributePath = mProcessingAttributePath.Value();
processingConcreteAttributePath.mEndpointId = mapping.endpoint_id;
VerifyOrExit(mDelegate, err = CHIP_ERROR_INCORRECT_STATE);
if (mDelegate->HasConflictWriteRequests(this, processingConcreteAttributePath))
{
DeliverListWriteEnd(processingConcreteAttributePath, true /* writeWasSuccessful */);
}
}
VerifyOrExit(mDelegate, err = CHIP_ERROR_INCORRECT_STATE);
if (mDelegate->HasConflictWriteRequests(this, dataAttributePath))
{
ChipLogDetail(DataManagement,
"Writing attribute endpoint=%u Cluster=" ChipLogFormatMEI " attribute=" ChipLogFormatMEI
" is conflict with other write transactions.",
mapping.endpoint_id, ChipLogValueMEI(dataAttributePath.mClusterId),
ChipLogValueMEI(dataAttributePath.mAttributeId));
continue;
}
if (shouldReportListWriteBegin)
{
DeliverListWriteBegin(dataAttributePath);
}
ChipLogDetail(DataManagement,
"Processing group attribute write for endpoint=%u Cluster=" ChipLogFormatMEI
" attribute=" ChipLogFormatMEI,
mapping.endpoint_id, ChipLogValueMEI(dataAttributePath.mClusterId),
ChipLogValueMEI(dataAttributePath.mAttributeId));
chip::TLV::TLVReader tmpDataReader(dataReader);
DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
DataModelCallbacks::OperationOrder::Pre, dataAttributePath);
err = WriteSingleClusterData(subjectDescriptor, dataAttributePath, tmpDataReader, this);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DataManagement,
"WriteSingleClusterData Endpoint=%u Cluster=" ChipLogFormatMEI " Attribute =" ChipLogFormatMEI
" failed: %" CHIP_ERROR_FORMAT,
mapping.endpoint_id, ChipLogValueMEI(dataAttributePath.mClusterId),
ChipLogValueMEI(dataAttributePath.mAttributeId), err.Format());
}
DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Write,
DataModelCallbacks::OperationOrder::Post, dataAttributePath);
}
dataAttributePath.mEndpointId = kInvalidEndpointId;
mProcessingAttributeIsList = dataAttributePath.IsListOperation();
mProcessingAttributePath.SetValue(dataAttributePath);
iterator->Release();
}
if (CHIP_END_OF_TLV == err)
{
err = CHIP_NO_ERROR;
}
err = DeliverFinalListWriteEndForGroupWrite(true);
exit:
// The DeliverFinalListWriteEndForGroupWrite above will deliver the successful state of the list write and clear the
// mProcessingAttributePath making the following call no-op. So we call it again after the exit label to deliver a failure state
// to the clusters. Ignore the error code since we need to deliver other more important failures.
DeliverFinalListWriteEndForGroupWrite(false);
return err;
}
Status WriteHandler::ProcessWriteRequest(System::PacketBufferHandle && aPayload, bool aIsTimedWrite)
{
CHIP_ERROR err = CHIP_NO_ERROR;
System::PacketBufferTLVReader reader;
WriteRequestMessage::Parser writeRequestParser;
AttributeDataIBs::Parser AttributeDataIBsParser;
TLV::TLVReader AttributeDataIBsReader;
// Default to InvalidAction for our status; that's what we want if any of
// the parsing of our overall structure or paths fails. Once we have a
// successfully parsed path, the only way we will get a failure return is if
// our path handling fails to AddStatus on us.
//
// TODO: That's not technically InvalidAction, and we should probably make
// our callees hand out Status as well.
Status status = Status::InvalidAction;
reader.Init(std::move(aPayload));
err = writeRequestParser.Init(reader);
SuccessOrExit(err);
#if CHIP_CONFIG_IM_PRETTY_PRINT
writeRequestParser.PrettyPrint();
#endif
err = writeRequestParser.GetSuppressResponse(&mSuppressResponse);
if (err == CHIP_END_OF_TLV)
{
err = CHIP_NO_ERROR;
}
SuccessOrExit(err);
err = writeRequestParser.GetTimedRequest(&mIsTimedRequest);
SuccessOrExit(err);
err = writeRequestParser.GetMoreChunkedMessages(&mHasMoreChunks);
if (err == CHIP_ERROR_END_OF_TLV)
{
err = CHIP_NO_ERROR;
}
SuccessOrExit(err);
if (mHasMoreChunks && (mExchangeCtx->IsGroupExchangeContext() || mIsTimedRequest))
{
// Sanity check: group exchange context should only have one chunk.
// Also, timed requests should not have more than one chunk.
ExitNow(err = CHIP_ERROR_INVALID_MESSAGE_TYPE);
}
err = writeRequestParser.GetWriteRequests(&AttributeDataIBsParser);
SuccessOrExit(err);
if (mIsTimedRequest != aIsTimedWrite)
{
// The message thinks it should be part of a timed interaction but it's
// not, or vice versa.
status = Status::TimedRequestMismatch;
goto exit;
}
AttributeDataIBsParser.GetReader(&AttributeDataIBsReader);
if (mExchangeCtx->IsGroupExchangeContext())
{
err = ProcessGroupAttributeDataIBs(AttributeDataIBsReader);
}
else
{
err = ProcessAttributeDataIBs(AttributeDataIBsReader);
}
SuccessOrExit(err);
SuccessOrExit(err = writeRequestParser.ExitContainer());
if (err == CHIP_NO_ERROR)
{
status = Status::Success;
}
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(DataManagement, "Failed to process write request: %" CHIP_ERROR_FORMAT, err.Format());
}
return status;
}
CHIP_ERROR WriteHandler::AddStatus(const ConcreteDataAttributePath & aPath,
const Protocols::InteractionModel::ClusterStatusCode & aStatus)
{
return AddStatusInternal(aPath, StatusIB{ aStatus });
}
CHIP_ERROR WriteHandler::AddClusterSpecificSuccess(const ConcreteDataAttributePath & aPath, ClusterStatus aClusterStatus)
{
return AddStatus(aPath, Protocols::InteractionModel::ClusterStatusCode::ClusterSpecificSuccess(aClusterStatus));
}
CHIP_ERROR WriteHandler::AddClusterSpecificFailure(const ConcreteDataAttributePath & aPath, ClusterStatus aClusterStatus)
{
return AddStatus(aPath, Protocols::InteractionModel::ClusterStatusCode::ClusterSpecificFailure(aClusterStatus));
}
CHIP_ERROR WriteHandler::AddStatusInternal(const ConcreteDataAttributePath & aPath, const StatusIB & aStatus)
{
AttributeStatusIBs::Builder & writeResponses = mWriteResponseBuilder.GetWriteResponses();
AttributeStatusIB::Builder & attributeStatusIB = writeResponses.CreateAttributeStatus();
if (!aStatus.IsSuccess())
{
mAttributeWriteSuccessful = false;
}
ReturnErrorOnFailure(writeResponses.GetError());
AttributePathIB::Builder & path = attributeStatusIB.CreatePath();
ReturnErrorOnFailure(attributeStatusIB.GetError());
ReturnErrorOnFailure(path.Encode(aPath));
StatusIB::Builder & statusIBBuilder = attributeStatusIB.CreateErrorStatus();
ReturnErrorOnFailure(attributeStatusIB.GetError());
statusIBBuilder.EncodeStatusIB(aStatus);
ReturnErrorOnFailure(statusIBBuilder.GetError());
ReturnErrorOnFailure(attributeStatusIB.EndOfAttributeStatusIB());
MoveToState(State::AddStatus);
return CHIP_NO_ERROR;
}
FabricIndex WriteHandler::GetAccessingFabricIndex() const
{
return mExchangeCtx->GetSessionHandle()->GetFabricIndex();
}
const char * WriteHandler::GetStateStr() const
{
#if CHIP_DETAIL_LOGGING
switch (mState)
{
case State::Uninitialized:
return "Uninitialized";
case State::Initialized:
return "Initialized";
case State::AddStatus:
return "AddStatus";
case State::Sending:
return "Sending";
}
#endif // CHIP_DETAIL_LOGGING
return "N/A";
}
void WriteHandler::MoveToState(const State aTargetState)
{
mState = aTargetState;
ChipLogDetail(DataManagement, "IM WH moving to [%s]", GetStateStr());
}
} // namespace app
} // namespace chip