Skip to content

Commit 1133276

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Some interface cleanup on AttributeValueEncoder. (#12661)
1) Address unaddressed or deferred review comments from #12019. 2) Add a simpler way to encode a null value.
1 parent 5b9a9b0 commit 1133276

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/app/AttributeAccessInterface.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ CHIP_ERROR AttributeValueEncoder::EncodeEmptyList()
6969
// Put an empty array before encoding the first array element for list chunking.
7070
AttributeReportBuilder builder;
7171

72+
mPath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
7273
ReturnErrorOnFailure(builder.PrepareAttribute(mAttributeReportIBsBuilder, mPath, mDataVersion));
7374
ReturnErrorOnFailure(builder.EncodeValue(mAttributeReportIBsBuilder, DataModel::List<uint8_t>()));
7475

@@ -82,6 +83,9 @@ CHIP_ERROR AttributeValueEncoder::EncodeEmptyList()
8283
// revert partial data.
8384
mEncodeState.mAllowPartialData = true;
8485

86+
// For all elements in the list, a report with append operation will be generated. This will not be changed during encoding
87+
// of each report since the users cannot access mPath.
88+
mPath.mListOp = ConcreteDataAttributePath::ListOperation::AppendItem;
8589
return CHIP_NO_ERROR;
8690
}
8791

src/app/AttributeAccessInterface.h

+22-9
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ class AttributeValueEncoder
147147
{}
148148

149149
/**
150-
* Encode builds a single AttributeReportIB in AttributeReportIBs.
151-
* When we are encoding a single element in the list, the actual path in the report contains a null list index as "append"
152-
* operation.
150+
* Encode a single value. This value will not be chunked; it will either be
151+
* entirely encoded or fail to be encoded. Consumers are allowed to make
152+
* either one call to Encode or one call to EncodeList to handle a read.
153153
*/
154154
template <typename... Ts>
155155
CHIP_ERROR Encode(Ts &&... aArgs)
@@ -158,6 +158,15 @@ class AttributeValueEncoder
158158
return EncodeAttributeReportIB(std::forward<Ts>(aArgs)...);
159159
}
160160

161+
/**
162+
* Encode an explicit null value.
163+
*/
164+
CHIP_ERROR EncodeNull()
165+
{
166+
// Doesn't matter what type Nullable we use here.
167+
return Encode(DataModel::Nullable<uint8_t>());
168+
}
169+
161170
/**
162171
* aCallback is expected to take a const auto & argument and Encode() on it as many times as needed to encode all the list
163172
* elements one by one. If any of those Encode() calls returns failure, aCallback must stop encoding and return failure. When
@@ -181,11 +190,7 @@ class AttributeValueEncoder
181190
// EmptyList acts as the beginning of the whole array type attribute report.
182191
// An empty list is encoded iff both mCurrentEncodingListIndex and mEncodeState.mCurrentEncodingListIndex are invalid
183192
// values. After encoding the empty list, mEncodeState.mCurrentEncodingListIndex and mCurrentEncodingListIndex are set to 0.
184-
mPath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll;
185193
ReturnErrorOnFailure(EncodeEmptyList());
186-
// For all elements in the list, a report with append operation will be generated. This will not be changed during encoding
187-
// of each report since the users cannot access mPath.
188-
mPath.mListOp = ConcreteDataAttributePath::ListOperation::AppendItem;
189194
ReturnErrorOnFailure(aCallback(ListEncodeHelper(*this)));
190195
// The Encode procedure finished without any error, clear the state.
191196
mEncodeState = AttributeEncodeState();
@@ -239,12 +244,16 @@ class AttributeValueEncoder
239244
}
240245

241246
/**
242-
* Actual logic for encoding a single AttributeReportIB in AttributeReportIBs.
247+
* Builds a single AttributeReportIB in AttributeReportIBs. The caller is
248+
* responsible for setting up mPath correctly.
249+
*
250+
* In particular, when we are encoding a single element in the list, mPath
251+
* must indicate a null list index to represent an "append" operation.
252+
* operation.
243253
*/
244254
template <typename... Ts>
245255
CHIP_ERROR EncodeAttributeReportIB(Ts &&... aArgs)
246256
{
247-
mTriedEncode = true;
248257
AttributeReportBuilder builder;
249258

250259
ReturnErrorOnFailure(builder.PrepareAttribute(mAttributeReportIBsBuilder, mPath, mDataVersion));
@@ -258,6 +267,10 @@ class AttributeValueEncoder
258267
*
259268
* If internal state indicates we have already encoded the empty list, this function will encode nothing, set
260269
* mCurrentEncodingListIndex to 0 and return CHIP_NO_ERROR.
270+
*
271+
* In all cases this function guarantees that mPath.mListOp is AppendItem
272+
* after it returns, because at that point we will be encoding the list
273+
* items.
261274
*/
262275
CHIP_ERROR EncodeEmptyList();
263276

0 commit comments

Comments
 (0)