From 16239bfe20583438ccb6fd3fc2fdffa6e10f29f2 Mon Sep 17 00:00:00 2001
From: Jakub Pawlowski <jpawlowski@google.com>
Date: Fri, 27 Jan 2017 05:53:07 -0800
Subject: [PATCH] Use new event type in BLE scan

Right now new event_type in scan result is translated to legacy
one and used through whole stack. This patch make use of new event_type
through whole stack instead.

Bug: 30622771
Test: sl4a FilteringTest
Change-Id: I7420c040b8c54fae00262c939bcabc6c6fcb16e6
---
 system/bta/include/bta_api.h         |   2 +-
 system/stack/btm/btm_ble_gap.cc      | 162 +++++++++++++--------------
 system/stack/btm/btm_ble_int.h       |   1 +
 system/stack/btm/btm_inq.cc          |   2 +-
 system/stack/include/btm_api_types.h |  13 +--
 5 files changed, 85 insertions(+), 95 deletions(-)

diff --git a/system/bta/include/bta_api.h b/system/bta/include/bta_api.h
index c1cfa8cd68d..da1d352640f 100644
--- a/system/bta/include/bta_api.h
+++ b/system/bta/include/bta_api.h
@@ -730,7 +730,7 @@ typedef struct {
   uint8_t* p_eir;  /* received EIR */
   uint8_t inq_result_type;
   uint8_t ble_addr_type;
-  tBTM_BLE_EVT_TYPE ble_evt_type;
+  uint16_t ble_evt_type;
   tBT_DEVICE_TYPE device_type;
   uint8_t flag;
 } tBTA_DM_INQ_RES;
diff --git a/system/stack/btm/btm_ble_gap.cc b/system/stack/btm/btm_ble_gap.cc
index 8a8627e9f97..09d0cb986fd 100644
--- a/system/stack/btm/btm_ble_gap.cc
+++ b/system/stack/btm/btm_ble_gap.cc
@@ -65,7 +65,7 @@ static tBTM_BLE_CTRL_FEATURES_CBACK* p_ctrl_le_feature_rd_cmpl_cback = NULL;
  ******************************************************************************/
 static void btm_ble_update_adv_flag(uint8_t flag);
 static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, uint8_t addr_type,
-                                         uint8_t evt_type, uint8_t data_len,
+                                         uint16_t evt_type, uint8_t data_len,
                                          uint8_t* data, int8_t rssi);
 static uint8_t btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB* p_cb,
                                                BD_ADDR_PTR p_peer_addr_ptr,
@@ -81,6 +81,26 @@ static void btm_ble_observer_timer_timeout(void* data);
 #define BTM_BLE_INQ_RESULT 0x01
 #define BTM_BLE_OBS_RESULT 0x02
 
+bool ble_evt_type_is_connectable(uint16_t evt_type) {
+  return evt_type & (1 << BLE_EVT_CONNECTABLE_BIT);
+}
+
+bool ble_evt_type_is_scannable(uint16_t evt_type) {
+  return evt_type & (1 << BLE_EVT_SCANNABLE_BIT);
+}
+
+bool ble_evt_type_is_directed(uint16_t evt_type) {
+  return evt_type & (1 << BLE_EVT_DIRECTED_BIT);
+}
+
+bool ble_evt_type_is_scan_resp(uint16_t evt_type) {
+  return evt_type & (1 << BLE_EVT_SCAN_RESPONSE_BIT);
+}
+
+bool ble_evt_type_is_legacy(uint16_t evt_type) {
+  return evt_type & (1 << BLE_EVT_LEGACY_BIT);
+}
+
 /* LE states combo bit to check */
 const uint8_t btm_le_state_combo_tbl[BTM_BLE_STATE_MAX][BTM_BLE_STATE_MAX][2] =
     {{
@@ -1541,8 +1561,7 @@ tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_INQ_INFO* p_cur,
 
   if (!controller_get_interface()->supports_ble()) return BTM_ERR_PROCESSING;
 
-  if (p_cur && p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_ADV &&
-      p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_DIR_ADV) {
+  if (p_cur && !ble_evt_type_is_connectable(p_cur->results.ble_evt_type)) {
     BTM_TRACE_DEBUG("name request to non-connectable device failed.");
     return BTM_ERR_PROCESSING;
   }
@@ -1632,23 +1651,17 @@ static void btm_ble_update_adv_flag(uint8_t flag) {
   p_adv_data->data_mask |= BTM_BLE_AD_BIT_FLAGS;
 }
 
-/*******************************************************************************
- *
- * Function         btm_ble_cache_adv_data
- *
- * Description      Update advertising cache data.
- *
- * Returns          void
- *
- ******************************************************************************/
+/**
+ * Update advertising cache data.
+ */
 void btm_ble_cache_adv_data(UNUSED_ATTR tBTM_INQ_RESULTS* p_cur,
-                            uint8_t data_len, uint8_t* p, uint8_t evt_type) {
+                            uint8_t data_len, uint8_t* p, bool is_scan_resp) {
   tBTM_BLE_INQ_CB* p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
   uint8_t* p_cache;
   uint8_t length;
 
   /* cache adv report/scan response data */
-  if (evt_type != BTM_BLE_SCAN_RSP_EVT) {
+  if (!is_scan_resp) {
     p_le_inq_cb->adv_len = 0;
     memset(p_le_inq_cb->adv_data_cache, 0, BTM_BLE_CACHE_ADV_DATA_MAX);
   }
@@ -1674,19 +1687,11 @@ void btm_ble_cache_adv_data(UNUSED_ATTR tBTM_INQ_RESULTS* p_cur,
   /* TODO */
 }
 
-/*******************************************************************************
- *
- * Function         btm_ble_is_discoverable
- *
- * Description      check ADV flag to make sure device is discoverable and match
- *                  the search condition
- *
- * Parameters
- *
- * Returns          void
- *
- ******************************************************************************/
-uint8_t btm_ble_is_discoverable(BD_ADDR bda, uint8_t evt_type) {
+/**
+ * Check ADV flag to make sure device is discoverable and match the search
+ * condition
+ */
+uint8_t btm_ble_is_discoverable(BD_ADDR bda) {
   uint8_t *p_flag, flag = 0, rt = 0;
   uint8_t data_len;
   tBTM_INQ_PARMS* p_cond = &btm_cb.btm_inq_vars.inqparms;
@@ -1850,19 +1855,11 @@ static void btm_ble_appearance_to_cod(uint16_t appearance, uint8_t* dev_class) {
   };
 }
 
-/*******************************************************************************
- *
- * Function         btm_ble_update_inq_result
- *
- * Description      Update adv packet information into inquiry result.
- *
- * Parameters
- *
- * Returns          void
- *
- ******************************************************************************/
+/**
+ * Update adv packet information into inquiry result.
+ */
 bool btm_ble_update_inq_result(tINQ_DB_ENT* p_i, uint8_t addr_type,
-                               uint8_t evt_type, uint8_t data_len,
+                               uint16_t evt_type, uint8_t data_len,
                                uint8_t* data, int8_t rssi) {
   bool to_report = true;
   tBTM_INQ_RESULTS* p_cur = &p_i->inq_info.results;
@@ -1876,7 +1873,9 @@ bool btm_ble_update_inq_result(tINQ_DB_ENT* p_i, uint8_t addr_type,
     BTM_TRACE_WARNING("EIR data too long %d. discard", data_len);
     return false;
   }
-  btm_ble_cache_adv_data(p_cur, data_len, data, evt_type);
+
+  bool is_scan_resp = ble_evt_type_is_scan_resp(evt_type);
+  btm_ble_cache_adv_data(p_cur, data_len, data, is_scan_resp);
 
   /* Save the info */
   p_cur->inq_result_type = BTM_INQ_RESULT_BLE;
@@ -1884,8 +1883,9 @@ bool btm_ble_update_inq_result(tINQ_DB_ENT* p_i, uint8_t addr_type,
   p_cur->rssi = rssi;
 
   /* active scan, always wait until get scan_rsp to report the result */
-  if ((btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI &&
-       (evt_type == BTM_BLE_CONNECT_EVT || evt_type == BTM_BLE_DISCOVER_EVT))) {
+  if (btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI &&
+      ble_evt_type_is_scannable(evt_type) &&
+      !ble_evt_type_is_scan_resp(evt_type)) {
     BTM_TRACE_DEBUG("%s: scan_rsp=false, to_report=false, scan_type_active=%d",
                     __func__, btm_cb.ble_ctr_cb.inq_var.scan_type);
     p_i->scan_rsp = false;
@@ -1942,7 +1942,7 @@ bool btm_ble_update_inq_result(tINQ_DB_ENT* p_i, uint8_t addr_type,
 
   /* if BR/EDR not supported is not set, assume is a DUMO device */
   if ((p_cur->flag & BTM_BLE_BREDR_NOT_SPT) == 0 &&
-      evt_type != BTM_BLE_CONNECT_DIR_EVT) {
+      !ble_evt_type_is_directed(evt_type)) {
     if (p_cur->ble_addr_type != BLE_ADDR_RANDOM) {
       BTM_TRACE_DEBUG("BR/EDR NOT support bit not set, treat as DUMO");
       p_cur->device_type |= BT_DEVICE_TYPE_DUMO;
@@ -2060,32 +2060,8 @@ void btm_ble_process_ext_adv_pkt(uint8_t data_len, uint8_t* data) {
       continue;
     }
 
-    // TODO(jpawlowski): event type should be passed to
-    // btm_ble_process_adv_pkt_cont. Legacy values should be transformed to new
-    // value in btm_ble_process_adv_pkt
-    uint8_t legacy_evt_type;
-    if (event_type == 0x0013) {
-      legacy_evt_type = 0x00;  // ADV_IND;
-    } else if (event_type == 0x0015) {
-      legacy_evt_type = 0x01;  // ADV_DIRECT_IND;
-    } else if (event_type == 0x0012) {
-      legacy_evt_type = 0x02;  // ADV_SCAN_IND;
-    } else if (event_type == 0x0010) {
-      legacy_evt_type = 0x03;  // ADV_NONCONN_IND;
-    } else if (event_type == 0x001B) {
-      legacy_evt_type = 0x02;  // SCAN_RSP;
-    } else if (event_type == 0x001A) {
-      legacy_evt_type = 0x02;  // SCAN_RSP;
-    } else {
-      BTM_TRACE_ERROR(
-          "Malformed LE Advertising Report Event from controller - unsupported "
-          "legacy event_type 0x%04x",
-          event_type);
-      return;
-    }
-
     btm_ble_process_adv_addr(bda, addr_type);
-    btm_ble_process_adv_pkt_cont(bda, addr_type, legacy_evt_type, pkt_data_len,
+    btm_ble_process_adv_pkt_cont(bda, addr_type, event_type, pkt_data_len,
                                  pkt_data, rssi);
   }
 }
@@ -2098,7 +2074,7 @@ void btm_ble_process_ext_adv_pkt(uint8_t data_len, uint8_t* data) {
 void btm_ble_process_adv_pkt(uint8_t data_len, uint8_t* data) {
   BD_ADDR bda;
   uint8_t* p = data;
-  uint8_t evt_type, addr_type, num_reports, pkt_data_len;
+  uint8_t legacy_evt_type, addr_type, num_reports, pkt_data_len;
   int8_t rssi;
 
   /* Only process the results if the inquiry is still active */
@@ -2115,7 +2091,7 @@ void btm_ble_process_adv_pkt(uint8_t data_len, uint8_t* data) {
     }
 
     /* Extract inquiry results */
-    STREAM_TO_UINT8(evt_type, p);
+    STREAM_TO_UINT8(legacy_evt_type, p);
     STREAM_TO_UINT8(addr_type, p);
     STREAM_TO_BDADDR(bda, p);
     STREAM_TO_UINT8(pkt_data_len, p);
@@ -2132,30 +2108,42 @@ void btm_ble_process_adv_pkt(uint8_t data_len, uint8_t* data) {
 
     btm_ble_process_adv_addr(bda, addr_type);
 
-    btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, pkt_data_len,
+    uint16_t event_type;
+    if (legacy_evt_type == 0x00) {  // ADV_IND;
+      event_type = 0x0013;
+    } else if (legacy_evt_type == 0x01) {  // ADV_DIRECT_IND;
+      event_type = 0x0015;
+    } else if (legacy_evt_type == 0x02) {  // ADV_SCAN_IND;
+      event_type = 0x0012;
+    } else if (legacy_evt_type == 0x03) {  // ADV_NONCONN_IND;
+      event_type = 0x0010;
+    } else if (legacy_evt_type == 0x04) {  // SCAN_RSP;
+      // We can't distinguish between "SCAN_RSP to an ADV_IND", and "SCAN_RSP to
+      // an ADV_SCAN_IND", so always return "SCAN_RSP to an ADV_IND"
+      event_type = 0x001B;
+    } else {
+      BTM_TRACE_ERROR(
+          "Malformed LE Advertising Report Event - unsupported "
+          "legacy_event_type 0x%02x",
+          legacy_evt_type);
+      return;
+    }
+
+    btm_ble_process_adv_pkt_cont(bda, addr_type, event_type, pkt_data_len,
                                  pkt_data, rssi);
   }
 }
 
-/*******************************************************************************
- *
- * Function         btm_ble_process_adv_pkt_cont
- *
- * Description      This function is called after random address resolution is
- *                  done, and proceed to process adv packet.
- *
- * Parameters
- *
- * Returns          void
- *
- ******************************************************************************/
+/**
+ * This function is called after random address resolution is done, and proceed
+ * to process adv packet.
+ */
 static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, uint8_t addr_type,
-                                         uint8_t evt_type, uint8_t data_len,
+                                         uint16_t evt_type, uint8_t data_len,
                                          uint8_t* data, int8_t rssi) {
   tINQ_DB_ENT* p_i;
   tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
   tBTM_INQ_RESULTS_CB* p_inq_results_cb = p_inq->p_inq_results_cb;
-  tBTM_INQ_RESULTS_CB* p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;
   tBTM_BLE_INQ_CB* p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
   bool update = true;
   uint8_t result = 0;
@@ -2194,7 +2182,7 @@ static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, uint8_t addr_type,
                                  rssi))
     return;
 
-  result = btm_ble_is_discoverable(bda, evt_type);
+  result = btm_ble_is_discoverable(bda);
   if (result == 0) {
     LOG_WARN(LOG_TAG,
              "%s device no longer discoverable, discarding advertising packet",
@@ -2230,6 +2218,8 @@ static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, uint8_t addr_type,
     (p_inq_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,
                        p_le_inq_cb->adv_data_cache);
   }
+
+  tBTM_INQ_RESULTS_CB* p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;
   if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT)) {
     (p_obs_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,
                        p_le_inq_cb->adv_data_cache);
diff --git a/system/stack/btm/btm_ble_int.h b/system/stack/btm/btm_ble_int.h
index fa9ce590d27..8b2b0636f10 100644
--- a/system/stack/btm/btm_ble_int.h
+++ b/system/stack/btm/btm_ble_int.h
@@ -35,6 +35,7 @@
 #include "hcidefs.h"
 #include "smp_api.h"
 
+extern bool ble_evt_type_is_connectable(uint16_t evt_type);
 extern void btm_ble_refresh_raddr_timer_timeout(void* data);
 extern void btm_ble_process_adv_pkt(uint8_t len, uint8_t* p);
 extern void btm_ble_process_ext_adv_pkt(uint8_t len, uint8_t* p);
diff --git a/system/stack/btm/btm_inq.cc b/system/stack/btm/btm_inq.cc
index 840c3ccc963..0cde992b1a9 100644
--- a/system/stack/btm/btm_inq.cc
+++ b/system/stack/btm/btm_inq.cc
@@ -965,7 +965,7 @@ tBTM_STATUS BTM_ReadRemoteDeviceName(BD_ADDR remote_bda, tBTM_CMPL_CB* p_cb,
   p_i = btm_inq_db_find(remote_bda);
   if (p_i != NULL) {
     p_cur = &p_i->inq_info;
-    if ((p_cur->results.ble_evt_type == BTM_BLE_EVT_NON_CONN_ADV) &&
+    if ((!ble_evt_type_is_connectable(p_cur->results.ble_evt_type)) &&
         (p_cur->results.device_type !=
          BT_DEVICE_TYPE_BREDR)) { /* Non-connectable LE device: do not request
                                      its name! */
diff --git a/system/stack/include/btm_api_types.h b/system/stack/include/btm_api_types.h
index 39dc61cba9a..a2f1a13427f 100644
--- a/system/stack/include/btm_api_types.h
+++ b/system/stack/include/btm_api_types.h
@@ -615,12 +615,11 @@ typedef struct /* contains the parameters passed to the inquiry functions */
 #define BTM_INQ_RESULT_BR 0x01
 #define BTM_INQ_RESULT_BLE 0x02
 
-#define BTM_BLE_EVT_CONN_ADV 0x00
-#define BTM_BLE_EVT_CONN_DIR_ADV 0x01
-#define BTM_BLE_EVT_DISC_ADV 0x02
-#define BTM_BLE_EVT_NON_CONN_ADV 0x03
-#define BTM_BLE_EVT_SCAN_RSP 0x04
-typedef uint8_t tBTM_BLE_EVT_TYPE;
+constexpr uint8_t BLE_EVT_CONNECTABLE_BIT = 0;
+constexpr uint8_t BLE_EVT_SCANNABLE_BIT = 1;
+constexpr uint8_t BLE_EVT_DIRECTED_BIT = 2;
+constexpr uint8_t BLE_EVT_SCAN_RESPONSE_BIT = 3;
+constexpr uint8_t BLE_EVT_LEGACY_BIT = 4;
 
 /* These are the fields returned in each device's response to the inquiry.  It
  * is returned in the results callback if registered.
@@ -638,7 +637,7 @@ typedef struct {
   tBT_DEVICE_TYPE device_type;
   uint8_t inq_result_type;
   uint8_t ble_addr_type;
-  tBTM_BLE_EVT_TYPE ble_evt_type;
+  uint16_t ble_evt_type;
   uint8_t flag;
 } tBTM_INQ_RESULTS;
 
-- 
GitLab