Send connected callback immediately when attempting to connect to already connected device

Instead of adding the device to the connection manager, just send the
successfull connection callback.

This patch is restoring behaviour from before Connection Manager
refactor. During the refactor this behaviour was broken.

Bug: 130273570
Test: sl4a GattConnectTest
Change-Id: Ic4fa7089a8262f6f792206496a7cfb9c83a16eb2
diff --git a/system/stack/gatt/gatt_main.cc b/system/stack/gatt/gatt_main.cc
index 49e7da9..9c63ac9 100644
--- a/system/stack/gatt/gatt_main.cc
+++ b/system/stack/gatt/gatt_main.cc
@@ -196,21 +196,38 @@
 bool gatt_connect(const RawAddress& rem_bda, tGATT_TCB* p_tcb,
                   tBT_TRANSPORT transport, uint8_t initiating_phys,
                   tGATT_IF gatt_if) {
-  bool gatt_ret = false;
-
   if (gatt_get_ch_state(p_tcb) != GATT_CH_OPEN)
     gatt_set_ch_state(p_tcb, GATT_CH_CONN);
 
-  if (transport == BT_TRANSPORT_LE) {
-    p_tcb->att_lcid = L2CAP_ATT_CID;
-
-    gatt_ret = connection_manager::direct_connect_add(gatt_if, rem_bda);
-  } else {
+  if (transport != BT_TRANSPORT_LE) {
     p_tcb->att_lcid = L2CA_ConnectReq(BT_PSM_ATT, rem_bda);
-    if (p_tcb->att_lcid != 0) gatt_ret = true;
+    return p_tcb->att_lcid != 0;
   }
 
-  return gatt_ret;
+  // Already connected, send the callback, mark the link as used
+  if (gatt_get_ch_state(p_tcb) == GATT_CH_OPEN) {
+    /*  very similar to gatt_send_conn_cback, but no good way to reuse the code
+     */
+
+    /* notifying application about the connection up event */
+    for (int i = 0; i < GATT_MAX_APPS; i++) {
+      tGATT_REG* p_reg = &gatt_cb.cl_rcb[i];
+
+      if (!p_reg->in_use || p_reg->gatt_if != gatt_if) continue;
+
+      gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, true, true);
+      if (p_reg->app_cb.p_conn_cb) {
+        uint16_t conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
+        (*p_reg->app_cb.p_conn_cb)(p_reg->gatt_if, p_tcb->peer_bda, conn_id,
+                                   true, 0, p_tcb->transport);
+      }
+    }
+
+    return true;
+  }
+
+  p_tcb->att_lcid = L2CAP_ATT_CID;
+  return connection_manager::direct_connect_add(gatt_if, rem_bda);
 }
 
 /*******************************************************************************