16
16
* limitations under the License.
17
17
*/
18
18
19
- #include " messaging/ExchangeContext.h"
20
19
#include < app/AppConfig.h>
21
20
#include < app/AttributeAccessInterfaceRegistry.h>
21
+ #include < app/AttributeValueDecoder.h>
22
22
#include < app/InteractionModelEngine.h>
23
23
#include < app/MessageDef/EventPathIB.h>
24
24
#include < app/MessageDef/StatusIB.h>
25
25
#include < app/StatusResponse.h>
26
26
#include < app/WriteHandler.h>
27
+ #include < app/data-model-provider/OperationTypes.h>
27
28
#include < app/reporting/Engine.h>
28
29
#include < app/util/MatterCallbacks.h>
29
30
#include < app/util/ember-compatibility-functions.h>
30
31
#include < credentials/GroupDataProvider.h>
32
+ #include < lib/core/CHIPError.h>
33
+ #include < lib/support/CodeUtils.h>
31
34
#include < lib/support/TypeTraits.h>
35
+ #include < messaging/ExchangeContext.h>
32
36
#include < protocols/interaction_model/StatusCode.h>
33
37
34
38
namespace chip {
@@ -38,10 +42,14 @@ using namespace Protocols::InteractionModel;
38
42
using Status = Protocols::InteractionModel::Status;
39
43
constexpr uint8_t kListAttributeType = 0x48 ;
40
44
41
- CHIP_ERROR WriteHandler::Init (WriteHandlerDelegate * apWriteHandlerDelegate)
45
+ CHIP_ERROR WriteHandler::Init (DataModel::Provider * apProvider, WriteHandlerDelegate * apWriteHandlerDelegate)
42
46
{
43
47
VerifyOrReturnError (!mExchangeCtx , CHIP_ERROR_INCORRECT_STATE);
44
48
VerifyOrReturnError (apWriteHandlerDelegate, CHIP_ERROR_INVALID_ARGUMENT);
49
+ #if CHIP_CONFIG_USE_DATA_MODEL_INTERFACE
50
+ VerifyOrReturnError (apProvider, CHIP_ERROR_INVALID_ARGUMENT);
51
+ mDataModelProvider = apProvider;
52
+ #endif
45
53
46
54
mDelegate = apWriteHandlerDelegate;
47
55
MoveToState (State::Initialized);
@@ -63,6 +71,9 @@ void WriteHandler::Close()
63
71
DeliverFinalListWriteEnd (false /* wasSuccessful */ );
64
72
mExchangeCtx .Release ();
65
73
mSuppressResponse = false ;
74
+ #if CHIP_CONFIG_USE_DATA_MODEL_INTERFACE
75
+ mDataModelProvider = nullptr ;
76
+ #endif
66
77
MoveToState (State::Uninitialized);
67
78
}
68
79
@@ -351,7 +362,7 @@ CHIP_ERROR WriteHandler::ProcessAttributeDataIBs(TLV::TLVReader & aAttributeData
351
362
err = CHIP_NO_ERROR;
352
363
}
353
364
SuccessOrExit (err);
354
- err = WriteSingleClusterData (subjectDescriptor, dataAttributePath, dataReader, this );
365
+ err = WriteClusterData (subjectDescriptor, dataAttributePath, dataReader);
355
366
if (err != CHIP_NO_ERROR)
356
367
{
357
368
mWriteResponseBuilder .GetWriteResponses ().Rollback (backup);
@@ -497,12 +508,11 @@ CHIP_ERROR WriteHandler::ProcessGroupAttributeDataIBs(TLV::TLVReader & aAttribut
497
508
498
509
DataModelCallbacks::GetInstance ()->AttributeOperation (DataModelCallbacks::OperationType::Write,
499
510
DataModelCallbacks::OperationOrder::Pre, dataAttributePath);
500
- err = WriteSingleClusterData (subjectDescriptor, dataAttributePath, tmpDataReader, this );
501
-
511
+ err = WriteClusterData (subjectDescriptor, dataAttributePath, tmpDataReader);
502
512
if (err != CHIP_NO_ERROR)
503
513
{
504
514
ChipLogError (DataManagement,
505
- " WriteSingleClusterData Endpoint=%u Cluster=" ChipLogFormatMEI " Attribute =" ChipLogFormatMEI
515
+ " WriteClusterData Endpoint=%u Cluster=" ChipLogFormatMEI " Attribute =" ChipLogFormatMEI
506
516
" failed: %" CHIP_ERROR_FORMAT,
507
517
mapping.endpoint_id , ChipLogValueMEI (dataAttributePath.mClusterId ),
508
518
ChipLogValueMEI (dataAttributePath.mAttributeId ), err.Format ());
@@ -691,5 +701,29 @@ void WriteHandler::MoveToState(const State aTargetState)
691
701
ChipLogDetail (DataManagement, " IM WH moving to [%s]" , GetStateStr ());
692
702
}
693
703
704
+ CHIP_ERROR WriteHandler::WriteClusterData (const Access::SubjectDescriptor & subject, const ConcreteDataAttributePath & path,
705
+ TLV::TLVReader & data)
706
+ {
707
+ // Writes do not have a checked-path. If data model interface is enabled (both checked and only version)
708
+ // the write is done via the DataModel interface
709
+ #if CHIP_CONFIG_USE_DATA_MODEL_INTERFACE
710
+ VerifyOrReturnError (mDataModelProvider != nullptr , CHIP_ERROR_INCORRECT_STATE);
711
+
712
+ DataModel::WriteAttributeRequest request;
713
+
714
+ request.path = path;
715
+ request.subjectDescriptor = subject;
716
+ request.writeFlags .Set (DataModel::WriteFlags::kTimed , IsTimedWrite ());
717
+
718
+ AttributeValueDecoder decoder (data, subject);
719
+
720
+ DataModel::ActionReturnStatus status = mDataModelProvider ->WriteAttribute (request, decoder);
721
+
722
+ return AddStatusInternal (path, StatusIB (status.GetStatusCode ()));
723
+ #else
724
+ return WriteSingleClusterData (subject, path, data, this );
725
+ #endif // CHIP_CONFIG_USE_DATA_MODEL_INTERFACE
726
+ }
727
+
694
728
} // namespace app
695
729
} // namespace chip
0 commit comments