Skip to content

Commit 342ac65

Browse files
address comments
1 parent fb5aaf7 commit 342ac65

File tree

5 files changed

+197
-5
lines changed

5 files changed

+197
-5
lines changed

examples/chip-tool/commands/clusters/ClusterCommand.h

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <app/tests/suites/commands/interaction_model/InteractionModel.h>
2222

2323
#include "DataModelLogger.h"
24+
#include "DataModelCustomHandler.h"
2425
#include "ModelCommand.h"
2526

2627
class ClusterCommand : public InteractionModelCommands, public ModelCommand, public chip::app::CommandSender::Callback
@@ -109,6 +110,7 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub
109110
return;
110111
}
111112
}
113+
DataModelCustomHandler::HandleCommand(path, data);
112114
}
113115

114116
virtual void OnError(const chip::app::CommandSender * client, CHIP_ERROR error) override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
#pragma once
20+
21+
#include <string>
22+
23+
#include <app-common/zap-generated/cluster-objects.h>
24+
#include <app/ConcreteAttributePath.h>
25+
#include <app/ConcreteCommandPath.h>
26+
#include <app/EventHeader.h>
27+
#include <app/MessageDef/StatusIB.h>
28+
#include <app/data-model/DecodableList.h>
29+
#include <commands/common/RemoteDataModelLogger.h>
30+
#include <lib/support/BytesToHex.h>
31+
32+
class DataModelCustomHandler
33+
{
34+
public:
35+
static CHIP_ERROR HandleAttribute(const chip::app::ConcreteDataAttributePath & path, chip::TLV::TLVReader * data);
36+
static CHIP_ERROR HandleCommand(const chip::app::ConcreteCommandPath & path, chip::TLV::TLVReader * data);
37+
static CHIP_ERROR HandleEvent(const chip::app::EventHeader & header, chip::TLV::TLVReader * data);
38+
39+
private:
40+
static CHIP_ERROR LogValue(const char * label, size_t indent, bool value)
41+
{
42+
DataModelLogger::LogString(label, indent, value ? "TRUE" : "FALSE");
43+
return CHIP_NO_ERROR;
44+
}
45+
46+
static CHIP_ERROR LogValue(const char * label, size_t indent, chip::CharSpan value)
47+
{
48+
DataModelLogger::LogString(label, indent, std::string(value.data(), value.size()));
49+
return CHIP_NO_ERROR;
50+
}
51+
52+
static CHIP_ERROR LogValue(const char * label, size_t indent, chip::ByteSpan value)
53+
{
54+
// CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE includes various prefixes we don't
55+
// control (timestamps, process ids, etc). Let's assume (hope?) that
56+
// those prefixes use up no more than half the total available space.
57+
// Right now it looks like the prefixes are 45 chars out of a 255 char
58+
// buffer.
59+
char buffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE / 2];
60+
size_t prefixSize = ComputePrefixSize(label, indent);
61+
if (prefixSize > ArraySize(buffer))
62+
{
63+
DataModelLogger::LogString("", 0, "Prefix is too long to fit in buffer");
64+
return CHIP_ERROR_INTERNAL;
65+
}
66+
67+
const size_t availableSize = ArraySize(buffer) - prefixSize;
68+
// Each byte ends up as two hex characters.
69+
const size_t bytesPerLogCall = availableSize / 2;
70+
std::string labelStr(label);
71+
while (value.size() > bytesPerLogCall)
72+
{
73+
ReturnErrorOnFailure(
74+
chip::Encoding::BytesToUppercaseHexString(value.data(), bytesPerLogCall, &buffer[0], ArraySize(buffer)));
75+
LogString(labelStr, indent, buffer);
76+
value = value.SubSpan(bytesPerLogCall);
77+
// For the second and following lines, make it clear that they are
78+
// continuation lines by replacing the label with "....".
79+
labelStr.replace(labelStr.begin(), labelStr.end(), labelStr.size(), '.');
80+
}
81+
ReturnErrorOnFailure(chip::Encoding::BytesToUppercaseHexString(value.data(), value.size(), &buffer[0], ArraySize(buffer)));
82+
LogString(labelStr, indent, buffer);
83+
84+
return CHIP_NO_ERROR;
85+
}
86+
87+
template <typename X,
88+
typename std::enable_if_t<
89+
std::is_integral<X>::value && !std::is_same<std::remove_cv_t<std::remove_reference_t<X>>, bool>::value, int> = 0>
90+
static CHIP_ERROR LogValue(const char * label, size_t indent, X value)
91+
{
92+
DataModelLogger::LogString(label, indent, std::to_string(value));
93+
return CHIP_NO_ERROR;
94+
}
95+
96+
template <typename X, typename std::enable_if_t<std::is_floating_point<X>::value, int> = 0>
97+
static CHIP_ERROR LogValue(const char * label, size_t indent, X value)
98+
{
99+
DataModelLogger::LogString(label, indent, std::to_string(value));
100+
return CHIP_NO_ERROR;
101+
}
102+
103+
template <typename X, typename std::enable_if_t<std::is_enum<X>::value, int> = 0>
104+
static CHIP_ERROR LogValue(const char * label, size_t indent, X value)
105+
{
106+
return DataModelLogger::LogValue(label, indent, chip::to_underlying(value));
107+
}
108+
109+
template <typename X>
110+
static CHIP_ERROR LogValue(const char * label, size_t indent, chip::BitFlags<X> value)
111+
{
112+
return DataModelLogger::LogValue(label, indent, value.Raw());
113+
}
114+
115+
template <typename T>
116+
static CHIP_ERROR LogValue(const char * label, size_t indent, const chip::app::DataModel::DecodableList<T> & value)
117+
{
118+
size_t count = 0;
119+
ReturnErrorOnFailure(value.ComputeSize(&count));
120+
DataModelLogger::LogString(label, indent, std::to_string(count) + " entries");
121+
122+
auto iter = value.begin();
123+
size_t i = 0;
124+
while (iter.Next())
125+
{
126+
++i;
127+
std::string itemLabel = std::string("[") + std::to_string(i) + "]";
128+
ReturnErrorOnFailure(DataModelLogger::LogValue(itemLabel.c_str(), indent + 1, iter.GetValue()));
129+
}
130+
if (iter.GetStatus() != CHIP_NO_ERROR)
131+
{
132+
DataModelLogger::LogString(indent + 1, "List truncated due to invalid value");
133+
}
134+
return iter.GetStatus();
135+
}
136+
137+
template <typename T>
138+
static CHIP_ERROR LogValue(const char * label, size_t indent, const chip::app::DataModel::Nullable<T> & value)
139+
{
140+
if (value.IsNull())
141+
{
142+
DataModelLogger::LogString(label, indent, "null");
143+
return CHIP_NO_ERROR;
144+
}
145+
146+
return DataModelLogger::LogValue(label, indent, value.Value());
147+
}
148+
149+
template <typename T>
150+
static CHIP_ERROR LogValue(const char * label, size_t indent, const chip::Optional<T> & value)
151+
{
152+
if (value.HasValue())
153+
{
154+
return DataModelLogger::LogValue(label, indent, value.Value());
155+
}
156+
157+
return CHIP_NO_ERROR;
158+
}
159+
160+
#include <zap-generated/cluster/logging/DataModelLogger.h>
161+
162+
static void LogString(size_t indent, const std::string string) { LogString("", indent, string); }
163+
164+
static void LogString(const std::string label, size_t indent, const std::string string)
165+
{
166+
std::string prefix = ComputePrefix(label, indent);
167+
168+
ChipLogProgress(chipTool, "%s%s", prefix.c_str(), string.c_str());
169+
}
170+
171+
private:
172+
static std::string ComputePrefix(const std::string label, size_t indent)
173+
{
174+
std::string prefix;
175+
for (size_t i = 0; i < indent; ++i)
176+
{
177+
prefix.append(" ");
178+
}
179+
if (label.size() > 0)
180+
{
181+
prefix.append(label);
182+
prefix.append(":");
183+
}
184+
prefix.append(" ");
185+
186+
return prefix;
187+
}
188+
189+
static size_t ComputePrefixSize(const std::string label, size_t indent) { return ComputePrefix(label, indent).size(); }
190+
};

examples/chip-tool/commands/pairing/PairingCommand.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ CommissioningParameters PairingCommand::GetCommissioningParameters()
136136
params.SetDSTOffsets(mDSTOffsetList);
137137
}
138138

139-
if (!mSkipICDRegistration.ValueOr(true))
139+
if (mICDRegistration.ValueOr(true))
140140
{
141141
params.SetICDRegistrationStrategy(ICDRegistrationStrategy::kBeforeComplete);
142142

examples/chip-tool/commands/pairing/PairingCommand.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class PairingCommand : public CHIPCommand,
6565
"Bypass the attestation verifier. If not provided or false, the attestation verifier is not bypassed."
6666
" If true, the commissioning will continue in case of attestation verification failure.");
6767
AddArgument("case-auth-tags", 1, UINT32_MAX, &mCASEAuthTags, "The CATs to be encoded in the NOC sent to the commissionee");
68-
AddArgument("skip-icd-registration", 0, 1, &mSkipICDRegistration,
69-
"Skip registering for check-ins from ICDs during commissioning. Default: true");
68+
AddArgument("icd-registration", 0, 1, &mICDRegistration,
69+
"registering for check-ins from ICDs during commissioning. Default: false");
7070
AddArgument("icd-check-in-nodeid", 0, UINT64_MAX, &mICDCheckInNodeId,
7171
"The check-in node id for the ICD, default: node id of the commissioner.");
7272
AddArgument("icd-monitored-subject", 0, UINT64_MAX, &mICDMonitoredSubject,
@@ -233,7 +233,7 @@ class PairingCommand : public CHIPCommand,
233233
chip::Optional<bool> mBypassAttestationVerifier;
234234
chip::Optional<std::vector<uint32_t>> mCASEAuthTags;
235235
chip::Optional<char *> mCountryCode;
236-
chip::Optional<bool> mSkipICDRegistration;
236+
chip::Optional<bool> mICDRegistration;
237237
chip::Optional<NodeId> mICDCheckInNodeId;
238238
chip::Optional<chip::ByteSpan> mICDSymmetricKey;
239239
chip::Optional<uint64_t> mICDMonitoredSubject;

scripts/tests/chiptest/test_definition.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def Run(self, runner, apps_register, paths: ApplicationPaths, pics_file: str,
373373
dependencies=[apps_register], timeout_seconds=timeout_seconds)
374374
else:
375375
pairing_cmd = paths.chip_tool_with_python_cmd + \
376-
['pairing', 'code', TEST_NODE_ID, setupCode, '--skip-icd-registration', 'false']
376+
['pairing', 'code', TEST_NODE_ID, setupCode, '--icd-registration', 'false']
377377
test_cmd = paths.chip_tool_with_python_cmd + ['tests', self.run_name] + ['--PICS', pics_file]
378378
server_args = ['--server_path', paths.chip_tool[-1]] + \
379379
['--server_arguments', 'interactive server' +

0 commit comments

Comments
 (0)