Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / darwin / Framework / CHIP / CHIPDevicePairingDelegateBridge.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 "CHIPDevicePairingDelegateBridge.h"
19 #import "CHIPError.h"
20
21 CHIPDevicePairingDelegateBridge::CHIPDevicePairingDelegateBridge(void)
22     : mDelegate(nil)
23 {
24 }
25
26 CHIPDevicePairingDelegateBridge::~CHIPDevicePairingDelegateBridge(void) {}
27
28 void CHIPDevicePairingDelegateBridge::setDelegate(id<CHIPDevicePairingDelegate> delegate, dispatch_queue_t queue)
29 {
30     if (delegate && queue) {
31         mDelegate = delegate;
32         mQueue = queue;
33     } else {
34         mDelegate = nil;
35         mQueue = nil;
36     }
37 }
38
39 CHIPPairingStatus CHIPDevicePairingDelegateBridge::MapStatus(chip::RendezvousSessionDelegate::Status status)
40 {
41     CHIPPairingStatus rv = kUnknownStatus;
42     switch (status) {
43     case chip::RendezvousSessionDelegate::Status::SecurePairingSuccess:
44         rv = kSecurePairingSuccess;
45         break;
46     case chip::RendezvousSessionDelegate::Status::SecurePairingFailed:
47         rv = kSecurePairingFailed;
48         break;
49     case chip::RendezvousSessionDelegate::Status::NetworkProvisioningSuccess:
50         rv = kNetworkProvisioningSuccess;
51         break;
52     case chip::RendezvousSessionDelegate::Status::NetworkProvisioningFailed:
53         rv = kNetworkProvisioningFailed;
54         break;
55     }
56     return rv;
57 }
58
59 void CHIPDevicePairingDelegateBridge::OnStatusUpdate(chip::RendezvousSessionDelegate::Status status)
60 {
61     NSLog(@"DevicePairingDelegate status updated: %d", status);
62
63     id<CHIPDevicePairingDelegate> strongDelegate = mDelegate;
64     if ([strongDelegate respondsToSelector:@selector(onStatusUpdate:)]) {
65         if (strongDelegate && mQueue) {
66             CHIPPairingStatus pairingStatus = MapStatus(status);
67             dispatch_async(mQueue, ^{
68                 [strongDelegate onStatusUpdate:pairingStatus];
69             });
70         }
71     }
72 }
73
74 void CHIPDevicePairingDelegateBridge::OnNetworkCredentialsRequested(chip::RendezvousDeviceCredentialsDelegate * callback)
75 {
76     NSLog(@"DevicePairingDelegate Requesting network credentials");
77
78     mCallback = callback;
79
80     id<CHIPDevicePairingDelegate> strongDelegate = mDelegate;
81     if (strongDelegate && mQueue) {
82         dispatch_async(mQueue, ^{
83             [strongDelegate onNetworkCredentialsRequested:kNetworkCredentialTypeWiFi];
84         });
85     }
86 }
87
88 void CHIPDevicePairingDelegateBridge::SendWiFiCredentials(NSString * ssid, NSString * password)
89 {
90     if (mCallback) {
91         mCallback->SendNetworkCredentials([ssid UTF8String], [password UTF8String]);
92     } else {
93         NSLog(@"Couldn't Send WiFi Credentials, are you sure pairing is in progress?");
94     }
95 }
96
97 void CHIPDevicePairingDelegateBridge::SendThreadCredentials(NSData * threadDataSet)
98 {
99     NSLog(@"Thread Provisioning is still a WIP, pairing will timeout...");
100 }
101
102 void CHIPDevicePairingDelegateBridge::OnOperationalCredentialsRequested(
103     const char * csr, size_t csr_length, chip::RendezvousDeviceCredentialsDelegate * callback)
104 {
105     NSLog(@"DevicePairingDelegate Requesting operational credentials");
106 }
107
108 void CHIPDevicePairingDelegateBridge::OnPairingComplete(CHIP_ERROR error)
109 {
110     NSLog(@"DevicePairingDelegate Pairing complete. Status %d", error);
111
112     id<CHIPDevicePairingDelegate> strongDelegate = mDelegate;
113     if ([strongDelegate respondsToSelector:@selector(onPairingComplete:)]) {
114         if (strongDelegate && mQueue) {
115             dispatch_async(mQueue, ^{
116                 NSError * nsError = [CHIPError errorForCHIPErrorCode:error];
117                 [strongDelegate onPairingComplete:nsError];
118             });
119         }
120     }
121 }
122
123 void CHIPDevicePairingDelegateBridge::OnPairingDeleted(CHIP_ERROR error)
124 {
125     NSLog(@"DevicePairingDelegate Pairing deleted. Status %d", error);
126
127     id<CHIPDevicePairingDelegate> strongDelegate = mDelegate;
128     if ([strongDelegate respondsToSelector:@selector(onPairingDeleted:)]) {
129         if (strongDelegate && mQueue) {
130             dispatch_async(mQueue, ^{
131                 NSError * nsError = [CHIPError errorForCHIPErrorCode:error];
132                 [strongDelegate onPairingDeleted:nsError];
133             });
134         }
135     }
136 }