Skip to content

Commit b29c1de

Browse files
authored
Allow CommandSender request to be built using DataModel::EncodableToTLV (#33782)
Allow CommandSender request to be built using DataModel::EncodableToTLV
1 parent 23e0f3b commit b29c1de

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

src/app/CommandSender.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,18 @@ CHIP_ERROR CommandSender::FinishCommand(FinishCommandParameters & aFinishCommand
537537
return FinishCommandInternal(aFinishCommandParams);
538538
}
539539

540+
CHIP_ERROR CommandSender::AddRequestData(const CommandPathParams & aCommandPath, const DataModel::EncodableToTLV & aEncodable,
541+
AddRequestDataParameters & aAddRequestDataParams)
542+
{
543+
PrepareCommandParameters prepareCommandParams(aAddRequestDataParams);
544+
ReturnErrorOnFailure(PrepareCommand(aCommandPath, prepareCommandParams));
545+
TLV::TLVWriter * writer = GetCommandDataIBTLVWriter();
546+
VerifyOrReturnError(writer != nullptr, CHIP_ERROR_INCORRECT_STATE);
547+
ReturnErrorOnFailure(aEncodable.EncodeTo(*writer, TLV::ContextTag(CommandDataIB::Tag::kFields)));
548+
FinishCommandParameters finishCommandParams(aAddRequestDataParams);
549+
return FinishCommand(finishCommandParams);
550+
}
551+
540552
CHIP_ERROR CommandSender::FinishCommandInternal(FinishCommandParameters & aFinishCommandParams)
541553
{
542554
CHIP_ERROR err = CHIP_NO_ERROR;

src/app/CommandSender.h

+24-16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <app/MessageDef/InvokeResponseMessage.h>
3434
#include <app/MessageDef/StatusIB.h>
3535
#include <app/PendingResponseTrackerImpl.h>
36+
#include <app/data-model/EncodableToTLV.h>
3637
#include <app/data-model/Encode.h>
3738
#include <lib/core/CHIPCore.h>
3839
#include <lib/core/Optional.h>
@@ -375,6 +376,22 @@ class CommandSender final : public Messaging::ExchangeDelegate
375376

376377
TLV::TLVWriter * GetCommandDataIBTLVWriter();
377378

379+
/**
380+
* API for adding request data using DataModel::EncodableToTLV.
381+
*
382+
* @param [in] aCommandPath The path of the command being requested.
383+
* @param [in] aEncodable The request data to encode into the
384+
* `CommandFields` member of `CommandDataIB`.
385+
* @param [in] aAddRequestDataParams parameters associated with building the
386+
* InvokeRequestMessage that are associated with this request.
387+
*
388+
* This API will not fail if this is an untimed invoke but the command provided requires a timed
389+
* invoke interaction. If the caller wants that to fail before sending the command, they should call
390+
* the templated version of AddRequestData.
391+
*/
392+
CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const DataModel::EncodableToTLV & aEncodable,
393+
AddRequestDataParameters & aAddRequestDataParams);
394+
378395
/**
379396
* API for adding a data request. The template parameter T is generally
380397
* expected to be a ClusterName::Commands::CommandName::Type struct, but any
@@ -391,15 +408,18 @@ class CommandSender final : public Messaging::ExchangeDelegate
391408
return AddRequestData(aCommandPath, aData, addRequestDataParams);
392409
}
393410

394-
template <typename CommandDataT>
411+
template <typename CommandDataT,
412+
typename std::enable_if_t<!std::is_base_of_v<DataModel::EncodableToTLV, CommandDataT>, int> = 0>
395413
CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData,
396414
AddRequestDataParameters & aAddRequestDataParams)
397415
{
398416
VerifyOrReturnError(!CommandDataT::MustUseTimedInvoke() || aAddRequestDataParams.timedInvokeTimeoutMs.HasValue(),
399417
CHIP_ERROR_INVALID_ARGUMENT);
400418

401-
return AddRequestDataInternal(aCommandPath, aData, aAddRequestDataParams);
419+
DataModel::EncodableType<CommandDataT> encodable(aData);
420+
return AddRequestData(aCommandPath, encodable, aAddRequestDataParams);
402421
}
422+
403423
template <typename CommandDataT>
404424
CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData,
405425
const Optional<uint16_t> & aTimedInvokeTimeoutMs)
@@ -426,7 +446,8 @@ class CommandSender final : public Messaging::ExchangeDelegate
426446
CHIP_ERROR TestOnlyAddRequestDataNoTimedCheck(const CommandPathParams & aCommandPath, const CommandDataT & aData,
427447
AddRequestDataParameters & aAddRequestDataParams)
428448
{
429-
return AddRequestDataInternal(aCommandPath, aData, aAddRequestDataParams);
449+
DataModel::EncodableType<CommandDataT> encodable(aData);
450+
return AddRequestData(aCommandPath, encodable, aAddRequestDataParams);
430451
}
431452

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

450471
private:
451-
template <typename CommandDataT>
452-
CHIP_ERROR AddRequestDataInternal(const CommandPathParams & aCommandPath, const CommandDataT & aData,
453-
AddRequestDataParameters & aAddRequestDataParams)
454-
{
455-
PrepareCommandParameters prepareCommandParams(aAddRequestDataParams);
456-
ReturnErrorOnFailure(PrepareCommand(aCommandPath, prepareCommandParams));
457-
TLV::TLVWriter * writer = GetCommandDataIBTLVWriter();
458-
VerifyOrReturnError(writer != nullptr, CHIP_ERROR_INCORRECT_STATE);
459-
ReturnErrorOnFailure(DataModel::Encode(*writer, TLV::ContextTag(CommandDataIB::Tag::kFields), aData));
460-
FinishCommandParameters finishCommandParams(aAddRequestDataParams);
461-
return FinishCommand(finishCommandParams);
462-
}
463-
464472
CHIP_ERROR FinishCommandInternal(FinishCommandParameters & aFinishCommandParams);
465473

466474
public:

0 commit comments

Comments
 (0)