diff --git a/examples/darwin-framework-tool/commands/clusters/ClusterCommandBridge.h b/examples/darwin-framework-tool/commands/clusters/ClusterCommandBridge.h index 7bf34f11a62d94..50b9cfed5d912b 100644 --- a/examples/darwin-framework-tool/commands/clusters/ClusterCommandBridge.h +++ b/examples/darwin-framework-tool/commands/clusters/ClusterCommandBridge.h @@ -47,22 +47,8 @@ class ClusterCommand : public ModelCommand { CHIP_ERROR SendCommand(MTRBaseDevice * _Nonnull device, chip::EndpointId endpointId) override { - chip::TLV::TLVWriter writer; - chip::TLV::TLVReader reader; - - mData = static_cast(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen)); - VerifyOrReturnError(mData != nullptr, CHIP_ERROR_NO_MEMORY); - - writer.Init(mData, mDataMaxLen); - - ReturnErrorOnFailure(mPayload.Encode(writer, chip::TLV::AnonymousTag())); - reader.Init(mData, writer.GetLengthWritten()); - ReturnErrorOnFailure(reader.Next()); - - id commandFields = NSObjectFromCHIPTLV(&reader); - if (commandFields == nil) { - return CHIP_ERROR_INTERNAL; - } + id commandFields; + ReturnErrorOnFailure(GetCommandFields(&commandFields)); return ClusterCommand::SendCommand(device, endpointId, mClusterId, mCommandId, commandFields); } @@ -136,6 +122,35 @@ class ClusterCommand : public ModelCommand { NSError * _Nullable mError = nil; private: + CHIP_ERROR GetCommandFields(id _Nonnull * _Nonnull outCommandFields) + { + CHIP_ERROR err = CHIP_NO_ERROR; + chip::TLV::TLVWriter writer; + chip::TLV::TLVReader reader; + + mData = static_cast(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen)); + VerifyOrExit(mData != nullptr, err = CHIP_ERROR_NO_MEMORY); + + writer.Init(mData, mDataMaxLen); + + err = mPayload.Encode(writer, chip::TLV::AnonymousTag()); + SuccessOrExit(err); + + reader.Init(mData, writer.GetLengthWritten()); + err = reader.Next(); + SuccessOrExit(err); + + *outCommandFields = NSObjectFromCHIPTLV(&reader); + VerifyOrDo(nil != *outCommandFields, err = CHIP_ERROR_INTERNAL); + + exit: + if (nullptr != mData) { + chip::Platform::MemoryFree(mData); + mData = nullptr; + } + return err; + } + chip::ClusterId mClusterId; chip::CommandId mCommandId; diff --git a/examples/darwin-framework-tool/commands/clusters/WriteAttributeCommandBridge.h b/examples/darwin-framework-tool/commands/clusters/WriteAttributeCommandBridge.h index 66003042cccbe4..f274ea21aa654b 100644 --- a/examples/darwin-framework-tool/commands/clusters/WriteAttributeCommandBridge.h +++ b/examples/darwin-framework-tool/commands/clusters/WriteAttributeCommandBridge.h @@ -49,23 +49,8 @@ class WriteAttribute : public ModelCommand { CHIP_ERROR SendCommand(MTRBaseDevice * _Nonnull device, chip::EndpointId endpointId) override { - chip::TLV::TLVWriter writer; - chip::TLV::TLVReader reader; - - mData = static_cast(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen)); - VerifyOrReturnError(mData != nullptr, CHIP_ERROR_NO_MEMORY); - - writer.Init(mData, mDataMaxLen); - - ReturnErrorOnFailure(mAttributeValue.Encode(writer, chip::TLV::AnonymousTag())); - reader.Init(mData, writer.GetLengthWritten()); - ReturnErrorOnFailure(reader.Next()); - - id value = NSObjectFromCHIPTLV(&reader); - if (value == nil) { - return CHIP_ERROR_INTERNAL; - } - + id value; + ReturnErrorOnFailure(GetValue(&value)); return WriteAttribute::SendCommand(device, endpointId, mClusterId, mAttributeId, value); } @@ -122,6 +107,35 @@ class WriteAttribute : public ModelCommand { chip::Optional mDataVersion; private: + CHIP_ERROR GetValue(id _Nonnull * _Nonnull outValue) + { + CHIP_ERROR err = CHIP_NO_ERROR; + chip::TLV::TLVWriter writer; + chip::TLV::TLVReader reader; + + mData = static_cast(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen)); + VerifyOrExit(mData != nullptr, err = CHIP_ERROR_NO_MEMORY); + + writer.Init(mData, mDataMaxLen); + + err = mAttributeValue.Encode(writer, chip::TLV::AnonymousTag()); + SuccessOrExit(err); + + reader.Init(mData, writer.GetLengthWritten()); + err = reader.Next(); + SuccessOrExit(err); + + *outValue = NSObjectFromCHIPTLV(&reader); + VerifyOrDo(nil != *outValue, err = CHIP_ERROR_INTERNAL); + + exit: + if (nullptr != mData) { + chip::Platform::MemoryFree(mData); + mData = nullptr; + } + return err; + } + chip::ClusterId mClusterId; chip::AttributeId mAttributeId; CHIP_ERROR mError = CHIP_NO_ERROR;