diff --git a/src/app/CommandSender.cpp b/src/app/CommandSender.cpp index af317f76003900..fe263eb8923cc7 100644 --- a/src/app/CommandSender.cpp +++ b/src/app/CommandSender.cpp @@ -537,6 +537,18 @@ CHIP_ERROR CommandSender::FinishCommand(FinishCommandParameters & aFinishCommand return FinishCommandInternal(aFinishCommandParams); } +CHIP_ERROR CommandSender::AddRequestData(const CommandPathParams & aCommandPath, const DataModel::EncodableToTLV & aEncodable, + AddRequestDataParameters & aAddRequestDataParams) +{ + PrepareCommandParameters prepareCommandParams(aAddRequestDataParams); + ReturnErrorOnFailure(PrepareCommand(aCommandPath, prepareCommandParams)); + TLV::TLVWriter * writer = GetCommandDataIBTLVWriter(); + VerifyOrReturnError(writer != nullptr, CHIP_ERROR_INCORRECT_STATE); + ReturnErrorOnFailure(aEncodable.EncodeTo(*writer, TLV::ContextTag(CommandDataIB::Tag::kFields))); + FinishCommandParameters finishCommandParams(aAddRequestDataParams); + return FinishCommand(finishCommandParams); +} + CHIP_ERROR CommandSender::FinishCommandInternal(FinishCommandParameters & aFinishCommandParams) { CHIP_ERROR err = CHIP_NO_ERROR; diff --git a/src/app/CommandSender.h b/src/app/CommandSender.h index 25603b3a1f3787..5542d2469bb743 100644 --- a/src/app/CommandSender.h +++ b/src/app/CommandSender.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -375,6 +376,22 @@ class CommandSender final : public Messaging::ExchangeDelegate TLV::TLVWriter * GetCommandDataIBTLVWriter(); + /** + * API for adding request data using DataModel::EncodableToTLV. + * + * @param [in] aCommandPath The path of the command being requested. + * @param [in] aEncodable The request data to encode into the + * `CommandFields` member of `CommandDataIB`. + * @param [in] aAddRequestDataParams parameters associated with building the + * InvokeRequestMessage that are associated with this request. + * + * This API will not fail if this is an untimed invoke but the command provided requires a timed + * invoke interaction. If the caller wants that to fail before sending the command, they should call + * the templated version of AddRequestData. + */ + CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const DataModel::EncodableToTLV & aEncodable, + AddRequestDataParameters & aAddRequestDataParams); + /** * API for adding a data request. The template parameter T is generally * expected to be a ClusterName::Commands::CommandName::Type struct, but any @@ -391,15 +408,18 @@ class CommandSender final : public Messaging::ExchangeDelegate return AddRequestData(aCommandPath, aData, addRequestDataParams); } - template + template , int> = 0> CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData, AddRequestDataParameters & aAddRequestDataParams) { VerifyOrReturnError(!CommandDataT::MustUseTimedInvoke() || aAddRequestDataParams.timedInvokeTimeoutMs.HasValue(), CHIP_ERROR_INVALID_ARGUMENT); - return AddRequestDataInternal(aCommandPath, aData, aAddRequestDataParams); + DataModel::EncodableType encodable(aData); + return AddRequestData(aCommandPath, encodable, aAddRequestDataParams); } + template CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData, const Optional & aTimedInvokeTimeoutMs) @@ -426,7 +446,8 @@ class CommandSender final : public Messaging::ExchangeDelegate CHIP_ERROR TestOnlyAddRequestDataNoTimedCheck(const CommandPathParams & aCommandPath, const CommandDataT & aData, AddRequestDataParameters & aAddRequestDataParams) { - return AddRequestDataInternal(aCommandPath, aData, aAddRequestDataParams); + DataModel::EncodableType encodable(aData); + return AddRequestData(aCommandPath, encodable, aAddRequestDataParams); } CHIP_ERROR TestOnlyFinishCommand(FinishCommandParameters & aFinishCommandParams) @@ -448,19 +469,6 @@ class CommandSender final : public Messaging::ExchangeDelegate #endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST private: - template - CHIP_ERROR AddRequestDataInternal(const CommandPathParams & aCommandPath, const CommandDataT & aData, - AddRequestDataParameters & aAddRequestDataParams) - { - PrepareCommandParameters prepareCommandParams(aAddRequestDataParams); - ReturnErrorOnFailure(PrepareCommand(aCommandPath, prepareCommandParams)); - TLV::TLVWriter * writer = GetCommandDataIBTLVWriter(); - VerifyOrReturnError(writer != nullptr, CHIP_ERROR_INCORRECT_STATE); - ReturnErrorOnFailure(DataModel::Encode(*writer, TLV::ContextTag(CommandDataIB::Tag::kFields), aData)); - FinishCommandParameters finishCommandParams(aAddRequestDataParams); - return FinishCommand(finishCommandParams); - } - CHIP_ERROR FinishCommandInternal(FinishCommandParameters & aFinishCommandParams); public: