From 3f2bba46006685b6d484e83c3785081de7f0056f Mon Sep 17 00:00:00 2001 From: Terence Hampson Date: Thu, 6 Jun 2024 15:32:24 +0000 Subject: [PATCH] Allow CommandSender request to be built using DataModel::EncodableToTLV --- src/app/CommandSender.cpp | 13 +++++++++ src/app/CommandSender.h | 56 ++++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/app/CommandSender.cpp b/src/app/CommandSender.cpp index af317f76003900..ef564f2d7515d9 100644 --- a/src/app/CommandSender.cpp +++ b/src/app/CommandSender.cpp @@ -537,6 +537,19 @@ CHIP_ERROR CommandSender::FinishCommand(FinishCommandParameters & aFinishCommand return FinishCommandInternal(aFinishCommandParams); } +CHIP_ERROR CommandSender::AddRequestDataInternal( + const CommandPathParams & aCommandPath, 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..6a78c770c5cf17 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,26 @@ class CommandSender final : public Messaging::ExchangeDelegate TLV::TLVWriter * GetCommandDataIBTLVWriter(); + /** + * API for adding a request data. The `aEncodable` is generally expected to encode + * a ClusterName::Commands::CommandName::Type struct, however any object should work. + * + * @param [in] aCommandPath The path of the command being requested. + * @param [in] aEncodable - an encodable that places the command data structure + * for `aResponseCommandId` into a TLV Writer. + * @param [in] aAddRequestDataParams parameters associated with building the + * InvokeRequestMessage that are associated with this request. + * + * This API does not validate if command provided requires timed invoke. If caller + * wants that certainty they should call templated version of AddRequestData. + */ + CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, + DataModel::EncodableToTLV & aEncodable, + AddRequestDataParameters & aAddRequestDataParams) + { + return AddRequestDataInternal(aCommandPath, aEncodable, aAddRequestDataParams); + } + /** * API for adding a data request. The template parameter T is generally * expected to be a ClusterName::Commands::CommandName::Type struct, but any @@ -388,24 +409,19 @@ class CommandSender final : public Messaging::ExchangeDelegate CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData) { AddRequestDataParameters addRequestDataParams; - return AddRequestData(aCommandPath, aData, addRequestDataParams); + DataModel::EncodableType encoder(aData); + return AddRequestData(aCommandPath, encoder, addRequestDataParams); } - template - 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); - } template CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData, const Optional & aTimedInvokeTimeoutMs) { + VerifyOrReturnError(!CommandDataT::MustUseTimedInvoke() || aTimedInvokeTimeoutMs.HasValue(), + CHIP_ERROR_INVALID_ARGUMENT); AddRequestDataParameters addRequestDataParams(aTimedInvokeTimeoutMs); - return AddRequestData(aCommandPath, aData, addRequestDataParams); + DataModel::EncodableType encoder(aData); + return AddRequestData(aCommandPath, encoder, addRequestDataParams); } /** @@ -426,7 +442,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 encoder(aData); + return AddRequestDataInternal(aCommandPath, encoder, aAddRequestDataParams); } CHIP_ERROR TestOnlyFinishCommand(FinishCommandParameters & aFinishCommandParams) @@ -448,18 +465,9 @@ 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 AddRequestDataInternal(const CommandPathParams & aCommandPath, + DataModel::EncodableToTLV & aEncodable, + AddRequestDataParameters & aAddRequestDataParams); CHIP_ERROR FinishCommandInternal(FinishCommandParameters & aFinishCommandParams);