Merge "[AWARE] Add capability info: max subscribe addresses"
diff --git a/bluetooth/1.0/vts/functional/Android.bp b/bluetooth/1.0/vts/functional/Android.bp
index 086ac99..2012c20 100644
--- a/bluetooth/1.0/vts/functional/Android.bp
+++ b/bluetooth/1.0/vts/functional/Android.bp
@@ -15,9 +15,9 @@
 //
 
 cc_test {
-    name: "bluetooth_hidl_hal_test",
+    name: "VtsHalBluetoothV1_0TargetTest",
     gtest: true,
-    srcs: ["bluetooth_hidl_hal_test.cpp"],
+    srcs: ["VtsHalBluetoothV1_0TargetTest.cpp"],
     shared_libs: [
         "libbase",
         "liblog",
diff --git a/bluetooth/1.0/vts/functional/bluetooth_hidl_hal_test.cpp b/bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp
similarity index 100%
rename from bluetooth/1.0/vts/functional/bluetooth_hidl_hal_test.cpp
rename to bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp
diff --git a/ir/1.0/vts/functional/Android.bp b/ir/1.0/vts/functional/Android.bp
index 1acd2a0..5689474 100644
--- a/ir/1.0/vts/functional/Android.bp
+++ b/ir/1.0/vts/functional/Android.bp
@@ -15,9 +15,9 @@
 //
 
 cc_test {
-    name: "ir_hidl_hal_test",
+    name: "VtsHalIrV1_0TargetTest",
     gtest: true,
-    srcs: ["ir_hidl_hal_test.cpp"],
+    srcs: ["VtsHalIrV1_0TargetTest.cpp"],
     shared_libs: [
         "libbase",
         "liblog",
diff --git a/ir/1.0/vts/functional/ir_hidl_hal_test.cpp b/ir/1.0/vts/functional/VtsHalIrV1_0TargetTest.cpp
similarity index 100%
rename from ir/1.0/vts/functional/ir_hidl_hal_test.cpp
rename to ir/1.0/vts/functional/VtsHalIrV1_0TargetTest.cpp
diff --git a/nfc/1.0/vts/functional/Android.bp b/nfc/1.0/vts/functional/Android.bp
index 0f9eb3d..080887f 100644
--- a/nfc/1.0/vts/functional/Android.bp
+++ b/nfc/1.0/vts/functional/Android.bp
@@ -15,9 +15,9 @@
 //
 
 cc_test {
-    name: "nfc_hidl_hal_test",
+    name: "VtsHalNfcV1_0TargetTest",
     gtest: true,
-    srcs: ["nfc_hidl_hal_test.cpp"],
+    srcs: ["VtsHalNfcV1_0TargetTest.cpp"],
     shared_libs: [
         "libbase",
         "liblog",
diff --git a/nfc/1.0/vts/functional/nfc_hidl_hal_test.cpp b/nfc/1.0/vts/functional/VtsHalNfcV1_0TargetTest.cpp
similarity index 100%
rename from nfc/1.0/vts/functional/nfc_hidl_hal_test.cpp
rename to nfc/1.0/vts/functional/VtsHalNfcV1_0TargetTest.cpp
diff --git a/radio/1.0/vts/functional/Android.bp b/radio/1.0/vts/functional/Android.bp
index a01e270..6615f03 100644
--- a/radio/1.0/vts/functional/Android.bp
+++ b/radio/1.0/vts/functional/Android.bp
@@ -15,12 +15,12 @@
 //
 
 cc_test {
-    name: "radio_hidl_hal_test",
+    name: "VtsHalRadioV1_0TargetTest",
     gtest: true,
     srcs: ["radio_hidl_hal_test.cpp",
            "radio_response.cpp",
            "radio_hidl_hal_icc.cpp",
-           "radio_hidl_hal_main.cpp"],
+           "VtsHalRadioV1_0TargetTest.cpp"],
     shared_libs: [
         "libbase",
         "liblog",
diff --git a/radio/1.0/vts/functional/radio_hidl_hal_main.cpp b/radio/1.0/vts/functional/VtsHalRadioV1_0TargetTest.cpp
similarity index 100%
rename from radio/1.0/vts/functional/radio_hidl_hal_main.cpp
rename to radio/1.0/vts/functional/VtsHalRadioV1_0TargetTest.cpp
diff --git a/wifi/1.0/default/hidl_callback_util.h b/wifi/1.0/default/hidl_callback_util.h
new file mode 100644
index 0000000..a1c6819
--- /dev/null
+++ b/wifi/1.0/default/hidl_callback_util.h
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef HIDL_CALLBACK_UTIL_H_
+#define HIDL_CALLBACK_UTIL_H_
+
+#include <set>
+
+#include <hidl/HidlSupport.h>
+
+namespace {
+// Type of callback invoked by the death handler.
+using on_death_cb_function = std::function<void(uint64_t)>;
+
+// Private class used to keep track of death of individual
+// callbacks stored in HidlCallbackHandler.
+template <typename CallbackType>
+class HidlDeathHandler : public android::hardware::hidl_death_recipient {
+ public:
+  HidlDeathHandler(const on_death_cb_function& user_cb_function)
+      : cb_function_(user_cb_function) {}
+  ~HidlDeathHandler() = default;
+
+  // Death notification for callbacks.
+  void serviceDied(
+      uint64_t cookie,
+      const android::wp<android::hidl::base::V1_0::IBase>& /* who */) override {
+    cb_function_(cookie);
+  }
+
+ private:
+  on_death_cb_function cb_function_;
+
+  DISALLOW_COPY_AND_ASSIGN(HidlDeathHandler);
+};
+}  // namespace
+
+namespace android {
+namespace hardware {
+namespace wifi {
+namespace V1_0 {
+namespace implementation {
+namespace hidl_callback_util {
+template <typename CallbackType>
+// Provides a class to manage callbacks for the various HIDL interfaces and
+// handle the death of the process hosting each callback.
+class HidlCallbackHandler {
+ public:
+  HidlCallbackHandler()
+      : death_handler_(new HidlDeathHandler<CallbackType>(
+            std::bind(&HidlCallbackHandler::onObjectDeath,
+                      this,
+                      std::placeholders::_1))) {}
+  ~HidlCallbackHandler() = default;
+
+  bool addCallback(const sp<CallbackType>& cb) {
+    // TODO(b/33818800): Can't compare proxies yet. So, use the cookie
+    // (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;
+    }
+    if (!cb->linkToDeath(death_handler_, cookie)) {
+      LOG(ERROR) << "Failed to register death notification";
+      return false;
+    }
+    cb_set_.insert(cb);
+    return true;
+  }
+
+  const std::set<android::sp<CallbackType>> getCallbacks() {
+    return cb_set_;
+  }
+
+  // Death notification for callbacks.
+  void onObjectDeath(uint64_t cookie) {
+    CallbackType *cb = reinterpret_cast<CallbackType*>(cookie);
+    const auto& iter =  cb_set_.find(cb);
+    if (iter == cb_set_.end()) {
+      LOG(ERROR) << "Unknown callback death notification received";
+      return;
+    }
+    cb_set_.erase(iter);
+    LOG(DEBUG) << "Dead callback removed from list";
+  }
+
+  void invalidate() {
+    for (const sp<CallbackType>& cb : cb_set_) {
+      if (!cb->unlinkToDeath(death_handler_)) {
+        LOG(ERROR) << "Failed to deregister death notification";
+      }
+    }
+    cb_set_.clear();
+  }
+
+ private:
+  std::set<sp<CallbackType>> cb_set_;
+  sp<HidlDeathHandler<CallbackType>> death_handler_;
+
+  DISALLOW_COPY_AND_ASSIGN(HidlCallbackHandler);
+};
+
+}  // namespace hidl_callback_util
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace wifi
+}  // namespace hardware
+}  // namespace android
+#endif  // HIDL_CALLBACK_UTIL_H_
diff --git a/wifi/1.0/default/wifi.cpp b/wifi/1.0/default/wifi.cpp
index 8feb836..3d482b4 100644
--- a/wifi/1.0/default/wifi.cpp
+++ b/wifi/1.0/default/wifi.cpp
@@ -85,8 +85,9 @@
 
 WifiStatus Wifi::registerEventCallbackInternal(
     const sp<IWifiEventCallback>& event_callback) {
-  // TODO(b/31632518): remove the callback when the client is destroyed
-  event_callbacks_.emplace_back(event_callback);
+  if (!event_cb_handler_.addCallback(event_callback)) {
+    return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
+  }
   return createWifiStatus(WifiStatusCode::SUCCESS);
 }
 
@@ -102,13 +103,13 @@
     // Create the chip instance once the HAL is started.
     chip_ = new WifiChip(kChipId, legacy_hal_, mode_controller_);
     run_state_ = RunState::STARTED;
-    for (const auto& callback : event_callbacks_) {
+    for (const auto& callback : event_cb_handler_.getCallbacks()) {
       if (!callback->onStart().isOk()) {
         LOG(ERROR) << "Failed to invoke onStart callback";
       };
     }
   } else {
-    for (const auto& callback : event_callbacks_) {
+    for (const auto& callback : event_cb_handler_.getCallbacks()) {
       if (!callback->onFailure(wifi_status).isOk()) {
         LOG(ERROR) << "Failed to invoke onFailure callback";
       }
@@ -126,13 +127,13 @@
   }
   WifiStatus wifi_status = stopLegacyHalAndDeinitializeModeController();
   if (wifi_status.code == WifiStatusCode::SUCCESS) {
-    for (const auto& callback : event_callbacks_) {
+    for (const auto& callback : event_cb_handler_.getCallbacks()) {
       if (!callback->onStop().isOk()) {
         LOG(ERROR) << "Failed to invoke onStop callback";
       };
     }
   } else {
-    for (const auto& callback : event_callbacks_) {
+    for (const auto& callback : event_cb_handler_.getCallbacks()) {
       if (!callback->onFailure(wifi_status).isOk()) {
         LOG(ERROR) << "Failed to invoke onFailure callback";
       }
diff --git a/wifi/1.0/default/wifi.h b/wifi/1.0/default/wifi.h
index 40d3552..c6fa84c 100644
--- a/wifi/1.0/default/wifi.h
+++ b/wifi/1.0/default/wifi.h
@@ -23,6 +23,7 @@
 #include <android/hardware/wifi/1.0/IWifi.h>
 #include <utils/Looper.h>
 
+#include "hidl_callback_util.h"
 #include "wifi_chip.h"
 #include "wifi_legacy_hal.h"
 #include "wifi_mode_controller.h"
@@ -71,8 +72,8 @@
   std::shared_ptr<legacy_hal::WifiLegacyHal> legacy_hal_;
   std::shared_ptr<mode_controller::WifiModeController> mode_controller_;
   RunState run_state_;
-  std::vector<sp<IWifiEventCallback>> event_callbacks_;
   sp<WifiChip> chip_;
+  hidl_callback_util::HidlCallbackHandler<IWifiEventCallback> event_cb_handler_;
 
   DISALLOW_COPY_AND_ASSIGN(Wifi);
 };
diff --git a/wifi/1.0/default/wifi_chip.cpp b/wifi/1.0/default/wifi_chip.cpp
index 0e2d54e..6aeedf8 100644
--- a/wifi/1.0/default/wifi_chip.cpp
+++ b/wifi/1.0/default/wifi_chip.cpp
@@ -63,7 +63,7 @@
 void WifiChip::invalidate() {
   invalidateAndRemoveAllIfaces();
   legacy_hal_.reset();
-  event_callbacks_.clear();
+  event_cb_handler_.invalidate();
   is_valid_ = false;
 }
 
@@ -71,8 +71,8 @@
   return is_valid_;
 }
 
-std::vector<sp<IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
-  return event_callbacks_;
+std::set<sp<IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
+  return event_cb_handler_.getCallbacks();
 }
 
 Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
@@ -353,8 +353,9 @@
 
 WifiStatus WifiChip::registerEventCallbackInternal(
     const sp<IWifiChipEventCallback>& event_callback) {
-  // TODO(b/31632518): remove the callback when the client is destroyed
-  event_callbacks_.emplace_back(event_callback);
+  if (!event_cb_handler_.addCallback(event_callback)) {
+    return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
+  }
   return createWifiStatus(WifiStatusCode::SUCCESS);
 }
 
@@ -414,14 +415,14 @@
   }
   WifiStatus status = handleChipConfiguration(mode_id);
   if (status.code != WifiStatusCode::SUCCESS) {
-    for (const auto& callback : event_callbacks_) {
+    for (const auto& callback : event_cb_handler_.getCallbacks()) {
       if (!callback->onChipReconfigureFailure(status).isOk()) {
         LOG(ERROR) << "Failed to invoke onChipReconfigureFailure callback";
       }
     }
     return status;
   }
-  for (const auto& callback : event_callbacks_) {
+  for (const auto& callback : event_cb_handler_.getCallbacks()) {
     if (!callback->onChipReconfigured(mode_id).isOk()) {
       LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
     }
@@ -503,7 +504,7 @@
   }
   std::string ifname = legacy_hal_.lock()->getApIfaceName();
   ap_iface_ = new WifiApIface(ifname, legacy_hal_);
-  for (const auto& callback : event_callbacks_) {
+  for (const auto& callback : event_cb_handler_.getCallbacks()) {
     if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
       LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
     }
@@ -533,7 +534,7 @@
     return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
   }
   invalidateAndClear(ap_iface_);
-  for (const auto& callback : event_callbacks_) {
+  for (const auto& callback : event_cb_handler_.getCallbacks()) {
     if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
       LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
     }
@@ -549,7 +550,7 @@
   }
   std::string ifname = legacy_hal_.lock()->getNanIfaceName();
   nan_iface_ = new WifiNanIface(ifname, legacy_hal_);
-  for (const auto& callback : event_callbacks_) {
+  for (const auto& callback : event_cb_handler_.getCallbacks()) {
     if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
       LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
     }
@@ -579,7 +580,7 @@
     return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
   }
   invalidateAndClear(nan_iface_);
-  for (const auto& callback : event_callbacks_) {
+  for (const auto& callback : event_cb_handler_.getCallbacks()) {
     if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
       LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
     }
@@ -595,7 +596,7 @@
   }
   std::string ifname = legacy_hal_.lock()->getP2pIfaceName();
   p2p_iface_ = new WifiP2pIface(ifname, legacy_hal_);
-  for (const auto& callback : event_callbacks_) {
+  for (const auto& callback : event_cb_handler_.getCallbacks()) {
     if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
       LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
     }
@@ -625,7 +626,7 @@
     return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
   }
   invalidateAndClear(p2p_iface_);
-  for (const auto& callback : event_callbacks_) {
+  for (const auto& callback : event_cb_handler_.getCallbacks()) {
     if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
       LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
     }
@@ -639,7 +640,7 @@
   }
   std::string ifname = legacy_hal_.lock()->getStaIfaceName();
   sta_iface_ = new WifiStaIface(ifname, legacy_hal_);
-  for (const auto& callback : event_callbacks_) {
+  for (const auto& callback : event_cb_handler_.getCallbacks()) {
     if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
       LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
     }
@@ -669,7 +670,7 @@
     return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
   }
   invalidateAndClear(sta_iface_);
-  for (const auto& callback : event_callbacks_) {
+  for (const auto& callback : event_cb_handler_.getCallbacks()) {
     if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
       LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
     }
diff --git a/wifi/1.0/default/wifi_chip.h b/wifi/1.0/default/wifi_chip.h
index 938b180..e1c2344 100644
--- a/wifi/1.0/default/wifi_chip.h
+++ b/wifi/1.0/default/wifi_chip.h
@@ -22,6 +22,7 @@
 #include <android-base/macros.h>
 #include <android/hardware/wifi/1.0/IWifiChip.h>
 
+#include "hidl_callback_util.h"
 #include "wifi_ap_iface.h"
 #include "wifi_legacy_hal.h"
 #include "wifi_mode_controller.h"
@@ -62,7 +63,7 @@
   // valid before processing them.
   void invalidate();
   bool isValid();
-  std::vector<sp<IWifiChipEventCallback>> getEventCallbacks();
+  std::set<sp<IWifiChipEventCallback>> getEventCallbacks();
 
   // HIDL methods exposed.
   Return<void> getId(getId_cb hidl_status_cb) override;
@@ -179,7 +180,6 @@
   ChipId chip_id_;
   std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal_;
   std::weak_ptr<mode_controller::WifiModeController> mode_controller_;
-  std::vector<sp<IWifiChipEventCallback>> event_callbacks_;
   sp<WifiApIface> ap_iface_;
   sp<WifiNanIface> nan_iface_;
   sp<WifiP2pIface> p2p_iface_;
@@ -191,6 +191,8 @@
   // registration mechanism. Use this to check if we have already
   // registered a callback.
   bool debug_ring_buffer_cb_registered_;
+  hidl_callback_util::HidlCallbackHandler<IWifiChipEventCallback>
+      event_cb_handler_;
 
   DISALLOW_COPY_AND_ASSIGN(WifiChip);
 };
diff --git a/wifi/1.0/default/wifi_nan_iface.cpp b/wifi/1.0/default/wifi_nan_iface.cpp
index 8d76f91..4165196 100644
--- a/wifi/1.0/default/wifi_nan_iface.cpp
+++ b/wifi/1.0/default/wifi_nan_iface.cpp
@@ -55,7 +55,7 @@
 
     switch (msg.response_type) {
     case legacy_hal::NAN_RESPONSE_ENABLED: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyEnableResponse(id, wifiNanStatus).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
           }
@@ -63,7 +63,7 @@
         break;
     }
     case legacy_hal::NAN_RESPONSE_DISABLED: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyDisableResponse(id, wifiNanStatus).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
           }
@@ -71,7 +71,7 @@
         break;
     }
     case legacy_hal::NAN_RESPONSE_PUBLISH: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyStartPublishResponse(id, wifiNanStatus,
                         msg.body.publish_response.publish_id).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
@@ -80,7 +80,7 @@
         break;
     }
     case legacy_hal::NAN_RESPONSE_PUBLISH_CANCEL: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyStopPublishResponse(id, wifiNanStatus).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
           }
@@ -88,7 +88,7 @@
         break;
     }
     case legacy_hal::NAN_RESPONSE_TRANSMIT_FOLLOWUP: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyTransmitFollowupResponse(id, wifiNanStatus).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
           }
@@ -96,7 +96,7 @@
         break;
     }
     case legacy_hal::NAN_RESPONSE_SUBSCRIBE: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyStartSubscribeResponse(id, wifiNanStatus,
                         msg.body.subscribe_response.subscribe_id).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
@@ -105,7 +105,7 @@
         break;
     }
     case legacy_hal::NAN_RESPONSE_SUBSCRIBE_CANCEL: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyStopSubscribeResponse(id, wifiNanStatus).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
           }
@@ -113,7 +113,7 @@
         break;
     }
     case legacy_hal::NAN_RESPONSE_CONFIG: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyConfigResponse(id, wifiNanStatus).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
           }
@@ -127,7 +127,7 @@
             LOG(ERROR) << "Failed to convert nan capabilities response";
             return;
         }
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyCapabilitiesResponse(id, wifiNanStatus,
                                                     hidl_struct).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
@@ -136,7 +136,7 @@
         break;
     }
     case legacy_hal::NAN_DP_INTERFACE_CREATE: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyCreateDataInterfaceResponse(id, wifiNanStatus).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
           }
@@ -144,7 +144,7 @@
         break;
     }
     case legacy_hal::NAN_DP_INTERFACE_DELETE: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyDeleteDataInterfaceResponse(id, wifiNanStatus).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
           }
@@ -152,7 +152,7 @@
         break;
     }
     case legacy_hal::NAN_DP_INITIATOR_RESPONSE: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyInitiateDataPathResponse(id, wifiNanStatus,
                 msg.body.data_request_response.ndp_instance_id).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
@@ -161,14 +161,14 @@
         break;
     }
     case legacy_hal::NAN_DP_RESPONDER_RESPONSE: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyRespondToDataPathIndicationResponse(id, wifiNanStatus).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
           }
         }
     }
     case legacy_hal::NAN_DP_END: {
-        for (const auto& callback : shared_ptr_this->event_callbacks_) {
+        for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
           if (!callback->notifyTerminateDataPathResponse(id, wifiNanStatus).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
           }
@@ -201,7 +201,7 @@
       hidl_struct.eventType = (NanClusterEventType) msg.event_type;
       hidl_struct.addr = msg.data.mac_addr.addr;
 
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         if (!callback->eventClusterEvent(hidl_struct).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
         }
@@ -219,7 +219,7 @@
       status.status = hidl_struct_util::convertLegacyNanStatusTypeToHidl(msg.reason);
       status.description = msg.nan_reason;
 
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         if (!callback->eventDisabled(status).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
         }
@@ -237,7 +237,7 @@
       status.status = hidl_struct_util::convertLegacyNanStatusTypeToHidl(msg.reason);
       status.description = msg.nan_reason;
 
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         if (!callback->eventPublishTerminated(msg.publish_id, status).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
         }
@@ -255,7 +255,7 @@
       status.status = hidl_struct_util::convertLegacyNanStatusTypeToHidl(msg.reason);
       status.description = msg.nan_reason;
 
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         if (!callback->eventSubscribeTerminated(msg.subscribe_id, status).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
         }
@@ -276,7 +276,7 @@
           return;
       }
 
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         if (!callback->eventMatch(hidl_struct).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
         }
@@ -290,7 +290,7 @@
         LOG(ERROR) << "Callback invoked on an invalid object";
         return;
       }
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         if (!callback->eventMatchExpired(msg.publish_subscribe_id,
                 msg.requestor_instance_id).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
@@ -312,7 +312,7 @@
           return;
       }
 
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         if (!callback->eventFollowupReceived(hidl_struct).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
         }
@@ -330,7 +330,7 @@
       status.status = hidl_struct_util::convertLegacyNanStatusTypeToHidl(msg.reason);
       status.description = msg.nan_reason;
 
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         if (!callback->eventTransmitFollowup(msg.id, status).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
         }
@@ -351,7 +351,7 @@
           return;
       }
 
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         if (!callback->eventDataPathRequest(hidl_struct).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
         }
@@ -372,7 +372,7 @@
           return;
       }
 
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         if (!callback->eventDataPathConfirm(hidl_struct).isOk()) {
             LOG(ERROR) << "Failed to invoke the callback";
         }
@@ -386,7 +386,7 @@
         LOG(ERROR) << "Callback invoked on an invalid object";
         return;
       }
-      for (const auto& callback : shared_ptr_this->event_callbacks_) {
+      for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
         for (int i = 0; i < msg.num_ndp_instances; ++i) {
             if (!callback->eventDataPathTerminated(msg.ndp_instance_id[i]).isOk()) {
                 LOG(ERROR) << "Failed to invoke the callback";
@@ -410,7 +410,7 @@
 
 void WifiNanIface::invalidate() {
   legacy_hal_.reset();
-  event_callbacks_.clear();
+  event_cb_handler_.invalidate();
   is_valid_ = false;
 }
 
@@ -418,6 +418,10 @@
   return is_valid_;
 }
 
+std::set<sp<IWifiNanIfaceEventCallback>> WifiNanIface::getEventCallbacks() {
+  return event_cb_handler_.getCallbacks();
+}
+
 Return<void> WifiNanIface::getName(getName_cb hidl_status_cb) {
   return validateAndCall(this,
                          WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
@@ -609,11 +613,9 @@
 
 WifiStatus WifiNanIface::registerEventCallbackInternal(
     const sp<IWifiNanIfaceEventCallback>& callback) {
-  // TODO(b/31632518): remove the callback when the client is destroyed and/or
-  // make sure that the same callback is only registered once (i.e. detect duplicates)
-  // OR: consider having a single listener - not clear why multiple listeners (managers) are
-  // necessary, nor how they would coordinate (at least command IDs).
-  event_callbacks_.emplace_back(callback);
+  if (!event_cb_handler_.addCallback(callback)) {
+    return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
+  }
   return createWifiStatus(WifiStatusCode::SUCCESS);
 }
 
diff --git a/wifi/1.0/default/wifi_nan_iface.h b/wifi/1.0/default/wifi_nan_iface.h
index d1da60e..e1edd29 100644
--- a/wifi/1.0/default/wifi_nan_iface.h
+++ b/wifi/1.0/default/wifi_nan_iface.h
@@ -21,6 +21,7 @@
 #include <android/hardware/wifi/1.0/IWifiNanIface.h>
 #include <android/hardware/wifi/1.0/IWifiNanIfaceEventCallback.h>
 
+#include "hidl_callback_util.h"
 #include "wifi_legacy_hal.h"
 
 namespace android {
@@ -119,10 +120,13 @@
   WifiStatus terminateDataPathRequestInternal(
       uint16_t cmd_id, uint32_t ndpInstanceId);
 
+  std::set<sp<IWifiNanIfaceEventCallback>> getEventCallbacks();
+
   std::string ifname_;
   std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal_;
-  std::vector<sp<IWifiNanIfaceEventCallback>> event_callbacks_;
   bool is_valid_;
+  hidl_callback_util::HidlCallbackHandler<IWifiNanIfaceEventCallback>
+      event_cb_handler_;
 
   DISALLOW_COPY_AND_ASSIGN(WifiNanIface);
 };
diff --git a/wifi/1.0/default/wifi_sta_iface.cpp b/wifi/1.0/default/wifi_sta_iface.cpp
index 6100334..55c9cf7 100644
--- a/wifi/1.0/default/wifi_sta_iface.cpp
+++ b/wifi/1.0/default/wifi_sta_iface.cpp
@@ -35,7 +35,7 @@
 
 void WifiStaIface::invalidate() {
   legacy_hal_.reset();
-  event_callbacks_.clear();
+  event_cb_handler_.invalidate();
   is_valid_ = false;
 }
 
@@ -43,8 +43,8 @@
   return is_valid_;
 }
 
-std::vector<sp<IWifiStaIfaceEventCallback>> WifiStaIface::getEventCallbacks() {
-  return event_callbacks_;
+std::set<sp<IWifiStaIfaceEventCallback>> WifiStaIface::getEventCallbacks() {
+  return event_cb_handler_.getCallbacks();
 }
 
 Return<void> WifiStaIface::getName(getName_cb hidl_status_cb) {
@@ -293,8 +293,9 @@
 
 WifiStatus WifiStaIface::registerEventCallbackInternal(
     const sp<IWifiStaIfaceEventCallback>& callback) {
-  // TODO(b/31632518): remove the callback when the client is destroyed
-  event_callbacks_.emplace_back(callback);
+  if (!event_cb_handler_.addCallback(callback)) {
+    return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
+  }
   return createWifiStatus(WifiStatusCode::SUCCESS);
 }
 
@@ -570,7 +571,8 @@
   return createWifiStatusFromLegacyError(legacy_status);
 }
 
-WifiStatus WifiStaIface::setScanningMacOuiInternal(const std::array<uint8_t, 3>& oui) {
+WifiStatus WifiStaIface::setScanningMacOuiInternal(
+    const std::array<uint8_t, 3>& oui) {
   legacy_hal::wifi_error legacy_status =
       legacy_hal_.lock()->setScanningMacOui(oui);
   return createWifiStatusFromLegacyError(legacy_status);
diff --git a/wifi/1.0/default/wifi_sta_iface.h b/wifi/1.0/default/wifi_sta_iface.h
index bc2d75f..5f0ffe9 100644
--- a/wifi/1.0/default/wifi_sta_iface.h
+++ b/wifi/1.0/default/wifi_sta_iface.h
@@ -21,6 +21,7 @@
 #include <android/hardware/wifi/1.0/IWifiStaIface.h>
 #include <android/hardware/wifi/1.0/IWifiStaIfaceEventCallback.h>
 
+#include "hidl_callback_util.h"
 #include "wifi_legacy_hal.h"
 
 namespace android {
@@ -39,7 +40,7 @@
   // Refer to |WifiChip::invalidate()|.
   void invalidate();
   bool isValid();
-  std::vector<sp<IWifiStaIfaceEventCallback>> getEventCallbacks();
+  std::set<sp<IWifiStaIfaceEventCallback>> getEventCallbacks();
 
   // HIDL methods exposed.
   Return<void> getName(getName_cb hidl_status_cb) override;
@@ -151,8 +152,9 @@
 
   std::string ifname_;
   std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal_;
-  std::vector<sp<IWifiStaIfaceEventCallback>> event_callbacks_;
   bool is_valid_;
+  hidl_callback_util::HidlCallbackHandler<IWifiStaIfaceEventCallback>
+      event_cb_handler_;
 
   DISALLOW_COPY_AND_ASSIGN(WifiStaIface);
 };
diff --git a/wifi/1.0/vts/functional/Android.bp b/wifi/1.0/vts/functional/Android.bp
index 8a5d7e0..01eeef5 100644
--- a/wifi/1.0/vts/functional/Android.bp
+++ b/wifi/1.0/vts/functional/Android.bp
@@ -15,10 +15,10 @@
 //
 
 cc_test {
-    name: "wifi_hidl_test",
+    name: "VtsHalWifiV1_0TargetTest",
     gtest: true,
     srcs: [
-        "main.cpp",
+        "VtsHalWifiV1_0TargetTest.cpp",
         "wifi_ap_iface_hidl_test.cpp",
         "wifi_chip_hidl_test.cpp",
         "wifi_hidl_call_util_selftest.cpp",
diff --git a/wifi/1.0/vts/functional/main.cpp b/wifi/1.0/vts/functional/VtsHalWifiV1_0TargetTest.cpp
similarity index 100%
rename from wifi/1.0/vts/functional/main.cpp
rename to wifi/1.0/vts/functional/VtsHalWifiV1_0TargetTest.cpp
diff --git a/wifi/supplicant/1.0/ISupplicantStaIfaceCallback.hal b/wifi/supplicant/1.0/ISupplicantStaIfaceCallback.hal
index 34237f0..dd1d1c4 100644
--- a/wifi/supplicant/1.0/ISupplicantStaIfaceCallback.hal
+++ b/wifi/supplicant/1.0/ISupplicantStaIfaceCallback.hal
@@ -214,45 +214,46 @@
    * Used to indicate the result of ANQP (either for IEEE 802.11u Interworking
    * or Hotspot 2.0) query.
    *
-   * @param macAddress MAC address of the access point.
+   * @param bssid BSSID of the access point.
    * @param data ANQP data fetched from the access point.
    *        All the fields in this struct must be empty if the query failed.
    * @param hs20Data ANQP data fetched from the Hotspot 2.0 access point.
    *        All the fields in this struct must be empty if the query failed.
    */
-  oneway onAnqpQueryDone(MacAddress macAddress,
-                         AnqpData data,
-                         Hs20AnqpData hs20Data);
+  oneway onAnqpQueryDone(Bssid bssid, AnqpData data, Hs20AnqpData hs20Data);
 
   /**
    * Used to indicate the result of Hotspot 2.0 Icon query.
    *
-   * @param macAddress MAC address of the access point.
+   * @param bssid BSSID of the access point.
    * @param fileName Name of the file that was requested.
    * @param data Icon data fetched from the access point.
    *        Must be empty if the query failed.
    */
-  oneway onHs20IconQueryDone(MacAddress macAddress,
-                             string fileName,
-                             vec<uint8_t> data);
+  oneway onHs20IconQueryDone(Bssid bssid, string fileName, vec<uint8_t> data);
 
   /**
    * Used to indicate a Hotspot 2.0 subscription remediation event.
    *
+   * @param bssid BSSID of the access point.
    * @param osuMethod OSU method.
    * @param url URL of the server.
    */
-  oneway onHs20SubscriptionRemediation(OsuMethod osuMethod, string url);
+  oneway onHs20SubscriptionRemediation(Bssid bssid,
+                                       OsuMethod osuMethod,
+                                       string url);
 
   /**
    * Used to indicate a Hotspot 2.0 imminent deauth notice.
    *
+   * @param bssid BSSID of the access point.
    * @param reasonCode Code to indicate the deauth reason.
    *        Refer to section 3.2.1.2 of the Hotspot 2.0 spec.
    * @param reAuthDelayInSec Delay before reauthenticating.
    * @param url URL of the server.
    */
-  oneway onHs20DeauthImminentNotice(uint32_t reasonCode,
+  oneway onHs20DeauthImminentNotice(Bssid bssid,
+                                    uint32_t reasonCode,
                                     uint32_t reAuthDelayInSec,
                                     string url);
 
diff --git a/wifi/supplicant/1.0/vts/functional/Android.mk b/wifi/supplicant/1.0/vts/functional/Android.mk
index 8fa649f..52fecc2 100644
--- a/wifi/supplicant/1.0/vts/functional/Android.mk
+++ b/wifi/supplicant/1.0/vts/functional/Android.mk
@@ -16,10 +16,10 @@
 LOCAL_PATH := $(call my-dir)
 
 include $(CLEAR_VARS)
-LOCAL_MODULE := supplicant_hidl_test
+LOCAL_MODULE := VtsHalWifiSupplicantV1_0TargetTest
 LOCAL_CPPFLAGS := -Wall -Werror -Wextra
 LOCAL_SRC_FILES := \
-    main.cpp \
+    VtsHalWifiSupplicantV1_0TargetTest.cpp \
     supplicant_hidl_test.cpp \
     supplicant_hidl_test_utils.cpp \
     supplicant_p2p_iface_hidl_test.cpp \
diff --git a/wifi/supplicant/1.0/vts/functional/main.cpp b/wifi/supplicant/1.0/vts/functional/VtsHalWifiSupplicantV1_0TargetTest.cpp
similarity index 100%
rename from wifi/supplicant/1.0/vts/functional/main.cpp
rename to wifi/supplicant/1.0/vts/functional/VtsHalWifiSupplicantV1_0TargetTest.cpp