Skip to content

Commit 3f2bba4

Browse files
committed
Allow CommandSender request to be built using DataModel::EncodableToTLV
1 parent d15f6c1 commit 3f2bba4

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

src/app/CommandSender.cpp

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

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

src/app/CommandSender.h

+32-24
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,26 @@ class CommandSender final : public Messaging::ExchangeDelegate
375376

376377
TLV::TLVWriter * GetCommandDataIBTLVWriter();
377378

379+
/**
380+
* API for adding a request data. The `aEncodable` is generally expected to encode
381+
* a ClusterName::Commands::CommandName::Type struct, however any object should work.
382+
*
383+
* @param [in] aCommandPath The path of the command being requested.
384+
* @param [in] aEncodable - an encodable that places the command data structure
385+
* for `aResponseCommandId` into a TLV Writer.
386+
* @param [in] aAddRequestDataParams parameters associated with building the
387+
* InvokeRequestMessage that are associated with this request.
388+
*
389+
* This API does not validate if command provided requires timed invoke. If caller
390+
* wants that certainty they should call templated version of AddRequestData.
391+
*/
392+
CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath,
393+
DataModel::EncodableToTLV & aEncodable,
394+
AddRequestDataParameters & aAddRequestDataParams)
395+
{
396+
return AddRequestDataInternal(aCommandPath, aEncodable, aAddRequestDataParams);
397+
}
398+
378399
/**
379400
* API for adding a data request. The template parameter T is generally
380401
* expected to be a ClusterName::Commands::CommandName::Type struct, but any
@@ -388,24 +409,19 @@ class CommandSender final : public Messaging::ExchangeDelegate
388409
CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData)
389410
{
390411
AddRequestDataParameters addRequestDataParams;
391-
return AddRequestData(aCommandPath, aData, addRequestDataParams);
412+
DataModel::EncodableType<CommandDataT> encoder(aData);
413+
return AddRequestData(aCommandPath, encoder, addRequestDataParams);
392414
}
393415

394-
template <typename CommandDataT>
395-
CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData,
396-
AddRequestDataParameters & aAddRequestDataParams)
397-
{
398-
VerifyOrReturnError(!CommandDataT::MustUseTimedInvoke() || aAddRequestDataParams.timedInvokeTimeoutMs.HasValue(),
399-
CHIP_ERROR_INVALID_ARGUMENT);
400-
401-
return AddRequestDataInternal(aCommandPath, aData, aAddRequestDataParams);
402-
}
403416
template <typename CommandDataT>
404417
CHIP_ERROR AddRequestData(const CommandPathParams & aCommandPath, const CommandDataT & aData,
405418
const Optional<uint16_t> & aTimedInvokeTimeoutMs)
406419
{
420+
VerifyOrReturnError(!CommandDataT::MustUseTimedInvoke() || aTimedInvokeTimeoutMs.HasValue(),
421+
CHIP_ERROR_INVALID_ARGUMENT);
407422
AddRequestDataParameters addRequestDataParams(aTimedInvokeTimeoutMs);
408-
return AddRequestData(aCommandPath, aData, addRequestDataParams);
423+
DataModel::EncodableType<CommandDataT> encoder(aData);
424+
return AddRequestData(aCommandPath, encoder, addRequestDataParams);
409425
}
410426

411427
/**
@@ -426,7 +442,8 @@ class CommandSender final : public Messaging::ExchangeDelegate
426442
CHIP_ERROR TestOnlyAddRequestDataNoTimedCheck(const CommandPathParams & aCommandPath, const CommandDataT & aData,
427443
AddRequestDataParameters & aAddRequestDataParams)
428444
{
429-
return AddRequestDataInternal(aCommandPath, aData, aAddRequestDataParams);
445+
DataModel::EncodableType<CommandDataT> encoder(aData);
446+
return AddRequestDataInternal(aCommandPath, encoder, aAddRequestDataParams);
430447
}
431448

432449
CHIP_ERROR TestOnlyFinishCommand(FinishCommandParameters & aFinishCommandParams)
@@ -448,18 +465,9 @@ class CommandSender final : public Messaging::ExchangeDelegate
448465
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
449466

450467
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-
}
468+
CHIP_ERROR AddRequestDataInternal(const CommandPathParams & aCommandPath,
469+
DataModel::EncodableToTLV & aEncodable,
470+
AddRequestDataParameters & aAddRequestDataParams);
463471

464472
CHIP_ERROR FinishCommandInternal(FinishCommandParameters & aFinishCommandParams);
465473

0 commit comments

Comments
 (0)