[Darwin] Implemented download diagnostics log for MTRDevice_XPC (#35998)
* [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
* Restyler fixes
* Added documentation
* Restyle fixes
diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm
index 61aad70..aefa2ff 100644
--- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm
+++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm
@@ -269,6 +269,39 @@
}
}
+- (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 36e2be5..9ecec60 100644
--- a/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h
+++ b/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h
@@ -41,6 +41,13 @@
- (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
+/* Note: The consumer of the completion block should move the file that the url points to or open it for reading before the
+ * completion handler returns. Otherwise, the file will be deleted, and the data will be lost.
+ */
+- (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