Skip to content

Commit

Permalink
esp32_diagnostic_trace: add CircularDiagnosticBuffer class to improve…
Browse files Browse the repository at this point in the history
… design
  • Loading branch information
esp committed Dec 6, 2024
1 parent 5a80c25 commit 07a880c
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ size_t LogProvider::GetSizeForIntent(IntentEnum intent)
{
case IntentEnum::kEndUserSupport: {
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
return DiagnosticStorageImpl::GetInstance().GetDataSize();
return CircularDiagnosticBuffer::GetInstance().GetDataSize();
#else
return static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart);
#endif
Expand Down Expand Up @@ -119,7 +119,7 @@ CHIP_ERROR LogProvider::PrepareLogContextForIntent(LogContext * context, IntentE
{
case IntentEnum::kEndUserSupport: {
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
CircularDiagnosticBuffer & diagnosticStorage = CircularDiagnosticBuffer::GetInstance();
MutableByteSpan endUserSupportSpan(endUserBuffer, CONFIG_END_USER_BUFFER_SIZE);

if (diagnosticStorage.IsEmptyBuffer())
Expand Down
4 changes: 3 additions & 1 deletion examples/temperature-measurement-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ extern "C" void app_main()
#endif

#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance(endUserBuffer, CONFIG_END_USER_BUFFER_SIZE);
memset(endUserBuffer, 0, CONFIG_END_USER_BUFFER_SIZE);
CircularDiagnosticBuffer & diagnosticStorage = CircularDiagnosticBuffer::GetInstance();
diagnosticStorage.Init(endUserBuffer, CONFIG_END_USER_BUFFER_SIZE);
static ESP32Diagnostics diagnosticBackend(diagnosticStorage);
Tracing::Register(diagnosticBackend);
#endif
Expand Down
1 change: 0 additions & 1 deletion src/tracing/esp32_diagnostic_trace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static_library("backend") {
sources = [
"Counter.cpp",
"Counter.h",
"DiagnosticStorageManager.cpp",
"DiagnosticStorageManager.h",
"DiagnosticTracing.cpp",
"DiagnosticTracing.h",
Expand Down
1 change: 0 additions & 1 deletion src/tracing/esp32_diagnostic_trace/Counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <lib/support/CHIPMemString.h>
#include <map>
#include <string.h>
#include <map>

namespace chip {
namespace Tracing {
Expand Down
164 changes: 0 additions & 164 deletions src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.cpp

This file was deleted.

121 changes: 108 additions & 13 deletions src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,128 @@
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPMem.h>
#include <tracing/esp32_diagnostic_trace/Diagnostics.h>
using namespace chip::TLV;

namespace chip {
namespace Tracing {
namespace Diagnostics {
class DiagnosticStorageImpl : public DiagnosticStorageInterface
class CircularDiagnosticBuffer : public TLVCircularBuffer, public DiagnosticStorageInterface
{
public:
static DiagnosticStorageImpl & GetInstance(uint8_t * buffer = nullptr, size_t bufferSize = 0);
// Singleton instance getter
static CircularDiagnosticBuffer & GetInstance()
{
static CircularDiagnosticBuffer instance;
return instance;
}

DiagnosticStorageImpl(const DiagnosticStorageImpl &) = delete;
DiagnosticStorageImpl & operator=(const DiagnosticStorageImpl &) = delete;
// Delete copy constructor and assignment operator to ensure singleton
CircularDiagnosticBuffer(const CircularDiagnosticBuffer &) = delete;
CircularDiagnosticBuffer & operator=(const CircularDiagnosticBuffer &) = delete;

CHIP_ERROR Store(DiagnosticEntry & diagnostic) override;
void Init(uint8_t * buffer, size_t bufferLength) { TLVCircularBuffer::Init(buffer, bufferLength); }

CHIP_ERROR Retrieve(MutableByteSpan & payload) override;
CHIP_ERROR Store(DiagnosticEntry & entry) override
{
CircularTLVWriter writer;
writer.Init(*this);

bool IsEmptyBuffer();
CHIP_ERROR err = entry.Encode(writer);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to write entry: %s", chip::ErrorStr(err));
}
return err;
}

uint32_t GetDataSize();
CHIP_ERROR Retrieve(MutableByteSpan & span) override
{
printf("**********************************************Retrieval Started*****************************************\n");
CHIP_ERROR err = CHIP_NO_ERROR;
TLVReader reader;
reader.Init(*this);

private:
DiagnosticStorageImpl(uint8_t * buffer, size_t bufferSize);
DiagnosticStorageImpl();
~DiagnosticStorageImpl();
TLVWriter writer;
writer.Init(span.data(), span.size());

TLVType outWriterContainer;
err = writer.StartContainer(AnonymousTag(), kTLVType_List, outWriterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to start container"));

while (CHIP_NO_ERROR == reader.Next())
{
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to read next TLV element: %s", ErrorStr(err)));

if (reader.GetType() == kTLVType_Structure && reader.GetTag() == AnonymousTag())
{
TLVType outerReaderContainer;
err = reader.EnterContainer(outerReaderContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to enter outer TLV container: %s", ErrorStr(err)));

err = reader.Next();
VerifyOrReturnError(
err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to read next TLV element in outer container: %s", ErrorStr(err)));

if ((reader.GetType() == kTLVType_Structure) &&
(reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::METRIC) ||
reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::TRACE) ||
reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::COUNTER)))
{
if ((reader.GetLengthRead() - writer.GetLengthWritten()) < (writer.GetRemainingFreeLength()))
{
err = writer.CopyElement(reader);
if (err == CHIP_ERROR_BUFFER_TOO_SMALL)
{
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current element");
break;
}
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to copy TLV element"));
}
else
{
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current TLV");
break;
}
}
else
{
ChipLogError(DeviceLayer, "Unexpected TLV element in outer container");
reader.ExitContainer(outerReaderContainer);
return CHIP_ERROR_WRONG_TLV_TYPE;
}
err = reader.ExitContainer(outerReaderContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to exit outer TLV container: %s", ErrorStr(err)));
}
else
{
ChipLogError(DeviceLayer, "Unexpected TLV element type or tag in outer container");
}
}

chip::TLV::TLVCircularBuffer mEndUserCircularBuffer;
err = writer.EndContainer(outWriterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to close outer container"));
err = writer.Finalize();
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to finalize TLV writing"));
span.reduce_size(writer.GetLengthWritten());
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n",
writer.GetLengthWritten());
ChipLogProgress(DeviceLayer, "Retrieval successful");
return CHIP_NO_ERROR;
}

bool IsEmptyBuffer() override { return DataLength() == 0; }

uint32_t GetDataSize() override { return DataLength(); }

private:
CircularDiagnosticBuffer() : TLVCircularBuffer(nullptr, 0) {}
~CircularDiagnosticBuffer() override = default;
};

} // namespace Diagnostics
} // namespace Tracing
} // namespace chip
Loading

0 comments on commit 07a880c

Please sign in to comment.