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 (project-chip#24387)
  • Loading branch information
vivien-apple authored and David Lechner committed Mar 22, 2023
1 parent e72db70 commit d6ae36c
Show file tree
Hide file tree
Showing 12 changed files with 349 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
130 changes: 130 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,25 @@
#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";

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

class DataModelLogger
{
Expand All @@ -34,7 +51,119 @@ 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[kClusterErrorIdKey] = statusValue;
}

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 +323,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
Loading

0 comments on commit d6ae36c

Please sign in to comment.