/*******************************************************************************
*
* Function GATT_Connect
*
* Description This function initiate a connecttion to a remote device on
* GATT channel.
*
* Parameters gatt_if: applicaiton interface
* bd_addr: peer device address.
* is_direct: is a direct conenection or a background auto
* connection
*
* Returns true if connection started; false if connection start
* failure.
*
******************************************************************************/
bool GATT_Connect(tGATT_IF gatt_if, const RawAddress& bd_addr, bool is_direct,
tBT_TRANSPORT transport, bool opportunistic) {
uint8_t phy = controller_get_interface()->get_le_all_initiating_phys();
return GATT_Connect(gatt_if, bd_addr, is_direct, transport, opportunistic,
phy);
}
bool GATT_Connect(tGATT_IF gatt_if, const RawAddress& bd_addr, bool is_direct,
tBT_TRANSPORT transport, bool opportunistic,
uint8_t initiating_phys) {
/* Make sure app is registered */
tGATT_REG* p_reg = gatt_get_regcb(gatt_if);
if (!p_reg) {
LOG(ERROR) << __func__
<< ": Unable to find registered app gatt_if=" << +gatt_if;
return false;
}
if (!is_direct && transport != BT_TRANSPORT_LE) {
LOG(ERROR) << __func__
<< ": Unsupported transport for background connection gatt_if="
<< +gatt_if;
return false;
}
if (opportunistic) {
LOG(INFO) << __func__
<< ": Registered for opportunistic connection gatt_if="
<< +gatt_if;
return true;
}
bool ret;
if (is_direct) {
LOG_DEBUG("Starting direct connect gatt_if=%u address=%s", gatt_if,
bd_addr.ToString().c_str());
ret = gatt_act_connect(p_reg, bd_addr, transport, initiating_phys);
} else {
LOG_DEBUG("Starting background connect gatt_if=%u address=%s", gatt_if,
bd_addr.ToString().c_str());
if (!BTM_BackgroundConnectAddressKnown(bd_addr)) {
// RPA can rotate, causing address to "expire" in the background
// connection list. RPA is allowed for direct connect, as such request
// times out after 30 seconds
LOG(INFO) << __func__
<< ": Unable to add RPA to background connection gatt_if="
<< +gatt_if;
ret = true;
} else {
LOG_DEBUG("Adding to acceptlist device:%s", PRIVATE_ADDRESS(bd_addr));
ret = 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 necessarily create tcb
if (p_tcb && ret)
gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, true, !is_direct);
return ret;
}