From ac532d14c8943027ce77c7242612d711d840c7cc Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 30 Aug 2022 17:22:34 +0200 Subject: [PATCH] [darwin_framework_tool] Add a shortcut (CTL('^')) to restart the stack while in interactive mode --- .../commands/common/CHIPCommandBridge.h | 2 ++ .../commands/common/CHIPCommandBridge.mm | 30 ++++++++++++++++--- .../interactive/InteractiveCommands.mm | 28 +++++++++++++++-- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.h b/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.h index a16eb224db52e6..23eb9bb52a7df7 100644 --- a/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.h +++ b/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.h @@ -95,6 +95,8 @@ class CHIPCommandBridge : public Command static std::set sDeferredCleanups; + void RestartCommissioners(); + private: CHIP_ERROR InitializeCommissioner(std::string key, chip::FabricId fabricId, const chip::Credentials::AttestationTrustStore * trustStore); diff --git a/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm b/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm index 40d265b3e171e1..06791fe0653eb1 100644 --- a/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm +++ b/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm @@ -33,6 +33,8 @@ dispatch_queue_t CHIPCommandBridge::mOTAProviderCallbackQueue; OTAProviderDelegate * CHIPCommandBridge::mOTADelegate; +CHIPToolKeypair * gNocSigner = [[CHIPToolKeypair alloc] init]; + CHIP_ERROR CHIPCommandBridge::Run() { ChipLogProgress(chipTool, "Running Command"); @@ -61,7 +63,7 @@ return CHIP_NO_ERROR; } NSData * ipk; - CHIPToolKeypair * nocSigner = [[CHIPToolKeypair alloc] init]; + gNocSigner = [[CHIPToolKeypair alloc] init]; storage = [[CHIPToolPersistentStorageDelegate alloc] init]; mOTADelegate = [[OTAProviderDelegate alloc] init]; @@ -82,13 +84,13 @@ return CHIP_ERROR_INTERNAL; } - ReturnLogErrorOnFailure([nocSigner createOrLoadKeys:storage]); + ReturnLogErrorOnFailure([gNocSigner createOrLoadKeys:storage]); - ipk = [nocSigner getIPK]; + ipk = [gNocSigner getIPK]; constexpr const char * identities[] = { kIdentityAlpha, kIdentityBeta, kIdentityGamma }; for (size_t i = 0; i < ArraySize(identities); ++i) { - auto controllerParams = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:nocSigner + auto controllerParams = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:gNocSigner fabricId:(i + 1) ipk:ipk]; @@ -134,6 +136,26 @@ MTRDeviceController * CHIPCommandBridge::GetCommissioner(const char * identity) { return mControllers[identity]; } +void CHIPCommandBridge::RestartCommissioners() +{ + for (auto & pair : mControllers) { + [pair.second shutdown]; + } + + auto factory = [MTRControllerFactory sharedInstance]; + NSData * ipk = [gNocSigner getIPK]; + + constexpr const char * identities[] = { kIdentityAlpha, kIdentityBeta, kIdentityGamma }; + for (size_t i = 0; i < ArraySize(identities); ++i) { + auto controllerParams = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:gNocSigner + fabricId:(i + 1) + ipk:ipk]; + + auto controller = [factory startControllerOnExistingFabric:controllerParams]; + mControllers[identities[i]] = controller; + } +} + void CHIPCommandBridge::ShutdownCommissioner() { ChipLogProgress(chipTool, "Shutting down controller"); diff --git a/examples/darwin-framework-tool/commands/interactive/InteractiveCommands.mm b/examples/darwin-framework-tool/commands/interactive/InteractiveCommands.mm index 0a4089abf749c8..7252f26c6887a8 100644 --- a/examples/darwin-framework-tool/commands/interactive/InteractiveCommands.mm +++ b/examples/darwin-framework-tool/commands/interactive/InteractiveCommands.mm @@ -19,9 +19,8 @@ #include "InteractiveCommands.h" #import +#include #include -#include -#include #include char kInteractiveModeName[] = ""; @@ -32,6 +31,22 @@ namespace { +class RestartCommand : public CHIPCommandBridge { +public: + RestartCommand() + : CHIPCommandBridge("restart") + { + } + + CHIP_ERROR RunCommand() override + { + RestartCommissioners(); + return CHIP_NO_ERROR; + } + + chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(0); } +}; + void ClearLine() { printf("\r\x1B[0J"); // Move cursor to the beginning of the line and clear from cursor to end of the screen @@ -63,6 +78,13 @@ void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category, return command; } +el_status_t RestartFunction() +{ + RestartCommand cmd; + cmd.RunCommand(); + return CSstay; +} + CHIP_ERROR InteractiveStartCommand::RunCommand() { read_history(kInteractiveModeHistoryFilePath); @@ -71,6 +93,8 @@ void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category, // is dumped to stdout while the user is typing a command. chip::Logging::SetLogRedirectCallback(LoggingCallback); + el_bind_key(CTL('^'), RestartFunction); + char * command = nullptr; while (YES) { command = GetCommand(command);