Prefer fake OSI over libosi

Bug: 283886636
Test: bt_host_test_bta
Test: atest net_test_bta

Change-Id: I08f96532a9662b1f853533fb2d87e8d3b7fafa6d
diff --git a/system/bta/Android.bp b/system/bta/Android.bp
index d05fb13..af9044d 100644
--- a/system/bta/Android.bp
+++ b/system/bta/Android.bp
@@ -202,6 +202,7 @@
         ":LegacyStackSdp",
         ":TestCommonLogMsg",
         ":TestCommonMockFunctions",
+        ":TestFakeOsi",
         ":TestMockBtif",
         ":TestMockMainShim",
         ":TestMockStackBtm",
@@ -237,7 +238,6 @@
         "libchrome",
         "libcom.android.sysprop.bluetooth",
         "libgmock",
-        "libosi",
     ],
     data: [
         ":audio_set_configurations_bfbs",
@@ -302,16 +302,31 @@
         "BluetoothGeneratedPackets_h",
     ],
     srcs: [
+        ":LegacyStackSdp",
         ":OsiCompatSources",
         ":TestCommonLogMsg",
         ":TestCommonMainHandler",
         ":TestCommonMockFunctions",
+        ":TestFakeOsi",
         ":TestMockBtaSdp",
         ":TestMockBtif",
         ":TestMockDevice",
         ":TestMockMainShim",
-        ":TestMockOsi",
-        ":TestMockStack",
+        ":TestMockSrvcDis",
+        ":TestMockStackA2dp",
+        ":TestMockStackAcl",
+        ":TestMockStackAvct",
+        ":TestMockStackAvdt",
+        ":TestMockStackAvrc",
+        ":TestMockStackBtm",
+        ":TestMockStackCryptotoolbox",
+        ":TestMockStackGap",
+        ":TestMockStackGatt",
+        ":TestMockStackHid",
+        ":TestMockStackL2cap",
+        ":TestMockStackMetrics",
+        ":TestMockStackPan",
+        ":TestMockStackRfcomm",
         "ar/bta_ar.cc",
         "av/bta_av_aact.cc",
         "av/bta_av_act.cc",
diff --git a/system/bta/test/bta_dm_cust_uuid_test.cc b/system/bta/test/bta_dm_cust_uuid_test.cc
index a0cdcb5..bae16a7 100644
--- a/system/bta/test/bta_dm_cust_uuid_test.cc
+++ b/system/bta/test/bta_dm_cust_uuid_test.cc
@@ -18,15 +18,22 @@
 
 #include <gtest/gtest.h>
 
+#include <memory>
+
 #include "bta/dm/bta_dm_int.h"
 #include "stack/include/bt_hdr.h"
+#include "test/fake/fake_osi.h"
 #include "types/bluetooth/uuid.h"
 
 using bluetooth::Uuid;
 
 class BtaCustUuid : public testing::Test {
  protected:
-  void SetUp() override { bta_dm_cb = {}; }
+  void SetUp() override {
+    fake_osi_ = std::make_unique<test::fake::FakeOsi>();
+    bta_dm_cb = {};
+  }
+  std::unique_ptr<test::fake::FakeOsi> fake_osi_;
 };
 
 namespace {
diff --git a/system/bta/test/bta_dm_test.cc b/system/bta/test/bta_dm_test.cc
index c46a297..d75d090 100644
--- a/system/bta/test/bta_dm_test.cc
+++ b/system/bta/test/bta_dm_test.cc
@@ -19,6 +19,7 @@
 #include <gtest/gtest.h>
 
 #include <chrono>
+#include <memory>
 
 #include "bta/dm/bta_dm_int.h"
 #include "bta/hf_client/bta_hf_client_int.h"
@@ -31,6 +32,7 @@
 #include "stack/include/btm_status.h"
 #include "test/common/main_handler.h"
 #include "test/common/mock_functions.h"
+#include "test/fake/fake_osi.h"
 #include "test/mock/mock_osi_alarm.h"
 #include "test/mock/mock_osi_allocator.h"
 #include "test/mock/mock_stack_acl.h"
@@ -63,33 +65,12 @@
 
 }  // namespace
 
-struct alarm_t {
-  alarm_t(const char* name){};
-  int any_value;
-};
-
 class BtaDmTest : public testing::Test {
  protected:
   void SetUp() override {
     reset_mock_function_count_map();
     bluetooth::common::InitFlags::Load(test_flags);
-    test::mock::osi_alarm::alarm_new.body = [](const char* name) -> alarm_t* {
-      return new alarm_t(name);
-    };
-    test::mock::osi_alarm::alarm_free.body = [](alarm_t* alarm) {
-      delete alarm;
-    };
-    test::mock::osi_allocator::osi_malloc.body = [](size_t size) {
-      return malloc(size);
-    };
-    test::mock::osi_allocator::osi_calloc.body = [](size_t size) {
-      return calloc(1UL, size);
-    };
-    test::mock::osi_allocator::osi_free.body = [](void* ptr) { free(ptr); };
-    test::mock::osi_allocator::osi_free_and_reset.body = [](void** ptr) {
-      free(*ptr);
-      *ptr = nullptr;
-    };
+    fake_osi_ = std::make_unique<test::fake::FakeOsi>();
 
     main_thread_start_up();
     post_on_bt_main([]() { LOG_INFO("Main thread started up"); });
@@ -108,14 +89,9 @@
     bta_dm_deinit_cb();
     post_on_bt_main([]() { LOG_INFO("Main thread shutting down"); });
     main_thread_shut_down();
-
-    test::mock::osi_alarm::alarm_new = {};
-    test::mock::osi_alarm::alarm_free = {};
-    test::mock::osi_allocator::osi_malloc = {};
-    test::mock::osi_allocator::osi_calloc = {};
-    test::mock::osi_allocator::osi_free = {};
-    test::mock::osi_allocator::osi_free_and_reset = {};
   }
+
+  std::unique_ptr<test::fake::FakeOsi> fake_osi_;
 };
 
 TEST_F(BtaDmTest, nop) {
diff --git a/system/bta/test/bta_hf_client_add_record_test.cc b/system/bta/test/bta_hf_client_add_record_test.cc
index 851315f..5af4668 100644
--- a/system/bta/test/bta_hf_client_add_record_test.cc
+++ b/system/bta/test/bta_hf_client_add_record_test.cc
@@ -19,19 +19,18 @@
 #include <base/logging.h>
 #include <gtest/gtest.h>
 
+#include <memory>
+
 #include "bta/hf_client/bta_hf_client_int.h"
 #include "bta/include/bta_hf_client_api.h"
-#include "types/bluetooth/uuid.h"
-
-static uint16_t gVersion;
+#include "test/fake/fake_osi.h"
 
 class BtaHfClientAddRecordTest : public ::testing::Test {
  protected:
-  void SetUp() override {
-    gVersion = 0;
-  }
+  void SetUp() override { fake_osi_ = std::make_unique<test::fake::FakeOsi>(); }
 
   void TearDown() override {}
+  std::unique_ptr<test::fake::FakeOsi> fake_osi_;
 };
 
 TEST_F(BtaHfClientAddRecordTest, test_hf_client_add_record) {
diff --git a/system/bta/test/bta_hf_client_test.cc b/system/bta/test/bta_hf_client_test.cc
index ca45b44..d92585c 100644
--- a/system/bta/test/bta_hf_client_test.cc
+++ b/system/bta/test/bta_hf_client_test.cc
@@ -18,9 +18,12 @@
 
 #include <gtest/gtest.h>
 
+#include <memory>
+
 #include "bta/hf_client/bta_hf_client_int.h"
 #include "bta/include/bta_hf_client_api.h"
 #include "common/message_loop_thread.h"
+#include "test/fake/fake_osi.h"
 #include "types/raw_address.h"
 
 namespace base {
@@ -37,10 +40,12 @@
 class BtaHfClientTest : public testing::Test {
  protected:
   void SetUp() override {
+    fake_osi_ = std::make_unique<test::fake::FakeOsi>();
     // Reset the memory block, this is the state on which the allocate handle
     // would start operating
     bta_hf_client_cb_arr_init();
   }
+  std::unique_ptr<test::fake::FakeOsi> fake_osi_;
 };
 
 // Test that when we can allocate a device on the block and then check
diff --git a/system/bta/test/bta_sdp_test.cc b/system/bta/test/bta_sdp_test.cc
index abc10db..d9ad012 100644
--- a/system/bta/test/bta_sdp_test.cc
+++ b/system/bta/test/bta_sdp_test.cc
@@ -18,39 +18,24 @@
 #include <gtest/gtest.h>
 #include <stdarg.h>
 
+#include <memory>
 #include <string>
 
 #include "bta/dm/bta_dm_int.h"
 #include "test/common/main_handler.h"
-#include "test/mock/mock_osi_alarm.h"
-#include "test/mock/mock_osi_allocator.h"
+#include "test/fake/fake_osi.h"
 #include "test/mock/mock_stack_gatt_api.h"
 
 void BTA_dm_on_hw_on();
 void BTA_dm_on_hw_off();
 
-struct alarm_t {
-  alarm_t(const char* name){};
-  int any_value;
-};
-
 class BtaSdpTest : public testing::Test {
  protected:
   void SetUp() override {
-    test::mock::osi_allocator::osi_calloc.body = [](size_t size) -> void* {
-      return calloc(1, size);
-    };
-    test::mock::osi_allocator::osi_free.body = [](void* ptr) { free(ptr); };
-    test::mock::osi_alarm::alarm_new.body = [](const char* name) -> alarm_t* {
-      return new alarm_t(name);
-    };
-    test::mock::osi_alarm::alarm_free.body = [](alarm_t* alarm) {
-      delete alarm;
-    };
+    fake_osi_ = std::make_unique<test::fake::FakeOsi>();
     test::mock::stack_gatt_api::GATT_Register.body =
         [](const bluetooth::Uuid& p_app_uuid128, const std::string name,
            tGATT_CBACK* p_cb_info, bool eatt_support) { return 5; };
-
     main_thread_start_up();
     sync_main_handler();
 
@@ -64,11 +49,8 @@
     main_thread_shut_down();
 
     test::mock::stack_gatt_api::GATT_Register = {};
-    test::mock::osi_allocator::osi_calloc = {};
-    test::mock::osi_allocator::osi_free = {};
-    test::mock::osi_alarm::alarm_new = {};
-    test::mock::osi_alarm::alarm_free = {};
   }
+  std::unique_ptr<test::fake::FakeOsi> fake_osi_;
 };
 
 class BtaSdpRegisteredTest : public BtaSdpTest {
diff --git a/system/test/Android.bp b/system/test/Android.bp
index 32cc9d2..5656eca 100644
--- a/system/test/Android.bp
+++ b/system/test/Android.bp
@@ -206,6 +206,27 @@
 }
 
 filegroup {
+    name: "TestMockStackAvct",
+    srcs: [
+        "mock/mock_stack_avct_*.cc",
+    ],
+}
+
+filegroup {
+    name: "TestMockStackAvdt",
+    srcs: [
+        "mock/mock_stack_avdt_*.cc",
+    ],
+}
+
+filegroup {
+    name: "TestMockStackAvrc",
+    srcs: [
+        "mock/mock_stack_avrc_*.cc",
+    ],
+}
+
+filegroup {
     name: "TestMockStackL2cap",
     srcs: [
         "mock/mock_stack_l2cap_*.cc",
@@ -315,6 +336,20 @@
 }
 
 filegroup {
+    name: "TestMockStackHid",
+    srcs: [
+        "mock/mock_stack_hid*.cc",
+    ],
+}
+
+filegroup {
+    name: "TestMockStackPan",
+    srcs: [
+        "mock/mock_stack_pan*.cc",
+    ],
+}
+
+filegroup {
     name: "TestMockStackSdp",
     srcs: [
         "mock/mock_stack_sdp*.cc",