|
| 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 | +} |
0 commit comments