Skip to content

Commit

Permalink
Implement MTRBaseCluster reads/subscribes on top of MTRBaseDevice. (#…
Browse files Browse the repository at this point in the history
…29557)

This significantly reduces the size of the framework and speeds up compilation of MTRBaseClusters.
  • Loading branch information
bzbarsky-apple authored Oct 12, 2023
1 parent 9555e32 commit ffd1571
Show file tree
Hide file tree
Showing 15 changed files with 31,043 additions and 93,665 deletions.
266 changes: 0 additions & 266 deletions src/darwin/Framework/CHIP/MTRBaseClusterUtils.h

This file was deleted.

92 changes: 92 additions & 0 deletions src/darwin/Framework/CHIP/MTRBaseDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,54 @@ - (void)readAttributesWithEndpointID:(NSNumber * _Nullable)endpointID
[self readAttributePaths:attributePaths eventPaths:nil params:params queue:queue completion:completion];
}

- (void)_readKnownAttributeWithEndpointID:(NSNumber *)endpointID
clusterID:(NSNumber *)clusterID
attributeID:(NSNumber *)attributeID
params:(MTRReadParams * _Nullable)params
queue:(dispatch_queue_t)queue
completion:(void (^)(id _Nullable value, NSError * _Nullable error))completion
{
auto * attributePath = [MTRAttributePath attributePathWithEndpointID:endpointID clusterID:clusterID attributeID:attributeID];

auto innerCompletion = ^(NSArray<NSDictionary<NSString *, id> *> * _Nullable values, NSError * _Nullable error) {
if (error != nil) {
completion(nil, error);
return;
}

// Preserving the old behavior: we don't fail on multiple reports, but
// just report the first one.
if (values.count == 0) {
completion(nil, [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeSchemaMismatch userInfo:nil]);
return;
}

NSDictionary<NSString *, id> * value = values[0];
NSError * initError;
auto * report = [[MTRAttributeReport alloc] initWithResponseValue:value error:&initError];
if (initError != nil) {
completion(nil, initError);
return;
}

if (![report.path isEqual:attributePath]) {
// For some reason the server returned data for the wrong
// attribute, even though it happened to decode to our type.
completion(nil, [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeSchemaMismatch userInfo:nil]);
return;
}

completion(report.value, report.error);
};

[self readAttributesWithEndpointID:endpointID
clusterID:clusterID
attributeID:attributeID
params:params
queue:queue
completion:innerCompletion];
}

- (void)readAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullable)attributePaths
eventPaths:(NSArray<MTREventRequestPath *> * _Nullable)eventPaths
params:(MTRReadParams * _Nullable)params
Expand Down Expand Up @@ -1420,6 +1468,50 @@ - (void)subscribeToAttributesWithEndpointID:(NSNumber * _Nullable)endpointID
resubscriptionScheduled:nil];
}

- (void)_subscribeToKnownAttributeWithEndpointID:(NSNumber *)endpointID
clusterID:(NSNumber *)clusterID
attributeID:(NSNumber *)attributeID
params:(MTRSubscribeParams *)params
queue:(dispatch_queue_t)queue
reportHandler:(void (^)(id _Nullable value, NSError * _Nullable error))reportHandler
subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptionEstablished
{
auto * attributePath = [MTRAttributePath attributePathWithEndpointID:endpointID clusterID:clusterID attributeID:attributeID];

auto innerReportHandler = ^(NSArray<NSDictionary<NSString *, id> *> * _Nullable values, NSError * _Nullable error) {
if (error != nil) {
reportHandler(nil, error);
return;
}

for (NSDictionary<NSString *, id> * value in values) {
NSError * initError;
auto * report = [[MTRAttributeReport alloc] initWithResponseValue:value error:&initError];
if (initError != nil) {
reportHandler(nil, initError);
continue;
}

if (![report.path isEqual:attributePath]) {
// For some reason the server returned data for the wrong
// attribute, even though it happened to decode to our type.
reportHandler(nil, [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeSchemaMismatch userInfo:nil]);
continue;
}

reportHandler(report.value, report.error);
}
};

[self subscribeToAttributesWithEndpointID:endpointID
clusterID:clusterID
attributeID:attributeID
params:params
queue:queue
reportHandler:innerReportHandler
subscriptionEstablished:subscriptionEstablished];
}

- (void)subscribeToAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullable)attributePaths
eventPaths:(NSArray<MTREventRequestPath *> * _Nullable)eventPaths
params:(MTRSubscribeParams * _Nullable)params
Expand Down
Loading

0 comments on commit ffd1571

Please sign in to comment.