Merge "Update makefiles for logging."
diff --git a/boot/1.0/default/BootControl.h b/boot/1.0/default/BootControl.h
index 73af4f4..be8a814 100644
--- a/boot/1.0/default/BootControl.h
+++ b/boot/1.0/default/BootControl.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_boot_V1_0_BootControl_H_
-#define HIDL_GENERATED_android_hardware_boot_V1_0_BootControl_H_
+#ifndef ANDROID_HARDWARE_BOOT_V1_0_BOOTCONTROL_H
+#define ANDROID_HARDWARE_BOOT_V1_0_BOOTCONTROL_H
 
 #include <android/hardware/boot/1.0/IBootControl.h>
 #include <hidl/Status.h>
@@ -43,4 +43,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_boot_V1_0_BootControl_H_
+#endif  // ANDROID_HARDWARE_BOOT_V1_0_BOOTCONTROL_H
diff --git a/nfc/1.0/default/Nfc.h b/nfc/1.0/default/Nfc.h
index e596495..5257636 100644
--- a/nfc/1.0/default/Nfc.h
+++ b/nfc/1.0/default/Nfc.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_nfc_V1_0_Nfc_H_
-#define HIDL_GENERATED_android_hardware_nfc_V1_0_Nfc_H_
+#ifndef ANDROID_HARDWARE_NFC_V1_0_NFC_H
+#define ANDROID_HARDWARE_NFC_V1_0_NFC_H
 
 #include <android/hardware/nfc/1.0/INfc.h>
 #include <hidl/Status.h>
@@ -56,4 +56,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_nfc_V1_0_Nfc_H_
+#endif  // ANDROID_HARDWARE_NFC_V1_0_NFC_H
diff --git a/tests/bar/1.0/default/Bar.cpp b/tests/bar/1.0/default/Bar.cpp
index 2158cd8..c07e2fa 100644
--- a/tests/bar/1.0/default/Bar.cpp
+++ b/tests/bar/1.0/default/Bar.cpp
@@ -153,8 +153,8 @@
     return Void();
 }
 
-Return<void> Bar::expectNullHandle(const native_handle_t* h, const Abc& xyz, expectNullHandle_cb _hidl_cb) {
-    ALOGI("SERVER(Bar) h = %p, xyz.z = %p", h, static_cast<void*>(xyz.z));
+Return<void> Bar::expectNullHandle(const hidl_handle& h, const Abc& xyz, expectNullHandle_cb _hidl_cb) {
+    ALOGI("SERVER(Bar) h = %p, xyz.z = %p", h.getNativeHandle(), xyz.z.getNativeHandle());
     _hidl_cb(h == nullptr, xyz.z == nullptr);
     return Void();
 }
diff --git a/tests/bar/1.0/default/Bar.h b/tests/bar/1.0/default/Bar.h
index 4e22b61..d23c1e1 100644
--- a/tests/bar/1.0/default/Bar.h
+++ b/tests/bar/1.0/default/Bar.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_tests_bar_V1_0_Bar_H_
-#define HIDL_GENERATED_android_hardware_tests_bar_V1_0_Bar_H_
+#ifndef ANDROID_HARDWARE_TESTS_BAR_V1_0_BAR_H
+#define ANDROID_HARDWARE_TESTS_BAR_V1_0_BAR_H
 
 #include <android/hardware/tests/bar/1.0/IBar.h>
 #include <hidl/Status.h>
@@ -64,7 +64,7 @@
 
     // Methods from ::android::hardware::tests::bar::V1_0::IBar follow.
     Return<void> thisIsNew()  override;
-    Return<void> expectNullHandle(const native_handle_t* h, const Abc& xyz, expectNullHandle_cb _hidl_cb)  override;
+    Return<void> expectNullHandle(const hidl_handle& h, const Abc& xyz, expectNullHandle_cb _hidl_cb)  override;
 
 private:
     sp<IFoo> mFoo;
@@ -79,4 +79,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_tests_bar_V1_0_Bar_H_
+#endif  // ANDROID_HARDWARE_TESTS_BAR_V1_0_BAR_H
diff --git a/tests/bar/1.0/default/ImportTypes.h b/tests/bar/1.0/default/ImportTypes.h
index b43be70..59c7bea 100644
--- a/tests/bar/1.0/default/ImportTypes.h
+++ b/tests/bar/1.0/default/ImportTypes.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_tests_bar_V1_0_ImportTypes_H_
-#define HIDL_GENERATED_android_hardware_tests_bar_V1_0_ImportTypes_H_
+#ifndef ANDROID_HARDWARE_TESTS_BAR_V1_0_IMPORTTYPES_H
+#define ANDROID_HARDWARE_TESTS_BAR_V1_0_IMPORTTYPES_H
 
 #include <android/hardware/tests/bar/1.0/IImportTypes.h>
 #include <hidl/Status.h>
@@ -33,4 +33,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_tests_bar_V1_0_ImportTypes_H_
+#endif  // ANDROID_HARDWARE_TESTS_BAR_V1_0_IMPORTTYPES_H
diff --git a/tests/foo/1.0/default/Foo.cpp b/tests/foo/1.0/default/Foo.cpp
index f7cc7c0..a1df3b2 100644
--- a/tests/foo/1.0/default/Foo.cpp
+++ b/tests/foo/1.0/default/Foo.cpp
@@ -309,7 +309,7 @@
 }
 
 Return<void> Foo::createHandles(uint32_t size, createHandles_cb _hidl_cb) {
-    hidl_vec<const native_handle_t*> handles;
+    hidl_vec<hidl_handle> handles;
     handles.resize(size);
     for(uint32_t i = 0; i < size; ++i) {
         createMyHandle([&](const MyHandle& h) {
diff --git a/tests/foo/1.0/default/Foo.h b/tests/foo/1.0/default/Foo.h
index 703c210..84629db 100644
--- a/tests/foo/1.0/default/Foo.h
+++ b/tests/foo/1.0/default/Foo.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_tests_foo_V1_0_Foo_H_
-#define HIDL_GENERATED_android_hardware_tests_foo_V1_0_Foo_H_
+#ifndef ANDROID_HARDWARE_TESTS_FOO_V1_0_FOO_H
+#define ANDROID_HARDWARE_TESTS_FOO_V1_0_FOO_H
 
 #include <android/hardware/tests/foo/1.0/IFoo.h>
 #include <hidl/Status.h>
@@ -71,4 +71,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_tests_foo_V1_0_Foo_H_
+#endif  // ANDROID_HARDWARE_TESTS_FOO_V1_0_FOO_H
diff --git a/tests/foo/1.0/default/FooCallback.h b/tests/foo/1.0/default/FooCallback.h
index 00233b5..3164a9d 100644
--- a/tests/foo/1.0/default/FooCallback.h
+++ b/tests/foo/1.0/default/FooCallback.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_tests_foo_V1_0_FooCallback_H_
-#define HIDL_GENERATED_android_hardware_tests_foo_V1_0_FooCallback_H_
+#ifndef ANDROID_HARDWARE_TESTS_FOO_V1_0_FOOCALLBACK_H
+#define ANDROID_HARDWARE_TESTS_FOO_V1_0_FOOCALLBACK_H
 
 #include <android/hardware/tests/foo/1.0/IFooCallback.h>
 #include <hidl/Status.h>
@@ -43,4 +43,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_tests_foo_V1_0_FooCallback_H_
+#endif  // ANDROID_HARDWARE_TESTS_FOO_V1_0_FOOCALLBACK_H
diff --git a/tests/inheritance/1.0/default/Child.h b/tests/inheritance/1.0/default/Child.h
index 0d34e83..268dfeb 100644
--- a/tests/inheritance/1.0/default/Child.h
+++ b/tests/inheritance/1.0/default/Child.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Child_H_
-#define HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Child_H_
+#ifndef ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_CHILD_H
+#define ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_CHILD_H
 
 #include <android/hardware/tests/inheritance/1.0/IChild.h>
 #include <hidl/Status.h>
@@ -41,4 +41,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Child_H_
+#endif  // ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_CHILD_H
diff --git a/tests/inheritance/1.0/default/Fetcher.h b/tests/inheritance/1.0/default/Fetcher.h
index d389853..da9b153 100644
--- a/tests/inheritance/1.0/default/Fetcher.h
+++ b/tests/inheritance/1.0/default/Fetcher.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Fetcher_H_
-#define HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Fetcher_H_
+#ifndef ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_FETCHER_H
+#define ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_FETCHER_H
 
 #include "Child.h"
 #include <android/hardware/tests/inheritance/1.0/IFetcher.h>
@@ -42,4 +42,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Fetcher_H_
+#endif  // ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_FETCHER_H
diff --git a/tests/inheritance/1.0/default/Grandparent.h b/tests/inheritance/1.0/default/Grandparent.h
index e1113bf..5458b01 100644
--- a/tests/inheritance/1.0/default/Grandparent.h
+++ b/tests/inheritance/1.0/default/Grandparent.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Grandparent_H_
-#define HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Grandparent_H_
+#ifndef ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_GRANDPARENT_H
+#define ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_GRANDPARENT_H
 
 #include <android/hardware/tests/inheritance/1.0/IGrandparent.h>
 #include <hidl/Status.h>
@@ -34,4 +34,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Grandparent_H_
+#endif  // ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_GRANDPARENT_H
diff --git a/tests/inheritance/1.0/default/Parent.h b/tests/inheritance/1.0/default/Parent.h
index 2e07fdc..48085fd 100644
--- a/tests/inheritance/1.0/default/Parent.h
+++ b/tests/inheritance/1.0/default/Parent.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Parent_H_
-#define HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Parent_H_
+#ifndef ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_PARENT_H
+#define ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_PARENT_H
 
 #include <android/hardware/tests/inheritance/1.0/IParent.h>
 #include <hidl/Status.h>
@@ -37,4 +37,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_tests_inheritance_V1_0_Parent_H_
+#endif  // ANDROID_HARDWARE_TESTS_INHERITANCE_V1_0_PARENT_H
diff --git a/tests/libhwbinder/1.0/default/Benchmark.h b/tests/libhwbinder/1.0/default/Benchmark.h
index 454fdf2..74b3745 100644
--- a/tests/libhwbinder/1.0/default/Benchmark.h
+++ b/tests/libhwbinder/1.0/default/Benchmark.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_benchmark_V1_0_Benchmark_H_
-#define HIDL_GENERATED_android_hardware_benchmark_V1_0_Benchmark_H_
+#ifndef ANDROID_HARDWARE_BENCHMARK_V1_0_BENCHMARK_H
+#define ANDROID_HARDWARE_BENCHMARK_V1_0_BENCHMARK_H
 
 #include <android/hardware/tests/libhwbinder/1.0/IBenchmark.h>
 #include <hidl/Status.h>
@@ -28,4 +28,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_benchmark_V1_0_Benchmark_H_
+#endif  // ANDROID_HARDWARE_BENCHMARK_V1_0_BENCHMARK_H
diff --git a/tests/pointer/1.0/default/Graph.h b/tests/pointer/1.0/default/Graph.h
index cbd5a8a..03bd163 100644
--- a/tests/pointer/1.0/default/Graph.h
+++ b/tests/pointer/1.0/default/Graph.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_tests_pointer_V1_0_Graph_H_
-#define HIDL_GENERATED_android_hardware_tests_pointer_V1_0_Graph_H_
+#ifndef ANDROID_HARDWARE_TESTS_POINTER_V1_0_GRAPH_H
+#define ANDROID_HARDWARE_TESTS_POINTER_V1_0_GRAPH_H
 
 #include <android/hardware/tests/pointer/1.0/IGraph.h>
 #include <hidl/Status.h>
@@ -44,4 +44,4 @@
 }  // namespace hardware
 }  // namespace android
 
-#endif  // HIDL_GENERATED_android_hardware_tests_pointer_V1_0_Graph_H_
+#endif  // ANDROID_HARDWARE_TESTS_POINTER_V1_0_GRAPH_H
diff --git a/tests/pointer/1.0/default/Pointer.h b/tests/pointer/1.0/default/Pointer.h
index 41d96fd..87b0f56 100644
--- a/tests/pointer/1.0/default/Pointer.h
+++ b/tests/pointer/1.0/default/Pointer.h
@@ -1,5 +1,5 @@
-#ifndef HIDL_GENERATED_android_hardware_tests_pointer_V1_0_Pointer_H_
-#define HIDL_GENERATED_android_hardware_tests_pointer_V1_0_Pointer_H_
+#ifndef ANDROID_HARDWARE_TESTS_POINTER_V1_0_POINTER_H
+#define ANDROID_HARDWARE_TESTS_POINTER_V1_0_POINTER_H
 
 #include <android/hardware/tests/pointer/1.0/IPointer.h>
 #include <android-base/logging.h>
@@ -345,4 +345,4 @@
 
 #undef PUSH_ERROR_IF
 
-#endif  // HIDL_GENERATED_android_hardware_tests_pointer_V1_0_Pointer_H_
+#endif  // ANDROID_HARDWARE_TESTS_POINTER_V1_0_POINTER_H
diff --git a/wifi/1.0/default/wifi_legacy_hal.cpp b/wifi/1.0/default/wifi_legacy_hal.cpp
index 28b7f9a..15b6bfc 100644
--- a/wifi/1.0/default/wifi_legacy_hal.cpp
+++ b/wifi/1.0/default/wifi_legacy_hal.cpp
@@ -28,8 +28,14 @@
 namespace V1_0 {
 namespace implementation {
 namespace legacy_hal {
-// Constants used in the class.
+// Constants ported over from the legacy HAL calling code
+// (com_android_server_wifi_WifiNative.cpp). This will all be thrown
+// away when this shim layer is replaced by the real vendor
+// implementation.
 static constexpr uint32_t kMaxVersionStringLength = 256;
+static constexpr uint32_t kMaxCachedGscanResults = 64;
+static constexpr uint32_t kMaxGscanFrequenciesForBand = 64;
+static constexpr uint32_t kLinkLayerStatsDataMpduSizeThreshold = 128;
 
 // Legacy HAL functions accept "C" style function pointers, so use global
 // functions to pass to the legacy HAL function and store the corresponding
@@ -57,6 +63,40 @@
     on_firmware_memory_dump_internal_callback(buffer, buffer_size);
   }
 }
+
+// Callback to be invoked for Gscan events.
+std::function<void(wifi_request_id, wifi_scan_event)>
+    on_gscan_event_internal_callback;
+void onGscanEvent(wifi_request_id id, wifi_scan_event event) {
+  if (on_gscan_event_internal_callback) {
+    on_gscan_event_internal_callback(id, event);
+  }
+}
+
+// Callback to be invoked for Gscan full results.
+std::function<void(wifi_request_id, wifi_scan_result*, uint32_t)>
+    on_gscan_full_result_internal_callback;
+void onGscanFullResult(wifi_request_id id,
+                       wifi_scan_result* result,
+                       uint32_t buckets_scanned) {
+  if (on_gscan_full_result_internal_callback) {
+    on_gscan_full_result_internal_callback(id, result, buckets_scanned);
+  }
+}
+
+// Callback to be invoked for link layer stats results.
+std::function<void((wifi_request_id, wifi_iface_stat*, int, wifi_radio_stat*))>
+    on_link_layer_stats_result_internal_callback;
+void onLinkLayerStatsDataResult(wifi_request_id id,
+                                wifi_iface_stat* iface_stat,
+                                int num_radios,
+                                wifi_radio_stat* radio_stat) {
+  if (on_link_layer_stats_result_internal_callback) {
+    on_link_layer_stats_result_internal_callback(
+        id, iface_stat, num_radios, radio_stat);
+  }
+}
+
 // End of the free-standing "C" style callbacks.
 
 WifiLegacyHal::WifiLegacyHal()
@@ -206,6 +246,162 @@
       wlan_interface_handle_, program.data(), program.size());
 }
 
+std::pair<wifi_error, wifi_gscan_capabilities>
+WifiLegacyHal::getGscanCapabilities() {
+  wifi_gscan_capabilities caps;
+  wifi_error status = global_func_table_.wifi_get_gscan_capabilities(
+      wlan_interface_handle_, &caps);
+  return {status, caps};
+}
+
+wifi_error WifiLegacyHal::startGscan(
+    wifi_request_id id,
+    const wifi_scan_cmd_params& params,
+    const std::function<void(wifi_request_id)>& on_failure_user_callback,
+    const on_gscan_results_callback& on_results_user_callback,
+    const on_gscan_full_result_callback& on_full_result_user_callback) {
+  // If there is already an ongoing background scan, reject new scan requests.
+  if (on_gscan_event_internal_callback ||
+      on_gscan_full_result_internal_callback) {
+    return WIFI_ERROR_NOT_AVAILABLE;
+  }
+
+  // This callback will be used to either trigger |on_results_user_callback| or
+  // |on_failure_user_callback|.
+  on_gscan_event_internal_callback =
+      [on_failure_user_callback, on_results_user_callback, this](
+          wifi_request_id id, wifi_scan_event event) {
+        switch (event) {
+          case WIFI_SCAN_RESULTS_AVAILABLE:
+          case WIFI_SCAN_THRESHOLD_NUM_SCANS:
+          case WIFI_SCAN_THRESHOLD_PERCENT: {
+            wifi_error status;
+            std::vector<wifi_cached_scan_results> cached_scan_results;
+            std::tie(status, cached_scan_results) = getGscanCachedResults();
+            if (status == WIFI_SUCCESS) {
+              on_results_user_callback(id, cached_scan_results);
+              return;
+            }
+          }
+          // Fall through if failed. Failure to retrieve cached scan results
+          // should trigger a background scan failure.
+          case WIFI_SCAN_FAILED:
+            on_failure_user_callback(id);
+            on_gscan_event_internal_callback = nullptr;
+            on_gscan_full_result_internal_callback = nullptr;
+            return;
+        }
+        LOG(FATAL) << "Unexpected gscan event received: " << event;
+      };
+
+  on_gscan_full_result_internal_callback = [on_full_result_user_callback](
+      wifi_request_id id, wifi_scan_result* result, uint32_t buckets_scanned) {
+    if (result) {
+      on_full_result_user_callback(id, result, buckets_scanned);
+    }
+  };
+
+  wifi_scan_result_handler handler = {onGscanFullResult, onGscanEvent};
+  wifi_error status = global_func_table_.wifi_start_gscan(
+      id, wlan_interface_handle_, params, handler);
+  if (status != WIFI_SUCCESS) {
+    on_gscan_event_internal_callback = nullptr;
+    on_gscan_full_result_internal_callback = nullptr;
+  }
+  return status;
+}
+
+wifi_error WifiLegacyHal::stopGscan(wifi_request_id id) {
+  // If there is no an ongoing background scan, reject stop requests.
+  // TODO(b/32337212): This needs to be handled by the HIDL object because we
+  // need to return the NOT_STARTED error code.
+  if (!on_gscan_event_internal_callback &&
+      !on_gscan_full_result_internal_callback) {
+    return WIFI_ERROR_NOT_AVAILABLE;
+  }
+  wifi_error status =
+      global_func_table_.wifi_stop_gscan(id, wlan_interface_handle_);
+  // If the request Id is wrong, don't stop the ongoing background scan. Any
+  // other error should be treated as the end of background scan.
+  if (status != WIFI_ERROR_INVALID_REQUEST_ID) {
+    on_gscan_event_internal_callback = nullptr;
+    on_gscan_full_result_internal_callback = nullptr;
+  }
+  return status;
+}
+
+std::pair<wifi_error, std::vector<uint32_t>>
+WifiLegacyHal::getValidFrequenciesForGscan(wifi_band band) {
+  static_assert(sizeof(uint32_t) >= sizeof(wifi_channel),
+                "Wifi Channel cannot be represented in output");
+  std::vector<uint32_t> freqs;
+  freqs.resize(kMaxGscanFrequenciesForBand);
+  int32_t num_freqs = 0;
+  wifi_error status = global_func_table_.wifi_get_valid_channels(
+      wlan_interface_handle_,
+      band,
+      freqs.size(),
+      reinterpret_cast<wifi_channel*>(freqs.data()),
+      &num_freqs);
+  CHECK(num_freqs >= 0 &&
+        static_cast<uint32_t>(num_freqs) <= kMaxGscanFrequenciesForBand);
+  freqs.resize(num_freqs);
+  return {status, std::move(freqs)};
+}
+
+wifi_error WifiLegacyHal::enableLinkLayerStats(bool debug) {
+  wifi_link_layer_params params;
+  params.mpdu_size_threshold = kLinkLayerStatsDataMpduSizeThreshold;
+  params.aggressive_statistics_gathering = debug;
+  return global_func_table_.wifi_set_link_stats(wlan_interface_handle_, params);
+}
+
+wifi_error WifiLegacyHal::disableLinkLayerStats() {
+  // TODO: Do we care about these responses?
+  uint32_t clear_mask_rsp;
+  uint8_t stop_rsp;
+  return global_func_table_.wifi_clear_link_stats(
+      wlan_interface_handle_, 0xFFFFFFFF, &clear_mask_rsp, 1, &stop_rsp);
+}
+
+std::pair<wifi_error, LinkLayerStats> WifiLegacyHal::getLinkLayerStats() {
+  LinkLayerStats link_stats{};
+  LinkLayerStats* link_stats_ptr = &link_stats;
+
+  on_link_layer_stats_result_internal_callback = [&link_stats_ptr](
+      wifi_request_id /* id */,
+      wifi_iface_stat* iface_stats_ptr,
+      int num_radios,
+      wifi_radio_stat* radio_stats_ptr) {
+    if (iface_stats_ptr != nullptr) {
+      link_stats_ptr->iface = *iface_stats_ptr;
+      link_stats_ptr->iface.num_peers = 0;
+    } else {
+      LOG(ERROR) << "Invalid iface stats in link layer stats";
+    }
+    if (num_radios == 1 && radio_stats_ptr != nullptr) {
+      link_stats_ptr->radio = *radio_stats_ptr;
+      // Copy over the tx level array to the separate vector.
+      if (radio_stats_ptr->num_tx_levels > 0 &&
+          radio_stats_ptr->tx_time_per_levels != nullptr) {
+        link_stats_ptr->radio_tx_time_per_levels.assign(
+            radio_stats_ptr->tx_time_per_levels,
+            radio_stats_ptr->tx_time_per_levels +
+                radio_stats_ptr->num_tx_levels);
+      }
+      link_stats_ptr->radio.num_tx_levels = 0;
+      link_stats_ptr->radio.tx_time_per_levels = nullptr;
+    } else {
+      LOG(ERROR) << "Invalid radio stats in link layer stats";
+    }
+  };
+
+  wifi_error status = global_func_table_.wifi_get_link_stats(
+      0, wlan_interface_handle_, {onLinkLayerStatsDataResult});
+  on_link_layer_stats_result_internal_callback = nullptr;
+  return {status, link_stats};
+}
+
 wifi_error WifiLegacyHal::retrieveWlanInterfaceHandle() {
   const std::string& ifname_to_find = getStaIfaceName();
   wifi_interface_handle* iface_handles = nullptr;
@@ -245,12 +441,44 @@
   if_tool.SetWifiUpState(false);
 }
 
+std::pair<wifi_error, std::vector<wifi_cached_scan_results>>
+WifiLegacyHal::getGscanCachedResults() {
+  std::vector<wifi_cached_scan_results> cached_scan_results;
+  cached_scan_results.resize(kMaxCachedGscanResults);
+  int32_t num_results = 0;
+  wifi_error status = global_func_table_.wifi_get_cached_gscan_results(
+      wlan_interface_handle_,
+      true /* always flush */,
+      cached_scan_results.size(),
+      cached_scan_results.data(),
+      &num_results);
+  CHECK(num_results >= 0 &&
+        static_cast<uint32_t>(num_results) <= kMaxCachedGscanResults);
+  cached_scan_results.resize(num_results);
+  // Check for invalid IE lengths in these cached scan results and correct it.
+  for (auto& cached_scan_result : cached_scan_results) {
+    int num_scan_results = cached_scan_result.num_results;
+    for (int i = 0; i < num_scan_results; i++) {
+      auto& scan_result = cached_scan_result.results[i];
+      if (scan_result.ie_length > 0) {
+        LOG(ERROR) << "Cached scan result has non-zero IE length "
+                   << scan_result.ie_length;
+        scan_result.ie_length = 0;
+      }
+    }
+  }
+  return {status, std::move(cached_scan_results)};
+}
+
 void WifiLegacyHal::invalidate() {
   global_handle_ = nullptr;
   wlan_interface_handle_ = nullptr;
   on_stop_complete_internal_callback = nullptr;
   on_driver_memory_dump_internal_callback = nullptr;
   on_firmware_memory_dump_internal_callback = nullptr;
+  on_gscan_event_internal_callback = nullptr;
+  on_gscan_full_result_internal_callback = nullptr;
+  on_link_layer_stats_result_internal_callback = nullptr;
 }
 
 }  // namespace legacy_hal
diff --git a/wifi/1.0/default/wifi_legacy_hal.h b/wifi/1.0/default/wifi_legacy_hal.h
index 8bd146a..df1c3d6 100644
--- a/wifi/1.0/default/wifi_legacy_hal.h
+++ b/wifi/1.0/default/wifi_legacy_hal.h
@@ -39,6 +39,31 @@
   uint32_t max_len;
 };
 
+// WARNING: We don't care about the variable sized members of either
+// |wifi_iface_stat|, |wifi_radio_stat| structures. So, using the pragma
+// to escape the compiler warnings regarding this.
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wgnu-variable-sized-type-not-at-end"
+// The |wifi_radio_stat.tx_time_per_levels| stats is provided as a pointer in
+// |wifi_radio_stat| structure in the legacy HAL API. Separate that out
+// into a separate return element to avoid passing pointers around.
+struct LinkLayerStats {
+  wifi_iface_stat iface;
+  wifi_radio_stat radio;
+  std::vector<uint32_t> radio_tx_time_per_levels;
+};
+#pragma GCC diagnostic pop
+
+// Full scan results contain IE info and are hence passed by reference, to
+// preserve the variable length array member |ie_data|. Callee must not retain
+// the pointer.
+using on_gscan_full_result_callback =
+    std::function<void(wifi_request_id, const wifi_scan_result*, uint32_t)>;
+// These scan results don't contain any IE info, so no need to pass by
+// reference.
+using on_gscan_results_callback = std::function<void(
+    wifi_request_id, const std::vector<wifi_cached_scan_results>&)>;
+
 /**
  * Class that encapsulates all legacy HAL interactions.
  * This class manages the lifetime of the event loop thread used by legacy HAL.
@@ -65,12 +90,41 @@
   // APF functions.
   std::pair<wifi_error, PacketFilterCapabilities> getPacketFilterCapabilities();
   wifi_error setPacketFilter(const std::vector<uint8_t>& program);
+  // Gscan functions.
+  std::pair<wifi_error, wifi_gscan_capabilities> getGscanCapabilities();
+  // These API's provides a simplified interface over the legacy Gscan API's:
+  // a) All scan events from the legacy HAL API other than the
+  //    |WIFI_SCAN_FAILED| are treated as notification of results.
+  //    This method then retrieves the cached scan results from the legacy
+  //    HAL API and triggers the externally provided |on_results_user_callback|
+  //    on success.
+  // b) |WIFI_SCAN_FAILED| scan event or failure to retrieve cached scan results
+  //    triggers the externally provided |on_failure_user_callback|.
+  // c) Full scan result event triggers the externally provided
+  //    |on_full_result_user_callback|.
+  wifi_error startGscan(
+      wifi_request_id id,
+      const wifi_scan_cmd_params& params,
+      const std::function<void(wifi_request_id)>& on_failure_callback,
+      const on_gscan_results_callback& on_results_callback,
+      const on_gscan_full_result_callback& on_full_result_callback);
+  wifi_error stopGscan(wifi_request_id id);
+  std::pair<wifi_error, std::vector<uint32_t>> getValidFrequenciesForGscan(
+      wifi_band band);
+  // Link layer stats functions.
+  wifi_error enableLinkLayerStats(bool debug);
+  wifi_error disableLinkLayerStats();
+  std::pair<wifi_error, LinkLayerStats> getLinkLayerStats();
 
  private:
   // Retrieve the interface handle to be used for the "wlan" interface.
   wifi_error retrieveWlanInterfaceHandle();
   // Run the legacy HAL event loop thread.
   void runEventLoop();
+  // Retrieve the cached gscan results to pass the results back to the external
+  // callbacks.
+  std::pair<wifi_error, std::vector<wifi_cached_scan_results>>
+  getGscanCachedResults();
   void invalidate();
 
   // Event loop thread used by legacy HAL.