Skip to content

Commit

Permalink
Allow CommandSender request to be built using DataModel::EncodableToTLV
Browse files Browse the repository at this point in the history
  • Loading branch information
tehampson committed Jun 6, 2024
1 parent d15f6c1 commit 3f2bba4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
13 changes: 13 additions & 0 deletions src/app/CommandSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
56 changes: 32 additions & 24 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,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
Expand All @@ -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<CommandDataT> encoder(aData);
return AddRequestData(aCommandPath, encoder, addRequestDataParams);
}

template <typename CommandDataT>
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 <typename CommandDataT>
CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData,
const Optional<uint16_t> & aTimedInvokeTimeoutMs)
{
VerifyOrReturnError(!CommandDataT::MustUseTimedInvoke() || aTimedInvokeTimeoutMs.HasValue(),
CHIP_ERROR_INVALID_ARGUMENT);
AddRequestDataParameters addRequestDataParams(aTimedInvokeTimeoutMs);
return AddRequestData(aCommandPath, aData, addRequestDataParams);
DataModel::EncodableType<CommandDataT> encoder(aData);
return AddRequestData(aCommandPath, encoder, addRequestDataParams);
}

/**
Expand All @@ -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<CommandDataT> encoder(aData);
return AddRequestDataInternal(aCommandPath, encoder, aAddRequestDataParams);
}

CHIP_ERROR TestOnlyFinishCommand(FinishCommandParameters & aFinishCommandParams)
Expand All @@ -448,18 +465,9 @@ 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 AddRequestDataInternal(const CommandPathParams & aCommandPath,
DataModel::EncodableToTLV & aEncodable,
AddRequestDataParameters & aAddRequestDataParams);

CHIP_ERROR FinishCommandInternal(FinishCommandParameters & aFinishCommandParams);

Expand Down

0 comments on commit 3f2bba4

Please sign in to comment.