Add HAL APIs for WiFi CHRE NAN RTT

1. Add new APIs to enable/disable CHRE NAN RTT
2. Add a new API to register callback.
   This callback should report events from WLAN driver for CHRE.

Test: New APIs work fine
Bug: 206614765
Change-Id: I8c8ab002064a9556be2e7d3972703bb3255a3a41
diff --git a/wifi/1.6/default/hidl_callback_util.h b/wifi/1.6/default/hidl_callback_util.h
index 3ac54c1..aab0ae5 100644
--- a/wifi/1.6/default/hidl_callback_util.h
+++ b/wifi/1.6/default/hidl_callback_util.h
@@ -20,6 +20,7 @@
 #include <set>
 
 #include <hidl/HidlSupport.h>
+#include <hidl/HidlTransportSupport.h>
 
 namespace {
 // Type of callback invoked by the death handler.
@@ -68,9 +69,11 @@
         // (callback proxy's raw pointer) to track the death of individual
         // clients.
         uint64_t cookie = reinterpret_cast<uint64_t>(cb.get());
-        if (cb_set_.find(cb) != cb_set_.end()) {
-            LOG(WARNING) << "Duplicate death notification registration";
-            return true;
+        for (const auto& s : cb_set_) {
+            if (interfacesEqual(cb, s)) {
+                LOG(ERROR) << "Duplicate death notification registration";
+                return true;
+            }
         }
         if (!cb->linkToDeath(death_handler_, cookie)) {
             LOG(ERROR) << "Failed to register death notification";
diff --git a/wifi/1.6/default/wifi_legacy_hal.cpp b/wifi/1.6/default/wifi_legacy_hal.cpp
index b006e45..8a75fd8 100644
--- a/wifi/1.6/default/wifi_legacy_hal.cpp
+++ b/wifi/1.6/default/wifi_legacy_hal.cpp
@@ -357,6 +357,15 @@
     }
 }
 
+// Callback to report current CHRE NAN state
+std::function<void(chre_nan_rtt_state)> on_chre_nan_rtt_internal_callback;
+void onAsyncChreNanRttState(chre_nan_rtt_state state) {
+    const auto lock = hidl_sync_util::acquireGlobalLock();
+    if (on_chre_nan_rtt_internal_callback) {
+        on_chre_nan_rtt_internal_callback(state);
+    }
+}
+
 // End of the free-standing "C" style callbacks.
 
 WifiLegacyHal::WifiLegacyHal(const std::weak_ptr<wifi_system::InterfaceTool> iface_tool,
@@ -1551,6 +1560,30 @@
     return {status, radio_combination_matrix_ptr};
 }
 
+wifi_error WifiLegacyHal::chreNanRttRequest(const std::string& iface_name, bool enable) {
+    if (enable)
+        return global_func_table_.wifi_nan_rtt_chre_enable_request(0, getIfaceHandle(iface_name),
+                                                                   NULL);
+    else
+        return global_func_table_.wifi_nan_rtt_chre_disable_request(0, getIfaceHandle(iface_name));
+}
+
+wifi_error WifiLegacyHal::chreRegisterHandler(const std::string& iface_name,
+                                              const ChreCallbackHandlers& handler) {
+    if (on_chre_nan_rtt_internal_callback) {
+        return WIFI_ERROR_NOT_AVAILABLE;
+    }
+
+    on_chre_nan_rtt_internal_callback = handler.on_wifi_chre_nan_rtt_state;
+
+    wifi_error status = global_func_table_.wifi_chre_register_handler(getIfaceHandle(iface_name),
+                                                                      {onAsyncChreNanRttState});
+    if (status != WIFI_SUCCESS) {
+        on_chre_nan_rtt_internal_callback = nullptr;
+    }
+    return status;
+}
+
 void WifiLegacyHal::invalidate() {
     global_handle_ = nullptr;
     iface_name_to_handle_.clear();
@@ -1586,6 +1619,7 @@
     on_twt_event_teardown_completion_callback = nullptr;
     on_twt_event_info_frame_received_callback = nullptr;
     on_twt_event_device_notify_callback = nullptr;
+    on_chre_nan_rtt_internal_callback = nullptr;
 }
 
 }  // namespace legacy_hal
diff --git a/wifi/1.6/default/wifi_legacy_hal.h b/wifi/1.6/default/wifi_legacy_hal.h
index 638bfa1..ab21e8e 100644
--- a/wifi/1.6/default/wifi_legacy_hal.h
+++ b/wifi/1.6/default/wifi_legacy_hal.h
@@ -36,6 +36,7 @@
 namespace legacy_hal {
 // Import all the types defined inside the legacy HAL header files into this
 // namespace.
+using ::chre_nan_rtt_state;
 using ::frame_info;
 using ::frame_type;
 using ::FRAME_TYPE_80211_MGMT;
@@ -458,6 +459,12 @@
     std::function<void(const TwtDeviceNotify&)> on_device_notify;
 };
 
+// CHRE response and event callbacks struct.
+struct ChreCallbackHandlers {
+    // Callback for CHRE NAN RTT
+    std::function<void(chre_nan_rtt_state)> on_wifi_chre_nan_rtt_state;
+};
+
 /**
  * Class that encapsulates all legacy HAL interactions.
  * This class manages the lifetime of the event loop thread used by legacy HAL.
@@ -670,6 +677,12 @@
 
     std::pair<wifi_error, wifi_radio_combination_matrix*> getSupportedRadioCombinationsMatrix();
 
+    // CHRE NAN RTT function
+    wifi_error chreNanRttRequest(const std::string& iface_name, bool enable);
+
+    wifi_error chreRegisterHandler(const std::string& iface_name,
+                                   const ChreCallbackHandlers& handler);
+
   private:
     // Retrieve interface handles for all the available interfaces.
     wifi_error retrieveIfaceHandles();
diff --git a/wifi/1.6/default/wifi_legacy_hal_stubs.cpp b/wifi/1.6/default/wifi_legacy_hal_stubs.cpp
index 05a27cf..5ad22f5 100644
--- a/wifi/1.6/default/wifi_legacy_hal_stubs.cpp
+++ b/wifi/1.6/default/wifi_legacy_hal_stubs.cpp
@@ -163,6 +163,9 @@
     populateStubFor(&hal_fn->wifi_trigger_subsystem_restart);
     populateStubFor(&hal_fn->wifi_set_indoor_state);
     populateStubFor(&hal_fn->wifi_get_supported_radio_combinations_matrix);
+    populateStubFor(&hal_fn->wifi_nan_rtt_chre_enable_request);
+    populateStubFor(&hal_fn->wifi_nan_rtt_chre_disable_request);
+    populateStubFor(&hal_fn->wifi_chre_register_handler);
     return true;
 }
 }  // namespace legacy_hal