17
17
* limitations under the License.
18
18
*/
19
19
20
- #include < esp_log.h>
21
20
#include < lib/core/CHIPError.h>
22
21
#include < lib/support/logging/CHIPLogging.h>
23
22
#include < tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h>
24
23
25
24
namespace chip {
26
25
namespace Tracing {
27
26
28
- DiagnosticStorageImpl::DiagnosticStorageImpl () :
29
- mEndUserCircularBuffer (mEndUserBuffer , END_USER_BUFFER_SIZE), mNetworkCircularBuffer (mNetworkBuffer , NETWORK_BUFFER_SIZE)
30
- {}
27
+ namespace Diagnostics {
28
+ DiagnosticStorageImpl::DiagnosticStorageImpl (uint8_t * buffer, size_t bufferSize)
29
+ : mEndUserCircularBuffer (buffer, bufferSize) {}
30
+
31
+ DiagnosticStorageImpl & DiagnosticStorageImpl::GetInstance (uint8_t * buffer, size_t bufferSize) {
32
+ static DiagnosticStorageImpl instance (buffer, bufferSize);
33
+ return instance;
34
+ }
31
35
32
36
DiagnosticStorageImpl::~DiagnosticStorageImpl () {}
33
37
@@ -39,10 +43,10 @@ CHIP_ERROR DiagnosticStorageImpl::Store(DiagnosticEntry & diagnostic)
39
43
writer.Init (mEndUserCircularBuffer );
40
44
41
45
// Start a TLV structure container (Anonymous)
42
- chip::TLV:: TLVType outerContainer;
43
- err = writer.StartContainer (AnonymousTag (), chip::TLV::TLVType:: kTLVType_Structure , outerContainer);
46
+ TLVType outerContainer;
47
+ err = writer.StartContainer (AnonymousTag (), kTLVType_Structure , outerContainer);
44
48
VerifyOrReturnError (err == CHIP_NO_ERROR, err,
45
- ChipLogError (DeviceLayer, " Failed to start TLV container for metric : %s" , chip:: ErrorStr (err)));
49
+ ChipLogError (DeviceLayer, " Failed to start TLV container for metric : %s" , ErrorStr (err)));
46
50
47
51
err = diagnostic.Encode (writer);
48
52
if (err != CHIP_NO_ERROR)
@@ -55,32 +59,24 @@ CHIP_ERROR DiagnosticStorageImpl::Store(DiagnosticEntry & diagnostic)
55
59
}
56
60
err = writer.EndContainer (outerContainer);
57
61
VerifyOrReturnError (err == CHIP_NO_ERROR, err,
58
- ChipLogError (DeviceLayer, " Failed to end TLV container for metric : %s" , chip:: ErrorStr (err)));
62
+ ChipLogError (DeviceLayer, " Failed to end TLV container for metric : %s" , ErrorStr (err)));
59
63
60
64
err = writer.Finalize ();
61
65
VerifyOrReturnError (err == CHIP_NO_ERROR, err, ChipLogError (DeviceLayer, " Failed to finalize TLV writing" ));
62
-
63
- printf (" Total Data Length in Buffer : %lu\n Total available length in buffer : %lu\n Total buffer length : %lu\n " ,
64
- mEndUserCircularBuffer .DataLength (), mEndUserCircularBuffer .AvailableDataLength (),
65
- mEndUserCircularBuffer .GetTotalDataLength ());
66
66
return CHIP_NO_ERROR;
67
67
}
68
68
69
69
CHIP_ERROR DiagnosticStorageImpl::Retrieve (MutableByteSpan & payload)
70
70
{
71
- printf (" ***************************************************************************RETRIEVAL "
72
- " STARTED**********************************************************\n " );
73
71
CHIP_ERROR err = CHIP_NO_ERROR;
74
- chip::TLV::TLVReader reader;
72
+ CircularTLVReader reader;
75
73
reader.Init (mEndUserCircularBuffer );
76
74
77
- chip::TLV:: TLVWriter writer;
75
+ TLVWriter writer;
78
76
writer.Init (payload);
79
77
80
- uint32_t totalBufferSize = 0 ;
81
-
82
- chip::TLV::TLVType outWriterContainer;
83
- err = writer.StartContainer (AnonymousTag (), chip::TLV::TLVType::kTLVType_List , outWriterContainer);
78
+ TLVType outWriterContainer;
79
+ err = writer.StartContainer (AnonymousTag (), kTLVType_List , outWriterContainer);
84
80
VerifyOrReturnError (err == CHIP_NO_ERROR, err, ChipLogError (DeviceLayer, " Failed to start container" ));
85
81
86
82
while (true )
@@ -92,35 +88,29 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
92
88
break ;
93
89
}
94
90
VerifyOrReturnError (err == CHIP_NO_ERROR, err,
95
- ChipLogError (DeviceLayer, " Failed to read next TLV element: %s" , chip:: ErrorStr (err)));
91
+ ChipLogError (DeviceLayer, " Failed to read next TLV element: %s" , ErrorStr (err)));
96
92
97
- if (reader.GetType () == chip::TLV::TLVType:: kTLVType_Structure && reader.GetTag () == chip::TLV:: AnonymousTag ())
93
+ if (reader.GetType () == kTLVType_Structure && reader.GetTag () == AnonymousTag ())
98
94
{
99
- chip::TLV:: TLVType outerReaderContainer;
95
+ TLVType outerReaderContainer;
100
96
err = reader.EnterContainer (outerReaderContainer);
101
97
VerifyOrReturnError (err == CHIP_NO_ERROR, err,
102
- ChipLogError (DeviceLayer, " Failed to enter outer TLV container: %s" , chip:: ErrorStr (err)));
98
+ ChipLogError (DeviceLayer, " Failed to enter outer TLV container: %s" , ErrorStr (err)));
103
99
104
100
err = reader.Next ();
105
- VerifyOrReturnError (
106
- err == CHIP_NO_ERROR, err,
107
- ChipLogError (DeviceLayer, " Failed to read next TLV element in outer container: %s" , chip::ErrorStr (err)));
101
+ VerifyOrReturnError (err == CHIP_NO_ERROR, err, ChipLogError (DeviceLayer, " Failed to read next TLV element in outer container: %s" , ErrorStr (err)));
108
102
109
103
// Check if the current element is a METRIC or TRACE container
110
- if ((reader.GetType () == chip::TLV:: kTLVType_Structure ) &&
104
+ if ((reader.GetType () == kTLVType_Structure ) &&
111
105
(reader.GetTag () == ContextTag (DIAGNOSTICS_TAG::METRIC) || reader.GetTag () == ContextTag (DIAGNOSTICS_TAG::TRACE) || reader.GetTag () == ContextTag (DIAGNOSTICS_TAG::COUNTER)))
112
106
{
113
- ESP_LOGW (" SIZE" , " Total read till now: %ld Total write till now: %ld" , reader.GetLengthRead (), writer.GetLengthWritten ());
114
-
115
107
if ((reader.GetLengthRead () - writer.GetLengthWritten ()) < writer.GetRemainingFreeLength ()) {
116
108
err = writer.CopyElement (reader);
117
109
if (err == CHIP_ERROR_BUFFER_TOO_SMALL) {
118
110
ChipLogProgress (DeviceLayer, " Buffer too small to occupy current element" );
119
111
break ;
120
112
}
121
113
VerifyOrReturnError (err == CHIP_NO_ERROR, err, ChipLogError (DeviceLayer, " Failed to copy TLV element" ));
122
- ChipLogProgress (DeviceLayer, " Read metric container successfully" );
123
- mEndUserCircularBuffer .EvictHead ();
124
114
}
125
115
else {
126
116
ChipLogProgress (DeviceLayer, " Buffer too small to occupy current TLV" );
@@ -136,18 +126,12 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
136
126
// Exit the outer container
137
127
err = reader.ExitContainer (outerReaderContainer);
138
128
VerifyOrReturnError (err == CHIP_NO_ERROR, err,
139
- ChipLogError (DeviceLayer, " Failed to exit outer TLV container: %s" , chip::ErrorStr (err)));
140
-
141
-
129
+ ChipLogError (DeviceLayer, " Failed to exit outer TLV container: %s" , ErrorStr (err)));
142
130
}
143
131
else
144
132
{
145
133
ChipLogError (DeviceLayer, " Unexpected TLV element type or tag in outer container" );
146
134
}
147
-
148
- printf (" Total Data Length in Buffer: %lu\n Total available length in buffer: %lu\n Total buffer length: %lu\n " ,
149
- mEndUserCircularBuffer .DataLength (), mEndUserCircularBuffer .AvailableDataLength (),
150
- mEndUserCircularBuffer .GetTotalDataLength ());
151
135
}
152
136
153
137
err = writer.EndContainer (outWriterContainer);
@@ -156,7 +140,7 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
156
140
err = writer.Finalize ();
157
141
VerifyOrReturnError (err == CHIP_NO_ERROR, err, ChipLogError (DeviceLayer, " Failed to finalize TLV writing" ));
158
142
payload.reduce_size (writer.GetLengthWritten ());
159
- printf ( " ---------------Total written bytes successfully : %ld----------------\n " , writer.GetLengthWritten ());
143
+ ChipLogProgress (DeviceLayer, " ---------------Total written bytes successfully : %ld----------------\n " , writer.GetLengthWritten ());
160
144
ChipLogProgress (DeviceLayer, " Retrieval successful" );
161
145
return CHIP_NO_ERROR;
162
146
}
@@ -165,6 +149,6 @@ bool DiagnosticStorageImpl::IsEmptyBuffer()
165
149
{
166
150
return mEndUserCircularBuffer .DataLength () == 0 ;
167
151
}
168
-
152
+ } // namespace Diagnostics
169
153
} // namespace Tracing
170
154
} // namespace chip
0 commit comments