Skip to content

Commit

Permalink
[darwin_framework_tool] Add a shortcut (CTL('^')) to restart the stac…
Browse files Browse the repository at this point in the history
…k while in interactive mode
  • Loading branch information
vivien-apple committed Aug 31, 2022
1 parent 0a36d9f commit ac532d1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class CHIPCommandBridge : public Command

static std::set<CHIPCommandBridge *> sDeferredCleanups;

void RestartCommissioners();

private:
CHIP_ERROR InitializeCommissioner(std::string key, chip::FabricId fabricId,
const chip::Credentials::AttestationTrustStore * trustStore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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];
Expand All @@ -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];

Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
#include "InteractiveCommands.h"
#import <Matter/Matter.h>

#include <editline.h>
#include <iomanip>
#include <readline/history.h>
#include <readline/readline.h>
#include <sstream>

char kInteractiveModeName[] = "";
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit ac532d1

Please sign in to comment.