3 * Copyright (c) 2020 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 "CHIPSetupPayload.h"
20 #import <setup_payload/SetupPayload.h>
22 @implementation CHIPOptionalQRCodeInfo
25 @implementation CHIPSetupPayload {
26 chip::SetupPayload _chipSetupPayload;
29 - (RendezvousInformationFlags)valueOf:(chip::RendezvousInformationFlags)value
31 RendezvousInformationFlags rv = kRendezvousInformationNone;
33 case chip::RendezvousInformationFlags::kNone:
34 rv = kRendezvousInformationNone;
36 case chip::RendezvousInformationFlags::kWiFi:
37 rv = kRendezvousInformationWiFi;
39 case chip::RendezvousInformationFlags::kBLE:
40 rv = kRendezvousInformationBLE;
42 case chip::RendezvousInformationFlags::kThread:
43 rv = kRendezvousInformationThread;
45 case chip::RendezvousInformationFlags::kEthernet:
46 rv = kRendezvousInformationEthernet;
48 case chip::RendezvousInformationFlags::kAllMask:
49 rv = kRendezvousInformationAllMask;
55 - (id)initWithSetupPayload:(chip::SetupPayload)setupPayload
57 if (self = [super init]) {
58 _chipSetupPayload = setupPayload;
59 _version = [NSNumber numberWithUnsignedChar:setupPayload.version];
60 _vendorID = [NSNumber numberWithUnsignedShort:setupPayload.vendorID];
61 _productID = [NSNumber numberWithUnsignedShort:setupPayload.productID];
62 _requiresCustomFlow = setupPayload.requiresCustomFlow == 1;
63 _rendezvousInformation = [self valueOf:setupPayload.rendezvousInformation];
64 _discriminator = [NSNumber numberWithUnsignedShort:setupPayload.discriminator];
65 _setUpPINCode = [NSNumber numberWithUnsignedLong:setupPayload.setUpPINCode];
67 [self getSerialNumber:setupPayload];
72 - (void)getSerialNumber:(chip::SetupPayload)setupPayload
74 std::string serialNumberC;
75 CHIP_ERROR err = setupPayload.getSerialNumber(serialNumberC);
76 if (err == CHIP_NO_ERROR) {
77 _serialNumber = [NSString stringWithUTF8String:serialNumberC.c_str()];
81 - (NSArray<CHIPOptionalQRCodeInfo *> *)getAllOptionalVendorData:(NSError * __autoreleasing *)error
83 NSMutableArray<CHIPOptionalQRCodeInfo *> * allOptionalData = [NSMutableArray new];
84 std::vector<chip::OptionalQRCodeInfo> chipOptionalData = _chipSetupPayload.getAllOptionalVendorData();
85 for (chip::OptionalQRCodeInfo chipInfo : chipOptionalData) {
86 CHIPOptionalQRCodeInfo * info = [CHIPOptionalQRCodeInfo new];
87 info.tag = [NSNumber numberWithUnsignedChar:chipInfo.tag];
88 switch (chipInfo.type) {
89 case chip::optionalQRCodeInfoTypeString:
90 info.infoType = [NSNumber numberWithInt:kOptionalQRCodeInfoTypeString];
91 info.stringValue = [NSString stringWithUTF8String:chipInfo.data.c_str()];
93 case chip::optionalQRCodeInfoTypeInt32:
94 info.infoType = [NSNumber numberWithInt:kOptionalQRCodeInfoTypeInt32];
95 info.integerValue = [NSNumber numberWithInt:chipInfo.int32];
99 *error = [NSError errorWithDomain:CHIPErrorDomain code:CHIPErrorCodeInvalidArgument userInfo:nil];
103 [allOptionalData addObject:info];
105 return allOptionalData;