|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 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 "CommissionerCommands.h" |
| 20 | + |
| 21 | +constexpr uint16_t kPayloadMaxSize = 64; |
| 22 | + |
| 23 | +CHIP_ERROR CommissionerCommands::PairWithQRCode(chip::NodeId nodeId, const chip::CharSpan payload) |
| 24 | +{ |
| 25 | + VerifyOrReturnError(payload.size() > 0 && payload.size() < kPayloadMaxSize, CHIP_ERROR_INVALID_ARGUMENT); |
| 26 | + |
| 27 | + GetCurrentCommissioner().RegisterPairingDelegate(this); |
| 28 | + |
| 29 | + char qrCode[kPayloadMaxSize]; |
| 30 | + memcpy(qrCode, payload.data(), payload.size()); |
| 31 | + return GetCurrentCommissioner().PairDevice(nodeId, qrCode); |
| 32 | +} |
| 33 | + |
| 34 | +CHIP_ERROR CommissionerCommands::PairWithManualCode(chip::NodeId nodeId, const chip::CharSpan payload) |
| 35 | +{ |
| 36 | + VerifyOrReturnError(payload.size() > 0 && payload.size() < kPayloadMaxSize, CHIP_ERROR_INVALID_ARGUMENT); |
| 37 | + |
| 38 | + GetCurrentCommissioner().RegisterPairingDelegate(this); |
| 39 | + |
| 40 | + char manualCode[kPayloadMaxSize]; |
| 41 | + memcpy(manualCode, payload.data(), payload.size()); |
| 42 | + return GetCurrentCommissioner().PairDevice(nodeId, manualCode); |
| 43 | +} |
| 44 | + |
| 45 | +CHIP_ERROR CommissionerCommands::Unpair(chip::NodeId nodeId) |
| 46 | +{ |
| 47 | + return GetCurrentCommissioner().UnpairDevice(nodeId); |
| 48 | +} |
| 49 | + |
| 50 | +void CommissionerCommands::OnStatusUpdate(DevicePairingDelegate::Status status) |
| 51 | +{ |
| 52 | + switch (status) |
| 53 | + { |
| 54 | + case DevicePairingDelegate::Status::SecurePairingSuccess: |
| 55 | + ChipLogProgress(chipTool, "Secure Pairing Success"); |
| 56 | + break; |
| 57 | + case DevicePairingDelegate::Status::SecurePairingFailed: |
| 58 | + ChipLogError(chipTool, "Secure Pairing Failed"); |
| 59 | + break; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +void CommissionerCommands::OnPairingComplete(CHIP_ERROR err) |
| 64 | +{ |
| 65 | + if (CHIP_NO_ERROR != err) |
| 66 | + { |
| 67 | + ChipLogError(chipTool, "Pairing Complete Failure: %s", ErrorStr(err)); |
| 68 | + LogErrorOnFailure(ContinueOnChipMainThread(err)); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void CommissionerCommands::OnPairingDeleted(CHIP_ERROR err) |
| 73 | +{ |
| 74 | + if (CHIP_NO_ERROR != err) |
| 75 | + { |
| 76 | + ChipLogError(chipTool, "Pairing Delete Failure: %s", ErrorStr(err)); |
| 77 | + } |
| 78 | + |
| 79 | + LogErrorOnFailure(ContinueOnChipMainThread(err)); |
| 80 | +} |
| 81 | + |
| 82 | +void CommissionerCommands::OnCommissioningComplete(chip::NodeId nodeId, CHIP_ERROR err) |
| 83 | +{ |
| 84 | + if (CHIP_NO_ERROR != err) |
| 85 | + { |
| 86 | + ChipLogError(chipTool, "Commissioning Complete Failure: %s", ErrorStr(err)); |
| 87 | + } |
| 88 | + |
| 89 | + LogErrorOnFailure(ContinueOnChipMainThread(err)); |
| 90 | +} |
0 commit comments