Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fcd9fe5

Browse files
committedMay 29, 2024
Renames based on review comments
1 parent 2ac70c0 commit fcd9fe5

File tree

4 files changed

+41
-54
lines changed

4 files changed

+41
-54
lines changed
 

‎src/app/CommandHandler.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,22 @@ Status CommandHandler::OnInvokeCommandRequest(CommandHandlerExchangeInterface &
113113
return status;
114114
}
115115

116-
CHIP_ERROR CommandHandler::TryAddResponseData(const ConcreteCommandPath & path, CommandId commandId,
117-
DataModel::EncoderToTLV & encoder)
116+
CHIP_ERROR CommandHandler::TryAddResponseData(const ConcreteCommandPath & aCommandPath, CommandId aResponseCommandId,
117+
DataModel::EncodableToTLV & aEncodable)
118118
{
119-
ConcreteCommandPath responseCommandPath = { path.mEndpointId, path.mClusterId, commandId };
120-
ReturnErrorOnFailure(TryAddResponseDataPreEncode(path, responseCommandPath));
119+
ConcreteCommandPath responseCommandPath = { aCommandPath.mEndpointId, aCommandPath.mClusterId, aResponseCommandId };
120+
121+
InvokeResponseParameters prepareParams(aRequestCommandPath);
122+
prepareParams.SetStartOrEndDataStruct(false);
123+
124+
{
125+
ScopedChange<bool> internalCallToAddResponse(mInternalCallToAddResponseData, true);
126+
ReturnErrorOnFailure(PrepareInvokeResponseCommand(aResponseCommandPath, prepareParams));
127+
}
128+
121129
TLV::TLVWriter * writer = GetCommandDataIBTLVWriter();
122130
VerifyOrReturnError(writer != nullptr, CHIP_ERROR_INCORRECT_STATE);
123-
ReturnErrorOnFailure(encoder.Encode(*writer, TLV::ContextTag(CommandDataIB::Tag::kFields)));
131+
ReturnErrorOnFailure(aEncodable.EncodeTo(*writer, TLV::ContextTag(CommandDataIB::Tag::kFields)));
124132
return FinishCommand(/* aEndDataStruct = */ false);
125133
}
126134

‎src/app/CommandHandler.h

+20-41
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
#include <app/CommandHandlerExchangeInterface.h>
3434
#include <app/CommandPathRegistry.h>
3535
#include <app/ConcreteCommandPath.h>
36+
#include <app/data-model/EncodableToTLV.h>
3637
#include <app/data-model/Encode.h>
37-
#include <app/data-model/EncoderToTLV.h>
3838
#include <lib/core/CHIPCore.h>
3939
#include <lib/core/TLV.h>
4040
#include <lib/core/TLVDebug.h>
@@ -329,7 +329,7 @@ class CommandHandler
329329
template <typename CommandData>
330330
CHIP_ERROR AddResponseData(const ConcreteCommandPath & aRequestCommandPath, const CommandData & aData)
331331
{
332-
DataModel::ObjectEncoderToTLV<CommandData> encoder(aData);
332+
DataModel::ObjectEncodableToTLV<CommandData> encoder(aData);
333333
return AddResponseData(aRequestCommandPath, CommandData::GetCommandId(), encoder);
334334
}
335335

@@ -340,19 +340,20 @@ class CommandHandler
340340
*
341341
* @param [in] aRequestCommandPath the concrete path of the command we are
342342
* responding to.
343-
* @param [in] commandId the command whose content is being encoded.
344-
* @param [in] encoder - an encoder that places the command data structure for `commandId`
343+
* @param [in] aResponseCommandId the command whose content is being encoded.
344+
* @param [in] encoder - an encoder that places the command data structure for `aResponseCommandId`
345345
* into a TLV Writer.
346346
*
347347
* Most applications are likely to use `AddResponseData` as a more convenient
348348
* one-call that auto-sets command ID and creates the underlying encoders.
349349
*/
350-
CHIP_ERROR AddResponseData(const ConcreteCommandPath & aRequestCommandPath, CommandId commandId,
351-
DataModel::EncoderToTLV & encoder)
350+
CHIP_ERROR AddResponseData(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
351+
DataModel::EncodableToTLV & aEncodable)
352352
{
353353
// Return early when response should not be sent out.
354354
VerifyOrReturnValue(ResponsesAccepted(), CHIP_NO_ERROR);
355-
return TryAddingResponse([&]() -> CHIP_ERROR { return TryAddResponseData(aRequestCommandPath, commandId, encoder); });
355+
return TryAddingResponse(
356+
[&]() -> CHIP_ERROR { return TryAddResponseData(aRequestCommandPath, aResponseCommandId, aEncodable); });
356357
}
357358

358359
/**
@@ -372,19 +373,20 @@ class CommandHandler
372373
template <typename CommandData>
373374
void AddResponse(const ConcreteCommandPath & aRequestCommandPath, const CommandData & aData)
374375
{
375-
DataModel::ObjectEncoderToTLV<CommandData> encoder(aData);
376-
return AddResponse(aRequestCommandPath, CommandData::GetCommandId(), encoder);
376+
DataModel::ObjectEncodableToTLV<CommandData> encodable(aData);
377+
return AddResponse(aRequestCommandPath, CommandData::GetCommandId(), encodable);
377378
}
378379

379380
/**
380-
* API for adding a response with a given encoder of TLV data.
381+
* API for adding a response with a given encodable of TLV data.
381382
*
382-
* The encoder would generally encode a ClusterName::Commands::CommandName::Type with
383+
* The encodable would generally encode a ClusterName::Commands::CommandName::Type with
383384
* the corresponding `GetCommandId` call.
384385
*/
385-
void AddResponse(const ConcreteCommandPath & aRequestCommandPath, CommandId commandId, DataModel::EncoderToTLV & encoder)
386+
void AddResponse(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
387+
DataModel::EncodableToTLV & encodable)
386388
{
387-
if (AddResponseData(aRequestCommandPath, commandId, encoder) != CHIP_NO_ERROR)
389+
if (AddResponseData(aRequestCommandPath, aResponseCommandId, encodable) != CHIP_NO_ERROR)
388390
{
389391
AddStatus(aRequestCommandPath, Protocols::InteractionModel::Status::Failure);
390392
}
@@ -638,40 +640,17 @@ class CommandHandler
638640

639641
CHIP_ERROR AddStatusInternal(const ConcreteCommandPath & aCommandPath, const StatusIB & aStatus);
640642

641-
/**
642-
* Non-templated function called before DataModel::Encode when attempting to add a response,
643-
* which does all the work needed before encoding the actual type-dependent data into the buffer.
644-
*
645-
* **Important:** If this function fails, the TLV buffer may be left in an inconsistent state.
646-
* Callers should create snapshots as necessary before invoking this function and implement
647-
* rollback mechanisms if needed.
648-
*
649-
* **Usage:** This function is intended to be called exclusively by TryAddResponseData. It was
650-
* factored out to optimize code size.
651-
*
652-
* @param aRequestCommandPath The concrete path of the command being responded to.
653-
* @param aResponseCommandPath The concrete path of the command response.
654-
*/
655-
CHIP_ERROR TryAddResponseDataPreEncode(const ConcreteCommandPath & aRequestCommandPath,
656-
const ConcreteCommandPath & aResponseCommandPath)
657-
{
658-
InvokeResponseParameters prepareParams(aRequestCommandPath);
659-
prepareParams.SetStartOrEndDataStruct(false);
660-
661-
ScopedChange<bool> internalCallToAddResponse(mInternalCallToAddResponseData, true);
662-
return PrepareInvokeResponseCommand(aResponseCommandPath, prepareParams);
663-
}
664-
665643
/**
666644
* If this function fails, it may leave our TLV buffer in an inconsistent state.
667645
* Callers should snapshot as needed before calling this function, and roll back
668646
* as needed afterward.
669647
*
670-
* @param [in] path the concrete path of the command we are responding to
671-
* @param [in] commandId the id of the command to encode
672-
* @param [in] encoder the data to encode for the given commandId
648+
* @param [in] aCommandPath the concrete path of the command we are responding to
649+
* @param [in] aResponseCommandId the id of the command to encode
650+
* @param [in] aEncodable the data to encode for the given aResponseCommandId
673651
*/
674-
CHIP_ERROR TryAddResponseData(const ConcreteCommandPath & path, CommandId commandId, DataModel::EncoderToTLV & encoder);
652+
CHIP_ERROR TryAddResponseData(const ConcreteCommandPath & aCommandPath, CommandId aResponseCommandId,
653+
DataModel::EncodableToTLV & aEncodable);
675654

676655
void SetExchangeInterface(CommandHandlerExchangeInterface * commandResponder);
677656

‎src/app/data-model/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ source_set("data-model") {
1818
"BasicTypes.h",
1919
"DecodableList.h",
2020
"Decode.h",
21+
"EncodableToTLV.h",
2122
"Encode.h",
22-
"EncoderToTLV.h",
2323
"FabricScoped.h",
2424
"FabricScopedPreEncodedValue.cpp",
2525
"FabricScopedPreEncodedValue.h",

‎src/app/data-model/EncoderToTLV.h ‎src/app/data-model/EncodableToTLV.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,30 @@ namespace DataModel {
2727

2828
/// Defines an abstract class of something that can be encoded
2929
/// into a TLV with a given data tag
30-
class EncoderToTLV
30+
class EncodableToTLV
3131
{
3232
public:
33-
virtual ~EncoderToTLV() = default;
33+
virtual ~EncodableToTLV() = default;
3434

35-
virtual CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag) = 0;
35+
virtual CHIP_ERROR EncodeTo(TLV::TLVWriter & writer, TLV::Tag tag) = 0;
3636
};
3737

38-
/// An `EncoderToTLV` that uses `DataModel::Encode` to encode things.
38+
/// An `EncodableToTLV` that uses `DataModel::Encode` to encode things.
3939
///
4040
/// Generally useful to encode things like <ClusterName>::Commands::<CommandName>::Type
4141
/// structures.
4242
template <typename T>
43-
class ObjectEncoderToTLV : public EncoderToTLV
43+
class ObjectEncodableToTLV : public EncodableToTLV
4444
{
4545
public:
4646
/// Encodes the given value via `DataModel::Encode` when the underlying
4747
/// encode is called.
4848
///
4949
/// LIFETIME NOTE: uses a reference to value, so value must live longer than
5050
/// this object.
51-
ObjectEncoderToTLV(const T & value) : mValue(value) {}
51+
ObjectEncodableToTLV(const T & value) : mValue(value) {}
5252

53-
CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag) override { return DataModel::Encode(writer, tag, mValue); }
53+
CHIP_ERROR EncodeTo(TLV::TLVWriter & writer, TLV::Tag tag) override { return DataModel::Encode(writer, tag, mValue); }
5454

5555
private:
5656
const T & mValue;

0 commit comments

Comments
 (0)
Please sign in to comment.