Merge "Remove viral dependency on libhwbinder. (2/2)"
diff --git a/automotive/vehicle/2.0/default/VehicleService.cpp b/automotive/vehicle/2.0/default/VehicleService.cpp
index f88ce7b..95057cc 100644
--- a/automotive/vehicle/2.0/default/VehicleService.cpp
+++ b/automotive/vehicle/2.0/default/VehicleService.cpp
@@ -34,7 +34,7 @@
     configureRpcThreadpool(1, true /* callerWillJoin */);
 
     ALOGI("Registering as service...");
-    service->registerAsService("Vehicle");
+    service->registerAsService();
 
     ALOGI("Ready");
     joinRpcThreadpool();
diff --git a/bluetooth/1.0/default/Android.bp b/bluetooth/1.0/default/Android.bp
index f82bc1f..69cefd6 100644
--- a/bluetooth/1.0/default/Android.bp
+++ b/bluetooth/1.0/default/Android.bp
@@ -34,6 +34,7 @@
     ],
     static_libs: [
         "android.hardware.bluetooth-async",
+        "android.hardware.bluetooth-hci",
     ],
 }
 
@@ -51,6 +52,21 @@
     ],
 }
 
+cc_library_static {
+    name: "android.hardware.bluetooth-hci",
+    srcs: [
+        "hci_packetizer.cc",
+    ],
+    export_include_dirs: ["."],
+    shared_libs: [
+        "libbase",
+        "libcutils",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+    ],
+}
+
 cc_test {
     name: "bluetooth-vendor-interface-unit-tests",
     srcs: [
diff --git a/bluetooth/1.0/default/hci_internals.h b/bluetooth/1.0/default/hci_internals.h
index d5714be..1e1f300 100644
--- a/bluetooth/1.0/default/hci_internals.h
+++ b/bluetooth/1.0/default/hci_internals.h
@@ -16,6 +16,8 @@
 
 #pragma once
 
+#include <stdlib.h>
+
 // HCI UART transport packet types (Volume 4, Part A, 2)
 enum HciPacketType {
   HCI_PACKET_TYPE_UNKNOWN = 0,
diff --git a/bluetooth/1.0/default/hci_packetizer.cc b/bluetooth/1.0/default/hci_packetizer.cc
new file mode 100644
index 0000000..1a50196
--- /dev/null
+++ b/bluetooth/1.0/default/hci_packetizer.cc
@@ -0,0 +1,115 @@
+//
+// Copyright 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.
+//
+
+#include "hci_packetizer.h"
+
+#define LOG_TAG "android.hardware.bluetooth.hci_packetizer"
+#include <android-base/logging.h>
+#include <cutils/properties.h>
+#include <utils/Log.h>
+
+#include <dlfcn.h>
+#include <fcntl.h>
+
+namespace {
+
+const size_t preamble_size_for_type[] = {
+    0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE,
+    HCI_EVENT_PREAMBLE_SIZE};
+const size_t packet_length_offset_for_type[] = {
+    0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO,
+    HCI_LENGTH_OFFSET_EVT};
+
+size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) {
+  size_t offset = packet_length_offset_for_type[type];
+  if (type != HCI_PACKET_TYPE_ACL_DATA) return preamble[offset];
+  return (((preamble[offset + 1]) << 8) | preamble[offset]);
+}
+
+}  // namespace
+
+namespace android {
+namespace hardware {
+namespace bluetooth {
+namespace hci {
+
+HciPacketType HciPacketizer::GetPacketType() const {
+  return hci_packet_type_;
+}
+
+const hidl_vec<uint8_t>& HciPacketizer::GetPacket() const {
+  return hci_packet_;
+}
+
+void HciPacketizer::OnDataReady(int fd) {
+  switch (hci_parser_state_) {
+    case HCI_IDLE: {
+      uint8_t buffer[1] = {0};
+      size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
+      CHECK(bytes_read == 1);
+      hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
+      CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA &&
+            hci_packet_type_ <= HCI_PACKET_TYPE_EVENT)
+          << "buffer[0] = " << static_cast<unsigned int>(buffer[0]);
+      hci_parser_state_ = HCI_TYPE_READY;
+      hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_];
+      hci_packet_bytes_read_ = 0;
+      break;
+    }
+
+    case HCI_TYPE_READY: {
+      size_t bytes_read = TEMP_FAILURE_RETRY(
+          read(fd, hci_packet_preamble_ + hci_packet_bytes_read_,
+               hci_packet_bytes_remaining_));
+      CHECK(bytes_read > 0);
+      hci_packet_bytes_remaining_ -= bytes_read;
+      hci_packet_bytes_read_ += bytes_read;
+      if (hci_packet_bytes_remaining_ == 0) {
+        size_t packet_length =
+            HciGetPacketLengthForType(hci_packet_type_, hci_packet_preamble_);
+        hci_packet_.resize(preamble_size_for_type[hci_packet_type_] +
+                           packet_length);
+        memcpy(hci_packet_.data(), hci_packet_preamble_,
+               preamble_size_for_type[hci_packet_type_]);
+        hci_packet_bytes_remaining_ = packet_length;
+        hci_parser_state_ = HCI_PAYLOAD;
+        hci_packet_bytes_read_ = 0;
+      }
+      break;
+    }
+
+    case HCI_PAYLOAD: {
+      size_t bytes_read = TEMP_FAILURE_RETRY(
+          read(fd,
+               hci_packet_.data() + preamble_size_for_type[hci_packet_type_] +
+                   hci_packet_bytes_read_,
+               hci_packet_bytes_remaining_));
+      CHECK(bytes_read > 0);
+      hci_packet_bytes_remaining_ -= bytes_read;
+      hci_packet_bytes_read_ += bytes_read;
+      if (hci_packet_bytes_remaining_ == 0) {
+        hci_packet_ready_cb_();
+        hci_parser_state_ = HCI_IDLE;
+      }
+      break;
+    }
+  }
+}
+
+}  // namespace hci
+}  // namespace bluetooth
+}  // namespace hardware
+}  // namespace android
diff --git a/bluetooth/1.0/default/hci_packetizer.h b/bluetooth/1.0/default/hci_packetizer.h
new file mode 100644
index 0000000..e9c01dc
--- /dev/null
+++ b/bluetooth/1.0/default/hci_packetizer.h
@@ -0,0 +1,54 @@
+//
+// Copyright 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.
+//
+
+#pragma once
+
+#include <functional>
+
+#include <hidl/HidlSupport.h>
+
+#include "hci_internals.h"
+
+namespace android {
+namespace hardware {
+namespace bluetooth {
+namespace hci {
+
+using ::android::hardware::hidl_vec;
+using HciPacketReadyCallback = std::function<void(void)>;
+
+class HciPacketizer {
+ public:
+  HciPacketizer(HciPacketReadyCallback packet_cb) : hci_packet_ready_cb_(packet_cb) {};
+  void OnDataReady(int fd);
+  const hidl_vec<uint8_t>& GetPacket() const;
+  HciPacketType GetPacketType() const;
+
+ protected:
+  enum HciParserState { HCI_IDLE, HCI_TYPE_READY, HCI_PAYLOAD };
+  HciParserState hci_parser_state_{HCI_IDLE};
+  HciPacketType hci_packet_type_{HCI_PACKET_TYPE_UNKNOWN};
+  uint8_t hci_packet_preamble_[HCI_PREAMBLE_SIZE_MAX];
+  hidl_vec<uint8_t> hci_packet_;
+  size_t hci_packet_bytes_remaining_;
+  size_t hci_packet_bytes_read_;
+  HciPacketReadyCallback hci_packet_ready_cb_;
+};
+
+}  // namespace hci
+}  // namespace bluetooth
+}  // namespace hardware
+}  // namespace android
diff --git a/bluetooth/1.0/default/vendor_interface.cc b/bluetooth/1.0/default/vendor_interface.cc
index 7737dd2..3878129 100644
--- a/bluetooth/1.0/default/vendor_interface.cc
+++ b/bluetooth/1.0/default/vendor_interface.cc
@@ -51,19 +51,6 @@
 
 VendorInterface* g_vendor_interface = nullptr;
 
-const size_t preamble_size_for_type[] = {
-    0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE,
-    HCI_EVENT_PREAMBLE_SIZE};
-const size_t packet_length_offset_for_type[] = {
-    0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO,
-    HCI_LENGTH_OFFSET_EVT};
-
-size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) {
-  size_t offset = packet_length_offset_for_type[type];
-  if (type != HCI_PACKET_TYPE_ACL_DATA) return preamble[offset];
-  return (((preamble[offset + 1]) << 8) | preamble[offset]);
-}
-
 HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
   size_t packet_size = data.size() + sizeof(HC_BT_HDR);
   HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
@@ -274,7 +261,7 @@
   ALOGI("%s UART fd: %d", __func__, uart_fd_);
 
   fd_watcher_.WatchFdForNonBlockingReads(uart_fd_,
-                                         [this](int fd) { OnDataReady(fd); });
+                                         [this](int fd) { hci_packetizer_.OnDataReady(fd); });
 
   // Initially, the power management is off.
   lpm_wake_deasserted = true;
@@ -370,72 +357,26 @@
   recent_activity_flag = false;
 }
 
-void VendorInterface::OnDataReady(int fd) {
-  switch (hci_parser_state_) {
-    case HCI_IDLE: {
-      uint8_t buffer[1] = {0};
-      size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
-      CHECK(bytes_read == 1);
-      hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
-      // TODO(eisenbach): Check for workaround(s)
-      CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA &&
-            hci_packet_type_ <= HCI_PACKET_TYPE_EVENT)
-          << "buffer[0] = " << static_cast<unsigned int>(buffer[0]);
-      hci_parser_state_ = HCI_TYPE_READY;
-      hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_];
-      hci_packet_bytes_read_ = 0;
-      break;
-    }
+void VendorInterface::OnPacketReady() {
+  VendorInterface::get()->HandleIncomingPacket();
+}
 
-    case HCI_TYPE_READY: {
-      size_t bytes_read = TEMP_FAILURE_RETRY(
-          read(fd, hci_packet_preamble_ + hci_packet_bytes_read_,
-               hci_packet_bytes_remaining_));
-      CHECK(bytes_read > 0);
-      hci_packet_bytes_remaining_ -= bytes_read;
-      hci_packet_bytes_read_ += bytes_read;
-      if (hci_packet_bytes_remaining_ == 0) {
-        size_t packet_length =
-            HciGetPacketLengthForType(hci_packet_type_, hci_packet_preamble_);
-        hci_packet_.resize(preamble_size_for_type[hci_packet_type_] +
-                           packet_length);
-        memcpy(hci_packet_.data(), hci_packet_preamble_,
-               preamble_size_for_type[hci_packet_type_]);
-        hci_packet_bytes_remaining_ = packet_length;
-        hci_parser_state_ = HCI_PAYLOAD;
-        hci_packet_bytes_read_ = 0;
-      }
-      break;
-    }
-
-    case HCI_PAYLOAD: {
-      size_t bytes_read = TEMP_FAILURE_RETRY(
-          read(fd,
-               hci_packet_.data() + preamble_size_for_type[hci_packet_type_] +
-                   hci_packet_bytes_read_,
-               hci_packet_bytes_remaining_));
-      CHECK(bytes_read > 0);
-      hci_packet_bytes_remaining_ -= bytes_read;
-      hci_packet_bytes_read_ += bytes_read;
-      if (hci_packet_bytes_remaining_ == 0) {
+void VendorInterface::HandleIncomingPacket() {
+  HciPacketType hci_packet_type = hci_packetizer_.GetPacketType();
+  hidl_vec<uint8_t> hci_packet = hci_packetizer_.GetPacket();
         if (internal_command.cb != nullptr &&
-            hci_packet_type_ == HCI_PACKET_TYPE_EVENT &&
-            internal_command_event_match(hci_packet_)) {
+            hci_packet_type == HCI_PACKET_TYPE_EVENT &&
+            internal_command_event_match(hci_packet)) {
           HC_BT_HDR* bt_hdr =
-              WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_);
+              WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet);
 
           // The callbacks can send new commands, so don't zero after calling.
           tINT_CMD_CBACK saved_cb = internal_command.cb;
           internal_command.cb = nullptr;
           saved_cb(bt_hdr);
         } else {
-          packet_read_cb_(hci_packet_type_, hci_packet_);
+          packet_read_cb_(hci_packet_type, hci_packet);
         }
-        hci_parser_state_ = HCI_IDLE;
-      }
-      break;
-    }
-  }
 }
 
 }  // namespace implementation
diff --git a/bluetooth/1.0/default/vendor_interface.h b/bluetooth/1.0/default/vendor_interface.h
index 98357f5..8115640 100644
--- a/bluetooth/1.0/default/vendor_interface.h
+++ b/bluetooth/1.0/default/vendor_interface.h
@@ -20,7 +20,7 @@
 
 #include "async_fd_watcher.h"
 #include "bt_vendor_lib.h"
-#include "hci_internals.h"
+#include "hci_packetizer.h"
 
 namespace android {
 namespace hardware {
@@ -46,6 +46,8 @@
 
   void OnFirmwareConfigured(uint8_t result);
 
+  static void OnPacketReady();
+
  private:
   virtual ~VendorInterface() = default;
 
@@ -55,7 +57,7 @@
 
   void OnTimeout();
 
-  void OnDataReady(int fd);
+  void HandleIncomingPacket();
 
   void *lib_handle_;
   bt_vendor_interface_t *lib_interface_;
@@ -64,13 +66,7 @@
   PacketReadCallback packet_read_cb_;
   InitializeCompleteCallback initialize_complete_cb_;
 
-  enum HciParserState { HCI_IDLE, HCI_TYPE_READY, HCI_PAYLOAD };
-  HciParserState hci_parser_state_{HCI_IDLE};
-  HciPacketType hci_packet_type_{HCI_PACKET_TYPE_UNKNOWN};
-  uint8_t hci_packet_preamble_[HCI_PREAMBLE_SIZE_MAX];
-  hidl_vec<uint8_t> hci_packet_;
-  size_t hci_packet_bytes_remaining_;
-  size_t hci_packet_bytes_read_;
+  hci::HciPacketizer hci_packetizer_ {VendorInterface::OnPacketReady};
 
   FirmwareStartupTimer *firmware_startup_timer_;
 };
diff --git a/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp b/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp
index e1e10f8..038e1dd 100644
--- a/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp
+++ b/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp
@@ -66,6 +66,7 @@
 #define MAX_VIDEO_WIDTH    4096
 #define MAX_VIDEO_HEIGHT   2160
 #define STREAM_BUFFER_TIMEOUT 3  // sec.
+#define DUMP_OUTPUT "/dev/null"
 
 struct AvailableStream {
     int32_t width;
@@ -462,9 +463,11 @@
                 });
 
             native_handle_t* raw_handle = native_handle_create(1, 0);
-            raw_handle->data[0] = 1; // std out
+            raw_handle->data[0] = open(DUMP_OUTPUT, O_RDWR);
+            ASSERT_GE(raw_handle->data[0], 0);
             hidl_handle handle = raw_handle;
             device3_2->dumpState(handle);
+            close(raw_handle->data[0]);
             native_handle_delete(raw_handle);
         }
     }
@@ -500,9 +503,11 @@
                 });
 
             native_handle_t* raw_handle = native_handle_create(1, 0);
-            raw_handle->data[0] = 1; // std out
+            raw_handle->data[0] = open(DUMP_OUTPUT, O_RDWR);
+            ASSERT_GE(raw_handle->data[0], 0);
             hidl_handle handle = raw_handle;
             device3_2->dumpState(handle);
+            close(raw_handle->data[0]);
             native_handle_delete(raw_handle);
 
             session->close();
diff --git a/dumpstate/1.0/default/service.cpp b/dumpstate/1.0/default/service.cpp
index 0d5bd94..85bea12 100644
--- a/dumpstate/1.0/default/service.cpp
+++ b/dumpstate/1.0/default/service.cpp
@@ -29,6 +29,6 @@
 int main (int /* argc */, char * /* argv */ []) {
     sp<IDumpstateDevice> dumpstate = new DumpstateDevice;
     configureRpcThreadpool(1, true);
-    dumpstate->registerAsService("dumpstate");
+    dumpstate->registerAsService();
     joinRpcThreadpool();
 }
diff --git a/gatekeeper/1.0/default/service.cpp b/gatekeeper/1.0/default/service.cpp
index 407cf71..5cbdafb 100644
--- a/gatekeeper/1.0/default/service.cpp
+++ b/gatekeeper/1.0/default/service.cpp
@@ -24,5 +24,5 @@
 using android::hardware::defaultPassthroughServiceImplementation;
 
 int main() {
-    return defaultPassthroughServiceImplementation<IGatekeeper>("gatekeeper");
+    return defaultPassthroughServiceImplementation<IGatekeeper>();
 }
diff --git a/gatekeeper/1.0/vts/functional/VtsHalGatekeeperV1_0TargetTest.cpp b/gatekeeper/1.0/vts/functional/VtsHalGatekeeperV1_0TargetTest.cpp
index 7da2293..bdc3956 100644
--- a/gatekeeper/1.0/vts/functional/VtsHalGatekeeperV1_0TargetTest.cpp
+++ b/gatekeeper/1.0/vts/functional/VtsHalGatekeeperV1_0TargetTest.cpp
@@ -187,7 +187,7 @@
   GatekeeperHidlTest() : uid_(0) {}
   virtual void SetUp() override {
     GatekeeperResponse rsp;
-    gatekeeper_ = IGatekeeper::getService("gatekeeper");
+    gatekeeper_ = IGatekeeper::getService();
     ASSERT_NE(nullptr, gatekeeper_.get());
     doDeleteAllUsers(rsp);
   }
diff --git a/gnss/1.0/default/Gnss.cpp b/gnss/1.0/default/Gnss.cpp
index 10024fb..9493737 100644
--- a/gnss/1.0/default/Gnss.cpp
+++ b/gnss/1.0/default/Gnss.cpp
@@ -676,7 +676,7 @@
     return mGnssBatching;
 }
 
-IGnss* HIDL_FETCH_IGnss(const char* hal) {
+IGnss* HIDL_FETCH_IGnss(const char* /* hal */) {
     hw_module_t* module;
     IGnss* iface = nullptr;
     int err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
@@ -687,10 +687,10 @@
         if (err == 0) {
             iface = new Gnss(reinterpret_cast<gps_device_t*>(device));
         } else {
-            ALOGE("gnssDevice open %s failed: %d", hal, err);
+            ALOGE("gnssDevice open %s failed: %d", GPS_HARDWARE_MODULE_ID, err);
         }
     } else {
-      ALOGE("gnss hw_get_module %s failed: %d", hal, err);
+      ALOGE("gnss hw_get_module %s failed: %d", GPS_HARDWARE_MODULE_ID, err);
     }
     return iface;
 }
diff --git a/gnss/1.0/default/service.cpp b/gnss/1.0/default/service.cpp
index 4e040c5..5a8acc1 100644
--- a/gnss/1.0/default/service.cpp
+++ b/gnss/1.0/default/service.cpp
@@ -8,5 +8,5 @@
 using android::hardware::defaultPassthroughServiceImplementation;
 
 int main() {
-    return defaultPassthroughServiceImplementation<IGnss>("gnss");
+    return defaultPassthroughServiceImplementation<IGnss>();
 }
diff --git a/gnss/1.0/vts/functional/VtsHalGnssV1_0TargetTest.cpp b/gnss/1.0/vts/functional/VtsHalGnssV1_0TargetTest.cpp
index f1cd9ec..3a9de9a 100644
--- a/gnss/1.0/vts/functional/VtsHalGnssV1_0TargetTest.cpp
+++ b/gnss/1.0/vts/functional/VtsHalGnssV1_0TargetTest.cpp
@@ -45,7 +45,7 @@
      * callbacks trigger.
      */
 
-    gnss_hal_ = IGnss::getService("gnss");
+    gnss_hal_ = IGnss::getService();
     ASSERT_NE(gnss_hal_, nullptr);
 
     gnss_cb_ = new GnssCallback(*this);
@@ -276,4 +276,4 @@
   int status = RUN_ALL_TESTS();
   ALOGI("Test result = %d", status);
   return status;
-}
\ No newline at end of file
+}
diff --git a/graphics/allocator/2.0/default/service.cpp b/graphics/allocator/2.0/default/service.cpp
index 525b342..a43740c 100644
--- a/graphics/allocator/2.0/default/service.cpp
+++ b/graphics/allocator/2.0/default/service.cpp
@@ -24,5 +24,5 @@
 using android::hardware::defaultPassthroughServiceImplementation;
 
 int main() {
-    return defaultPassthroughServiceImplementation<IAllocator>("gralloc");
+    return defaultPassthroughServiceImplementation<IAllocator>();
 }
diff --git a/graphics/allocator/2.0/vts/functional/VtsHalGraphicsAllocatorV2_0TargetTest.cpp b/graphics/allocator/2.0/vts/functional/VtsHalGraphicsAllocatorV2_0TargetTest.cpp
index a0443d6..d45463e 100644
--- a/graphics/allocator/2.0/vts/functional/VtsHalGraphicsAllocatorV2_0TargetTest.cpp
+++ b/graphics/allocator/2.0/vts/functional/VtsHalGraphicsAllocatorV2_0TargetTest.cpp
@@ -72,7 +72,7 @@
 class GraphicsAllocatorHidlTest : public ::testing::Test {
  protected:
   void SetUp() override {
-    mAllocator = IAllocator::getService("gralloc");
+    mAllocator = IAllocator::getService();
     ASSERT_NE(mAllocator, nullptr);
 
     mAllocator->createClient([this](const auto& error, const auto& client) {
diff --git a/graphics/composer/2.1/vts/functional/VtsHalGraphicsComposerV2_1TargetTest.cpp b/graphics/composer/2.1/vts/functional/VtsHalGraphicsComposerV2_1TargetTest.cpp
index e3e35bb..60cc2a5 100644
--- a/graphics/composer/2.1/vts/functional/VtsHalGraphicsComposerV2_1TargetTest.cpp
+++ b/graphics/composer/2.1/vts/functional/VtsHalGraphicsComposerV2_1TargetTest.cpp
@@ -749,7 +749,7 @@
 
  private:
   void SetUpGralloc() {
-    mAllocator = IAllocator::getService("gralloc");
+    mAllocator = IAllocator::getService();
     ASSERT_NE(nullptr, mAllocator.get());
 
     mAllocator->createClient([this](const auto& error, const auto& client) {
@@ -759,7 +759,7 @@
     });
     ASSERT_NE(nullptr, mAllocatorClient.get());
 
-    mMapper = IMapper::getService("gralloc-mapper");
+    mMapper = IMapper::getService();
     ASSERT_NE(nullptr, mMapper.get());
     ASSERT_FALSE(mMapper->isRemote());
   }
diff --git a/graphics/mapper/2.0/vts/functional/VtsHalGraphicsMapperV2_0TargetTest.cpp b/graphics/mapper/2.0/vts/functional/VtsHalGraphicsMapperV2_0TargetTest.cpp
index 840da1a..65ad63a 100644
--- a/graphics/mapper/2.0/vts/functional/VtsHalGraphicsMapperV2_0TargetTest.cpp
+++ b/graphics/mapper/2.0/vts/functional/VtsHalGraphicsMapperV2_0TargetTest.cpp
@@ -36,7 +36,7 @@
 class GraphicsMapperHidlTest : public ::testing::Test {
  protected:
   void SetUp() override {
-    mAllocator = IAllocator::getService("gralloc");
+    mAllocator = IAllocator::getService();
     ASSERT_NE(mAllocator, nullptr);
 
     mAllocator->createClient([this](const auto& error, const auto& client) {
@@ -46,7 +46,7 @@
     });
     ASSERT_NE(mAllocatorClient, nullptr);
 
-    mMapper = IMapper::getService("gralloc-mapper");
+    mMapper = IMapper::getService();
     ASSERT_NE(nullptr, mMapper.get());
     ASSERT_FALSE(mMapper->isRemote());
 
diff --git a/health/1.0/default/HealthService.cpp b/health/1.0/default/HealthService.cpp
index 107f33d..55848d2 100644
--- a/health/1.0/default/HealthService.cpp
+++ b/health/1.0/default/HealthService.cpp
@@ -23,5 +23,5 @@
 using android::hardware::defaultPassthroughServiceImplementation;
 
 int main() {
-    return defaultPassthroughServiceImplementation<IHealth>("health");
+    return defaultPassthroughServiceImplementation<IHealth>();
 }
diff --git a/usb/1.0/default/Usb.cpp b/usb/1.0/default/Usb.cpp
index f46ff66..6eb8842 100644
--- a/usb/1.0/default/Usb.cpp
+++ b/usb/1.0/default/Usb.cpp
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <assert.h>
 #include <dirent.h>
 #include <iostream>
 #include <fstream>
@@ -34,6 +35,9 @@
 namespace V1_0 {
 namespace implementation {
 
+// Set by the signal handler to destroy the thread
+volatile bool destroyThread;
+
 int32_t readFile(std::string filename, std::string& contents) {
     std::ifstream file(filename);
 
@@ -172,17 +176,22 @@
                 ports++;
             }
         }
+
+        if (ports == 0) {
+            closedir(dp);
+            return Status::SUCCESS;
+        }
+
         names.resize(ports);
         rewinddir(dp);
 
         while ((ep = readdir (dp))) {
-            /* Check to see if new ports were added since the first pass. */
-            if (current >= ports) {
-               rewinddir(dp);
-               goto rescan;
-            }
-
             if (ep->d_type == DT_LNK) {
+                /* Check to see if new ports were added since the first pass. */
+                if (current >= ports) {
+                    rewinddir(dp);
+                    goto rescan;
+                }
                 names[current++] = ep->d_name;
             }
         }
@@ -234,7 +243,7 @@
 
     if (result == Status::SUCCESS) {
         currentPortStatus.resize(names.size());
-        for(std::vector<std::string>::size_type i = 0; i != names.size(); i++) {
+        for(std::vector<std::string>::size_type i = 0; i < names.size(); i++) {
             ALOGI("%s", names[i].c_str());
             currentPortStatus[i].portName = names[i];
 
@@ -374,7 +383,7 @@
         goto error;
     }
 
-    while (1) {
+    while (!destroyThread) {
         struct epoll_event events[64];
 
         nevents = epoll_wait(epoll_fd, events, 64, -1);
@@ -392,6 +401,7 @@
         }
     }
 
+    ALOGI("exiting worker thread");
 error:
     close(uevent_fd);
 
@@ -401,26 +411,61 @@
     return NULL;
 }
 
+void sighandler(int sig)
+{
+    if (sig == SIGUSR1) {
+        destroyThread = true;
+        ALOGI("destroy set");
+        return;
+    }
+    signal(SIGUSR1, sighandler);
+}
 
 Return<void> Usb::setCallback(const sp<IUsbCallback>& callback) {
 
-    if (mCallback != NULL) {
-        ALOGE("Callback already registered");
+    pthread_mutex_lock(&mLock);
+    if ((mCallback == NULL && callback == NULL) ||
+            (mCallback != NULL && callback != NULL)) {
+        mCallback = callback;
+        pthread_mutex_unlock(&mLock);
         return Void();
     }
 
     mCallback = callback;
     ALOGI("registering callback");
 
-    if (pthread_create(&mPoll, NULL, work, this)) {
-        ALOGE("pthread creation failed %d", errno);
-        mCallback = NULL;
+    if (mCallback == NULL) {
+        if  (!pthread_kill(mPoll, SIGUSR1)) {
+            pthread_join(mPoll, NULL);
+            ALOGI("pthread destroyed");
+        }
+        pthread_mutex_unlock(&mLock);
         return Void();
     }
 
+    destroyThread = false;
+    signal(SIGUSR1, sighandler);
+
+    if (pthread_create(&mPoll, NULL, work, this)) {
+        ALOGE("pthread creation failed %d", errno);
+        mCallback = NULL;
+    }
+    pthread_mutex_unlock(&mLock);
     return Void();
 }
 
+// Protects *usb assignment
+pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+Usb *usb;
+
+Usb::Usb() {
+    pthread_mutex_lock(&lock);
+    // Make this a singleton class
+    assert(usb == NULL);
+    usb = this;
+    pthread_mutex_unlock(&lock);
+}
+
 }  // namespace implementation
 }  // namespace V1_0
 }  // namespace usb
diff --git a/usb/1.0/default/Usb.h b/usb/1.0/default/Usb.h
index cf418f4..c34d080 100644
--- a/usb/1.0/default/Usb.h
+++ b/usb/1.0/default/Usb.h
@@ -32,6 +32,7 @@
 using ::android::sp;
 
 struct Usb : public IUsb {
+    Usb();
     Return<void> switchRole(const hidl_string& portName, const PortRole& role) override;
     Return<void> setCallback(const sp<IUsbCallback>& callback) override;
     Return<void> queryPortStatus() override;
@@ -39,6 +40,7 @@
     sp<IUsbCallback> mCallback;
     private:
         pthread_t mPoll;
+        pthread_mutex_t mLock = PTHREAD_MUTEX_INITIALIZER;
 };
 
 }  // namespace implementation