Skip to content

Commit

Permalink
fix the darwin side
Browse files Browse the repository at this point in the history
  • Loading branch information
nivi-apple committed Oct 14, 2023
1 parent ac33998 commit 4ea2b81
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 39 deletions.
17 changes: 11 additions & 6 deletions src/app/clusters/diagnostic-logs-server/diagnostic-logs-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ static_assert(kDiagnosticLogsDelegateTableSize < kEmberInvalidEndpointIndex, "Di
// -----------------------------------------------------------------------------
// Delegate Implementation

using ::Delegate;
//

namespace {

Delegate * gDelegateTable[kDiagnosticLogsDelegateTableSize] = { nullptr };


Delegate * GetDelegate(EndpointId endpoint)
{
uint16_t ep = emberAfGetClusterServerEndpointIndex(endpoint, Id,
Expand Down Expand Up @@ -87,6 +88,15 @@ void SetDefaultDelegate(EndpointId endpoint, Delegate * delegate)
gDelegateTable[ep] = delegate;
}
}

} // namespace DiagnosticLogs
} // namespace Clusters
} // namespace app
} // namespace chip

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters::DiagnosticLogs;

DiagnosticLogsServer DiagnosticLogsServer::mInstance;

Expand Down Expand Up @@ -295,11 +305,6 @@ void DiagnosticLogsServer::InvokeCommand(HandlerContext & handlerContext)
});
}

} // namespace DiagnosticLogs
} // namespace Clusters
} // namespace app
} // namespace chip

bool emberAfDiagnosticLogsClusterRetrieveLogsRequestCallback(
CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
const Commands::RetrieveLogsRequest::DecodableType & commandData)
Expand Down
18 changes: 9 additions & 9 deletions src/app/clusters/diagnostic-logs-server/diagnostic-logs-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ namespace app {
namespace Clusters {
namespace DiagnosticLogs {

/**
* Set the default delegate of the diagnostic logs cluster for the specified endpoint
*
* @param endpoint ID of the endpoint
*
* @param delegate The default delegate at the endpoint
*/
void SetDefaultDelegate(EndpointId endpoint, Delegate * delegate);

/// A reference implementation for DiagnosticLogs source.
class DiagnosticLogsServer : public CommandHandlerInterface
{
Expand All @@ -52,15 +61,6 @@ class DiagnosticLogsServer : public CommandHandlerInterface
CHIP_ERROR Init();
void Shutdown();

/**
* Set the default delegate of the diagnostic logs cluster for the specified endpoint
*
* @param endpoint ID of the endpoint
*
* @param delegate The default delegate at the endpoint
*/
void SetDefaultDelegate(EndpointId endpoint, Delegate * delegate);

// Inherited from CommandHandlerInterface
void InvokeCommand(HandlerContext & handlerContext) override;

Expand Down
5 changes: 2 additions & 3 deletions src/darwin/Framework/CHIP/MTRDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ typedef NS_ENUM(NSInteger, MTRDiagnosticLogType) {
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;

MTR_PROVISIONALLY_AVAILABLE
@interface MTRDiagnosticLogResult : NSObject

/**
Expand Down Expand Up @@ -252,7 +251,7 @@ 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;
completion:(void (^)(MTRDiagnosticLogResult * _Nullable logResult, NSError * error))completion;

@end

Expand Down
40 changes: 20 additions & 20 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#import "MTRBaseDevice_Internal.h"
#import "MTRBaseSubscriptionCallback.h"
#import "MTRCluster.h"
#import "MTRClusters.h"
#import "MTRClusters_Internal.h"
#import "MTRClusterConstants.h"
#import "MTRDeviceController_Internal.h"
#import "MTRDevice_Internal.h"
Expand Down Expand Up @@ -1140,17 +1142,17 @@ - (void)openCommissioningWindowWithDiscriminator:(NSNumber *)discriminator
[baseDevice openCommissioningWindowWithDiscriminator:discriminator duration:duration queue:queue completion:completion];
}

- (NSString*)getFileDesignatorForLogType(NSInteger logType)
/*- (NSString*)getFileDesignatorForLogType(MTRDiagnosticLogType type)
{
NSString *fileDesignator = [NSString stringWithFormat:@"%@%i", @"/tmp/", self.nodeID];
switch (logType)
switch (type)
{
case MTRDiagnosticLogTypeEndUserSupport:
return @"/tmp/endusersupport.log
case MTRDiagnosticLogTypeNetworkDiagnostics:
case MTRDiagnosticLogTypeCrash:
}
}
return fileDesignator;
}*/


// Once the consumer calls this API-
Expand All @@ -1170,9 +1172,8 @@ - (void)_downloadLogOfType:(MTRDiagnosticLogType)type
queue:(dispatch_queue_t)queue
completion:(void (^)(MTRDiagnosticLogResult * _Nullable logResult, NSError * error))completion
{
if (type != MTRDiagnosticLogTypeEndUserSupport && type != MTRDiagnosticLogTypeEndUserSupport && type != MTRDiagnosticLogTypeCrash)
if (type != MTRDiagnosticLogTypeEndUserSupport && type != MTRDiagnosticLogTypeNetworkDiagnostics && type != MTRDiagnosticLogTypeCrash)
{
MTRDiagnosticLogResult logResult = [[MTRDiagnosticLogResult alloc] init];
NSError * error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeInvalidArgument userInfo:nil];
completion(nil, error);
return;
Expand All @@ -1181,35 +1182,34 @@ - (void)_downloadLogOfType:(MTRDiagnosticLogType)type
// 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];
// 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];
MTRClusterDiagnosticLogs * cluster = [[MTRClusterDiagnosticLogs alloc] initWithDevice:self endpointID:@(0) queue:queue];

if (cluster == nil)
{
NSError * error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeInvalidState userInfo:nil];
completion(error, nil);
completion(nil, error);
return;
}

auto * commandParams = [[MTRDiagnosticLogsClusterRetrieveLogsRequestParams alloc] init];
commandParams.intent = logType;
commandParams.requestedProtocol = chip::app::Clusters::DiagnosticLogs::TransferProtocolEnum::kBdx;
commandParams.transferFileDesignator =
MTRDiagnosticLogsClusterRetrieveLogsRequestParams * requestParams = [[MTRDiagnosticLogsClusterRetrieveLogsRequestParams alloc] init];
requestParams.intent = [NSNumber numberWithInt:type];
requestParams.requestedProtocol = [NSNumber numberWithUnsignedChar:chip::to_underlying(chip::app::Clusters::DiagnosticLogs::TransferProtocolEnum::kBdx)];
requestParams.transferFileDesignator = @"bdx_file";

[cluster retrieveLogsRequestWithParams:commandParams completion:(void (^)(MTRDiagnosticLogsClusterRetrieveLogsResponseParams * _Nullable data, NSError * _Nullable error)) {
};

[cluster retrieveLogsRequestWithParams:requestParams completion:^(MTRDiagnosticLogsClusterRetrieveLogsResponseParams * _Nullable data, NSError * _Nullable error) {
if (error) {
completion(nil, error);
}
}];
}

- (void)downloadLogOfType:(MTRDiagnosticLogType)type
Expand Down
78 changes: 78 additions & 0 deletions src/darwin/Framework/CHIP/MTRDiagnosticLogsTransferHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <protocols/bdx/TransferFacilitator.h>

//#include <lib/core/OTAImageHeader.h>
//#include <platform/CHIPDeviceLayer.h>
//#include <platform/OTAImageProcessor.h>

/**
* This class inherits from the AsyncResponder class and handles the BDX messages for a BDX transfer session.
* It overrides the HandleTransferSessionOutput virtual method and provides an implementation for it to handle
* the OutputEvents that are generated by the BDX transfer session state machine.
*
* An MTRDiagnosticLogsTransferHandler will be associated with a specific BDX transfer session.
*
* The lifecycle of this class is managed by the AsyncFacilitator class which calls the virtual CleanUp method
* that is implemented by this class to clean up and destroy itself and the AsyncFacilitator instances.
* Note: An object of this class can't be used after CleanUp has been called.
*/
class MTRDiagnosticLogsTransferHandler : public chip::bdx::Responder
{
public:
MTRDiagnosticLogsTransferHandler();
~MTRDiagnosticLogsTransferHandler();

void HandleTransferSessionOutput(chip::bdx::TransferSession::OutputEvent & event) override;

void CleanUp();

protected:
CHIP_ERROR OnMessageReceived(chip::Messaging::ExchangeContext * ec, const chip::PayloadHeader & payloadHeader,
chip::System::PacketBufferHandle && payload) override;

private:
CHIP_ERROR PrepareForTransfer(chip::System::Layer * layer, chip::FabricIndex fabricIndex, chip::NodeId nodeId);

CHIP_ERROR ConfigureState(chip::FabricIndex fabricIndex, chip::NodeId nodeId);

CHIP_ERROR OnMessageToSend(chip::bdx::TransferSession::OutputEvent & event);

CHIP_ERROR OnTransferSessionBegin(chip::bdx::TransferSession::OutputEvent & event);

CHIP_ERROR OnTransferSessionEnd(chip::bdx::TransferSession::OutputEvent & event);

CHIP_ERROR OnBlockReceived(chip::bdx::TransferSession::OutputEvent & event);

CHIP_ERROR OnUnsolicitedMessageReceived(
const chip::PayloadHeader & payloadHeader, chip::Messaging::ExchangeDelegate * _Nonnull & newDelegate);

// The fabric index of the node with which the BDX session is established.
chip::Optional<chip::FabricIndex> mFabricIndex;

// The node id of the node with which the BDX session is established.
chip::Optional<chip::NodeId> mNodeId;

// write to a file temporarily
// std::ofstream mOfs;
//MutableByteSpan mBlock;
//OTAImageHeaderParser mHeaderParser;

uint64_t downloadedBytes = 0;
uint64_t totalFileBytes = 0;
};
Loading

0 comments on commit 4ea2b81

Please sign in to comment.