Matter SDK Coverage Report
Current view: top level - app/reporting - Engine.cpp (source / functions) Coverage Total Hit
Test: SHA:e98a48c2e59f85a25417956e1d105721433aa5d1 Lines: 93.8 % 520 488
Test Date: 2026-01-09 16:53:50 Functions: 97.7 % 43 42

            Line data    Source code
       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              : #include <access/AccessRestrictionProvider.h>
      20              : #include <access/Privilege.h>
      21              : #include <app/AppConfig.h>
      22              : #include <app/AttributePathExpandIterator.h>
      23              : #include <app/ConcreteEventPath.h>
      24              : #include <app/GlobalAttributes.h>
      25              : #include <app/InteractionModelEngine.h>
      26              : #include <app/MessageDef/StatusIB.h>
      27              : #include <app/data-model-provider/ActionReturnStatus.h>
      28              : #include <app/data-model-provider/MetadataLookup.h>
      29              : #include <app/data-model-provider/MetadataTypes.h>
      30              : #include <app/data-model-provider/Provider.h>
      31              : #include <app/icd/server/ICDServerConfig.h>
      32              : #include <app/reporting/Engine.h>
      33              : #include <app/reporting/reporting.h>
      34              : #include <app/util/MatterCallbacks.h>
      35              : #include <lib/core/CHIPError.h>
      36              : #include <lib/core/DataModelTypes.h>
      37              : #include <lib/support/CodeUtils.h>
      38              : #include <protocols/interaction_model/StatusCode.h>
      39              : 
      40              : #include <optional>
      41              : 
      42              : #if CHIP_CONFIG_ENABLE_ICD_SERVER
      43              : #include <app/icd/server/ICDNotifier.h> // nogncheck
      44              : #endif
      45              : 
      46              : using namespace chip::Access;
      47              : 
      48              : namespace chip {
      49              : namespace app {
      50              : namespace reporting {
      51              : namespace {
      52              : 
      53              : using DataModel::ReadFlags;
      54              : using Protocols::InteractionModel::Status;
      55              : 
      56              : /// Returns the status of ACL validation.
      57              : ///   If the return value has a status set, that means the ACL check failed,
      58              : ///   the read must not be performed, and the returned status (which may
      59              : ///   be success, when dealing with non-concrete paths) should be used
      60              : ///   as the status for the read.
      61              : ///
      62              : ///   If the returned value is std::nullopt, that means the ACL check passed and the
      63              : ///   read should proceed.
      64         9848 : std::optional<CHIP_ERROR> ValidateReadAttributeACL(const SubjectDescriptor & subjectDescriptor,
      65              :                                                    const ConcreteReadAttributePath & path, Privilege requiredPrivilege)
      66              : {
      67              : 
      68         9848 :     RequestPath requestPath{ .cluster     = path.mClusterId,
      69         9848 :                              .endpoint    = path.mEndpointId,
      70              :                              .requestType = RequestType::kAttributeReadRequest,
      71         9848 :                              .entityId    = path.mAttributeId };
      72              : 
      73         9848 :     CHIP_ERROR err = GetAccessControl().Check(subjectDescriptor, requestPath, requiredPrivilege);
      74        19696 :     if (err == CHIP_NO_ERROR)
      75              :     {
      76         9847 :         return std::nullopt;
      77              :     }
      78            2 :     VerifyOrReturnError((err == CHIP_ERROR_ACCESS_DENIED) || (err == CHIP_ERROR_ACCESS_RESTRICTED_BY_ARL), err);
      79              : 
      80              :     // Implementation of 8.4.3.2 of the spec for path expansion
      81            1 :     if (path.mExpanded)
      82              :     {
      83            0 :         return CHIP_NO_ERROR;
      84              :     }
      85              : 
      86              :     // access denied and access restricted have specific codes for IM
      87            2 :     return err == CHIP_ERROR_ACCESS_DENIED ? CHIP_IM_GLOBAL_STATUS(UnsupportedAccess) : CHIP_IM_GLOBAL_STATUS(AccessRestricted);
      88              : }
      89              : 
      90              : /// Checks that the given attribute path corresponds to a readable attribute. If not, it
      91              : /// will return the corresponding failure status.
      92         4924 : std::optional<Status> ValidateAttributeIsReadable(DataModel::Provider * dataModel, const ConcreteReadAttributePath & path,
      93              :                                                   const std::optional<DataModel::AttributeEntry> & entry)
      94              : {
      95         4924 :     if (!entry.has_value())
      96              :     {
      97            1 :         return DataModel::ValidateClusterPath(dataModel, path, Status::UnsupportedAttribute);
      98              :     }
      99              : 
     100         4923 :     if (!entry->GetReadPrivilege().has_value())
     101              :     {
     102            0 :         return Status::UnsupportedRead;
     103              :     }
     104              : 
     105         4923 :     return std::nullopt;
     106              : }
     107              : 
     108         4925 : DataModel::ActionReturnStatus RetrieveClusterData(DataModel::Provider * dataModel, const SubjectDescriptor & subjectDescriptor,
     109              :                                                   BitFlags<ReadFlags> flags, AttributeReportIBs::Builder & reportBuilder,
     110              :                                                   const ConcreteReadAttributePath & path, AttributeEncodeState * encoderState)
     111              : {
     112         4925 :     ChipLogDetail(DataManagement, "<RE:Run> Cluster %" PRIx32 ", Attribute %" PRIx32 " is dirty", path.mClusterId,
     113              :                   path.mAttributeId);
     114         4925 :     DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Read,
     115              :                                                           DataModelCallbacks::OperationOrder::Pre, path);
     116              : 
     117         4925 :     DataModel::ReadAttributeRequest readRequest;
     118              : 
     119         4925 :     readRequest.readFlags         = flags;
     120         4925 :     readRequest.subjectDescriptor = &subjectDescriptor;
     121         4925 :     readRequest.path              = path;
     122              : 
     123         4925 :     DataModel::ServerClusterFinder serverClusterFinder(dataModel);
     124              : 
     125         4925 :     DataVersion version = 0;
     126         4925 :     if (auto clusterInfo = serverClusterFinder.Find(path); clusterInfo.has_value())
     127              :     {
     128         4924 :         version = clusterInfo->dataVersion;
     129              :     }
     130              :     else
     131              :     {
     132            1 :         ChipLogError(DataManagement, "Read request on unknown cluster - no data version available");
     133              :     }
     134              : 
     135         4925 :     TLV::TLVWriter checkpoint;
     136         4925 :     reportBuilder.Checkpoint(checkpoint);
     137              : 
     138         4925 :     DataModel::ActionReturnStatus status(CHIP_NO_ERROR);
     139         4925 :     bool isFabricFiltered = flags.Has(ReadFlags::kFabricFiltered);
     140         4925 :     AttributeValueEncoder attributeValueEncoder(reportBuilder, subjectDescriptor, path, version, isFabricFiltered, encoderState);
     141              : 
     142              :     // TODO: we explicitly DO NOT validate that path is a valid cluster path (even more, above serverClusterFinder
     143              :     //       explicitly ignores that case).
     144              :     //       Validation of attribute existence is done after ACL, in `ValidateAttributeIsReadable` below
     145              :     //
     146              :     //       See https://github.com/project-chip/connectedhomeip/issues/37410
     147              : 
     148              :     // Execute the ACL Access Granting Algorithm before existence checks, assuming the required_privilege for the element is
     149              :     // View, to determine if the subject would have had at least some access against the concrete path. This is done so we don't
     150              :     // leak information if we do fail existence checks.
     151              : 
     152         4925 :     DataModel::AttributeFinder finder(dataModel);
     153         4925 :     std::optional<DataModel::AttributeEntry> entry = finder.Find(path);
     154              : 
     155         4925 :     if (auto access_status = ValidateReadAttributeACL(subjectDescriptor, path, Privilege::kView); access_status.has_value())
     156              :     {
     157            1 :         status = *access_status;
     158              :     }
     159         4924 :     else if (auto readable_status = ValidateAttributeIsReadable(dataModel, path, entry); readable_status.has_value())
     160              :     {
     161            1 :         status = *readable_status;
     162              :     }
     163              :     // Execute the ACL Access Granting Algorithm against the concrete path a second time, using the actual required_privilege.
     164              :     // entry->GetReadPrivilege() is guaranteed to have a value, since that condition is checked in the previous condition (inside
     165              :     // ValidateAttributeIsReadable()).
     166              :     // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
     167         9846 :     else if (auto required_privilege_status = ValidateReadAttributeACL(subjectDescriptor, path, entry->GetReadPrivilege().value());
     168         4923 :              required_privilege_status.has_value())
     169              :     {
     170            0 :         status = *required_privilege_status;
     171              :     }
     172         4923 :     else if (IsSupportedGlobalAttributeNotInMetadata(readRequest.path.mAttributeId))
     173              :     {
     174              :         // Global attributes are NOT directly handled by data model providers, instead
     175              :         // they are routed through metadata.
     176         1402 :         status = ReadGlobalAttributeFromMetadata(dataModel, readRequest.path, attributeValueEncoder);
     177              :     }
     178              :     else
     179              :     {
     180         3521 :         status = dataModel->ReadAttribute(readRequest, attributeValueEncoder);
     181              :     }
     182              : 
     183         4925 :     if (status.IsSuccess())
     184              :     {
     185              :         // TODO: this callback being only executed on success is awkward. The Write callback is always done
     186              :         //       for both read and write.
     187              :         //
     188              :         //       For now this preserves existing/previous code logic, however we should consider to ALWAYS
     189              :         //       call this.
     190         4527 :         DataModelCallbacks::GetInstance()->AttributeOperation(DataModelCallbacks::OperationType::Read,
     191              :                                                               DataModelCallbacks::OperationOrder::Post, path);
     192         4527 :         return status;
     193              :     }
     194              : 
     195              :     // Encoder state is relevant for errors in case they are retryable.
     196              :     //
     197              :     // Generally only out of space encoding errors would be retryable, however we save the state
     198              :     // for all errors in case this is information that is useful (retry or error position).
     199          398 :     if (encoderState != nullptr)
     200              :     {
     201          398 :         *encoderState = attributeValueEncoder.GetState();
     202              :     }
     203              : 
     204              : #if CHIP_CONFIG_DATA_MODEL_EXTRA_LOGGING
     205              :     // Out of space errors may be chunked data, reporting those cases would be very confusing
     206              :     // as they are not fully errors. Report only others (which presumably are not recoverable
     207              :     // and will be sent to the client as well).
     208          398 :     if (!status.IsOutOfSpaceEncodingResponse())
     209              :     {
     210            2 :         DataModel::ActionReturnStatus::StringStorage storage;
     211            2 :         ChipLogError(DataManagement, "Failed to read attribute: %s", status.c_str(storage));
     212              :     }
     213              : #endif
     214          398 :     return status;
     215         4925 : }
     216              : 
     217          109 : bool IsClusterDataVersionEqualTo(DataModel::Provider * dataModel, const ConcreteClusterPath & path, DataVersion dataVersion)
     218              : {
     219          109 :     DataModel::ServerClusterFinder serverClusterFinder(dataModel);
     220          109 :     auto info = serverClusterFinder.Find(path);
     221              : 
     222          109 :     return info.has_value() && (info->dataVersion == dataVersion);
     223          109 : }
     224              : 
     225              : /// Check if the given `err` is a known ACL error that can be translated into
     226              : /// a StatusIB (UnsupportedAccess/AccessRestricted)
     227              : ///
     228              : /// Returns true if the error could be translated and places the result into `outStatus`.
     229              : /// `path` is used for logging.
     230          113 : bool IsTranslatableAclError(const ConcreteEventPath & path, const CHIP_ERROR & err, StatusIB & outStatus)
     231              : {
     232          337 :     if ((err != CHIP_ERROR_ACCESS_DENIED) && (err != CHIP_ERROR_ACCESS_RESTRICTED_BY_ARL))
     233              :     {
     234          111 :         return false;
     235              :     }
     236              : 
     237            4 :     ChipLogDetail(InteractionModel, "Access to event (%u, " ChipLogFormatMEI ", " ChipLogFormatMEI ") denied by %s",
     238              :                   path.mEndpointId, ChipLogValueMEI(path.mClusterId), ChipLogValueMEI(path.mEventId),
     239              :                   err == CHIP_ERROR_ACCESS_DENIED ? "ACL" : "ARL");
     240              : 
     241            4 :     outStatus = err == CHIP_ERROR_ACCESS_DENIED ? StatusIB(Status::UnsupportedAccess) : StatusIB(Status::AccessRestricted);
     242            2 :     return true;
     243              : }
     244              : 
     245           58 : CHIP_ERROR CheckEventValidity(const ConcreteEventPath & path, const SubjectDescriptor & subjectDescriptor,
     246              :                               DataModel::Provider * provider, StatusIB & outStatus)
     247              : {
     248              :     // We validate ACL before Path, however this means we do not want the real ACL check
     249              :     // to be blocked by a `Invalid endpoint id` error when checking event info.
     250              :     // As a result, we check for VIEW privilege on the cluster first (most permissive)
     251              :     // and will do a 2nd check for the actual required privilege as a followup.
     252           58 :     RequestPath requestPath{
     253           58 :         .cluster     = path.mClusterId,
     254           58 :         .endpoint    = path.mEndpointId,
     255              :         .requestType = RequestType::kEventReadRequest,
     256           58 :         .entityId    = path.mEventId,
     257           58 :     };
     258           58 :     CHIP_ERROR err = GetAccessControl().Check(subjectDescriptor, requestPath, Access::Privilege::kView);
     259           58 :     if (IsTranslatableAclError(path, err, outStatus))
     260              :     {
     261            2 :         return CHIP_NO_ERROR;
     262              :     }
     263           56 :     ReturnErrorOnFailure(err);
     264              : 
     265              :     DataModel::EventEntry eventInfo;
     266           56 :     err = provider->EventInfo(path, eventInfo);
     267          112 :     if (err != CHIP_NO_ERROR)
     268              :     {
     269              :         // cannot get event data to validate. Event is not supported.
     270              :         // we still fall through into "ValidateClusterPath" to try to return a `better code`
     271              :         // (i.e. say invalid endpoint or cluster), however if path seems ok we will
     272              :         // return unsupported event as we failed to get event metadata.
     273            1 :         outStatus = StatusIB(DataModel::ValidateClusterPath(provider, path, Status::UnsupportedEvent));
     274            1 :         return CHIP_NO_ERROR;
     275              :     }
     276              : 
     277              :     // Although EventInfo() was successful, we still need to Validate Cluster Path since providers MAY return CHIP_NO_ERROR although
     278              :     // events are unknown.
     279           55 :     Status status = DataModel::ValidateClusterPath(provider, path, Status::Success);
     280           55 :     if (status != Status::Success)
     281              :     {
     282              :         // a valid status available: failure
     283            0 :         outStatus = StatusIB(status);
     284            0 :         return CHIP_NO_ERROR;
     285              :     }
     286              : 
     287              :     // Per spec, the required-privilege ACL check is performed only after path existence is validated
     288           55 :     err = GetAccessControl().Check(subjectDescriptor, requestPath, eventInfo.readPrivilege);
     289           55 :     if (IsTranslatableAclError(path, err, outStatus))
     290              :     {
     291            0 :         return CHIP_NO_ERROR;
     292              :     }
     293           55 :     ReturnErrorOnFailure(err);
     294              : 
     295              :     // set up the status as "OK" Since all above checks passed
     296           55 :     outStatus = StatusIB(Status::Success);
     297              : 
     298              :     // Status was set above = Success
     299           55 :     return CHIP_NO_ERROR;
     300              : }
     301              : 
     302              : } // namespace
     303              : 
     304           93 : Engine::Engine(InteractionModelEngine * apImEngine) : mpImEngine(apImEngine) {}
     305              : 
     306          468 : CHIP_ERROR Engine::Init(EventManagement * apEventManagement)
     307              : {
     308          468 :     VerifyOrReturnError(apEventManagement != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
     309          468 :     mNumReportsInFlight = 0;
     310          468 :     mCurReadHandlerIdx  = 0;
     311          468 :     mpEventManagement   = apEventManagement;
     312              : 
     313          468 :     return CHIP_NO_ERROR;
     314              : }
     315              : 
     316          342 : void Engine::Shutdown()
     317              : {
     318              :     // Flush out the event buffer synchronously
     319          342 :     ScheduleUrgentEventDeliverySync();
     320              : 
     321          342 :     mNumReportsInFlight = 0;
     322          342 :     mCurReadHandlerIdx  = 0;
     323          342 :     mGlobalDirtySet.ReleaseAll();
     324          342 : }
     325              : 
     326         4722 : bool Engine::IsClusterDataVersionMatch(const SingleLinkedListNode<DataVersionFilter> * aDataVersionFilterList,
     327              :                                        const ConcreteReadAttributePath & aPath)
     328              : {
     329         4722 :     bool existPathMatch       = false;
     330         4722 :     bool existVersionMismatch = false;
     331        43484 :     for (auto filter = aDataVersionFilterList; filter != nullptr; filter = filter->mpNext)
     332              :     {
     333        38762 :         if (aPath.mEndpointId == filter->mValue.mEndpointId && aPath.mClusterId == filter->mValue.mClusterId)
     334              :         {
     335          109 :             existPathMatch = true;
     336              : 
     337          109 :             if (!IsClusterDataVersionEqualTo(mpImEngine->GetDataModelProvider(),
     338          218 :                                              ConcreteClusterPath(filter->mValue.mEndpointId, filter->mValue.mClusterId),
     339          109 :                                              filter->mValue.mDataVersion.Value()))
     340              :             {
     341           79 :                 existVersionMismatch = true;
     342              :             }
     343              :         }
     344              :     }
     345         4722 :     return existPathMatch && !existVersionMismatch;
     346              : }
     347              : 
     348         2495 : static bool IsOutOfWriterSpaceError(CHIP_ERROR err)
     349              : {
     350         6574 :     return err == CHIP_ERROR_NO_MEMORY || err == CHIP_ERROR_BUFFER_TOO_SMALL;
     351              : }
     352              : 
     353         1980 : CHIP_ERROR Engine::BuildSingleReportDataAttributeReportIBs(ReportDataMessage::Builder & aReportDataBuilder,
     354              :                                                            ReadHandler * apReadHandler, bool * apHasMoreChunks,
     355              :                                                            bool * apHasEncodedData)
     356              : {
     357         1980 :     CHIP_ERROR err            = CHIP_NO_ERROR;
     358         1980 :     bool attributeDataWritten = false;
     359         1980 :     bool hasMoreChunks        = true;
     360         1980 :     TLV::TLVWriter backup;
     361         1980 :     const uint32_t kReservedSizeEndOfReportIBs = 1;
     362         1980 :     bool reservedEndOfReportIBs                = false;
     363              : 
     364         1980 :     aReportDataBuilder.Checkpoint(backup);
     365              : 
     366         1980 :     AttributeReportIBs::Builder & attributeReportIBs = aReportDataBuilder.CreateAttributeReportIBs();
     367         1980 :     size_t emptyReportDataLength                     = 0;
     368              : 
     369         1980 :     SuccessOrExit(err = aReportDataBuilder.GetError());
     370              : 
     371         1980 :     emptyReportDataLength = attributeReportIBs.GetWriter()->GetLengthWritten();
     372              :     //
     373              :     // Reserve enough space for closing out the Report IB list
     374              :     //
     375         1980 :     SuccessOrExit(err = attributeReportIBs.GetWriter()->ReserveBuffer(kReservedSizeEndOfReportIBs));
     376         1980 :     reservedEndOfReportIBs = true;
     377              : 
     378              :     {
     379              :         // TODO: Figure out how AttributePathExpandIterator should handle read
     380              :         // vs write paths.
     381         1980 :         ConcreteAttributePath readPath;
     382              : 
     383         1980 :         ChipLogDetail(DataManagement,
     384              :                       "Building Reports for ReadHandler with LastReportGeneration = 0x" ChipLogFormatX64
     385              :                       " DirtyGeneration = 0x" ChipLogFormatX64,
     386              :                       ChipLogValueX64(apReadHandler->mPreviousReportsBeginGeneration),
     387              :                       ChipLogValueX64(apReadHandler->mDirtyGeneration));
     388              : 
     389              :         // This ReadHandler is not generating reports, so we reset the iterator for a clean start.
     390         1980 :         if (!apReadHandler->IsReporting())
     391              :         {
     392         1168 :             apReadHandler->ResetPathIterator();
     393              :         }
     394              : 
     395              : #if CONFIG_BUILD_FOR_HOST_UNIT_TEST
     396         1980 :         uint32_t attributesRead = 0;
     397              : #endif
     398              : 
     399              :         // For each path included in the interested path of the read handler...
     400         1980 :         for (RollbackAttributePathExpandIterator iterator(mpImEngine->GetDataModelProvider(),
     401         1980 :                                                           apReadHandler->AttributeIterationPosition());
     402         6958 :              iterator.Next(readPath); iterator.MarkCompleted())
     403              :         {
     404         5394 :             if (!apReadHandler->IsPriming())
     405              :             {
     406          672 :                 bool concretePathDirty = false;
     407              :                 // TODO: Optimize this implementation by making the iterator only emit intersected paths.
     408          672 :                 mGlobalDirtySet.ForEachActiveObject([&](auto * dirtyPath) {
     409          815 :                     if (dirtyPath->IsAttributePathSupersetOf(readPath))
     410              :                     {
     411              :                         // We don't need to worry about paths that were already marked dirty before the last time this read handler
     412              :                         // started a report that it completed: those paths already got reported.
     413          252 :                         if (dirtyPath->mGeneration > apReadHandler->mPreviousReportsBeginGeneration)
     414              :                         {
     415          249 :                             concretePathDirty = true;
     416          249 :                             return Loop::Break;
     417              :                         }
     418              :                     }
     419          566 :                     return Loop::Continue;
     420              :                 });
     421              : 
     422          672 :                 if (!concretePathDirty)
     423              :                 {
     424              :                     // This attribute is not dirty, we just skip this one.
     425          423 :                     continue;
     426              :                 }
     427              :             }
     428              :             else
     429              :             {
     430         4722 :                 if (IsClusterDataVersionMatch(apReadHandler->GetDataVersionFilterList(), readPath))
     431              :                 {
     432           26 :                     continue;
     433              :                 }
     434              :             }
     435              : 
     436              : #if CONFIG_BUILD_FOR_HOST_UNIT_TEST
     437         4945 :             attributesRead++;
     438         4945 :             if (attributesRead > mMaxAttributesPerChunk)
     439              :             {
     440          416 :                 ExitNow(err = CHIP_ERROR_BUFFER_TOO_SMALL);
     441              :             }
     442              : #endif
     443              : 
     444              :             // If we are processing a read request, or the initial report of a subscription, just regard all paths as dirty
     445              :             // paths.
     446         4925 :             TLV::TLVWriter attributeBackup;
     447         4925 :             attributeReportIBs.Checkpoint(attributeBackup);
     448         4925 :             ConcreteReadAttributePath pathForRetrieval(readPath);
     449              :             // Load the saved state from previous encoding session for chunking of one single attribute (list chunking).
     450         4925 :             AttributeEncodeState encodeState = apReadHandler->GetAttributeEncodeState();
     451         4925 :             BitFlags<ReadFlags> flags;
     452         4925 :             flags.Set(ReadFlags::kFabricFiltered, apReadHandler->IsFabricFiltered());
     453         4925 :             flags.Set(ReadFlags::kAllowsLargePayload, apReadHandler->AllowsLargePayload());
     454              :             DataModel::ActionReturnStatus status =
     455         4925 :                 RetrieveClusterData(mpImEngine->GetDataModelProvider(), apReadHandler->GetSubjectDescriptor(), flags,
     456              :                                     attributeReportIBs, pathForRetrieval, &encodeState);
     457         4925 :             if (status.IsError())
     458              :             {
     459              :                 // Operation error set, since this will affect early return or override on status encoding
     460              :                 // it will also be used for error reporting below.
     461          398 :                 err = status.GetUnderlyingError();
     462              : 
     463              :                 // If error is not an "out of writer space" error, rollback and encode status.
     464              :                 // Otherwise, if partial data allowed, save the encode state.
     465              :                 // Otherwise roll back. If we have already encoded some chunks, we are done; otherwise encode status.
     466              : 
     467          398 :                 if (encodeState.AllowPartialData() && status.IsOutOfSpaceEncodingResponse())
     468              :                 {
     469          255 :                     ChipLogDetail(DataManagement,
     470              :                                   "List does not fit in packet, chunk between list items for clusterId: " ChipLogFormatMEI
     471              :                                   ", attributeId: " ChipLogFormatMEI,
     472              :                                   ChipLogValueMEI(pathForRetrieval.mClusterId), ChipLogValueMEI(pathForRetrieval.mAttributeId));
     473              :                     // Encoding is aborted but partial data is allowed, then we don't rollback and save the state for next chunk.
     474              :                     // The expectation is that RetrieveClusterData has already reset attributeReportIBs to a good state (rolled
     475              :                     // back any partially-written AttributeReportIB instances, reset its error status).  Since AllowPartialData()
     476              :                     // is true, we may not have encoded a complete attribute value, but we did, if we encoded anything, encode a
     477              :                     // set of complete AttributeReportIB instances that represent part of the attribute value.
     478          255 :                     apReadHandler->SetAttributeEncodeState(encodeState);
     479              :                 }
     480              :                 else
     481              :                 {
     482              :                     // We met a error during writing reports, one common case is we are running out of buffer, rollback the
     483              :                     // attributeReportIB to avoid any partial data.
     484          143 :                     attributeReportIBs.Rollback(attributeBackup);
     485          143 :                     apReadHandler->SetAttributeEncodeState(AttributeEncodeState());
     486              : 
     487          143 :                     if (!status.IsOutOfSpaceEncodingResponse())
     488              :                     {
     489            2 :                         ChipLogError(DataManagement,
     490              :                                      "Fail to retrieve data, roll back and encode status on clusterId: " ChipLogFormatMEI
     491              :                                      ", attributeId: " ChipLogFormatMEI "err = %" CHIP_ERROR_FORMAT,
     492              :                                      ChipLogValueMEI(pathForRetrieval.mClusterId), ChipLogValueMEI(pathForRetrieval.mAttributeId),
     493              :                                      err.Format());
     494              :                         // Try to encode our error as a status response.
     495            2 :                         err = attributeReportIBs.EncodeAttributeStatus(pathForRetrieval, StatusIB(status.GetStatusCode()));
     496            4 :                         if (err != CHIP_NO_ERROR)
     497              :                         {
     498              :                             // OK, just roll back again and give up; if we still ran out of space we
     499              :                             // will send this status response in the next chunk.
     500            0 :                             attributeReportIBs.Rollback(attributeBackup);
     501              :                         }
     502              :                     }
     503              :                     else
     504              :                     {
     505          141 :                         ChipLogDetail(DataManagement,
     506              :                                       "Next attribute value does not fit in packet, roll back on clusterId: " ChipLogFormatMEI
     507              :                                       ", attributeId: " ChipLogFormatMEI ", err = %" CHIP_ERROR_FORMAT,
     508              :                                       ChipLogValueMEI(pathForRetrieval.mClusterId), ChipLogValueMEI(pathForRetrieval.mAttributeId),
     509              :                                       err.Format());
     510              :                     }
     511              :                 }
     512              :             }
     513         4925 :             SuccessOrExit(err);
     514              :             // Successfully encoded the attribute, clear the internal state.
     515         4529 :             apReadHandler->SetAttributeEncodeState(AttributeEncodeState());
     516         1980 :         }
     517              : 
     518              :         // We just visited all paths interested by this read handler and did not abort in the middle of iteration, there are no more
     519              :         // chunks for this report.
     520         1564 :         hasMoreChunks = false;
     521              :     }
     522         1980 : exit:
     523         1980 :     if (attributeReportIBs.GetWriter()->GetLengthWritten() != emptyReportDataLength)
     524              :     {
     525              :         // We may encounter BUFFER_TOO_SMALL with nothing actually written for the case of list chunking, so we check if we have
     526              :         // actually
     527         1309 :         attributeDataWritten = true;
     528              :     }
     529              : 
     530         1980 :     if (apHasEncodedData != nullptr)
     531              :     {
     532         1980 :         *apHasEncodedData = attributeDataWritten;
     533              :     }
     534              :     //
     535              :     // Running out of space is an error that we're expected to handle - the incompletely written DataIB has already been rolled back
     536              :     // earlier to ensure only whole and complete DataIBs are present in the stream.
     537              :     //
     538              :     // We can safely clear out the error so that the rest of the machinery to close out the reports, etc. will function correctly.
     539              :     // These are are guaranteed to not fail since we've already reserved memory for the remaining 'close out' TLV operations in this
     540              :     // function and its callers.
     541              :     //
     542         1980 :     if (IsOutOfWriterSpaceError(err) && reservedEndOfReportIBs)
     543              :     {
     544          416 :         ChipLogDetail(DataManagement, "<RE:Run> We cannot put more chunks into this report. Enable chunking.");
     545          416 :         err = CHIP_NO_ERROR;
     546              :     }
     547              : 
     548              :     //
     549              :     // Only close out the report if we haven't hit an error yet so far.
     550              :     //
     551         3960 :     if (err == CHIP_NO_ERROR)
     552              :     {
     553         1980 :         TEMPORARY_RETURN_IGNORED attributeReportIBs.GetWriter()->UnreserveBuffer(kReservedSizeEndOfReportIBs);
     554              : 
     555         1980 :         err = attributeReportIBs.EndOfAttributeReportIBs();
     556              : 
     557              :         //
     558              :         // We reserved space for this earlier - consequently, the call to end the ReportIBs should
     559              :         // never fail, so assert if we do since that's a logic bug.
     560              :         //
     561         3960 :         VerifyOrDie(err == CHIP_NO_ERROR);
     562              :     }
     563              : 
     564              :     //
     565              :     // Rollback the the entire ReportIB array if we never wrote any attributes
     566              :     // AND never hit an error.
     567              :     //
     568         2651 :     if (!attributeDataWritten && err == CHIP_NO_ERROR)
     569              :     {
     570          671 :         aReportDataBuilder.Rollback(backup);
     571              :     }
     572              : 
     573              :     // hasMoreChunks + no data encoded is a flag that we have encountered some trouble when processing the attribute.
     574              :     // BuildAndSendSingleReportData will abort the read transaction if we encoded no attribute and no events but hasMoreChunks is
     575              :     // set.
     576         1980 :     if (apHasMoreChunks != nullptr)
     577              :     {
     578         1980 :         *apHasMoreChunks = hasMoreChunks;
     579              :     }
     580              : 
     581         1980 :     return err;
     582              : }
     583              : 
     584          864 : CHIP_ERROR Engine::CheckAccessDeniedEventPaths(TLV::TLVWriter & aWriter, bool & aHasEncodedData, ReadHandler * apReadHandler)
     585              : {
     586              :     using Protocols::InteractionModel::Status;
     587              : 
     588          864 :     CHIP_ERROR err = CHIP_NO_ERROR;
     589         1759 :     for (auto current = apReadHandler->mpEventPathList; current != nullptr;)
     590              :     {
     591          895 :         if (current->mValue.IsWildcardPath())
     592              :         {
     593          837 :             current = current->mpNext;
     594          837 :             continue;
     595              :         }
     596              : 
     597           58 :         ConcreteEventPath path(current->mValue.mEndpointId, current->mValue.mClusterId, current->mValue.mEventId);
     598              : 
     599           58 :         StatusIB statusIB;
     600              : 
     601           58 :         ReturnErrorOnFailure(
     602              :             CheckEventValidity(path, apReadHandler->GetSubjectDescriptor(), mpImEngine->GetDataModelProvider(), statusIB));
     603              : 
     604           58 :         if (statusIB.IsFailure())
     605              :         {
     606            3 :             TLV::TLVWriter checkpoint = aWriter;
     607            3 :             err                       = EventReportIB::ConstructEventStatusIB(aWriter, path, statusIB);
     608            6 :             if (err != CHIP_NO_ERROR)
     609              :             {
     610            0 :                 aWriter = checkpoint;
     611            0 :                 break;
     612              :             }
     613            3 :             aHasEncodedData = true;
     614              :         }
     615              : 
     616           58 :         current = current->mpNext;
     617              :     }
     618              : 
     619          864 :     return err;
     620              : }
     621              : 
     622         1980 : CHIP_ERROR Engine::BuildSingleReportDataEventReports(ReportDataMessage::Builder & aReportDataBuilder, ReadHandler * apReadHandler,
     623              :                                                      bool aBufferIsUsed, bool * apHasMoreChunks, bool * apHasEncodedData)
     624              : {
     625         1980 :     CHIP_ERROR err        = CHIP_NO_ERROR;
     626         1980 :     size_t eventCount     = 0;
     627         1980 :     bool hasEncodedStatus = false;
     628         1980 :     TLV::TLVWriter backup;
     629         1980 :     bool eventClean    = true;
     630         1980 :     auto & eventMin    = apReadHandler->GetEventMin();
     631         1980 :     bool hasMoreChunks = false;
     632              : 
     633         1980 :     aReportDataBuilder.Checkpoint(backup);
     634              : 
     635         1980 :     VerifyOrExit(apReadHandler->GetEventPathList() != nullptr, );
     636              : 
     637              :     // If the mpEventManagement is not valid or has not been initialized,
     638              :     // skip the rest of processing
     639          891 :     VerifyOrExit(mpEventManagement != nullptr && mpEventManagement->IsValid(),
     640              :                  ChipLogError(DataManagement, "EventManagement has not yet initialized"));
     641              : 
     642          888 :     eventClean = apReadHandler->CheckEventClean(*mpEventManagement);
     643              : 
     644              :     // proceed only if there are new events.
     645          888 :     if (eventClean)
     646              :     {
     647           24 :         ExitNow(); // Read clean, move along
     648              :     }
     649              : 
     650              :     {
     651              :         // Just like what we do in BuildSingleReportDataAttributeReportIBs(), we need to reserve one byte for end of container tag
     652              :         // when encoding events to ensure we can close the container successfully.
     653          864 :         const uint32_t kReservedSizeEndOfReportIBs = 1;
     654          864 :         EventReportIBs::Builder & eventReportIBs   = aReportDataBuilder.CreateEventReports();
     655          864 :         SuccessOrExit(err = aReportDataBuilder.GetError());
     656          864 :         VerifyOrExit(eventReportIBs.GetWriter() != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
     657          864 :         SuccessOrExit(err = eventReportIBs.GetWriter()->ReserveBuffer(kReservedSizeEndOfReportIBs));
     658              : 
     659          864 :         err = CheckAccessDeniedEventPaths(*(eventReportIBs.GetWriter()), hasEncodedStatus, apReadHandler);
     660          864 :         SuccessOrExit(err);
     661              : 
     662          864 :         err = mpEventManagement->FetchEventsSince(*(eventReportIBs.GetWriter()), apReadHandler->GetEventPathList(), eventMin,
     663          864 :                                                   eventCount, apReadHandler->GetSubjectDescriptor());
     664              : 
     665         3456 :         if ((err == CHIP_END_OF_TLV) || (err == CHIP_ERROR_TLV_UNDERRUN) || (err == CHIP_NO_ERROR))
     666              :         {
     667          349 :             err           = CHIP_NO_ERROR;
     668          349 :             hasMoreChunks = false;
     669              :         }
     670          515 :         else if (IsOutOfWriterSpaceError(err))
     671              :         {
     672              :             // when first cluster event is too big to fit in the packet, ignore that cluster event.
     673              :             // However, we may have encoded some attributes before, we don't skip it in that case.
     674          515 :             if (eventCount == 0)
     675              :             {
     676          206 :                 if (!aBufferIsUsed)
     677              :                 {
     678            0 :                     eventMin++;
     679              :                 }
     680          206 :                 ChipLogDetail(DataManagement, "<RE:Run> first cluster event is too big so that it fails to fit in the packet!");
     681          206 :                 err = CHIP_NO_ERROR;
     682              :             }
     683              :             else
     684              :             {
     685              :                 // `FetchEventsSince` has filled the available space
     686              :                 // within the allowed buffer before it fit all the
     687              :                 // available events.  This is an expected condition,
     688              :                 // so we do not propagate the error to higher levels;
     689              :                 // instead, we terminate the event processing for now
     690          309 :                 err = CHIP_NO_ERROR;
     691              :             }
     692          515 :             hasMoreChunks = true;
     693              :         }
     694              :         else
     695              :         {
     696              :             // All other errors are propagated to higher level.
     697              :             // Exiting here and returning an error will lead to
     698              :             // abandoning subscription.
     699            0 :             ExitNow();
     700              :         }
     701              : 
     702          864 :         SuccessOrExit(err = eventReportIBs.GetWriter()->UnreserveBuffer(kReservedSizeEndOfReportIBs));
     703          864 :         SuccessOrExit(err = eventReportIBs.EndOfEventReports());
     704              :     }
     705          864 :     ChipLogDetail(DataManagement, "Fetched %u events", static_cast<unsigned int>(eventCount));
     706              : 
     707            0 : exit:
     708         1980 :     if (apHasEncodedData != nullptr)
     709              :     {
     710         1980 :         *apHasEncodedData = hasEncodedStatus || (eventCount != 0);
     711              :     }
     712              : 
     713              :     // Maybe encoding the attributes has already used up all space.
     714         3960 :     if ((err == CHIP_NO_ERROR || IsOutOfWriterSpaceError(err)) && !(hasEncodedStatus || (eventCount != 0)))
     715              :     {
     716         1339 :         aReportDataBuilder.Rollback(backup);
     717         1339 :         err = CHIP_NO_ERROR;
     718              :     }
     719              : 
     720              :     // hasMoreChunks + no data encoded is a flag that we have encountered some trouble when processing the attribute.
     721              :     // BuildAndSendSingleReportData will abort the read transaction if we encoded no attribute and no events but hasMoreChunks is
     722              :     // set.
     723         1980 :     if (apHasMoreChunks != nullptr)
     724              :     {
     725         1980 :         *apHasMoreChunks = hasMoreChunks;
     726              :     }
     727         1980 :     return err;
     728              : }
     729              : 
     730         1980 : CHIP_ERROR Engine::BuildAndSendSingleReportData(ReadHandler * apReadHandler)
     731              : {
     732         1980 :     CHIP_ERROR err = CHIP_NO_ERROR;
     733         1980 :     System::PacketBufferTLVWriter reportDataWriter;
     734         1980 :     ReportDataMessage::Builder reportDataBuilder;
     735         1980 :     System::PacketBufferHandle bufHandle = nullptr;
     736         1980 :     uint16_t reservedSize                = 0;
     737         1980 :     bool hasMoreChunks                   = false;
     738         1980 :     bool needCloseReadHandler            = false;
     739         1980 :     size_t reportBufferMaxSize           = 0;
     740              : 
     741              :     // Reserved size for the MoreChunks boolean flag, which takes up 1 byte for the control tag and 1 byte for the context tag.
     742         1980 :     const uint32_t kReservedSizeForMoreChunksFlag = 1 + 1;
     743              : 
     744              :     // Reserved size for the uint8_t InteractionModelRevision flag, which takes up 1 byte for the control tag and 1 byte for the
     745              :     // context tag, 1 byte for value
     746         1980 :     const uint32_t kReservedSizeForIMRevision = 1 + 1 + 1;
     747              : 
     748              :     // Reserved size for the end of report message, which is an end-of-container (i.e 1 byte for the control tag).
     749         1980 :     const uint32_t kReservedSizeForEndOfReportMessage = 1;
     750              : 
     751              :     // Reserved size for an empty EventReportIBs, so we can at least check if there are any events need to be reported.
     752         1980 :     const uint32_t kReservedSizeForEventReportIBs = 3; // type, tag, end of container
     753              : 
     754         1980 :     VerifyOrExit(apReadHandler != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);
     755         1980 :     VerifyOrExit(apReadHandler->GetSession() != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
     756              : 
     757         1980 :     reportBufferMaxSize = apReadHandler->GetReportBufferMaxSize();
     758              : 
     759         1980 :     bufHandle = System::PacketBufferHandle::New(reportBufferMaxSize);
     760         1980 :     VerifyOrExit(!bufHandle.IsNull(), err = CHIP_ERROR_NO_MEMORY);
     761              : 
     762         1980 :     if (bufHandle->AvailableDataLength() > reportBufferMaxSize)
     763              :     {
     764            0 :         reservedSize = static_cast<uint16_t>(bufHandle->AvailableDataLength() - reportBufferMaxSize);
     765              :     }
     766              : 
     767         1980 :     reportDataWriter.Init(std::move(bufHandle));
     768              : 
     769              : #if CONFIG_BUILD_FOR_HOST_UNIT_TEST
     770         1980 :     SuccessOrExit(err = reportDataWriter.ReserveBuffer(mReservedSize));
     771              : #endif
     772              : 
     773              :     // Always limit the size of the generated packet to fit within the max size returned by the ReadHandler regardless
     774              :     // of the available buffer capacity.
     775              :     // Also, we need to reserve some extra space for the MIC field.
     776         1980 :     SuccessOrExit(
     777              :         err = reportDataWriter.ReserveBuffer(static_cast<uint32_t>(reservedSize + Crypto::CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES)));
     778              : 
     779              :     // Create a report data.
     780         1980 :     err = reportDataBuilder.Init(&reportDataWriter);
     781         1980 :     SuccessOrExit(err);
     782              : 
     783         1980 :     if (apReadHandler->IsType(ReadHandler::InteractionType::Subscribe))
     784              :     {
     785              : #if CHIP_CONFIG_ENABLE_ICD_SERVER
     786              :         // Notify the ICDManager that we are about to send a subscription report before we prepare the Report payload.
     787              :         // This allows the ICDManager to trigger any necessary updates and have the information in the report about to be sent.
     788              :         app::ICDNotifier::GetInstance().NotifySubscriptionReport();
     789              : #endif // CHIP_CONFIG_ENABLE_ICD_SERVER
     790              : 
     791          435 :         SubscriptionId subscriptionId = 0;
     792          435 :         apReadHandler->GetSubscriptionId(subscriptionId);
     793          435 :         reportDataBuilder.SubscriptionId(subscriptionId);
     794              :     }
     795              : 
     796         1980 :     SuccessOrExit(err = reportDataWriter.ReserveBuffer(kReservedSizeForMoreChunksFlag + kReservedSizeForIMRevision +
     797              :                                                        kReservedSizeForEndOfReportMessage + kReservedSizeForEventReportIBs));
     798              : 
     799              :     {
     800         1980 :         bool hasMoreChunksForAttributes = false;
     801         1980 :         bool hasMoreChunksForEvents     = false;
     802         1980 :         bool hasEncodedAttributes       = false;
     803         1980 :         bool hasEncodedEvents           = false;
     804              : 
     805         1980 :         err = BuildSingleReportDataAttributeReportIBs(reportDataBuilder, apReadHandler, &hasMoreChunksForAttributes,
     806              :                                                       &hasEncodedAttributes);
     807         2011 :         SuccessOrExit(err);
     808         1980 :         SuccessOrExit(err = reportDataWriter.UnreserveBuffer(kReservedSizeForEventReportIBs));
     809         1980 :         err = BuildSingleReportDataEventReports(reportDataBuilder, apReadHandler, hasEncodedAttributes, &hasMoreChunksForEvents,
     810              :                                                 &hasEncodedEvents);
     811         1980 :         SuccessOrExit(err);
     812              : 
     813         1980 :         hasMoreChunks = hasMoreChunksForAttributes || hasMoreChunksForEvents;
     814              : 
     815         1980 :         if (!hasEncodedAttributes && !hasEncodedEvents && hasMoreChunks)
     816              :         {
     817           31 :             ChipLogError(DataManagement,
     818              :                          "No data actually encoded but hasMoreChunks flag is set, close read handler! (attribute too big?)");
     819           31 :             err = apReadHandler->SendStatusReport(Protocols::InteractionModel::Status::ResourceExhausted);
     820           62 :             if (err == CHIP_NO_ERROR)
     821              :             {
     822           31 :                 needCloseReadHandler = true;
     823              :             }
     824           31 :             ExitNow();
     825              :         }
     826              :     }
     827              : 
     828         1949 :     SuccessOrExit(err = reportDataBuilder.GetError());
     829         1949 :     SuccessOrExit(err = reportDataWriter.UnreserveBuffer(kReservedSizeForMoreChunksFlag + kReservedSizeForIMRevision +
     830              :                                                          kReservedSizeForEndOfReportMessage));
     831         1949 :     if (hasMoreChunks)
     832              :     {
     833          866 :         reportDataBuilder.MoreChunkedMessages(true);
     834              :     }
     835         1083 :     else if (apReadHandler->IsType(ReadHandler::InteractionType::Read))
     836              :     {
     837          706 :         reportDataBuilder.SuppressResponse(true);
     838              :     }
     839              : 
     840              :     //
     841              :     // Since we've already reserved space for both the MoreChunked/SuppressResponse flags, as well as
     842              :     // the end-of-container flag for the end of the report, we should never hit an error closing out the message.
     843              :     //
     844         1949 :     SuccessOrDie(reportDataBuilder.EndOfReportDataMessage());
     845              : 
     846         1949 :     err = reportDataWriter.Finalize(&bufHandle);
     847         1949 :     SuccessOrExit(err);
     848              : 
     849         1949 :     ChipLogDetail(DataManagement, "<RE> Sending report (payload has %" PRIu32 " bytes)...", reportDataWriter.GetLengthWritten());
     850         1949 :     err = SendReport(apReadHandler, std::move(bufHandle), hasMoreChunks);
     851         1949 :     SuccessOrExitAction(
     852              :         err, ChipLogError(DataManagement, "<RE> Error sending out report data with %" CHIP_ERROR_FORMAT "!", err.Format()));
     853              : 
     854         1945 :     ChipLogDetail(DataManagement, "<RE> ReportsInFlight = %" PRIu32 " with readHandler %" PRIu32 ", RE has %s", mNumReportsInFlight,
     855              :                   mCurReadHandlerIdx, hasMoreChunks ? "more messages" : "no more messages");
     856              : 
     857            0 : exit:
     858         3960 :     if (err != CHIP_NO_ERROR || (apReadHandler->IsType(ReadHandler::InteractionType::Read) && !hasMoreChunks) ||
     859              :         needCloseReadHandler)
     860              :     {
     861              :         //
     862              :         // In the case of successful report generation and we're on the last chunk of a read, we don't expect
     863              :         // any further activity on this exchange. The EC layer will automatically close our EC, so shutdown the ReadHandler
     864              :         // gracefully.
     865              :         //
     866          739 :         apReadHandler->Close();
     867              :     }
     868              : 
     869         3960 :     return err;
     870         1980 : }
     871              : 
     872         1750 : void Engine::Run(System::Layer * aSystemLayer, void * apAppState)
     873              : {
     874         1750 :     Engine * const pEngine = reinterpret_cast<Engine *>(apAppState);
     875         1750 :     pEngine->mRunScheduled = false;
     876         1750 :     pEngine->Run();
     877         1750 : }
     878              : 
     879         2146 : CHIP_ERROR Engine::ScheduleRun()
     880              : {
     881         2146 :     if (IsRunScheduled())
     882              :     {
     883          396 :         return CHIP_NO_ERROR;
     884              :     }
     885              : 
     886         1750 :     Messaging::ExchangeManager * exchangeManager = mpImEngine->GetExchangeManager();
     887         1750 :     if (exchangeManager == nullptr)
     888              :     {
     889            0 :         return CHIP_ERROR_INCORRECT_STATE;
     890              :     }
     891         1750 :     SessionManager * sessionManager = exchangeManager->GetSessionManager();
     892         1750 :     if (sessionManager == nullptr)
     893              :     {
     894            0 :         return CHIP_ERROR_INCORRECT_STATE;
     895              :     }
     896         1750 :     System::Layer * systemLayer = sessionManager->SystemLayer();
     897         1750 :     if (systemLayer == nullptr)
     898              :     {
     899            0 :         return CHIP_ERROR_INCORRECT_STATE;
     900              :     }
     901         3500 :     ReturnErrorOnFailure(systemLayer->ScheduleWork(Run, this));
     902         1750 :     mRunScheduled = true;
     903         1750 :     return CHIP_NO_ERROR;
     904              : }
     905              : 
     906         2092 : void Engine::Run()
     907              : {
     908         2092 :     uint32_t numReadHandled = 0;
     909              : 
     910              :     // We may be deallocating read handlers as we go.  Track how many we had
     911              :     // initially, so we make sure to go through all of them.
     912         2092 :     size_t initialAllocated = mpImEngine->mReadHandlers.Allocated();
     913         4303 :     while ((mNumReportsInFlight < CHIP_IM_MAX_REPORTS_IN_FLIGHT) && (numReadHandled < initialAllocated))
     914              :     {
     915              :         ReadHandler * readHandler =
     916         2215 :             mpImEngine->ActiveHandlerAt(mCurReadHandlerIdx % (uint32_t) mpImEngine->mReadHandlers.Allocated());
     917         2215 :         VerifyOrDie(readHandler != nullptr);
     918              : 
     919         2215 :         if (readHandler->ShouldReportUnscheduled() || mpImEngine->GetReportScheduler()->IsReportableNow(readHandler))
     920              :         {
     921              : 
     922         1979 :             mRunningReadHandler = readHandler;
     923         1979 :             CHIP_ERROR err      = BuildAndSendSingleReportData(readHandler);
     924         1979 :             mRunningReadHandler = nullptr;
     925         3958 :             if (err != CHIP_NO_ERROR)
     926              :             {
     927            4 :                 return;
     928              :             }
     929              :         }
     930              : 
     931         2211 :         numReadHandled++;
     932              :         // If readHandler removed itself from our list, we also decremented
     933              :         // mCurReadHandlerIdx to account for that removal, so it's safe to
     934              :         // increment here.
     935         2211 :         mCurReadHandlerIdx++;
     936              :     }
     937              : 
     938              :     //
     939              :     // If our tracker has exceeded the bounds of the handler list, reset it back to 0.
     940              :     // This isn't strictly necessary, but does make it easier to debug issues in this code if they
     941              :     // do arise.
     942              :     //
     943         2088 :     if (mCurReadHandlerIdx >= mpImEngine->mReadHandlers.Allocated())
     944              :     {
     945         2031 :         mCurReadHandlerIdx = 0;
     946              :     }
     947              : 
     948         2088 :     bool allReadClean = true;
     949              : 
     950         2088 :     mpImEngine->mReadHandlers.ForEachActiveObject([&allReadClean](ReadHandler * handler) {
     951         2865 :         if (handler->IsDirty())
     952              :         {
     953          868 :             allReadClean = false;
     954          868 :             return Loop::Break;
     955              :         }
     956              : 
     957         1997 :         return Loop::Continue;
     958              :     });
     959              : 
     960         2088 :     if (allReadClean)
     961              :     {
     962         1220 :         ChipLogDetail(DataManagement, "All ReadHandler-s are clean, clear GlobalDirtySet");
     963              : 
     964         1220 :         mGlobalDirtySet.ReleaseAll();
     965              :     }
     966              : }
     967              : 
     968          276 : bool Engine::MergeOverlappedAttributePath(const AttributePathParams & aAttributePath)
     969              : {
     970          276 :     return Loop::Break == mGlobalDirtySet.ForEachActiveObject([&](auto * path) {
     971          214 :         if (path->IsAttributePathSupersetOf(aAttributePath))
     972              :         {
     973          112 :             path->mGeneration = GetDirtySetGeneration();
     974          112 :             return Loop::Break;
     975              :         }
     976          102 :         if (aAttributePath.IsAttributePathSupersetOf(*path))
     977              :         {
     978              :             // TODO: the wildcard input path may be superset of next paths in globalDirtySet, it is fine at this moment, since
     979              :             // when building report, it would use the first path of globalDirtySet to compare against interested paths read clients
     980              :             // want.
     981              :             // It is better to eliminate the duplicate wildcard paths in follow-up
     982            2 :             path->mGeneration  = GetDirtySetGeneration();
     983            2 :             path->mEndpointId  = aAttributePath.mEndpointId;
     984            2 :             path->mClusterId   = aAttributePath.mClusterId;
     985            2 :             path->mListIndex   = aAttributePath.mListIndex;
     986            2 :             path->mAttributeId = aAttributePath.mAttributeId;
     987            2 :             return Loop::Break;
     988              :         }
     989          100 :         return Loop::Continue;
     990          276 :     });
     991              : }
     992              : 
     993            8 : bool Engine::ClearTombPaths()
     994              : {
     995            8 :     bool pathReleased = false;
     996            8 :     mGlobalDirtySet.ForEachActiveObject([&](auto * path) {
     997           64 :         if (path->mGeneration == 0)
     998              :         {
     999           28 :             mGlobalDirtySet.ReleaseObject(path);
    1000           28 :             pathReleased = true;
    1001              :         }
    1002           64 :         return Loop::Continue;
    1003              :     });
    1004            8 :     return pathReleased;
    1005              : }
    1006              : 
    1007            5 : bool Engine::MergeDirtyPathsUnderSameCluster()
    1008              : {
    1009            5 :     mGlobalDirtySet.ForEachActiveObject([&](auto * outerPath) {
    1010           40 :         if (outerPath->HasWildcardClusterId() || outerPath->mGeneration == 0)
    1011              :         {
    1012           14 :             return Loop::Continue;
    1013              :         }
    1014           26 :         mGlobalDirtySet.ForEachActiveObject([&](auto * innerPath) {
    1015          208 :             if (innerPath == outerPath)
    1016              :             {
    1017           26 :                 return Loop::Continue;
    1018              :             }
    1019              :             // We don't support paths with a wildcard endpoint + a concrete cluster in global dirty set, so we do a simple == check
    1020              :             // here.
    1021          182 :             if (innerPath->mEndpointId != outerPath->mEndpointId || innerPath->mClusterId != outerPath->mClusterId)
    1022              :             {
    1023          168 :                 return Loop::Continue;
    1024              :             }
    1025           14 :             if (innerPath->mGeneration > outerPath->mGeneration)
    1026              :             {
    1027            0 :                 outerPath->mGeneration = innerPath->mGeneration;
    1028              :             }
    1029           14 :             outerPath->SetWildcardAttributeId();
    1030              : 
    1031              :             // The object pool does not allow us to release objects in a nested iteration, mark the path as a tomb by setting its
    1032              :             // generation to 0 and then clear it later.
    1033           14 :             innerPath->mGeneration = 0;
    1034           14 :             return Loop::Continue;
    1035              :         });
    1036           26 :         return Loop::Continue;
    1037              :     });
    1038              : 
    1039            5 :     return ClearTombPaths();
    1040              : }
    1041              : 
    1042            3 : bool Engine::MergeDirtyPathsUnderSameEndpoint()
    1043              : {
    1044            3 :     mGlobalDirtySet.ForEachActiveObject([&](auto * outerPath) {
    1045           24 :         if (outerPath->HasWildcardEndpointId() || outerPath->mGeneration == 0)
    1046              :         {
    1047           14 :             return Loop::Continue;
    1048              :         }
    1049           10 :         mGlobalDirtySet.ForEachActiveObject([&](auto * innerPath) {
    1050           80 :             if (innerPath == outerPath)
    1051              :             {
    1052           10 :                 return Loop::Continue;
    1053              :             }
    1054           70 :             if (innerPath->mEndpointId != outerPath->mEndpointId)
    1055              :             {
    1056           56 :                 return Loop::Continue;
    1057              :             }
    1058           14 :             if (innerPath->mGeneration > outerPath->mGeneration)
    1059              :             {
    1060            0 :                 outerPath->mGeneration = innerPath->mGeneration;
    1061              :             }
    1062           14 :             outerPath->SetWildcardClusterId();
    1063           14 :             outerPath->SetWildcardAttributeId();
    1064              : 
    1065              :             // The object pool does not allow us to release objects in a nested iteration, mark the path as a tomb by setting its
    1066              :             // generation to 0 and then clear it later.
    1067           14 :             innerPath->mGeneration = 0;
    1068           14 :             return Loop::Continue;
    1069              :         });
    1070           10 :         return Loop::Continue;
    1071              :     });
    1072            3 :     return ClearTombPaths();
    1073              : }
    1074              : 
    1075          189 : CHIP_ERROR Engine::InsertPathIntoDirtySet(const AttributePathParams & aAttributePath)
    1076              : {
    1077          189 :     VerifyOrReturnError(!MergeOverlappedAttributePath(aAttributePath), CHIP_NO_ERROR);
    1078              : 
    1079           82 :     if (mGlobalDirtySet.Exhausted() && !MergeDirtyPathsUnderSameCluster() && !MergeDirtyPathsUnderSameEndpoint())
    1080              :     {
    1081            1 :         ChipLogDetail(DataManagement, "Global dirty set pool exhausted, merge all paths.");
    1082            1 :         mGlobalDirtySet.ReleaseAll();
    1083            1 :         auto object         = mGlobalDirtySet.CreateObject();
    1084            1 :         object->mGeneration = GetDirtySetGeneration();
    1085              :     }
    1086              : 
    1087           82 :     VerifyOrReturnError(!MergeOverlappedAttributePath(aAttributePath), CHIP_NO_ERROR);
    1088           79 :     ChipLogDetail(DataManagement, "Cannot merge the new path into any existing path, create one.");
    1089              : 
    1090           79 :     auto object = mGlobalDirtySet.CreateObject();
    1091           79 :     if (object == nullptr)
    1092              :     {
    1093              :         // This should not happen, this path should be merged into the wildcard endpoint at least.
    1094            0 :         ChipLogError(DataManagement, "mGlobalDirtySet pool full, cannot handle more entries!");
    1095            0 :         return CHIP_ERROR_NO_MEMORY;
    1096              :     }
    1097           79 :     *object             = aAttributePath;
    1098           79 :     object->mGeneration = GetDirtySetGeneration();
    1099              : 
    1100           79 :     return CHIP_NO_ERROR;
    1101              : }
    1102              : 
    1103         5412 : CHIP_ERROR Engine::SetDirty(const AttributePathParams & aAttributePath)
    1104              : {
    1105         5412 :     BumpDirtySetGeneration();
    1106              : 
    1107         5412 :     bool intersectsInterestPath     = false;
    1108         5412 :     DataModel::Provider * dataModel = mpImEngine->GetDataModelProvider();
    1109         5412 :     mpImEngine->mReadHandlers.ForEachActiveObject([&dataModel, &aAttributePath, &intersectsInterestPath](ReadHandler * handler) {
    1110              :         // We call AttributePathIsDirty for both read interactions and subscribe interactions, since we may send inconsistent
    1111              :         // attribute data between two chunks. AttributePathIsDirty will not schedule a new run for read handlers which are
    1112              :         // waiting for a response to the last message chunk for read interactions.
    1113          477 :         if (handler->CanStartReporting() || handler->IsAwaitingReportResponse())
    1114              :         {
    1115          934 :             for (auto object = handler->GetAttributePathList(); object != nullptr; object = object->mpNext)
    1116              :             {
    1117          802 :                 if (object->mValue.Intersects(aAttributePath))
    1118              :                 {
    1119          345 :                     handler->AttributePathIsDirty(dataModel, aAttributePath);
    1120          345 :                     intersectsInterestPath = true;
    1121          345 :                     break;
    1122              :                 }
    1123              :             }
    1124              :         }
    1125              : 
    1126          477 :         return Loop::Continue;
    1127              :     });
    1128              : 
    1129         5412 :     if (!intersectsInterestPath)
    1130              :     {
    1131         5228 :         return CHIP_NO_ERROR;
    1132              :     }
    1133          184 :     ReturnErrorOnFailure(InsertPathIntoDirtySet(aAttributePath));
    1134              : 
    1135          184 :     return CHIP_NO_ERROR;
    1136              : }
    1137              : 
    1138         1949 : CHIP_ERROR Engine::SendReport(ReadHandler * apReadHandler, System::PacketBufferHandle && aPayload, bool aHasMoreChunks)
    1139              : {
    1140         1949 :     CHIP_ERROR err = CHIP_NO_ERROR;
    1141              : 
    1142              :     // We can only have 1 report in flight for any given read - increment and break out.
    1143         1949 :     mNumReportsInFlight++;
    1144         1949 :     err = apReadHandler->SendReportData(std::move(aPayload), aHasMoreChunks);
    1145         3898 :     if (err != CHIP_NO_ERROR)
    1146              :     {
    1147            4 :         --mNumReportsInFlight;
    1148              :     }
    1149         1949 :     return err;
    1150              : }
    1151              : 
    1152         1945 : void Engine::OnReportConfirm()
    1153              : {
    1154         1945 :     VerifyOrDie(mNumReportsInFlight > 0);
    1155              : 
    1156         1945 :     if (mNumReportsInFlight == CHIP_IM_MAX_REPORTS_IN_FLIGHT)
    1157              :     {
    1158              :         // We could have other things waiting to go now that this report is no
    1159              :         // longer in flight.
    1160           61 :         TEMPORARY_RETURN_IGNORED ScheduleRun();
    1161              :     }
    1162         1945 :     mNumReportsInFlight--;
    1163         1945 :     ChipLogDetail(DataManagement, "<RE> OnReportConfirm: NumReports = %" PRIu32, mNumReportsInFlight);
    1164         1945 : }
    1165              : 
    1166           20 : void Engine::GetMinEventLogPosition(uint32_t & aMinLogPosition)
    1167              : {
    1168           20 :     mpImEngine->mReadHandlers.ForEachActiveObject([&aMinLogPosition](ReadHandler * handler) {
    1169           20 :         if (handler->IsType(ReadHandler::InteractionType::Read))
    1170              :         {
    1171            0 :             return Loop::Continue;
    1172              :         }
    1173              : 
    1174           20 :         uint32_t initialWrittenEventsBytes = handler->GetLastWrittenEventsBytes();
    1175           20 :         if (initialWrittenEventsBytes < aMinLogPosition)
    1176              :         {
    1177           20 :             aMinLogPosition = initialWrittenEventsBytes;
    1178              :         }
    1179              : 
    1180           20 :         return Loop::Continue;
    1181              :     });
    1182           20 : }
    1183              : 
    1184           20 : CHIP_ERROR Engine::ScheduleBufferPressureEventDelivery(uint32_t aBytesWritten)
    1185              : {
    1186           20 :     uint32_t minEventLogPosition = aBytesWritten;
    1187           20 :     GetMinEventLogPosition(minEventLogPosition);
    1188           20 :     if (aBytesWritten - minEventLogPosition > CHIP_CONFIG_EVENT_LOGGING_BYTE_THRESHOLD)
    1189              :     {
    1190            0 :         ChipLogDetail(DataManagement, "<RE> Buffer overfilled CHIP_CONFIG_EVENT_LOGGING_BYTE_THRESHOLD %d, schedule engine run",
    1191              :                       CHIP_CONFIG_EVENT_LOGGING_BYTE_THRESHOLD);
    1192            0 :         return ScheduleRun();
    1193              :     }
    1194           20 :     return CHIP_NO_ERROR;
    1195              : }
    1196              : 
    1197          665 : CHIP_ERROR Engine::NewEventGenerated(ConcreteEventPath & aPath, uint32_t aBytesConsumed)
    1198              : {
    1199              :     // If we literally have no read handlers right now that care about any events,
    1200              :     // we don't need to call schedule run for event.
    1201              :     // If schedule run is called, actually we would not delivery events as well.
    1202              :     // Just wanna save one schedule run here
    1203          665 :     if (mpImEngine->mEventPathPool.Allocated() == 0)
    1204              :     {
    1205          633 :         return CHIP_NO_ERROR;
    1206              :     }
    1207              : 
    1208           32 :     bool isUrgentEvent = false;
    1209           32 :     mpImEngine->mReadHandlers.ForEachActiveObject([&aPath, &isUrgentEvent](ReadHandler * handler) {
    1210           40 :         if (handler->IsType(ReadHandler::InteractionType::Read))
    1211              :         {
    1212            0 :             return Loop::Continue;
    1213              :         }
    1214              : 
    1215          104 :         for (auto * interestedPath = handler->GetEventPathList(); interestedPath != nullptr;
    1216           64 :              interestedPath        = interestedPath->mpNext)
    1217              :         {
    1218           76 :             if (interestedPath->mValue.IsEventPathSupersetOf(aPath) && interestedPath->mValue.mIsUrgentEvent)
    1219              :             {
    1220           12 :                 isUrgentEvent = true;
    1221           12 :                 handler->ForceDirtyState();
    1222           12 :                 break;
    1223              :             }
    1224              :         }
    1225              : 
    1226           40 :         return Loop::Continue;
    1227              :     });
    1228              : 
    1229           32 :     if (isUrgentEvent)
    1230              :     {
    1231           12 :         ChipLogDetail(DataManagement, "Urgent event will be sent once reporting is not blocked by the min interval");
    1232           12 :         return CHIP_NO_ERROR;
    1233              :     }
    1234              : 
    1235           20 :     return ScheduleBufferPressureEventDelivery(aBytesConsumed);
    1236              : }
    1237              : 
    1238          342 : void Engine::ScheduleUrgentEventDeliverySync(Optional<FabricIndex> fabricIndex)
    1239              : {
    1240          342 :     mpImEngine->mReadHandlers.ForEachActiveObject([fabricIndex](ReadHandler * handler) {
    1241            0 :         if (handler->IsType(ReadHandler::InteractionType::Read))
    1242              :         {
    1243            0 :             return Loop::Continue;
    1244              :         }
    1245              : 
    1246            0 :         if (fabricIndex.HasValue() && fabricIndex.Value() != handler->GetAccessingFabricIndex())
    1247              :         {
    1248            0 :             return Loop::Continue;
    1249              :         }
    1250              : 
    1251            0 :         handler->ForceDirtyState();
    1252              : 
    1253            0 :         return Loop::Continue;
    1254              :     });
    1255              : 
    1256          342 :     Run();
    1257          342 : }
    1258              : 
    1259         5232 : void Engine::MarkDirty(const AttributePathParams & path)
    1260              : {
    1261         5232 :     CHIP_ERROR err = SetDirty(path);
    1262        10464 :     if (err != CHIP_NO_ERROR)
    1263              :     {
    1264            0 :         ChipLogError(DataManagement, "Failed to set path dirty: %" CHIP_ERROR_FORMAT, err.Format());
    1265              :     }
    1266         5232 : }
    1267              : 
    1268              : } // namespace reporting
    1269              : } // namespace app
    1270              : } // namespace chip
        

Generated by: LCOV version 2.0-1