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