Read by UUID for PTS tests (4/5)

Add a hidden api for reading characteristic by UUID for PTS.

Bug: 35150313
Test: sl4a GattReadTest.byUuid
Change-Id: I072473c5f7b761707774efd89f8a22bfdf012135
diff --git a/bta/gatt/bta_gattc_act.cc b/bta/gatt/bta_gattc_act.cc
index 413e9e7..3ff6457 100644
--- a/bta/gatt/bta_gattc_act.cc
+++ b/bta/gatt/bta_gattc_act.cc
@@ -933,13 +933,23 @@
 void bta_gattc_read(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data) {
   if (!bta_gattc_enqueue(p_clcb, p_data)) return;
 
-  tGATT_READ_PARAM read_param;
-  memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
-  read_param.by_handle.handle = p_data->api_read.handle;
-  read_param.by_handle.auth_req = p_data->api_read.auth_req;
+  tBTA_GATT_STATUS status;
+  if (p_data->api_read.handle != 0) {
+    tGATT_READ_PARAM read_param;
+    memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
+    read_param.by_handle.handle = p_data->api_read.handle;
+    read_param.by_handle.auth_req = p_data->api_read.auth_req;
+    status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
+  } else {
+    tGATT_READ_PARAM read_param;
+    memset(&read_param, 0, sizeof(tGATT_READ_BY_TYPE));
 
-  tBTA_GATT_STATUS status =
-      GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
+    read_param.char_type.s_handle = p_data->api_read.s_handle;
+    read_param.char_type.e_handle = p_data->api_read.e_handle;
+    read_param.char_type.uuid = p_data->api_read.uuid;
+    read_param.char_type.auth_req = p_data->api_read.auth_req;
+    status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_TYPE, &read_param);
+  }
 
   /* read fail */
   if (status != BTA_GATT_OK) {
@@ -1080,7 +1090,11 @@
   GATT_READ_OP_CB cb = p_clcb->p_q_cmd->api_read.read_cb;
   void* my_cb_data = p_clcb->p_q_cmd->api_read.read_cb_data;
 
+  // if it was read by handle, return the handle requested, if read by UUID, use
+  // handle returned from remote
   uint16_t handle = p_clcb->p_q_cmd->api_read.handle;
+  if (handle == 0) handle = p_data->p_cmpl->att_value.handle;
+
   osi_free_and_reset((void**)&p_clcb->p_q_cmd);
 
   if (cb) {
diff --git a/bta/gatt/bta_gattc_api.cc b/bta/gatt/bta_gattc_api.cc
index 58eb834..e024b02 100644
--- a/bta/gatt/bta_gattc_api.cc
+++ b/bta/gatt/bta_gattc_api.cc
@@ -353,6 +353,30 @@
   bta_sys_sendmsg(p_buf);
 }
 
+/**
+ * This function is called to read a value of characteristic with uuid equal to
+ * |uuid|
+ */
+void BTA_GATTC_ReadUsingCharUuid(uint16_t conn_id, tBT_UUID uuid,
+                                 uint16_t s_handle, uint16_t e_handle,
+                                 tBTA_GATT_AUTH_REQ auth_req,
+                                 GATT_READ_OP_CB callback, void* cb_data) {
+  tBTA_GATTC_API_READ* p_buf =
+      (tBTA_GATTC_API_READ*)osi_calloc(sizeof(tBTA_GATTC_API_READ));
+
+  p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
+  p_buf->hdr.layer_specific = conn_id;
+  p_buf->auth_req = auth_req;
+  p_buf->handle = 0;
+  p_buf->uuid = uuid;
+  p_buf->s_handle = s_handle;
+  p_buf->e_handle = e_handle;
+  p_buf->read_cb = callback;
+  p_buf->read_cb_data = cb_data;
+
+  bta_sys_sendmsg(p_buf);
+}
+
 /*******************************************************************************
  *
  * Function         BTA_GATTC_ReadCharDescr
diff --git a/bta/gatt/bta_gattc_int.h b/bta/gatt/bta_gattc_int.h
index 0c39b07..68cf5a0 100644
--- a/bta/gatt/bta_gattc_int.h
+++ b/bta/gatt/bta_gattc_int.h
@@ -109,7 +109,15 @@
 typedef struct {
   BT_HDR hdr;
   tBTA_GATT_AUTH_REQ auth_req;
+
+  // read by handle data
   uint16_t handle;
+
+  // read by UUID data
+  tBT_UUID uuid;
+  uint16_t s_handle;
+  uint16_t e_handle;
+
   tBTA_GATTC_EVT cmpl_evt;
   GATT_READ_OP_CB read_cb;
   void* read_cb_data;
diff --git a/bta/include/bta_gatt_api.h b/bta/include/bta_gatt_api.h
index 187800e..9e023b8 100644
--- a/bta/include/bta_gatt_api.h
+++ b/bta/include/bta_gatt_api.h
@@ -777,6 +777,15 @@
                                   tBTA_GATT_AUTH_REQ auth_req,
                                   GATT_READ_OP_CB callback, void* cb_data);
 
+/**
+ * This function is called to read a value of characteristic with uuid equal to
+ * |uuid|
+ */
+void BTA_GATTC_ReadUsingCharUuid(uint16_t conn_id, tBT_UUID uuid,
+                                 uint16_t s_handle, uint16_t e_handle,
+                                 tBTA_GATT_AUTH_REQ auth_req,
+                                 GATT_READ_OP_CB callback, void* cb_data);
+
 /*******************************************************************************
  *
  * Function         BTA_GATTC_ReadCharDescr
diff --git a/btif/src/btif_gatt_client.cc b/btif/src/btif_gatt_client.cc
index 0a069bd..c3db741 100644
--- a/btif/src/btif_gatt_client.cc
+++ b/btif/src/btif_gatt_client.cc
@@ -402,6 +402,32 @@
                                auth_req, read_char_cb, nullptr));
 }
 
+void read_using_char_uuid_cb(uint16_t conn_id, tGATT_STATUS status,
+                             uint16_t handle, uint16_t len, uint8_t* value,
+                             void* data) {
+  btgatt_read_params_t* params = new btgatt_read_params_t;
+  params->value_type = 0x00 /* GATTC_READ_VALUE_TYPE_VALUE */;
+  params->status = status;
+  params->handle = handle;
+  params->value.len = len;
+  CHECK(len <= BTGATT_MAX_ATTR_LEN);
+  if (len > 0) memcpy(params->value.value, value, len);
+
+  CLI_CBACK_IN_JNI(read_characteristic_cb, conn_id, status,
+                   base::Owned(params));
+}
+
+bt_status_t btif_gattc_read_using_char_uuid(int conn_id, bt_uuid_t* uuid,
+                                            uint16_t s_handle,
+                                            uint16_t e_handle, int auth_req) {
+  CHECK_BTGATT_INIT();
+  tBT_UUID bt_uuid;
+  btif_to_bta_uuid(&bt_uuid, uuid);
+  return do_in_jni_thread(Bind(&BTA_GATTC_ReadUsingCharUuid, conn_id, bt_uuid,
+                               s_handle, e_handle, auth_req,
+                               read_using_char_uuid_cb, nullptr));
+}
+
 void read_desc_cb(uint16_t conn_id, tGATT_STATUS status, uint16_t handle,
                   uint16_t len, uint8_t* value, void* data) {
   btgatt_read_params_t* params = new btgatt_read_params_t;
@@ -584,6 +610,7 @@
     btif_gattc_refresh,
     btif_gattc_search_service,
     btif_gattc_read_char,
+    btif_gattc_read_using_char_uuid,
     btif_gattc_write_char,
     btif_gattc_read_char_descr,
     btif_gattc_write_char_descr,
diff --git a/service/hal/fake_bluetooth_gatt_interface.cc b/service/hal/fake_bluetooth_gatt_interface.cc
index 7e426c5..f2593f5 100644
--- a/service/hal/fake_bluetooth_gatt_interface.cc
+++ b/service/hal/fake_bluetooth_gatt_interface.cc
@@ -108,6 +108,7 @@
     nullptr,  // refresh
     nullptr,  // search_service
     nullptr,  // read_characteristic
+    nullptr,  // read_using_characteristic_uuid
     nullptr,  // write_characteristic
     nullptr,  // read_descriptor
     nullptr,  // write_descriptor