25
25
#include < crypto/RandUtils.h>
26
26
#include < lib/support/logging/CHIPLogging.h>
27
27
#include < platform/CHIPDeviceLayer.h>
28
+ #include < platform/DiagnosticDataProvider.h>
28
29
#include < platform/Zephyr/InetUtils.h>
29
30
30
31
#include < zephyr/kernel.h>
@@ -151,7 +152,7 @@ void WiFiManager::WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mg
151
152
Platform::UniquePtr<uint8_t > eventData (new uint8_t [cb->info_length ]);
152
153
VerifyOrReturn (eventData);
153
154
memcpy (eventData.get (), cb->info , cb->info_length );
154
- sEventHandlerMap [mgmtEvent](std::move (eventData));
155
+ sEventHandlerMap [mgmtEvent](std::move (eventData), cb-> info_length );
155
156
}
156
157
}
157
158
@@ -292,8 +293,11 @@ CHIP_ERROR WiFiManager::GetNetworkStatistics(NetworkStatistics & stats) const
292
293
return CHIP_NO_ERROR;
293
294
}
294
295
295
- void WiFiManager::ScanResultHandler (Platform::UniquePtr<uint8_t > data)
296
+ void WiFiManager::ScanResultHandler (Platform::UniquePtr<uint8_t > data, size_t length )
296
297
{
298
+ // Validate that input data size matches the expected one.
299
+ VerifyOrReturn (length == sizeof (wifi_scan_result));
300
+
297
301
// Contrary to other handlers, offload accumulating of the scan results from the CHIP thread to the caller's thread
298
302
const wifi_scan_result * scanResult = reinterpret_cast <const wifi_scan_result *>(data.get ());
299
303
@@ -337,8 +341,11 @@ void WiFiManager::ScanResultHandler(Platform::UniquePtr<uint8_t> data)
337
341
}
338
342
}
339
343
340
- void WiFiManager::ScanDoneHandler (Platform::UniquePtr<uint8_t > data)
344
+ void WiFiManager::ScanDoneHandler (Platform::UniquePtr<uint8_t > data, size_t length )
341
345
{
346
+ // Validate that input data size matches the expected one.
347
+ VerifyOrReturn (length == sizeof (wifi_status));
348
+
342
349
CHIP_ERROR err = SystemLayer ().ScheduleLambda ([capturedData = data.get ()] {
343
350
Platform::UniquePtr<uint8_t > safePtr (capturedData);
344
351
uint8_t * rawData = safePtr.get ();
@@ -416,8 +423,13 @@ void WiFiManager::SendRouterSolicitation(System::Layer * layer, void * param)
416
423
}
417
424
}
418
425
419
- void WiFiManager::ConnectHandler (Platform::UniquePtr<uint8_t > data)
426
+ void WiFiManager::ConnectHandler (Platform::UniquePtr<uint8_t > data, size_t length )
420
427
{
428
+ using app::Clusters::WiFiNetworkDiagnostics::AssociationFailureCauseEnum;
429
+
430
+ // Validate that input data size matches the expected one.
431
+ VerifyOrReturn (length == sizeof (wifi_status));
432
+
421
433
CHIP_ERROR err = SystemLayer ().ScheduleLambda ([capturedData = data.get ()] {
422
434
Platform::UniquePtr<uint8_t > safePtr (capturedData);
423
435
uint8_t * rawData = safePtr.get ();
@@ -432,6 +444,32 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr<uint8_t> data)
432
444
{
433
445
Instance ().mHandling .mOnConnectionDone (connStatus);
434
446
}
447
+
448
+ WiFiDiagnosticsDelegate * delegate = GetDiagnosticDataProvider ().GetWiFiDiagnosticsDelegate ();
449
+ if (delegate)
450
+ {
451
+ uint16_t reason = Instance ().GetLastDisconnectReason ();
452
+ uint8_t associationFailureCause;
453
+
454
+ switch (connStatus)
455
+ {
456
+ case WIFI_STATUS_CONN_WRONG_PASSWORD:
457
+ associationFailureCause = to_underlying (AssociationFailureCauseEnum::kAuthenticationFailed );
458
+ break ;
459
+ case WIFI_STATUS_CONN_FAIL:
460
+ case WIFI_STATUS_CONN_TIMEOUT:
461
+ associationFailureCause = to_underlying (AssociationFailureCauseEnum::kAssociationFailed );
462
+ break ;
463
+ case WIFI_STATUS_CONN_AP_NOT_FOUND:
464
+ associationFailureCause = to_underlying (AssociationFailureCauseEnum::kSsidNotFound );
465
+ break ;
466
+ default :
467
+ associationFailureCause = to_underlying (AssociationFailureCauseEnum::kUnknown );
468
+ break ;
469
+ }
470
+
471
+ delegate->OnAssociationFailureDetected (associationFailureCause, reason);
472
+ }
435
473
}
436
474
else // The connection has been established successfully.
437
475
{
@@ -457,6 +495,13 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr<uint8_t> data)
457
495
{
458
496
ChipLogError (DeviceLayer, " Cannot post event [error: %s]" , ErrorStr (error));
459
497
}
498
+
499
+ WiFiDiagnosticsDelegate * delegate = GetDiagnosticDataProvider ().GetWiFiDiagnosticsDelegate ();
500
+ if (delegate)
501
+ {
502
+ delegate->OnConnectionStatusChanged (
503
+ to_underlying (app::Clusters::WiFiNetworkDiagnostics::ConnectionStatusEnum::kConnected ));
504
+ }
460
505
}
461
506
// cleanup the provisioning data as it is configured per each connect request
462
507
Instance ().ClearStationProvisioningData ();
@@ -469,13 +514,55 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr<uint8_t> data)
469
514
}
470
515
}
471
516
472
- void WiFiManager::DisconnectHandler (Platform::UniquePtr<uint8_t >)
517
+ void WiFiManager::DisconnectHandler (Platform::UniquePtr<uint8_t > data, size_t length )
473
518
{
474
- SystemLayer ().ScheduleLambda ([] {
519
+ // Validate that input data size matches the expected one.
520
+ VerifyOrReturn (length == sizeof (wifi_status));
521
+
522
+ CHIP_ERROR err = SystemLayer ().ScheduleLambda ([capturedData = data.get ()] {
523
+ Platform::UniquePtr<uint8_t > safePtr (capturedData);
524
+ uint8_t * rawData = safePtr.get ();
525
+ const wifi_status * status = reinterpret_cast <const wifi_status *>(rawData);
526
+ uint16_t reason;
527
+
528
+ switch (status->disconn_reason )
529
+ {
530
+ case WIFI_REASON_DISCONN_UNSPECIFIED:
531
+ reason = WLAN_REASON_UNSPECIFIED;
532
+ break ;
533
+ case WIFI_REASON_DISCONN_USER_REQUEST:
534
+ reason = WLAN_REASON_DEAUTH_LEAVING;
535
+ break ;
536
+ case WIFI_REASON_DISCONN_AP_LEAVING:
537
+ reason = WLAN_REASON_DEAUTH_LEAVING;
538
+ break ;
539
+ case WIFI_REASON_DISCONN_INACTIVITY:
540
+ reason = WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY;
541
+ break ;
542
+ default :
543
+ reason = WLAN_REASON_UNSPECIFIED;
544
+ break ;
545
+ }
546
+ Instance ().SetLastDisconnectReason (reason);
547
+
475
548
ChipLogProgress (DeviceLayer, " WiFi station disconnected" );
476
549
Instance ().mWiFiState = WIFI_STATE_DISCONNECTED;
477
550
Instance ().PostConnectivityStatusChange (kConnectivity_Lost );
551
+
552
+ WiFiDiagnosticsDelegate * delegate = GetDiagnosticDataProvider ().GetWiFiDiagnosticsDelegate ();
553
+ if (delegate)
554
+ {
555
+ delegate->OnConnectionStatusChanged (
556
+ to_underlying (app::Clusters::WiFiNetworkDiagnostics::ConnectionStatusEnum::kNotConnected ));
557
+ delegate->OnDisconnectionDetected (reason);
558
+ }
478
559
});
560
+
561
+ if (CHIP_NO_ERROR == err)
562
+ {
563
+ // the ownership has been transferred to the worker thread - release the buffer
564
+ data.release ();
565
+ }
479
566
}
480
567
481
568
void WiFiManager::IPv6AddressChangeHandler (const void * data)
@@ -586,5 +673,15 @@ CHIP_ERROR WiFiManager::SetLowPowerMode(bool onoff)
586
673
return CHIP_NO_ERROR;
587
674
}
588
675
676
+ void WiFiManager::SetLastDisconnectReason (uint16_t reason)
677
+ {
678
+ mLastDisconnectedReason = reason;
679
+ }
680
+
681
+ uint16_t WiFiManager::GetLastDisconnectReason ()
682
+ {
683
+ return mLastDisconnectedReason ;
684
+ }
685
+
589
686
} // namespace DeviceLayer
590
687
} // namespace chip
0 commit comments