Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / darwin / CHIPTool / CHIPTool / View Controllers / Bindings / BindingsViewController.m
1 /**
2  *
3  *    Copyright (c) 2021 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 "BindingsViewController.h"
19 #import "CHIPUIViewUtils.h"
20 #import "DefaultsUtils.h"
21
22 @interface BindingsViewController ()
23 @property (nonatomic, strong) UITextField * nodeIDTextField;
24 @property (nonatomic, strong) UITextField * groupIDTextField;
25 @property (nonatomic, strong) UITextField * endpointIDTextField;
26 @property (nonatomic, strong) UITextField * clusterIDTextField;
27
28 @property (nonatomic, strong) CHIPBinding * cluster;
29 @end
30
31 @implementation BindingsViewController
32
33 - (void)viewDidLoad
34 {
35     [super viewDidLoad];
36     [self setupUI];
37
38     UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
39     [self.view addGestureRecognizer:tap];
40
41     self.cluster = [[CHIPBinding alloc] initWithDevice:CHIPGetPairedDevice() endpoint:1 queue:dispatch_get_main_queue()];
42 }
43
44 - (void)dismissKeyboard
45 {
46     [_nodeIDTextField resignFirstResponder];
47     [_endpointIDTextField resignFirstResponder];
48     [_groupIDTextField resignFirstResponder];
49     [_clusterIDTextField resignFirstResponder];
50 }
51
52 // MARK: UI helpers
53
54 - (void)setupUI
55 {
56     self.view.backgroundColor = UIColor.whiteColor;
57
58     // Title
59     UILabel * titleLabel = [CHIPUIViewUtils addTitle:@"Bindings" toView:self.view];
60
61     // stack view
62     UIStackView * stackView = [UIStackView new];
63     stackView.axis = UILayoutConstraintAxisVertical;
64     stackView.distribution = UIStackViewDistributionFill;
65     stackView.alignment = UIStackViewAlignmentLeading;
66     stackView.spacing = 30;
67     [self.view addSubview:stackView];
68
69     stackView.translatesAutoresizingMaskIntoConstraints = false;
70     [stackView.topAnchor constraintEqualToAnchor:titleLabel.bottomAnchor constant:30].active = YES;
71     [stackView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:30].active = YES;
72     [stackView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-30].active = YES;
73
74     // nodeID entry
75     UILabel * nodeIDLabel = [UILabel new];
76     nodeIDLabel.text = @"Node ID";
77     _nodeIDTextField = [UITextField new];
78     UIView * nodeIDView = [CHIPUIViewUtils viewWithLabel:nodeIDLabel textField:_nodeIDTextField];
79     [stackView addArrangedSubview:nodeIDView];
80     nodeIDView.translatesAutoresizingMaskIntoConstraints = false;
81     [nodeIDView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
82
83     // groupID entry
84     UILabel * groupIDLabel = [UILabel new];
85     groupIDLabel.text = @"Group ID";
86     _groupIDTextField = [UITextField new];
87     UIView * groupIDView = [CHIPUIViewUtils viewWithLabel:groupIDLabel textField:_groupIDTextField];
88     [stackView addArrangedSubview:groupIDView];
89     [groupIDView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
90
91     // endpointID entry
92     UILabel * endpointIDLabel = [UILabel new];
93     endpointIDLabel.text = @"Endpoint ID";
94     _endpointIDTextField = [UITextField new];
95     UIView * endpointIDView = [CHIPUIViewUtils viewWithLabel:endpointIDLabel textField:_endpointIDTextField];
96     [stackView addArrangedSubview:endpointIDView];
97     endpointIDView.translatesAutoresizingMaskIntoConstraints = false;
98     [endpointIDView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
99
100     // clusterID entry
101     UILabel * clusterIDLabel = [UILabel new];
102     clusterIDLabel.text = @"Cluster ID";
103     _clusterIDTextField = [UITextField new];
104     UIView * clusterIDView = [CHIPUIViewUtils viewWithLabel:clusterIDLabel textField:_clusterIDTextField];
105     [stackView addArrangedSubview:clusterIDView];
106     [clusterIDView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
107
108     // Buttons
109     UIButton * bindButton = [UIButton new];
110     [bindButton setTitle:@"Bind" forState:UIControlStateNormal];
111     UIButton * unbindButton = [UIButton new];
112     [unbindButton setTitle:@"Unbind" forState:UIControlStateNormal];
113     [bindButton addTarget:self action:@selector(bind:) forControlEvents:UIControlEventTouchUpInside];
114     [unbindButton addTarget:self action:@selector(unbind:) forControlEvents:UIControlEventTouchUpInside];
115     UIStackView * stackViewButtons = [CHIPUIViewUtils stackViewWithButtons:@[ bindButton, unbindButton ]];
116
117     [stackView addArrangedSubview:stackViewButtons];
118     [stackViewButtons.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
119
120     [self _clearTextFields];
121 }
122
123 - (void)_clearTextFields
124 {
125     CHIPDeviceController * chipController = InitializeCHIP();
126     _nodeIDTextField.text = [NSString stringWithFormat:@"%@", chipController.getControllerNodeId];
127     _endpointIDTextField.text = @"1";
128     _groupIDTextField.text = @"0";
129     _clusterIDTextField.text = @"";
130 }
131
132 // MARK: Button methods
133
134 - (IBAction)bind:(id)sender
135 {
136     uint64_t nodeId;
137     NSScanner * scanner = [NSScanner scannerWithString:_nodeIDTextField.text];
138     [scanner scanUnsignedLongLong:&nodeId];
139     int endpointId = [_endpointIDTextField.text intValue];
140     int groupId = [_groupIDTextField.text intValue];
141     int clusterId = [_clusterIDTextField.text intValue];
142
143     [self.cluster bind:nodeId
144                   groupId:groupId
145                endpointId:endpointId
146                 clusterId:clusterId
147         completionHandler:^(NSError * _Nullable error, NSDictionary * _Nullable values) {
148             NSString * resultString
149                 = (error == nil) ? @"Bind command: success!" : [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code];
150             NSLog(resultString, nil);
151         }];
152 }
153
154 - (IBAction)unbind:(id)sender
155 {
156     int nodeId = [_nodeIDTextField.text intValue];
157     int endpointId = [_endpointIDTextField.text intValue];
158     int groupId = [_groupIDTextField.text intValue];
159     int clusterId = [_clusterIDTextField.text intValue];
160
161     [self.cluster unbind:nodeId
162                   groupId:groupId
163                endpointId:endpointId
164                 clusterId:clusterId
165         completionHandler:^(NSError * _Nullable error, NSDictionary * _Nullable values) {
166             NSString * resultString = (error == nil) ? @"Unbind command: success!"
167                                                      : [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code];
168             NSLog(resultString, nil);
169         }];
170 }
171
172 @end