19
19
#pragma once
20
20
21
21
#include < lib/core/TLVCircularBuffer.h>
22
+ #define kMaxStringValueSize 128
22
23
23
24
namespace chip {
24
25
namespace Tracing {
@@ -30,7 +31,7 @@ enum class DiagTag : uint8_t
30
31
{
31
32
TIMESTAMP = 0 ,
32
33
LABEL,
33
- VALUE
34
+ VALUE,
34
35
};
35
36
36
37
/* *
@@ -55,39 +56,122 @@ class DiagnosticEntry
55
56
* failure of the encoding operation.
56
57
*/
57
58
virtual CHIP_ERROR Encode (chip::TLV::CircularTLVWriter & writer) const = 0;
59
+
60
+ virtual CHIP_ERROR Decode (chip::TLV::TLVReader & reader) = 0;
58
61
};
59
62
60
63
template <typename T>
61
64
class Diagnostic : public DiagnosticEntry
62
65
{
63
66
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
+ {}
65
69
66
70
CHIP_ERROR Encode (chip::TLV::CircularTLVWriter & writer) const override
67
71
{
68
72
chip::TLV::TLVType DiagnosticOuterContainer = chip::TLV::kTLVType_NotSpecified ;
69
73
ReturnErrorOnFailure (
70
74
writer.StartContainer (chip::TLV::AnonymousTag (), chip::TLV::kTLVType_Structure , DiagnosticOuterContainer));
75
+
76
+ // Write timestamp
71
77
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 )
74
81
{
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));
76
86
}
77
87
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 >)
78
108
{
79
109
ReturnErrorOnFailure (writer.Put (chip::TLV::ContextTag (DiagTag::VALUE), value_));
80
110
}
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
+ }
81
119
ReturnErrorOnFailure (writer.EndContainer (DiagnosticOuterContainer));
82
120
ReturnErrorOnFailure (writer.Finalize ());
83
121
ChipLogProgress (DeviceLayer, " Diagnostic Value written to storage successfully. label: %s\n " , label_);
84
122
return CHIP_NO_ERROR;
85
123
}
86
124
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
+
87
171
private:
88
- const char * label_;
89
- T value_;
90
- uint32_t timestamp_;
172
+ char * label_ = nullptr ;
173
+ T value_{} ;
174
+ uint32_t timestamp_ = 0 ;
91
175
};
92
176
93
177
/* *
0 commit comments