From c7330112d2990043977e9d00fcc1597ef49d62c8 Mon Sep 17 00:00:00 2001
From: Alice Kuo <aliceypkuo@google.com>
Date: Tue, 19 Jan 2021 22:21:45 +0800
Subject: [PATCH] csip: Extract the RSI AD type to notify upper layer

A CSIP supported device shall contains the RSI AD type in the
advertising report. Update the information to upper layer.
As users trigger to pair, setting need to know this device support CSIP
to handle the further UX behavior.

Bug: 178981521
Bug: 150670922
Test: Manual test
Change-Id: I8f018cd32e73b9b5cef6120ee63a708c980e1869
---
 system/bta/dm/bta_dm_act.cc              |  1 +
 system/bta/include/bta_api.h             |  1 +
 system/btif/src/btif_dm.cc               |  9 +++++++-
 system/include/hardware/bluetooth.h      |  7 ++++++
 system/stack/btm/btm_ble_gap.cc          | 28 ++++++++++++++++++++++--
 system/stack/btm/neighbor_inquiry.h      |  1 +
 system/stack/include/btm_ble_api_types.h |  1 +
 7 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/system/bta/dm/bta_dm_act.cc b/system/bta/dm/bta_dm_act.cc
index 72de399ea22..154ab9a9647 100644
--- a/system/bta/dm/bta_dm_act.cc
+++ b/system/bta/dm/bta_dm_act.cc
@@ -1829,6 +1829,7 @@ static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
   result.inq_res.inq_result_type = p_inq->inq_result_type;
   result.inq_res.device_type = p_inq->device_type;
   result.inq_res.flag = p_inq->flag;
+  result.inq_res.include_rsi = p_inq->include_rsi;
 
   /* application will parse EIR to find out remote device name */
   result.inq_res.p_eir = p_eir;
diff --git a/system/bta/include/bta_api.h b/system/bta/include/bta_api.h
index 30e315c68c2..b9ae66e7a39 100644
--- a/system/bta/include/bta_api.h
+++ b/system/bta/include/bta_api.h
@@ -429,6 +429,7 @@ typedef struct {
   uint16_t ble_periodic_adv_int;
   tBT_DEVICE_TYPE device_type;
   uint8_t flag;
+  bool include_rsi; /* true, if ADV contains RSI data */
 } tBTA_DM_INQ_RES;
 
 /* Structure associated with BTA_DM_INQ_CMPL_EVT */
diff --git a/system/btif/src/btif_dm.cc b/system/btif/src/btif_dm.cc
index 79ba7ade2ac..09145250d0f 100644
--- a/system/btif/src/btif_dm.cc
+++ b/system/btif/src/btif_dm.cc
@@ -1181,7 +1181,7 @@ static void btif_dm_search_devices_evt(tBTA_DM_SEARCH_EVT event,
       }
 
       {
-        bt_property_t properties[5];
+        bt_property_t properties[6];
         bt_device_type_t dev_type;
         uint32_t num_properties = 0;
         bt_status_t status;
@@ -1238,6 +1238,13 @@ static void btif_dm_search_devices_evt(tBTA_DM_SEARCH_EVT event,
                                    &(p_search_data->inq_res.rssi));
         num_properties++;
 
+        /* CSIP supported device */
+        BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
+                                   BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER,
+                                   sizeof(bool),
+                                   &(p_search_data->inq_res.include_rsi));
+        num_properties++;
+
         status =
             btif_storage_add_remote_device(&bdaddr, num_properties, properties);
         ASSERTC(status == BT_STATUS_SUCCESS,
diff --git a/system/include/hardware/bluetooth.h b/system/include/hardware/bluetooth.h
index 97d7d1d1473..24b1a89209f 100644
--- a/system/include/hardware/bluetooth.h
+++ b/system/include/hardware/bluetooth.h
@@ -325,6 +325,13 @@ typedef enum {
 
   BT_PROPERTY_DYNAMIC_AUDIO_BUFFER,
 
+  /**
+   * Description - True if Remote is a Member of a Coordinated Set.
+   * Access mode - GET.
+   * Data Type - bool.
+   */
+  BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER,
+
   BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP = 0xFF,
 } bt_property_type_t;
 
diff --git a/system/stack/btm/btm_ble_gap.cc b/system/stack/btm/btm_ble_gap.cc
index 04688b35f97..9aa417b141f 100644
--- a/system/stack/btm/btm_ble_gap.cc
+++ b/system/stack/btm/btm_ble_gap.cc
@@ -1959,6 +1959,13 @@ void btm_ble_process_adv_pkt_cont(uint16_t evt_type, uint8_t addr_type,
     return;
   }
 
+  bool include_rsi = false;
+  uint8_t len;
+  if (AdvertiseDataParser::GetFieldByType(adv_data, BTM_BLE_AD_TYPE_RSI,
+                                          &len)) {
+    include_rsi = true;
+  }
+
   tINQ_DB_ENT* p_i = btm_inq_db_find(bda);
 
   /* Check if this address has already been processed for this inquiry */
@@ -1966,7 +1973,8 @@ void btm_ble_process_adv_pkt_cont(uint16_t evt_type, uint8_t addr_type,
     /* never been report as an LE device */
     if (p_i && (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ||
                 /* scan response to be updated */
-                (!p_i->scan_rsp))) {
+                (!p_i->scan_rsp) ||
+                (!p_i->inq_info.results.include_rsi && include_rsi))) {
       update = true;
     } else if (btm_cb.ble_ctr_cb.is_ble_observe_active()) {
       update = false;
@@ -1997,6 +2005,10 @@ void btm_ble_process_adv_pkt_cont(uint16_t evt_type, uint8_t addr_type,
                             secondary_phy, advertising_sid, tx_power, rssi,
                             periodic_adv_int, adv_data);
 
+  if (include_rsi) {
+    (&p_i->inq_info.results)->include_rsi = true;
+  }
+
   tBTM_INQ_RESULTS_CB* p_opportunistic_obs_results_cb =
       btm_cb.ble_ctr_cb.p_opportunistic_obs_results_cb;
   if (p_opportunistic_obs_results_cb) {
@@ -2041,6 +2053,13 @@ void btm_ble_process_adv_pkt_cont_for_inquiry(
   tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
   bool update = true;
 
+  bool include_rsi = false;
+  uint8_t len;
+  if (AdvertiseDataParser::GetFieldByType(advertising_data, BTM_BLE_AD_TYPE_RSI,
+                                          &len)) {
+    include_rsi = true;
+  }
+
   tINQ_DB_ENT* p_i = btm_inq_db_find(bda);
 
   /* Check if this address has already been processed for this inquiry */
@@ -2048,7 +2067,8 @@ void btm_ble_process_adv_pkt_cont_for_inquiry(
     /* never been report as an LE device */
     if (p_i && (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ||
                 /* scan response to be updated */
-                (!p_i->scan_rsp))) {
+                (!p_i->scan_rsp) ||
+                (!p_i->inq_info.results.include_rsi && include_rsi))) {
       update = true;
     } else if (btm_cb.ble_ctr_cb.is_ble_observe_active()) {
       update = false;
@@ -2079,6 +2099,10 @@ void btm_ble_process_adv_pkt_cont_for_inquiry(
                             secondary_phy, advertising_sid, tx_power, rssi,
                             periodic_adv_int, advertising_data);
 
+  if (include_rsi) {
+    (&p_i->inq_info.results)->include_rsi = true;
+  }
+
   tBTM_INQ_RESULTS_CB* p_opportunistic_obs_results_cb =
       btm_cb.ble_ctr_cb.p_opportunistic_obs_results_cb;
   if (p_opportunistic_obs_results_cb) {
diff --git a/system/stack/btm/neighbor_inquiry.h b/system/stack/btm/neighbor_inquiry.h
index 8d1b0d547b5..c36320e02cb 100644
--- a/system/stack/btm/neighbor_inquiry.h
+++ b/system/stack/btm/neighbor_inquiry.h
@@ -116,6 +116,7 @@ typedef struct {
   int8_t ble_tx_power;
   uint16_t ble_periodic_adv_int;
   uint8_t flag;
+  bool include_rsi;
 } tBTM_INQ_RESULTS;
 
 /****************************************
diff --git a/system/stack/include/btm_ble_api_types.h b/system/stack/include/btm_ble_api_types.h
index dfee8062930..2cd6a02cc7a 100644
--- a/system/stack/include/btm_ble_api_types.h
+++ b/system/stack/include/btm_ble_api_types.h
@@ -295,6 +295,7 @@ typedef void(tBTM_RAND_ENC_CB)(tBTM_RAND_ENC* p1);
   HCI_EIR_COMPLETE_16BITS_UUID_TYPE /* 0x03 \
                                        */
 #define BTM_BLE_AD_TYPE_APPEARANCE 0x19
+#define BTM_BLE_AD_TYPE_RSI HCI_EIR_RSI_TYPE /* 0x2E */
 
 /*  Min/max Preferred  number of payload octets that the local Controller
     should include in a single Link Layer Data Channel PDU. */
-- 
GitLab