Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow CommandSender request to be built using DataModel::EncodableToTLV #33782

12 changes: 12 additions & 0 deletions src/app/CommandSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 24 additions & 16 deletions src/app/CommandSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <app/MessageDef/InvokeResponseMessage.h>
#include <app/MessageDef/StatusIB.h>
#include <app/PendingResponseTrackerImpl.h>
#include <app/data-model/EncodableToTLV.h>
#include <app/data-model/Encode.h>
#include <lib/core/CHIPCore.h>
#include <lib/core/Optional.h>
Expand Down Expand Up @@ -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
Expand All @@ -391,15 +408,18 @@ class CommandSender final : public Messaging::ExchangeDelegate
return AddRequestData(aCommandPath, aData, addRequestDataParams);
}

template <typename CommandDataT>
template <typename CommandDataT,
typename std::enable_if_t<!std::is_base_of_v<DataModel::EncodableToTLV, CommandDataT>, 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<CommandDataT> encodable(aData);
return AddRequestData(aCommandPath, encodable, aAddRequestDataParams);
}

template <typename CommandDataT>
CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData,
const Optional<uint16_t> & aTimedInvokeTimeoutMs)
Expand All @@ -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<CommandDataT> encodable(aData);
return AddRequestData(aCommandPath, encodable, aAddRequestDataParams);
}

CHIP_ERROR TestOnlyFinishCommand(FinishCommandParameters & aFinishCommandParams)
Expand All @@ -448,19 +469,6 @@ class CommandSender final : public Messaging::ExchangeDelegate
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST

private:
template <typename CommandDataT>
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:
Expand Down
Loading