BTA Application registration refactor
am: 221e9bf6c9

Change-Id: I731c9a69bdd9513ee95e77b28e1308049c3fede0
diff --git a/bta/dm/bta_dm_act.cc b/bta/dm/bta_dm_act.cc
index aad4c7f..c6268be 100644
--- a/bta/dm/bta_dm_act.cc
+++ b/bta/dm/bta_dm_act.cc
@@ -26,6 +26,8 @@
 #define LOG_TAG "bt_bta_dm"
 
 #include <assert.h>
+#include <base/bind.h>
+#include <base/callback.h>
 #include <string.h>
 
 #include "bt_common.h"
@@ -4749,11 +4751,15 @@
  *
  ******************************************************************************/
 static void bta_dm_gattc_register(void) {
-  tBT_UUID app_uuid = {LEN_UUID_128, {0}};
-
   if (bta_dm_search_cb.client_if == BTA_GATTS_INVALID_IF) {
-    memset(&app_uuid.uu.uuid128, 0x87, LEN_UUID_128);
-    BTA_GATTC_AppRegister(&app_uuid, bta_dm_gattc_callback);
+    BTA_GATTC_AppRegister(bta_dm_gattc_callback,
+                          base::Bind([](uint8_t client_id, uint8_t status) {
+                            if (status == BTA_GATT_OK)
+                              bta_dm_search_cb.client_if = client_id;
+                            else
+                              bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;
+
+                          }));
   }
 }
 
@@ -4900,7 +4906,8 @@
  *
  * Function         bta_dm_close_gatt_conn
  *
- * Description      This function close the GATT connection after delay timeout.
+ * Description      This function close the GATT connection after delay
+ *timeout.
  *
  * Parameters:
  *
@@ -5004,15 +5011,6 @@
   APPL_TRACE_DEBUG("bta_dm_gattc_callback event = %d", event);
 
   switch (event) {
-    case BTA_GATTC_REG_EVT:
-      APPL_TRACE_DEBUG("BTA_GATTC_REG_EVT client_if = %d",
-                       p_data->reg_oper.client_if);
-      if (p_data->reg_oper.status == BTA_GATT_OK)
-        bta_dm_search_cb.client_if = p_data->reg_oper.client_if;
-      else
-        bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;
-      break;
-
     case BTA_GATTC_OPEN_EVT:
       bta_dm_proc_open_evt(&p_data->open);
       break;
diff --git a/bta/gatt/bta_gattc_act.cc b/bta/gatt/bta_gattc_act.cc
index 306fe27..62a473f 100644
--- a/bta/gatt/bta_gattc_act.cc
+++ b/bta/gatt/bta_gattc_act.cc
@@ -27,6 +27,7 @@
 
 #include <string.h>
 
+#include <base/callback.h>
 #include "bt_common.h"
 #include "bt_target.h"
 #include "bta_gattc_int.h"
@@ -157,22 +158,18 @@
  * Returns          void
  *
  ******************************************************************************/
-void bta_gattc_register(tBTA_GATTC_DATA* p_data) {
-  tBTA_GATTC cb_data;
-  uint8_t i;
-  tBT_UUID* p_app_uuid = &p_data->api_reg.app_uuid;
+void bta_gattc_register(tBT_UUID* p_app_uuid, tBTA_GATTC_CBACK* p_cback,
+                        BtaAppRegisterCallback cb) {
   tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
-
+  uint8_t client_if = 0;
   APPL_TRACE_DEBUG("bta_gattc_register state %d", bta_gattc_cb.state);
-  memset(&cb_data, 0, sizeof(cb_data));
-  cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES;
 
   /* check if  GATTC module is already enabled . Else enable */
   if (bta_gattc_cb.state == BTA_GATTC_STATE_DISABLED) {
     bta_gattc_enable();
   }
   /* todo need to check duplicate uuid */
-  for (i = 0; i < BTA_GATTC_CL_MAX; i++) {
+  for (uint8_t i = 0; i < BTA_GATTC_CL_MAX; i++) {
     if (!bta_gattc_cb.cl_rcb[i].in_use) {
       if ((p_app_uuid == NULL) ||
           (bta_gattc_cb.cl_rcb[i].client_if =
@@ -181,11 +178,11 @@
         status = BTA_GATT_ERROR;
       } else {
         bta_gattc_cb.cl_rcb[i].in_use = true;
-        bta_gattc_cb.cl_rcb[i].p_cback = p_data->api_reg.p_cback;
+        bta_gattc_cb.cl_rcb[i].p_cback = p_cback;
         memcpy(&bta_gattc_cb.cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID));
 
         /* BTA use the same client interface as BTE GATT statck */
-        cb_data.reg_oper.client_if = bta_gattc_cb.cl_rcb[i].client_if;
+        client_if = bta_gattc_cb.cl_rcb[i].client_if;
 
         tBTA_GATTC_INT_START_IF* p_buf = (tBTA_GATTC_INT_START_IF*)osi_malloc(
             sizeof(tBTA_GATTC_INT_START_IF));
@@ -199,14 +196,7 @@
     }
   }
 
-  /* callback with register event */
-  if (p_data->api_reg.p_cback) {
-    if (p_app_uuid != NULL)
-      memcpy(&(cb_data.reg_oper.app_uuid), p_app_uuid, sizeof(tBT_UUID));
-
-    cb_data.reg_oper.status = status;
-    (*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT, (tBTA_GATTC*)&cb_data);
-  }
+  if (!cb.is_null()) cb.Run(client_if, status);
 }
 /*******************************************************************************
  *
diff --git a/bta/gatt/bta_gattc_api.cc b/bta/gatt/bta_gattc_api.cc
index 108812e..820f96a 100644
--- a/bta/gatt/bta_gattc_api.cc
+++ b/bta/gatt/bta_gattc_api.cc
@@ -26,8 +26,11 @@
 
 #include <string.h>
 
+#include <base/bind.h>
+#include <base/bind_helpers.h>
 #include <base/callback.h>
 #include "bt_common.h"
+#include "bta_closure_api.h"
 #include "bta_gatt_api.h"
 #include "bta_gattc_int.h"
 #include "bta_sys.h"
@@ -63,33 +66,29 @@
   bta_sys_deregister(BTA_ID_GATTC);
 }
 
-/*******************************************************************************
- *
- * Function         BTA_GATTC_AppRegister
- *
- * Description      This function is called to register application callbacks
- *                    with BTA GATTC module.
- *
- * Parameters       p_app_uuid - applicaiton UUID
- *                  p_client_cb - pointer to the application callback function.
- *
- * Returns          None
- *
- ******************************************************************************/
-void BTA_GATTC_AppRegister(tBT_UUID* p_app_uuid,
-                           tBTA_GATTC_CBACK* p_client_cb) {
-  tBTA_GATTC_API_REG* p_buf =
-      (tBTA_GATTC_API_REG*)osi_malloc(sizeof(tBTA_GATTC_API_REG));
+static void create_random_uuid(tBT_UUID* uuid) {
+  uuid->len = LEN_UUID_128;
 
+  for (int i = 0; i < 16; ++i) {
+    uuid->uu.uuid128[i] = (uint8_t)(rand() % 256);
+  }
+}
+
+/**
+ * This function is called to register application callbacks with BTA GATTC
+ * module. |client_cb| pointer to the application callback function.
+ * |cb| one time callback when registration is finished
+ */
+void BTA_GATTC_AppRegister(tBTA_GATTC_CBACK* p_client_cb,
+                           BtaAppRegisterCallback cb) {
   if (bta_sys_is_register(BTA_ID_GATTC) == false)
     bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg);
 
-  p_buf->hdr.event = BTA_GATTC_API_REG_EVT;
-  if (p_app_uuid != NULL)
-    memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
-  p_buf->p_cback = p_client_cb;
-
-  bta_sys_sendmsg(p_buf);
+  // base::Owned will own and free app_uuid
+  tBT_UUID* uuid = new tBT_UUID;
+  create_random_uuid(uuid);
+  do_in_bta_thread(FROM_HERE, base::Bind(&bta_gattc_register, base::Owned(uuid),
+                                         p_client_cb, std::move(cb)));
 }
 
 /*******************************************************************************
diff --git a/bta/gatt/bta_gattc_int.h b/bta/gatt/bta_gattc_int.h
index 70b400d..7001298 100644
--- a/bta/gatt/bta_gattc_int.h
+++ b/bta/gatt/bta_gattc_int.h
@@ -60,7 +60,6 @@
   BTA_GATTC_INT_DISCONN_EVT,
 
   BTA_GATTC_INT_START_IF_EVT,
-  BTA_GATTC_API_REG_EVT,
   BTA_GATTC_API_DEREG_EVT,
   BTA_GATTC_API_DISABLE_EVT,
   BTA_GATTC_ENC_CMPL_EVT
@@ -90,12 +89,6 @@
 /* internal strucutre for GATTC register API  */
 typedef struct {
   BT_HDR hdr;
-  tBT_UUID app_uuid;
-  tBTA_GATTC_CBACK* p_cback;
-} tBTA_GATTC_API_REG;
-
-typedef struct {
-  BT_HDR hdr;
   tBTA_GATTC_IF client_if;
 } tBTA_GATTC_INT_START_IF;
 
@@ -186,7 +179,6 @@
 
 typedef union {
   BT_HDR hdr;
-  tBTA_GATTC_API_REG api_reg;
   tBTA_GATTC_API_DEREG api_dereg;
   tBTA_GATTC_API_OPEN api_conn;
   tBTA_GATTC_API_CANCEL_OPEN api_cancel_conn;
@@ -362,7 +354,8 @@
 
 /* function processed outside SM */
 extern void bta_gattc_disable();
-extern void bta_gattc_register(tBTA_GATTC_DATA* p_data);
+extern void bta_gattc_register(tBT_UUID* p_app_uuid, tBTA_GATTC_CBACK* p_data,
+                               BtaAppRegisterCallback cb);
 extern void bta_gattc_start_if(tBTA_GATTC_DATA* p_data);
 extern void bta_gattc_process_api_open(tBTA_GATTC_DATA* p_msg);
 extern void bta_gattc_process_api_open_cancel(tBTA_GATTC_DATA* p_msg);
diff --git a/bta/gatt/bta_gattc_main.cc b/bta/gatt/bta_gattc_main.cc
index 3fe0d66..16efce0 100644
--- a/bta/gatt/bta_gattc_main.cc
+++ b/bta/gatt/bta_gattc_main.cc
@@ -372,10 +372,6 @@
       bta_gattc_disable();
       break;
 
-    case BTA_GATTC_API_REG_EVT:
-      bta_gattc_register((tBTA_GATTC_DATA*)p_msg);
-      break;
-
     case BTA_GATTC_INT_START_IF_EVT:
       bta_gattc_start_if((tBTA_GATTC_DATA*)p_msg);
       break;
@@ -473,8 +469,6 @@
       return "BTA_GATTC_INT_DISCONN_EVT";
     case BTA_GATTC_INT_START_IF_EVT:
       return "BTA_GATTC_INT_START_IF_EVT";
-    case BTA_GATTC_API_REG_EVT:
-      return "BTA_GATTC_API_REG_EVT";
     case BTA_GATTC_API_DEREG_EVT:
       return "BTA_GATTC_API_DEREG_EVT";
     case BTA_GATTC_API_REFRESH_EVT:
diff --git a/bta/hh/bta_hh_le.cc b/bta/hh/bta_hh_le.cc
index 6361237..2aea885 100644
--- a/bta/hh/bta_hh_le.cc
+++ b/bta/hh/bta_hh_le.cc
@@ -26,6 +26,8 @@
 
 #include <string.h>
 
+#include <base/bind.h>
+#include <base/callback.h>
 #include <list>
 #include <unordered_map>
 #include <unordered_set>
@@ -317,8 +319,6 @@
  *
  ******************************************************************************/
 void bta_hh_le_enable(void) {
-  char app_name[LEN_UUID_128 + 1];
-  tBT_UUID app_uuid = {LEN_UUID_128, {0}};
   uint8_t xx;
 
   bta_hh_cb.gatt_if = BTA_GATTS_INVALID_IF;
@@ -326,36 +326,20 @@
   for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++)
     bta_hh_cb.le_cb_index[xx] = BTA_HH_IDX_INVALID;
 
-  memset(app_name, 0, LEN_UUID_128 + 1);
-  strncpy(app_name, "BTA HH OVER LE", LEN_UUID_128);
+  BTA_GATTC_AppRegister(bta_hh_gattc_callback,
+                        base::Bind([](uint8_t client_id, uint8_t r_status) {
+                          tBTA_HH_STATUS status = BTA_HH_ERR;
 
-  memcpy((void*)app_uuid.uu.uuid128, (void*)app_name, LEN_UUID_128);
+                          if (r_status == BTA_GATT_OK) {
+                            bta_hh_cb.gatt_if = client_id;
+                            status = BTA_HH_OK;
+                          } else
+                            bta_hh_cb.gatt_if = BTA_GATTS_INVALID_IF;
 
-  BTA_GATTC_AppRegister(&app_uuid, bta_hh_gattc_callback);
-
-  return;
-}
-
-/*******************************************************************************
- *
- * Function         bta_hh_le_register_cmpl
- *
- * Description      BTA HH register with BTA GATTC completed
- *
- * Parameters:
- *
- ******************************************************************************/
-void bta_hh_le_register_cmpl(tBTA_GATTC_REG* p_reg) {
-  tBTA_HH_STATUS status = BTA_HH_ERR;
-
-  if (p_reg->status == BTA_GATT_OK) {
-    bta_hh_cb.gatt_if = p_reg->client_if;
-    status = BTA_HH_OK;
-  } else
-    bta_hh_cb.gatt_if = BTA_GATTS_INVALID_IF;
-
-  /* signal BTA call back event */
-  (*bta_hh_cb.p_cback)(BTA_HH_ENABLE_EVT, (tBTA_HH*)&status);
+                          /* signal BTA call back event */
+                          (*bta_hh_cb.p_cback)(BTA_HH_ENABLE_EVT,
+                                               (tBTA_HH*)&status);
+                        }));
 }
 
 /*******************************************************************************
@@ -2201,10 +2185,6 @@
   if (p_data == NULL) return;
 
   switch (event) {
-    case BTA_GATTC_REG_EVT: /* 0 */
-      bta_hh_le_register_cmpl(&p_data->reg_oper);
-      break;
-
     case BTA_GATTC_DEREG_EVT: /* 1 */
       bta_hh_cleanup_disable(p_data->reg_oper.status);
       break;
diff --git a/bta/include/bta_gatt_api.h b/bta/include/bta_gatt_api.h
index bc71e37..f441b95 100644
--- a/bta/include/bta_gatt_api.h
+++ b/bta/include/bta_gatt_api.h
@@ -106,7 +106,6 @@
 #define BTA_GATT_INVALID_CONN_ID GATT_INVALID_CONN_ID
 
 /* Client callback function events */
-#define BTA_GATTC_REG_EVT 0          /* GATT client is registered. */
 #define BTA_GATTC_DEREG_EVT 1        /* GATT client deregistered event */
 #define BTA_GATTC_OPEN_EVT 2         /* GATTC open request status  event */
 #define BTA_GATTC_CLOSE_EVT 5        /* GATTC  close request status event */
@@ -558,21 +557,16 @@
  ******************************************************************************/
 extern void BTA_GATTC_Disable(void);
 
-/*******************************************************************************
- *
- * Function         BTA_GATTC_AppRegister
- *
- * Description      This function is called to register application callbacks
- *                    with BTA GATTC module.
- *
- * Parameters       p_app_uuid - applicaiton UUID
- *                  p_client_cb - pointer to the application callback function.
- *
- * Returns          None
- *
- ******************************************************************************/
-extern void BTA_GATTC_AppRegister(tBT_UUID* p_app_uuid,
-                                  tBTA_GATTC_CBACK* p_client_cb);
+using BtaAppRegisterCallback =
+    base::Callback<void(uint8_t /* app_id */, uint8_t /* status */)>;
+
+/**
+ * This function is called to register application callbacks with BTA GATTC
+ *module.
+ * p_client_cb - pointer to the application callback function.
+ **/
+extern void BTA_GATTC_AppRegister(tBTA_GATTC_CBACK* p_client_cb,
+                                  BtaAppRegisterCallback cb);
 
 /*******************************************************************************
  *
diff --git a/btif/src/btif_ble_scanner.cc b/btif/src/btif_ble_scanner.cc
index f757233..492f159 100644
--- a/btif/src/btif_ble_scanner.cc
+++ b/btif/src/btif_ble_scanner.cc
@@ -47,6 +47,8 @@
 using base::Bind;
 using base::Owned;
 using std::vector;
+using RegisterCallback =
+    base::Callback<void(uint8_t /* scanner_id */, uint8_t /* status */)>;
 
 extern bt_status_t do_in_jni_thread(const base::Closure& task);
 extern const btgatt_callbacks_t* bt_gatt_callbacks;
@@ -166,14 +168,6 @@
 
   tBTA_GATTC* p_data = (tBTA_GATTC*)p_param;
   switch (event) {
-    case BTA_GATTC_REG_EVT: {
-      bt_uuid_t app_uuid;
-      bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.app_uuid);
-      HAL_CBACK(bt_gatt_callbacks, scanner->register_scanner_cb,
-                p_data->reg_oper.status, p_data->reg_oper.client_if, &app_uuid);
-      break;
-    }
-
     case BTA_GATTC_DEREG_EVT:
       break;
 
@@ -184,7 +178,7 @@
     }
 
     default:
-      LOG_ERROR(LOG_TAG, "%s: Unhandled event (%d)!", __func__, event);
+      LOG_DEBUG(LOG_TAG, "%s: Unhandled event (%d)", __func__, event);
       break;
   }
 }
@@ -369,16 +363,32 @@
   SCAN_CBACK_IN_JNI(track_adv_event_cb, Owned(btif_scan_track_cb));
 }
 
-void btif_gattc_register_scanner_impl(tBT_UUID uuid) {
-  BTA_GATTC_AppRegister(&uuid, bta_gatts_cback);
-}
-
 bt_status_t btif_gattc_register_scanner(bt_uuid_t* uuid) {
   CHECK_BTGATT_INIT();
 
   tBT_UUID bt_uuid;
   btif_to_bta_uuid(&bt_uuid, uuid);
-  return do_in_jni_thread(Bind(&btif_gattc_register_scanner_impl, bt_uuid));
+
+  return do_in_jni_thread(Bind(
+      [](tBT_UUID bt_uuid) {
+        BTA_GATTC_AppRegister(
+            bta_gatts_cback,
+            base::Bind(
+                [](tBT_UUID bt_uuid, uint8_t client_id, uint8_t status) {
+                  do_in_jni_thread(Bind(
+                      [](tBT_UUID bt_uuid, uint8_t client_id, uint8_t status) {
+                        bt_uuid_t app_uuid;
+                        bta_to_btif_uuid(&app_uuid, &bt_uuid);
+
+                        HAL_CBACK(bt_gatt_callbacks,
+                                  scanner->register_scanner_cb, status,
+                                  client_id, &app_uuid);
+                      },
+                      bt_uuid, client_id, status));
+                },
+                bt_uuid));
+      },
+      bt_uuid));
 }
 
 void btif_gattc_unregister_scanner_impl(int client_if) {
diff --git a/btif/src/btif_gatt_client.cc b/btif/src/btif_gatt_client.cc
index dcfb526..cb2109b 100644
--- a/btif/src/btif_gatt_client.cc
+++ b/btif/src/btif_gatt_client.cc
@@ -100,14 +100,6 @@
 
   tBTA_GATTC* p_data = (tBTA_GATTC*)p_param;
   switch (event) {
-    case BTA_GATTC_REG_EVT: {
-      bt_uuid_t app_uuid;
-      bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.app_uuid);
-      HAL_CBACK(bt_gatt_callbacks, client->register_client_cb,
-                p_data->reg_oper.status, p_data->reg_oper.client_if, &app_uuid);
-      break;
-    }
-
     case BTA_GATTC_DEREG_EVT:
       break;
 
@@ -214,16 +206,30 @@
  *  Client API Functions
  ******************************************************************************/
 
-void btif_gattc_register_app_impl(tBT_UUID uuid) {
-  BTA_GATTC_AppRegister(&uuid, bta_gattc_cback);
-}
-
 bt_status_t btif_gattc_register_app(bt_uuid_t* uuid) {
   CHECK_BTGATT_INIT();
 
   tBT_UUID bt_uuid;
   btif_to_bta_uuid(&bt_uuid, uuid);
-  return do_in_jni_thread(Bind(&btif_gattc_register_app_impl, bt_uuid));
+
+  return do_in_jni_thread(Bind(
+      [](tBT_UUID bt_uuid) {
+        BTA_GATTC_AppRegister(
+            bta_gattc_cback,
+            base::Bind(
+                [](tBT_UUID bt_uuid, uint8_t client_id, uint8_t status) {
+                  do_in_jni_thread(Bind(
+                      [](tBT_UUID bt_uuid, uint8_t client_id, uint8_t status) {
+                        bt_uuid_t app_uuid;
+                        bta_to_btif_uuid(&app_uuid, &bt_uuid);
+                        HAL_CBACK(bt_gatt_callbacks, client->register_client_cb,
+                                  status, client_id, &app_uuid);
+                      },
+                      bt_uuid, client_id, status));
+                },
+                bt_uuid));
+      },
+      bt_uuid));
 }
 
 void btif_gattc_unregister_app_impl(int client_if) {