summaryrefslogtreecommitdiffstats
path: root/stack/rfcomm/port_rfc.c
blob: ef84fef9df0eaf03740615515c037f7d36a0eb27 (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
/*****************************************************************************
**
**  Name:          port_rfc.c
**
**  Description:   This module contains functions for port emulation entity
**                 and RFCOMM communications
**
**
**  Copyright (c) 1999-2009, Broadcom Corp., All Rights Reserved.
**  Broadcom Bluetooth Core. Proprietary and confidential.
******************************************************************************/
#include <string.h>

#include "bt_target.h"
#include "gki.h"
#include "rfcdefs.h"
#include "port_api.h"
#include "btm_int.h"
#include "btm_api.h"
#include "port_int.h"
#include "rfc_int.h"

/* 
** Local function definitions
*/
UINT32 port_rfc_send_tx_data (tPORT *p_port);
void   port_rfc_closed (tPORT *p_port, UINT8 res);
void   port_get_credits (tPORT *p_port, UINT8 k);


/*******************************************************************************
**
** Function         port_open_continue
**
** Description      This function is called after security manager completes
**                  required security checks.
**
** Returns          void
**
*******************************************************************************/
int port_open_continue (tPORT *p_port)
{
    tRFC_MCB *p_mcb;

    RFCOMM_TRACE_EVENT0 ("port_open_continue");

    /* Check if multiplexer channel has already been established */
    if ((p_mcb = rfc_alloc_multiplexer_channel (p_port->bd_addr, TRUE)) == NULL)
    {
        RFCOMM_TRACE_WARNING0 ("port_open_continue no mx channel");
        port_release_port (p_port);
        return (PORT_NO_RESOURCES);
    }

    p_port->rfc.p_mcb = p_mcb;

    p_mcb->port_inx[p_port->dlci] = p_port->inx;

    /* Connection is up and we know local and remote features, select MTU */
    port_select_mtu (p_port);

    if (p_mcb->state == RFC_MX_STATE_CONNECTED)
    {
        RFCOMM_ParNegReq (p_mcb, p_port->dlci, p_port->mtu);
    }
    else if ((p_mcb->state == RFC_MX_STATE_IDLE)
           ||(p_mcb->state == RFC_MX_STATE_DISC_WAIT_UA))
    {
        /* In RFC_MX_STATE_IDLE state, MX state machine will create connection           */
        /* In RFC_MX_STATE_DISC_WAIT_UA state, MX state machine will recreate connection */
        /*    after disconnecting is completed                                           */
        RFCOMM_StartReq (p_mcb);
    }
    else
    {
        /* MX state machine ignores RFC_MX_EVENT_START_REQ in these states */
        /* When it enters RFC_MX_STATE_CONNECTED, it will check any openning ports */
        RFCOMM_TRACE_DEBUG1 ("port_open_continue: mx state(%d) mx channel is openning", p_mcb->state);
    }
    return (PORT_SUCCESS);
}


/*******************************************************************************
**
** Function         port_start_control
**
** Description      This function is called in the BTU_TASK context to 
**                  send control information
**
** Returns          void
**
*******************************************************************************/
void port_start_control (tPORT *p_port)
{
    tRFC_MCB *p_mcb = p_port->rfc.p_mcb;

    if (p_mcb == NULL)
        return;

    RFCOMM_ControlReq (p_mcb, p_port->dlci, &p_port->local_ctrl);
}


/*******************************************************************************
**
** Function         port_start_par_neg
**
** Description      This function is called in the BTU_TASK context to 
**                  send configuration information
**
** Returns          void
**
*******************************************************************************/
void port_start_par_neg (tPORT *p_port)
{
    tRFC_MCB *p_mcb = p_port->rfc.p_mcb;

    if (p_mcb == NULL)
        return;

    RFCOMM_PortNegReq (p_mcb, p_port->dlci, &p_port->user_port_pars);
}


/*******************************************************************************
**
** Function         port_start_close
**
** Description      This function is called in the BTU_TASK context to 
**                  release DLC
**
** Returns          void
**
*******************************************************************************/
void port_start_close (tPORT *p_port)
{
    tRFC_MCB *p_mcb = p_port->rfc.p_mcb;
    UINT8  old_signals;
    UINT32 events = 0;

    /* At first indicate to the user that signals on the connection were dropped */
    p_port->line_status |= LINE_STATUS_FAILED;
    old_signals = p_port->peer_ctrl.modem_signal;

    p_port->peer_ctrl.modem_signal &= ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);

    events |= port_get_signal_changes (p_port, old_signals, p_port->peer_ctrl.modem_signal);

    if(p_port->ev_mask & PORT_EV_CONNECT_ERR)
        events |= PORT_EV_CONNECT_ERR;

    if(p_port->ev_mask & PORT_EV_ERR)
        events |= PORT_EV_ERR;

    if ((p_port->p_callback != NULL) && events)
        p_port->p_callback (events, p_port->inx);


    /* Check if RFCOMM side has been closed while the message was queued */
    if ((p_mcb == NULL) || (p_port->rfc.state == RFC_STATE_CLOSED))
    {
        /* Call management callback function before calling port_release_port() to clear tPort */
        if (p_port->p_mgmt_callback)
            p_port->p_mgmt_callback (PORT_CLOSED, p_port->inx);

        port_release_port (p_port);
    }
    else
    {
        RFCOMM_DlcReleaseReq (p_mcb, p_port->dlci);
    }
}


/*******************************************************************************
**
** Function         PORT_StartCnf
**
** Description      This function is called from the RFCOMM layer when
**                  establishing of the multiplexer channel is completed.
**                  Continue establishing of the connection for all ports that
**                  are in the OPENING state
**
*******************************************************************************/
void PORT_StartCnf (tRFC_MCB *p_mcb, UINT16 result)
{
    tPORT   *p_port;
    int     i;
    BOOLEAN no_ports_up = TRUE;

    RFCOMM_TRACE_EVENT1 ("PORT_StartCnf result:%d", result);

    p_port = &rfc_cb.port.port[0];
    for (i = 0; i < MAX_RFC_PORTS; i++, p_port++)
    {
        if (p_port->rfc.p_mcb == p_mcb)
        {
            no_ports_up = FALSE;

            if (result == RFCOMM_SUCCESS)
                RFCOMM_ParNegReq (p_mcb, p_port->dlci, p_port->mtu);
            else
            {
                RFCOMM_TRACE_WARNING1 ("PORT_StartCnf failed result:%d", result);

                /* Warning: result is also set to 4 when l2cap connection
                   fails due to l2cap connect cnf (no_resources) */
                if( result == HCI_ERR_PAGE_TIMEOUT )
                    p_port->error = PORT_PAGE_TIMEOUT;
                else
                    p_port->error = PORT_START_FAILED;

                rfc_release_multiplexer_channel (p_mcb);
                p_port->rfc.p_mcb = NULL;

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

                if (p_port->p_mgmt_callback)
                    p_port->p_mgmt_callback (PORT_START_FAILED, p_port->inx);

                port_release_port (p_port);
            }
        }
    }

    /* There can be a situation when after starting connection, user closes the */
    /* port, we can catch it here to close multiplexor channel */
    if (no_ports_up)
    {
        rfc_check_mcb_active (p_mcb);
    }
}


/*******************************************************************************
**
** Function         PORT_StartInd
**
** Description      This function is called from the RFCOMM layer when
**                  some peer device wants to establish a multiplexer 
**                  connection.  Check if there are any ports open with this
**                  or not assigned multiplexer.
**
*******************************************************************************/
void PORT_StartInd (tRFC_MCB *p_mcb)
{
    tPORT *p_port;
    int   i;

    RFCOMM_TRACE_EVENT0 ("PORT_StartInd");

    p_port = &rfc_cb.port.port[0];
    for (i = 0; i < MAX_RFC_PORTS; i++, p_port++)
    {
        if ((p_port->rfc.p_mcb == NULL)
         || (p_port->rfc.p_mcb == p_mcb))
        {
            RFCOMM_StartRsp (p_mcb, RFCOMM_SUCCESS);
            return;
        }
    }
    RFCOMM_StartRsp (p_mcb, RFCOMM_ERROR);
}


/*******************************************************************************
**
** Function         PORT_ParNegInd
**
** Description      This function is called from the RFCOMM layer to change
**                  DLCI parameters (currently only MTU is negotiated).
**                  If can not find the port do not accept the request.
**                  Otherwise save the MTU size supported by the peer.
**
*******************************************************************************/
void PORT_ParNegInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT8 cl, UINT8 k)
{
    tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
    UINT8 our_cl;
    UINT8 our_k;

    RFCOMM_TRACE_EVENT2 ("PORT_ParNegInd dlci:%d mtu:%d", dlci, mtu);

    if (!p_port)
    {
        /* This can be a first request for this port */
        p_port = port_find_dlci_port (dlci);
        if (!p_port)
        {
            /* If the port cannot be opened, send a DM.  Per Errata 1205 */
            rfc_send_dm(p_mcb, dlci, FALSE);
            /* check if this is the last port open, some headsets have 
            problem, they don't disconnect if we send DM */
            rfc_check_mcb_active( p_mcb ); 
            RFCOMM_TRACE_EVENT0( "PORT_ParNegInd: port not found" );
            return;
        }
        p_mcb->port_inx[dlci] = p_port->inx;
    }

    memcpy (p_port->bd_addr, p_mcb->bd_addr, BD_ADDR_LEN);

    /* Connection is up and we know local and remote features, select MTU */
    port_select_mtu (p_port);

    p_port->rfc.p_mcb   = p_mcb;
    p_port->mtu         = (p_port->mtu < mtu) ? p_port->mtu : mtu;
    p_port->peer_mtu    = p_port->mtu;

    /* Negotiate the flow control mechanism.  If flow control mechanism for */
    /* mux has not been set yet, set it now.  If either we or peer wants TS 07.10, */
    /* use that.  Otherwise both must want credit based, so use that. If flow is */
    /* already defined for this mux, we respond with that value. */
    if (p_mcb->flow == PORT_FC_UNDEFINED)
    {  
        if ((PORT_FC_DEFAULT == PORT_FC_TS710) || (cl == RFCOMM_PN_CONV_LAYER_TYPE_1))
        {
            p_mcb->flow = PORT_FC_TS710;
        }
        else
        {
            p_mcb->flow = PORT_FC_CREDIT;
        }
    }

    /* Regardless of our flow control mechanism, if the PN cl is zero, we must */
    /* respond with zero.  "A responding implementation must set this field to 14 */
    /* if (and only if) the PN request was 15."  This could happen if a PN is sent */
    /* after the DLCI is already established-- the PN in that case must have cl = 0. */
    /* See RFCOMM spec 5.5.3 */
    if (cl == RFCOMM_PN_CONV_LAYER_TYPE_1)
    {
        our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1;
        our_k = 0;
    }
    else if (p_mcb->flow == PORT_FC_CREDIT)
    {
        /* get credits */
        port_get_credits (p_port, k);

        /* Set convergence layer and number of credits (k) */
        our_cl = RFCOMM_PN_CONV_LAYER_CBFC_R;
        our_k = (p_port->credit_rx_max < RFCOMM_K_MAX) ? p_port->credit_rx_max : RFCOMM_K_MAX;
        p_port->credit_rx = our_k;
    }
    else
    {
        /* must not be using credit based flow control; use TS 7.10 */
        our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1;
        our_k = 0;
    }
    RFCOMM_ParNegRsp (p_mcb, dlci, p_port->mtu, our_cl, our_k);
}


/*******************************************************************************
**
** Function         PORT_ParNegCnf
**
** Description      This function is called from the RFCOMM layer to change
**                  DLCI parameters (currently only MTU is negotiated).
**                  Save the MTU size supported by the peer.
**                  If the confirmation is received during the port opening
**                  procedure send EstablishRequest to continue.
**
*******************************************************************************/
void PORT_ParNegCnf (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT8 cl, UINT8 k)
{
    tPORT   *p_port = port_find_mcb_dlci_port (p_mcb, dlci);

    RFCOMM_TRACE_EVENT4 ("PORT_ParNegCnf dlci:%d mtu:%d cl: %d k: %d", dlci, mtu, cl, k);

    if (!p_port)
        return;

    /* Flow control mechanism not set yet.  Negotiate flow control mechanism. */ 
    if (p_mcb->flow == PORT_FC_UNDEFINED)
    {
        /* Our stack is configured for TS07.10 and they responded with credit-based. */
        /* This is illegal-- negotiation fails. */         
        if ((PORT_FC_DEFAULT == PORT_FC_TS710) && (cl == RFCOMM_PN_CONV_LAYER_CBFC_R))
        {
            rfc_send_disc (p_mcb, p_port->dlci);
            rfc_port_closed (p_port);
            return;
        }
        /* Our stack is configured for credit-based and they responded with credit-based. */
        else if (cl == RFCOMM_PN_CONV_LAYER_CBFC_R)
        {
            p_mcb->flow = PORT_FC_CREDIT;
        }
        /* They responded with any other value.  Treat this as negotiation to TS07.10. */
        else
        {
            p_mcb->flow = PORT_FC_TS710;
        }
    }
    /* If mux flow control mechanism set, we honor that setting regardless of */
    /* the CL value in their response.  This allows us to gracefully accept any */
    /* illegal PN negotiation scenarios. */

    p_port->mtu         = (p_port->mtu < mtu) ? p_port->mtu : mtu;
    p_port->peer_mtu    = p_port->mtu;

    if (p_mcb->flow == PORT_FC_CREDIT)
    {
        port_get_credits (p_port, k);
    }

    if (p_port->state == PORT_STATE_OPENING)
        RFCOMM_DlcEstablishReq (p_mcb, p_port->dlci, p_port->mtu);
}


/*******************************************************************************
**
** Function         PORT_DlcEstablishInd
**
** Description      This function is called from the RFCOMM layer when peer
**                  device wants to establish a new DLC.  If this is not the 
**                  first message in the establishment procedure port_handle 
**                  has a handle to the port control block otherwise the control
**                  block should be found based on the muliplexer channel and 
**                  dlci.  The block should be allocated allocated before
**                  meaning that application already made open.
**
*******************************************************************************/
void PORT_DlcEstablishInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu)
{
    tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);

    RFCOMM_TRACE_EVENT2 ("PORT_DlcEstablishInd dlci:%d mtu:%d", dlci, mtu);

    if (!p_port)
    {
        /* This can be a first request for this port */
        p_port = port_find_dlci_port (dlci);
        if (!p_port)
        {
            RFCOMM_DlcEstablishRsp (p_mcb, dlci, 0, RFCOMM_ERROR);
            return;
        }
        p_mcb->port_inx[dlci] = p_port->inx;
    }

    /* If L2CAP's mtu less then RFCOMM's take it */
    if (mtu && (mtu < p_port->peer_mtu))
        p_port->peer_mtu = mtu;

    /* If there was an inactivity timer running for MCB stop it */
    rfc_timer_stop (p_mcb);

    RFCOMM_DlcEstablishRsp (p_mcb, dlci, p_port->mtu, RFCOMM_SUCCESS);

    /* This is the server side.  If application wants to know when connection */
    /* is established, thats the place */
    if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED))
        (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx);

    if (p_port->p_mgmt_callback)
        p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx);

    p_port->state = PORT_STATE_OPENED;
}


/*******************************************************************************
**
** Function         PORT_DlcEstablishCnf
**
** Description      This function is called from the RFCOMM layer when peer
**                  acknowledges establish procedure (SABME/UA).  Send reply
**                  to the user and set state to OPENED if result was 
**                  successfull.
**
*******************************************************************************/
void PORT_DlcEstablishCnf (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT16 result)
{
    tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);

    RFCOMM_TRACE_EVENT3 ("PORT_DlcEstablishCnf dlci:%d mtu:%d result:%d", dlci, mtu, result);

    if (!p_port)
        return;

    if (result != RFCOMM_SUCCESS)
    {
        p_port->error = PORT_START_FAILED;
        port_rfc_closed (p_port, PORT_START_FAILED);
        return;
    }

    /* If L2CAP's mtu less then RFCOMM's take it */
    if (mtu && (mtu < p_port->peer_mtu))
        p_port->peer_mtu = mtu;

    /* If there was an inactivity timer running for MCB stop it */
    rfc_timer_stop (p_mcb);

    if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED))
        (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx);

    if (p_port->p_mgmt_callback)
        p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx);

    p_port->state = PORT_STATE_OPENED;

    /* RPN is required only if we want to tell DTE how the port should be opened */
    if ((p_port->uuid == UUID_SERVCLASS_DIALUP_NETWORKING) 
     || (p_port->uuid == UUID_SERVCLASS_FAX))
        RFCOMM_PortNegReq (p_port->rfc.p_mcb, p_port->dlci, NULL);
    else
        RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
}


/*******************************************************************************
**
** Function         PORT_PortNegInd
**
** Description      This function is called from the RFCOMM layer when peer
**                  device wants to set parameters of the port.  As per the spec
**                  this message has to be sent before the first data packet 
**                  and can be sent before establish.  The block should be 
**                  allocated before meaning that application already made open.
**
*******************************************************************************/
void PORT_PortNegInd (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars,
                      UINT16 param_mask)
{
    tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);

    RFCOMM_TRACE_EVENT0 ("PORT_PortNegInd");

    if (!p_port)
    {
        /* This can be a first request for this port */
        p_port = port_find_dlci_port (dlci);
        if (!p_port)
        {
            RFCOMM_PortNegRsp (p_mcb, dlci, p_pars, 0);
            return;
        }
        p_mcb->port_inx[dlci] = p_port->inx;
    }

    /* Check if the flow control is acceptable on local side */
    p_port->peer_port_pars = *p_pars;
    RFCOMM_PortNegRsp (p_mcb, dlci, p_pars, param_mask);
}


/*******************************************************************************
**
** Function         PORT_PortNegCnf
**
** Description      This function is called from the RFCOMM layer to change
**                  state for the port.  Propagate change to the user.
**
*******************************************************************************/
void PORT_PortNegCnf (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars, UINT16 result)
{
    tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);

    RFCOMM_TRACE_EVENT0 ("PORT_PortNegCnf");

    if (!p_port)
    {
        RFCOMM_TRACE_WARNING0 ("PORT_PortNegCnf no port");
        return;
    }
    /* Port negotiation failed. Drop the connection */
    if (result != RFCOMM_SUCCESS)
    {
        p_port->error = PORT_PORT_NEG_FAILED;

        RFCOMM_DlcReleaseReq (p_mcb, p_port->dlci);

        port_rfc_closed (p_port, PORT_PORT_NEG_FAILED);
        return;
    }

    if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT))
    {
        RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
    }
    else
    {
        RFCOMM_TRACE_WARNING0 ("PORT_PortNegCnf Control Already sent");
    }
}


/*******************************************************************************
**
** Function         PORT_ControlInd
**
** Description      This function is called from the RFCOMM layer on the modem
**                  signal change.  Propagate change to the user.
**
*******************************************************************************/
void PORT_ControlInd (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars)
{
    tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
    UINT32 event;
    UINT8  old_signals;

    RFCOMM_TRACE_EVENT0 ("PORT_ControlInd");

    if (!p_port)
        return;

    old_signals = p_port->peer_ctrl.modem_signal;

    event = port_get_signal_changes (p_port, old_signals, p_pars->modem_signal);

    p_port->peer_ctrl = *p_pars;

    if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT))
        RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
    else
    {
        /* If this is the first time we received control RFCOMM is connected */
        if (!(p_port->port_ctrl & PORT_CTRL_IND_RECEIVED))
        {
            event |= (PORT_EV_CONNECTED & p_port->ev_mask);
        }

        if (p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED)
        {
            event |= port_rfc_send_tx_data(p_port);
        }
    }

    p_port->port_ctrl |= (PORT_CTRL_IND_RECEIVED | PORT_CTRL_IND_RESPONDED);

    if (p_pars->break_signal)
        event |= (PORT_EV_BREAK & p_port->ev_mask);

    /* execute call back function only if the application is registered for events */
    if (event && p_port->p_callback)
        (p_port->p_callback)(event, p_port->inx);

    RFCOMM_TRACE_EVENT4 ("PORT_ControlInd DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
        ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
        ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
        ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
        ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));

}


/*******************************************************************************
**
** Function         PORT_ControlCnf
**
** Description      This function is called from the RFCOMM layer when
**                  peer acknowleges change of the modem signals.
**
*******************************************************************************/
void PORT_ControlCnf (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars)
{
    tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
    UINT32 event = 0;

    RFCOMM_TRACE_EVENT0 ("PORT_ControlCnf");

    if (!p_port)
        return;

    if (!(p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED))
    {
        p_port->port_ctrl |= PORT_CTRL_REQ_CONFIRMED;

        if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)
            event = (p_port->ev_mask & PORT_EV_CONNECTED);
    }

    if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)
    {
        event |= port_rfc_send_tx_data(p_port);
    }

    /* execute call back function only if the application is registered for events */
    if (event && p_port->p_callback)
        (p_port->p_callback)(event, p_port->inx);
}


/*******************************************************************************
**
** Function         PORT_LineStatusInd
**
** Description      This function is called from the RFCOMM layer when
**                  peer indicates change in the line status
**
*******************************************************************************/
void PORT_LineStatusInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT8 line_status)
{
    tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
    UINT32 event = 0;

    RFCOMM_TRACE_EVENT0 ("PORT_LineStatusInd");

    if (!p_port)
        return;

    p_port->line_status |= line_status;

    if (line_status & PORT_ERR_OVERRUN)
        event |= PORT_EV_OVERRUN;

    if (line_status & PORT_ERR_BREAK)
        event |= PORT_EV_BREAK;

    if (line_status & ~(PORT_ERR_OVERRUN | PORT_ERR_BREAK))
        event |= PORT_EV_ERR;

    if ((p_port->p_callback != NULL) && (p_port->ev_mask & event))
          p_port->p_callback ((p_port->ev_mask & event), p_port->inx);
}


/*******************************************************************************
**
** Function         PORT_DlcReleaseInd
**
** Description      This function is called from the RFCOMM layer when
**                  DLC connection is released.
**
*******************************************************************************/
void PORT_DlcReleaseInd (tRFC_MCB *p_mcb, UINT8 dlci)
{
    tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);

    RFCOMM_TRACE_EVENT0 ("PORT_DlcReleaseInd");

    if (!p_port)
        return;

    port_rfc_closed (p_port, PORT_CLOSED);
}


/*******************************************************************************
**
** Function         PORT_CloseInd
**
** Description      This function is called from the RFCOMM layer when
**                  multiplexer connection is released.
**
*******************************************************************************/
void PORT_CloseInd (tRFC_MCB *p_mcb)
{
    tPORT  *p_port;
    int    i;

    RFCOMM_TRACE_EVENT0 ("PORT_CloseInd");

    p_port = &rfc_cb.port.port[0];
    for (i = 0; i < MAX_RFC_PORTS; i++, p_port++)
    {
        if (p_port->rfc.p_mcb == p_mcb) 
        {
            port_rfc_closed (p_port, PORT_PEER_CONNECTION_FAILED);
        }
    }
    rfc_release_multiplexer_channel (p_mcb);
}

/*******************************************************************************
**
** Function         Port_TimeOutCloseMux
**
** Description      This function is called when RFCOMM timesout on a command 
**                  as a result multiplexer connection is closed.
**
*******************************************************************************/
void Port_TimeOutCloseMux (tRFC_MCB *p_mcb)
{
    tPORT  *p_port;
    int    i;

    RFCOMM_TRACE_EVENT0 ("Port_TimeOutCloseMux");

    p_port = &rfc_cb.port.port[0];
    for (i = 0; i < MAX_RFC_PORTS; i++, p_port++)
    {
        if (p_port->rfc.p_mcb == p_mcb) 
        {
            port_rfc_closed (p_port, PORT_PEER_TIMEOUT);
        }
    }
}


/*******************************************************************************
**
** Function         PORT_DataInd
**
** Description      This function is called from the RFCOMM layer when data
**                  buffer is received from the peer.  
**
*******************************************************************************/
void PORT_DataInd (tRFC_MCB *p_mcb, UINT8 dlci, BT_HDR *p_buf)
{
    tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
    UINT8  rx_char1;
    UINT32 events = 0;
    UINT8  *p;
    int    i;

    RFCOMM_TRACE_EVENT1 ("PORT_DataInd with data length %d", p_buf->len);
    if (!p_port)
    {
        GKI_freebuf (p_buf);
        return;
    }
    /* If client registered callout callback with flow control we can just deliver receive data */
    if (p_port->p_data_co_callback)
    {
        /* Another packet is delivered to user.  Send credits to peer if required */

        if(p_port->p_data_co_callback(p_port->inx, (UINT8*)p_buf, -1, DATA_CO_CALLBACK_TYPE_INCOMING))
            port_flow_control_peer(p_port, TRUE, 1);
        else port_flow_control_peer(p_port, FALSE, 0);
        //GKI_freebuf (p_buf);
        return;
    }

    /* If client registered callback we can just deliver receive data */
    if (p_port->p_data_callback)
    {
        /* Another packet is delivered to user.  Send credits to peer if required */
        port_flow_control_peer(p_port, TRUE, 1);

        p_port->p_data_callback (p_port->inx, (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
        GKI_freebuf (p_buf);
        return;
    }

    /* Check if rx queue exceeds the limit */
    if ((p_port->rx.queue_size + p_buf->len > PORT_RX_CRITICAL_WM)
     || (p_port->rx.queue.count + 1 > p_port->rx_buf_critical))
    {
        RFCOMM_TRACE_EVENT0 ("PORT_DataInd. Buffer over run. Dropping the buffer");
        GKI_freebuf (p_buf);

        RFCOMM_LineStatusReq (p_mcb, dlci, LINE_STATUS_OVERRUN);
        return;
    }

    /* If user registered to receive notification when a particular byte is */
    /* received we mast check all received bytes */
    if (((rx_char1 = p_port->user_port_pars.rx_char1) != 0)
     && (p_port->ev_mask & PORT_EV_RXFLAG))
    {
        for (i = 0, p = (UINT8 *)(p_buf + 1) + p_buf->offset; i < p_buf->len; i++)
        {
            if (*p++ == rx_char1)
            {
                events |= PORT_EV_RXFLAG;
                break;
            }
        }
    }

    PORT_SCHEDULE_LOCK;

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

    PORT_SCHEDULE_UNLOCK;

    /* perform flow control procedures if necessary */
    port_flow_control_peer(p_port, FALSE, 0);

    /* If user indicated flow control can not deliver any notifications to him */
    if (p_port->rx.user_fc)
    {
        if (events & PORT_EV_RXFLAG)
            p_port->rx_flag_ev_pending = TRUE;

        return;
    }

    events |= PORT_EV_RXCHAR;

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

    if (p_port->p_callback && events)
        p_port->p_callback (events, p_port->inx);
}


/*******************************************************************************
**
** Function         PORT_FlowInd
**
** Description      This function is called from the RFCOMM layer on the flow 
**                  control signal change.  Propagate change to the user.
**
*******************************************************************************/
void PORT_FlowInd (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN enable_data)
{
    tPORT  *p_port = (tPORT *)NULL;
    UINT32 events = 0;
    int    i;

    RFCOMM_TRACE_EVENT1 ("PORT_FlowInd fc:%d", enable_data);

    if (dlci == 0)
    {
        p_mcb->peer_ready = enable_data;
    }
    else
    {
        if ((p_port = port_find_mcb_dlci_port (p_mcb, dlci)) == NULL)
            return;

        p_port->tx.peer_fc = !enable_data;
    }

    for (i = 0; i < MAX_RFC_PORTS; i++)
    {
        /* If DLCI is 0 event applies to all ports */
        if (dlci == 0)
        {
            p_port = &rfc_cb.port.port[i];
            if (!p_port->in_use 
             || (p_port->rfc.p_mcb != p_mcb)
             || (p_port->rfc.state != RFC_STATE_OPENED))
                continue;
        }
        events = 0;
        
        /* Check if flow of data is still enabled */
        events |= port_flow_control_user (p_port);

        /* Check if data can be sent and send it */
        events |= port_rfc_send_tx_data (p_port);

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

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

        /* If DLCI is not 0 event applies to one port only */
        if (dlci != 0)
            break;
    }
}


/*******************************************************************************
**
** Function         port_rfc_send_tx_data
**
** Description      This function is when forward data can be sent to the peer
**
*******************************************************************************/
UINT32 port_rfc_send_tx_data (tPORT *p_port)
{
    UINT32 events = 0;
    BT_HDR *p_buf;

    /* if there is data to be sent */
    if (p_port->tx.queue_size > 0)
    {
        /* while the rfcomm peer is not flow controlling us, and peer is ready */
        while (!p_port->tx.peer_fc && p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready)
        {
            /* get data from tx queue and send it */
            PORT_SCHEDULE_LOCK;

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

                PORT_SCHEDULE_UNLOCK;

                RFCOMM_TRACE_DEBUG1 ("Sending RFCOMM_DataReq tx.queue_size=%d", p_port->tx.queue_size);

                RFCOMM_DataReq (p_port->rfc.p_mcb, p_port->dlci, p_buf);
            
                events |= PORT_EV_TXCHAR;

                if (p_port->tx.queue_size == 0)
                {
                    events |= PORT_EV_TXEMPTY;
                    break;
                }  
            }
            /* queue is empty-- all data sent */
            else
            {
                PORT_SCHEDULE_UNLOCK;

                events |= PORT_EV_TXEMPTY;
                break;
            }
        }
        /* If we flow controlled user based on the queue size enable data again */
        events |= port_flow_control_user (p_port);
    }
    return (events & p_port->ev_mask);
}


/*******************************************************************************
**
** Function         port_rfc_closed
**
** Description      This function when RFCOMM side of port is closed
**
*******************************************************************************/
void port_rfc_closed (tPORT *p_port, UINT8 res)
{
    UINT8     old_signals;
    UINT32    events = 0;
    tRFC_MCB *p_mcb = p_port->rfc.p_mcb;

    if ((p_port->state == PORT_STATE_OPENING) && (p_port->is_server))
    {
        /* The servr side has not been informed that connection is up, ignore */
        RFCOMM_TRACE_EVENT0 ("port_rfc_closed in OPENING state ignored");

        rfc_port_timer_stop (p_port);
        p_port->rfc.state = RFC_STATE_CLOSED;

        if (p_mcb)
        {
            p_mcb->port_inx[p_port->dlci] = 0;

            /* If there are no more ports opened on this MCB release it */
            rfc_check_mcb_active (p_mcb);
            p_port->rfc.p_mcb = NULL;
        }

        /* Need to restore DLCI to listening state
         * if the server was on the initiating RFC
         */
        p_port->dlci &= 0xfe;

        return;
    }

    if ((p_port->state != PORT_STATE_CLOSING) && (p_port->state != PORT_STATE_CLOSED))
    {
        p_port->line_status |= LINE_STATUS_FAILED;

        old_signals = p_port->peer_ctrl.modem_signal;

        p_port->peer_ctrl.modem_signal &= ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);

        events |= port_get_signal_changes (p_port, old_signals, p_port->peer_ctrl.modem_signal);

        if(p_port->ev_mask & PORT_EV_CONNECT_ERR)
            events |= PORT_EV_CONNECT_ERR;
    }
    RFCOMM_TRACE_EVENT2 ("port_rfc_closed state:%d sending events:%x", p_port->state, events);

    if ((p_port->p_callback != NULL) && events)
        p_port->p_callback (events, p_port->inx);

    if (p_port->p_mgmt_callback)
        p_port->p_mgmt_callback (res, p_port->inx);

    p_port->rfc.state = RFC_STATE_CLOSED;

    port_release_port (p_port);
}


/*******************************************************************************
**
** Function         port_get_credits
**
** Description      Set initial values for credits.
**                  Adjust max number of rx credits based on negotiated MTU.  
**                  Check max allowed num of bytes, max allowed num buffers, 
**                  should be less then 255 
**
*******************************************************************************/
void port_get_credits (tPORT *p_port, UINT8 k)
{
    p_port->credit_tx = k;
    if (p_port->credit_tx == 0)
        p_port->tx.peer_fc = TRUE;
}