Vivien Nicolas | 02cbf69 | 2021-10-11 20:46:29 +0200 | [diff] [blame] | 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 | |
Boris Zbarsky | 1afe006 | 2022-02-09 02:12:13 -0500 | [diff] [blame] | 21 | #include <app/ConcreteClusterPath.h> |
Vivien Nicolas | 02cbf69 | 2021-10-11 20:46:29 +0200 | [diff] [blame] | 22 | #include <app/util/basic-types.h> |
Jerry Johns | 55349a5 | 2021-11-20 01:33:41 -0800 | [diff] [blame] | 23 | #include <lib/core/Optional.h> |
Vivien Nicolas | 02cbf69 | 2021-10-11 20:46:29 +0200 | [diff] [blame] | 24 | |
| 25 | namespace chip { |
| 26 | namespace app { |
| 27 | |
| 28 | /** |
Jerry Johns | 55349a5 | 2021-11-20 01:33:41 -0800 | [diff] [blame] | 29 | * A representation of a concrete attribute path. This does not convey any list index specifiers. |
Marc Lepage | d27ac43 | 2021-12-10 10:12:02 -0500 | [diff] [blame] | 30 | * |
| 31 | * The expanded flag can be set to indicate that a concrete path was expanded from a wildcard |
| 32 | * or group path. |
Vivien Nicolas | 02cbf69 | 2021-10-11 20:46:29 +0200 | [diff] [blame] | 33 | */ |
Boris Zbarsky | 1afe006 | 2022-02-09 02:12:13 -0500 | [diff] [blame] | 34 | struct ConcreteAttributePath : public ConcreteClusterPath |
Vivien Nicolas | 02cbf69 | 2021-10-11 20:46:29 +0200 | [diff] [blame] | 35 | { |
Boris Zbarsky | 1afe006 | 2022-02-09 02:12:13 -0500 | [diff] [blame] | 36 | ConcreteAttributePath() |
| 37 | { |
| 38 | // Note: mExpanded is in the superclass, so we can't use a field |
| 39 | // initializer. |
| 40 | mExpanded = false; |
| 41 | } |
Jerry Johns | 55349a5 | 2021-11-20 01:33:41 -0800 | [diff] [blame] | 42 | |
Vivien Nicolas | 02cbf69 | 2021-10-11 20:46:29 +0200 | [diff] [blame] | 43 | ConcreteAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) : |
Boris Zbarsky | 1afe006 | 2022-02-09 02:12:13 -0500 | [diff] [blame] | 44 | ConcreteClusterPath(aEndpointId, aClusterId), mAttributeId(aAttributeId) |
Vivien Nicolas | 5ab4533 | 2021-10-20 18:21:59 +0200 | [diff] [blame] | 45 | { |
Boris Zbarsky | 1afe006 | 2022-02-09 02:12:13 -0500 | [diff] [blame] | 46 | // Note: mExpanded is in the supercclass, so we can't use a field |
| 47 | // initializer. |
| 48 | mExpanded = false; |
| 49 | } |
| 50 | |
Boris Zbarsky | 0d16348 | 2023-11-20 12:22:01 -0500 | [diff] [blame] | 51 | bool IsValid() const { return ConcreteClusterPath::HasValidIds() && IsValidAttributeId(mAttributeId); } |
| 52 | |
Boris Zbarsky | 1afe006 | 2022-02-09 02:12:13 -0500 | [diff] [blame] | 53 | bool operator==(const ConcreteAttributePath & aOther) const |
| 54 | { |
| 55 | return ConcreteClusterPath::operator==(aOther) && (mAttributeId == aOther.mAttributeId); |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Song GUO | 01b5137 | 2022-04-08 16:57:04 +0800 | [diff] [blame] | 58 | bool operator!=(const ConcreteAttributePath & aOther) const { return !(*this == aOther); } |
| 59 | |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 60 | bool operator<(const ConcreteAttributePath & path) const |
| 61 | { |
| 62 | return (mEndpointId < path.mEndpointId) || ((mEndpointId == path.mEndpointId) && (mClusterId < path.mClusterId)) || |
Boris Zbarsky | 1afe006 | 2022-02-09 02:12:13 -0500 | [diff] [blame] | 63 | ((mEndpointId == path.mEndpointId) && (mClusterId == path.mClusterId) && (mAttributeId < path.mAttributeId)); |
Vivien Nicolas | 5ab4533 | 2021-10-20 18:21:59 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Vivien Nicolas | e9ce9b7 | 2021-10-21 19:52:19 +0200 | [diff] [blame] | 66 | AttributeId mAttributeId = 0; |
Vivien Nicolas | 02cbf69 | 2021-10-11 20:46:29 +0200 | [diff] [blame] | 67 | }; |
Jerry Johns | 55349a5 | 2021-11-20 01:33:41 -0800 | [diff] [blame] | 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 | ConcreteReadAttributePath() {} |
| 77 | |
Marc Lepage | d27ac43 | 2021-12-10 10:12:02 -0500 | [diff] [blame] | 78 | ConcreteReadAttributePath(const ConcreteAttributePath & path) : ConcreteAttributePath(path) {} |
Jerry Johns | 55349a5 | 2021-11-20 01:33:41 -0800 | [diff] [blame] | 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 | |
Matthew Swartwout | 573511d | 2024-02-16 11:17:32 -0800 | [diff] [blame] | 90 | 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 Johns | 55349a5 | 2021-11-20 01:33:41 -0800 | [diff] [blame] | 94 | 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 | */ |
| 102 | struct 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 Lepage | d27ac43 | 2021-12-10 10:12:02 -0500 | [diff] [blame] | 116 | ConcreteDataAttributePath(const ConcreteAttributePath & path) : ConcreteAttributePath(path) {} |
| 117 | |
Jerry Johns | 55349a5 | 2021-11-20 01:33:41 -0800 | [diff] [blame] | 118 | ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId) : |
| 119 | ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId) |
| 120 | {} |
| 121 | |
yunhanw-google | 6a291ca | 2022-02-11 17:48:41 -0800 | [diff] [blame] | 122 | ConcreteDataAttributePath(EndpointId aEndpointId, ClusterId aClusterId, AttributeId aAttributeId, |
| 123 | const Optional<DataVersion> & aDataVersion) : |
| 124 | ConcreteAttributePath(aEndpointId, aClusterId, aAttributeId), |
| 125 | mDataVersion(aDataVersion) |
yunhanw-google | 0edb9f9 | 2022-02-11 00:50:59 -0800 | [diff] [blame] | 126 | {} |
| 127 | |
Jerry Johns | 55349a5 | 2021-11-20 01:33:41 -0800 | [diff] [blame] | 128 | 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-google | 6b63542 | 2023-11-30 10:34:00 -0800 | [diff] [blame] | 139 | void LogPath() const |
| 140 | { |
| 141 | ChipLogProgress(DataManagement, "Concrete Attribute Path: (%d, " ChipLogFormatMEI ", " ChipLogFormatMEI ") ", mEndpointId, |
| 142 | ChipLogValueMEI(mClusterId), ChipLogValueMEI(mAttributeId)); |
| 143 | } |
| 144 | |
Matthew Swartwout | 573511d | 2024-02-16 11:17:32 -0800 | [diff] [blame] | 145 | bool MatchesConcreteAttributePath(const ConcreteAttributePath & aOther) { return ConcreteAttributePath::operator==(aOther); } |
| 146 | |
Matthew Swartwout | 84913af | 2024-03-21 07:43:16 -0700 | [diff] [blame] | 147 | 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 Swartwout | 573511d | 2024-02-16 11:17:32 -0800 | [diff] [blame] | 156 | |
Jerry Johns | 55349a5 | 2021-11-20 01:33:41 -0800 | [diff] [blame] | 157 | // |
| 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-google | 6a291ca | 2022-02-11 17:48:41 -0800 | [diff] [blame] | 161 | uint16_t mListIndex = 0; |
| 162 | ListOperation mListOp = ListOperation::NotList; |
| 163 | Optional<DataVersion> mDataVersion = NullOptional; |
Jerry Johns | 55349a5 | 2021-11-20 01:33:41 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
Vivien Nicolas | 02cbf69 | 2021-10-11 20:46:29 +0200 | [diff] [blame] | 166 | } // namespace app |
| 167 | } // namespace chip |