Implement dumpsys for BufferHubBinderService

Now you can run "adb shell dumpsys bufferhubd" to get information from
the bufferhubd binder service. Information from BufferHubService, which
is the PDX service, will also be dumped. If the PDX service is not
present, a message will indicate that.

Note that currently the binder service has no valuable info to share, so
this command actually only return the PDX service info.

Renaming the variable name in bufferhubd.cpp is for preventing
confusion. As during migrating we will have the binder service and pdx
service at the same time, distinguishing them is necessary while both of
them are called "bufferhubd service".

Adjust some newlines to meet 80-char column limit.

Test: "adb shell dumpsys bufferhubd"
Should out put the same as command
"adb shell pdx_tool --dump /dev/socket/pdx/system/buffer_hub/client"

Change-Id: Ie1386cde39ed973051734e63768216f1345a1945
Fixes: 115435506
diff --git a/services/vr/bufferhubd/buffer_hub_binder.cpp b/services/vr/bufferhubd/buffer_hub_binder.cpp
index def15f1..4ff9890 100644
--- a/services/vr/bufferhubd/buffer_hub_binder.cpp
+++ b/services/vr/bufferhubd/buffer_hub_binder.cpp
@@ -1,28 +1,58 @@
 #include <stdio.h>
 
+#include <binder/IServiceManager.h>
+#include <binder/IPCThreadState.h>
+#include <binder/ProcessState.h>
 #include <log/log.h>
 #include <private/dvr/buffer_hub_binder.h>
 
 namespace android {
 namespace dvr {
 
-status_t BufferHubBinderService::start() {
-  ProcessState::self()->startThreadPool();
+status_t BufferHubBinderService::start(
+    const std::shared_ptr<BufferHubService> &pdx_service) {
   IPCThreadState::self()->disableBackgroundScheduling(true);
-  status_t result = BinderService<BufferHubBinderService>::publish();
-  if (result != OK) {
+
+  BufferHubBinderService* service = new BufferHubBinderService();
+  service->pdx_service_ = pdx_service;
+
+  // Not using BinderService::publish because need to get an instance of this
+  // class (above). Following code is the same as
+  // BinderService::publishAndJoinThreadPool
+  sp<IServiceManager> sm = defaultServiceManager();
+  status_t result = sm->addService(String16(getServiceName()), service,
+      /*allowIsolated =*/ false,
+      /*dump flags =*/ IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
+  if (result != NO_ERROR) {
     ALOGE("Publishing bufferhubd failed with error %d", result);
     return result;
   }
 
+  sp<ProcessState> process_self(ProcessState::self());
+  process_self->startThreadPool();
+  process_self->giveThreadPoolName();
+  IPCThreadState::self()->joinThreadPool();
+
   return result;
 }
 
-status_t BufferHubBinderService::dump(int fd, const Vector<String16> & /* args */) {
-  // TODO(b/115435506): not implemented yet
+status_t BufferHubBinderService::dump(int fd, const Vector<String16> &args) {
   FILE *out = fdopen(dup(fd), "w");
 
-  fprintf(out, "BufferHubBinderService::dump(): Not Implemented.\n");
+  // Currently not supporting args, so notify the user.
+  if (!args.isEmpty()){
+    fprintf(out, "Note: dumpsys bufferhubd currently does not support args."
+        "Input arguments are ignored.\n");
+  }
+
+  // TODO(b/116526156): output real data in this class once we have it
+  if (pdx_service_){
+    // BufferHubService::Dumpstate(size_t) is not actually using the param
+    // So just using 0 as the length
+    fprintf(out, "%s", pdx_service_->DumpState(0).c_str());
+  } else {
+    fprintf(out, "PDX service not registered or died.\n");
+  }
 
   fclose(out);
   return NO_ERROR;
@@ -30,4 +60,4 @@
 
 
 }  // namespace dvr
-}  // namespace android
\ No newline at end of file
+}  // namespace android
diff --git a/services/vr/bufferhubd/bufferhubd.cpp b/services/vr/bufferhubd/bufferhubd.cpp
index 0ca7edc..3e10b26 100644
--- a/services/vr/bufferhubd/bufferhubd.cpp
+++ b/services/vr/bufferhubd/bufferhubd.cpp
@@ -10,7 +10,7 @@
 
 int main(int, char**) {
   int ret = -1;
-  std::shared_ptr<android::pdx::Service> service;
+  std::shared_ptr<android::dvr::BufferHubService> pdx_service;
   std::unique_ptr<android::pdx::ServiceDispatcher> dispatcher;
 
   // We need to be able to create endpoints with full perms.
@@ -33,15 +33,16 @@
   else
     ALOGI("New nofile limit is %llu/%llu.", rlim.rlim_cur, rlim.rlim_max);
 
-  CHECK_ERROR(android::dvr::BufferHubBinderService::start() != android::OK,
-              error, "Failed to create bufferhub binder service\n");
-
   dispatcher = android::pdx::ServiceDispatcher::Create();
   CHECK_ERROR(!dispatcher, error, "Failed to create service dispatcher\n");
 
-  service = android::dvr::BufferHubService::Create();
-  CHECK_ERROR(!service, error, "Failed to create bufferhubd service\n");
-  dispatcher->AddService(service);
+  pdx_service = android::dvr::BufferHubService::Create();
+  CHECK_ERROR(!pdx_service, error, "Failed to create bufferhub pdx service\n");
+  dispatcher->AddService(pdx_service);
+
+  ret = android::dvr::BufferHubBinderService::start(pdx_service);
+  CHECK_ERROR(ret != android::NO_ERROR, error,
+              "Failed to create bufferhub binder service\n");
 
   ret = dvrSetSchedulerClass(0, "graphics");
   CHECK_ERROR(ret < 0, error, "Failed to set thread priority");
diff --git a/services/vr/bufferhubd/include/private/dvr/buffer_hub_binder.h b/services/vr/bufferhubd/include/private/dvr/buffer_hub_binder.h
index c0281fd..07f05cc 100644
--- a/services/vr/bufferhubd/include/private/dvr/buffer_hub_binder.h
+++ b/services/vr/bufferhubd/include/private/dvr/buffer_hub_binder.h
@@ -2,6 +2,7 @@
 #define ANDROID_DVR_BUFFER_HUB_BINDER_H
 
 #include <binder/BinderService.h>
+#include <private/dvr/buffer_hub.h>
 
 #include "android/dvr/BnBufferHub.h"
 
@@ -10,14 +11,17 @@
 
 class BufferHubBinderService : public BinderService<BufferHubBinderService>, public BnBufferHub {
  public:
-  static status_t start();
+  static status_t start(const std::shared_ptr<BufferHubService> &pdx_service);
   static const char* getServiceName() { return "bufferhubd"; }
   // Dump bufferhub related information to given fd (usually stdout)
   // usage: adb shell dumpsys bufferhubd
   virtual status_t dump(int fd, const Vector<String16> &args) override;
+
+ private:
+  std::shared_ptr<BufferHubService> pdx_service_;
 };
 
 }  // namespace dvr
 }  // namespace android
 
-#endif // ANDROID_DVR_BUFFER_HUB_BINDER_H
\ No newline at end of file
+#endif // ANDROID_DVR_BUFFER_HUB_BINDER_H
diff --git a/services/vr/bufferhubd/tests/Android.bp b/services/vr/bufferhubd/tests/Android.bp
index 4d1d43f..92bda78 100644
--- a/services/vr/bufferhubd/tests/Android.bp
+++ b/services/vr/bufferhubd/tests/Android.bp
@@ -6,11 +6,18 @@
         "-DTRACE=0",
         "-DATRACE_TAG=ATRACE_TAG_GRAPHICS",
     ],
-    static_libs: ["libbufferhubd"],
+    header_libs: ["libdvr_headers"],
+    static_libs: [
+        "libbufferhub",
+        "libbufferhubd",
+        "libgmock",
+    ],
     shared_libs: [
         "libbase",
         "libbinder",
         "liblog",
+        "libpdx_default_transport",
+        "libui",
         "libutils",
     ],
 }
\ No newline at end of file
diff --git a/services/vr/bufferhubd/tests/buffer_hub_binder_service-test.cpp b/services/vr/bufferhubd/tests/buffer_hub_binder_service-test.cpp
index e3f2825..0b64cf0 100644
--- a/services/vr/bufferhubd/tests/buffer_hub_binder_service-test.cpp
+++ b/services/vr/bufferhubd/tests/buffer_hub_binder_service-test.cpp
@@ -1,6 +1,7 @@
-#include <private/dvr/buffer_hub_binder.h>
-
+#include <binder/IServiceManager.h>
+#include <gmock/gmock.h>
 #include <gtest/gtest.h>
+#include <private/dvr/buffer_hub_binder.h>
 
 namespace android {
 namespace dvr {
@@ -12,8 +13,12 @@
 };
 
 TEST_F(BufferHubBinderServiceTest, TestInitialize) {
-  // Test if start binder server returns OK
-  EXPECT_EQ(BufferHubBinderService::start(), OK);
+  // Create a new service will kill the current one.
+  // So just check if Binder service is running
+  sp<IServiceManager> sm = defaultServiceManager();
+  sp<IBinder> service = sm->checkService(
+      String16(BufferHubBinderService::getServiceName()));
+  EXPECT_THAT(service, ::testing::Ne(nullptr));
 }
 
 }  // namespace