Merge "GKI_exception on running longer batch scans" into lmp-dev
diff --git a/bta/dm/bta_dm_api.c b/bta/dm/bta_dm_api.c
index 12262b2..701f6a6 100644
--- a/bta/dm/bta_dm_api.c
+++ b/bta/dm/bta_dm_api.c
@@ -2544,13 +2544,18 @@
 void BTA_VendorCleanup (void)
 {
     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
+    BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
 
 #if (BLE_INCLUDED == TRUE && BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE)
-    btm_ble_adv_filter_cleanup();
-    btm_ble_vendor_cleanup();
-#endif
+    if (cmn_ble_vsc_cb.max_filter > 0)
+    {
+        btm_ble_adv_filter_cleanup();
+        btm_ble_vendor_cleanup();
+    }
 
-   BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
+    if (cmn_ble_vsc_cb.tot_scan_results_strg > 0)
+        btm_ble_batchscan_cleanup();
+#endif
 
    if(cmn_ble_vsc_cb.adv_inst_max > 0)
       btm_ble_multi_adv_cleanup();
diff --git a/btif/src/btif_gatt_client.c b/btif/src/btif_gatt_client.c
index 2c0a3d1..3a27447 100644
--- a/btif/src/btif_gatt_client.c
+++ b/btif/src/btif_gatt_client.c
@@ -732,7 +732,7 @@
             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
             uint8_t *p_rep_data = NULL;
 
-            if (p_data->read_reports.data_len > 0)
+            if (p_data->read_reports.data_len > 0 && NULL != p_data->read_reports.p_rep_data)
             {
                 p_rep_data = GKI_getbuf(p_data->read_reports.data_len);
                 memcpy(p_rep_data, p_data->read_reports.p_rep_data, p_data->read_reports.data_len);
@@ -741,6 +741,8 @@
             HAL_CBACK(bt_gatt_callbacks, client->batchscan_reports_cb
                     , p_data->client_if, p_data->status, p_data->read_reports.report_format
                     , p_data->read_reports.num_records, p_data->read_reports.data_len, p_rep_data);
+            if (NULL != p_rep_data)
+                GKI_freebuf(p_rep_data);
             break;
         }
 
@@ -926,6 +928,7 @@
                                             UINT8* p_rep_data, tBTA_STATUS status)
 {
     btgatt_batch_track_cb_t btif_scan_track_cb;
+    memset(&btif_scan_track_cb, 0, sizeof(btgatt_batch_track_cb_t));
     BTIF_TRACE_DEBUG("%s - client_if:%d, %d, %d, %d",__FUNCTION__, ref_value, status, num_records,
                                     data_len);
 
diff --git a/stack/btm/btm_ble_batchscan.c b/stack/btm/btm_ble_batchscan.c
index 3b7dda7..b974786 100644
--- a/stack/btm/btm_ble_batchscan.c
+++ b/stack/btm/btm_ble_batchscan.c
@@ -48,7 +48,7 @@
 **  Local functions
 *******************************************************************************/
 void btm_ble_batchscan_vsc_cmpl_cback (tBTM_VSC_CMPL *p_params);
-
+void btm_ble_batchscan_cleanup(void);
 
 /*******************************************************************************
 **
@@ -172,7 +172,6 @@
     {
         len = ble_batchscan_cb.main_rep_q.data_len[index];
         p_orig_data = ble_batchscan_cb.main_rep_q.p_data[index];
-
         if (NULL != p_orig_data)
         {
             p_app_data = GKI_getbuf(len + data_len);
@@ -180,16 +179,17 @@
             memcpy(p_app_data+len, p_data, data_len);
             GKI_freebuf(p_orig_data);
             ble_batchscan_cb.main_rep_q.p_data[index] = p_app_data;
+            ble_batchscan_cb.main_rep_q.num_records[index] += num_records;
+            ble_batchscan_cb.main_rep_q.data_len[index] += data_len;
         }
         else
         {
             p_app_data = GKI_getbuf(data_len);
             memcpy(p_app_data, p_data, data_len);
             ble_batchscan_cb.main_rep_q.p_data[index] = p_app_data;
+            ble_batchscan_cb.main_rep_q.num_records[index] = num_records;
+            ble_batchscan_cb.main_rep_q.data_len[index] = data_len;
         }
-
-        ble_batchscan_cb.main_rep_q.num_records[index] += num_records;
-        ble_batchscan_cb.main_rep_q.data_len[index] += data_len;
     }
 }
 
@@ -373,26 +373,7 @@
                                 status, ble_batchscan_cb.cur_state, cb_evt);
             /* Clear the queues here */
             if(BTM_SUCCESS == status && BTM_BLE_SCAN_DISABLE_CALLED == cur_state)
-            {
-                for (index = 0; index < BTM_BLE_BATCH_REP_MAIN_Q_SIZE; index++)
-                {
-                    ble_batchscan_cb.main_rep_q.rep_mode[index] = 0;
-                    if (NULL != ble_batchscan_cb.main_rep_q.p_data[index])
-                        GKI_freebuf(ble_batchscan_cb.main_rep_q.p_data[index]);
-                    ble_batchscan_cb.main_rep_q.p_data[index] = NULL;
-                    ble_batchscan_cb.main_rep_q.ref_value[index] = 0;
-                    ble_batchscan_cb.main_rep_q.num_records[index] = 0;
-                }
-
-                for (index = 0; index < BTM_BLE_BATCH_SCAN_MAX; index++)
-                {
-                    ble_batchscan_cb.op_q.sub_code[index] = 0;
-                    ble_batchscan_cb.op_q.ref_value[index] = 0;
-                    ble_batchscan_cb.op_q.cur_state[index] = 0;
-                }
-                ble_batchscan_cb.op_q.pending_idx = 0;
-                ble_batchscan_cb.op_q.next_idx = 0;
-            }
+                btm_ble_batchscan_cleanup();
 
              if (cb_evt != 0 && NULL != ble_batchscan_cb.p_setup_cback)
                 ble_batchscan_cb.p_setup_cback(cb_evt, ref_value, status);
@@ -832,8 +813,8 @@
             status = btm_ble_read_batchscan_reports(scan_mode, ref_value);
             if (BTM_CMD_STARTED != status)
             {
-                    btm_ble_batchscan_deq_rep_data(scan_mode, &ref_value,
-                                                   &num_records, &p_data, &data_len);
+                btm_ble_batchscan_deq_rep_data(scan_mode, &ref_value,
+                                               &num_records, &p_data, &data_len);
             }
         }
     }
@@ -899,4 +880,31 @@
     BTM_RegisterForVSEvents(btm_ble_batchscan_filter_track_adv_vse_cback, TRUE);
 }
 
+/*******************************************************************************
+**
+** Function         btm_ble_batchscan_cleanup
+**
+** Description      This function cleans the batch scan control block.
+**
+** Parameters       None
+**
+** Returns          void
+**
+*******************************************************************************/
+void btm_ble_batchscan_cleanup(void)
+{
+    int index = 0;
+    BTM_TRACE_EVENT (" btm_ble_batchscan_cleanup");
+
+    for (index = 0; index < BTM_BLE_BATCH_REP_MAIN_Q_SIZE; index++)
+    {
+        if (NULL != ble_batchscan_cb.main_rep_q.p_data[index])
+            GKI_freebuf(ble_batchscan_cb.main_rep_q.p_data[index]);
+        ble_batchscan_cb.main_rep_q.p_data[index] = NULL;
+    }
+
+    memset(&ble_batchscan_cb, 0, sizeof(tBTM_BLE_BATCH_SCAN_CB));
+    memset(&ble_advtrack_cb, 0, sizeof(tBTM_BLE_ADV_TRACK_CB));
+}
+
 #endif
diff --git a/stack/btm/btm_ble_int.h b/stack/btm/btm_ble_int.h
index b8e33a2..e8aba02 100644
--- a/stack/btm/btm_ble_int.h
+++ b/stack/btm/btm_ble_int.h
@@ -400,6 +400,7 @@
 extern void btm_ble_multi_adv_enb_privacy(BOOLEAN enable);
 extern char btm_ble_map_adv_tx_power(int tx_power_index);
 extern void btm_ble_batchscan_init(void);
+extern void btm_ble_batchscan_cleanup(void);
 extern void btm_ble_adv_filter_init(void);
 extern void btm_ble_adv_filter_cleanup(void);
 extern BOOLEAN btm_ble_topology_check(tBTM_BLE_STATE_MASK request);