From 21ff6f738acb6a1ecfc89bbc81594da82eb3a9ff Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Fri, 19 Jan 2024 19:10:27 +0100 Subject: [PATCH] [darwin-framework-tool] Add bdx commands to darwin-framework-tool --- examples/darwin-framework-tool/BUILD.gn | 2 + .../commands/bdx/Commands.h | 32 ++++++++++ .../commands/bdx/DownloadLogCommand.h | 45 ++++++++++++++ .../commands/bdx/DownloadLogCommand.mm | 58 +++++++++++++++++++ examples/darwin-framework-tool/main.mm | 2 + .../Matter.xcodeproj/project.pbxproj | 20 +++++++ 6 files changed, 159 insertions(+) create mode 100644 examples/darwin-framework-tool/commands/bdx/Commands.h create mode 100644 examples/darwin-framework-tool/commands/bdx/DownloadLogCommand.h create mode 100644 examples/darwin-framework-tool/commands/bdx/DownloadLogCommand.mm diff --git a/examples/darwin-framework-tool/BUILD.gn b/examples/darwin-framework-tool/BUILD.gn index 7ee396dd04e24a..1973c64a889e20 100644 --- a/examples/darwin-framework-tool/BUILD.gn +++ b/examples/darwin-framework-tool/BUILD.gn @@ -163,6 +163,8 @@ executable("darwin-framework-tool") { "${chip_root}/examples/chip-tool/commands/common/Commands.h", "${chip_root}/examples/chip-tool/commands/common/HexConversion.h", "${chip_root}/zzz_generated/chip-tool/zap-generated/cluster/ComplexArgumentParser.cpp", + "commands/bdx/Commands.h", + "commands/bdx/DownloadLogCommand.mm", "commands/clusters/ClusterCommandBridge.h", "commands/clusters/ModelCommandBridge.mm", "commands/clusters/ReportCommandBridge.h", diff --git a/examples/darwin-framework-tool/commands/bdx/Commands.h b/examples/darwin-framework-tool/commands/bdx/Commands.h new file mode 100644 index 00000000000000..126ea055d03ba9 --- /dev/null +++ b/examples/darwin-framework-tool/commands/bdx/Commands.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * 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. + * + */ + +#pragma once + +#include "commands/bdx/DownloadLogCommand.h" +#include "commands/common/Commands.h" + +void registerCommandsBdx(Commands & commands) +{ + const char * clusterName = "Bdx"; + commands_list clusterCommands = { + make_unique(), // + }; + + commands.RegisterCommandSet(clusterName, clusterCommands, "Commands related to BDX"); +} diff --git a/examples/darwin-framework-tool/commands/bdx/DownloadLogCommand.h b/examples/darwin-framework-tool/commands/bdx/DownloadLogCommand.h new file mode 100644 index 00000000000000..ce48270d28b6ac --- /dev/null +++ b/examples/darwin-framework-tool/commands/bdx/DownloadLogCommand.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * 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. + * + */ + +#pragma once + +#include "../common/CHIPCommandBridge.h" + +class DownloadLogCommand : public CHIPCommandBridge +{ +public: + DownloadLogCommand() : CHIPCommandBridge("download") + { + AddArgument("node-id", 0, UINT64_MAX, &mNodeId, "Node to download the logs from."); + AddArgument("log-type", 0, 2, &mLogType, + "The type of log being requested. This should correspond to a value in the enum MTRDiagnosticLogType."); + AddArgument("timeout", 0, UINT16_MAX, &mTimeout, + "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 log is fully retrieved or an error occurs."); + } + + /////////// CHIPCommandBridge Interface ///////// + CHIP_ERROR RunCommand() override; + chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(10); } + +private: + chip::NodeId mNodeId; + uint8_t mLogType; + uint16_t mTimeout; +}; diff --git a/examples/darwin-framework-tool/commands/bdx/DownloadLogCommand.mm b/examples/darwin-framework-tool/commands/bdx/DownloadLogCommand.mm new file mode 100644 index 00000000000000..494ec964f14b63 --- /dev/null +++ b/examples/darwin-framework-tool/commands/bdx/DownloadLogCommand.mm @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * 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. + * + */ + +#import + +#import "MTRError_Utils.h" + +#include "DownloadLogCommand.h" + +CHIP_ERROR DownloadLogCommand::RunCommand() +{ + ChipLogProgress(chipTool, "Downloading logs from node 0x" ChipLogFormatX64, ChipLogValueX64(mNodeId)); + + MTRDeviceController * commissioner = CurrentCommissioner(); + auto * device = [MTRDevice deviceWithNodeID:@(mNodeId) controller:commissioner]; + + auto logType = static_cast(mLogType); + auto queue = dispatch_queue_create("com.chip.bdx.downloader", DISPATCH_QUEUE_SERIAL); + + auto * self = this; + auto completion = ^(NSURL * url, NSError * error) { + // A non-nil url indicates the presence of content, which can occur even in error scenarios like timeouts. + if (nil != url) { + NSError * readError = nil; + auto * data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&readError]; + VerifyOrReturn(nil == readError, self->SetCommandExitStatus(MTRErrorToCHIPErrorCode(readError))); + + auto * content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + NSLog(@"Content: %@", content); + } + + VerifyOrReturn(nil == error, self->SetCommandExitStatus(MTRErrorToCHIPErrorCode(error))); + + // The url is nil when there are no logs on the target device. + if (nil == url) { + NSLog(@"No logs has been found onto node 0x" ChipLogFormatX64, ChipLogValueX64(mNodeId)); + } + self->SetCommandExitStatus(CHIP_NO_ERROR); + }; + + [device downloadLogOfType:logType timeout:mTimeout queue:queue completion:completion]; + return CHIP_NO_ERROR; +} diff --git a/examples/darwin-framework-tool/main.mm b/examples/darwin-framework-tool/main.mm index 8c96d490dc5ed8..070ae3188407d4 100644 --- a/examples/darwin-framework-tool/main.mm +++ b/examples/darwin-framework-tool/main.mm @@ -20,6 +20,7 @@ #import "logging/logging.h" +#include "commands/bdx/Commands.h" #include "commands/common/Commands.h" #include "commands/delay/Commands.h" #include "commands/discover/Commands.h" @@ -38,6 +39,7 @@ int main(int argc, const char * argv[]) dft::logging::Setup(); Commands commands; + registerCommandsBdx(commands); registerCommandsPairing(commands); registerCommandsDelay(commands); registerCommandsDiscover(commands); diff --git a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj index 5c91426c85b298..1396e6f7fec936 100644 --- a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj +++ b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj @@ -305,6 +305,9 @@ B4E2621B2AA0D02000DBA5BC /* SleepCommand.mm in Sources */ = {isa = PBXBuildFile; fileRef = B4E262192AA0D01D00DBA5BC /* SleepCommand.mm */; }; B4E2621E2AA0D02D00DBA5BC /* WaitForCommissioneeCommand.mm in Sources */ = {isa = PBXBuildFile; fileRef = B4E2621C2AA0D02A00DBA5BC /* WaitForCommissioneeCommand.mm */; }; B4FCD56A2B5EDBD300832859 /* MTRDiagnosticLogsType.h in Headers */ = {isa = PBXBuildFile; fileRef = B4FCD5692B5EDBD300832859 /* MTRDiagnosticLogsType.h */; settings = {ATTRIBUTES = (Public, ); }; }; + B4FCD5702B603A6300832859 /* Commands.h in Headers */ = {isa = PBXBuildFile; fileRef = B4FCD56D2B603A6300832859 /* Commands.h */; }; + B4FCD5712B603A6300832859 /* DownloadLogCommand.h in Headers */ = {isa = PBXBuildFile; fileRef = B4FCD56E2B603A6300832859 /* DownloadLogCommand.h */; }; + B4FCD5722B603A6300832859 /* DownloadLogCommand.mm in Sources */ = {isa = PBXBuildFile; fileRef = B4FCD56F2B603A6300832859 /* DownloadLogCommand.mm */; }; BA09EB43247477BA00605257 /* libCHIP.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BA09EB3F2474762900605257 /* libCHIP.a */; }; D4772A46285AE98400383630 /* MTRClusterConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = D4772A45285AE98300383630 /* MTRClusterConstants.h */; settings = {ATTRIBUTES = (Public, ); }; }; /* End PBXBuildFile section */ @@ -678,6 +681,9 @@ B4E262192AA0D01D00DBA5BC /* SleepCommand.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SleepCommand.mm; sourceTree = ""; }; B4E2621C2AA0D02A00DBA5BC /* WaitForCommissioneeCommand.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WaitForCommissioneeCommand.mm; sourceTree = ""; }; B4FCD5692B5EDBD300832859 /* MTRDiagnosticLogsType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRDiagnosticLogsType.h; sourceTree = ""; }; + B4FCD56D2B603A6300832859 /* Commands.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Commands.h; sourceTree = ""; }; + B4FCD56E2B603A6300832859 /* DownloadLogCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DownloadLogCommand.h; sourceTree = ""; }; + B4FCD56F2B603A6300832859 /* DownloadLogCommand.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DownloadLogCommand.mm; sourceTree = ""; }; BA09EB3F2474762900605257 /* libCHIP.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libCHIP.a; path = lib/libCHIP.a; sourceTree = BUILT_PRODUCTS_DIR; }; BA107AEE2470CFBB004287EB /* chip_xcode_build_connector.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = chip_xcode_build_connector.sh; sourceTree = ""; }; D437613E285BDC0D0051FEA2 /* MTRErrorTestUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRErrorTestUtils.h; sourceTree = ""; }; @@ -746,6 +752,7 @@ 037C3D7B2991BD4F00B7EEE2 /* commands */ = { isa = PBXGroup; children = ( + B4FCD56C2B603A6300832859 /* bdx */, B4E262182AA0CFFE00DBA5BC /* delay */, 03FB93DA2A46200A0048CB35 /* discover */, 037C3D7C2991BD4F00B7EEE2 /* pairing */, @@ -1347,6 +1354,16 @@ path = delay; sourceTree = ""; }; + B4FCD56C2B603A6300832859 /* bdx */ = { + isa = PBXGroup; + children = ( + B4FCD56D2B603A6300832859 /* Commands.h */, + B4FCD56E2B603A6300832859 /* DownloadLogCommand.h */, + B4FCD56F2B603A6300832859 /* DownloadLogCommand.mm */, + ); + path = bdx; + sourceTree = ""; + }; BA09EB3E2474762900605257 /* Frameworks */ = { isa = PBXGroup; children = ( @@ -1376,6 +1393,7 @@ 037C3DCE2991BD5100B7EEE2 /* CHIPCommandBridge.h in Headers */, 037C3DD22991BD5200B7EEE2 /* InteractiveCommands.h in Headers */, 037C3DAF2991BD4F00B7EEE2 /* DeviceControllerDelegateBridge.h in Headers */, + B4FCD5712B603A6300832859 /* DownloadLogCommand.h in Headers */, 037C3DC32991BD5100B7EEE2 /* Commands.h in Headers */, 037C3DB82991BD5000B7EEE2 /* ClusterCommandBridge.h in Headers */, 037C3DC82991BD5100B7EEE2 /* CHIPToolKeypair.h in Headers */, @@ -1388,6 +1406,7 @@ 037C3DB92991BD5000B7EEE2 /* ReportCommandBridge.h in Headers */, 037C3DBE2991BD5000B7EEE2 /* OTASoftwareUpdateInteractive.h in Headers */, 037C3DBD2991BD5000B7EEE2 /* OTAProviderDelegate.h in Headers */, + B4FCD5702B603A6300832859 /* Commands.h in Headers */, 037C3DB02991BD4F00B7EEE2 /* Commands.h in Headers */, 037C3DC02991BD5100B7EEE2 /* Commands.h in Headers */, B4E262172AA0CF2000DBA5BC /* RemoteDataModelLogger.h in Headers */, @@ -1652,6 +1671,7 @@ B45373C12A9FEA9100807602 /* close.c in Sources */, 039546A62991E151006D42A8 /* InteractionModel.cpp in Sources */, B45373E72A9FEBA400807602 /* parsers.c in Sources */, + B4FCD5722B603A6300832859 /* DownloadLogCommand.mm in Sources */, B45373BF2A9FEA9100807602 /* adopt.c in Sources */, B45373F32A9FEC1A00807602 /* server-ws.c in Sources */, 03F430AA2994113500166449 /* sysunix.c in Sources */,