3 * Copyright (c) 2020-2021 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #import "CHIPDevice_Internal.h"
19 #import "CHIPLogging.h"
20 #import <CHIP/CHIPError.h>
21 #import <setup_payload/ManualSetupPayloadGenerator.h>
22 #import <setup_payload/SetupPayload.h>
24 @interface CHIPDevice ()
26 @property (nonatomic, readonly, strong, nonnull) NSRecursiveLock * lock;
27 @property (readonly) chip::Controller::Device * cppDevice;
31 @implementation CHIPDevice
35 if (self = [super init]) {
36 _lock = [[NSRecursiveLock alloc] init];
41 - (instancetype)initWithDevice:(chip::Controller::Device *)device
43 if (self = [super init]) {
49 - (chip::Controller::Device *)internalDevice
54 - (BOOL)openPairingWindow:(NSUInteger)duration error:(NSError * __autoreleasing *)error
56 CHIP_ERROR err = CHIP_NO_ERROR;
58 chip::SetupPayload setupPayload;
60 if (duration > UINT32_MAX) {
61 CHIP_LOG_ERROR("Error: Duration %tu is too large. Max value %d", duration, UINT32_MAX);
63 *error = [CHIPError errorForCHIPErrorCode:CHIP_ERROR_INVALID_INTEGER_VALUE];
69 err = self.cppDevice->OpenPairingWindow(
70 (uint32_t) duration, chip::Controller::Device::PairingWindowOption::kOriginalSetupCode, setupPayload);
73 if (err != CHIP_NO_ERROR) {
74 CHIP_LOG_ERROR("Error(%d): %@, Open Pairing Window failed", err, [CHIPError errorForCHIPErrorCode:err]);
76 *error = [CHIPError errorForCHIPErrorCode:err];
84 - (NSString *)openPairingWindowWithPIN:(NSUInteger)duration
85 discriminator:(NSUInteger)discriminator
86 setupPIN:(NSUInteger)setupPIN
87 error:(NSError * __autoreleasing *)error
89 CHIP_ERROR err = CHIP_NO_ERROR;
91 chip::SetupPayload setupPayload;
93 if (duration > UINT32_MAX) {
94 CHIP_LOG_ERROR("Error: Duration %tu is too large. Max value %d", duration, UINT32_MAX);
96 *error = [CHIPError errorForCHIPErrorCode:CHIP_ERROR_INVALID_INTEGER_VALUE];
101 if (discriminator > 0xfff) {
102 CHIP_LOG_ERROR("Error: Discriminator %tu is too large. Max value %d", discriminator, 0xfff);
104 *error = [CHIPError errorForCHIPErrorCode:CHIP_ERROR_INVALID_INTEGER_VALUE];
108 setupPayload.discriminator = (uint16_t) discriminator;
111 setupPIN &= ((1 << chip::kSetupPINCodeFieldLengthInBits) - 1);
112 setupPayload.setUpPINCode = (uint32_t) setupPIN;
115 err = self.cppDevice->OpenPairingWindow(
116 (uint32_t) duration, chip::Controller::Device::PairingWindowOption::kTokenWithProvidedPIN, setupPayload);
119 if (err != CHIP_NO_ERROR) {
120 CHIP_LOG_ERROR("Error(%d): %@, Open Pairing Window failed", err, [CHIPError errorForCHIPErrorCode:err]);
122 *error = [CHIPError errorForCHIPErrorCode:err];
127 chip::ManualSetupPayloadGenerator generator(setupPayload);
130 if (generator.payloadDecimalStringRepresentation(outCode) == CHIP_NO_ERROR) {
131 CHIP_LOG_ERROR("Setup code is %s", outCode.c_str());
133 CHIP_LOG_ERROR("Failed to get decimal setup code");
137 return [NSString stringWithCString:outCode.c_str() encoding:[NSString defaultCStringEncoding]];
142 bool isActive = false;
145 isActive = self.cppDevice->IsActive();
148 return isActive ? YES : NO;