Skip to content

Commit ed14abb

Browse files
committed
backend: Add description for diagnosticstorage interface, remove unncessary comments, format files, namespace changes
1 parent 46217f2 commit ed14abb

File tree

9 files changed

+159
-133
lines changed

9 files changed

+159
-133
lines changed

examples/temperature-measurement-app/esp32/main/diagnostic-logs-provider-delegate-impl.cpp

+30-31
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,14 @@ size_t LogProvider::GetSizeForIntent(IntentEnum intent)
7575
{
7676
switch (intent)
7777
{
78-
case IntentEnum::kEndUserSupport:
79-
{
80-
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
81-
return DIAGNOSTIC_BUFFER_SIZE;
82-
#else
83-
return static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart);
84-
#endif
85-
}
86-
break;
78+
case IntentEnum::kEndUserSupport: {
79+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
80+
return DIAGNOSTIC_BUFFER_SIZE;
81+
#else
82+
return static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart);
83+
#endif
84+
}
85+
break;
8786
case IntentEnum::kNetworkDiag:
8887
return static_cast<size_t>(networkDiagnosticLogEnd - networkDiagnosticLogStart);
8988
case IntentEnum::kCrashLogs:
@@ -115,28 +114,28 @@ CHIP_ERROR LogProvider::PrepareLogContextForIntent(LogContext * context, IntentE
115114
switch (intent)
116115
{
117116
case IntentEnum::kEndUserSupport: {
118-
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
119-
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
120-
MutableByteSpan endUserSupportSpan(endUserBuffer, DIAGNOSTIC_BUFFER_SIZE);
121-
122-
if (diagnosticStorage.IsEmptyBuffer())
123-
{
124-
ChipLogError(DeviceLayer, "Empty Diagnostic buffer");
125-
return CHIP_ERROR_NOT_FOUND;
126-
}
127-
// Retrieve data from the diagnostic storage
128-
CHIP_ERROR err = diagnosticStorage.Retrieve(endUserSupportSpan);
129-
if (err != CHIP_NO_ERROR)
130-
{
131-
ChipLogError(DeviceLayer, "Failed to retrieve data: %s", chip::ErrorStr(err));
132-
return err;
133-
}
134-
// Now, assign the span to the EndUserSupport object or whatever is required
135-
context->EndUserSupport.span = endUserSupportSpan;
136-
#else
137-
context->EndUserSupport.span =
138-
ByteSpan(&endUserSupportLogStart[0], static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart));
139-
#endif
117+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
118+
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
119+
MutableByteSpan endUserSupportSpan(endUserBuffer, DIAGNOSTIC_BUFFER_SIZE);
120+
121+
if (diagnosticStorage.IsEmptyBuffer())
122+
{
123+
ChipLogError(DeviceLayer, "Empty Diagnostic buffer");
124+
return CHIP_ERROR_NOT_FOUND;
125+
}
126+
// Retrieve data from the diagnostic storage
127+
CHIP_ERROR err = diagnosticStorage.Retrieve(endUserSupportSpan);
128+
if (err != CHIP_NO_ERROR)
129+
{
130+
ChipLogError(DeviceLayer, "Failed to retrieve data: %s", chip::ErrorStr(err));
131+
return err;
132+
}
133+
// Now, assign the span to the EndUserSupport object or whatever is required
134+
context->EndUserSupport.span = endUserSupportSpan;
135+
#else
136+
context->EndUserSupport.span =
137+
ByteSpan(&endUserSupportLogStart[0], static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart));
138+
#endif
140139
}
141140
break;
142141

src/tracing/esp32_diagnostic_trace/BUILD.gn

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ static_library("backend") {
2727
sources = [
2828
"Counter.cpp",
2929
"Counter.h",
30-
"DiagnosticTracing.cpp",
31-
"DiagnosticTracing.h",
3230
"DiagnosticStorageManager.cpp",
3331
"DiagnosticStorageManager.h",
32+
"DiagnosticTracing.cpp",
33+
"DiagnosticTracing.h",
3434
"Diagnostics.h",
3535
]
3636

src/tracing/esp32_diagnostic_trace/Counter.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include <string.h>
2020
#include <tracing/esp32_diagnostic_trace/Counter.h>
2121

22-
using namespace chip;
23-
22+
namespace chip {
23+
namespace Tracing {
2424
namespace Diagnostics {
2525

2626
// This is a one time allocation for counters. It is not supposed to be freed.
@@ -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,8 +61,10 @@ 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

6868
} // namespace Diagnostics
69+
} // namespace Tracing
70+
} // namespace chip

src/tracing/esp32_diagnostic_trace/Counter.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
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"
27-
28-
using namespace chip::Tracing::Diagnostics;
2927

28+
namespace chip {
29+
namespace Tracing {
3030
namespace Diagnostics {
3131

3232
/**
@@ -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

@@ -56,3 +56,5 @@ class ESPDiagnosticCounter
5656
};
5757

5858
} // namespace Diagnostics
59+
} // namespace Tracing
60+
} // namespace chip

src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.cpp

+16-13
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323

2424
namespace chip {
2525
namespace Tracing {
26+
using namespace chip::TLV;
2627

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

31-
DiagnosticStorageImpl & DiagnosticStorageImpl::GetInstance(uint8_t * buffer, size_t bufferSize) {
31+
DiagnosticStorageImpl & DiagnosticStorageImpl::GetInstance(uint8_t * buffer, size_t bufferSize)
32+
{
3233
static DiagnosticStorageImpl instance(buffer, bufferSize);
3334
return instance;
3435
}
@@ -42,7 +43,6 @@ CHIP_ERROR DiagnosticStorageImpl::Store(DiagnosticEntry & diagnostic)
4243
CircularTLVWriter writer;
4344
writer.Init(mEndUserCircularBuffer);
4445

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

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

103-
// Check if the current element is a METRIC or TRACE container
104104
if ((reader.GetType() == kTLVType_Structure) &&
105-
(reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::METRIC) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::TRACE) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::COUNTER)))
105+
(reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::METRIC) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::TRACE) ||
106+
reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::COUNTER)))
106107
{
107-
if ((reader.GetLengthRead() - writer.GetLengthWritten()) < writer.GetRemainingFreeLength()) {
108+
if ((reader.GetLengthRead() - writer.GetLengthWritten()) < writer.GetRemainingFreeLength())
109+
{
108110
err = writer.CopyElement(reader);
109-
if (err == CHIP_ERROR_BUFFER_TOO_SMALL) {
111+
if (err == CHIP_ERROR_BUFFER_TOO_SMALL)
112+
{
110113
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current element");
111114
break;
112115
}
113116
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to copy TLV element"));
114117
}
115-
else {
118+
else
119+
{
116120
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current TLV");
117121
break;
118122
}
@@ -123,7 +127,6 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
123127
reader.ExitContainer(outerReaderContainer);
124128
return CHIP_ERROR_WRONG_TLV_TYPE;
125129
}
126-
// Exit the outer container
127130
err = reader.ExitContainer(outerReaderContainer);
128131
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
129132
ChipLogError(DeviceLayer, "Failed to exit outer TLV container: %s", ErrorStr(err)));
@@ -136,11 +139,11 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
136139

137140
err = writer.EndContainer(outWriterContainer);
138141
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to close outer container"));
139-
// Finalize the writing process
140142
err = writer.Finalize();
141143
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to finalize TLV writing"));
142144
payload.reduce_size(writer.GetLengthWritten());
143-
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n", writer.GetLengthWritten());
145+
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n",
146+
writer.GetLengthWritten());
144147
ChipLogProgress(DeviceLayer, "Retrieval successful");
145148
return CHIP_NO_ERROR;
146149
}

src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h

+5-8
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,23 @@
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 {
2727
namespace Diagnostics {
28-
using namespace chip::Platform;
29-
using chip::TLV::TLVType;
3028
class DiagnosticStorageImpl : public DiagnosticStorageInterface
3129
{
3230
public:
31+
static DiagnosticStorageImpl & GetInstance(uint8_t * buffer = nullptr, size_t bufferSize = 0);
3332

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

3936
CHIP_ERROR Store(DiagnosticEntry & diagnostic) override;
4037

41-
CHIP_ERROR Retrieve(MutableByteSpan &payload) override;
38+
CHIP_ERROR Retrieve(MutableByteSpan & payload) override;
4239

4340
bool IsEmptyBuffer();
4441

@@ -47,7 +44,7 @@ class DiagnosticStorageImpl : public DiagnosticStorageInterface
4744
DiagnosticStorageImpl();
4845
~DiagnosticStorageImpl();
4946

50-
TLVCircularBuffer mEndUserCircularBuffer;
47+
chip::TLV::TLVCircularBuffer mEndUserCircularBuffer;
5148
};
5249
} // namespace Diagnostics
5350
} // namespace Tracing

src/tracing/esp32_diagnostic_trace/DiagnosticTracing.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void ESP32Diagnostics::LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo & info) {}
111111
void ESP32Diagnostics::LogMetricEvent(const MetricEvent & event)
112112
{
113113
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
114-
CHIP_ERROR err = CHIP_NO_ERROR;
114+
CHIP_ERROR err = CHIP_NO_ERROR;
115115
switch (event.ValueType())
116116
{
117117
case ValueType::kInt32: {
@@ -146,24 +146,28 @@ void ESP32Diagnostics::LogMetricEvent(const MetricEvent & event)
146146

147147
void ESP32Diagnostics::TraceCounter(const char * label)
148148
{
149-
::Diagnostics::ESPDiagnosticCounter::GetInstance(label)->ReportMetrics();
149+
ESPDiagnosticCounter::GetInstance(label)->ReportMetrics();
150150
}
151151

152-
void ESP32Diagnostics::TraceBegin(const char * label, const char * group) {
152+
void ESP32Diagnostics::TraceBegin(const char * label, const char * group)
153+
{
153154
StoreDiagnostics(label, group);
154155
}
155156

156-
void ESP32Diagnostics::TraceEnd(const char * label, const char * group) {
157+
void ESP32Diagnostics::TraceEnd(const char * label, const char * group)
158+
{
157159
StoreDiagnostics(label, group);
158160
}
159161

160-
void ESP32Diagnostics::TraceInstant(const char * label, const char * group) {
162+
void ESP32Diagnostics::TraceInstant(const char * label, const char * group)
163+
{
161164
StoreDiagnostics(label, group);
162165
}
163166

164-
void ESP32Diagnostics::StoreDiagnostics(const char* label, const char* group) {
165-
CHIP_ERROR err = CHIP_NO_ERROR;
166-
HashValue hashValue = MurmurHash(group);
167+
void ESP32Diagnostics::StoreDiagnostics(const char * label, const char * group)
168+
{
169+
CHIP_ERROR err = CHIP_NO_ERROR;
170+
HashValue hashValue = MurmurHash(group);
167171
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
168172
if (IsPermitted(hashValue))
169173
{

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

0 commit comments

Comments
 (0)