summaryrefslogtreecommitdiffstats
path: root/stack/rfcomm/port_api.c
blob: 1290ff99b6dc4805c851fe88b00ed4737896762d (plain)
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
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
659
660
661
662
663
664
665
666
667
668
669
670
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
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
1031
1032
1033
1034
1035
1036
1037
1038
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
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
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
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
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
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
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
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
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
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
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
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
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
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
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
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
/*****************************************************************************
**
**  Name:          port_api.c
**
**  Description:   this file contains the Serial Port API code
**
**
**  Copyright (c) 1999-2009, Broadcom Corp., All Rights Reserved.
**  WIDCOMM Bluetooth Core. Proprietary and confidential.
******************************************************************************/

#include <string.h>
#include "bt_target.h"
#include "gki.h"
#include "rfcdefs.h"
#include "port_api.h"
#include "port_int.h"
#include "btm_int.h"
#include "btm_api.h"
#include "rfc_int.h"
#include "l2c_api.h"
#include "sdp_api.h"

/* duration of break in 200ms units */
#define PORT_BREAK_DURATION     1

#include <cutils/log.h>
#define info(fmt, ...)  ALOGI ("%s: " fmt,__FUNCTION__,  ## __VA_ARGS__)
#define debug(fmt, ...) ALOGD ("%s: " fmt,__FUNCTION__,  ## __VA_ARGS__)
#define error(fmt, ...) ALOGE ("## ERROR : %s: " fmt "##",__FUNCTION__,  ## __VA_ARGS__)
#define asrt(s) if(!(s)) ALOGE ("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)

/*******************************************************************************
**
** Function         RFCOMM_CreateConnection
**
** Description      RFCOMM_CreateConnection function is used from the application
**                  to establish serial port connection to the peer device,
**                  or allow RFCOMM to accept a connection from the peer
**                  application.
**
** Parameters:      scn          - Service Channel Number as registered with
**                                 the SDP (server) or obtained using SDP from
**                                 the peer device (client).
**                  is_server    - TRUE if requesting application is a server
**                  mtu          - Maximum frame size the application can accept
**                  bd_addr      - BD_ADDR of the peer (client)
**                  mask         - specifies events to be enabled.  A value
**                                 of zero disables all events.
**                  p_handle     - OUT pointer to the handle.
**                  p_mgmt_cb    - pointer to callback function to receive
**                                 connection up/down events.
** Notes:
**
** Server can call this function with the same scn parameter multiple times if
** it is ready to accept multiple simulteneous connections.
**
** DLCI for the connection is (scn * 2 + 1) if client originates connection on
** existing none initiator multiplexer channel.  Otherwise it is (scn * 2).
** For the server DLCI can be changed later if client will be calling it using
** (scn * 2 + 1) dlci.
**
*******************************************************************************/
int RFCOMM_CreateConnection (UINT16 uuid, UINT8 scn, BOOLEAN is_server,
                             UINT16 mtu, BD_ADDR bd_addr, UINT16 *p_handle,
                             tPORT_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_API3 ("RFCOMM_CreateConnection() called SCN: %d is_server:%d mtu:%d",
                       scn, is_server, mtu);
    RFCOMM_TRACE_API6 ("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 ))
    {
        /* Server Channel Number(SCN) should be in range 1...30 */
        RFCOMM_TRACE_ERROR0 ("RFCOMM_CreateConnection - invalid SCN");
        return (PORT_INVALID_SCN);
    }

    /* For client that originate connection on the existing none initiator */
    /* multiplexer channel DLCI should be odd */
    if (p_mcb && !p_mcb->is_initiator && !is_server)
        dlci = (scn << 1) + 1;
    else
        dlci = (scn << 1);

    /* For the server side always allocate a new port.  On the client side */
    /* do not allow the same (dlci, bd_addr) to be opened twice by application */
    if (!is_server && ((p_port = port_find_port (dlci, bd_addr)) != NULL))
    {
        /* if existing port is also a client port */
        if (p_port->is_server == FALSE)
        {
            RFCOMM_TRACE_ERROR3 ("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_WARNING0 ("RFCOMM_CreateConnection - no resources");
        return (PORT_NO_RESOURCES);
    }

    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_EVENT2 ("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;

    /* If the MTU is not specified (0), keep MTU decision until the
     * PN frame has to be send
     * at that time connection should be established and we
     * will know for sure our prefered MTU
     */

    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;

    /* server doesn't need to release port when closing */
    if( is_server )
    {
        p_port->keep_port_handle = TRUE;

        /* keep mtu that user asked, p_port->mtu could be updated during param negotiation */
        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 this is not initiator of the connection need to just wait */
    if (p_port->is_server)
    {
        return (PORT_SUCCESS);
    }

    /* Open will be continued after security checks are passed */
    return port_open_continue (p_port);
}


/*******************************************************************************
**
** Function         RFCOMM_RemoveConnection
**
** Description      This function is called to close the specified connection.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**
*******************************************************************************/
int RFCOMM_RemoveConnection (UINT16 handle)
{
    tPORT      *p_port;

    RFCOMM_TRACE_API1 ("RFCOMM_RemoveConnection() handle:%d", handle);

    /* Check if handle is valid to avoid crashing */
    if ((handle == 0) || (handle > MAX_RFC_PORTS))
    {
        RFCOMM_TRACE_ERROR1 ("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_EVENT1 ("RFCOMM_RemoveConnection() Not opened:%d", handle);
        return (PORT_SUCCESS);
    }

    p_port->state = PORT_STATE_CLOSING;

    port_start_close (p_port);

    return (PORT_SUCCESS);
}

/*******************************************************************************
**
** Function         RFCOMM_RemoveServer
**
** Description      This function is called to close the server port.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**
*******************************************************************************/
int RFCOMM_RemoveServer (UINT16 handle)
{
    tPORT      *p_port;

    RFCOMM_TRACE_API1 ("RFCOMM_RemoveServer() handle:%d", handle);

    /* Check if handle is valid to avoid crashing */
    if ((handle == 0) || (handle > MAX_RFC_PORTS))
    {
        RFCOMM_TRACE_ERROR1 ("RFCOMM_RemoveServer() BAD handle:%d", handle);
        return (PORT_BAD_HANDLE);
    }
    p_port = &rfc_cb.port.port[handle - 1];

    /* Do not report any events to the client any more. */
    p_port->p_mgmt_callback = NULL;

    if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    {
        RFCOMM_TRACE_EVENT1 ("RFCOMM_RemoveServer() Not opened:%d", handle);
        return (PORT_SUCCESS);
    }

    /* this port will be deallocated after closing */
    p_port->keep_port_handle = FALSE;
    p_port->state = PORT_STATE_CLOSING;

    port_start_close (p_port);

    return (PORT_SUCCESS);
}

/*******************************************************************************
**
** Function         PORT_SetEventCallback
**
** Description      This function is called to provide an address of the
**                  function which will be called when one of the events
**                  specified in the mask occures.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_callback - address of the callback function which should
**                               be called from the RFCOMM when an event
**                               specified in the mask occures.
**
**
*******************************************************************************/
int PORT_SetEventCallback (UINT16 port_handle, tPORT_CALLBACK *p_port_cb)
{
    tPORT  *p_port;

    /* Check if handle is valid to avoid crashing */
    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_API1 ("PORT_SetEventCallback() handle:%d", port_handle);

    p_port->p_callback = p_port_cb;

    return (PORT_SUCCESS);
}


/*******************************************************************************
**
** Function         PORT_SetDataCallback
**
** Description      This function is when a data packet is received
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_callback - address of the callback function which should
**                               be called from the RFCOMM when data packet
**                               is received.
**
**
*******************************************************************************/
int PORT_SetDataCallback (UINT16 port_handle, tPORT_DATA_CALLBACK *p_port_cb)
{
    tPORT  *p_port;

    RFCOMM_TRACE_API2 ("PORT_SetDataCallback() handle:%d cb 0x%x", port_handle, p_port_cb);

    /* Check if handle is valid to avoid crashing */
    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);
}
/*******************************************************************************
**
** Function         PORT_SetCODataCallback
**
** Description      This function is when a data packet is received
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_callback - address of the callback function which should
**                               be called from the RFCOMM when data packet
**                               is received.
**
**
*******************************************************************************/
int PORT_SetDataCOCallback (UINT16 port_handle, tPORT_DATA_CO_CALLBACK *p_port_cb)
{
    tPORT  *p_port;

    RFCOMM_TRACE_API2 ("PORT_SetDataCOCallback() handle:%d cb 0x%x", port_handle, p_port_cb);

    /* Check if handle is valid to avoid crashing */
    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);
}



/*******************************************************************************
**
** Function         PORT_SetEventMask
**
** Description      This function is called to close the specified connection.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  mask   - Bitmask of the events the host is interested in
**
*******************************************************************************/
int PORT_SetEventMask (UINT16 port_handle, UINT32 mask)
{
    tPORT  *p_port;

    RFCOMM_TRACE_API2 ("PORT_SetEventMask() handle:%d mask:0x%x", port_handle, mask);

    /* Check if handle is valid to avoid crashing */
    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);
}


/*******************************************************************************
**
** Function         PORT_CheckConnection
**
** Description      This function returns PORT_SUCCESS if connection referenced
**                  by handle is up and running
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  bd_addr    - OUT bd_addr of the peer
**                  p_lcid     - OUT L2CAP's LCID
**
*******************************************************************************/
int PORT_CheckConnection (UINT16 handle, BD_ADDR bd_addr, UINT16 *p_lcid)
{
    tPORT      *p_port;

    RFCOMM_TRACE_API1 ("PORT_CheckConnection() handle:%d", handle);

    /* Check if handle is valid to avoid crashing */
    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
     || (p_port->rfc.state != RFC_STATE_OPENED))
    {
        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);
}

/*******************************************************************************
**
** Function         PORT_IsOpening
**
** Description      This function returns TRUE if there is any RFCOMM connection
**                  opening in process.
**
** Parameters:      TRUE if any connection opening is found
**                  bd_addr    - bd_addr of the peer
**
*******************************************************************************/
BOOLEAN PORT_IsOpening (BD_ADDR bd_addr)
{
    UINT8   xx, yy;
    tRFC_MCB *p_mcb = NULL;
    tPORT  *p_port;
    BOOLEAN found_port;

    /* Check for any rfc_mcb which is in the middle of opening. */
    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)))
            {
                /* Port is not established yet. */
                memcpy (bd_addr, rfc_cb.port.rfc_mcb[xx].bd_addr, BD_ADDR_LEN);
                return TRUE;
            }
        }
    }

    return FALSE;
}

/*******************************************************************************
**
** Function         PORT_SetState
**
** Description      This function configures connection according to the
**                  specifications in the tPORT_STATE structure.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_settings - Pointer to a tPORT_STATE structure containing
**                               configuration information for the connection.
**
**
*******************************************************************************/
int PORT_SetState (UINT16 handle, tPORT_STATE *p_settings)
{
    tPORT      *p_port;
    UINT8       baud_rate;

    RFCOMM_TRACE_API1 ("PORT_SetState() handle:%d", handle);

    /* Check if handle is valid to avoid crashing */
    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_API2 ("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;

    /* for now we've been asked to pass only baud rate */
    if (baud_rate != p_settings->baud_rate)
    {
        port_start_par_neg (p_port);
    }
    return (PORT_SUCCESS);
}

/*******************************************************************************
**
** Function         PORT_GetRxQueueCnt
**
** Description      This function return number of buffers on the rx queue.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_rx_queue_count - Pointer to return queue count in.
**
*******************************************************************************/
int PORT_GetRxQueueCnt (UINT16 handle, UINT16 *p_rx_queue_count)
{
    tPORT      *p_port;

    RFCOMM_TRACE_API1 ("PORT_GetRxQueueCnt() handle:%d", handle);

    /* Check if handle is valid to avoid crashing */
    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_API2 ("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);
}

/*******************************************************************************
**
** Function         PORT_GetState
**
** Description      This function is called to fill tPORT_STATE structure
**                  with the curremt control settings for the port
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_settings - Pointer to a tPORT_STATE structure in which
**                               configuration information is returned.
**
*******************************************************************************/
int PORT_GetState (UINT16 handle, tPORT_STATE *p_settings)
{
    tPORT      *p_port;

    RFCOMM_TRACE_API1 ("PORT_GetState() handle:%d", handle);

    /* Check if handle is valid to avoid crashing */
    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);
}


/*******************************************************************************
**
** Function         PORT_Control
**
** Description      This function directs a specified connection to pass control
**                  control information to the peer device.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  signal     = specify the function to be passed
**
*******************************************************************************/
int PORT_Control (UINT16 handle, UINT8 signal)
{
    tPORT      *p_port;
    UINT8      old_modem_signal;

    RFCOMM_TRACE_API2 ("PORT_Control() handle:%d signal:0x%x", handle, signal);

    /* Check if handle is valid to avoid crashing */
    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_EVENT4 ("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);
}


/*******************************************************************************
**
** Function         PORT_FlowControl
**
** Description      This function directs a specified connection to pass
**                  flow control message to the peer device.  Enable flag passed
**                  shows if port can accept more data.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  enable     - enables data flow
**
*******************************************************************************/
int PORT_FlowControl (UINT16 handle, BOOLEAN enable)
{
    tPORT      *p_port;
    BOOLEAN    old_fc;
    UINT32     events;

    RFCOMM_TRACE_API2 ("PORT_FlowControl() handle:%d enable: %d", handle, enable);

    /* Check if handle is valid to avoid crashing */
    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;

        /* FC is set if user is set or peer is set */
        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);
    }

    /* Need to take care of the case when we could not deliver events */
    /* to the application because we were flow controlled */
    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);
}


/*******************************************************************************
**
** Function         PORT_GetModemStatus
**
** Description      This function retrieves modem control signals.  Normally
**                  application will call this function after a callback
**                  function is called with notification that one of signals
**                  has been changed.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_signal   - specify the pointer to control signals info
**
*******************************************************************************/
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_API2 ("PORT_GetModemStatus() handle:%d signal:%x", handle, *p_signal);

    return (PORT_SUCCESS);
}


/*******************************************************************************
**
** Function         PORT_ClearError
**
** Description      This function retreives information about a communications
**                  error and reports current status of a connection.  The
**                  function should be called when an error occures to clear
**                  the connection error flag and to enable additional read
**                  and write operations.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_errors   - pointer of the variable to receive error codes
**                  p_status   - pointer to the tPORT_STATUS structur to receive
**                               connection status
**
*******************************************************************************/
int PORT_ClearError (UINT16 handle, UINT16 *p_errors, tPORT_STATUS *p_status)
{
    tPORT  *p_port;

    RFCOMM_TRACE_API1 ("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;

    /* This is the only call to clear error status.  We can not clear */
    /* connection failed status.  To clean it port should be closed and reopened */
    p_port->line_status = (p_port->line_status & LINE_STATUS_FAILED);

    PORT_GetQueueStatus (handle, p_status);
    return (PORT_SUCCESS);
}


/*******************************************************************************
**
** Function         PORT_SendError
**
** Description      This function send a communications error to the peer device
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  errors     - receive error codes
**
*******************************************************************************/
int PORT_SendError (UINT16 handle, UINT8 errors)
{
    tPORT      *p_port;

    RFCOMM_TRACE_API2 ("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);
}


/*******************************************************************************
**
** Function         PORT_GetQueueStatus
**
** Description      This function reports current status of a connection.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_status   - pointer to the tPORT_STATUS structur to receive
**                               connection status
**
*******************************************************************************/
int PORT_GetQueueStatus (UINT16 handle, tPORT_STATUS *p_status)
{
    tPORT      *p_port;

    /* RFCOMM_TRACE_API1 ("PORT_GetQueueStatus() 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_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);
}


/*******************************************************************************
**
** Function         PORT_Purge
**
** Description      This function discards all the data from the output or
**                  input queues of the specified connection.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  purge_flags - specify the action to take.
**
*******************************************************************************/
int PORT_Purge (UINT16 handle, UINT8 purge_flags)
{
    tPORT      *p_port;
    BT_HDR     *p_buf;
    UINT16      count;
    UINT32     events;

    RFCOMM_TRACE_API2 ("PORT_Purge() handle:%d flags:0x%x", handle, purge_flags);

    /* Check if handle is valid to avoid crashing */
    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)
    {
        PORT_SCHEDULE_LOCK;  /* to prevent missing credit */

        count = p_port->rx.queue.count;

        while ((p_buf = (BT_HDR *)GKI_dequeue (&p_port->rx.queue)) != NULL)
            GKI_freebuf (p_buf);

        p_port->rx.queue_size = 0;

        PORT_SCHEDULE_UNLOCK;

        /* If we flowed controlled peer based on rx_queue size enable data again */
        if (count)
            port_flow_control_peer (p_port, TRUE, count);
    }

    if (purge_flags & PORT_PURGE_TXCLEAR)
    {
        PORT_SCHEDULE_LOCK;  /* to prevent tx.queue_size from being negative */

        while ((p_buf = (BT_HDR *)GKI_dequeue (&p_port->tx.queue)) != NULL)
            GKI_freebuf (p_buf);

        p_port->tx.queue_size = 0;

        PORT_SCHEDULE_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);
}


/*******************************************************************************
**
** Function         PORT_ReadData
**
** Description      Normally not GKI aware application will call this function
**                  after receiving PORT_EV_RXCHAR event.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_data      - Data area
**                  max_len     - Byte count requested
**                  p_len       - Byte count received
**
*******************************************************************************/
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_API2 ("PORT_ReadData() handle:%d max_len:%d", handle, max_len);

    /* Initialize this in case of an error */
    *p_len = 0;

    /* Check if handle is valid to avoid crashing */
    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_buf = (BT_HDR *)GKI_getfirst (&p_port->rx.queue);
    if (!p_buf)
        return (PORT_SUCCESS);

    count = 0;

    while (max_len && p_buf)
    {
        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;

            PORT_SCHEDULE_LOCK;

            p_port->rx.queue_size -= max_len;

            PORT_SCHEDULE_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;

            PORT_SCHEDULE_LOCK;

            p_port->rx.queue_size -= p_buf->len;

            if (max_len)
            {
                p_data  += p_buf->len;
                p_buf = (BT_HDR *)GKI_getnext (p_buf);
            }

            GKI_freebuf (GKI_dequeue (&p_port->rx.queue));

            PORT_SCHEDULE_UNLOCK;

            count++;
        }
    }

    if (*p_len == 1)
    {
        RFCOMM_TRACE_EVENT3 ("PORT_ReadData queue:%d returned:%d %x", p_port->rx.queue_size, *p_len, (p_data[0]));
    }
    else
    {
        RFCOMM_TRACE_EVENT2 ("PORT_ReadData queue:%d returned:%d", p_port->rx.queue_size, *p_len);
    }

    /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
    /* check if it can be resumed now */
    port_flow_control_peer (p_port, TRUE, count);

    return (PORT_SUCCESS);
}


/*******************************************************************************
**
** Function         PORT_Read
**
** Description      Normally application will call this function after receiving
**                  PORT_EV_RXCHAR event.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  pp_buf      - pointer to address of buffer with data,
**
*******************************************************************************/
int PORT_Read (UINT16 handle, BT_HDR **pp_buf)
{
    tPORT      *p_port;
    BT_HDR     *p_buf;

    RFCOMM_TRACE_API1 ("PORT_Read() handle:%d", handle);

    /* Check if handle is valid to avoid crashing */
    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);
    }

    PORT_SCHEDULE_LOCK;

    p_buf = (BT_HDR *)GKI_dequeue (&p_port->rx.queue);
    if (p_buf)
    {
        p_port->rx.queue_size -= p_buf->len;

        PORT_SCHEDULE_UNLOCK;

        /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
        /* check if it can be resumed now */
        port_flow_control_peer (p_port, TRUE, 1);
    }
    else
    {
        PORT_SCHEDULE_UNLOCK;
    }

    *pp_buf = p_buf;
    return (PORT_SUCCESS);
}


/*******************************************************************************
**
** Function         port_write
**
** Description      This function when a data packet is received from the apper
**                  layer task.
**
** Parameters:      p_port     - pointer to address of port control block
**                  p_buf      - pointer to address of buffer with data,
**
*******************************************************************************/
static int port_write (tPORT *p_port, BT_HDR *p_buf)
{
    /* We should not allow to write data in to server port when connection is not opened */
    if (p_port->is_server && (p_port->rfc.state != RFC_STATE_OPENED))
    {
        GKI_freebuf (p_buf);
        return (PORT_CLOSED);
    }

    /* Keep the data in pending queue if peer does not allow data, or */
    /* Peer is not ready or Port is not yet opened or initial port control */
    /* command has not been sent */
    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)
         || (p_port->tx.queue.count > PORT_TX_BUF_CRITICAL_WM))
        {
            RFCOMM_TRACE_WARNING1 ("PORT_Write: Queue size: %d",
                                   p_port->tx.queue_size);

            GKI_freebuf (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_EVENT4 ("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);

        GKI_enqueue (&p_port->tx.queue, p_buf);
        p_port->tx.queue_size += p_buf->len;

        return (PORT_CMD_PENDING);
    }
    else
    {
        RFCOMM_TRACE_EVENT0 ("PORT_Write : Data is being sent");

        RFCOMM_DataReq (p_port->rfc.p_mcb, p_port->dlci, p_buf);
        return (PORT_SUCCESS);
    }
}

/*******************************************************************************
**
** Function         PORT_Write
**
** Description      This function when a data packet is received from the apper
**                  layer task.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  pp_buf      - pointer to address of buffer with data,
**
*******************************************************************************/
int PORT_Write (UINT16 handle, BT_HDR *p_buf)
{
    tPORT  *p_port;
    UINT32 event = 0;
    int    rc;

    RFCOMM_TRACE_API1 ("PORT_Write() handle:%d", handle);

    /* Check if handle is valid to avoid crashing */
    if ((handle == 0) || (handle > MAX_RFC_PORTS))
    {
        GKI_freebuf (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))
    {
        GKI_freebuf (p_buf);
        return (PORT_NOT_OPENED);
    }

    if (p_port->line_status)
    {
        RFCOMM_TRACE_WARNING1 ("PORT_Write: Data dropped line_status:0x%x",
                               p_port->line_status);
        GKI_freebuf (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;
    }
    /* Mask out all events that are not of interest to user */
    event &= p_port->ev_mask;

    /* Send event to the application */
    if (p_port->p_callback && event)
        (p_port->p_callback)(event, p_port->inx);

    return (PORT_SUCCESS);
}
/*******************************************************************************
**
** Function         PORT_WriteDataCO
**
** Description      Normally not GKI aware application will call this function
**                  to send data to the port by callout functions
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  fd         - socket fd
**                  p_len      - Byte count returned
**
*******************************************************************************/
int PORT_WriteDataCO (UINT16 handle, int* p_len)
{

    tPORT      *p_port;
    BT_HDR     *p_buf;
    UINT32     event = 0;
    int        rc = 0;
    UINT16     length;

    RFCOMM_TRACE_API1 ("PORT_WriteDataCO() handle:%d", handle);
    int written;
    *p_len = 0;

    /* Check if handle is valid to avoid crashing */
    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_WARNING1 ("PORT_WriteDataByFd() no port state:%d", p_port->state);
        return (PORT_NOT_OPENED);
    }

    if (!p_port->peer_mtu)
    {
        RFCOMM_TRACE_ERROR1 ("PORT_WriteDataByFd() peer_mtu:%d", p_port->peer_mtu);
        return (PORT_UNKNOWN_ERROR);
    }
    int available = 0;
    //if(ioctl(fd, FIONREAD, &available) < 0)
    if(p_port->p_data_co_callback(handle, (UINT8*)&available, sizeof(available),
                                DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE) == FALSE)
    {
        RFCOMM_TRACE_ERROR1("p_data_co_callback DATA_CO_CALLBACK_TYPE_INCOMING_SIZE failed, available:%d", available);
        return (PORT_UNKNOWN_ERROR);
    }
    /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len */
    length = RFCOMM_DATA_POOL_BUF_SIZE -
            (UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);

    /* If there are buffers scheduled for transmission check if requested */
    /* data fits into the end of the queue */
    PORT_SCHEDULE_LOCK;

    if (((p_buf = (BT_HDR *)p_port->tx.queue.p_last) != NULL)
     && (((int)p_buf->len + available) <= (int)p_port->peer_mtu)
     && (((int)p_buf->len + available) <= (int)length))
    {
        //if(recv(fd, (UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, available, 0) != available)
        if(p_port->p_data_co_callback(handle, (UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len,
                                    available, DATA_CO_CALLBACK_TYPE_OUTGOING) == FALSE)

        {
            error("p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, available:%d", available);
            return (PORT_UNKNOWN_ERROR);
        }
        //memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len);
        p_port->tx.queue_size += (UINT16)available;

        *p_len = available;
        p_buf->len += (UINT16)available;

        PORT_SCHEDULE_UNLOCK;

        return (PORT_SUCCESS);
    }

    PORT_SCHEDULE_UNLOCK;

    //int max_read = length < p_port->peer_mtu ? length : p_port->peer_mtu;

    //max_read = available < max_read ? available : max_read;

    while (available)
    {
        /* if we're over buffer high water mark, we're done */
        if ((p_port->tx.queue_size  > PORT_TX_HIGH_WM)
         || (p_port->tx.queue.count > PORT_TX_BUF_HIGH_WM))
            break;

        /* continue with rfcomm data write */
        p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_DATA_POOL_ID);
        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 (available < (int)length)
            length = (UINT16)available;
        p_buf->len = length;
        p_buf->event          = BT_EVT_TO_BTU_SP_DATA;

        //memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, length);
        //if(recv(fd, (UINT8 *)(p_buf + 1) + p_buf->offset, (int)length, 0) != (int)length)
        if(p_port->p_data_co_callback(handle, (UINT8 *)(p_buf + 1) + p_buf->offset, length,
                                      DATA_CO_CALLBACK_TYPE_OUTGOING) == FALSE)
        {
            error("p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, length:%d", length);
            return (PORT_UNKNOWN_ERROR);
        }


        RFCOMM_TRACE_EVENT1 ("PORT_WriteData %d bytes", length);

        rc = port_write (p_port, p_buf);

        /* If queue went below the threashold need to send flow control */
        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;
    }
    if (!available && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED))
        event |= PORT_EV_TXEMPTY;

    /* Mask out all events that are not of interest to user */
    event &= p_port->ev_mask;

    /* Send event to the application */
    if (p_port->p_callback && event)
        (p_port->p_callback)(event, p_port->inx);

    return (PORT_SUCCESS);
}



/*******************************************************************************
**
** Function         PORT_WriteData
**
** Description      Normally not GKI aware application will call this function
**                  to send data to the port.
**
** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
**                  p_data      - Data area
**                  max_len     - Byte count requested
**                  p_len       - Byte count received
**
*******************************************************************************/
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_API1 ("PORT_WriteData() max_len:%d", max_len);

    *p_len = 0;

    /* Check if handle is valid to avoid crashing */
    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_WARNING1 ("PORT_WriteData() no port state:%d", p_port->state);
        return (PORT_NOT_OPENED);
    }

    if (!max_len || !p_port->peer_mtu)
    {
        RFCOMM_TRACE_ERROR1 ("PORT_WriteData() peer_mtu:%d", p_port->peer_mtu);
        return (PORT_UNKNOWN_ERROR);
    }

    /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len */
    length = RFCOMM_DATA_POOL_BUF_SIZE -
            (UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);

    /* If there are buffers scheduled for transmission check if requested */
    /* data fits into the end of the queue */
    PORT_SCHEDULE_LOCK;

    if (((p_buf = (BT_HDR *)p_port->tx.queue.p_last) != 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;

        PORT_SCHEDULE_UNLOCK;

        return (PORT_SUCCESS);
    }

    PORT_SCHEDULE_UNLOCK;

    while (max_len)
    {
        /* if we're over buffer high water mark, we're done */
        if ((p_port->tx.queue_size  > PORT_TX_HIGH_WM)
         || (p_port->tx.queue.count > PORT_TX_BUF_HIGH_WM))
            break;

        /* continue with rfcomm data write */
        p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_DATA_POOL_ID);
        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_EVENT1 ("PORT_WriteData %d bytes", length);

        rc = port_write (p_port, p_buf);

        /* If queue went below the threashold need to send flow control */
        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;

    /* Mask out all events that are not of interest to user */
    event &= p_port->ev_mask;

    /* Send event to the application */
    if (p_port->p_callback && event)
        (p_port->p_callback)(event, p_port->inx);

    return (PORT_SUCCESS);
}


/*******************************************************************************
**
** Function         PORT_Test
**
** Description      Application can call this function to send RFCOMM Test frame
**
** Parameters:      handle      - Handle returned in the RFCOMM_CreateConnection
**                  p_data      - Data area
**                  max_len     - Byte count requested
**
*******************************************************************************/
int PORT_Test (UINT16 handle, UINT8 *p_data, UINT16 len)
{
    BT_HDR   *p_buf;
    tPORT    *p_port;

    RFCOMM_TRACE_API1 ("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 *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) != 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);
    }
}

/*******************************************************************************
**
** Function         RFCOMM_Init
**
** Description      This function is called to initialize RFCOMM layer
**
*******************************************************************************/
void RFCOMM_Init (void)
{
    memset (&rfc_cb, 0, sizeof (tRFC_CB));  /* Init RFCOMM control block */

    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;    /* No traces */
#endif

    rfcomm_l2cap_if_init ();
}

/*******************************************************************************
**
** Function         PORT_SetTraceLevel
**
** Description      This function sets the trace level for RFCOMM. If called with
**                  a value of 0xFF, it simply reads the current trace level.
**
** Returns          the new (current) trace level
**
*******************************************************************************/
UINT8 PORT_SetTraceLevel (UINT8 new_level)
{
    if (new_level != 0xFF)
        rfc_cb.trace_level = new_level;

    return (rfc_cb.trace_level);
}