OSDN Git Service

Centralize call to gatt_update_app_use_link_flag for GATT_Connect
authorJakub Pawlowski <jpawlowski@google.com>
Tue, 13 Nov 2018 17:31:08 +0000 (18:31 +0100)
committerMyles Watson <mylesgw@google.com>
Wed, 28 Nov 2018 17:12:34 +0000 (17:12 +0000)
Currently both code paths, doing direct and background connection, call
gatt_update_app_use_link_flag. Move this call to common code.

Bug: 112827989
Test: sl4a GattConnectTest
Change-Id: I5d55f6467ffca78ff785ecdad02dd049ea5481bf

stack/gatt/gatt_api.cc
stack/gatt/gatt_int.h
stack/gatt/gatt_main.cc
stack/gatt/gatt_utils.cc

index 852f801..3b6b59b 100644 (file)
@@ -1099,25 +1099,39 @@ bool GATT_Connect(tGATT_IF gatt_if, const RawAddress& bd_addr, bool is_direct,
 bool GATT_Connect(tGATT_IF gatt_if, const RawAddress& bd_addr, bool is_direct,
                   tBT_TRANSPORT transport, bool opportunistic,
                   uint8_t initiating_phys) {
-  LOG(INFO) << __func__ << "gatt_if=" << +gatt_if << " " << bd_addr;
+  LOG(INFO) << __func__ << "gatt_if=" << +gatt_if << ", address=" << bd_addr;
 
   /* Make sure app is registered */
   tGATT_REG* p_reg = gatt_get_regcb(gatt_if);
   if (!p_reg) {
     LOG(ERROR) << "gatt_if = " << +gatt_if << " is not registered";
-    return (false);
+    return false;
   }
 
-  if (is_direct)
-    return gatt_act_connect(p_reg, bd_addr, transport, opportunistic,
-                            initiating_phys);
-
-  // is not direct
-  if (transport != BT_TRANSPORT_LE) {
+  if (!is_direct && transport != BT_TRANSPORT_LE) {
     LOG(ERROR) << "Unsupported transport for background connection";
     return false;
   }
-  return gatt_auto_connect_dev_add(gatt_if, bd_addr);
+
+  bool ret;
+  if (is_direct) {
+    ret = gatt_act_connect(p_reg, bd_addr, transport, initiating_phys);
+  } else {
+    ret = gatt::connection_manager::background_connect_add(gatt_if, bd_addr);
+  }
+
+  tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
+  // background connections don't necesarly create tcb
+  if (!p_tcb) return ret;
+
+  if (ret) {
+    if (!opportunistic)
+      gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, true, !is_direct);
+    else
+      VLOG(1) << __func__
+              << ": connection is opportunistic, not updating app usage";
+  }
+  return ret;
 }
 
 /*******************************************************************************
index 6f882b6..198b6d1 100644 (file)
@@ -396,8 +396,7 @@ extern void gatt_set_err_rsp(bool enable, uint8_t req_op_code,
 /* from gatt_main.cc */
 extern bool gatt_disconnect(tGATT_TCB* p_tcb);
 extern bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr,
-                             tBT_TRANSPORT transport, bool opportunistic,
-                             int8_t initiating_phys);
+                             tBT_TRANSPORT transport, int8_t initiating_phys);
 extern bool gatt_connect(const RawAddress& rem_bda, tGATT_TCB* p_tcb,
                          tBT_TRANSPORT transport, uint8_t initiating_phys);
 extern void gatt_data_process(tGATT_TCB& p_tcb, BT_HDR* p_buf);
@@ -467,8 +466,6 @@ extern tGATT_HDL_LIST_ELEM* gatt_find_hdl_buffer_by_handle(uint16_t handle);
 extern tGATTS_SRV_CHG* gatt_add_srv_chg_clt(tGATTS_SRV_CHG* p_srv_chg);
 
 /* for background connection */
-extern bool gatt_auto_connect_dev_add(tGATT_IF gatt_if,
-                                      const RawAddress& bd_addr);
 extern bool gatt_auto_connect_dev_remove(tGATT_IF gatt_if,
                                          const RawAddress& bd_addr);
 
index 51877ee..375d3df 100644 (file)
@@ -343,48 +343,37 @@ void gatt_update_app_use_link_flag(tGATT_IF gatt_if, tGATT_TCB* p_tcb,
 
 /** GATT connection initiation */
 bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr,
-                      tBT_TRANSPORT transport, bool opportunistic,
-                      int8_t initiating_phys) {
-  bool ret = false;
-
+                      tBT_TRANSPORT transport, int8_t initiating_phys) {
   tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
   if (p_tcb != NULL) {
-    ret = true;
-    uint8_t st = gatt_get_ch_state(p_tcb);
-
     /* before link down, another app try to open a GATT connection */
+    uint8_t st = gatt_get_ch_state(p_tcb);
     if (st == GATT_CH_OPEN && p_tcb->app_hold_link.empty() &&
         transport == BT_TRANSPORT_LE) {
       if (!gatt_connect(bd_addr, p_tcb, transport, initiating_phys))
-        ret = false;
+        return false;
     } else if (st == GATT_CH_CLOSING) {
       /* need to complete the closing first */
-      ret = false;
-    }
-  } else {
-    p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, transport);
-    if (!p_tcb) {
-      ret = 0;
-      LOG(ERROR) << "Max TCB for gatt_if [ " << +p_reg->gatt_if << "] reached.";
-    } else {
-      if (!gatt_connect(bd_addr, p_tcb, transport, initiating_phys)) {
-        LOG(ERROR) << "gatt_connect failed";
-        fixed_queue_free(p_tcb->pending_ind_q, NULL);
-        *p_tcb = tGATT_TCB();
-      } else
-        ret = true;
+      return false;
     }
+
+    return true;
   }
 
-  if (ret) {
-    if (!opportunistic)
-      gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, true, false);
-    else
-      VLOG(1) << __func__
-              << ": connection is opportunistic, not updating app usage";
+  p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, transport);
+  if (!p_tcb) {
+    LOG(ERROR) << "Max TCB for gatt_if [ " << +p_reg->gatt_if << "] reached.";
+    return false;
   }
 
-  return ret;
+  if (!gatt_connect(bd_addr, p_tcb, transport, initiating_phys)) {
+    LOG(ERROR) << "gatt_connect failed";
+    fixed_queue_free(p_tcb->pending_ind_q, NULL);
+    *p_tcb = tGATT_TCB();
+    return false;
+  }
+
+  return true;
 }
 
 /** This callback function is called by L2CAP to indicate that the ATT fixed
index 246dea9..91fecc4 100644 (file)
@@ -1296,26 +1296,6 @@ uint8_t* gatt_dbg_op_name(uint8_t op_code) {
     return (uint8_t*)"Op Code Exceed Max";
 }
 
-/**
- * This function add a device for background connection procedure.
- *
- * Parameters       p_reg: application record,
- *                  bd_addr: peer device address.
- *
- * Returns          true if connection started; false otherwise.
- */
-bool gatt_auto_connect_dev_add(tGATT_IF gatt_if, const RawAddress& bd_addr) {
-  VLOG(1) << __func__;
-
-  tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);
-  bool ret = gatt::connection_manager::background_connect_add(gatt_if, bd_addr);
-  if (ret && p_tcb != NULL) {
-    /* if a connected device, update the link holding number */
-    gatt_update_app_use_link_flag(gatt_if, p_tcb, true, true);
-  }
-  return ret;
-}
-
 /** Remove the application interface for the specified background device */
 bool gatt_auto_connect_dev_remove(tGATT_IF gatt_if, const RawAddress& bd_addr) {
   tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);