Matter SDK Coverage Report
Current view: top level - controller - CurrentFabricRemover.cpp (source / functions) Coverage Total Hit
Test: SHA:b879ecb8e99e175eea0a293a888bda853da2b19c Lines: 0.0 % 85 0
Test Date: 2025-01-17 19:00:11 Functions: 0.0 % 13 0

            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 <controller/CurrentFabricRemover.h>
      20              : 
      21              : #include <app-common/zap-generated/cluster-objects.h>
      22              : 
      23              : using namespace chip::app::Clusters;
      24              : 
      25              : namespace chip {
      26              : namespace Controller {
      27              : 
      28            0 : CHIP_ERROR CurrentFabricRemover::RemoveCurrentFabric(NodeId remoteNodeId, Callback::Callback<OnCurrentFabricRemove> * callback)
      29              : {
      30            0 :     mRemoteNodeId                = remoteNodeId;
      31            0 :     mCurrentFabricRemoveCallback = callback;
      32            0 :     mNextStep                    = Step::kReadCurrentFabricIndex;
      33              : 
      34            0 :     return mController->GetConnectedDevice(remoteNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
      35              : }
      36              : 
      37            0 : CHIP_ERROR CurrentFabricRemover::ReadCurrentFabricIndex(Messaging::ExchangeManager & exchangeMgr,
      38              :                                                         const SessionHandle & sessionHandle)
      39              : {
      40              :     using TypeInfo = OperationalCredentials::Attributes::CurrentFabricIndex::TypeInfo;
      41            0 :     ClusterBase cluster(exchangeMgr, sessionHandle, kRootEndpointId);
      42              : 
      43            0 :     return cluster.ReadAttribute<TypeInfo>(this, OnSuccessReadCurrentFabricIndex, OnReadAttributeFailure);
      44            0 : }
      45              : 
      46            0 : CHIP_ERROR CurrentFabricRemover::SendRemoveFabricIndex(Messaging::ExchangeManager & exchangeMgr,
      47              :                                                        const SessionHandle & sessionHandle)
      48              : {
      49            0 :     if (mFabricIndex == kUndefinedFabricIndex)
      50              :     {
      51            0 :         return CHIP_ERROR_INVALID_FABRIC_INDEX;
      52              :     }
      53              : 
      54            0 :     OperationalCredentials::Commands::RemoveFabric::Type request;
      55            0 :     request.fabricIndex = mFabricIndex;
      56              : 
      57            0 :     ClusterBase cluster(exchangeMgr, sessionHandle, 0);
      58              : 
      59            0 :     return cluster.InvokeCommand(request, this, OnSuccessRemoveFabric, OnCommandFailure);
      60            0 : }
      61              : 
      62            0 : void CurrentFabricRemover::OnDeviceConnectedFn(void * context, Messaging::ExchangeManager & exchangeMgr,
      63              :                                                const SessionHandle & sessionHandle)
      64              : {
      65            0 :     CHIP_ERROR err = CHIP_NO_ERROR;
      66            0 :     auto * self    = static_cast<CurrentFabricRemover *>(context);
      67            0 :     VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Device connected callback with null context. Ignoring"));
      68              : 
      69            0 :     switch (self->mNextStep)
      70              :     {
      71            0 :     case Step::kReadCurrentFabricIndex: {
      72            0 :         err = self->ReadCurrentFabricIndex(exchangeMgr, sessionHandle);
      73            0 :         break;
      74              :     }
      75            0 :     case Step::kSendRemoveFabric: {
      76            0 :         err = self->SendRemoveFabricIndex(exchangeMgr, sessionHandle);
      77            0 :         break;
      78              :     }
      79            0 :     default:
      80            0 :         err = CHIP_ERROR_INCORRECT_STATE;
      81            0 :         break;
      82              :     }
      83              : 
      84            0 :     if (err != CHIP_NO_ERROR)
      85              :     {
      86            0 :         ChipLogError(Controller, "Current Fabric Remover failure : %" CHIP_ERROR_FORMAT, err.Format());
      87            0 :         FinishRemoveCurrentFabric(context, err);
      88              :     }
      89              : }
      90              : 
      91            0 : void CurrentFabricRemover::OnDeviceConnectionFailureFn(void * context, const ScopedNodeId & peerId, CHIP_ERROR err)
      92              : {
      93            0 :     ChipLogProgress(Controller, "OnDeviceConnectionFailureFn: %" CHIP_ERROR_FORMAT, err.Format());
      94              : 
      95            0 :     auto * self = static_cast<CurrentFabricRemover *>(context);
      96            0 :     VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Device connected failure callback with null context. Ignoring"));
      97              : 
      98            0 :     FinishRemoveCurrentFabric(context, err);
      99              : }
     100              : 
     101            0 : void CurrentFabricRemover::OnSuccessReadCurrentFabricIndex(void * context, FabricIndex fabricIndex)
     102              : {
     103            0 :     auto * self = static_cast<CurrentFabricRemover *>(context);
     104            0 :     VerifyOrReturn(self != nullptr,
     105              :                    ChipLogProgress(Controller, "Success Read Current Fabric index callback with null context. Ignoring"));
     106            0 :     self->mFabricIndex = fabricIndex;
     107            0 :     self->mNextStep    = Step::kSendRemoveFabric;
     108            0 :     CHIP_ERROR err     = self->mController->GetConnectedDevice(self->mRemoteNodeId, &self->mOnDeviceConnectedCallback,
     109              :                                                                &self->mOnDeviceConnectionFailureCallback);
     110            0 :     if (err != CHIP_NO_ERROR)
     111              :     {
     112            0 :         FinishRemoveCurrentFabric(context, err);
     113              :     }
     114              : }
     115              : 
     116            0 : void CurrentFabricRemover::OnReadAttributeFailure(void * context, CHIP_ERROR err)
     117              : {
     118            0 :     ChipLogProgress(Controller, "OnReadAttributeFailure %" CHIP_ERROR_FORMAT, err.Format());
     119              : 
     120            0 :     auto * self = static_cast<CurrentFabricRemover *>(context);
     121            0 :     VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Read Attribute failure callback with null context. Ignoring"));
     122              : 
     123            0 :     FinishRemoveCurrentFabric(context, err);
     124              : }
     125              : 
     126            0 : void CurrentFabricRemover::OnSuccessRemoveFabric(void * context,
     127              :                                                  const OperationalCredentials::Commands::NOCResponse::DecodableType & data)
     128              : {
     129            0 :     auto * self = static_cast<CurrentFabricRemover *>(context);
     130            0 :     VerifyOrReturn(self != nullptr,
     131              :                    ChipLogProgress(Controller, "Success Remove Fabric command callback with null context. Ignoring"));
     132              : 
     133            0 :     FinishRemoveCurrentFabric(context, CHIP_NO_ERROR);
     134              : }
     135              : 
     136            0 : void CurrentFabricRemover::OnCommandFailure(void * context, CHIP_ERROR err)
     137              : {
     138            0 :     ChipLogProgress(Controller, "OnCommandFailure %" CHIP_ERROR_FORMAT, err.Format());
     139              : 
     140            0 :     auto * self = static_cast<CurrentFabricRemover *>(context);
     141            0 :     VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Send command failure callback with null context. Ignoring"));
     142              : 
     143            0 :     FinishRemoveCurrentFabric(context, err);
     144              : }
     145              : 
     146            0 : void CurrentFabricRemover::FinishRemoveCurrentFabric(void * context, CHIP_ERROR err)
     147              : {
     148            0 :     if (err == CHIP_NO_ERROR)
     149              :     {
     150            0 :         ChipLogProgress(Controller, "Remove Current Fabric succeeded.");
     151              :     }
     152              :     else
     153              :     {
     154            0 :         ChipLogError(Controller, "Remove Current Fabric Failed : %" CHIP_ERROR_FORMAT, err.Format());
     155              :     }
     156            0 :     auto * self     = static_cast<CurrentFabricRemover *>(context);
     157            0 :     self->mNextStep = Step::kAcceptRemoveFabricStart;
     158            0 :     if (self->mCurrentFabricRemoveCallback != nullptr)
     159              :     {
     160            0 :         self->mCurrentFabricRemoveCallback->mCall(self->mCurrentFabricRemoveCallback->mContext, self->mRemoteNodeId, err);
     161              :     }
     162            0 : }
     163              : 
     164            0 : AutoCurrentFabricRemover::AutoCurrentFabricRemover(DeviceController * controller) :
     165            0 :     CurrentFabricRemover(controller), mOnRemoveCurrentFabricCallback(OnRemoveCurrentFabric, this)
     166            0 : {}
     167              : 
     168            0 : CHIP_ERROR AutoCurrentFabricRemover::RemoveCurrentFabric(DeviceController * controller, NodeId remoteNodeId)
     169              : {
     170              :     // Not using Platform::New because we want to keep our constructor private.
     171            0 :     auto * remover = new (std::nothrow) AutoCurrentFabricRemover(controller);
     172            0 :     if (remover == nullptr)
     173              :     {
     174            0 :         return CHIP_ERROR_NO_MEMORY;
     175              :     }
     176              : 
     177            0 :     CHIP_ERROR err = remover->CurrentFabricRemover::RemoveCurrentFabric(remoteNodeId, &remover->mOnRemoveCurrentFabricCallback);
     178            0 :     if (err != CHIP_NO_ERROR)
     179              :     {
     180            0 :         delete remover;
     181              :     }
     182              :     // Else will clean up when the callback is called.
     183            0 :     return err;
     184              : }
     185              : 
     186            0 : void AutoCurrentFabricRemover::OnRemoveCurrentFabric(void * context, NodeId remoteNodeId, CHIP_ERROR status)
     187              : {
     188            0 :     auto * self = static_cast<AutoCurrentFabricRemover *>(context);
     189            0 :     delete self;
     190            0 : }
     191              : } // namespace Controller
     192              : } // namespace chip
        

Generated by: LCOV version 2.0-1