From 0769e709f0ebc603318d9e81d49286976a384df5 Mon Sep 17 00:00:00 2001
From: Hansong Zhang <hsz@google.com>
Date: Mon, 1 Mar 2021 17:52:04 -0800
Subject: [PATCH] Add host support for libbluetooth.so

Bug: 181590011
Tag: #refactor
Test: compile
Change-Id: I9a53eeaac5a5c612d47671bca4f95cf252faeb93
---
 system/btif/Android.bp                      |  4 +--
 system/btif/src/btif_debug_btsnoop.cc       | 27 ++++++++++++---------
 system/gd/btaa/host/activity_attribution.cc |  2 ++
 system/main/Android.bp                      | 22 +++++++++++------
 4 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/system/btif/Android.bp b/system/btif/Android.bp
index 24ef39c4e64..5095311745f 100755
--- a/system/btif/Android.bp
+++ b/system/btif/Android.bp
@@ -118,9 +118,6 @@ cc_library_static {
                 "libaaudio",
                 "libfmq",
             ],
-            whole_static_libs: [
-                "libbt-audio-hal-interface",
-            ],
             srcs: ["src/btif_avrcp_audio_track.cc"],
         },
         host: {
@@ -130,6 +127,7 @@ cc_library_static {
     whole_static_libs: [
         "avrcp-target-service",
         "lib-bt-packets",
+        "libbt-audio-hal-interface",
         "libaudio-a2dp-hw-utils",
     ],
     cflags: [
diff --git a/system/btif/src/btif_debug_btsnoop.cc b/system/btif/src/btif_debug_btsnoop.cc
index e72a87519ba..5ebfdae7bd5 100644
--- a/system/btif/src/btif_debug_btsnoop.cc
+++ b/system/btif/src/btif_debug_btsnoop.cc
@@ -35,12 +35,6 @@
 static const size_t BTSNOOP_MEM_BUFFER_SIZE = (256 * 1024);
 #endif
 
-// Block size for copying buffers (for compression/encoding etc.)
-static const size_t BLOCK_SIZE = 16384;
-
-// Maximum line length in bugreport (should be multiple of 4 for base64 output)
-static const uint8_t MAX_LINE_LENGTH = 128;
-
 static std::mutex buffer_mutex;
 static ringbuffer_t* buffer = NULL;
 static uint64_t last_timestamp_ms = 0;
@@ -134,6 +128,18 @@ static size_t btsnoop_calculate_packet_length(uint16_t type,
   }
 }
 
+void btif_debug_btsnoop_init(void) {
+  if (buffer == NULL) buffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
+  btsnoop_mem_set_callback(btsnoop_cb);
+}
+
+#ifndef OS_ANDROID
+void btif_debug_btsnoop_dump(int fd) {}
+#else
+
+// Block size for copying buffers (for compression/encoding etc.)
+static constexpr size_t BLOCK_SIZE = 16384;
+
 static bool btsnoop_compress(ringbuffer_t* rb_dst, ringbuffer_t* rb_src) {
   CHECK(rb_dst != NULL);
   CHECK(rb_src != NULL);
@@ -175,11 +181,6 @@ static bool btsnoop_compress(ringbuffer_t* rb_dst, ringbuffer_t* rb_src) {
   return rc;
 }
 
-void btif_debug_btsnoop_init(void) {
-  if (buffer == NULL) buffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
-  btsnoop_mem_set_callback(btsnoop_cb);
-}
-
 void btif_debug_btsnoop_dump(int fd) {
   ringbuffer_t* ringbuffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
   if (ringbuffer == NULL) {
@@ -219,6 +220,9 @@ void btif_debug_btsnoop_dump(int fd) {
 
   while (ringbuffer_size(ringbuffer) > 0) {
     size_t read = ringbuffer_pop(ringbuffer, b64_in, 3);
+    // Maximum line length in bugreport (should be multiple of 4 for base64
+    // output)
+    constexpr uint8_t MAX_LINE_LENGTH = 128;
     if (line_length >= MAX_LINE_LENGTH) {
       dprintf(fd, "\n");
       line_length = 0;
@@ -232,3 +236,4 @@ void btif_debug_btsnoop_dump(int fd) {
 error:
   ringbuffer_free(ringbuffer);
 }
+#endif
diff --git a/system/gd/btaa/host/activity_attribution.cc b/system/gd/btaa/host/activity_attribution.cc
index a29aaacdbfe..e9f41344007 100644
--- a/system/gd/btaa/host/activity_attribution.cc
+++ b/system/gd/btaa/host/activity_attribution.cc
@@ -36,5 +36,7 @@ void ActivityAttribution::Start() {}
 
 void ActivityAttribution::Stop() {}
 
+const ModuleFactory ActivityAttribution::Factory = ModuleFactory([]() { return new ActivityAttribution(); });
+
 }  // namespace activity_attribution
 }  // namespace bluetooth
diff --git a/system/main/Android.bp b/system/main/Android.bp
index b9fa910447d..864202837af 100644
--- a/system/main/Android.bp
+++ b/system/main/Android.bp
@@ -99,14 +99,10 @@ cc_library_shared {
         "android.hardware.bluetooth.audio@2.1",
         "android.hardware.bluetooth@1.0",
         "android.hardware.bluetooth@1.1",
-        "android.system.suspend.control-V1-ndk",
-        "libaaudio",
         "libbinder_ndk",
         "libcrypto",
         "libcutils",
-        "libdl",
         "libflatbuffers-cpp",
-        "libfmq",
         "libhidlbase",
         "liblog",
         "libprocessgroup",
@@ -137,6 +133,19 @@ cc_library_shared {
         "libosi",
         "libbt-protos-lite",
     ],
+    target: {
+        android: {
+            shared_libs: [
+                "android.system.suspend.control-V1-ndk",
+                "libaaudio",
+                "libfmq",
+            ],
+            required: [
+                "libldacBT_enc",
+                "libldacBT_abr",
+            ],
+        },
+    },
     // Shared library link options.
     // References to global symbols and functions should bind to the library
     // itself. This is to avoid issues with some of the unit/system tests
@@ -146,15 +155,14 @@ cc_library_shared {
     required: [
         "bt_did.conf",
         "bt_stack.conf",
-        "libldacBT_enc",
-        "libldacBT_abr",
     ],
     cflags: [
         "-DBUILDCFG",
     ],
     sanitize: {
-        scs: true,
+        never: true,
     },
+    host_supported: true,
 }
 
 cc_library_static {
-- 
GitLab