Skip to content

Commit

Permalink
Add wifi config screen to CHIPTool
Browse files Browse the repository at this point in the history
  • Loading branch information
shana-apple committed Jul 23, 2020
1 parent ecadaf0 commit 2754297
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 78 deletions.
20 changes: 20 additions & 0 deletions src/darwin/CHIPTool/CHIPTool.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
B204A627244E1D0600C7C0E1 /* QRCodeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B204A626244E1D0600C7C0E1 /* QRCodeViewController.m */; };
B204A62C244E1D0700C7C0E1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B204A62B244E1D0700C7C0E1 /* Assets.xcassets */; };
B204A632244E1D0700C7C0E1 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = B204A631244E1D0700C7C0E1 /* main.m */; };
B2946A4224C99D53005C87D0 /* WifiViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B2946A4124C99D53005C87D0 /* WifiViewController.m */; };
B2946A9B24C9A7BF005C87D0 /* DefaultsUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = B2946A9A24C9A7BF005C87D0 /* DefaultsUtils.m */; };
B2F53AEB245B0D140010745E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B2F53AE9245B0D140010745E /* LaunchScreen.storyboard */; };
B2F53AEE245B0D1A0010745E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B2F53AEC245B0D190010745E /* Main.storyboard */; };
B2F53AEF245B0D910010745E /* CHIP.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B2E0D7BE245B0C59003C5B48 /* CHIP.framework */; };
Expand Down Expand Up @@ -64,6 +66,10 @@
B204A631244E1D0700C7C0E1 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
B204A637244E1D0700C7C0E1 /* CHIPToolTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CHIPToolTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
B204A63D244E1D0700C7C0E1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B2946A4024C99D53005C87D0 /* WifiViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WifiViewController.h; sourceTree = "<group>"; };
B2946A4124C99D53005C87D0 /* WifiViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WifiViewController.m; sourceTree = "<group>"; };
B2946A9924C9A7BF005C87D0 /* DefaultsUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DefaultsUtils.h; sourceTree = "<group>"; };
B2946A9A24C9A7BF005C87D0 /* DefaultsUtils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DefaultsUtils.m; sourceTree = "<group>"; };
B2E0D7BE245B0C59003C5B48 /* CHIP.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = CHIP.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B2F53AEA245B0D140010745E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = UI/Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
B2F53AED245B0D190010745E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = UI/Base.lproj/Main.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -156,9 +162,12 @@
B204A622244E1D0600C7C0E1 /* SceneDelegate.h */,
B204A623244E1D0600C7C0E1 /* SceneDelegate.m */,
B204A61F244E1D0600C7C0E1 /* AppDelegate.h */,
B2946A3F24C99D21005C87D0 /* Wifi */,
0C79937824858B3B0047A373 /* QRCode */,
0C79937924858B4F0047A373 /* Echo client */,
0CA0E0D0248599C4009087B9 /* OnOffCluster */,
B2946A9924C9A7BF005C87D0 /* DefaultsUtils.h */,
B2946A9A24C9A7BF005C87D0 /* DefaultsUtils.m */,
);
path = CHIPTool;
sourceTree = "<group>";
Expand All @@ -171,6 +180,15 @@
path = CHIPToolTests;
sourceTree = "<group>";
};
B2946A3F24C99D21005C87D0 /* Wifi */ = {
isa = PBXGroup;
children = (
B2946A4024C99D53005C87D0 /* WifiViewController.h */,
B2946A4124C99D53005C87D0 /* WifiViewController.m */,
);
path = Wifi;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -273,10 +291,12 @@
buildActionMask = 2147483647;
files = (
B204A627244E1D0600C7C0E1 /* QRCodeViewController.m in Sources */,
B2946A9B24C9A7BF005C87D0 /* DefaultsUtils.m in Sources */,
991DC091247747F500C13860 /* EchoViewController.m in Sources */,
B204A621244E1D0600C7C0E1 /* AppDelegate.m in Sources */,
B204A632244E1D0700C7C0E1 /* main.m in Sources */,
B204A624244E1D0600C7C0E1 /* SceneDelegate.m in Sources */,
B2946A4224C99D53005C87D0 /* WifiViewController.m in Sources */,
0C47BE4424885B97005E97F6 /* CHIPViewControllerBase.m in Sources */,
0CA0E0CF248599BB009087B9 /* OnOffViewController.m in Sources */,
);
Expand Down
25 changes: 25 additions & 0 deletions src/darwin/CHIPTool/CHIPTool/DefaultsUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
*
* Copyright (c) 2020 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

extern NSString *const kCHIPToolDefaultsDomain;
extern NSString *const kNetworkSSIDDefaultsKey;
extern NSString *const kNetworkPasswordDefaultsKey;

id CHIPGetDomainValueForKey(NSString *domain, NSString *key);
void CHIPSetDomainValueForKey(NSString *domain, NSString *key, id value);
37 changes: 37 additions & 0 deletions src/darwin/CHIPTool/CHIPTool/DefaultsUtils.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
*
* Copyright (c) 2020 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "DefaultsUtils.h"

NSString *const kCHIPToolDefaultsDomain = @"com.apple.chiptool";
NSString *const kNetworkSSIDDefaultsKey = @"networkSSID";
NSString *const kNetworkPasswordDefaultsKey = @"networkPassword";

id CHIPGetDomainValueForKey(NSString *domain, NSString *key) {
id value = (id)CFBridgingRelease(CFPreferencesCopyAppValue( (CFStringRef)key, (CFStringRef)domain));
if (value) {
return value;
}
return nil;
}

void CHIPSetDomainValueForKey(NSString *domain, NSString *key, id value) {
CFPreferencesSetAppValue((CFStringRef)key, (__bridge CFPropertyListRef _Nullable)(value), (CFStringRef)domain);
CFPreferencesAppSynchronize((CFStringRef)domain);
}


Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@
* limitations under the License.
*/

//
// OnOffViewController.h
// CHIPTool
//
// Created by Bhaskar on 6/1/20.
// Copyright © 2020 CHIP. All rights reserved.
//

#import "CHIPViewControllerBase.h"
#import <UIKit/UIKit.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@
* limitations under the License.
*/

//
// OnOffViewController.m
// CHIPTool
//
// Created by Bhaskar on 6/1/20.
// Copyright © 2020 CHIP. All rights reserved.
//

#import "OnOffViewController.h"
#import <CHIP/CHIP.h>

Expand Down
59 changes: 56 additions & 3 deletions src/darwin/CHIPTool/CHIPTool/QRCode/QRCodeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

// local imports
#import <CHIP/CHIP.h>
#import "DefaultsUtils.h"

// system imports
#import <AVFoundation/AVFoundation.h>
Expand Down Expand Up @@ -163,6 +164,58 @@ - (void)showPayload:(CHIPSetupPayload *)payload decimalString:(nullable NSString
[self handleRendezVous:payload];
}

- (void)retrieveWifiCredentialsAndRendezVous:(NSString *)name
{
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Wifi Configuration"
message: @"Input network SSID and password that your phone is connected to."
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Network SSID";
textField.textColor = [UIColor whiteColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;

NSString *networkSSID = CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey);
if ([networkSSID length] > 0) {
textField.text = networkSSID;
}
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
[textField setSecureTextEntry:YES];
textField.placeholder = @"Password";
textField.textColor = [UIColor whiteColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.secureTextEntry = YES;

NSString *networkPassword = CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkPasswordDefaultsKey);
if ([networkPassword length] > 0) {
textField.text = networkPassword;
}
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}]];

__weak typeof(self) weakSelf = self;
[alertController addAction:[UIAlertAction actionWithTitle:@"Rendez Vous" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
typeof(self) strongSelf = weakSelf;
if (strongSelf) {
NSArray * textfields = alertController.textFields;
UITextField * networkSSID = textfields[0];
UITextField * networkPassword = textfields[1];
if ([networkSSID.text length] > 0) {
CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey, networkSSID.text);
}

if ([networkPassword.text length] > 0) {
CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkPasswordDefaultsKey, networkPassword.text);
}
NSLog(@"New SSID: %@ Password: %@", networkSSID.text, networkPassword.text);
[strongSelf handleRendezVousBLE:name networkSSID:networkSSID.text networkPassword:networkPassword.text];
}
}]];
[self presentViewController:alertController animated:YES completion:nil];
}

- (void)updateUIFields:(CHIPSetupPayload *)payload decimalString:(nullable NSString *)decimalString
{
if (decimalString) {
Expand Down Expand Up @@ -237,8 +290,8 @@ - (void)handleRendezVous:(CHIPSetupPayload *)payload
[self handleRendezVousWiFi:[self getNetworkName:payload.discriminator]];
break;
case kRendezvousInformationBLE:
NSLog(@"Rendezvous BLE");
[self handleRendezVousBLE:[self getNetworkName:payload.discriminator]];
NSLog(@"Rendezvous BLE, retrieving wifi credentials");
[self retrieveWifiCredentialsAndRendezVous:[self getNetworkName:payload.discriminator]];
break;
}
}
Expand All @@ -250,7 +303,7 @@ - (NSString *)getNetworkName:(NSNumber *)discriminator
return peripheralFullName;
}

- (void)handleRendezVousBLE:(NSString *)name
- (void)handleRendezVousBLE:(NSString *)name networkSSID:(NSString *)networkSSID networkPassword:(NSString *)networkPassword
{
NSError * error;
[self.chipController connect:name error:&error];
Expand Down
Loading

0 comments on commit 2754297

Please sign in to comment.