[Silabs] Wifi Scan Semaphore (#32538)

* Added a timer and mutex to replace the osThreadYield loops

* Applied suggesitons, changed comment style and added release to prevent blocking on next start Timer
Co-authored-by: mkardous-silabs <84793247+mkardous-silabs@users.noreply.github.com>

* Using semaphore only

* Moved init log, changed semaphore create failure status
diff --git a/examples/platform/silabs/MatterConfig.cpp b/examples/platform/silabs/MatterConfig.cpp
index 1c148a5..dd2368f 100644
--- a/examples/platform/silabs/MatterConfig.cpp
+++ b/examples/platform/silabs/MatterConfig.cpp
@@ -293,6 +293,7 @@
     sl_status_t status;
     if ((status = wfx_wifi_rsi_init()) != SL_STATUS_OK)
     {
+        SILABS_LOG("wfx_wifi_rsi_init failed with status: %x", status);
         ReturnErrorOnFailure((CHIP_ERROR) status);
     }
 #endif // SLI_SI91X_MCU_INTERFACE
diff --git a/examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.c b/examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.c
index 079f12f..79ead87 100644
--- a/examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.c
+++ b/examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.c
@@ -98,6 +98,9 @@
 
 volatile sl_status_t callback_status = SL_STATUS_OK;
 
+// Scan semaphore
+static osSemaphoreId_t sScanSemaphore;
+
 /******************************************************************
  * @fn   int32_t wfx_rsi_get_ap_info(wfx_wifi_scan_result_t *ap)
  * @brief
@@ -305,8 +308,16 @@
     status = sl_wifi_init(&config, NULL, sl_wifi_default_event_handler);
     if (status != SL_STATUS_OK)
     {
-        SILABS_LOG("wfx_wifi_rsi_init failed %x", status);
+        return status;
     }
+
+    // Create Sempaphore for scan
+    sScanSemaphore = osSemaphoreNew(1, 0, NULL);
+    if (sScanSemaphore == NULL)
+    {
+        return SL_STATUS_ALLOCATION_FAILED;
+    }
+
     return status;
 }
 
@@ -421,6 +432,8 @@
 #else
         wfx_rsi.sec.security = WFX_SEC_WPA2;
 #endif /* WIFI_ENABLE_SECURITY_WPA3_TRANSITION */
+
+        osSemaphoreRelease(sScanSemaphore);
         return SL_STATUS_FAIL;
     }
     wfx_rsi.sec.security = WFX_SEC_UNSPECIFIED;
@@ -457,6 +470,8 @@
     }
     wfx_rsi.dev_state &= ~WFX_RSI_ST_SCANSTARTED;
     scan_results_complete = true;
+
+    osSemaphoreRelease(sScanSemaphore);
     return SL_STATUS_OK;
 }
 sl_status_t show_scan_results(sl_wifi_scan_result_t * scan_result)
@@ -501,6 +516,7 @@
 {
     callback_status          = show_scan_results(result);
     bg_scan_results_complete = true;
+    osSemaphoreRelease(sScanSemaphore);
     return SL_STATUS_OK;
 }
 /***************************************************************************************
@@ -529,12 +545,7 @@
 #endif
     if (SL_STATUS_IN_PROGRESS == status)
     {
-        const uint32_t start = osKernelGetTickCount();
-        while (!scan_results_complete && (osKernelGetTickCount() - start) <= WIFI_SCAN_TIMEOUT_TICK)
-        {
-            osThreadYield();
-        }
-        status = scan_results_complete ? callback_status : SL_STATUS_TIMEOUT;
+        osSemaphoreAcquire(sScanSemaphore, WIFI_SCAN_TIMEOUT_TICK);
     }
 }
 
@@ -817,12 +828,7 @@
                 status = sl_wifi_start_scan(SL_WIFI_CLIENT_2_4GHZ_INTERFACE, NULL, &wifi_scan_configuration);
                 if (SL_STATUS_IN_PROGRESS == status)
                 {
-                    const uint32_t start = osKernelGetTickCount();
-                    while (!bg_scan_results_complete && (osKernelGetTickCount() - start) <= WIFI_SCAN_TIMEOUT_TICK)
-                    {
-                        osThreadYield();
-                    }
-                    status = bg_scan_results_complete ? callback_status : SL_STATUS_TIMEOUT;
+                    osSemaphoreAcquire(sScanSemaphore, WIFI_SCAN_TIMEOUT_TICK);
                 }
             }
         }