Skip to content

Commit

Permalink
backend: Add description for diagnosticstorage interface, remove unnc…
Browse files Browse the repository at this point in the history
…essary comments, format files
  • Loading branch information
pimpalemahesh committed Nov 15, 2024
1 parent 8b29ccb commit 5ef9201
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 69 deletions.
6 changes: 3 additions & 3 deletions src/tracing/esp32_diagnostic_trace/Counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ ESPDiagnosticCounter * ESPDiagnosticCounter::GetInstance(const char * label)
VerifyOrDie(ptr != nullptr);

ESPDiagnosticCounter * newInstance = new (ptr) ESPDiagnosticCounter(label);
newInstance->mNext = mHead;
mHead = newInstance;
newInstance->mNext = mHead;
mHead = newInstance;

return newInstance;
}
Expand All @@ -61,7 +61,7 @@ void ESPDiagnosticCounter::ReportMetrics()
CHIP_ERROR err = CHIP_NO_ERROR;
Counter counter(label, instanceCount, esp_log_timestamp());
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
err = diagnosticStorage.Store(counter);
err = diagnosticStorage.Store(counter);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data"));
}

Expand Down
4 changes: 2 additions & 2 deletions src/tracing/esp32_diagnostic_trace/Counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

#pragma once

#include "tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h"
#include <esp_diagnostics_metrics.h>
#include <esp_log.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CHIPMemString.h>
#include <string.h>
#include "tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h"

using namespace chip::Tracing::Diagnostics;

Expand All @@ -41,7 +41,7 @@ class ESPDiagnosticCounter
{
private:
static ESPDiagnosticCounter * mHead; // head of the counter list
const char * label; // unique key ,it is used as a static string.
const char * label; // unique key ,it is used as a static string.
int32_t instanceCount;
ESPDiagnosticCounter * mNext; // pointer to point to the next entry in the list

Expand Down
28 changes: 15 additions & 13 deletions src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ namespace chip {
namespace Tracing {

namespace Diagnostics {
DiagnosticStorageImpl::DiagnosticStorageImpl(uint8_t * buffer, size_t bufferSize)
: mEndUserCircularBuffer(buffer, bufferSize) {}
DiagnosticStorageImpl::DiagnosticStorageImpl(uint8_t * buffer, size_t bufferSize) : mEndUserCircularBuffer(buffer, bufferSize) {}

DiagnosticStorageImpl & DiagnosticStorageImpl::GetInstance(uint8_t * buffer, size_t bufferSize) {
DiagnosticStorageImpl & DiagnosticStorageImpl::GetInstance(uint8_t * buffer, size_t bufferSize)
{
static DiagnosticStorageImpl instance(buffer, bufferSize);
return instance;
}
Expand All @@ -42,7 +42,6 @@ CHIP_ERROR DiagnosticStorageImpl::Store(DiagnosticEntry & diagnostic)
CircularTLVWriter writer;
writer.Init(mEndUserCircularBuffer);

// Start a TLV structure container (Anonymous)
TLVType outerContainer;
err = writer.StartContainer(AnonymousTag(), kTLVType_Structure, outerContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
Expand Down Expand Up @@ -98,21 +97,25 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
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)));
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to read next TLV element in outer container: %s", ErrorStr(err)));

// Check if the current element is a METRIC or TRACE container
if ((reader.GetType() == kTLVType_Structure) &&
(reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::METRIC) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::TRACE) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::COUNTER)))
(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()) {
if ((reader.GetLengthRead() - writer.GetLengthWritten()) < writer.GetRemainingFreeLength())
{
err = writer.CopyElement(reader);
if (err == CHIP_ERROR_BUFFER_TOO_SMALL) {
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 {
else
{
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current TLV");
break;
}
Expand All @@ -123,7 +126,6 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
reader.ExitContainer(outerReaderContainer);
return CHIP_ERROR_WRONG_TLV_TYPE;
}
// Exit the outer container
err = reader.ExitContainer(outerReaderContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to exit outer TLV container: %s", ErrorStr(err)));
Expand All @@ -136,11 +138,11 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)

err = writer.EndContainer(outWriterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to close outer container"));
// Finalize the writing process
err = writer.Finalize();
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to finalize TLV writing"));
payload.reduce_size(writer.GetLengthWritten());
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n", writer.GetLengthWritten());
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n",
writer.GetLengthWritten());
ChipLogProgress(DeviceLayer, "Retrieval successful");
return CHIP_NO_ERROR;
}
Expand Down
9 changes: 4 additions & 5 deletions src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#pragma once

#include "Diagnostics.h"
#include <lib/support/CHIPMem.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPMem.h>

namespace chip {
namespace Tracing {
Expand All @@ -30,15 +30,14 @@ using chip::TLV::TLVType;
class DiagnosticStorageImpl : public DiagnosticStorageInterface
{
public:
static DiagnosticStorageImpl & GetInstance(uint8_t * buffer = nullptr, size_t bufferSize = 0);

static DiagnosticStorageImpl& GetInstance(uint8_t * buffer = nullptr, size_t bufferSize = 0);

DiagnosticStorageImpl(const DiagnosticStorageImpl &) = delete;
DiagnosticStorageImpl(const DiagnosticStorageImpl &) = delete;
DiagnosticStorageImpl & operator=(const DiagnosticStorageImpl &) = delete;

CHIP_ERROR Store(DiagnosticEntry & diagnostic) override;

CHIP_ERROR Retrieve(MutableByteSpan &payload) override;
CHIP_ERROR Retrieve(MutableByteSpan & payload) override;

bool IsEmptyBuffer();

Expand Down
18 changes: 7 additions & 11 deletions src/tracing/esp32_diagnostic_trace/DiagnosticTracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
* limitations under the License.
*/

#include <esp_log.h>
#include <lib/core/CHIPError.h>
#include <tracing/backend.h>
#include <tracing/metric_event.h>
#include <tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h>
#include <esp_log.h>

#include <tracing/metric_event.h>

#include <memory>
namespace chip {
Expand All @@ -35,14 +34,11 @@ namespace Diagnostics {
class ESP32Diagnostics : public ::chip::Tracing::Backend
{
public:
ESP32Diagnostics(uint8_t *buffer, size_t buffer_size)
{
DiagnosticStorageImpl::GetInstance(buffer, buffer_size);
}
ESP32Diagnostics(uint8_t * buffer, size_t buffer_size) { DiagnosticStorageImpl::GetInstance(buffer, buffer_size); }

// Deleted copy constructor and assignment operator to prevent copying
ESP32Diagnostics(const ESP32Diagnostics&) = delete;
ESP32Diagnostics& operator=(const ESP32Diagnostics&) = delete;
ESP32Diagnostics(const ESP32Diagnostics &) = delete;
ESP32Diagnostics & operator=(const ESP32Diagnostics &) = delete;

void TraceBegin(const char * label, const char * group) override;

Expand All @@ -60,12 +56,12 @@ class ESP32Diagnostics : public ::chip::Tracing::Backend
void LogNodeDiscovered(NodeDiscoveredInfo &) override;
void LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo &) override;
void LogMetricEvent(const MetricEvent &) override;
void StoreDiagnostics(const char* label, const char* group);
void StoreDiagnostics(const char * label, const char * group);

private:
using ValueType = MetricEvent::Value::Type;
};

} // namespace Diagnostics
} // namespace Tracing
} // namespace chip
} // namespace chip
93 changes: 58 additions & 35 deletions src/tracing/esp32_diagnostic_trace/Diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#pragma once

#include <lib/core/CHIPError.h>
#include <lib/support/Span.h>
#include <lib/core/TLVCircularBuffer.h>
#include <lib/support/Span.h>

namespace chip {
namespace Tracing {
Expand All @@ -30,34 +30,36 @@ using namespace chip::TLV;

enum class DIAGNOSTICS_TAG
{
METRIC = 0,
TRACE = 1,
COUNTER = 2,
LABEL = 3,
GROUP = 4,
VALUE = 5,
TIMESTAMP = 6
METRIC = 0,
TRACE = 1,
COUNTER = 2,
LABEL = 3,
GROUP = 4,
VALUE = 5,
TIMESTAMP = 6
};

class DiagnosticEntry {
class DiagnosticEntry
{
public:
virtual ~DiagnosticEntry() = default;
virtual CHIP_ERROR Encode(CircularTLVWriter &writer) = 0;
virtual ~DiagnosticEntry() = default;
virtual CHIP_ERROR Encode(CircularTLVWriter & writer) = 0;
};

template<typename T>
class Metric : public DiagnosticEntry {
template <typename T>
class Metric : public DiagnosticEntry
{
public:
Metric(const char* label, T value, uint32_t timestamp)
: label_(label), value_(value), timestamp_(timestamp) {}
Metric(const char * label, T value, uint32_t timestamp) : label_(label), value_(value), timestamp_(timestamp) {}

Metric() {}

const char* GetLabel() const { return label_; }
const char * GetLabel() const { return label_; }
T GetValue() const { return value_; }
uint32_t GetTimestamp() const { return timestamp_; }

CHIP_ERROR Encode(CircularTLVWriter &writer) override {
CHIP_ERROR Encode(CircularTLVWriter & writer) override
{
CHIP_ERROR err = CHIP_NO_ERROR;
TLVType metricContainer;
err = writer.StartContainer(ContextTag(DIAGNOSTICS_TAG::METRIC), kTLVType_Structure, metricContainer);
Expand Down Expand Up @@ -87,23 +89,24 @@ class Metric : public DiagnosticEntry {
}

private:
const char* label_;
const char * label_;
T value_;
uint32_t timestamp_;
};

class Trace : public DiagnosticEntry {
class Trace : public DiagnosticEntry
{
public:
Trace(const char* label, const char* group, uint32_t timestamp)
: label_(label), group_(group), timestamp_(timestamp) {}
Trace(const char * label, const char * group, uint32_t timestamp) : label_(label), group_(group), timestamp_(timestamp) {}

Trace() {}

const char* GetLabel() const { return label_; }
const char * GetLabel() const { return label_; }
uint32_t GetTimestamp() const { return timestamp_; }
const char* GetGroup() const { return group_; }
const char * GetGroup() const { return group_; }

CHIP_ERROR Encode(CircularTLVWriter &writer) override {
CHIP_ERROR Encode(CircularTLVWriter & writer) override
{
CHIP_ERROR err = CHIP_NO_ERROR;
TLVType traceContainer;
err = writer.StartContainer(ContextTag(DIAGNOSTICS_TAG::TRACE), kTLVType_Structure, traceContainer);
Expand Down Expand Up @@ -133,23 +136,24 @@ class Trace : public DiagnosticEntry {
}

private:
const char* label_;
const char* group_;
const char * label_;
const char * group_;
uint32_t timestamp_;
};

class Counter : public DiagnosticEntry {
class Counter : public DiagnosticEntry
{
public:
Counter(const char* label, uint32_t count, uint32_t timestamp)
: label_(label), count_(count), timestamp_(timestamp) {}
Counter(const char * label, uint32_t count, uint32_t timestamp) : label_(label), count_(count), timestamp_(timestamp) {}

Counter() {}

uint32_t GetCount() const { return count_; }

uint32_t GetTimestamp() const { return timestamp_; }

CHIP_ERROR Encode(CircularTLVWriter &writer) override {
CHIP_ERROR Encode(CircularTLVWriter & writer) override
{
CHIP_ERROR err = CHIP_NO_ERROR;
TLVType counterContainer;
err = writer.StartContainer(ContextTag(DIAGNOSTICS_TAG::COUNTER), kTLVType_Structure, counterContainer);
Expand Down Expand Up @@ -179,18 +183,37 @@ class Counter : public DiagnosticEntry {
}

private:
const char* label_;
const char * label_;
uint32_t count_;
uint32_t timestamp_;
};

class DiagnosticStorageInterface {
/**
* @brief Interface for storing and retrieving diagnostic data.
*/
class DiagnosticStorageInterface
{
public:
/**
* @brief Virtual destructor for the interface.
*/
virtual ~DiagnosticStorageInterface() = default;

virtual CHIP_ERROR Store(DiagnosticEntry& diagnostic) = 0;

virtual CHIP_ERROR Retrieve(MutableByteSpan &payload) = 0;
/**
* @brief Stores a diagnostic entry.
* @param diagnostic Reference to a DiagnosticEntry object containing the
* diagnostic data to store.
* @return CHIP_ERROR Returns CHIP_NO_ERROR on success, or an appropriate error code on failure.
*/
virtual CHIP_ERROR Store(DiagnosticEntry & diagnostic) = 0;

/**
* @brief Retrieves diagnostic data as a payload.
* @param payload Reference to a MutableByteSpan where the retrieved
* diagnostic data will be stored.
* @return CHIP_ERROR Returns CHIP_NO_ERROR on success, or an appropriate error code on failure.
*/
virtual CHIP_ERROR Retrieve(MutableByteSpan & payload) = 0;
};

} // namespace Diagnostics
Expand Down

0 comments on commit 5ef9201

Please sign in to comment.