Skip to content

Commit 19ae943

Browse files
yufengwangcarestyled-commitsandy31415
authored
Implement python test for TC_DGWIFI_2_2 (project-chip#37046)
* Implement python test script for TC_DGWIFI_2_2 * Restyled by clang-format * Update src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.h Co-authored-by: Andrei Litvin <andy314@gmail.com> * Use wait event instead of hard sleep --------- Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Andrei Litvin <andy314@gmail.com>
1 parent e8196ff commit 19ae943

File tree

9 files changed

+486
-47
lines changed

9 files changed

+486
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
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 <app-common/zap-generated/cluster-enums.h>
19+
#include <app/clusters/wifi-network-diagnostics-server/WiFiDiagnosticsTestEventTriggerHandler.h>
20+
#include <app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.h>
21+
#include <platform/CHIPDeviceLayer.h>
22+
#include <platform/DiagnosticDataProvider.h>
23+
24+
#include <ctime>
25+
#include <string>
26+
27+
using namespace chip;
28+
using namespace chip::app;
29+
using namespace chip::DeviceLayer;
30+
using namespace chip::app::Clusters;
31+
32+
namespace {
33+
34+
/**
35+
* @brief Helper function to simulate a Disconnection event
36+
*/
37+
void SetTestEventTrigger_Disconnection()
38+
{
39+
uint16_t reasonCode = 3; // Deauthenticated because sending STA is leaving (or has left) IBSS or ESS.
40+
41+
WiFiDiagnosticsServer::Instance().OnDisconnectionDetected(reasonCode);
42+
}
43+
44+
/**
45+
* @brief Helper function to simulate an Association Failure event
46+
*/
47+
void SetTestEventTrigger_AssociationFailure()
48+
{
49+
uint8_t associationFailureCause =
50+
static_cast<uint8_t>(WiFiNetworkDiagnostics::AssociationFailureCauseEnum::kAuthenticationFailed);
51+
uint16_t status = 4; // IEEE 802.11-2020 Status Codes, AP is unable to handle additional associated STAs
52+
53+
WiFiDiagnosticsServer::Instance().OnAssociationFailureDetected(associationFailureCause, status);
54+
}
55+
56+
/**
57+
* @brief Helper function to simulate a Connection Status event
58+
*/
59+
void SetTestEventTrigger_ConnectionStatus()
60+
{
61+
uint8_t connectionStatus = static_cast<uint8_t>(WiFiNetworkDiagnostics::ConnectionStatusEnum::kNotConnected);
62+
WiFiDiagnosticsServer::Instance().OnConnectionStatusChanged(connectionStatus);
63+
}
64+
65+
} // anonymous namespace
66+
67+
bool HandleWiFiDiagnosticsTestEventTrigger(uint64_t eventTrigger)
68+
{
69+
// Convert raw trigger to our enum
70+
WiFiDiagnosticsTrigger trigger = static_cast<WiFiDiagnosticsTrigger>(eventTrigger);
71+
72+
switch (trigger)
73+
{
74+
case WiFiDiagnosticsTrigger::kDisconnection:
75+
ChipLogProgress(Support, "[WiFiDiagnostics-Test-Event] => Disconnection triggered");
76+
SetTestEventTrigger_Disconnection();
77+
break;
78+
79+
case WiFiDiagnosticsTrigger::kAssociationFailure:
80+
ChipLogProgress(Support, "[WiFiDiagnostics-Test-Event] => AssociationFailure triggered");
81+
SetTestEventTrigger_AssociationFailure();
82+
break;
83+
84+
case WiFiDiagnosticsTrigger::kConnectionStatus:
85+
ChipLogProgress(Support, "[WiFiDiagnostics-Test-Event] => ConnectionStatus triggered");
86+
SetTestEventTrigger_ConnectionStatus();
87+
break;
88+
89+
default:
90+
// If we get here, the trigger value is unknown to this handler
91+
return false;
92+
}
93+
94+
// Indicate that we handled the trigger successfully
95+
return true;
96+
}

examples/all-clusters-app/linux/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ source_set("chip-all-clusters-common") {
6060
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/static-supported-modes-manager.cpp",
6161
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/static-supported-temperature-levels.cpp",
6262
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/tcc-mode.cpp",
63+
"${chip_root}/examples/all-clusters-app/all-clusters-common/src/wifi-diagnostics-stub.cpp",
6364
"${chip_root}/examples/all-clusters-app/linux/diagnostic-logs-provider-delegate-impl.cpp",
6465
"${chip_root}/examples/energy-management-app/energy-management-common/common/src/EnergyTimeUtils.cpp",
6566
"${chip_root}/examples/energy-management-app/energy-management-common/device-energy-management/src/DeviceEnergyManagementDelegateImpl.cpp",

examples/all-clusters-app/linux/args.gni

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ chip_enable_smoke_co_trigger = true
3131
chip_enable_boolean_state_configuration_trigger = true
3232
chip_enable_water_heater_management_trigger = true
3333
chip_enable_software_diagnostics_trigger = true
34+
chip_enable_wifi_diagnostics_trigger = true

examples/platform/linux/AppMain.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
#if CHIP_DEVICE_CONFIG_ENABLE_SOFTWARE_DIAGNOSTIC_TRIGGER
7474
#include <app/clusters/software-diagnostics-server/SoftwareDiagnosticsTestEventTriggerHandler.h>
7575
#endif
76+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_DIAGNOSTIC_TRIGGER
77+
#include <app/clusters/wifi-network-diagnostics-server/WiFiDiagnosticsTestEventTriggerHandler.h>
78+
#endif
7679
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
7780
#include <app/clusters/ota-requestor/OTATestEventTriggerHandler.h>
7881
#endif
@@ -592,6 +595,10 @@ void ChipLinuxAppMainLoop(AppMainLoopImplementation * impl)
592595
static SoftwareDiagnosticsTestEventTriggerHandler sSoftwareDiagnosticsTestEventTriggerHandler;
593596
sTestEventTriggerDelegate.AddHandler(&sSoftwareDiagnosticsTestEventTriggerHandler);
594597
#endif
598+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_DIAGNOSTIC_TRIGGER
599+
static WiFiDiagnosticsTestEventTriggerHandler sWiFiDiagnosticsTestEventTriggerHandler;
600+
sTestEventTriggerDelegate.AddHandler(&sWiFiDiagnosticsTestEventTriggerHandler);
601+
#endif
595602
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
596603
// We want to allow triggering OTA queries if OTA requestor is enabled
597604
static OTATestEventTriggerHandler sOtaTestEventTriggerHandler;

examples/platform/linux/BUILD.gn

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ if (current_os != "nuttx") {
2626

2727
declare_args() {
2828
chip_enable_software_diagnostics_trigger = false
29+
chip_enable_wifi_diagnostics_trigger = false
2930
chip_enable_smoke_co_trigger = false
3031
chip_enable_boolean_state_configuration_trigger = false
3132
chip_enable_energy_evse_trigger = false
@@ -48,6 +49,10 @@ source_set("software-diagnostics-test-event-trigger") {
4849
sources = [ "${chip_root}/src/app/clusters/software-diagnostics-server/SoftwareDiagnosticsTestEventTriggerHandler.h" ]
4950
}
5051

52+
source_set("wifi-diagnostics-test-event-trigger") {
53+
sources = [ "${chip_root}/src/app/clusters/wifi-network-diagnostics-server/WiFiDiagnosticsTestEventTriggerHandler.h" ]
54+
}
55+
5156
source_set("smco-test-event-trigger") {
5257
sources = [ "${chip_root}/src/app/clusters/smoke-co-alarm-server/SmokeCOTestEventTriggerHandler.h" ]
5358
}
@@ -100,6 +105,7 @@ source_set("app-main") {
100105
":smco-test-event-trigger",
101106
":software-diagnostics-test-event-trigger",
102107
":water-heater-management-test-event-trigger",
108+
":wifi-diagnostics-test-event-trigger",
103109
"${chip_root}/src/data-model-providers/codegen:instance-header",
104110
"${chip_root}/src/lib",
105111
"${chip_root}/src/platform/logging:default",
@@ -150,6 +156,7 @@ source_set("app-main") {
150156
"CHIP_DEVICE_CONFIG_ENABLE_ENERGY_EVSE_TRIGGER=${chip_enable_energy_evse_trigger}",
151157
"CHIP_DEVICE_CONFIG_ENABLE_ENERGY_REPORTING_TRIGGER=${chip_enable_energy_reporting_trigger}",
152158
"CHIP_DEVICE_CONFIG_ENABLE_WATER_HEATER_MANAGEMENT_TRIGGER=${chip_enable_water_heater_management_trigger}",
159+
"CHIP_DEVICE_CONFIG_ENABLE_WIFI_DIAGNOSTIC_TRIGGER=${chip_enable_wifi_diagnostics_trigger}",
153160
"CHIP_DEVICE_CONFIG_ENABLE_DEVICE_ENERGY_MANAGEMENT_TRIGGER=${chip_enable_device_energy_management_trigger}",
154161
]
155162

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
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 <app-common/zap-generated/cluster-objects.h>
21+
#include <app/TestEventTriggerDelegate.h>
22+
23+
/**
24+
* @brief User handler for handling the test event trigger
25+
*
26+
* @note If TestEventTrigger is enabled, it needs to be implemented in the app
27+
*
28+
* @param eventTrigger Event trigger to handle
29+
*
30+
* @retval true on success
31+
* @retval false if error happened
32+
*/
33+
bool HandleWiFiDiagnosticsTestEventTrigger(uint64_t eventTrigger);
34+
35+
namespace chip {
36+
37+
/*
38+
* These Test EventTrigger values are specified in the TC_DGWIFI test plan
39+
* and are defined conditions used in test events.
40+
*
41+
* They are sent along with the enableKey (manufacturer defined secret)
42+
* in the General Diagnostic cluster TestEventTrigger command
43+
*/
44+
enum class WiFiDiagnosticsTrigger : uint64_t
45+
{
46+
// Simulate a disconnection via de-authentication or dis-association.
47+
kDisconnection = 0x0036000000000000,
48+
49+
// Force a scenario where the DUT exhausts all internal retries,
50+
// and triggers an AssociationFailure event.
51+
kAssociationFailure = 0x0036000000000001,
52+
53+
// Simulate disconnecting and reconnecting the node’s Wi-Fi,
54+
// so that a ConnectionStatus event is triggered.
55+
kConnectionStatus = 0x0036000000000002,
56+
};
57+
58+
class WiFiDiagnosticsTestEventTriggerHandler : public TestEventTriggerHandler
59+
{
60+
public:
61+
explicit WiFiDiagnosticsTestEventTriggerHandler() {}
62+
63+
/** This function must return True if the eventTrigger is recognised and handled
64+
* It must return False to allow a higher level TestEvent handler to check other
65+
* clusters that may handle it.
66+
*/
67+
CHIP_ERROR HandleEventTrigger(uint64_t eventTrigger) override
68+
{
69+
if (HandleWiFiDiagnosticsTestEventTrigger(eventTrigger))
70+
{
71+
return CHIP_NO_ERROR;
72+
}
73+
return CHIP_ERROR_INVALID_ARGUMENT;
74+
}
75+
};
76+
77+
} // namespace chip

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

+58-47
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include "wifi-network-diagnostics-server.h"
1819
#include <app-common/zap-generated/attributes/Accessors.h>
1920
#include <app-common/zap-generated/cluster-objects.h>
2021
#include <app-common/zap-generated/ids/Attributes.h>
@@ -26,7 +27,6 @@
2627
#include <app/EventLogging.h>
2728
#include <app/util/attribute-storage.h>
2829
#include <lib/core/Optional.h>
29-
#include <platform/DiagnosticDataProvider.h>
3030
#include <tracing/macros.h>
3131
#include <tracing/metric_event.h>
3232

@@ -240,70 +240,81 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::Read(const ConcreteReadAttributePath & aPat
240240
return CHIP_NO_ERROR;
241241
}
242242

243-
class WiFiDiagnosticsDelegate : public DeviceLayer::WiFiDiagnosticsDelegate
243+
} // anonymous namespace
244+
245+
namespace chip {
246+
namespace app {
247+
namespace Clusters {
248+
249+
WiFiDiagnosticsServer WiFiDiagnosticsServer::instance;
250+
251+
/**********************************************************
252+
* WiFiDiagnosticsServer Implementation
253+
*********************************************************/
254+
255+
WiFiDiagnosticsServer & WiFiDiagnosticsServer::Instance()
256+
{
257+
return instance;
258+
}
259+
260+
void WiFiDiagnosticsServer::OnDisconnectionDetected(uint16_t reasonCode)
244261
{
245-
// Gets called when the Node detects Node’s Wi-Fi connection has been disconnected.
246-
void OnDisconnectionDetected(uint16_t reasonCode) override
262+
MATTER_TRACE_SCOPE("OnDisconnectionDetected", "WiFiDiagnosticsDelegate");
263+
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnDisconnectionDetected");
264+
265+
for (auto endpoint : EnabledEndpointsWithServerCluster(WiFiNetworkDiagnostics::Id))
247266
{
248-
MATTER_TRACE_SCOPE("OnDisconnectionDetected", "WiFiDiagnosticsDelegate");
249-
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnDisconnectionDetected");
267+
// If Wi-Fi Network Diagnostics cluster is implemented on this endpoint
268+
Events::Disconnection::Type event{ reasonCode };
269+
EventNumber eventNumber;
250270

251-
for (auto endpoint : EnabledEndpointsWithServerCluster(WiFiNetworkDiagnostics::Id))
271+
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
252272
{
253-
// If Wi-Fi Network Diagnostics cluster is implemented on this endpoint
254-
Events::Disconnection::Type event{ reasonCode };
255-
EventNumber eventNumber;
256-
257-
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
258-
{
259-
ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record Disconnection event");
260-
}
273+
ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record Disconnection event");
261274
}
262275
}
276+
}
263277

264-
// Gets called when the Node fails to associate or authenticate an access point.
265-
void OnAssociationFailureDetected(uint8_t associationFailureCause, uint16_t status) override
266-
{
267-
MATTER_TRACE_SCOPE("OnAssociationFailureDetected", "WiFiDiagnosticsDelegate");
268-
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnAssociationFailureDetected");
278+
void WiFiDiagnosticsServer::OnAssociationFailureDetected(uint8_t associationFailureCause, uint16_t status)
279+
{
280+
MATTER_TRACE_SCOPE("OnAssociationFailureDetected", "WiFiDiagnosticsDelegate");
281+
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnAssociationFailureDetected");
269282

270-
Events::AssociationFailure::Type event{ static_cast<AssociationFailureCauseEnum>(associationFailureCause), status };
283+
Events::AssociationFailure::Type event{ static_cast<AssociationFailureCauseEnum>(associationFailureCause), status };
271284

272-
for (auto endpoint : EnabledEndpointsWithServerCluster(WiFiNetworkDiagnostics::Id))
273-
{
274-
// If Wi-Fi Network Diagnostics cluster is implemented on this endpoint
275-
EventNumber eventNumber;
285+
for (auto endpoint : EnabledEndpointsWithServerCluster(WiFiNetworkDiagnostics::Id))
286+
{
287+
// If Wi-Fi Network Diagnostics cluster is implemented on this endpoint
288+
EventNumber eventNumber;
276289

277-
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
278-
{
279-
ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record AssociationFailure event");
280-
}
290+
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
291+
{
292+
ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record AssociationFailure event");
281293
}
282294
}
295+
}
296+
297+
void WiFiDiagnosticsServer::OnConnectionStatusChanged(uint8_t connectionStatus)
298+
{
299+
MATTER_TRACE_SCOPE("OnConnectionStatusChanged", "WiFiDiagnosticsDelegate");
300+
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnConnectionStatusChanged");
283301

284-
// Gets when the Node’s connection status to a Wi-Fi network has changed.
285-
void OnConnectionStatusChanged(uint8_t connectionStatus) override
302+
Events::ConnectionStatus::Type event{ static_cast<ConnectionStatusEnum>(connectionStatus) };
303+
for (auto endpoint : EnabledEndpointsWithServerCluster(WiFiNetworkDiagnostics::Id))
286304
{
287-
MATTER_TRACE_SCOPE("OnConnectionStatusChanged", "WiFiDiagnosticsDelegate");
288-
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnConnectionStatusChanged");
305+
// If Wi-Fi Network Diagnostics cluster is implemented on this endpoint
306+
EventNumber eventNumber;
289307

290-
Events::ConnectionStatus::Type event{ static_cast<ConnectionStatusEnum>(connectionStatus) };
291-
for (auto endpoint : EnabledEndpointsWithServerCluster(WiFiNetworkDiagnostics::Id))
308+
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
292309
{
293-
// If Wi-Fi Network Diagnostics cluster is implemented on this endpoint
294-
EventNumber eventNumber;
295-
296-
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
297-
{
298-
ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record ConnectionStatus event");
299-
}
310+
ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record ConnectionStatus event");
300311
}
301312
}
302-
};
303-
304-
WiFiDiagnosticsDelegate gDiagnosticDelegate;
313+
}
305314

306-
} // anonymous namespace
315+
} // namespace Clusters
316+
} // namespace app
317+
} // namespace chip
307318

308319
bool emberAfWiFiNetworkDiagnosticsClusterResetCountsCallback(app::CommandHandler * commandObj,
309320
const app::ConcreteCommandPath & commandPath,
@@ -318,5 +329,5 @@ bool emberAfWiFiNetworkDiagnosticsClusterResetCountsCallback(app::CommandHandler
318329
void MatterWiFiNetworkDiagnosticsPluginServerInitCallback()
319330
{
320331
AttributeAccessInterfaceRegistry::Instance().Register(&gAttrAccess);
321-
GetDiagnosticDataProvider().SetWiFiDiagnosticsDelegate(&gDiagnosticDelegate);
332+
GetDiagnosticDataProvider().SetWiFiDiagnosticsDelegate(&WiFiDiagnosticsServer::Instance());
322333
}

0 commit comments

Comments
 (0)