From c878aa971507e8d7ddb28aadf5f72bed6b71dd5c Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Fri, 1 Jul 2022 10:06:12 -0700 Subject: [PATCH] Removing old API --- .../TemperatureSensorViewController.m | 51 ++++++++++--------- src/darwin/Framework/CHIP/MTRDevice.h | 33 ------------ src/darwin/Framework/CHIP/MTRDevice.mm | 25 --------- .../Framework/CHIP/MTRDeviceController.h | 4 +- .../CHIP/MTRDeviceControllerOverXPC.m | 20 ++++---- src/darwin/Framework/CHIP/MTRDeviceOverXPC.m | 10 ++-- .../Framework/CHIPTests/MTRDeviceTests.m | 17 +++---- .../CHIPTests/MTRXPCListenerSampleTests.m | 22 +++++--- .../Framework/CHIPTests/MTRXPCProtocolTests.m | 12 +++-- 9 files changed, 74 insertions(+), 120 deletions(-) diff --git a/src/darwin/CHIPTool/CHIPTool/View Controllers/Temperature Sensor/TemperatureSensorViewController.m b/src/darwin/CHIPTool/CHIPTool/View Controllers/Temperature Sensor/TemperatureSensorViewController.m index 42e33875c420f9..29748052f34253 100644 --- a/src/darwin/CHIPTool/CHIPTool/View Controllers/Temperature Sensor/TemperatureSensorViewController.m +++ b/src/darwin/CHIPTool/CHIPTool/View Controllers/Temperature Sensor/TemperatureSensorViewController.m @@ -226,31 +226,34 @@ - (void)reportFromUserEnteredSettings maxInterval:maxIntervalSeconds params:nil cacheContainer:nil - reportHandler:^(NSArray * _Nullable reports, NSError * _Nullable error) { - if (error) { - NSLog(@"Status: update reportAttributeMeasuredValue completed with error %@", - [error description]); - return; - } - for (MTRAttributeReport * report in reports) { - // These should be exposed by the SDK - if ([report.path.cluster isEqualToNumber:@(MTRClusterTemperatureMeasurementID)] && - [report.path.attribute - isEqualToNumber:@(MTRClusterTemperatureMeasurementAttributeMeasuredValueID)]) { - if (report.error != nil) { - NSLog(@"Error reading temperature: %@", report.error); - } else { - __auto_type controller = [TemperatureSensorViewController currentController]; - if (controller != nil) { - [controller updateTempInUI:((NSNumber *) report.value).shortValue]; - } - } - } - } + attributeReportHandler:^(NSArray * _Nullable reports) { + if ( !reports ) + return; + for (MTRAttributeReport * report in reports) { + // These should be exposed by the SDK + if ([report.path.cluster isEqualToNumber:@(MTRClusterTemperatureMeasurementID)] && + [report.path.attribute + isEqualToNumber:@(MTRClusterTemperatureMeasurementAttributeMeasuredValueID)]) { + if (report.error != nil) { + NSLog(@"Error reading temperature: %@", report.error); + } else { + __auto_type controller = [TemperatureSensorViewController currentController]; + if (controller != nil) { + [controller updateTempInUI:((NSNumber *) report.value).shortValue]; + } + } + } + } + } + eventReportHandler:nil + errorHandler:^(NSError * error) { + NSLog(@"Status: update reportAttributeMeasuredValue completed with error %@", + [error description]); } - subscriptionEstablished:^ { - - }]; + subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler { if (error) { + NSLog(@"Status: update reportAttributeMeasuredValue completed"); + } + ]; } else { NSLog(@"Status: Failed to establish a connection with the device"); } diff --git a/src/darwin/Framework/CHIP/MTRDevice.h b/src/darwin/Framework/CHIP/MTRDevice.h index 33d40c3d5a90e5..2aedc1a9546bf7 100644 --- a/src/darwin/Framework/CHIP/MTRDevice.h +++ b/src/darwin/Framework/CHIP/MTRDevice.h @@ -98,39 +98,6 @@ extern NSString * const MTRArrayValueType; - (instancetype)init NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE; -/** - * Subscribe to receive attribute reports for everything (all endpoints, all - * clusters, all attributes, all events) on the device. - * - * A non-nil attribute cache container will cache attribute values, retrievable - * through the designated attribute cache container. - * - * reportHandler will be called any time a data update is available (with a - * non-nil "value" and nil "error"), or any time there is an error for the - * entire subscription (with a nil "value" and non-nil "error"). If it's called - * with an error, that will terminate the subscription. - * - * The array passed to reportHandler will contain MTRAttributeReport - * instances. Errors for specific paths, not the whole subscription, will be - * reported via those objects. - * - * reportHandler is not supported over XPC at the moment. - * - * subscriptionEstablished block, if not nil, will be called once the - * subscription is established. This will be _after_ the first (priming) call - * to reportHandler. Note that if the MTRSubscribeParams are set to - * automatically resubscribe this can end up being called more than once. - * - * TODO: Remove this once the replacement below is adopted - */ -- (void)subscribeWithQueue:(dispatch_queue_t)queue - minInterval:(uint16_t)minInterval - maxInterval:(uint16_t)maxInterval - params:(nullable MTRSubscribeParams *)params - cacheContainer:(MTRAttributeCacheContainer * _Nullable)attributeCacheContainer - reportHandler:(void (^)(NSArray * _Nullable value, NSError * _Nullable error))reportHandler - subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler; - /** * Subscribe to receive attribute reports for everything (all endpoints, all * clusters, all attributes, all events) on the device. diff --git a/src/darwin/Framework/CHIP/MTRDevice.mm b/src/darwin/Framework/CHIP/MTRDevice.mm index 4508cd13f359f9..434055aec82acf 100644 --- a/src/darwin/Framework/CHIP/MTRDevice.mm +++ b/src/darwin/Framework/CHIP/MTRDevice.mm @@ -336,31 +336,6 @@ - (instancetype)initWithDevice:(chip::DeviceProxy *)device } // anonymous namespace -- (void)subscribeWithQueue:(dispatch_queue_t)queue - minInterval:(uint16_t)minInterval - maxInterval:(uint16_t)maxInterval - params:(nullable MTRSubscribeParams *)params - cacheContainer:(MTRAttributeCacheContainer * _Nullable)attributeCacheContainer - reportHandler:(void (^)(NSArray * _Nullable value, NSError * _Nullable error))reportHandler - subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler -{ - [self subscribeWithQueue:queue - minInterval:minInterval - maxInterval:maxInterval - params:params - cacheContainer:attributeCacheContainer - attributeReportHandler:^(NSArray * _Nullable value) { - reportHandler(value, nil); - } - eventReportHandler:^(NSArray * _Nullable value) { - ; // do nothing - } - errorHandler:^(NSError * _Nullable error) { - reportHandler(nil, error); - } - subscriptionEstablished:subscriptionEstablishedHandler]; -} - - (void)subscribeWithQueue:(dispatch_queue_t)queue minInterval:(uint16_t)minInterval maxInterval:(uint16_t)maxInterval diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.h b/src/darwin/Framework/CHIP/MTRDeviceController.h index 2a949145469e96..829e80fe6fa5da 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.h +++ b/src/darwin/Framework/CHIP/MTRDeviceController.h @@ -96,9 +96,7 @@ typedef void (^MTRDeviceConnectionCallback)(MTRDevice * _Nullable device, NSErro - (BOOL)stopDevicePairing:(uint64_t)deviceID error:(NSError * __autoreleasing *)error; - (nullable MTRDevice *)getDeviceBeingCommissioned:(uint64_t)deviceId error:(NSError * __autoreleasing *)error; -- (BOOL)getConnectedDevice:(uint64_t)deviceID - queue:(dispatch_queue_t)queue - completionHandler:(MTRDeviceConnectionCallback)completionHandler; +- (BOOL)getDevice:(uint64_t)deviceID queue:(dispatch_queue_t)queue completionHandler:(MTRDeviceConnectionCallback)completionHandler; - (BOOL)openPairingWindow:(uint64_t)deviceID duration:(NSUInteger)duration error:(NSError * __autoreleasing *)error; - (nullable NSString *)openPairingWindowWithPIN:(uint64_t)deviceID diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerOverXPC.m b/src/darwin/Framework/CHIP/MTRDeviceControllerOverXPC.m index 9f3178384121c5..54d3f8bccf9852 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerOverXPC.m +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerOverXPC.m @@ -51,7 +51,7 @@ - (BOOL)pairDevice:(uint64_t)deviceID setupPINCode:(uint32_t)setupPINCode error:(NSError * __autoreleasing *)error { - CHIP_LOG_ERROR("CHIPDevice doesn't support pairDevice over XPC"); + CHIP_LOG_ERROR("MTRDevice doesn't support pairDevice over XPC"); return NO; } @@ -62,13 +62,13 @@ - (BOOL)pairDevice:(uint64_t)deviceID setupPINCode:(uint32_t)setupPINCode error:(NSError * __autoreleasing *)error { - CHIP_LOG_ERROR("CHIPDevice doesn't support pairDevice over XPC"); + CHIP_LOG_ERROR("MTRDevice doesn't support pairDevice over XPC"); return NO; } - (BOOL)pairDevice:(uint64_t)deviceID onboardingPayload:(NSString *)onboardingPayload error:(NSError * __autoreleasing *)error { - CHIP_LOG_ERROR("CHIPDevice doesn't support pairDevice over XPC"); + CHIP_LOG_ERROR("MTRDevice doesn't support pairDevice over XPC"); return NO; } @@ -76,30 +76,30 @@ - (BOOL)commissionDevice:(uint64_t)deviceId commissioningParams:(MTRCommissioningParameters *)commissioningParams error:(NSError * __autoreleasing *)error { - CHIP_LOG_ERROR("CHIPDevice doesn't support pairDevice over XPC"); + CHIP_LOG_ERROR("MTRDevice doesn't support pairDevice over XPC"); return NO; } - (void)setListenPort:(uint16_t)port { - CHIP_LOG_ERROR("CHIPDevice doesn't support setListenPort over XPC"); + CHIP_LOG_ERROR("MTRDevice doesn't support setListenPort over XPC"); } - (BOOL)stopDevicePairing:(uint64_t)deviceID error:(NSError * __autoreleasing *)error { - CHIP_LOG_ERROR("CHIPDevice doesn't support stopDevicePairing over XPC"); + CHIP_LOG_ERROR("MTRDevice doesn't support stopDevicePairing over XPC"); return NO; } - (nullable MTRDevice *)getDeviceBeingCommissioned:(uint64_t)deviceId error:(NSError * __autoreleasing *)error { - CHIP_LOG_ERROR("CHIPDevice doesn't support getDeviceBeingCommissioned over XPC"); + CHIP_LOG_ERROR("MTRDevice doesn't support getDeviceBeingCommissioned over XPC"); return nil; } - (BOOL)deviceBeingCommissionedOverBLE:(uint64_t)deviceId { - CHIP_LOG_ERROR("CHIPDevice doesn't support deviceBeingCommissionedOverBLE over XPC"); + CHIP_LOG_ERROR("MTRDevice doesn't support deviceBeingCommissionedOverBLE over XPC"); return NO; } @@ -146,7 +146,7 @@ - (BOOL)getConnectedDevice:(uint64_t)deviceID - (BOOL)openPairingWindow:(uint64_t)deviceID duration:(NSUInteger)duration error:(NSError * __autoreleasing *)error { - CHIP_LOG_ERROR("CHIPDevice doesn't support openPairingWindow over XPC"); + CHIP_LOG_ERROR("MTRDevice doesn't support openPairingWindow over XPC"); return NO; } @@ -156,7 +156,7 @@ - (nullable NSString *)openPairingWindowWithPIN:(uint64_t)deviceID setupPIN:(NSUInteger)setupPIN error:(NSError * __autoreleasing *)error { - CHIP_LOG_ERROR("CHIPDevice doesn't support openPairingWindow over XPC"); + CHIP_LOG_ERROR("MTRDevice doesn't support openPairingWindow over XPC"); return nil; } diff --git a/src/darwin/Framework/CHIP/MTRDeviceOverXPC.m b/src/darwin/Framework/CHIP/MTRDeviceOverXPC.m index 77382e02b53cd8..acb80e579ce14f 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceOverXPC.m +++ b/src/darwin/Framework/CHIP/MTRDeviceOverXPC.m @@ -51,8 +51,10 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue maxInterval:(uint16_t)maxInterval params:(nullable MTRSubscribeParams *)params cacheContainer:(MTRAttributeCacheContainer * _Nullable)attributeCacheContainer - reportHandler:(void (^)(NSArray * _Nullable value, NSError * _Nullable error))reportHandler - subscriptionEstablished:(void (^_Nullable)(void))subscriptionEstablishedHandler + attributeReportHandler:(void (^)(NSArray * value))attributeReportHandler + eventReportHandler:(void (^)(NSArray * value))eventReportHandler + errorHandler:(void (^)(NSError * error))errorHandler + subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler; { CHIP_LOG_DEBUG("Subscribing all attributes... Note that reportHandler is not supported."); if (attributeCacheContainer) { @@ -70,7 +72,7 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue completion:^(NSError * _Nullable error) { dispatch_async(queue, ^{ if (error) { - reportHandler(nil, error); + errorHandler(error); } else { subscriptionEstablishedHandler(); } @@ -81,7 +83,7 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue } else { CHIP_LOG_ERROR("Failed to obtain XPC connection to write attribute"); dispatch_async(queue, ^{ - reportHandler(nil, [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeGeneralError userInfo:nil]); + errorHandler([NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeGeneralError userInfo:nil]); }); } }]; diff --git a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m index b034dc1dc7f9a5..e95c19318dc121 100644 --- a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m +++ b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m @@ -708,22 +708,19 @@ - (void)test011_ReadCachedAttribute XCTestExpectation * subscribeExpectation = [self expectationWithDescription:@"Subscription complete"]; NSLog(@"Subscribing..."); - __block void (^reportHandler)(NSArray * _Nullable value, NSError * _Nullable error); [device subscribeWithQueue:queue minInterval:2 maxInterval:60 params:nil cacheContainer:attributeCacheContainer - reportHandler:^(NSArray * _Nullable value, NSError * _Nullable error) { - NSLog(@"Received report: %@, error: %@", value, error); - if (reportHandler) { - __auto_type handler = reportHandler; - reportHandler = nil; - handler(value, error); - } + attributeReportHandler:^(NSArray * value) { + NSLog(@"Received report: %@", value); } - subscriptionEstablished:^{ - NSLog(@"Subscription established"); + eventReportHandler:nil + errorHandler:^(NSError * error) { + NSLog(@"Received report error: %@", error); + } + subscriptionEstablished:^() { [subscribeExpectation fulfill]; }]; [self waitForExpectations:@[ subscribeExpectation ] timeout:60]; diff --git a/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m b/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m index c07279d89444f2..7f0c06028c59f7 100644 --- a/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m +++ b/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m @@ -378,14 +378,18 @@ - (void)subscribeWithController:(id _Nullable)controller maxInterval:[maxInterval unsignedShortValue] params:[MTRDeviceController decodeXPCSubscribeParams:params] cacheContainer:attributeCacheContainer - reportHandler:^(NSArray * _Nullable value, NSError * _Nullable error) { - NSLog(@"Report received: %@, error: %@", value, error); - if (error && ![established[0] boolValue]) { + attributeReportHandler:^(NSArray * value) { + NSLog(@"Received report: %@", value); + } + eventReportHandler:nil + errorHandler:^(NSError * error) { + NSLog(@"Received report error: %@", error); + if (![established[0] boolValue]) { established[0] = @YES; completion(error); } } - subscriptionEstablished:^{ + subscriptionEstablished:^() { NSLog(@"Attribute cache subscription succeeded for device %llu", nodeId); if (attributeCacheContainer) { [self.attributeCacheDictionary setObject:attributeCacheContainer forKey:@(nodeId)]; @@ -1783,10 +1787,14 @@ - (void)test900_SubscribeAttributeCache maxInterval:60 params:nil cacheContainer:attributeCacheContainer - reportHandler:^(NSArray * _Nullable value, NSError * _Nullable error) { - NSLog(@"Report for attribute cache: %@, error: %@", value, error); + attributeReportHandler:^(NSArray * value) { + NSLog(@"Report for attribute cache: %@", error); } - subscriptionEstablished:^{ + eventReportHandler:nil + errorHandler:^(NSError * error) { + NSLog(@"Received error for attribute cache: %@", error); + } + subscriptionEstablished:^() { NSLog(@"Attribute cache subscribed attributes"); [expectation fulfill]; }]; diff --git a/src/darwin/Framework/CHIPTests/MTRXPCProtocolTests.m b/src/darwin/Framework/CHIPTests/MTRXPCProtocolTests.m index ed9a7bfeee9d18..a7d92e3f2feddf 100644 --- a/src/darwin/Framework/CHIPTests/MTRXPCProtocolTests.m +++ b/src/darwin/Framework/CHIPTests/MTRXPCProtocolTests.m @@ -113,20 +113,24 @@ - (void)subscribeWithDeviceController:(MTRDeviceController *)deviceController maxInterval:43200 params:params cacheContainer:self - reportHandler:^(NSArray * _Nullable value, NSError * _Nullable error) { - NSLog(@"Report received for attribute cache: %@, error: %@", value, error); + attributeReportHandler:^(NSArray * value) { + NSLog(@"Report received for attribute cache: %@", value); + } + eventReportHandler:nil + errorHandler:^(NSError * error) { + NSLog(@"Report error received for attribute cache: %@", error); if (![established[0] boolValue]) { established[0] = @YES; completionHandler(error); } } - subscriptionEstablished:^{ + subscriptionEstablished:^() { NSLog(@"Attribute cache subscription succeeded for device %llu", deviceId); if (![established[0] boolValue]) { established[0] = @YES; completionHandler(nil); } - }]; + }]; }]; } @end