Move start advertise method to advertising interface (3/3)

Advertising related code should be exposed through
BleAdvertiserInterface. Move client.listen function into this interface.
Remove reduntant clientif parameter.

Bug: 30622771
Bug: 24099160
Test: no tests necessary
Change-Id: Icc865c062e3d7c4b83bbeea6ff5b08bd2c1c4d9a
diff --git a/bta/gatt/bta_gattc_api.cc b/bta/gatt/bta_gattc_api.cc
index c217e76..7654091 100644
--- a/bta/gatt/bta_gattc_api.cc
+++ b/bta/gatt/bta_gattc_api.cc
@@ -718,40 +718,4 @@
     bta_sys_sendmsg(p_buf);
 }
 
-/*******************************************************************************
-**
-** Function         BTA_GATTC_Listen
-**
-** Description      Start advertisement to listen for connection request for a GATT
-**                  client application.
-**
-** Parameters       start: to start or stop listening for connection
-**
-** Returns          void
-**
-*******************************************************************************/
-void BTA_GATTC_Listen(bool start, base::Callback<void(uint8_t /* status */)> cb)
-{
-    GATT_Listen(start);
-    cb.Run(BTA_GATT_OK);
-}
-
-/*******************************************************************************
-**
-** Function         BTA_GATTC_Broadcast
-**
-** Description      Start broadcasting (non-connectable advertisements)
-**
-** Parameters       client_if: client interface.
-**                  start: to start or stop listening for connection
-**
-** Returns          void
-**
-*******************************************************************************/
-void BTA_GATTC_Broadcast(bool start, base::Callback<void(uint8_t /* status */)> cb)
-{
-    uint8_t status = BTM_BleBroadcast(start);
-    cb.Run(status);
-}
-
 #endif /* BTA_GATT_INCLUDED */
diff --git a/bta/include/bta_gatt_api.h b/bta/include/bta_gatt_api.h
index 880cd08..ffe939c 100644
--- a/bta/include/bta_gatt_api.h
+++ b/bta/include/bta_gatt_api.h
@@ -922,36 +922,6 @@
 *******************************************************************************/
 extern void BTA_GATTC_Refresh(const BD_ADDR remote_bda);
 
-
-/*******************************************************************************
-**
-** Function         BTA_GATTC_Listen
-**
-** Description      Start advertisement to listen for connection request.
-**
-** Parameters       client_if: server interface.
-**                  start: to start or stop listening for connection
-**
-** Returns          void
-**
-*******************************************************************************/
-extern void BTA_GATTC_Listen(bool start, base::Callback<void(uint8_t /* status */)> cb);
-
-/*******************************************************************************
-**
-** Function         BTA_GATTC_Broadcast
-**
-** Description      Start broadcasting (non-connectable advertisements)
-**
-** Parameters       client_if: client interface.
-**                  start: to start or stop listening for connection
-**
-** Returns          void
-**
-*******************************************************************************/
-extern void BTA_GATTC_Broadcast(bool start, base::Callback<void(uint8_t /* status */)> cb);
-
-
 /*******************************************************************************
 **
 ** Function         BTA_GATTC_ConfigureMTU
diff --git a/btif/src/btif_ble_advertiser.cc b/btif/src/btif_ble_advertiser.cc
index 2d73062..f74c6e4 100644
--- a/btif/src/btif_ble_advertiser.cc
+++ b/btif/src/btif_ble_advertiser.cc
@@ -127,6 +127,21 @@
     }
   }
 
+  void EnableCb(BleAdvertiserCb cb, uint8_t status) {
+    LOG(INFO) << __func__ << " status: " << +status;
+    do_in_jni_thread(Bind(cb, status));
+  }
+
+  void Enable(bool start, BleAdvertiserCb cb) override {
+#if (defined(BLE_PERIPHERAL_MODE_SUPPORT) && \
+     (BLE_PERIPHERAL_MODE_SUPPORT == true))
+    do_in_jni_thread(Bind(&GATT_Listen, start));
+#else
+    do_in_jni_thread(Bind(&BTM_BleBroadcast, start));
+#endif
+    cb.Run(BT_STATUS_SUCCESS);
+  }
+
   void MultiAdvSetParametersCb(BleAdvertiserCb cb, uint8_t status) {
     LOG(INFO) << __func__ << " status: " << +status ;
     do_in_jni_thread(Bind(cb, status));
diff --git a/btif/src/btif_gatt_client.cc b/btif/src/btif_gatt_client.cc
index d9d064b..3c49656 100644
--- a/btif/src/btif_gatt_client.cc
+++ b/btif/src/btif_gatt_client.cc
@@ -328,21 +328,6 @@
       Bind(&btif_gattc_close_impl, client_if, base::Owned(address), conn_id));
 }
 
-void btif_gattc_listen_cb(int client_if, uint8_t status)
-{
-  HAL_CBACK(bt_gatt_callbacks, client->listen_cb, status, client_if);
-}
-
-bt_status_t btif_gattc_listen(int client_if, bool start) {
-  CHECK_BTGATT_INIT();
-#if (defined(BLE_PERIPHERAL_MODE_SUPPORT) && \
-     (BLE_PERIPHERAL_MODE_SUPPORT == true))
-  return do_in_jni_thread(Bind(&BTA_GATTC_Listen, start, base::Bind(&btif_gattc_listen_cb, client_if)));
-#else
-  return do_in_jni_thread(Bind(&BTA_GATTC_Broadcast, start, base::Bind(&btif_gattc_listen_cb, client_if)));
-#endif
-}
-
 bt_status_t btif_gattc_refresh(int client_if, const bt_bdaddr_t* bd_addr) {
   CHECK_BTGATT_INIT();
   // Closure will own this value and free it.
@@ -561,7 +546,6 @@
     btif_gattc_unregister_app,
     btif_gattc_open,
     btif_gattc_close,
-    btif_gattc_listen,
     btif_gattc_refresh,
     btif_gattc_search_service,
     btif_gattc_read_char,
diff --git a/service/gatt_server_old.cc b/service/gatt_server_old.cc
index 22a4a62..ceda435 100644
--- a/service/gatt_server_old.cc
+++ b/service/gatt_server_old.cc
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#include <base/bind.h>
 #include <algorithm>
 #include <array>
 #include <condition_variable>
@@ -335,6 +336,13 @@
   }
 }
 
+void EnableAdvertisingCallback(uint8_t status) {
+  LOG_INFO(LOG_TAG, "%s: status:%d", __func__, status);
+  // This terminates a Start call.
+  std::lock_guard<std::mutex> lock(g_internal->lock);
+  g_internal->api_synchronize.notify_one();
+}
+
 void RegisterClientCallback(int status, int client_if, bt_uuid_t *app_uuid) {
   LOG_INFO(LOG_TAG, "%s: status:%d client_if:%d uuid[0]:%u", __func__, status,
       client_if, app_uuid->uu[0]);
@@ -344,11 +352,9 @@
   g_internal->gatt->advertiser->SetData(false, {/*TODO: put inverval 2,2 here*/});
 
   // TODO(icoolidge): Deprecated, use multi-adv interface.
-  // This calls back to ListenCallback.
-  bt_status_t btstat = g_internal->gatt->client->listen(client_if, true);
-  if (btstat != BT_STATUS_SUCCESS) {
-    LOG_ERROR(LOG_TAG, "Failed to start listening");
-  }
+  // This calls back to EnableAdvertisingCallback.
+  g_internal->gatt->advertiser->Enable(true,
+                                       base::Bind(&EnableAdvertisingCallback));
 }
 
 void RegisterScannerCallback(int status, int scanner_id, bt_uuid_t *app_uuid) {
@@ -356,13 +362,6 @@
       scanner_id, app_uuid->uu[0]);
 }
 
-void ListenCallback(int status, int client_if) {
-  LOG_INFO(LOG_TAG, "%s: status:%d client_if:%d", __func__, status, client_if);
-  // This terminates a Start call.
-  std::lock_guard<std::mutex> lock(g_internal->lock);
-  g_internal->api_synchronize.notify_one();
-}
-
 void ServiceStoppedCallback(int status, int server_if, int srvc_handle) {
   LOG_INFO(LOG_TAG, "%s: status:%d server_if:%d srvc_handle:%d", __func__,
       status, server_if, srvc_handle);
@@ -435,7 +434,6 @@
     nullptr, /* write_descriptor_cb; */
     nullptr, /* execute_write_cb; */
     nullptr, /* read_remote_rssi_cb; */
-    ListenCallback,
     nullptr, /* configure_mtu_cb; */
     nullptr, /* congestion_cb; */
     nullptr, /* get_gatt_db_cb; */
diff --git a/service/hal/bluetooth_gatt_interface.cc b/service/hal/bluetooth_gatt_interface.cc
index 9dc2151..f7ddecb 100644
--- a/service/hal/bluetooth_gatt_interface.cc
+++ b/service/hal/bluetooth_gatt_interface.cc
@@ -194,14 +194,6 @@
     WriteDescriptorCallback(g_interface, conn_id, status, handle));
 }
 
-void ListenCallback(int status, int client_if) {
-  shared_lock<shared_timed_mutex> lock(g_instance_lock);
-  VLOG(2) << __func__ << " - status: " << status << " client_if: " << client_if;
-  VERIFY_INTERFACE_OR_RETURN();
-
-  FOR_EACH_CLIENT_OBSERVER(ListenCallback(g_interface, status, client_if));
-}
-
 void MtuChangedCallback(int conn_id, int status, int mtu) {
   shared_lock<shared_timed_mutex> lock(g_instance_lock);
   VERIFY_INTERFACE_OR_RETURN();
@@ -429,7 +421,6 @@
     WriteDescriptorCallback,
     nullptr,  // execute_write_cb
     nullptr,  // read_remote_rssi_cb
-    ListenCallback,
     MtuChangedCallback,
     nullptr,  // congestion_cb
     GetGattDbCallback,
@@ -676,13 +667,6 @@
   // Do nothing
 }
 
-void BluetoothGattInterface::ClientObserver::ListenCallback(
-    BluetoothGattInterface* /* gatt_iface */,
-    int /* status */,
-    int /* client_if */) {
-  // Do nothing.
-}
-
 void BluetoothGattInterface::ClientObserver::MtuChangedCallback(
     BluetoothGattInterface* /* gatt_iface */,
     int /* conn_id */,
diff --git a/service/hal/bluetooth_gatt_interface.h b/service/hal/bluetooth_gatt_interface.h
index 6cb4e34..3b7c297 100644
--- a/service/hal/bluetooth_gatt_interface.h
+++ b/service/hal/bluetooth_gatt_interface.h
@@ -114,10 +114,6 @@
         BluetoothGattInterface* gatt_iface,
         int conn_id, int status, uint16_t handle);
 
-    virtual void ListenCallback(
-        BluetoothGattInterface* gatt_iface,
-        int status, int client_if);
-
     virtual void MtuChangedCallback(
         BluetoothGattInterface* gatt_iface,
         int conn_id, int status, int mtu);
diff --git a/service/hal/fake_bluetooth_gatt_interface.cc b/service/hal/fake_bluetooth_gatt_interface.cc
index 56b7514..dbbd533 100644
--- a/service/hal/fake_bluetooth_gatt_interface.cc
+++ b/service/hal/fake_bluetooth_gatt_interface.cc
@@ -146,7 +146,6 @@
   FakeUnregisterClient,
   FakeConnect,
   FakeDisconnect,
-  nullptr,  // listen
   nullptr,  // refresh
   nullptr,  // search_service
   nullptr,  // read_characteristic
diff --git a/service/test/low_energy_advertiser_unittest.cc b/service/test/low_energy_advertiser_unittest.cc
index 4befebc..13c4eed 100644
--- a/service/test/low_energy_advertiser_unittest.cc
+++ b/service/test/low_energy_advertiser_unittest.cc
@@ -51,6 +51,7 @@
   MOCK_METHOD1(Unregister, void(uint8_t));
 
   MOCK_METHOD2(SetData, void(bool set_scan_rsp, vector<uint8_t> data));
+  MOCK_METHOD2(Enable, void(bool enablle, BleAdvertiserCb cb));
   MOCK_METHOD7(MultiAdvSetParameters,
                void(int advertiser_id, int min_interval, int max_interval,
                     int adv_type, int chnl_map, int tx_power,
diff --git a/stack/include/ble_advertiser.h b/stack/include/ble_advertiser.h
index 4ef2ff2..af7e90e 100644
--- a/stack/include/ble_advertiser.h
+++ b/stack/include/ble_advertiser.h
@@ -38,6 +38,8 @@
                                   const uint16_t disc_mode);
 void btm_gen_resolvable_private_addr(void *p_cmd_cplt_cback);
 void btm_acl_update_conn_addr(uint8_t conn_handle, BD_ADDR address);
+void GATT_Listen(bool start);
+tBTM_STATUS BTM_BleBroadcast(bool start);
 
 // methods we expose to c code:
 void btm_ble_multi_adv_cleanup(void);
diff --git a/test/suite/gatt/gatt_test.cc b/test/suite/gatt/gatt_test.cc
index 16f53ed..50791f1 100644
--- a/test/suite/gatt/gatt_test.cc
+++ b/test/suite/gatt/gatt_test.cc
@@ -40,7 +40,6 @@
 
   register_client_callback_sem_ = semaphore_new(0);
   scan_result_callback_sem_ = semaphore_new(0);
-  listen_callback_sem_ = semaphore_new(0);
 
   register_server_callback_sem_ = semaphore_new(0);
   service_added_callback_sem_ = semaphore_new(0);
@@ -66,7 +65,6 @@
 
   semaphore_free(register_client_callback_sem_);
   semaphore_free(scan_result_callback_sem_);
-  semaphore_free(listen_callback_sem_);
 
   semaphore_free(register_server_callback_sem_);
   semaphore_free(service_added_callback_sem_);
@@ -106,14 +104,6 @@
   semaphore_post(scan_result_callback_sem_);
 }
 
-void GattTest::ListenCallback(
-    bluetooth::hal::BluetoothGattInterface* /* unused */,
-    int status, int client_if) {
-  status_ = status;
-  client_interface_id_ = client_if;
-  semaphore_post(listen_callback_sem_);
-}
-
 // GATT server callbacks
 void GattTest::RegisterServerCallback(
     bluetooth::hal::BluetoothGattInterface* /* unused */,
diff --git a/test/suite/gatt/gatt_test.h b/test/suite/gatt/gatt_test.h
index 57e54f0..b9bdd08 100644
--- a/test/suite/gatt/gatt_test.h
+++ b/test/suite/gatt/gatt_test.h
@@ -63,9 +63,6 @@
   void ScanResultCallback(
       bluetooth::hal::BluetoothGattInterface* /* unused */,
       const bt_bdaddr_t& bda, int rssi, vector<uint8_t> adv_data) override;
-  void ListenCallback(
-      bluetooth::hal::BluetoothGattInterface* /* unused */,
-      int status, int client_if) override;
 
   // bluetooth::hal::BluetoothGattInterface::ServerObserver overrides
   void RegisterServerCallback(
diff --git a/test/suite/gatt/gatt_unittest.cc b/test/suite/gatt/gatt_unittest.cc
index 2f66fff..64435a3 100644
--- a/test/suite/gatt/gatt_unittest.cc
+++ b/test/suite/gatt/gatt_unittest.cc
@@ -50,31 +50,6 @@
   gatt_client_interface()->unregister_client(client_interface_id());
 }
 
-TEST_F(GattTest, GattClientAdvertise) {
-  // Registers a new client app.
-  bt_uuid_t gatt_client_uuid;
-  create_random_uuid(&gatt_client_uuid, DEFAULT_RANDOM_SEED);
-  gatt_client_interface()->register_client(&gatt_client_uuid);
-  semaphore_wait(register_client_callback_sem_);
-  EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
-    << "Error registering GATT client app callback.";
-
-  // Starts advertising.
-  gatt_client_interface()->listen(client_interface_id(), true);
-  semaphore_wait(listen_callback_sem_);
-  EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
-    << "Error starting BLE advertisement.";
-
-  // Stops advertising.
-  gatt_client_interface()->listen(client_interface_id(), false);
-  semaphore_wait(listen_callback_sem_);
-  EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
-    << "Error stopping BLE advertisement.";
-
-  // Unregisters gatt server. No callback is expected.
-  gatt_client_interface()->unregister_client(client_interface_id());
-}
-
 TEST_F(GattTest, GattServerRegister) {
   // Registers gatt server.
   bt_uuid_t gatt_server_uuid;