Skip to content

Commit 5d42d92

Browse files
Add a helper method to get the descriptor cluster info from MTRDevice. (project-chip#36769)
1 parent 73cc4f1 commit 5d42d92

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/darwin/Framework/CHIP/MTRDevice.h

+9
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
221221
*/
222222
- (NSArray<NSDictionary<NSString *, id> *> *)readAttributePaths:(NSArray<MTRAttributeRequestPath *> *)attributePaths MTR_AVAILABLE(ios(18.2), macos(15.2), watchos(11.2), tvos(18.2));
223223

224+
/**
225+
* Read all known attributes from descriptor clusters on all known endpoints.
226+
*
227+
* @return A dictionary with the paths of the attributes as keys and the
228+
* data-values (as described in the documentation for
229+
* MTRDeviceResponseHandler) as values.
230+
*/
231+
- (NSDictionary<MTRAttributePath *, NSDictionary<NSString *, id> *> *)descriptorClusters MTR_NEWLY_AVAILABLE;
232+
224233
/**
225234
* Invoke a command with a designated command path
226235
*

src/darwin/Framework/CHIP/MTRDevice.mm

+26
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,32 @@ - (void)writeAttributeWithEndpointID:(NSNumber *)endpointID
363363
return [NSArray array];
364364
}
365365

366+
- (NSDictionary<MTRAttributePath *, NSDictionary<NSString *, id> *> *)descriptorClusters
367+
{
368+
@autoreleasepool {
369+
// For now, we have a temp array that we should make sure dies as soon
370+
// as possible.
371+
//
372+
// TODO: We should have a version of readAttributePaths that returns a
373+
// dictionary in the format we want here.
374+
auto path = [MTRAttributeRequestPath requestPathWithEndpointID:nil
375+
clusterID:@(MTRClusterIDTypeDescriptorID)
376+
attributeID:nil];
377+
auto * data = [self readAttributePaths:@[ path ]];
378+
379+
auto * retval = [NSMutableDictionary dictionaryWithCapacity:data.count];
380+
for (NSDictionary * item in data) {
381+
// We double-check that the things in our dictionaries are the right
382+
// thing, because XPC has no way to check that for us.
383+
if (MTR_SAFE_CAST(item[MTRAttributePathKey], MTRAttributePath) && MTR_SAFE_CAST(item[MTRDataKey], NSDictionary)) {
384+
retval[item[MTRAttributePathKey]] = item[MTRDataKey];
385+
}
386+
}
387+
388+
return retval;
389+
}
390+
}
391+
366392
- (void)invokeCommandWithEndpointID:(NSNumber *)endpointID
367393
clusterID:(NSNumber *)clusterID
368394
commandID:(NSNumber *)commandID

src/darwin/Framework/CHIPTests/MTRDeviceTests.m

+16
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,22 @@ - (void)test017_TestMTRDeviceBasics
15881588
XCTAssertEqualObjects([NSSet setWithArray:variousThings],
15891589
[[NSSet setWithArray:clusterRevisions] setByAddingObjectsFromSet:[NSSet setWithArray:basicInformationAllRootAttributes]]);
15901590

1591+
// Quick test for descriptorClusters
1592+
__auto_type * descriptorPath = [MTRAttributeRequestPath requestPathWithEndpointID:nil clusterID:@(MTRClusterIDTypeDescriptorID) attributeID:nil];
1593+
__auto_type * descriptorRead = [device descriptorClusters];
1594+
__auto_type * descriptorWildcardRead = [device readAttributePaths:@[ descriptorPath ]];
1595+
XCTAssertEqual(descriptorRead.count, descriptorWildcardRead.count);
1596+
for (MTRAttributePath * path in descriptorRead) {
1597+
__auto_type * expectedObj = @{
1598+
MTRAttributePathKey : path,
1599+
MTRDataKey : descriptorRead[path],
1600+
};
1601+
XCTAssertTrue([descriptorWildcardRead containsObject:expectedObj]);
1602+
}
1603+
for (NSDictionary * item in descriptorWildcardRead) {
1604+
XCTAssertEqualObjects(descriptorRead[item[MTRAttributePathKey]], item[MTRDataKey]);
1605+
}
1606+
15911607
// Some quick tests for waitForAttributeValues. First, values that we know
15921608
// are already there:
15931609
XCTestExpectation * deviceTypesWaitExpectation = [self expectationWithDescription:@"deviceTypes is already the value we expect"];

0 commit comments

Comments
 (0)