Skip to content

Commit 7f32ff8

Browse files
Darwin: Add MTRCommissioningParameters.readEndpointInformation (project-chip#37118)
* Darwin: Tidy up some device type meta-data classes - Move MTRDeviceTypeRevision out of ServerEndpoint directory - Move MTRProductIdentity into its own file - Implement NSCopying and equality on MTRDeviceType and MTRProductIdentity - Implement description on all 3 types - Add tests * Darwin: Add MTRCommissioningParameters.readEndpointInformation Endpoint information is made availalable to the delegate via an MTRCommissioneeInfo object containing MTREndpointInfo objects. * Apply suggestions from code review Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Use NSString literal for MTRDeviceTypeData * Process endpoints in best-effort fashion even with invalid / missing DeviceTypeList Also add some additional comments to parsing logic and a couple more tests. * Address further review comments for Darwin layer * Move MTRCommissioneeInfo into its own set of files * Make MTRCommissioneeInfo and related types conform to NSSecureCoding Also conform to NSCopying, mark as sendable, and implement isEqual/hash. * Fix terminology a bit. --------- Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent 2376bdd commit 7f32ff8

32 files changed

+1543
-188
lines changed

src/darwin/Framework/CHIP/MTRAttributeTLVValueDecoder_Internal.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
/*
2-
*
3-
* Copyright (c) 2021 Project CHIP Authors
1+
/**
2+
* Copyright (c) 2021-2024 Project CHIP Authors
43
* All rights reserved.
54
*
65
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,17 +15,21 @@
1615
* limitations under the License.
1716
*/
1817

19-
#pragma once
20-
2118
#import <Foundation/Foundation.h>
2219

20+
#include <app/ClusterStateCache.h>
2321
#include <app/ConcreteAttributePath.h>
2422
#include <lib/core/CHIPError.h>
2523
#include <lib/core/TLV.h>
2624

2725
NS_ASSUME_NONNULL_BEGIN
2826

27+
// Decodes an attribute value TLV into a typed ObjC value (see MTRStructsObjc.h)
2928
id _Nullable MTRDecodeAttributeValue(const chip::app::ConcreteAttributePath & aPath, chip::TLV::TLVReader & aReader,
3029
CHIP_ERROR * aError);
3130

31+
// Wrapper around the precending function that reads the attribute from a ClusterStateCache.
32+
id _Nullable MTRDecodeAttributeValue(const chip::app::ConcreteAttributePath & aPath, const chip::app::ClusterStateCache & aCache,
33+
CHIP_ERROR * aError);
34+
3235
NS_ASSUME_NONNULL_END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
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 "MTRAttributeTLVValueDecoder_Internal.h"
19+
20+
NS_ASSUME_NONNULL_BEGIN
21+
22+
using namespace chip;
23+
24+
id _Nullable MTRDecodeAttributeValue(const chip::app::ConcreteAttributePath & aPath,
25+
const chip::app::ClusterStateCache & aCache,
26+
CHIP_ERROR * aError)
27+
{
28+
TLV::TLVReader reader;
29+
*aError = aCache.Get(aPath, reader);
30+
VerifyOrReturnValue(*aError == CHIP_NO_ERROR, nil);
31+
return MTRDecodeAttributeValue(aPath, reader, aError);
32+
}
33+
34+
NS_ASSUME_NONNULL_END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) 2024 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Matter/MTRDefines.h>
18+
19+
@class MTRProductIdentity;
20+
@class MTREndpointInfo;
21+
22+
NS_ASSUME_NONNULL_BEGIN
23+
24+
/**
25+
* Information read from the commissionee device during commissioning.
26+
*/
27+
NS_SWIFT_SENDABLE
28+
MTR_NEWLY_AVAILABLE
29+
@interface MTRCommissioneeInfo : NSObject <NSCopying, NSSecureCoding>
30+
31+
/**
32+
* The product identity (VID / PID) of the commissionee.
33+
*/
34+
@property (nonatomic, copy, readonly) MTRProductIdentity * productIdentity;
35+
36+
/**
37+
* Endpoint information for all endpoints of the commissionee.
38+
* Will be present only if readEndpointInformation is set to YES on MTRCommissioningParameters.
39+
*
40+
* Use `rootEndpoint` and `-[MTREndpointInfo children]` to traverse endpoints in composition order.
41+
*/
42+
@property (nonatomic, copy, readonly, nullable) NSDictionary<NSNumber *, MTREndpointInfo *> * endpointsById;
43+
44+
/**
45+
* Endpoint information for the root endpoint of the commissionee.
46+
* Will be present only if readEndpointInformation is set to YES on MTRCommissioningParameters.
47+
*/
48+
@property (nonatomic, copy, readonly, nullable) MTREndpointInfo * rootEndpoint;
49+
50+
@end
51+
52+
NS_ASSUME_NONNULL_END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Copyright (c) 2024 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "MTRCommissioneeInfo_Internal.h"
18+
19+
#import "MTRDefines_Internal.h"
20+
#import "MTREndpointInfo_Internal.h"
21+
#import "MTRProductIdentity.h"
22+
#import "MTRUtilities.h"
23+
24+
NS_ASSUME_NONNULL_BEGIN
25+
26+
MTR_DIRECT_MEMBERS
27+
@implementation MTRCommissioneeInfo
28+
29+
- (instancetype)initWithCommissioningInfo:(const chip::Controller::ReadCommissioningInfo &)info
30+
{
31+
self = [super init];
32+
_productIdentity = [[MTRProductIdentity alloc] initWithVendorID:@(info.basic.vendorId) productID:@(info.basic.productId)];
33+
34+
// TODO: We should probably hold onto our MTRCommissioningParameters so we can look at `readEndpointInformation`
35+
// instead of just reading whatever Descriptor cluster information happens to be in the cache.
36+
auto * endpoints = [MTREndpointInfo endpointsFromAttributeCache:info.attributes];
37+
if (endpoints.count > 0) {
38+
_endpointsById = endpoints;
39+
}
40+
41+
return self;
42+
}
43+
44+
static NSString * const sProductIdentityCodingKey = @"pi";
45+
static NSString * const sEndpointsCodingKey = @"ep";
46+
47+
- (nullable instancetype)initWithCoder:(NSCoder *)coder
48+
{
49+
self = [super init];
50+
_productIdentity = [coder decodeObjectOfClass:MTRProductIdentity.class forKey:sProductIdentityCodingKey];
51+
VerifyOrReturnValue(_productIdentity != nil, nil);
52+
_endpointsById = [coder decodeDictionaryWithKeysOfClass:NSNumber.class
53+
objectsOfClass:MTREndpointInfo.class
54+
forKey:sEndpointsCodingKey];
55+
return self;
56+
}
57+
58+
- (void)encodeWithCoder:(NSCoder *)coder
59+
{
60+
[coder encodeObject:_productIdentity forKey:sProductIdentityCodingKey];
61+
[coder encodeObject:_endpointsById forKey:sEndpointsCodingKey];
62+
}
63+
64+
+ (BOOL)supportsSecureCoding
65+
{
66+
return YES;
67+
}
68+
69+
- (NSUInteger)hash
70+
{
71+
return _productIdentity.hash;
72+
}
73+
74+
- (BOOL)isEqual:(id)object
75+
{
76+
VerifyOrReturnValue([object class] == [self class], NO);
77+
MTRCommissioneeInfo * other = object;
78+
VerifyOrReturnValue(MTREqualObjects(_productIdentity, other->_productIdentity), NO);
79+
VerifyOrReturnValue(MTREqualObjects(_endpointsById, other->_endpointsById), NO);
80+
return YES;
81+
}
82+
83+
- (id)copyWithZone:(nullable NSZone *)zone
84+
{
85+
return self; // immutable
86+
}
87+
88+
- (nullable MTREndpointInfo *)rootEndpoint
89+
{
90+
return self.endpointsById[@0];
91+
}
92+
93+
@end
94+
95+
NS_ASSUME_NONNULL_END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) 2024 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Matter/MTRCommissioneeInfo.h>
18+
19+
#import "MTRDefines_Internal.h"
20+
21+
#include <controller/CommissioningDelegate.h>
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
MTR_DIRECT_MEMBERS
26+
@interface MTRCommissioneeInfo ()
27+
28+
- (instancetype)initWithCommissioningInfo:(const chip::Controller::ReadCommissioningInfo &)info;
29+
30+
@end
31+
32+
NS_ASSUME_NONNULL_END

src/darwin/Framework/CHIP/MTRCommissioningParameters.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
2-
*
3-
* Copyright (c) 2022-2023 Project CHIP Authors
2+
* Copyright (c) 2022-2024 Project CHIP Authors
43
*
54
* Licensed under the Apache License, Version 2.0 (the "License");
65
* you may not use this file except in compliance with the License.
@@ -97,6 +96,12 @@ MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
9796
*/
9897
@property (nonatomic, copy, nullable) NSString * countryCode MTR_AVAILABLE(ios(17.0), macos(14.0), watchos(10.0), tvos(17.0));
9998

99+
/**
100+
* Read device type information from all endpoints during commissioning.
101+
* Defaults to NO.
102+
*/
103+
@property (nonatomic, assign) BOOL readEndpointInformation MTR_NEWLY_AVAILABLE;
104+
100105
@end
101106

102107
@interface MTRCommissioningParameters (Deprecated)

src/darwin/Framework/CHIP/MTRDefines_Internal.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2023 Project CHIP Authors
2+
* Copyright (c) 2023-2024 Project CHIP Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,8 +30,12 @@
3030

3131
#ifdef DEBUG
3232
#define MTR_TESTABLE MTR_EXPORT
33+
#define MTR_TESTABLE_DIRECT
34+
#define MTR_TESTABLE_DIRECT_MEMBERS
3335
#else
3436
#define MTR_TESTABLE
37+
#define MTR_TESTABLE_DIRECT MTR_DIRECT
38+
#define MTR_TESTABLE_DIRECT_MEMBERS MTR_DIRECT_MEMBERS
3539
#endif
3640

3741
// clang-format off

src/darwin/Framework/CHIP/MTRDeviceController.mm

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
2-
*
3-
* Copyright (c) 2020-2023 Project CHIP Authors
2+
* Copyright (c) 2020-2024 Project CHIP Authors
43
*
54
* Licensed under the Apache License, Version 2.0 (the "License");
65
* you may not use this file except in compliance with the License.
@@ -656,11 +655,13 @@ - (void)controller:(MTRDeviceController *)controller
656655
} logString:__PRETTY_FUNCTION__];
657656
}
658657

659-
- (void)controller:(MTRDeviceController *)controller readCommissioningInfo:(MTRProductIdentity *)info
658+
- (void)controller:(MTRDeviceController *)controller readCommissioneeInfo:(MTRCommissioneeInfo *)info
660659
{
661660
[self _callDelegatesWithBlock:^(id<MTRDeviceControllerDelegate> delegate) {
662-
if ([delegate respondsToSelector:@selector(controller:readCommissioningInfo:)]) {
663-
[delegate controller:controller readCommissioningInfo:info];
661+
if ([delegate respondsToSelector:@selector(controller:readCommissioneeInfo:)]) {
662+
[delegate controller:controller readCommissioneeInfo:info];
663+
} else if ([delegate respondsToSelector:@selector(controller:readCommissioningInfo:)]) {
664+
[delegate controller:controller readCommissioningInfo:info.productIdentity];
664665
}
665666
} logString:__PRETTY_FUNCTION__];
666667
}

src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h

+16-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
2-
*
3-
* Copyright (c) 2020-2023 Project CHIP Authors
2+
* Copyright (c) 2020-2024 Project CHIP Authors
43
*
54
* Licensed under the Apache License, Version 2.0 (the "License");
65
* you may not use this file except in compliance with the License.
@@ -20,6 +19,13 @@
2019

2120
NS_ASSUME_NONNULL_BEGIN
2221

22+
@class MTRCommissioneeInfo;
23+
@class MTRDeviceController;
24+
@class MTRDeviceTypeRevision;
25+
@class MTREndpointInfo;
26+
@class MTRMetrics;
27+
@class MTRProductIdentity;
28+
2329
typedef NS_ENUM(NSInteger, MTRCommissioningStatus) {
2430
MTRCommissioningStatusUnknown = 0,
2531
MTRCommissioningStatusSuccess = 1,
@@ -29,22 +35,6 @@ typedef NS_ENUM(NSInteger, MTRCommissioningStatus) {
2935
= 3,
3036
} MTR_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4));
3137

32-
/**
33-
* A representation of a (vendor, product) pair that identifies a specific product.
34-
*/
35-
MTR_AVAILABLE(ios(17.0), macos(14.0), watchos(10.0), tvos(17.0))
36-
@interface MTRProductIdentity : NSObject
37-
38-
@property (nonatomic, copy, readonly) NSNumber * vendorID;
39-
40-
@property (nonatomic, copy, readonly) NSNumber * productID;
41-
42-
- (instancetype)initWithVendorID:(NSNumber *)vendorID productID:(NSNumber *)productID;
43-
@end
44-
45-
@class MTRDeviceController;
46-
@class MTRMetrics;
47-
4838
/**
4939
* The protocol definition for the MTRDeviceControllerDelegate.
5040
*
@@ -98,14 +88,18 @@ MTR_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
9888
metrics:(MTRMetrics *)metrics MTR_AVAILABLE(ios(17.6), macos(14.6), watchos(10.6), tvos(17.6));
9989

10090
/**
101-
* Notify the delegate when commissioning infomation has been read from the Basic
102-
* Information cluster of the commissionee.
91+
* Notify the delegate when commissioning infomation has been read from the commissionee.
10392
*
104-
* At the point when this notification happens, device attestation has not been performed yet,
93+
* Note that this notification happens before device attestation is performed,
10594
* so the information delivered by this notification should not be trusted.
10695
*/
10796
- (void)controller:(MTRDeviceController *)controller
108-
readCommissioningInfo:(MTRProductIdentity *)info MTR_AVAILABLE(ios(17.0), macos(14.0), watchos(10.0), tvos(17.0));
97+
readCommissioneeInfo:(MTRCommissioneeInfo *)info MTR_NEWLY_AVAILABLE;
98+
99+
- (void)controller:(MTRDeviceController *)controller
100+
readCommissioningInfo:(MTRProductIdentity *)info
101+
MTR_AVAILABLE(ios(17.0), macos(14.0), watchos(10.0), tvos(17.0))
102+
MTR_NEWLY_DEPRECATED("Use controller:readCommissioneeInfo:");
109103

110104
/**
111105
* Notify the delegate when the suspended state changed of the controller, after this happens

0 commit comments

Comments
 (0)