Skip to content

Commit 2fed176

Browse files
authored
Decouple ember functions from wifi diagnostic cluster (project-chip#37097)
* Decouple ember functions from general diagnostic cluster * Address review comment
1 parent bdbd217 commit 2fed176

File tree

5 files changed

+51
-123
lines changed

5 files changed

+51
-123
lines changed

scripts/tools/zap/tests/outputs/all-clusters-app/app-templates/IMClusterCommandHandler.cpp

-44
Original file line numberDiff line numberDiff line change
@@ -1748,47 +1748,6 @@ Protocols::InteractionModel::Status DispatchServerCommand(CommandHandler * apCom
17481748

17491749
} // namespace ValveConfigurationAndControl
17501750

1751-
namespace WiFiNetworkDiagnostics {
1752-
1753-
Protocols::InteractionModel::Status DispatchServerCommand(CommandHandler * apCommandObj, const ConcreteCommandPath & aCommandPath,
1754-
TLV::TLVReader & aDataTlv)
1755-
{
1756-
CHIP_ERROR TLVError = CHIP_NO_ERROR;
1757-
bool wasHandled = false;
1758-
{
1759-
switch (aCommandPath.mCommandId)
1760-
{
1761-
case Commands::ResetCounts::Id: {
1762-
Commands::ResetCounts::DecodableType commandData;
1763-
TLVError = DataModel::Decode(aDataTlv, commandData);
1764-
if (TLVError == CHIP_NO_ERROR)
1765-
{
1766-
wasHandled = emberAfWiFiNetworkDiagnosticsClusterResetCountsCallback(apCommandObj, aCommandPath, commandData);
1767-
}
1768-
break;
1769-
}
1770-
default: {
1771-
// Unrecognized command ID, error status will apply.
1772-
ChipLogError(Zcl, "Unknown command " ChipLogFormatMEI " for cluster " ChipLogFormatMEI,
1773-
ChipLogValueMEI(aCommandPath.mCommandId), ChipLogValueMEI(aCommandPath.mClusterId));
1774-
return Protocols::InteractionModel::Status::UnsupportedCommand;
1775-
}
1776-
}
1777-
}
1778-
1779-
if (CHIP_NO_ERROR != TLVError || !wasHandled)
1780-
{
1781-
ChipLogProgress(Zcl, "Failed to dispatch command, TLVError=%" CHIP_ERROR_FORMAT, TLVError.Format());
1782-
return Protocols::InteractionModel::Status::InvalidCommand;
1783-
}
1784-
1785-
// We use success as a marker that no special handling is required
1786-
// This is to avoid having a std::optional which uses slightly more code.
1787-
return Protocols::InteractionModel::Status::Success;
1788-
}
1789-
1790-
} // namespace WiFiNetworkDiagnostics
1791-
17921751
namespace WindowCovering {
17931752

17941753
Protocols::InteractionModel::Status DispatchServerCommand(CommandHandler * apCommandObj, const ConcreteCommandPath & aCommandPath,
@@ -1964,9 +1923,6 @@ void DispatchSingleClusterCommand(const ConcreteCommandPath & aCommandPath, TLV:
19641923
case Clusters::ValveConfigurationAndControl::Id:
19651924
errorStatus = Clusters::ValveConfigurationAndControl::DispatchServerCommand(apCommandObj, aCommandPath, aReader);
19661925
break;
1967-
case Clusters::WiFiNetworkDiagnostics::Id:
1968-
errorStatus = Clusters::WiFiNetworkDiagnostics::DispatchServerCommand(apCommandObj, aCommandPath, aReader);
1969-
break;
19701926
case Clusters::WindowCovering::Id:
19711927
errorStatus = Clusters::WindowCovering::DispatchServerCommand(apCommandObj, aCommandPath, aReader);
19721928
break;

scripts/tools/zap/tests/outputs/lighting-app/app-templates/IMClusterCommandHandler.cpp

-44
Original file line numberDiff line numberDiff line change
@@ -969,47 +969,6 @@ Protocols::InteractionModel::Status DispatchServerCommand(CommandHandler * apCom
969969

970970
} // namespace ThreadNetworkDiagnostics
971971

972-
namespace WiFiNetworkDiagnostics {
973-
974-
Protocols::InteractionModel::Status DispatchServerCommand(CommandHandler * apCommandObj, const ConcreteCommandPath & aCommandPath,
975-
TLV::TLVReader & aDataTlv)
976-
{
977-
CHIP_ERROR TLVError = CHIP_NO_ERROR;
978-
bool wasHandled = false;
979-
{
980-
switch (aCommandPath.mCommandId)
981-
{
982-
case Commands::ResetCounts::Id: {
983-
Commands::ResetCounts::DecodableType commandData;
984-
TLVError = DataModel::Decode(aDataTlv, commandData);
985-
if (TLVError == CHIP_NO_ERROR)
986-
{
987-
wasHandled = emberAfWiFiNetworkDiagnosticsClusterResetCountsCallback(apCommandObj, aCommandPath, commandData);
988-
}
989-
break;
990-
}
991-
default: {
992-
// Unrecognized command ID, error status will apply.
993-
ChipLogError(Zcl, "Unknown command " ChipLogFormatMEI " for cluster " ChipLogFormatMEI,
994-
ChipLogValueMEI(aCommandPath.mCommandId), ChipLogValueMEI(aCommandPath.mClusterId));
995-
return Protocols::InteractionModel::Status::UnsupportedCommand;
996-
}
997-
}
998-
}
999-
1000-
if (CHIP_NO_ERROR != TLVError || !wasHandled)
1001-
{
1002-
ChipLogProgress(Zcl, "Failed to dispatch command, TLVError=%" CHIP_ERROR_FORMAT, TLVError.Format());
1003-
return Protocols::InteractionModel::Status::InvalidCommand;
1004-
}
1005-
1006-
// We use success as a marker that no special handling is required
1007-
// This is to avoid having a std::optional which uses slightly more code.
1008-
return Protocols::InteractionModel::Status::Success;
1009-
}
1010-
1011-
} // namespace WiFiNetworkDiagnostics
1012-
1013972
} // namespace Clusters
1014973

1015974
void DispatchSingleClusterCommand(const ConcreteCommandPath & aCommandPath, TLV::TLVReader & aReader, CommandHandler * apCommandObj)
@@ -1054,9 +1013,6 @@ void DispatchSingleClusterCommand(const ConcreteCommandPath & aCommandPath, TLV:
10541013
case Clusters::ThreadNetworkDiagnostics::Id:
10551014
errorStatus = Clusters::ThreadNetworkDiagnostics::DispatchServerCommand(apCommandObj, aCommandPath, aReader);
10561015
break;
1057-
case Clusters::WiFiNetworkDiagnostics::Id:
1058-
errorStatus = Clusters::WiFiNetworkDiagnostics::DispatchServerCommand(apCommandObj, aCommandPath, aReader);
1059-
break;
10601016
default:
10611017
ChipLogError(Zcl, "Unknown cluster " ChipLogFormatMEI, ChipLogValueMEI(aCommandPath.mClusterId));
10621018
errorStatus = Protocols::InteractionModel::Status::UnsupportedCluster;

src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp

+50-29
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <app/AttributeAccessInterface.h>
2424
#include <app/AttributeAccessInterfaceRegistry.h>
2525
#include <app/CommandHandler.h>
26+
#include <app/CommandHandlerInterface.h>
27+
#include <app/CommandHandlerInterfaceRegistry.h>
2628
#include <app/ConcreteCommandPath.h>
2729
#include <app/EventLogging.h>
2830
#include <app/util/attribute-storage.h>
@@ -40,11 +42,15 @@ using chip::DeviceLayer::GetDiagnosticDataProvider;
4042

4143
namespace {
4244

43-
class WiFiDiagosticsAttrAccess : public AttributeAccessInterface
45+
class WiFiDiagosticsGlobalInstance : public AttributeAccessInterface, public CommandHandlerInterface
4446
{
4547
public:
4648
// Register for the WiFiNetworkDiagnostics cluster on all endpoints.
47-
WiFiDiagosticsAttrAccess() : AttributeAccessInterface(Optional<EndpointId>::Missing(), WiFiNetworkDiagnostics::Id) {}
49+
WiFiDiagosticsGlobalInstance(DiagnosticDataProvider & diagnosticProvider) :
50+
AttributeAccessInterface(Optional<EndpointId>::Missing(), WiFiNetworkDiagnostics::Id),
51+
CommandHandlerInterface(Optional<EndpointId>::Missing(), WiFiNetworkDiagnostics::Id),
52+
mDiagnosticProvider(diagnosticProvider)
53+
{}
4854

4955
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
5056

@@ -57,14 +63,20 @@ class WiFiDiagosticsAttrAccess : public AttributeAccessInterface
5763
CHIP_ERROR ReadWiFiVersion(AttributeValueEncoder & aEncoder);
5864
CHIP_ERROR ReadChannelNumber(AttributeValueEncoder & aEncoder);
5965
CHIP_ERROR ReadWiFiRssi(AttributeValueEncoder & aEncoder);
66+
67+
void InvokeCommand(HandlerContext & ctx) override;
68+
69+
void HandleResetCounts(HandlerContext & ctx, const Commands::ResetCounts::DecodableType & commandData);
70+
71+
DiagnosticDataProvider & mDiagnosticProvider;
6072
};
6173

6274
template <typename T, typename Type>
63-
CHIP_ERROR WiFiDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(T &), Type & data,
64-
AttributeValueEncoder & aEncoder)
75+
CHIP_ERROR WiFiDiagosticsGlobalInstance::ReadIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(T &), Type & data,
76+
AttributeValueEncoder & aEncoder)
6577
{
6678
T value;
67-
CHIP_ERROR err = (DeviceLayer::GetDiagnosticDataProvider().*getter)(value);
79+
CHIP_ERROR err = (mDiagnosticProvider.*getter)(value);
6880

6981
if (err == CHIP_NO_ERROR)
7082
{
@@ -78,13 +90,13 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (DiagnosticDataP
7890
return aEncoder.Encode(data);
7991
}
8092

81-
CHIP_ERROR WiFiDiagosticsAttrAccess::ReadWiFiBssId(AttributeValueEncoder & aEncoder)
93+
CHIP_ERROR WiFiDiagosticsGlobalInstance::ReadWiFiBssId(AttributeValueEncoder & aEncoder)
8294
{
8395
Attributes::Bssid::TypeInfo::Type bssid;
8496

8597
uint8_t bssidBytes[chip::DeviceLayer::kMaxHardwareAddrSize];
8698
MutableByteSpan bssidSpan(bssidBytes);
87-
if (DeviceLayer::GetDiagnosticDataProvider().GetWiFiBssId(bssidSpan) == CHIP_NO_ERROR)
99+
if (mDiagnosticProvider.GetWiFiBssId(bssidSpan) == CHIP_NO_ERROR)
88100
{
89101
if (!bssidSpan.empty())
90102
{
@@ -101,12 +113,12 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::ReadWiFiBssId(AttributeValueEncoder & aEnco
101113
return aEncoder.Encode(bssid);
102114
}
103115

104-
CHIP_ERROR WiFiDiagosticsAttrAccess::ReadSecurityType(AttributeValueEncoder & aEncoder)
116+
CHIP_ERROR WiFiDiagosticsGlobalInstance::ReadSecurityType(AttributeValueEncoder & aEncoder)
105117
{
106118
Attributes::SecurityType::TypeInfo::Type securityType;
107119
SecurityTypeEnum value = SecurityTypeEnum::kUnspecified;
108120

109-
if (DeviceLayer::GetDiagnosticDataProvider().GetWiFiSecurityType(value) == CHIP_NO_ERROR)
121+
if (mDiagnosticProvider.GetWiFiSecurityType(value) == CHIP_NO_ERROR)
110122
{
111123
securityType.SetNonNull(value);
112124
ChipLogProgress(Zcl, "The current type of Wi-Fi security used: %d", to_underlying(value));
@@ -119,12 +131,12 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::ReadSecurityType(AttributeValueEncoder & aE
119131
return aEncoder.Encode(securityType);
120132
}
121133

122-
CHIP_ERROR WiFiDiagosticsAttrAccess::ReadWiFiVersion(AttributeValueEncoder & aEncoder)
134+
CHIP_ERROR WiFiDiagosticsGlobalInstance::ReadWiFiVersion(AttributeValueEncoder & aEncoder)
123135
{
124136
Attributes::WiFiVersion::TypeInfo::Type version;
125137
WiFiVersionEnum value = WiFiVersionEnum::kUnknownEnumValue;
126138

127-
if (DeviceLayer::GetDiagnosticDataProvider().GetWiFiVersion(value) == CHIP_NO_ERROR)
139+
if (mDiagnosticProvider.GetWiFiVersion(value) == CHIP_NO_ERROR)
128140
{
129141
version.SetNonNull(value);
130142
ChipLogProgress(Zcl, "The current 802.11 standard version in use by the Node: %d", to_underlying(value));
@@ -137,12 +149,12 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::ReadWiFiVersion(AttributeValueEncoder & aEn
137149
return aEncoder.Encode(version);
138150
}
139151

140-
CHIP_ERROR WiFiDiagosticsAttrAccess::ReadChannelNumber(AttributeValueEncoder & aEncoder)
152+
CHIP_ERROR WiFiDiagosticsGlobalInstance::ReadChannelNumber(AttributeValueEncoder & aEncoder)
141153
{
142154
Attributes::ChannelNumber::TypeInfo::Type channelNumber;
143155
uint16_t value = 0;
144156

145-
if (DeviceLayer::GetDiagnosticDataProvider().GetWiFiChannelNumber(value) == CHIP_NO_ERROR)
157+
if (mDiagnosticProvider.GetWiFiChannelNumber(value) == CHIP_NO_ERROR)
146158
{
147159
channelNumber.SetNonNull(value);
148160
ChipLogProgress(Zcl, "The channel that Wi-Fi communication is currently operating on is: %d", value);
@@ -155,12 +167,12 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::ReadChannelNumber(AttributeValueEncoder & a
155167
return aEncoder.Encode(channelNumber);
156168
}
157169

158-
CHIP_ERROR WiFiDiagosticsAttrAccess::ReadWiFiRssi(AttributeValueEncoder & aEncoder)
170+
CHIP_ERROR WiFiDiagosticsGlobalInstance::ReadWiFiRssi(AttributeValueEncoder & aEncoder)
159171
{
160172
Attributes::Rssi::TypeInfo::Type rssi;
161173
int8_t value = 0;
162174

163-
if (DeviceLayer::GetDiagnosticDataProvider().GetWiFiRssi(value) == CHIP_NO_ERROR)
175+
if (mDiagnosticProvider.GetWiFiRssi(value) == CHIP_NO_ERROR)
164176
{
165177
rssi.SetNonNull(value);
166178
ChipLogProgress(Zcl, "The current RSSI of the Node’s Wi-Fi radio in dB: %d", value);
@@ -174,9 +186,7 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::ReadWiFiRssi(AttributeValueEncoder & aEncod
174186
return aEncoder.Encode(rssi);
175187
}
176188

177-
WiFiDiagosticsAttrAccess gAttrAccess;
178-
179-
CHIP_ERROR WiFiDiagosticsAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
189+
CHIP_ERROR WiFiDiagosticsGlobalInstance::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
180190
{
181191
if (aPath.mClusterId != WiFiNetworkDiagnostics::Id)
182192
{
@@ -240,6 +250,25 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::Read(const ConcreteReadAttributePath & aPat
240250
return CHIP_NO_ERROR;
241251
}
242252

253+
void WiFiDiagosticsGlobalInstance::InvokeCommand(HandlerContext & handlerContext)
254+
{
255+
switch (handlerContext.mRequestPath.mCommandId)
256+
{
257+
case Commands::ResetCounts::Id:
258+
CommandHandlerInterface::HandleCommand<Commands::ResetCounts::DecodableType>(
259+
handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleResetCounts(ctx, commandData); });
260+
break;
261+
}
262+
}
263+
264+
void WiFiDiagosticsGlobalInstance::HandleResetCounts(HandlerContext & ctx, const Commands::ResetCounts::DecodableType & commandData)
265+
{
266+
mDiagnosticProvider.ResetWiFiNetworkDiagnosticsCounts();
267+
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Protocols::InteractionModel::Status::Success);
268+
}
269+
270+
WiFiDiagosticsGlobalInstance gWiFiDiagosticsInstance(DeviceLayer::GetDiagnosticDataProvider());
271+
243272
} // anonymous namespace
244273

245274
namespace chip {
@@ -316,18 +345,10 @@ void WiFiDiagnosticsServer::OnConnectionStatusChanged(uint8_t connectionStatus)
316345
} // namespace app
317346
} // namespace chip
318347

319-
bool emberAfWiFiNetworkDiagnosticsClusterResetCountsCallback(app::CommandHandler * commandObj,
320-
const app::ConcreteCommandPath & commandPath,
321-
const Commands::ResetCounts::DecodableType & commandData)
322-
{
323-
DeviceLayer::GetDiagnosticDataProvider().ResetWiFiNetworkDiagnosticsCounts();
324-
commandObj->AddStatus(commandPath, Protocols::InteractionModel::Status::Success);
325-
326-
return true;
327-
}
328-
329348
void MatterWiFiNetworkDiagnosticsPluginServerInitCallback()
330349
{
331-
AttributeAccessInterfaceRegistry::Instance().Register(&gAttrAccess);
350+
AttributeAccessInterfaceRegistry::Instance().Register(&gWiFiDiagosticsInstance);
351+
CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(&gWiFiDiagosticsInstance);
352+
332353
GetDiagnosticDataProvider().SetWiFiDiagnosticsDelegate(&WiFiDiagnosticsServer::Instance());
333354
}

src/app/common/templates/config-data.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ CommandHandlerInterfaceOnlyClusters:
5252
- General Commissioning
5353
- General Diagnostics
5454
- Software Diagnostics
55+
- Wi-Fi Network Diagnostics
5556

5657
# We need a more configurable way of deciding which clusters have which init functions....
5758
# See https://github.com/project-chip/connectedhomeip/issues/4369

zzz_generated/app-common/app-common/zap-generated/callback.h

-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)