|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-2024 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include <lib/core/DataModelTypes.h> |
| 20 | + |
| 21 | +namespace chip { |
| 22 | +namespace app { |
| 23 | + |
| 24 | +/** |
| 25 | + * Maintains the internal state of list encoding |
| 26 | + */ |
| 27 | +class AttributeEncodeState |
| 28 | +{ |
| 29 | +public: |
| 30 | + AttributeEncodeState() = default; |
| 31 | + |
| 32 | + bool AllowPartialData() const { return mAllowPartialData; } |
| 33 | + ListIndex CurrentEncodingListIndex() const { return mCurrentEncodingListIndex; } |
| 34 | + |
| 35 | + AttributeEncodeState & SetAllowPartialData(bool allow) |
| 36 | + { |
| 37 | + mAllowPartialData = allow; |
| 38 | + return *this; |
| 39 | + } |
| 40 | + |
| 41 | + AttributeEncodeState & SetCurrentEncodingListIndex(ListIndex idx) |
| 42 | + { |
| 43 | + mCurrentEncodingListIndex = idx; |
| 44 | + return *this; |
| 45 | + } |
| 46 | + |
| 47 | + void Reset() |
| 48 | + { |
| 49 | + mCurrentEncodingListIndex = kInvalidListIndex; |
| 50 | + mAllowPartialData = false; |
| 51 | + } |
| 52 | + |
| 53 | +private: |
| 54 | + /** |
| 55 | + * If set to kInvalidListIndex, indicates that we have not encoded any data for the list yet and |
| 56 | + * need to start by encoding an empty list before we start encoding any list items. |
| 57 | + * |
| 58 | + * When set to a valid ListIndex value, indicates the index of the next list item that needs to be |
| 59 | + * encoded (i.e. the count of items encoded so far). |
| 60 | + */ |
| 61 | + ListIndex mCurrentEncodingListIndex = kInvalidListIndex; |
| 62 | + |
| 63 | + /** |
| 64 | + * When an attempt to encode an attribute returns an error, the buffer may contain tailing dirty data |
| 65 | + * (since the put was aborted). The report engine normally rolls back the buffer to right before encoding |
| 66 | + * of the attribute started on errors. |
| 67 | + * |
| 68 | + * When chunking a list, EncodeListItem will atomically encode list items, ensuring that the |
| 69 | + * state of the buffer is valid to send (i.e. contains no trailing garbage), and return an error |
| 70 | + * if the list doesn't entirely fit. In this situation, mAllowPartialData is set to communicate to the |
| 71 | + * report engine that it should not roll back the list items. |
| 72 | + * |
| 73 | + * TODO: There might be a better name for this variable. |
| 74 | + */ |
| 75 | + bool mAllowPartialData = false; |
| 76 | +}; |
| 77 | + |
| 78 | +} // namespace app |
| 79 | +} // namespace chip |
0 commit comments