Skip to content

Commit d6e27ed

Browse files
pimpalemaheshesp
authored and
esp
committed
backend: update methods to propagate error to level up
- pass diagnostic storage instance as a pointer to backend - replace ESPLog statement with ChipLog for logging diagnostic-storage: update IsEmptyBuffer method to IsBufferEmpty - change diagnostic TAG enum class
1 parent 775dbb4 commit d6e27ed

File tree

8 files changed

+38
-39
lines changed

8 files changed

+38
-39
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ CHIP_ERROR LogProvider::PrepareLogContextForIntent(LogContext * context, IntentE
124124
VerifyOrReturnError(mStorageInstance != nullptr, CHIP_ERROR_INTERNAL,
125125
ChipLogError(DeviceLayer, "Diagnostic Storage instance cannot be null."));
126126
MutableByteSpan endUserSupportSpan(retrievalBuffer, CONFIG_RETRIEVAL_BUFFER_SIZE);
127-
VerifyOrReturnError(!mStorageInstance->IsEmptyBuffer(), CHIP_ERROR_NOT_FOUND,
127+
VerifyOrReturnError(!mStorageInstance->IsBufferEmpty(), CHIP_ERROR_NOT_FOUND,
128128
ChipLogError(DeviceLayer, "Empty Diagnostic buffer"));
129129
// Retrieve data from the diagnostic storage
130130
CHIP_ERROR err = mStorageInstance->Retrieve(endUserSupportSpan, sReadEntries);

examples/temperature-measurement-app/esp32/main/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static void InitServer(intptr_t context)
8484
Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config
8585
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
8686
diagnosticStorage.Init(endUserBuffer, CONFIG_END_USER_BUFFER_SIZE);
87-
static ESP32Diagnostics diagnosticBackend(diagnosticStorage);
87+
static ESP32Diagnostics diagnosticBackend(&diagnosticStorage);
8888
Tracing::Register(diagnosticBackend);
8989
#endif // CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
9090
}

src/tracing/esp32_diagnostic_trace/Counter.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ uint32_t ESPDiagnosticCounter::GetInstanceCount(const char * label) const
4242
return mCounterList[label];
4343
}
4444

45-
void ESPDiagnosticCounter::ReportMetrics(const char * label, DiagnosticStorageInterface & mStorageInstance)
45+
CHIP_ERROR ESPDiagnosticCounter::ReportMetrics(const char * label, DiagnosticStorageInterface * mStorageInstance)
4646
{
4747
CHIP_ERROR err = CHIP_NO_ERROR;
48+
VerifyOrReturnError(mStorageInstance != nullptr, err, ChipLogError(DeviceLayer, "Diagnostic Storage Instance cannot be NULL"));
4849
Diagnostic<uint32_t> counter(label, GetInstanceCount(label), esp_log_timestamp());
49-
err = mStorageInstance.Store(counter);
50-
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data"));
50+
err = mStorageInstance->Store(counter);
51+
return err;
5152
}
5253

5354
} // namespace Diagnostics

src/tracing/esp32_diagnostic_trace/Counter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ESPDiagnosticCounter
4545

4646
uint32_t GetInstanceCount(const char * label) const;
4747

48-
void ReportMetrics(const char * label, DiagnosticStorageInterface & mStorageInstance);
48+
CHIP_ERROR ReportMetrics(const char * label, DiagnosticStorageInterface * mStorageInstance);
4949

5050
private:
5151
ESPDiagnosticCounter() {}

src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h

+3-11
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,11 @@ class CircularDiagnosticBuffer : public chip::TLV::TLVCircularBuffer, public Dia
2828
public:
2929
CircularDiagnosticBuffer(uint8_t * buffer, size_t bufferLength) : chip::TLV::TLVCircularBuffer(buffer, bufferLength) {}
3030

31-
// Delete copy constructor and assignment operator to ensure singleton
32-
CircularDiagnosticBuffer(const CircularDiagnosticBuffer &) = delete;
33-
CircularDiagnosticBuffer & operator=(const CircularDiagnosticBuffer &) = delete;
34-
3531
CHIP_ERROR Store(const DiagnosticEntry & entry) override
3632
{
33+
CHIP_ERROR err = CHIP_NO_ERROR;
3734
mWriter.Init(*this);
38-
39-
CHIP_ERROR err = entry.Encode(mWriter);
40-
if (err != CHIP_NO_ERROR)
41-
{
42-
ChipLogError(DeviceLayer, "Failed to write entry: %s", chip::ErrorStr(err));
43-
}
35+
ReturnLogErrorOnFailure(entry.Encode(mWriter));
4436
return err;
4537
}
4638

@@ -87,7 +79,7 @@ class CircularDiagnosticBuffer : public chip::TLV::TLVCircularBuffer, public Dia
8779
return CHIP_NO_ERROR;
8880
}
8981

90-
bool IsEmptyBuffer() override { return DataLength() == 0; }
82+
bool IsBufferEmpty() override { return DataLength() == 0; }
9183

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

src/tracing/esp32_diagnostic_trace/DiagnosticTracing.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,33 @@ void ESP32Diagnostics::LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo & info) {}
102102
void ESP32Diagnostics::LogMetricEvent(const MetricEvent & event)
103103
{
104104
CHIP_ERROR err = CHIP_NO_ERROR;
105+
VerifyOrReturn(mStorageInstance != nullptr, ChipLogError(DeviceLayer, "Diagnostic Storage Instance cannot be NULL"));
105106
switch (event.ValueType())
106107
{
107108
case ValueType::kInt32: {
108-
ESP_LOGI("mtr", "The value of %s is %ld ", event.key(), event.ValueInt32());
109+
ChipLogProgress(DeviceLayer, "The value of %s is %" PRId32, event.key(), event.ValueInt32());
109110
Diagnostic<int32_t> metric(event.key(), event.ValueInt32(), esp_log_timestamp());
110-
err = mStorageInstance.Store(metric);
111+
err = mStorageInstance->Store(metric);
111112
}
112113
break;
113114

114115
case ValueType::kUInt32: {
115-
ESP_LOGI("mtr", "The value of %s is %lu ", event.key(), event.ValueUInt32());
116+
ChipLogProgress(DeviceLayer, "The value of %s is %" PRId32, event.key(), event.ValueUInt32());
116117
Diagnostic<uint32_t> metric(event.key(), event.ValueUInt32(), esp_log_timestamp());
117-
err = mStorageInstance.Store(metric);
118+
err = mStorageInstance->Store(metric);
118119
}
119120
break;
120121

121122
case ValueType::kChipErrorCode:
122-
ESP_LOGI("mtr", "The value of %s is error with code %lu ", event.key(), event.ValueErrorCode());
123+
ChipLogProgress(DeviceLayer, "The value of %s is error with code %lu ", event.key(), event.ValueErrorCode());
123124
break;
124125

125126
case ValueType::kUndefined:
126-
ESP_LOGI("mtr", "The value of %s is undefined", event.key());
127+
ChipLogProgress(DeviceLayer, "The value of %s is undefined", event.key());
127128
break;
128129

129130
default:
130-
ESP_LOGI("mtr", "The value of %s is of an UNKNOWN TYPE", event.key());
131+
ChipLogProgress(DeviceLayer, "The value of %s is of an UNKNOWN TYPE", event.key());
131132
break;
132133
}
133134

@@ -136,14 +137,16 @@ void ESP32Diagnostics::LogMetricEvent(const MetricEvent & event)
136137

137138
void ESP32Diagnostics::TraceCounter(const char * label)
138139
{
139-
ESPDiagnosticCounter::GetInstance(label).ReportMetrics(label, mStorageInstance);
140+
CHIP_ERROR err = ESPDiagnosticCounter::GetInstance(label).ReportMetrics(label, mStorageInstance);
141+
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter Diagnostic data"));
140142
}
141143

142144
void ESP32Diagnostics::TraceBegin(const char * label, const char * group)
143145
{
144146
if (IsPermitted(group))
145147
{
146-
StoreDiagnostics(label, group);
148+
CHIP_ERROR err = StoreDiagnostics(label, group);
149+
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Trace Diagnostic data"));
147150
}
148151
}
149152

@@ -153,15 +156,18 @@ void ESP32Diagnostics::TraceInstant(const char * label, const char * value)
153156
{
154157
if (!IsPermitted(value))
155158
{
156-
StoreDiagnostics(label, value);
159+
CHIP_ERROR err = StoreDiagnostics(label, value);
160+
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Trace Diagnostic data"));
157161
}
158162
}
159163

160-
void ESP32Diagnostics::StoreDiagnostics(const char * label, const char * group)
164+
CHIP_ERROR ESP32Diagnostics::StoreDiagnostics(const char * label, const char * group)
161165
{
166+
CHIP_ERROR err = CHIP_NO_ERROR;
167+
VerifyOrReturnError(mStorageInstance != nullptr, err, ChipLogError(DeviceLayer, "Diagnostic Storage Instance cannot be NULL"));
162168
Diagnostic<const char *> trace(label, group, esp_log_timestamp());
163-
VerifyOrReturn(mStorageInstance.Store(trace) == CHIP_NO_ERROR,
164-
ChipLogError(DeviceLayer, "Failed to store Trace Diagnostic data"));
169+
err = mStorageInstance->Store(trace);
170+
return err;
165171
}
166172

167173
} // namespace Diagnostics

src/tracing/esp32_diagnostic_trace/DiagnosticTracing.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace Diagnostics {
3232
class ESP32Diagnostics : public ::chip::Tracing::Backend
3333
{
3434
public:
35-
ESP32Diagnostics(DiagnosticStorageInterface & storageInstance) : mStorageInstance(storageInstance) {}
35+
ESP32Diagnostics(DiagnosticStorageInterface * storageInstance) : mStorageInstance(storageInstance) {}
3636

3737
// Deleted copy constructor and assignment operator to prevent copying
3838
ESP32Diagnostics(const ESP32Diagnostics &) = delete;
@@ -54,11 +54,11 @@ class ESP32Diagnostics : public ::chip::Tracing::Backend
5454
void LogNodeDiscovered(NodeDiscoveredInfo &) override;
5555
void LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo &) override;
5656
void LogMetricEvent(const MetricEvent &) override;
57-
void StoreDiagnostics(const char * label, const char * group);
5857

5958
private:
6059
using ValueType = MetricEvent::Value::Type;
61-
DiagnosticStorageInterface & mStorageInstance;
60+
DiagnosticStorageInterface * mStorageInstance;
61+
CHIP_ERROR StoreDiagnostics(const char * label, const char * group);
6262
};
6363

6464
} // namespace Diagnostics

src/tracing/esp32_diagnostic_trace/Diagnostics.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Tracing {
2626
namespace Diagnostics {
2727

2828
// Diagnostic TAGs
29-
enum D_TAG
29+
enum class DiagTag : uint8_t
3030
{
3131
TIMESTAMP = 0,
3232
LABEL,
@@ -68,15 +68,15 @@ class Diagnostic : public DiagnosticEntry
6868
chip::TLV::TLVType DiagnosticOuterContainer = chip::TLV::kTLVType_NotSpecified;
6969
ReturnErrorOnFailure(
7070
writer.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, DiagnosticOuterContainer));
71-
ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(D_TAG::TIMESTAMP), timestamp_));
72-
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(D_TAG::LABEL), label_));
71+
ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(DiagTag::TIMESTAMP), timestamp_));
72+
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(DiagTag::LABEL), label_));
7373
if constexpr (std::is_same_v<T, const char *>)
7474
{
75-
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(D_TAG::VALUE), value_));
75+
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(DiagTag::VALUE), value_));
7676
}
7777
else
7878
{
79-
ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(D_TAG::VALUE), value_));
79+
ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(DiagTag::VALUE), value_));
8080
}
8181
ReturnErrorOnFailure(writer.EndContainer(DiagnosticOuterContainer));
8282
ReturnErrorOnFailure(writer.Finalize());
@@ -121,7 +121,7 @@ class DiagnosticStorageInterface
121121
* @brief Checks if the storage buffer is empty.
122122
* @return bool true if the buffer contains no stored diagnostic data, otherwise false.
123123
*/
124-
virtual bool IsEmptyBuffer() = 0;
124+
virtual bool IsBufferEmpty() = 0;
125125

126126
/**
127127
* @brief Retrieves the size of the data currently stored in the buffer.

0 commit comments

Comments
 (0)