Line data Source code
1 : /*
2 : * Copyright (c) 2024 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 <data-model-providers/codegen/CodegenDataModelProvider.h>
18 :
19 : #include <optional>
20 : #include <variant>
21 :
22 : #include <access/AccessControl.h>
23 : #include <access/Privilege.h>
24 : #include <access/RequestPath.h>
25 : #include <app-common/zap-generated/attribute-type.h>
26 : #include <app/AttributeAccessInterface.h>
27 : #include <app/AttributeAccessInterfaceRegistry.h>
28 : #include <app/AttributeValueEncoder.h>
29 : #include <app/RequiredPrivilege.h>
30 : #include <app/data-model/FabricScoped.h>
31 : #include <app/util/af-types.h>
32 : #include <app/util/attribute-metadata.h>
33 : #include <app/util/attribute-storage-detail.h>
34 : #include <app/util/attribute-storage-null-handling.h>
35 : #include <app/util/attribute-storage.h>
36 : #include <app/util/ember-io-storage.h>
37 : #include <app/util/endpoint-config-api.h>
38 : #include <app/util/odd-sized-integers.h>
39 : #include <data-model-providers/codegen/EmberAttributeDataBuffer.h>
40 : #include <data-model-providers/codegen/EmberMetadata.h>
41 : #include <lib/core/CHIPError.h>
42 : #include <lib/support/CodeUtils.h>
43 : #include <lib/support/Span.h>
44 :
45 : #include <zap-generated/endpoint_config.h>
46 :
47 : namespace chip {
48 : namespace app {
49 :
50 : namespace {
51 :
52 : using namespace chip::app::Compatibility::Internal;
53 : using Protocols::InteractionModel::Status;
54 :
55 : /// Attempts to read via an attribute access interface (AAI)
56 : ///
57 : /// If it returns a CHIP_ERROR, then this is a FINAL result (i.e. either failure or success).
58 : ///
59 : /// If it returns std::nullopt, then there is no AAI to handle the given path
60 : /// and processing should figure out the value otherwise (generally from other ember data)
61 1801 : std::optional<CHIP_ERROR> TryReadViaAccessInterface(const ConcreteAttributePath & path, AttributeAccessInterface * aai,
62 : AttributeValueEncoder & encoder)
63 : {
64 : // Processing can happen only if an attribute access interface actually exists..
65 1801 : if (aai == nullptr)
66 : {
67 39 : return std::nullopt;
68 : }
69 :
70 1762 : CHIP_ERROR err = aai->Read(path, encoder);
71 :
72 1762 : if (err != CHIP_NO_ERROR)
73 : {
74 : // Implementation of 8.4.3.2 of the spec for path expansion
75 138 : if (path.mExpanded && (err == CHIP_IM_GLOBAL_STATUS(UnsupportedRead)))
76 : {
77 1 : return CHIP_NO_ERROR;
78 : }
79 :
80 137 : return err;
81 : }
82 :
83 : // If the encoder tried to encode, then a value should have been written.
84 : // - if encode, assume DONE (i.e. FINAL CHIP_NO_ERROR)
85 : // - if no encode, say that processing must continue
86 3248 : return encoder.TriedEncode() ? std::make_optional(CHIP_NO_ERROR) : std::nullopt;
87 : }
88 :
89 : } // namespace
90 :
91 : /// separated-out ReadAttribute implementation (given existing complexity)
92 : ///
93 : /// Generally will:
94 : /// - validate ACL (only for non-internal requests)
95 : /// - Try to read attribute via the AttributeAccessInterface
96 : /// - Try to read the value from ember RAM storage
97 1807 : DataModel::ActionReturnStatus CodegenDataModelProvider::ReadAttribute(const DataModel::ReadAttributeRequest & request,
98 : AttributeValueEncoder & encoder)
99 : {
100 1807 : ChipLogDetail(DataManagement,
101 : "Reading attribute: Cluster=" ChipLogFormatMEI " Endpoint=0x%x AttributeId=" ChipLogFormatMEI " (expanded=%d)",
102 : ChipLogValueMEI(request.path.mClusterId), request.path.mEndpointId, ChipLogValueMEI(request.path.mAttributeId),
103 : request.path.mExpanded);
104 :
105 1807 : if (auto * cluster = mRegistry.Get(request.path); cluster != nullptr)
106 : {
107 1 : return cluster->ReadAttribute(request, encoder);
108 : }
109 :
110 1806 : auto metadata = Ember::FindAttributeMetadata(request.path);
111 :
112 : // Explicit failure in finding a suitable metadata
113 1806 : if (const Status * status = std::get_if<Status>(&metadata))
114 : {
115 5 : VerifyOrDie((*status == Status::UnsupportedEndpoint) || //
116 : (*status == Status::UnsupportedCluster) || //
117 : (*status == Status::UnsupportedAttribute));
118 5 : return *status;
119 : }
120 :
121 : // Read via AAI
122 3602 : std::optional<CHIP_ERROR> aai_result = TryReadViaAccessInterface(
123 1801 : request.path, AttributeAccessInterfaceRegistry::Instance().Get(request.path.mEndpointId, request.path.mClusterId), encoder);
124 1801 : VerifyOrReturnError(!aai_result.has_value(), *aai_result);
125 :
126 39 : const EmberAfAttributeMetadata * attributeMetadata = std::get<const EmberAfAttributeMetadata *>(metadata);
127 : // We can only get a status or metadata.
128 39 : VerifyOrDie(attributeMetadata != nullptr);
129 :
130 : // At this point, we have to use ember directly to read the data.
131 : EmberAfAttributeSearchRecord record;
132 39 : record.endpoint = request.path.mEndpointId;
133 39 : record.clusterId = request.path.mClusterId;
134 39 : record.attributeId = request.path.mAttributeId;
135 39 : Protocols::InteractionModel::Status status = emAfReadOrWriteAttribute(
136 39 : &record, &attributeMetadata, gEmberAttributeIOBufferSpan.data(), static_cast<uint16_t>(gEmberAttributeIOBufferSpan.size()),
137 : /* write = */ false);
138 :
139 39 : if (status != Protocols::InteractionModel::Status::Success)
140 : {
141 2 : return CHIP_ERROR_IM_GLOBAL_STATUS_VALUE(status);
142 : }
143 :
144 37 : VerifyOrReturnError(attributeMetadata != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
145 :
146 37 : MutableByteSpan data = gEmberAttributeIOBufferSpan;
147 37 : Ember::EmberAttributeDataBuffer emberData(attributeMetadata, data);
148 37 : return encoder.Encode(emberData);
149 : }
150 :
151 : } // namespace app
152 : } // namespace chip
|