Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fabric-Sync] Run interactive mode in Fabric-Admin by default #33455

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/fabric-admin/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class CHIPCommand : public Command
CHIPCommand(const char * commandName, CredentialIssuerCommands * credIssuerCmds, const char * helpText = nullptr) :
Command(commandName, helpText), mCredIssuerCmds(credIssuerCmds)
{
AddArgument("log-file-path", &mLogFilePath,
"Path to the log file where the output is redirected. Can be absolute or relative to the current working "
"directory.");
AddArgument("paa-trust-store-path", &mPaaTrustStorePath,
"Path to directory holding PAA certificate information. Can be absolute or relative to the current working "
"directory.");
Expand Down Expand Up @@ -156,6 +159,7 @@ class CHIPCommand : public Command
// identity without shutting it down or something in between...
PersistentStorage mCommissionerStorage;
#endif // CONFIG_USE_LOCAL_STORAGE
chip::Optional<char *> mLogFilePath;
chip::PersistentStorageOperationalKeystore mOperationalKeystore;
chip::Credentials::PersistentStorageOpCertStore mOpCertStore;
static chip::Crypto::RawKeySessionKeystore sSessionKeystore;
Expand Down
57 changes: 51 additions & 6 deletions examples/fabric-admin/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
#include "InteractiveCommands.h"

#include <platform/logging/LogV.h>
#include <system/SystemClock.h>

#include <editline.h>

#include <stdarg.h>
#include <stdio.h>
#include <string>
#include <vector>

Expand All @@ -31,16 +34,52 @@ constexpr char kInteractiveModeStopCommand[] = "quit()";

namespace {

// File pointer for the log file
FILE * sLogFile = nullptr;

void OpenLogFile(const char * filePath)
{
sLogFile = fopen(filePath, "a");
if (sLogFile == nullptr)
{
perror("Failed to open log file");
}
}

void CloseLogFile()
{
if (sLogFile != nullptr)
{
fclose(sLogFile);
sLogFile = nullptr;
}
}

void ClearLine()
{
printf("\r\x1B[0J"); // Move cursor to the beginning of the line and clear from cursor to end of the screen
}

void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category, const char * msg, va_list args)
{
ClearLine();
chip::Logging::Platform::LogV(module, category, msg, args);
ClearLine();
if (sLogFile == nullptr)
{
return;
}

uint64_t timeMs = chip::System::SystemClock().GetMonotonicMilliseconds64().count();
uint64_t seconds = timeMs / 1000;
uint64_t milliseconds = timeMs % 1000;

flockfile(sLogFile);

fprintf(sLogFile, "[%llu.%06llu] CHIP:%s: ", static_cast<unsigned long long>(seconds),
static_cast<unsigned long long>(milliseconds), module);
vfprintf(sLogFile, msg, args);
fprintf(sLogFile, "\n");
fflush(sLogFile);

funlockfile(sLogFile);
}

} // namespace
Expand Down Expand Up @@ -90,9 +129,13 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
{
read_history(GetHistoryFilePath().c_str());

// Logs needs to be redirected in order to refresh the screen appropriately when something
// is dumped to stdout while the user is typing a command.
chip::Logging::SetLogRedirectCallback(LoggingCallback);
if (mLogFilePath.HasValue())
{
OpenLogFile(mLogFilePath.Value());

// Redirect logs to the custom logging callback
chip::Logging::SetLogRedirectCallback(LoggingCallback);
}

char * command = nullptr;
int status;
Expand All @@ -112,6 +155,8 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
}

SetCommandExitStatus(CHIP_NO_ERROR);
CloseLogFile();

return CHIP_NO_ERROR;
}

Expand Down
23 changes: 22 additions & 1 deletion examples/fabric-admin/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,26 @@
#include "commands/pairing/Commands.h"
#include <zap-generated/cluster/Commands.h>

#include <iostream>
#include <string>
#include <vector>

// ================================================================================
// Main Code
// ================================================================================
int main(int argc, char * argv[])
{
// Convert command line arguments to a vector of strings for easier manipulation
std::vector<std::string> args(argv, argv + argc);

// Check if "interactive" and "start" are not in the arguments
if (args.size() < 3 || args[1] != "interactive" || args[2] != "start")
{
// Insert "interactive" and "start" after the executable name
args.insert(args.begin() + 1, "interactive");
args.insert(args.begin() + 2, "start");
}

ExampleCredentialIssuerCommands credIssuerCommands;
Commands commands;

Expand All @@ -36,5 +51,11 @@ int main(int argc, char * argv[])
registerClusters(commands, &credIssuerCommands);
registerCommandsSubscriptions(commands, &credIssuerCommands);

return commands.Run(argc, argv);
std::vector<char *> c_args;
for (auto & arg : args)
{
c_args.push_back(const_cast<char *>(arg.c_str()));
}

return commands.Run(static_cast<int>(c_args.size()), c_args.data());
}
Loading