blob: 63921f9066c2a0c625900b9f4203d9f41b22d702 [file] [log] [blame]
joonhaengHeo89df4ac2023-01-07 05:34:04 +09001/*
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#pragma once
20
21#include <app/OperationalSessionSetup.h>
22#include <controller/CHIPDeviceController.h>
23#include <lib/core/CHIPCallback.h>
24
25namespace chip {
26namespace Controller {
27
28typedef void (*OnCurrentFabricRemove)(void * context, NodeId remoteNodeId, CHIP_ERROR status);
29
30/**
31 * A helper class to remove fabric given some parameters.
32 */
33class CurrentFabricRemover
34{
35public:
36 CurrentFabricRemover(DeviceController * controller) :
37 mController(controller), mOnDeviceConnectedCallback(&OnDeviceConnectedFn, this),
38 mOnDeviceConnectionFailureCallback(&OnDeviceConnectionFailureFn, this)
39 {}
40
41 enum class Step : uint8_t
42 {
43 // Ready to start removing a fabric.
44 kAcceptRemoveFabricStart = 0,
45 // Need to get Current Fabric Index.
46 kReadCurrentFabricIndex,
47 // Need to send Remove Fabric Command.
48 kSendRemoveFabric,
49 };
50
51 /*
52 * @brief
53 * Try to look up the device attached to our controller with the given
54 * remote node id and ask it to remove Fabric.
55 * If function returns an error, callback will never be be executed. Otherwise, callback will always be executed.
56 *
57 * @param[in] remoteNodeId The remote device Id
58 * @param[in] callback The callback to call once the remote fabric is completed or not.
59 */
60 CHIP_ERROR RemoveCurrentFabric(NodeId remoteNodeId, Callback::Callback<OnCurrentFabricRemove> * callback);
61
62private:
63 DeviceController * mController;
64
65 chip::Callback::Callback<OnDeviceConnected> mOnDeviceConnectedCallback;
66 chip::Callback::Callback<OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
67 chip::Callback::Callback<OnCurrentFabricRemove> * mCurrentFabricRemoveCallback;
68
69 NodeId mRemoteNodeId;
70 FabricIndex mFabricIndex = kUndefinedFabricIndex;
71 Step mNextStep = Step::kAcceptRemoveFabricStart;
72
Marcos Bcae63192023-01-13 14:53:14 -070073 CHIP_ERROR ReadCurrentFabricIndex(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle);
74 CHIP_ERROR SendRemoveFabricIndex(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle);
joonhaengHeo89df4ac2023-01-07 05:34:04 +090075
Marcos Bcae63192023-01-13 14:53:14 -070076 static void OnDeviceConnectedFn(void * context, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle);
joonhaengHeo89df4ac2023-01-07 05:34:04 +090077 static void OnDeviceConnectionFailureFn(void * context, const ScopedNodeId & peerId, CHIP_ERROR error);
78
79 static void OnSuccessReadCurrentFabricIndex(void * context, FabricIndex fabricIndex);
80 static void OnReadAttributeFailure(void * context, CHIP_ERROR error);
81
82 static void
83 OnSuccessRemoveFabric(void * context,
84 const chip::app::Clusters::OperationalCredentials::Commands::NOCResponse::DecodableType & data);
85 static void OnCommandFailure(void * context, CHIP_ERROR error);
86
87 static void FinishRemoveCurrentFabric(void * context, CHIP_ERROR err);
88};
89
90/**
91 * A helper class that can be used by consumers that don't care about the callback from the
92 * remove fabric process and just want automatic cleanup of the CurrentFabricRemover when done
93 * with it.
94 */
95class AutoCurrentFabricRemover : private CurrentFabricRemover
96{
97public:
98 // Takes the same arguments as CurrentFabricRemover::RemoveCurrentFabric except without the callback.
99 static CHIP_ERROR RemoveCurrentFabric(DeviceController * controller, NodeId remoteNodeId);
100
101private:
102 AutoCurrentFabricRemover(DeviceController * controller);
103 static void OnRemoveCurrentFabric(void * context, NodeId remoteNodeId, CHIP_ERROR status);
104 chip::Callback::Callback<OnCurrentFabricRemove> mOnRemoveCurrentFabricCallback;
105};
106
107} // namespace Controller
108} // namespace chip