Skip to content

Commit 5ef9201

Browse files
committed
backend: Add description for diagnosticstorage interface, remove unncessary comments, format files
1 parent 8b29ccb commit 5ef9201

File tree

6 files changed

+89
-69
lines changed

6 files changed

+89
-69
lines changed

src/tracing/esp32_diagnostic_trace/Counter.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ ESPDiagnosticCounter * ESPDiagnosticCounter::GetInstance(const char * label)
4545
VerifyOrDie(ptr != nullptr);
4646

4747
ESPDiagnosticCounter * newInstance = new (ptr) ESPDiagnosticCounter(label);
48-
newInstance->mNext = mHead;
49-
mHead = newInstance;
48+
newInstance->mNext = mHead;
49+
mHead = newInstance;
5050

5151
return newInstance;
5252
}
@@ -61,7 +61,7 @@ void ESPDiagnosticCounter::ReportMetrics()
6161
CHIP_ERROR err = CHIP_NO_ERROR;
6262
Counter counter(label, instanceCount, esp_log_timestamp());
6363
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
64-
err = diagnosticStorage.Store(counter);
64+
err = diagnosticStorage.Store(counter);
6565
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data"));
6666
}
6767

src/tracing/esp32_diagnostic_trace/Counter.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
#pragma once
2020

21+
#include "tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h"
2122
#include <esp_diagnostics_metrics.h>
2223
#include <esp_log.h>
2324
#include <lib/support/CHIPMem.h>
2425
#include <lib/support/CHIPMemString.h>
2526
#include <string.h>
26-
#include "tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h"
2727

2828
using namespace chip::Tracing::Diagnostics;
2929

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

src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.cpp

+15-13
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ namespace chip {
2525
namespace Tracing {
2626

2727
namespace Diagnostics {
28-
DiagnosticStorageImpl::DiagnosticStorageImpl(uint8_t * buffer, size_t bufferSize)
29-
: mEndUserCircularBuffer(buffer, bufferSize) {}
28+
DiagnosticStorageImpl::DiagnosticStorageImpl(uint8_t * buffer, size_t bufferSize) : mEndUserCircularBuffer(buffer, bufferSize) {}
3029

31-
DiagnosticStorageImpl & DiagnosticStorageImpl::GetInstance(uint8_t * buffer, size_t bufferSize) {
30+
DiagnosticStorageImpl & DiagnosticStorageImpl::GetInstance(uint8_t * buffer, size_t bufferSize)
31+
{
3232
static DiagnosticStorageImpl instance(buffer, bufferSize);
3333
return instance;
3434
}
@@ -42,7 +42,6 @@ CHIP_ERROR DiagnosticStorageImpl::Store(DiagnosticEntry & diagnostic)
4242
CircularTLVWriter writer;
4343
writer.Init(mEndUserCircularBuffer);
4444

45-
// Start a TLV structure container (Anonymous)
4645
TLVType outerContainer;
4746
err = writer.StartContainer(AnonymousTag(), kTLVType_Structure, outerContainer);
4847
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
@@ -98,21 +97,25 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
9897
ChipLogError(DeviceLayer, "Failed to enter outer TLV container: %s", ErrorStr(err)));
9998

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

103-
// Check if the current element is a METRIC or TRACE container
104103
if ((reader.GetType() == kTLVType_Structure) &&
105-
(reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::METRIC) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::TRACE) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::COUNTER)))
104+
(reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::METRIC) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::TRACE) ||
105+
reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::COUNTER)))
106106
{
107-
if ((reader.GetLengthRead() - writer.GetLengthWritten()) < writer.GetRemainingFreeLength()) {
107+
if ((reader.GetLengthRead() - writer.GetLengthWritten()) < writer.GetRemainingFreeLength())
108+
{
108109
err = writer.CopyElement(reader);
109-
if (err == CHIP_ERROR_BUFFER_TOO_SMALL) {
110+
if (err == CHIP_ERROR_BUFFER_TOO_SMALL)
111+
{
110112
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current element");
111113
break;
112114
}
113115
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to copy TLV element"));
114116
}
115-
else {
117+
else
118+
{
116119
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current TLV");
117120
break;
118121
}
@@ -123,7 +126,6 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
123126
reader.ExitContainer(outerReaderContainer);
124127
return CHIP_ERROR_WRONG_TLV_TYPE;
125128
}
126-
// Exit the outer container
127129
err = reader.ExitContainer(outerReaderContainer);
128130
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
129131
ChipLogError(DeviceLayer, "Failed to exit outer TLV container: %s", ErrorStr(err)));
@@ -136,11 +138,11 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
136138

137139
err = writer.EndContainer(outWriterContainer);
138140
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to close outer container"));
139-
// Finalize the writing process
140141
err = writer.Finalize();
141142
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to finalize TLV writing"));
142143
payload.reduce_size(writer.GetLengthWritten());
143-
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n", writer.GetLengthWritten());
144+
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n",
145+
writer.GetLengthWritten());
144146
ChipLogProgress(DeviceLayer, "Retrieval successful");
145147
return CHIP_NO_ERROR;
146148
}

src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#pragma once
2020

2121
#include "Diagnostics.h"
22-
#include <lib/support/CHIPMem.h>
2322
#include <lib/core/CHIPError.h>
23+
#include <lib/support/CHIPMem.h>
2424

2525
namespace chip {
2626
namespace Tracing {
@@ -30,15 +30,14 @@ using chip::TLV::TLVType;
3030
class DiagnosticStorageImpl : public DiagnosticStorageInterface
3131
{
3232
public:
33+
static DiagnosticStorageImpl & GetInstance(uint8_t * buffer = nullptr, size_t bufferSize = 0);
3334

34-
static DiagnosticStorageImpl& GetInstance(uint8_t * buffer = nullptr, size_t bufferSize = 0);
35-
36-
DiagnosticStorageImpl(const DiagnosticStorageImpl &) = delete;
35+
DiagnosticStorageImpl(const DiagnosticStorageImpl &) = delete;
3736
DiagnosticStorageImpl & operator=(const DiagnosticStorageImpl &) = delete;
3837

3938
CHIP_ERROR Store(DiagnosticEntry & diagnostic) override;
4039

41-
CHIP_ERROR Retrieve(MutableByteSpan &payload) override;
40+
CHIP_ERROR Retrieve(MutableByteSpan & payload) override;
4241

4342
bool IsEmptyBuffer();
4443

src/tracing/esp32_diagnostic_trace/DiagnosticTracing.h

+7-11
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
* limitations under the License.
1919
*/
2020

21+
#include <esp_log.h>
2122
#include <lib/core/CHIPError.h>
2223
#include <tracing/backend.h>
23-
#include <tracing/metric_event.h>
2424
#include <tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h>
25-
#include <esp_log.h>
26-
25+
#include <tracing/metric_event.h>
2726

2827
#include <memory>
2928
namespace chip {
@@ -35,14 +34,11 @@ namespace Diagnostics {
3534
class ESP32Diagnostics : public ::chip::Tracing::Backend
3635
{
3736
public:
38-
ESP32Diagnostics(uint8_t *buffer, size_t buffer_size)
39-
{
40-
DiagnosticStorageImpl::GetInstance(buffer, buffer_size);
41-
}
37+
ESP32Diagnostics(uint8_t * buffer, size_t buffer_size) { DiagnosticStorageImpl::GetInstance(buffer, buffer_size); }
4238

4339
// Deleted copy constructor and assignment operator to prevent copying
44-
ESP32Diagnostics(const ESP32Diagnostics&) = delete;
45-
ESP32Diagnostics& operator=(const ESP32Diagnostics&) = delete;
40+
ESP32Diagnostics(const ESP32Diagnostics &) = delete;
41+
ESP32Diagnostics & operator=(const ESP32Diagnostics &) = delete;
4642

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

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

6561
private:
6662
using ValueType = MetricEvent::Value::Type;
6763
};
6864

6965
} // namespace Diagnostics
7066
} // namespace Tracing
71-
} // namespace chip
67+
} // namespace chip

src/tracing/esp32_diagnostic_trace/Diagnostics.h

+58-35
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#pragma once
2020

2121
#include <lib/core/CHIPError.h>
22-
#include <lib/support/Span.h>
2322
#include <lib/core/TLVCircularBuffer.h>
23+
#include <lib/support/Span.h>
2424

2525
namespace chip {
2626
namespace Tracing {
@@ -30,34 +30,36 @@ using namespace chip::TLV;
3030

3131
enum class DIAGNOSTICS_TAG
3232
{
33-
METRIC = 0,
34-
TRACE = 1,
35-
COUNTER = 2,
36-
LABEL = 3,
37-
GROUP = 4,
38-
VALUE = 5,
39-
TIMESTAMP = 6
33+
METRIC = 0,
34+
TRACE = 1,
35+
COUNTER = 2,
36+
LABEL = 3,
37+
GROUP = 4,
38+
VALUE = 5,
39+
TIMESTAMP = 6
4040
};
4141

42-
class DiagnosticEntry {
42+
class DiagnosticEntry
43+
{
4344
public:
44-
virtual ~DiagnosticEntry() = default;
45-
virtual CHIP_ERROR Encode(CircularTLVWriter &writer) = 0;
45+
virtual ~DiagnosticEntry() = default;
46+
virtual CHIP_ERROR Encode(CircularTLVWriter & writer) = 0;
4647
};
4748

48-
template<typename T>
49-
class Metric : public DiagnosticEntry {
49+
template <typename T>
50+
class Metric : public DiagnosticEntry
51+
{
5052
public:
51-
Metric(const char* label, T value, uint32_t timestamp)
52-
: label_(label), value_(value), timestamp_(timestamp) {}
53+
Metric(const char * label, T value, uint32_t timestamp) : label_(label), value_(value), timestamp_(timestamp) {}
5354

5455
Metric() {}
5556

56-
const char* GetLabel() const { return label_; }
57+
const char * GetLabel() const { return label_; }
5758
T GetValue() const { return value_; }
5859
uint32_t GetTimestamp() const { return timestamp_; }
5960

60-
CHIP_ERROR Encode(CircularTLVWriter &writer) override {
61+
CHIP_ERROR Encode(CircularTLVWriter & writer) override
62+
{
6163
CHIP_ERROR err = CHIP_NO_ERROR;
6264
TLVType metricContainer;
6365
err = writer.StartContainer(ContextTag(DIAGNOSTICS_TAG::METRIC), kTLVType_Structure, metricContainer);
@@ -87,23 +89,24 @@ class Metric : public DiagnosticEntry {
8789
}
8890

8991
private:
90-
const char* label_;
92+
const char * label_;
9193
T value_;
9294
uint32_t timestamp_;
9395
};
9496

95-
class Trace : public DiagnosticEntry {
97+
class Trace : public DiagnosticEntry
98+
{
9699
public:
97-
Trace(const char* label, const char* group, uint32_t timestamp)
98-
: label_(label), group_(group), timestamp_(timestamp) {}
100+
Trace(const char * label, const char * group, uint32_t timestamp) : label_(label), group_(group), timestamp_(timestamp) {}
99101

100102
Trace() {}
101103

102-
const char* GetLabel() const { return label_; }
104+
const char * GetLabel() const { return label_; }
103105
uint32_t GetTimestamp() const { return timestamp_; }
104-
const char* GetGroup() const { return group_; }
106+
const char * GetGroup() const { return group_; }
105107

106-
CHIP_ERROR Encode(CircularTLVWriter &writer) override {
108+
CHIP_ERROR Encode(CircularTLVWriter & writer) override
109+
{
107110
CHIP_ERROR err = CHIP_NO_ERROR;
108111
TLVType traceContainer;
109112
err = writer.StartContainer(ContextTag(DIAGNOSTICS_TAG::TRACE), kTLVType_Structure, traceContainer);
@@ -133,23 +136,24 @@ class Trace : public DiagnosticEntry {
133136
}
134137

135138
private:
136-
const char* label_;
137-
const char* group_;
139+
const char * label_;
140+
const char * group_;
138141
uint32_t timestamp_;
139142
};
140143

141-
class Counter : public DiagnosticEntry {
144+
class Counter : public DiagnosticEntry
145+
{
142146
public:
143-
Counter(const char* label, uint32_t count, uint32_t timestamp)
144-
: label_(label), count_(count), timestamp_(timestamp) {}
147+
Counter(const char * label, uint32_t count, uint32_t timestamp) : label_(label), count_(count), timestamp_(timestamp) {}
145148

146149
Counter() {}
147150

148151
uint32_t GetCount() const { return count_; }
149152

150153
uint32_t GetTimestamp() const { return timestamp_; }
151154

152-
CHIP_ERROR Encode(CircularTLVWriter &writer) override {
155+
CHIP_ERROR Encode(CircularTLVWriter & writer) override
156+
{
153157
CHIP_ERROR err = CHIP_NO_ERROR;
154158
TLVType counterContainer;
155159
err = writer.StartContainer(ContextTag(DIAGNOSTICS_TAG::COUNTER), kTLVType_Structure, counterContainer);
@@ -179,18 +183,37 @@ class Counter : public DiagnosticEntry {
179183
}
180184

181185
private:
182-
const char* label_;
186+
const char * label_;
183187
uint32_t count_;
184188
uint32_t timestamp_;
185189
};
186190

187-
class DiagnosticStorageInterface {
191+
/**
192+
* @brief Interface for storing and retrieving diagnostic data.
193+
*/
194+
class DiagnosticStorageInterface
195+
{
188196
public:
197+
/**
198+
* @brief Virtual destructor for the interface.
199+
*/
189200
virtual ~DiagnosticStorageInterface() = default;
190201

191-
virtual CHIP_ERROR Store(DiagnosticEntry& diagnostic) = 0;
192-
193-
virtual CHIP_ERROR Retrieve(MutableByteSpan &payload) = 0;
202+
/**
203+
* @brief Stores a diagnostic entry.
204+
* @param diagnostic Reference to a DiagnosticEntry object containing the
205+
* diagnostic data to store.
206+
* @return CHIP_ERROR Returns CHIP_NO_ERROR on success, or an appropriate error code on failure.
207+
*/
208+
virtual CHIP_ERROR Store(DiagnosticEntry & diagnostic) = 0;
209+
210+
/**
211+
* @brief Retrieves diagnostic data as a payload.
212+
* @param payload Reference to a MutableByteSpan where the retrieved
213+
* diagnostic data will be stored.
214+
* @return CHIP_ERROR Returns CHIP_NO_ERROR on success, or an appropriate error code on failure.
215+
*/
216+
virtual CHIP_ERROR Retrieve(MutableByteSpan & payload) = 0;
194217
};
195218

196219
} // namespace Diagnostics

0 commit comments

Comments
 (0)