1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
24
25
33
34
35
36
37
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
72
73
82
83
84
85
88
89
93
94
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
125
126
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
189
190
191
192
193
194
195
196
197
198
199
200
201
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
254
255
256
257
258
259
260
261
262
263
264
265
266
267
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
347
348
349
350
351
352
353
354
355
356
357
358
359
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
392
393
394
395
396
397
398
399
400
401
402
403
404
410
411
412
413
414
415
416
417
418
419
420
421
422
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
460
461
462
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
524
525
526
527
528
529
530
531
532
533
534
535
541
542
543
544
545
546
547
548
549
550
551
554
555
556
557
558
559
560
561
562
563
564
567
570
571
574
575
576
581
582
583
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
637
638
639
640
643
644
645
646
647
648
649
650
651
657
658
659
660
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/* ... */
/* ... */
#include <string.h>
#include "stack/bt_types.h"
#include "common/bt_target.h"
#include "common/bt_defs.h"
#include "stack/avct_api.h"
#include "avct_int.h"
#include "stack/btm_api.h"
#include "osi/allocator.h"8 includes
#if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE)
const UINT8 avct_lcb_pkt_type_len[] = {
AVCT_HDR_LEN_SINGLE,
AVCT_HDR_LEN_START,
AVCT_HDR_LEN_CONT,
AVCT_HDR_LEN_END
}{...};
/* ... */
static BT_HDR *avct_lcb_msg_asmbl(tAVCT_LCB *p_lcb, BT_HDR *p_buf)
{
UINT8 *p;
UINT8 pkt_type;
BT_HDR *p_ret;
UINT16 buf_len;
p = (UINT8 *)(p_buf + 1) + p_buf->offset;
AVCT_PRS_PKT_TYPE(p, pkt_type);
if (p_buf->len < avct_lcb_pkt_type_len[pkt_type]) {
osi_free(p_buf);
AVCT_TRACE_WARNING("Bad length during reassembly");
p_ret = NULL;
}{...}
else if (pkt_type == AVCT_PKT_TYPE_SINGLE) {
if (p_lcb->p_rx_msg != NULL) {
AVCT_TRACE_WARNING("Got single during reassembly");
}{...}
osi_free(p_lcb->p_rx_msg);
p_lcb->p_rx_msg = NULL;
p_ret = p_buf;
}{...}
else if (pkt_type == AVCT_PKT_TYPE_START) {
if (p_lcb->p_rx_msg != NULL) {
AVCT_TRACE_WARNING("Got start during reassembly");
}{...}
osi_free(p_lcb->p_rx_msg);
/* ... */
p_lcb->p_rx_msg = (BT_HDR *)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
if (p_lcb->p_rx_msg == NULL) {
AVCT_TRACE_ERROR ("Cannot alloc buffer for reassembly !!");
osi_free(p_buf);
}{...} else {
memcpy (p_lcb->p_rx_msg, p_buf,
sizeof(BT_HDR) + p_buf->offset + p_buf->len);
osi_free(p_buf);
p = (UINT8 *)(p_lcb->p_rx_msg + 1) + p_lcb->p_rx_msg->offset;
*(p + 1) = *p;
p_lcb->p_rx_msg->offset += p_lcb->p_rx_msg->len;
p_lcb->p_rx_msg->len -= 1;
}{...}
p_ret = NULL;
}{...}
else {
if (p_lcb->p_rx_msg == NULL) {
osi_free(p_buf);
AVCT_TRACE_WARNING("Pkt type=%d out of order", pkt_type);
p_ret = NULL;
}{...} else {
/* ... */
buf_len = BT_DEFAULT_BUFFER_SIZE - sizeof(BT_HDR);
p_buf->offset += AVCT_HDR_LEN_CONT;
p_buf->len -= AVCT_HDR_LEN_CONT;
if ((p_lcb->p_rx_msg->offset + p_buf->len) > buf_len) {
AVCT_TRACE_WARNING("%s: Fragmented message too big!", __func__);
osi_free(p_lcb->p_rx_msg);
p_lcb->p_rx_msg = NULL;
osi_free(p_buf);
p_ret = NULL;
}{...} else {
memcpy((UINT8 *)(p_lcb->p_rx_msg + 1) + p_lcb->p_rx_msg->offset,
(UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
if (pkt_type == AVCT_PKT_TYPE_END) {
p_lcb->p_rx_msg->offset -= p_lcb->p_rx_msg->len;
p_lcb->p_rx_msg->len += p_buf->len;
p_ret = p_lcb->p_rx_msg;
p_lcb->p_rx_msg = NULL;
}{...} else {
p_lcb->p_rx_msg->offset += p_buf->len;
p_lcb->p_rx_msg->len += p_buf->len;
p_ret = NULL;
}{...}
osi_free(p_buf);
}{...}
}{...}
}{...}
return p_ret;
}{...}
/* ... */
void avct_lcb_chnl_open(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
UINT16 result = AVCT_RESULT_FAIL;
UNUSED(p_data);
BTM_SetOutService(p_lcb->peer_addr, BTM_SEC_SERVICE_AVCTP, 0);
p_lcb->ch_state = AVCT_CH_CONN;
if ((p_lcb->ch_lcid = L2CA_ConnectReq(AVCT_PSM, p_lcb->peer_addr)) == 0) {
avct_lcb_event(p_lcb, AVCT_LCB_LL_CLOSE_EVT, (tAVCT_LCB_EVT *) &result);
}{...}
}{...}
/* ... */
void avct_lcb_unbind_disc(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
UNUSED(p_lcb);
avct_ccb_dealloc(p_data->p_ccb, AVCT_DISCONNECT_CFM_EVT, 0, NULL);
}{...}
/* ... */
void avct_lcb_open_ind(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
tAVCT_CCB *p_ccb = &avct_cb.ccb[0];
int i;
BOOLEAN bind = FALSE;
for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
if (p_ccb->allocated) {
if (p_ccb->p_lcb == p_lcb) {
bind = TRUE;
L2CA_SetTxPriority(p_lcb->ch_lcid, L2CAP_CHNL_PRIORITY_HIGH);
p_ccb->cc.p_ctrl_cback(avct_ccb_to_idx(p_ccb), AVCT_CONNECT_CFM_EVT,
0, p_lcb->peer_addr);
}{...}
else if ((p_ccb->p_lcb == NULL) && (p_ccb->cc.role == AVCT_ACP) &&
(avct_lcb_has_pid(p_lcb, p_ccb->cc.pid) == NULL)) {
bind = TRUE;
p_ccb->p_lcb = p_lcb;
L2CA_SetTxPriority(p_lcb->ch_lcid, L2CAP_CHNL_PRIORITY_HIGH);
p_ccb->cc.p_ctrl_cback(avct_ccb_to_idx(p_ccb), AVCT_CONNECT_IND_EVT,
0, p_lcb->peer_addr);
}{...}
}{...}
}{...}
if (bind == FALSE) {
avct_lcb_event(p_lcb, AVCT_LCB_INT_CLOSE_EVT, p_data);
}{...}
}{...}
/* ... */
void avct_lcb_open_fail(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
tAVCT_CCB *p_ccb = &avct_cb.ccb[0];
int i;
for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
if (p_ccb->allocated && (p_ccb->p_lcb == p_lcb)) {
avct_ccb_dealloc(p_ccb, AVCT_CONNECT_CFM_EVT,
p_data->result, p_lcb->peer_addr);
}{...}
}{...}
}{...}
/* ... */
void avct_lcb_close_ind(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
tAVCT_CCB *p_ccb = &avct_cb.ccb[0];
int i;
UNUSED(p_data);
for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
if (p_ccb->allocated && (p_ccb->p_lcb == p_lcb)) {
if (p_ccb->cc.role == AVCT_INT) {
avct_ccb_dealloc(p_ccb, AVCT_DISCONNECT_IND_EVT,
0, p_lcb->peer_addr);
}{...} else {
p_ccb->p_lcb = NULL;
(*p_ccb->cc.p_ctrl_cback)(avct_ccb_to_idx(p_ccb), AVCT_DISCONNECT_IND_EVT,
0, p_lcb->peer_addr);
}{...}
}{...}
}{...}
}{...}
/* ... */
void avct_lcb_close_cfm(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
tAVCT_CCB *p_ccb = &avct_cb.ccb[0];
int i;
UINT8 event;
for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
if (p_ccb->allocated && (p_ccb->p_lcb == p_lcb)) {
if (p_ccb->ch_close) {
p_ccb->ch_close = FALSE;
event = AVCT_DISCONNECT_CFM_EVT;
}{...} else {
event = AVCT_DISCONNECT_IND_EVT;
}{...}
if (p_ccb->cc.role == AVCT_INT) {
avct_ccb_dealloc(p_ccb, event, p_data->result, p_lcb->peer_addr);
}{...} else {
p_ccb->p_lcb = NULL;
(*p_ccb->cc.p_ctrl_cback)(avct_ccb_to_idx(p_ccb), event,
p_data->result, p_lcb->peer_addr);
}{...}
}{...}
}{...}
}{...}
/* ... */
void avct_lcb_bind_conn(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
p_data->p_ccb->p_lcb = p_lcb;
(*p_data->p_ccb->cc.p_ctrl_cback)(avct_ccb_to_idx(p_data->p_ccb),
AVCT_CONNECT_CFM_EVT, 0, p_lcb->peer_addr);
}{...}
/* ... */
void avct_lcb_chk_disc(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
AVCT_TRACE_EVENT("avct_lcb_chk_disc");
#if (AVCT_BROWSE_INCLUDED == TRUE)
avct_close_bcb(p_lcb, p_data);
#endif
if (avct_lcb_last_ccb(p_lcb, p_data->p_ccb)) {
AVCT_TRACE_EVENT("closing");
p_data->p_ccb->ch_close = TRUE;
avct_lcb_event(p_lcb, AVCT_LCB_INT_CLOSE_EVT, p_data);
}{...} else {
AVCT_TRACE_EVENT("dealloc ccb");
avct_lcb_unbind_disc(p_lcb, p_data);
}{...}
}{...}
/* ... */
void avct_lcb_chnl_disc(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
UNUSED(p_data);
L2CA_DisconnectReq(p_lcb->ch_lcid);
}{...}
/* ... */
void avct_lcb_bind_fail(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
UNUSED(p_lcb);
avct_ccb_dealloc(p_data->p_ccb, AVCT_CONNECT_CFM_EVT, AVCT_RESULT_FAIL, NULL);
}{...}
/* ... */
void avct_lcb_cong_ind(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
tAVCT_CCB *p_ccb = &avct_cb.ccb[0];
int i;
UINT8 event;
BT_HDR *p_buf;
event = (p_data->cong) ? AVCT_CONG_IND_EVT : AVCT_UNCONG_IND_EVT;
p_lcb->cong = p_data->cong;
if (p_lcb->cong == FALSE && !fixed_queue_is_empty(p_lcb->tx_q))
{
while (!p_lcb->cong &&
(p_buf = (BT_HDR *)fixed_queue_dequeue(p_lcb->tx_q, 0)) != NULL)
{
if (L2CA_DataWrite(p_lcb->ch_lcid, p_buf) == L2CAP_DW_CONGESTED)
{
p_lcb->cong = TRUE;
}{...}
}{...}
}{...}
for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
if (p_ccb->allocated && (p_ccb->p_lcb == p_lcb)) {
(*p_ccb->cc.p_ctrl_cback)(avct_ccb_to_idx(p_ccb), event, 0, p_lcb->peer_addr);
}{...}
}{...}
}{...}
/* ... */
void avct_lcb_discard_msg(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
UNUSED(p_lcb);
AVCT_TRACE_WARNING("Dropping msg");
osi_free(p_data->ul_msg.p_buf);
p_data->ul_msg.p_buf = NULL;
}{...}
/* ... */
void avct_lcb_send_msg(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
UINT16 curr_msg_len;
UINT8 pkt_type;
UINT8 hdr_len;
UINT8 *p;
UINT8 nosp = 0;
UINT16 temp;
UINT16 buf_size = p_lcb->peer_mtu + L2CAP_MIN_OFFSET + BT_HDR_SIZE;
curr_msg_len = p_data->ul_msg.p_buf->len;
if (curr_msg_len <= (p_lcb->peer_mtu - AVCT_HDR_LEN_SINGLE)) {
pkt_type = AVCT_PKT_TYPE_SINGLE;
}{...} else {
pkt_type = AVCT_PKT_TYPE_START;
temp = (curr_msg_len + AVCT_HDR_LEN_START - p_lcb->peer_mtu);
nosp = temp / (p_lcb->peer_mtu - 1) + 1;
if ( (temp % (p_lcb->peer_mtu - 1)) != 0) {
nosp++;
}{...}
}{...}
while (curr_msg_len != 0) {
BT_HDR *p_buf;
hdr_len = avct_lcb_pkt_type_len[pkt_type];
if (p_data->ul_msg.p_buf->len > (p_lcb->peer_mtu - hdr_len)) {
if ((p_buf = (BT_HDR *) osi_malloc(buf_size)) == NULL) {
AVCT_TRACE_ERROR ("avct_lcb_send_msg cannot alloc buffer!!");
osi_free(p_data->ul_msg.p_buf);
break;
}{...}
p_buf->offset = L2CAP_MIN_OFFSET + hdr_len;
p_buf->len = p_lcb->peer_mtu - hdr_len;
memcpy((UINT8 *)(p_buf + 1) + p_buf->offset,
(UINT8 *)(p_data->ul_msg.p_buf + 1) + p_data->ul_msg.p_buf->offset, p_buf->len);
p_data->ul_msg.p_buf->offset += p_buf->len;
p_data->ul_msg.p_buf->len -= p_buf->len;
}{...} else {
p_buf = p_data->ul_msg.p_buf;
}{...}
curr_msg_len -= p_buf->len;
p_buf->len += hdr_len;
p_buf->offset -= hdr_len;
p = (UINT8 *)(p_buf + 1) + p_buf->offset;
AVCT_BLD_HDR(p, p_data->ul_msg.label, pkt_type, p_data->ul_msg.cr);
if (pkt_type == AVCT_PKT_TYPE_START) {
UINT8_TO_STREAM(p, nosp);
}{...}
if ((pkt_type == AVCT_PKT_TYPE_START) || (pkt_type == AVCT_PKT_TYPE_SINGLE)) {
UINT16_TO_BE_STREAM(p, p_data->ul_msg.p_ccb->cc.pid);
}{...}
if (p_lcb->cong == TRUE) {
fixed_queue_enqueue(p_lcb->tx_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
}{...}
else {
if (L2CA_DataWrite(p_lcb->ch_lcid, p_buf) == L2CAP_DW_CONGESTED) {
p_lcb->cong = TRUE;
}{...}
}{...}
if (curr_msg_len > (p_lcb->peer_mtu - AVCT_HDR_LEN_END)) {
pkt_type = AVCT_PKT_TYPE_CONT;
}{...} else {
pkt_type = AVCT_PKT_TYPE_END;
}{...}
}{...}
AVCT_TRACE_DEBUG ("avct_lcb_send_msg tx_q_count:%d",
fixed_queue_length(p_lcb->tx_q));
return;
}{...}
/* ... */
void avct_lcb_free_msg_ind(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
UNUSED(p_lcb);
if (p_data == NULL) {
return;
}{...}
osi_free(p_data->p_buf);
p_data->p_buf = NULL;
}{...}
/* ... */
void avct_lcb_msg_ind(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
UINT8 *p;
UINT8 label, type, cr_ipid;
UINT16 pid;
tAVCT_CCB *p_ccb;
BT_HDR *p_buf;
/* ... */
p_data->p_buf->layer_specific = AVCT_DATA_CTRL;
if ((p_data->p_buf = avct_lcb_msg_asmbl(p_lcb, p_data->p_buf)) == NULL) {
return;
}{...}
p = (UINT8 *)(p_data->p_buf + 1) + p_data->p_buf->offset;
AVCT_PRS_HDR(p, label, type, cr_ipid);
UNUSED(type);
if (cr_ipid == AVCT_CR_IPID_INVALID) {
AVCT_TRACE_WARNING("Invalid cr_ipid %d", cr_ipid);
osi_free(p_data->p_buf);
p_data->p_buf = NULL;
return;
}{...}
BE_STREAM_TO_UINT16(pid, p);
if ((p_ccb = avct_lcb_has_pid(p_lcb, pid)) != NULL) {
p_data->p_buf->offset += AVCT_HDR_LEN_SINGLE;
p_data->p_buf->len -= AVCT_HDR_LEN_SINGLE;
(*p_ccb->cc.p_msg_cback)(avct_ccb_to_idx(p_ccb), label, cr_ipid, p_data->p_buf);
}{...} else {
AVCT_TRACE_WARNING("No ccb for PID=%x", pid);
osi_free(p_data->p_buf);
p_data->p_buf = NULL;
if (cr_ipid == AVCT_CMD) {
if ((p_buf = (BT_HDR *) osi_malloc(AVCT_CMD_BUF_SIZE)) != NULL) {
p_buf->len = AVCT_HDR_LEN_SINGLE;
p_buf->offset = AVCT_MSG_OFFSET - AVCT_HDR_LEN_SINGLE;
p = (UINT8 *)(p_buf + 1) + p_buf->offset;
AVCT_BLD_HDR(p, label, AVCT_PKT_TYPE_SINGLE, AVCT_REJ);
UINT16_TO_BE_STREAM(p, pid);
L2CA_DataWrite(p_lcb->ch_lcid, p_buf);
}{...}
}{...}
}{...}
}{...}
/* ... */
#endif