1 /******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /*******************************************************************************
20 *
21 * Filename: btif_hl.c
22 *
23 * Description: Health Device Profile Bluetooth Interface
24 *
25 *
26 ******************************************************************************/
27 #define LOG_TAG "bt_btif_hl"
28
29 #include "btif_hl.h"
30
31 #include <base/logging.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <pthread.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/poll.h>
41 #include <sys/prctl.h>
42 #include <sys/select.h>
43 #include <sys/socket.h>
44 #include <sys/types.h>
45 #include <sys/un.h>
46 #include <time.h>
47 #include <unistd.h>
48
49 #include <hardware/bt_hl.h>
50
51 #include "bta_api.h"
52 #include "btif_common.h"
53 #include "btif_storage.h"
54 #include "btif_util.h"
55 #include "btu.h"
56 #include "mca_api.h"
57 #include "osi/include/list.h"
58 #include "osi/include/log.h"
59 #include "osi/include/osi.h"
60
61 #define MAX_DATATYPE_SUPPORTED 8
62
63 extern int btif_hl_update_maxfd(int max_org_s);
64 extern void btif_hl_select_monitor_callback(fd_set* p_cur_set,
65 UNUSED_ATTR fd_set* p_org_set);
66 extern void btif_hl_select_wakeup_callback(fd_set* p_org_set,
67 int wakeup_signal);
68 extern int btif_hl_update_maxfd(int max_org_s);
69 extern void btif_hl_select_monitor_callback(fd_set* p_cur_set,
70 UNUSED_ATTR fd_set* p_org_set);
71 extern void btif_hl_select_wakeup_callback(fd_set* p_org_set,
72 int wakeup_signal);
73 extern void btif_hl_soc_thread_init(void);
74 extern void btif_hl_release_mcl_sockets(uint8_t app_idx, uint8_t mcl_idx);
75 extern bool btif_hl_create_socket(uint8_t app_idx, uint8_t mcl_idx,
76 uint8_t mdl_idx);
77 extern void btif_hl_release_socket(uint8_t app_idx, uint8_t mcl_idx,
78 uint8_t mdl_idx);
79
80 extern fixed_queue_t* btu_general_alarm_queue;
81
82 btif_hl_cb_t btif_hl_cb;
83 btif_hl_cb_t* p_btif_hl_cb = &btif_hl_cb;
84
85 /*******************************************************************************
86 * Static variables
87 ******************************************************************************/
88 static bthl_callbacks_t bt_hl_callbacks_cb;
89 static bthl_callbacks_t* bt_hl_callbacks = NULL;
90
91 /* signal socketpair to wake up select loop */
92
93 const int btif_hl_signal_select_wakeup = 1;
94 const int btif_hl_signal_select_exit = 2;
95 const int btif_hl_signal_select_close_connected = 3;
96
97 static int listen_s = -1;
98 static int connected_s = -1;
99 static pthread_t select_thread_id = -1;
100 static int signal_fds[2] = {-1, -1};
101 static list_t* soc_queue;
102 static int reg_counter;
103
104 static inline int btif_hl_select_wakeup(void);
105 static inline int btif_hl_select_close_connected(void);
106 static inline int btif_hl_close_select_thread(void);
107 static uint8_t btif_hl_get_next_app_id(void);
108 static int btif_hl_get_next_channel_id(uint8_t app_id);
109 static void btif_hl_init_next_app_id(void);
110 static void btif_hl_init_next_channel_id(void);
111 static void btif_hl_ctrl_cback(tBTA_HL_CTRL_EVT event, tBTA_HL_CTRL* p_data);
112 static void btif_hl_set_state(btif_hl_state_t state);
113 static btif_hl_state_t btif_hl_get_state(void);
114 static void btif_hl_cback(tBTA_HL_EVT event, tBTA_HL* p_data);
115 static void btif_hl_proc_cb_evt(uint16_t event, char* p_param);
116
117 #define CHECK_CALL_CBACK(P_CB, P_CBACK, ...) \
118 do { \
119 if ((P_CB) && (P_CB)->P_CBACK) { \
120 (P_CB)->P_CBACK(__VA_ARGS__); \
121 } else { \
122 ASSERTC(0, "Callback is NULL", 0); \
123 } \
124 } while (0)
125
126 #define BTIF_HL_CALL_CBACK(P_CB, P_CBACK, ...) \
127 do { \
128 if ((p_btif_hl_cb->state != BTIF_HL_STATE_DISABLING) && \
129 (p_btif_hl_cb->state != BTIF_HL_STATE_DISABLED)) { \
130 if ((P_CB) && (P_CB)->P_CBACK) { \
131 (P_CB)->P_CBACK(__VA_ARGS__); \
132 } else { \
133 ASSERTC(0, "Callback is NULL", 0); \
134 } \
135 } \
136 } while (0)
137
138 #define CHECK_BTHL_INIT() \
139 do { \
140 if (bt_hl_callbacks == NULL) { \
141 BTIF_TRACE_WARNING("BTHL: %s: BTHL not initialized", __func__); \
142 return BT_STATUS_NOT_READY; \
143 } \
144 } while (0)
145
146 static const btif_hl_data_type_cfg_t data_type_table[] = {
147 /* Data Specilization Ntx Nrx (from Bluetooth SIG's
148 HDP whitepaper)*/
149 {BTIF_HL_DATA_TYPE_PULSE_OXIMETER, 9216, 256},
150 {BTIF_HL_DATA_TYPE_BLOOD_PRESSURE_MON, 896, 224},
151 {BTIF_HL_DATA_TYPE_BODY_THERMOMETER, 896, 224},
152 {BTIF_HL_DATA_TYPE_BODY_WEIGHT_SCALE, 896, 224},
153 {BTIF_HL_DATA_TYPE_GLUCOSE_METER, 896, 224},
154 {BTIF_HL_DATA_TYPE_STEP_COUNTER, 6624, 224},
155 {BTIF_HL_DATA_TYPE_BCA, 7730, 1230},
156 {BTIF_HL_DATA_TYPE_PEAK_FLOW, 2030, 224},
157 {BTIF_HL_DATA_TYPE_ACTIVITY_HUB, 5120, 224},
158 {BTIF_HL_DATA_TYPE_AMM, 1024, 64}};
159
160 #define BTIF_HL_DATA_TABLE_SIZE \
161 (sizeof(data_type_table) / sizeof(btif_hl_data_type_cfg_t))
162 #define BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE \
163 10240 /* use this size if the data type is not \
164 defined in the table; for future proof */
165 #define BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE \
166 512 /* use this size if the data type is not \
167 defined in the table; for future proof */
168
169 #define BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE 1024
170
171 /*******************************************************************************
172 * Static utility functions
173 ******************************************************************************/
174
175 #define BTIF_IF_GET_NAME 16
btif_hl_display_calling_process_name(void)176 void btif_hl_display_calling_process_name(void) {
177 char name[16];
178 prctl(BTIF_IF_GET_NAME, name, 0, 0, 0);
179 BTIF_TRACE_DEBUG("Process name (%s)", name);
180 }
181 #define BTIF_TIMEOUT_CCH_NO_DCH_MS (30 * 1000)
182
183 /*******************************************************************************
184 *
185 * Function btif_hl_if_channel_setup_pending
186 *
187 * Description check whether channel id is in setup pending state or not
188 *
189 * Returns bool
190 *
191 ******************************************************************************/
btif_hl_if_channel_setup_pending(int channel_id,uint8_t * p_app_idx,uint8_t * p_mcl_idx)192 bool btif_hl_if_channel_setup_pending(int channel_id, uint8_t* p_app_idx,
193 uint8_t* p_mcl_idx) {
194 btif_hl_app_cb_t* p_acb;
195 btif_hl_mcl_cb_t* p_mcb;
196 uint8_t i, j;
197 bool found = false;
198
199 *p_app_idx = 0;
200 *p_mcl_idx = 0;
201 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
202 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
203 if (p_acb->in_use) {
204 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
205 p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j);
206 if (p_mcb->in_use && p_mcb->is_connected &&
207 p_mcb->pcb.channel_id == channel_id) {
208 found = true;
209 *p_app_idx = i;
210 *p_mcl_idx = j;
211 break;
212 }
213 }
214 }
215 if (found) break;
216 }
217 BTIF_TRACE_DEBUG("%s found=%d channel_id=0x%08x", __func__, found, channel_id,
218 *p_app_idx, *p_mcl_idx);
219 return found;
220 }
221 /*******************************************************************************
222 *
223 * Function btif_hl_num_dchs_in_use
224 *
225 * Description find number of DCHs in use
226 *
227 * Returns uint8_t
228 ******************************************************************************/
btif_hl_num_dchs_in_use(uint8_t mcl_handle)229 uint8_t btif_hl_num_dchs_in_use(uint8_t mcl_handle) {
230 btif_hl_app_cb_t* p_acb;
231 btif_hl_mcl_cb_t* p_mcb;
232 uint8_t i, j, x;
233 uint8_t cnt = 0;
234
235 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
236 BTIF_TRACE_DEBUG("btif_hl_num_dchs:i = %d", i);
237 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
238 if (p_acb && p_acb->in_use) {
239 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
240 if (p_acb->mcb[j].in_use)
241 BTIF_TRACE_DEBUG(
242 "btif_hl_num_dchs:mcb in use j=%d, mcl_handle=%d,mcb handle=%d",
243 j, mcl_handle, p_acb->mcb[j].mcl_handle);
244 if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
245 p_mcb = &p_acb->mcb[j];
246 BTIF_TRACE_DEBUG("btif_hl_num_dchs: mcl handle found j =%d", j);
247 for (x = 0; x < BTA_HL_NUM_MDLS_PER_MCL; x++) {
248 if (p_mcb->mdl[x].in_use) {
249 BTIF_TRACE_DEBUG("btif_hl_num_dchs_in_use:found x =%d", x);
250 cnt++;
251 }
252 }
253 }
254 }
255 }
256 }
257
258 BTIF_TRACE_DEBUG("%s dch in use count=%d", __func__, cnt);
259 return cnt;
260 }
261 /*******************************************************************************
262 *
263 * Function btif_hl_timer_timeout
264 *
265 * Description Process timer timeout
266 *
267 * Returns void
268 ******************************************************************************/
btif_hl_timer_timeout(void * data)269 void btif_hl_timer_timeout(void* data) {
270 btif_hl_mcl_cb_t* p_mcb = (btif_hl_mcl_cb_t*)data;
271
272 BTIF_TRACE_DEBUG("%s", __func__);
273 if (p_mcb->is_connected) {
274 BTIF_TRACE_DEBUG("Idle timeout Close CCH mcl_handle=%d", p_mcb->mcl_handle);
275 BTA_HlCchClose(p_mcb->mcl_handle);
276 } else {
277 BTIF_TRACE_DEBUG("CCH idle timeout But CCH not connected");
278 }
279 }
280
281 /*******************************************************************************
282 *
283 * Function btif_hl_stop_cch_timer
284 *
285 * Description stop CCH timer
286 *
287 * Returns void
288 ******************************************************************************/
btif_hl_stop_cch_timer(uint8_t app_idx,uint8_t mcl_idx)289 void btif_hl_stop_cch_timer(uint8_t app_idx, uint8_t mcl_idx) {
290 btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
291
292 BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx);
293 alarm_cancel(p_mcb->cch_timer);
294 }
295
296 /*******************************************************************************
297 *
298 * Function btif_hl_start_cch_timer
299 *
300 * Description start CCH timer
301 *
302 * Returns void
303 ******************************************************************************/
btif_hl_start_cch_timer(uint8_t app_idx,uint8_t mcl_idx)304 void btif_hl_start_cch_timer(uint8_t app_idx, uint8_t mcl_idx) {
305 btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
306 BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx);
307
308 alarm_free(p_mcb->cch_timer);
309 p_mcb->cch_timer = alarm_new("btif_hl.mcl_cch_timer");
310 alarm_set_on_queue(p_mcb->cch_timer, BTIF_TIMEOUT_CCH_NO_DCH_MS,
311 btif_hl_timer_timeout, p_mcb, btu_general_alarm_queue);
312 }
313
314 /*******************************************************************************
315 *
316 * Function btif_hl_find_mdl_idx
317 *
318 * Description Find the MDL index using MDL ID
319 *
320 * Returns bool
321 *
322 ******************************************************************************/
btif_hl_find_mdl_idx(uint8_t app_idx,uint8_t mcl_idx,uint16_t mdl_id,uint8_t * p_mdl_idx)323 static bool btif_hl_find_mdl_idx(uint8_t app_idx, uint8_t mcl_idx,
324 uint16_t mdl_id, uint8_t* p_mdl_idx) {
325 btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
326 bool found = false;
327 uint8_t i;
328
329 for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) {
330 if (p_mcb->mdl[i].in_use && (mdl_id != 0) &&
331 (p_mcb->mdl[i].mdl_id == mdl_id)) {
332 found = true;
333 *p_mdl_idx = i;
334 break;
335 }
336 }
337
338 BTIF_TRACE_DEBUG("%s found=%d mdl_id=%d mdl_idx=%d ", __func__, found, mdl_id,
339 i);
340
341 return found;
342 }
343
344 /*******************************************************************************
345 *
346 * Function btif_hl_is_the_first_reliable_existed
347 *
348 * Description This function checks whether the first reliable DCH channel
349 * has been setup on the MCL or not
350 *
351 * Returns bool - true exist
352 * false does not exist
353 *
354 ******************************************************************************/
btif_hl_is_the_first_reliable_existed(uint8_t app_idx,uint8_t mcl_idx)355 bool btif_hl_is_the_first_reliable_existed(uint8_t app_idx, uint8_t mcl_idx) {
356 btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
357 bool is_existed = false;
358 uint8_t i;
359
360 for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) {
361 if (p_mcb->mdl[i].in_use && p_mcb->mdl[i].is_the_first_reliable) {
362 is_existed = true;
363 break;
364 }
365 }
366
367 BTIF_TRACE_DEBUG("bta_hl_is_the_first_reliable_existed is_existed=%d ",
368 is_existed);
369 return is_existed;
370 }
371 /*******************************************************************************
372 *
373 * Function btif_hl_clean_delete_mdl
374 *
375 * Description Cleanup the delete mdl control block
376 *
377 * Returns Nothing
378 *
379 ******************************************************************************/
btif_hl_clean_delete_mdl(btif_hl_delete_mdl_t * p_cb)380 static void btif_hl_clean_delete_mdl(btif_hl_delete_mdl_t* p_cb) {
381 BTIF_TRACE_DEBUG("%s", __func__);
382 memset(p_cb, 0, sizeof(btif_hl_delete_mdl_t));
383 }
384
385 /*******************************************************************************
386 *
387 * Function btif_hl_clean_pcb
388 *
389 * Description Cleanup the pending chan control block
390 *
391 * Returns Nothing
392 *
393 ******************************************************************************/
btif_hl_clean_pcb(btif_hl_pending_chan_cb_t * p_pcb)394 static void btif_hl_clean_pcb(btif_hl_pending_chan_cb_t* p_pcb) {
395 BTIF_TRACE_DEBUG("%s", __func__);
396 memset(p_pcb, 0, sizeof(btif_hl_pending_chan_cb_t));
397 }
398
399 /*******************************************************************************
400 *
401 * Function btif_hl_clean_mdl_cb
402 *
403 * Description Cleanup the MDL control block
404 *
405 * Returns Nothing
406 *
407 ******************************************************************************/
btif_hl_clean_mdl_cb(btif_hl_mdl_cb_t * p_dcb)408 static void btif_hl_clean_mdl_cb(btif_hl_mdl_cb_t* p_dcb) {
409 BTIF_TRACE_DEBUG("%s", __func__);
410 osi_free_and_reset((void**)&p_dcb->p_rx_pkt);
411 osi_free_and_reset((void**)&p_dcb->p_tx_pkt);
412 memset(p_dcb, 0, sizeof(btif_hl_mdl_cb_t));
413 }
414
415 /*******************************************************************************
416 *
417 * Function btif_hl_reset_mcb
418 *
419 * Description Reset MCL control block
420 *
421 * Returns bool
422 *
423 ******************************************************************************/
btif_hl_clean_mcl_cb(uint8_t app_idx,uint8_t mcl_idx)424 static void btif_hl_clean_mcl_cb(uint8_t app_idx, uint8_t mcl_idx) {
425 btif_hl_mcl_cb_t* p_mcb;
426 BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx);
427 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
428 alarm_free(p_mcb->cch_timer);
429 memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t));
430 }
431
432 /*******************************************************************************
433 *
434 * Function btif_hl_find_sdp_idx_using_mdep_filter
435 *
436 * Description This function finds the SDP record index using MDEP filter
437 * parameters
438 *
439 * Returns bool
440 *
441 ******************************************************************************/
btif_hl_reset_mdep_filter(uint8_t app_idx)442 static void btif_hl_reset_mdep_filter(uint8_t app_idx) {
443 btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
444 p_acb->filter.num_elems = 0;
445 }
446
447 /*******************************************************************************
448 *
449 * Function btif_hl_find_sdp_idx_using_mdep_filter
450 *
451 * Description This function finds the SDP record index using MDEP filter
452 * parameters
453 *
454 * Returns bool
455 *
456 ******************************************************************************/
btif_hl_find_sdp_idx_using_mdep_filter(uint8_t app_idx,uint8_t mcl_idx,uint8_t * p_sdp_idx)457 static bool btif_hl_find_sdp_idx_using_mdep_filter(uint8_t app_idx,
458 uint8_t mcl_idx,
459 uint8_t* p_sdp_idx) {
460 btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
461 btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
462 uint8_t i, j, num_recs, num_elems, num_mdeps, mdep_idx;
463 tBTA_HL_MDEP_ROLE peer_mdep_role;
464 uint16_t data_type;
465 tBTA_HL_SDP_MDEP_CFG* p_mdep;
466 bool found = false;
467 bool elem_found;
468
469 BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter");
470 num_recs = p_mcb->sdp.num_recs;
471 num_elems = p_acb->filter.num_elems;
472 if (!num_elems) {
473 BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter num_elem=0");
474 *p_sdp_idx = 0;
475 found = true;
476 return found;
477 }
478
479 for (i = 0; i < num_recs; i++) {
480 num_mdeps = p_mcb->sdp.sdp_rec[i].num_mdeps;
481 for (j = 0; j < num_elems; j++) {
482 data_type = p_acb->filter.elem[j].data_type;
483 peer_mdep_role = p_acb->filter.elem[j].peer_mdep_role;
484 elem_found = false;
485 mdep_idx = 0;
486 while (!elem_found && mdep_idx < num_mdeps) {
487 p_mdep = &(p_mcb->sdp.sdp_rec[i].mdep_cfg[mdep_idx]);
488 if ((p_mdep->data_type == data_type) &&
489 (p_mdep->mdep_role == peer_mdep_role)) {
490 elem_found = true;
491 } else {
492 mdep_idx++;
493 }
494 }
495
496 if (!elem_found) {
497 found = false;
498 break;
499 } else {
500 found = true;
501 }
502 }
503
504 if (found) {
505 BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter found idx=%d",
506 i);
507 *p_sdp_idx = i;
508 break;
509 }
510 }
511
512 BTIF_TRACE_DEBUG("%s found=%d sdp_idx=%d", __func__, found, *p_sdp_idx);
513
514 btif_hl_reset_mdep_filter(app_idx);
515
516 return found;
517 }
518 /*******************************************************************************
519 *
520 * Function btif_hl_is_reconnect_possible
521 *
522 * Description check reconnect is possible or not
523 *
524 * Returns bool
525 *
526 ******************************************************************************/
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)527 bool btif_hl_is_reconnect_possible(uint8_t app_idx, uint8_t mcl_idx,
528 int mdep_cfg_idx,
529 tBTA_HL_DCH_OPEN_PARAM* p_dch_open_api,
530 tBTA_HL_MDL_ID* p_mdl_id) {
531 btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
532 btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
533 tBTA_HL_DCH_CFG local_cfg = p_dch_open_api->local_cfg;
534 tBTA_HL_DCH_MODE dch_mode = BTA_HL_DCH_MODE_RELIABLE;
535 bool use_mdl_dch_mode = false;
536 btif_hl_mdl_cfg_t* p_mdl;
537 btif_hl_mdl_cfg_t* p_mdl1;
538 uint8_t i, j;
539 bool is_reconnect_ok = false;
540 bool stream_mode_avail = false;
541 uint16_t data_type =
542 p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type;
543 tBTA_HL_MDEP_ID peer_mdep_id = p_dch_open_api->peer_mdep_id;
544 uint8_t mdl_idx;
545
546 BTIF_TRACE_DEBUG("%s app_idx=%d mcl_idx=%d mdep_cfg_idx=%d", __func__,
547 app_idx, mcl_idx, mdep_cfg_idx);
548 switch (local_cfg) {
549 case BTA_HL_DCH_CFG_NO_PREF:
550 if (!btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) {
551 dch_mode = BTA_HL_DCH_MODE_RELIABLE;
552 } else {
553 use_mdl_dch_mode = true;
554 }
555 break;
556 case BTA_HL_DCH_CFG_RELIABLE:
557 dch_mode = BTA_HL_DCH_MODE_RELIABLE;
558 break;
559 case BTA_HL_DCH_CFG_STREAMING:
560 dch_mode = BTA_HL_DCH_MODE_STREAMING;
561 break;
562 default:
563 BTIF_TRACE_ERROR("Invalid local_cfg=%d", local_cfg);
564 return is_reconnect_ok;
565 break;
566 }
567
568 BTIF_TRACE_DEBUG("local_cfg=%d use_mdl_dch_mode=%d dch_mode=%d ", local_cfg,
569 use_mdl_dch_mode, dch_mode);
570
571 for (i = 0, p_mdl = &p_acb->mdl_cfg[0]; i < BTA_HL_NUM_MDL_CFGS;
572 i++, p_mdl++) {
573 if (p_mdl->base.active && p_mdl->extra.data_type == data_type &&
574 (p_mdl->extra.peer_mdep_id != BTA_HL_INVALID_MDEP_ID &&
575 p_mdl->extra.peer_mdep_id == peer_mdep_id) &&
576 memcpy(p_mdl->base.peer_bd_addr, p_mcb->bd_addr, sizeof(BD_ADDR)) &&
577 p_mdl->base.mdl_id &&
578 !btif_hl_find_mdl_idx(app_idx, mcl_idx, p_mdl->base.mdl_id, &mdl_idx)) {
579 BTIF_TRACE_DEBUG("i=%d Matched active=%d mdl_id =%d, mdl_dch_mode=%d",
580 i, p_mdl->base.active, p_mdl->base.mdl_id,
581 p_mdl->base.dch_mode);
582 if (!use_mdl_dch_mode) {
583 if (p_mdl->base.dch_mode == dch_mode) {
584 is_reconnect_ok = true;
585 *p_mdl_id = p_mdl->base.mdl_id;
586 BTIF_TRACE_DEBUG("reconnect is possible dch_mode=%d mdl_id=%d",
587 dch_mode, p_mdl->base.mdl_id);
588 break;
589 }
590 } else {
591 is_reconnect_ok = true;
592 for (j = i, p_mdl1 = &p_acb->mdl_cfg[i]; j < BTA_HL_NUM_MDL_CFGS;
593 j++, p_mdl1++) {
594 if (p_mdl1->base.active && p_mdl1->extra.data_type == data_type &&
595 (p_mdl1->extra.peer_mdep_id != BTA_HL_INVALID_MDEP_ID &&
596 p_mdl1->extra.peer_mdep_id == peer_mdep_id) &&
597 memcpy(p_mdl1->base.peer_bd_addr, p_mcb->bd_addr,
598 sizeof(BD_ADDR)) &&
599 p_mdl1->base.dch_mode == BTA_HL_DCH_MODE_STREAMING) {
600 stream_mode_avail = true;
601 BTIF_TRACE_DEBUG("found streaming mode mdl index=%d", j);
602 break;
603 }
604 }
605
606 if (stream_mode_avail) {
607 dch_mode = BTA_HL_DCH_MODE_STREAMING;
608 *p_mdl_id = p_mdl1->base.mdl_id;
609 BTIF_TRACE_DEBUG(
610 "reconnect is ok index=%d dch_mode=streaming mdl_id=%d", j,
611 *p_mdl_id);
612 break;
613 } else {
614 dch_mode = p_mdl->base.dch_mode;
615 *p_mdl_id = p_mdl->base.mdl_id;
616 BTIF_TRACE_DEBUG("reconnect is ok index=%d dch_mode=%d mdl_id=%d", i,
617 p_mdl->base.dch_mode, *p_mdl_id);
618 break;
619 }
620 }
621 }
622 }
623
624 BTIF_TRACE_DEBUG("is_reconnect_ok dch_mode=%d mdl_id=%d", is_reconnect_ok,
625 dch_mode, *p_mdl_id);
626 return is_reconnect_ok;
627 }
628
629 /*******************************************************************************
630 *
631 * Function btif_hl_dch_open
632 *
633 * Description Process DCH open request using the DCH Open API parameters
634 *
635 * Returns bool
636 *
637 ******************************************************************************/
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)638 bool btif_hl_dch_open(uint8_t app_id, BD_ADDR bd_addr,
639 tBTA_HL_DCH_OPEN_PARAM* p_dch_open_api, int mdep_cfg_idx,
640 btif_hl_pend_dch_op_t op, int* channel_id) {
641 btif_hl_app_cb_t* p_acb;
642 btif_hl_mcl_cb_t* p_mcb;
643 btif_hl_pending_chan_cb_t* p_pcb;
644 uint8_t app_idx, mcl_idx;
645 bool status = false;
646 tBTA_HL_MDL_ID mdl_id;
647 tBTA_HL_DCH_RECONNECT_PARAM reconnect_param;
648
649 BTIF_TRACE_DEBUG("%s app_id=%d ", __func__, app_id);
650 BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr[0], bd_addr[1],
651 bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
652
653 if (btif_hl_find_app_idx(app_id, &app_idx)) {
654 if (btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) {
655 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
656
657 p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
658 if (!p_pcb->in_use) {
659 p_mcb->req_ctrl_psm = p_dch_open_api->ctrl_psm;
660
661 p_pcb->in_use = true;
662 *channel_id = p_pcb->channel_id =
663 (int)btif_hl_get_next_channel_id(app_id);
664 p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING;
665 p_pcb->mdep_cfg_idx = mdep_cfg_idx;
666 memcpy(p_pcb->bd_addr, bd_addr, sizeof(BD_ADDR));
667 p_pcb->op = op;
668
669 if (p_mcb->sdp.num_recs) {
670 if (p_mcb->valid_sdp_idx) {
671 p_dch_open_api->ctrl_psm = p_mcb->ctrl_psm;
672 }
673
674 if (!btif_hl_is_reconnect_possible(app_idx, mcl_idx, mdep_cfg_idx,
675 p_dch_open_api, &mdl_id)) {
676 BTIF_TRACE_DEBUG("Issue DCH open");
677 BTA_HlDchOpen(p_mcb->mcl_handle, p_dch_open_api);
678 } else {
679 reconnect_param.ctrl_psm = p_mcb->ctrl_psm;
680 reconnect_param.mdl_id = mdl_id;
681 ;
682 BTIF_TRACE_DEBUG("Issue Reconnect ctrl_psm=0x%x mdl_id=0x%x",
683 reconnect_param.ctrl_psm, reconnect_param.mdl_id);
684 BTA_HlDchReconnect(p_mcb->mcl_handle, &reconnect_param);
685 }
686
687 status = true;
688 } else {
689 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
690 p_mcb->cch_oper = BTIF_HL_CCH_OP_DCH_OPEN;
691 BTA_HlSdpQuery(app_id, p_acb->app_handle, bd_addr);
692 status = true;
693 }
694 }
695 }
696 }
697
698 BTIF_TRACE_DEBUG("status=%d ", status);
699 return status;
700 }
701 /*******************************************************************************
702 *
703 * Function btif_hl_copy_bda
704 *
705 * Description copy bt_bdaddr_t to BD_ADDR format
706 *
707 * Returns void
708 *
709 ******************************************************************************/
btif_hl_copy_bda(bt_bdaddr_t * bd_addr,BD_ADDR bda)710 void btif_hl_copy_bda(bt_bdaddr_t* bd_addr, BD_ADDR bda) {
711 uint8_t i;
712 for (i = 0; i < 6; i++) {
713 bd_addr->address[i] = bda[i];
714 }
715 }
716 /*******************************************************************************
717 *
718 * Function btif_hl_copy_bda
719 *
720 * Description display bt_bdaddr_t
721 *
722 * Returns bool
723 *
724 ******************************************************************************/
btif_hl_display_bt_bda(bt_bdaddr_t * bd_addr)725 void btif_hl_display_bt_bda(bt_bdaddr_t* bd_addr) {
726 BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr->address[0],
727 bd_addr->address[1], bd_addr->address[2],
728 bd_addr->address[3], bd_addr->address[4],
729 bd_addr->address[5]);
730 }
731
732 /*******************************************************************************
733 *
734 * Function btif_hl_dch_abort
735 *
736 * Description Process DCH abort request
737 *
738 * Returns Nothing
739 *
740 ******************************************************************************/
btif_hl_dch_abort(uint8_t app_idx,uint8_t mcl_idx)741 void btif_hl_dch_abort(uint8_t app_idx, uint8_t mcl_idx) {
742 btif_hl_mcl_cb_t* p_mcb;
743
744 BTIF_TRACE_DEBUG("%s app_idx=%d mcl_idx=%d", __func__, app_idx, mcl_idx);
745 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
746 if (p_mcb->is_connected) {
747 BTA_HlDchAbort(p_mcb->mcl_handle);
748 } else {
749 p_mcb->pcb.abort_pending = true;
750 }
751 }
752 /*******************************************************************************
753 *
754 * Function btif_hl_cch_open
755 *
756 * Description Process CCH open request
757 *
758 * Returns Nothing
759 *
760 ******************************************************************************/
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)761 bool btif_hl_cch_open(uint8_t app_id, BD_ADDR bd_addr, uint16_t ctrl_psm,
762 int mdep_cfg_idx, btif_hl_pend_dch_op_t op,
763 int* channel_id) {
764 btif_hl_app_cb_t* p_acb;
765 btif_hl_mcl_cb_t* p_mcb;
766 btif_hl_pending_chan_cb_t* p_pcb;
767 uint8_t app_idx, mcl_idx;
768 bool status = true;
769
770 BTIF_TRACE_DEBUG("%s app_id=%d ctrl_psm=%d mdep_cfg_idx=%d op=%d", __func__,
771 app_id, ctrl_psm, mdep_cfg_idx, op);
772 BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr[0], bd_addr[1],
773 bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
774
775 if (btif_hl_find_app_idx(app_id, &app_idx)) {
776 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
777
778 if (!btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) {
779 if (btif_hl_find_avail_mcl_idx(app_idx, &mcl_idx)) {
780 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
781 alarm_free(p_mcb->cch_timer);
782 memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t));
783 p_mcb->in_use = true;
784 bdcpy(p_mcb->bd_addr, bd_addr);
785
786 if (!ctrl_psm) {
787 p_mcb->cch_oper = BTIF_HL_CCH_OP_MDEP_FILTERING;
788 } else {
789 p_mcb->cch_oper = BTIF_HL_CCH_OP_MATCHED_CTRL_PSM;
790 p_mcb->req_ctrl_psm = ctrl_psm;
791 }
792
793 p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
794 p_pcb->in_use = true;
795 p_pcb->mdep_cfg_idx = mdep_cfg_idx;
796 memcpy(p_pcb->bd_addr, bd_addr, sizeof(BD_ADDR));
797 p_pcb->op = op;
798
799 switch (op) {
800 case BTIF_HL_PEND_DCH_OP_OPEN:
801 *channel_id = p_pcb->channel_id =
802 (int)btif_hl_get_next_channel_id(app_id);
803 p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING;
804 break;
805 case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
806 p_pcb->channel_id = p_acb->delete_mdl.channel_id;
807 p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_DESTROYED_PENDING;
808 break;
809 default:
810 break;
811 }
812 BTA_HlSdpQuery(app_id, p_acb->app_handle, bd_addr);
813 } else {
814 status = false;
815 BTIF_TRACE_ERROR("Open CCH request discarded- No mcl cb");
816 }
817 } else {
818 status = false;
819 BTIF_TRACE_ERROR("Open CCH request discarded- already in USE");
820 }
821 } else {
822 status = false;
823 BTIF_TRACE_ERROR("Invalid app_id=%d", app_id);
824 }
825
826 if (channel_id) {
827 BTIF_TRACE_DEBUG("status=%d channel_id=0x%08x", status, *channel_id);
828 } else {
829 BTIF_TRACE_DEBUG("status=%d ", status);
830 }
831 return status;
832 }
833
834 /*******************************************************************************
835 *
836 * Function btif_hl_find_mdl_idx_using_handle
837 *
838 * Description Find the MDL index using channel id
839 *
840 * Returns bool
841 *
842 ******************************************************************************/
btif_hl_find_mdl_cfg_idx_using_channel_id(int channel_id,uint8_t * p_app_idx,uint8_t * p_mdl_cfg_idx)843 bool btif_hl_find_mdl_cfg_idx_using_channel_id(int channel_id,
844 uint8_t* p_app_idx,
845 uint8_t* p_mdl_cfg_idx) {
846 btif_hl_app_cb_t* p_acb;
847 btif_hl_mdl_cfg_t* p_mdl;
848 bool found = false;
849 uint8_t i, j;
850 int mdl_cfg_channel_id;
851
852 *p_app_idx = 0;
853 *p_mdl_cfg_idx = 0;
854 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
855 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
856 for (j = 0; j < BTA_HL_NUM_MDL_CFGS; j++) {
857 p_mdl = BTIF_HL_GET_MDL_CFG_PTR(i, j);
858 mdl_cfg_channel_id = *(BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(i, j));
859 if (p_acb->in_use && p_mdl->base.active &&
860 (mdl_cfg_channel_id == channel_id)) {
861 found = true;
862 *p_app_idx = i;
863 *p_mdl_cfg_idx = j;
864 break;
865 }
866 }
867 }
868
869 BTIF_TRACE_EVENT("%s found=%d channel_id=0x%08x, app_idx=%d mdl_cfg_idx=%d ",
870 __func__, found, channel_id, i, j);
871 return found;
872 }
873 /*******************************************************************************
874 *
875 * Function btif_hl_find_mdl_idx_using_handle
876 *
877 * Description Find the MDL index using channel id
878 *
879 * Returns bool
880 *
881 ******************************************************************************/
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)882 bool btif_hl_find_mdl_idx_using_channel_id(int channel_id, uint8_t* p_app_idx,
883 uint8_t* p_mcl_idx,
884 uint8_t* p_mdl_idx) {
885 btif_hl_app_cb_t* p_acb;
886 btif_hl_mcl_cb_t* p_mcb;
887 btif_hl_mdl_cb_t* p_dcb;
888 bool found = false;
889 uint8_t i, j, k;
890
891 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
892 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
893 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
894 p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j);
895 for (k = 0; k < BTA_HL_NUM_MDLS_PER_MCL; k++) {
896 p_dcb = BTIF_HL_GET_MDL_CB_PTR(i, j, k);
897 if (p_acb->in_use && p_mcb->in_use && p_dcb->in_use &&
898 (p_dcb->channel_id == channel_id)) {
899 found = true;
900 *p_app_idx = i;
901 *p_mcl_idx = j;
902 *p_mdl_idx = k;
903 break;
904 }
905 }
906 }
907 }
908 BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d ", __func__,
909 found, i, j, k);
910 return found;
911 }
912
913 /*******************************************************************************
914 *
915 * Function btif_hl_find_channel_id_using_mdl_id
916 *
917 * Description Find channel id using mdl_id'
918 *
919 * Returns bool
920 ******************************************************************************/
btif_hl_find_channel_id_using_mdl_id(uint8_t app_idx,tBTA_HL_MDL_ID mdl_id,int * p_channel_id)921 bool btif_hl_find_channel_id_using_mdl_id(uint8_t app_idx,
922 tBTA_HL_MDL_ID mdl_id,
923 int* p_channel_id) {
924 btif_hl_app_cb_t* p_acb;
925 btif_hl_mdl_cfg_t* p_mdl;
926 bool found = false;
927 uint8_t j = 0;
928 int mdl_cfg_channel_id;
929 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
930 if (p_acb && p_acb->in_use) {
931 for (j = 0; j < BTA_HL_NUM_MDL_CFGS; j++) {
932 p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, j);
933 mdl_cfg_channel_id = *(BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx, j));
934 if (p_mdl->base.active && (p_mdl->base.mdl_id == mdl_id)) {
935 found = true;
936 *p_channel_id = mdl_cfg_channel_id;
937 break;
938 }
939 }
940 }
941 BTIF_TRACE_EVENT(
942 "%s found=%d channel_id=0x%08x, mdl_id=0x%x app_idx=%d mdl_cfg_idx=%d ",
943 __func__, found, *p_channel_id, mdl_id, app_idx, j);
944 return found;
945 }
946
947 /*******************************************************************************
948 *
949 * Function btif_hl_find_mdl_idx_using_handle
950 *
951 * Description Find the MDL index using handle
952 *
953 * Returns bool
954 *
955 ******************************************************************************/
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)956 bool btif_hl_find_mdl_idx_using_handle(tBTA_HL_MDL_HANDLE mdl_handle,
957 uint8_t* p_app_idx, uint8_t* p_mcl_idx,
958 uint8_t* p_mdl_idx) {
959 btif_hl_app_cb_t* p_acb;
960 btif_hl_mcl_cb_t* p_mcb;
961 btif_hl_mdl_cb_t* p_dcb;
962 bool found = false;
963 uint8_t i, j, k;
964
965 *p_app_idx = 0;
966 *p_mcl_idx = 0;
967 *p_mdl_idx = 0;
968 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
969 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
970 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
971 p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j);
972 for (k = 0; k < BTA_HL_NUM_MDLS_PER_MCL; k++) {
973 p_dcb = BTIF_HL_GET_MDL_CB_PTR(i, j, k);
974 if (p_acb->in_use && p_mcb->in_use && p_dcb->in_use &&
975 (p_dcb->mdl_handle == mdl_handle)) {
976 found = true;
977 *p_app_idx = i;
978 *p_mcl_idx = j;
979 *p_mdl_idx = k;
980 break;
981 }
982 }
983 }
984 }
985
986 BTIF_TRACE_EVENT("%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d ", __func__,
987 found, i, j, k);
988 return found;
989 }
990 /*******************************************************************************
991 *
992 * Function btif_hl_find_peer_mdep_id
993 *
994 * Description Find the peer MDEP ID from the received SPD records
995 *
996 * Returns bool
997 *
998 ******************************************************************************/
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)999 static bool btif_hl_find_peer_mdep_id(uint8_t app_id, BD_ADDR bd_addr,
1000 tBTA_HL_MDEP_ROLE local_mdep_role,
1001 uint16_t data_type,
1002 tBTA_HL_MDEP_ID* p_peer_mdep_id) {
1003 uint8_t app_idx, mcl_idx;
1004 btif_hl_mcl_cb_t* p_mcb;
1005 tBTA_HL_SDP_REC* p_rec;
1006 uint8_t i, num_mdeps;
1007 bool found = false;
1008 tBTA_HL_MDEP_ROLE peer_mdep_role;
1009
1010 BTIF_TRACE_DEBUG("%s app_id=%d local_mdep_role=%d, data_type=%d", __func__,
1011 app_id, local_mdep_role, data_type);
1012
1013 BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr[0], bd_addr[1],
1014 bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
1015
1016 BTIF_TRACE_DEBUG("local_mdep_role=%d", local_mdep_role);
1017 BTIF_TRACE_DEBUG("data_type=%d", data_type);
1018
1019 if (local_mdep_role == BTA_HL_MDEP_ROLE_SINK)
1020 peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
1021 else
1022 peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
1023
1024 if (btif_hl_find_app_idx(app_id, &app_idx)) {
1025 if (btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) {
1026 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1027
1028 BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d", app_idx, mcl_idx);
1029 BTIF_TRACE_DEBUG("valid_spd_idx=%d sdp_idx=%d", p_mcb->valid_sdp_idx,
1030 p_mcb->sdp_idx);
1031 if (p_mcb->valid_sdp_idx) {
1032 p_rec = &p_mcb->sdp.sdp_rec[p_mcb->sdp_idx];
1033 num_mdeps = p_rec->num_mdeps;
1034 BTIF_TRACE_DEBUG("num_mdeps=%d", num_mdeps);
1035
1036 for (i = 0; i < num_mdeps; i++) {
1037 BTIF_TRACE_DEBUG("p_rec->mdep_cfg[%d].mdep_role=%d", i,
1038 p_rec->mdep_cfg[i].mdep_role);
1039 BTIF_TRACE_DEBUG("p_rec->mdep_cfg[%d].data_type =%d", i,
1040 p_rec->mdep_cfg[i].data_type);
1041 if ((p_rec->mdep_cfg[i].mdep_role == peer_mdep_role) &&
1042 (p_rec->mdep_cfg[i].data_type == data_type)) {
1043 found = true;
1044 *p_peer_mdep_id = p_rec->mdep_cfg[i].mdep_id;
1045 break;
1046 }
1047 }
1048 }
1049 }
1050 }
1051
1052 BTIF_TRACE_DEBUG("found =%d *p_peer_mdep_id=%d", found, *p_peer_mdep_id);
1053
1054 return found;
1055 }
1056
1057 /*******************************************************************************
1058 *
1059 * Function btif_hl_find_mdep_cfg_idx
1060 *
1061 * Description Find the MDEP configuration index using local MDEP_ID
1062 *
1063 * Returns bool
1064 *
1065 ******************************************************************************/
btif_hl_find_mdep_cfg_idx(uint8_t app_idx,tBTA_HL_MDEP_ID local_mdep_id,uint8_t * p_mdep_cfg_idx)1066 static bool btif_hl_find_mdep_cfg_idx(uint8_t app_idx,
1067 tBTA_HL_MDEP_ID local_mdep_id,
1068 uint8_t* p_mdep_cfg_idx) {
1069 btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
1070 tBTA_HL_SUP_FEATURE* p_sup_feature = &p_acb->sup_feature;
1071 bool found = false;
1072 uint8_t i;
1073
1074 for (i = 0; i < p_sup_feature->num_of_mdeps; i++) {
1075 BTIF_TRACE_DEBUG("btif_hl_find_mdep_cfg_idx: mdep_id=%d app_idx = %d",
1076 p_sup_feature->mdep[i].mdep_id, app_idx);
1077 if (p_sup_feature->mdep[i].mdep_id == local_mdep_id) {
1078 found = true;
1079 *p_mdep_cfg_idx = i;
1080 break;
1081 }
1082 }
1083
1084 BTIF_TRACE_DEBUG("%s found=%d mdep_idx=%d local_mdep_id=%d app_idx=%d ",
1085 __func__, found, i, local_mdep_id, app_idx);
1086 return found;
1087 }
1088
1089 /*******************************************************************************
1090 *
1091 * Function btif_hl_find_mcl_idx
1092 *
1093 * Description Find the MCL index using BD address
1094 *
1095 * Returns bool
1096 *
1097 ******************************************************************************/
btif_hl_find_mcl_idx(uint8_t app_idx,BD_ADDR p_bd_addr,uint8_t * p_mcl_idx)1098 bool btif_hl_find_mcl_idx(uint8_t app_idx, BD_ADDR p_bd_addr,
1099 uint8_t* p_mcl_idx) {
1100 bool found = false;
1101 uint8_t i;
1102 btif_hl_mcl_cb_t* p_mcb;
1103
1104 *p_mcl_idx = 0;
1105 for (i = 0; i < BTA_HL_NUM_MCLS; i++) {
1106 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, i);
1107 if (p_mcb->in_use && (!memcmp(p_mcb->bd_addr, p_bd_addr, BD_ADDR_LEN))) {
1108 found = true;
1109 *p_mcl_idx = i;
1110 break;
1111 }
1112 }
1113
1114 BTIF_TRACE_DEBUG("%s found=%d idx=%d", __func__, found, i);
1115 return found;
1116 }
1117 /*******************************************************************************
1118 *
1119 * Function btif_hl_init
1120 *
1121 * Description HL initialization function.
1122 *
1123 * Returns void
1124 *
1125 ******************************************************************************/
btif_hl_init(void)1126 static void btif_hl_init(void) {
1127 BTIF_TRACE_DEBUG("%s", __func__);
1128 memset(p_btif_hl_cb, 0, sizeof(btif_hl_cb_t));
1129 btif_hl_init_next_app_id();
1130 btif_hl_init_next_channel_id();
1131 }
1132 /*******************************************************************************
1133 *
1134 * Function btif_hl_disable
1135 *
1136 * Description Disable initialization function.
1137 *
1138 * Returns void
1139 *
1140 ******************************************************************************/
btif_hl_disable(void)1141 static void btif_hl_disable(void) {
1142 BTIF_TRACE_DEBUG("%s", __func__);
1143
1144 if ((p_btif_hl_cb->state != BTIF_HL_STATE_DISABLING) &&
1145 (p_btif_hl_cb->state != BTIF_HL_STATE_DISABLED)) {
1146 btif_hl_set_state(BTIF_HL_STATE_DISABLING);
1147 BTA_HlDisable();
1148 }
1149 }
1150 /*******************************************************************************
1151 *
1152 * Function btif_hl_is_no_active_app
1153 *
1154 * Description Find whether or not any APP is still in use
1155 *
1156 * Returns bool
1157 *
1158 ******************************************************************************/
btif_hl_is_no_active_app(void)1159 static bool btif_hl_is_no_active_app(void) {
1160 bool no_active_app = true;
1161 uint8_t i;
1162
1163 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
1164 if (btif_hl_cb.acb[i].in_use) {
1165 no_active_app = false;
1166 break;
1167 }
1168 }
1169
1170 BTIF_TRACE_DEBUG("%s no_active_app=%d ", __func__, no_active_app);
1171 return no_active_app;
1172 }
1173
1174 /*******************************************************************************
1175 *
1176 * Function btif_hl_free_app_idx
1177 *
1178 * Description free an application control block
1179 *
1180 * Returns void
1181 *
1182 ******************************************************************************/
btif_hl_free_app_idx(uint8_t app_idx)1183 static void btif_hl_free_app_idx(uint8_t app_idx) {
1184 if ((app_idx < BTA_HL_NUM_APPS) && btif_hl_cb.acb[app_idx].in_use) {
1185 btif_hl_cb.acb[app_idx].in_use = false;
1186 for (size_t i = 0; i < BTA_HL_NUM_MCLS; i++)
1187 alarm_free(btif_hl_cb.acb[app_idx].mcb[i].cch_timer);
1188 memset(&btif_hl_cb.acb[app_idx], 0, sizeof(btif_hl_app_cb_t));
1189 }
1190 }
1191 /*******************************************************************************
1192 *
1193 * Function btif_hl_set_state
1194 *
1195 * Description set HL state
1196 *
1197 * Returns void
1198 *
1199 ******************************************************************************/
btif_hl_set_state(btif_hl_state_t state)1200 static void btif_hl_set_state(btif_hl_state_t state) {
1201 BTIF_TRACE_DEBUG("btif_hl_set_state: %d ---> %d ", p_btif_hl_cb->state,
1202 state);
1203 p_btif_hl_cb->state = state;
1204 }
1205
1206 /*******************************************************************************
1207 *
1208 * Function btif_hl_set_state
1209 *
1210 * Description get HL state
1211 *
1212 * Returns btif_hl_state_t
1213 *
1214 ******************************************************************************/
1215
btif_hl_get_state(void)1216 static btif_hl_state_t btif_hl_get_state(void) {
1217 BTIF_TRACE_DEBUG("btif_hl_get_state: %d ", p_btif_hl_cb->state);
1218 return p_btif_hl_cb->state;
1219 }
1220
1221 /*******************************************************************************
1222 *
1223 * Function btif_hl_find_data_type_idx
1224 *
1225 * Description Find the index in the data type table
1226 *
1227 * Returns bool
1228 *
1229 ******************************************************************************/
btif_hl_find_data_type_idx(uint16_t data_type,uint8_t * p_idx)1230 static bool btif_hl_find_data_type_idx(uint16_t data_type, uint8_t* p_idx) {
1231 bool found = false;
1232 uint8_t i;
1233
1234 for (i = 0; i < BTIF_HL_DATA_TABLE_SIZE; i++) {
1235 if (data_type_table[i].data_type == data_type) {
1236 found = true;
1237 *p_idx = i;
1238 break;
1239 }
1240 }
1241
1242 BTIF_TRACE_DEBUG("%s found=%d, data_type=0x%x idx=%d", __func__, found,
1243 data_type, i);
1244 return found;
1245 }
1246
1247 /*******************************************************************************
1248 *
1249 * Function btif_hl_get_max_tx_apdu_size
1250 *
1251 * Description Find the maximum TX APDU size for the specified data type and
1252 * MDEP role
1253 *
1254 * Returns uint16_t
1255 *
1256 ******************************************************************************/
btif_hl_get_max_tx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role,uint16_t data_type)1257 uint16_t btif_hl_get_max_tx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role,
1258 uint16_t data_type) {
1259 uint8_t idx;
1260 uint16_t max_tx_apdu_size = 0;
1261
1262 if (btif_hl_find_data_type_idx(data_type, &idx)) {
1263 if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) {
1264 max_tx_apdu_size = data_type_table[idx].max_tx_apdu_size;
1265 } else {
1266 max_tx_apdu_size = data_type_table[idx].max_rx_apdu_size;
1267 }
1268 } else {
1269 if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) {
1270 max_tx_apdu_size = BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE;
1271 } else {
1272 max_tx_apdu_size = BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE;
1273 }
1274 }
1275
1276 BTIF_TRACE_DEBUG("%s mdep_role=%d data_type=0x%4x size=%d", __func__,
1277 mdep_role, data_type, max_tx_apdu_size);
1278 return max_tx_apdu_size;
1279 }
1280
1281 /*******************************************************************************
1282 *
1283 * Function btif_hl_get_max_rx_apdu_size
1284 *
1285 * Description Find the maximum RX APDU size for the specified data type and
1286 * MDEP role
1287 *
1288 * Returns uint16_t
1289 *
1290 ******************************************************************************/
btif_hl_get_max_rx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role,uint16_t data_type)1291 uint16_t btif_hl_get_max_rx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role,
1292 uint16_t data_type) {
1293 uint8_t idx;
1294 uint16_t max_rx_apdu_size = 0;
1295
1296 if (btif_hl_find_data_type_idx(data_type, &idx)) {
1297 if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) {
1298 max_rx_apdu_size = data_type_table[idx].max_rx_apdu_size;
1299 } else {
1300 max_rx_apdu_size = data_type_table[idx].max_tx_apdu_size;
1301 }
1302 } else {
1303 if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) {
1304 max_rx_apdu_size = BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE;
1305 } else {
1306 max_rx_apdu_size = BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE;
1307 }
1308 }
1309
1310 BTIF_TRACE_DEBUG("%s mdep_role=%d data_type=0x%4x size=%d", __func__,
1311 mdep_role, data_type, max_rx_apdu_size);
1312
1313 return max_rx_apdu_size;
1314 }
1315
1316 /*******************************************************************************
1317 *
1318 * Function btif_hl_if_channel_setup_pending
1319 *
1320 * Description
1321 *
1322 * Returns bool
1323 *
1324 ******************************************************************************/
1325
btif_hl_get_bta_mdep_role(bthl_mdep_role_t mdep,tBTA_HL_MDEP_ROLE * p)1326 static bool btif_hl_get_bta_mdep_role(bthl_mdep_role_t mdep,
1327 tBTA_HL_MDEP_ROLE* p) {
1328 bool status = true;
1329 switch (mdep) {
1330 case BTHL_MDEP_ROLE_SOURCE:
1331 *p = BTA_HL_MDEP_ROLE_SOURCE;
1332 break;
1333 case BTHL_MDEP_ROLE_SINK:
1334 *p = BTA_HL_MDEP_ROLE_SINK;
1335 break;
1336 default:
1337 *p = BTA_HL_MDEP_ROLE_SOURCE;
1338 status = false;
1339 break;
1340 }
1341
1342 BTIF_TRACE_DEBUG("%s status=%d bta_mdep_role=%d (%d:btif)", __func__, status,
1343 *p, mdep);
1344 return status;
1345 }
1346 /*******************************************************************************
1347 *
1348 * Function btif_hl_get_bta_channel_type
1349 *
1350 * Description convert bthl channel type to BTA DCH channel type
1351 *
1352 * Returns bool
1353 *
1354 ******************************************************************************/
1355
btif_hl_get_bta_channel_type(bthl_channel_type_t channel_type,tBTA_HL_DCH_CFG * p)1356 static bool btif_hl_get_bta_channel_type(bthl_channel_type_t channel_type,
1357 tBTA_HL_DCH_CFG* p) {
1358 bool status = true;
1359 switch (channel_type) {
1360 case BTHL_CHANNEL_TYPE_RELIABLE:
1361 *p = BTA_HL_DCH_CFG_RELIABLE;
1362 break;
1363 case BTHL_CHANNEL_TYPE_STREAMING:
1364 *p = BTA_HL_DCH_CFG_STREAMING;
1365 break;
1366 case BTHL_CHANNEL_TYPE_ANY:
1367 *p = BTA_HL_DCH_CFG_NO_PREF;
1368 break;
1369 default:
1370 status = false;
1371 break;
1372 }
1373 BTIF_TRACE_DEBUG("%s status = %d BTA DCH CFG=%d (1-rel 2-strm", __func__,
1374 status, *p);
1375 return status;
1376 }
1377 /*******************************************************************************
1378 *
1379 * Function btif_hl_get_next_app_id
1380 *
1381 * Description get next applcation id
1382 *
1383 * Returns uint8_t
1384 *
1385 ******************************************************************************/
1386
btif_hl_get_next_app_id()1387 static uint8_t btif_hl_get_next_app_id() {
1388 uint8_t next_app_id = btif_hl_cb.next_app_id;
1389
1390 btif_hl_cb.next_app_id++;
1391 return next_app_id;
1392 }
1393 /*******************************************************************************
1394 *
1395 * Function btif_hl_get_next_channel_id
1396 *
1397 * Description get next channel id
1398 *
1399 * Returns int
1400 *
1401 ******************************************************************************/
btif_hl_get_next_channel_id(uint8_t app_id)1402 static int btif_hl_get_next_channel_id(uint8_t app_id) {
1403 uint16_t next_channel_id = btif_hl_cb.next_channel_id;
1404 int channel_id;
1405 btif_hl_cb.next_channel_id++;
1406 channel_id = (app_id << 16) + next_channel_id;
1407 BTIF_TRACE_DEBUG("%s channel_id=0x%08x, app_id=0x%02x next_channel_id=0x%04x",
1408 __func__, channel_id, app_id, next_channel_id);
1409 return channel_id;
1410 }
1411 /*******************************************************************************
1412 *
1413 * Function btif_hl_get_app_id
1414 *
1415 * Description get the applicaiton id associated with the channel id
1416 *
1417 * Returns uint8_t
1418 *
1419 ******************************************************************************/
1420
btif_hl_get_app_id(int channel_id)1421 static uint8_t btif_hl_get_app_id(int channel_id) {
1422 uint8_t app_id = (uint8_t)(channel_id >> 16);
1423 BTIF_TRACE_DEBUG("%s channel_id=0x%08x, app_id=0x%02x ", __func__, channel_id,
1424 app_id);
1425 return app_id;
1426 }
1427 /*******************************************************************************
1428 *
1429 * Function btif_hl_init_next_app_id
1430 *
1431 * Description initialize the application id
1432 *
1433 * Returns void
1434 *
1435 ******************************************************************************/
btif_hl_init_next_app_id(void)1436 static void btif_hl_init_next_app_id(void) { btif_hl_cb.next_app_id = 1; }
1437 /*******************************************************************************
1438 *
1439 * Function btif_hl_init_next_channel_id
1440 *
1441 * Description initialize the channel id
1442 *
1443 * Returns void
1444 *
1445 ******************************************************************************/
btif_hl_init_next_channel_id(void)1446 static void btif_hl_init_next_channel_id(void) {
1447 btif_hl_cb.next_channel_id = 1;
1448 }
1449
1450 /*******************************************************************************
1451 *
1452 * Function btif_hl_find_app_idx_using_handle
1453 *
1454 * Description Find the applicaiton index using handle
1455 *
1456 * Returns bool
1457 *
1458 ******************************************************************************/
btif_hl_find_app_idx_using_handle(tBTA_HL_APP_HANDLE app_handle,uint8_t * p_app_idx)1459 bool btif_hl_find_app_idx_using_handle(tBTA_HL_APP_HANDLE app_handle,
1460 uint8_t* p_app_idx) {
1461 bool found = false;
1462 uint8_t i;
1463
1464 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
1465 if (btif_hl_cb.acb[i].in_use &&
1466 (btif_hl_cb.acb[i].app_handle == app_handle)) {
1467 found = true;
1468 *p_app_idx = i;
1469 break;
1470 }
1471 }
1472
1473 BTIF_TRACE_EVENT("%s status=%d handle=%d app_idx=%d ", __func__, found,
1474 app_handle, i);
1475
1476 return found;
1477 }
1478
1479 /*******************************************************************************
1480 *
1481 * Function btif_hl_find_app_idx_using_app_id
1482 *
1483 * Description Find the applicaiton index using app_id
1484 *
1485 * Returns bool
1486 *
1487 ******************************************************************************/
btif_hl_find_app_idx_using_app_id(uint8_t app_id,uint8_t * p_app_idx)1488 bool btif_hl_find_app_idx_using_app_id(uint8_t app_id, uint8_t* p_app_idx) {
1489 bool found = false;
1490 uint8_t i;
1491
1492 *p_app_idx = 0;
1493 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
1494 if (btif_hl_cb.acb[i].in_use && (btif_hl_cb.acb[i].app_id == app_id)) {
1495 found = true;
1496 *p_app_idx = i;
1497 break;
1498 }
1499 }
1500
1501 BTIF_TRACE_EVENT("%s found=%d app_id=%d app_idx=%d ", __func__, found, app_id,
1502 i);
1503
1504 return found;
1505 }
1506
1507 /*******************************************************************************
1508 *
1509 * Function btif_hl_find_mcl_idx_using_handle
1510 *
1511 * Description Find the MCL index using handle
1512 *
1513 * Returns bool
1514 *
1515 ******************************************************************************/
btif_hl_find_mcl_idx_using_handle(tBTA_HL_MCL_HANDLE mcl_handle,uint8_t * p_app_idx,uint8_t * p_mcl_idx)1516 bool btif_hl_find_mcl_idx_using_handle(tBTA_HL_MCL_HANDLE mcl_handle,
1517 uint8_t* p_app_idx, uint8_t* p_mcl_idx) {
1518 btif_hl_app_cb_t* p_acb;
1519 bool found = false;
1520 uint8_t i, j;
1521
1522 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
1523 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
1524 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
1525 if (p_acb->mcb[j].in_use)
1526 BTIF_TRACE_DEBUG(
1527 "btif_hl_find_mcl_idx_using_handle:app_idx=%d,"
1528 "mcl_idx =%d mcl_handle=%d",
1529 i, j, p_acb->mcb[j].mcl_handle);
1530 if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
1531 found = true;
1532 *p_app_idx = i;
1533 *p_mcl_idx = j;
1534 break;
1535 }
1536 }
1537 }
1538 BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d", __func__, found, i, j);
1539 return found;
1540 }
1541
1542 /*******************************************************************************
1543 *
1544 * Function btif_hl_find_mdl_idx_using_mdl_id
1545 *
1546 * Description Find the mdl index using mdl_id
1547 *
1548 * Returns bool
1549 *
1550 ******************************************************************************/
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)1551 bool btif_hl_find_mcl_idx_using_mdl_id(uint8_t mdl_id, uint8_t mcl_handle,
1552 uint8_t* p_app_idx, uint8_t* p_mcl_idx) {
1553 btif_hl_app_cb_t* p_acb;
1554 btif_hl_mcl_cb_t* p_mcb;
1555 bool found = false;
1556 uint8_t i, j, x;
1557
1558 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
1559 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
1560 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
1561 if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
1562 p_mcb = &p_acb->mcb[j];
1563 BTIF_TRACE_DEBUG(
1564 "btif_hl_find_mcl_idx_using_mdl_id: mcl handle found j =%d", j);
1565 for (x = 0; x < BTA_HL_NUM_MDLS_PER_MCL; x++) {
1566 if (p_mcb->mdl[x].in_use && p_mcb->mdl[x].mdl_id == mdl_id) {
1567 BTIF_TRACE_DEBUG("btif_hl_find_mcl_idx_using_mdl_id:found x =%d",
1568 x);
1569 found = true;
1570 *p_app_idx = i;
1571 *p_mcl_idx = j;
1572 break;
1573 }
1574 }
1575 }
1576 }
1577 }
1578 BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d", __func__, found, i, j);
1579 return found;
1580 }
1581
1582 /*******************************************************************************
1583 *
1584 * Function btif_hl_find_mcl_idx_using_deleted_mdl_id
1585 *
1586 * Description Find the app index deleted_mdl_id
1587 *
1588 * Returns bool
1589 *
1590 ******************************************************************************/
btif_hl_find_app_idx_using_deleted_mdl_id(uint8_t mdl_id,uint8_t * p_app_idx)1591 bool btif_hl_find_app_idx_using_deleted_mdl_id(uint8_t mdl_id,
1592 uint8_t* p_app_idx) {
1593 btif_hl_app_cb_t* p_acb;
1594 bool found = false;
1595 uint8_t i;
1596
1597 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
1598 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
1599 if (p_acb->delete_mdl.active) {
1600 BTIF_TRACE_DEBUG("%s: app_idx=%d, mdl_id=%d", __func__, i, mdl_id);
1601 }
1602 if (p_acb->delete_mdl.active && (p_acb->delete_mdl.mdl_id == mdl_id)) {
1603 found = true;
1604 *p_app_idx = i;
1605 break;
1606 }
1607 }
1608 BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i);
1609 return found;
1610 }
1611
1612 /*******************************************************************************
1613 *
1614 * Function btif_hl_stop_timer_using_handle
1615 *
1616 * Description clean control channel cb using handle
1617 *
1618 * Returns void
1619 *
1620 ******************************************************************************/
btif_hl_stop_timer_using_handle(tBTA_HL_MCL_HANDLE mcl_handle)1621 static void btif_hl_stop_timer_using_handle(tBTA_HL_MCL_HANDLE mcl_handle) {
1622 btif_hl_app_cb_t* p_acb;
1623 uint8_t i, j;
1624
1625 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
1626 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
1627 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
1628 if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
1629 btif_hl_stop_cch_timer(i, j);
1630 }
1631 }
1632 }
1633 }
1634
1635 /*******************************************************************************
1636 *
1637 * Function btif_hl_find_mcl_idx_using_app_idx
1638 *
1639 * Description Find the MCL index using handle
1640 *
1641 * Returns bool
1642 *
1643 ******************************************************************************/
btif_hl_find_mcl_idx_using_app_idx(tBTA_HL_MCL_HANDLE mcl_handle,uint8_t p_app_idx,uint8_t * p_mcl_idx)1644 bool btif_hl_find_mcl_idx_using_app_idx(tBTA_HL_MCL_HANDLE mcl_handle,
1645 uint8_t p_app_idx, uint8_t* p_mcl_idx) {
1646 btif_hl_app_cb_t* p_acb;
1647 bool found = false;
1648 uint8_t j;
1649
1650 p_acb = BTIF_HL_GET_APP_CB_PTR(p_app_idx);
1651 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
1652 if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
1653 found = true;
1654 *p_mcl_idx = j;
1655 break;
1656 }
1657 }
1658 BTIF_TRACE_DEBUG("%s found=%dmcl_idx=%d", __func__, found, j);
1659 return found;
1660 }
1661
1662 /*******************************************************************************
1663 *
1664 * Function btif_hl_clean_mdls_using_app_idx
1665 *
1666 * Description clean dch cpntrol bloack using app_idx
1667 *
1668 * Returns void
1669 *
1670 ******************************************************************************/
btif_hl_clean_mdls_using_app_idx(uint8_t app_idx)1671 void btif_hl_clean_mdls_using_app_idx(uint8_t app_idx) {
1672 btif_hl_app_cb_t* p_acb;
1673 btif_hl_mcl_cb_t* p_mcb;
1674 btif_hl_mdl_cb_t* p_dcb;
1675 uint8_t j, x, y;
1676 bt_bdaddr_t bd_addr;
1677
1678 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
1679 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
1680 if (p_acb->mcb[j].in_use) {
1681 p_mcb = &p_acb->mcb[j];
1682 BTIF_TRACE_DEBUG(
1683 "btif_hl_find_mcl_idx_using_mdl_id: mcl handle found j =%d", j);
1684 for (x = 0; x < BTA_HL_NUM_MDLS_PER_MCL; x++) {
1685 if (p_mcb->mdl[x].in_use) {
1686 p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, j, x);
1687 btif_hl_release_socket(app_idx, j, x);
1688 for (y = 0; y < 6; y++) {
1689 bd_addr.address[y] = p_mcb->bd_addr[y];
1690 }
1691 BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, p_acb->app_id,
1692 &bd_addr, p_dcb->local_mdep_cfg_idx,
1693 p_dcb->channel_id, BTHL_CONN_STATE_DISCONNECTED,
1694 0);
1695 btif_hl_clean_mdl_cb(p_dcb);
1696 if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle))
1697 BTA_HlCchClose(p_mcb->mcl_handle);
1698 BTIF_TRACE_DEBUG("remote DCH close success mdl_idx=%d", x);
1699 }
1700 }
1701 }
1702 }
1703 }
1704
1705 /*******************************************************************************
1706 *
1707 * Function btif_hl_find_app_idx
1708 *
1709 * Description Find the application index using application ID
1710 *
1711 * Returns bool
1712 *
1713 ******************************************************************************/
btif_hl_find_app_idx(uint8_t app_id,uint8_t * p_app_idx)1714 bool btif_hl_find_app_idx(uint8_t app_id, uint8_t* p_app_idx) {
1715 bool found = false;
1716 uint8_t i;
1717
1718 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
1719 if (btif_hl_cb.acb[i].in_use && (btif_hl_cb.acb[i].app_id == app_id)) {
1720 found = true;
1721 *p_app_idx = i;
1722 break;
1723 }
1724 }
1725 BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i);
1726
1727 return found;
1728 }
1729
1730 /*******************************************************************************
1731 *
1732 * Function btif_hl_find_app_idx
1733 *
1734 * Description Find the application index using application ID
1735 *
1736 * Returns bool
1737 *
1738 ******************************************************************************/
btif_hl_find_app_idx_using_mdepId(uint8_t mdep_id,uint8_t * p_app_idx)1739 bool btif_hl_find_app_idx_using_mdepId(uint8_t mdep_id, uint8_t* p_app_idx) {
1740 bool found = false;
1741 uint8_t i;
1742
1743 *p_app_idx = 0;
1744 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
1745 BTIF_TRACE_DEBUG("btif_hl_find_app_idx_using_mdepId: MDEP-ID = %d",
1746 btif_hl_cb.acb[i].sup_feature.mdep[0].mdep_id);
1747 if (btif_hl_cb.acb[i].in_use &&
1748 (btif_hl_cb.acb[i].sup_feature.mdep[0].mdep_id == mdep_id)) {
1749 found = true;
1750 *p_app_idx = i;
1751 break;
1752 }
1753 }
1754 BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i);
1755
1756 return found;
1757 }
1758
1759 /*******************************************************************************
1760 *
1761 * Function btif_hl_find_avail_mdl_idx
1762 *
1763 * Description Find a not in-use MDL index
1764 *
1765 * Returns bool
1766 *
1767 ******************************************************************************/
btif_hl_find_avail_mdl_idx(uint8_t app_idx,uint8_t mcl_idx,uint8_t * p_mdl_idx)1768 bool btif_hl_find_avail_mdl_idx(uint8_t app_idx, uint8_t mcl_idx,
1769 uint8_t* p_mdl_idx) {
1770 btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1771 bool found = false;
1772 uint8_t i;
1773
1774 for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) {
1775 if (!p_mcb->mdl[i].in_use) {
1776 btif_hl_clean_mdl_cb(&p_mcb->mdl[i]);
1777 found = true;
1778 *p_mdl_idx = i;
1779 break;
1780 }
1781 }
1782
1783 BTIF_TRACE_DEBUG("%s found=%d idx=%d", __func__, found, i);
1784 return found;
1785 }
1786
1787 /*******************************************************************************
1788 *
1789 * Function btif_hl_find_avail_mcl_idx
1790 *
1791 * Description Find a not in-use MDL index
1792 *
1793 * Returns bool
1794 *
1795 ******************************************************************************/
btif_hl_find_avail_mcl_idx(uint8_t app_idx,uint8_t * p_mcl_idx)1796 bool btif_hl_find_avail_mcl_idx(uint8_t app_idx, uint8_t* p_mcl_idx) {
1797 bool found = false;
1798 uint8_t i;
1799
1800 for (i = 0; i < BTA_HL_NUM_MCLS; i++) {
1801 if (!btif_hl_cb.acb[app_idx].mcb[i].in_use) {
1802 found = true;
1803 *p_mcl_idx = i;
1804 break;
1805 }
1806 }
1807 BTIF_TRACE_DEBUG("%s found=%d mcl_idx=%d", __func__, found, i);
1808 return found;
1809 }
1810
1811 /*******************************************************************************
1812 *
1813 * Function btif_hl_find_avail_app_idx
1814 *
1815 * Description Find a not in-use APP index
1816 *
1817 * Returns bool
1818 *
1819 ******************************************************************************/
btif_hl_find_avail_app_idx(uint8_t * p_idx)1820 static bool btif_hl_find_avail_app_idx(uint8_t* p_idx) {
1821 bool found = false;
1822 uint8_t i;
1823
1824 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
1825 if (!btif_hl_cb.acb[i].in_use) {
1826 found = true;
1827 *p_idx = i;
1828 break;
1829 }
1830 }
1831
1832 BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i);
1833 return found;
1834 }
1835
1836 /*******************************************************************************
1837 *
1838 * Function btif_hl_proc_dereg_cfm
1839 *
1840 * Description Process the de-registration confirmation
1841 *
1842 * Returns Nothing
1843 *
1844 ******************************************************************************/
btif_hl_proc_dereg_cfm(tBTA_HL * p_data)1845 static void btif_hl_proc_dereg_cfm(tBTA_HL* p_data)
1846
1847 {
1848 btif_hl_app_cb_t* p_acb;
1849 uint8_t app_idx;
1850 int app_id = 0;
1851 bthl_app_reg_state_t state = BTHL_APP_REG_STATE_DEREG_SUCCESS;
1852
1853 BTIF_TRACE_DEBUG("%s de-reg status=%d app_handle=%d", __func__,
1854 p_data->dereg_cfm.status, p_data->dereg_cfm.app_handle);
1855
1856 if (btif_hl_find_app_idx_using_app_id(p_data->dereg_cfm.app_id, &app_idx)) {
1857 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
1858 app_id = (int)p_acb->app_id;
1859 if (p_data->dereg_cfm.status == BTA_HL_STATUS_OK) {
1860 btif_hl_clean_mdls_using_app_idx(app_idx);
1861 for (size_t i = 0; i < BTA_HL_NUM_MCLS; i++)
1862 alarm_free(p_acb->mcb[i].cch_timer);
1863 memset(p_acb, 0, sizeof(btif_hl_app_cb_t));
1864 } else
1865 state = BTHL_APP_REG_STATE_DEREG_FAILED;
1866
1867 BTIF_TRACE_DEBUG("call reg state callback app_id=%d state=%d", app_id,
1868 state);
1869 BTIF_HL_CALL_CBACK(bt_hl_callbacks, app_reg_state_cb, app_id, state);
1870
1871 if (btif_hl_is_no_active_app()) {
1872 btif_hl_disable();
1873 }
1874 }
1875 }
1876
1877 /*******************************************************************************
1878 *
1879 * Function btif_hl_proc_reg_cfm
1880 *
1881 * Description Process the registration confirmation
1882 *
1883 * Returns Nothing
1884 *
1885 ******************************************************************************/
btif_hl_proc_reg_cfm(tBTA_HL * p_data)1886 static void btif_hl_proc_reg_cfm(tBTA_HL* p_data) {
1887 btif_hl_app_cb_t* p_acb;
1888 uint8_t app_idx;
1889 bthl_app_reg_state_t state = BTHL_APP_REG_STATE_REG_SUCCESS;
1890
1891 BTIF_TRACE_DEBUG("%s reg status=%d app_handle=%d", __func__,
1892 p_data->reg_cfm.status, p_data->reg_cfm.app_handle);
1893
1894 if (btif_hl_find_app_idx(p_data->reg_cfm.app_id, &app_idx)) {
1895 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
1896 if (p_data->reg_cfm.status == BTA_HL_STATUS_OK) {
1897 p_acb->app_handle = p_data->reg_cfm.app_handle;
1898 } else {
1899 btif_hl_free_app_idx(app_idx);
1900 state = BTHL_APP_REG_STATE_REG_FAILED;
1901 }
1902
1903 BTIF_TRACE_DEBUG("%s call reg state callback app_id=%d reg state=%d",
1904 __func__, p_data->reg_cfm.app_id, state);
1905 BTIF_HL_CALL_CBACK(bt_hl_callbacks, app_reg_state_cb,
1906 ((int)p_data->reg_cfm.app_id), state);
1907 }
1908 }
1909
1910 /*******************************************************************************
1911 *
1912 * Function btif_hl_set_chan_cb_state
1913 *
1914 * Description set the channel callback state
1915 *
1916 * Returns void
1917 *
1918 ******************************************************************************/
btif_hl_set_chan_cb_state(uint8_t app_idx,uint8_t mcl_idx,btif_hl_chan_cb_state_t state)1919 void btif_hl_set_chan_cb_state(uint8_t app_idx, uint8_t mcl_idx,
1920 btif_hl_chan_cb_state_t state) {
1921 btif_hl_pending_chan_cb_t* p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
1922 btif_hl_chan_cb_state_t cur_state = p_pcb->cb_state;
1923
1924 if (cur_state != state) {
1925 p_pcb->cb_state = state;
1926 BTIF_TRACE_DEBUG("%s state %d--->%d", __func__, cur_state, state);
1927 }
1928 }
1929 /*******************************************************************************
1930 *
1931 * Function btif_hl_send_destroyed_cb
1932 *
1933 * Description send the channel destroyed callback
1934 *
1935 * Returns void
1936 *
1937 ******************************************************************************/
btif_hl_send_destroyed_cb(btif_hl_app_cb_t * p_acb)1938 void btif_hl_send_destroyed_cb(btif_hl_app_cb_t* p_acb) {
1939 bt_bdaddr_t bd_addr;
1940 int app_id = (int)btif_hl_get_app_id(p_acb->delete_mdl.channel_id);
1941
1942 btif_hl_copy_bda(&bd_addr, p_acb->delete_mdl.bd_addr);
1943 BTIF_TRACE_DEBUG("%s", __func__);
1944 BTIF_TRACE_DEBUG(
1945 "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d, state=%d "
1946 "fd=%d",
1947 p_acb->delete_mdl.channel_id, p_acb->delete_mdl.mdep_cfg_idx,
1948 BTHL_CONN_STATE_DESTROYED, 0);
1949 btif_hl_display_bt_bda(&bd_addr);
1950
1951 BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
1952 p_acb->delete_mdl.mdep_cfg_idx,
1953 p_acb->delete_mdl.channel_id, BTHL_CONN_STATE_DESTROYED,
1954 0);
1955 }
1956 /*******************************************************************************
1957 *
1958 * Function btif_hl_send_disconnecting_cb
1959 *
1960 * Description send a channel disconnecting callback
1961 *
1962 * Returns void
1963 *
1964 ******************************************************************************/
btif_hl_send_disconnecting_cb(uint8_t app_idx,uint8_t mcl_idx,uint8_t mdl_idx)1965 void btif_hl_send_disconnecting_cb(uint8_t app_idx, uint8_t mcl_idx,
1966 uint8_t mdl_idx) {
1967 btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
1968 btif_hl_soc_cb_t* p_scb = p_dcb->p_scb;
1969 bt_bdaddr_t bd_addr;
1970 int app_id = (int)btif_hl_get_app_id(p_scb->channel_id);
1971
1972 btif_hl_copy_bda(&bd_addr, p_scb->bd_addr);
1973
1974 BTIF_TRACE_DEBUG("%s", __func__);
1975 BTIF_TRACE_DEBUG(
1976 "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d, "
1977 "state=%d fd=%d",
1978 p_scb->channel_id, p_scb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTING,
1979 p_scb->socket_id[0]);
1980 btif_hl_display_bt_bda(&bd_addr);
1981 BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
1982 p_scb->mdep_cfg_idx, p_scb->channel_id,
1983 BTHL_CONN_STATE_DISCONNECTING, p_scb->socket_id[0]);
1984 }
1985 /*******************************************************************************
1986 *
1987 * Function btif_hl_send_setup_connecting_cb
1988 *
1989 * Description send a channel connecting callback
1990 *
1991 * Returns void
1992 *
1993 ******************************************************************************/
btif_hl_send_setup_connecting_cb(uint8_t app_idx,uint8_t mcl_idx)1994 void btif_hl_send_setup_connecting_cb(uint8_t app_idx, uint8_t mcl_idx) {
1995 btif_hl_pending_chan_cb_t* p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
1996 bt_bdaddr_t bd_addr;
1997 int app_id = (int)btif_hl_get_app_id(p_pcb->channel_id);
1998
1999 btif_hl_copy_bda(&bd_addr, p_pcb->bd_addr);
2000
2001 if (p_pcb->in_use &&
2002 p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING) {
2003 BTIF_TRACE_DEBUG("%s", __func__);
2004 BTIF_TRACE_DEBUG(
2005 "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d "
2006 "state=%d fd=%d",
2007 p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_CONNECTING, 0);
2008 btif_hl_display_bt_bda(&bd_addr);
2009
2010 BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
2011 p_pcb->mdep_cfg_idx, p_pcb->channel_id,
2012 BTHL_CONN_STATE_CONNECTING, 0);
2013 btif_hl_set_chan_cb_state(app_idx, mcl_idx,
2014 BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING);
2015 }
2016 }
2017 /*******************************************************************************
2018 *
2019 * Function btif_hl_send_setup_disconnected_cb
2020 *
2021 * Description send a channel disconnected callback
2022 *
2023 * Returns void
2024 *
2025 ******************************************************************************/
btif_hl_send_setup_disconnected_cb(uint8_t app_idx,uint8_t mcl_idx)2026 void btif_hl_send_setup_disconnected_cb(uint8_t app_idx, uint8_t mcl_idx) {
2027 btif_hl_pending_chan_cb_t* p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
2028 bt_bdaddr_t bd_addr;
2029 int app_id = (int)btif_hl_get_app_id(p_pcb->channel_id);
2030
2031 btif_hl_copy_bda(&bd_addr, p_pcb->bd_addr);
2032
2033 BTIF_TRACE_DEBUG("%s p_pcb->in_use=%d", __func__, p_pcb->in_use);
2034 if (p_pcb->in_use) {
2035 BTIF_TRACE_DEBUG("%p_pcb->cb_state=%d", p_pcb->cb_state);
2036 if (p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING) {
2037 BTIF_TRACE_DEBUG(
2038 "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d "
2039 "state=%d fd=%d",
2040 p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_CONNECTING,
2041 0);
2042 btif_hl_display_bt_bda(&bd_addr);
2043 BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
2044 p_pcb->mdep_cfg_idx, p_pcb->channel_id,
2045 BTHL_CONN_STATE_CONNECTING, 0);
2046
2047 BTIF_TRACE_DEBUG(
2048 "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d "
2049 "state=%d fd=%d",
2050 p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTED,
2051 0);
2052 btif_hl_display_bt_bda(&bd_addr);
2053 BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
2054 p_pcb->mdep_cfg_idx, p_pcb->channel_id,
2055 BTHL_CONN_STATE_DISCONNECTED, 0);
2056 } else if (p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING) {
2057 BTIF_TRACE_DEBUG(
2058 "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d "
2059 "state=%d fd=%d",
2060 p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTED,
2061 0);
2062 btif_hl_display_bt_bda(&bd_addr);
2063 BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr,
2064 p_pcb->mdep_cfg_idx, p_pcb->channel_id,
2065 BTHL_CONN_STATE_DISCONNECTED, 0);
2066 }
2067 btif_hl_clean_pcb(p_pcb);
2068 }
2069 }
2070 /*******************************************************************************
2071 *
2072 * Function btif_hl_proc_sdp_query_cfm
2073 *
2074 * Description Process the SDP query confirmation
2075 *
2076 * Returns Nothing
2077 *
2078 ******************************************************************************/
btif_hl_proc_sdp_query_cfm(tBTA_HL * p_data)2079 static bool btif_hl_proc_sdp_query_cfm(tBTA_HL* p_data) {
2080 btif_hl_app_cb_t* p_acb;
2081 btif_hl_mcl_cb_t* p_mcb;
2082 tBTA_HL_SDP* p_sdp;
2083 tBTA_HL_CCH_OPEN_PARAM open_param;
2084 uint8_t app_idx, mcl_idx, sdp_idx = 0;
2085 uint8_t num_recs, i, num_mdeps, j;
2086 btif_hl_cch_op_t old_cch_oper;
2087 bool status = false;
2088 btif_hl_pending_chan_cb_t* p_pcb;
2089
2090 BTIF_TRACE_DEBUG("%s", __func__);
2091
2092 p_sdp = p_data->sdp_query_cfm.p_sdp;
2093 num_recs = p_sdp->num_recs;
2094
2095 BTIF_TRACE_DEBUG("num of SDP records=%d", num_recs);
2096 for (i = 0; i < num_recs; i++) {
2097 BTIF_TRACE_DEBUG("rec_idx=%d ctrl_psm=0x%x data_psm=0x%x", (i + 1),
2098 p_sdp->sdp_rec[i].ctrl_psm, p_sdp->sdp_rec[i].data_psm);
2099 BTIF_TRACE_DEBUG("MCAP supported procedures=0x%x",
2100 p_sdp->sdp_rec[i].mcap_sup_proc);
2101 num_mdeps = p_sdp->sdp_rec[i].num_mdeps;
2102 BTIF_TRACE_DEBUG("num of mdeps =%d", num_mdeps);
2103 for (j = 0; j < num_mdeps; j++) {
2104 BTIF_TRACE_DEBUG("mdep_idx=%d mdep_id=0x%x data_type=0x%x mdep_role=0x%x",
2105 (j + 1), p_sdp->sdp_rec[i].mdep_cfg[j].mdep_id,
2106 p_sdp->sdp_rec[i].mdep_cfg[j].data_type,
2107 p_sdp->sdp_rec[i].mdep_cfg[j].mdep_role);
2108 }
2109 }
2110
2111 if (btif_hl_find_app_idx_using_app_id(p_data->sdp_query_cfm.app_id,
2112 &app_idx)) {
2113 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
2114
2115 if (btif_hl_find_mcl_idx(app_idx, p_data->sdp_query_cfm.bd_addr,
2116 &mcl_idx)) {
2117 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
2118 if (p_mcb->cch_oper != BTIF_HL_CCH_OP_NONE) {
2119 memcpy(&p_mcb->sdp, p_sdp, sizeof(tBTA_HL_SDP));
2120 old_cch_oper = p_mcb->cch_oper;
2121 p_mcb->cch_oper = BTIF_HL_CCH_OP_NONE;
2122
2123 switch (old_cch_oper) {
2124 case BTIF_HL_CCH_OP_MDEP_FILTERING:
2125 status = btif_hl_find_sdp_idx_using_mdep_filter(app_idx, mcl_idx,
2126 &sdp_idx);
2127 break;
2128 default:
2129 break;
2130 }
2131
2132 if (status) {
2133 p_mcb->sdp_idx = sdp_idx;
2134 p_mcb->valid_sdp_idx = true;
2135 p_mcb->ctrl_psm = p_mcb->sdp.sdp_rec[sdp_idx].ctrl_psm;
2136
2137 switch (old_cch_oper) {
2138 case BTIF_HL_CCH_OP_MDEP_FILTERING:
2139 p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
2140 if (p_pcb->in_use) {
2141 if (!p_pcb->abort_pending) {
2142 switch (p_pcb->op) {
2143 case BTIF_HL_PEND_DCH_OP_OPEN:
2144 btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
2145 break;
2146 case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
2147 default:
2148 break;
2149 }
2150 open_param.ctrl_psm = p_mcb->ctrl_psm;
2151 bdcpy(open_param.bd_addr, p_mcb->bd_addr);
2152 open_param.sec_mask =
2153 (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
2154 BTA_HlCchOpen(p_acb->app_id, p_acb->app_handle, &open_param);
2155 } else {
2156 BTIF_TRACE_DEBUG("channel abort pending");
2157 }
2158 }
2159 break;
2160
2161 case BTIF_HL_CCH_OP_DCH_OPEN:
2162 status = btif_hl_proc_pending_op(app_idx, mcl_idx);
2163 break;
2164
2165 default:
2166 BTIF_TRACE_ERROR("Invalid CCH oper %d", old_cch_oper);
2167 break;
2168 }
2169 } else {
2170 BTIF_TRACE_ERROR("Can not find SDP idx discard CCH Open request");
2171 }
2172 }
2173 }
2174 }
2175 return status;
2176 }
2177
2178 /*******************************************************************************
2179 *
2180 * Function btif_hl_proc_cch_open_ind
2181 *
2182 * Description Process the CCH open indication
2183 *
2184 * Returns Nothing
2185 *
2186 ******************************************************************************/
btif_hl_proc_cch_open_ind(tBTA_HL * p_data)2187 static void btif_hl_proc_cch_open_ind(tBTA_HL* p_data)
2188
2189 {
2190 btif_hl_mcl_cb_t* p_mcb;
2191 uint8_t mcl_idx;
2192 int i;
2193
2194 BTIF_TRACE_DEBUG("%s", __func__);
2195 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
2196 if (btif_hl_cb.acb[i].in_use) {
2197 if (!btif_hl_find_mcl_idx(i, p_data->cch_open_ind.bd_addr, &mcl_idx)) {
2198 if (btif_hl_find_avail_mcl_idx(i, &mcl_idx)) {
2199 p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, mcl_idx);
2200 alarm_free(p_mcb->cch_timer);
2201 memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t));
2202 p_mcb->in_use = true;
2203 p_mcb->is_connected = true;
2204 p_mcb->mcl_handle = p_data->cch_open_ind.mcl_handle;
2205 bdcpy(p_mcb->bd_addr, p_data->cch_open_ind.bd_addr);
2206 btif_hl_start_cch_timer(i, mcl_idx);
2207 }
2208 } else {
2209 BTIF_TRACE_ERROR("The MCL already exist for cch_open_ind");
2210 }
2211 }
2212 }
2213 }
2214
2215 /*******************************************************************************
2216 *
2217 * Function btif_hl_proc_pending_op
2218 *
2219 * Description Process the pending dch operation.
2220 *
2221 * Returns Nothing
2222 *
2223 ******************************************************************************/
btif_hl_proc_pending_op(uint8_t app_idx,uint8_t mcl_idx)2224 bool btif_hl_proc_pending_op(uint8_t app_idx, uint8_t mcl_idx)
2225
2226 {
2227 btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
2228 btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
2229 btif_hl_pending_chan_cb_t* p_pcb;
2230 bool status = false;
2231 tBTA_HL_DCH_OPEN_PARAM dch_open;
2232 tBTA_HL_MDL_ID mdl_id;
2233 tBTA_HL_DCH_RECONNECT_PARAM reconnect_param;
2234
2235 p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
2236 if (p_pcb->in_use) {
2237 switch (p_pcb->op) {
2238 case BTIF_HL_PEND_DCH_OP_OPEN:
2239 if (!p_pcb->abort_pending) {
2240 BTIF_TRACE_DEBUG("op BTIF_HL_PEND_DCH_OP_OPEN");
2241 dch_open.ctrl_psm = p_mcb->ctrl_psm;
2242 dch_open.local_mdep_id =
2243 p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_id;
2244 if (btif_hl_find_peer_mdep_id(
2245 p_acb->app_id, p_mcb->bd_addr,
2246 p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx]
2247 .mdep_cfg.mdep_role,
2248 p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx]
2249 .mdep_cfg.data_cfg[0]
2250 .data_type,
2251 &dch_open.peer_mdep_id)) {
2252 dch_open.local_cfg = p_acb->channel_type[p_pcb->mdep_cfg_idx];
2253 if ((p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx]
2254 .mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SOURCE) &&
2255 !btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) {
2256 dch_open.local_cfg = BTA_HL_DCH_CFG_RELIABLE;
2257 }
2258 dch_open.sec_mask = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
2259 BTIF_TRACE_DEBUG("dch_open.local_cfg=%d ", dch_open.local_cfg);
2260 btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
2261
2262 if (!btif_hl_is_reconnect_possible(app_idx, mcl_idx,
2263 p_pcb->mdep_cfg_idx, &dch_open,
2264 &mdl_id)) {
2265 BTIF_TRACE_DEBUG("Issue DCH open, mcl_handle=%d",
2266 p_mcb->mcl_handle);
2267 BTA_HlDchOpen(p_mcb->mcl_handle, &dch_open);
2268 } else {
2269 reconnect_param.ctrl_psm = p_mcb->ctrl_psm;
2270 reconnect_param.mdl_id = mdl_id;
2271 ;
2272 BTIF_TRACE_DEBUG("Issue Reconnect ctrl_psm=0x%x mdl_id=0x%x",
2273 reconnect_param.ctrl_psm,
2274 reconnect_param.mdl_id);
2275 BTA_HlDchReconnect(p_mcb->mcl_handle, &reconnect_param);
2276 }
2277 status = true;
2278 }
2279 } else {
2280 btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
2281 status = true;
2282 }
2283 break;
2284 case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
2285 BTA_HlDeleteMdl(p_mcb->mcl_handle, p_acb->delete_mdl.mdl_id);
2286 status = true;
2287 break;
2288
2289 default:
2290 break;
2291 }
2292 }
2293 return status;
2294 }
2295
2296 /*******************************************************************************
2297 *
2298 * Function btif_hl_proc_cch_open_cfm
2299 *
2300 * Description Process the CCH open confirmation
2301 *
2302 * Returns Nothing
2303 *
2304 ******************************************************************************/
btif_hl_proc_cch_open_cfm(tBTA_HL * p_data)2305 static bool btif_hl_proc_cch_open_cfm(tBTA_HL* p_data)
2306
2307 {
2308 btif_hl_mcl_cb_t* p_mcb;
2309 uint8_t app_idx, mcl_idx;
2310 bool status = false;
2311
2312 BTIF_TRACE_DEBUG("%s", __func__);
2313
2314 if (btif_hl_find_app_idx_using_app_id(p_data->cch_open_cfm.app_id,
2315 &app_idx)) {
2316 BTIF_TRACE_DEBUG("app_idx=%d", app_idx);
2317 if (btif_hl_find_mcl_idx(app_idx, p_data->cch_open_cfm.bd_addr, &mcl_idx)) {
2318 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
2319 BTIF_TRACE_DEBUG("mcl_idx=%d, mcl_handle=%d", mcl_idx,
2320 p_data->cch_open_cfm.mcl_handle);
2321 p_mcb->mcl_handle = p_data->cch_open_cfm.mcl_handle;
2322 p_mcb->is_connected = true;
2323 status = btif_hl_proc_pending_op(app_idx, mcl_idx);
2324 if (status) btif_hl_start_cch_timer(app_idx, mcl_idx);
2325 }
2326 }
2327
2328 return status;
2329 }
2330
2331 /*******************************************************************************
2332 *
2333 * Function btif_hl_clean_mcb_using_handle
2334 *
2335 * Description clean control channel cb using handle
2336 *
2337 * Returns void
2338 *
2339 ******************************************************************************/
btif_hl_clean_mcb_using_handle(tBTA_HL_MCL_HANDLE mcl_handle)2340 static void btif_hl_clean_mcb_using_handle(tBTA_HL_MCL_HANDLE mcl_handle) {
2341 btif_hl_app_cb_t* p_acb;
2342 uint8_t i, j;
2343
2344 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
2345 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
2346 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
2347 if (p_acb->mcb[j].in_use)
2348 BTIF_TRACE_DEBUG(
2349 "btif_hl_find_mcl_idx_using_handle: app_idx=%d,"
2350 "mcl_idx =%d mcl_handle=%d",
2351 i, j, p_acb->mcb[j].mcl_handle);
2352 if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
2353 btif_hl_stop_cch_timer(i, j);
2354 btif_hl_release_mcl_sockets(i, j);
2355 btif_hl_send_setup_disconnected_cb(i, j);
2356 btif_hl_clean_mcl_cb(i, j);
2357 }
2358 }
2359 }
2360 }
2361
2362 /*******************************************************************************
2363 *
2364 * Function btif_hl_proc_cch_close_ind
2365 *
2366 * Description Process the CCH close indication
2367 *
2368 * Returns Nothing
2369 *
2370 ******************************************************************************/
btif_hl_proc_cch_close_ind(tBTA_HL * p_data)2371 static void btif_hl_proc_cch_close_ind(tBTA_HL* p_data)
2372
2373 {
2374 BTIF_TRACE_DEBUG("%s", __func__);
2375
2376 btif_hl_clean_mcb_using_handle(p_data->cch_close_ind.mcl_handle);
2377 }
2378
2379 /*******************************************************************************
2380 *
2381 * Function btif_hl_proc_cch_close_cfm
2382 *
2383 * Description Process the CCH close confirmation
2384 *
2385 * Returns Nothing
2386 *
2387 ******************************************************************************/
btif_hl_proc_cch_close_cfm(tBTA_HL * p_data)2388 static void btif_hl_proc_cch_close_cfm(tBTA_HL* p_data) {
2389 BTIF_TRACE_DEBUG("%s", __func__);
2390
2391 btif_hl_clean_mcb_using_handle(p_data->cch_close_ind.mcl_handle);
2392 }
2393
2394 /*******************************************************************************
2395 *
2396 * Function btif_hl_proc_create_ind
2397 *
2398 * Description Process the MDL create indication
2399 *
2400 * Returns Nothing
2401 *
2402 ******************************************************************************/
btif_hl_proc_create_ind(tBTA_HL * p_data)2403 static void btif_hl_proc_create_ind(tBTA_HL* p_data) {
2404 btif_hl_app_cb_t* p_acb;
2405 btif_hl_mcl_cb_t* p_mcb;
2406 tBTA_HL_MDEP* p_mdep;
2407 uint8_t orig_app_idx, mcl_idx, mdep_cfg_idx;
2408 bool first_reliable_exist;
2409 bool success = true;
2410 tBTA_HL_DCH_CFG rsp_cfg = BTA_HL_DCH_CFG_UNKNOWN;
2411 tBTA_HL_DCH_CREATE_RSP rsp_code = BTA_HL_DCH_CREATE_RSP_CFG_REJ;
2412 tBTA_HL_DCH_CREATE_RSP_PARAM create_rsp_param;
2413
2414 BTIF_TRACE_DEBUG("%s", __func__);
2415
2416 // Find the correct app_idx based on the mdep_id;
2417 btif_hl_find_app_idx_using_mdepId(p_data->dch_create_ind.local_mdep_id,
2418 &orig_app_idx);
2419 if (btif_hl_find_mcl_idx(orig_app_idx, p_data->dch_create_ind.bd_addr,
2420 &mcl_idx)) {
2421 p_acb = BTIF_HL_GET_APP_CB_PTR(orig_app_idx);
2422 p_mcb = BTIF_HL_GET_MCL_CB_PTR(orig_app_idx, mcl_idx);
2423
2424 if (btif_hl_find_mdep_cfg_idx(orig_app_idx,
2425 p_data->dch_create_ind.local_mdep_id,
2426 &mdep_cfg_idx)) {
2427 p_mdep = &(p_acb->sup_feature.mdep[mdep_cfg_idx]);
2428 first_reliable_exist =
2429 btif_hl_is_the_first_reliable_existed(orig_app_idx, mcl_idx);
2430 switch (p_mdep->mdep_cfg.mdep_role) {
2431 case BTA_HL_MDEP_ROLE_SOURCE:
2432 if (p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_NO_PREF) {
2433 if (first_reliable_exist) {
2434 rsp_cfg = p_acb->channel_type[mdep_cfg_idx];
2435 } else {
2436 rsp_cfg = BTA_HL_DCH_CFG_RELIABLE;
2437 }
2438 rsp_code = BTA_HL_DCH_CREATE_RSP_SUCCESS;
2439 }
2440
2441 break;
2442 case BTA_HL_MDEP_ROLE_SINK:
2443
2444 BTIF_TRACE_DEBUG("btif_hl_proc_create_ind:BTA_HL_MDEP_ROLE_SINK");
2445 if ((p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_RELIABLE) ||
2446 (first_reliable_exist &&
2447 (p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_STREAMING))) {
2448 rsp_code = BTA_HL_DCH_CREATE_RSP_SUCCESS;
2449 rsp_cfg = p_data->dch_create_ind.cfg;
2450 BTIF_TRACE_DEBUG(
2451 "btif_hl_proc_create_ind:BTA_HL_MDEP_ROLE_SINK cfg = %d",
2452 rsp_cfg);
2453 }
2454 break;
2455 default:
2456 break;
2457 }
2458 }
2459 } else {
2460 success = false;
2461 }
2462
2463 if (success) {
2464 BTIF_TRACE_DEBUG("create response rsp_code=%d rsp_cfg=%d", rsp_code,
2465 rsp_cfg);
2466 create_rsp_param.local_mdep_id = p_data->dch_create_ind.local_mdep_id;
2467 create_rsp_param.mdl_id = p_data->dch_create_ind.mdl_id;
2468 create_rsp_param.rsp_code = rsp_code;
2469 create_rsp_param.cfg_rsp = rsp_cfg;
2470 BTA_HlDchCreateRsp(p_mcb->mcl_handle, &create_rsp_param);
2471 }
2472 }
2473
2474 /*******************************************************************************
2475 *
2476 * Function btif_hl_proc_dch_open_ind
2477 *
2478 * Description Process the DCH open indication
2479 *
2480 * Returns Nothing
2481 *
2482 ******************************************************************************/
btif_hl_proc_dch_open_ind(tBTA_HL * p_data)2483 static void btif_hl_proc_dch_open_ind(tBTA_HL* p_data)
2484
2485 {
2486 btif_hl_mdl_cb_t* p_dcb;
2487 uint8_t orig_app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
2488 bool close_dch = false;
2489
2490 BTIF_TRACE_DEBUG("%s", __func__);
2491
2492 // Find the correct app_idx based on the mdep_id;
2493 btif_hl_find_app_idx_using_mdepId(p_data->dch_open_ind.local_mdep_id,
2494 &orig_app_idx);
2495
2496 if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_open_ind.mcl_handle,
2497 orig_app_idx, &mcl_idx)) {
2498 if (btif_hl_find_avail_mdl_idx(orig_app_idx, mcl_idx, &mdl_idx)) {
2499 p_dcb = BTIF_HL_GET_MDL_CB_PTR(orig_app_idx, mcl_idx, mdl_idx);
2500
2501 if (btif_hl_find_mdep_cfg_idx(orig_app_idx,
2502 p_data->dch_open_ind.local_mdep_id,
2503 &mdep_cfg_idx)) {
2504 p_dcb->in_use = true;
2505 p_dcb->mdl_handle = p_data->dch_open_ind.mdl_handle;
2506 p_dcb->local_mdep_cfg_idx = mdep_cfg_idx;
2507 p_dcb->local_mdep_id = p_data->dch_open_ind.local_mdep_id;
2508 p_dcb->mdl_id = p_data->dch_open_ind.mdl_id;
2509 p_dcb->dch_mode = p_data->dch_open_ind.dch_mode;
2510 p_dcb->dch_mode = p_data->dch_open_ind.dch_mode;
2511 p_dcb->is_the_first_reliable = p_data->dch_open_ind.first_reliable;
2512 p_dcb->mtu = p_data->dch_open_ind.mtu;
2513
2514 if (btif_hl_find_channel_id_using_mdl_id(orig_app_idx, p_dcb->mdl_id,
2515 &p_dcb->channel_id)) {
2516 BTIF_TRACE_DEBUG(" app_idx=%d mcl_idx=%d mdl_idx=%d channel_id=%d",
2517 orig_app_idx, mcl_idx, mdl_idx, p_dcb->channel_id);
2518 if (!btif_hl_create_socket(orig_app_idx, mcl_idx, mdl_idx)) {
2519 BTIF_TRACE_ERROR("Unable to create socket");
2520 close_dch = true;
2521 }
2522 } else {
2523 BTIF_TRACE_ERROR("Unable find channel id for mdl_id=0x%x",
2524 p_dcb->mdl_id);
2525 close_dch = true;
2526 }
2527 } else {
2528 BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",
2529 p_data->dch_open_cfm.local_mdep_id);
2530 close_dch = true;
2531 }
2532
2533 if (close_dch) btif_hl_clean_mdl_cb(p_dcb);
2534 } else
2535 close_dch = true;
2536 } else
2537 close_dch = true;
2538
2539 if (close_dch) BTA_HlDchClose(p_data->dch_open_cfm.mdl_handle);
2540 }
2541
2542 /*******************************************************************************
2543 *
2544 * Function btif_hl_proc_dch_open_cfm
2545 *
2546 * Description Process the DCH close confirmation
2547 *
2548 * Returns Nothing
2549 *
2550 ******************************************************************************/
btif_hl_proc_dch_open_cfm(tBTA_HL * p_data)2551 static bool btif_hl_proc_dch_open_cfm(tBTA_HL* p_data)
2552
2553 {
2554 btif_hl_mdl_cb_t* p_dcb;
2555 btif_hl_pending_chan_cb_t* p_pcb;
2556 uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
2557 bool status = false;
2558 bool close_dch = false;
2559
2560 BTIF_TRACE_DEBUG("%s", __func__);
2561
2562 // Find the correct app_idx based on the mdep_id;
2563 btif_hl_find_app_idx_using_mdepId(p_data->dch_open_cfm.local_mdep_id,
2564 &app_idx);
2565
2566 if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_open_cfm.mcl_handle,
2567 app_idx, &mcl_idx)) {
2568 p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
2569
2570 if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) {
2571 p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
2572
2573 if (btif_hl_find_mdep_cfg_idx(app_idx, p_data->dch_open_cfm.local_mdep_id,
2574 &mdep_cfg_idx)) {
2575 p_dcb->in_use = true;
2576 p_dcb->mdl_handle = p_data->dch_open_cfm.mdl_handle;
2577 p_dcb->local_mdep_cfg_idx = mdep_cfg_idx;
2578 p_dcb->local_mdep_id = p_data->dch_open_cfm.local_mdep_id;
2579 p_dcb->mdl_id = p_data->dch_open_cfm.mdl_id;
2580 p_dcb->dch_mode = p_data->dch_open_cfm.dch_mode;
2581 p_dcb->is_the_first_reliable = p_data->dch_open_cfm.first_reliable;
2582 p_dcb->mtu = p_data->dch_open_cfm.mtu;
2583 p_dcb->channel_id = p_pcb->channel_id;
2584
2585 BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx,
2586 mdl_idx);
2587 btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
2588 if (btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) {
2589 status = true;
2590 BTIF_TRACE_DEBUG(
2591 "app_idx=%d mcl_idx=%d mdl_idx=%d p_dcb->channel_id=0x%08x",
2592 app_idx, mcl_idx, mdl_idx, p_dcb->channel_id);
2593 btif_hl_clean_pcb(p_pcb);
2594 } else {
2595 BTIF_TRACE_ERROR("Unable to create socket");
2596 close_dch = true;
2597 }
2598 } else {
2599 BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",
2600 p_data->dch_open_cfm.local_mdep_id);
2601 close_dch = true;
2602 }
2603
2604 if (close_dch) {
2605 btif_hl_clean_mdl_cb(p_dcb);
2606 BTA_HlDchClose(p_data->dch_open_cfm.mdl_handle);
2607 }
2608 }
2609 }
2610
2611 return status;
2612 }
2613 /*******************************************************************************
2614 *
2615 * Function btif_hl_proc_dch_reconnect_cfm
2616 *
2617 * Description Process the DCH reconnect indication
2618 *
2619 * Returns Nothing
2620 *
2621 ******************************************************************************/
btif_hl_proc_dch_reconnect_cfm(tBTA_HL * p_data)2622 static bool btif_hl_proc_dch_reconnect_cfm(tBTA_HL* p_data) {
2623 btif_hl_mdl_cb_t* p_dcb;
2624 btif_hl_pending_chan_cb_t* p_pcb;
2625 uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
2626 bool status = false;
2627 bool close_dch = false;
2628
2629 BTIF_TRACE_DEBUG("%s", __func__);
2630
2631 btif_hl_find_app_idx_using_mdepId(p_data->dch_reconnect_cfm.local_mdep_id,
2632 &app_idx);
2633
2634 if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_reconnect_cfm.mcl_handle,
2635 app_idx, &mcl_idx)) {
2636 p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
2637
2638 if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) {
2639 p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
2640
2641 if (btif_hl_find_mdep_cfg_idx(app_idx,
2642 p_data->dch_reconnect_cfm.local_mdep_id,
2643 &mdep_cfg_idx)) {
2644 p_dcb->in_use = true;
2645 p_dcb->mdl_handle = p_data->dch_reconnect_cfm.mdl_handle;
2646 p_dcb->local_mdep_cfg_idx = mdep_cfg_idx;
2647 p_dcb->local_mdep_id = p_data->dch_reconnect_cfm.local_mdep_id;
2648 p_dcb->mdl_id = p_data->dch_reconnect_cfm.mdl_id;
2649 p_dcb->dch_mode = p_data->dch_reconnect_cfm.dch_mode;
2650 p_dcb->is_the_first_reliable = p_data->dch_reconnect_cfm.first_reliable;
2651 p_dcb->mtu = p_data->dch_reconnect_cfm.mtu;
2652 p_dcb->channel_id = p_pcb->channel_id;
2653
2654 BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx,
2655 mdl_idx);
2656 btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
2657 if (btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) {
2658 status = true;
2659 BTIF_TRACE_DEBUG(
2660 "app_idx=%d mcl_idx=%d mdl_idx=%d p_dcb->channel_id=0x%08x",
2661 app_idx, mcl_idx, mdl_idx, p_dcb->channel_id);
2662 btif_hl_clean_pcb(p_pcb);
2663 } else {
2664 BTIF_TRACE_ERROR("Unable to create socket");
2665 close_dch = true;
2666 }
2667 } else {
2668 BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",
2669 p_data->dch_open_cfm.local_mdep_id);
2670 close_dch = true;
2671 }
2672
2673 if (close_dch) {
2674 btif_hl_clean_mdl_cb(p_dcb);
2675 BTA_HlDchClose(p_data->dch_reconnect_cfm.mdl_handle);
2676 }
2677 }
2678 }
2679
2680 return status;
2681 }
2682 /*******************************************************************************
2683 *
2684 * Function btif_hl_proc_dch_reconnect_ind
2685 *
2686 * Description Process the DCH reconnect indication
2687 *
2688 * Returns Nothing
2689 *
2690 ******************************************************************************/
btif_hl_proc_dch_reconnect_ind(tBTA_HL * p_data)2691 static void btif_hl_proc_dch_reconnect_ind(tBTA_HL* p_data)
2692
2693 {
2694 btif_hl_app_cb_t* p_acb;
2695 btif_hl_mdl_cb_t* p_dcb;
2696 uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
2697 bool close_dch = false;
2698
2699 BTIF_TRACE_DEBUG("%s", __func__);
2700
2701 // Find the correct app_idx based on the mdep_id;
2702 btif_hl_find_app_idx_using_mdepId(p_data->dch_reconnect_ind.local_mdep_id,
2703 &app_idx);
2704
2705 if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_reconnect_ind.mcl_handle,
2706 app_idx, &mcl_idx)) {
2707 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
2708 BTIF_TRACE_DEBUG(
2709 "btif_hl_proc_dch_reconnect_ind: app_idx = %d, mcl_idx = %d", app_idx,
2710 mcl_idx);
2711
2712 if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) {
2713 p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
2714
2715 if (btif_hl_find_mdep_cfg_idx(app_idx,
2716 p_data->dch_reconnect_ind.local_mdep_id,
2717 &mdep_cfg_idx)) {
2718 p_dcb->in_use = true;
2719 p_dcb->mdl_handle = p_data->dch_reconnect_ind.mdl_handle;
2720 p_dcb->local_mdep_cfg_idx = mdep_cfg_idx;
2721 p_dcb->local_mdep_id = p_data->dch_reconnect_ind.local_mdep_id;
2722 p_dcb->mdl_id = p_data->dch_reconnect_ind.mdl_id;
2723 p_dcb->dch_mode = p_data->dch_reconnect_ind.dch_mode;
2724 p_dcb->dch_mode = p_data->dch_reconnect_ind.dch_mode;
2725 p_dcb->is_the_first_reliable = p_data->dch_reconnect_ind.first_reliable;
2726 p_dcb->mtu = p_data->dch_reconnect_ind.mtu;
2727 p_dcb->channel_id = btif_hl_get_next_channel_id(p_acb->app_id);
2728
2729 BTIF_TRACE_DEBUG(" app_idx=%d mcl_idx=%d mdl_idx=%d channel_id=%d",
2730 app_idx, mcl_idx, mdl_idx, p_dcb->channel_id);
2731 if (!btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) {
2732 BTIF_TRACE_ERROR("Unable to create socket");
2733 close_dch = true;
2734 }
2735 } else {
2736 BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",
2737 p_data->dch_open_cfm.local_mdep_id);
2738 close_dch = true;
2739 }
2740
2741 if (close_dch) btif_hl_clean_mdl_cb(p_dcb);
2742 } else
2743 close_dch = true;
2744 } else
2745 close_dch = true;
2746
2747 if (close_dch) BTA_HlDchClose(p_data->dch_reconnect_ind.mdl_handle);
2748 }
2749
2750 /*******************************************************************************
2751 *
2752 * Function btif_hl_proc_dch_close_ind
2753 *
2754 * Description Process the DCH close indication
2755 *
2756 * Returns Nothing
2757 *
2758 ******************************************************************************/
btif_hl_proc_dch_close_ind(tBTA_HL * p_data)2759 static void btif_hl_proc_dch_close_ind(tBTA_HL* p_data)
2760
2761 {
2762 btif_hl_mdl_cb_t* p_dcb;
2763 btif_hl_mcl_cb_t* p_mcb;
2764 uint8_t app_idx, mcl_idx, mdl_idx;
2765
2766 BTIF_TRACE_DEBUG("%s", __func__);
2767 if (btif_hl_find_mdl_idx_using_handle(p_data->dch_close_ind.mdl_handle,
2768 &app_idx, &mcl_idx, &mdl_idx)) {
2769 p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
2770 btif_hl_release_socket(app_idx, mcl_idx, mdl_idx);
2771 btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
2772 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
2773 btif_hl_clean_mdl_cb(p_dcb);
2774 if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle))
2775 btif_hl_start_cch_timer(app_idx, mcl_idx);
2776 BTIF_TRACE_DEBUG("remote DCH close success mdl_idx=%d", mdl_idx);
2777 }
2778 }
2779
2780 /*******************************************************************************
2781 *
2782 * Function btif_hl_proc_dch_close_cfm
2783 *
2784 * Description Process the DCH reconnect confirmation
2785 *
2786 * Returns Nothing
2787 *
2788 ******************************************************************************/
btif_hl_proc_dch_close_cfm(tBTA_HL * p_data)2789 static void btif_hl_proc_dch_close_cfm(tBTA_HL* p_data)
2790
2791 {
2792 btif_hl_mdl_cb_t* p_dcb;
2793 btif_hl_mcl_cb_t* p_mcb;
2794 uint8_t app_idx, mcl_idx, mdl_idx;
2795
2796 BTIF_TRACE_DEBUG("%s", __func__);
2797 if (btif_hl_find_mdl_idx_using_handle(p_data->dch_close_cfm.mdl_handle,
2798 &app_idx, &mcl_idx, &mdl_idx)) {
2799 p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
2800 btif_hl_release_socket(app_idx, mcl_idx, mdl_idx);
2801 btif_hl_clean_mdl_cb(p_dcb);
2802 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
2803 if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle))
2804 btif_hl_start_cch_timer(app_idx, mcl_idx);
2805 BTIF_TRACE_DEBUG(" local DCH close success mdl_idx=%d", mdl_idx);
2806 }
2807 }
2808
2809 /*******************************************************************************
2810 *
2811 * Function btif_hl_proc_abort_ind
2812 *
2813 * Description Process the abort indicaiton
2814 *
2815 * Returns Nothing
2816 *
2817 ******************************************************************************/
btif_hl_proc_abort_ind(tBTA_HL_MCL_HANDLE mcl_handle)2818 static void btif_hl_proc_abort_ind(tBTA_HL_MCL_HANDLE mcl_handle) {
2819 BTIF_TRACE_DEBUG("%s", __func__);
2820 btif_hl_app_cb_t* p_acb;
2821 uint8_t i, j;
2822
2823 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
2824 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
2825 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
2826 if (p_acb->mcb[j].in_use)
2827 BTIF_TRACE_DEBUG(
2828 "btif_hl_find_mcl_idx_using_handle: app_idx=%d,mcl_idx =%d "
2829 "mcl_handle=%d",
2830 i, j, p_acb->mcb[j].mcl_handle);
2831 if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
2832 btif_hl_stop_cch_timer(i, j);
2833 btif_hl_send_setup_disconnected_cb(i, j);
2834 btif_hl_clean_mcl_cb(i, j);
2835 }
2836 }
2837 }
2838 }
2839
2840 /*******************************************************************************
2841 *
2842 * Function btif_hl_proc_abort_cfm
2843 *
2844 * Description Process the abort confirmation
2845 *
2846 * Returns Nothing
2847 *
2848 ******************************************************************************/
btif_hl_proc_abort_cfm(tBTA_HL_MCL_HANDLE mcl_handle)2849 static void btif_hl_proc_abort_cfm(tBTA_HL_MCL_HANDLE mcl_handle) {
2850 BTIF_TRACE_DEBUG("%s", __func__);
2851 btif_hl_app_cb_t* p_acb;
2852 uint8_t i, j;
2853
2854 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
2855 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
2856 for (j = 0; j < BTA_HL_NUM_MCLS; j++) {
2857 if (p_acb->mcb[j].in_use)
2858 BTIF_TRACE_DEBUG(
2859 "btif_hl_find_mcl_idx_using_handle: app_idx=%d,mcl_idx =%d "
2860 "mcl_handle=%d",
2861 i, j, p_acb->mcb[j].mcl_handle);
2862 if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) {
2863 btif_hl_stop_cch_timer(i, j);
2864 btif_hl_send_setup_disconnected_cb(i, j);
2865 btif_hl_clean_mcl_cb(i, j);
2866 }
2867 }
2868 }
2869 }
2870
2871 /*******************************************************************************
2872 *
2873 * Function btif_hl_proc_send_data_cfm
2874 *
2875 * Description Process the send data confirmation
2876 *
2877 * Returns Nothing
2878 *
2879 ******************************************************************************/
btif_hl_proc_send_data_cfm(tBTA_HL_MDL_HANDLE mdl_handle,UNUSED_ATTR tBTA_HL_STATUS status)2880 static void btif_hl_proc_send_data_cfm(tBTA_HL_MDL_HANDLE mdl_handle,
2881 UNUSED_ATTR tBTA_HL_STATUS status) {
2882 uint8_t app_idx, mcl_idx, mdl_idx;
2883 btif_hl_mdl_cb_t* p_dcb;
2884
2885 BTIF_TRACE_DEBUG("%s", __func__);
2886 if (btif_hl_find_mdl_idx_using_handle(mdl_handle, &app_idx, &mcl_idx,
2887 &mdl_idx)) {
2888 p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
2889 osi_free_and_reset((void**)&p_dcb->p_tx_pkt);
2890 BTIF_TRACE_DEBUG("send success free p_tx_pkt tx_size=%d", p_dcb->tx_size);
2891 p_dcb->tx_size = 0;
2892 }
2893 }
2894
2895 /*******************************************************************************
2896 *
2897 * Function btif_hl_proc_dch_cong_ind
2898 *
2899 * Description Process the DCH congestion change indication
2900 *
2901 * Returns Nothing
2902 *
2903 ******************************************************************************/
btif_hl_proc_dch_cong_ind(tBTA_HL * p_data)2904 static void btif_hl_proc_dch_cong_ind(tBTA_HL* p_data)
2905
2906 {
2907 btif_hl_mdl_cb_t* p_dcb;
2908 uint8_t app_idx, mcl_idx, mdl_idx;
2909
2910 BTIF_TRACE_DEBUG("btif_hl_proc_dch_cong_ind");
2911
2912 if (btif_hl_find_mdl_idx_using_handle(p_data->dch_cong_ind.mdl_handle,
2913 &app_idx, &mcl_idx, &mdl_idx)) {
2914 p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
2915 p_dcb->cong = p_data->dch_cong_ind.cong;
2916 }
2917 }
2918
2919 /*******************************************************************************
2920 *
2921 * Function btif_hl_proc_reg_request
2922 *
2923 * Description Process registration request
2924 *
2925 * Returns void
2926 *
2927 ******************************************************************************/
btif_hl_proc_reg_request(uint8_t app_idx,uint8_t app_id,tBTA_HL_REG_PARAM * p_reg_param,UNUSED_ATTR tBTA_HL_CBACK * p_cback)2928 static void btif_hl_proc_reg_request(uint8_t app_idx, uint8_t app_id,
2929 tBTA_HL_REG_PARAM* p_reg_param,
2930 UNUSED_ATTR tBTA_HL_CBACK* p_cback) {
2931 BTIF_TRACE_DEBUG("%s app_idx=%d app_id=%d", __func__, app_idx, app_id);
2932
2933 if (reg_counter > 1) {
2934 BTIF_TRACE_DEBUG("btif_hl_proc_reg_request: calling uPDATE");
2935 BTA_HlUpdate(app_id, p_reg_param, true, btif_hl_cback);
2936 } else
2937 BTA_HlRegister(app_id, p_reg_param, btif_hl_cback);
2938 }
2939
2940 /*******************************************************************************
2941 *
2942 * Function btif_hl_proc_cb_evt
2943 *
2944 * Description Process HL callback events
2945 *
2946 * Returns void
2947 *
2948 ******************************************************************************/
btif_hl_proc_cb_evt(uint16_t event,char * p_param)2949 static void btif_hl_proc_cb_evt(uint16_t event, char* p_param) {
2950 btif_hl_evt_cb_t* p_data = (btif_hl_evt_cb_t*)p_param;
2951 bt_bdaddr_t bd_addr;
2952 bthl_channel_state_t state = BTHL_CONN_STATE_DISCONNECTED;
2953 bool send_chan_cb = true;
2954 tBTA_HL_REG_PARAM reg_param;
2955 btif_hl_app_cb_t* p_acb;
2956
2957 BTIF_TRACE_DEBUG("%s event %d", __func__, event);
2958 btif_hl_display_calling_process_name();
2959
2960 switch (event) {
2961 case BTIF_HL_SEND_CONNECTED_CB:
2962 case BTIF_HL_SEND_DISCONNECTED_CB:
2963 if (p_data->chan_cb.cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING)
2964 state = BTHL_CONN_STATE_CONNECTED;
2965 else if (p_data->chan_cb.cb_state ==
2966 BTIF_HL_CHAN_CB_STATE_DISCONNECTED_PENDING)
2967 state = BTHL_CONN_STATE_DISCONNECTED;
2968 else
2969 send_chan_cb = false;
2970
2971 if (send_chan_cb) {
2972 btif_hl_copy_bda(&bd_addr, p_data->chan_cb.bd_addr);
2973 BTIF_TRACE_DEBUG(
2974 "state callbk: ch_id=0x%08x cb_state=%d state=%d fd=%d",
2975 p_data->chan_cb.channel_id, p_data->chan_cb.cb_state, state,
2976 p_data->chan_cb.fd);
2977 btif_hl_display_bt_bda(&bd_addr);
2978 BTIF_HL_CALL_CBACK(
2979 bt_hl_callbacks, channel_state_cb, p_data->chan_cb.app_id, &bd_addr,
2980 p_data->chan_cb.mdep_cfg_index, p_data->chan_cb.channel_id, state,
2981 p_data->chan_cb.fd);
2982 }
2983
2984 break;
2985
2986 case BTIF_HL_REG_APP:
2987 reg_counter++;
2988 p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->reg.app_idx);
2989 BTIF_TRACE_DEBUG("Rcv BTIF_HL_REG_APP app_idx=%d reg_pending=%d",
2990 p_data->reg.app_idx, p_acb->reg_pending);
2991 if (btif_hl_get_state() == BTIF_HL_STATE_ENABLED && p_acb->reg_pending) {
2992 BTIF_TRACE_DEBUG("Rcv BTIF_HL_REG_APP reg_counter=%d", reg_counter);
2993 p_acb->reg_pending = false;
2994 reg_param.dev_type = p_acb->dev_type;
2995 reg_param.sec_mask = BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT;
2996 reg_param.p_srv_name = p_acb->srv_name;
2997 reg_param.p_srv_desp = p_acb->srv_desp;
2998 reg_param.p_provider_name = p_acb->provider_name;
2999 btif_hl_proc_reg_request(p_data->reg.app_idx, p_acb->app_id, ®_param,
3000 btif_hl_cback);
3001 } else {
3002 BTIF_TRACE_DEBUG("reg request is processed state=%d reg_pending=%d",
3003 btif_hl_get_state(), p_acb->reg_pending);
3004 }
3005 break;
3006
3007 case BTIF_HL_UNREG_APP:
3008 reg_counter--;
3009 BTIF_TRACE_DEBUG("Rcv BTIF_HL_UNREG_APP app_idx=%d",
3010 p_data->unreg.app_idx);
3011 p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->unreg.app_idx);
3012 if (btif_hl_get_state() == BTIF_HL_STATE_ENABLED) {
3013 if (reg_counter >= 1)
3014 BTA_HlUpdate(p_acb->app_id, NULL, false, NULL);
3015 else
3016 BTA_HlDeregister(p_acb->app_id, p_acb->app_handle);
3017 }
3018 break;
3019
3020 case BTIF_HL_UPDATE_MDL:
3021 BTIF_TRACE_DEBUG("Rcv BTIF_HL_UPDATE_MDL app_idx=%d",
3022 p_data->update_mdl.app_idx);
3023 p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->update_mdl.app_idx);
3024 break;
3025
3026 default:
3027 BTIF_TRACE_ERROR("Unknown event %d", event);
3028 break;
3029 }
3030 }
3031
3032 /*******************************************************************************
3033 *
3034 * Function btif_hl_upstreams_evt
3035 *
3036 * Description Process HL events
3037 *
3038 * Returns void
3039 *
3040 ******************************************************************************/
btif_hl_upstreams_evt(uint16_t event,char * p_param)3041 static void btif_hl_upstreams_evt(uint16_t event, char* p_param) {
3042 tBTA_HL* p_data = (tBTA_HL*)p_param;
3043 uint8_t app_idx, mcl_idx;
3044 btif_hl_app_cb_t* p_acb;
3045 btif_hl_mcl_cb_t* p_mcb = NULL;
3046 btif_hl_pend_dch_op_t pending_op;
3047 bool status;
3048
3049 BTIF_TRACE_DEBUG("%s event %d", __func__, event);
3050 btif_hl_display_calling_process_name();
3051 switch (event) {
3052 case BTA_HL_REGISTER_CFM_EVT:
3053 BTIF_TRACE_DEBUG("Rcv BTA_HL_REGISTER_CFM_EVT");
3054 BTIF_TRACE_DEBUG("app_id=%d app_handle=%d status=%d ",
3055 p_data->reg_cfm.app_id, p_data->reg_cfm.app_handle,
3056 p_data->reg_cfm.status);
3057
3058 btif_hl_proc_reg_cfm(p_data);
3059 break;
3060 case BTA_HL_SDP_INFO_IND_EVT:
3061 BTIF_TRACE_DEBUG("Rcv BTA_HL_SDP_INFO_IND_EVT");
3062 BTIF_TRACE_DEBUG(
3063 "app_handle=%d ctrl_psm=0x%04x data_psm=0x%04x x_spec=%d "
3064 "mcap_sup_procs=0x%02x",
3065 p_data->sdp_info_ind.app_handle, p_data->sdp_info_ind.ctrl_psm,
3066 p_data->sdp_info_ind.data_psm, p_data->sdp_info_ind.data_x_spec,
3067 p_data->sdp_info_ind.mcap_sup_procs);
3068 // btif_hl_proc_sdp_info_ind(p_data);
3069 break;
3070
3071 case BTA_HL_DEREGISTER_CFM_EVT:
3072 BTIF_TRACE_DEBUG("Rcv BTA_HL_DEREGISTER_CFM_EVT");
3073 BTIF_TRACE_DEBUG("app_handle=%d status=%d ", p_data->dereg_cfm.app_handle,
3074 p_data->dereg_cfm.status);
3075 btif_hl_proc_dereg_cfm(p_data);
3076 break;
3077
3078 case BTA_HL_SDP_QUERY_CFM_EVT:
3079 BTIF_TRACE_DEBUG("Rcv BTA_HL_SDP_QUERY_CFM_EVT");
3080 BTIF_TRACE_DEBUG("app_handle=%d app_id =%d,status =%d",
3081 p_data->sdp_query_cfm.app_handle,
3082 p_data->sdp_query_cfm.app_id,
3083 p_data->sdp_query_cfm.status);
3084
3085 BTIF_TRACE_DEBUG(
3086 "DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]",
3087 p_data->sdp_query_cfm.bd_addr[0], p_data->sdp_query_cfm.bd_addr[1],
3088 p_data->sdp_query_cfm.bd_addr[2], p_data->sdp_query_cfm.bd_addr[3],
3089 p_data->sdp_query_cfm.bd_addr[4], p_data->sdp_query_cfm.bd_addr[5]);
3090
3091 if (p_data->sdp_query_cfm.status == BTA_HL_STATUS_OK)
3092 status = btif_hl_proc_sdp_query_cfm(p_data);
3093 else
3094 status = false;
3095
3096 if (!status) {
3097 BTIF_TRACE_DEBUG("BTA_HL_SDP_QUERY_CFM_EVT Status = %d",
3098 p_data->sdp_query_cfm.status);
3099 if (btif_hl_find_app_idx_using_app_id(p_data->sdp_query_cfm.app_id,
3100 &app_idx)) {
3101 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
3102 if (btif_hl_find_mcl_idx(app_idx, p_data->sdp_query_cfm.bd_addr,
3103 &mcl_idx)) {
3104 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
3105 if ((p_mcb->cch_oper == BTIF_HL_CCH_OP_MDEP_FILTERING) ||
3106 (p_mcb->cch_oper == BTIF_HL_CCH_OP_DCH_OPEN)) {
3107 pending_op = p_mcb->pcb.op;
3108 switch (pending_op) {
3109 case BTIF_HL_PEND_DCH_OP_OPEN:
3110 btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
3111 break;
3112 case BTIF_HL_PEND_DCH_OP_RECONNECT:
3113 case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
3114 default:
3115 break;
3116 }
3117 if (!p_mcb->is_connected) btif_hl_clean_mcl_cb(app_idx, mcl_idx);
3118 }
3119 }
3120 }
3121 }
3122
3123 break;
3124
3125 case BTA_HL_CCH_OPEN_CFM_EVT:
3126 BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_OPEN_CFM_EVT");
3127 BTIF_TRACE_DEBUG(
3128 "app_id=%d,app_handle=%d mcl_handle=%d status =%d",
3129 p_data->cch_open_cfm.app_id, p_data->cch_open_cfm.app_handle,
3130 p_data->cch_open_cfm.mcl_handle, p_data->cch_open_cfm.status);
3131 BTIF_TRACE_DEBUG(
3132 "DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]",
3133 p_data->cch_open_cfm.bd_addr[0], p_data->cch_open_cfm.bd_addr[1],
3134 p_data->cch_open_cfm.bd_addr[2], p_data->cch_open_cfm.bd_addr[3],
3135 p_data->cch_open_cfm.bd_addr[4], p_data->cch_open_cfm.bd_addr[5]);
3136
3137 if (p_data->cch_open_cfm.status == BTA_HL_STATUS_OK ||
3138 p_data->cch_open_cfm.status == BTA_HL_STATUS_DUPLICATE_CCH_OPEN) {
3139 status = btif_hl_proc_cch_open_cfm(p_data);
3140 } else {
3141 status = false;
3142 }
3143
3144 if (!status) {
3145 if (btif_hl_find_app_idx_using_app_id(p_data->cch_open_cfm.app_id,
3146 &app_idx)) {
3147 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
3148 if (btif_hl_find_mcl_idx(app_idx, p_data->cch_open_cfm.bd_addr,
3149 &mcl_idx)) {
3150 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
3151 pending_op = p_mcb->pcb.op;
3152 switch (pending_op) {
3153 case BTIF_HL_PEND_DCH_OP_OPEN:
3154 btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
3155 break;
3156 case BTIF_HL_PEND_DCH_OP_RECONNECT:
3157 case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
3158 default:
3159 break;
3160 }
3161 btif_hl_clean_mcl_cb(app_idx, mcl_idx);
3162 }
3163 }
3164 }
3165 break;
3166
3167 case BTA_HL_DCH_OPEN_CFM_EVT:
3168 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_OPEN_CFM_EVT");
3169 BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=0x%x status=%d ",
3170 p_data->dch_open_cfm.mcl_handle,
3171 p_data->dch_open_cfm.mdl_handle,
3172 p_data->dch_open_cfm.status);
3173 BTIF_TRACE_DEBUG(
3174 "first_reliable =%d dch_mode=%d local_mdep_id=%d mdl_id=%d mtu=%d",
3175 p_data->dch_open_cfm.first_reliable, p_data->dch_open_cfm.dch_mode,
3176 p_data->dch_open_cfm.local_mdep_id, p_data->dch_open_cfm.mdl_id,
3177 p_data->dch_open_cfm.mtu);
3178 if (p_data->dch_open_cfm.status == BTA_HL_STATUS_OK) {
3179 status = btif_hl_proc_dch_open_cfm(p_data);
3180 } else {
3181 status = false;
3182 }
3183
3184 if (!status) {
3185 if (btif_hl_find_mcl_idx_using_handle(p_data->dch_open_cfm.mcl_handle,
3186 &app_idx, &mcl_idx)) {
3187 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
3188 pending_op = p_mcb->pcb.op;
3189 switch (pending_op) {
3190 case BTIF_HL_PEND_DCH_OP_OPEN:
3191 btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
3192 break;
3193 case BTIF_HL_PEND_DCH_OP_RECONNECT:
3194 case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
3195 default:
3196 break;
3197 }
3198 }
3199 }
3200 break;
3201
3202 case BTA_HL_CCH_OPEN_IND_EVT:
3203 BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_OPEN_IND_EVT");
3204 BTIF_TRACE_DEBUG("app_handle=%d mcl_handle=%d",
3205 p_data->cch_open_ind.app_handle,
3206 p_data->cch_open_ind.mcl_handle);
3207 BTIF_TRACE_DEBUG(
3208 "DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]",
3209 p_data->cch_open_ind.bd_addr[0], p_data->cch_open_ind.bd_addr[1],
3210 p_data->cch_open_ind.bd_addr[2], p_data->cch_open_ind.bd_addr[3],
3211 p_data->cch_open_ind.bd_addr[4], p_data->cch_open_ind.bd_addr[5]);
3212
3213 btif_hl_proc_cch_open_ind(p_data);
3214 break;
3215
3216 case BTA_HL_DCH_CREATE_IND_EVT:
3217 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_CREATE_IND_EVT");
3218 BTIF_TRACE_DEBUG("mcl_handle=%d", p_data->dch_create_ind.mcl_handle);
3219 BTIF_TRACE_DEBUG("local_mdep_id =%d mdl_id=%d cfg=%d",
3220 p_data->dch_create_ind.local_mdep_id,
3221 p_data->dch_create_ind.mdl_id,
3222 p_data->dch_create_ind.cfg);
3223 btif_hl_proc_create_ind(p_data);
3224 break;
3225
3226 case BTA_HL_DCH_OPEN_IND_EVT:
3227 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_OPEN_IND_EVT");
3228 BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=0x%x",
3229 p_data->dch_open_ind.mcl_handle,
3230 p_data->dch_open_ind.mdl_handle);
3231 BTIF_TRACE_DEBUG(
3232 "first_reliable =%d dch_mode=%d local_mdep_id=%d mdl_id=%d mtu=%d",
3233 p_data->dch_open_ind.first_reliable, p_data->dch_open_ind.dch_mode,
3234 p_data->dch_open_ind.local_mdep_id, p_data->dch_open_ind.mdl_id,
3235 p_data->dch_open_ind.mtu);
3236
3237 btif_hl_proc_dch_open_ind(p_data);
3238 break;
3239
3240 case BTA_HL_DELETE_MDL_IND_EVT:
3241 BTIF_TRACE_DEBUG("Rcv BTA_HL_DELETE_MDL_IND_EVT");
3242 BTIF_TRACE_DEBUG("mcl_handle=%d mdl_id=0x%x",
3243 p_data->delete_mdl_ind.mcl_handle,
3244 p_data->delete_mdl_ind.mdl_id);
3245 break;
3246
3247 case BTA_HL_DELETE_MDL_CFM_EVT:
3248 BTIF_TRACE_DEBUG("Rcv BTA_HL_DELETE_MDL_CFM_EVT");
3249 BTIF_TRACE_DEBUG("mcl_handle=%d mdl_id=0x%x status=%d",
3250 p_data->delete_mdl_cfm.mcl_handle,
3251 p_data->delete_mdl_cfm.mdl_id,
3252 p_data->delete_mdl_cfm.status);
3253
3254 if (btif_hl_find_app_idx_using_deleted_mdl_id(
3255 p_data->delete_mdl_cfm.mdl_id, &app_idx)) {
3256 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
3257 btif_hl_send_destroyed_cb(p_acb);
3258 btif_hl_clean_delete_mdl(&p_acb->delete_mdl);
3259 }
3260 break;
3261
3262 case BTA_HL_DCH_RECONNECT_CFM_EVT:
3263 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_RECONNECT_CFM_EVT");
3264 BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=%d status=%d ",
3265 p_data->dch_reconnect_cfm.mcl_handle,
3266 p_data->dch_reconnect_cfm.mdl_handle,
3267 p_data->dch_reconnect_cfm.status);
3268 BTIF_TRACE_DEBUG("first_reliable =%d dch_mode=%d mdl_id=%d mtu=%d",
3269 p_data->dch_reconnect_cfm.first_reliable,
3270 p_data->dch_reconnect_cfm.dch_mode,
3271 p_data->dch_reconnect_cfm.mdl_id,
3272 p_data->dch_reconnect_cfm.mtu);
3273
3274 if (p_data->dch_reconnect_cfm.status == BTA_HL_STATUS_OK) {
3275 status = btif_hl_proc_dch_reconnect_cfm(p_data);
3276 } else {
3277 status = false;
3278 }
3279
3280 if (!status) {
3281 if (btif_hl_find_mcl_idx_using_handle(p_data->dch_open_cfm.mcl_handle,
3282 &app_idx, &mcl_idx)) {
3283 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
3284 pending_op = p_mcb->pcb.op;
3285 switch (pending_op) {
3286 case BTIF_HL_PEND_DCH_OP_OPEN:
3287 btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
3288 break;
3289 case BTIF_HL_PEND_DCH_OP_RECONNECT:
3290 case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
3291 default:
3292 break;
3293 }
3294 }
3295 }
3296
3297 break;
3298
3299 case BTA_HL_CCH_CLOSE_CFM_EVT:
3300 BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_CLOSE_CFM_EVT");
3301 BTIF_TRACE_DEBUG("mcl_handle=%d status =%d",
3302 p_data->cch_close_cfm.mcl_handle,
3303 p_data->cch_close_cfm.status);
3304 if (p_data->cch_close_cfm.status == BTA_HL_STATUS_OK) {
3305 btif_hl_proc_cch_close_cfm(p_data);
3306 }
3307 break;
3308
3309 case BTA_HL_CCH_CLOSE_IND_EVT:
3310 BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_CLOSE_IND_EVT");
3311 BTIF_TRACE_DEBUG("mcl_handle =%d intentional_close=%s",
3312 p_data->cch_close_ind.mcl_handle,
3313 (p_data->cch_close_ind.intentional ? "Yes" : "No"));
3314
3315 btif_hl_proc_cch_close_ind(p_data);
3316 break;
3317
3318 case BTA_HL_DCH_CLOSE_IND_EVT:
3319 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_CLOSE_IND_EVT");
3320 BTIF_TRACE_DEBUG("mdl_handle=%d intentional_close=%s",
3321 p_data->dch_close_ind.mdl_handle,
3322 (p_data->dch_close_ind.intentional ? "Yes" : "No"));
3323
3324 btif_hl_proc_dch_close_ind(p_data);
3325 break;
3326
3327 case BTA_HL_DCH_CLOSE_CFM_EVT:
3328 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_CLOSE_CFM_EVT");
3329 BTIF_TRACE_DEBUG("mdl_handle=%d status=%d ",
3330 p_data->dch_close_cfm.mdl_handle,
3331 p_data->dch_close_cfm.status);
3332
3333 if (p_data->dch_close_cfm.status == BTA_HL_STATUS_OK) {
3334 btif_hl_proc_dch_close_cfm(p_data);
3335 }
3336 break;
3337
3338 case BTA_HL_DCH_ECHO_TEST_CFM_EVT:
3339 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_ECHO_TEST_CFM_EVT");
3340 BTIF_TRACE_DEBUG("mcl_handle=%d status=%d",
3341 p_data->echo_test_cfm.mcl_handle,
3342 p_data->echo_test_cfm.status);
3343 /* not supported */
3344 break;
3345
3346 case BTA_HL_DCH_RECONNECT_IND_EVT:
3347 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_RECONNECT_IND_EVT");
3348
3349 BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=5d",
3350 p_data->dch_reconnect_ind.mcl_handle,
3351 p_data->dch_reconnect_ind.mdl_handle);
3352 BTIF_TRACE_DEBUG("first_reliable =%d dch_mode=%d mdl_id=%d mtu=%d",
3353 p_data->dch_reconnect_ind.first_reliable,
3354 p_data->dch_reconnect_ind.dch_mode,
3355 p_data->dch_reconnect_ind.mdl_id,
3356 p_data->dch_reconnect_ind.mtu);
3357
3358 btif_hl_proc_dch_reconnect_ind(p_data);
3359 break;
3360
3361 case BTA_HL_CONG_CHG_IND_EVT:
3362 BTIF_TRACE_DEBUG("Rcv BTA_HL_CONG_CHG_IND_EVT");
3363 BTIF_TRACE_DEBUG("mdl_handle=%d cong =%d",
3364 p_data->dch_cong_ind.mdl_handle,
3365 p_data->dch_cong_ind.cong);
3366 btif_hl_proc_dch_cong_ind(p_data);
3367 break;
3368
3369 case BTA_HL_DCH_ABORT_IND_EVT:
3370 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_ABORT_IND_EVT");
3371 BTIF_TRACE_DEBUG("mcl_handle=%d", p_data->dch_abort_ind.mcl_handle);
3372 btif_hl_proc_abort_ind(p_data->dch_abort_ind.mcl_handle);
3373 break;
3374 case BTA_HL_DCH_ABORT_CFM_EVT:
3375 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_ABORT_CFM_EVT");
3376 BTIF_TRACE_DEBUG("mcl_handle=%d status =%d",
3377 p_data->dch_abort_cfm.mcl_handle,
3378 p_data->dch_abort_cfm.status);
3379 if (p_data->dch_abort_cfm.status == BTA_HL_STATUS_OK) {
3380 btif_hl_proc_abort_cfm(p_data->dch_abort_ind.mcl_handle);
3381 }
3382 break;
3383
3384 case BTA_HL_DCH_SEND_DATA_CFM_EVT:
3385 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_SEND_DATA_CFM_EVT");
3386 BTIF_TRACE_DEBUG("mdl_handle=0x%x status =%d",
3387 p_data->dch_send_data_cfm.mdl_handle,
3388 p_data->dch_send_data_cfm.status);
3389 btif_hl_proc_send_data_cfm(p_data->dch_send_data_cfm.mdl_handle,
3390 p_data->dch_send_data_cfm.status);
3391 break;
3392
3393 case BTA_HL_DCH_RCV_DATA_IND_EVT:
3394 BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_RCV_DATA_IND_EVT");
3395 BTIF_TRACE_DEBUG("mdl_handle=0x%x ", p_data->dch_rcv_data_ind.mdl_handle);
3396 /* do nothing here */
3397 break;
3398
3399 default:
3400 BTIF_TRACE_DEBUG("Unknown Event (0x%02x)...", event);
3401 break;
3402 }
3403 }
3404
3405 /*******************************************************************************
3406 *
3407 * Function btif_hl_cback
3408 *
3409 * Description Callback function for HL events
3410 *
3411 * Returns void
3412 *
3413 ******************************************************************************/
btif_hl_cback(tBTA_HL_EVT event,tBTA_HL * p_data)3414 static void btif_hl_cback(tBTA_HL_EVT event, tBTA_HL* p_data) {
3415 bt_status_t status;
3416 int param_len = 0;
3417 BTIF_TRACE_DEBUG("%s event %d", __func__, event);
3418 btif_hl_display_calling_process_name();
3419 switch (event) {
3420 case BTA_HL_REGISTER_CFM_EVT:
3421 param_len = sizeof(tBTA_HL_REGISTER_CFM);
3422 break;
3423 case BTA_HL_SDP_INFO_IND_EVT:
3424 param_len = sizeof(tBTA_HL_SDP_INFO_IND);
3425 break;
3426 case BTA_HL_DEREGISTER_CFM_EVT:
3427 param_len = sizeof(tBTA_HL_DEREGISTER_CFM);
3428 break;
3429 case BTA_HL_SDP_QUERY_CFM_EVT:
3430 param_len = sizeof(tBTA_HL_SDP_QUERY_CFM);
3431 break;
3432 case BTA_HL_CCH_OPEN_CFM_EVT:
3433 param_len = sizeof(tBTA_HL_CCH_OPEN_CFM);
3434 break;
3435 case BTA_HL_DCH_OPEN_CFM_EVT:
3436 param_len = sizeof(tBTA_HL_DCH_OPEN_CFM);
3437 break;
3438 case BTA_HL_CCH_OPEN_IND_EVT:
3439 param_len = sizeof(tBTA_HL_CCH_OPEN_IND);
3440 break;
3441 case BTA_HL_DCH_CREATE_IND_EVT:
3442 param_len = sizeof(tBTA_HL_DCH_CREATE_IND);
3443 break;
3444 case BTA_HL_DCH_OPEN_IND_EVT:
3445 param_len = sizeof(tBTA_HL_DCH_OPEN_IND);
3446 break;
3447 case BTA_HL_DELETE_MDL_IND_EVT:
3448 param_len = sizeof(tBTA_HL_MDL_IND);
3449 break;
3450 case BTA_HL_DELETE_MDL_CFM_EVT:
3451 param_len = sizeof(tBTA_HL_MDL_CFM);
3452 break;
3453 case BTA_HL_DCH_RECONNECT_CFM_EVT:
3454 param_len = sizeof(tBTA_HL_DCH_OPEN_CFM);
3455 break;
3456 case BTA_HL_CCH_CLOSE_CFM_EVT:
3457 param_len = sizeof(tBTA_HL_MCL_CFM);
3458 break;
3459 case BTA_HL_CCH_CLOSE_IND_EVT:
3460 param_len = sizeof(tBTA_HL_CCH_CLOSE_IND);
3461 break;
3462 case BTA_HL_DCH_CLOSE_IND_EVT:
3463 param_len = sizeof(tBTA_HL_DCH_CLOSE_IND);
3464 break;
3465 case BTA_HL_DCH_CLOSE_CFM_EVT:
3466 param_len = sizeof(tBTA_HL_MDL_CFM);
3467 break;
3468 case BTA_HL_DCH_ECHO_TEST_CFM_EVT:
3469 param_len = sizeof(tBTA_HL_MCL_CFM);
3470 break;
3471 case BTA_HL_DCH_RECONNECT_IND_EVT:
3472 param_len = sizeof(tBTA_HL_DCH_OPEN_IND);
3473 break;
3474 case BTA_HL_CONG_CHG_IND_EVT:
3475 param_len = sizeof(tBTA_HL_DCH_CONG_IND);
3476 break;
3477 case BTA_HL_DCH_ABORT_IND_EVT:
3478 param_len = sizeof(tBTA_HL_MCL_IND);
3479 break;
3480 case BTA_HL_DCH_ABORT_CFM_EVT:
3481 param_len = sizeof(tBTA_HL_MCL_CFM);
3482 break;
3483 case BTA_HL_DCH_SEND_DATA_CFM_EVT:
3484 param_len = sizeof(tBTA_HL_MDL_CFM);
3485 break;
3486 case BTA_HL_DCH_RCV_DATA_IND_EVT:
3487 param_len = sizeof(tBTA_HL_MDL_IND);
3488 break;
3489 default:
3490 param_len = sizeof(tBTA_HL_MDL_IND);
3491 break;
3492 }
3493 status = btif_transfer_context(btif_hl_upstreams_evt, (uint16_t)event,
3494 (char*)p_data, param_len, NULL);
3495
3496 /* catch any failed context transfers */
3497 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
3498 }
3499
3500 /*******************************************************************************
3501 *
3502 * Function btif_hl_upstreams_ctrl_evt
3503 *
3504 * Description Callback function for HL control events in the BTIF task
3505 * context
3506 *
3507 * Returns void
3508 *
3509 ******************************************************************************/
btif_hl_upstreams_ctrl_evt(uint16_t event,char * p_param)3510 static void btif_hl_upstreams_ctrl_evt(uint16_t event, char* p_param) {
3511 tBTA_HL_CTRL* p_data = (tBTA_HL_CTRL*)p_param;
3512 uint8_t i;
3513 tBTA_HL_REG_PARAM reg_param;
3514 btif_hl_app_cb_t* p_acb;
3515
3516 BTIF_TRACE_DEBUG("%s event %d", __func__, event);
3517 btif_hl_display_calling_process_name();
3518
3519 switch (event) {
3520 case BTA_HL_CTRL_ENABLE_CFM_EVT:
3521 BTIF_TRACE_DEBUG("Rcv BTA_HL_CTRL_ENABLE_CFM_EVT");
3522 BTIF_TRACE_DEBUG("status=%d", p_data->enable_cfm.status);
3523
3524 if (p_data->enable_cfm.status == BTA_HL_STATUS_OK) {
3525 btif_hl_set_state(BTIF_HL_STATE_ENABLED);
3526
3527 for (i = 0; i < BTA_HL_NUM_APPS; i++) {
3528 p_acb = BTIF_HL_GET_APP_CB_PTR(i);
3529 if (p_acb->in_use && p_acb->reg_pending) {
3530 p_acb->reg_pending = false;
3531 reg_param.dev_type = p_acb->dev_type;
3532 reg_param.sec_mask = BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT;
3533 reg_param.p_srv_name = p_acb->srv_name;
3534 reg_param.p_srv_desp = p_acb->srv_desp;
3535 reg_param.p_provider_name = p_acb->provider_name;
3536
3537 BTIF_TRACE_DEBUG("Register pending app_id=%d", p_acb->app_id);
3538 btif_hl_proc_reg_request(i, p_acb->app_id, ®_param,
3539 btif_hl_cback);
3540 }
3541 }
3542 }
3543
3544 break;
3545 case BTA_HL_CTRL_DISABLE_CFM_EVT:
3546 BTIF_TRACE_DEBUG("Rcv BTA_HL_CTRL_DISABLE_CFM_EVT");
3547 BTIF_TRACE_DEBUG("status=%d", p_data->disable_cfm.status);
3548
3549 if (p_data->disable_cfm.status == BTA_HL_STATUS_OK) {
3550 for (size_t i = 0; i < BTA_HL_NUM_APPS; i++) {
3551 for (size_t j = 0; j < BTA_HL_NUM_MCLS; j++) {
3552 alarm_free(p_btif_hl_cb->acb[i].mcb[j].cch_timer);
3553 }
3554 }
3555 memset(p_btif_hl_cb, 0, sizeof(btif_hl_cb_t));
3556 btif_hl_set_state(BTIF_HL_STATE_DISABLED);
3557 }
3558
3559 break;
3560 default:
3561 break;
3562 }
3563 }
3564
3565 /*******************************************************************************
3566 *
3567 * Function btif_hl_ctrl_cback
3568 *
3569 * Description Callback function for HL control events
3570 *
3571 * Returns void
3572 *
3573 ******************************************************************************/
btif_hl_ctrl_cback(tBTA_HL_CTRL_EVT event,tBTA_HL_CTRL * p_data)3574 static void btif_hl_ctrl_cback(tBTA_HL_CTRL_EVT event, tBTA_HL_CTRL* p_data) {
3575 bt_status_t status;
3576 int param_len = 0;
3577
3578 BTIF_TRACE_DEBUG("%s event %d", __func__, event);
3579 btif_hl_display_calling_process_name();
3580
3581 switch (event) {
3582 case BTA_HL_CTRL_ENABLE_CFM_EVT:
3583 case BTA_HL_CTRL_DISABLE_CFM_EVT:
3584 param_len = sizeof(tBTA_HL_CTRL_ENABLE_DISABLE);
3585 break;
3586 default:
3587 break;
3588 }
3589
3590 status = btif_transfer_context(btif_hl_upstreams_ctrl_evt, (uint16_t)event,
3591 (char*)p_data, param_len, NULL);
3592 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
3593 }
3594 /*******************************************************************************
3595 *
3596 * Function connect_channel
3597 *
3598 * Description connect a data channel
3599 *
3600 * Returns bt_status_t
3601 *
3602 ******************************************************************************/
connect_channel(int app_id,bt_bdaddr_t * bd_addr,int mdep_cfg_index,int * channel_id)3603 static bt_status_t connect_channel(int app_id, bt_bdaddr_t* bd_addr,
3604 int mdep_cfg_index, int* channel_id) {
3605 uint8_t app_idx, mcl_idx;
3606 btif_hl_app_cb_t* p_acb = NULL;
3607 btif_hl_pending_chan_cb_t* p_pcb = NULL;
3608 btif_hl_mcl_cb_t* p_mcb = NULL;
3609 bt_status_t status = BT_STATUS_SUCCESS;
3610 tBTA_HL_DCH_OPEN_PARAM dch_open;
3611 BD_ADDR bda;
3612 uint8_t i;
3613
3614 CHECK_BTHL_INIT();
3615 BTIF_TRACE_EVENT("%s", __func__);
3616 btif_hl_display_calling_process_name();
3617
3618 for (i = 0; i < 6; i++) {
3619 bda[i] = (uint8_t)bd_addr->address[i];
3620 }
3621 if (btif_hl_find_app_idx(((uint8_t)app_id), &app_idx)) {
3622 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
3623 if (btif_hl_find_mcl_idx(app_idx, bda, &mcl_idx)) {
3624 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
3625 if (p_mcb->is_connected) {
3626 dch_open.ctrl_psm = p_mcb->ctrl_psm;
3627 dch_open.local_mdep_id =
3628 p_acb->sup_feature.mdep[mdep_cfg_index].mdep_id;
3629 BTIF_TRACE_DEBUG(
3630 "connect_channel: app_idx =%d, mdep_cfg_indx =%d, mdep_id =%d "
3631 "app_id= %d",
3632 app_idx, mdep_cfg_index, dch_open.local_mdep_id, app_id);
3633 if (btif_hl_find_peer_mdep_id(
3634 p_acb->app_id, p_mcb->bd_addr,
3635 p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role,
3636 p_acb->sup_feature.mdep[mdep_cfg_index]
3637 .mdep_cfg.data_cfg[0]
3638 .data_type,
3639 &dch_open.peer_mdep_id)) {
3640 dch_open.local_cfg = p_acb->channel_type[mdep_cfg_index];
3641 if ((p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role ==
3642 BTA_HL_MDEP_ROLE_SOURCE) &&
3643 !btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) {
3644 dch_open.local_cfg = BTA_HL_DCH_CFG_RELIABLE;
3645 }
3646 dch_open.sec_mask = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
3647
3648 if (!btif_hl_dch_open(p_acb->app_id, bda, &dch_open, mdep_cfg_index,
3649 BTIF_HL_PEND_DCH_OP_OPEN, channel_id)) {
3650 status = BT_STATUS_FAIL;
3651 BTIF_TRACE_EVENT("%s loc0 status = BT_STATUS_FAIL", __func__);
3652 }
3653 } else {
3654 p_mcb->cch_oper = BTIF_HL_CCH_OP_MDEP_FILTERING;
3655
3656 p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
3657 p_pcb->in_use = true;
3658 p_pcb->mdep_cfg_idx = mdep_cfg_index;
3659 memcpy(p_pcb->bd_addr, bda, sizeof(BD_ADDR));
3660 p_pcb->op = BTIF_HL_PEND_DCH_OP_OPEN;
3661 BTA_HlSdpQuery(app_id, p_acb->app_handle, bda);
3662 }
3663 } else {
3664 status = BT_STATUS_FAIL;
3665 }
3666 } else {
3667 p_acb->filter.num_elems = 1;
3668 p_acb->filter.elem[0].data_type = p_acb->sup_feature.mdep[mdep_cfg_index]
3669 .mdep_cfg.data_cfg[mdep_cfg_index]
3670 .data_type;
3671 if (p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role ==
3672 BTA_HL_MDEP_ROLE_SINK)
3673 p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
3674 else
3675 p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
3676
3677 if (!btif_hl_cch_open(p_acb->app_id, bda, 0, mdep_cfg_index,
3678 BTIF_HL_PEND_DCH_OP_OPEN, channel_id)) {
3679 status = BT_STATUS_FAIL;
3680 }
3681 }
3682 } else {
3683 status = BT_STATUS_FAIL;
3684 }
3685
3686 BTIF_TRACE_DEBUG("%s status=%d channel_id=0x%08x", __func__, status,
3687 *channel_id);
3688
3689 return status;
3690 }
3691 /*******************************************************************************
3692 *
3693 * Function destroy_channel
3694 *
3695 * Description destroy a data channel
3696 *
3697 * Returns bt_status_t
3698 *
3699 ******************************************************************************/
destroy_channel(int channel_id)3700 static bt_status_t destroy_channel(int channel_id) {
3701 uint8_t app_idx, mcl_idx, mdl_cfg_idx, mdep_cfg_idx = 0;
3702 bt_status_t status = BT_STATUS_SUCCESS;
3703 btif_hl_mdl_cfg_t* p_mdl;
3704 btif_hl_mcl_cb_t* p_mcb;
3705 btif_hl_app_cb_t* p_acb;
3706
3707 CHECK_BTHL_INIT();
3708 BTIF_TRACE_EVENT("%s channel_id=0x%08x", __func__, channel_id);
3709 btif_hl_display_calling_process_name();
3710
3711 if (btif_hl_if_channel_setup_pending(channel_id, &app_idx, &mcl_idx)) {
3712 btif_hl_dch_abort(app_idx, mcl_idx);
3713 } else {
3714 if (btif_hl_find_mdl_cfg_idx_using_channel_id(channel_id, &app_idx,
3715 &mdl_cfg_idx))
3716 // if(btif_hl_find_mdl_idx_using_channel_id(channel_id,
3717 // &app_idx,&mcl_idx, &mdl_idx))
3718 {
3719 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
3720 if (!p_acb->delete_mdl.active) {
3721 p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, mdl_cfg_idx);
3722 p_acb->delete_mdl.active = true;
3723 p_acb->delete_mdl.mdl_id = p_mdl->base.mdl_id;
3724 p_acb->delete_mdl.channel_id = channel_id;
3725 p_acb->delete_mdl.mdep_cfg_idx = p_mdl->extra.mdep_cfg_idx;
3726 memcpy(p_acb->delete_mdl.bd_addr, p_mdl->base.peer_bd_addr,
3727 sizeof(BD_ADDR));
3728
3729 if (btif_hl_find_mcl_idx(app_idx, p_mdl->base.peer_bd_addr, &mcl_idx)) {
3730 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
3731 if (p_mcb->is_connected) {
3732 BTIF_TRACE_DEBUG("calling BTA_HlDeleteMdl mdl_id=%d",
3733 p_acb->delete_mdl.mdl_id);
3734 BTA_HlDeleteMdl(p_mcb->mcl_handle, p_acb->delete_mdl.mdl_id);
3735 } else {
3736 status = BT_STATUS_FAIL;
3737 }
3738 } else {
3739 BTIF_TRACE_DEBUG("btif_hl_delete_mdl calling btif_hl_cch_open");
3740 mdep_cfg_idx = p_mdl->extra.mdep_cfg_idx;
3741 p_acb->filter.num_elems = 1;
3742 p_acb->filter.elem[0].data_type =
3743 p_acb->sup_feature.mdep[mdep_cfg_idx]
3744 .mdep_cfg.data_cfg[mdep_cfg_idx]
3745 .data_type;
3746 if (p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.mdep_role ==
3747 BTA_HL_MDEP_ROLE_SINK)
3748 p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
3749 else
3750 p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
3751 if (btif_hl_cch_open(p_acb->app_id, p_acb->delete_mdl.bd_addr, 0,
3752 mdep_cfg_idx, BTIF_HL_PEND_DCH_OP_DELETE_MDL,
3753 NULL)) {
3754 status = BT_STATUS_FAIL;
3755 }
3756 }
3757
3758 if (status == BT_STATUS_FAIL) {
3759 /* fail for now */
3760 btif_hl_clean_delete_mdl(&p_acb->delete_mdl);
3761 }
3762 } else {
3763 status = BT_STATUS_BUSY;
3764 }
3765 } else {
3766 status = BT_STATUS_FAIL;
3767 }
3768 }
3769 return status;
3770 }
3771 /*******************************************************************************
3772 *
3773 * Function unregister_application
3774 *
3775 * Description unregister an HDP application
3776 *
3777 * Returns bt_status_t
3778 *
3779 ******************************************************************************/
unregister_application(int app_id)3780 static bt_status_t unregister_application(int app_id) {
3781 uint8_t app_idx;
3782 int len;
3783 bt_status_t status = BT_STATUS_SUCCESS;
3784 btif_hl_evt_cb_t evt_param;
3785
3786 CHECK_BTHL_INIT();
3787 BTIF_TRACE_EVENT("%s app_id=%d", __func__, app_id);
3788 btif_hl_display_calling_process_name();
3789
3790 if (btif_hl_find_app_idx(((uint8_t)app_id), &app_idx)) {
3791 evt_param.unreg.app_idx = app_idx;
3792 reg_counter--;
3793 len = sizeof(btif_hl_unreg_t);
3794 status = btif_transfer_context(btif_hl_proc_cb_evt, BTIF_HL_UNREG_APP,
3795 (char*)&evt_param, len, NULL);
3796 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
3797 } else {
3798 status = BT_STATUS_FAIL;
3799 }
3800
3801 BTIF_TRACE_DEBUG("de-reg return status=%d", status);
3802 return status;
3803 }
3804 /*******************************************************************************
3805 *
3806 * Function register_application
3807 *
3808 * Description register an HDP application
3809 *
3810 * Returns bt_status_t
3811 *
3812 ******************************************************************************/
register_application(bthl_reg_param_t * p_reg_param,int * app_id)3813 static bt_status_t register_application(bthl_reg_param_t* p_reg_param,
3814 int* app_id) {
3815 btif_hl_app_cb_t* p_acb;
3816 tBTA_HL_SUP_FEATURE* p_sup;
3817 tBTA_HL_MDEP_CFG* p_cfg;
3818 tBTA_HL_MDEP_DATA_TYPE_CFG* p_data;
3819 uint8_t app_idx = 0, i = 0;
3820 bthl_mdep_cfg_t* p_mdep_cfg;
3821 bt_status_t status = BT_STATUS_SUCCESS;
3822 btif_hl_evt_cb_t evt_param;
3823 int len;
3824
3825 CHECK_BTHL_INIT();
3826 BTIF_TRACE_EVENT("%s", __func__);
3827 btif_hl_display_calling_process_name();
3828
3829 if (btif_hl_get_state() == BTIF_HL_STATE_DISABLED) {
3830 btif_hl_init();
3831 btif_hl_set_state(BTIF_HL_STATE_ENABLING);
3832 BTA_HlEnable(btif_hl_ctrl_cback);
3833 }
3834
3835 if (!btif_hl_find_avail_app_idx(&app_idx)) {
3836 BTIF_TRACE_ERROR("Unable to allocate a new application control block");
3837 return BT_STATUS_FAIL;
3838 }
3839
3840 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
3841 p_acb->in_use = true;
3842
3843 p_acb->app_id = btif_hl_get_next_app_id();
3844
3845 if (p_reg_param->application_name != NULL)
3846 strncpy(p_acb->application_name, p_reg_param->application_name,
3847 BTIF_HL_APPLICATION_NAME_LEN);
3848
3849 if (p_reg_param->provider_name != NULL)
3850 strncpy(p_acb->provider_name, p_reg_param->provider_name,
3851 BTA_PROVIDER_NAME_LEN);
3852
3853 if (p_reg_param->srv_name != NULL)
3854 strncpy(p_acb->srv_name, p_reg_param->srv_name, BTA_SERVICE_NAME_LEN);
3855
3856 if (p_reg_param->srv_desp != NULL)
3857 strncpy(p_acb->srv_desp, p_reg_param->srv_desp, BTA_SERVICE_DESP_LEN);
3858
3859 p_sup = &p_acb->sup_feature;
3860 p_sup->advertize_source_sdp = true;
3861 p_sup->echo_cfg.max_rx_apdu_size = BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE;
3862 p_sup->echo_cfg.max_tx_apdu_size = BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE;
3863 p_sup->num_of_mdeps = p_reg_param->number_of_mdeps;
3864
3865 for (i = 0, p_mdep_cfg = p_reg_param->mdep_cfg; i < p_sup->num_of_mdeps;
3866 i++, p_mdep_cfg++) {
3867 p_cfg = &p_sup->mdep[i].mdep_cfg;
3868 p_cfg->num_of_mdep_data_types = 1;
3869 p_data = &p_cfg->data_cfg[0];
3870
3871 if (!btif_hl_get_bta_mdep_role(p_mdep_cfg->mdep_role,
3872 &(p_cfg->mdep_role))) {
3873 BTIF_TRACE_ERROR("Invalid mdep_role=%d", p_mdep_cfg->mdep_role);
3874 status = BT_STATUS_FAIL;
3875 break;
3876 } else {
3877 if (p_cfg->mdep_role == BTA_HL_MDEP_ROLE_SINK)
3878 p_sup->app_role_mask |= BTA_HL_MDEP_ROLE_MASK_SINK;
3879 else
3880 p_sup->app_role_mask |= BTA_HL_MDEP_ROLE_MASK_SOURCE;
3881
3882 if ((p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SINK) &&
3883 (p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SOURCE)) {
3884 p_acb->dev_type = BTA_HL_DEVICE_TYPE_DUAL;
3885 } else if (p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SINK) {
3886 p_acb->dev_type = BTA_HL_DEVICE_TYPE_SINK;
3887 } else {
3888 p_acb->dev_type = BTA_HL_DEVICE_TYPE_SOURCE;
3889 }
3890
3891 p_data->data_type = (uint16_t)p_mdep_cfg->data_type;
3892 p_data->max_rx_apdu_size =
3893 btif_hl_get_max_rx_apdu_size(p_cfg->mdep_role, p_data->data_type);
3894 p_data->max_tx_apdu_size =
3895 btif_hl_get_max_tx_apdu_size(p_cfg->mdep_role, p_data->data_type);
3896
3897 if (p_mdep_cfg->mdep_description != NULL)
3898 strncpy(p_data->desp, p_mdep_cfg->mdep_description,
3899 BTA_SERVICE_DESP_LEN);
3900
3901 if (!btif_hl_get_bta_channel_type(p_mdep_cfg->channel_type,
3902 &(p_acb->channel_type[i]))) {
3903 BTIF_TRACE_ERROR("Invalid channel_type=%d", p_mdep_cfg->channel_type);
3904 status = BT_STATUS_FAIL;
3905 break;
3906 }
3907 }
3908 }
3909
3910 if (status == BT_STATUS_SUCCESS) {
3911 *app_id = (int)p_acb->app_id;
3912 evt_param.reg.app_idx = app_idx;
3913 len = sizeof(btif_hl_reg_t);
3914 p_acb->reg_pending = true;
3915 BTIF_TRACE_DEBUG("calling btif_transfer_context status=%d app_id=%d",
3916 status, *app_id);
3917 status = btif_transfer_context(btif_hl_proc_cb_evt, BTIF_HL_REG_APP,
3918 (char*)&evt_param, len, NULL);
3919 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
3920
3921 } else {
3922 btif_hl_free_app_idx(app_idx);
3923 }
3924
3925 BTIF_TRACE_DEBUG("register_application status=%d app_id=%d", status, *app_id);
3926 return status;
3927 }
3928
3929 /*******************************************************************************
3930 *
3931 * Function btif_hl_save_mdl_cfg
3932 *
3933 * Description Save the MDL configuration
3934 *
3935 * Returns bool
3936 *
3937 ******************************************************************************/
btif_hl_save_mdl_cfg(uint8_t mdep_id,uint8_t item_idx,tBTA_HL_MDL_CFG * p_mdl_cfg)3938 bool btif_hl_save_mdl_cfg(uint8_t mdep_id, uint8_t item_idx,
3939 tBTA_HL_MDL_CFG* p_mdl_cfg) {
3940 btif_hl_mdl_cfg_t* p_mdl = NULL;
3941 bool success = false;
3942 btif_hl_app_cb_t* p_acb;
3943 btif_hl_mcl_cb_t* p_mcb;
3944 uint8_t app_idx, mcl_idx, len;
3945 bt_status_t bt_status;
3946 btif_hl_evt_cb_t evt_param;
3947 int* p_channel_id;
3948
3949 BTIF_TRACE_DEBUG(
3950 "%s mdep_id=%d item_idx=%d, local_mdep_id=%d mdl_id=0x%x dch_mode=%d",
3951 __func__, mdep_id, item_idx, p_mdl_cfg->local_mdep_id, p_mdl_cfg->mdl_id,
3952 p_mdl_cfg->dch_mode);
3953
3954 if (btif_hl_find_app_idx_using_mdepId(mdep_id, &app_idx)) {
3955 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
3956 p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, item_idx);
3957 p_channel_id = BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx, item_idx);
3958 if (p_mdl) {
3959 memcpy(&p_mdl->base, p_mdl_cfg, sizeof(tBTA_HL_MDL_CFG));
3960 if (btif_hl_find_mcl_idx(app_idx, p_mdl->base.peer_bd_addr, &mcl_idx)) {
3961 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
3962 if (p_mcb->pcb.in_use)
3963 *p_channel_id = p_mcb->pcb.channel_id;
3964 else
3965 *p_channel_id = btif_hl_get_next_channel_id(p_acb->app_id);
3966 p_mdl->extra.mdep_cfg_idx = p_mcb->pcb.mdep_cfg_idx;
3967 p_mdl->extra.data_type =
3968 p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx]
3969 .mdep_cfg.data_cfg[0]
3970 .data_type;
3971
3972 if (!btif_hl_find_peer_mdep_id(
3973 p_acb->app_id, p_mcb->bd_addr,
3974 p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx]
3975 .mdep_cfg.mdep_role,
3976 p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx]
3977 .mdep_cfg.data_cfg[0]
3978 .data_type,
3979 &p_mdl->extra.peer_mdep_id)) {
3980 p_mdl->extra.peer_mdep_id = BTA_HL_INVALID_MDEP_ID;
3981 }
3982 BTIF_TRACE_DEBUG("%s app_idx=%d item_idx=%d mld_id=0x%x", __func__,
3983 app_idx, item_idx, p_mdl->base.mdl_id);
3984 evt_param.update_mdl.app_idx = app_idx;
3985 len = sizeof(btif_hl_update_mdl_t);
3986 BTIF_TRACE_DEBUG("send BTIF_HL_UPDATE_MDL event app_idx=%d ", app_idx);
3987 bt_status =
3988 btif_transfer_context(btif_hl_proc_cb_evt, BTIF_HL_UPDATE_MDL,
3989 (char*)&evt_param, len, NULL);
3990 if (bt_status == BT_STATUS_SUCCESS) {
3991 success = true;
3992 }
3993 ASSERTC(bt_status == BT_STATUS_SUCCESS, "context transfer failed",
3994 bt_status);
3995 }
3996 }
3997 }
3998 BTIF_TRACE_DEBUG("%s success=%d ", __func__, success);
3999
4000 return success;
4001 }
4002
4003 /*******************************************************************************
4004 *
4005 * Function btif_hl_delete_mdl_cfg
4006 *
4007 * Description Delete the MDL configuration
4008 *
4009 * Returns bool
4010 *
4011 ******************************************************************************/
btif_hl_delete_mdl_cfg(uint8_t mdep_id,uint8_t item_idx)4012 bool btif_hl_delete_mdl_cfg(uint8_t mdep_id, uint8_t item_idx) {
4013 btif_hl_mdl_cfg_t* p_mdl = NULL;
4014 bool success = false;
4015 uint8_t app_idx, len;
4016 bt_status_t bt_status;
4017 btif_hl_evt_cb_t evt_param;
4018
4019 if (btif_hl_find_app_idx_using_mdepId(mdep_id, &app_idx)) {
4020 p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, item_idx);
4021 if (p_mdl) {
4022 memset(p_mdl, 0, sizeof(btif_hl_mdl_cfg_t));
4023 evt_param.update_mdl.app_idx = app_idx;
4024 len = sizeof(btif_hl_update_mdl_t);
4025 BTIF_TRACE_DEBUG("send BTIF_HL_UPDATE_MDL event app_idx=%d ", app_idx);
4026 bt_status = btif_transfer_context(btif_hl_proc_cb_evt, BTIF_HL_UPDATE_MDL,
4027 (char*)&evt_param, len, NULL);
4028 if (bt_status == BT_STATUS_SUCCESS) {
4029 success = true;
4030 }
4031 ASSERTC(bt_status == BT_STATUS_SUCCESS, "context transfer failed",
4032 bt_status);
4033 }
4034 }
4035
4036 BTIF_TRACE_DEBUG("%s success=%d ", __func__, success);
4037 return success;
4038 }
4039
4040 /*******************************************************************************
4041 *
4042 * Function init
4043 *
4044 * Description initializes the hl interface
4045 *
4046 * Returns bt_status_t
4047 *
4048 ******************************************************************************/
init(bthl_callbacks_t * callbacks)4049 static bt_status_t init(bthl_callbacks_t* callbacks) {
4050 bt_status_t status = BT_STATUS_SUCCESS;
4051
4052 BTIF_TRACE_EVENT("%s", __func__);
4053 btif_hl_display_calling_process_name();
4054 bt_hl_callbacks_cb = *callbacks;
4055 bt_hl_callbacks = &bt_hl_callbacks_cb;
4056 btif_hl_soc_thread_init();
4057 reg_counter = 0;
4058 return status;
4059 }
4060 /*******************************************************************************
4061 *
4062 * Function cleanup
4063 *
4064 * Description Closes the HL interface
4065 *
4066 * Returns void
4067 *
4068 ******************************************************************************/
cleanup(void)4069 static void cleanup(void) {
4070 BTIF_TRACE_EVENT("%s", __func__);
4071 btif_hl_display_calling_process_name();
4072 if (bt_hl_callbacks) {
4073 btif_disable_service(BTA_HDP_SERVICE_ID);
4074 bt_hl_callbacks = NULL;
4075 reg_counter = 0;
4076 }
4077
4078 btif_hl_disable();
4079 btif_hl_close_select_thread();
4080 }
4081
4082 static const bthl_interface_t bthlInterface = {
4083 sizeof(bthl_interface_t),
4084 init,
4085 register_application,
4086 unregister_application,
4087 connect_channel,
4088 destroy_channel,
4089 cleanup,
4090 };
4091
4092 /*******************************************************************************
4093 *
4094 * Function btif_hl_get_interface
4095 *
4096 * Description Get the hl callback interface
4097 *
4098 * Returns bthf_interface_t
4099 *
4100 ******************************************************************************/
btif_hl_get_interface()4101 const bthl_interface_t* btif_hl_get_interface() {
4102 BTIF_TRACE_EVENT("%s", __func__);
4103 return &bthlInterface;
4104 }
4105
4106 /*******************************************************************************
4107 *
4108 * Function btif_hl_update_maxfd
4109 *
4110 * Description Update the max fd if the input fd is greater than the current max
4111 * fd
4112 *
4113 * Returns int
4114 *
4115 ******************************************************************************/
btif_hl_update_maxfd(int max_org_s)4116 int btif_hl_update_maxfd(int max_org_s) {
4117 int maxfd = max_org_s;
4118
4119 BTIF_TRACE_DEBUG("btif_hl_update_maxfd max_org_s= %d", max_org_s);
4120 for (const list_node_t* node = list_begin(soc_queue);
4121 node != list_end(soc_queue); node = list_next(node)) {
4122 btif_hl_soc_cb_t* p_scb = (btif_hl_soc_cb_t*)list_node(node);
4123 if (maxfd < p_scb->max_s) {
4124 maxfd = p_scb->max_s;
4125 BTIF_TRACE_DEBUG("btif_hl_update_maxfd maxfd=%d", maxfd);
4126 }
4127 }
4128
4129 BTIF_TRACE_DEBUG("btif_hl_update_maxfd final *p_max_s=%d", maxfd);
4130 return maxfd;
4131 }
4132 /*******************************************************************************
4133 *
4134 * Function btif_hl_get_socket_state
4135 *
4136 * Description get socket state
4137 *
4138 * Returns btif_hl_soc_state_t
4139 *
4140 ******************************************************************************/
btif_hl_get_socket_state(btif_hl_soc_cb_t * p_scb)4141 btif_hl_soc_state_t btif_hl_get_socket_state(btif_hl_soc_cb_t* p_scb) {
4142 BTIF_TRACE_DEBUG("btif_hl_get_socket_state state=%d", p_scb->state);
4143 return p_scb->state;
4144 }
4145 /*******************************************************************************
4146 *
4147 * Function btif_hl_set_socket_state
4148 *
4149 * Description set socket state
4150 *
4151 * Returns void
4152 *
4153 ******************************************************************************/
btif_hl_set_socket_state(btif_hl_soc_cb_t * p_scb,btif_hl_soc_state_t new_state)4154 void btif_hl_set_socket_state(btif_hl_soc_cb_t* p_scb,
4155 btif_hl_soc_state_t new_state) {
4156 BTIF_TRACE_DEBUG("btif_hl_set_socket_state %d---->%d", p_scb->state,
4157 new_state);
4158 p_scb->state = new_state;
4159 }
4160 /*******************************************************************************
4161 *
4162 * Function btif_hl_release_mcl_sockets
4163 *
4164 * Description Release all sockets on the MCL
4165 *
4166 * Returns void
4167 *
4168 ******************************************************************************/
btif_hl_release_mcl_sockets(uint8_t app_idx,uint8_t mcl_idx)4169 void btif_hl_release_mcl_sockets(uint8_t app_idx, uint8_t mcl_idx) {
4170 uint8_t i;
4171 btif_hl_mdl_cb_t* p_dcb;
4172 bool found = false;
4173 BTIF_TRACE_DEBUG("%s", __func__);
4174 for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) {
4175 p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, i);
4176 if (p_dcb && p_dcb->in_use && p_dcb->p_scb) {
4177 BTIF_TRACE_DEBUG("found socket for app_idx=%d mcl_id=%d, mdl_idx=%d",
4178 app_idx, mcl_idx, i);
4179 btif_hl_set_socket_state(p_dcb->p_scb, BTIF_HL_SOC_STATE_W4_REL);
4180 p_dcb->p_scb = NULL;
4181 found = true;
4182 }
4183 }
4184 if (found) btif_hl_select_close_connected();
4185 }
4186 /*******************************************************************************
4187 *
4188 * Function btif_hl_release_socket
4189 *
4190 * Description release a specified socket
4191 *
4192 * Returns void
4193 *
4194 ******************************************************************************/
btif_hl_release_socket(uint8_t app_idx,uint8_t mcl_idx,uint8_t mdl_idx)4195 void btif_hl_release_socket(uint8_t app_idx, uint8_t mcl_idx, uint8_t mdl_idx) {
4196 btif_hl_soc_cb_t* p_scb = NULL;
4197 btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
4198
4199 BTIF_TRACE_DEBUG("%s", __func__);
4200 BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx,
4201 mdl_idx);
4202
4203 if (p_dcb && p_dcb->p_scb) {
4204 p_scb = p_dcb->p_scb;
4205 btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_W4_REL);
4206 p_dcb->p_scb = NULL;
4207 btif_hl_select_close_connected();
4208 }
4209 }
4210 /*******************************************************************************
4211 *
4212 * Function btif_hl_create_socket
4213 *
4214 * Description create a socket
4215 *
4216 * Returns bool
4217 *
4218 ******************************************************************************/
btif_hl_create_socket(uint8_t app_idx,uint8_t mcl_idx,uint8_t mdl_idx)4219 bool btif_hl_create_socket(uint8_t app_idx, uint8_t mcl_idx, uint8_t mdl_idx) {
4220 btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
4221 btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
4222 bool status = false;
4223
4224 BTIF_TRACE_DEBUG("%s", __func__);
4225
4226 if (p_dcb) {
4227 btif_hl_soc_cb_t* p_scb =
4228 (btif_hl_soc_cb_t*)osi_malloc(sizeof(btif_hl_soc_cb_t));
4229 if (socketpair(AF_UNIX, SOCK_STREAM, 0, p_scb->socket_id) >= 0) {
4230 BTIF_TRACE_DEBUG("socket id[0]=%d id[1]=%d", p_scb->socket_id[0],
4231 p_scb->socket_id[1]);
4232 p_dcb->p_scb = p_scb;
4233 p_scb->app_idx = app_idx;
4234 p_scb->mcl_idx = mcl_idx;
4235 p_scb->mdl_idx = mdl_idx;
4236 p_scb->channel_id = p_dcb->channel_id;
4237 p_scb->mdep_cfg_idx = p_dcb->local_mdep_cfg_idx;
4238 memcpy(p_scb->bd_addr, p_mcb->bd_addr, sizeof(BD_ADDR));
4239 btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_W4_ADD);
4240 p_scb->max_s = p_scb->socket_id[1];
4241 list_append(soc_queue, (void*)p_scb);
4242 btif_hl_select_wakeup();
4243 status = true;
4244 } else {
4245 osi_free_and_reset((void**)&p_scb);
4246 }
4247 }
4248
4249 BTIF_TRACE_DEBUG("%s status=%d", __func__, status);
4250 return status;
4251 }
4252 /*******************************************************************************
4253 *
4254 * Function btif_hl_add_socket_to_set
4255 *
4256 * Description Add a socket
4257 *
4258 * Returns void
4259 *
4260 ******************************************************************************/
btif_hl_add_socket_to_set(fd_set * p_org_set)4261 void btif_hl_add_socket_to_set(fd_set* p_org_set) {
4262 btif_hl_mdl_cb_t* p_dcb = NULL;
4263 btif_hl_mcl_cb_t* p_mcb = NULL;
4264 btif_hl_app_cb_t* p_acb = NULL;
4265 btif_hl_evt_cb_t evt_param;
4266 bt_status_t status;
4267 int len;
4268
4269 BTIF_TRACE_DEBUG("entering %s", __func__);
4270
4271 for (const list_node_t* node = list_begin(soc_queue);
4272 node != list_end(soc_queue); node = list_next(node)) {
4273 btif_hl_soc_cb_t* p_scb = (btif_hl_soc_cb_t*)list_node(node);
4274
4275 BTIF_TRACE_DEBUG("btif_hl_add_socket_to_set first p_scb=0x%x", p_scb);
4276 if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_ADD) {
4277 btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_W4_READ);
4278 FD_SET(p_scb->socket_id[1], p_org_set);
4279 BTIF_TRACE_DEBUG("found and set socket_id=%d is_set=%d",
4280 p_scb->socket_id[1],
4281 FD_ISSET(p_scb->socket_id[1], p_org_set));
4282 p_mcb = BTIF_HL_GET_MCL_CB_PTR(p_scb->app_idx, p_scb->mcl_idx);
4283 p_dcb = BTIF_HL_GET_MDL_CB_PTR(p_scb->app_idx, p_scb->mcl_idx,
4284 p_scb->mdl_idx);
4285 p_acb = BTIF_HL_GET_APP_CB_PTR(p_scb->app_idx);
4286 if (p_mcb && p_dcb) {
4287 btif_hl_stop_timer_using_handle(p_mcb->mcl_handle);
4288 evt_param.chan_cb.app_id = p_acb->app_id;
4289 memcpy(evt_param.chan_cb.bd_addr, p_mcb->bd_addr, sizeof(BD_ADDR));
4290 evt_param.chan_cb.channel_id = p_dcb->channel_id;
4291 evt_param.chan_cb.fd = p_scb->socket_id[0];
4292 evt_param.chan_cb.mdep_cfg_index = (int)p_dcb->local_mdep_cfg_idx;
4293 evt_param.chan_cb.cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING;
4294 len = sizeof(btif_hl_send_chan_state_cb_t);
4295 status = btif_transfer_context(btif_hl_proc_cb_evt,
4296 BTIF_HL_SEND_CONNECTED_CB,
4297 (char*)&evt_param, len, NULL);
4298 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
4299 }
4300 }
4301 }
4302 BTIF_TRACE_DEBUG("leaving %s", __func__);
4303 }
4304
4305 /*******************************************************************************
4306 *
4307 * Function btif_hl_close_socket
4308 *
4309 * Description close a socket
4310 *
4311 * Returns void
4312 *
4313 ******************************************************************************/
btif_hl_close_socket(fd_set * p_org_set)4314 void btif_hl_close_socket(fd_set* p_org_set) {
4315 BTIF_TRACE_DEBUG("entering %s", __func__);
4316 for (const list_node_t* node = list_begin(soc_queue);
4317 node != list_end(soc_queue); node = list_next(node)) {
4318 btif_hl_soc_cb_t* p_scb = (btif_hl_soc_cb_t*)list_node(node);
4319 if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_REL) {
4320 BTIF_TRACE_DEBUG("app_idx=%d mcl_id=%d, mdl_idx=%d", p_scb->app_idx,
4321 p_scb->mcl_idx, p_scb->mdl_idx);
4322 btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_IDLE);
4323 if (p_scb->socket_id[1] != -1) {
4324 FD_CLR(p_scb->socket_id[1], p_org_set);
4325 shutdown(p_scb->socket_id[1], SHUT_RDWR);
4326 close(p_scb->socket_id[1]);
4327
4328 btif_hl_evt_cb_t evt_param;
4329 evt_param.chan_cb.app_id = (int)btif_hl_get_app_id(p_scb->channel_id);
4330 memcpy(evt_param.chan_cb.bd_addr, p_scb->bd_addr, sizeof(BD_ADDR));
4331 evt_param.chan_cb.channel_id = p_scb->channel_id;
4332 evt_param.chan_cb.fd = p_scb->socket_id[0];
4333 evt_param.chan_cb.mdep_cfg_index = (int)p_scb->mdep_cfg_idx;
4334 evt_param.chan_cb.cb_state = BTIF_HL_CHAN_CB_STATE_DISCONNECTED_PENDING;
4335 int len = sizeof(btif_hl_send_chan_state_cb_t);
4336 bt_status_t status = btif_transfer_context(
4337 btif_hl_proc_cb_evt, BTIF_HL_SEND_DISCONNECTED_CB,
4338 (char*)&evt_param, len, NULL);
4339 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
4340 }
4341 }
4342 }
4343
4344 for (const list_node_t* node = list_begin(soc_queue);
4345 node != list_end(soc_queue);) {
4346 // We may mutate this list so we need keep track of
4347 // the current node and only remove items behind.
4348 btif_hl_soc_cb_t* p_scb = (btif_hl_soc_cb_t*)list_node(node);
4349 BTIF_TRACE_DEBUG("p_scb=0x%x", p_scb);
4350 node = list_next(node);
4351 if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_IDLE) {
4352 btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR(
4353 p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx);
4354 BTIF_TRACE_DEBUG(
4355 "idle socket app_idx=%d mcl_id=%d, mdl_idx=%d p_dcb->in_use=%d",
4356 p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx, p_dcb->in_use);
4357 list_remove(soc_queue, p_scb);
4358 osi_free(p_scb);
4359 p_dcb->p_scb = NULL;
4360 }
4361 }
4362 BTIF_TRACE_DEBUG("leaving %s", __func__);
4363 }
4364
4365 /*******************************************************************************
4366 *
4367 * Function btif_hl_select_wakeup_callback
4368 *
4369 * Description Select wakup callback to add or close a socket
4370 *
4371 * Returns void
4372 *
4373 ******************************************************************************/
4374
btif_hl_select_wakeup_callback(fd_set * p_org_set,int wakeup_signal)4375 void btif_hl_select_wakeup_callback(fd_set* p_org_set, int wakeup_signal) {
4376 BTIF_TRACE_DEBUG("entering %s wakeup_signal=0x%04x", __func__, wakeup_signal);
4377
4378 if (wakeup_signal == btif_hl_signal_select_wakeup) {
4379 btif_hl_add_socket_to_set(p_org_set);
4380 } else if (wakeup_signal == btif_hl_signal_select_close_connected) {
4381 btif_hl_close_socket(p_org_set);
4382 }
4383 BTIF_TRACE_DEBUG("leaving %s", __func__);
4384 }
4385
4386 /*******************************************************************************
4387 *
4388 * Function btif_hl_select_monitor_callback
4389 *
4390 * Description Select monitor callback to check pending socket actions
4391 *
4392 * Returns void
4393 *
4394 ******************************************************************************/
btif_hl_select_monitor_callback(fd_set * p_cur_set,UNUSED_ATTR fd_set * p_org_set)4395 void btif_hl_select_monitor_callback(fd_set* p_cur_set,
4396 UNUSED_ATTR fd_set* p_org_set) {
4397 BTIF_TRACE_DEBUG("entering %s", __func__);
4398
4399 for (const list_node_t* node = list_begin(soc_queue);
4400 node != list_end(soc_queue); node = list_next(node)) {
4401 btif_hl_soc_cb_t* p_scb = (btif_hl_soc_cb_t*)list_node(node);
4402 if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_READ) {
4403 if (FD_ISSET(p_scb->socket_id[1], p_cur_set)) {
4404 BTIF_TRACE_DEBUG("read data state= BTIF_HL_SOC_STATE_W4_READ");
4405 btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR(
4406 p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx);
4407 CHECK(p_dcb != NULL);
4408 if (p_dcb->p_tx_pkt) {
4409 BTIF_TRACE_ERROR(
4410 "Rcv new pkt but the last pkt is still not been"
4411 " sent tx_size=%d",
4412 p_dcb->tx_size);
4413 osi_free_and_reset((void**)&p_dcb->p_tx_pkt);
4414 }
4415 p_dcb->p_tx_pkt = (uint8_t*)osi_malloc(p_dcb->mtu);
4416 ssize_t r;
4417 OSI_NO_INTR(r = recv(p_scb->socket_id[1], p_dcb->p_tx_pkt, p_dcb->mtu,
4418 MSG_DONTWAIT));
4419 if (r > 0) {
4420 BTIF_TRACE_DEBUG("btif_hl_select_monitor_callback send data r =%d",
4421 r);
4422 p_dcb->tx_size = r;
4423 BTIF_TRACE_DEBUG(
4424 "btif_hl_select_monitor_callback send data tx_size=%d",
4425 p_dcb->tx_size);
4426 BTA_HlSendData(p_dcb->mdl_handle, p_dcb->tx_size);
4427 } else {
4428 BTIF_TRACE_DEBUG(
4429 "btif_hl_select_monitor_callback receive failed r=%d", r);
4430 BTA_HlDchClose(p_dcb->mdl_handle);
4431 }
4432 }
4433 }
4434 }
4435
4436 if (list_is_empty(soc_queue))
4437 BTIF_TRACE_DEBUG("btif_hl_select_monitor_queue is empty");
4438
4439 BTIF_TRACE_DEBUG("leaving %s", __func__);
4440 }
4441
4442 /*******************************************************************************
4443 *
4444 * Function btif_hl_select_wakeup_init
4445 *
4446 * Description select loop wakup init
4447 *
4448 * Returns int
4449 *
4450 ******************************************************************************/
btif_hl_select_wakeup_init(fd_set * set)4451 static inline int btif_hl_select_wakeup_init(fd_set* set) {
4452 BTIF_TRACE_DEBUG("%s", __func__);
4453 if (signal_fds[0] == -1 &&
4454 socketpair(AF_UNIX, SOCK_STREAM, 0, signal_fds) < 0) {
4455 BTIF_TRACE_ERROR("socketpair failed: %s", strerror(errno));
4456 return -1;
4457 }
4458
4459 BTIF_TRACE_DEBUG(
4460 "btif_hl_select_wakeup_init signal_fds[0]=%d signal_fds[1]=%d",
4461 signal_fds[0], signal_fds[1]);
4462 FD_SET(signal_fds[0], set);
4463
4464 return signal_fds[0];
4465 }
4466
4467 /*******************************************************************************
4468 *
4469 * Function btif_hl_select_wakeup
4470 *
4471 * Description send a signal to wakupo the select loop
4472 *
4473 * Returns int
4474 *
4475 ******************************************************************************/
btif_hl_select_wakeup(void)4476 static inline int btif_hl_select_wakeup(void) {
4477 char sig_on = btif_hl_signal_select_wakeup;
4478
4479 BTIF_TRACE_DEBUG("%s", __func__);
4480
4481 ssize_t ret;
4482 OSI_NO_INTR(ret = send(signal_fds[1], &sig_on, sizeof(sig_on), 0));
4483
4484 return (int)ret;
4485 }
4486
4487 /*******************************************************************************
4488 *
4489 * Function btif_hl_select_close_connected
4490 *
4491 * Description send a signal to close a socket
4492 *
4493 * Returns int
4494 *
4495 ******************************************************************************/
btif_hl_select_close_connected(void)4496 static inline int btif_hl_select_close_connected(void) {
4497 char sig_on = btif_hl_signal_select_close_connected;
4498
4499 BTIF_TRACE_DEBUG("%s", __func__);
4500
4501 ssize_t ret;
4502 OSI_NO_INTR(ret = send(signal_fds[1], &sig_on, sizeof(sig_on), 0));
4503
4504 return (int)ret;
4505 }
4506
4507 /*******************************************************************************
4508 *
4509 * Function btif_hl_close_select_thread
4510 *
4511 * Description send signal to close the thread and then close all signal FDs
4512 *
4513 * Returns int
4514 *
4515 ******************************************************************************/
btif_hl_close_select_thread(void)4516 static inline int btif_hl_close_select_thread(void) {
4517 ssize_t result = 0;
4518 char sig_on = btif_hl_signal_select_exit;
4519
4520 BTIF_TRACE_DEBUG("%", __func__);
4521
4522 OSI_NO_INTR(result = send(signal_fds[1], &sig_on, sizeof(sig_on), 0));
4523
4524 if (btif_is_enabled()) {
4525 /* Wait for the select_thread_id to exit if BT is still enabled
4526 and only this profile getting cleaned up*/
4527 if (select_thread_id != -1) {
4528 pthread_join(select_thread_id, NULL);
4529 select_thread_id = -1;
4530 }
4531 }
4532 list_free(soc_queue);
4533 soc_queue = NULL;
4534
4535 return (int)result;
4536 }
4537
4538 /*******************************************************************************
4539 *
4540 * Function btif_hl_select_wake_reset
4541 *
4542 * Description clear the received signal for the select loop
4543 *
4544 * Returns int
4545 *
4546 ******************************************************************************/
btif_hl_select_wake_reset(void)4547 static inline int btif_hl_select_wake_reset(void) {
4548 char sig_recv = 0;
4549
4550 BTIF_TRACE_DEBUG("%s", __func__);
4551
4552 ssize_t r;
4553 OSI_NO_INTR(
4554 r = recv(signal_fds[0], &sig_recv, sizeof(sig_recv), MSG_WAITALL));
4555
4556 return (int)sig_recv;
4557 }
4558 /*******************************************************************************
4559 *
4560 * Function btif_hl_select_wake_signaled
4561 *
4562 * Description check whether a fd is set or not
4563 *
4564 * Returns int
4565 *
4566 ******************************************************************************/
btif_hl_select_wake_signaled(fd_set * set)4567 static inline int btif_hl_select_wake_signaled(fd_set* set) {
4568 BTIF_TRACE_DEBUG("btif_hl_select_wake_signaled");
4569 return FD_ISSET(signal_fds[0], set);
4570 }
4571 /*******************************************************************************
4572 *
4573 * Function btif_hl_thread_cleanup
4574 *
4575 * Description shut down and clean up the select loop
4576 *
4577 * Returns void
4578 *
4579 ******************************************************************************/
btif_hl_thread_cleanup()4580 static void btif_hl_thread_cleanup() {
4581 if (listen_s != -1) close(listen_s);
4582 if (connected_s != -1) {
4583 shutdown(connected_s, SHUT_RDWR);
4584 close(connected_s);
4585 }
4586 listen_s = connected_s = -1;
4587 BTIF_TRACE_DEBUG("hl thread cleanup");
4588 }
4589 /*******************************************************************************
4590 *
4591 * Function btif_hl_select_thread
4592 *
4593 * Description the select loop
4594 *
4595 * Returns void
4596 *
4597 ******************************************************************************/
btif_hl_select_thread(UNUSED_ATTR void * arg)4598 static void* btif_hl_select_thread(UNUSED_ATTR void* arg) {
4599 fd_set org_set, curr_set;
4600 int r, max_curr_s, max_org_s;
4601
4602 BTIF_TRACE_DEBUG("entered btif_hl_select_thread");
4603 FD_ZERO(&org_set);
4604 max_org_s = btif_hl_select_wakeup_init(&org_set);
4605 BTIF_TRACE_DEBUG("max_s=%d ", max_org_s);
4606
4607 for (;;) {
4608 r = 0;
4609 BTIF_TRACE_DEBUG("set curr_set = org_set ");
4610 curr_set = org_set;
4611 max_curr_s = max_org_s;
4612 int ret = select((max_curr_s + 1), &curr_set, NULL, NULL, NULL);
4613 BTIF_TRACE_DEBUG("select unblocked ret=%d", ret);
4614 if (ret == -1) {
4615 if (errno == EINTR) continue;
4616 BTIF_TRACE_DEBUG("select() ret -1, exit the thread");
4617 btif_hl_thread_cleanup();
4618 select_thread_id = -1;
4619 return 0;
4620 } else if (ret) {
4621 BTIF_TRACE_DEBUG("btif_hl_select_wake_signaled, signal ret=%d", ret);
4622 if (btif_hl_select_wake_signaled(&curr_set)) {
4623 r = btif_hl_select_wake_reset();
4624 BTIF_TRACE_DEBUG("btif_hl_select_wake_signaled, signal:%d", r);
4625 if (r == btif_hl_signal_select_wakeup ||
4626 r == btif_hl_signal_select_close_connected) {
4627 btif_hl_select_wakeup_callback(&org_set, r);
4628 } else if (r == btif_hl_signal_select_exit) {
4629 btif_hl_thread_cleanup();
4630 BTIF_TRACE_DEBUG(
4631 "Exit hl_select_thread for btif_hl_signal_select_exit");
4632 return 0;
4633 }
4634 }
4635
4636 btif_hl_select_monitor_callback(&curr_set, &org_set);
4637 max_org_s = btif_hl_update_maxfd(max_org_s);
4638 } else
4639 BTIF_TRACE_DEBUG("no data, select ret: %d\n", ret);
4640 }
4641 BTIF_TRACE_DEBUG("leaving hl_select_thread");
4642 return 0;
4643 }
4644
4645 /*******************************************************************************
4646 *
4647 * Function create_thread
4648 *
4649 * Description creat a select loop
4650 *
4651 * Returns pthread_t
4652 *
4653 ******************************************************************************/
create_thread(void * (* start_routine)(void *),void * arg)4654 static inline pthread_t create_thread(void* (*start_routine)(void*),
4655 void* arg) {
4656 BTIF_TRACE_DEBUG("create_thread: entered");
4657 pthread_attr_t thread_attr;
4658
4659 pthread_attr_init(&thread_attr);
4660 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
4661 pthread_t thread_id = -1;
4662 if (pthread_create(&thread_id, &thread_attr, start_routine, arg) != 0) {
4663 BTIF_TRACE_ERROR("pthread_create : %s", strerror(errno));
4664 return -1;
4665 }
4666 BTIF_TRACE_DEBUG("create_thread: thread created successfully");
4667 return thread_id;
4668 }
4669
4670 /*******************************************************************************
4671 *
4672 * Function btif_hl_soc_thread_init
4673 *
4674 * Description HL select loop init function.
4675 *
4676 * Returns void
4677 *
4678 ******************************************************************************/
btif_hl_soc_thread_init(void)4679 void btif_hl_soc_thread_init(void) {
4680 BTIF_TRACE_DEBUG("%s", __func__);
4681 soc_queue = list_new(NULL);
4682 if (soc_queue == NULL)
4683 LOG_ERROR(LOG_TAG, "%s unable to allocate resources for thread", __func__);
4684 select_thread_id = create_thread(btif_hl_select_thread, NULL);
4685 }
4686 /*******************************************************************************
4687 *
4688 * Function btif_hl_load_mdl_config
4689 *
4690 * Description load the MDL configuation from the application control block
4691 *
4692 * Returns bool
4693 *
4694 ******************************************************************************/
btif_hl_load_mdl_config(uint8_t app_id,uint8_t buffer_size,tBTA_HL_MDL_CFG * p_mdl_buf)4695 bool btif_hl_load_mdl_config(uint8_t app_id, uint8_t buffer_size,
4696 tBTA_HL_MDL_CFG* p_mdl_buf) {
4697 uint8_t app_idx;
4698 bool result = false;
4699 btif_hl_app_cb_t* p_acb;
4700 tBTA_HL_MDL_CFG* p;
4701 int i;
4702 BTIF_TRACE_DEBUG("%s", __func__);
4703
4704 if (btif_hl_find_app_idx(app_id, &app_idx)) {
4705 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
4706 for (i = 0, p = p_mdl_buf; i < buffer_size; i++, p++) {
4707 memcpy(p, &p_acb->mdl_cfg[i].base, sizeof(tBTA_HL_MDL_CFG));
4708 }
4709 result = true;
4710 }
4711
4712 BTIF_TRACE_DEBUG("result=%d", result);
4713 return result;
4714 }
4715