Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / darwin / Framework / CHIP / CHIPSetupPayload.mm
1 /**
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
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 #import "CHIPSetupPayload.h"
19 #import "CHIPError.h"
20 #import <setup_payload/SetupPayload.h>
21
22 @implementation CHIPOptionalQRCodeInfo
23 @end
24
25 @implementation CHIPSetupPayload {
26     chip::SetupPayload _chipSetupPayload;
27 }
28
29 - (RendezvousInformationFlags)valueOf:(chip::RendezvousInformationFlags)value
30 {
31     RendezvousInformationFlags rv = kRendezvousInformationNone;
32     switch (value) {
33     case chip::RendezvousInformationFlags::kNone:
34         rv = kRendezvousInformationNone;
35         break;
36     case chip::RendezvousInformationFlags::kWiFi:
37         rv = kRendezvousInformationWiFi;
38         break;
39     case chip::RendezvousInformationFlags::kBLE:
40         rv = kRendezvousInformationBLE;
41         break;
42     case chip::RendezvousInformationFlags::kThread:
43         rv = kRendezvousInformationThread;
44         break;
45     case chip::RendezvousInformationFlags::kEthernet:
46         rv = kRendezvousInformationEthernet;
47         break;
48     case chip::RendezvousInformationFlags::kAllMask:
49         rv = kRendezvousInformationAllMask;
50         break;
51     }
52     return rv;
53 }
54
55 - (id)initWithSetupPayload:(chip::SetupPayload)setupPayload
56 {
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];
66
67         [self getSerialNumber:setupPayload];
68     }
69     return self;
70 }
71
72 - (void)getSerialNumber:(chip::SetupPayload)setupPayload
73 {
74     std::string serialNumberC;
75     CHIP_ERROR err = setupPayload.getSerialNumber(serialNumberC);
76     if (err == CHIP_NO_ERROR) {
77         _serialNumber = [NSString stringWithUTF8String:serialNumberC.c_str()];
78     }
79 }
80
81 - (NSArray<CHIPOptionalQRCodeInfo *> *)getAllOptionalVendorData:(NSError * __autoreleasing *)error
82 {
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()];
92             break;
93         case chip::optionalQRCodeInfoTypeInt32:
94             info.infoType = [NSNumber numberWithInt:kOptionalQRCodeInfoTypeInt32];
95             info.integerValue = [NSNumber numberWithInt:chipInfo.int32];
96             break;
97         default:
98             if (error) {
99                 *error = [NSError errorWithDomain:CHIPErrorDomain code:CHIPErrorCodeInvalidArgument userInfo:nil];
100             }
101             return @[];
102         }
103         [allOptionalData addObject:info];
104     }
105     return allOptionalData;
106 }
107 @end