Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021 Project CHIP Authors
4 : * All rights reserved.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : #pragma once
20 :
21 : #include <app/ConcreteClusterPath.h>
22 : #include <app/util/basic-types.h>
23 : #include <lib/core/Optional.h>
24 :
25 : namespace chip {
26 : namespace app {
27 :
28 : /**
29 : * A representation of a concrete attribute path. This does not convey any list index specifiers.
30 : *
31 : * The expanded flag can be set to indicate that a concrete path was expanded from a wildcard
32 : * or group path.
33 : */
34 : struct ConcreteAttributePath : public ConcreteClusterPath
35 : {
36 19970 : CHIP_CPP20(constexpr) ConcreteAttributePath()
37 19970 : {
38 : // Note: mExpanded is in the superclass, so we can't use a field
39 : // initializer.
40 19970 : mExpanded = false;
41 19970 : }
42 :
43 : CHIP_CPP20(constexpr)
44 57781 : ConcreteAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) :
45 57781 : ConcreteClusterPath(aEndpointId, aClusterId), mAttributeId(aAttributeId)
46 : {
47 : // Note: mExpanded is in the supercclass, so we can't use a field
48 : // initializer.
49 57781 : mExpanded = false;
50 57781 : }
51 :
52 5216 : bool IsValid() const { return ConcreteClusterPath::HasValidIds() && IsValidAttributeId(mAttributeId); }
53 :
54 24778 : bool operator==(const ConcreteAttributePath & aOther) const
55 : {
56 24778 : return ConcreteClusterPath::operator==(aOther) && (mAttributeId == aOther.mAttributeId);
57 : }
58 :
59 15 : bool operator!=(const ConcreteAttributePath & aOther) const { return !(*this == aOther); }
60 :
61 533 : bool operator<(const ConcreteAttributePath & path) const
62 : {
63 1016 : return (mEndpointId < path.mEndpointId) || ((mEndpointId == path.mEndpointId) && (mClusterId < path.mClusterId)) ||
64 1016 : ((mEndpointId == path.mEndpointId) && (mClusterId == path.mClusterId) && (mAttributeId < path.mAttributeId));
65 : }
66 :
67 : AttributeId mAttributeId = 0;
68 : };
69 :
70 : /**
71 : * A representation of a concrete path as it appears in a Read or Subscribe
72 : * request after path expansion. This contains support for expressing an
73 : * optional list index.
74 : */
75 : struct ConcreteReadAttributePath : public ConcreteAttributePath
76 : {
77 : ConcreteReadAttributePath() {}
78 :
79 13205 : ConcreteReadAttributePath(const ConcreteAttributePath & path) : ConcreteAttributePath(path) {}
80 :
81 1900 : ConcreteReadAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) :
82 1900 : ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId)
83 1900 : {}
84 :
85 : ConcreteReadAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId, uint16_t aListIndex) :
86 : ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId)
87 : {
88 : mListIndex.SetValue(aListIndex);
89 : }
90 :
91 : bool operator==(const ConcreteReadAttributePath & aOther) const = delete;
92 : bool operator!=(const ConcreteReadAttributePath & aOther) const = delete;
93 : bool operator<(const ConcreteReadAttributePath & aOther) const = delete;
94 :
95 : Optional<uint16_t> mListIndex;
96 : };
97 :
98 : /**
99 : * A representation of a concrete path as it appears in a Report or Write
100 : * request after path expansion. This contains support for expressing list and list item-specific operations
101 : * like replace, update, delete and append.
102 : */
103 : struct ConcreteDataAttributePath : public ConcreteAttributePath
104 : {
105 : enum class ListOperation : uint8_t
106 : {
107 : NotList, // Path points to an attribute that isn't a list.
108 : ReplaceAll, // Path points to an attribute that is a list, indicating that the contents of the list should be replaced in
109 : // its entirety.
110 : ReplaceItem, // Path points to a specific item in a list, indicating that that item should be replaced in its entirety.
111 : DeleteItem, // Path points to a specific item in a list, indicating that that item should be deleted from the list.
112 : AppendItem // Path points to an attribute that is a list, indicating that an item should be appended into the list.
113 : };
114 :
115 18633 : ConcreteDataAttributePath() {}
116 :
117 430 : ConcreteDataAttributePath(const ConcreteAttributePath & path) : ConcreteAttributePath(path) {}
118 :
119 7147 : ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) :
120 7147 : ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId)
121 7147 : {}
122 :
123 1026 : ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId,
124 1026 : const Optional<DataVersion> & aDataVersion) :
125 : ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId),
126 1026 : mDataVersion(aDataVersion)
127 1026 : {}
128 :
129 13 : ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId, ListOperation aListOp,
130 13 : uint16_t aListIndex) :
131 13 : ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId)
132 : {
133 13 : mListOp = aListOp;
134 13 : mListIndex = aListIndex;
135 13 : }
136 :
137 30764 : bool IsListOperation() const { return mListOp != ListOperation::NotList; }
138 19453 : bool IsListItemOperation() const { return ((mListOp != ListOperation::NotList) && (mListOp != ListOperation::ReplaceAll)); }
139 :
140 : void LogPath() const
141 : {
142 : ChipLogProgress(DataManagement, "Concrete Attribute Path: (%d, " ChipLogFormatMEI ", " ChipLogFormatMEI ") ", mEndpointId,
143 : ChipLogValueMEI(mClusterId), ChipLogValueMEI(mAttributeId));
144 : }
145 :
146 5211 : bool MatchesConcreteAttributePath(const ConcreteAttributePath & aOther) const
147 : {
148 5211 : return ConcreteAttributePath::operator==(aOther);
149 : }
150 :
151 11 : bool operator==(const ConcreteDataAttributePath & aOther) const
152 : {
153 18 : return ConcreteAttributePath::operator==(aOther) && (mListIndex == aOther.mListIndex) && (mListOp == aOther.mListOp) &&
154 18 : (mDataVersion == aOther.mDataVersion);
155 : }
156 :
157 5 : bool operator!=(const ConcreteDataAttributePath & aOther) const { return !(*this == aOther); }
158 :
159 : bool operator<(const ConcreteDataAttributePath & aOther) const = delete;
160 :
161 18453 : bool operator==(const ConcreteAttributePath & aOther) const { return ConcreteAttributePath::operator==(aOther); }
162 :
163 : //
164 : // This index is only valid if `mListOp` is set to a list item operation, i.e
165 : // ReplaceItem, DeleteItem or AppendItem. Otherwise, it is to be ignored.
166 : //
167 : uint16_t mListIndex = 0;
168 : ListOperation mListOp = ListOperation::NotList;
169 : Optional<DataVersion> mDataVersion = NullOptional;
170 : };
171 :
172 : } // namespace app
173 : } // namespace chip
|