Skip to content

Commit 4a71023

Browse files
committed
move AttributeEncodeState to a separate file
1 parent 8ae866f commit 4a71023

File tree

3 files changed

+82
-55
lines changed

3 files changed

+82
-55
lines changed

src/app/AttributeEncodeState.h

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

src/app/AttributeValueEncoder.h

+1-54
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <access/SubjectDescriptor.h>
2020
#include <app/AttributeReportBuilder.h>
21+
#include <app/AttributeEncodeState.h>
2122
#include <app/ConcreteAttributePath.h>
2223
#include <app/MessageDef/AttributeReportIBs.h>
2324
#include <app/data-model/FabricScoped.h>
@@ -28,60 +29,6 @@
2829
namespace chip {
2930
namespace app {
3031

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-
8532
/**
8633
* The AttributeValueEncoder is a helper class for filling report payloads into AttributeReportIBs.
8734
* The attribute value encoder can be initialized with a AttributeEncodeState for saving and recovering its state between encode

src/app/BUILD.gn

+2-1
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,11 @@ source_set("events") {
277277

278278
static_library("attribute-access") {
279279
sources = [
280-
"AttributeAccessInterface.h",
281280
"AttributeAccessInterfaceCache.h",
281+
"AttributeAccessInterface.h",
282282
"AttributeAccessInterfaceRegistry.cpp",
283283
"AttributeAccessInterfaceRegistry.h",
284+
"AttributeEncodeState.h",
284285
"AttributeReportBuilder.cpp",
285286
"AttributeReportBuilder.h",
286287
"AttributeValueDecoder.h",

0 commit comments

Comments
 (0)