Skip to content

Commit

Permalink
Allows Fabric Admin to process command other than console (#33765)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored Jun 6, 2024
1 parent 321f895 commit 9c7fbe8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
#include <unistd.h>

#if defined(PW_RPC_ENABLED)
#include "pw_assert/check.h"
#include "pw_hdlc/decoder.h"
#include "pw_hdlc/default_addresses.h"
#include "pw_hdlc/rpc_channel.h"
#include "pw_rpc/client.h"
#include "pw_stream/socket_stream.h"

#include <rpc/RpcClient.h>
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdarg.h>
#include <stdio.h>
#include <string>
#include <thread>
#include <vector>

#if defined(PW_RPC_ENABLED)
Expand All @@ -44,6 +45,26 @@ constexpr uint16_t kRetryIntervalS = 5;
// File pointer for the log file
FILE * sLogFile = nullptr;

std::queue<std::string> sCommandQueue;
std::mutex sQueueMutex;
std::condition_variable sQueueCondition;

void ReadCommandThread()
{
char * command;
while (true)
{
command = readline(kInteractiveModePrompt);
if (command != nullptr && *command)
{
std::unique_lock<std::mutex> lock(sQueueMutex);
sCommandQueue.push(command);
free(command);
sQueueCondition.notify_one();
}
}
}

void OpenLogFile(const char * filePath)
{
sLogFile = fopen(filePath, "a");
Expand Down Expand Up @@ -113,13 +134,22 @@ void ExecuteDeferredConnect(intptr_t ignored)

char * InteractiveStartCommand::GetCommand(char * command)
{
std::unique_lock<std::mutex> lock(sQueueMutex);
sQueueCondition.wait(lock, [&] { return !sCommandQueue.empty(); });

std::string cmd = sCommandQueue.front();
sCommandQueue.pop();

if (command != nullptr)
{
free(command);
command = nullptr;
}

command = readline(kInteractiveModePrompt);
command = new char[cmd.length() + 1];
strcpy(command, cmd.c_str());

ChipLogProgress(NotSpecified, "GetCommand: %s", command);

// Do not save empty lines
if (command != nullptr && *command)
Expand Down Expand Up @@ -168,6 +198,9 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
DeviceLayer::PlatformMgr().ScheduleWork(ExecuteDeferredConnect, 0);
#endif

std::thread readCommands(ReadCommandThread);
readCommands.detach();

char * command = nullptr;
int status;
while (true)
Expand Down Expand Up @@ -213,3 +246,12 @@ bool InteractiveCommand::NeedsOperationalAdvertising()
{
return mAdvertiseOperational.ValueOr(true);
}

void PushCommand(const std::string & command)
{
std::unique_lock<std::mutex> lock(sQueueMutex);

ChipLogProgress(NotSpecified, "PushCommand: %s", command.c_str());
sCommandQueue.push(command);
sQueueCondition.notify_one();
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ class InteractiveStartCommand : public InteractiveCommand
char * GetCommand(char * command);
std::string GetHistoryFilePath() const;
};

void PushCommand(const std::string & command);
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class OpenCommissioningWindowCommand : public CHIPCommand

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;

// We issue multiple data model operations for this command, and the default
// timeout for those is 10 seconds, so default to 20 seconds.
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(mTimeout.ValueOr(20)); }
Expand Down

0 comments on commit 9c7fbe8

Please sign in to comment.