Skip to content

Commit

Permalink
made changes to darwin side
Browse files Browse the repository at this point in the history
  • Loading branch information
nivi-apple committed Oct 13, 2023
1 parent 0c9d730 commit ac33998
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 120 deletions.
86 changes: 0 additions & 86 deletions src/darwin/Framework/CHIP/MTRBaseDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1772,92 +1772,6 @@ - (void)timerFire:(NSTimer *)timer
[timer invalidate];
}

- (NSString*)getFileDesignatorForLogType(NSInteger logType)
{
NSString *fileDesignator = [NSString stringWithFormat:@"%@%i", @"/tmp/", self.nodeID];
switch (logType)
{
case MTRDiagnosticLogTypeEndUserSupport:
return @"/tmp/endusersupport.log
case MTRDiagnosticLogTypeNetworkDiagnostics:
case MTRDiagnosticLogTypeCrash:
}
}
// Once the consumer calls this API-
// a) if types are not valid, return an error. no logs
// b) start a timer with the timeout if provided.
// do the following until all requested log types are retrieved
// c) For each type requested in the bitmap, send a command to the diagnostic logs cluster with protocol BDX/intent type - log type
// d) when timeout expires, call the completion with what has been received in filepath array so far. set the timeout expired bool in the response
// e) if one file type was requested, call completion and send the filepath otherwise save the filepath in an array.
// f) if an error occurs or BDX times out and there are no more pending log files, call the completion with what has been received in filepath array so far.
// g) if more than one file types was requested, keep track of pending request and repeat c-> f
// h) if an error occurs or BDX times out and there are more pending requests, move on to the next log type.
// h) when all requested logs have been retreived successfully or an error/timeout occured send the array of filepaths
- (void)_downloadDiagnosticLogsOfTypes:(NSArray<NSNumber *> *)types
timeout:(NSTimeInterval)timeout
queue:(dispatch_queue_t)queue
completion:(void (^)(NSError * _Nullable error, MTRDiagnosticLogResult * _Nullable logResult))completion
{
for (NSNumber * type in types) {
NSInteger typeValue = [type intValue];
if (typeValue != MTRDiagnosticLogTypeEndUserSupport && typeValue != MTRDiagnosticLogTypeEndUserSupport && typeValue != MTRDiagnosticLogTypeCrash)
{
MTRDiagnosticLogResult logResult = [[MTRDiagnosticLogResult alloc] init];
logResult.error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeInvalidArgument userInfo:nil];
completion(logResult);
return;
}
}
// Start a timer for the timeout and abort transfer and return the log result
if (timeout > 0)
{
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(timerFire:) userInfo:nil repeats:NO];
}
// Get the first type from the array. if any of the types are invalid should we return no logs.
__block NSMutableArray<NSNumber *> * logTypes = [NSMutableArray arrayWithArray:types];
NSInteger logType = logTypes[0];
// Create a handler for BDX and start listening for sendinit
// send a command with the intent type mapping the values
// remove the object at index 0.
// completiion handler
// TODO: fix the endpoint id part of the code. Take in input
MTRBaseClusterDiagnosticLogs * cluster = [[MTRBaseClusterDiagnosticLogs alloc] initWithDevice:self endpointID:@(0) queue:queue];
if (cluster == nil)
{
NSError * error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeInvalidState userInfo:nil];
completion(error, nil);
return;
}
auto * commandParams = [[MTRDiagnosticLogsClusterRetrieveLogsRequestParams alloc] init];
commandParams.intent = logType;
commandParams.requestedProtocol = chip::app::Clusters::DiagnosticLogs::TransferProtocolEnum::kBdx;
commandParams.transferFileDesignator =
[cluster retrieveLogsRequestWithParams:commandParams completion:(void (^)(MTRDiagnosticLogsClusterRetrieveLogsResponseParams * _Nullable data, NSError * _Nullable error)) {
};
}
- (void)downloadDiagnosticLogsOfTypes:(NSArray<NSNumber *> *)types
timeout:(NSTimeInterval)timeout
queue:(dispatch_queue_t)queue
completion:(void (^)(NSError * _Nullable error, MTRDiagnosticLogResult * _Nullable logResult))completion
{
[self _downloadDiagnosticLogsOfTypes:types
timeout:timeout
queue:queue
completion:completion];
}
#ifdef DEBUG
// This method is for unit testing only
- (void)failSubscribers:(dispatch_queue_t)queue completion:(void (^)(void))completion
Expand Down
49 changes: 21 additions & 28 deletions src/darwin/Framework/CHIP/MTRDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ typedef NS_ENUM(NSUInteger, MTRDeviceState) {
};

/**
* This enum is used to specify the type of logs requested from this device.
* This enum is used to specify the type of log requested from this device.
*
* The logs types are : End User Support, Network Diagnostics and Crash logs.
* The log types are : End User Support, Network Diagnostics and Crash logs.
*/
typedef NS_ENUM(NSInteger, MTRDiagnosticLogType) {
MTRDiagnosticLogTypeEndUserSupport = 0, // End user support logs are requested
MTRDiagnosticLogTypeNetworkDiagnostics = 1, // Network Diagnostics logs are requested
MTRDiagnosticLogTypeCrash = 2 // Crash logs are requested
MTRDiagnosticLogTypeEndUserSupport = 0, // End user support log is requested
MTRDiagnosticLogTypeNetworkDiagnostics = 1, // Network Diagnostics log is requested
MTRDiagnosticLogTypeCrash = 2 // Crash log is requested
} MTR_PROVISIONALLY_AVAILABLE;

@protocol MTRDeviceDelegate;
Expand All @@ -47,27 +47,21 @@ MTR_PROVISIONALLY_AVAILABLE

/**
* The URL representing the location of the end user support log file that was downloaded.
* If no end user support log was requested or retreived, this will be set to nil.
* If no end user support log was requested or retrieved, this will be set to nil.
*/
@property (readonly, nonatomic, nullable) NSURL *endUserSupportLog;
@property (readonly, nonatomic, nullable) NSURL * endUserSupportLog;

/**
* The URL representing the location of the network diagnostics log file that was downloaded.
* If no network diagnostics log was requested or retreived, this will be set to nil.
*/
@property (readonly, nonatomic, nullable) NSURL *networkDiagnosticsLog;
@property (readonly, nonatomic, nullable) NSURL * networkDiagnosticsLog;

/**
* The URL representing the location of the crash log file that was downloaded.
* If no crash log was requested or retrieved, this will be set to nil.
*/
@property (readonly, nonatomic, nullable) NSURL *crashLog;

/**
* If an error occured while retreiving the logs, this will be set to the error.
* if no error occurs, this will be set to nil.
*/
@property (readonly, nonatomic, nullable) NSError *error;
@property (readonly, nonatomic, nullable) NSURL * crashLog;

@end

Expand Down Expand Up @@ -242,24 +236,23 @@ MTR_PROVISIONALLY_AVAILABLE
MTR_AVAILABLE(ios(17.0), macos(14.0), watchos(10.0), tvos(17.0));

/**
* Requests diagnostic logs of the desired types from the device.
* Download log of the desired type from the device.
*
* Note: The consumer of this API should move the files that the NSURLs in the MTRDiagnosticLogResult point to
* or open it for reading before the completion handler returns. Otherwise, the files will be deleted, and the data will be lost.
* Note: The consumer of this API should move the file that the NSURL in the MTRDiagnosticLogResult points to
* or open it for reading before the completion handler returns. Otherwise, the file will be deleted, and the data will be lost.
*
* @param types The types of logs being requested. This should correspond to values in the enum MTRDiagnosticLogType.
* @param timeout The timeout for getting the logs. If the timeout expires, completion
* will be called with whatever logs have been retrieved by that point
* (which might be none, and might include partial logs).
* @param type The type of log being requested. This should correspond to a value in the enum MTRDiagnosticLogType.
* @param timeout The timeout for getting the log. If the timeout expires, completion will be called with whatever
* has been retrieved by that point (which might be none or a partial log).
* If the timeout is set to 0, the request will not expire and completion will not be called until
* the logs are fully retrieved or an error occurs.
* the log is fully retrieved or an error occurs.
* @param queue The queue on which completion will be called.
* @param completion The completion that will be called to pass in the file paths for the requested logs.
* @param completion The completion that will be called to pass in the URL for the requested log.
*/
- (void)downloadDiagnosticLogsOfTypes:(NSArray<NSNumber *> *)types
timeout:(NSTimeInterval)timeout
queue:(dispatch_queue_t)queue
completion:(void (^)(MTRDiagnosticLogResult * _Nullable logResult))completion MTR_PROVISIONALLY_AVAILABLE;
- (void)downloadLogOfType:(MTRDiagnosticLogType)type
timeout:(NSTimeInterval)timeout
queue:(dispatch_queue_t)queue
completion:(void (^)(MTRDiagnosticLogResult * _Nullable logResult, NSError * error))completion MTR_PROVISIONALLY_AVAILABLE;

@end

Expand Down
83 changes: 77 additions & 6 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1140,13 +1140,84 @@ - (void)openCommissioningWindowWithDiscriminator:(NSNumber *)discriminator
[baseDevice openCommissioningWindowWithDiscriminator:discriminator duration:duration queue:queue completion:completion];
}

- (void)downloadDiagnosticLogsOfTypes:(NSArray<NSNumber *> *)types
timeout:(NSTimeInterval)timeout
queue:(dispatch_queue_t)queue
completion:(void (^)(MTRDiagnosticLogResult * _Nullable logResult))completion
- (NSString*)getFileDesignatorForLogType(NSInteger logType)
{
auto * baseDevice = [self newBaseDevice];
[baseDevice downloadDiagnosticLogsOfTypes:types timeout:timeout queue:queue completion:completion];
NSString *fileDesignator = [NSString stringWithFormat:@"%@%i", @"/tmp/", self.nodeID];
switch (logType)
{
case MTRDiagnosticLogTypeEndUserSupport:
return @"/tmp/endusersupport.log
case MTRDiagnosticLogTypeNetworkDiagnostics:
case MTRDiagnosticLogTypeCrash:
}
}
// Once the consumer calls this API-
// a) if types are not valid, return an error. no logs
// b) start a timer with the timeout if provided.
// do the following until all requested log types are retrieved
// c) For each type requested in the bitmap, send a command to the diagnostic logs cluster with protocol BDX/intent type - log type
// d) when timeout expires, call the completion with what has been received in filepath array so far. set the timeout expired bool in the response
// e) if one file type was requested, call completion and send the filepath otherwise save the filepath in an array.
// f) if an error occurs or BDX times out and there are no more pending log files, call the completion with what has been received in filepath array so far.
// g) if more than one file types was requested, keep track of pending request and repeat c-> f
// h) if an error occurs or BDX times out and there are more pending requests, move on to the next log type.
// h) when all requested logs have been retreived successfully or an error/timeout occured send the array of filepaths
- (void)_downloadLogOfType:(MTRDiagnosticLogType)type
timeout:(NSTimeInterval)timeout
queue:(dispatch_queue_t)queue
completion:(void (^)(MTRDiagnosticLogResult * _Nullable logResult, NSError * error))completion
{
if (type != MTRDiagnosticLogTypeEndUserSupport && type != MTRDiagnosticLogTypeEndUserSupport && type != MTRDiagnosticLogTypeCrash)
{
MTRDiagnosticLogResult logResult = [[MTRDiagnosticLogResult alloc] init];
NSError * error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeInvalidArgument userInfo:nil];
completion(nil, error);
return;
}
// Start a timer for the timeout and abort transfer and return the log result
if (timeout > 0)
{
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(timerFire:) userInfo:nil repeats:NO];
}
// Get the first type from the array. if any of the types are invalid should we return no logs.
__block NSMutableArray<NSNumber *> * logTypes = [NSMutableArray arrayWithArray:types];
NSInteger logType = logTypes[0];
// Create a handler for BDX and start listening for sendinit
// send a command with the intent type mapping the values
// remove the object at index 0.
// completiion handler
// TODO: fix the endpoint id part of the code. Take in input
MTRBaseClusterDiagnosticLogs * cluster = [[MTRBaseClusterDiagnosticLogs alloc] initWithDevice:self endpointID:@(0) queue:queue];
if (cluster == nil)
{
NSError * error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeInvalidState userInfo:nil];
completion(error, nil);
return;
}
auto * commandParams = [[MTRDiagnosticLogsClusterRetrieveLogsRequestParams alloc] init];
commandParams.intent = logType;
commandParams.requestedProtocol = chip::app::Clusters::DiagnosticLogs::TransferProtocolEnum::kBdx;
commandParams.transferFileDesignator =
[cluster retrieveLogsRequestWithParams:commandParams completion:(void (^)(MTRDiagnosticLogsClusterRetrieveLogsResponseParams * _Nullable data, NSError * _Nullable error)) {
};
}
- (void)downloadLogOfType:(MTRDiagnosticLogType)type
timeout:(NSTimeInterval)timeout
queue:(dispatch_queue_t)queue
completion:(void (^)(MTRDiagnosticLogResult * _Nullable logResult, NSError * error))completion
{
[self _downloadLogOfType:type timeout:timeout queue:queue completion:completion];
}
#pragma mark - Cache management
Expand Down

0 comments on commit ac33998

Please sign in to comment.