Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MTRBaseCluster reads/subscribes on top of MTRBaseDevice. #29557

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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