Skip to content

Commit

Permalink
write the block to file
Browse files Browse the repository at this point in the history
  • Loading branch information
nivi-apple committed Oct 17, 2023
1 parent ae33c61 commit fc4c26f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
9 changes: 3 additions & 6 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ - (void)_downloadLogOfType:(MTRDiagnosticLogType)type
return;
}

NSString *fileName = [NSString stringWithFormat:@"%@_%@", self.nodeID, [self toLogTypeString:type]];
NSString *fileName = [NSString stringWithFormat:@"%@_%@_%@", [NSDate date], self.nodeID, [self toLogTypeString:type]];

NSURL *filePath = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:fileName]];

Expand Down Expand Up @@ -1248,17 +1248,14 @@ - (void)_downloadLogOfType:(MTRDiagnosticLogType)type
completion(nil, error);
return;
}
// If the response has a log content, copy it into the temporary location and send the URL otherwise we will send the full file once BDX succeeds.
if (response != nil && response.logContent != nil)
{
if ([response.logContent writeToURL:filePath atomically:YES])
{
completion(filePath, nil);
} else {
NSError * error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeInvalidState userInfo:nil];
completion(nil, error);
return;
}

//completion(data, error);
}
}];
}
Expand Down
2 changes: 2 additions & 0 deletions src/darwin/Framework/CHIP/MTRDiagnosticLogsTransferHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class MTRDiagnosticLogsTransferHandler : public chip::bdx::Responder
//OTAImageHeaderParser mHeaderParser;
NSURL * _Nullable mFileURL;

NSFileHandle * mFileHandle;

uint64_t downloadedBytes = 0;
uint64_t totalFileBytes = 0;
};
35 changes: 25 additions & 10 deletions src/darwin/Framework/CHIP/MTRDiagnosticLogsTransferHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

#import "MTRDeviceControllerFactory_Internal.h"
#import "MTRDeviceController_Internal.h"
#import "NSStringSpanConversion.h"

#import "NSStringSpanConversion.h"
#import "NSDataSpanConversion.h"

#include "MTRDiagnosticLogsTransferHandler.h"
#include <MTRError_Internal.h>
Expand Down Expand Up @@ -71,11 +69,15 @@

VerifyOrReturnError(mFabricIndex.HasValue(), CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mNodeId.HasValue(), CHIP_ERROR_INCORRECT_STATE);
uint16_t fdl = 0;
const unsigned char * fd = mTransfer.GetFileDesignator(fdl);
NSError *error = nil;
NSFileHandle * mFileHandle = [NSFileHandle fileHandleForWritingToURL:mFileURL error:&error];

if (!mFileHandle)
{
// TODO: Map NSError to BDX error
LogErrorOnFailure(mTransfer.AbortTransfer(GetBdxStatusCodeFromChipError(CHIP_ERROR_INCORRECT_STATE)));
}

VerifyOrReturnError(fdl <= bdx::kMaxFileDesignatorLen, CHIP_ERROR_INVALID_ARGUMENT);

TransferSession::TransferAcceptData acceptData;
acceptData.ControlMode = bdx::TransferControlFlags::kSenderDrive;
acceptData.MaxBlockSize = mTransfer.GetTransferBlockSize();
Expand All @@ -102,6 +104,12 @@
}

// Notify the MTRDevice via the callback that the BDX transfer has completed and then return from there
// TODO: see if we can move to reset
if (mFileHandle)
{
[mFileHandle closeFile];
mFileHandle = nullptr;
}
Reset();
return CHIP_NO_ERROR;
}
Expand All @@ -113,13 +121,19 @@
VerifyOrReturnError(mFabricIndex.HasValue(), CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mNodeId.HasValue(), CHIP_ERROR_INCORRECT_STATE);

auto * controller = [[MTRDeviceControllerFactory sharedInstance] runningControllerForFabricIndex:mFabricIndex.Value()];
VerifyOrReturnError(controller != nil, CHIP_ERROR_INCORRECT_STATE);

chip::ByteSpan blockData(event.blockdata.Data, event.blockdata.Length);

// TODO: process the block. parse header if any. write to file
downloadedBytes += blockData.size();
if (mFileHandle)
{
[mFileHandle seekToEndOfFile];
NSError * error = nil;
if (![mFileHandle writeData:AsData(blockData) error:&error])
{
LogErrorOnFailure(mTransfer.AbortTransfer(GetBdxStatusCodeFromChipError(CHIP_ERROR_INCORRECT_STATE)));
}
}
ChipLogError(BDX, "Got block data of size %llu", downloadedBytes);
CHIP_ERROR err = mTransfer.PrepareBlockAck();
LogErrorOnFailure(err);
Expand Down Expand Up @@ -177,6 +191,7 @@

void MTRDiagnosticLogsTransferHandler::Reset() {
mFileURL = nullptr;

mFabricIndex.ClearValue();
mNodeId.ClearValue();
mTransfer.Reset();
Expand Down

0 comments on commit fc4c26f

Please sign in to comment.