Skip to content

Commit

Permalink
Merge pull request #907 from shana-apple/sazria/TLV-906
Browse files Browse the repository at this point in the history
Add TLV implementation in the Darwin CHIP framework
  • Loading branch information
saurabhst authored Jun 1, 2020
2 parents 43cb72c + e2bdbec commit 18335ba
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 15 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
36 changes: 28 additions & 8 deletions src/darwin/CHIPTool/CHIPTool/UI/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="jdn-ck-R8w">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="jdn-ck-R8w">
<device id="retina5_9" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
Expand All @@ -19,7 +19,7 @@
<rect key="frame" x="0.0" y="209" width="375" height="356"/>
<subviews>
<button opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="2Mj-J7-ksb">
<rect key="frame" x="304.66666666666669" y="15" width="45" height="34"/>
<rect key="frame" x="305" y="15" width="45" height="34"/>
<color key="backgroundColor" systemColor="systemBlueColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="width" constant="45" id="Ebl-wZ-SPm"/>
Expand All @@ -35,7 +35,7 @@
</connections>
</button>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Manual Code" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="CuJ-0W-LIn">
<rect key="frame" x="38.999999999999986" y="15" width="255.66666666666663" height="34"/>
<rect key="frame" x="39" y="15" width="256" height="34"/>
<color key="backgroundColor" white="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
Expand All @@ -61,7 +61,7 @@
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="YKC-8r-kzn">
<rect key="frame" x="312" y="110" width="30" height="23"/>
<rect key="frame" x="312.66666666666669" y="110" width="29.333333333333314" height="23"/>
<state key="normal" image="camera.fill" catalog="system">
<preferredSymbolConfiguration key="preferredSymbolConfiguration" scale="large"/>
</state>
Expand All @@ -70,7 +70,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="eNw-2a-OUY">
<rect key="frame" x="299" y="110" width="56" height="23"/>
<rect key="frame" x="299.33333333333331" y="110" width="56" height="23"/>
<constraints>
<constraint firstAttribute="height" constant="23" id="xVp-Aa-IeI"/>
</constraints>
Expand Down Expand Up @@ -184,11 +184,26 @@
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="00000000000000000000" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="faq-rn-i79">
<rect key="frame" x="124.66666666666669" y="-11" width="225" height="20.333333333333332"/>
<rect key="frame" x="125" y="-11" width="225" height="20.333333333333332"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="17"/>
<color key="textColor" systemColor="systemBlueColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="serial #" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1iE-d7-szF">
<rect key="frame" x="294" y="79" width="56" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" systemColor="systemBlueColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="N/A" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="FMS-vV-ZwI">
<rect key="frame" x="203" y="105.66666666666669" width="147" height="20.333333333333329"/>
<constraints>
<constraint firstAttribute="width" constant="147" id="iGr-vF-KJR"/>
</constraints>
<fontDescription key="fontDescription" type="italicSystem" pointSize="17"/>
<color key="textColor" systemColor="systemBlueColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
Expand All @@ -209,6 +224,7 @@
<constraint firstAttribute="trailing" secondItem="rNL-CF-kwb" secondAttribute="trailing" constant="22" id="b9Q-dH-RMA"/>
<constraint firstItem="A6N-re-z72" firstAttribute="leading" secondItem="qzu-Wd-XtE" secondAttribute="leading" id="bsV-pY-Ywq"/>
<constraint firstItem="XVG-Ph-rHO" firstAttribute="firstBaseline" secondItem="ml9-Kz-In1" secondAttribute="baseline" constant="24" symbolType="layoutAnchor" id="cMw-wo-i90"/>
<constraint firstItem="FMS-vV-ZwI" firstAttribute="centerY" secondItem="fjy-2o-l8j" secondAttribute="centerY" id="cyE-R2-IX7"/>
<constraint firstItem="fjy-2o-l8j" firstAttribute="firstBaseline" secondItem="qzu-Wd-XtE" secondAttribute="baseline" constant="24" symbolType="layoutAnchor" id="dAU-ik-gFV"/>
<constraint firstAttribute="trailing" secondItem="pcj-Jl-2Wz" secondAttribute="trailing" constant="22" id="eFk-HZ-y7o"/>
<constraint firstItem="HiF-tu-Xki" firstAttribute="firstBaseline" secondItem="eQW-ha-eiK" secondAttribute="baseline" constant="24" symbolType="layoutAnchor" id="eco-wR-pHG"/>
Expand All @@ -217,8 +233,9 @@
<constraint firstAttribute="trailing" secondItem="HiF-tu-Xki" secondAttribute="trailing" constant="22" id="hZO-IV-4Zn"/>
<constraint firstAttribute="trailing" secondItem="CGg-gC-ek0" secondAttribute="trailing" constant="22" id="jsn-SE-Yi2"/>
<constraint firstItem="CGg-gC-ek0" firstAttribute="firstBaseline" secondItem="A6N-re-z72" secondAttribute="baseline" constant="24" symbolType="layoutAnchor" id="lSd-xx-xJ1"/>
<constraint firstAttribute="trailing" secondItem="XVG-Ph-rHO" secondAttribute="trailing" constant="22" id="lyB-Lk-lva"/>
<constraint firstItem="rNL-CF-kwb" firstAttribute="leading" secondItem="zTo-M0-YqB" secondAttribute="leading" id="rD6-rx-hbC"/>
<constraint firstAttribute="trailing" secondItem="XVG-Ph-rHO" secondAttribute="trailing" constant="22" id="tO2-Q4-Vwx"/>
<constraint firstItem="1iE-d7-szF" firstAttribute="centerY" secondItem="qzu-Wd-XtE" secondAttribute="centerY" id="tSS-RD-sD4"/>
<constraint firstItem="zTo-M0-YqB" firstAttribute="top" secondItem="0zj-2E-c4c" secondAttribute="bottom" constant="45" id="xvF-cz-TQK"/>
<constraint firstItem="0zj-2E-c4c" firstAttribute="top" secondItem="A6N-re-z72" secondAttribute="bottom" constant="45" id="xwu-AA-8lr"/>
<constraint firstAttribute="height" constant="420" id="zrG-5a-d69"/>
Expand Down Expand Up @@ -250,6 +267,7 @@
<constraint firstItem="w8i-8R-6Fk" firstAttribute="firstBaseline" secondItem="faq-rn-i79" secondAttribute="baseline" constant="24" symbolType="layoutAnchor" id="QQY-lR-jPw"/>
<constraint firstItem="pR3-Sf-AVi" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="QUe-xX-RNy"/>
<constraint firstItem="eNw-2a-OUY" firstAttribute="centerY" secondItem="YKC-8r-kzn" secondAttribute="centerY" id="SBy-oE-nCI"/>
<constraint firstItem="1iE-d7-szF" firstAttribute="trailing" secondItem="2Mj-J7-ksb" secondAttribute="trailing" id="Tf8-Tj-d4W"/>
<constraint firstItem="faq-rn-i79" firstAttribute="trailing" secondItem="2Mj-J7-ksb" secondAttribute="trailing" id="Vj8-pF-sq6"/>
<constraint firstItem="bp0-Ss-koQ" firstAttribute="top" secondItem="CuJ-0W-LIn" secondAttribute="bottom" constant="30" id="Wln-do-7iB"/>
<constraint firstItem="YKC-8r-kzn" firstAttribute="centerY" secondItem="cyT-BB-1AD" secondAttribute="centerY" constant="1.5" id="YtM-ga-TqJ"/>
Expand All @@ -259,6 +277,7 @@
<constraint firstItem="cyT-BB-1AD" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="40" id="ncT-ZV-lhY"/>
<constraint firstItem="cyT-BB-1AD" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="34" id="oHh-t9-dHE"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="YKC-8r-kzn" secondAttribute="trailing" constant="33" id="oYR-cy-bjg"/>
<constraint firstItem="FMS-vV-ZwI" firstAttribute="trailing" secondItem="2Mj-J7-ksb" secondAttribute="trailing" id="quC-I6-RaK"/>
<constraint firstItem="rTH-Nc-7Dl" firstAttribute="centerX" secondItem="bp0-Ss-koQ" secondAttribute="centerX" id="yBP-Ef-Vwy"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
Expand All @@ -276,6 +295,7 @@
<outlet property="qrCodeButton" destination="YKC-8r-kzn" id="nDk-hl-Frp"/>
<outlet property="qrCodeViewPreview" destination="pR3-Sf-AVi" id="qop-Kq-984"/>
<outlet property="rendezVousInformation" destination="pcj-Jl-2Wz" id="P5x-8A-zpx"/>
<outlet property="serialNumber" destination="FMS-vV-ZwI" id="XBs-kO-3PM"/>
<outlet property="setupPayloadView" destination="bp0-Ss-koQ" id="gDL-dz-Neu"/>
<outlet property="setupPinCodeLabel" destination="CGg-gC-ek0" id="3jg-7C-I3f"/>
<outlet property="vendorID" destination="rNL-CF-kwb" id="01c-FS-zhC"/>
Expand All @@ -284,7 +304,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="41" y="-284"/>
<point key="canvasLocation" x="40.799999999999997" y="-284.48275862068965"/>
</scene>
<!--Echo-->
<scene sceneID="qLg-El-QgV">
Expand Down
1 change: 1 addition & 0 deletions src/darwin/CHIPTool/CHIPTool/UI/QRCodeViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
@property (weak, nonatomic) IBOutlet UILabel * rendezVousInformation;
@property (weak, nonatomic) IBOutlet UILabel * vendorID;
@property (weak, nonatomic) IBOutlet UILabel * productID;
@property (weak, nonatomic) IBOutlet UILabel * serialNumber;

- (IBAction)startScanningQRCode:(id)sender;
- (IBAction)stopScanningQRCode:(id)sender;
Expand Down
44 changes: 42 additions & 2 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 @@ -139,7 +145,7 @@ - (void)showPayload:(CHIPSetupPayload *)payload decimalString:(nullable NSString
self->_discriminatorLabel.text = [NSString stringWithFormat:@"%@", payload.discriminator];
self->_setupPinCodeLabel.text = [NSString stringWithFormat:@"%@", payload.setUpPINCode];
self->_rendezVousInformation.text = NOT_APPLICABLE_STRING;

self->_serialNumber.text = NOT_APPLICABLE_STRING;
// TODO: Only display vid and pid if present
self->_vendorID.text = [NSString stringWithFormat:@"%@", payload.vendorID];
self->_productID.text = [NSString stringWithFormat:@"%@", payload.productID];
Expand All @@ -149,12 +155,46 @@ - (void)showPayload:(CHIPSetupPayload *)payload decimalString:(nullable NSString
self->_discriminatorLabel.text = [NSString stringWithFormat:@"%@", payload.discriminator];
self->_setupPinCodeLabel.text = [NSString stringWithFormat:@"%@", payload.setUpPINCode];
self->_rendezVousInformation.text = [NSString stringWithFormat:@"%@", payload.rendezvousInformation];

if ([payload.serialNumber length] > 0) {
self->_serialNumber.text = payload.serialNumber;
} else {
self->_serialNumber.text = NOT_APPLICABLE_STRING;
}
// TODO: Only display vid and pid if present
self->_vendorID.text = [NSString stringWithFormat:@"%@", payload.vendorID];
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
3 changes: 2 additions & 1 deletion src/darwin/Framework/CHIP/CHIPError.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ typedef NS_ERROR_ENUM(CHIPErrorDomain, CHIPErrorCode) {
CHIPErrorCodeInvalidArgument = 3,
CHIPErrorCodeInvalidMessageLength = 4,
CHIPErrorCodeInvalidState = 5,
CHIPERRORCodeWrongAddressType = 6,
CHIPErrorCodeWrongAddressType = 6,
CHIPErrorCodeIntegrityCheckFailed = 7,
};

@interface CHIPError : NSObject
Expand Down
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/CHIPError.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ + (NSError *)errorForCHIPErrorCode:(CHIP_ERROR)errorCode
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeInvalidState
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Invalid object state.", nil) }];
case CHIP_ERROR_INTEGRITY_CHECK_FAILED:
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeIntegrityCheckFailed
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Integrity check failed.", nil) }];
default:
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeUndefinedError
Expand Down
Loading

0 comments on commit 18335ba

Please sign in to comment.