|
28 | 28 | namespace chip {
|
29 | 29 | namespace app {
|
30 | 30 |
|
| 31 | +/** |
| 32 | + * Maintains the internal state of list encoding |
| 33 | + */ |
| 34 | +class AttributeEncodeState |
| 35 | +{ |
| 36 | +public: |
| 37 | + AttributeEncodeState() = default; |
| 38 | + |
| 39 | + bool AllowPartialData() const { return mAllowPartialData; } |
| 40 | + ListIndex CurrentEncodingListIndex() const { return mCurrentEncodingListIndex; } |
| 41 | + |
| 42 | + AttributeEncodeState & SetAllowPartialData(bool allow) |
| 43 | + { |
| 44 | + mAllowPartialData = allow; |
| 45 | + return *this; |
| 46 | + } |
| 47 | + |
| 48 | + AttributeEncodeState & SetCurrentEncodingListIndex(ListIndex idx) |
| 49 | + { |
| 50 | + mCurrentEncodingListIndex = idx; |
| 51 | + return *this; |
| 52 | + } |
| 53 | + |
| 54 | + void Reset() |
| 55 | + { |
| 56 | + mCurrentEncodingListIndex = kInvalidListIndex; |
| 57 | + mAllowPartialData = false; |
| 58 | + } |
| 59 | + |
| 60 | +private: |
| 61 | + /** |
| 62 | + * If set to kInvalidListIndex, indicates that we have not encoded any data for the list yet and |
| 63 | + * need to start by encoding an empty list before we start encoding any list items. |
| 64 | + * |
| 65 | + * When set to a valid ListIndex value, indicates the index of the next list item that needs to be |
| 66 | + * encoded (i.e. the count of items encoded so far). |
| 67 | + */ |
| 68 | + ListIndex mCurrentEncodingListIndex = kInvalidListIndex; |
| 69 | + |
| 70 | + /** |
| 71 | + * When an attempt to encode an attribute returns an error, the buffer may contain tailing dirty data |
| 72 | + * (since the put was aborted). The report engine normally rolls back the buffer to right before encoding |
| 73 | + * of the attribute started on errors. |
| 74 | + * |
| 75 | + * When chunking a list, EncodeListItem will atomically encode list items, ensuring that the |
| 76 | + * state of the buffer is valid to send (i.e. contains no trailing garbage), and return an error |
| 77 | + * if the list doesn't entirely fit. In this situation, mAllowPartialData is set to communicate to the |
| 78 | + * report engine that it should not roll back the list items. |
| 79 | + * |
| 80 | + * TODO: There might be a better name for this variable. |
| 81 | + */ |
| 82 | + bool mAllowPartialData = false; |
| 83 | +}; |
| 84 | + |
31 | 85 | /**
|
32 | 86 | * The AttributeValueEncoder is a helper class for filling report payloads into AttributeReportIBs.
|
33 | 87 | * The attribute value encoder can be initialized with a AttributeEncodeState for saving and recovering its state between encode
|
@@ -68,37 +122,6 @@ class AttributeValueEncoder
|
68 | 122 | AttributeValueEncoder & mAttributeValueEncoder;
|
69 | 123 | };
|
70 | 124 |
|
71 |
| - class AttributeEncodeState |
72 |
| - { |
73 |
| - public: |
74 |
| - AttributeEncodeState() : mAllowPartialData(false), mCurrentEncodingListIndex(kInvalidListIndex) {} |
75 |
| - bool AllowPartialData() const { return mAllowPartialData; } |
76 |
| - |
77 |
| - private: |
78 |
| - friend class AttributeValueEncoder; |
79 |
| - /** |
80 |
| - * When an attempt to encode an attribute returns an error, the buffer may contain tailing dirty data |
81 |
| - * (since the put was aborted). The report engine normally rolls back the buffer to right before encoding |
82 |
| - * of the attribute started on errors. |
83 |
| - * |
84 |
| - * When chunking a list, EncodeListItem will atomically encode list items, ensuring that the |
85 |
| - * state of the buffer is valid to send (i.e. contains no trailing garbage), and return an error |
86 |
| - * if the list doesn't entirely fit. In this situation, mAllowPartialData is set to communicate to the |
87 |
| - * report engine that it should not roll back the list items. |
88 |
| - * |
89 |
| - * TODO: There might be a better name for this variable. |
90 |
| - */ |
91 |
| - bool mAllowPartialData = false; |
92 |
| - /** |
93 |
| - * If set to kInvalidListIndex, indicates that we have not encoded any data for the list yet and |
94 |
| - * need to start by encoding an empty list before we start encoding any list items. |
95 |
| - * |
96 |
| - * When set to a valid ListIndex value, indicates the index of the next list item that needs to be |
97 |
| - * encoded (i.e. the count of items encoded so far). |
98 |
| - */ |
99 |
| - ListIndex mCurrentEncodingListIndex = kInvalidListIndex; |
100 |
| - }; |
101 |
| - |
102 | 125 | AttributeValueEncoder(AttributeReportIBs::Builder & aAttributeReportIBsBuilder, Access::SubjectDescriptor subjectDescriptor,
|
103 | 126 | const ConcreteAttributePath & aPath, DataVersion aDataVersion, bool aIsFabricFiltered = false,
|
104 | 127 | const AttributeEncodeState & aState = AttributeEncodeState()) :
|
@@ -194,7 +217,7 @@ class AttributeValueEncoder
|
194 | 217 | {
|
195 | 218 | // EncodeListItem must be called after EnsureListStarted(), thus mCurrentEncodingListIndex and
|
196 | 219 | // mEncodeState.mCurrentEncodingListIndex are not invalid values.
|
197 |
| - if (mCurrentEncodingListIndex < mEncodeState.mCurrentEncodingListIndex) |
| 220 | + if (mCurrentEncodingListIndex < mEncodeState.CurrentEncodingListIndex()) |
198 | 221 | {
|
199 | 222 | // We have encoded this element in previous chunks, skip it.
|
200 | 223 | mCurrentEncodingListIndex++;
|
@@ -225,7 +248,7 @@ class AttributeValueEncoder
|
225 | 248 | }
|
226 | 249 |
|
227 | 250 | mCurrentEncodingListIndex++;
|
228 |
| - mEncodeState.mCurrentEncodingListIndex++; |
| 251 | + mEncodeState.SetCurrentEncodingListIndex(mCurrentEncodingListIndex); |
229 | 252 | mEncodedAtLeastOneListItem = true;
|
230 | 253 | return CHIP_NO_ERROR;
|
231 | 254 | }
|
|
0 commit comments