Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2020-2023 Project CHIP Authors
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/util/attribute-storage.h>
18 :
19 : #include <app/AttributeAccessInterfaceRegistry.h>
20 : #include <app/CommandHandlerInterfaceRegistry.h>
21 : #include <app/InteractionModelEngine.h>
22 : #include <app/persistence/AttributePersistenceProvider.h>
23 : #include <app/persistence/AttributePersistenceProviderInstance.h>
24 : #include <app/persistence/PascalString.h>
25 : #include <app/reporting/reporting.h>
26 : #include <app/util/attribute-metadata.h>
27 : #include <app/util/attribute-storage-detail.h>
28 : #include <app/util/config.h>
29 : #include <app/util/ember-io-storage.h>
30 : #include <app/util/ember-strings.h>
31 : #include <app/util/endpoint-config-api.h>
32 : #include <app/util/generic-callbacks.h>
33 : #include <data-model-providers/codegen/CodegenDataModelProvider.h>
34 : #include <lib/core/CHIPConfig.h>
35 : #include <lib/core/CHIPError.h>
36 : #include <lib/support/CodeUtils.h>
37 : #include <lib/support/Span.h>
38 : #include <lib/support/logging/CHIPLogging.h>
39 : #include <platform/LockTracker.h>
40 : #include <protocols/interaction_model/StatusCode.h>
41 :
42 : using chip::Protocols::InteractionModel::Status;
43 :
44 : // Attribute storage depends on knowing the current layout/setup of attributes
45 : // and corresponding callbacks. Specifically:
46 : // - zap-generated/callback.h is needed because endpoint_config will call the
47 : // corresponding callbacks (via GENERATED_FUNCTION_ARRAYS) and the include
48 : // for it is:
49 : // util/config.h -> zap-generated/endpoint_config.h
50 : #include <app-common/zap-generated/callback.h>
51 :
52 : using namespace chip;
53 : using namespace chip::app;
54 :
55 : //------------------------------------------------------------------------------
56 : // Globals
57 : // This is not declared CONST in order to handle dynamic endpoint information
58 : // retrieved from tokens.
59 : EmberAfDefinedEndpoint emAfEndpoints[MAX_ENDPOINT_COUNT];
60 :
61 : #if (ATTRIBUTE_MAX_SIZE == 0)
62 : #define ACTUAL_ATTRIBUTE_SIZE 1
63 : #else
64 : #define ACTUAL_ATTRIBUTE_SIZE ATTRIBUTE_MAX_SIZE
65 : #endif
66 :
67 : uint8_t attributeData[ACTUAL_ATTRIBUTE_SIZE];
68 :
69 : // ----- internal-only methods, not part of the external API -----
70 :
71 : // Loads the attributes from built-in default and storage.
72 : static void emAfLoadAttributeDefaults(EndpointId endpoint, Optional<ClusterId> = NullOptional);
73 :
74 : static bool emAfMatchCluster(const EmberAfCluster * cluster, const EmberAfAttributeSearchRecord * attRecord);
75 : static bool emAfMatchAttribute(const EmberAfCluster * cluster, const EmberAfAttributeMetadata * am,
76 : const EmberAfAttributeSearchRecord * attRecord);
77 :
78 : // If server == true, returns the number of server clusters,
79 : // otherwise number of client clusters on the endpoint at the given index.
80 : static uint8_t emberAfClusterCountByIndex(uint16_t endpointIndex, bool server);
81 :
82 : // Check whether there is an endpoint defined with the given endpoint id that is
83 : // enabled.
84 : static bool emberAfEndpointIsEnabled(EndpointId endpoint);
85 :
86 : static void emberAfIncreaseDataVersion(const chip::app::ConcreteClusterPath & aConcreteClusterPath);
87 :
88 : namespace {
89 :
90 : uint16_t emberEndpointCount = 0;
91 :
92 : /// Determines a incremental unique index for ember
93 : /// metadata that is increased whenever a structural change is made to the
94 : /// ember metadata (e.g. changing dynamic endpoints or enabling/disabling endpoints)
95 : unsigned emberMetadataStructureGeneration = 0;
96 :
97 : // If we have attributes that are more than 4 bytes, then
98 : // we need this data block for the defaults
99 : #if (defined(GENERATED_DEFAULTS) && GENERATED_DEFAULTS_COUNT)
100 : constexpr const uint8_t generatedDefaults[] = GENERATED_DEFAULTS;
101 : #define ZAP_LONG_DEFAULTS_INDEX(index) \
102 : { \
103 : &generatedDefaults[index] \
104 : }
105 : #endif // GENERATED_DEFAULTS
106 :
107 : #if (defined(GENERATED_MIN_MAX_DEFAULTS) && GENERATED_MIN_MAX_DEFAULT_COUNT)
108 : constexpr const EmberAfAttributeMinMaxValue minMaxDefaults[] = GENERATED_MIN_MAX_DEFAULTS;
109 : #define ZAP_MIN_MAX_DEFAULTS_INDEX(index) \
110 : { \
111 : &minMaxDefaults[index] \
112 : }
113 : #endif // GENERATED_MIN_MAX_DEFAULTS
114 :
115 : #ifdef GENERATED_FUNCTION_ARRAYS
116 : GENERATED_FUNCTION_ARRAYS
117 : #endif
118 :
119 : #ifdef GENERATED_COMMANDS
120 : constexpr const CommandId generatedCommands[] = GENERATED_COMMANDS;
121 : #define ZAP_GENERATED_COMMANDS_INDEX(index) (&generatedCommands[index])
122 : #endif // GENERATED_COMMANDS
123 :
124 : #if (defined(GENERATED_EVENTS) && (GENERATED_EVENT_COUNT > 0))
125 : constexpr const EventId generatedEvents[] = GENERATED_EVENTS;
126 : #define ZAP_GENERATED_EVENTS_INDEX(index) (&generatedEvents[index])
127 : #endif // GENERATED_EVENTS
128 :
129 : [[maybe_unused]] constexpr const EmberAfAttributeMetadata generatedAttributes[] = GENERATED_ATTRIBUTES;
130 : #define ZAP_ATTRIBUTE_INDEX(index) (&generatedAttributes[index])
131 :
132 : #ifdef GENERATED_CLUSTERS
133 : constexpr const EmberAfCluster generatedClusters[] = GENERATED_CLUSTERS;
134 : #define ZAP_CLUSTER_INDEX(index) (&generatedClusters[index])
135 : #endif
136 :
137 : #if FIXED_ENDPOINT_COUNT > 0
138 : constexpr const EmberAfEndpointType generatedEmberAfEndpointTypes[] = GENERATED_ENDPOINT_TYPES;
139 : constexpr const EmberAfDeviceType fixedDeviceTypeList[] = FIXED_DEVICE_TYPES;
140 :
141 : // Not const, because these need to mutate.
142 : DataVersion fixedEndpointDataVersions[ZAP_FIXED_ENDPOINT_DATA_VERSION_COUNT];
143 : #endif // FIXED_ENDPOINT_COUNT > 0
144 :
145 0 : bool emberAfIsThisDataTypeAListType(EmberAfAttributeType dataType)
146 : {
147 0 : return dataType == ZCL_ARRAY_ATTRIBUTE_TYPE;
148 : }
149 :
150 13922 : uint16_t findIndexFromEndpoint(EndpointId endpoint, bool ignoreDisabledEndpoints)
151 : {
152 13922 : if (endpoint == kInvalidEndpointId)
153 : {
154 0 : return kEmberInvalidEndpointIndex;
155 : }
156 :
157 : uint16_t epi;
158 28202 : for (epi = 0; epi < emberAfEndpointCount(); epi++)
159 : {
160 42027 : if (emAfEndpoints[epi].endpoint == endpoint &&
161 13881 : (!ignoreDisabledEndpoints || emAfEndpoints[epi].bitmask.Has(EmberAfEndpointOptions::isEnabled)))
162 : {
163 13866 : return epi;
164 : }
165 : }
166 56 : return kEmberInvalidEndpointIndex;
167 : }
168 :
169 : // Returns the index of a given endpoint. Considers disabled endpoints.
170 0 : uint16_t emberAfIndexFromEndpointIncludingDisabledEndpoints(EndpointId endpoint)
171 : {
172 0 : return findIndexFromEndpoint(endpoint, false /* ignoreDisabledEndpoints */);
173 : }
174 :
175 0 : CHIP_ERROR ValidateDataContent(ByteSpan span, const EmberAfAttributeMetadata * am)
176 : {
177 0 : if (emberAfIsStringAttributeType(am->attributeType))
178 : {
179 0 : VerifyOrReturnValue(Storage::ShortPascalBytes::IsValid(span), CHIP_ERROR_INCORRECT_STATE);
180 0 : return CHIP_NO_ERROR;
181 : }
182 :
183 0 : if (emberAfIsLongStringAttributeType(am->attributeType))
184 : {
185 0 : VerifyOrReturnValue(Storage::LongPascalBytes::IsValid(span), CHIP_ERROR_INCORRECT_STATE);
186 0 : return CHIP_NO_ERROR;
187 : }
188 :
189 0 : VerifyOrReturnValue(span.size() == am->size, CHIP_ERROR_INCORRECT_STATE);
190 0 : return CHIP_NO_ERROR;
191 : }
192 :
193 : } // anonymous namespace
194 :
195 : // Initial configuration
196 30 : void emberAfEndpointConfigure()
197 : {
198 : uint16_t ep;
199 :
200 30 : emberEndpointCount = FIXED_ENDPOINT_COUNT;
201 :
202 : #if FIXED_ENDPOINT_COUNT > 0
203 :
204 : static_assert(FIXED_ENDPOINT_COUNT <= std::numeric_limits<decltype(ep)>::max(),
205 : "FIXED_ENDPOINT_COUNT must not exceed the size of the endpoint data type");
206 :
207 30 : constexpr uint16_t fixedEndpoints[] = FIXED_ENDPOINT_ARRAY;
208 30 : constexpr uint16_t fixedDeviceTypeListLengths[] = FIXED_DEVICE_TYPE_LENGTHS;
209 30 : constexpr uint16_t fixedDeviceTypeListOffsets[] = FIXED_DEVICE_TYPE_OFFSETS;
210 30 : constexpr uint8_t fixedEmberAfEndpointTypes[] = FIXED_ENDPOINT_TYPES;
211 30 : constexpr EndpointId fixedParentEndpoints[] = FIXED_PARENT_ENDPOINTS;
212 :
213 : #if ZAP_FIXED_ENDPOINT_DATA_VERSION_COUNT > 0
214 : // Initialize our data version storage. If
215 : // ZAP_FIXED_ENDPOINT_DATA_VERSION_COUNT == 0, gcc complains about a memset
216 : // with size equal to number of elements without multiplication by element
217 : // size, because the sizeof() is also 0 in that case...
218 : if (Crypto::DRBG_get_bytes(reinterpret_cast<uint8_t *>(fixedEndpointDataVersions), sizeof(fixedEndpointDataVersions)) !=
219 : CHIP_NO_ERROR)
220 : {
221 : // Now what? At least 0-init it.
222 : memset(fixedEndpointDataVersions, 0, sizeof(fixedEndpointDataVersions));
223 : }
224 : #endif // ZAP_FIXED_ENDPOINT_DATA_VERSION_COUNT > 0
225 :
226 30 : DataVersion * currentDataVersions = fixedEndpointDataVersions;
227 60 : for (ep = 0; ep < FIXED_ENDPOINT_COUNT; ep++)
228 : {
229 30 : emAfEndpoints[ep].endpoint = fixedEndpoints[ep];
230 30 : emAfEndpoints[ep].deviceTypeList =
231 30 : Span<const EmberAfDeviceType>(&fixedDeviceTypeList[fixedDeviceTypeListOffsets[ep]], fixedDeviceTypeListLengths[ep]);
232 30 : emAfEndpoints[ep].endpointType = &generatedEmberAfEndpointTypes[fixedEmberAfEndpointTypes[ep]];
233 30 : emAfEndpoints[ep].dataVersions = currentDataVersions;
234 30 : emAfEndpoints[ep].parentEndpointId = fixedParentEndpoints[ep];
235 :
236 30 : constexpr const DeviceTypeId kRootnodeId = 0x0016;
237 30 : constexpr const DeviceTypeId kAggregatorId = 0x000E;
238 30 : constexpr const DeviceTypeId kBridgedNode = 0x0013;
239 30 : emAfEndpoints[ep].bitmask.Set(EmberAfEndpointOptions::isEnabled);
240 30 : for (const auto & deviceType : emAfEndpoints[ep].deviceTypeList)
241 : {
242 : // Default composition for all device types is set to tree. Except rootnode, aggregator and
243 : // bridgednode, which are full-family. Clients can manually override these defaults using
244 : // SetFlatCompositionForEndpoint / SetTreeCompositionForEndpoint at application init.
245 : // TODO: This information should come from the schema XML, not be hardcoded.
246 30 : if ((deviceType.deviceTypeId == kRootnodeId) || (deviceType.deviceTypeId == kAggregatorId) ||
247 0 : (deviceType.deviceTypeId == kBridgedNode))
248 : {
249 30 : emAfEndpoints[ep].bitmask.Set(EmberAfEndpointOptions::isFlatComposition);
250 30 : break;
251 : }
252 : }
253 :
254 : // Increment currentDataVersions by 1 (slot) for every server cluster
255 : // this endpoint has.
256 30 : currentDataVersions += emberAfClusterCountByIndex(ep, /* server = */ true);
257 : }
258 :
259 : #endif // FIXED_ENDPOINT_COUNT > 0
260 :
261 : #if CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT
262 : if (MAX_ENDPOINT_COUNT > FIXED_ENDPOINT_COUNT)
263 : {
264 : //
265 : // Reset instances tracking dynamic endpoints to safe defaults.
266 : //
267 150 : for (ep = FIXED_ENDPOINT_COUNT; ep < MAX_ENDPOINT_COUNT; ep++)
268 : {
269 120 : emAfEndpoints[ep] = EmberAfDefinedEndpoint();
270 : }
271 : }
272 : #endif
273 30 : }
274 :
275 28 : void emberAfSetDynamicEndpointCount(uint16_t dynamicEndpointCount)
276 : {
277 28 : emberEndpointCount = static_cast<uint16_t>(FIXED_ENDPOINT_COUNT + dynamicEndpointCount);
278 28 : }
279 :
280 0 : uint16_t emberAfGetDynamicIndexFromEndpoint(EndpointId id)
281 : {
282 0 : if (id == kInvalidEndpointId)
283 : {
284 0 : return kEmberInvalidEndpointIndex;
285 : }
286 :
287 : uint16_t index;
288 0 : for (index = FIXED_ENDPOINT_COUNT; index < MAX_ENDPOINT_COUNT; index++)
289 : {
290 0 : if (emAfEndpoints[index].endpoint == id)
291 : {
292 0 : return static_cast<uint16_t>(index - FIXED_ENDPOINT_COUNT);
293 : }
294 : }
295 0 : return kEmberInvalidEndpointIndex;
296 : }
297 :
298 28 : CHIP_ERROR emberAfSetDynamicEndpoint(uint16_t index, EndpointId id, const EmberAfEndpointType * ep,
299 : const Span<DataVersion> & dataVersionStorage, Span<const EmberAfDeviceType> deviceTypeList,
300 : EndpointId parentEndpointId)
301 : {
302 28 : return emberAfSetDynamicEndpointWithEpUniqueId(index, id, ep, dataVersionStorage, deviceTypeList, {}, parentEndpointId);
303 : }
304 :
305 28 : CHIP_ERROR emberAfSetDynamicEndpointWithEpUniqueId(uint16_t index, EndpointId id, const EmberAfEndpointType * ep,
306 : const Span<DataVersion> & dataVersionStorage,
307 : Span<const EmberAfDeviceType> deviceTypeList, CharSpan endpointUniqueId,
308 : EndpointId parentEndpointId)
309 : {
310 28 : auto realIndex = index + FIXED_ENDPOINT_COUNT;
311 :
312 28 : if (realIndex >= MAX_ENDPOINT_COUNT)
313 : {
314 0 : return CHIP_ERROR_NO_MEMORY;
315 : }
316 28 : if (id == kInvalidEndpointId)
317 : {
318 0 : return CHIP_ERROR_INVALID_ARGUMENT;
319 : }
320 :
321 28 : auto serverClusterCount = emberAfClusterCountForEndpointType(ep, /* server = */ true);
322 28 : if (dataVersionStorage.size() < serverClusterCount)
323 : {
324 0 : return CHIP_ERROR_NO_MEMORY;
325 : }
326 :
327 28 : index = static_cast<uint16_t>(realIndex);
328 140 : for (uint16_t i = FIXED_ENDPOINT_COUNT; i < MAX_ENDPOINT_COUNT; i++)
329 : {
330 112 : if (emAfEndpoints[i].endpoint == id)
331 : {
332 0 : return CHIP_ERROR_ENDPOINT_EXISTS;
333 : }
334 : }
335 :
336 28 : const size_t bufferSize = Compatibility::Internal::gEmberAttributeIOBufferSpan.size();
337 62 : for (uint8_t i = 0; i < ep->clusterCount; i++)
338 : {
339 34 : const EmberAfCluster * cluster = &(ep->cluster[i]);
340 34 : if (!cluster->attributes)
341 : {
342 0 : continue;
343 : }
344 :
345 146 : for (uint16_t j = 0; j < cluster->attributeCount; j++)
346 : {
347 112 : const EmberAfAttributeMetadata * attr = &(cluster->attributes[j]);
348 112 : uint16_t attrSize = emberAfAttributeSize(attr);
349 112 : if (attrSize > bufferSize)
350 : {
351 0 : ChipLogError(DataManagement,
352 : "Attribute size %u exceeds max size %lu, (attrId=" ChipLogFormatMEI ", clusterId=" ChipLogFormatMEI
353 : ")",
354 : attrSize, static_cast<unsigned long>(bufferSize), ChipLogValueMEI(attr->attributeId),
355 : ChipLogValueMEI(cluster->clusterId));
356 0 : return CHIP_ERROR_NO_MEMORY;
357 : }
358 : }
359 : }
360 28 : emAfEndpoints[index].endpoint = id;
361 28 : emAfEndpoints[index].deviceTypeList = deviceTypeList;
362 28 : emAfEndpoints[index].endpointType = ep;
363 28 : emAfEndpoints[index].dataVersions = dataVersionStorage.data();
364 : #if CHIP_CONFIG_USE_ENDPOINT_UNIQUE_ID
365 : MutableCharSpan targetSpan(emAfEndpoints[index].endpointUniqueId);
366 : if (CopyCharSpanToMutableCharSpan(endpointUniqueId, targetSpan) != CHIP_NO_ERROR)
367 : {
368 : return CHIP_ERROR_BUFFER_TOO_SMALL;
369 : }
370 :
371 : // Ensure that the size of emAfEndpoints[index].endpointUniqueId fits within uint8_t
372 : static_assert(sizeof(emAfEndpoints[0].endpointUniqueId) <= UINT8_MAX,
373 : "The size of emAfEndpoints[index].endpointUniqueId must fit within uint8_t");
374 :
375 : emAfEndpoints[index].endpointUniqueIdSize = static_cast<uint8_t>(targetSpan.size());
376 : #endif
377 : // Start the endpoint off as disabled.
378 28 : emAfEndpoints[index].bitmask.Clear(EmberAfEndpointOptions::isEnabled);
379 28 : emAfEndpoints[index].parentEndpointId = parentEndpointId;
380 :
381 28 : emberAfSetDynamicEndpointCount(MAX_ENDPOINT_COUNT - FIXED_ENDPOINT_COUNT);
382 :
383 : // Initialize the data versions.
384 28 : size_t dataSize = sizeof(DataVersion) * serverClusterCount;
385 28 : if (dataSize != 0)
386 : {
387 56 : if (Crypto::DRBG_get_bytes(reinterpret_cast<uint8_t *>(dataVersionStorage.data()), dataSize) != CHIP_NO_ERROR)
388 : {
389 : // Now what? At least 0-init it.
390 0 : memset(dataVersionStorage.data(), 0, dataSize);
391 : }
392 : }
393 :
394 : // Now enable the endpoint.
395 28 : emberAfEndpointEnableDisable(id, true);
396 :
397 28 : emberMetadataStructureGeneration++;
398 28 : return CHIP_NO_ERROR;
399 : }
400 :
401 27 : EndpointId emberAfClearDynamicEndpoint(uint16_t index, MatterClusterShutdownType shutdownType)
402 : {
403 27 : EndpointId ep = 0;
404 :
405 27 : index = static_cast<uint16_t>(index + FIXED_ENDPOINT_COUNT);
406 :
407 54 : if ((index < MAX_ENDPOINT_COUNT) && (emAfEndpoints[index].endpoint != kInvalidEndpointId) &&
408 27 : (emberAfEndpointIndexIsEnabled(index)))
409 : {
410 27 : ep = emAfEndpoints[index].endpoint;
411 27 : emberAfEndpointEnableDisable(ep, false, shutdownType);
412 27 : emAfEndpoints[index].endpoint = kInvalidEndpointId;
413 : }
414 :
415 27 : emberMetadataStructureGeneration++;
416 27 : return ep;
417 : }
418 :
419 13853 : uint16_t emberAfFixedEndpointCount()
420 : {
421 13853 : return FIXED_ENDPOINT_COUNT;
422 : }
423 :
424 62652 : uint16_t emberAfEndpointCount()
425 : {
426 62652 : return emberEndpointCount;
427 : }
428 :
429 14236 : bool emberAfEndpointIndexIsEnabled(uint16_t index)
430 : {
431 14236 : return (emAfEndpoints[index].bitmask.Has(EmberAfEndpointOptions::isEnabled));
432 : }
433 :
434 : // This function is used to call the per-cluster attribute changed callback
435 0 : void emAfClusterAttributeChangedCallback(const ConcreteAttributePath & attributePath)
436 : {
437 0 : const EmberAfCluster * cluster = emberAfFindServerCluster(attributePath.mEndpointId, attributePath.mClusterId);
438 0 : if (cluster != nullptr)
439 : {
440 0 : EmberAfGenericClusterFunction f = emberAfFindClusterFunction(cluster, MATTER_CLUSTER_FLAG_ATTRIBUTE_CHANGED_FUNCTION);
441 0 : if (f != nullptr)
442 : {
443 0 : ((EmberAfClusterAttributeChangedCallback) f)(attributePath);
444 : }
445 : }
446 0 : }
447 :
448 : // This function is used to call the per-cluster pre-attribute changed callback
449 0 : Status emAfClusterPreAttributeChangedCallback(const ConcreteAttributePath & attributePath, EmberAfAttributeType attributeType,
450 : uint16_t size, uint8_t * value)
451 : {
452 0 : const EmberAfCluster * cluster = emberAfFindServerCluster(attributePath.mEndpointId, attributePath.mClusterId);
453 0 : if (cluster == nullptr)
454 : {
455 0 : if (!emberAfEndpointIsEnabled(attributePath.mEndpointId))
456 : {
457 0 : return Status::UnsupportedEndpoint;
458 : }
459 0 : return Status::UnsupportedCluster;
460 : }
461 :
462 0 : Status status = Status::Success;
463 : // Casting and calling a function pointer on the same line results in ignoring the return
464 : // of the call on gcc-arm-none-eabi-9-2019-q4-major
465 0 : EmberAfClusterPreAttributeChangedCallback f = (EmberAfClusterPreAttributeChangedCallback) (emberAfFindClusterFunction(
466 : cluster, MATTER_CLUSTER_FLAG_PRE_ATTRIBUTE_CHANGED_FUNCTION));
467 0 : if (f != nullptr)
468 : {
469 0 : status = f(attributePath, attributeType, size, value);
470 : }
471 0 : return status;
472 : }
473 :
474 59 : static void initializeEndpoint(EmberAfDefinedEndpoint * definedEndpoint)
475 : {
476 : uint8_t clusterIndex;
477 59 : const EmberAfEndpointType * epType = definedEndpoint->endpointType;
478 3604 : for (clusterIndex = 0; clusterIndex < epType->clusterCount; clusterIndex++)
479 : {
480 3545 : const EmberAfCluster * cluster = &(epType->cluster[clusterIndex]);
481 : EmberAfGenericClusterFunction f;
482 3545 : if (cluster->IsServer())
483 : {
484 : // Call the code-driven init callback before the emberAf... one,
485 : // so the latter can be used to configure code-driven clusters
486 35 : MatterClusterServerInitCallback(definedEndpoint->endpoint, cluster->clusterId);
487 : }
488 3545 : emberAfClusterInitCallback(definedEndpoint->endpoint, cluster->clusterId);
489 3545 : f = emberAfFindClusterFunction(cluster, MATTER_CLUSTER_FLAG_INIT_FUNCTION);
490 3545 : if (f != nullptr)
491 : {
492 0 : ((EmberAfInitFunction) f)(definedEndpoint->endpoint);
493 : }
494 : }
495 59 : }
496 :
497 28 : static void shutdownEndpoint(EmberAfDefinedEndpoint * definedEndpoint, MatterClusterShutdownType shutdownType)
498 : {
499 : // Call shutdown callbacks from clusters, mainly for canceling pending timers
500 : uint8_t clusterIndex;
501 28 : const EmberAfEndpointType * epType = definedEndpoint->endpointType;
502 62 : for (clusterIndex = 0; clusterIndex < epType->clusterCount; clusterIndex++)
503 : {
504 34 : const EmberAfCluster * cluster = &(epType->cluster[clusterIndex]);
505 34 : if (cluster->IsServer())
506 : {
507 34 : MatterClusterServerShutdownCallback(definedEndpoint->endpoint, cluster->clusterId, shutdownType);
508 : }
509 34 : EmberAfGenericClusterFunction f = emberAfFindClusterFunction(cluster, MATTER_CLUSTER_FLAG_SHUTDOWN_FUNCTION);
510 34 : if (f != nullptr)
511 : {
512 0 : ((EmberAfShutdownFunction) f)(definedEndpoint->endpoint);
513 : }
514 : }
515 :
516 28 : CommandHandlerInterfaceRegistry::Instance().UnregisterAllCommandHandlersForEndpoint(definedEndpoint->endpoint);
517 28 : AttributeAccessInterfaceRegistry::Instance().UnregisterAllForEndpoint(definedEndpoint->endpoint);
518 28 : }
519 :
520 : // Calls the init functions.
521 30 : void emAfCallInits()
522 : {
523 : uint16_t index;
524 60 : for (index = 0; index < emberAfEndpointCount(); index++)
525 : {
526 30 : if (emberAfEndpointIndexIsEnabled(index))
527 : {
528 30 : initializeEndpoint(&(emAfEndpoints[index]));
529 : }
530 : }
531 30 : }
532 :
533 : // Returns the pointer to metadata, or null if it is not found
534 6914 : const EmberAfAttributeMetadata * emberAfLocateAttributeMetadata(EndpointId endpoint, ClusterId clusterId, AttributeId attributeId)
535 : {
536 6914 : const EmberAfAttributeMetadata * metadata = nullptr;
537 : EmberAfAttributeSearchRecord record;
538 6914 : record.endpoint = endpoint;
539 6914 : record.clusterId = clusterId;
540 6914 : record.attributeId = attributeId;
541 6914 : emAfReadOrWriteAttribute(&record, &metadata,
542 : nullptr, // buffer
543 : 0, // buffer size
544 : false); // write?
545 6914 : return metadata;
546 : }
547 :
548 : // This function does mem copy, but smartly, which means that if the type is a
549 : // string, it will copy as much as it can.
550 : // If src == NULL, then this method will set memory to zeroes
551 : // See documentation for emAfReadOrWriteAttribute for the semantics of
552 : // readLength when reading and writing.
553 0 : static Status typeSensitiveMemCopy(ClusterId clusterId, uint8_t * dest, uint8_t * src, const EmberAfAttributeMetadata * am,
554 : bool write, uint16_t readLength)
555 : {
556 0 : EmberAfAttributeType attributeType = am->attributeType;
557 : // readLength == 0 for a read indicates that we should just trust that the
558 : // caller has enough space for an attribute...
559 0 : bool ignoreReadLength = write || (readLength == 0);
560 0 : uint16_t bufferSize = ignoreReadLength ? am->size : readLength;
561 :
562 0 : if (emberAfIsStringAttributeType(attributeType))
563 : {
564 0 : if (bufferSize < 1)
565 : {
566 0 : return Status::ResourceExhausted;
567 : }
568 0 : emberAfCopyString(dest, src, bufferSize - 1);
569 : }
570 0 : else if (emberAfIsLongStringAttributeType(attributeType))
571 : {
572 0 : if (bufferSize < 2)
573 : {
574 0 : return Status::ResourceExhausted;
575 : }
576 0 : emberAfCopyLongString(dest, src, bufferSize - 2);
577 : }
578 0 : else if (emberAfIsThisDataTypeAListType(attributeType))
579 : {
580 0 : if (bufferSize < 2)
581 : {
582 0 : return Status::ResourceExhausted;
583 : }
584 :
585 : // Just copy the length.
586 0 : memmove(dest, src, 2);
587 : }
588 : else
589 : {
590 0 : if (!ignoreReadLength && readLength < am->size)
591 : {
592 0 : return Status::ResourceExhausted;
593 : }
594 0 : if (src == nullptr)
595 : {
596 0 : memset(dest, 0, am->size);
597 : }
598 : else
599 : {
600 0 : memmove(dest, src, am->size);
601 : }
602 : }
603 0 : return Status::Success;
604 : }
605 :
606 : /**
607 : * @brief Matches a cluster based on cluster id and direction.
608 : *
609 : * This function assumes that the passed cluster's endpoint already
610 : * matches the endpoint of the EmberAfAttributeSearchRecord.
611 : *
612 : * Clusters match if:
613 : * 1. Cluster ids match AND
614 : * 2. Cluster is a server cluster (because there are no client attributes).
615 : */
616 6914 : bool emAfMatchCluster(const EmberAfCluster * cluster, const EmberAfAttributeSearchRecord * attRecord)
617 : {
618 6914 : return (cluster->clusterId == attRecord->clusterId && (cluster->mask & MATTER_CLUSTER_FLAG_SERVER));
619 : }
620 :
621 : /**
622 : * @brief Matches an attribute based on attribute id.
623 : * This function assumes that the passed cluster already matches the
624 : * clusterId and direction of the passed EmberAfAttributeSearchRecord.
625 : *
626 : * Attributes match if attr ids match.
627 : */
628 10021 : bool emAfMatchAttribute(const EmberAfCluster * cluster, const EmberAfAttributeMetadata * am,
629 : const EmberAfAttributeSearchRecord * attRecord)
630 : {
631 10021 : return (am->attributeId == attRecord->attributeId);
632 : }
633 :
634 : // When reading non-string attributes, this function returns an error when destination
635 : // buffer isn't large enough to accommodate the attribute type. For strings, the
636 : // function will copy at most readLength bytes. This means the resulting string
637 : // may be truncated. The length byte(s) in the resulting string will reflect
638 : // any truncation. If readLength is zero, we are working with backwards-
639 : // compatibility wrapper functions and we just cross our fingers and hope for
640 : // the best.
641 : //
642 : // When writing attributes, readLength is ignored. For non-string attributes,
643 : // this function assumes the source buffer is the same size as the attribute
644 : // type. For strings, the function will copy as many bytes as will fit in the
645 : // attribute. This means the resulting string may be truncated. The length
646 : // byte(s) in the resulting string will reflect any truncated.
647 6914 : Status emAfReadOrWriteAttribute(const EmberAfAttributeSearchRecord * attRecord, const EmberAfAttributeMetadata ** metadata,
648 : uint8_t * buffer, uint16_t readLength, bool write)
649 : {
650 6914 : assertChipStackLockedByCurrentThread();
651 :
652 6914 : uint16_t attributeOffsetIndex = 0;
653 :
654 13853 : for (uint16_t ep = 0; ep < emberAfEndpointCount(); ep++)
655 : {
656 : // Is this a dynamic endpoint?
657 13853 : bool isDynamicEndpoint = (ep >= emberAfFixedEndpointCount());
658 :
659 13853 : if (emAfEndpoints[ep].endpoint == attRecord->endpoint)
660 : {
661 6914 : const EmberAfEndpointType * endpointType = emAfEndpoints[ep].endpointType;
662 : uint8_t clusterIndex;
663 6914 : if (!emberAfEndpointIndexIsEnabled(ep))
664 : {
665 0 : continue;
666 : }
667 6914 : for (clusterIndex = 0; clusterIndex < endpointType->clusterCount; clusterIndex++)
668 : {
669 6914 : const EmberAfCluster * cluster = &(endpointType->cluster[clusterIndex]);
670 6914 : if (emAfMatchCluster(cluster, attRecord))
671 : { // Got the cluster
672 : uint16_t attrIndex;
673 10021 : for (attrIndex = 0; attrIndex < cluster->attributeCount; attrIndex++)
674 : {
675 10021 : const EmberAfAttributeMetadata * am = &(cluster->attributes[attrIndex]);
676 10021 : if (emAfMatchAttribute(cluster, am, attRecord))
677 : { // Got the attribute
678 : // If passed metadata location is not null, populate
679 6914 : if (metadata != nullptr)
680 : {
681 6914 : *metadata = am;
682 : }
683 :
684 6914 : uint8_t * attributeLocation = attributeData + attributeOffsetIndex;
685 : uint8_t *src, *dst;
686 6914 : if (write)
687 : {
688 0 : src = buffer;
689 0 : dst = attributeLocation;
690 0 : if (!emberAfAttributeWriteAccessCallback(attRecord->endpoint, attRecord->clusterId,
691 0 : am->attributeId))
692 : {
693 0 : return Status::UnsupportedAccess;
694 : }
695 : }
696 : else
697 : {
698 6914 : if (buffer == nullptr)
699 : {
700 6914 : return Status::Success;
701 : }
702 :
703 0 : src = attributeLocation;
704 0 : dst = buffer;
705 0 : if (!emberAfAttributeReadAccessCallback(attRecord->endpoint, attRecord->clusterId, am->attributeId))
706 : {
707 0 : return Status::UnsupportedAccess;
708 : }
709 : }
710 :
711 : // Is the attribute externally stored?
712 0 : if (am->mask & MATTER_ATTRIBUTE_FLAG_EXTERNAL_STORAGE)
713 : {
714 0 : if (write)
715 : {
716 0 : return emberAfExternalAttributeWriteCallback(attRecord->endpoint, attRecord->clusterId, am,
717 0 : buffer);
718 : }
719 :
720 0 : if (readLength < emberAfAttributeSize(am))
721 : {
722 : // Prevent a potential buffer overflow
723 0 : return Status::ResourceExhausted;
724 : }
725 :
726 0 : return emberAfExternalAttributeReadCallback(attRecord->endpoint, attRecord->clusterId, am, buffer,
727 0 : emberAfAttributeSize(am));
728 : }
729 :
730 : // Internal storage is only supported for fixed endpoints
731 0 : if (!isDynamicEndpoint)
732 : {
733 0 : return typeSensitiveMemCopy(attRecord->clusterId, dst, src, am, write, readLength);
734 : }
735 :
736 0 : return Status::Failure;
737 : }
738 :
739 : // Not the attribute we are looking for
740 : // Increase the index if attribute is not externally stored
741 3107 : if (!(am->mask & MATTER_ATTRIBUTE_FLAG_EXTERNAL_STORAGE))
742 : {
743 0 : attributeOffsetIndex = static_cast<uint16_t>(attributeOffsetIndex + emberAfAttributeSize(am));
744 : }
745 : }
746 :
747 : // Attribute is not in the cluster.
748 0 : return Status::UnsupportedAttribute;
749 : }
750 :
751 : // Not the cluster we are looking for
752 0 : attributeOffsetIndex = static_cast<uint16_t>(attributeOffsetIndex + cluster->clusterSize);
753 : }
754 :
755 : // Cluster is not in the endpoint.
756 0 : return Status::UnsupportedCluster;
757 : }
758 :
759 : // Not the endpoint we are looking for
760 : // Dynamic endpoints are external and don't factor into storage size
761 6939 : if (!isDynamicEndpoint)
762 : {
763 6914 : attributeOffsetIndex = static_cast<uint16_t>(attributeOffsetIndex + emAfEndpoints[ep].endpointType->endpointSize);
764 : }
765 : }
766 0 : return Status::UnsupportedEndpoint; // Sorry, endpoint was not found.
767 : }
768 :
769 4313 : const EmberAfEndpointType * emberAfFindEndpointType(EndpointId endpointId)
770 : {
771 4313 : uint16_t ep = emberAfIndexFromEndpoint(endpointId);
772 4313 : if (ep == kEmberInvalidEndpointIndex)
773 : {
774 8 : return nullptr;
775 : }
776 4305 : return emAfEndpoints[ep].endpointType;
777 : }
778 :
779 9504 : const EmberAfCluster * emberAfFindClusterInType(const EmberAfEndpointType * endpointType, ClusterId clusterId,
780 : EmberAfClusterMask mask, uint8_t * index)
781 : {
782 : uint8_t i;
783 9504 : uint8_t scopedIndex = 0;
784 :
785 9535 : for (i = 0; i < endpointType->clusterCount; i++)
786 : {
787 9525 : const EmberAfCluster * cluster = &(endpointType->cluster[i]);
788 :
789 9525 : if (mask == 0 || ((cluster->mask & mask) != 0))
790 : {
791 9525 : if (cluster->clusterId == clusterId)
792 : {
793 9494 : if (index)
794 : {
795 9459 : *index = scopedIndex;
796 : }
797 :
798 9494 : return cluster;
799 : }
800 :
801 31 : scopedIndex++;
802 : }
803 : }
804 :
805 10 : return nullptr;
806 : }
807 :
808 9469 : uint8_t emberAfClusterIndex(EndpointId endpoint, ClusterId clusterId, EmberAfClusterMask mask)
809 : {
810 19053 : for (uint16_t ep = 0; ep < emberAfEndpointCount(); ep++)
811 : {
812 : // Check the endpoint id first, because that way we avoid examining the
813 : // endpoint type for endpoints that are not actually defined.
814 19043 : if (emAfEndpoints[ep].endpoint == endpoint)
815 : {
816 9469 : const EmberAfEndpointType * endpointType = emAfEndpoints[ep].endpointType;
817 9469 : uint8_t index = 0xFF;
818 9469 : if (emberAfFindClusterInType(endpointType, clusterId, mask, &index) != nullptr)
819 : {
820 9459 : return index;
821 : }
822 : }
823 : }
824 10 : return 0xFF;
825 : }
826 :
827 : // Returns whether the given endpoint has the server of the given cluster on it.
828 0 : bool emberAfContainsServer(EndpointId endpoint, ClusterId clusterId)
829 : {
830 0 : return (emberAfFindServerCluster(endpoint, clusterId) != nullptr);
831 : }
832 :
833 : // Returns whether the given endpoint has the client of the given cluster on it.
834 0 : bool emberAfContainsClient(EndpointId endpoint, ClusterId clusterId)
835 : {
836 0 : uint16_t ep = emberAfIndexFromEndpoint(endpoint);
837 0 : if (ep == kEmberInvalidEndpointIndex)
838 : {
839 0 : return false;
840 : }
841 :
842 0 : return (emberAfFindClusterInType(emAfEndpoints[ep].endpointType, clusterId, MATTER_CLUSTER_FLAG_CLIENT) != nullptr);
843 : }
844 :
845 : // This will find the first server that has the clusterId given from the index of endpoint.
846 0 : bool emberAfContainsServerFromIndex(uint16_t index, ClusterId clusterId)
847 : {
848 0 : if (index == kEmberInvalidEndpointIndex)
849 : {
850 0 : return false;
851 : }
852 :
853 0 : return emberAfFindClusterInType(emAfEndpoints[index].endpointType, clusterId, MATTER_CLUSTER_FLAG_SERVER);
854 : }
855 :
856 : namespace chip {
857 : namespace app {
858 :
859 0 : EnabledEndpointsWithServerCluster::EnabledEndpointsWithServerCluster(ClusterId clusterId) :
860 0 : mEndpointCount(emberAfEndpointCount()), mClusterId(clusterId)
861 : {
862 0 : EnsureMatchingEndpoint();
863 0 : }
864 :
865 0 : EndpointId EnabledEndpointsWithServerCluster::operator*() const
866 : {
867 0 : return emberAfEndpointFromIndex(mEndpointIndex);
868 : }
869 :
870 0 : EnabledEndpointsWithServerCluster & EnabledEndpointsWithServerCluster::operator++()
871 : {
872 0 : ++mEndpointIndex;
873 0 : EnsureMatchingEndpoint();
874 0 : return *this;
875 : }
876 :
877 0 : void EnabledEndpointsWithServerCluster::EnsureMatchingEndpoint()
878 : {
879 0 : for (; mEndpointIndex < mEndpointCount; ++mEndpointIndex)
880 : {
881 0 : if (!emberAfEndpointIndexIsEnabled(mEndpointIndex))
882 : {
883 0 : continue;
884 : }
885 :
886 0 : if (emberAfContainsServerFromIndex(mEndpointIndex, mClusterId))
887 : {
888 0 : break;
889 : }
890 : }
891 0 : }
892 :
893 : } // namespace app
894 : } // namespace chip
895 :
896 : // Finds the cluster that matches endpoint, clusterId, direction.
897 36 : const EmberAfCluster * emberAfFindServerCluster(EndpointId endpoint, ClusterId clusterId)
898 : {
899 36 : uint16_t ep = emberAfIndexFromEndpoint(endpoint);
900 36 : if (ep == kEmberInvalidEndpointIndex)
901 : {
902 1 : return nullptr;
903 : }
904 :
905 35 : return emberAfFindClusterInType(emAfEndpoints[ep].endpointType, clusterId, MATTER_CLUSTER_FLAG_SERVER);
906 : }
907 :
908 : // Returns cluster within the endpoint; Does not ignore disabled endpoints
909 0 : const EmberAfCluster * emberAfFindClusterIncludingDisabledEndpoints(EndpointId endpoint, ClusterId clusterId,
910 : EmberAfClusterMask mask)
911 : {
912 0 : uint16_t ep = emberAfIndexFromEndpointIncludingDisabledEndpoints(endpoint);
913 0 : if (ep < MAX_ENDPOINT_COUNT)
914 : {
915 0 : return emberAfFindClusterInType(emAfEndpoints[ep].endpointType, clusterId, mask);
916 : }
917 0 : return nullptr;
918 : }
919 :
920 0 : uint16_t emberAfGetClusterServerEndpointIndex(EndpointId endpoint, ClusterId cluster, uint16_t fixedClusterServerEndpointCount)
921 : {
922 0 : VerifyOrDie(fixedClusterServerEndpointCount <= FIXED_ENDPOINT_COUNT);
923 0 : uint16_t epIndex = findIndexFromEndpoint(endpoint, true /*ignoreDisabledEndpoints*/);
924 :
925 : // Endpoint must be configured and enabled
926 0 : if (epIndex == kEmberInvalidEndpointIndex)
927 : {
928 0 : return kEmberInvalidEndpointIndex;
929 : }
930 :
931 0 : if (emberAfFindClusterInType(emAfEndpoints[epIndex].endpointType, cluster, MATTER_CLUSTER_FLAG_SERVER) == nullptr)
932 : {
933 : // The provided endpoint does not contain the given cluster server.
934 0 : return kEmberInvalidEndpointIndex;
935 : }
936 :
937 : #if FIXED_ENDPOINT_COUNT > 0
938 0 : if (epIndex < FIXED_ENDPOINT_COUNT)
939 : {
940 : // This endpoint is a fixed one.
941 : // Return the index of this endpoint in the list of fixed endpoints that support the given cluster.
942 0 : uint16_t adjustedEndpointIndex = 0;
943 0 : for (uint16_t i = 0; i < epIndex; i++)
944 : {
945 : // Increase adjustedEndpointIndex for every endpoint containing the cluster server
946 : // before our endpoint of interest
947 0 : if (emAfEndpoints[i].endpoint != kInvalidEndpointId &&
948 0 : (emberAfFindClusterInType(emAfEndpoints[i].endpointType, cluster, MATTER_CLUSTER_FLAG_SERVER) != nullptr))
949 : {
950 0 : adjustedEndpointIndex++;
951 : }
952 : }
953 :
954 : // If this asserts, the provided fixedClusterServerEndpointCount doesn't match the app data model.
955 0 : VerifyOrDie(adjustedEndpointIndex < fixedClusterServerEndpointCount);
956 0 : epIndex = adjustedEndpointIndex;
957 : }
958 : else
959 : #endif // FIXED_ENDPOINT_COUNT > 0
960 : {
961 : // This is a dynamic endpoint.
962 : // Its index is just its index in the dynamic endpoint list, offset by fixedClusterServerEndpointCount.
963 0 : epIndex = static_cast<uint16_t>(fixedClusterServerEndpointCount + (epIndex - FIXED_ENDPOINT_COUNT));
964 : }
965 :
966 0 : return epIndex;
967 : }
968 :
969 0 : bool emberAfEndpointIsEnabled(EndpointId endpoint)
970 : {
971 0 : uint16_t index = findIndexFromEndpoint(endpoint, false /* ignoreDisabledEndpoints */);
972 :
973 0 : if (kEmberInvalidEndpointIndex == index)
974 : {
975 0 : return false;
976 : }
977 :
978 0 : return emberAfEndpointIndexIsEnabled(index);
979 : }
980 :
981 57 : bool emberAfEndpointEnableDisable(EndpointId endpoint, bool enable, MatterClusterShutdownType shutdownType)
982 : {
983 57 : uint16_t index = findIndexFromEndpoint(endpoint, false /* ignoreDisabledEndpoints */);
984 : bool currentlyEnabled;
985 :
986 57 : if (kEmberInvalidEndpointIndex == index)
987 : {
988 0 : return false;
989 : }
990 :
991 57 : currentlyEnabled = emAfEndpoints[index].bitmask.Has(EmberAfEndpointOptions::isEnabled);
992 :
993 57 : if (enable)
994 : {
995 29 : emAfEndpoints[index].bitmask.Set(EmberAfEndpointOptions::isEnabled);
996 : }
997 :
998 57 : if (currentlyEnabled != enable)
999 : {
1000 57 : if (enable)
1001 : {
1002 29 : initializeEndpoint(&(emAfEndpoints[index]));
1003 : }
1004 : else
1005 : {
1006 28 : shutdownEndpoint(&(emAfEndpoints[index]), shutdownType);
1007 28 : emAfEndpoints[index].bitmask.Clear(EmberAfEndpointOptions::isEnabled);
1008 : }
1009 :
1010 : // The Descriptor cluster on Endpoint 0 subscribing to OnEndpointChanged events.
1011 : //
1012 : // NOTE: this should eventually be refactored for descriptor cluster to detect these changes
1013 57 : EndpointId parentEndpointId = emberAfParentEndpointFromIndex(index);
1014 57 : while (parentEndpointId != kInvalidEndpointId)
1015 : {
1016 0 : emberAfAttributeChanged(parentEndpointId, Clusters::Descriptor::Id, Clusters::Descriptor::Attributes::PartsList::Id);
1017 0 : uint16_t parentIndex = emberAfIndexFromEndpoint(parentEndpointId);
1018 0 : if (parentIndex == kEmberInvalidEndpointIndex)
1019 : {
1020 : // Something has gone wrong.
1021 0 : break;
1022 : }
1023 0 : parentEndpointId = emberAfParentEndpointFromIndex(parentIndex);
1024 : }
1025 :
1026 57 : CodegenDataModelProvider::Instance().NotifyEndpointChanged(
1027 : endpoint, enable ? DataModel::EndpointChangeType::kAdded : DataModel::EndpointChangeType::kRemoved);
1028 57 : emberAfAttributeChanged(/* endpoint = */ 0, Clusters::Descriptor::Id, Clusters::Descriptor::Attributes::PartsList::Id);
1029 : }
1030 :
1031 57 : emberMetadataStructureGeneration++;
1032 57 : return true;
1033 : }
1034 :
1035 12139 : unsigned emberAfMetadataStructureGeneration()
1036 : {
1037 12139 : return emberMetadataStructureGeneration;
1038 : }
1039 :
1040 : // Returns the index of a given endpoint. Does not consider disabled endpoints.
1041 13865 : uint16_t emberAfIndexFromEndpoint(EndpointId endpoint)
1042 : {
1043 13865 : return findIndexFromEndpoint(endpoint, true /* ignoreDisabledEndpoints */);
1044 : }
1045 :
1046 2921 : EndpointId emberAfEndpointFromIndex(uint16_t index)
1047 : {
1048 2921 : return emAfEndpoints[index].endpoint;
1049 : }
1050 :
1051 2978 : EndpointId emberAfParentEndpointFromIndex(uint16_t index)
1052 : {
1053 2978 : return emAfEndpoints[index].parentEndpointId;
1054 : }
1055 :
1056 : // If server == true, returns the number of server clusters,
1057 : // otherwise number of client clusters on this endpoint
1058 0 : uint8_t emberAfClusterCount(EndpointId endpoint, bool server)
1059 : {
1060 0 : uint16_t index = emberAfIndexFromEndpoint(endpoint);
1061 0 : if (index == kEmberInvalidEndpointIndex)
1062 : {
1063 0 : return 0;
1064 : }
1065 :
1066 0 : return emberAfClusterCountByIndex(index, server);
1067 : }
1068 :
1069 30 : uint8_t emberAfClusterCountByIndex(uint16_t endpointIndex, bool server)
1070 : {
1071 30 : const EmberAfDefinedEndpoint * de = &(emAfEndpoints[endpointIndex]);
1072 30 : if (de->endpointType == nullptr)
1073 : {
1074 0 : return 0;
1075 : }
1076 :
1077 30 : return emberAfClusterCountForEndpointType(de->endpointType, server);
1078 : }
1079 :
1080 4363 : uint8_t emberAfClusterCountForEndpointType(const EmberAfEndpointType * type, bool server)
1081 : {
1082 4363 : const EmberAfClusterMask cluster_mask = server ? MATTER_CLUSTER_FLAG_SERVER : MATTER_CLUSTER_FLAG_CLIENT;
1083 :
1084 4363 : return static_cast<uint8_t>(std::count_if(type->cluster, type->cluster + type->clusterCount,
1085 13741 : [=](const EmberAfCluster & cluster) { return (cluster.mask & cluster_mask) != 0; }));
1086 : }
1087 :
1088 0 : uint8_t emberAfGetClusterCountForEndpoint(EndpointId endpoint)
1089 : {
1090 0 : uint16_t index = emberAfIndexFromEndpoint(endpoint);
1091 0 : if (index == kEmberInvalidEndpointIndex)
1092 : {
1093 0 : return 0;
1094 : }
1095 0 : return emAfEndpoints[index].endpointType->clusterCount;
1096 : }
1097 :
1098 0 : Span<const EmberAfDeviceType> emberAfDeviceTypeListFromEndpoint(EndpointId endpoint, CHIP_ERROR & err)
1099 : {
1100 0 : return emberAfDeviceTypeListFromEndpointIndex(emberAfIndexFromEndpoint(endpoint), err);
1101 : }
1102 :
1103 0 : chip::Span<const EmberAfDeviceType> emberAfDeviceTypeListFromEndpointIndex(unsigned endpointIndex, CHIP_ERROR & err)
1104 : {
1105 0 : if (endpointIndex == 0xFFFF)
1106 : {
1107 0 : err = CHIP_ERROR_INVALID_ARGUMENT;
1108 0 : return Span<const EmberAfDeviceType>();
1109 : }
1110 :
1111 0 : err = CHIP_NO_ERROR;
1112 0 : return emAfEndpoints[endpointIndex].deviceTypeList;
1113 : }
1114 :
1115 0 : void GetSemanticTagsForEndpoint(EndpointId endpoint,
1116 : Span<const Clusters::Descriptor::Structs::SemanticTagStruct::Type> & semanticTags)
1117 : {
1118 0 : uint16_t endpointIndex = emberAfIndexFromEndpoint(endpoint);
1119 :
1120 0 : if (endpointIndex == 0xFFFF)
1121 : {
1122 0 : semanticTags = Span<const Clusters::Descriptor::Structs::SemanticTagStruct::Type>();
1123 0 : return;
1124 : }
1125 0 : semanticTags = emAfEndpoints[endpointIndex].tagList;
1126 : }
1127 :
1128 0 : CHIP_ERROR GetSemanticTagForEndpointAtIndex(EndpointId endpoint, size_t index,
1129 : Clusters::Descriptor::Structs::SemanticTagStruct::Type & tag)
1130 : {
1131 0 : uint16_t endpointIndex = emberAfIndexFromEndpoint(endpoint);
1132 :
1133 0 : if (endpointIndex == 0xFFFF || index >= emAfEndpoints[endpointIndex].tagList.size())
1134 : {
1135 0 : return CHIP_ERROR_NOT_FOUND;
1136 : }
1137 0 : tag = emAfEndpoints[endpointIndex].tagList[index];
1138 0 : return CHIP_NO_ERROR;
1139 : }
1140 : #if CHIP_CONFIG_USE_ENDPOINT_UNIQUE_ID
1141 : CHIP_ERROR emberAfGetEndpointUniqueIdForEndPoint(EndpointId endpoint, MutableCharSpan & epUniqueIdMutSpan)
1142 : {
1143 : uint16_t endpointIndex = emberAfIndexFromEndpoint(endpoint);
1144 :
1145 : if (endpointIndex == 0xFFFF)
1146 : {
1147 : return CHIP_ERROR_NOT_FOUND;
1148 : }
1149 :
1150 : CharSpan epUniqueIdSpan(emAfEndpoints[endpointIndex].endpointUniqueId, emAfEndpoints[endpointIndex].endpointUniqueIdSize);
1151 : return CopyCharSpanToMutableCharSpan(epUniqueIdSpan, epUniqueIdMutSpan);
1152 : }
1153 : #endif
1154 0 : CHIP_ERROR emberAfSetDeviceTypeList(EndpointId endpoint, Span<const EmberAfDeviceType> deviceTypeList)
1155 : {
1156 0 : uint16_t endpointIndex = emberAfIndexFromEndpoint(endpoint);
1157 0 : if (endpointIndex == 0xFFFF)
1158 : {
1159 0 : return CHIP_ERROR_INVALID_ARGUMENT;
1160 : }
1161 :
1162 0 : emAfEndpoints[endpointIndex].deviceTypeList = deviceTypeList;
1163 0 : return CHIP_NO_ERROR;
1164 : }
1165 :
1166 0 : CHIP_ERROR SetTagList(EndpointId endpoint, Span<const Clusters::Descriptor::Structs::SemanticTagStruct::Type> tagList)
1167 : {
1168 0 : uint16_t endpointIndex = emberAfIndexFromEndpoint(endpoint);
1169 0 : if (endpointIndex == 0xFFFF)
1170 : {
1171 0 : return CHIP_ERROR_INVALID_ARGUMENT;
1172 : }
1173 :
1174 0 : emAfEndpoints[endpointIndex].tagList = tagList;
1175 0 : return CHIP_NO_ERROR;
1176 : }
1177 :
1178 : // Returns the cluster of Nth server or client cluster,
1179 : // depending on server toggle.
1180 0 : const EmberAfCluster * emberAfGetNthCluster(EndpointId endpoint, uint8_t n, bool server)
1181 : {
1182 0 : uint16_t index = emberAfIndexFromEndpoint(endpoint);
1183 0 : if (index == kEmberInvalidEndpointIndex)
1184 : {
1185 0 : return nullptr;
1186 : }
1187 :
1188 0 : const EmberAfEndpointType * endpointType = emAfEndpoints[index].endpointType;
1189 0 : const EmberAfClusterMask cluster_mask = server ? MATTER_CLUSTER_FLAG_SERVER : MATTER_CLUSTER_FLAG_CLIENT;
1190 0 : const uint8_t clusterCount = endpointType->clusterCount;
1191 :
1192 0 : uint8_t c = 0;
1193 0 : for (uint8_t i = 0; i < clusterCount; i++)
1194 : {
1195 0 : const EmberAfCluster * cluster = &(endpointType->cluster[i]);
1196 :
1197 0 : if ((cluster->mask & cluster_mask) == 0)
1198 : {
1199 0 : continue;
1200 : }
1201 :
1202 0 : if (c == n)
1203 : {
1204 0 : return cluster;
1205 : }
1206 :
1207 0 : c++;
1208 : }
1209 0 : return nullptr;
1210 : }
1211 :
1212 : // Returns the cluster id of Nth server or client cluster,
1213 : // depending on server toggle.
1214 : // Returns Optional<ClusterId>::Missing() if cluster does not exist.
1215 0 : Optional<ClusterId> emberAfGetNthClusterId(EndpointId endpoint, uint8_t n, bool server)
1216 : {
1217 0 : const EmberAfCluster * cluster = emberAfGetNthCluster(endpoint, n, server);
1218 0 : if (cluster == nullptr)
1219 : {
1220 0 : return Optional<ClusterId>::Missing();
1221 : }
1222 0 : return Optional<ClusterId>(cluster->clusterId);
1223 : }
1224 :
1225 : // Returns number of clusters put into the passed cluster list
1226 : // for the given endpoint and client/server polarity
1227 0 : uint8_t emberAfGetClustersFromEndpoint(EndpointId endpoint, ClusterId * clusterList, uint8_t listLen, bool server)
1228 : {
1229 0 : uint8_t clusterCount = emberAfClusterCount(endpoint, server);
1230 : uint8_t i;
1231 : const EmberAfCluster * cluster;
1232 0 : if (clusterCount > listLen)
1233 : {
1234 0 : clusterCount = listLen;
1235 : }
1236 0 : for (i = 0; i < clusterCount; i++)
1237 : {
1238 0 : cluster = emberAfGetNthCluster(endpoint, i, server);
1239 0 : clusterList[i] = (cluster == nullptr ? kEmberInvalidEndpointIndex : cluster->clusterId);
1240 : }
1241 0 : return clusterCount;
1242 : }
1243 :
1244 30 : void emberAfInitializeAttributes(EndpointId endpoint)
1245 : {
1246 30 : emAfLoadAttributeDefaults(endpoint);
1247 30 : }
1248 :
1249 30 : void emAfLoadAttributeDefaults(EndpointId endpoint, Optional<ClusterId> clusterId)
1250 : {
1251 : uint16_t ep;
1252 : uint8_t clusterI;
1253 : uint16_t attr;
1254 : uint8_t * ptr;
1255 30 : uint16_t epCount = emberAfEndpointCount();
1256 : uint8_t attrData[ATTRIBUTE_LARGEST];
1257 30 : auto * attrStorage = GetAttributePersistenceProvider();
1258 : // Don't check whether we actually have an attrStorage here, because it's OK
1259 : // to have one if none of our attributes have NVM storage.
1260 :
1261 60 : for (ep = 0; ep < epCount; ep++)
1262 : {
1263 : EmberAfDefinedEndpoint * de;
1264 30 : if (endpoint != kInvalidEndpointId)
1265 : {
1266 0 : ep = emberAfIndexFromEndpoint(endpoint);
1267 0 : if (ep == kEmberInvalidEndpointIndex)
1268 : {
1269 0 : return;
1270 : }
1271 : }
1272 30 : de = &(emAfEndpoints[ep]);
1273 :
1274 3540 : for (clusterI = 0; clusterI < de->endpointType->clusterCount; clusterI++)
1275 : {
1276 3510 : const EmberAfCluster * cluster = &(de->endpointType->cluster[clusterI]);
1277 3510 : if (clusterId.HasValue())
1278 : {
1279 0 : if (clusterId.Value() != cluster->clusterId)
1280 : {
1281 0 : continue;
1282 : }
1283 : }
1284 :
1285 : // when the attributeCount is high, the loop takes too long to run and a
1286 : // watchdog kicks in causing a reset. As a workaround, we'll
1287 : // conditionally manually reset the watchdog. 300 sounds like a good
1288 : // magic number for now.
1289 3510 : if (cluster->attributeCount > 300)
1290 : {
1291 : // halResetWatchdog();
1292 : }
1293 3510 : for (attr = 0; attr < cluster->attributeCount; attr++)
1294 : {
1295 0 : const EmberAfAttributeMetadata * am = &(cluster->attributes[attr]);
1296 0 : ptr = nullptr; // Will get set to the value to write, as needed.
1297 :
1298 : // First check for a persisted value.
1299 0 : if (am->IsAutomaticallyPersisted())
1300 : {
1301 0 : VerifyOrDieWithMsg(attrStorage != nullptr, Zcl, "Attribute persistence needs a persistence provider");
1302 0 : MutableByteSpan bytes(attrData);
1303 : CHIP_ERROR err =
1304 0 : attrStorage->ReadValue(ConcreteAttributePath(de->endpoint, cluster->clusterId, am->attributeId), bytes);
1305 0 : if (err == CHIP_NO_ERROR)
1306 : {
1307 0 : err = ValidateDataContent(bytes, am);
1308 : }
1309 :
1310 0 : if (err == CHIP_NO_ERROR)
1311 : {
1312 0 : ptr = attrData;
1313 : }
1314 : else
1315 : {
1316 0 : ChipLogDetail(
1317 : DataManagement,
1318 : "Failed to read stored attribute (%u, " ChipLogFormatMEI ", " ChipLogFormatMEI ": %" CHIP_ERROR_FORMAT,
1319 : de->endpoint, ChipLogValueMEI(cluster->clusterId), ChipLogValueMEI(am->attributeId), err.Format());
1320 : // Just fall back to default value.
1321 : }
1322 : }
1323 :
1324 0 : if (!am->IsExternal())
1325 : {
1326 : EmberAfAttributeSearchRecord record;
1327 0 : record.endpoint = de->endpoint;
1328 0 : record.clusterId = cluster->clusterId;
1329 0 : record.attributeId = am->attributeId;
1330 :
1331 0 : if (ptr == nullptr)
1332 : {
1333 0 : size_t defaultValueSizeForBigEndianNudger = 0;
1334 : // Bypasses compiler warning about unused variable for little endian platforms.
1335 : (void) defaultValueSizeForBigEndianNudger;
1336 0 : if ((am->mask & MATTER_ATTRIBUTE_FLAG_MIN_MAX) != 0U)
1337 : {
1338 : // This is intentionally 2 and not 4 bytes since defaultValue in min/max
1339 : // attributes is still uint16_t.
1340 0 : if (emberAfAttributeSize(am) <= 2)
1341 : {
1342 : static_assert(sizeof(am->defaultValue.ptrToMinMaxValue->defaultValue.defaultValue) == 2,
1343 : "if statement relies on size of max/min defaultValue being 2");
1344 0 : ptr = (uint8_t *) &(am->defaultValue.ptrToMinMaxValue->defaultValue.defaultValue);
1345 0 : defaultValueSizeForBigEndianNudger =
1346 : sizeof(am->defaultValue.ptrToMinMaxValue->defaultValue.defaultValue);
1347 : }
1348 : else
1349 : {
1350 0 : ptr = (uint8_t *) am->defaultValue.ptrToMinMaxValue->defaultValue.ptrToDefaultValue;
1351 : }
1352 : }
1353 : else
1354 : {
1355 0 : if ((emberAfAttributeSize(am) <= 4) && !emberAfIsStringAttributeType(am->attributeType))
1356 : {
1357 0 : ptr = (uint8_t *) &(am->defaultValue.defaultValue);
1358 0 : defaultValueSizeForBigEndianNudger = sizeof(am->defaultValue.defaultValue);
1359 : }
1360 : else
1361 : {
1362 0 : ptr = (uint8_t *) am->defaultValue.ptrToDefaultValue;
1363 : }
1364 : }
1365 : // At this point, ptr either points to a default value, or is NULL, in which case
1366 : // it should be treated as if it is pointing to an array of all zeroes.
1367 :
1368 : #if (CHIP_CONFIG_BIG_ENDIAN_TARGET)
1369 : // The default values for attributes that are less than or equal to
1370 : // defaultValueSizeForBigEndianNudger in bytes are stored in an
1371 : // uint32_t. On big-endian platforms, a pointer to the default value
1372 : // of size less than defaultValueSizeForBigEndianNudger will point to the wrong
1373 : // byte. So, for those cases, nudge the pointer forward so it points
1374 : // to the correct byte.
1375 : if (emberAfAttributeSize(am) < defaultValueSizeForBigEndianNudger && ptr != NULL)
1376 : {
1377 : ptr += (defaultValueSizeForBigEndianNudger - emberAfAttributeSize(am));
1378 : }
1379 : #endif // BIGENDIAN
1380 : }
1381 :
1382 0 : emAfReadOrWriteAttribute(&record,
1383 : nullptr, // metadata - unused
1384 : ptr,
1385 : 0, // buffer size - unused
1386 : true); // write?
1387 : }
1388 : }
1389 : }
1390 30 : if (endpoint != kInvalidEndpointId)
1391 : {
1392 0 : break;
1393 : }
1394 : }
1395 : }
1396 :
1397 : // 'data' argument may be null, since we changed the ptrToDefaultValue
1398 : // to be null instead of pointing to all zeroes.
1399 : // This function has to be able to deal with that.
1400 0 : void emAfSaveAttributeToStorageIfNeeded(uint8_t * data, EndpointId endpoint, ClusterId clusterId,
1401 : const EmberAfAttributeMetadata * metadata)
1402 : {
1403 : // Get out of here if this attribute isn't marked non-volatile.
1404 0 : if (!metadata->IsAutomaticallyPersisted())
1405 : {
1406 0 : return;
1407 : }
1408 :
1409 : // TODO: Maybe we should have a separate constant for the size of the
1410 : // largest non-volatile attribute?
1411 0 : uint8_t allZeroData[ATTRIBUTE_LARGEST] = { 0 };
1412 0 : if (data == nullptr)
1413 : {
1414 0 : data = allZeroData;
1415 : }
1416 :
1417 : size_t dataSize;
1418 0 : EmberAfAttributeType type = metadata->attributeType;
1419 0 : if (emberAfIsStringAttributeType(type))
1420 : {
1421 0 : dataSize = emberAfStringLength(data) + 1;
1422 : }
1423 0 : else if (emberAfIsLongStringAttributeType(type))
1424 : {
1425 0 : dataSize = emberAfLongStringLength(data) + 2;
1426 : }
1427 : else
1428 : {
1429 0 : dataSize = metadata->size;
1430 : }
1431 :
1432 0 : auto * attrStorage = GetAttributePersistenceProvider();
1433 0 : if (attrStorage)
1434 : {
1435 0 : TEMPORARY_RETURN_IGNORED attrStorage->WriteValue(ConcreteAttributePath(endpoint, clusterId, metadata->attributeId),
1436 0 : ByteSpan(data, dataSize));
1437 : }
1438 : else
1439 : {
1440 0 : ChipLogProgress(DataManagement, "Can't store attribute value: no persistence provider");
1441 : }
1442 : }
1443 :
1444 : // This function returns the actual function point from the array,
1445 : // iterating over the function bits.
1446 3579 : EmberAfGenericClusterFunction emberAfFindClusterFunction(const EmberAfCluster * cluster, EmberAfClusterMask functionMask)
1447 : {
1448 3579 : EmberAfClusterMask mask = 0x01;
1449 3579 : uint8_t functionIndex = 0;
1450 :
1451 3579 : if ((cluster->mask & functionMask) == 0)
1452 : {
1453 3579 : return nullptr;
1454 : }
1455 :
1456 0 : while (mask < functionMask)
1457 : {
1458 0 : if ((cluster->mask & mask) != 0)
1459 : {
1460 0 : functionIndex++;
1461 : }
1462 0 : mask = static_cast<EmberAfClusterMask>(mask << 1);
1463 : }
1464 0 : return cluster->functions[functionIndex];
1465 : }
1466 :
1467 : namespace chip {
1468 : namespace app {
1469 :
1470 0 : CHIP_ERROR SetParentEndpointForEndpoint(EndpointId childEndpoint, EndpointId parentEndpoint)
1471 : {
1472 0 : uint16_t childIndex = emberAfIndexFromEndpoint(childEndpoint);
1473 0 : uint16_t parentIndex = emberAfIndexFromEndpoint(parentEndpoint);
1474 :
1475 0 : if (childIndex == kEmberInvalidEndpointIndex || parentIndex == kEmberInvalidEndpointIndex)
1476 : {
1477 0 : return CHIP_ERROR_INVALID_ARGUMENT;
1478 : }
1479 0 : emAfEndpoints[childIndex].parentEndpointId = parentEndpoint;
1480 0 : return CHIP_NO_ERROR;
1481 : }
1482 :
1483 0 : CHIP_ERROR SetFlatCompositionForEndpoint(EndpointId endpoint)
1484 : {
1485 0 : uint16_t index = emberAfIndexFromEndpoint(endpoint);
1486 0 : if (index == kEmberInvalidEndpointIndex)
1487 : {
1488 0 : return CHIP_ERROR_INVALID_ARGUMENT;
1489 : }
1490 0 : emAfEndpoints[index].bitmask.Set(EmberAfEndpointOptions::isFlatComposition);
1491 0 : return CHIP_NO_ERROR;
1492 : }
1493 :
1494 0 : CHIP_ERROR SetTreeCompositionForEndpoint(EndpointId endpoint)
1495 : {
1496 0 : uint16_t index = emberAfIndexFromEndpoint(endpoint);
1497 0 : if (index == kEmberInvalidEndpointIndex)
1498 : {
1499 0 : return CHIP_ERROR_INVALID_ARGUMENT;
1500 : }
1501 0 : emAfEndpoints[index].bitmask.Clear(EmberAfEndpointOptions::isFlatComposition);
1502 0 : return CHIP_NO_ERROR;
1503 : }
1504 :
1505 0 : bool IsFlatCompositionForEndpoint(EndpointId endpoint)
1506 : {
1507 0 : uint16_t index = emberAfIndexFromEndpoint(endpoint);
1508 0 : if (index == kEmberInvalidEndpointIndex)
1509 : {
1510 0 : return false;
1511 : }
1512 0 : return emAfEndpoints[index].bitmask.Has(EmberAfEndpointOptions::isFlatComposition);
1513 : }
1514 :
1515 0 : bool IsTreeCompositionForEndpoint(EndpointId endpoint)
1516 : {
1517 0 : uint16_t index = emberAfIndexFromEndpoint(endpoint);
1518 0 : if (index == kEmberInvalidEndpointIndex)
1519 : {
1520 0 : return false;
1521 : }
1522 0 : return !emAfEndpoints[index].bitmask.Has(EmberAfEndpointOptions::isFlatComposition);
1523 : }
1524 :
1525 2921 : EndpointComposition GetCompositionForEndpointIndex(uint16_t endpointIndex)
1526 : {
1527 2921 : VerifyOrReturnValue(endpointIndex < MATTER_ARRAY_SIZE(emAfEndpoints), EndpointComposition::kInvalid);
1528 2921 : if (emAfEndpoints[endpointIndex].bitmask.Has(EmberAfEndpointOptions::isFlatComposition))
1529 : {
1530 1441 : return EndpointComposition::kFullFamily;
1531 : }
1532 1480 : return EndpointComposition::kTree;
1533 : }
1534 :
1535 : } // namespace app
1536 : } // namespace chip
1537 :
1538 0 : uint16_t emberAfGetServerAttributeCount(EndpointId endpoint, ClusterId cluster)
1539 : {
1540 0 : const EmberAfCluster * clusterObj = emberAfFindServerCluster(endpoint, cluster);
1541 0 : VerifyOrReturnError(clusterObj != nullptr, 0);
1542 0 : return clusterObj->attributeCount;
1543 : }
1544 :
1545 0 : uint16_t emberAfGetServerAttributeIndexByAttributeId(EndpointId endpoint, ClusterId cluster, AttributeId attributeId)
1546 : {
1547 0 : const EmberAfCluster * clusterObj = emberAfFindServerCluster(endpoint, cluster);
1548 0 : VerifyOrReturnError(clusterObj != nullptr, UINT16_MAX);
1549 :
1550 0 : for (uint16_t i = 0; i < clusterObj->attributeCount; i++)
1551 : {
1552 0 : if (clusterObj->attributes[i].attributeId == attributeId)
1553 : {
1554 0 : return i;
1555 : }
1556 : }
1557 0 : return UINT16_MAX;
1558 : }
1559 :
1560 0 : Optional<AttributeId> emberAfGetServerAttributeIdByIndex(EndpointId endpoint, ClusterId cluster, uint16_t attributeIndex)
1561 : {
1562 0 : const EmberAfCluster * clusterObj = emberAfFindServerCluster(endpoint, cluster);
1563 0 : if (clusterObj == nullptr || clusterObj->attributeCount <= attributeIndex)
1564 : {
1565 0 : return Optional<AttributeId>::Missing();
1566 : }
1567 0 : return Optional<AttributeId>(clusterObj->attributes[attributeIndex].attributeId);
1568 : }
1569 :
1570 9516 : DataVersion * emberAfDataVersionStorage(const ConcreteClusterPath & aConcreteClusterPath)
1571 : {
1572 9516 : uint16_t index = emberAfIndexFromEndpoint(aConcreteClusterPath.mEndpointId);
1573 9516 : if (index == kEmberInvalidEndpointIndex)
1574 : {
1575 : // Unknown endpoint.
1576 47 : return nullptr;
1577 : }
1578 9469 : const EmberAfDefinedEndpoint & ep = emAfEndpoints[index];
1579 9469 : if (!ep.dataVersions)
1580 : {
1581 : // No storage provided.
1582 0 : return nullptr;
1583 : }
1584 :
1585 : // This does a second walk over endpoints to find the right one, but
1586 : // probably worth it to avoid duplicating code.
1587 : auto clusterIndex =
1588 9469 : emberAfClusterIndex(aConcreteClusterPath.mEndpointId, aConcreteClusterPath.mClusterId, MATTER_CLUSTER_FLAG_SERVER);
1589 9469 : if (clusterIndex == 0xFF)
1590 : {
1591 : // No such cluster on this endpoint.
1592 10 : return nullptr;
1593 : }
1594 :
1595 9459 : return ep.dataVersions + clusterIndex;
1596 : }
1597 :
1598 5203 : void emberAfIncreaseDataVersion(const chip::app::ConcreteClusterPath & aConcreteClusterPath)
1599 : {
1600 :
1601 5203 : DataVersion * version = emberAfDataVersionStorage(aConcreteClusterPath);
1602 5203 : if (version == nullptr)
1603 : {
1604 57 : ChipLogError(DataManagement, "Endpoint %x, Cluster " ChipLogFormatMEI " not found in emberAfIncreaseDataVersion!",
1605 : aConcreteClusterPath.mEndpointId, ChipLogValueMEI(aConcreteClusterPath.mClusterId));
1606 : }
1607 : else
1608 : {
1609 5146 : (*(version))++;
1610 5146 : ChipLogDetail(DataManagement, "Endpoint %x, Cluster " ChipLogFormatMEI " update version to %" PRIx32,
1611 : aConcreteClusterPath.mEndpointId, ChipLogValueMEI(aConcreteClusterPath.mClusterId), *version);
1612 : }
1613 5203 : }
1614 :
1615 5203 : void emberAfAttributeChanged(EndpointId endpoint, ClusterId clusterId, AttributeId attributeId)
1616 : {
1617 5203 : const ConcreteAttributePath path(endpoint, clusterId, attributeId);
1618 :
1619 5203 : emberAfIncreaseDataVersion(path);
1620 5203 : CodegenDataModelProvider::Instance().NotifyAttributeChanged(path, chip::app::DataModel::AttributeChangeType::kReportable);
1621 5203 : }
|