33
33
#include < app/CommandHandlerExchangeInterface.h>
34
34
#include < app/CommandPathRegistry.h>
35
35
#include < app/ConcreteCommandPath.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/TLV.h>
56
57
namespace chip {
57
58
namespace app {
58
59
59
- // / Defines an abstract class of something that can be encoded
60
- // / into a TLV with a given data tag
61
- class EncoderToTLV
62
- {
63
- public:
64
- virtual ~EncoderToTLV () = default ;
65
-
66
- virtual CHIP_ERROR Encode (TLV::TLVWriter &, TLV::Tag tag) = 0;
67
- };
68
-
69
- // / An `EncoderToTLV` the uses `DataModel::Encode` to encode things.
70
- // /
71
- // / Generally useful to encode things like <ClusterName>::Commands::<CommandName>::Type
72
- // / structures.
73
- template <typename T>
74
- class DataModelEncoderToTLV : public EncoderToTLV
75
- {
76
- public:
77
- DataModelEncoderToTLV (const T & value) : mValue (value) {}
78
-
79
- virtual CHIP_ERROR Encode (TLV::TLVWriter & writer, TLV::Tag tag) { return DataModel::Encode (writer, tag, mValue ); }
80
-
81
- private:
82
- const T & mValue ;
83
- };
84
-
85
60
class CommandHandler
86
61
{
87
62
public:
@@ -267,7 +242,7 @@ class CommandHandler
267
242
* Adds the given command status and returns any failures in adding statuses (e.g. out
268
243
* of buffer space) to the caller
269
244
*/
270
- CHIP_ERROR FallibleAddStatus (const ConcreteCommandPath & aCommandPath , const Protocols::InteractionModel::Status aStatus,
245
+ CHIP_ERROR FallibleAddStatus (const ConcreteCommandPath & aRequestCommandPath , const Protocols::InteractionModel::Status aStatus,
271
246
const char * context = nullptr );
272
247
273
248
/* *
@@ -277,9 +252,9 @@ class CommandHandler
277
252
void AddStatus (const ConcreteCommandPath & aCommandPath, const Protocols::InteractionModel::Status aStatus,
278
253
const char * context = nullptr );
279
254
280
- CHIP_ERROR AddClusterSpecificSuccess (const ConcreteCommandPath & aCommandPath , ClusterStatus aClusterStatus);
255
+ CHIP_ERROR AddClusterSpecificSuccess (const ConcreteCommandPath & aRequestCommandPath , ClusterStatus aClusterStatus);
281
256
282
- CHIP_ERROR AddClusterSpecificFailure (const ConcreteCommandPath & aCommandPath , ClusterStatus aClusterStatus);
257
+ CHIP_ERROR AddClusterSpecificFailure (const ConcreteCommandPath & aRequestCommandPath , ClusterStatus aClusterStatus);
283
258
284
259
/* *
285
260
* This adds a new CommandDataIB element into InvokeResponses for the associated
@@ -350,37 +325,34 @@ class CommandHandler
350
325
* @param [in] aRequestCommandPath the concrete path of the command we are
351
326
* responding to.
352
327
* @param [in] aData the data for the response.
353
- *
354
- * NOTE: this is a convenience function for `AddResponseDataViaEncoder`
355
328
*/
356
329
template <typename CommandData>
357
- inline CHIP_ERROR AddResponseData (const ConcreteCommandPath & aRequestCommandPath, const CommandData & aData)
330
+ CHIP_ERROR AddResponseData (const ConcreteCommandPath & aRequestCommandPath, const CommandData & aData)
358
331
{
359
- DataModelEncoderToTLV <CommandData> encoder (aData);
360
- return AddResponseDataViaEncoder (aRequestCommandPath, CommandData::GetCommandId (), encoder);
332
+ DataModel::EncodableType <CommandData> encoder (aData);
333
+ return AddResponseData (aRequestCommandPath, CommandData::GetCommandId (), encoder);
361
334
}
362
335
363
336
/* *
364
- * API for adding a data response. The encoded is generally expected to encode
365
- * a ClusterName::Commands::CommandName::Type struct, but any
366
- * object should work.
337
+ * API for adding a data response. The `aEncodable` is generally expected to encode
338
+ * a ClusterName::Commands::CommandName::Type struct, however any object should work.
367
339
*
368
340
* @param [in] aRequestCommandPath the concrete path of the command we are
369
341
* responding to.
370
- * @param [in] commandId the command whose content is being encoded.
371
- * @param [in] encoder - an encoder that places the command data structure for `commandId`
372
- * into a TLV Writer.
342
+ * @param [in] aResponseCommandId the command whose content is being encoded.
343
+ * @param [in] aEncodable - an encodable that places the command data structure
344
+ * for `aResponseCommandId` into a TLV Writer.
373
345
*
374
346
* Most applications are likely to use `AddResponseData` as a more convenient
375
347
* one-call that auto-sets command ID and creates the underlying encoders.
376
348
*/
377
- CHIP_ERROR AddResponseDataViaEncoder (const ConcreteCommandPath & aRequestCommandPath, CommandId commandId ,
378
- EncoderToTLV & encoder )
349
+ CHIP_ERROR AddResponseData (const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId ,
350
+ DataModel::EncodableToTLV & aEncodable )
379
351
{
380
352
// Return early when response should not be sent out.
381
353
VerifyOrReturnValue (ResponsesAccepted (), CHIP_NO_ERROR);
382
354
return TryAddingResponse (
383
- [&]() -> CHIP_ERROR { return TryAddResponseDataViaEncoder (aRequestCommandPath, commandId, encoder ); });
355
+ [&]() -> CHIP_ERROR { return TryAddResponseData (aRequestCommandPath, aResponseCommandId, aEncodable ); });
384
356
}
385
357
386
358
/* *
@@ -398,21 +370,22 @@ class CommandHandler
398
370
* @param [in] aData the data for the response.
399
371
*/
400
372
template <typename CommandData>
401
- inline void AddResponse (const ConcreteCommandPath & aRequestCommandPath, const CommandData & aData)
373
+ void AddResponse (const ConcreteCommandPath & aRequestCommandPath, const CommandData & aData)
402
374
{
403
- DataModelEncoderToTLV <CommandData> encoder (aData);
404
- return AddResponseViaEncoder (aRequestCommandPath, CommandData::GetCommandId (), encoder );
375
+ DataModel::EncodableType <CommandData> encodable (aData);
376
+ return AddResponse (aRequestCommandPath, CommandData::GetCommandId (), encodable );
405
377
}
406
378
407
379
/* *
408
- * API for adding a response with a given encoder of TLV data.
380
+ * API for adding a response with a given encodable of TLV data.
409
381
*
410
- * The encoder would generally encode a ClusterName::Commands::CommandName::Type with
382
+ * The encodable would generally encode a ClusterName::Commands::CommandName::Type with
411
383
* the corresponding `GetCommandId` call.
412
384
*/
413
- void AddResponseViaEncoder (const ConcreteCommandPath & aRequestCommandPath, CommandId commandId, EncoderToTLV & encoder)
385
+ void AddResponse (const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
386
+ DataModel::EncodableToTLV & aEncodable)
414
387
{
415
- if (AddResponseDataViaEncoder (aRequestCommandPath, commandId, encoder ) != CHIP_NO_ERROR)
388
+ if (AddResponseData (aRequestCommandPath, aResponseCommandId, aEncodable ) != CHIP_NO_ERROR)
416
389
{
417
390
AddStatus (aRequestCommandPath, Protocols::InteractionModel::Status::Failure);
418
391
}
@@ -666,49 +639,17 @@ class CommandHandler
666
639
667
640
CHIP_ERROR AddStatusInternal (const ConcreteCommandPath & aCommandPath, const StatusIB & aStatus);
668
641
669
- /* *
670
- * Non-templated function called before DataModel::Encode when attempting to add a response,
671
- * which does all the work needed before encoding the actual type-dependent data into the buffer.
672
- *
673
- * **Important:** If this function fails, the TLV buffer may be left in an inconsistent state.
674
- * Callers should create snapshots as necessary before invoking this function and implement
675
- * rollback mechanisms if needed.
676
- *
677
- * **Usage:** This function is intended to be called exclusively by TryAddResponseData. It was
678
- * factored out to optimize code size.
679
- *
680
- * @param aRequestCommandPath The concrete path of the command being responded to.
681
- * @param aResponseCommandPath The concrete path of the command response.
682
- */
683
- CHIP_ERROR TryAddResponseDataPreEncode (const ConcreteCommandPath & aRequestCommandPath,
684
- const ConcreteCommandPath & aResponseCommandPath)
685
- {
686
- InvokeResponseParameters prepareParams (aRequestCommandPath);
687
- prepareParams.SetStartOrEndDataStruct (false );
688
-
689
- ScopedChange<bool > internalCallToAddResponse (mInternalCallToAddResponseData , true );
690
- return PrepareInvokeResponseCommand (aResponseCommandPath, prepareParams);
691
- }
692
-
693
642
/* *
694
643
* If this function fails, it may leave our TLV buffer in an inconsistent state.
695
644
* Callers should snapshot as needed before calling this function, and roll back
696
645
* as needed afterward.
697
646
*
698
- * @param [in] aRequestCommandPath the concrete path of the command we are
699
- * responding to.
700
- * @param [in] aData the data for the response.
647
+ * @param [in] aRequestCommandPath the concrete path of the command we are responding to
648
+ * @param [in] aResponseCommandId the id of the command to encode
649
+ * @param [in] aEncodable the data to encode for the given aResponseCommandId
701
650
*/
702
- CHIP_ERROR TryAddResponseDataViaEncoder (const ConcreteCommandPath & aRequestCommandPath, CommandId commandId,
703
- EncoderToTLV & encoder)
704
- {
705
- ConcreteCommandPath responseCommandPath = { aRequestCommandPath.mEndpointId , aRequestCommandPath.mClusterId , commandId };
706
- ReturnErrorOnFailure (TryAddResponseDataPreEncode (aRequestCommandPath, responseCommandPath));
707
- TLV::TLVWriter * writer = GetCommandDataIBTLVWriter ();
708
- VerifyOrReturnError (writer != nullptr , CHIP_ERROR_INCORRECT_STATE);
709
- ReturnErrorOnFailure (encoder.Encode (*writer, TLV::ContextTag (CommandDataIB::Tag::kFields )));
710
- return FinishCommand (/* aEndDataStruct = */ false );
711
- }
651
+ CHIP_ERROR TryAddResponseData (const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
652
+ DataModel::EncodableToTLV & aEncodable);
712
653
713
654
void SetExchangeInterface (CommandHandlerExchangeInterface * commandResponder);
714
655
0 commit comments