LE: Fix problem where pending command is not sent after pairing

If a read/write or other operation triggers SMP pairing with a remote
device, the operation should complete once pairing has finished. This
patch fixes a bug where the pending command was not sent after pairing.

Bug: 16405565
Change-Id: Idb2e54931c71b0a9e009cac318712eb9f5c7e15a
diff --git a/system/bta/gatt/bta_gattc_act.c b/system/bta/gatt/bta_gattc_act.c
index 8a77564..cfc3668 100644
--- a/system/bta/gatt/bta_gattc_act.c
+++ b/system/bta/gatt/bta_gattc_act.c
@@ -1497,13 +1497,15 @@
             return;
         }
 
-        /* service handle change void the response, discard it */
-        if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
+        /* discard responses if service change indication is received before operation completed */
+        if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg)
         {
-            p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
-            bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
+            APPL_TRACE_DEBUG("Discard all responses when service change indication is received.");
+            p_data->op_cmpl.status = GATT_ERROR;
         }
-        else if (op == GATTC_OPTYPE_READ)
+
+        /* service handle change void the response, discard it */
+        if (op == GATTC_OPTYPE_READ)
             bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
 
         else if (op == GATTC_OPTYPE_WRITE)
@@ -1514,6 +1516,12 @@
 
         else if (op == GATTC_OPTYPE_CONFIG)
             bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
+
+        if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
+        {
+            p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
+            bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
+        }
     }
 }
 /*******************************************************************************
diff --git a/system/stack/btm/btm_sec.c b/system/stack/btm/btm_sec.c
index c9a185a..7aab0a9 100644
--- a/system/stack/btm/btm_sec.c
+++ b/system/stack/btm/btm_sec.c
@@ -5878,9 +5878,10 @@
 ** Parameters:      void
 **
 *******************************************************************************/
-void btm_sec_dev_rec_cback_event (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 res, BOOLEAN transport)
+void btm_sec_dev_rec_cback_event (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 res, BOOLEAN is_le_transport)
 {
     tBTM_SEC_CALLBACK   *p_callback = p_dev_rec->p_callback;
+    tBT_TRANSPORT transport = is_le_transport ? BT_TRANSPORT_LE : BT_TRANSPORT_BR_EDR;
 
     if (p_dev_rec->p_callback)
     {
diff --git a/system/stack/gatt/gatt_auth.c b/system/stack/gatt/gatt_auth.c
index 8386d88..10cf76e 100644
--- a/system/stack/gatt/gatt_auth.c
+++ b/system/stack/gatt/gatt_auth.c
@@ -131,7 +131,7 @@
 *******************************************************************************/
 void gatt_sec_check_complete(BOOLEAN sec_check_ok, tGATT_CLCB   *p_clcb, UINT8 sec_act)
 {
-    if (GKI_queue_is_empty(&p_clcb->p_tcb->pending_enc_clcb))
+    if (p_clcb && p_clcb->p_tcb && GKI_queue_is_empty(&p_clcb->p_tcb->pending_enc_clcb))
         gatt_set_sec_act(p_clcb->p_tcb, GATT_SEC_NONE);
 
     if (!sec_check_ok)