Skip to content

Commit 0916562

Browse files
committed
diagnostics: add decode method
1 parent 427ae7b commit 0916562

File tree

3 files changed

+96
-12
lines changed

3 files changed

+96
-12
lines changed

src/tracing/esp32_diagnostic_trace/Counter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ CHIP_ERROR ESPDiagnosticCounter::ReportMetrics(const char * label, DiagnosticSto
4646
{
4747
VerifyOrReturnError(storageInstance != nullptr, CHIP_ERROR_INCORRECT_STATE,
4848
ChipLogError(DeviceLayer, "Diagnostic Storage Instance cannot be NULL"));
49-
Diagnostic<uint32_t> counter(label, GetInstanceCount(label), esp_log_timestamp());
49+
Diagnostic<uint32_t> counter(const_cast<char *>(label), GetInstanceCount(label), esp_log_timestamp());
5050
return storageInstance->Store(counter);
5151
}
5252

src/tracing/esp32_diagnostic_trace/DiagnosticTracing.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ void ESP32Diagnostics::LogMetricEvent(const MetricEvent & event)
112112
{
113113
case ValueType::kInt32: {
114114
ChipLogProgress(DeviceLayer, "The value of %s is %" PRId32, event.key(), event.ValueInt32());
115-
Diagnostic<int32_t> metric(event.key(), event.ValueInt32(), esp_log_timestamp());
115+
Diagnostic<int32_t> metric(const_cast<char *>(event.key()), event.ValueInt32(), esp_log_timestamp());
116116
ReturnOnFailure(mStorageInstance->Store(metric));
117117
}
118118
break;
119119

120120
case ValueType::kUInt32: {
121121
ChipLogProgress(DeviceLayer, "The value of %s is %" PRId32, event.key(), event.ValueUInt32());
122-
Diagnostic<uint32_t> metric(event.key(), event.ValueUInt32(), esp_log_timestamp());
122+
Diagnostic<uint32_t> metric(const_cast<char *>(event.key()), event.ValueUInt32(), esp_log_timestamp());
123123
ReturnOnFailure(mStorageInstance->Store(metric));
124124
}
125125
break;
@@ -161,7 +161,7 @@ CHIP_ERROR ESP32Diagnostics::StoreDiagnostics(const char * label, const char * g
161161
{
162162
VerifyOrReturnError(mStorageInstance != nullptr, CHIP_ERROR_INCORRECT_STATE,
163163
ChipLogError(DeviceLayer, "Diagnostic Storage Instance cannot be NULL"));
164-
Diagnostic<const char *> trace(label, group, esp_log_timestamp());
164+
Diagnostic<char *> trace(const_cast<char *>(label), const_cast<char *>(group), esp_log_timestamp());
165165
return mStorageInstance->Store(trace);
166166
}
167167

src/tracing/esp32_diagnostic_trace/Diagnostics.h

+92-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#pragma once
2020

2121
#include <lib/core/TLVCircularBuffer.h>
22+
#define kMaxStringValueSize 128
2223

2324
namespace chip {
2425
namespace Tracing {
@@ -30,7 +31,7 @@ enum class DiagTag : uint8_t
3031
{
3132
TIMESTAMP = 0,
3233
LABEL,
33-
VALUE
34+
VALUE,
3435
};
3536

3637
/**
@@ -55,39 +56,122 @@ class DiagnosticEntry
5556
* failure of the encoding operation.
5657
*/
5758
virtual CHIP_ERROR Encode(chip::TLV::CircularTLVWriter & writer) const = 0;
59+
60+
virtual CHIP_ERROR Decode(chip::TLV::TLVReader & reader) = 0;
5861
};
5962

6063
template <typename T>
6164
class Diagnostic : public DiagnosticEntry
6265
{
6366
public:
64-
Diagnostic(const char * label, T value, uint32_t timestamp) : label_(label), value_(value), timestamp_(timestamp) {}
67+
Diagnostic(char * label = nullptr, T value = T{}, uint32_t timestamp = 0) : label_(label), value_(value), timestamp_(timestamp)
68+
{}
6569

6670
CHIP_ERROR Encode(chip::TLV::CircularTLVWriter & writer) const override
6771
{
6872
chip::TLV::TLVType DiagnosticOuterContainer = chip::TLV::kTLVType_NotSpecified;
6973
ReturnErrorOnFailure(
7074
writer.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, DiagnosticOuterContainer));
75+
76+
// Write timestamp
7177
ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(DiagTag::TIMESTAMP), timestamp_));
72-
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(DiagTag::LABEL), label_));
73-
if constexpr (std::is_same_v<T, const char *>)
78+
79+
// Write label
80+
if (strlen(label_) > kMaxStringValueSize)
7481
{
75-
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(DiagTag::VALUE), value_));
82+
char labelBuffer[kMaxStringValueSize + 1];
83+
memcpy(labelBuffer, label_, kMaxStringValueSize);
84+
labelBuffer[kMaxStringValueSize] = '\0';
85+
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(DiagTag::LABEL), labelBuffer));
7686
}
7787
else
88+
{
89+
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(DiagTag::LABEL), label_));
90+
}
91+
92+
// Write value
93+
if constexpr (std::is_same_v<T, char *>)
94+
{
95+
if (strlen(value_) > kMaxStringValueSize)
96+
{
97+
char valueBuffer[kMaxStringValueSize + 1];
98+
memcpy(valueBuffer, value_, kMaxStringValueSize);
99+
valueBuffer[kMaxStringValueSize] = '\0';
100+
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(DiagTag::VALUE), valueBuffer));
101+
}
102+
else
103+
{
104+
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(DiagTag::VALUE), value_));
105+
}
106+
}
107+
else if constexpr (std::is_same_v<T, uint32_t>)
78108
{
79109
ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(DiagTag::VALUE), value_));
80110
}
111+
else if constexpr (std::is_same_v<T, int32_t>)
112+
{
113+
ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(DiagTag::VALUE), value_));
114+
}
115+
else
116+
{
117+
return CHIP_ERROR_INVALID_ARGUMENT;
118+
}
81119
ReturnErrorOnFailure(writer.EndContainer(DiagnosticOuterContainer));
82120
ReturnErrorOnFailure(writer.Finalize());
83121
ChipLogProgress(DeviceLayer, "Diagnostic Value written to storage successfully. label: %s\n", label_);
84122
return CHIP_NO_ERROR;
85123
}
86124

125+
CHIP_ERROR Decode(chip::TLV::TLVReader & reader) override
126+
{
127+
TLV::TLVType containerType;
128+
ReturnErrorOnFailure(reader.EnterContainer(containerType));
129+
// Read timestamp
130+
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(DiagTag::TIMESTAMP)));
131+
ReturnErrorOnFailure(reader.Get(timestamp_));
132+
133+
// Read label
134+
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(DiagTag::LABEL)));
135+
uint32_t labelSize = reader.GetLength();
136+
char labelBuffer[kMaxStringValueSize + 1];
137+
ReturnErrorOnFailure(reader.GetString(labelBuffer, kMaxStringValueSize + 1));
138+
memcpy(label_, labelBuffer, labelSize + 1);
139+
140+
// Read value
141+
ReturnErrorOnFailure(reader.Next());
142+
if constexpr (std::is_same_v<T, char *>)
143+
{
144+
uint32_t valueSize = reader.GetLength();
145+
char valueBuffer[kMaxStringValueSize + 1];
146+
ReturnErrorOnFailure(reader.GetString(valueBuffer, kMaxStringValueSize + 1));
147+
memcpy(value_, valueBuffer, valueSize + 1);
148+
}
149+
else if constexpr (std::is_same_v<T, uint32_t>)
150+
{
151+
ReturnErrorOnFailure(reader.Get(value_));
152+
}
153+
else if constexpr (std::is_same_v<T, int32_t>)
154+
{
155+
ReturnErrorOnFailure(reader.Get(value_));
156+
}
157+
else
158+
{
159+
return CHIP_ERROR_INVALID_ARGUMENT;
160+
}
161+
162+
ReturnErrorOnFailure(reader.ExitContainer(containerType));
163+
return CHIP_NO_ERROR;
164+
}
165+
166+
// Getters
167+
char * GetLabel() const { return label_; }
168+
T GetValue() const { return value_; }
169+
uint32_t GetTimestamp() const { return timestamp_; }
170+
87171
private:
88-
const char * label_;
89-
T value_;
90-
uint32_t timestamp_;
172+
char * label_ = nullptr;
173+
T value_{};
174+
uint32_t timestamp_ = 0;
91175
};
92176

93177
/**

0 commit comments

Comments
 (0)