am 3d4f5c71: am b83a8234: Merge changes Ifc373f95,I95eb887a,I1e6be2ab,Ia6ea939a,I147d0138, ... into klp-modular-dev

* commit '3d4f5c715204c3c5eeb21e351aff73072000f61e': (24 commits)
  Make all warnings fatal compilation errors
  Major warnings cleanup
  Add macro UNUSED() to bt_utils.h
  Fix unused parameter warning in static function
  Add "static" to a static function
  Fix a number of unused parameter warnings in static functions
  Fix bug with wrong parameter passed to logu
  Default send_ind_evt should be FALSE
  Fix warning "implicit declaration of function 'GAP_BleReadPeerPrefConnParams'"
  Fix unused parameter warnings in static functions by changing prototype
  Fix some unused parameter warnings in static functions
  Fix warnings about implicit declaration of bdcmp and bdcpy
  Fix warning "comparison is always true due to limited range of data type"
  Fix warning "suggest braces around empty body in an 'if' statement"
  Fix warning "comparison is always true due to limited range of data type"
  Add a function prototype for btm_ble_test_command_complete
  Change argument to return ptr rather than assign to unused local
  Fix warning "comparison is always true due to limited range of data type"
  Fix warnings about implicit declarations
  Fix warning "implicit declaration of function 'bdcmp'"
  ...
diff --git a/bta/gatt/bta_gattc_act.c b/bta/gatt/bta_gattc_act.c
index bc23d5d..1841c25 100644
--- a/bta/gatt/bta_gattc_act.c
+++ b/bta/gatt/bta_gattc_act.c
@@ -777,7 +777,13 @@
     bta_gattc_clcb_dealloc(p_clcb);
 
     if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT)
+    {
         cb_data.close.status = GATT_Disconnect(p_data->hdr.layer_specific);
+    }
+    else if (p_data->hdr.event == BTA_GATTC_INT_DISCONN_EVT)
+    {
+        cb_data.close.status = p_data->int_conn.reason;
+    }
 
     if(p_cback)
         (* p_cback)(BTA_GATTC_CLOSE_EVT,   (tBTA_GATTC *)&cb_data);
diff --git a/btif/src/btif_av.c b/btif/src/btif_av.c
index 3c0c293..f893c9e 100755
--- a/btif/src/btif_av.c
+++ b/btif/src/btif_av.c
@@ -39,6 +39,7 @@
 #include "gki.h"
 #include "bd.h"
 #include "btu.h"
+#include "bt_utils.h"
 
 /*****************************************************************************
 **  Constants & Macros
@@ -587,9 +588,17 @@
 
             HAL_CBACK(bt_av_callbacks, audio_state_cb,
                 BTAV_AUDIO_STATE_STARTED, &(btif_av_cb.peer_bda));
+
+            /* increase the a2dp consumer task priority temporarily when start
+            ** audio playing, to avoid overflow the audio packet queue. */
+            adjust_priority_a2dp(TRUE);
+
             break;
 
         case BTIF_SM_EXIT_EVT:
+            /* restore the a2dp consumer task priority when stop audio playing. */
+            adjust_priority_a2dp(FALSE);
+
             break;
 
         case BTIF_AV_START_STREAM_REQ_EVT:
diff --git a/main/bte_main.c b/main/bte_main.c
index cf21456..b9cbf91 100644
--- a/main/bte_main.c
+++ b/main/bte_main.c
@@ -198,12 +198,12 @@
 
     lpm_enabled = FALSE;
 
-    bte_hci_enable();
-
     GKI_create_task((TASKPTR)btu_task, BTU_TASK, BTE_BTU_TASK_STR,
                     (UINT16 *) ((UINT8 *)bte_btu_stack + BTE_BTU_STACK_SIZE),
                     sizeof(bte_btu_stack));
 
+    bte_hci_enable();
+
     GKI_run(0);
 }
 
diff --git a/stack/btm/btm_ble.c b/stack/btm/btm_ble.c
index 0db65e4..4998e2d 100644
--- a/stack/btm/btm_ble.c
+++ b/stack/btm/btm_ble.c
@@ -1594,7 +1594,28 @@
         role = HCI_ROLE_UNKNOWN;
 
         if (status == HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT)
+        {
             btm_ble_dir_adv_tout();
+        }
+        /* this is to work around broadcom firmware problem to handle
+         * unsolicited command complete event for HCI_LE_Create_Connection_Cancel
+         * and LE connection complete event with status error code (0x2)
+         * unknown connection identifier from bluetooth controller
+         * the workaround is to release the HCI connection to avoid out of sync
+         * with bluetooth controller, which cause BT can't be turned off.
+        */
+        else if ((status == HCI_ERR_NO_CONNECTION) &&
+                 (btm_ble_get_conn_st() != BLE_CONN_CANCEL))
+        {
+            tL2C_LCB    *p_lcb;
+            handle = HCID_GET_HANDLE (handle);
+            p_lcb = l2cu_find_lcb_by_handle (handle);
+            if (p_lcb != NULL)
+            {
+                l2c_link_hci_disc_comp (handle, HCI_ERR_PEER_USER);
+                btm_sec_disconnected (handle, HCI_ERR_PEER_USER);
+            }
+        }
     }
 
     btm_ble_set_conn_st(BLE_CONN_IDLE);
diff --git a/stack/btm/btm_ble_gap.c b/stack/btm/btm_ble_gap.c
index 5e5b5db..f9ef2c6 100644
--- a/stack/btm/btm_ble_gap.c
+++ b/stack/btm/btm_ble_gap.c
@@ -1151,6 +1151,10 @@
     BD_NAME bd_name;
 
     memset(bd_name, 0, (BD_NAME_LEN + 1));
+    if (length > BD_NAME_LEN)
+    {
+        length = BD_NAME_LEN;
+    }
     memcpy((UINT8*)bd_name, p_name, length);
 
     if ((!status) || (length==0))
diff --git a/utils/include/bt_utils.h b/utils/include/bt_utils.h
index d601f2e..ee21861 100644
--- a/utils/include/bt_utils.h
+++ b/utils/include/bt_utils.h
@@ -39,6 +39,7 @@
 void bt_utils_init();
 void bt_utils_cleanup();
 void raise_priority_a2dp(tHIGH_PRIORITY_TASK high_task);
+void adjust_priority_a2dp(int start);
 
 #define UNUSED(x) (void)(x)
 
diff --git a/utils/src/bt_utils.c b/utils/src/bt_utils.c
index aeb9292..6decacf 100644
--- a/utils/src/bt_utils.c
+++ b/utils/src/bt_utils.c
@@ -50,6 +50,8 @@
 static BOOLEAN g_DoSchedulingGroup[TASK_HIGH_MAX];
 static pthread_mutex_t         gIdxLock;
 static int g_TaskIdx;
+static int g_TaskIDs[TASK_HIGH_MAX];
+#define INVALID_TASK_ID  (-1)
 
 /*****************************************************************************
 **
@@ -67,6 +69,7 @@
     for(i = 0; i < TASK_HIGH_MAX; i++) {
         g_DoSchedulingGroupOnce[i] = PTHREAD_ONCE_INIT;
         g_DoSchedulingGroup[i] = TRUE;
+        g_TaskIDs[i] = INVALID_TASK_ID;
     }
     pthread_mutexattr_init(&lock_attr);
     pthread_mutex_init(&gIdxLock, &lock_attr);
@@ -126,6 +129,7 @@
         // set_sched_policy does not support tid == 0
         rc = set_sched_policy(tid, SP_FOREGROUND);
     }
+    g_TaskIDs[high_task] = tid;
     pthread_mutex_unlock(&gIdxLock);
 
     if (rc) {
@@ -137,3 +141,31 @@
     }
 }
 
+/*****************************************************************************
+**
+** Function        adjust_priority_a2dp
+**
+** Description     increase the a2dp consumer task priority temporarily when start
+**                 audio playing, to avoid overflow the audio packet queue, restore
+**                 the a2dp consumer task priority when stop audio playing.
+**
+** Returns         void
+**
+*******************************************************************************/
+void adjust_priority_a2dp(int start) {
+    int priority = start ? ANDROID_PRIORITY_URGENT_AUDIO : ANDROID_PRIORITY_AUDIO;
+    int tid;
+    int i;
+
+    for (i = TASK_HIGH_GKI_TIMER; i < TASK_HIGH_MAX; i++)
+    {
+        tid = g_TaskIDs[i];
+        if (tid != INVALID_TASK_ID)
+        {
+            if (setpriority(PRIO_PROCESS, tid, priority) < 0)
+            {
+                ALOGW("failed to change priority tid: %d to %d", tid, priority);
+            }
+        }
+    }
+}