1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
156
157
158
159
160
161
162
163
164
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
221
222
223
224
225
226
227
228
229
230
231
232
239
245
246
253
260
261
268
269
270
271
272
273
274
275
276
277
278
279
286
292
293
300
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
328
329
330
336
337
338
339
340
341
342
343
344
345
346
347
348
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
448
449
450
451
452
453
454
455
456
457
458
459
460
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
497
498
499
507
508
509
510
/* ... */
/* ... */
#include "common/bt_target.h"
#include "stack/btm_api.h"
#include "btm_int.h"
#include "stack/rfcdefs.h"
#include "stack/port_api.h"
#include "stack/port_ext.h"
#include "port_int.h"
#include "rfc_int.h"
#include "stack/btu.h"
#include "common/bt_defs.h"
#include "osi/allocator.h"
#include "osi/mutex.h"
#include <string.h>13 includes
#if (defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)
/* ... */
static const UINT8 rfc_crctable[] = {
0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75, 0x0E, 0x9F, 0xED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0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5, 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9, 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD, 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1, 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
}{...};
/* ... */
UINT8 rfc_calc_fcs (UINT16 len, UINT8 *p)
{
UINT8 fcs = 0xFF;
while (len--) {
fcs = rfc_crctable[fcs ^ *p++];
}{...}
return (0xFF - fcs);
}{...}
/* ... */
BOOLEAN rfc_check_fcs (UINT16 len, UINT8 *p, UINT8 received_fcs)
{
UINT8 fcs = 0xFF;
while (len--) {
fcs = rfc_crctable[fcs ^ *p++];
}{...}
fcs = rfc_crctable[fcs ^ received_fcs];
return (fcs == 0xCF);
}{...}
/* ... */
tRFC_MCB *rfc_alloc_multiplexer_channel (BD_ADDR bd_addr, BOOLEAN is_initiator)
{
int i, j;
tRFC_MCB *p_mcb = NULL;
RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel: bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d", is_initiator);
for (i = 0; i < MAX_BD_CONNECTIONS; i++) {
RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel rfc_cb.port.rfc_mcb[%d].state:%d",
i, rfc_cb.port.rfc_mcb[i].state);
RFCOMM_TRACE_DEBUG("(rfc_cb.port.rfc_mcb[i].bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
rfc_cb.port.rfc_mcb[i].bd_addr[0], rfc_cb.port.rfc_mcb[i].bd_addr[1],
rfc_cb.port.rfc_mcb[i].bd_addr[2], rfc_cb.port.rfc_mcb[i].bd_addr[3],
rfc_cb.port.rfc_mcb[i].bd_addr[4], rfc_cb.port.rfc_mcb[i].bd_addr[5]);
if ((rfc_cb.port.rfc_mcb[i].state != RFC_MX_STATE_IDLE)
&& (!memcmp (rfc_cb.port.rfc_mcb[i].bd_addr, bd_addr, BD_ADDR_LEN))) {
if (rfc_cb.port.rfc_mcb[i].state == RFC_MX_STATE_CONNECTED) {
rfc_timer_stop (&rfc_cb.port.rfc_mcb[i]);
}{...}
RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d, found, state:%d, p_mcb:%p",
is_initiator, rfc_cb.port.rfc_mcb[i].state, &rfc_cb.port.rfc_mcb[i]);
return (&rfc_cb.port.rfc_mcb[i]);
}{...}
}{...}
for (i = 0, j = rfc_cb.rfc.last_mux + 1; i < MAX_BD_CONNECTIONS; i++, j++) {
if (j >= MAX_BD_CONNECTIONS) {
j = 0;
}{...}
p_mcb = &rfc_cb.port.rfc_mcb[j];
if (rfc_cb.port.rfc_mcb[j].state == RFC_MX_STATE_IDLE) {
fixed_queue_free(p_mcb->cmd_q, NULL);
rfc_timer_free(p_mcb);
memset (p_mcb, 0, sizeof (tRFC_MCB));
memcpy (p_mcb->bd_addr, bd_addr, BD_ADDR_LEN);
RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d, create new p_mcb:%p, index:%d",
is_initiator, &rfc_cb.port.rfc_mcb[j], j);
p_mcb->cmd_q = fixed_queue_new(QUEUE_SIZE_MAX);
p_mcb->is_initiator = is_initiator;
rfc_timer_start (p_mcb, RFC_MCB_INIT_INACT_TIMER);
rfc_cb.rfc.last_mux = (UINT8) j;
return (p_mcb);
}{...}
}{...}
return (NULL);
}{...}
void osi_free_fun(void *p)
{
osi_free(p);
}{...}
/* ... */
void rfc_release_multiplexer_channel (tRFC_MCB *p_mcb)
{
rfc_timer_free (p_mcb);
fixed_queue_free(p_mcb->cmd_q, osi_free_fun);
memset (p_mcb, 0, sizeof (tRFC_MCB));
p_mcb->state = RFC_MX_STATE_IDLE;
}{...}
/* ... */
void rfc_timer_start (tRFC_MCB *p_mcb, UINT16 timeout)
{
TIMER_LIST_ENT *p_tle = &p_mcb->tle;
RFCOMM_TRACE_EVENT ("rfc_timer_start - timeout:%d", timeout);
p_tle->param = (UINT32)p_mcb;
btu_start_timer (p_tle, BTU_TTYPE_RFCOMM_MFC, timeout);
}{...}
/* ... */
void rfc_timer_stop (tRFC_MCB *p_mcb)
{
RFCOMM_TRACE_EVENT ("rfc_timer_stop");
btu_stop_timer (&p_mcb->tle);
}{...}
/* ... */
void rfc_timer_free (tRFC_MCB *p_mcb)
{
RFCOMM_TRACE_EVENT ("rfc_timer_free");
btu_free_timer (&p_mcb->tle);
memset(&p_mcb->tle, 0, sizeof(TIMER_LIST_ENT));
}{...}
/* ... */
void rfc_port_timer_start (tPORT *p_port, UINT16 timeout)
{
TIMER_LIST_ENT *p_tle = &p_port->rfc.tle;
RFCOMM_TRACE_EVENT ("rfc_port_timer_start - timeout:%d", timeout);
p_tle->param = (UINT32)p_port;
btu_start_timer (p_tle, BTU_TTYPE_RFCOMM_PORT, timeout);
}{...}
/* ... */
void rfc_port_timer_stop (tPORT *p_port)
{
RFCOMM_TRACE_EVENT ("rfc_port_timer_stop");
btu_stop_timer (&p_port->rfc.tle);
}{...}
/* ... */
void rfc_port_timer_free (tPORT *p_port)
{
RFCOMM_TRACE_EVENT ("rfc_port_timer_stop");
btu_free_timer (&p_port->rfc.tle);
memset(&p_port->rfc.tle, 0, sizeof(TIMER_LIST_ENT));
}{...}
/* ... */
void rfc_check_mcb_active (tRFC_MCB *p_mcb)
{
UINT16 i;
for (i = 0; i < RFCOMM_MAX_DLCI; i++) {
if (p_mcb->port_inx[i] != 0) {
p_mcb->is_disc_initiator = FALSE;
return;
}{...}
}{...}
if (p_mcb->is_disc_initiator) {
p_mcb->is_disc_initiator = FALSE;
rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CLOSE_REQ, NULL);
}{...} else {
rfc_timer_start (p_mcb, RFC_MCB_RELEASE_INACT_TIMER);
}{...}
}{...}
/* ... */
void rfcomm_process_timeout (TIMER_LIST_ENT *p_tle)
{
switch (p_tle->event) {
case BTU_TTYPE_RFCOMM_MFC:
rfc_mx_sm_execute ((tRFC_MCB *)p_tle->param, RFC_EVENT_TIMEOUT, NULL);
break;
...
case BTU_TTYPE_RFCOMM_PORT:
rfc_port_sm_execute ((tPORT *)p_tle->param, RFC_EVENT_TIMEOUT, NULL);
break;
...
default:
break;...
}{...}
}{...}
/* ... */
void rfc_sec_check_complete (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, UINT8 res)
{
tPORT *p_port = (tPORT *)p_ref_data;
UNUSED(bd_addr);
UNUSED(transport);
if (!p_port->in_use
|| ((p_port->rfc.state != RFC_STATE_ORIG_WAIT_SEC_CHECK)
&& (p_port->rfc.state != RFC_STATE_TERM_WAIT_SEC_CHECK))) {
return;
}{...}
rfc_port_sm_execute ((tPORT *)p_ref_data, RFC_EVENT_SEC_COMPLETE, &res);
}{...}
/* ... */
void rfc_port_closed (tPORT *p_port)
{
tRFC_MCB *p_mcb = p_port->rfc.p_mcb;
RFCOMM_TRACE_DEBUG ("rfc_port_closed");
rfc_port_timer_stop (p_port);
p_port->rfc.state = RFC_STATE_CLOSED;
if (p_mcb) {
p_mcb->port_inx[p_port->dlci] = 0;
rfc_check_mcb_active (p_mcb);
}{...}
port_rfc_closed (p_port, PORT_CLOSED);
}{...}
/* ... */
void rfc_inc_credit (tPORT *p_port, UINT8 credit)
{
if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
p_port->credit_tx += credit;
RFCOMM_TRACE_EVENT ("rfc_inc_credit:%d", p_port->credit_tx);
if (p_port->tx.peer_fc == TRUE) {
PORT_FlowInd(p_port->rfc.p_mcb, p_port->dlci, TRUE);
}{...}
}{...}
}{...}
/* ... */
void rfc_dec_credit (tPORT *p_port)
{
if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
if (p_port->credit_tx > 0) {
p_port->credit_tx--;
}{...}
if (p_port->credit_tx == 0) {
p_port->tx.peer_fc = TRUE;
}{...}
}{...}
}{...}
/* ... */
void rfc_check_send_cmd(tRFC_MCB *p_mcb, BT_HDR *p_buf)
{
BT_HDR *p;
if (p_buf != NULL) {
if (p_mcb->cmd_q == NULL) {
RFCOMM_TRACE_ERROR("%s: empty queue: p_mcb = %p p_mcb->lcid = %u cached p_mcb = %p",
__func__, p_mcb, p_mcb->lcid,
rfc_find_lcid_mcb(p_mcb->lcid));
}{...}
fixed_queue_enqueue(p_mcb->cmd_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
}{...}
while (p_mcb->l2cap_congested == FALSE) {
if ((p = (BT_HDR *)fixed_queue_dequeue(p_mcb->cmd_q, 0)) == NULL) {
break;
}{...}
L2CA_DataWrite (p_mcb->lcid, p);
}{...}
}{...}
/* ... */
#endif