/src/connectedhomeip/examples/common/tracing/decoder/TraceDecoderProtocols.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2022 Project CHIP Authors |
3 | | * All rights reserved. |
4 | | * |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | * |
17 | | */ |
18 | | |
19 | | #include "TraceDecoderProtocols.h" |
20 | | |
21 | | #include "bdx/Decoder.h" |
22 | | #include "echo/Decoder.h" |
23 | | #include "interaction_model/Decoder.h" |
24 | | #include "secure_channel/Decoder.h" |
25 | | #include "udc/Decoder.h" |
26 | | |
27 | | #include <lib/core/TLVDebug.h> |
28 | | #include <lib/support/BytesToHex.h> |
29 | | #include <protocols/Protocols.h> |
30 | | |
31 | | namespace { |
32 | | |
33 | | constexpr char kUnknown[] = "Unknown"; |
34 | | |
35 | | void ENFORCE_FORMAT(1, 2) TLVPrettyPrinter(const char * aFormat, ...) |
36 | 0 | { |
37 | 0 | va_list args; |
38 | 0 | va_start(args, aFormat); |
39 | 0 | vprintf(aFormat, args); |
40 | 0 | va_end(args); |
41 | 0 | } |
42 | | } // namespace |
43 | | |
44 | | namespace chip { |
45 | | namespace trace { |
46 | | |
47 | | const char * ToProtocolName(uint16_t vendorId, uint16_t protocolId) |
48 | 0 | { |
49 | 0 | const char * name = nullptr; |
50 | |
|
51 | 0 | auto protocol = Protocols::Id(static_cast<VendorId>(vendorId), protocolId); |
52 | 0 | if (protocol == Protocols::SecureChannel::Id) |
53 | 0 | { |
54 | 0 | name = secure_channel::ToProtocolName(); |
55 | 0 | } |
56 | 0 | else if (protocol == Protocols::InteractionModel::Id) |
57 | 0 | { |
58 | 0 | name = interaction_model::ToProtocolName(); |
59 | 0 | } |
60 | 0 | else if (protocol == Protocols::BDX::Id) |
61 | 0 | { |
62 | 0 | name = bdx::ToProtocolName(); |
63 | 0 | } |
64 | 0 | else if (protocol == Protocols::UserDirectedCommissioning::Id) |
65 | 0 | { |
66 | 0 | name = udc::ToProtocolName(); |
67 | 0 | } |
68 | 0 | else if (protocol == Protocols::Echo::Id) |
69 | 0 | { |
70 | 0 | name = echo::ToProtocolName(); |
71 | 0 | } |
72 | 0 | else |
73 | 0 | { |
74 | 0 | name = kUnknown; |
75 | 0 | } |
76 | |
|
77 | 0 | return name; |
78 | 0 | } |
79 | | |
80 | | const char * ToProtocolMessageTypeName(uint16_t vendorId, uint16_t protocolId, uint8_t protocolCode) |
81 | 0 | { |
82 | 0 | const char * name = nullptr; |
83 | |
|
84 | 0 | auto protocol = Protocols::Id(static_cast<VendorId>(vendorId), protocolId); |
85 | 0 | if (protocol == Protocols::SecureChannel::Id) |
86 | 0 | { |
87 | 0 | name = secure_channel::ToProtocolMessageTypeName(protocolCode); |
88 | 0 | } |
89 | 0 | else if (protocol == Protocols::InteractionModel::Id) |
90 | 0 | { |
91 | 0 | name = interaction_model::ToProtocolMessageTypeName(protocolCode); |
92 | 0 | } |
93 | 0 | else if (protocol == Protocols::BDX::Id) |
94 | 0 | { |
95 | 0 | name = bdx::ToProtocolMessageTypeName(protocolCode); |
96 | 0 | } |
97 | 0 | else if (protocol == Protocols::UserDirectedCommissioning::Id) |
98 | 0 | { |
99 | 0 | name = udc::ToProtocolMessageTypeName(protocolCode); |
100 | 0 | } |
101 | 0 | else if (protocol == Protocols::Echo::Id) |
102 | 0 | { |
103 | 0 | name = echo::ToProtocolMessageTypeName(protocolCode); |
104 | 0 | } |
105 | 0 | else |
106 | 0 | { |
107 | 0 | name = kUnknown; |
108 | 0 | } |
109 | |
|
110 | 0 | return name; |
111 | 0 | } |
112 | | |
113 | | CHIP_ERROR LogAsProtocolMessage(uint16_t vendorId, uint16_t protocolId, uint8_t protocolCode, const char * payload, size_t len, |
114 | | bool interactionModelResponse) |
115 | 0 | { |
116 | 0 | constexpr uint16_t kMaxPayloadLen = 2048; |
117 | 0 | uint8_t data[kMaxPayloadLen] = {}; |
118 | 0 | size_t dataLen = Encoding::HexToBytes(payload, len, data, sizeof(data)); |
119 | 0 | VerifyOrReturnError(dataLen != 0, CHIP_ERROR_INVALID_ARGUMENT); |
120 | | |
121 | 0 | CHIP_ERROR err = CHIP_NO_ERROR; |
122 | |
|
123 | 0 | auto protocol = Protocols::Id(static_cast<VendorId>(vendorId), protocolId); |
124 | 0 | if (protocol == Protocols::SecureChannel::Id) |
125 | 0 | { |
126 | 0 | err = secure_channel::LogAsProtocolMessage(protocolCode, data, dataLen); |
127 | 0 | } |
128 | 0 | else if (protocol == Protocols::InteractionModel::Id) |
129 | 0 | { |
130 | 0 | err = interaction_model::LogAsProtocolMessage(protocolCode, data, dataLen, interactionModelResponse); |
131 | 0 | } |
132 | 0 | else if (protocol == Protocols::BDX::Id) |
133 | 0 | { |
134 | 0 | err = bdx::LogAsProtocolMessage(protocolCode, data, dataLen); |
135 | 0 | } |
136 | 0 | else if (protocol == Protocols::UserDirectedCommissioning::Id) |
137 | 0 | { |
138 | 0 | err = udc::LogAsProtocolMessage(protocolCode, data, dataLen); |
139 | 0 | } |
140 | 0 | else if (protocol == Protocols::Echo::Id) |
141 | 0 | { |
142 | 0 | err = echo::LogAsProtocolMessage(protocolCode, data, dataLen); |
143 | 0 | } |
144 | 0 | else |
145 | 0 | { |
146 | 0 | err = CHIP_ERROR_NOT_IMPLEMENTED; |
147 | 0 | } |
148 | |
|
149 | 0 | if (CHIP_NO_ERROR != err) |
150 | 0 | { |
151 | 0 | ChipLogError(DataManagement, "Error: %s", chip::ErrorStr(err)); |
152 | 0 | TLV::TLVReader reader; |
153 | 0 | reader.Init(data, dataLen); |
154 | 0 | ReturnErrorOnFailure(reader.Next()); |
155 | 0 | err = TLV::Debug::Dump(reader, TLVPrettyPrinter); |
156 | 0 | if (CHIP_END_OF_TLV == err) |
157 | 0 | { |
158 | 0 | err = CHIP_NO_ERROR; |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | 0 | return err; |
163 | 0 | } |
164 | | |
165 | | } // namespace trace |
166 | | } // namespace chip |