blob: 16c6c45f1bb28219fa8647b7835b4db8e09b7f89 [file] [log] [blame]
/******************************************************************************
*
* Copyright (C) 2009-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/*******************************************************************************
*
* Filename: btif_hl.c
*
* Description: Health Device Profile Bluetooth Interface
*
*
******************************************************************************/
#define LOG_TAG "bt_btif_hl"
#include "btif_hl.h"
#include <base/logging.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <sys/prctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#include <hardware/bt_hl.h>
#include "bta_api.h"
#include "btif_common.h"
#include "btif_storage.h"
#include "btif_util.h"
#include "btu.h"
#include "mca_api.h"
#include "osi/include/list.h"
#include "osi/include/log.h"
#include "osi/include/osi.h"
#define MAX_DATATYPE_SUPPORTED 8
extern int btif_hl_update_maxfd(int max_org_s);
extern void btif_hl_select_monitor_callback(fd_set* p_cur_set,
UNUSED_ATTR fd_set* p_org_set);
extern void btif_hl_select_wakeup_callback(fd_set* p_org_set,
int wakeup_signal);
extern int btif_hl_update_maxfd(int max_org_s);
extern void btif_hl_select_monitor_callback(fd_set* p_cur_set,
UNUSED_ATTR fd_set* p_org_set);
extern void btif_hl_select_wakeup_callback(fd_set* p_org_set,
int wakeup_signal);
extern void btif_hl_soc_thread_init(void);
extern void btif_hl_release_mcl_sockets(uint8_t app_idx, uint8_t mcl_idx);
extern bool btif_hl_create_socket(uint8_t app_idx, uint8_t mcl_idx,
uint8_t mdl_idx);
extern void btif_hl_release_socket(uint8_t app_idx, uint8_t mcl_idx,
uint8_t mdl_idx);
extern fixed_queue_t* btu_general_alarm_queue;
btif_hl_cb_t btif_hl_cb;
btif_hl_cb_t* p_btif_hl_cb = &btif_hl_cb;
/*******************************************************************************
* Static variables
******************************************************************************/
static bthl_callbacks_t bt_hl_callbacks_cb;
static bthl_callbacks_t* bt_hl_callbacks = NULL;
/* signal socketpair to wake up select loop */
const int btif_hl_signal_select_wakeup = 1;
const int btif_hl_signal_select_exit = 2;
const int btif_hl_signal_select_close_connected = 3;
static int listen_s = -1;
static int connected_s = -1;
static pthread_t select_thread_id = -1;
static int signal_fds[2] = {-1, -1};
static list_t* soc_queue;
static int reg_counter;
static inline int btif_hl_select_wakeup(void);
static inline int btif_hl_select_close_connected(void);
static inline int btif_hl_close_select_thread(void);
static uint8_t btif_hl_get_next_app_id(void);
static int btif_hl_get_next_channel_id(uint8_t app_id);
static void btif_hl_init_next_app_id(void);
static void btif_hl_init_next_channel_id(void);
static void btif_hl_ctrl_cback(tBTA_HL_CTRL_EVT event, tBTA_HL_CTRL* p_data);
static void btif_hl_set_state(btif_hl_state_t state);
static btif_hl_state_t btif_hl_get_state(void);
static void btif_hl_cback(tBTA_HL_EVT event, tBTA_HL* p_data);
static void btif_hl_proc_cb_evt(uint16_t event, char* p_param);
#define CHECK_CALL_CBACK(P_CB, P_CBACK, ...) \
do { \
if ((P_CB) && (P_CB)->P_CBACK) { \
(P_CB)->P_CBACK(__VA_ARGS__); \
} else { \
ASSERTC(0, "Callback is NULL", 0); \
} \
} while (0)
#define BTIF_HL_CALL_CBACK(P_CB, P_CBACK, ...) \
do { \
if ((p_btif_hl_cb->state != BTIF_HL_STATE_DISABLING) && \
(p_btif_hl_cb->state != BTIF_HL_STATE_DISABLED)) { \
if ((P_CB) && (P_CB)->P_CBACK) { \
(P_CB)->P_CBACK(__VA_ARGS__); \
} else { \
ASSERTC(0, "Callback is NULL", 0); \
} \
} \
} while (0)
#define CHECK_BTHL_INIT() \
do { \
if (bt_hl_callbacks == NULL) { \
BTIF_TRACE_WARNING("BTHL: %s: BTHL not initialized", __func__); \
return BT_STATUS_NOT_READY; \
} \
} while (0)
static const btif_hl_data_type_cfg_t data_type_table[] = {
/* Data Specilization Ntx Nrx (from Bluetooth SIG's
HDP whitepaper)*/
{BTIF_HL_DATA_TYPE_PULSE_OXIMETER, 9216, 256},
{BTIF_HL_DATA_TYPE_BLOOD_PRESSURE_MON, 896, 224},
{BTIF_HL_DATA_TYPE_BODY_THERMOMETER, 896, 224},
{BTIF_HL_DATA_TYPE_BODY_WEIGHT_SCALE, 896, 224},
{BTIF_HL_DATA_TYPE_GLUCOSE_METER, 896, 224},
{BTIF_HL_DATA_TYPE_STEP_COUNTER, 6624, 224},
{BTIF_HL_DATA_TYPE_BCA, 7730, 1230},
{BTIF_HL_DATA_TYPE_PEAK_FLOW, 2030, 224},
{BTIF_HL_DATA_TYPE_ACTIVITY_HUB, 5120, 224},
{BTIF_HL_DATA_TYPE_AMM, 1024, 64}};
#define BTIF_HL_DATA_TABLE_SIZE \
(sizeof(data_type_table) / sizeof(btif_hl_data_type_cfg_t))
#define BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE \
10240 /* use this size if the data type is not \
defined in the table; for future proof */
#define BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE \
512 /* use this size if the data type is not \
defined in the table; for future proof */
#define BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE 1024
/*******************************************************************************
* Static utility functions
******************************************************************************/
#define BTIF_IF_GET_NAME 16
void btif_hl_display_calling_process_name(void) {
char name[16];
prctl(BTIF_IF_GET_NAME, name, 0, 0, 0);
BTIF_TRACE_DEBUG("Process name (%s)", name);
}
#define BTIF_TIMEOUT_CCH_NO_DCH_MS (30 * 1000)
/*******************************************************************************
*
* Function btif_hl_if_channel_setup_pending
*
* Description check whether channel id is in setup pending state or not
*
* Returns bool
*
******************************************************************************/
bool btif_hl_if_channel_setup_pending(int channel_id, uint8_t* p_app_idx,
uint8_t* p_mcl_idx) {
btif_hl_app_cb_t* p_acb;
btif_hl_mcl_cb_t* p_mcb;
uint8_t i, j;
bool found = false;
*p_app_idx = 0;
*p_mcl_idx = 0;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
p_acb = BTIF_HL_GET_APP_CB_PTR(i);
if (p_acb->in_use) {
for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j);
if (p_mcb->in_use && p_mcb->is_connected &&
p_mcb->pcb.channel_id == channel_id) {
found = true;
*p_app_idx = i;
*p_mcl_idx = j;
break;
}
}
}
if (found) break;
}
BTIF_TRACE_DEBUG("%s found=%d channel_id=0x%08x", __func__, found, channel_id,
*p_app_idx, *p_mcl_idx);
return found;
}
/*******************************************************************************
*
* Function btif_hl_num_dchs_in_use
*
* Description find number of DCHs in use
*
* Returns uint8_t
******************************************************************************/
uint8_t btif_hl_num_dchs_in_use(uint8_t mcl_handle) {
btif_hl_app_cb_t* p_acb;
btif_hl_mcl_cb_t* p_mcb;
uint8_t i, j, x;
uint8_t cnt = 0;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
BTIF_TRACE_DEBUG("btif_hl_num_dchs:i = %d", i);
p_acb = BTIF_HL_GET_APP_CB_PTR(i);
if (p_acb && p_acb->in_use) {
for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
if (p_acb->mcb[j].in_use)
BTIF_TRACE_DEBUG(
"btif_hl_num_dchs:mcb in use j=%d, mcl_handle=%d,mcb handle=%d",
j, mcl_handle, p_acb->mcb[j].mcl_handle);
if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
p_mcb = &p_acb->mcb[j];
BTIF_TRACE_DEBUG("btif_hl_num_dchs: mcl handle found j =%d", j);
for (x = 0; x < BTA_HL_NUM_MDLS_PER_MCL; x++) {
if (p_mcb->mdl[x].in_use) {
BTIF_TRACE_DEBUG("btif_hl_num_dchs_in_use:found x =%d", x);
cnt++;
}
}
}
}
}
}
BTIF_TRACE_DEBUG("%s dch in use count=%d", __func__, cnt);
return cnt;
}
/*******************************************************************************
*
* Function btif_hl_timer_timeout
*
* Description Process timer timeout
*
* Returns void
******************************************************************************/
void btif_hl_timer_timeout(void* data) {
btif_hl_mcl_cb_t* p_mcb = (btif_hl_mcl_cb_t*)data;
BTIF_TRACE_DEBUG("%s", __func__);
if (p_mcb->is_connected) {
BTIF_TRACE_DEBUG("Idle timeout Close CCH mcl_handle=%d", p_mcb->mcl_handle);
BTA_HlCchClose(p_mcb->mcl_handle);
} else {
BTIF_TRACE_DEBUG("CCH idle timeout But CCH not connected");
}
}
/*******************************************************************************
*
* Function btif_hl_stop_cch_timer
*
* Description stop CCH timer
*
* Returns void
******************************************************************************/
void btif_hl_stop_cch_timer(uint8_t app_idx, uint8_t mcl_idx) {
btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx);
alarm_cancel(p_mcb->cch_timer);
}
/*******************************************************************************
*
* Function btif_hl_start_cch_timer
*
* Description start CCH timer
*
* Returns void
******************************************************************************/
void btif_hl_start_cch_timer(uint8_t app_idx, uint8_t mcl_idx) {
btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx);
alarm_free(p_mcb->cch_timer);
p_mcb->cch_timer = alarm_new("btif_hl.mcl_cch_timer");
alarm_set_on_queue(p_mcb->cch_timer, BTIF_TIMEOUT_CCH_NO_DCH_MS,
btif_hl_timer_timeout, p_mcb, btu_general_alarm_queue);
}
/*******************************************************************************
*
* Function btif_hl_find_mdl_idx
*
* Description Find the MDL index using MDL ID
*
* Returns bool
*
******************************************************************************/
static bool btif_hl_find_mdl_idx(uint8_t app_idx, uint8_t mcl_idx,
uint16_t mdl_id, uint8_t* p_mdl_idx) {
btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
bool found = false;
uint8_t i;
for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) {
if (p_mcb->mdl[i].in_use && (mdl_id != 0) &&
(p_mcb->mdl[i].mdl_id == mdl_id)) {
found = true;
*p_mdl_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d mdl_id=%d mdl_idx=%d ", __func__, found, mdl_id,
i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_is_the_first_reliable_existed
*
* Description This function checks whether the first reliable DCH channel
* has been setup on the MCL or not
*
* Returns bool - true exist
* false does not exist
*
******************************************************************************/
bool btif_hl_is_the_first_reliable_existed(uint8_t app_idx, uint8_t mcl_idx) {
btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
bool is_existed = false;
uint8_t i;
for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) {
if (p_mcb->mdl[i].in_use && p_mcb->mdl[i].is_the_first_reliable) {
is_existed = true;
break;
}
}
BTIF_TRACE_DEBUG("bta_hl_is_the_first_reliable_existed is_existed=%d ",
is_existed);
return is_existed;
}
/*******************************************************************************
*
* Function btif_hl_clean_delete_mdl
*
* Description Cleanup the delete mdl control block
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_clean_delete_mdl(btif_hl_delete_mdl_t* p_cb) {
BTIF_TRACE_DEBUG("%s", __func__);
memset(p_cb, 0, sizeof(btif_hl_delete_mdl_t));
}
/*******************************************************************************
*
* Function btif_hl_clean_pcb
*
* Description Cleanup the pending chan control block
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_clean_pcb(btif_hl_pending_chan_cb_t* p_pcb) {
BTIF_TRACE_DEBUG("%s", __func__);
memset(p_pcb, 0, sizeof(btif_hl_pending_chan_cb_t));
}
/*******************************************************************************
*
* Function btif_hl_clean_mdl_cb
*
* Description Cleanup the MDL control block
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_clean_mdl_cb(btif_hl_mdl_cb_t* p_dcb) {
BTIF_TRACE_DEBUG("%s", __func__);
osi_free_and_reset((void**)&p_dcb->p_rx_pkt);
osi_free_and_reset((void**)&p_dcb->p_tx_pkt);
memset(p_dcb, 0, sizeof(btif_hl_mdl_cb_t));
}
/*******************************************************************************
*
* Function btif_hl_reset_mcb
*
* Description Reset MCL control block
*
* Returns bool
*
******************************************************************************/
static void btif_hl_clean_mcl_cb(uint8_t app_idx, uint8_t mcl_idx) {
btif_hl_mcl_cb_t* p_mcb;
BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx);
p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
alarm_free(p_mcb->cch_timer);
memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t));
}
/*******************************************************************************
*
* Function btif_hl_find_sdp_idx_using_mdep_filter
*
* Description This function finds the SDP record index using MDEP filter
* parameters
*
* Returns bool
*
******************************************************************************/
static void btif_hl_reset_mdep_filter(uint8_t app_idx) {
btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
p_acb->filter.num_elems = 0;
}
/*******************************************************************************
*
* Function btif_hl_find_sdp_idx_using_mdep_filter
*
* Description This function finds the SDP record index using MDEP filter
* parameters
*
* Returns bool
*
******************************************************************************/
static bool btif_hl_find_sdp_idx_using_mdep_filter(uint8_t app_idx,
uint8_t mcl_idx,
uint8_t* p_sdp_idx) {
btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
uint8_t i, j, num_recs, num_elems, num_mdeps, mdep_idx;
tBTA_HL_MDEP_ROLE peer_mdep_role;
uint16_t data_type;
tBTA_HL_SDP_MDEP_CFG* p_mdep;
bool found = false;
bool elem_found;
BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter");
num_recs = p_mcb->sdp.num_recs;
num_elems = p_acb->filter.num_elems;
if (!num_elems) {
BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter num_elem=0");
*p_sdp_idx = 0;
found = true;
return found;
}
for (i = 0; i < num_recs; i++) {
num_mdeps = p_mcb->sdp.sdp_rec[i].num_mdeps;
for (j = 0; j < num_elems; j++) {
data_type = p_acb->filter.elem[j].data_type;
peer_mdep_role = p_acb->filter.elem[j].peer_mdep_role;
elem_found = false;
mdep_idx = 0;
while (!elem_found && mdep_idx < num_mdeps) {
p_mdep = &(p_mcb->sdp.sdp_rec[i].mdep_cfg[mdep_idx]);
if ((p_mdep->data_type == data_type) &&
(p_mdep->mdep_role == peer_mdep_role)) {
elem_found = true;
} else {
mdep_idx++;
}
}
if (!elem_found) {
found = false;
break;
} else {
found = true;
}
}
if (found) {
BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter found idx=%d",
i);
*p_sdp_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d sdp_idx=%d", __func__, found, *p_sdp_idx);
btif_hl_reset_mdep_filter(app_idx);
return found;
}
/*******************************************************************************
*
* Function btif_hl_is_reconnect_possible
*
* Description check reconnect is possible or not
*
* Returns bool
*
******************************************************************************/
bool btif_hl_is_reconnect_possible(uint8_t app_idx, uint8_t mcl_idx,
int mdep_cfg_idx,
tBTA_HL_DCH_OPEN_PARAM* p_dch_open_api,
tBTA_HL_MDL_ID* p_mdl_id) {
btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
tBTA_HL_DCH_CFG local_cfg = p_dch_open_api->local_cfg;
tBTA_HL_DCH_MODE dch_mode = BTA_HL_DCH_MODE_RELIABLE;
bool use_mdl_dch_mode = false;
btif_hl_mdl_cfg_t* p_mdl;
btif_hl_mdl_cfg_t* p_mdl1;
uint8_t i, j;
bool is_reconnect_ok = false;
bool stream_mode_avail = false;
uint16_t data_type =
p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type;
tBTA_HL_MDEP_ID peer_mdep_id = p_dch_open_api->peer_mdep_id;
uint8_t mdl_idx;
BTIF_TRACE_DEBUG("%s app_idx=%d mcl_idx=%d mdep_cfg_idx=%d", __func__,
app_idx, mcl_idx, mdep_cfg_idx);
switch (local_cfg) {
case BTA_HL_DCH_CFG_NO_PREF:
if (!btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) {
dch_mode = BTA_HL_DCH_MODE_RELIABLE;
} else {
use_mdl_dch_mode = true;
}
break;
case BTA_HL_DCH_CFG_RELIABLE:
dch_mode = BTA_HL_DCH_MODE_RELIABLE;
break;
case BTA_HL_DCH_CFG_STREAMING:
dch_mode = BTA_HL_DCH_MODE_STREAMING;
break;
default:
BTIF_TRACE_ERROR("Invalid local_cfg=%d", local_cfg);
return is_reconnect_ok;
break;
}
BTIF_TRACE_DEBUG("local_cfg=%d use_mdl_dch_mode=%d dch_mode=%d ", local_cfg,
use_mdl_dch_mode, dch_mode);
for (i = 0, p_mdl = &p_acb->mdl_cfg[0]; i < BTA_HL_NUM_MDL_CFGS;
i++, p_mdl++) {
if (p_mdl->base.active && p_mdl->extra.data_type == data_type &&
(p_mdl->extra.peer_mdep_id != BTA_HL_INVALID_MDEP_ID &&
p_mdl->extra.peer_mdep_id == peer_mdep_id) &&
memcpy(p_mdl->base.peer_bd_addr, p_mcb->bd_addr, sizeof(BD_ADDR)) &&
p_mdl->base.mdl_id &&
!btif_hl_find_mdl_idx(app_idx, mcl_idx, p_mdl->base.mdl_id, &mdl_idx)) {
BTIF_TRACE_DEBUG("i=%d Matched active=%d mdl_id =%d, mdl_dch_mode=%d",
i, p_mdl->base.active, p_mdl->base.mdl_id,
p_mdl->base.dch_mode);
if (!use_mdl_dch_mode) {
if (p_mdl->base.dch_mode == dch_mode) {
is_reconnect_ok = true;
*p_mdl_id = p_mdl->base.mdl_id;
BTIF_TRACE_DEBUG("reconnect is possible dch_mode=%d mdl_id=%d",
dch_mode, p_mdl->base.mdl_id);
break;
}
} else {
is_reconnect_ok = true;
for (j = i, p_mdl1 = &p_acb->mdl_cfg[i]; j < BTA_HL_NUM_MDL_CFGS;
j++, p_mdl1++) {
if (p_mdl1->base.active && p_mdl1->extra.data_type == data_type &&
(p_mdl1->extra.peer_mdep_id != BTA_HL_INVALID_MDEP_ID &&
p_mdl1->extra.peer_mdep_id == peer_mdep_id) &&
memcpy(p_mdl1->base.peer_bd_addr, p_mcb->bd_addr,
sizeof(BD_ADDR)) &&
p_mdl1->base.dch_mode == BTA_HL_DCH_MODE_STREAMING) {
stream_mode_avail = true;
BTIF_TRACE_DEBUG("found streaming mode mdl index=%d", j);
break;
}
}
if (stream_mode_avail) {
dch_mode = BTA_HL_DCH_MODE_STREAMING;
*p_mdl_id = p_mdl1->base.mdl_id;
BTIF_TRACE_DEBUG(
"reconnect is ok index=%d dch_mode=streaming mdl_id=%d", j,
*p_mdl_id);
break;
} else {
dch_mode = p_mdl->base.dch_mode;
*p_mdl_id = p_mdl->base.mdl_id;
BTIF_TRACE_DEBUG("reconnect is ok index=%d dch_mode=%d mdl_id=%d", i,
p_mdl->base.dch_mode, *p_mdl_id);
break;
}
}
}
}
BTIF_TRACE_DEBUG("is_reconnect_ok dch_mode=%d mdl_id=%d", is_reconnect_ok,
dch_mode, *p_mdl_id);
return is_reconnect_ok;
}
/*******************************************************************************
*
* Function btif_hl_dch_open
*
* Description Process DCH open request using the DCH Open API parameters
*
* Returns bool
*
******************************************************************************/
bool btif_hl_dch_open(uint8_t app_id, BD_ADDR bd_addr,
tBTA_HL_DCH_OPEN_PARAM* p_dch_open_api, int mdep_cfg_idx,
btif_hl_pend_dch_op_t op, int* channel_id) {
btif_hl_app_cb_t* p_acb;
btif_hl_mcl_cb_t* p_mcb;
btif_hl_pending_chan_cb_t* p_pcb;
uint8_t app_idx, mcl_idx;
bool status = false;
tBTA_HL_MDL_ID mdl_id;
tBTA_HL_DCH_RECONNECT_PARAM reconnect_param;
BTIF_TRACE_DEBUG("%s app_id=%d ", __func__, app_id);
BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr[0], bd_addr[1],
bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
if (btif_hl_find_app_idx(app_id, &app_idx)) {
if (btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) {
p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
if (!p_pcb->in_use) {
p_mcb->req_ctrl_psm = p_dch_open_api->ctrl_psm;
p_pcb->in_use = true;
*channel_id = p_pcb->channel_id =
(int)btif_hl_get_next_channel_id(app_id);
p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING;
p_pcb->mdep_cfg_idx = mdep_cfg_idx;
memcpy(p_pcb->bd_addr, bd_addr, sizeof(BD_ADDR));
p_pcb->op = op;
if (p_mcb->sdp.num_recs) {
if (p_mcb->valid_sdp_idx) {
p_dch_open_api->ctrl_psm = p_mcb->ctrl_psm;
}
if (!btif_hl_is_reconnect_possible(app_idx, mcl_idx, mdep_cfg_idx,
p_dch_open_api, &mdl_id)) {
BTIF_TRACE_DEBUG("Issue DCH open");
BTA_HlDchOpen(p_mcb->mcl_handle, p_dch_open_api);
} else {
reconnect_param.ctrl_psm = p_mcb->ctrl_psm;
reconnect_param.mdl_id = mdl_id;
;
BTIF_TRACE_DEBUG("Issue Reconnect ctrl_psm=0x%x mdl_id=0x%x",
reconnect_param.ctrl_psm, reconnect_param.mdl_id);
BTA_HlDchReconnect(p_mcb->mcl_handle, &reconnect_param);
}
status = true;
} else {
p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
p_mcb->cch_oper = BTIF_HL_CCH_OP_DCH_OPEN;
BTA_HlSdpQuery(app_id, p_acb->app_handle, bd_addr);
status = true;
}
}
}
}
BTIF_TRACE_DEBUG("status=%d ", status);
return status;
}
/*******************************************************************************
*
* Function btif_hl_copy_bda
*
* Description copy bt_bdaddr_t to BD_ADDR format
*
* Returns void
*
******************************************************************************/
void btif_hl_copy_bda(bt_bdaddr_t* bd_addr, BD_ADDR bda) {
uint8_t i;
for (i = 0; i < 6; i++) {
bd_addr->address[i] = bda[i];
}
}
/*******************************************************************************
*
* Function btif_hl_copy_bda
*
* Description display bt_bdaddr_t
*
* Returns bool
*
******************************************************************************/
void btif_hl_display_bt_bda(bt_bdaddr_t* bd_addr) {
BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr->address[0],
bd_addr->address[1], bd_addr->address[2],
bd_addr->address[3], bd_addr->address[4],
bd_addr->address[5]);
}
/*******************************************************************************
*
* Function btif_hl_dch_abort
*
* Description Process DCH abort request
*
* Returns Nothing
*
******************************************************************************/
void btif_hl_dch_abort(uint8_t app_idx, uint8_t mcl_idx) {
btif_hl_mcl_cb_t* p_mcb;
BTIF_TRACE_DEBUG("%s app_idx=%d mcl_idx=%d", __func__, app_idx, mcl_idx);
p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
if (p_mcb->is_connected) {
BTA_HlDchAbort(p_mcb->mcl_handle);
} else {
p_mcb->pcb.abort_pending = true;
}
}
/*******************************************************************************
*
* Function btif_hl_cch_open
*
* Description Process CCH open request
*
* Returns Nothing
*
******************************************************************************/
bool btif_hl_cch_open(uint8_t app_id, BD_ADDR bd_addr, uint16_t ctrl_psm,
int mdep_cfg_idx, btif_hl_pend_dch_op_t op,
int* channel_id) {
btif_hl_app_cb_t* p_acb;
btif_hl_mcl_cb_t* p_mcb;
btif_hl_pending_chan_cb_t* p_pcb;
uint8_t app_idx, mcl_idx;
bool status = true;
BTIF_TRACE_DEBUG("%s app_id=%d ctrl_psm=%d mdep_cfg_idx=%d op=%d", __func__,
app_id, ctrl_psm, mdep_cfg_idx, op);
BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr[0], bd_addr[1],
bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
if (btif_hl_find_app_idx(app_id, &app_idx)) {
p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
if (!btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) {
if (btif_hl_find_avail_mcl_idx(app_idx, &mcl_idx)) {
p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
alarm_free(p_mcb->cch_timer);
memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t));
p_mcb->in_use = true;
bdcpy(p_mcb->bd_addr, bd_addr);
if (!ctrl_psm) {
p_mcb->cch_oper = BTIF_HL_CCH_OP_MDEP_FILTERING;
} else {
p_mcb->cch_oper = BTIF_HL_CCH_OP_MATCHED_CTRL_PSM;
p_mcb->req_ctrl_psm = ctrl_psm;
}
p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
p_pcb->in_use = true;
p_pcb->mdep_cfg_idx = mdep_cfg_idx;
memcpy(p_pcb->bd_addr, bd_addr, sizeof(BD_ADDR));
p_pcb->op = op;
switch (op) {
case BTIF_HL_PEND_DCH_OP_OPEN:
*channel_id = p_pcb->channel_id =
(int)btif_hl_get_next_channel_id(app_id);
p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING;
break;
case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
p_pcb->channel_id = p_acb->delete_mdl.channel_id;
p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_DESTROYED_PENDING;
break;
default:
break;
}
BTA_HlSdpQuery(app_id, p_acb->app_handle, bd_addr);
} else {
status = false;
BTIF_TRACE_ERROR("Open CCH request discarded- No mcl cb");
}
} else {
status = false;
BTIF_TRACE_ERROR("Open CCH request discarded- already in USE");
}
} else {
status = false;
BTIF_TRACE_ERROR("Invalid app_id=%d", app_id);
}
if (channel_id) {
BTIF_TRACE_DEBUG("status=%d channel_id=0x%08x", status, *channel_id);
} else {
BTIF_TRACE_DEBUG("status=%d ", status);
}
return status;
}
/*******************************************************************************
*
* Function btif_hl_find_mdl_idx_using_handle
*
* Description Find the MDL index using channel id
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_mdl_cfg_idx_using_channel_id(int channel_id,
uint8_t* p_app_idx,
uint8_t* p_mdl_cfg_idx) {
btif_hl_app_cb_t* p_acb;
btif_hl_mdl_cfg_t* p_mdl;
bool found = false;
uint8_t i, j;
int mdl_cfg_channel_id;
*p_app_idx = 0;
*p_mdl_cfg_idx = 0;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
p_acb = BTIF_HL_GET_APP_CB_PTR(i);
for (j = 0; j < BTA_HL_NUM_MDL_CFGS; j++) {
p_mdl = BTIF_HL_GET_MDL_CFG_PTR(i, j);
mdl_cfg_channel_id = *(BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(i, j));
if (p_acb->in_use && p_mdl->base.active &&
(mdl_cfg_channel_id == channel_id)) {
found = true;
*p_app_idx = i;
*p_mdl_cfg_idx = j;
break;
}
}
}
BTIF_TRACE_EVENT("%s found=%d channel_id=0x%08x, app_idx=%d mdl_cfg_idx=%d ",
__func__, found, channel_id, i, j);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_mdl_idx_using_handle
*
* Description Find the MDL index using channel id
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_mdl_idx_using_channel_id(int channel_id, uint8_t* p_app_idx,
uint8_t* p_mcl_idx,
uint8_t* p_mdl_idx) {
btif_hl_app_cb_t* p_acb;
btif_hl_mcl_cb_t* p_mcb;
btif_hl_mdl_cb_t* p_dcb;
bool found = false;
uint8_t i, j, k;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
p_acb = BTIF_HL_GET_APP_CB_PTR(i);
for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j);
for (k = 0; k < BTA_HL_NUM_MDLS_PER_MCL; k++) {
p_dcb = BTIF_HL_GET_MDL_CB_PTR(i, j, k);
if (p_acb->in_use && p_mcb->in_use && p_dcb->in_use &&
(p_dcb->channel_id == channel_id)) {
found = true;
*p_app_idx = i;
*p_mcl_idx = j;
*p_mdl_idx = k;
break;
}
}
}
}
BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d ", __func__,
found, i, j, k);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_channel_id_using_mdl_id
*
* Description Find channel id using mdl_id'
*
* Returns bool
******************************************************************************/
bool btif_hl_find_channel_id_using_mdl_id(uint8_t app_idx,
tBTA_HL_MDL_ID mdl_id,
int* p_channel_id) {
btif_hl_app_cb_t* p_acb;
btif_hl_mdl_cfg_t* p_mdl;
bool found = false;
uint8_t j = 0;
int mdl_cfg_channel_id;
p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
if (p_acb && p_acb->in_use) {
for (j = 0; j < BTA_HL_NUM_MDL_CFGS; j++) {
p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, j);
mdl_cfg_channel_id = *(BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx, j));
if (p_mdl->base.active && (p_mdl->base.mdl_id == mdl_id)) {
found = true;
*p_channel_id = mdl_cfg_channel_id;
break;
}
}
}
BTIF_TRACE_EVENT(
"%s found=%d channel_id=0x%08x, mdl_id=0x%x app_idx=%d mdl_cfg_idx=%d ",
__func__, found, *p_channel_id, mdl_id, app_idx, j);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_mdl_idx_using_handle
*
* Description Find the MDL index using handle
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_mdl_idx_using_handle(tBTA_HL_MDL_HANDLE mdl_handle,
uint8_t* p_app_idx, uint8_t* p_mcl_idx,
uint8_t* p_mdl_idx) {
btif_hl_app_cb_t* p_acb;
btif_hl_mcl_cb_t* p_mcb;
btif_hl_mdl_cb_t* p_dcb;
bool found = false;
uint8_t i, j, k;
*p_app_idx = 0;
*p_mcl_idx = 0;
*p_mdl_idx = 0;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
p_acb = BTIF_HL_GET_APP_CB_PTR(i);
for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j);
for (k = 0; k < BTA_HL_NUM_MDLS_PER_MCL; k++) {
p_dcb = BTIF_HL_GET_MDL_CB_PTR(i, j, k);
if (p_acb->in_use && p_mcb->in_use && p_dcb->in_use &&
(p_dcb->mdl_handle == mdl_handle)) {
found = true;
*p_app_idx = i;
*p_mcl_idx = j;
*p_mdl_idx = k;
break;
}
}
}
}
BTIF_TRACE_EVENT("%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d ", __func__,
found, i, j, k);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_peer_mdep_id
*
* Description Find the peer MDEP ID from the received SPD records
*
* Returns bool
*
******************************************************************************/
static bool btif_hl_find_peer_mdep_id(uint8_t app_id, BD_ADDR bd_addr,
tBTA_HL_MDEP_ROLE local_mdep_role,
uint16_t data_type,
tBTA_HL_MDEP_ID* p_peer_mdep_id) {
uint8_t app_idx, mcl_idx;
btif_hl_mcl_cb_t* p_mcb;
tBTA_HL_SDP_REC* p_rec;
uint8_t i, num_mdeps;
bool found = false;
tBTA_HL_MDEP_ROLE peer_mdep_role;
BTIF_TRACE_DEBUG("%s app_id=%d local_mdep_role=%d, data_type=%d", __func__,
app_id, local_mdep_role, data_type);
BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr[0], bd_addr[1],
bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
BTIF_TRACE_DEBUG("local_mdep_role=%d", local_mdep_role);
BTIF_TRACE_DEBUG("data_type=%d", data_type);
if (local_mdep_role == BTA_HL_MDEP_ROLE_SINK)
peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
else
peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
if (btif_hl_find_app_idx(app_id, &app_idx)) {
if (btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) {
p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d", app_idx, mcl_idx);
BTIF_TRACE_DEBUG("valid_spd_idx=%d sdp_idx=%d", p_mcb->valid_sdp_idx,
p_mcb->sdp_idx);
if (p_mcb->valid_sdp_idx) {
p_rec = &p_mcb->sdp.sdp_rec[p_mcb->sdp_idx];
num_mdeps = p_rec->num_mdeps;
BTIF_TRACE_DEBUG("num_mdeps=%d", num_mdeps);
for (i = 0; i < num_mdeps; i++) {
BTIF_TRACE_DEBUG("p_rec->mdep_cfg[%d].mdep_role=%d", i,
p_rec->mdep_cfg[i].mdep_role);
BTIF_TRACE_DEBUG("p_rec->mdep_cfg[%d].data_type =%d", i,
p_rec->mdep_cfg[i].data_type);
if ((p_rec->mdep_cfg[i].mdep_role == peer_mdep_role) &&
(p_rec->mdep_cfg[i].data_type == data_type)) {
found = true;
*p_peer_mdep_id = p_rec->mdep_cfg[i].mdep_id;
break;
}
}
}
}
}
BTIF_TRACE_DEBUG("found =%d *p_peer_mdep_id=%d", found, *p_peer_mdep_id);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_mdep_cfg_idx
*
* Description Find the MDEP configuration index using local MDEP_ID
*
* Returns bool
*
******************************************************************************/
static bool btif_hl_find_mdep_cfg_idx(uint8_t app_idx,
tBTA_HL_MDEP_ID local_mdep_id,
uint8_t* p_mdep_cfg_idx) {
btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
tBTA_HL_SUP_FEATURE* p_sup_feature = &p_acb->sup_feature;
bool found = false;
uint8_t i;
for (i = 0; i < p_sup_feature->num_of_mdeps; i++) {
BTIF_TRACE_DEBUG("btif_hl_find_mdep_cfg_idx: mdep_id=%d app_idx = %d",
p_sup_feature->mdep[i].mdep_id, app_idx);
if (p_sup_feature->mdep[i].mdep_id == local_mdep_id) {
found = true;
*p_mdep_cfg_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d mdep_idx=%d local_mdep_id=%d app_idx=%d ",
__func__, found, i, local_mdep_id, app_idx);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_mcl_idx
*
* Description Find the MCL index using BD address
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_mcl_idx(uint8_t app_idx, BD_ADDR p_bd_addr,
uint8_t* p_mcl_idx) {
bool found = false;
uint8_t i;
btif_hl_mcl_cb_t* p_mcb;
*p_mcl_idx = 0;
for (i = 0; i < BTA_HL_NUM_MCLS; i++) {
p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, i);
if (p_mcb->in_use && (!memcmp(p_mcb->bd_addr, p_bd_addr, BD_ADDR_LEN))) {
found = true;
*p_mcl_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d idx=%d", __func__, found, i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_init
*
* Description HL initialization function.
*
* Returns void
*
******************************************************************************/
static void btif_hl_init(void) {
BTIF_TRACE_DEBUG("%s", __func__);
memset(p_btif_hl_cb, 0, sizeof(btif_hl_cb_t));
btif_hl_init_next_app_id();
btif_hl_init_next_channel_id();
}
/*******************************************************************************
*
* Function btif_hl_disable
*
* Description Disable initialization function.
*
* Returns void
*
******************************************************************************/
static void btif_hl_disable(void) {
BTIF_TRACE_DEBUG("%s", __func__);
if ((p_btif_hl_cb->state != BTIF_HL_STATE_DISABLING) &&
(p_btif_hl_cb->state != BTIF_HL_STATE_DISABLED)) {
btif_hl_set_state(BTIF_HL_STATE_DISABLING);
BTA_HlDisable();
}
}
/*******************************************************************************
*
* Function btif_hl_is_no_active_app
*
* Description Find whether or not any APP is still in use
*
* Returns bool
*
******************************************************************************/
static bool btif_hl_is_no_active_app(void) {
bool no_active_app = true;
uint8_t i;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
if (btif_hl_cb.acb[i].in_use) {
no_active_app = false;
break;
}
}
BTIF_TRACE_DEBUG("%s no_active_app=%d ", __func__, no_active_app);
return no_active_app;
}
/*******************************************************************************
*
* Function btif_hl_free_app_idx
*
* Description free an application control block
*
* Returns void
*
******************************************************************************/
static void btif_hl_free_app_idx(uint8_t app_idx) {
if ((app_idx < BTA_HL_NUM_APPS) && btif_hl_cb.acb[app_idx].in_use) {
btif_hl_cb.acb[app_idx].in_use = false;
for (size_t i = 0; i < BTA_HL_NUM_MCLS; i++)
alarm_free(btif_hl_cb.acb[app_idx].mcb[i].cch_timer);
memset(&btif_hl_cb.acb[app_idx], 0, sizeof(btif_hl_app_cb_t));
}
}
/*******************************************************************************
*
* Function btif_hl_set_state
*
* Description set HL state
*
* Returns void
*
******************************************************************************/
static void btif_hl_set_state(btif_hl_state_t state) {
BTIF_TRACE_DEBUG("btif_hl_set_state: %d ---> %d ", p_btif_hl_cb->state,
state);
p_btif_hl_cb->state = state;
}
/*******************************************************************************
*
* Function btif_hl_set_state
*
* Description get HL state
*
* Returns btif_hl_state_t
*
******************************************************************************/
static btif_hl_state_t btif_hl_get_state(void) {
BTIF_TRACE_DEBUG("btif_hl_get_state: %d ", p_btif_hl_cb->state);
return p_btif_hl_cb->state;
}
/*******************************************************************************
*
* Function btif_hl_find_data_type_idx
*
* Description Find the index in the data type table
*
* Returns bool
*
******************************************************************************/
static bool btif_hl_find_data_type_idx(uint16_t data_type, uint8_t* p_idx) {
bool found = false;
uint8_t i;
for (i = 0; i < BTIF_HL_DATA_TABLE_SIZE; i++) {
if (data_type_table[i].data_type == data_type) {
found = true;
*p_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d, data_type=0x%x idx=%d", __func__, found,
data_type, i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_get_max_tx_apdu_size
*
* Description Find the maximum TX APDU size for the specified data type and
* MDEP role
*
* Returns uint16_t
*
******************************************************************************/
uint16_t btif_hl_get_max_tx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role,
uint16_t data_type) {
uint8_t idx;
uint16_t max_tx_apdu_size = 0;
if (btif_hl_find_data_type_idx(data_type, &idx)) {
if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) {
max_tx_apdu_size = data_type_table[idx].max_tx_apdu_size;
} else {
max_tx_apdu_size = data_type_table[idx].max_rx_apdu_size;
}
} else {
if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) {
max_tx_apdu_size = BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE;
} else {
max_tx_apdu_size = BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE;
}
}
BTIF_TRACE_DEBUG("%s mdep_role=%d data_type=0x%4x size=%d", __func__,
mdep_role, data_type, max_tx_apdu_size);
return max_tx_apdu_size;
}
/*******************************************************************************
*
* Function btif_hl_get_max_rx_apdu_size
*
* Description Find the maximum RX APDU size for the specified data type and
* MDEP role
*
* Returns uint16_t
*
******************************************************************************/
uint16_t btif_hl_get_max_rx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role,
uint16_t data_type) {
uint8_t idx;
uint16_t max_rx_apdu_size = 0;
if (btif_hl_find_data_type_idx(data_type, &idx)) {
if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) {
max_rx_apdu_size = data_type_table[idx].max_rx_apdu_size;
} else {
max_rx_apdu_size = data_type_table[idx].max_tx_apdu_size;
}
} else {
if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) {
max_rx_apdu_size = BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE;
} else {
max_rx_apdu_size = BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE;
}
}
BTIF_TRACE_DEBUG("%s mdep_role=%d data_type=0x%4x size=%d", __func__,
mdep_role, data_type, max_rx_apdu_size);
return max_rx_apdu_size;
}
/*******************************************************************************
*
* Function btif_hl_if_channel_setup_pending
*
* Description
*
* Returns bool
*
******************************************************************************/
static bool btif_hl_get_bta_mdep_role(bthl_mdep_role_t mdep,
tBTA_HL_MDEP_ROLE* p) {
bool status = true;
switch (mdep) {
case BTHL_MDEP_ROLE_SOURCE:
*p = BTA_HL_MDEP_ROLE_SOURCE;
break;
case BTHL_MDEP_ROLE_SINK:
*p = BTA_HL_MDEP_ROLE_SINK;
break;
default:
*p = BTA_HL_MDEP_ROLE_SOURCE;
status = false;
break;
}
BTIF_TRACE_DEBUG("%s status=%d bta_mdep_role=%d (%d:btif)", __func__, status,
*p, mdep);
return status;
}
/*******************************************************************************
*
* Function btif_hl_get_bta_channel_type
*
* Description convert bthl channel type to BTA DCH channel type
*
* Returns bool
*
******************************************************************************/
static bool btif_hl_get_bta_channel_type(bthl_channel_type_t channel_type,
tBTA_HL_DCH_CFG* p) {
bool status = true;
switch (channel_type) {
case BTHL_CHANNEL_TYPE_RELIABLE:
*p = BTA_HL_DCH_CFG_RELIABLE;
break;
case BTHL_CHANNEL_TYPE_STREAMING:
*p = BTA_HL_DCH_CFG_STREAMING;
break;
case BTHL_CHANNEL_TYPE_ANY:
*p = BTA_HL_DCH_CFG_NO_PREF;
break;
default:
status = false;
break;
}
BTIF_TRACE_DEBUG("%s status = %d BTA DCH CFG=%d (1-rel 2-strm", __func__,
status, *p);
return status;
}
/*******************************************************************************
*
* Function btif_hl_get_next_app_id
*
* Description get next applcation id
*
* Returns uint8_t
*
******************************************************************************/
static uint8_t btif_hl_get_next_app_id() {
uint8_t next_app_id = btif_hl_cb.next_app_id;
btif_hl_cb.next_app_id++;
return next_app_id;
}
/*******************************************************************************
*
* Function btif_hl_get_next_channel_id
*
* Description get next channel id
*
* Returns int
*
******************************************************************************/
static int btif_hl_get_next_channel_id(uint8_t app_id) {
uint16_t next_channel_id = btif_hl_cb.next_channel_id;
int channel_id;
btif_hl_cb.next_channel_id++;
channel_id = (app_id << 16) + next_channel_id;
BTIF_TRACE_DEBUG("%s channel_id=0x%08x, app_id=0x%02x next_channel_id=0x%04x",
__func__, channel_id, app_id, next_channel_id);
return channel_id;
}
/*******************************************************************************
*
* Function btif_hl_get_app_id
*
* Description get the applicaiton id associated with the channel id
*
* Returns uint8_t
*
******************************************************************************/
static uint8_t btif_hl_get_app_id(int channel_id) {
uint8_t app_id = (uint8_t)(channel_id >> 16);
BTIF_TRACE_DEBUG("%s channel_id=0x%08x, app_id=0x%02x ", __func__, channel_id,
app_id);
return app_id;
}
/*******************************************************************************
*
* Function btif_hl_init_next_app_id
*
* Description initialize the application id
*
* Returns void
*
******************************************************************************/
static void btif_hl_init_next_app_id(void) { btif_hl_cb.next_app_id = 1; }
/*******************************************************************************
*
* Function btif_hl_init_next_channel_id
*
* Description initialize the channel id
*
* Returns void
*
******************************************************************************/
static void btif_hl_init_next_channel_id(void) {
btif_hl_cb.next_channel_id = 1;
}
/*******************************************************************************
*
* Function btif_hl_find_app_idx_using_handle
*
* Description Find the applicaiton index using handle
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_app_idx_using_handle(tBTA_HL_APP_HANDLE app_handle,
uint8_t* p_app_idx) {
bool found = false;
uint8_t i;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
if (btif_hl_cb.acb[i].in_use &&
(btif_hl_cb.acb[i].app_handle == app_handle)) {
found = true;
*p_app_idx = i;
break;
}
}
BTIF_TRACE_EVENT("%s status=%d handle=%d app_idx=%d ", __func__, found,
app_handle, i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_app_idx_using_app_id
*
* Description Find the applicaiton index using app_id
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_app_idx_using_app_id(uint8_t app_id, uint8_t* p_app_idx) {
bool found = false;
uint8_t i;
*p_app_idx = 0;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
if (btif_hl_cb.acb[i].in_use && (btif_hl_cb.acb[i].app_id == app_id)) {
found = true;
*p_app_idx = i;
break;
}
}
BTIF_TRACE_EVENT("%s found=%d app_id=%d app_idx=%d ", __func__, found, app_id,
i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_mcl_idx_using_handle
*
* Description Find the MCL index using handle
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_mcl_idx_using_handle(tBTA_HL_MCL_HANDLE mcl_handle,
uint8_t* p_app_idx, uint8_t* p_mcl_idx) {
btif_hl_app_cb_t* p_acb;
bool found = false;
uint8_t i, j;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
p_acb = BTIF_HL_GET_APP_CB_PTR(i);
for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
if (p_acb->mcb[j].in_use)
BTIF_TRACE_DEBUG(
"btif_hl_find_mcl_idx_using_handle:app_idx=%d,"
"mcl_idx =%d mcl_handle=%d",
i, j, p_acb->mcb[j].mcl_handle);
if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
found = true;
*p_app_idx = i;
*p_mcl_idx = j;
break;
}
}
}
BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d", __func__, found, i, j);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_mdl_idx_using_mdl_id
*
* Description Find the mdl index using mdl_id
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_mcl_idx_using_mdl_id(uint8_t mdl_id, uint8_t mcl_handle,
uint8_t* p_app_idx, uint8_t* p_mcl_idx) {
btif_hl_app_cb_t* p_acb;
btif_hl_mcl_cb_t* p_mcb;
bool found = false;
uint8_t i, j, x;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
p_acb = BTIF_HL_GET_APP_CB_PTR(i);
for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
p_mcb = &p_acb->mcb[j];
BTIF_TRACE_DEBUG(
"btif_hl_find_mcl_idx_using_mdl_id: mcl handle found j =%d", j);
for (x = 0; x < BTA_HL_NUM_MDLS_PER_MCL; x++) {
if (p_mcb->mdl[x].in_use && p_mcb->mdl[x].mdl_id == mdl_id) {
BTIF_TRACE_DEBUG("btif_hl_find_mcl_idx_using_mdl_id:found x =%d",
x);
found = true;
*p_app_idx = i;
*p_mcl_idx = j;
break;
}
}
}
}
}
BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d", __func__, found, i, j);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_mcl_idx_using_deleted_mdl_id
*
* Description Find the app index deleted_mdl_id
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_app_idx_using_deleted_mdl_id(uint8_t mdl_id,
uint8_t* p_app_idx) {
btif_hl_app_cb_t* p_acb;
bool found = false;
uint8_t i;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
p_acb = BTIF_HL_GET_APP_CB_PTR(i);
if (p_acb->delete_mdl.active) {
BTIF_TRACE_DEBUG("%s: app_idx=%d, mdl_id=%d", __func__, i, mdl_id);
}
if (p_acb->delete_mdl.active && (p_acb->delete_mdl.mdl_id == mdl_id)) {
found = true;
*p_app_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_stop_timer_using_handle
*
* Description clean control channel cb using handle
*
* Returns void
*
******************************************************************************/
static void btif_hl_stop_timer_using_handle(tBTA_HL_MCL_HANDLE mcl_handle) {
btif_hl_app_cb_t* p_acb;
uint8_t i, j;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
p_acb = BTIF_HL_GET_APP_CB_PTR(i);
for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
btif_hl_stop_cch_timer(i, j);
}
}
}
}
/*******************************************************************************
*
* Function btif_hl_find_mcl_idx_using_app_idx
*
* Description Find the MCL index using handle
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_mcl_idx_using_app_idx(tBTA_HL_MCL_HANDLE mcl_handle,
uint8_t p_app_idx, uint8_t* p_mcl_idx) {
btif_hl_app_cb_t* p_acb;
bool found = false;
uint8_t j;
p_acb = BTIF_HL_GET_APP_CB_PTR(p_app_idx);
for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
found = true;
*p_mcl_idx = j;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%dmcl_idx=%d", __func__, found, j);
return found;
}
/*******************************************************************************
*
* Function btif_hl_clean_mdls_using_app_idx
*
* Description clean dch cpntrol bloack using app_idx
*
* Returns void
*
******************************************************************************/
void btif_hl_clean_mdls_using_app_idx(uint8_t app_idx) {
btif_hl_app_cb_t* p_acb;
btif_hl_mcl_cb_t* p_mcb;
btif_hl_mdl_cb_t* p_dcb;
uint8_t j, x, y;
bt_bdaddr_t bd_addr;
p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
if (p_acb->mcb[j].in_use) {
p_mcb = &p_acb->mcb[j];
BTIF_TRACE_DEBUG(
"btif_hl_find_mcl_idx_using_mdl_id: mcl handle found j =%d", j);
for (x = 0; x < BTA_HL_NUM_MDLS_PER_MCL; x++) {
if (p_mcb->mdl[x].in_use) {
p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, j, x);
btif_hl_release_socket(app_idx, j, x);
for (y = 0; y < 6; y++) {
bd_addr.address[y] = p_mcb->bd_addr[y];
}
BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, p_acb->app_id,
&bd_addr, p_dcb->local_mdep_cfg_idx,
p_dcb->channel_id, BTHL_CONN_STATE_DISCONNECTED,
0);
btif_hl_clean_mdl_cb(p_dcb);
if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle))
BTA_HlCchClose(p_mcb->mcl_handle);
BTIF_TRACE_DEBUG("remote DCH close success mdl_idx=%d", x);
}
}
}
}
}
/*******************************************************************************
*
* Function btif_hl_find_app_idx
*
* Description Find the application index using application ID
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_app_idx(uint8_t app_id, uint8_t* p_app_idx) {
bool found = false;
uint8_t i;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
if (btif_hl_cb.acb[i].in_use && (btif_hl_cb.acb[i].app_id == app_id)) {
found = true;
*p_app_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_app_idx
*
* Description Find the application index using application ID
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_app_idx_using_mdepId(uint8_t mdep_id, uint8_t* p_app_idx) {
bool found = false;
uint8_t i;
*p_app_idx = 0;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
BTIF_TRACE_DEBUG("btif_hl_find_app_idx_using_mdepId: MDEP-ID = %d",
btif_hl_cb.acb[i].sup_feature.mdep[0].mdep_id);
if (btif_hl_cb.acb[i].in_use &&
(btif_hl_cb.acb[i].sup_feature.mdep[0].mdep_id == mdep_id)) {
found = true;
*p_app_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_avail_mdl_idx
*
* Description Find a not in-use MDL index
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_avail_mdl_idx(uint8_t app_idx, uint8_t mcl_idx,
uint8_t* p_mdl_idx) {
btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
bool found = false;
uint8_t i;
for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) {
if (!p_mcb->mdl[i].in_use) {
btif_hl_clean_mdl_cb(&p_mcb->mdl[i]);
found = true;
*p_mdl_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d idx=%d", __func__, found, i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_avail_mcl_idx
*
* Description Find a not in-use MDL index
*
* Returns bool
*
******************************************************************************/
bool btif_hl_find_avail_mcl_idx(uint8_t app_idx, uint8_t* p_mcl_idx) {
bool found = false;
uint8_t i;
for (i = 0; i < BTA_HL_NUM_MCLS; i++) {
if (!btif_hl_cb.acb[app_idx].mcb[i].in_use) {
found = true;
*p_mcl_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d mcl_idx=%d", __func__, found, i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_find_avail_app_idx
*
* Description Find a not in-use APP index
*
* Returns bool
*
******************************************************************************/
static bool btif_hl_find_avail_app_idx(uint8_t* p_idx) {
bool found = false;
uint8_t i;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
if (!btif_hl_cb.acb[i].in_use) {
found = true;
*p_idx = i;
break;
}
}
BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i);
return found;
}
/*******************************************************************************
*
* Function btif_hl_proc_dereg_cfm
*
* Description Process the de-registration confirmation
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_proc_dereg_cfm(tBTA_HL* p_data)
{
btif_hl_app_cb_t* p_acb;
uint8_t app_idx;
int app_id = 0;
bthl_app_reg_state_t state = BTHL_APP_REG_STATE_DEREG_SUCCESS;
BTIF_TRACE_DEBUG("%s de-reg status=%d app_handle=%d", __func__,
p_data->dereg_cfm.status, p_data->dereg_cfm.app_handle);
if (btif_hl_find_app_idx_using_app_id(p_data->dereg_cfm.app_id, &app_idx)) {
p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
app_id = (int)p_acb->app_id;
if (p_data->dereg_cfm.status == BTA_HL_STATUS_OK) {
btif_hl_clean_mdls_using_app_idx(app_idx);
for (size_t i = 0; i < BTA_HL_NUM_MCLS; i++)
alarm_free(p_acb->mcb[i].cch_timer);
memset(p_acb, 0, sizeof(btif_hl_app_cb_t));
} else
state = BTHL_APP_REG_STATE_DEREG_FAILED;
BTIF_TRACE_DEBUG("call reg state callback app_id=%d state=%d", app_id,
state);
BTIF_HL_CALL_CBACK(bt_hl_callbacks, app_reg_state_cb, app_id, state);
if (btif_hl_is_no_active_app()) {
btif_hl_disable();
}
}
}
/*******************************************************************************
*
* Function btif_hl_proc_reg_cfm
*
* Description Process the registration confirmation
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_proc_reg_cfm(tBTA_HL* p_data) {
btif_hl_app_cb_t* p_acb;
uint8_t app_idx;
bthl_app_reg_state_t state = BTHL_APP_REG_STATE_REG_SUCCESS;
BTIF_TRACE_DEBUG("%s reg status=%d app_handle=%d", __func__,
p_data->reg_cfm.status, p_data->reg_cfm.app_handle);
if (btif_hl_find_app_idx(p_data->reg_cfm.app_id, &app_idx)) {
p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
if (p_data->reg_cfm.status == BTA_HL_STATUS_OK) {
p_acb->app_handle = p_data->reg_cfm.app_handle;
} else {
btif_hl_free_app_idx(app_idx);
state = BTHL_APP_REG_STATE_REG_FAILED;
}
BTIF_TRACE_DEBUG("%s call reg state callback app_id=%d reg state=%d",
__func__, p_data->reg_cfm.app_id, state);
BTIF_HL_CALL_CBACK(bt_hl_callbacks, app_reg_state_cb,
((int)p_data->reg_cfm.app_id), state);
}
}
/*******************************************************************************
*
* Function btif_hl_set_chan_cb_state
*
* Description set the channel callback state
*
* Returns void
*
******************************************************************************/
void btif_hl_set_chan_cb_state(uint8_t app_idx, uint8_t mcl_idx,
btif_hl_chan_cb_state_t state) {
btif_hl_pending_chan_cb_t* p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
btif_hl_chan_cb_state_t cur_state = p_pcb->cb_state;
if (cur_state != state) {
p_pcb->cb_state = state;
BTIF_TRACE_DEBUG("%s state %d--->%d", __func__, cur_state, state);
}
}
/*******************************************************************************
*
* Function btif_hl_send_destroyed_cb
*
* Description send the channel destroyed callback
*
* Returns void
*
******************************************************************************/
void btif_hl_send_destroyed_cb(btif_hl_app_cb_t* p_acb) {
bt_bdaddr_t bd_addr;
int app_id = (int)btif_hl_get_app_id(p_acb->delete_mdl.channel_id);
btif_hl_copy_bda(&bd_addr, p_acb->delete_mdl.bd_addr);
BTIF_TRACE_DEBUG("%s", __func__);
BTIF_TRACE_DEBUG(
"call channel state callback channel_id=0x%08x mdep_cfg_idx=%d, state=%d "
"fd=%d",
p_acb->delete_mdl.channel_id, p_acb->delete_mdl.mdep_cfg_idx,
BTHL_CONN_STATE_DESTROYED, 0);
btif_hl_display_bt_bda(&bd_addr);
BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
p_acb->delete_mdl.mdep_cfg_idx,
p_acb->delete_mdl.channel_id, BTHL_CONN_STATE_DESTROYED,
0);
}
/*******************************************************************************
*
* Function btif_hl_send_disconnecting_cb
*
* Description send a channel disconnecting callback
*
* Returns void
*
******************************************************************************/
void btif_hl_send_disconnecting_cb(uint8_t app_idx, uint8_t mcl_idx,
uint8_t mdl_idx) {
btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
btif_hl_soc_cb_t* p_scb = p_dcb->p_scb;
bt_bdaddr_t bd_addr;
int app_id = (int)btif_hl_get_app_id(p_scb->channel_id);
btif_hl_copy_bda(&bd_addr, p_scb->bd_addr);
BTIF_TRACE_DEBUG("%s", __func__);
BTIF_TRACE_DEBUG(
"call channel state callback channel_id=0x%08x mdep_cfg_idx=%d, "
"state=%d fd=%d",
p_scb->channel_id, p_scb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTING,
p_scb->socket_id[0]);
btif_hl_display_bt_bda(&bd_addr);
BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
p_scb->mdep_cfg_idx, p_scb->channel_id,
BTHL_CONN_STATE_DISCONNECTING, p_scb->socket_id[0]);
}
/*******************************************************************************
*
* Function btif_hl_send_setup_connecting_cb
*
* Description send a channel connecting callback
*
* Returns void
*
******************************************************************************/
void btif_hl_send_setup_connecting_cb(uint8_t app_idx, uint8_t mcl_idx) {
btif_hl_pending_chan_cb_t* p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
bt_bdaddr_t bd_addr;
int app_id = (int)btif_hl_get_app_id(p_pcb->channel_id);
btif_hl_copy_bda(&bd_addr, p_pcb->bd_addr);
if (p_pcb->in_use &&
p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING) {
BTIF_TRACE_DEBUG("%s", __func__);
BTIF_TRACE_DEBUG(
"call channel state callback channel_id=0x%08x mdep_cfg_idx=%d "
"state=%d fd=%d",
p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_CONNECTING, 0);
btif_hl_display_bt_bda(&bd_addr);
BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
p_pcb->mdep_cfg_idx, p_pcb->channel_id,
BTHL_CONN_STATE_CONNECTING, 0);
btif_hl_set_chan_cb_state(app_idx, mcl_idx,
BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING);
}
}
/*******************************************************************************
*
* Function btif_hl_send_setup_disconnected_cb
*
* Description send a channel disconnected callback
*
* Returns void
*
******************************************************************************/
void btif_hl_send_setup_disconnected_cb(uint8_t app_idx, uint8_t mcl_idx) {
btif_hl_pending_chan_cb_t* p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
bt_bdaddr_t bd_addr;
int app_id = (int)btif_hl_get_app_id(p_pcb->channel_id);
btif_hl_copy_bda(&bd_addr, p_pcb->bd_addr);
BTIF_TRACE_DEBUG("%s p_pcb->in_use=%d", __func__, p_pcb->in_use);
if (p_pcb->in_use) {
BTIF_TRACE_DEBUG("%p_pcb->cb_state=%d", p_pcb->cb_state);
if (p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING) {
BTIF_TRACE_DEBUG(
"call channel state callback channel_id=0x%08x mdep_cfg_idx=%d "
"state=%d fd=%d",
p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_CONNECTING,
0);
btif_hl_display_bt_bda(&bd_addr);
BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
p_pcb->mdep_cfg_idx, p_pcb->channel_id,
BTHL_CONN_STATE_CONNECTING, 0);
BTIF_TRACE_DEBUG(
"call channel state callback channel_id=0x%08x mdep_cfg_idx=%d "
"state=%d fd=%d",
p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTED,
0);
btif_hl_display_bt_bda(&bd_addr);
BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
p_pcb->mdep_cfg_idx, p_pcb->channel_id,
BTHL_CONN_STATE_DISCONNECTED, 0);
} else if (p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING) {
BTIF_TRACE_DEBUG(
"call channel state callback channel_id=0x%08x mdep_cfg_idx=%d "
"state=%d fd=%d",
p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTED,
0);
btif_hl_display_bt_bda(&bd_addr);
BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
p_pcb->mdep_cfg_idx, p_pcb->channel_id,
BTHL_CONN_STATE_DISCONNECTED, 0);
}
btif_hl_clean_pcb(p_pcb);
}
}
/*******************************************************************************
*
* Function btif_hl_proc_sdp_query_cfm
*
* Description Process the SDP query confirmation
*
* Returns Nothing
*
******************************************************************************/
static bool btif_hl_proc_sdp_query_cfm(tBTA_HL* p_data) {
btif_hl_app_cb_t* p_acb;
btif_hl_mcl_cb_t* p_mcb;
tBTA_HL_SDP* p_sdp;
tBTA_HL_CCH_OPEN_PARAM open_param;
uint8_t app_idx, mcl_idx, sdp_idx = 0;
uint8_t num_recs, i, num_mdeps, j;
btif_hl_cch_op_t old_cch_oper;
bool status = false;
btif_hl_pending_chan_cb_t* p_pcb;
BTIF_TRACE_DEBUG("%s", __func__);
p_sdp = p_data->sdp_query_cfm.p_sdp;
num_recs = p_sdp->num_recs;
BTIF_TRACE_DEBUG("num of SDP records=%d", num_recs);
for (i = 0; i < num_recs; i++) {
BTIF_TRACE_DEBUG("rec_idx=%d ctrl_psm=0x%x data_psm=0x%x", (i + 1),
p_sdp->sdp_rec[i].ctrl_psm, p_sdp->sdp_rec[i].data_psm);
BTIF_TRACE_DEBUG("MCAP supported procedures=0x%x",
p_sdp->sdp_rec[i].mcap_sup_proc);
num_mdeps = p_sdp->sdp_rec[i].num_mdeps;
BTIF_TRACE_DEBUG("num of mdeps =%d", num_mdeps);
for (j = 0; j < num_mdeps; j++) {
BTIF_TRACE_DEBUG("mdep_idx=%d mdep_id=0x%x data_type=0x%x mdep_role=0x%x",
(j + 1), p_sdp->sdp_rec[i].mdep_cfg[j].mdep_id,
p_sdp->sdp_rec[i].mdep_cfg[j].data_type,
p_sdp->sdp_rec[i].mdep_cfg[j].mdep_role);
}
}
if (btif_hl_find_app_idx_using_app_id(p_data->sdp_query_cfm.app_id,
&app_idx)) {
p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
if (btif_hl_find_mcl_idx(app_idx, p_data->sdp_query_cfm.bd_addr,
&mcl_idx)) {
p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
if (p_mcb->cch_oper != BTIF_HL_CCH_OP_NONE) {
memcpy(&p_mcb->sdp, p_sdp, sizeof(tBTA_HL_SDP));
old_cch_oper = p_mcb->cch_oper;
p_mcb->cch_oper = BTIF_HL_CCH_OP_NONE;
switch (old_cch_oper) {
case BTIF_HL_CCH_OP_MDEP_FILTERING:
status = btif_hl_find_sdp_idx_using_mdep_filter(app_idx, mcl_idx,
&sdp_idx);
break;
default:
break;
}
if (status) {
p_mcb->sdp_idx = sdp_idx;
p_mcb->valid_sdp_idx = true;
p_mcb->ctrl_psm = p_mcb->sdp.sdp_rec[sdp_idx].ctrl_psm;
switch (old_cch_oper) {
case BTIF_HL_CCH_OP_MDEP_FILTERING:
p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
if (p_pcb->in_use) {
if (!p_pcb->abort_pending) {
switch (p_pcb->op) {
case BTIF_HL_PEND_DCH_OP_OPEN:
btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
break;
case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
default:
break;
}
open_param.ctrl_psm = p_mcb->ctrl_psm;
bdcpy(open_param.bd_addr, p_mcb->bd_addr);
open_param.sec_mask =
(BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
BTA_HlCchOpen(p_acb->app_id, p_acb->app_handle, &open_param);
} else {
BTIF_TRACE_DEBUG("channel abort pending");
}
}
break;
case BTIF_HL_CCH_OP_DCH_OPEN:
status = btif_hl_proc_pending_op(app_idx, mcl_idx);
break;
default:
BTIF_TRACE_ERROR("Invalid CCH oper %d", old_cch_oper);
break;
}
} else {
BTIF_TRACE_ERROR("Can not find SDP idx discard CCH Open request");
}
}
}
}
return status;
}
/*******************************************************************************
*
* Function btif_hl_proc_cch_open_ind
*
* Description Process the CCH open indication
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_proc_cch_open_ind(tBTA_HL* p_data)
{
btif_hl_mcl_cb_t* p_mcb;
uint8_t mcl_idx;
int i;
BTIF_TRACE_DEBUG("%s", __func__);
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
if (btif_hl_cb.acb[i].in_use) {
if (!btif_hl_find_mcl_idx(i, p_data->cch_open_ind.bd_addr, &mcl_idx)) {
if (btif_hl_find_avail_mcl_idx(i, &mcl_idx)) {
p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, mcl_idx);
alarm_free(p_mcb->cch_timer);
memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t));
p_mcb->in_use = true;
p_mcb->is_connected = true;
p_mcb->mcl_handle = p_data->cch_open_ind.mcl_handle;
bdcpy(p_mcb->bd_addr, p_data->cch_open_ind.bd_addr);
btif_hl_start_cch_timer(i, mcl_idx);
}
} else {
BTIF_TRACE_ERROR("The MCL already exist for cch_open_ind");
}
}
}
}
/*******************************************************************************
*
* Function btif_hl_proc_pending_op
*
* Description Process the pending dch operation.
*
* Returns Nothing
*
******************************************************************************/
bool btif_hl_proc_pending_op(uint8_t app_idx, uint8_t mcl_idx)
{
btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
btif_hl_pending_chan_cb_t* p_pcb;
bool status = false;
tBTA_HL_DCH_OPEN_PARAM dch_open;
tBTA_HL_MDL_ID mdl_id;
tBTA_HL_DCH_RECONNECT_PARAM reconnect_param;
p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
if (p_pcb->in_use) {
switch (p_pcb->op) {
case BTIF_HL_PEND_DCH_OP_OPEN:
if (!p_pcb->abort_pending) {
BTIF_TRACE_DEBUG("op BTIF_HL_PEND_DCH_OP_OPEN");
dch_open.ctrl_psm = p_mcb->ctrl_psm;
dch_open.local_mdep_id =
p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_id;
if (btif_hl_find_peer_mdep_id(
p_acb->app_id, p_mcb->bd_addr,
p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx]
.mdep_cfg.mdep_role,
p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx]
.mdep_cfg.data_cfg[0]
.data_type,
&dch_open.peer_mdep_id)) {
dch_open.local_cfg = p_acb->channel_type[p_pcb->mdep_cfg_idx];
if ((p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx]
.mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SOURCE) &&
!btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) {
dch_open.local_cfg = BTA_HL_DCH_CFG_RELIABLE;
}
dch_open.sec_mask = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
BTIF_TRACE_DEBUG("dch_open.local_cfg=%d ", dch_open.local_cfg);
btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
if (!btif_hl_is_reconnect_possible(app_idx, mcl_idx,
p_pcb->mdep_cfg_idx, &dch_open,
&mdl_id)) {
BTIF_TRACE_DEBUG("Issue DCH open, mcl_handle=%d",
p_mcb->mcl_handle);
BTA_HlDchOpen(p_mcb->mcl_handle, &dch_open);
} else {
reconnect_param.ctrl_psm = p_mcb->ctrl_psm;
reconnect_param.mdl_id = mdl_id;
;
BTIF_TRACE_DEBUG("Issue Reconnect ctrl_psm=0x%x mdl_id=0x%x",
reconnect_param.ctrl_psm,
reconnect_param.mdl_id);
BTA_HlDchReconnect(p_mcb->mcl_handle, &reconnect_param);
}
status = true;
}
} else {
btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
status = true;
}
break;
case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
BTA_HlDeleteMdl(p_mcb->mcl_handle, p_acb->delete_mdl.mdl_id);
status = true;
break;
default:
break;
}
}
return status;
}
/*******************************************************************************
*
* Function btif_hl_proc_cch_open_cfm
*
* Description Process the CCH open confirmation
*
* Returns Nothing
*
******************************************************************************/
static bool btif_hl_proc_cch_open_cfm(tBTA_HL* p_data)
{
btif_hl_mcl_cb_t* p_mcb;
uint8_t app_idx, mcl_idx;
bool status = false;
BTIF_TRACE_DEBUG("%s", __func__);
if (btif_hl_find_app_idx_using_app_id(p_data->cch_open_cfm.app_id,
&app_idx)) {
BTIF_TRACE_DEBUG("app_idx=%d", app_idx);
if (btif_hl_find_mcl_idx(app_idx, p_data->cch_open_cfm.bd_addr, &mcl_idx)) {
p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
BTIF_TRACE_DEBUG("mcl_idx=%d, mcl_handle=%d", mcl_idx,
p_data->cch_open_cfm.mcl_handle);
p_mcb->mcl_handle = p_data->cch_open_cfm.mcl_handle;
p_mcb->is_connected = true;
status = btif_hl_proc_pending_op(app_idx, mcl_idx);
if (status) btif_hl_start_cch_timer(app_idx, mcl_idx);
}
}
return status;
}
/*******************************************************************************
*
* Function btif_hl_clean_mcb_using_handle
*
* Description clean control channel cb using handle
*
* Returns void
*
******************************************************************************/
static void btif_hl_clean_mcb_using_handle(tBTA_HL_MCL_HANDLE mcl_handle) {
btif_hl_app_cb_t* p_acb;
uint8_t i, j;
for (i = 0; i < BTA_HL_NUM_APPS; i++) {
p_acb = BTIF_HL_GET_APP_CB_PTR(i);
for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
if (p_acb->mcb[j].in_use)
BTIF_TRACE_DEBUG(
"btif_hl_find_mcl_idx_using_handle: app_idx=%d,"
"mcl_idx =%d mcl_handle=%d",
i, j, p_acb->mcb[j].mcl_handle);
if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
btif_hl_stop_cch_timer(i, j);
btif_hl_release_mcl_sockets(i, j);
btif_hl_send_setup_disconnected_cb(i, j);
btif_hl_clean_mcl_cb(i, j);
}
}
}
}
/*******************************************************************************
*
* Function btif_hl_proc_cch_close_ind
*
* Description Process the CCH close indication
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_proc_cch_close_ind(tBTA_HL* p_data)
{
BTIF_TRACE_DEBUG("%s", __func__);
btif_hl_clean_mcb_using_handle(p_data->cch_close_ind.mcl_handle);
}
/*******************************************************************************
*
* Function btif_hl_proc_cch_close_cfm
*
* Description Process the CCH close confirmation
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_proc_cch_close_cfm(tBTA_HL* p_data) {
BTIF_TRACE_DEBUG("%s", __func__);
btif_hl_clean_mcb_using_handle(p_data->cch_close_ind.mcl_handle);
}
/*******************************************************************************
*
* Function btif_hl_proc_create_ind
*
* Description Process the MDL create indication
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_proc_create_ind(tBTA_HL* p_data) {
btif_hl_app_cb_t* p_acb;
btif_hl_mcl_cb_t* p_mcb;
tBTA_HL_MDEP* p_mdep;
uint8_t orig_app_idx, mcl_idx, mdep_cfg_idx;
bool first_reliable_exist;
bool success = true;
tBTA_HL_DCH_CFG rsp_cfg = BTA_HL_DCH_CFG_UNKNOWN;
tBTA_HL_DCH_CREATE_RSP rsp_code = BTA_HL_DCH_CREATE_RSP_CFG_REJ;
tBTA_HL_DCH_CREATE_RSP_PARAM create_rsp_param;
BTIF_TRACE_DEBUG("%s", __func__);
// Find the correct app_idx based on the mdep_id;
btif_hl_find_app_idx_using_mdepId(p_data->dch_create_ind.local_mdep_id,
&orig_app_idx);
if (btif_hl_find_mcl_idx(orig_app_idx, p_data->dch_create_ind.bd_addr,
&mcl_idx)) {
p_acb = BTIF_HL_GET_APP_CB_PTR(orig_app_idx);
p_mcb = BTIF_HL_GET_MCL_CB_PTR(orig_app_idx, mcl_idx);
if (btif_hl_find_mdep_cfg_idx(orig_app_idx,
p_data->dch_create_ind.local_mdep_id,
&mdep_cfg_idx)) {
p_mdep = &(p_acb->sup_feature.mdep[mdep_cfg_idx]);
first_reliable_exist =
btif_hl_is_the_first_reliable_existed(orig_app_idx, mcl_idx);
switch (p_mdep->mdep_cfg.mdep_role) {
case BTA_HL_MDEP_ROLE_SOURCE:
if (p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_NO_PREF) {
if (first_reliable_exist) {
rsp_cfg = p_acb->channel_type[mdep_cfg_idx];
} else {
rsp_cfg = BTA_HL_DCH_CFG_RELIABLE;
}
rsp_code = BTA_HL_DCH_CREATE_RSP_SUCCESS;
}
break;
case BTA_HL_MDEP_ROLE_SINK:
BTIF_TRACE_DEBUG("btif_hl_proc_create_ind:BTA_HL_MDEP_ROLE_SINK");
if ((p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_RELIABLE) ||
(first_reliable_exist &&
(p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_STREAMING))) {
rsp_code = BTA_HL_DCH_CREATE_RSP_SUCCESS;
rsp_cfg = p_data->dch_create_ind.cfg;
BTIF_TRACE_DEBUG(
"btif_hl_proc_create_ind:BTA_HL_MDEP_ROLE_SINK cfg = %d",
rsp_cfg);
}
break;
default:
break;
}
}
} else {
success = false;
}
if (success) {
BTIF_TRACE_DEBUG("create response rsp_code=%d rsp_cfg=%d", rsp_code,
rsp_cfg);
create_rsp_param.local_mdep_id = p_data->dch_create_ind.local_mdep_id;
create_rsp_param.mdl_id = p_data->dch_create_ind.mdl_id;
create_rsp_param.rsp_code = rsp_code;
create_rsp_param.cfg_rsp = rsp_cfg;
BTA_HlDchCreateRsp(p_mcb->mcl_handle, &create_rsp_param);
}
}
/*******************************************************************************
*
* Function btif_hl_proc_dch_open_ind
*
* Description Process the DCH open indication
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_proc_dch_open_ind(tBTA_HL* p_data)
{
btif_hl_mdl_cb_t* p_dcb;
uint8_t orig_app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
bool close_dch = false;
BTIF_TRACE_DEBUG("%s", __func__);
// Find the correct app_idx based on the mdep_id;
btif_hl_find_app_idx_using_mdepId(p_data->dch_open_ind.local_mdep_id,
&orig_app_idx);
if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_open_ind.mcl_handle,
orig_app_idx, &mcl_idx)) {
if (btif_hl_find_avail_mdl_idx(orig_app_idx, mcl_idx, &mdl_idx)) {
p_dcb = BTIF_HL_GET_MDL_CB_PTR(orig_app_idx, mcl_idx, mdl_idx);
if (btif_hl_find_mdep_cfg_idx(orig_app_idx,
p_data->dch_open_ind.local_mdep_id,
&mdep_cfg_idx)) {
p_dcb->in_use = true;
p_dcb->mdl_handle = p_data->dch_open_ind.mdl_handle;
p_dcb->local_mdep_cfg_idx = mdep_cfg_idx;
p_dcb->local_mdep_id = p_data->dch_open_ind.local_mdep_id;
p_dcb->mdl_id = p_data->dch_open_ind.mdl_id;
p_dcb->dch_mode = p_data->dch_open_ind.dch_mode;
p_dcb->dch_mode = p_data->dch_open_ind.dch_mode;
p_dcb->is_the_first_reliable = p_data->dch_open_ind.first_reliable;
p_dcb->mtu = p_data->dch_open_ind.mtu;
if (btif_hl_find_channel_id_using_mdl_id(orig_app_idx, p_dcb->mdl_id,
&p_dcb->channel_id)) {
BTIF_TRACE_DEBUG(" app_idx=%d mcl_idx=%d mdl_idx=%d channel_id=%d",
orig_app_idx, mcl_idx, mdl_idx, p_dcb->channel_id);
if (!btif_hl_create_socket(orig_app_idx, mcl_idx, mdl_idx)) {
BTIF_TRACE_ERROR("Unable to create socket");
close_dch = true;
}
} else {
BTIF_TRACE_ERROR("Unable find channel id for mdl_id=0x%x",
p_dcb->mdl_id);
close_dch = true;
}
} else {
BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",
p_data->dch_open_cfm.local_mdep_id);
close_dch = true;
}
if (close_dch) btif_hl_clean_mdl_cb(p_dcb);
} else
close_dch = true;
} else
close_dch = true;
if (close_dch) BTA_HlDchClose(p_data->dch_open_cfm.mdl_handle);
}
/*******************************************************************************
*
* Function btif_hl_proc_dch_open_cfm
*
* Description Process the DCH close confirmation
*
* Returns Nothing
*
******************************************************************************/
static bool btif_hl_proc_dch_open_cfm(tBTA_HL* p_data)
{
btif_hl_mdl_cb_t* p_dcb;
btif_hl_pending_chan_cb_t* p_pcb;
uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
bool status = false;
bool close_dch = false;
BTIF_TRACE_DEBUG("%s", __func__);
// Find the correct app_idx based on the mdep_id;
btif_hl_find_app_idx_using_mdepId(p_data->dch_open_cfm.local_mdep_id,
&app_idx);
if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_open_cfm.mcl_handle,
app_idx, &mcl_idx)) {
p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) {
p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
if (btif_hl_find_mdep_cfg_idx(app_idx, p_data->dch_open_cfm.local_mdep_id,
&mdep_cfg_idx)) {
p_dcb->in_use = true;
p_dcb->mdl_handle = p_data->dch_open_cfm.mdl_handle;
p_dcb->local_mdep_cfg_idx = mdep_cfg_idx;
p_dcb->local_mdep_id = p_data->dch_open_cfm.local_mdep_id;
p_dcb->mdl_id = p_data->dch_open_cfm.mdl_id;
p_dcb->dch_mode = p_data->dch_open_cfm.dch_mode;
p_dcb->is_the_first_reliable = p_data->dch_open_cfm.first_reliable;
p_dcb->mtu = p_data->dch_open_cfm.mtu;
p_dcb->channel_id = p_pcb->channel_id;
BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx,
mdl_idx);
btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
if (btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) {
status = true;
BTIF_TRACE_DEBUG(
"app_idx=%d mcl_idx=%d mdl_idx=%d p_dcb->channel_id=0x%08x",
app_idx, mcl_idx, mdl_idx, p_dcb->channel_id);
btif_hl_clean_pcb(p_pcb);
} else {
BTIF_TRACE_ERROR("Unable to create socket");
close_dch = true;
}
} else {
BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",
p_data->dch_open_cfm.local_mdep_id);
close_dch = true;
}
if (close_dch) {
btif_hl_clean_mdl_cb(p_dcb);
BTA_HlDchClose(p_data->dch_open_cfm.mdl_handle);
}
}
}
return status;
}
/*******************************************************************************
*
* Function btif_hl_proc_dch_reconnect_cfm
*
* Description Process the DCH reconnect indication
*
* Returns Nothing
*
******************************************************************************/
static bool btif_hl_proc_dch_reconnect_cfm(tBTA_HL* p_data) {
btif_hl_mdl_cb_t* p_dcb;
btif_hl_pending_chan_cb_t* p_pcb;
uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
bool status = false;
bool close_dch = false;
BTIF_TRACE_DEBUG("%s", __func__);
btif_hl_find_app_idx_using_mdepId(p_data->dch_reconnect_cfm.local_mdep_id,
&app_idx);
if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_reconnect_cfm.mcl_handle,
app_idx, &mcl_idx)) {
p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) {
p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
if (btif_hl_find_mdep_cfg_idx(app_idx,
p_data->dch_reconnect_cfm.local_mdep_id,
&mdep_cfg_idx)) {
p_dcb->in_use = true;
p_dcb->mdl_handle = p_data->dch_reconnect_cfm.mdl_handle;
p_dcb->local_mdep_cfg_idx = mdep_cfg_idx;
p_dcb->local_mdep_id = p_data->dch_reconnect_cfm.local_mdep_id;
p_dcb->mdl_id = p_data->dch_reconnect_cfm.mdl_id;
p_dcb->dch_mode = p_data->dch_reconnect_cfm.dch_mode;
p_dcb->is_the_first_reliable = p_data->dch_reconnect_cfm.first_reliable;
p_dcb->mtu = p_data->dch_reconnect_cfm.mtu;
p_dcb->channel_id = p_pcb->channel_id;
BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx,
mdl_idx);
btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
if (btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) {
status = true;
BTIF_TRACE_DEBUG(
"app_idx=%d mcl_idx=%d mdl_idx=%d p_dcb->channel_id=0x%08x",
app_idx, mcl_idx, mdl_idx, p_dcb->channel_id);
btif_hl_clean_pcb(p_pcb);
} else {
BTIF_TRACE_ERROR("Unable to create socket");
close_dch = true;
}
} else {
BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",
p_data->dch_open_cfm.local_mdep_id);
close_dch = true;
}
if (close_dch) {
btif_hl_clean_mdl_cb(p_dcb);
BTA_HlDchClose(p_data->dch_reconnect_cfm.mdl_handle);
}
}
}
return status;
}
/*******************************************************************************
*
* Function btif_hl_proc_dch_reconnect_ind
*
* Description Process the DCH reconnect indication
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_proc_dch_reconnect_ind(tBTA_HL* p_data)
{
btif_hl_app_cb_t* p_acb;
btif_hl_mdl_cb_t* p_dcb;
uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
bool close_dch = false;
BTIF_TRACE_DEBUG("%s", __func__);
// Find the correct app_idx based on the mdep_id;
btif_hl_find_app_idx_using_mdepId(p_data->dch_reconnect_ind.local_mdep_id,
&app_idx);
if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_reconnect_ind.mcl_handle,
app_idx, &mcl_idx)) {
p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
BTIF_TRACE_DEBUG(
"btif_hl_proc_dch_reconnect_ind: app_idx = %d, mcl_idx = %d", app_idx,
mcl_idx);
if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) {
p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
if (btif_hl_find_mdep_cfg_idx(app_idx,
p_data->dch_reconnect_ind.local_mdep_id,
&mdep_cfg_idx)) {
p_dcb->in_use = true;
p_dcb->mdl_handle = p_data->dch_reconnect_ind.mdl_handle;
p_dcb->local_mdep_cfg_idx = mdep_cfg_idx;
p_dcb->local_mdep_id = p_data->dch_reconnect_ind.local_mdep_id;
p_dcb->mdl_id = p_data->dch_reconnect_ind.mdl_id;
p_dcb->dch_mode = p_data->dch_reconnect_ind.dch_mode;
p_dcb->dch_mode = p_data->dch_reconnect_ind.dch_mode;
p_dcb->is_the_first_reliable = p_data->dch_reconnect_ind.first_reliable;
p_dcb->mtu = p_data->dch_reconnect_ind.mtu;
p_dcb->channel_id = btif_hl_get_next_channel_id(p_acb->app_id);
BTIF_TRACE_DEBUG(" app_idx=%d mcl_idx=%d mdl_idx=%d channel_id=%d",
app_idx, mcl_idx, mdl_idx, p_dcb->channel_id);
if (!btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) {
BTIF_TRACE_ERROR("Unable to create socket");
close_dch = true;
}
} else {
BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",
p_data->dch_open_cfm.local_mdep_id);
close_dch = true;
}
if (close_dch) btif_hl_clean_mdl_cb(p_dcb);
} else
close_dch = true;
} else
close_dch = true;
if (close_dch) BTA_HlDchClose(p_data->dch_reconnect_ind.mdl_handle);
}
/*******************************************************************************
*
* Function btif_hl_proc_dch_close_ind
*
* Description Process the DCH close indication
*
* Returns Nothing
*
******************************************************************************/
static void btif_hl_proc_dch_close_ind(tBTA_HL* p_data)
{
btif_hl_mdl_cb_t* p_dcb;
btif_hl_mcl_cb_t* p_mcb;
uint8_t app_idx, mcl_idx, mdl_idx;
BTIF_TRACE_DEBUG("%s", __func__);
if (btif_hl_find_mdl_idx_using_handle(p_data->dch_close_ind.mdl_handle,
&app_idx, &mcl_idx, &mdl_idx)) {
p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);