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 : ChipLogError(Controller, "Remove Current Fabric Result : %" CHIP_ERROR_FORMAT, err.Format());
149 0 : auto * self = static_cast<CurrentFabricRemover *>(context);
150 0 : self->mNextStep = Step::kAcceptRemoveFabricStart;
151 0 : if (self->mCurrentFabricRemoveCallback != nullptr)
152 : {
153 0 : self->mCurrentFabricRemoveCallback->mCall(self->mCurrentFabricRemoveCallback->mContext, self->mRemoteNodeId, err);
154 : }
155 0 : }
156 :
157 0 : AutoCurrentFabricRemover::AutoCurrentFabricRemover(DeviceController * controller) :
158 0 : CurrentFabricRemover(controller), mOnRemoveCurrentFabricCallback(OnRemoveCurrentFabric, this)
159 0 : {}
160 :
161 0 : CHIP_ERROR AutoCurrentFabricRemover::RemoveCurrentFabric(DeviceController * controller, NodeId remoteNodeId)
162 : {
163 : // Not using Platform::New because we want to keep our constructor private.
164 0 : auto * remover = new (std::nothrow) AutoCurrentFabricRemover(controller);
165 0 : if (remover == nullptr)
166 : {
167 0 : return CHIP_ERROR_NO_MEMORY;
168 : }
169 :
170 0 : CHIP_ERROR err = remover->CurrentFabricRemover::RemoveCurrentFabric(remoteNodeId, &remover->mOnRemoveCurrentFabricCallback);
171 0 : if (err != CHIP_NO_ERROR)
172 : {
173 0 : delete remover;
174 : }
175 : // Else will clean up when the callback is called.
176 0 : return err;
177 : }
178 :
179 0 : void AutoCurrentFabricRemover::OnRemoveCurrentFabric(void * context, NodeId remoteNodeId, CHIP_ERROR status)
180 : {
181 0 : auto * self = static_cast<AutoCurrentFabricRemover *>(context);
182 0 : delete self;
183 0 : }
184 : } // namespace Controller
185 : } // namespace chip
|