Skip to content

Commit 3e0220d

Browse files
andy31415andreilitvinrestyled-commits
authored
Use mandatory metadata when possible to reduce copy & paste errors. (project-chip#41361)
* Use mandatory metadata when possible to reduce copy & paste errors. * Fix network commissioning as well * Update network commissioning to use the consolidated builder instead of old code. * Restyled by clang-format --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 4cf1f96 commit 3e0220d

4 files changed

Lines changed: 32 additions & 30 deletions

File tree

src/app/clusters/basic-information/BasicInformationCluster.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ inline constexpr uint32_t kRevisionWithoutUniqueId = 3;
4949

5050
// This is NOT the same as the auto-generated attributes:
5151
// see comment below about UniqueID (which we make behave as optional)
52+
//
53+
// TLDR: DO NOT USE kMandatoryMetadata (because unique ID is special for backwards compat builds)
5254
constexpr DataModel::AttributeEntry kMandatoryAttributes[] = {
5355
DataModelRevision::kMetadataEntry,
5456
VendorName::kMetadataEntry,
@@ -404,6 +406,7 @@ CHIP_ERROR BasicInformationCluster::Attributes(const ConcreteClusterPath & path,
404406

405407
AttributeListBuilder listBuilder(builder);
406408

409+
// NOTE: do NOT use kMandatoryMetadata (see comment on kMandatoryAttributes: UniqueID is special)
407410
return listBuilder.Append(Span(kMandatoryAttributes), Span(optionalAttributes), mEnabledOptionalAttributes);
408411
}
409412

src/app/clusters/network-commissioning/NetworkCommissioningCluster.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
#include "NetworkCommissioningCluster.h"
1818

19+
#include <app/server-cluster/AttributeListBuilder.h>
1920
#include <app/server-cluster/DefaultServerCluster.h>
2021
#include <clusters/NetworkCommissioning/AttributeIds.h>
2122
#include <clusters/NetworkCommissioning/CommandIds.h>
@@ -226,43 +227,47 @@ CHIP_ERROR NetworkCommissioningCluster::Attributes(const ConcreteClusterPath & p
226227
using namespace NetworkCommissioning::Attributes;
227228
using NetworkCommissioning::Feature;
228229

229-
// mandatory attributes
230-
ReturnErrorOnFailure(builder.AppendElements({
231-
MaxNetworks::kMetadataEntry,
232-
Networks::kMetadataEntry,
233-
InterfaceEnabled::kMetadataEntry,
234-
LastNetworkingStatus::kMetadataEntry,
235-
LastNetworkID::kMetadataEntry,
236-
LastConnectErrorValue::kMetadataEntry,
237-
}));
230+
AttributeListBuilder attributeListBuilder(builder);
231+
232+
constexpr DataModel::AttributeEntry kOptionalAttributes[] = {
233+
ScanMaxTimeSeconds::kMetadataEntry, //
234+
ConnectMaxTimeSeconds::kMetadataEntry, //
235+
SupportedThreadFeatures::kMetadataEntry, //
236+
ThreadVersion::kMetadataEntry, //
237+
SupportedWiFiBands::kMetadataEntry, //
238+
};
239+
240+
chip::app::OptionalAttributeSet<ScanMaxTimeSeconds::Id, //
241+
ConnectMaxTimeSeconds::Id, //
242+
SupportedThreadFeatures::Id, //
243+
ThreadVersion::Id, //
244+
SupportedWiFiBands::Id //
245+
>
246+
optionalAttributes;
238247

239248
// NOTE: thread and wifi are mutually exclusive features
240249
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
241250
if (mLogic.Features().Has(Feature::kThreadNetworkInterface))
242251
{
243-
ReturnErrorOnFailure(builder.AppendElements({
244-
ScanMaxTimeSeconds::kMetadataEntry,
245-
ConnectMaxTimeSeconds::kMetadataEntry,
246-
SupportedThreadFeatures::kMetadataEntry,
247-
ThreadVersion::kMetadataEntry,
248-
}));
252+
optionalAttributes.ForceSet<ScanMaxTimeSeconds::Id>();
253+
optionalAttributes.ForceSet<ConnectMaxTimeSeconds::Id>();
254+
optionalAttributes.ForceSet<SupportedThreadFeatures::Id>();
255+
optionalAttributes.ForceSet<ThreadVersion::Id>();
249256
}
250257
else
251258
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
252259
#if (CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION || CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP)
253260
if (mLogic.Features().Has(Feature::kWiFiNetworkInterface))
254261
{
255-
ReturnErrorOnFailure(builder.AppendElements({
256-
ScanMaxTimeSeconds::kMetadataEntry,
257-
ConnectMaxTimeSeconds::kMetadataEntry,
258-
SupportedWiFiBands::kMetadataEntry,
259-
}));
262+
optionalAttributes.ForceSet<ScanMaxTimeSeconds::Id>();
263+
optionalAttributes.ForceSet<ConnectMaxTimeSeconds::Id>();
264+
optionalAttributes.ForceSet<SupportedWiFiBands::Id>();
260265
}
261266
else
262267
#endif // (CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION || CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP)
263268
{}
264269

265-
return builder.AppendElements(DefaultServerCluster::GlobalAttributes());
270+
return attributeListBuilder.Append(Span(kMandatoryMetadata), Span(kOptionalAttributes), optionalAttributes);
266271
}
267272

268273
} // namespace Clusters

src/app/clusters/time-format-localization-server/time-format-localization-cluster.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ namespace app {
2727
namespace Clusters {
2828

2929
namespace {
30-
constexpr DataModel::AttributeEntry kMandatoryAttributes[] = {
31-
TimeFormatLocalization::Attributes::HourFormat::kMetadataEntry,
32-
};
3330
class AutoReleaseIterator
3431
{
3532
public:
@@ -225,7 +222,8 @@ CHIP_ERROR TimeFormatLocalizationCluster::Attributes(const ConcreteClusterPath &
225222
optionalAttributeSet.Set<TimeFormatLocalization::Attributes::SupportedCalendarTypes::Id>();
226223
}
227224

228-
return listBuilder.Append(Span(kMandatoryAttributes), Span(optionalAttributes), optionalAttributeSet);
225+
return listBuilder.Append(Span(TimeFormatLocalization::Attributes::kMandatoryMetadata), Span(optionalAttributes),
226+
optionalAttributeSet);
229227
}
230228

231229
} // namespace Clusters

src/app/clusters/webrtc-transport-requestor-server/webrtc-transport-requestor-cluster.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ constexpr DataModel::AcceptedCommandEntry kAcceptedCommands[] = {
3939
Commands::End::kMetadataEntry,
4040
};
4141

42-
constexpr DataModel::AttributeEntry kMandatoryAttributes[] = {
43-
CurrentSessions::kMetadataEntry,
44-
};
45-
4642
NodeId GetNodeIdFromCtx(const CommandHandler & commandHandler)
4743
{
4844
auto descriptor = commandHandler.GetSubjectDescriptor();
@@ -143,7 +139,7 @@ CHIP_ERROR WebRTCTransportRequestorServer::Attributes(const ConcreteClusterPath
143139
ReadOnlyBufferBuilder<DataModel::AttributeEntry> & builder)
144140
{
145141
AttributeListBuilder listBuilder(builder);
146-
return listBuilder.Append(Span(kMandatoryAttributes), {}, {});
142+
return listBuilder.Append(Span(kMandatoryMetadata), {}, {});
147143
}
148144

149145
uint16_t WebRTCTransportRequestorServer::GenerateSessionId()

0 commit comments

Comments
 (0)