blob: cfde88ab9bd374a0ba57d5582e2f9290f6629e15 [file] [log] [blame]
Vivien Nicolas02cbf692021-10-11 20:46:29 +02001/*
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
Boris Zbarsky1afe0062022-02-09 02:12:13 -050021#include <app/ConcreteClusterPath.h>
Vivien Nicolas02cbf692021-10-11 20:46:29 +020022#include <app/util/basic-types.h>
Jerry Johns55349a52021-11-20 01:33:41 -080023#include <lib/core/Optional.h>
Vivien Nicolas02cbf692021-10-11 20:46:29 +020024
25namespace chip {
26namespace app {
27
28/**
Jerry Johns55349a52021-11-20 01:33:41 -080029 * A representation of a concrete attribute path. This does not convey any list index specifiers.
Marc Lepaged27ac432021-12-10 10:12:02 -050030 *
31 * The expanded flag can be set to indicate that a concrete path was expanded from a wildcard
32 * or group path.
Vivien Nicolas02cbf692021-10-11 20:46:29 +020033 */
Boris Zbarsky1afe0062022-02-09 02:12:13 -050034struct ConcreteAttributePath : public ConcreteClusterPath
Vivien Nicolas02cbf692021-10-11 20:46:29 +020035{
Boris Zbarsky1afe0062022-02-09 02:12:13 -050036 ConcreteAttributePath()
37 {
38 // Note: mExpanded is in the superclass, so we can't use a field
39 // initializer.
40 mExpanded = false;
41 }
Jerry Johns55349a52021-11-20 01:33:41 -080042
Vivien Nicolas02cbf692021-10-11 20:46:29 +020043 ConcreteAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) :
Boris Zbarsky1afe0062022-02-09 02:12:13 -050044 ConcreteClusterPath(aEndpointId, aClusterId), mAttributeId(aAttributeId)
Vivien Nicolas5ab45332021-10-20 18:21:59 +020045 {
Boris Zbarsky1afe0062022-02-09 02:12:13 -050046 // Note: mExpanded is in the supercclass, so we can't use a field
47 // initializer.
48 mExpanded = false;
49 }
50
Boris Zbarsky0d163482023-11-20 12:22:01 -050051 bool IsValid() const { return ConcreteClusterPath::HasValidIds() && IsValidAttributeId(mAttributeId); }
52
Boris Zbarsky1afe0062022-02-09 02:12:13 -050053 bool operator==(const ConcreteAttributePath & aOther) const
54 {
55 return ConcreteClusterPath::operator==(aOther) && (mAttributeId == aOther.mAttributeId);
Jerry Johnsc8027a52021-12-13 17:20:15 -080056 }
57
Song GUO01b51372022-04-08 16:57:04 +080058 bool operator!=(const ConcreteAttributePath & aOther) const { return !(*this == aOther); }
59
Jerry Johnsc8027a52021-12-13 17:20:15 -080060 bool operator<(const ConcreteAttributePath & path) const
61 {
62 return (mEndpointId < path.mEndpointId) || ((mEndpointId == path.mEndpointId) && (mClusterId < path.mClusterId)) ||
Boris Zbarsky1afe0062022-02-09 02:12:13 -050063 ((mEndpointId == path.mEndpointId) && (mClusterId == path.mClusterId) && (mAttributeId < path.mAttributeId));
Vivien Nicolas5ab45332021-10-20 18:21:59 +020064 }
65
Vivien Nicolase9ce9b72021-10-21 19:52:19 +020066 AttributeId mAttributeId = 0;
Vivien Nicolas02cbf692021-10-11 20:46:29 +020067};
Jerry Johns55349a52021-11-20 01:33:41 -080068
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 */
74struct ConcreteReadAttributePath : public ConcreteAttributePath
75{
76 ConcreteReadAttributePath() {}
77
Marc Lepaged27ac432021-12-10 10:12:02 -050078 ConcreteReadAttributePath(const ConcreteAttributePath & path) : ConcreteAttributePath(path) {}
Jerry Johns55349a52021-11-20 01:33:41 -080079
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
Matthew Swartwout573511d2024-02-16 11:17:32 -080090 bool operator==(const ConcreteReadAttributePath & aOther) const = delete;
91 bool operator!=(const ConcreteReadAttributePath & aOther) const = delete;
92 bool operator<(const ConcreteReadAttributePath & aOther) const = delete;
93
Jerry Johns55349a52021-11-20 01:33:41 -080094 Optional<uint16_t> mListIndex;
95};
96
97/**
98 * A representation of a concrete path as it appears in a Report or Write
99 * request after path expansion. This contains support for expressing list and list item-specific operations
100 * like replace, update, delete and append.
101 */
102struct ConcreteDataAttributePath : public ConcreteAttributePath
103{
104 enum class ListOperation
105 {
106 NotList, // Path points to an attribute that isn't a list.
107 ReplaceAll, // Path points to an attribute that is a list, indicating that the contents of the list should be replaced in
108 // its entirety.
109 ReplaceItem, // Path points to a specific item in a list, indicating that that item should be replaced in its entirety.
110 DeleteItem, // Path points to a specific item in a list, indicating that that item should be deleted from the list.
111 AppendItem // Path points to an attribute that is a list, indicating that an item should be appended into the list.
112 };
113
114 ConcreteDataAttributePath() {}
115
Marc Lepaged27ac432021-12-10 10:12:02 -0500116 ConcreteDataAttributePath(const ConcreteAttributePath & path) : ConcreteAttributePath(path) {}
117
Jerry Johns55349a52021-11-20 01:33:41 -0800118 ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) :
119 ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId)
120 {}
121
yunhanw-google6a291ca2022-02-11 17:48:41 -0800122 ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId,
123 const Optional<DataVersion> & aDataVersion) :
124 ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId),
125 mDataVersion(aDataVersion)
yunhanw-google0edb9f92022-02-11 00:50:59 -0800126 {}
127
Jerry Johns55349a52021-11-20 01:33:41 -0800128 ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId, ListOperation aListOp,
129 uint16_t aListIndex) :
130 ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId)
131 {
132 mListOp = aListOp;
133 mListIndex = aListIndex;
134 }
135
136 bool IsListOperation() const { return mListOp != ListOperation::NotList; }
137 bool IsListItemOperation() const { return ((mListOp != ListOperation::NotList) && (mListOp != ListOperation::ReplaceAll)); }
138
yunhanw-google6b635422023-11-30 10:34:00 -0800139 void LogPath() const
140 {
141 ChipLogProgress(DataManagement, "Concrete Attribute Path: (%d, " ChipLogFormatMEI ", " ChipLogFormatMEI ") ", mEndpointId,
142 ChipLogValueMEI(mClusterId), ChipLogValueMEI(mAttributeId));
143 }
144
Matthew Swartwout573511d2024-02-16 11:17:32 -0800145 bool MatchesConcreteAttributePath(const ConcreteAttributePath & aOther) { return ConcreteAttributePath::operator==(aOther); }
146
Matthew Swartwout84913af2024-03-21 07:43:16 -0700147 bool operator==(const ConcreteDataAttributePath & aOther) const
148 {
149 return ConcreteAttributePath::operator==(aOther) && (mListIndex == aOther.mListIndex) && (mListOp == aOther.mListOp) &&
150 (mDataVersion == aOther.mDataVersion);
151 }
152
153 bool operator!=(const ConcreteDataAttributePath & aOther) const { return !(*this == aOther); }
154
155 bool operator<(const ConcreteDataAttributePath & aOther) const = delete;
Matthew Swartwout573511d2024-02-16 11:17:32 -0800156
Jerry Johns55349a52021-11-20 01:33:41 -0800157 //
158 // This index is only valid if `mListOp` is set to a list item operation, i.e
159 // ReplaceItem, DeleteItem or AppendItem. Otherwise, it is to be ignored.
160 //
yunhanw-google6a291ca2022-02-11 17:48:41 -0800161 uint16_t mListIndex = 0;
162 ListOperation mListOp = ListOperation::NotList;
163 Optional<DataVersion> mDataVersion = NullOptional;
Jerry Johns55349a52021-11-20 01:33:41 -0800164};
165
Vivien Nicolas02cbf692021-10-11 20:46:29 +0200166} // namespace app
167} // namespace chip