blob: 4c25000ed91b01104053ac50a14ce78f2db11140 [file] [log] [blame]
Boris Zbarsky954a9202022-06-02 09:59:05 -04001/*
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 Woodf5c12c92022-07-02 00:43:40 -070018#import <Matter/Matter.h>
Boris Zbarsky954a9202022-06-02 09:59:05 -040019
Boris Zbarsky2c9cd922022-10-13 00:49:05 -040020#import "MTRError_Utils.h"
21
Boris Zbarsky954a9202022-06-02 09:59:05 -040022#include "OpenCommissioningWindowCommand.h"
23
24CHIP_ERROR OpenCommissioningWindowCommand::RunCommand()
25{
Boris Zbarsky12802fb2022-10-12 21:45:38 -040026 mWorkQueue = dispatch_queue_create("com.chip.open_commissioning_window", DISPATCH_QUEUE_SERIAL);
Boris Zbarsky2c9cd922022-10-13 00:49:05 -040027 auto * controller = CurrentCommissioner();
28 auto * device = [MTRDevice deviceWithNodeID:mNodeId deviceController:controller];
29
Boris Zbarsky12802fb2022-10-12 21:45:38 -040030 auto * self = this;
Boris Zbarsky2c9cd922022-10-13 00:49:05 -040031 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 Zbarsky12802fb2022-10-12 21:45:38 -040073
Boris Zbarsky954a9202022-06-02 09:59:05 -040074 return CHIP_NO_ERROR;
75}