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 1473 : ConcreteAttributePath()
37 1473 : {
38 : // Note: mExpanded is in the superclass, so we can't use a field
39 : // initializer.
40 1473 : mExpanded = false;
41 1473 : }
42 :
43 5431 : ConcreteAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) :
44 5431 : ConcreteClusterPath(aEndpointId, aClusterId), mAttributeId(aAttributeId)
45 : {
46 : // Note: mExpanded is in the supercclass, so we can't use a field
47 : // initializer.
48 5431 : mExpanded = false;
49 5431 : }
50 :
51 3707 : bool IsValid() const { return ConcreteClusterPath::HasValidIds() && IsValidAttributeId(mAttributeId); }
52 :
53 17295 : bool operator==(const ConcreteAttributePath & aOther) const
54 : {
55 17295 : return ConcreteClusterPath::operator==(aOther) && (mAttributeId == aOther.mAttributeId);
56 : }
57 :
58 : bool operator!=(const ConcreteAttributePath & aOther) const { return !(*this == aOther); }
59 :
60 364 : bool operator<(const ConcreteAttributePath & path) const
61 : {
62 702 : return (mEndpointId < path.mEndpointId) || ((mEndpointId == path.mEndpointId) && (mClusterId < path.mClusterId)) ||
63 702 : ((mEndpointId == path.mEndpointId) && (mClusterId == path.mClusterId) && (mAttributeId < path.mAttributeId));
64 : }
65 :
66 : AttributeId mAttributeId = 0;
67 : };
68 :
69 : /**
70 : * A representation of a concrete path as it appears in a Read or Subscribe
71 : * request after path expansion. This contains support for expressing an
72 : * optional list index.
73 : */
74 : struct ConcreteReadAttributePath : public ConcreteAttributePath
75 : {
76 1930 : ConcreteReadAttributePath() {}
77 :
78 6305 : ConcreteReadAttributePath(const ConcreteAttributePath & path) : ConcreteAttributePath(path) {}
79 :
80 : ConcreteReadAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) :
81 : ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId)
82 : {}
83 :
84 : ConcreteReadAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId, uint16_t aListIndex) :
85 : ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId)
86 : {
87 : mListIndex.SetValue(aListIndex);
88 : }
89 :
90 : Optional<uint16_t> mListIndex;
91 : };
92 :
93 : /**
94 : * A representation of a concrete path as it appears in a Report or Write
95 : * request after path expansion. This contains support for expressing list and list item-specific operations
96 : * like replace, update, delete and append.
97 : */
98 : struct ConcreteDataAttributePath : public ConcreteAttributePath
99 : {
100 : enum class ListOperation
101 : {
102 : NotList, // Path points to an attribute that isn't a list.
103 : ReplaceAll, // Path points to an attribute that is a list, indicating that the contents of the list should be replaced in
104 : // its entirety.
105 : ReplaceItem, // Path points to a specific item in a list, indicating that that item should be replaced in its entirety.
106 : DeleteItem, // Path points to a specific item in a list, indicating that that item should be deleted from the list.
107 : AppendItem // Path points to an attribute that is a list, indicating that an item should be appended into the list.
108 : };
109 :
110 5475 : ConcreteDataAttributePath() {}
111 :
112 13 : ConcreteDataAttributePath(const ConcreteAttributePath & path) : ConcreteAttributePath(path) {}
113 :
114 4950 : ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) :
115 4950 : ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId)
116 4950 : {}
117 :
118 : ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId,
119 : const Optional<DataVersion> & aDataVersion) :
120 : ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId),
121 : mDataVersion(aDataVersion)
122 : {}
123 :
124 0 : ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId, ListOperation aListOp,
125 0 : uint16_t aListIndex) :
126 0 : ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId)
127 : {
128 0 : mListOp = aListOp;
129 0 : mListIndex = aListIndex;
130 0 : }
131 :
132 19627 : bool IsListOperation() const { return mListOp != ListOperation::NotList; }
133 152 : bool IsListItemOperation() const { return ((mListOp != ListOperation::NotList) && (mListOp != ListOperation::ReplaceAll)); }
134 :
135 : void LogPath() const
136 : {
137 : ChipLogProgress(DataManagement, "Concrete Attribute Path: (%d, " ChipLogFormatMEI ", " ChipLogFormatMEI ") ", mEndpointId,
138 : ChipLogValueMEI(mClusterId), ChipLogValueMEI(mAttributeId));
139 : }
140 :
141 : //
142 : // This index is only valid if `mListOp` is set to a list item operation, i.e
143 : // ReplaceItem, DeleteItem or AppendItem. Otherwise, it is to be ignored.
144 : //
145 : uint16_t mListIndex = 0;
146 : ListOperation mListOp = ListOperation::NotList;
147 : Optional<DataVersion> mDataVersion = NullOptional;
148 : };
149 :
150 : } // namespace app
151 : } // namespace chip
|