|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +#include "AddDeviceCommand.h" |
| 20 | + |
| 21 | +#include <admin/DeviceManager.h> |
| 22 | +#include <lib/shell/streamer.h> |
| 23 | + |
| 24 | +using namespace ::chip; |
| 25 | + |
| 26 | +namespace commands { |
| 27 | + |
| 28 | +AddDeviceCommand::AddDeviceCommand(chip::NodeId nodeId, uint32_t setupPINCode, const char * remoteAddr, uint16_t remotePort) : |
| 29 | + mNodeId(nodeId), mSetupPINCode(setupPINCode), mRemoteAddr(remoteAddr), mRemotePort(remotePort) |
| 30 | +{} |
| 31 | + |
| 32 | +void AddDeviceCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err) |
| 33 | +{ |
| 34 | + if (mNodeId != deviceId) |
| 35 | + { |
| 36 | + if (err != CHIP_NO_ERROR) |
| 37 | + { |
| 38 | + ChipLogError(NotSpecified, |
| 39 | + "Failed to pair non-specified device (0x:" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT, |
| 40 | + ChipLogValueX64(deviceId), err.Format()); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + ChipLogProgress(NotSpecified, "Commissioning complete for non-specified device: NodeId: " ChipLogFormatX64, |
| 45 | + ChipLogValueX64(deviceId)); |
| 46 | + } |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (err == CHIP_NO_ERROR) |
| 51 | + { |
| 52 | + ChipLogProgress(NotSpecified, "Successfully paired device: NodeId: " ChipLogFormatX64, ChipLogValueX64(mNodeId)); |
| 53 | + |
| 54 | + admin::DeviceMgr().UpdateLastUsedNodeId(mNodeId); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + ChipLogError(NotSpecified, "Failed to pair device (0x:" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT, |
| 59 | + ChipLogValueX64(deviceId), err.Format()); |
| 60 | + } |
| 61 | + |
| 62 | + CommandRegistry::Instance().ResetActiveCommand(); |
| 63 | +} |
| 64 | + |
| 65 | +CHIP_ERROR AddDeviceCommand::RunCommand() |
| 66 | +{ |
| 67 | + if (admin::DeviceMgr().IsCurrentBridgeDevice(mNodeId)) |
| 68 | + { |
| 69 | + // print to console |
| 70 | + fprintf(stderr, "The specified node ID has been reserved by the Fabric Bridge.\n"); |
| 71 | + return CHIP_ERROR_INVALID_ARGUMENT; |
| 72 | + } |
| 73 | + |
| 74 | + admin::PairingManager::Instance().SetPairingDelegate(this); |
| 75 | + |
| 76 | + return admin::DeviceMgr().PairRemoteDevice(mNodeId, mSetupPINCode, mRemoteAddr, mRemotePort); |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace commands |
0 commit comments