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;
}
/*******************************************************************************
/* 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);
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);
/** 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
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);