Boris Zbarsky | 954a920 | 2022-06-02 09:59:05 -0400 | [diff] [blame] | 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 | |
Justin Wood | f5c12c9 | 2022-07-02 00:43:40 -0700 | [diff] [blame] | 18 | #import <Matter/Matter.h> |
Boris Zbarsky | 954a920 | 2022-06-02 09:59:05 -0400 | [diff] [blame] | 19 | |
Boris Zbarsky | 2c9cd92 | 2022-10-13 00:49:05 -0400 | [diff] [blame^] | 20 | #import "MTRError_Utils.h" |
| 21 | |
Boris Zbarsky | 954a920 | 2022-06-02 09:59:05 -0400 | [diff] [blame] | 22 | #include "OpenCommissioningWindowCommand.h" |
| 23 | |
| 24 | CHIP_ERROR OpenCommissioningWindowCommand::RunCommand() |
| 25 | { |
Boris Zbarsky | 12802fb | 2022-10-12 21:45:38 -0400 | [diff] [blame] | 26 | mWorkQueue = dispatch_queue_create("com.chip.open_commissioning_window", DISPATCH_QUEUE_SERIAL); |
Boris Zbarsky | 2c9cd92 | 2022-10-13 00:49:05 -0400 | [diff] [blame^] | 27 | auto * controller = CurrentCommissioner(); |
| 28 | auto * device = [MTRDevice deviceWithNodeID:mNodeId deviceController:controller]; |
| 29 | |
Boris Zbarsky | 12802fb | 2022-10-12 21:45:38 -0400 | [diff] [blame] | 30 | auto * self = this; |
Boris Zbarsky | 2c9cd92 | 2022-10-13 00:49:05 -0400 | [diff] [blame^] | 31 | if (mCommissioningWindowOption == 0) { |
| 32 | auto * cluster = [[MTRClusterAdministratorCommissioning alloc] initWithDevice:device endpoint:0 queue:mWorkQueue]; |
| 33 | auto * params = [[MTRAdministratorCommissioningClusterOpenBasicCommissioningWindowParams alloc] init]; |
| 34 | params.commissioningTimeout = @(mCommissioningWindowTimeoutMs); |
| 35 | params.timedInvokeTimeoutMs = @(10000); |
| 36 | [cluster openBasicCommissioningWindowWithParams:params |
| 37 | expectedValues:nil |
| 38 | expectedValueInterval:nil |
| 39 | completionHandler:^(NSError * _Nullable error) { |
| 40 | if (error == nil) { |
| 41 | self->SetCommandExitStatus(CHIP_NO_ERROR); |
| 42 | } else { |
| 43 | self->SetCommandExitStatus(MTRErrorToCHIPErrorCode(error)); |
| 44 | } |
| 45 | }]; |
| 46 | } else { |
| 47 | [device |
| 48 | openCommissioningWindowWithSetupPasscode:[MTRSetupPayload generateRandomSetupPasscode] |
| 49 | discriminator:@(mDiscriminator) |
| 50 | duration:@(mCommissioningWindowTimeoutMs) |
| 51 | queue:mWorkQueue |
| 52 | completion:^(MTRSetupPayload * _Nullable payload, NSError * error) { |
| 53 | if (error != nil) { |
| 54 | self->SetCommandExitStatus(MTRErrorToCHIPErrorCode(error)); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if (payload == nil) { |
| 59 | self->SetCommandExitStatus(CHIP_ERROR_INVALID_ARGUMENT); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | auto * pairingCode = [payload manualEntryCode]; |
| 64 | if (pairingCode == nil) { |
| 65 | self->SetCommandExitStatus(CHIP_ERROR_INVALID_ARGUMENT); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | ChipLogProgress(chipTool, "Setup code: %s\n", [[payload manualEntryCode] UTF8String]); |
| 70 | self->SetCommandExitStatus(CHIP_NO_ERROR); |
| 71 | }]; |
| 72 | } |
Boris Zbarsky | 12802fb | 2022-10-12 21:45:38 -0400 | [diff] [blame] | 73 | |
Boris Zbarsky | 954a920 | 2022-06-02 09:59:05 -0400 | [diff] [blame] | 74 | return CHIP_NO_ERROR; |
| 75 | } |