30
30
constexpr uint16_t kMaxLineLen = 4096 ;
31
31
constexpr const char * jsonPrefix = " json\t " ;
32
32
33
- // Json keys
34
- constexpr const char * kProtocolIdKey = " protocol_id" ;
35
- constexpr const char * kProtocolCodeKey = " protocol_opcode" ;
36
- constexpr const char * kSessionIdKey = " session_id" ;
37
- constexpr const char * kExchangeIdKey = " exchange_id" ;
38
- constexpr const char * kMessageCounterKey = " msg_counter" ;
39
- constexpr const char * kSecurityFlagsKey = " security_flags" ;
40
- constexpr const char * kMessageFlagsKey = " msg_flags" ;
41
- constexpr const char * kSourceNodeIdKey = " source_node_id" ;
42
- constexpr const char * kDestinationNodeIdKey = " dest_node_id" ;
43
- constexpr const char * kDestinationGroupIdKey = " group_id" ;
44
- constexpr const char * kExchangeFlagsKey = " exchange_flags" ;
45
- constexpr const char * kIsInitiatorKey = " is_initiator" ;
46
- constexpr const char * kNeedsAckKey = " is_ack_requested" ;
47
- constexpr const char * kAckMsgKey = " acknowledged_msg_counter" ;
48
- constexpr const char * kPayloadDataKey = " payload_hex" ;
49
- constexpr const char * kPayloadSizeKey = " payload_size" ;
50
- constexpr const char * kPayloadEncryptedDataKey = " payload_hex_encrypted" ;
51
- constexpr const char * kPayloadEncryptedSizeKey = " payload_size_encrypted" ;
52
- constexpr const char * kPayloadEncryptedBufferPtrKey = " buffer_ptr" ;
53
- constexpr const char * kSourceKey = " source" ;
54
- constexpr const char * kDestinationKey = " destination" ;
55
-
56
33
namespace chip {
57
34
namespace trace {
35
+ namespace {
36
+
37
+ // Json keys
38
+ constexpr const char * kProtocolIdKey = " protocol_id" ;
39
+ constexpr const char * kProtocolCodeKey = " protocol_opcode" ;
40
+ constexpr const char * kSessionIdKey = " session_id" ;
41
+ constexpr const char * kExchangeIdKey = " exchange_id" ;
42
+ constexpr const char * kMessageCounterKey = " msg_counter" ;
43
+ constexpr const char * kSecurityFlagsKey = " security_flags" ;
44
+ constexpr const char * kMessageFlagsKey = " msg_flags" ;
45
+ constexpr const char * kSourceNodeIdKey = " source_node_id" ;
46
+ constexpr const char * kDestinationNodeIdKey = " dest_node_id" ;
47
+ constexpr const char * kDestinationGroupIdKey = " group_id" ;
48
+ constexpr const char * kExchangeFlagsKey = " exchange_flags" ;
49
+ constexpr const char * kIsInitiatorKey = " is_initiator" ;
50
+ constexpr const char * kNeedsAckKey = " is_ack_requested" ;
51
+ constexpr const char * kAckMsgKey = " acknowledged_msg_counter" ;
52
+ constexpr const char * kPayloadDataKey = " payload_hex" ;
53
+ constexpr const char * kPayloadSizeKey = " payload_size" ;
54
+ constexpr const char * kDirectionKey = " direction" ;
55
+ constexpr const char * kPeerAddress = " peer_address" ;
56
+
57
+ bool IsOutbound (const Json::Value & json)
58
+ {
59
+ VerifyOrReturnValue (json.isMember (kDirectionKey ), false );
60
+ return strcmp (json[kDirectionKey ].asCString (), " outbound" ) == 0 ;
61
+ }
62
+
63
+ bool IsInbound (const Json::Value & json)
64
+ {
65
+ VerifyOrReturnValue (json.isMember (kDirectionKey ), false );
66
+ return strcmp (json[kDirectionKey ].asCString (), " inbound" ) == 0 ;
67
+ }
68
+
69
+ } // namespace
58
70
59
71
using namespace logging ;
60
72
@@ -83,45 +95,8 @@ CHIP_ERROR TraceDecoder::ReadString(const char * str)
83
95
str += strlen (jsonPrefix);
84
96
85
97
Json::Reader reader;
86
-
87
- if (mJsonBuffer .empty ())
88
- {
89
- VerifyOrReturnError (reader.parse (str, mJsonBuffer ), CHIP_ERROR_INVALID_ARGUMENT);
90
- VerifyOrReturnError (mJsonBuffer .isMember (kPayloadDataKey ) && mJsonBuffer .isMember (kPayloadSizeKey ),
91
- CHIP_ERROR_INCORRECT_STATE);
92
- return CHIP_NO_ERROR;
93
- }
94
-
95
98
Json::Value json;
96
99
VerifyOrReturnError (reader.parse (str, json), CHIP_ERROR_INVALID_ARGUMENT);
97
-
98
- // If there is a source, then it means the previously saved payload is an encrypted to decode, otherwise
99
- // the previously saved payload is the non encrypted version, and the current decoded one is the encrypted version.
100
- if (mJsonBuffer .isMember (kSourceKey ))
101
- {
102
- json[kPayloadEncryptedDataKey ] = mJsonBuffer [kPayloadDataKey ];
103
- json[kPayloadEncryptedSizeKey ] = mJsonBuffer [kPayloadSizeKey ];
104
- }
105
- else
106
- {
107
- auto data = json[kPayloadDataKey ];
108
- auto size = json[kPayloadSizeKey ];
109
- json[kPayloadDataKey ] = mJsonBuffer [kPayloadDataKey ];
110
- json[kPayloadSizeKey ] = mJsonBuffer [kPayloadSizeKey ];
111
- json[kPayloadEncryptedDataKey ] = data;
112
- json[kPayloadEncryptedSizeKey ] = size;
113
- }
114
- mJsonBuffer .removeMember (kPayloadDataKey );
115
- mJsonBuffer .removeMember (kPayloadSizeKey );
116
-
117
- // If there is additional data in the previously saved json copy all of it.
118
- for (const auto & key : mJsonBuffer .getMemberNames ())
119
- {
120
- json[key] = mJsonBuffer [key];
121
- mJsonBuffer .removeMember (key);
122
- }
123
-
124
- VerifyOrReturnError (json.isMember (kSourceKey ) || json.isMember (kDestinationKey ), CHIP_ERROR_INCORRECT_STATE);
125
100
VerifyOrReturnError (json.isMember (kProtocolIdKey ), CHIP_ERROR_INCORRECT_STATE);
126
101
VerifyOrReturnError (json.isMember (kProtocolCodeKey ), CHIP_ERROR_INCORRECT_STATE);
127
102
@@ -138,20 +113,19 @@ CHIP_ERROR TraceDecoder::LogJSON(Json::Value & json)
138
113
return CHIP_NO_ERROR;
139
114
}
140
115
141
- if (!mOptions .mEnableMessageInitiator && json. isMember ( kDestinationKey ))
116
+ if (!mOptions .mEnableMessageInitiator && IsOutbound (json ))
142
117
{
143
118
return CHIP_NO_ERROR;
144
119
}
145
120
146
- if (!mOptions .mEnableMessageResponder && json. isMember ( kSourceKey ))
121
+ if (!mOptions .mEnableMessageResponder && IsInbound (json ))
147
122
{
148
123
return CHIP_NO_ERROR;
149
124
}
150
125
151
- bool isResponse = json. isMember ( kSourceKey ) ? true : false ;
126
+ bool isResponse = IsInbound (json) ;
152
127
ReturnErrorOnFailure (LogAndConsumeProtocol (json));
153
128
ReturnErrorOnFailure (MaybeLogAndConsumeHeaderFlags (json));
154
- ReturnErrorOnFailure (MaybeLogAndConsumeEncryptedPayload (json));
155
129
ReturnErrorOnFailure (MaybeLogAndConsumePayload (json, isResponse));
156
130
ReturnErrorOnFailure (MaybeLogAndConsumeOthers (json));
157
131
@@ -168,26 +142,6 @@ CHIP_ERROR TraceDecoder::MaybeLogAndConsumeHeaderFlags(Json::Value & json)
168
142
return CHIP_NO_ERROR;
169
143
}
170
144
171
- CHIP_ERROR TraceDecoder::MaybeLogAndConsumeEncryptedPayload (Json::Value & json)
172
- {
173
- if (mOptions .mEnableDataEncryptedPayload )
174
- {
175
- size_t size = static_cast <uint16_t >(json[kPayloadEncryptedSizeKey ].asLargestUInt ());
176
- if (size)
177
- {
178
- auto payload = json[kPayloadEncryptedDataKey ].asString ();
179
- auto bufferPtr = json[kPayloadEncryptedBufferPtrKey ].asString ();
180
- auto scopedIndent = ScopedLogIndentWithSize (" Encrypted Payload" , size);
181
- Log (" data" , payload.c_str ());
182
- Log (" buffer_ptr" , bufferPtr.c_str ());
183
- }
184
- }
185
- json.removeMember (kPayloadEncryptedSizeKey );
186
- json.removeMember (kPayloadEncryptedDataKey );
187
- json.removeMember (kPayloadEncryptedBufferPtrKey );
188
- return CHIP_NO_ERROR;
189
- }
190
-
191
145
CHIP_ERROR TraceDecoder::MaybeLogAndConsumeOthers (Json::Value & json)
192
146
{
193
147
std::vector<std::string> keys = json.getMemberNames ();
@@ -218,8 +172,16 @@ CHIP_ERROR TraceDecoder::LogAndConsumeProtocol(Json::Value & json)
218
172
219
173
chip::StringBuilderBase builder (protocolInfo, sizeof (protocolInfo));
220
174
221
- builder.Add (json.isMember (kSourceKey ) ? " << from " : " >> to " );
222
- builder.Add (json.isMember (kSourceKey ) ? json[kSourceKey ].asCString () : json[kDestinationKey ].asCString ());
175
+ builder.Add (IsInbound (json) ? " << from " : " >> to " );
176
+
177
+ if (json.isMember (kPeerAddress ))
178
+ {
179
+ builder.Add (json[kPeerAddress ].asCString ());
180
+ }
181
+ else
182
+ {
183
+ builder.Add (" UNKNOWN" );
184
+ }
223
185
224
186
builder.Add (" " );
225
187
auto msgCounter = static_cast <uint32_t >(json[kMessageCounterKey ].asLargestUInt ());
@@ -258,11 +220,10 @@ CHIP_ERROR TraceDecoder::LogAndConsumeProtocol(Json::Value & json)
258
220
259
221
ChipLogProgress (DataManagement, " %s" , builder.c_str ());
260
222
261
- json.removeMember (kSourceKey );
262
- json.removeMember (kDestinationKey );
263
223
json.removeMember (kSessionIdKey );
264
224
json.removeMember (kExchangeIdKey );
265
225
json.removeMember (kMessageCounterKey );
226
+ json.removeMember (kDirectionKey );
266
227
267
228
return CHIP_NO_ERROR;
268
229
}
0 commit comments