wifi(implementation): Make WifiLegacyHal.stop() blocking

IWifi::stop() is currently non-blocking which makes it hard for the
client to determing when the stop is fully complete. This for example
causes wificond to disable the wlan0 interface while the legacy HAL
stop is being processed. So, add a timed wait to let the legacy HAL
complete processing of the stop before we unblock the IWifi::stop()
HIDL call.

Bug: 64611487
Test: Manual tests by wifi state toggling and verifying the order of
events in logs:
08-15 19:17:53.302   796   796 I android.hardware.wifi@1.0-service:
Stopping legacy HAL
08-15 19:17:53.302   796   796 I WifiHAL : Sent msg on exit sock to
unblock poll()
08-15 19:17:53.302   796  4793 E CLD80211:
/vendor/bin/hw/android.hardware.wifi@1.0-service: Could not find group
host_logs, errno: 0 id: -2
08-15 19:17:53.302   796  4793 E CLD80211:
/vendor/bin/hw/android.hardware.wifi@1.0-service: Could not find group
fw_logs, errno: 0 id: -2
08-15 19:17:53.302   796  4793 E CLD80211:
/vendor/bin/hw/android.hardware.wifi@1.0-service: Could not find group
per_pkt_stats, errno: 0 id: -2
08-15 19:17:53.302   796  4793 E CLD80211:
/vendor/bin/hw/android.hardware.wifi@1.0-service: Could not find group
diag_events, errno: 0 id: -2
08-15 19:17:53.302   796  4793 E CLD80211:
/vendor/bin/hw/android.hardware.wifi@1.0-service: Could not find group
fatal_events, errno: 0 id: -2
08-15 19:17:53.302   796  4793 I CLD80211:
/vendor/bin/hw/android.hardware.wifi@1.0-service: Sent msg on exit sock
to unblock poll()
08-15 19:17:53.302   796  4793 I android.hardware.wifi@1.0-service:
Legacy HAL stop complete callback received
08-15 19:17:53.304   802   838 D CHRE    : @ 151.328: [Platform] wifi:
has 0, enabled 0
08-15 19:17:53.321   796  4793 I android.hardware.wifi@1.0-service:
Legacy HAL event loop terminated
08-15 19:17:53.321   796   796 I android.hardware.wifi@1.0-service:
Legacy HAL stop complete
08-15 19:17:53.522   796   796 I android.hardware.wifi@1.0-service: Wifi
HAL stopped

Test: Will send for regression tests.
Change-Id: I394c11724e9459a4b9a6b970e2bcb4e0ad65fefc
diff --git a/wifi/1.1/default/wifi_legacy_hal.h b/wifi/1.1/default/wifi_legacy_hal.h
index caa1bd5..5498803 100644
--- a/wifi/1.1/default/wifi_legacy_hal.h
+++ b/wifi/1.1/default/wifi_legacy_hal.h
@@ -20,6 +20,7 @@
 #include <functional>
 #include <thread>
 #include <vector>
+#include <condition_variable>
 
 #include <wifi_system/interface_tool.h>
 
@@ -149,8 +150,10 @@
   wifi_error initialize();
   // Start the legacy HAL and the event looper thread.
   wifi_error start();
-  // Deinitialize the legacy HAL and stop the event looper thread.
-  wifi_error stop(const std::function<void()>& on_complete_callback);
+  // Deinitialize the legacy HAL and wait for the event loop thread to exit
+  // using a predefined timeout.
+  wifi_error stop(std::unique_lock<std::recursive_mutex>* lock,
+                  const std::function<void()>& on_complete_callback);
   // Wrappers for all the functions in the legacy HAL function table.
   std::pair<wifi_error, std::string> getDriverVersion();
   std::pair<wifi_error, std::string> getFirmwareVersion();
@@ -293,6 +296,7 @@
   wifi_interface_handle wlan_interface_handle_;
   // Flag to indicate if we have initiated the cleanup of legacy HAL.
   std::atomic<bool> awaiting_event_loop_termination_;
+  std::condition_variable_any stop_wait_cv_;
   // Flag to indicate if the legacy HAL has been started.
   bool is_started_;
   wifi_system::InterfaceTool iface_tool_;