forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi-network-diagnostics-server.cpp
361 lines (310 loc) · 12.8 KB
/
wifi-network-diagnostics-server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wifi-network-diagnostics-server.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/AttributeAccessInterface.h>
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/CommandHandler.h>
#include <app/CommandHandlerInterface.h>
#include <app/CommandHandlerInterfaceRegistry.h>
#include <app/ConcreteCommandPath.h>
#include <app/EventLogging.h>
#include <app/util/attribute-storage.h>
#include <lib/core/Optional.h>
#include <tracing/macros.h>
#include <tracing/metric_event.h>
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::WiFiNetworkDiagnostics;
using namespace chip::app::Clusters::WiFiNetworkDiagnostics::Attributes;
using chip::DeviceLayer::DiagnosticDataProvider;
using chip::DeviceLayer::GetDiagnosticDataProvider;
namespace {
class WiFiDiagnosticsGlobalInstance : public AttributeAccessInterface, public CommandHandlerInterface
{
public:
// Register for the WiFiNetworkDiagnostics cluster on all endpoints.
WiFiDiagnosticsGlobalInstance(DiagnosticDataProvider & diagnosticProvider) :
AttributeAccessInterface(Optional<EndpointId>::Missing(), WiFiNetworkDiagnostics::Id),
CommandHandlerInterface(Optional<EndpointId>::Missing(), WiFiNetworkDiagnostics::Id),
mDiagnosticProvider(diagnosticProvider)
{}
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
private:
template <typename T, typename Type>
CHIP_ERROR ReadIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(T &), Type & data, AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadWiFiBssId(AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadSecurityType(AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadWiFiVersion(AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadChannelNumber(AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadWiFiRssi(AttributeValueEncoder & aEncoder);
void InvokeCommand(HandlerContext & ctx) override;
#ifdef WI_FI_NETWORK_DIAGNOSTICS_ENABLE_RESET_COUNTS_CMD
void HandleResetCounts(HandlerContext & ctx, const Commands::ResetCounts::DecodableType & commandData);
#endif
DiagnosticDataProvider & mDiagnosticProvider;
};
template <typename T, typename Type>
CHIP_ERROR WiFiDiagnosticsGlobalInstance::ReadIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(T &), Type & data,
AttributeValueEncoder & aEncoder)
{
T value;
CHIP_ERROR err = (mDiagnosticProvider.*getter)(value);
if (err == CHIP_NO_ERROR)
{
data.SetNonNull(value);
}
else
{
ChipLogProgress(Zcl, "The WiFi interface is not currently configured or operational.");
}
return aEncoder.Encode(data);
}
CHIP_ERROR WiFiDiagnosticsGlobalInstance::ReadWiFiBssId(AttributeValueEncoder & aEncoder)
{
Attributes::Bssid::TypeInfo::Type bssid;
uint8_t bssidBytes[chip::DeviceLayer::kMaxHardwareAddrSize];
MutableByteSpan bssidSpan(bssidBytes);
if (mDiagnosticProvider.GetWiFiBssId(bssidSpan) == CHIP_NO_ERROR)
{
if (!bssidSpan.empty())
{
bssid.SetNonNull(bssidSpan);
ChipLogProgress(Zcl, "Node is currently connected to Wi-Fi network with BSSID:");
ChipLogByteSpan(Zcl, bssidSpan);
}
}
else
{
ChipLogProgress(Zcl, "The WiFi interface is not currently connected.");
}
return aEncoder.Encode(bssid);
}
CHIP_ERROR WiFiDiagnosticsGlobalInstance::ReadSecurityType(AttributeValueEncoder & aEncoder)
{
Attributes::SecurityType::TypeInfo::Type securityType;
SecurityTypeEnum value = SecurityTypeEnum::kUnspecified;
if (mDiagnosticProvider.GetWiFiSecurityType(value) == CHIP_NO_ERROR)
{
securityType.SetNonNull(value);
ChipLogProgress(Zcl, "The current type of Wi-Fi security used: %d", to_underlying(value));
}
else
{
ChipLogProgress(Zcl, "The WiFi interface is not currently configured or operational.");
}
return aEncoder.Encode(securityType);
}
CHIP_ERROR WiFiDiagnosticsGlobalInstance::ReadWiFiVersion(AttributeValueEncoder & aEncoder)
{
Attributes::WiFiVersion::TypeInfo::Type version;
WiFiVersionEnum value = WiFiVersionEnum::kUnknownEnumValue;
if (mDiagnosticProvider.GetWiFiVersion(value) == CHIP_NO_ERROR)
{
version.SetNonNull(value);
ChipLogProgress(Zcl, "The current 802.11 standard version in use by the Node: %d", to_underlying(value));
}
else
{
ChipLogProgress(Zcl, "The current 802.11 standard version in use by the Node is not available");
}
return aEncoder.Encode(version);
}
CHIP_ERROR WiFiDiagnosticsGlobalInstance::ReadChannelNumber(AttributeValueEncoder & aEncoder)
{
Attributes::ChannelNumber::TypeInfo::Type channelNumber;
uint16_t value = 0;
if (mDiagnosticProvider.GetWiFiChannelNumber(value) == CHIP_NO_ERROR)
{
channelNumber.SetNonNull(value);
ChipLogProgress(Zcl, "The channel that Wi-Fi communication is currently operating on is: %d", value);
}
else
{
ChipLogProgress(Zcl, "The WiFi interface is not currently configured or operational.");
}
return aEncoder.Encode(channelNumber);
}
CHIP_ERROR WiFiDiagnosticsGlobalInstance::ReadWiFiRssi(AttributeValueEncoder & aEncoder)
{
Attributes::Rssi::TypeInfo::Type rssi;
int8_t value = 0;
if (mDiagnosticProvider.GetWiFiRssi(value) == CHIP_NO_ERROR)
{
rssi.SetNonNull(value);
ChipLogProgress(Zcl, "The current RSSI of the Node’s Wi-Fi radio in dB: %d", value);
MATTER_LOG_METRIC(chip::Tracing::kMetricWiFiRSSI, value);
}
else
{
ChipLogProgress(Zcl, "The WiFi interface is not currently configured or operational.");
}
return aEncoder.Encode(rssi);
}
CHIP_ERROR WiFiDiagnosticsGlobalInstance::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
if (aPath.mClusterId != WiFiNetworkDiagnostics::Id)
{
// We shouldn't have been called at all.
return CHIP_ERROR_INVALID_ARGUMENT;
}
switch (aPath.mAttributeId)
{
case Bssid::Id: {
return ReadWiFiBssId(aEncoder);
}
case Attributes::SecurityType::Id: {
return ReadSecurityType(aEncoder);
}
case WiFiVersion::Id: {
return ReadWiFiVersion(aEncoder);
}
case ChannelNumber::Id: {
return ReadChannelNumber(aEncoder);
}
case Rssi::Id: {
return ReadWiFiRssi(aEncoder);
}
case BeaconLostCount::Id: {
Attributes::BeaconLostCount::TypeInfo::Type count;
return ReadIfSupported(&DiagnosticDataProvider::GetWiFiBeaconLostCount, count, aEncoder);
}
case BeaconRxCount::Id: {
Attributes::BeaconRxCount::TypeInfo::Type count;
return ReadIfSupported(&DiagnosticDataProvider::GetWiFiBeaconRxCount, count, aEncoder);
}
case PacketMulticastRxCount::Id: {
Attributes::PacketMulticastRxCount::TypeInfo::Type count;
return ReadIfSupported(&DiagnosticDataProvider::GetWiFiPacketMulticastRxCount, count, aEncoder);
}
case PacketMulticastTxCount::Id: {
Attributes::PacketMulticastTxCount::TypeInfo::Type count;
return ReadIfSupported(&DiagnosticDataProvider::GetWiFiPacketMulticastTxCount, count, aEncoder);
}
case PacketUnicastRxCount::Id: {
Attributes::PacketUnicastRxCount::TypeInfo::Type count;
return ReadIfSupported(&DiagnosticDataProvider::GetWiFiPacketUnicastRxCount, count, aEncoder);
}
case PacketUnicastTxCount::Id: {
Attributes::PacketUnicastTxCount::TypeInfo::Type count;
return ReadIfSupported(&DiagnosticDataProvider::GetWiFiPacketUnicastTxCount, count, aEncoder);
}
case CurrentMaxRate::Id: {
Attributes::CurrentMaxRate::TypeInfo::Type rate;
return ReadIfSupported(&DiagnosticDataProvider::GetWiFiCurrentMaxRate, rate, aEncoder);
}
case OverrunCount::Id: {
Attributes::OverrunCount::TypeInfo::Type count;
return ReadIfSupported(&DiagnosticDataProvider::GetWiFiOverrunCount, count, aEncoder);
}
default: {
break;
}
}
return CHIP_NO_ERROR;
}
void WiFiDiagnosticsGlobalInstance::InvokeCommand(HandlerContext & handlerContext)
{
switch (handlerContext.mRequestPath.mCommandId)
{
#ifdef WI_FI_NETWORK_DIAGNOSTICS_ENABLE_RESET_COUNTS_CMD
case Commands::ResetCounts::Id:
CommandHandlerInterface::HandleCommand<Commands::ResetCounts::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleResetCounts(ctx, commandData); });
break;
#endif
}
}
#ifdef WI_FI_NETWORK_DIAGNOSTICS_ENABLE_RESET_COUNTS_CMD
void WiFiDiagnosticsGlobalInstance::HandleResetCounts(HandlerContext & ctx,
const Commands::ResetCounts::DecodableType & commandData)
{
mDiagnosticProvider.ResetWiFiNetworkDiagnosticsCounts();
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Protocols::InteractionModel::Status::Success);
}
#endif
WiFiDiagnosticsGlobalInstance gWiFiDiagnosticsInstance(DeviceLayer::GetDiagnosticDataProvider());
} // anonymous namespace
namespace chip {
namespace app {
namespace Clusters {
WiFiDiagnosticsServer WiFiDiagnosticsServer::instance;
/**********************************************************
* WiFiDiagnosticsServer Implementation
*********************************************************/
WiFiDiagnosticsServer & WiFiDiagnosticsServer::Instance()
{
return instance;
}
void WiFiDiagnosticsServer::OnDisconnectionDetected(uint16_t reasonCode)
{
MATTER_TRACE_SCOPE("OnDisconnectionDetected", "WiFiDiagnosticsDelegate");
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnDisconnectionDetected");
for (auto endpoint : EnabledEndpointsWithServerCluster(WiFiNetworkDiagnostics::Id))
{
// If Wi-Fi Network Diagnostics cluster is implemented on this endpoint
Events::Disconnection::Type event{ reasonCode };
EventNumber eventNumber;
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
{
ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record Disconnection event");
}
}
}
void WiFiDiagnosticsServer::OnAssociationFailureDetected(uint8_t associationFailureCause, uint16_t status)
{
MATTER_TRACE_SCOPE("OnAssociationFailureDetected", "WiFiDiagnosticsDelegate");
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnAssociationFailureDetected");
Events::AssociationFailure::Type event{ static_cast<AssociationFailureCauseEnum>(associationFailureCause), status };
for (auto endpoint : EnabledEndpointsWithServerCluster(WiFiNetworkDiagnostics::Id))
{
// If Wi-Fi Network Diagnostics cluster is implemented on this endpoint
EventNumber eventNumber;
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
{
ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record AssociationFailure event");
}
}
}
void WiFiDiagnosticsServer::OnConnectionStatusChanged(uint8_t connectionStatus)
{
MATTER_TRACE_SCOPE("OnConnectionStatusChanged", "WiFiDiagnosticsDelegate");
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnConnectionStatusChanged");
Events::ConnectionStatus::Type event{ static_cast<ConnectionStatusEnum>(connectionStatus) };
for (auto endpoint : EnabledEndpointsWithServerCluster(WiFiNetworkDiagnostics::Id))
{
// If Wi-Fi Network Diagnostics cluster is implemented on this endpoint
EventNumber eventNumber;
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
{
ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record ConnectionStatus event");
}
}
}
} // namespace Clusters
} // namespace app
} // namespace chip
void MatterWiFiNetworkDiagnosticsPluginServerInitCallback()
{
AttributeAccessInterfaceRegistry::Instance().Register(&gWiFiDiagnosticsInstance);
CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(&gWiFiDiagnosticsInstance);
GetDiagnosticDataProvider().SetWiFiDiagnosticsDelegate(&WiFiDiagnosticsServer::Instance());
}