33
33
#include < app/MessageDef/InvokeResponseMessage.h>
34
34
#include < app/MessageDef/StatusIB.h>
35
35
#include < app/PendingResponseTrackerImpl.h>
36
+ #include < app/data-model/EncodableToTLV.h>
36
37
#include < app/data-model/Encode.h>
37
38
#include < lib/core/CHIPCore.h>
38
39
#include < lib/core/Optional.h>
@@ -375,6 +376,22 @@ class CommandSender final : public Messaging::ExchangeDelegate
375
376
376
377
TLV::TLVWriter * GetCommandDataIBTLVWriter ();
377
378
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
+
378
395
/* *
379
396
* API for adding a data request. The template parameter T is generally
380
397
* expected to be a ClusterName::Commands::CommandName::Type struct, but any
@@ -391,15 +408,18 @@ class CommandSender final : public Messaging::ExchangeDelegate
391
408
return AddRequestData (aCommandPath, aData, addRequestDataParams);
392
409
}
393
410
394
- template <typename CommandDataT>
411
+ template <typename CommandDataT,
412
+ typename std::enable_if_t <!std::is_base_of_v<DataModel::EncodableToTLV, CommandDataT>, int > = 0 >
395
413
CHIP_ERROR AddRequestData (const CommandPathParams & aCommandPath, const CommandDataT & aData,
396
414
AddRequestDataParameters & aAddRequestDataParams)
397
415
{
398
416
VerifyOrReturnError (!CommandDataT::MustUseTimedInvoke () || aAddRequestDataParams.timedInvokeTimeoutMs .HasValue (),
399
417
CHIP_ERROR_INVALID_ARGUMENT);
400
418
401
- return AddRequestDataInternal (aCommandPath, aData, aAddRequestDataParams);
419
+ DataModel::EncodableType<CommandDataT> encodable (aData);
420
+ return AddRequestData (aCommandPath, encodable, aAddRequestDataParams);
402
421
}
422
+
403
423
template <typename CommandDataT>
404
424
CHIP_ERROR AddRequestData (const CommandPathParams & aCommandPath, const CommandDataT & aData,
405
425
const Optional<uint16_t > & aTimedInvokeTimeoutMs)
@@ -426,7 +446,8 @@ class CommandSender final : public Messaging::ExchangeDelegate
426
446
CHIP_ERROR TestOnlyAddRequestDataNoTimedCheck (const CommandPathParams & aCommandPath, const CommandDataT & aData,
427
447
AddRequestDataParameters & aAddRequestDataParams)
428
448
{
429
- return AddRequestDataInternal (aCommandPath, aData, aAddRequestDataParams);
449
+ DataModel::EncodableType<CommandDataT> encodable (aData);
450
+ return AddRequestData (aCommandPath, encodable, aAddRequestDataParams);
430
451
}
431
452
432
453
CHIP_ERROR TestOnlyFinishCommand (FinishCommandParameters & aFinishCommandParams)
@@ -448,19 +469,6 @@ class CommandSender final : public Messaging::ExchangeDelegate
448
469
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
449
470
450
471
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
-
464
472
CHIP_ERROR FinishCommandInternal (FinishCommandParameters & aFinishCommandParams);
465
473
466
474
public:
0 commit comments