Skip to content

Commit

Permalink
Detect a Demo QRCode and show a dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
sagar-apple committed May 30, 2020
1 parent 46c6567 commit 50b8bf3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
11 changes: 8 additions & 3 deletions examples/wifi-echo/server/esp32/main/QRCodeWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
#include "QRCodeWidget.h"

#define MAX_SSID_LEN 32
// A temporary value assigned for this example's QRCode
// Spells CHIP on a dialer
#define EXAMPLE_VENDOR_ID 3447
// Used to indicate that an SSID has been added to the QRCode
#define EXAMPLE_VENDOR_TAG 1

#if CONFIG_HAVE_DISPLAY

Expand Down Expand Up @@ -76,10 +81,10 @@ string createSetupPayload()
GetAPName(ap_ssid, sizeof(ap_ssid));
SetupPayload payload;
payload.version = 1;
payload.vendorID = 2;
payload.productID = 3;
payload.vendorID = EXAMPLE_VENDOR_ID;
payload.productID = 1;
uint64_t tag;
VendorTag(2, tag);
VendorTag(EXAMPLE_VENDOR_TAG, tag);
OptionalQRCodeInfo stringInfo;
stringInfo.tag = tag;
stringInfo.type = optionalQRCodeInfoTypeString;
Expand Down
4 changes: 2 additions & 2 deletions src/darwin/CHIPTool/CHIPTool.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
};
};
};
buildConfigurationList = B204A617244E1D0600C7C0E1 /* Build configuration list for PBXProject "chiptool" */;
buildConfigurationList = B204A617244E1D0600C7C0E1 /* Build configuration list for PBXProject "CHIPTool" */;
compatibilityVersion = "Xcode 9.3";
developmentRegion = en;
hasScannedForEncodings = 0;
Expand Down Expand Up @@ -476,7 +476,7 @@
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
B204A617244E1D0600C7C0E1 /* Build configuration list for PBXProject "chiptool" */ = {
B204A617244E1D0600C7C0E1 /* Build configuration list for PBXProject "CHIPTool" */ = {
isa = XCConfigurationList;
buildConfigurations = (
B204A649244E1D0700C7C0E1 /* Debug iOS */,
Expand Down
36 changes: 36 additions & 0 deletions src/darwin/CHIPTool/CHIPTool/UI/QRCodeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
#define ERROR_DISPLAY_TIME 2.0 * NSEC_PER_SEC
#define QR_CODE_FREEZE 1.0 * NSEC_PER_SEC

// The expected Vendor ID for CHIP demos
// Spells CHIP on a dialer
#define EXAMPLE_VENDOR_ID 3447
#define EXAMPLE_VENDOR_TAG 1
#define MAX_SSID_LEN 32

#define NOT_APPLICABLE_STRING @"N/A"

@interface QRCodeViewController ()
Expand Down Expand Up @@ -159,6 +165,36 @@ - (void)showPayload:(CHIPSetupPayload *)payload decimalString:(nullable NSString
self->_productID.text = [NSString stringWithFormat:@"%@", payload.productID];
}
self->_setupPayloadView.hidden = NO;

if ([payload.vendorID isEqualToNumber:[NSNumber numberWithInt:EXAMPLE_VENDOR_ID]]) {
NSArray * optionalInfo = [payload getAllOptionalData:nil];
for (CHIPOptionalQRCodeInfo * info in optionalInfo) {
NSNumber * tag = [CHIPSetupPayload vendorTag:info.tag error:nil];
if (tag && tag.intValue == EXAMPLE_VENDOR_TAG) {
// If the vendor id and tag match the example values, there should be an ssid encoded
if ([info.infoType isEqualToNumber:[NSNumber numberWithInt:kOptionalQRCodeInfoTypeString]]) {
if ([info.stringValue length] > MAX_SSID_LEN) {
NSLog(@"Unexpected SSID String...");
} else {
// show SoftAP detection
[self RequestConnectSoftAPWithSSID:info.stringValue];
}
}
}
}
}
}


- (void)RequestConnectSoftAPWithSSID:(NSString*)ssid {
NSString* message = [NSString stringWithFormat:@"The scanned CHIP accessory supports a SoftAP.\n\nSSID: %@\n\nUse WiFi Settings to connect to it.", ssid];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"SoftAP Detected"
message:message
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel
handler:nil];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
}

// MARK: QR Code
Expand Down

0 comments on commit 50b8bf3

Please sign in to comment.