|
| 1 | +/* |
| 2 | + * Copyright (c) 2022-2025 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 | +#include <app/GlobalAttributes.h> |
| 18 | +#include <app/data-model-provider/MetadataLookup.h> |
| 19 | +#include <protocols/interaction_model/StatusCode.h> |
| 20 | + |
| 21 | +using chip::Protocols::InteractionModel::Status; |
| 22 | + |
| 23 | +namespace chip { |
| 24 | +namespace app { |
| 25 | + |
| 26 | +bool IsSupportedGlobalAttributeNotInMetadata(AttributeId attributeId) |
| 27 | +{ |
| 28 | + for (auto & attr : GlobalAttributesNotInMetadata) |
| 29 | + { |
| 30 | + if (attr == attributeId) |
| 31 | + { |
| 32 | + return true; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return false; |
| 37 | +} |
| 38 | + |
| 39 | +DataModel::ActionReturnStatus ReadGlobalAttributeFromMetadata(DataModel::Provider * provider, const ConcreteAttributePath & path, |
| 40 | + AttributeValueEncoder & encoder) |
| 41 | +{ |
| 42 | + CHIP_ERROR err; |
| 43 | + |
| 44 | + switch (path.mAttributeId) |
| 45 | + { |
| 46 | + case Clusters::Globals::Attributes::GeneratedCommandList::Id: { |
| 47 | + DataModel::ListBuilder<CommandId> builder; |
| 48 | + err = provider->GeneratedCommands(path, builder); |
| 49 | + if (err != CHIP_NO_ERROR) |
| 50 | + { |
| 51 | + break; |
| 52 | + } |
| 53 | + auto buffer = builder.TakeBuffer(); |
| 54 | + |
| 55 | + return encoder.EncodeList([&buffer](const auto & listEncodeHelper) { |
| 56 | + for (auto id : buffer) |
| 57 | + { |
| 58 | + // NOTE: cast to u64 because TLV encodes all numbers the same (no TLV sideffects) |
| 59 | + // and this reduces template variants for Encode, saving flash. |
| 60 | + ReturnErrorOnFailure(listEncodeHelper.Encode(static_cast<uint64_t>(id))); |
| 61 | + } |
| 62 | + return CHIP_NO_ERROR; |
| 63 | + }); |
| 64 | + } |
| 65 | + case Clusters::Globals::Attributes::AcceptedCommandList::Id: { |
| 66 | + DataModel::ListBuilder<DataModel::AcceptedCommandEntry> builder; |
| 67 | + err = provider->AcceptedCommands(path, builder); |
| 68 | + if (err != CHIP_NO_ERROR) |
| 69 | + { |
| 70 | + break; |
| 71 | + } |
| 72 | + auto buffer = builder.TakeBuffer(); |
| 73 | + |
| 74 | + return encoder.EncodeList([&buffer](const auto & listEncodeHelper) { |
| 75 | + for (auto entry : buffer) |
| 76 | + { |
| 77 | + // NOTE: cast to u64 because TLV encodes all numbers the same (no TLV sideffects) |
| 78 | + // and this reduces template variants for Encode, saving flash. |
| 79 | + ReturnErrorOnFailure(listEncodeHelper.Encode(static_cast<uint64_t>(entry.commandId))); |
| 80 | + } |
| 81 | + return CHIP_NO_ERROR; |
| 82 | + }); |
| 83 | + } |
| 84 | + case Clusters::Globals::Attributes::AttributeList::Id: { |
| 85 | + DataModel::ListBuilder<DataModel::AttributeEntry> builder; |
| 86 | + err = provider->Attributes(path, builder); |
| 87 | + if (err != CHIP_NO_ERROR) |
| 88 | + { |
| 89 | + break; |
| 90 | + } |
| 91 | + auto buffer = builder.TakeBuffer(); |
| 92 | + |
| 93 | + return encoder.EncodeList([&buffer](const auto & listEncodeHelper) { |
| 94 | + for (auto entry : buffer) |
| 95 | + { |
| 96 | + // NOTE: cast to u64 because TLV encodes all numbers the same (no TLV sideffects) |
| 97 | + // and this reduces template variants for Encode, saving flash. |
| 98 | + ReturnErrorOnFailure(listEncodeHelper.Encode(static_cast<uint64_t>(entry.attributeId))); |
| 99 | + } |
| 100 | + |
| 101 | + for (auto id : GlobalAttributesNotInMetadata) |
| 102 | + { |
| 103 | + // NOTE: cast to u64 because TLV encodes all numbers the same (no TLV sideffects) |
| 104 | + // and this reduces template variants for Encode, saving flash. |
| 105 | + ReturnErrorOnFailure(listEncodeHelper.Encode(static_cast<uint64_t>(id))); |
| 106 | + } |
| 107 | + |
| 108 | + return CHIP_NO_ERROR; |
| 109 | + }); |
| 110 | + } |
| 111 | + default: |
| 112 | + return CHIP_ERROR_INVALID_ARGUMENT; |
| 113 | + } |
| 114 | + |
| 115 | + // if we get here, the path was NOT valid |
| 116 | + if (err == CHIP_ERROR_NOT_FOUND) |
| 117 | + { |
| 118 | + // The `Failure` here is arbitrary: we expect ReadGlobalAttributeFromMetadata to be |
| 119 | + // an internal API used for global attributes only and call preconditions say that |
| 120 | + // should never happen. |
| 121 | + // |
| 122 | + // Code only takes this path if one of |
| 123 | + // `GeneratedCommands`/`AcceptedCommands`/`Attribute` return a NOT_FOUND and |
| 124 | + // that would indicate an invalid cluster (which should have been pre-validated by |
| 125 | + // the caller). |
| 126 | + return DataModel::ValidateClusterPath(provider, path, Status::Failure); |
| 127 | + } |
| 128 | + return err; |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace app |
| 132 | +} // namespace chip |
0 commit comments