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

Draft: TLV-JSON-TLV converter #27279

Closed
wants to merge 1 commit into from
Closed
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
311 changes: 311 additions & 0 deletions examples/common/json/JsonTLV.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2015-2017 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* This file implements interfaces for debugging and logging
* CHIP TLV.
*
*/

#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif

#include <ctype.h>
#include <inttypes.h>
#include <string.h>

#include <lib/core/TLV.h>
#include <lib/core/TLVUtilities.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

#include "JsonTLV.h"

namespace chip {

namespace TLV {

namespace Json {

void ENFORCE_FORMAT(1, 2) TLVDumpWriter(const char * aFormat, ...)
{
va_list args;

va_start(args, aFormat);

vprintf(aFormat, args);

va_end(args);
}

static void ENFORCE_FORMAT(2, 3) FormattedWrite(Encoding::BufferWriter & writer, const char * aFormat, ...)
{
char out_buf[100];

va_list args;

va_start(args, aFormat);

vsnprintf(out_buf, sizeof(out_buf), aFormat, args);

va_end(args);

writer.Put(out_buf);
}

/**
* Write the TLV element referenced by @a aReader in human-readable form using
* @a aWriter.
*
* @param[in] aWriter The writer to log the TLV data.
* @param[in] aIndent The indentation for logging the current depth into
* the TLV data.
* @param[in] aReader A read-only reference to the TLV reader containing
* the TLV data to log.
* @param[in] aDepth The current depth into the TLV data.
*
*/
static void WriteHandlerJSON(Encoding::BufferWriter & aWriter, const char * aIndent, const TLVReader & aReader, size_t aDepth)
{
const TLVType type = aReader.GetType();
const Tag tag = aReader.GetTag();
const uint32_t len = aReader.GetLength();
const uint8_t * strbuf = nullptr;
CHIP_ERROR err = CHIP_NO_ERROR;
TLVReader temp;
TLVTagControl tagControl;

temp.Init(aReader);
tagControl = static_cast<TLVTagControl>(temp.GetControlByte() & kTLVTagControlMask);

for (size_t i = 0; i < aDepth; i++)
FormattedWrite(aWriter, "%s", aIndent);

if (TLVTypeIsContainer(type))
{
if (tagControl == TLVTagControl::ContextSpecific)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about profile tags in TLV lists?

{
FormattedWrite(aWriter, "\"0x%x\" : ", TagNumFromTag(tag));
}

// add any prefix
switch (type)
{
case kTLVType_Structure:
FormattedWrite(aWriter, "{\n");
break;

case kTLVType_Array:
FormattedWrite(aWriter, "[\n");
break;

case kTLVType_List:
FormattedWrite(aWriter, "[\n");
break;

default:
break;
}
}
else
{
if (tagControl == TLVTagControl::ContextSpecific)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this does not seem to handle profile tags right. But also, shouldn't that "tag" : prefix just be written out independently of whether the tag is a container or not?

{
FormattedWrite(aWriter, "\"0x%x\" : { \"tlvType\":\"0x%02x\", \"value\": ", TagNumFromTag(tag), type);
}
else
{
FormattedWrite(aWriter, "{ \"tlvType\":\"0x%02x\", \"value\": ", type);
}

switch (type)
{

case kTLVType_SignedInteger:
int64_t sVal;
err = temp.Get(sVal);
VerifyOrExit(err == CHIP_NO_ERROR, FormattedWrite(aWriter, "Error in kTLVType_SignedInteger"));
FormattedWrite(aWriter, "%" PRIi64, sVal);
break;

case kTLVType_UnsignedInteger:
uint64_t uVal;
err = temp.Get(uVal);
VerifyOrExit(err == CHIP_NO_ERROR, FormattedWrite(aWriter, "Error in kTLVType_UnsignedInteger"));
FormattedWrite(aWriter, "%" PRIu64, uVal);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're OK with the data corruptions for values > 2^53?

break;

case kTLVType_Boolean:
bool bVal;
err = temp.Get(bVal);
VerifyOrExit(err == CHIP_NO_ERROR, FormattedWrite(aWriter, "Error in kTLVType_Boolean"));
FormattedWrite(aWriter, "%s", bVal ? "true" : "false");
break;

case kTLVType_FloatingPointNumber:
double fpVal;
err = temp.Get(fpVal);
VerifyOrExit(err == CHIP_NO_ERROR, FormattedWrite(aWriter, "Error in kTLVType_FloatingPointNumber"));
FormattedWrite(aWriter, "%lf", fpVal);
break;

case kTLVType_UTF8String:
err = temp.GetDataPtr(strbuf);
VerifyOrExit(err == CHIP_NO_ERROR, FormattedWrite(aWriter, "Error in kTLVType_UTF8String"));
FormattedWrite(aWriter, "\"%-.*s\"", static_cast<int>(len), strbuf);
break;

case kTLVType_ByteString:
err = temp.GetDataPtr(strbuf);
VerifyOrExit(err == CHIP_NO_ERROR, FormattedWrite(aWriter, "Error in kTLVType_ByteString"));
FormattedWrite(aWriter, "hex:");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this produces valid JSON, unless I am missing something.....

for (uint32_t i = 0; i < len; i++)
{
FormattedWrite(aWriter, "%02X", strbuf[i]);
}
break;

case kTLVType_Null:
FormattedWrite(aWriter, "null");
break;

case kTLVType_NotSpecified:
FormattedWrite(aWriter, "ns");
break;

default:
FormattedWrite(aWriter, "Error: Type is not primitive.");
break;
}

FormattedWrite(aWriter, " }");
}

exit:
return;
}

static void WriteHandlerJSONAfter(Encoding::BufferWriter & aWriter, const char * aIndent, const TLVType type, size_t aDepth,
bool endOfList)
{
if (TLVTypeIsContainer(type))
{
for (size_t i = 0; i < aDepth; i++)
FormattedWrite(aWriter, "%s", aIndent);

// add any postfix
switch (type)
{
case kTLVType_Structure:
FormattedWrite(aWriter, "}");
break;

case kTLVType_Array:
FormattedWrite(aWriter, "]");
break;

case kTLVType_List:
FormattedWrite(aWriter, "]");
break;

default:
break;
}
}

if (!endOfList)
{
FormattedWrite(aWriter, ",");
}

FormattedWrite(aWriter, "\n");
}

/**
* Log the TLV data within the specified reader in human-readable form.
*
* @param[in] aReader A read-only reference to the TLV reader containing
* the TLV data to log.
* @param[in] aDepth The current depth into the TLV data.
* @param[in,out] aContext A pointer to the handler-specific context.
*
* @retval #CHIP_NO_ERROR On success.
*
* @retval #CHIP_ERROR_INVALID_ARGUMENT If aContext is NULL or if
* aContext->mWriter is NULL.
*
*/
CHIP_ERROR WriteHandlerJSON(const TLVReader & aReader, size_t aDepth, void * aContext)
{
static const char indent[] = " ";
JsonContext * context;

VerifyOrReturnError(aContext != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

context = static_cast<JsonContext *>(aContext);

WriteHandlerJSON(context->mWriter, indent, aReader, aDepth);

return CHIP_NO_ERROR;
}

CHIP_ERROR WriteHandlerJSONAfter(const TLVType theType, size_t aDepth, bool endOfList, void * aContext)
{
static const char indent[] = " ";
JsonContext * context;

VerifyOrReturnError(aContext != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

context = static_cast<JsonContext *>(aContext);

WriteHandlerJSONAfter(context->mWriter, indent, theType, aDepth, endOfList);

return CHIP_NO_ERROR;
}
/**
* Write the TLV data within the specified reader in human-readable form with
* the specified writer.
*
* @param[in] aReader A read-only reference to the TLV reader containing
* the TLV data to log.
*
* @param[in] aWriter A dump writer to log the TLV data of the TLV reader.
*
* @retval #CHIP_NO_ERROR On success.
*
*/
CHIP_ERROR WriteJSON(const TLVReader & aReader, Encoding::BufferWriter & aWriter)
{
void * context = nullptr;
JsonContext dumpContext = { aWriter, context };
CHIP_ERROR retval;

retval = Utilities::Iterate(aReader, WriteHandlerJSON, &dumpContext, true /* recurse */, WriteHandlerJSONAfter);

aWriter.Put('\0');

return retval;
}

} // namespace Json

} // namespace TLV

} // namespace chip
Loading