@@ -100,11 +100,16 @@ bool ps_requirement_added = false;
100
100
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
101
101
102
102
bool hasNotifiedWifiConnectivity = false ;
103
+ bool hasNotifiedIPV6 = false ;
104
+ #if (CHIP_DEVICE_CONFIG_ENABLE_IPV4)
105
+ bool hasNotifiedIPV4 = false ;
106
+ #endif /* CHIP_DEVICE_CONFIG_ENABLE_IPV4 */
103
107
104
108
wfx_wifi_scan_ext_t temp_reset;
105
109
106
110
osSemaphoreId_t sScanCompleteSemaphore ;
107
111
osSemaphoreId_t sScanInProgressSemaphore ;
112
+ osTimerId_t sDHCPTimer ;
108
113
osMessageQueueId_t sWifiEventQueue = nullptr ;
109
114
110
115
sl_net_wifi_lwip_context_t wifi_client_context;
@@ -203,6 +208,27 @@ constexpr uint8_t kWfxQueueSize = 10;
203
208
// TODO: Figure out why we actually need this, we are already handling failure and retries somewhere else.
204
209
constexpr uint16_t kWifiScanTimeoutTicks = 10000 ;
205
210
211
+ void DHCPTimerEventHandler (void * arg)
212
+ {
213
+ WifiEvent event = WifiEvent::kStationDhcpPoll ;
214
+ sl_matter_wifi_post_event (event);
215
+ }
216
+
217
+ void CancelDHCPTimer (void )
218
+ {
219
+ VerifyOrReturn (osTimerIsRunning (sDHCPTimer ), ChipLogDetail (DeviceLayer, " CancelDHCPTimer: timer not running" ));
220
+ VerifyOrReturn (osTimerStop (sDHCPTimer ) == osOK, ChipLogError (DeviceLayer, " CancelDHCPTimer: failed to stop timer" ));
221
+ }
222
+
223
+ void StartDHCPTimer (uint32_t timeout)
224
+ {
225
+ // Cancel timer if already started
226
+ CancelDHCPTimer ();
227
+
228
+ VerifyOrReturn (osTimerStart (sDHCPTimer , pdMS_TO_TICKS (timeout)) == osOK,
229
+ ChipLogError (DeviceLayer, " StartDHCPTimer: failed to start timer" ));
230
+ }
231
+
206
232
sl_status_t sl_wifi_siwx917_init (void )
207
233
{
208
234
sl_status_t status = SL_STATUS_OK;
@@ -331,10 +357,7 @@ sl_status_t SetWifiConfigurations()
331
357
VerifyOrReturnError (status == SL_STATUS_OK, status,
332
358
ChipLogError (DeviceLayer, " sl_wifi_set_listen_interval failed: 0x%lx" , status));
333
359
334
- // This is be triggered on the disconnect use case, providing the amount of TA tries
335
- // Setting the TA retry to 1 and giving the control to the M4 for improved power efficiency
336
- // When max_retry_attempts is set to 0, TA will retry indefinitely.
337
- sl_wifi_advanced_client_configuration_t client_config = { .max_retry_attempts = 1 };
360
+ sl_wifi_advanced_client_configuration_t client_config = { .max_retry_attempts = 5 };
338
361
status = sl_wifi_set_advanced_client_configuration (SL_WIFI_CLIENT_INTERFACE, &client_config);
339
362
VerifyOrReturnError (status == SL_STATUS_OK, status,
340
363
ChipLogError (DeviceLayer, " sl_wifi_set_advanced_client_configuration failed: 0x%lx" , status));
@@ -491,6 +514,11 @@ sl_status_t sl_matter_wifi_platform_init(void)
491
514
sWifiEventQueue = osMessageQueueNew (kWfxQueueSize , sizeof (WifiEvent), nullptr );
492
515
VerifyOrReturnError (sWifiEventQueue != nullptr , SL_STATUS_ALLOCATION_FAILED);
493
516
517
+ // Create timer for DHCP polling
518
+ // TODO: Use LWIP timer instead of creating a new one here
519
+ sDHCPTimer = osTimerNew (DHCPTimerEventHandler, osTimerPeriodic, nullptr , nullptr );
520
+ VerifyOrReturnError (sDHCPTimer != nullptr , SL_STATUS_ALLOCATION_FAILED);
521
+
494
522
return status;
495
523
}
496
524
@@ -634,29 +662,53 @@ sl_status_t bg_scan_callback_handler(sl_wifi_event_t event, sl_wifi_scan_result_
634
662
635
663
// / NotifyConnectivity
636
664
// / @brief Notify the application about the connectivity status if it has not been notified yet.
665
+ // / Helper function for HandleDHCPPolling.
637
666
void NotifyConnectivity (void )
638
667
{
639
668
VerifyOrReturn (!hasNotifiedWifiConnectivity);
640
669
wfx_connected_notify (CONNECTION_STATUS_SUCCESS, &wfx_rsi.ap_mac );
641
670
hasNotifiedWifiConnectivity = true ;
642
671
}
643
672
644
- // / NotifySuccessfulConnection
645
- // / @brief Processing function responsible for notifying the upper layers of a succesful connection attempt.
646
- void NotifySuccessfulConnection (void )
673
+ void HandleDHCPPolling (void )
647
674
{
675
+ WifiEvent event;
676
+
677
+ // TODO: Notify the application that the interface is not set up or Chipdie here because we are in an unkonwn state
648
678
struct netif * sta_netif = &wifi_client_context.netif ;
649
679
VerifyOrReturn (sta_netif != nullptr , ChipLogError (DeviceLayer, " HandleDHCPPolling: failed to get STA netif" ));
680
+
650
681
#if (CHIP_DEVICE_CONFIG_ENABLE_IPV4)
651
- wfx_dhcp_got_ipv4 ((uint32_t ) sta_netif->ip_addr .u_addr .ip4 .addr );
682
+ uint8_t dhcp_state = dhcpclient_poll (sta_netif);
683
+ if (dhcp_state == DHCP_ADDRESS_ASSIGNED && !hasNotifiedIPV4)
684
+ {
685
+ wfx_dhcp_got_ipv4 ((uint32_t ) sta_netif->ip_addr .u_addr .ip4 .addr );
686
+ hasNotifiedIPV4 = true ;
687
+ event = WifiEvent::kStationDhcpDone ;
688
+ sl_matter_wifi_post_event (event);
689
+ NotifyConnectivity ();
690
+ }
691
+ else if (dhcp_state == DHCP_OFF)
692
+ {
693
+ wfx_ip_changed_notify (IP_STATUS_FAIL);
694
+ hasNotifiedIPV4 = false ;
695
+ }
652
696
#endif /* CHIP_DEVICE_CONFIG_ENABLE_IPV4 */
653
- char addrStr[chip::Inet::IPAddress::kMaxStringLength ] = { 0 };
654
- VerifyOrReturn (ip6addr_ntoa_r (netif_ip6_addr (sta_netif, 0 ), addrStr, sizeof (addrStr)) != nullptr );
655
- ChipLogProgress (DeviceLayer, " SLAAC OK: linklocal addr: %s" , addrStr);
656
- wfx_ipv6_notify (GET_IPV6_SUCCESS);
657
- NotifyConnectivity ();
697
+ /* Checks if the assigned IPv6 address is preferred by evaluating
698
+ * the first block of IPv6 address ( block 0)
699
+ */
700
+ if ((ip6_addr_ispreferred (netif_ip6_addr_state (sta_netif, 0 ))) && !hasNotifiedIPV6)
701
+ {
702
+ char addrStr[chip::Inet::IPAddress::kMaxStringLength ] = { 0 };
703
+ VerifyOrReturn (ip6addr_ntoa_r (netif_ip6_addr (sta_netif, 0 ), addrStr, sizeof (addrStr)) != nullptr );
704
+ ChipLogProgress (DeviceLayer, " SLAAC OK: linklocal addr: %s" , addrStr);
705
+ wfx_ipv6_notify (GET_IPV6_SUCCESS);
706
+ hasNotifiedIPV6 = true ;
707
+ event = WifiEvent::kStationDhcpDone ;
708
+ sl_matter_wifi_post_event (event);
709
+ NotifyConnectivity ();
710
+ }
658
711
}
659
-
660
712
void sl_matter_wifi_post_event (WifiEvent event)
661
713
{
662
714
sl_status_t status = osMessageQueuePut (sWifiEventQueue , &event, 0 , 0 );
@@ -668,14 +720,19 @@ void sl_matter_wifi_post_event(WifiEvent event)
668
720
// Chipdie, etc.
669
721
}
670
722
}
671
- // / ResetConnectivityNotificationFlags
723
+ // / ResetDHCPNotificationFlags
672
724
// / @brief Reset the flags that are used to notify the application about DHCP connectivity
673
- // / and emits a WifiEvent::kConnectionComplete event to trigger DHCP polling checks. Helper function for ProcessEvent.
674
- void ResetConnectivityNotificationFlags (void )
725
+ // / and emits a WifiEvent::kStationDoDhcp event to trigger DHCP polling checks. Helper function for ProcessEvent.
726
+ void ResetDHCPNotificationFlags (void )
675
727
{
728
+
729
+ #if (CHIP_DEVICE_CONFIG_ENABLE_IPV4)
730
+ hasNotifiedIPV4 = false ;
731
+ #endif // CHIP_DEVICE_CONFIG_ENABLE_IPV4
732
+ hasNotifiedIPV6 = false ;
676
733
hasNotifiedWifiConnectivity = false ;
677
734
678
- WifiEvent event = WifiEvent::kConnectionComplete ;
735
+ WifiEvent event = WifiEvent::kStationDoDhcp ;
679
736
sl_matter_wifi_post_event (event);
680
737
}
681
738
@@ -687,7 +744,7 @@ void ProcessEvent(WifiEvent event)
687
744
case WifiEvent::kStationConnect :
688
745
ChipLogDetail (DeviceLayer, " WifiEvent::kStationConnect" );
689
746
wfx_rsi.dev_state .Set (WifiState::kStationConnected );
690
- ResetConnectivityNotificationFlags ();
747
+ ResetDHCPNotificationFlags ();
691
748
break ;
692
749
693
750
case WifiEvent::kStationDisconnect : {
@@ -700,7 +757,7 @@ void ProcessEvent(WifiEvent event)
700
757
.Clear (WifiState::kStationDhcpDone );
701
758
702
759
/* TODO: Implement disconnect notify */
703
- ResetConnectivityNotificationFlags ();
760
+ ResetDHCPNotificationFlags ();
704
761
#if (CHIP_DEVICE_CONFIG_ENABLE_IPV4)
705
762
wfx_ip_changed_notify (0 ); // for IPV4
706
763
wfx_ip_changed_notify (IP_STATUS_FAIL);
@@ -777,9 +834,20 @@ void ProcessEvent(WifiEvent event)
777
834
JoinWifiNetwork ();
778
835
break ;
779
836
780
- case WifiEvent::kConnectionComplete :
781
- ChipLogDetail (DeviceLayer, " WifiPlatformEvent::kConnectionComplete" );
782
- NotifySuccessfulConnection ();
837
+ case WifiEvent::kStationDoDhcp :
838
+ ChipLogDetail (DeviceLayer, " WifiEvent::kStationDoDhcp" );
839
+ StartDHCPTimer (WFX_RSI_DHCP_POLL_INTERVAL);
840
+ break ;
841
+
842
+ case WifiEvent::kStationDhcpDone :
843
+ ChipLogDetail (DeviceLayer, " WifiEvent::kStationDhcpDone" );
844
+ CancelDHCPTimer ();
845
+ break ;
846
+
847
+ case WifiEvent::kStationDhcpPoll :
848
+ ChipLogDetail (DeviceLayer, " WifiEvent::kStationDhcpPoll" );
849
+ HandleDHCPPolling ();
850
+ break ;
783
851
784
852
default :
785
853
break ;
0 commit comments