From 96eae59ebee77acc3bec756056db61403c318760 Mon Sep 17 00:00:00 2001 From: Anush Nadathur Date: Wed, 9 Oct 2024 11:15:06 -0700 Subject: [PATCH] [Darwin] Implemented download diagnostics log for MTRDevice_XPC - Implemented the downloadLogOfType API on MTRDevice_XPC to call into XPC server to forward the request - Fixed the protocol to take controllerID which is missing from existing protocol --- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 33 +++++++++++++++++++ .../CHIP/XPC Protocol/MTRXPCServerProtocol.h | 4 +++ 2 files changed, 37 insertions(+) diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index 61aad700353f08..590d16155b4f6b 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -269,6 +269,39 @@ - (void)_invokeCommandWithEndpointID:(NSNumber *)endpointID } } +- (void)downloadLogOfType:(MTRDiagnosticLogType)type + timeout:(NSTimeInterval)timeout + queue:(dispatch_queue_t)queue + completion:(void (^)(NSURL * _Nullable url, NSError * _Nullable error))completion +{ + NSXPCConnection * xpcConnection = [(MTRDeviceController_XPC *) [self deviceController] xpcConnection]; + + @try { + [[xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { + MTR_LOG_ERROR("Error: %@", error); + dispatch_async(queue, ^{ + completion(nil, [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeGeneralError userInfo:nil]); + }); + }] deviceController:[[self deviceController] uniqueIdentifier] + nodeID:[self nodeID] + downloadLogOfType:type + timeout:timeout + completion:^(NSURL * _Nullable url, NSError * _Nullable error) { + dispatch_async(queue, ^{ + completion(url, error); + if (url) { + [[NSFileManager defaultManager] removeItemAtPath:url.path error:nil]; + } + }); + }]; + } @catch (NSException * exception) { + MTR_LOG_ERROR("Exception sending XPC messsage: %@", exception); + dispatch_async(queue, ^{ + completion(nil, [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeGeneralError userInfo:nil]); + }); + } +} + // Not Supported via XPC //- (oneway void)deviceController:(NSUUID *)controller nodeID:(NSNumber *)nodeID openCommissioningWindowWithSetupPasscode:(NSNumber *)setupPasscode discriminator:(NSNumber *)discriminator duration:(NSNumber *)duration completion:(MTRDeviceOpenCommissioningWindowHandler)completion; //- (oneway void)deviceController:(NSUUID *)controller nodeID:(NSNumber *)nodeID openCommissioningWindowWithDiscriminator:(NSNumber *)discriminator duration:(NSNumber *)duration completion:(MTRDeviceOpenCommissioningWindowHandler)completion; diff --git a/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h b/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h index b404056c960a79..2cad22205dbad7 100644 --- a/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h +++ b/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h @@ -37,6 +37,10 @@ MTR_NEWLY_AVAILABLE - (oneway void)deviceController:(NSUUID *)controller nodeID:(NSNumber *)nodeID openCommissioningWindowWithSetupPasscode:(NSNumber *)setupPasscode discriminator:(NSNumber *)discriminator duration:(NSNumber *)duration completion:(MTRDeviceOpenCommissioningWindowHandler)completion; - (oneway void)downloadLogOfType:(MTRDiagnosticLogType)type nodeID:(NSNumber *)nodeID timeout:(NSTimeInterval)timeout completion:(void (^)(NSURL * _Nullable url, NSError * _Nullable error))completion; + +@optional +- (oneway void)deviceController:(NSUUID *)controller nodeID:(NSNumber *)nodeID downloadLogOfType:(MTRDiagnosticLogType)type timeout:(NSTimeInterval)timeout completion:(void (^)(NSURL * _Nullable url, NSError * _Nullable error))completion; + @end MTR_NEWLY_AVAILABLE