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
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
124
125
126
127
132
133
134
135
136
137
145
146
150
151
152
153
154
155
170
171
172
173
174
175
176
177
178
179
180
181
186
187
188
189
194
195
196
202
203
204
205
206
207
208
211
212
213
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
242
243
244
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
276
277
278
279
280
281
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
318
319
320
321
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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
378
379
380
381
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
412
413
414
415
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
446
447
448
449
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
482
483
484
485
488
489
490
493
494
495
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
527
528
529
530
531
532
533
539
540
541
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
576
577
578
579
582
583
586
587
588
589
590
591
592
593
594
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
620
621
622
623
626
627
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
661
662
663
664
667
668
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
699
700
701
702
705
706
707
708
709
710
714
718
722
726
730
734
738
741
742
743
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
785
786
787
788
791
792
795
796
797
798
803
804
805
806
807
808
811
812
813
814
815
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
856
857
858
859
862
863
866
867
868
869
874
875
876
877
878
879
882
883
884
885
886
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
925
926
927
928
931
932
935
936
937
938
943
944
945
946
947
948
949
952
953
954
955
956
957
958
959
963
964
965
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
993
994
995
996
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1033
1034
1035
1036
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1070
1071
1072
1073
1076
1077
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1106
1107
1108
1109
1112
1113
1114
1115
1116
1117
1118
1119
1120
1123
1124
1127
1128
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1160
1161
1162
1163
1166
1167
1168
1169
1170
1171
1172
1175
1176
1177
1178
1179
1180
1181
1184
1185
1186
1187
1188
1189
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1240
1241
1242
1243
1246
1247
1250
1251
1254
1255
1256
1257
1258
1259
1260
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1336
1337
1338
1341
1342
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1450
1451
1452
1453
1457
1458
1464
1465
1466
1467
1468
1477
1478
1479
1480
1481
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1515
1516
1517
1521
1522
1526
1527
1528
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1550
1551
1554
1555
1556
1557
1558
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1581
1582
1585
1586
1587
1588
1589
1590
1593
1594
1595
1596
1597
1598
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1636
1637
1638
1642
1643
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1678
1679
1680
1681
1684
1685
1686
1687
1688
1691
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1709
1710
1713
1714
1715
1716
1717
1718
1719
1722
1723
1724
1725
1726
1727
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1756
1757
1758
1761
1762
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1882
1883
/* ... */
/* ... */
#include <string.h>
#include "common/bt_target.h"
#include "stack/rfcdefs.h"
#include "stack/port_api.h"
#include "port_int.h"
#include "btm_int.h"
#include "stack/btm_api.h"
#include "rfc_int.h"
#include "stack/l2c_api.h"
#include "stack/sdp_api.h"
#include "osi/allocator.h"
#include "osi/mutex.h"12 includes
#if (defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)
#define PORT_BREAK_DURATION 1
static const char *result_code_strings[] = {
"Success",
"Unknown error",
"Already opened",
"Command pending",
"App not registered",
"No memory",
"No resources",
"Bad BD address",
"Unspecified error",
"Bad handle",
"Not opened",
"Line error",
"Start failed",
"Parameter negotiation failed",
"Port negotiation failed",
"Sec failed",
"Peer connection failed",
"Peer failed",
"Peer timeout",
"Closed",
"TX full",
"Local closed",
"Local timeout",
"TX queue disabled",
"Page timeout",
"Invalid SCN",
"Unknown result code"
}{...};
/* ... */
int RFCOMM_CreateConnection (UINT16 uuid, UINT8 scn, BOOLEAN is_server,
UINT16 mtu, BD_ADDR bd_addr, UINT16 *p_handle,
tPORT_MGMT_CALLBACK *p_mgmt_cb)
{
tPORT *p_port;
int i;
UINT8 dlci;
tRFC_MCB *p_mcb = port_find_mcb (bd_addr);
UINT16 rfcomm_mtu;
RFCOMM_TRACE_API ("RFCOMM_CreateConnection() BDA: %02x-%02x-%02x-%02x-%02x-%02x",
bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
*p_handle = 0;
if (( scn == 0 ) || (scn >= PORT_MAX_RFC_PORTS )) {
RFCOMM_TRACE_ERROR ("RFCOMM_CreateConnection - invalid SCN");
return (PORT_INVALID_SCN);
}{...}
if (p_mcb && !p_mcb->is_initiator && !is_server) {
dlci = (scn << 1) + 1;
}{...} else {
dlci = (scn << 1);
}{...}
RFCOMM_TRACE_API("RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, p_mcb:%p",
scn, dlci, is_server, mtu, p_mcb);
if (!is_server && ((p_port = port_find_port (dlci, bd_addr)) != NULL)) {
if (p_port->is_server == FALSE) {
RFCOMM_TRACE_ERROR ("RFCOMM_CreateConnection - already opened state:%d, RFC state:%d, MCB state:%d",
p_port->state, p_port->rfc.state, p_port->rfc.p_mcb ? p_port->rfc.p_mcb->state : 0);
return (PORT_ALREADY_OPENED);
}{...}
}{...}
if ((p_port = port_allocate_port (dlci, bd_addr)) == NULL) {
RFCOMM_TRACE_WARNING ("RFCOMM_CreateConnection - no resources");
return (PORT_NO_RESOURCES);
}{...}
RFCOMM_TRACE_API("RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, p_mcb:%p, p_port:%p",
scn, dlci, is_server, mtu, p_mcb, p_port);
p_port->default_signal_state = (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
switch (uuid) {
case UUID_PROTOCOL_OBEX:
p_port->default_signal_state = PORT_OBEX_DEFAULT_SIGNAL_STATE;
break;...
case UUID_SERVCLASS_SERIAL_PORT:
p_port->default_signal_state = PORT_SPP_DEFAULT_SIGNAL_STATE;
break;...
case UUID_SERVCLASS_LAN_ACCESS_USING_PPP:
p_port->default_signal_state = PORT_PPP_DEFAULT_SIGNAL_STATE;
break;...
case UUID_SERVCLASS_DIALUP_NETWORKING:
case UUID_SERVCLASS_FAX:
p_port->default_signal_state = PORT_DUN_DEFAULT_SIGNAL_STATE;
break;...
}{...}
RFCOMM_TRACE_EVENT ("RFCOMM_CreateConnection dlci:%d signal state:0x%x", dlci, p_port->default_signal_state);
*p_handle = p_port->inx;
p_port->state = PORT_STATE_OPENING;
p_port->uuid = uuid;
p_port->is_server = is_server;
p_port->scn = scn;
p_port->ev_mask = 0;
/* ... */
rfcomm_mtu = L2CAP_MTU_SIZE - RFCOMM_DATA_OVERHEAD;
if (mtu) {
p_port->mtu = (mtu < rfcomm_mtu) ? mtu : rfcomm_mtu;
}{...} else {
p_port->mtu = rfcomm_mtu;
}{...}
if ( is_server ) {
p_port->keep_port_handle = TRUE;
p_port->keep_mtu = p_port->mtu;
}{...}
p_port->local_ctrl.modem_signal = p_port->default_signal_state;
p_port->local_ctrl.fc = FALSE;
p_port->p_mgmt_callback = p_mgmt_cb;
for (i = 0; i < BD_ADDR_LEN; i++) {
p_port->bd_addr[i] = bd_addr[i];
}{...}
if (p_port->is_server) {
return (PORT_SUCCESS);
}{...}
return port_open_continue (p_port);
}{...}
/* ... */
int RFCOMM_RemoveConnection (UINT16 handle)
{
tPORT *p_port;
RFCOMM_TRACE_API ("RFCOMM_RemoveConnection() handle:%d", handle);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
RFCOMM_TRACE_ERROR ("RFCOMM_RemoveConnection() BAD handle:%d", handle);
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
RFCOMM_TRACE_EVENT ("RFCOMM_RemoveConnection() Not opened:%d", handle);
return (PORT_SUCCESS);
}{...}
p_port->state = PORT_STATE_CLOSING;
port_start_close (p_port);
return (PORT_SUCCESS);
}{...}
/* ... */
int RFCOMM_RemoveServer (UINT16 handle)
{
tPORT *p_port;
RFCOMM_TRACE_API ("RFCOMM_RemoveServer() handle:%d", handle);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
RFCOMM_TRACE_ERROR ("RFCOMM_RemoveServer() BAD handle:%d", handle);
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
p_port->p_mgmt_callback = NULL;
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
RFCOMM_TRACE_EVENT ("RFCOMM_RemoveServer() Not opened:%d", handle);
return (PORT_SUCCESS);
}{...}
p_port->keep_port_handle = FALSE;
p_port->state = PORT_STATE_CLOSING;
port_start_close (p_port);
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_SetEventCallback (UINT16 port_handle, tPORT_CALLBACK *p_port_cb)
{
tPORT *p_port;
if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[port_handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
RFCOMM_TRACE_API ("PORT_SetEventCallback() handle:%d", port_handle);
p_port->p_callback = p_port_cb;
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_ClearKeepHandleFlag (UINT16 port_handle)
{
tPORT *p_port;
if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[port_handle - 1];
p_port->keep_port_handle = 0;
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_SetDataCallback (UINT16 port_handle, tPORT_DATA_CALLBACK *p_port_cb)
{
tPORT *p_port;
if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[port_handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
p_port->p_data_callback = p_port_cb;
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_SetDataCOCallback (UINT16 port_handle, tPORT_DATA_CO_CALLBACK *p_port_cb)
{
tPORT *p_port;
if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[port_handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
p_port->p_data_co_callback = p_port_cb;
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_SetEventMask (UINT16 port_handle, UINT32 mask)
{
tPORT *p_port;
RFCOMM_TRACE_API ("PORT_SetEventMask() handle:%d mask:0x%x", port_handle, mask);
if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[port_handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
p_port->ev_mask = mask;
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_CheckConnection(UINT16 handle, BOOLEAN ignore_rfc_state, BD_ADDR bd_addr, UINT16 *p_lcid)
{
tPORT *p_port;
RFCOMM_TRACE_API ("PORT_CheckConnection() handle:%d", handle);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (!p_port->rfc.p_mcb || !p_port->rfc.p_mcb->peer_ready ||
(!ignore_rfc_state ? (p_port->rfc.state != RFC_STATE_OPENED) : false)) {
return (PORT_LINE_ERR);
}{...}
memcpy (bd_addr, p_port->rfc.p_mcb->bd_addr, BD_ADDR_LEN);
if (p_lcid) {
*p_lcid = p_port->rfc.p_mcb->lcid;
}{...}
return (PORT_SUCCESS);
}{...}
/* ... */
BOOLEAN PORT_IsOpening (BD_ADDR bd_addr)
{
UINT8 xx, yy;
tRFC_MCB *p_mcb = NULL;
tPORT *p_port;
BOOLEAN found_port;
for (xx = 0; xx < MAX_BD_CONNECTIONS; xx++) {
if ((rfc_cb.port.rfc_mcb[xx].state > RFC_MX_STATE_IDLE) &&
(rfc_cb.port.rfc_mcb[xx].state < RFC_MX_STATE_CONNECTED)) {
memcpy (bd_addr, rfc_cb.port.rfc_mcb[xx].bd_addr, BD_ADDR_LEN);
return TRUE;
}{...}
if (rfc_cb.port.rfc_mcb[xx].state == RFC_MX_STATE_CONNECTED) {
found_port = FALSE;
p_mcb = &rfc_cb.port.rfc_mcb[xx];
p_port = &rfc_cb.port.port[0];
for (yy = 0; yy < MAX_RFC_PORTS; yy++, p_port++) {
if (p_port->rfc.p_mcb == p_mcb) {
found_port = TRUE;
break;
}{...}
}{...}
if ((!found_port) ||
(found_port && (p_port->rfc.state < RFC_STATE_OPENED))) {
memcpy (bd_addr, rfc_cb.port.rfc_mcb[xx].bd_addr, BD_ADDR_LEN);
return TRUE;
}{...}
}{...}
}{...}
return FALSE;
}{...}
/* ... */
int PORT_SetState (UINT16 handle, tPORT_STATE *p_settings)
{
tPORT *p_port;
UINT8 baud_rate;
RFCOMM_TRACE_API ("PORT_SetState() handle:%d", handle);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (p_port->line_status) {
return (PORT_LINE_ERR);
}{...}
RFCOMM_TRACE_API ("PORT_SetState() handle:%d FC_TYPE:0x%x", handle,
p_settings->fc_type);
baud_rate = p_port->user_port_pars.baud_rate;
p_port->user_port_pars = *p_settings;
if (baud_rate != p_settings->baud_rate) {
port_start_par_neg (p_port);
}{...}
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_GetRxQueueCnt (UINT16 handle, UINT16 *p_rx_queue_count)
{
tPORT *p_port;
RFCOMM_TRACE_API ("PORT_GetRxQueueCnt() handle:%d", handle);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (p_port->line_status) {
return (PORT_LINE_ERR);
}{...}
*p_rx_queue_count = p_port->rx.queue_size;
RFCOMM_TRACE_API ("PORT_GetRxQueueCnt() p_rx_queue_count:%d, p_port->rx.queue.count = %d",
*p_rx_queue_count, p_port->rx.queue_size);
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_GetState (UINT16 handle, tPORT_STATE *p_settings)
{
tPORT *p_port;
RFCOMM_TRACE_API ("PORT_GetState() handle:%d", handle);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (p_port->line_status) {
return (PORT_LINE_ERR);
}{...}
*p_settings = p_port->user_port_pars;
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_Control (UINT16 handle, UINT8 signal)
{
tPORT *p_port;
UINT8 old_modem_signal;
RFCOMM_TRACE_API ("PORT_Control() handle:%d signal:0x%x", handle, signal);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
old_modem_signal = p_port->local_ctrl.modem_signal;
p_port->local_ctrl.break_signal = 0;
switch (signal) {
case PORT_SET_CTSRTS:
p_port->local_ctrl.modem_signal |= PORT_CTSRTS_ON;
break;
...
case PORT_CLR_CTSRTS:
p_port->local_ctrl.modem_signal &= ~PORT_CTSRTS_ON;
break;
...
case PORT_SET_DTRDSR:
p_port->local_ctrl.modem_signal |= PORT_DTRDSR_ON;
break;
...
case PORT_CLR_DTRDSR:
p_port->local_ctrl.modem_signal &= ~PORT_DTRDSR_ON;
break;
...
case PORT_SET_RI:
p_port->local_ctrl.modem_signal |= PORT_RING_ON;
break;
...
case PORT_CLR_RI:
p_port->local_ctrl.modem_signal &= ~PORT_RING_ON;
break;
...
case PORT_SET_DCD:
p_port->local_ctrl.modem_signal |= PORT_DCD_ON;
break;
...
case PORT_CLR_DCD:
p_port->local_ctrl.modem_signal &= ~PORT_DCD_ON;
break;...
}{...}
if (signal == PORT_BREAK) {
p_port->local_ctrl.break_signal = PORT_BREAK_DURATION;
}{...} else if (p_port->local_ctrl.modem_signal == old_modem_signal) {
return (PORT_SUCCESS);
}{...}
port_start_control (p_port);
RFCOMM_TRACE_EVENT ("PORT_Control DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_FlowControl (UINT16 handle, BOOLEAN enable)
{
tPORT *p_port;
BOOLEAN old_fc;
UINT32 events;
RFCOMM_TRACE_API ("PORT_FlowControl() handle:%d enable: %d", handle, enable);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (!p_port->rfc.p_mcb) {
return (PORT_NOT_OPENED);
}{...}
p_port->rx.user_fc = !enable;
if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
if (!p_port->rx.user_fc) {
port_flow_control_peer(p_port, TRUE, 0);
}{...}
}{...} else {
old_fc = p_port->local_ctrl.fc;
p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc);
if (p_port->local_ctrl.fc != old_fc) {
port_start_control (p_port);
}{...}
}{...}
if (enable && (p_port->rx.queue_size != 0)) {
events = PORT_EV_RXCHAR;
if (p_port->rx_flag_ev_pending) {
p_port->rx_flag_ev_pending = FALSE;
events |= PORT_EV_RXFLAG;
}{...}
events &= p_port->ev_mask;
if (p_port->p_callback && events) {
p_port->p_callback (events, p_port->inx);
}{...}
}{...}
return (PORT_SUCCESS);
}{...}
#if 0
/* ... */
int PORT_FlowControl_MaxCredit (UINT16 handle, BOOLEAN enable)
{
tPORT *p_port;
BOOLEAN old_fc;
UINT32 events;
RFCOMM_TRACE_API ("PORT_FlowControl() handle:%d enable: %d", handle, enable);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (!p_port->rfc.p_mcb) {
return (PORT_NOT_OPENED);
}{...}
p_port->rx.user_fc = !enable;
if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
if (!p_port->rx.user_fc) {
port_flow_control_peer(p_port, TRUE, p_port->credit_rx);
}{...}
}{...} else {
old_fc = p_port->local_ctrl.fc;
p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc);
if (p_port->local_ctrl.fc != old_fc) {
port_start_control (p_port);
}{...}
}{...}
if (enable && (p_port->rx.queue_size != 0)) {
events = PORT_EV_RXCHAR;
if (p_port->rx_flag_ev_pending) {
p_port->rx_flag_ev_pending = FALSE;
events |= PORT_EV_RXFLAG;
}{...}
events &= p_port->ev_mask;
if (p_port->p_callback && events) {
p_port->p_callback (events, p_port->inx);
}{...}
}{...}
return (PORT_SUCCESS);
}{...}
/* ... */#endif
/* ... */
int PORT_FlowControl_GiveCredit (UINT16 handle, BOOLEAN enable, UINT16 credits_given)
{
tPORT *p_port;
BOOLEAN old_fc;
UINT32 events;
RFCOMM_TRACE_DEBUG("%s handle:%d enable: %d, cred %d", __func__, handle, enable, credits_given);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (!p_port->rfc.p_mcb) {
return (PORT_NOT_OPENED);
}{...}
p_port->rx.user_fc = !enable;
if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
if (!p_port->rx.user_fc) {
port_flow_control_peer(p_port, TRUE, credits_given);
}{...}
}{...} else {
assert(0);
old_fc = p_port->local_ctrl.fc;
p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc);
if (p_port->local_ctrl.fc != old_fc) {
port_start_control (p_port);
}{...}
}{...}
if (enable && (p_port->rx.queue_size != 0)) {
assert(0);
events = PORT_EV_RXCHAR;
if (p_port->rx_flag_ev_pending) {
p_port->rx_flag_ev_pending = FALSE;
events |= PORT_EV_RXFLAG;
}{...}
events &= p_port->ev_mask;
if (p_port->p_callback && events) {
p_port->p_callback (events, p_port->inx);
}{...}
}{...}
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_GetModemStatus (UINT16 handle, UINT8 *p_signal)
{
tPORT *p_port;
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
*p_signal = p_port->peer_ctrl.modem_signal;
RFCOMM_TRACE_API ("PORT_GetModemStatus() handle:%d signal:%x", handle, *p_signal);
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_ClearError (UINT16 handle, UINT16 *p_errors, tPORT_STATUS *p_status)
{
tPORT *p_port;
RFCOMM_TRACE_API ("PORT_ClearError() handle:%d", handle);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
*p_errors = p_port->line_status;
p_port->line_status = (p_port->line_status & LINE_STATUS_FAILED);
PORT_GetQueueStatus (handle, p_status);
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_SendError (UINT16 handle, UINT8 errors)
{
tPORT *p_port;
RFCOMM_TRACE_API ("PORT_SendError() handle:%d errors:0x%x", handle, errors);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (!p_port->rfc.p_mcb) {
return (PORT_NOT_OPENED);
}{...}
RFCOMM_LineStatusReq (p_port->rfc.p_mcb, p_port->dlci, errors);
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_GetQueueStatus (UINT16 handle, tPORT_STATUS *p_status)
{
tPORT *p_port;
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
p_status->in_queue_size = (UINT16) p_port->rx.queue_size;
p_status->out_queue_size = (UINT16) p_port->tx.queue_size;
p_status->mtu_size = (UINT16) p_port->peer_mtu;
p_status->flags = 0;
if (!(p_port->peer_ctrl.modem_signal & PORT_CTSRTS_ON)) {
p_status->flags |= PORT_FLAG_CTS_HOLD;
}{...}
if (!(p_port->peer_ctrl.modem_signal & PORT_DTRDSR_ON)) {
p_status->flags |= PORT_FLAG_DSR_HOLD;
}{...}
if (!(p_port->peer_ctrl.modem_signal & PORT_DCD_ON)) {
p_status->flags |= PORT_FLAG_RLSD_HOLD;
}{...}
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_Purge (UINT16 handle, UINT8 purge_flags)
{
tPORT *p_port;
BT_HDR *p_buf;
UINT16 count;
UINT32 events;
RFCOMM_TRACE_API ("PORT_Purge() handle:%d flags:0x%x", handle, purge_flags);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (purge_flags & PORT_PURGE_RXCLEAR) {
osi_mutex_global_lock();
count = fixed_queue_length(p_port->rx.queue);
while ((p_buf = (BT_HDR *)fixed_queue_dequeue(p_port->rx.queue, 0)) != NULL) {
osi_free (p_buf);
}{...}
p_port->rx.queue_size = 0;
osi_mutex_global_unlock();
if (count) {
port_flow_control_peer (p_port, TRUE, count);
}{...}
}{...}
if (purge_flags & PORT_PURGE_TXCLEAR) {
osi_mutex_global_lock();
while ((p_buf = (BT_HDR *)fixed_queue_dequeue(p_port->tx.queue, 0)) != NULL) {
osi_free (p_buf);
}{...}
p_port->tx.queue_size = 0;
osi_mutex_global_unlock();
events = PORT_EV_TXEMPTY;
events |= port_flow_control_user (p_port);
events &= p_port->ev_mask;
if ((p_port->p_callback != NULL) && events) {
(p_port->p_callback)(events, p_port->inx);
}{...}
}{...}
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_ReadData (UINT16 handle, char *p_data, UINT16 max_len, UINT16 *p_len)
{
tPORT *p_port;
BT_HDR *p_buf;
UINT16 count;
RFCOMM_TRACE_API ("PORT_ReadData() handle:%d max_len:%d", handle, max_len);
*p_len = 0;
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (p_port->line_status) {
return (PORT_LINE_ERR);
}{...}
if (fixed_queue_is_empty(p_port->rx.queue)){
return (PORT_SUCCESS);
}{...}
count = 0;
while (max_len)
{
p_buf = (BT_HDR *)fixed_queue_try_peek_first(p_port->rx.queue);
if (p_buf == NULL){
break;
}{...}
if (p_buf->len > max_len) {
memcpy (p_data, (UINT8 *)(p_buf + 1) + p_buf->offset, max_len);
p_buf->offset += max_len;
p_buf->len -= max_len;
*p_len += max_len;
osi_mutex_global_lock();
p_port->rx.queue_size -= max_len;
osi_mutex_global_unlock();
break;
}{...} else {
memcpy (p_data, (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
*p_len += p_buf->len;
max_len -= p_buf->len;
osi_mutex_global_lock();
p_port->rx.queue_size -= p_buf->len;
if (max_len) {
p_data += p_buf->len;
}{...}
osi_free(fixed_queue_dequeue(p_port->rx.queue, 0));
osi_mutex_global_unlock();
count++;
}{...}
}{...}
if (*p_len == 1) {
RFCOMM_TRACE_EVENT ("PORT_ReadData queue:%d returned:%d %x", p_port->rx.queue_size, *p_len, (p_data[0]));
}{...} else {
RFCOMM_TRACE_EVENT ("PORT_ReadData queue:%d returned:%d", p_port->rx.queue_size, *p_len);
}{...}
port_flow_control_peer (p_port, TRUE, count);
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_Read (UINT16 handle, BT_HDR **pp_buf)
{
tPORT *p_port;
BT_HDR *p_buf;
RFCOMM_TRACE_API ("PORT_Read() handle:%d", handle);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (p_port->line_status) {
return (PORT_LINE_ERR);
}{...}
osi_mutex_global_lock();
p_buf = (BT_HDR *)fixed_queue_dequeue(p_port->rx.queue, 0);
if (p_buf) {
p_port->rx.queue_size -= p_buf->len;
osi_mutex_global_unlock();
port_flow_control_peer (p_port, TRUE, 1);
}{...} else {
osi_mutex_global_unlock();
}{...}
*pp_buf = p_buf;
return (PORT_SUCCESS);
}{...}
/* ... */
static int port_write (tPORT *p_port, BT_HDR *p_buf)
{
if (p_port->is_server && (p_port->rfc.state != RFC_STATE_OPENED)) {
osi_free (p_buf);
return (PORT_CLOSED);
}{...}
if (p_port->tx.peer_fc
|| !p_port->rfc.p_mcb
|| !p_port->rfc.p_mcb->peer_ready
|| (p_port->rfc.state != RFC_STATE_OPENED)
|| ((p_port->port_ctrl & (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED)) !=
(PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED))) {
if ((p_port->tx.queue_size > PORT_TX_CRITICAL_WM)
|| (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_CRITICAL_WM)){
RFCOMM_TRACE_WARNING ("PORT_Write: Queue size: %d",
p_port->tx.queue_size);
osi_free (p_buf);
if ((p_port->p_callback != NULL) && (p_port->ev_mask & PORT_EV_ERR)) {
p_port->p_callback (PORT_EV_ERR, p_port->inx);
}{...}
return (PORT_TX_FULL);
}{...}
RFCOMM_TRACE_EVENT ("PORT_Write : Data is enqued. flow disabled %d peer_ready %d state %d ctrl_state %x",
p_port->tx.peer_fc,
(p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready),
p_port->rfc.state,
p_port->port_ctrl);
fixed_queue_enqueue(p_port->tx.queue, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
p_port->tx.queue_size += p_buf->len;
return (PORT_CMD_PENDING);
}{...} else {
RFCOMM_TRACE_EVENT ("PORT_Write : Data is being sent");
RFCOMM_DataReq (p_port->rfc.p_mcb, p_port->dlci, p_buf);
return (PORT_SUCCESS);
}{...}
}{...}
/* ... */
int PORT_Write (UINT16 handle, BT_HDR *p_buf)
{
tPORT *p_port;
UINT32 event = 0;
int rc;
RFCOMM_TRACE_API ("PORT_Write() handle:%d", handle);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
osi_free (p_buf);
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
osi_free (p_buf);
return (PORT_NOT_OPENED);
}{...}
if (p_port->line_status) {
RFCOMM_TRACE_WARNING ("PORT_Write: Data dropped line_status:0x%x",
p_port->line_status);
osi_free (p_buf);
return (PORT_LINE_ERR);
}{...}
rc = port_write (p_port, p_buf);
event |= port_flow_control_user (p_port);
switch (rc) {
case PORT_TX_FULL:
event |= PORT_EV_ERR;
break;
...
case PORT_SUCCESS:
event |= (PORT_EV_TXCHAR | PORT_EV_TXEMPTY);
break;...
}{...}
event &= p_port->ev_mask;
if (p_port->p_callback && event) {
(p_port->p_callback)(event, p_port->inx);
}{...}
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_WriteDataCO (UINT16 handle, int *p_len, int len, UINT8 *p_data)
{
tPORT *p_port;
BT_HDR *p_buf;
UINT32 event = 0;
int rc = 0;
UINT16 length;
RFCOMM_TRACE_API ("PORT_WriteDataCO() handle:%d", handle);
*p_len = 0;
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
RFCOMM_TRACE_WARNING ("PORT_WriteDataByFd() no port state:%d", p_port->state);
return (PORT_NOT_OPENED);
}{...}
if (!p_port->peer_mtu) {
RFCOMM_TRACE_ERROR ("PORT_WriteDataByFd() peer_mtu:%d", p_port->peer_mtu);
return (PORT_UNKNOWN_ERROR);
}{...}
int available = 0;
available = len;
if (available == 0) {
return PORT_SUCCESS;
}{...}
length = RFCOMM_DATA_BUF_SIZE -
(UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);
while (available) {
if ((p_port->tx.queue_size > PORT_TX_HIGH_WM)
|| (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_HIGH_WM)) {
port_flow_control_user(p_port);
event |= PORT_EV_FC;
RFCOMM_TRACE_EVENT ("tx queue is full,tx.queue_size:%d,tx.queue.count:%d,available:%d",
p_port->tx.queue_size, fixed_queue_length(p_port->tx.queue), available);
break;
}{...}
if (p_port->peer_mtu < length) {
length = p_port->peer_mtu;
}{...}
if (available < (int)length) {
length = (UINT16)available;
}{...}
UINT16 alloc_size = (UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD
+ length + L2CAP_FCS_LEN);
p_buf = (BT_HDR *)osi_malloc(alloc_size);
if (!p_buf) {
RFCOMM_TRACE_EVENT ("PORT_WriteDataCO: out of heap.");
break;
}{...}
p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET;
p_buf->layer_specific = handle;
p_buf->len = length;
p_buf->event = BT_EVT_TO_BTU_SP_DATA;
memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, length);
RFCOMM_TRACE_EVENT ("PORT_WriteData %d bytes", length);
rc = port_write (p_port, p_buf);
event |= port_flow_control_user (p_port);
if (rc == PORT_SUCCESS) {
event |= PORT_EV_TXCHAR;
}{...}
if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING)) {
break;
}{...}
*p_len += length;
available -= (int)length;
p_data += length;
}{...}
if (!available && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED)) {
event |= PORT_EV_TXEMPTY;
}{...}
event &= p_port->ev_mask;
if (p_port->p_callback && event) {
(p_port->p_callback)(event, p_port->inx);
}{...}
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_WriteData (UINT16 handle, char *p_data, UINT16 max_len, UINT16 *p_len)
{
tPORT *p_port;
BT_HDR *p_buf;
UINT32 event = 0;
int rc = 0;
UINT16 length;
RFCOMM_TRACE_API ("PORT_WriteData() max_len:%d", max_len);
*p_len = 0;
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
RFCOMM_TRACE_WARNING ("PORT_WriteData() no port state:%d", p_port->state);
return (PORT_NOT_OPENED);
}{...}
if (!max_len || !p_port->peer_mtu) {
RFCOMM_TRACE_ERROR ("PORT_WriteData() peer_mtu:%d", p_port->peer_mtu);
return (PORT_UNKNOWN_ERROR);
}{...}
length = RFCOMM_DATA_BUF_SIZE -
(UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);
osi_mutex_global_lock();
if (((p_buf = (BT_HDR *)fixed_queue_try_peek_last(p_port->tx.queue)) != NULL)
&& ((p_buf->len + max_len) <= p_port->peer_mtu)
&& ((p_buf->len + max_len) <= length)) {
memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len);
p_port->tx.queue_size += max_len;
*p_len = max_len;
p_buf->len += max_len;
osi_mutex_global_unlock();
return (PORT_SUCCESS);
}{...}
osi_mutex_global_unlock();
while (max_len) {
if ((p_port->tx.queue_size > PORT_TX_HIGH_WM)
|| (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_HIGH_WM)) {
break;
}{...}
p_buf = (BT_HDR *)osi_malloc(RFCOMM_DATA_BUF_SIZE);
if (!p_buf) {
break;
}{...}
p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET;
p_buf->layer_specific = handle;
if (p_port->peer_mtu < length) {
length = p_port->peer_mtu;
}{...}
if (max_len < length) {
length = max_len;
}{...}
p_buf->len = length;
p_buf->event = BT_EVT_TO_BTU_SP_DATA;
memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, length);
RFCOMM_TRACE_EVENT ("PORT_WriteData %d bytes", length);
rc = port_write (p_port, p_buf);
event |= port_flow_control_user (p_port);
if (rc == PORT_SUCCESS) {
event |= PORT_EV_TXCHAR;
}{...}
if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING)) {
break;
}{...}
*p_len += length;
max_len -= length;
p_data += length;
}{...}
if (!max_len && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED)) {
event |= PORT_EV_TXEMPTY;
}{...}
event &= p_port->ev_mask;
if (p_port->p_callback && event) {
(p_port->p_callback)(event, p_port->inx);
}{...}
return (PORT_SUCCESS);
}{...}
/* ... */
int PORT_Test (UINT16 handle, UINT8 *p_data, UINT16 len)
{
BT_HDR *p_buf;
tPORT *p_port;
RFCOMM_TRACE_API ("PORT_Test() len:%d", len);
if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
return (PORT_BAD_HANDLE);
}{...}
p_port = &rfc_cb.port.port[handle - 1];
if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
return (PORT_NOT_OPENED);
}{...}
if (len > ((p_port->mtu == 0) ? RFCOMM_DEFAULT_MTU : p_port->mtu)) {
return (PORT_UNKNOWN_ERROR);
}{...}
if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) != NULL) {
p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2;
p_buf->len = len;
memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, p_buf->len);
rfc_send_test (p_port->rfc.p_mcb, TRUE, p_buf);
return (PORT_SUCCESS);
}{...} else {
return (PORT_NO_MEM);
}{...}
}{...}
/* ... */
bt_status_t RFCOMM_Init (void)
{
#if RFC_DYNAMIC_MEMORY == TRUE
rfc_cb_ptr = (tRFC_CB *)osi_malloc(sizeof(tRFC_CB));
if (rfc_cb_ptr == NULL) {
return BT_STATUS_NOMEM;
}{...}
/* ... */#endif
memset (&rfc_cb, 0, sizeof (tRFC_CB));
rfc_cb.rfc.last_mux = MAX_BD_CONNECTIONS;
#if defined(RFCOMM_INITIAL_TRACE_LEVEL)
rfc_cb.trace_level = RFCOMM_INITIAL_TRACE_LEVEL;
#else
rfc_cb.trace_level = BT_TRACE_LEVEL_NONE;
#endif
rfcomm_l2cap_if_init ();
return BT_STATUS_SUCCESS;
}{...}
/* ... */
void RFCOMM_Deinit(void)
{
#if RFC_DYNAMIC_MEMORY == TRUE
if (rfc_cb_ptr){
osi_free(rfc_cb_ptr);
rfc_cb_ptr = NULL;
}{...}
/* ... */#endif
}{...}
/* ... */
UINT8 PORT_SetTraceLevel (UINT8 new_level)
{
if (new_level != 0xFF) {
rfc_cb.trace_level = new_level;
}{...}
return (rfc_cb.trace_level);
}{...}
/* ... */
const char *PORT_GetResultString (const uint8_t result_code)
{
if (result_code > PORT_ERR_MAX) {
return result_code_strings[PORT_ERR_MAX];
}{...}
return result_code_strings[result_code];
}{...}
/* ... */
void PORT_SetL2capErtm (BOOLEAN enable_l2cap_ertm)
{
rfc_cb.port.enable_l2cap_ertm = enable_l2cap_ertm;
}{...}
/* ... */
#endif