Skip to content

Commit 7178050

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

File tree

9 files changed

+132
-109
lines changed

9 files changed

+132
-109
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

+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.cpp

+11-7
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: {
@@ -149,21 +149,25 @@ void ESP32Diagnostics::TraceCounter(const char * label)
149149
::Diagnostics::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)