Skip to content

Commit

Permalink
Allow CommandSender request to be built using DataModel::EncodableToT…
Browse files Browse the repository at this point in the history
…LV (#33782)

Allow CommandSender request to be built using DataModel::EncodableToTLV
  • Loading branch information
tehampson authored Jun 11, 2024
1 parent 23e0f3b commit b29c1de
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
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

0 comments on commit b29c1de

Please sign in to comment.