Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2025 Project CHIP Authors
4 : * All rights reserved.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 : #include <lib/support/CHIPMemString.h>
19 : #include <tracing/esp32_diagnostic_trace/DiagnosticEntry.h>
20 : using namespace chip::TLV;
21 :
22 : namespace chip {
23 : namespace Tracing {
24 : namespace Diagnostics {
25 :
26 31 : CHIP_ERROR WriteString(TLVWriter & writer, const DiagTag tag, const char * string)
27 : {
28 : char buffer[kMaxStringValueSize + 1];
29 31 : Platform::CopyString(buffer, string);
30 31 : return writer.PutString(chip::TLV::ContextTag(tag), buffer);
31 : }
32 :
33 17 : CHIP_ERROR Encode(TLVWriter & writer, const DiagnosticEntry & entry)
34 : {
35 17 : TLVType DiagnosticOuterContainer = kTLVType_NotSpecified;
36 17 : ReturnErrorOnFailure(writer.StartContainer(AnonymousTag(), kTLVType_Structure, DiagnosticOuterContainer));
37 17 : ReturnErrorOnFailure(writer.Put(ContextTag(DiagTag::kTimestamp), entry.timestamps_ms_since_boot));
38 17 : ReturnErrorOnFailure(WriteString(writer, DiagTag::kLabel, entry.label));
39 17 : switch (entry.type)
40 : {
41 0 : case ValueType::kInvalidType:
42 0 : return CHIP_ERROR_INVALID_ARGUMENT;
43 14 : case ValueType::kCharString:
44 14 : ReturnErrorOnFailure(WriteString(writer, DiagTag::kValue, entry.stringValue));
45 14 : break;
46 1 : case ValueType::kUnsignedInteger:
47 1 : ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(DiagTag::kValue), entry.uintValue));
48 1 : break;
49 2 : case ValueType::kSignedInteger:
50 2 : ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(DiagTag::kValue), entry.intValue));
51 2 : break;
52 : }
53 17 : ReturnErrorOnFailure(writer.EndContainer(DiagnosticOuterContainer));
54 17 : ReturnErrorOnFailure(writer.Finalize());
55 17 : ChipLogDetail(DeviceLayer, "Diagnostic Value written to storage successfully. label: %s\n", entry.label);
56 17 : return CHIP_NO_ERROR;
57 : }
58 :
59 16 : CHIP_ERROR Decode(TLVReader & reader, DiagnosticEntry & entry)
60 : {
61 : TLVType containerType;
62 16 : ReturnErrorOnFailure(reader.EnterContainer(containerType));
63 16 : ReturnErrorOnFailure(reader.Next(ContextTag(DiagTag::kTimestamp)));
64 16 : ReturnErrorOnFailure(reader.Get(entry.timestamps_ms_since_boot));
65 16 : ReturnErrorOnFailure(reader.Next(ContextTag(DiagTag::kLabel)));
66 16 : uint32_t labelSize = reader.GetLength();
67 16 : if (labelSize > kMaxStringValueSize)
68 : {
69 0 : return CHIP_ERROR_BUFFER_TOO_SMALL;
70 : }
71 16 : ReturnErrorOnFailure(reader.GetString(entry.label, kMaxStringValueSize + 1));
72 16 : ReturnErrorOnFailure(reader.Next(ContextTag(DiagTag::kValue)));
73 16 : switch (reader.GetType())
74 : {
75 0 : case kTLVType_NotSpecified:
76 0 : return CHIP_ERROR_INVALID_ARGUMENT;
77 13 : case kTLVType_UTF8String: {
78 13 : uint32_t valueSize = reader.GetLength();
79 13 : if (valueSize > kMaxStringValueSize)
80 : {
81 0 : return CHIP_ERROR_BUFFER_TOO_SMALL;
82 : }
83 13 : ReturnErrorOnFailure(reader.GetString(entry.stringValue, kMaxStringValueSize + 1));
84 13 : entry.type = ValueType::kCharString;
85 13 : break;
86 : }
87 1 : case kTLVType_UnsignedInteger:
88 1 : ReturnErrorOnFailure(reader.Get(entry.uintValue));
89 1 : entry.type = ValueType::kUnsignedInteger;
90 1 : break;
91 2 : case kTLVType_SignedInteger:
92 2 : ReturnErrorOnFailure(reader.Get(entry.intValue));
93 2 : entry.type = ValueType::kSignedInteger;
94 2 : break;
95 0 : default:
96 0 : ChipLogError(DeviceLayer, "Invalid value type encountered while decoding DiagnosticEntry");
97 0 : return CHIP_ERROR_INVALID_ARGUMENT;
98 : }
99 16 : return reader.ExitContainer(containerType);
100 : }
101 :
102 : } // namespace Diagnostics
103 : } // namespace Tracing
104 : } // namespace chip
|