3 * Copyright (c) 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 "BindingsViewController.h"
19 #import "CHIPUIViewUtils.h"
20 #import "DefaultsUtils.h"
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;
28 @property (nonatomic, strong) CHIPBinding * cluster;
31 @implementation BindingsViewController
38 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
39 [self.view addGestureRecognizer:tap];
41 self.cluster = [[CHIPBinding alloc] initWithDevice:CHIPGetPairedDevice() endpoint:1 queue:dispatch_get_main_queue()];
44 - (void)dismissKeyboard
46 [_nodeIDTextField resignFirstResponder];
47 [_endpointIDTextField resignFirstResponder];
48 [_groupIDTextField resignFirstResponder];
49 [_clusterIDTextField resignFirstResponder];
56 self.view.backgroundColor = UIColor.whiteColor;
59 UILabel * titleLabel = [CHIPUIViewUtils addTitle:@"Bindings" toView:self.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];
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;
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;
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;
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;
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;
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 ]];
117 [stackView addArrangedSubview:stackViewButtons];
118 [stackViewButtons.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
120 [self _clearTextFields];
123 - (void)_clearTextFields
125 CHIPDeviceController * chipController = InitializeCHIP();
126 _nodeIDTextField.text = [NSString stringWithFormat:@"%@", chipController.getControllerNodeId];
127 _endpointIDTextField.text = @"1";
128 _groupIDTextField.text = @"0";
129 _clusterIDTextField.text = @"";
132 // MARK: Button methods
134 - (IBAction)bind:(id)sender
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];
143 [self.cluster bind:nodeId
145 endpointId:endpointId
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);
154 - (IBAction)unbind:(id)sender
156 int nodeId = [_nodeIDTextField.text intValue];
157 int endpointId = [_endpointIDTextField.text intValue];
158 int groupId = [_groupIDTextField.text intValue];
159 int clusterId = [_clusterIDTextField.text intValue];
161 [self.cluster unbind:nodeId
163 endpointId:endpointId
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);