Skip to content

Commit 9c389ba

Browse files
authoredNov 5, 2024
[Fabric-Sync] Port 'add-bridge' and 'remove-bridge' commands (project-chip#36241)
* [Fabric-Sync] Implemenat 'add-bridge' command * Address review comments
1 parent 62a4a97 commit 9c389ba

15 files changed

+1524
-1
lines changed
 

‎examples/fabric-sync/BUILD.gn

+15-1
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,35 @@ assert(chip_build_tools)
2222
executable("fabric-sync") {
2323
cflags = [ "-Wconversion" ]
2424

25+
defines = []
26+
27+
if (chip_build_libshell) {
28+
defines += [ "ENABLE_CHIP_SHELL" ]
29+
}
30+
2531
include_dirs = [
2632
".",
2733
"${chip_root}/src/lib",
2834
]
2935

36+
if (chip_build_libshell) {
37+
include_dirs += [ "shell" ]
38+
}
39+
3040
sources = [ "main.cpp" ]
3141

3242
deps = [
43+
"${chip_root}/examples/fabric-sync/admin:fabric-admin-lib",
3344
"${chip_root}/examples/fabric-sync/bridge:fabric-bridge-lib",
34-
"${chip_root}/examples/fabric-sync/bridge:fabric-bridge-zap",
3545
"${chip_root}/examples/platform/linux:app-main",
3646
"${chip_root}/src/lib",
3747
"${chip_root}/third_party/inipp",
3848
]
3949

50+
if (chip_build_libshell) {
51+
deps += [ "${chip_root}/examples/fabric-sync/shell" ]
52+
}
53+
4054
output_dir = root_out_dir
4155
}
4256

‎examples/fabric-sync/admin/BUILD.gn

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) 2024 Project CHIP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import("//build_overrides/chip.gni")
16+
import("${chip_root}/src/app/chip_data_model.gni")
17+
18+
source_set("fabric-admin-lib") {
19+
sources = [
20+
"DeviceManager.cpp",
21+
"DeviceManager.h",
22+
"PairingManager.cpp",
23+
"PairingManager.h",
24+
]
25+
26+
deps = [
27+
"${chip_root}/examples/fabric-sync/bridge:fabric-bridge-lib",
28+
"${chip_root}/src/lib",
29+
]
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 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 "DeviceManager.h"
20+
21+
#include <crypto/RandUtils.h>
22+
#include <lib/support/StringBuilder.h>
23+
24+
#include <cstdio>
25+
#include <string>
26+
27+
using namespace chip;
28+
29+
// Define the static member
30+
DeviceManager DeviceManager::sInstance;
31+
32+
void DeviceManager::Init()
33+
{
34+
// TODO: (#34113) Init mLastUsedNodeId from chip config file
35+
mLastUsedNodeId = 1;
36+
mInitialized = true;
37+
38+
ChipLogProgress(NotSpecified, "DeviceManager initialized: last used nodeId " ChipLogFormatX64,
39+
ChipLogValueX64(mLastUsedNodeId));
40+
}
41+
42+
NodeId DeviceManager::GetNextAvailableNodeId()
43+
{
44+
mLastUsedNodeId++;
45+
VerifyOrDieWithMsg(mLastUsedNodeId < std::numeric_limits<NodeId>::max(), NotSpecified, "No more available NodeIds.");
46+
47+
return mLastUsedNodeId;
48+
}
49+
50+
void DeviceManager::UpdateLastUsedNodeId(NodeId nodeId)
51+
{
52+
if (nodeId > mLastUsedNodeId)
53+
{
54+
mLastUsedNodeId = nodeId;
55+
ChipLogProgress(NotSpecified, "Updating last used NodeId to " ChipLogFormatX64, ChipLogValueX64(mLastUsedNodeId));
56+
}
57+
}
58+
59+
void DeviceManager::SetRemoteBridgeNodeId(chip::NodeId nodeId)
60+
{
61+
mRemoteBridgeNodeId = nodeId;
62+
}
63+
64+
CHIP_ERROR DeviceManager::PairRemoteFabricBridge(NodeId nodeId, uint32_t setupPINCode, const char * deviceRemoteIp,
65+
uint16_t deviceRemotePort)
66+
{
67+
CHIP_ERROR err = PairingManager::Instance().PairDevice(nodeId, setupPINCode, deviceRemoteIp, deviceRemotePort);
68+
69+
if (err != CHIP_NO_ERROR)
70+
{
71+
ChipLogError(NotSpecified,
72+
"Failed to pair remote fabric bridge: Node ID " ChipLogFormatX64 " with error: %" CHIP_ERROR_FORMAT,
73+
ChipLogValueX64(nodeId), err.Format());
74+
return err;
75+
}
76+
77+
ChipLogProgress(NotSpecified, "Successfully paired remote fabric bridge: Node ID " ChipLogFormatX64, ChipLogValueX64(nodeId));
78+
return CHIP_NO_ERROR;
79+
}
80+
81+
CHIP_ERROR DeviceManager::UnpairRemoteFabricBridge()
82+
{
83+
if (mRemoteBridgeNodeId == kUndefinedNodeId)
84+
{
85+
ChipLogError(NotSpecified, "Remote bridge node ID is undefined; cannot unpair device.");
86+
return CHIP_ERROR_INCORRECT_STATE;
87+
}
88+
89+
CHIP_ERROR err = PairingManager::Instance().UnpairDevice(mRemoteBridgeNodeId);
90+
if (err != CHIP_NO_ERROR)
91+
{
92+
ChipLogError(NotSpecified, "Failed to unpair remote bridge device " ChipLogFormatX64, ChipLogValueX64(mRemoteBridgeNodeId));
93+
return err;
94+
}
95+
96+
ChipLogProgress(NotSpecified, "Successfully unpaired remote fabric bridge: Node ID " ChipLogFormatX64,
97+
ChipLogValueX64(mRemoteBridgeNodeId));
98+
return CHIP_NO_ERROR;
99+
}
100+
101+
void DeviceManager::OnDeviceRemoved(NodeId deviceId, CHIP_ERROR err)
102+
{
103+
if (err != CHIP_NO_ERROR)
104+
{
105+
ChipLogError(NotSpecified, "Failed to remove synced device:(" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT,
106+
ChipLogValueX64(deviceId), err.Format());
107+
return;
108+
}
109+
110+
ChipLogProgress(NotSpecified, "Synced device with NodeId:" ChipLogFormatX64 " has been removed.", ChipLogValueX64(deviceId));
111+
}
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 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 "PairingManager.h"
22+
23+
#include <app-common/zap-generated/cluster-objects.h>
24+
#include <platform/CHIPDeviceLayer.h>
25+
26+
class DeviceManager : public PairingDelegate
27+
{
28+
public:
29+
DeviceManager() = default;
30+
31+
void Init();
32+
33+
chip::NodeId GetNextAvailableNodeId();
34+
35+
chip::NodeId GetRemoteBridgeNodeId() const { return mRemoteBridgeNodeId; }
36+
37+
void UpdateLastUsedNodeId(chip::NodeId nodeId);
38+
39+
void SetRemoteBridgeNodeId(chip::NodeId nodeId);
40+
41+
bool IsFabricSyncReady() const { return mRemoteBridgeNodeId != chip::kUndefinedNodeId; }
42+
43+
/**
44+
* @brief Determines whether a given nodeId corresponds to the remote bridge device.
45+
*
46+
* @param nodeId The ID of the node being checked.
47+
*
48+
* @return true if the nodeId matches the remote bridge device; otherwise, false.
49+
*/
50+
bool IsCurrentBridgeDevice(chip::NodeId nodeId) const { return nodeId == mRemoteBridgeNodeId; }
51+
52+
/**
53+
* @brief Pair a remote fabric bridge with a given node ID.
54+
*
55+
* This function initiates the pairing process for a remote fabric bridge using the specified parameters.
56+
57+
* @param nodeId The user-defined ID for the node being commissioned. It doesn’t need to be the same ID,
58+
* as for the first fabric.
59+
* @param setupPINCode The setup PIN code used to authenticate the pairing process.
60+
* @param deviceRemoteIp The IP address of the remote device that is being paired as part of the fabric bridge.
61+
* @param deviceRemotePort The secured device port of the remote device that is being paired as part of the fabric bridge.
62+
*
63+
* @return CHIP_ERROR Returns CHIP_NO_ERROR on success or an appropriate error code on failure.
64+
*/
65+
CHIP_ERROR PairRemoteFabricBridge(chip::NodeId nodeId, uint32_t setupPINCode, const char * deviceRemoteIp,
66+
uint16_t deviceRemotePort);
67+
68+
CHIP_ERROR UnpairRemoteFabricBridge();
69+
70+
private:
71+
friend DeviceManager & DeviceMgr();
72+
73+
void OnDeviceRemoved(chip::NodeId deviceId, CHIP_ERROR err) override;
74+
75+
static DeviceManager sInstance;
76+
77+
chip::NodeId mLastUsedNodeId = 0;
78+
79+
// The Node ID of the remote bridge used for Fabric-Sync
80+
// This represents the bridge on the other ecosystem.
81+
chip::NodeId mRemoteBridgeNodeId = chip::kUndefinedNodeId;
82+
83+
bool mInitialized = false;
84+
};
85+
86+
/**
87+
* Returns the public interface of the DeviceManager singleton object.
88+
*
89+
* Applications should use this to access features of the DeviceManager
90+
* object.
91+
*/
92+
inline DeviceManager & DeviceMgr()
93+
{
94+
if (!DeviceManager::sInstance.mInitialized)
95+
{
96+
DeviceManager::sInstance.Init();
97+
}
98+
return DeviceManager::sInstance;
99+
}

0 commit comments

Comments
 (0)
Please sign in to comment.