Skip to content

Commit 7dce058

Browse files
Add nicer logging for InteractionModel::Status. (#16855)
* Add nicer logging for InteractionModel::Status. After this change we get things like: Error: IM Error 0x000005C6: General error: 0xc6 (NEEDS_TIMED_INTERACTION) instead of just Error: IM Error 0x000005C6: General error: 0xc6 * Address review comment
1 parent 98294c9 commit 7dce058

File tree

10 files changed

+206
-51
lines changed

10 files changed

+206
-51
lines changed

src/app/MessageDef/StatusIB.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ CHIP_ERROR StatusIB::Parser::CheckSchemaValidity() const
8484
{
8585
uint16_t status;
8686
ReturnErrorOnFailure(reader.Get(status));
87-
PRETTY_PRINT("\tstatus = 0x%" PRIx16 ",", status);
87+
PRETTY_PRINT("\tstatus = " ChipLogFormatIMStatus ",", ChipLogValueIMStatus(static_cast<Status>(status)));
8888
}
8989
#endif // CHIP_DETAIL_LOGGING
9090
}
@@ -195,13 +195,24 @@ bool FormatStatusIBError(char * buf, uint16_t bufSize, CHIP_ERROR err)
195195

196196
const char * desc = nullptr;
197197
#if !CHIP_CONFIG_SHORT_ERROR_STR
198-
constexpr char generalFormat[] = "General error: 0x%02x";
198+
constexpr char generalFormat[] = "General error: " ChipLogFormatIMStatus;
199199
constexpr char clusterFormat[] = "Cluster-specific error: 0x%02x";
200200

201201
// Formatting an 8-bit int will take at most 2 chars, and replace the '%02x'
202202
// so a buffer big enough to hold our format string will also hold our
203-
// formatted string.
204-
constexpr size_t formattedSize = max(sizeof(generalFormat), sizeof(clusterFormat));
203+
// formatted string, as long as we account for the possible string formats.
204+
constexpr size_t statusNameMaxLength =
205+
#define CHIP_IM_STATUS_CODE(name, spec_name, value) \
206+
max(sizeof(#spec_name),
207+
#include <protocols/interaction_model/StatusCodeList.h>
208+
#undef CHIP_IM_STATUS_CODE
209+
static_cast<size_t>(0)
210+
#define CHIP_IM_STATUS_CODE(name, spec_name, value) \
211+
)
212+
#include <protocols/interaction_model/StatusCodeList.h>
213+
#undef CHIP_IM_STATUS_CODE
214+
;
215+
constexpr size_t formattedSize = max(sizeof(generalFormat) + statusNameMaxLength, sizeof(clusterFormat));
205216
char formattedString[formattedSize];
206217

207218
StatusIB status;
@@ -212,7 +223,7 @@ bool FormatStatusIBError(char * buf, uint16_t bufSize, CHIP_ERROR err)
212223
}
213224
else
214225
{
215-
snprintf(formattedString, formattedSize, generalFormat, to_underlying(status.mStatus));
226+
snprintf(formattedString, formattedSize, generalFormat, ChipLogValueIMStatus(status.mStatus));
216227
}
217228
desc = formattedString;
218229
#endif // !CHIP_CONFIG_SHORT_ERROR_STR

src/app/tests/TestStatusIB.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <app/AppBuildConfig.h>
1919
#include <app/MessageDef/StatusIB.h>
20+
#include <lib/core/CHIPConfig.h>
2021
#include <lib/core/CHIPError.h>
2122
#include <lib/support/CHIPMem.h>
2223
#include <lib/support/ErrorStr.h>
@@ -96,7 +97,12 @@ void TestStatusIBErrorToString(nlTestSuite * aSuite, void * aContext)
9697
status.mStatus = Status::InvalidAction;
9798
CHIP_ERROR err = status.ToChipError();
9899
const char * str = ErrorStr(err);
100+
101+
#if CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
102+
NL_TEST_ASSERT(aSuite, strcmp(str, "IM Error 0x00000580: General error: 0x80 (INVALID_ACTION)") == 0);
103+
#else // CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
99104
NL_TEST_ASSERT(aSuite, strcmp(str, "IM Error 0x00000580: General error: 0x80") == 0);
105+
#endif // CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
100106

101107
status.mStatus = Status::Failure;
102108
status.mClusterStatus = MakeOptional(static_cast<ClusterStatus>(5));

src/lib/core/CHIPConfig.h

+14
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,20 @@ extern const char CHIP_NON_PRODUCTION_MARKER[];
17991799
#define CHIP_CONFIG_NETWORK_COMMISSIONING_DEBUG_TEXT_BUFFER_SIZE 64
18001800
#endif // CHIP_CONFIG_NETWORK_COMMISSIONING_DEBUG_TEXT_BUFFER_SIZE
18011801

1802+
/**
1803+
* @def CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
1804+
*
1805+
* If 1, IM status codes, when logged, will be formatted as "0xNN (NameOfCode)"
1806+
* If 0, IM status codes, when logged, will be formatted as "0xNN" In either
1807+
* case, the macro ChipLogFormatIMStatus expands to a suitable printf format
1808+
* string, which already includes the '%' in it, to be used with
1809+
* ChipLogValueIMStatus(status).
1810+
*/
1811+
1812+
#ifndef CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
1813+
#define CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT 0
1814+
#endif // CHIP_CONFIG_ERROR_FORMAT_AS_STRING
1815+
18021816
/**
18031817
* @}
18041818
*/

src/platform/Darwin/CHIPPlatformConfig.h

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
#define CHIP_CONFIG_VERBOSE_VERIFY_OR_DIE 1
4141

42+
#define CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT 1
43+
4244
// ==================== Security Adaptations ====================
4345

4446
// ==================== General Configuration Overrides ====================

src/platform/Linux/CHIPPlatformConfig.h

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ using CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE = const char *;
4343

4444
#define CHIP_CONFIG_VERBOSE_VERIFY_OR_DIE 1
4545

46+
#define CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT 1
47+
4648
// ==================== Security Adaptations ====================
4749

4850
// ==================== General Configuration Overrides ====================

src/protocols/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ static_library("protocols") {
2121
"echo/Echo.h",
2222
"echo/EchoClient.cpp",
2323
"echo/EchoServer.cpp",
24+
"interaction_model/StatusCode.cpp",
25+
"interaction_model/StatusCode.h",
2426
"secure_channel/MessageCounterManager.cpp",
2527
"secure_channel/MessageCounterManager.h",
2628
"user_directed_commissioning/UDCClientState.h",

src/protocols/interaction_model/Constants.h

+1-46
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <type_traits>
2929

3030
#include <protocols/Protocols.h>
31+
#include <protocols/interaction_model/StatusCode.h>
3132

3233
/**
3334
* @namespace chip::Protocols::InteractionModel
@@ -65,52 +66,6 @@ enum class MsgType : uint8_t
6566
TimedRequest = 0x0a,
6667
};
6768

68-
// This table comes from the IM's "Status Code Table" section from the Interaction Model spec.
69-
enum class Status : uint8_t
70-
{
71-
Success = 0x0,
72-
Failure = 0x01,
73-
InvalidSubscription = 0x7d,
74-
UnsupportedAccess = 0x7e,
75-
UnsupportedEndpoint = 0x7f,
76-
InvalidAction = 0x80,
77-
UnsupportedCommand = 0x81,
78-
Deprecated82 = 0x82,
79-
Deprecated83 = 0x83,
80-
Deprecated84 = 0x84,
81-
InvalidCommand = 0x85,
82-
UnsupportedAttribute = 0x86,
83-
ConstraintError = 0x87,
84-
InvalidValue = ConstraintError, // Deprecated
85-
UnsupportedWrite = 0x88,
86-
ResourceExhausted = 0x89,
87-
Deprecated8a = 0x8a,
88-
NotFound = 0x8b,
89-
UnreportableAttribute = 0x8c,
90-
InvalidDataType = 0x8d,
91-
Deprecated8e = 0x8e,
92-
UnsupportedRead = 0x8f,
93-
Deprecated90 = 0x90,
94-
Deprecated91 = 0x91,
95-
DataVersionMismatch = 0x92,
96-
Deprecated93 = 0x93,
97-
Timeout = 0x94,
98-
Reserved95 = 0x95,
99-
Reserved96 = 0x96,
100-
Reserved97 = 0x97,
101-
Reserved98 = 0x98,
102-
Reserved99 = 0x99,
103-
Reserved9a = 0x9a,
104-
Busy = 0x9c,
105-
Deprecatedc0 = 0xc0,
106-
Deprecatedc1 = 0xc1,
107-
Deprecatedc2 = 0xc2,
108-
UnsupportedCluster = 0xc3,
109-
Deprecatedc4 = 0xc4,
110-
NoUpstreamSubscription = 0xc5,
111-
NeedsTimedInteraction = 0xc6,
112-
UnsupportedEvent = 0xc7,
113-
};
11469
} // namespace InteractionModel
11570

11671
template <>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
#include <lib/core/CHIPConfig.h>
19+
#include <protocols/interaction_model/StatusCode.h>
20+
21+
namespace chip {
22+
namespace Protocols {
23+
namespace InteractionModel {
24+
25+
#if CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
26+
const char * StatusName(Status status)
27+
{
28+
switch (status)
29+
{
30+
#define CHIP_IM_STATUS_CODE(name, spec_name, value) \
31+
case Status(value): \
32+
return #spec_name;
33+
#include <protocols/interaction_model/StatusCodeList.h>
34+
#undef CHIP_IM_STATUS_CODE
35+
}
36+
37+
return "Unallocated";
38+
}
39+
#endif // CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
40+
41+
} // namespace InteractionModel
42+
} // namespace Protocols
43+
} // namespace chip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
#pragma once
19+
20+
#include <stdint.h>
21+
22+
#include <lib/core/CHIPConfig.h>
23+
#include <lib/support/TypeTraits.h>
24+
25+
#if CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
26+
#define ChipLogFormatIMStatus "0x%02x (%s)"
27+
#define ChipLogValueIMStatus(status) chip::to_underlying(status), chip::Protocols::InteractionModel::StatusName(status)
28+
#else // CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
29+
#define ChipLogFormatIMStatus "0x%02x"
30+
#define ChipLogValueIMStatus(status) chip::to_underlying(status)
31+
#endif // CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
32+
33+
namespace chip {
34+
namespace Protocols {
35+
namespace InteractionModel {
36+
37+
// This table comes from the IM's "Status Code Table" section from the Interaction Model spec.
38+
enum class Status : uint8_t
39+
{
40+
#define CHIP_IM_STATUS_CODE(name, spec_name, value) name = value,
41+
#include <protocols/interaction_model/StatusCodeList.h>
42+
#undef CHIP_IM_STATUS_CODE
43+
44+
InvalidValue = ConstraintError, // Deprecated
45+
};
46+
47+
#if CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
48+
const char * StatusName(Status status);
49+
#endif // CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
50+
51+
} // namespace InteractionModel
52+
} // namespace Protocols
53+
} // namespace chip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
* This file is designed to be included multiple times, hence does not use
20+
* include guards or #pragma once. Consumers must define the
21+
* CHIP_IM_STATUS_CODE(name, spec_name, value) macro to do whatever they want,
22+
* include this file, then undefine the macro.
23+
*/
24+
25+
// clang-format off
26+
CHIP_IM_STATUS_CODE(Success , SUCCESS , 0x0)
27+
CHIP_IM_STATUS_CODE(Failure , FAILURE , 0x01)
28+
CHIP_IM_STATUS_CODE(InvalidSubscription , INVALID_SUBSCRIPTION , 0x7d)
29+
CHIP_IM_STATUS_CODE(UnsupportedAccess , UNSUPPORTED_ACCESS , 0x7e)
30+
CHIP_IM_STATUS_CODE(UnsupportedEndpoint , UNSUPPORTED_ENDPOINT , 0x7f)
31+
CHIP_IM_STATUS_CODE(InvalidAction , INVALID_ACTION , 0x80)
32+
CHIP_IM_STATUS_CODE(UnsupportedCommand , UNSUPPORTED_COMMAND , 0x81)
33+
CHIP_IM_STATUS_CODE(Deprecated82 , Deprecated82 , 0x82)
34+
CHIP_IM_STATUS_CODE(Deprecated83 , Deprecated83 , 0x83)
35+
CHIP_IM_STATUS_CODE(Deprecated84 , Deprecated84 , 0x84)
36+
CHIP_IM_STATUS_CODE(InvalidCommand , INVALID_COMMAND , 0x85)
37+
CHIP_IM_STATUS_CODE(UnsupportedAttribute , UNSUPPORTED_ATTRIBUTE , 0x86)
38+
CHIP_IM_STATUS_CODE(ConstraintError , CONSTRAINT_ERROR , 0x87)
39+
CHIP_IM_STATUS_CODE(UnsupportedWrite , UNSUPPORTED_WRITE , 0x88)
40+
CHIP_IM_STATUS_CODE(ResourceExhausted , RESOURCE_EXHAUSTED , 0x89)
41+
CHIP_IM_STATUS_CODE(Deprecated8a , Deprecated8a , 0x8a)
42+
CHIP_IM_STATUS_CODE(NotFound , NOT_FOUND , 0x8b)
43+
CHIP_IM_STATUS_CODE(UnreportableAttribute , UNREPORTABLE_ATTRIBUTE , 0x8c)
44+
CHIP_IM_STATUS_CODE(InvalidDataType , INVALID_DATA_TYPE , 0x8d)
45+
CHIP_IM_STATUS_CODE(Deprecated8e , Deprecated8e , 0x8e)
46+
CHIP_IM_STATUS_CODE(UnsupportedRead , UNSUPPORTED_READ , 0x8f)
47+
CHIP_IM_STATUS_CODE(Deprecated90 , Deprecated90 , 0x90)
48+
CHIP_IM_STATUS_CODE(Deprecated91 , Deprecated91 , 0x91)
49+
CHIP_IM_STATUS_CODE(DataVersionMismatch , DATA_VERSION_MISMATCH , 0x92)
50+
CHIP_IM_STATUS_CODE(Deprecated93 , Deprecated93 , 0x93)
51+
CHIP_IM_STATUS_CODE(Timeout , TIMEOUT , 0x94)
52+
CHIP_IM_STATUS_CODE(Reserved95 , Reserved95 , 0x95)
53+
CHIP_IM_STATUS_CODE(Reserved96 , Reserved96 , 0x96)
54+
CHIP_IM_STATUS_CODE(Reserved97 , Reserved97 , 0x97)
55+
CHIP_IM_STATUS_CODE(Reserved98 , Reserved98 , 0x98)
56+
CHIP_IM_STATUS_CODE(Reserved99 , Reserved99 , 0x99)
57+
CHIP_IM_STATUS_CODE(Reserved9a , Reserved9a , 0x9a)
58+
CHIP_IM_STATUS_CODE(Busy , BUSY , 0x9c)
59+
CHIP_IM_STATUS_CODE(Deprecatedc0 , Deprecatedc0 , 0xc0)
60+
CHIP_IM_STATUS_CODE(Deprecatedc1 , Deprecatedc1 , 0xc1)
61+
CHIP_IM_STATUS_CODE(Deprecatedc2 , Deprecatedc2 , 0xc2)
62+
CHIP_IM_STATUS_CODE(UnsupportedCluster , UNSUPPORTED_CLUSTER , 0xc3)
63+
CHIP_IM_STATUS_CODE(Deprecatedc4 , Deprecatedc4 , 0xc4)
64+
CHIP_IM_STATUS_CODE(NoUpstreamSubscription, NO_UPSTREAM_SUBSCRIPTION, 0xc5)
65+
CHIP_IM_STATUS_CODE(NeedsTimedInteraction , NEEDS_TIMED_INTERACTION , 0xc6)
66+
CHIP_IM_STATUS_CODE(UnsupportedEvent , UNSUPPORTED_EVENT , 0xc7)
67+
// clang-format on

0 commit comments

Comments
 (0)