1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/******************************************************************************
*
* Copyright 2002-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This AVDTP adaption layer module interfaces to L2CAP
*
******************************************************************************/
#include "avdt_int.h"
#include "bt_target.h"
#include "bta/include/bta_av_api.h"
#include "btm_api.h"
#include "device/include/interop.h"
#include "l2c_api.h"
#include "l2cdefs.h"
#include "osi/include/allocator.h"
#include "osi/include/osi.h"
#include "stack/include/acl_api.h"
#include "stack/include/bt_hdr.h"
#include "types/raw_address.h"
#include <base/logging.h>
/* callback function declarations */
void avdt_l2c_connect_ind_cback(const RawAddress& bd_addr, uint16_t lcid,
uint16_t psm, uint8_t id);
void avdt_l2c_connect_cfm_cback(uint16_t lcid, uint16_t result);
void avdt_l2c_config_cfm_cback(uint16_t lcid, uint16_t result,
tL2CAP_CFG_INFO* p_cfg);
void avdt_l2c_config_ind_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg);
void avdt_l2c_disconnect_ind_cback(uint16_t lcid, bool ack_needed);
void avdt_l2c_congestion_ind_cback(uint16_t lcid, bool is_congested);
void avdt_l2c_data_ind_cback(uint16_t lcid, BT_HDR* p_buf);
static void avdt_on_l2cap_error(uint16_t lcid, uint16_t result);
/* L2CAP callback function structure */
const tL2CAP_APPL_INFO avdt_l2c_appl = {avdt_l2c_connect_ind_cback,
avdt_l2c_connect_cfm_cback,
avdt_l2c_config_ind_cback,
avdt_l2c_config_cfm_cback,
avdt_l2c_disconnect_ind_cback,
NULL,
avdt_l2c_data_ind_cback,
avdt_l2c_congestion_ind_cback,
NULL,
avdt_on_l2cap_error,
NULL,
NULL,
NULL};
/*******************************************************************************
*
* Function avdt_sec_check_complete_term
*
* Description The function called when Security Manager finishes
* verification of the service side connection
*
* Returns void
*
******************************************************************************/
static void avdt_sec_check_complete_term(const RawAddress* bd_addr,
tBT_TRANSPORT transport,
void* p_ref_data) {
AvdtpCcb* p_ccb = NULL;
AvdtpTransportChannel* p_tbl;
p_ccb = avdt_ccb_by_bd(*bd_addr);
p_tbl = avdt_ad_tc_tbl_by_st(AVDT_CHAN_SIG, p_ccb, AVDT_AD_ST_SEC_ACP);
if (p_tbl == NULL) return;
/* store idx in LCID table, store LCID in routing table */
avdtp_cb.ad.lcid_tbl[p_tbl->lcid] = avdt_ad_tc_tbl_to_idx(p_tbl);
avdtp_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][p_tbl->tcid].lcid = p_tbl->lcid;
/* transition to configuration state */
p_tbl->state = AVDT_AD_ST_CFG;
}
/*******************************************************************************
*
* Function avdt_sec_check_complete_orig
*
* Description The function called when Security Manager finishes
* verification of the service side connection
*
* Returns void
*
******************************************************************************/
static void avdt_sec_check_complete_orig(const RawAddress* bd_addr,
tBT_TRANSPORT trasnport,
UNUSED_ATTR void* p_ref_data,
uint8_t res) {
AvdtpCcb* p_ccb = NULL;
AvdtpTransportChannel* p_tbl;
AVDT_TRACE_DEBUG("avdt_sec_check_complete_orig res: %d", res);
if (bd_addr) p_ccb = avdt_ccb_by_bd(*bd_addr);
p_tbl = avdt_ad_tc_tbl_by_st(AVDT_CHAN_SIG, p_ccb, AVDT_AD_ST_SEC_INT);
if (p_tbl == NULL) return;
if (res == BTM_SUCCESS) {
/* set channel state */
p_tbl->state = AVDT_AD_ST_CFG;
} else {
avdt_l2c_disconnect(p_tbl->lcid);
avdt_ad_tc_close_ind(p_tbl);
}
}
/*******************************************************************************
*
* Function avdt_l2c_connect_ind_cback
*
* Description This is the L2CAP connect indication callback function.
*
*
* Returns void
*
******************************************************************************/
void avdt_l2c_connect_ind_cback(const RawAddress& bd_addr, uint16_t lcid,
UNUSED_ATTR uint16_t psm, uint8_t id) {
AvdtpCcb* p_ccb;
AvdtpTransportChannel* p_tbl = NULL;
uint16_t result;
/* do we already have a control channel for this peer? */
p_ccb = avdt_ccb_by_bd(bd_addr);
if (p_ccb == NULL) {
/* no, allocate ccb */
int channel_index = BTA_AvObtainPeerChannelIndex(bd_addr);
if (channel_index >= 0) {
p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
}
if (p_ccb == nullptr) {
p_ccb = avdt_ccb_alloc(bd_addr);
}
if (p_ccb == NULL) {
/* no ccb available, reject L2CAP connection */
result = L2CAP_CONN_NO_RESOURCES;
} else {
/* allocate and set up entry; first channel is always signaling */
p_tbl = avdt_ad_tc_tbl_alloc(p_ccb);
p_tbl->my_mtu = kAvdtpMtu;
p_tbl->tcid = AVDT_CHAN_SIG;
p_tbl->lcid = lcid;
p_tbl->state = AVDT_AD_ST_SEC_ACP;
p_tbl->cfg_flags = AVDT_L2C_CFG_CONN_ACP;
if (interop_match_addr(INTEROP_2MBPS_LINK_ONLY, &bd_addr)) {
// Disable 3DH packets for AVDT ACL to improve sensitivity on HS
btm_set_packet_types_from_address(
bd_addr,
(acl_get_supported_packet_types() | HCI_PKT_TYPES_MASK_NO_3_DH1 |
HCI_PKT_TYPES_MASK_NO_3_DH3 | HCI_PKT_TYPES_MASK_NO_3_DH5));
}
/* Assume security check is complete */
avdt_sec_check_complete_term(&p_ccb->peer_addr, BT_TRANSPORT_BR_EDR,
nullptr);
return;
}
} else {
/* deal with simultaneous control channel connect case */
p_tbl = avdt_ad_tc_tbl_by_st(AVDT_CHAN_SIG, p_ccb, AVDT_AD_ST_CONN);
if (p_tbl != NULL) {
/* reject their connection */
result = L2CAP_CONN_NO_RESOURCES;
} else {
/* This must be a traffic channel; are we accepting a traffic channel
* for this ccb?
*/
p_tbl = avdt_ad_tc_tbl_by_st(AVDT_CHAN_MEDIA, p_ccb, AVDT_AD_ST_ACP);
if (p_tbl != NULL) {
/* yes; proceed with connection */
result = L2CAP_CONN_OK;
} else {
/* this must be a reporting channel; are we accepting a reporting
* channel for this ccb?
*/
p_tbl = avdt_ad_tc_tbl_by_st(AVDT_CHAN_REPORT, p_ccb, AVDT_AD_ST_ACP);
if (p_tbl != NULL) {
/* yes; proceed with connection */
result = L2CAP_CONN_OK;
} else {
/* else we're not listening for traffic channel; reject */
result = L2CAP_CONN_NO_PSM;
}
}
}
}
/* If we reject the connection, send DisconnectReq */
if (result != L2CAP_CONN_OK) {
L2CA_DisconnectReq(lcid);
return;
}
/* if result ok, proceed with connection */
/* store idx in LCID table, store LCID in routing table */
avdtp_cb.ad.lcid_tbl[lcid] = avdt_ad_tc_tbl_to_idx(p_tbl);
avdtp_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][p_tbl->tcid].lcid = lcid;
/* transition to configuration state */
p_tbl->state = AVDT_AD_ST_CFG;
}
static void avdt_on_l2cap_error(uint16_t lcid, uint16_t result) {
avdt_l2c_disconnect(lcid);
}
/*******************************************************************************
*
* Function avdt_l2c_connect_cfm_cback
*
* Description This is the L2CAP connect confirm callback function.
*
*
* Returns void
*
******************************************************************************/
void avdt_l2c_connect_cfm_cback(uint16_t lcid, uint16_t result) {
AvdtpTransportChannel* p_tbl;
AvdtpCcb* p_ccb;
AVDT_TRACE_DEBUG("avdt_l2c_connect_cfm_cback lcid: %d, result: %d", lcid,
result);
/* look up info for this channel */
p_tbl = avdt_ad_tc_tbl_by_lcid(lcid);
if (p_tbl != NULL) {
/* if in correct state */
if (p_tbl->state == AVDT_AD_ST_CONN) {
/* if result successful */
if (result == L2CAP_CONN_OK) {
if (p_tbl->tcid != AVDT_CHAN_SIG) {
/* set channel state */
p_tbl->state = AVDT_AD_ST_CFG;
} else {
p_ccb = avdt_ccb_by_idx(p_tbl->ccb_idx);
if (p_ccb == NULL) {
result = L2CAP_CONN_NO_RESOURCES;
} else {
/* set channel state */
p_tbl->state = AVDT_AD_ST_SEC_INT;
p_tbl->lcid = lcid;
p_tbl->cfg_flags = AVDT_L2C_CFG_CONN_INT;
if (interop_match_addr(INTEROP_2MBPS_LINK_ONLY,
(const RawAddress*)&p_ccb->peer_addr)) {
// Disable 3DH packets for AVDT ACL to improve sensitivity on HS
btm_set_packet_types_from_address(
p_ccb->peer_addr,
(acl_get_supported_packet_types() |
HCI_PKT_TYPES_MASK_NO_3_DH1 | HCI_PKT_TYPES_MASK_NO_3_DH3 |
HCI_PKT_TYPES_MASK_NO_3_DH5));
}
/* Assume security check is complete */
avdt_sec_check_complete_orig(&p_ccb->peer_addr, BT_TRANSPORT_BR_EDR,
nullptr, BTM_SUCCESS);
}
}
}
/* failure; notify adaption that channel closed */
if (result != L2CAP_CONN_OK) {
LOG(ERROR) << __func__ << ": invoked with non OK status";
}
}
}
}
/*******************************************************************************
*
* Function avdt_l2c_config_cfm_cback
*
* Description This is the L2CAP config confirm callback function.
*
*
* Returns void
*
******************************************************************************/
void avdt_l2c_config_cfm_cback(uint16_t lcid, uint16_t initiator,
tL2CAP_CFG_INFO* p_cfg) {
avdt_l2c_config_ind_cback(lcid, p_cfg);
AvdtpTransportChannel* p_tbl;
AVDT_TRACE_DEBUG("%s: lcid: %d", __func__, lcid);
/* look up info for this channel */
p_tbl = avdt_ad_tc_tbl_by_lcid(lcid);
if (p_tbl != NULL) {
p_tbl->lcid = lcid;
/* if in correct state */
if (p_tbl->state == AVDT_AD_ST_CFG) {
avdt_ad_tc_open_ind(p_tbl);
}
}
}
/*******************************************************************************
*
* Function avdt_l2c_config_ind_cback
*
* Description This is the L2CAP config indication callback function.
*
*
* Returns void
*
******************************************************************************/
void avdt_l2c_config_ind_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg) {
AvdtpTransportChannel* p_tbl;
AVDT_TRACE_DEBUG("%s: lcid: %d", __func__, lcid);
/* look up info for this channel */
p_tbl = avdt_ad_tc_tbl_by_lcid(lcid);
if (p_tbl != NULL) {
/* store the mtu in tbl */
if (p_cfg->mtu_present) {
p_tbl->peer_mtu = p_cfg->mtu;
} else {
p_tbl->peer_mtu = L2CAP_DEFAULT_MTU;
}
AVDT_TRACE_DEBUG("%s: peer_mtu: %d, lcid: %d", __func__, p_tbl->peer_mtu,
lcid);
}
}
/*******************************************************************************
*
* Function avdt_l2c_disconnect_ind_cback
*
* Description This is the L2CAP disconnect indication callback function.
*
*
* Returns void
*
******************************************************************************/
void avdt_l2c_disconnect_ind_cback(uint16_t lcid, bool ack_needed) {
AvdtpTransportChannel* p_tbl;
AVDT_TRACE_DEBUG("avdt_l2c_disconnect_ind_cback lcid: %d, ack_needed: %d",
lcid, ack_needed);
/* look up info for this channel */
p_tbl = avdt_ad_tc_tbl_by_lcid(lcid);
if (p_tbl != NULL) {
avdt_ad_tc_close_ind(p_tbl);
}
}
void avdt_l2c_disconnect(uint16_t lcid) {
L2CA_DisconnectReq(lcid);
AvdtpTransportChannel* p_tbl;
AVDT_TRACE_DEBUG("avdt_l2c_disconnect_cfm_cback lcid: %d", lcid);
/* look up info for this channel */
p_tbl = avdt_ad_tc_tbl_by_lcid(lcid);
if (p_tbl != NULL) {
avdt_ad_tc_close_ind(p_tbl);
}
}
/*******************************************************************************
*
* Function avdt_l2c_congestion_ind_cback
*
* Description This is the L2CAP congestion indication callback function.
*
*
* Returns void
*
******************************************************************************/
void avdt_l2c_congestion_ind_cback(uint16_t lcid, bool is_congested) {
AvdtpTransportChannel* p_tbl;
/* look up info for this channel */
p_tbl = avdt_ad_tc_tbl_by_lcid(lcid);
if (p_tbl != NULL) {
avdt_ad_tc_cong_ind(p_tbl, is_congested);
}
}
/*******************************************************************************
*
* Function avdt_l2c_data_ind_cback
*
* Description This is the L2CAP data indication callback function.
*
*
* Returns void
*
******************************************************************************/
void avdt_l2c_data_ind_cback(uint16_t lcid, BT_HDR* p_buf) {
AvdtpTransportChannel* p_tbl;
/* look up info for this channel */
p_tbl = avdt_ad_tc_tbl_by_lcid(lcid);
if (p_tbl != NULL) {
avdt_ad_tc_data_ind(p_tbl, p_buf);
} else /* prevent buffer leak */
osi_free(p_buf);
}
|