Skip to content

Commit

Permalink
[chip-tool] Add DataModelLogger::LogJson command and makes it goes ov…
Browse files Browse the repository at this point in the history
…er websockets in interactive server mode
  • Loading branch information
vivien-apple committed Jan 12, 2023
1 parent 3670731 commit 8d2d905
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ static_library("chip-tool-utils") {
"${chip_root}/src/controller/data_model",
"${chip_root}/src/credentials:file_attestation_trust_store",
"${chip_root}/src/lib",
"${chip_root}/src/lib/support/jsontlv",
"${chip_root}/src/platform",
"${chip_root}/third_party/inipp",
"${chip_root}/third_party/jsoncpp",
Expand Down
6 changes: 6 additions & 0 deletions examples/chip-tool/commands/clusters/ClusterCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub
CHIP_ERROR error = status.ToChipError();
if (CHIP_NO_ERROR != error)
{
ReturnOnFailure(DataModelLogger::LogErrorAsJSON(path, status));

ChipLogError(chipTool, "Response Failure: %s", chip::ErrorStr(error));
mError = error;
return;
}

if (data != nullptr)
{
ReturnOnFailure(DataModelLogger::LogCommandAsJSON(path, data));

error = DataModelLogger::LogCommand(path, data);
if (CHIP_NO_ERROR != error)
{
Expand All @@ -93,6 +97,8 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub

virtual void OnError(const chip::app::CommandSender * client, CHIP_ERROR error) override
{
ReturnOnFailure(DataModelLogger::LogErrorAsJSON(error));

ChipLogProgress(chipTool, "Error: %s", chip::ErrorStr(error));
mError = error;
}
Expand Down
134 changes: 134 additions & 0 deletions examples/chip-tool/commands/clusters/DataModelLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,26 @@
#include <app/ConcreteAttributePath.h>
#include <app/ConcreteCommandPath.h>
#include <app/EventHeader.h>
#include <app/MessageDef/StatusIB.h>
#include <app/data-model/DecodableList.h>
#include <lib/support/BytesToHex.h>
#include <lib/support/jsontlv/TlvJson.h>

constexpr const char * kClusterIdKey = "clusterId";
constexpr const char * kEndpointIdKey = "endpointId";
constexpr const char * kAttributeIdKey = "attributeId";
constexpr const char * kEventIdKey = "eventId";
constexpr const char * kCommandIdKey = "commandId";
constexpr const char * kErrorIdKey = "error";
constexpr const char * kClusterErrorIdKey = "clusterError";
constexpr const char * kGenericFailure = "FAILURE";

class DataModelLoggerJSONDelegate
{
public:
CHIP_ERROR virtual LogJSON(const char *) = 0;
virtual ~DataModelLoggerJSONDelegate(){};
};

class DataModelLogger
{
Expand All @@ -34,7 +52,122 @@ class DataModelLogger
static CHIP_ERROR LogCommand(const chip::app::ConcreteCommandPath & path, chip::TLV::TLVReader * data);
static CHIP_ERROR LogEvent(const chip::app::EventHeader & header, chip::TLV::TLVReader * data);

static CHIP_ERROR LogAttributeAsJSON(const chip::app::ConcreteDataAttributePath & path, chip::TLV::TLVReader * data)
{
VerifyOrReturnError(mJSONDelegate != nullptr, CHIP_NO_ERROR);

Json::Value value;
value[kClusterIdKey] = path.mClusterId;
value[kEndpointIdKey] = path.mEndpointId;
value[kAttributeIdKey] = path.mAttributeId;

chip::TLV::TLVReader reader;
reader.Init(*data);
ReturnErrorOnFailure(chip::TlvToJson(reader, value));

auto valueStr = chip::JsonToString(value);
return mJSONDelegate->LogJSON(valueStr.c_str());
}

static CHIP_ERROR LogErrorAsJSON(const chip::app::ConcreteDataAttributePath & path, const chip::app::StatusIB & status)
{
VerifyOrReturnError(mJSONDelegate != nullptr, CHIP_NO_ERROR);

Json::Value value;
value[kClusterIdKey] = path.mClusterId;
value[kEndpointIdKey] = path.mEndpointId;
value[kAttributeIdKey] = path.mAttributeId;

return LogError(value, status);
}

static CHIP_ERROR LogCommandAsJSON(const chip::app::ConcreteCommandPath & path, chip::TLV::TLVReader * data)
{
VerifyOrReturnError(mJSONDelegate != nullptr, CHIP_NO_ERROR);

Json::Value value;
value[kClusterIdKey] = path.mClusterId;
value[kEndpointIdKey] = path.mEndpointId;
value[kCommandIdKey] = path.mCommandId;

chip::TLV::TLVReader reader;
reader.Init(*data);
ReturnErrorOnFailure(chip::TlvToJson(reader, value));

auto valueStr = chip::JsonToString(value);
return mJSONDelegate->LogJSON(valueStr.c_str());
}

static CHIP_ERROR LogErrorAsJSON(const chip::app::ConcreteCommandPath & path, const chip::app::StatusIB & status)
{
VerifyOrReturnError(mJSONDelegate != nullptr, CHIP_NO_ERROR);

Json::Value value;
value[kClusterIdKey] = path.mClusterId;
value[kEndpointIdKey] = path.mEndpointId;
value[kCommandIdKey] = path.mCommandId;

return LogError(value, status);
}

static CHIP_ERROR LogEventAsJSON(const chip::app::EventHeader & header, chip::TLV::TLVReader * data)
{
VerifyOrReturnError(mJSONDelegate != nullptr, CHIP_NO_ERROR);

Json::Value value;
value[kClusterIdKey] = header.mPath.mClusterId;
value[kEndpointIdKey] = header.mPath.mEndpointId;
value[kEventIdKey] = header.mPath.mEventId;

chip::TLV::TLVReader reader;
reader.Init(*data);
ReturnErrorOnFailure(chip::TlvToJson(reader, value));

auto valueStr = chip::JsonToString(value);
return mJSONDelegate->LogJSON(valueStr.c_str());
}

static CHIP_ERROR LogErrorAsJSON(const chip::app::EventHeader & header, const chip::app::StatusIB & status)
{
VerifyOrReturnError(mJSONDelegate != nullptr, CHIP_NO_ERROR);

Json::Value value;
value[kClusterIdKey] = header.mPath.mClusterId;
value[kEndpointIdKey] = header.mPath.mEndpointId;
value[kEventIdKey] = header.mPath.mEventId;

return LogError(value, status);
}

static CHIP_ERROR LogErrorAsJSON(const CHIP_ERROR & error)
{
Json::Value value;
chip::app::StatusIB status;
status.InitFromChipError(error);
return LogError(value, status);
}

static void SetJSONDelegate(DataModelLoggerJSONDelegate * delegate) { mJSONDelegate = delegate; }

private:
static CHIP_ERROR LogError(Json::Value & value, const chip::app::StatusIB & status)
{
if (status.mClusterStatus.HasValue())
{
auto statusValue = status.mClusterStatus.Value();
value[kErrorIdKey] = kGenericFailure;
value[kClusterErrorIdKey] = statusValue;
}
else
{
auto statusName = chip::Protocols::InteractionModel::StatusName(status.mStatus);
value[kErrorIdKey] = statusName;
}

auto valueStr = chip::JsonToString(value);
return mJSONDelegate->LogJSON(valueStr.c_str());
}

static CHIP_ERROR LogValue(const char * label, size_t indent, bool value)
{
DataModelLogger::LogString(label, indent, value ? "TRUE" : "FALSE");
Expand Down Expand Up @@ -194,4 +327,5 @@ class DataModelLogger
}

static size_t ComputePrefixSize(const std::string label, size_t indent) { return ComputePrefix(label, indent).size(); }
static DataModelLoggerJSONDelegate * mJSONDelegate;
};
6 changes: 6 additions & 0 deletions examples/chip-tool/commands/clusters/ReportCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ReportCommand : public InteractionModelReports, public ModelCommand, publi
CHIP_ERROR error = status.ToChipError();
if (CHIP_NO_ERROR != error)
{
ReturnOnFailure(DataModelLogger::LogErrorAsJSON(path, status));

ChipLogError(chipTool, "Response Failure: %s", chip::ErrorStr(error));
mError = error;
return;
Expand All @@ -49,6 +51,8 @@ class ReportCommand : public InteractionModelReports, public ModelCommand, publi
return;
}

ReturnOnFailure(DataModelLogger::LogAttributeAsJSON(path, data));

error = DataModelLogger::LogAttribute(path, data);
if (CHIP_NO_ERROR != error)
{
Expand All @@ -66,6 +70,8 @@ class ReportCommand : public InteractionModelReports, public ModelCommand, publi
CHIP_ERROR error = status->ToChipError();
if (CHIP_NO_ERROR != error)
{
ReturnOnFailure(DataModelLogger::LogErrorAsJSON(eventHeader, *status));

ChipLogError(chipTool, "Response Failure: %s", chip::ErrorStr(error));
mError = error;
return;
Expand Down
4 changes: 4 additions & 0 deletions examples/chip-tool/commands/clusters/WriteAttributeCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ class WriteAttribute : public InteractionModelWriter, public ModelCommand, publi
CHIP_ERROR error = status.ToChipError();
if (CHIP_NO_ERROR != error)
{
ReturnOnFailure(DataModelLogger::LogErrorAsJSON(path, status));

ChipLogError(chipTool, "Response Failure: %s", chip::ErrorStr(error));
mError = error;
}
}

void OnError(const chip::app::WriteClient * client, CHIP_ERROR error) override
{
ReturnOnFailure(DataModelLogger::LogErrorAsJSON(error));

ChipLogProgress(chipTool, "Error: %s", chip::ErrorStr(error));
mError = error;
}
Expand Down
69 changes: 65 additions & 4 deletions examples/chip-tool/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ constexpr const char * kInteractiveModePrompt = ">>> ";
constexpr uint8_t kInteractiveModeArgumentsMaxLength = 32;
constexpr const char * kInteractiveModeHistoryFilePath = "/tmp/chip_tool_history";
constexpr const char * kInteractiveModeStopCommand = "quit()";
constexpr const char * kSuccess = "SUCCESS";
constexpr const char * kFailure = "{\"error\": \"FAILURE\"}";
constexpr const char * kCategoryError = "Error";
constexpr const char * kCategoryProgress = "Progress";
constexpr const char * kCategoryDetail = "Detail";

namespace {

Expand All @@ -44,6 +49,45 @@ void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category,
ClearLine();
}

InteractiveServerCommand * gInteractiveServerCommand = nullptr;
void ENFORCE_FORMAT(3, 0) InteractiveServerLoggingCallback(const char * module, uint8_t category, const char * msg, va_list args)
{
const char * messageType = nullptr;
switch (category)
{
case chip::Logging::kLogCategory_Error:
messageType = kCategoryError;
break;
case chip::Logging::kLogCategory_Progress:
messageType = kCategoryProgress;
break;
case chip::Logging::kLogCategory_Detail:
messageType = kCategoryDetail;
break;
}

char message[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
vsnprintf(message, sizeof(message), msg, args);

char base64Message[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE * 2] = {};
chip::Base64Encode(reinterpret_cast<uint8_t *>(message), static_cast<uint16_t>(strlen(message)), base64Message);

std::string jsonLog;
jsonLog = jsonLog + "{";
jsonLog = jsonLog + " \"log\": {";
jsonLog = jsonLog + " \"module\": \"" + std::string(module) + "\",";
jsonLog = jsonLog + " \"category\": \"" + messageType + "\",";
jsonLog = jsonLog + " \"message\": \"" + base64Message + "\"";
jsonLog = jsonLog + " }";
jsonLog = jsonLog + "}";

va_list empty_va_list;
chip::Logging::Platform::LogV(module, category, message, empty_va_list);

VerifyOrReturn(nullptr != gInteractiveServerCommand);
gInteractiveServerCommand->LogJSON(jsonLog.c_str());
} // namespace

char * GetCommand(char * command)
{
if (command != nullptr)
Expand All @@ -67,15 +111,31 @@ char * GetCommand(char * command)

CHIP_ERROR InteractiveServerCommand::RunCommand()
{
// 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(InteractiveServerLoggingCallback);

DataModelLogger::SetJSONDelegate(this);
ReturnErrorOnFailure(mWebSocketServer.Run(mPort, this));

gInteractiveServerCommand = nullptr;
SetCommandExitStatus(CHIP_NO_ERROR);
return CHIP_NO_ERROR;
}

bool InteractiveServerCommand::OnWebSocketMessageReceived(char * msg)
{
return ParseCommand(msg);
gInteractiveServerCommand = this;
int status;
auto shouldStop = ParseCommand(msg, &status);

LogJSON(status == EXIT_SUCCESS ? kSuccess : kFailure);
return shouldStop;
}

CHIP_ERROR InteractiveServerCommand::LogJSON(const char * json)
{
return mWebSocketServer.Send(json);
}

CHIP_ERROR InteractiveStartCommand::RunCommand()
Expand All @@ -87,10 +147,11 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
chip::Logging::SetLogRedirectCallback(LoggingCallback);

char * command = nullptr;
int status;
while (true)
{
command = GetCommand(command);
if (command != nullptr && !ParseCommand(command))
if (command != nullptr && !ParseCommand(command, &status))
{
break;
}
Expand All @@ -106,7 +167,7 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
return CHIP_NO_ERROR;
}

bool InteractiveCommand::ParseCommand(char * command)
bool InteractiveCommand::ParseCommand(char * command, int * status)
{
if (strcmp(command, kInteractiveModeStopCommand) == 0)
{
Expand Down Expand Up @@ -134,7 +195,7 @@ bool InteractiveCommand::ParseCommand(char * command)
}

ClearLine();
mHandler->RunInteractive(argsCount, args);
*status = mHandler->RunInteractive(command);

// Do not delete arg[0]
while (--argsCount)
Expand Down
8 changes: 6 additions & 2 deletions examples/chip-tool/commands/interactive/InteractiveCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include "../clusters/DataModelLogger.h"
#include "../common/CHIPCommand.h"
#include "../common/Commands.h"

Expand All @@ -35,7 +36,7 @@ class InteractiveCommand : public CHIPCommand
/////////// CHIPCommand Interface /////////
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(0); }

bool ParseCommand(char * command);
bool ParseCommand(char * command, int * status);

private:
Commands * mHandler = nullptr;
Expand All @@ -52,7 +53,7 @@ class InteractiveStartCommand : public InteractiveCommand
CHIP_ERROR RunCommand() override;
};

class InteractiveServerCommand : public InteractiveCommand, public WebSocketServerDelegate
class InteractiveServerCommand : public InteractiveCommand, public WebSocketServerDelegate, public DataModelLoggerJSONDelegate
{
public:
InteractiveServerCommand(Commands * commandsHandler, CredentialIssuerCommands * credsIssuerConfig) :
Expand All @@ -67,6 +68,9 @@ class InteractiveServerCommand : public InteractiveCommand, public WebSocketServ
/////////// WebSocketServerDelegate Interface /////////
bool OnWebSocketMessageReceived(char * msg) override;

/////////// DataModelLoggerJSONDelegate interface /////////
CHIP_ERROR LogJSON(const char * json) override;

private:
WebSocketServer mWebSocketServer;
chip::Optional<uint16_t> mPort;
Expand Down
Loading

0 comments on commit 8d2d905

Please sign in to comment.