From 8c2f8384a8a18aaba704bed51560ab95c1eb5697 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Tue, 13 Nov 2018 18:31:08 +0100 Subject: [PATCH] Centralize call to gatt_update_app_use_link_flag for GATT_Connect 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 | 32 +++++++++++++++++++++++--------- stack/gatt/gatt_int.h | 5 +---- stack/gatt/gatt_main.cc | 47 ++++++++++++++++++----------------------------- stack/gatt/gatt_utils.cc | 20 -------------------- 4 files changed, 42 insertions(+), 62 deletions(-) diff --git a/stack/gatt/gatt_api.cc b/stack/gatt/gatt_api.cc index 852f8018c..3b6b59bfc 100644 --- a/stack/gatt/gatt_api.cc +++ b/stack/gatt/gatt_api.cc @@ -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; } /******************************************************************************* diff --git a/stack/gatt/gatt_int.h b/stack/gatt/gatt_int.h index 6f882b603..198b6d176 100644 --- a/stack/gatt/gatt_int.h +++ b/stack/gatt/gatt_int.h @@ -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); diff --git a/stack/gatt/gatt_main.cc b/stack/gatt/gatt_main.cc index 51877ee20..375d3dfe7 100644 --- a/stack/gatt/gatt_main.cc +++ b/stack/gatt/gatt_main.cc @@ -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 diff --git a/stack/gatt/gatt_utils.cc b/stack/gatt/gatt_utils.cc index 246dea91a..91fecc4ac 100644 --- a/stack/gatt/gatt_utils.cc +++ b/stack/gatt/gatt_utils.cc @@ -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); -- 2.11.0